b60392b9cb8b4edce1354bd93c85d7b5072f016a
[linux-flexiantxendom0-3.2.10.git] / arch / x86_64 / kernel / io_apic.c
1 /*
2  *      Intel IO-APIC support for multi-Pentium hosts.
3  *
4  *      Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5  *
6  *      Many thanks to Stig Venaas for trying out countless experimental
7  *      patches and reporting/debugging problems patiently!
8  *
9  *      (c) 1999, Multiple IO-APIC support, developed by
10  *      Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11  *      Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12  *      further tested and cleaned up by Zach Brown <zab@redhat.com>
13  *      and Ingo Molnar <mingo@redhat.com>
14  *
15  *      Fixes
16  *      Maciej W. Rozycki       :       Bits for genuine 82489DX APICs;
17  *                                      thanks to Eric Gilmore
18  *                                      and Rolf G. Tews
19  *                                      for testing these extensively
20  *      Paul Diefenbaugh        :       Added full ACPI support
21  */
22
23 #include <linux/mm.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/sched.h>
29 #include <linux/config.h>
30 #include <linux/smp_lock.h>
31 #include <linux/mc146818rtc.h>
32 #include <linux/acpi.h>
33
34 #include <asm/io.h>
35 #include <asm/smp.h>
36 #include <asm/desc.h>
37
38 int sis_apic_bug; /* not actually supported, dummy for compile */
39
40 #undef APIC_LOCKUP_DEBUG
41
42 #define APIC_LOCKUP_DEBUG
43
44 static spinlock_t ioapic_lock = SPIN_LOCK_UNLOCKED;
45
46 /*
47  * # of IRQ routing registers
48  */
49 int nr_ioapic_registers[MAX_IO_APICS];
50
51 /*
52  * Rough estimation of how many shared IRQs there are, can
53  * be changed anytime.
54  */
55 #define MAX_PLUS_SHARED_IRQS NR_IRQS
56 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
57
58 /*
59  * This is performance-critical, we want to do it O(1)
60  *
61  * the indexing order of this array favors 1:1 mappings
62  * between pins and IRQs.
63  */
64
65 static struct irq_pin_list {
66         short apic, pin, next;
67 } irq_2_pin[PIN_MAP_SIZE];
68
69 /*
70  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
71  * shared ISA-space IRQs, so we have to support them. We are super
72  * fast in the common case, and fast for shared ISA-space IRQs.
73  */
74 static void __init add_pin_to_irq(unsigned int irq, int apic, int pin)
75 {
76         static int first_free_entry = NR_IRQS;
77         struct irq_pin_list *entry = irq_2_pin + irq;
78
79         while (entry->next)
80                 entry = irq_2_pin + entry->next;
81
82         if (entry->pin != -1) {
83                 entry->next = first_free_entry;
84                 entry = irq_2_pin + entry->next;
85                 if (++first_free_entry >= PIN_MAP_SIZE)
86                         panic("io_apic.c: whoops");
87         }
88         entry->apic = apic;
89         entry->pin = pin;
90 }
91
92 #define __DO_ACTION(R, ACTION, FINAL)                                   \
93                                                                         \
94 {                                                                       \
95         int pin;                                                        \
96         struct irq_pin_list *entry = irq_2_pin + irq;                   \
97                                                                         \
98         for (;;) {                                                      \
99                 unsigned int reg;                                       \
100                 pin = entry->pin;                                       \
101                 if (pin == -1)                                          \
102                         break;                                          \
103                 reg = io_apic_read(entry->apic, 0x10 + R + pin*2);      \
104                 reg ACTION;                                             \
105                 io_apic_modify(entry->apic, reg);                       \
106                 if (!entry->next)                                       \
107                         break;                                          \
108                 entry = irq_2_pin + entry->next;                        \
109         }                                                               \
110         FINAL;                                                          \
111 }
112
113 #define DO_ACTION(name,R,ACTION, FINAL)                                 \
114                                                                         \
115         static void name##_IO_APIC_irq (unsigned int irq)               \
116         __DO_ACTION(R, ACTION, FINAL)
117
118 DO_ACTION( __mask,             0, |= 0x00010000, io_apic_sync(entry->apic) )
119                                                 /* mask = 1 */
120 DO_ACTION( __unmask,           0, &= 0xfffeffff, )
121                                                 /* mask = 0 */
122 DO_ACTION( __mask_and_edge,    0, = (reg & 0xffff7fff) | 0x00010000, )
123                                                 /* mask = 1, trigger = 0 */
124 DO_ACTION( __unmask_and_level, 0, = (reg & 0xfffeffff) | 0x00008000, )
125                                                 /* mask = 0, trigger = 1 */
126
127 static void mask_IO_APIC_irq (unsigned int irq)
128 {
129         unsigned long flags;
130
131         spin_lock_irqsave(&ioapic_lock, flags);
132         __mask_IO_APIC_irq(irq);
133         spin_unlock_irqrestore(&ioapic_lock, flags);
134 }
135
136 static void unmask_IO_APIC_irq (unsigned int irq)
137 {
138         unsigned long flags;
139
140         spin_lock_irqsave(&ioapic_lock, flags);
141         __unmask_IO_APIC_irq(irq);
142         spin_unlock_irqrestore(&ioapic_lock, flags);
143 }
144
145 void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
146 {
147         struct IO_APIC_route_entry entry;
148         unsigned long flags;
149
150         /*
151          * Disable it in the IO-APIC irq-routing table:
152          */
153         memset(&entry, 0, sizeof(entry));
154         entry.mask = 1;
155         spin_lock_irqsave(&ioapic_lock, flags);
156         io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
157         io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
158         spin_unlock_irqrestore(&ioapic_lock, flags);
159 }
160
161 static void clear_IO_APIC (void)
162 {
163         int apic, pin;
164
165         for (apic = 0; apic < nr_ioapics; apic++)
166                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
167                         clear_IO_APIC_pin(apic, pin);
168 }
169
170 /*
171  * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
172  * specific CPU-side IRQs.
173  */
174
175 #define MAX_PIRQS 8
176 int pirq_entries [MAX_PIRQS];
177 int pirqs_enabled;
178 int skip_ioapic_setup;
179
180 static int __init ioapic_setup(char *str)
181 {
182         skip_ioapic_setup = 1;
183         return 1;
184 }
185
186 __setup("noapic", ioapic_setup);
187
188 static int __init ioapic_pirq_setup(char *str)
189 {
190         int i, max;
191         int ints[MAX_PIRQS+1];
192
193         get_options(str, ARRAY_SIZE(ints), ints);
194
195         for (i = 0; i < MAX_PIRQS; i++)
196                 pirq_entries[i] = -1;
197
198         pirqs_enabled = 1;
199         printk(KERN_INFO "PIRQ redirection, working around broken MP-BIOS.\n");
200         max = MAX_PIRQS;
201         if (ints[0] < MAX_PIRQS)
202                 max = ints[0];
203
204         for (i = 0; i < max; i++) {
205                 printk(KERN_DEBUG "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
206                 /*
207                  * PIRQs are mapped upside down, usually.
208                  */
209                 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
210         }
211         return 1;
212 }
213
214 __setup("pirq=", ioapic_pirq_setup);
215
216 /*
217  * Find the IRQ entry number of a certain pin.
218  */
219 static int __init find_irq_entry(int apic, int pin, int type)
220 {
221         int i;
222
223         for (i = 0; i < mp_irq_entries; i++)
224                 if (mp_irqs[i].mpc_irqtype == type &&
225                     (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
226                      mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
227                     mp_irqs[i].mpc_dstirq == pin)
228                         return i;
229
230         return -1;
231 }
232
233 /*
234  * Find the pin to which IRQ[irq] (ISA) is connected
235  */
236 static int __init find_isa_irq_pin(int irq, int type)
237 {
238         int i;
239
240         for (i = 0; i < mp_irq_entries; i++) {
241                 int lbus = mp_irqs[i].mpc_srcbus;
242
243                 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
244                      mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
245                      mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
246                     (mp_irqs[i].mpc_irqtype == type) &&
247                     (mp_irqs[i].mpc_srcbusirq == irq))
248
249                         return mp_irqs[i].mpc_dstirq;
250         }
251         return -1;
252 }
253
254 /*
255  * Find a specific PCI IRQ entry.
256  * Not an __init, possibly needed by modules
257  */
258 static int pin_2_irq(int idx, int apic, int pin);
259
260 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
261 {
262         int apic, i, best_guess = -1;
263
264         Dprintk("querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
265                 bus, slot, pin);
266         if (mp_bus_id_to_pci_bus[bus] == -1) {
267                 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
268                 return -1;
269         }
270         for (i = 0; i < mp_irq_entries; i++) {
271                 int lbus = mp_irqs[i].mpc_srcbus;
272
273                 for (apic = 0; apic < nr_ioapics; apic++)
274                         if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
275                             mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
276                                 break;
277
278                 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
279                     !mp_irqs[i].mpc_irqtype &&
280                     (bus == lbus) &&
281                     (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
282                         int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
283
284                         if (!(apic || IO_APIC_IRQ(irq)))
285                                 continue;
286
287                         if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
288                                 return irq;
289                         /*
290                          * Use the first all-but-pin matching entry as a
291                          * best-guess fuzzy result for broken mptables.
292                          */
293                         if (best_guess < 0)
294                                 best_guess = irq;
295                 }
296         }
297         return best_guess;
298 }
299
300 /*
301  * EISA Edge/Level control register, ELCR
302  */
303 static int __init EISA_ELCR(unsigned int irq)
304 {
305         if (irq < 16) {
306                 unsigned int port = 0x4d0 + (irq >> 3);
307                 return (inb(port) >> (irq & 7)) & 1;
308         }
309         printk(KERN_INFO "Broken MPtable reports ISA irq %d\n", irq);
310         return 0;
311 }
312
313 /* EISA interrupts are always polarity zero and can be edge or level
314  * trigger depending on the ELCR value.  If an interrupt is listed as
315  * EISA conforming in the MP table, that means its trigger type must
316  * be read in from the ELCR */
317
318 #define default_EISA_trigger(idx)       (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
319 #define default_EISA_polarity(idx)      (0)
320
321 /* ISA interrupts are always polarity zero edge triggered,
322  * when listed as conforming in the MP table. */
323
324 #define default_ISA_trigger(idx)        (0)
325 #define default_ISA_polarity(idx)       (0)
326
327 /* PCI interrupts are always polarity one level triggered,
328  * when listed as conforming in the MP table. */
329
330 #define default_PCI_trigger(idx)        (1)
331 #define default_PCI_polarity(idx)       (1)
332
333 /* MCA interrupts are always polarity zero level triggered,
334  * when listed as conforming in the MP table. */
335
336 #define default_MCA_trigger(idx)        (1)
337 #define default_MCA_polarity(idx)       (0)
338
339 static int __init MPBIOS_polarity(int idx)
340 {
341         int bus = mp_irqs[idx].mpc_srcbus;
342         int polarity;
343
344         /*
345          * Determine IRQ line polarity (high active or low active):
346          */
347         switch (mp_irqs[idx].mpc_irqflag & 3)
348         {
349                 case 0: /* conforms, ie. bus-type dependent polarity */
350                 {
351                         switch (mp_bus_id_to_type[bus])
352                         {
353                                 case MP_BUS_ISA: /* ISA pin */
354                                 {
355                                         polarity = default_ISA_polarity(idx);
356                                         break;
357                                 }
358                                 case MP_BUS_EISA: /* EISA pin */
359                                 {
360                                         polarity = default_EISA_polarity(idx);
361                                         break;
362                                 }
363                                 case MP_BUS_PCI: /* PCI pin */
364                                 {
365                                         polarity = default_PCI_polarity(idx);
366                                         break;
367                                 }
368                                 case MP_BUS_MCA: /* MCA pin */
369                                 {
370                                         polarity = default_MCA_polarity(idx);
371                                         break;
372                                 }
373                                 default:
374                                 {
375                                         printk(KERN_WARNING "broken BIOS!!\n");
376                                         polarity = 1;
377                                         break;
378                                 }
379                         }
380                         break;
381                 }
382                 case 1: /* high active */
383                 {
384                         polarity = 0;
385                         break;
386                 }
387                 case 2: /* reserved */
388                 {
389                         printk(KERN_WARNING "broken BIOS!!\n");
390                         polarity = 1;
391                         break;
392                 }
393                 case 3: /* low active */
394                 {
395                         polarity = 1;
396                         break;
397                 }
398                 default: /* invalid */
399                 {
400                         printk(KERN_WARNING "broken BIOS!!\n");
401                         polarity = 1;
402                         break;
403                 }
404         }
405         return polarity;
406 }
407
408 static int __init MPBIOS_trigger(int idx)
409 {
410         int bus = mp_irqs[idx].mpc_srcbus;
411         int trigger;
412
413         /*
414          * Determine IRQ trigger mode (edge or level sensitive):
415          */
416         switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
417         {
418                 case 0: /* conforms, ie. bus-type dependent */
419                 {
420                         switch (mp_bus_id_to_type[bus])
421                         {
422                                 case MP_BUS_ISA: /* ISA pin */
423                                 {
424                                         trigger = default_ISA_trigger(idx);
425                                         break;
426                                 }
427                                 case MP_BUS_EISA: /* EISA pin */
428                                 {
429                                         trigger = default_EISA_trigger(idx);
430                                         break;
431                                 }
432                                 case MP_BUS_PCI: /* PCI pin */
433                                 {
434                                         trigger = default_PCI_trigger(idx);
435                                         break;
436                                 }
437                                 case MP_BUS_MCA: /* MCA pin */
438                                 {
439                                         trigger = default_MCA_trigger(idx);
440                                         break;
441                                 }
442                                 default:
443                                 {
444                                         printk(KERN_WARNING "broken BIOS!!\n");
445                                         trigger = 1;
446                                         break;
447                                 }
448                         }
449                         break;
450                 }
451                 case 1: /* edge */
452                 {
453                         trigger = 0;
454                         break;
455                 }
456                 case 2: /* reserved */
457                 {
458                         printk(KERN_WARNING "broken BIOS!!\n");
459                         trigger = 1;
460                         break;
461                 }
462                 case 3: /* level */
463                 {
464                         trigger = 1;
465                         break;
466                 }
467                 default: /* invalid */
468                 {
469                         printk(KERN_WARNING "broken BIOS!!\n");
470                         trigger = 0;
471                         break;
472                 }
473         }
474         return trigger;
475 }
476
477 static inline int irq_polarity(int idx)
478 {
479         return MPBIOS_polarity(idx);
480 }
481
482 static inline int irq_trigger(int idx)
483 {
484         return MPBIOS_trigger(idx);
485 }
486
487 static int pin_2_irq(int idx, int apic, int pin)
488 {
489         int irq, i;
490         int bus = mp_irqs[idx].mpc_srcbus;
491
492         /*
493          * Debugging check, we are in big trouble if this message pops up!
494          */
495         if (mp_irqs[idx].mpc_dstirq != pin)
496                 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
497
498         switch (mp_bus_id_to_type[bus])
499         {
500                 case MP_BUS_ISA: /* ISA pin */
501                 case MP_BUS_EISA:
502                 case MP_BUS_MCA:
503                 {
504                         irq = mp_irqs[idx].mpc_srcbusirq;
505                         break;
506                 }
507                 case MP_BUS_PCI: /* PCI pin */
508                 {
509                         /*
510                          * PCI IRQs are mapped in order
511                          */
512                         i = irq = 0;
513                         while (i < apic)
514                                 irq += nr_ioapic_registers[i++];
515                         irq += pin;
516                         break;
517                 }
518                 default:
519                 {
520                         printk(KERN_ERR "unknown bus type %d.\n",bus); 
521                         irq = 0;
522                         break;
523                 }
524         }
525
526         /*
527          * PCI IRQ command line redirection. Yes, limits are hardcoded.
528          */
529         if ((pin >= 16) && (pin <= 23)) {
530                 if (pirq_entries[pin-16] != -1) {
531                         if (!pirq_entries[pin-16]) {
532                                 printk(KERN_DEBUG "disabling PIRQ%d\n", pin-16);
533                         } else {
534                                 irq = pirq_entries[pin-16];
535                                 printk(KERN_DEBUG "using PIRQ%d -> IRQ %d\n",
536                                                 pin-16, irq);
537                         }
538                 }
539         }
540         return irq;
541 }
542
543 static inline int IO_APIC_irq_trigger(int irq)
544 {
545         int apic, idx, pin;
546
547         for (apic = 0; apic < nr_ioapics; apic++) {
548                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
549                         idx = find_irq_entry(apic,pin,mp_INT);
550                         if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
551                                 return irq_trigger(idx);
552                 }
553         }
554         /*
555          * nonexistent IRQs are edge default
556          */
557         return 0;
558 }
559
560 int irq_vector[NR_IRQS] = { FIRST_DEVICE_VECTOR , 0 };
561
562 static int __init assign_irq_vector(int irq)
563 {
564         static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
565         if (IO_APIC_VECTOR(irq) > 0)
566                 return IO_APIC_VECTOR(irq);
567 next:
568         current_vector += 8;
569         if (current_vector == IA32_SYSCALL_VECTOR)
570                 goto next;
571
572         if (current_vector > FIRST_SYSTEM_VECTOR) {
573                 offset++;
574                 current_vector = FIRST_DEVICE_VECTOR + offset;
575         }
576
577         if (current_vector == FIRST_SYSTEM_VECTOR)
578                 panic("ran out of interrupt sources!");
579
580         IO_APIC_VECTOR(irq) = current_vector;
581         return current_vector;
582 }
583
584 extern void (*interrupt[NR_IRQS])(void);
585 static struct hw_interrupt_type ioapic_level_irq_type;
586 static struct hw_interrupt_type ioapic_edge_irq_type;
587
588 void __init setup_IO_APIC_irqs(void)
589 {
590         struct IO_APIC_route_entry entry;
591         int apic, pin, idx, irq, first_notcon = 1, vector;
592         unsigned long flags;
593
594         printk(KERN_DEBUG "init IO_APIC IRQs\n");
595
596         for (apic = 0; apic < nr_ioapics; apic++) {
597         for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
598
599                 /*
600                  * add it to the IO-APIC irq-routing table:
601                  */
602                 memset(&entry,0,sizeof(entry));
603
604                 entry.delivery_mode = dest_LowestPrio;
605                 entry.dest_mode = INT_DELIVERY_MODE;
606                 entry.mask = 0;                         /* enable IRQ */
607                 entry.dest.logical.logical_dest = TARGET_CPUS;
608
609                 idx = find_irq_entry(apic,pin,mp_INT);
610                 if (idx == -1) {
611                         if (first_notcon) {
612                                 printk(KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
613                                 first_notcon = 0;
614                         } else
615                                 printk(", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
616                         continue;
617                 }
618
619                 entry.trigger = irq_trigger(idx);
620                 entry.polarity = irq_polarity(idx);
621
622                 if (irq_trigger(idx)) {
623                         entry.trigger = 1;
624                         entry.mask = 1;
625                         entry.dest.logical.logical_dest = TARGET_CPUS;
626                 }
627
628                 irq = pin_2_irq(idx, apic, pin);
629                 add_pin_to_irq(irq, apic, pin);
630
631                 if (!apic && !IO_APIC_IRQ(irq))
632                         continue;
633
634                 if (IO_APIC_IRQ(irq)) {
635                         vector = assign_irq_vector(irq);
636                         entry.vector = vector;
637
638                         if (IO_APIC_irq_trigger(irq))
639                                 irq_desc[irq].handler = &ioapic_level_irq_type;
640                         else
641                                 irq_desc[irq].handler = &ioapic_edge_irq_type;
642
643                         set_intr_gate(vector, interrupt[irq]);
644                 
645                         if (!apic && (irq < 16))
646                                 disable_8259A_irq(irq);
647                 }
648                 spin_lock_irqsave(&ioapic_lock, flags);
649                 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
650                 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
651                 spin_unlock_irqrestore(&ioapic_lock, flags);
652         }
653         }
654
655         if (!first_notcon)
656                 printk(" not connected.\n");
657 }
658
659 /*
660  * Set up the 8259A-master output pin as broadcast to all
661  * CPUs.
662  */
663 void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
664 {
665         struct IO_APIC_route_entry entry;
666         unsigned long flags;
667
668         memset(&entry,0,sizeof(entry));
669
670         disable_8259A_irq(0);
671
672         /* mask LVT0 */
673         apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
674
675         /*
676          * We use logical delivery to get the timer IRQ
677          * to the first CPU.
678          */
679         entry.dest_mode = INT_DELIVERY_MODE;
680         entry.mask = 0;                                 /* unmask IRQ now */
681         entry.dest.logical.logical_dest = TARGET_CPUS;
682         entry.delivery_mode = dest_LowestPrio;
683         entry.polarity = 0;
684         entry.trigger = 0;
685         entry.vector = vector;
686
687         /*
688          * The timer IRQ doesn't have to know that behind the
689          * scene we have a 8259A-master in AEOI mode ...
690          */
691         irq_desc[0].handler = &ioapic_edge_irq_type;
692
693         /*
694          * Add it to the IO-APIC irq-routing table:
695          */
696         spin_lock_irqsave(&ioapic_lock, flags);
697         io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
698         io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
699         spin_unlock_irqrestore(&ioapic_lock, flags);
700
701         enable_8259A_irq(0);
702 }
703
704 void __init UNEXPECTED_IO_APIC(void)
705 {
706 #if 0
707         printk(KERN_WARNING " WARNING: unexpected IO-APIC, please mail\n");
708         printk(KERN_WARNING "          to linux-smp@vger.kernel.org\n");
709 #endif
710 }
711
712 void __init print_IO_APIC(void)
713 {
714         int apic, i;
715         union IO_APIC_reg_00 reg_00;
716         union IO_APIC_reg_01 reg_01;
717         union IO_APIC_reg_02 reg_02;
718         unsigned long flags;
719
720         printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
721         for (i = 0; i < nr_ioapics; i++)
722                 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
723                        mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
724
725         /*
726          * We are a bit conservative about what we expect.  We have to
727          * know about every hardware change ASAP.
728          */
729         printk(KERN_INFO "testing the IO APIC.......................\n");
730
731         for (apic = 0; apic < nr_ioapics; apic++) {
732
733         spin_lock_irqsave(&ioapic_lock, flags);
734         reg_00.raw = io_apic_read(apic, 0);
735         reg_01.raw = io_apic_read(apic, 1);
736         if (reg_01.bits.version >= 0x10)
737                 reg_02.raw = io_apic_read(apic, 2);
738         spin_unlock_irqrestore(&ioapic_lock, flags);
739
740         printk("\n");
741         printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
742         printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
743         printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
744         if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
745                 UNEXPECTED_IO_APIC();
746
747         printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
748         printk(KERN_DEBUG ".......     : max redirection entries: %04X\n", reg_01.bits.entries);
749         if (    (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
750                 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
751                 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
752                 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
753                 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
754                 (reg_01.bits.entries != 0x2E) &&
755                 (reg_01.bits.entries != 0x3F) &&
756                 (reg_01.bits.entries != 0x03) 
757         )
758                 UNEXPECTED_IO_APIC();
759
760         printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
761         printk(KERN_DEBUG ".......     : IO APIC version: %04X\n", reg_01.bits.version);
762         if (    (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
763                 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
764                 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
765                 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
766                 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
767                 (reg_01.bits.version != 0x20)    /* Intel P64H (82806 AA) */
768         )
769                 UNEXPECTED_IO_APIC();
770         if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
771                 UNEXPECTED_IO_APIC();
772
773         if (reg_01.bits.version >= 0x10) {
774                 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
775                 printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
776                 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
777                         UNEXPECTED_IO_APIC();
778         }
779
780         printk(KERN_DEBUG ".... IRQ redirection table:\n");
781
782         printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
783                           " Stat Dest Deli Vect:   \n");
784
785         for (i = 0; i <= reg_01.bits.entries; i++) {
786                 struct IO_APIC_route_entry entry;
787
788                 spin_lock_irqsave(&ioapic_lock, flags);
789                 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
790                 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
791                 spin_unlock_irqrestore(&ioapic_lock, flags);
792
793                 printk(KERN_DEBUG " %02x %03X %02X  ",
794                         i,
795                         entry.dest.logical.logical_dest,
796                         entry.dest.physical.physical_dest
797                 );
798
799                 printk("%1d    %1d    %1d   %1d   %1d    %1d    %1d    %02X\n",
800                         entry.mask,
801                         entry.trigger,
802                         entry.irr,
803                         entry.polarity,
804                         entry.delivery_status,
805                         entry.dest_mode,
806                         entry.delivery_mode,
807                         entry.vector
808                 );
809         }
810         }
811         printk(KERN_DEBUG "IRQ to pin mappings:\n");
812         for (i = 0; i < NR_IRQS; i++) {
813                 struct irq_pin_list *entry = irq_2_pin + i;
814                 if (entry->pin < 0)
815                         continue;
816                 printk(KERN_DEBUG "IRQ%d ", i);
817                 for (;;) {
818                         printk("-> %d:%d", entry->apic, entry->pin);
819                         if (!entry->next)
820                                 break;
821                         entry = irq_2_pin + entry->next;
822                 }
823                 printk("\n");
824         }
825
826         printk(KERN_INFO ".................................... done.\n");
827
828         return;
829 }
830
831 static void print_APIC_bitfield (int base)
832 {
833         unsigned int v;
834         int i, j;
835
836         printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
837         for (i = 0; i < 8; i++) {
838                 v = apic_read(base + i*0x10);
839                 for (j = 0; j < 32; j++) {
840                         if (v & (1<<j))
841                                 printk("1");
842                         else
843                                 printk("0");
844                 }
845                 printk("\n");
846         }
847 }
848
849 void /*__init*/ print_local_APIC(void * dummy)
850 {
851         unsigned int v, ver, maxlvt;
852
853         printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
854                 smp_processor_id(), hard_smp_processor_id());
855         v = apic_read(APIC_ID);
856         printk(KERN_INFO "... APIC ID:      %08x (%01x)\n", v, GET_APIC_ID(v));
857         v = apic_read(APIC_LVR);
858         printk(KERN_INFO "... APIC VERSION: %08x\n", v);
859         ver = GET_APIC_VERSION(v);
860         maxlvt = get_maxlvt();
861
862         v = apic_read(APIC_TASKPRI);
863         printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
864
865         if (APIC_INTEGRATED(ver)) {                     /* !82489DX */
866                 v = apic_read(APIC_ARBPRI);
867                 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
868                         v & APIC_ARBPRI_MASK);
869                 v = apic_read(APIC_PROCPRI);
870                 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
871         }
872
873         v = apic_read(APIC_EOI);
874         printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
875         v = apic_read(APIC_RRR);
876         printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
877         v = apic_read(APIC_LDR);
878         printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
879         v = apic_read(APIC_DFR);
880         printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
881         v = apic_read(APIC_SPIV);
882         printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
883
884         printk(KERN_DEBUG "... APIC ISR field:\n");
885         print_APIC_bitfield(APIC_ISR);
886         printk(KERN_DEBUG "... APIC TMR field:\n");
887         print_APIC_bitfield(APIC_TMR);
888         printk(KERN_DEBUG "... APIC IRR field:\n");
889         print_APIC_bitfield(APIC_IRR);
890
891         if (APIC_INTEGRATED(ver)) {             /* !82489DX */
892                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
893                         apic_write(APIC_ESR, 0);
894                 v = apic_read(APIC_ESR);
895                 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
896         }
897
898         v = apic_read(APIC_ICR);
899         printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
900         v = apic_read(APIC_ICR2);
901         printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
902
903         v = apic_read(APIC_LVTT);
904         printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
905
906         if (maxlvt > 3) {                       /* PC is LVT#4. */
907                 v = apic_read(APIC_LVTPC);
908                 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
909         }
910         v = apic_read(APIC_LVT0);
911         printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
912         v = apic_read(APIC_LVT1);
913         printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
914
915         if (maxlvt > 2) {                       /* ERR is LVT#3. */
916                 v = apic_read(APIC_LVTERR);
917                 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
918         }
919
920         v = apic_read(APIC_TMICT);
921         printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
922         v = apic_read(APIC_TMCCT);
923         printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
924         v = apic_read(APIC_TDCR);
925         printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
926         printk("\n");
927 }
928
929 void print_all_local_APICs (void)
930 {
931         on_each_cpu(print_local_APIC, NULL, 1, 1);
932 }
933
934 void /*__init*/ print_PIC(void)
935 {
936         extern spinlock_t i8259A_lock;
937         unsigned int v;
938         unsigned long flags;
939
940         printk(KERN_DEBUG "\nprinting PIC contents\n");
941
942         spin_lock_irqsave(&i8259A_lock, flags);
943
944         v = inb(0xa1) << 8 | inb(0x21);
945         printk(KERN_DEBUG "... PIC  IMR: %04x\n", v);
946
947         v = inb(0xa0) << 8 | inb(0x20);
948         printk(KERN_DEBUG "... PIC  IRR: %04x\n", v);
949
950         outb(0x0b,0xa0);
951         outb(0x0b,0x20);
952         v = inb(0xa0) << 8 | inb(0x20);
953         outb(0x0a,0xa0);
954         outb(0x0a,0x20);
955
956         spin_unlock_irqrestore(&i8259A_lock, flags);
957
958         printk(KERN_DEBUG "... PIC  ISR: %04x\n", v);
959
960         v = inb(0x4d1) << 8 | inb(0x4d0);
961         printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
962 }
963
964 static void __init enable_IO_APIC(void)
965 {
966         union IO_APIC_reg_01 reg_01;
967         int i;
968         unsigned long flags;
969
970         for (i = 0; i < PIN_MAP_SIZE; i++) {
971                 irq_2_pin[i].pin = -1;
972                 irq_2_pin[i].next = 0;
973         }
974         if (!pirqs_enabled)
975                 for (i = 0; i < MAX_PIRQS; i++)
976                         pirq_entries[i] = -1;
977
978         /*
979          * The number of IO-APIC IRQ registers (== #pins):
980          */
981         for (i = 0; i < nr_ioapics; i++) {
982                 spin_lock_irqsave(&ioapic_lock, flags);
983                 reg_01.raw = io_apic_read(i, 1);
984                 spin_unlock_irqrestore(&ioapic_lock, flags);
985                 nr_ioapic_registers[i] = reg_01.bits.entries+1;
986         }
987
988         /*
989          * Do not trust the IO-APIC being empty at bootup
990          */
991         clear_IO_APIC();
992 }
993
994 /*
995  * Not an __init, needed by the reboot code
996  */
997 void disable_IO_APIC(void)
998 {
999         /*
1000          * Clear the IO-APIC before rebooting:
1001          */
1002         clear_IO_APIC();
1003
1004         disconnect_bsp_APIC();
1005 }
1006
1007 /*
1008  * function to set the IO-APIC physical IDs based on the
1009  * values stored in the MPC table.
1010  *
1011  * by Matt Domsch <Matt_Domsch@dell.com>  Tue Dec 21 12:25:05 CST 1999
1012  */
1013
1014 static void __init setup_ioapic_ids_from_mpc (void)
1015 {
1016         union IO_APIC_reg_00 reg_00;
1017         unsigned long phys_id_present_map = phys_cpu_present_map;
1018         int apic;
1019         int i;
1020         unsigned char old_id;
1021         unsigned long flags;
1022
1023         if (acpi_ioapic) return; /* ACPI does that already */
1024
1025         /*
1026          * Set the IOAPIC ID to the value stored in the MPC table.
1027          */
1028         for (apic = 0; apic < nr_ioapics; apic++) {
1029
1030                 /* Read the register 0 value */
1031                 spin_lock_irqsave(&ioapic_lock, flags);
1032                 reg_00.raw = io_apic_read(apic, 0);
1033                 spin_unlock_irqrestore(&ioapic_lock, flags);
1034                 
1035                 old_id = mp_ioapics[apic].mpc_apicid;
1036
1037                 if (mp_ioapics[apic].mpc_apicid >= 0xf) {
1038                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1039                                 apic, mp_ioapics[apic].mpc_apicid);
1040                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1041                                 reg_00.bits.ID);
1042                         mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1043                 }
1044
1045                 /*
1046                  * Sanity check, is the ID really free? Every APIC in a
1047                  * system must have a unique ID or we get lots of nice
1048                  * 'stuck on smp_invalidate_needed IPI wait' messages.
1049                  */
1050                 if (phys_id_present_map & (1 << mp_ioapics[apic].mpc_apicid)) {
1051                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1052                                 apic, mp_ioapics[apic].mpc_apicid);
1053                         for (i = 0; i < 0xf; i++)
1054                                 if (!(phys_id_present_map & (1 << i)))
1055                                         break;
1056                         if (i >= 0xf)
1057                                 panic("Max APIC ID exceeded!\n");
1058                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1059                                 i);
1060                         phys_id_present_map |= 1 << i;
1061                         mp_ioapics[apic].mpc_apicid = i;
1062                 } else {
1063                         printk(KERN_INFO 
1064                                "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1065                         phys_id_present_map |= 1 << mp_ioapics[apic].mpc_apicid;
1066                 }
1067
1068
1069                 /*
1070                  * We need to adjust the IRQ routing table
1071                  * if the ID changed.
1072                  */
1073                 if (old_id != mp_ioapics[apic].mpc_apicid)
1074                         for (i = 0; i < mp_irq_entries; i++)
1075                                 if (mp_irqs[i].mpc_dstapic == old_id)
1076                                         mp_irqs[i].mpc_dstapic
1077                                                 = mp_ioapics[apic].mpc_apicid;
1078
1079                 /*
1080                  * Read the right value from the MPC table and
1081                  * write it into the ID register.
1082                  */
1083                 printk(KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1084                                 mp_ioapics[apic].mpc_apicid);
1085
1086                 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1087                 spin_lock_irqsave(&ioapic_lock, flags);
1088                 io_apic_write(apic, 0, reg_00.raw);
1089                 spin_unlock_irqrestore(&ioapic_lock, flags);
1090
1091                 /*
1092                  * Sanity check
1093                  */
1094                 spin_lock_irqsave(&ioapic_lock, flags);
1095                 reg_00.raw = io_apic_read(apic, 0);
1096                 spin_unlock_irqrestore(&ioapic_lock, flags);
1097                 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1098                         panic("could not set ID!\n");
1099                 else
1100                         printk(" ok.\n");
1101         }
1102 }
1103
1104 /*
1105  * There is a nasty bug in some older SMP boards, their mptable lies
1106  * about the timer IRQ. We do the following to work around the situation:
1107  *
1108  *      - timer IRQ defaults to IO-APIC IRQ
1109  *      - if this function detects that timer IRQs are defunct, then we fall
1110  *        back to ISA timer IRQs
1111  */
1112 static int __init timer_irq_works(void)
1113 {
1114         unsigned long t1 = jiffies;
1115
1116         local_irq_enable();
1117         /* Let ten ticks pass... */
1118         mdelay((10 * 1000) / HZ);
1119
1120         /*
1121          * Expect a few ticks at least, to be sure some possible
1122          * glue logic does not lock up after one or two first
1123          * ticks in a non-ExtINT mode.  Also the local APIC
1124          * might have cached one ExtINT interrupt.  Finally, at
1125          * least one tick may be lost due to delays.
1126          */
1127
1128         /* jiffies wrap? */
1129         if (jiffies - t1 > 4)
1130                 return 1;
1131         return 0;
1132 }
1133
1134 /*
1135  * In the SMP+IOAPIC case it might happen that there are an unspecified
1136  * number of pending IRQ events unhandled. These cases are very rare,
1137  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1138  * better to do it this way as thus we do not have to be aware of
1139  * 'pending' interrupts in the IRQ path, except at this point.
1140  */
1141 /*
1142  * Edge triggered needs to resend any interrupt
1143  * that was delayed but this is now handled in the device
1144  * independent code.
1145  */
1146 #define enable_edge_ioapic_irq unmask_IO_APIC_irq
1147
1148 static void disable_edge_ioapic_irq (unsigned int irq) { /* nothing */ }
1149
1150 /*
1151  * Starting up a edge-triggered IO-APIC interrupt is
1152  * nasty - we need to make sure that we get the edge.
1153  * If it is already asserted for some reason, we need
1154  * return 1 to indicate that is was pending.
1155  *
1156  * This is not complete - we should be able to fake
1157  * an edge even if it isn't on the 8259A...
1158  */
1159
1160 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1161 {
1162         int was_pending = 0;
1163         unsigned long flags;
1164
1165         spin_lock_irqsave(&ioapic_lock, flags);
1166         if (irq < 16) {
1167                 disable_8259A_irq(irq);
1168                 if (i8259A_irq_pending(irq))
1169                         was_pending = 1;
1170         }
1171         __unmask_IO_APIC_irq(irq);
1172         spin_unlock_irqrestore(&ioapic_lock, flags);
1173
1174         return was_pending;
1175 }
1176
1177 #define shutdown_edge_ioapic_irq        disable_edge_ioapic_irq
1178
1179 /*
1180  * Once we have recorded IRQ_PENDING already, we can mask the
1181  * interrupt for real. This prevents IRQ storms from unhandled
1182  * devices.
1183  */
1184 static void ack_edge_ioapic_irq(unsigned int irq)
1185 {
1186         if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1187                                         == (IRQ_PENDING | IRQ_DISABLED))
1188                 mask_IO_APIC_irq(irq);
1189         ack_APIC_irq();
1190 }
1191
1192 static void end_edge_ioapic_irq (unsigned int i) { /* nothing */ }
1193
1194
1195 /*
1196  * Level triggered interrupts can just be masked,
1197  * and shutting down and starting up the interrupt
1198  * is the same as enabling and disabling them -- except
1199  * with a startup need to return a "was pending" value.
1200  *
1201  * Level triggered interrupts are special because we
1202  * do not touch any IO-APIC register while handling
1203  * them. We ack the APIC in the end-IRQ handler, not
1204  * in the start-IRQ-handler. Protection against reentrance
1205  * from the same interrupt is still provided, both by the
1206  * generic IRQ layer and by the fact that an unacked local
1207  * APIC does not accept IRQs.
1208  */
1209 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1210 {
1211         unmask_IO_APIC_irq(irq);
1212
1213         return 0; /* don't check for pending */
1214 }
1215
1216 #define shutdown_level_ioapic_irq       mask_IO_APIC_irq
1217 #define enable_level_ioapic_irq         unmask_IO_APIC_irq
1218 #define disable_level_ioapic_irq        mask_IO_APIC_irq
1219
1220 static void end_level_ioapic_irq (unsigned int irq)
1221 {
1222         unsigned long v;
1223         int i;
1224
1225 /*
1226  * It appears there is an erratum which affects at least version 0x11
1227  * of I/O APIC (that's the 82093AA and cores integrated into various
1228  * chipsets).  Under certain conditions a level-triggered interrupt is
1229  * erroneously delivered as edge-triggered one but the respective IRR
1230  * bit gets set nevertheless.  As a result the I/O unit expects an EOI
1231  * message but it will never arrive and further interrupts are blocked
1232  * from the source.  The exact reason is so far unknown, but the
1233  * phenomenon was observed when two consecutive interrupt requests
1234  * from a given source get delivered to the same CPU and the source is
1235  * temporarily disabled in between.
1236  *
1237  * A workaround is to simulate an EOI message manually.  We achieve it
1238  * by setting the trigger mode to edge and then to level when the edge
1239  * trigger mode gets detected in the TMR of a local APIC for a
1240  * level-triggered interrupt.  We mask the source for the time of the
1241  * operation to prevent an edge-triggered interrupt escaping meanwhile.
1242  * The idea is from Manfred Spraul.  --macro
1243  */
1244         i = IO_APIC_VECTOR(irq);
1245         v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1246
1247         ack_APIC_irq();
1248
1249         if (!(v & (1 << (i & 0x1f)))) {
1250 #ifdef APIC_LOCKUP_DEBUG
1251                 struct irq_pin_list *entry;
1252 #endif
1253
1254 #ifdef APIC_MISMATCH_DEBUG
1255                 atomic_inc(&irq_mis_count);
1256 #endif
1257                 spin_lock(&ioapic_lock);
1258                 __mask_and_edge_IO_APIC_irq(irq);
1259 #ifdef APIC_LOCKUP_DEBUG
1260                 for (entry = irq_2_pin + irq;;) {
1261                         unsigned int reg;
1262
1263                         if (entry->pin == -1)
1264                                 break;
1265                         reg = io_apic_read(entry->apic, 0x10 + entry->pin * 2);
1266                         if (reg & 0x00004000)
1267                                 printk(KERN_CRIT "Aieee!!!  Remote IRR"
1268                                         " still set after unlock!\n");
1269                         if (!entry->next)
1270                                 break;
1271                         entry = irq_2_pin + entry->next;
1272                 }
1273 #endif
1274                 __unmask_and_level_IO_APIC_irq(irq);
1275                 spin_unlock(&ioapic_lock);
1276         }
1277 }
1278
1279 static void mask_and_ack_level_ioapic_irq (unsigned int irq) { /* nothing */ }
1280
1281 static void set_ioapic_affinity (unsigned int irq, unsigned long mask)
1282 {
1283         unsigned long flags;
1284         /*
1285          * Only the first 8 bits are valid.
1286          */
1287         mask = mask << 24;
1288
1289         spin_lock_irqsave(&ioapic_lock, flags);
1290         __DO_ACTION(1, = mask, )
1291         spin_unlock_irqrestore(&ioapic_lock, flags);
1292 }
1293
1294 /*
1295  * Level and edge triggered IO-APIC interrupts need different handling,
1296  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1297  * handled with the level-triggered descriptor, but that one has slightly
1298  * more overhead. Level-triggered interrupts cannot be handled with the
1299  * edge-triggered handler, without risking IRQ storms and other ugly
1300  * races.
1301  */
1302
1303 static struct hw_interrupt_type ioapic_edge_irq_type = {
1304         .typename = "IO-APIC-edge",
1305         .startup = startup_edge_ioapic_irq,
1306         .shutdown = shutdown_edge_ioapic_irq,
1307         .enable = enable_edge_ioapic_irq,
1308         .disable = disable_edge_ioapic_irq,
1309         .ack = ack_edge_ioapic_irq,
1310         .end = end_edge_ioapic_irq,
1311         .set_affinity = set_ioapic_affinity,
1312 };
1313
1314 static struct hw_interrupt_type ioapic_level_irq_type = {
1315         .typename = "IO-APIC-level",
1316         .startup = startup_level_ioapic_irq,
1317         .shutdown = shutdown_level_ioapic_irq,
1318         .enable = enable_level_ioapic_irq,
1319         .disable = disable_level_ioapic_irq,
1320         .ack = mask_and_ack_level_ioapic_irq,
1321         .end = end_level_ioapic_irq,
1322         .set_affinity = set_ioapic_affinity,
1323 };
1324
1325 static inline void init_IO_APIC_traps(void)
1326 {
1327         int irq;
1328
1329         /*
1330          * NOTE! The local APIC isn't very good at handling
1331          * multiple interrupts at the same interrupt level.
1332          * As the interrupt level is determined by taking the
1333          * vector number and shifting that right by 4, we
1334          * want to spread these out a bit so that they don't
1335          * all fall in the same interrupt level.
1336          *
1337          * Also, we've got to be careful not to trash gate
1338          * 0x80, because int 0x80 is hm, kind of importantish. ;)
1339          */
1340         for (irq = 0; irq < NR_IRQS ; irq++) {
1341                 if (IO_APIC_IRQ(irq) && !IO_APIC_VECTOR(irq)) {
1342                         /*
1343                          * Hmm.. We don't have an entry for this,
1344                          * so default to an old-fashioned 8259
1345                          * interrupt if we can..
1346                          */
1347                         if (irq < 16)
1348                                 make_8259A_irq(irq);
1349                         else
1350                                 /* Strange. Oh, well.. */
1351                                 irq_desc[irq].handler = &no_irq_type;
1352                 }
1353         }
1354 }
1355
1356 static void enable_lapic_irq (unsigned int irq)
1357 {
1358         unsigned long v;
1359
1360         v = apic_read(APIC_LVT0);
1361         apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1362 }
1363
1364 static void disable_lapic_irq (unsigned int irq)
1365 {
1366         unsigned long v;
1367
1368         v = apic_read(APIC_LVT0);
1369         apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1370 }
1371
1372 static void ack_lapic_irq (unsigned int irq)
1373 {
1374         ack_APIC_irq();
1375 }
1376
1377 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1378
1379 static struct hw_interrupt_type lapic_irq_type = {
1380         .typename = "local-APIC-edge",
1381         .startup = NULL, /* startup_irq() not used for IRQ0 */
1382         .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1383         .enable = enable_lapic_irq,
1384         .disable = disable_lapic_irq,
1385         .ack = ack_lapic_irq,
1386         .end = end_lapic_irq,
1387 };
1388
1389 static void setup_nmi (void)
1390 {
1391         /*
1392          * Dirty trick to enable the NMI watchdog ...
1393          * We put the 8259A master into AEOI mode and
1394          * unmask on all local APICs LVT0 as NMI.
1395          *
1396          * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1397          * is from Maciej W. Rozycki - so we do not have to EOI from
1398          * the NMI handler or the timer interrupt.
1399          */ 
1400         printk(KERN_INFO "activating NMI Watchdog ...");
1401
1402         enable_NMI_through_LVT0(NULL);
1403
1404         printk(" done.\n");
1405 }
1406
1407 /*
1408  * This looks a bit hackish but it's about the only one way of sending
1409  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
1410  * not support the ExtINT mode, unfortunately.  We need to send these
1411  * cycles as some i82489DX-based boards have glue logic that keeps the
1412  * 8259A interrupt line asserted until INTA.  --macro
1413  */
1414 static inline void unlock_ExtINT_logic(void)
1415 {
1416         int pin, i;
1417         struct IO_APIC_route_entry entry0, entry1;
1418         unsigned char save_control, save_freq_select;
1419         unsigned long flags;
1420
1421         pin = find_isa_irq_pin(8, mp_INT);
1422         if (pin == -1)
1423                 return;
1424
1425         spin_lock_irqsave(&ioapic_lock, flags);
1426         *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1427         *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1428         spin_unlock_irqrestore(&ioapic_lock, flags);
1429         clear_IO_APIC_pin(0, pin);
1430
1431         memset(&entry1, 0, sizeof(entry1));
1432
1433         entry1.dest_mode = 0;                   /* physical delivery */
1434         entry1.mask = 0;                        /* unmask IRQ now */
1435         entry1.dest.physical.physical_dest = hard_smp_processor_id();
1436         entry1.delivery_mode = dest_ExtINT;
1437         entry1.polarity = entry0.polarity;
1438         entry1.trigger = 0;
1439         entry1.vector = 0;
1440
1441         spin_lock_irqsave(&ioapic_lock, flags);
1442         io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1443         io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1444         spin_unlock_irqrestore(&ioapic_lock, flags);
1445
1446         save_control = CMOS_READ(RTC_CONTROL);
1447         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1448         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1449                    RTC_FREQ_SELECT);
1450         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1451
1452         i = 100;
1453         while (i-- > 0) {
1454                 mdelay(10);
1455                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1456                         i -= 10;
1457         }
1458
1459         CMOS_WRITE(save_control, RTC_CONTROL);
1460         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1461         clear_IO_APIC_pin(0, pin);
1462
1463         spin_lock_irqsave(&ioapic_lock, flags);
1464         io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1465         io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1466         spin_unlock_irqrestore(&ioapic_lock, flags);
1467 }
1468
1469 /*
1470  * This code may look a bit paranoid, but it's supposed to cooperate with
1471  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
1472  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
1473  * fanatically on his truly buggy board.
1474  */
1475 static inline void check_timer(void)
1476 {
1477         int pin1, pin2;
1478         int vector;
1479
1480         /*
1481          * get/set the timer IRQ vector:
1482          */
1483         disable_8259A_irq(0);
1484         vector = assign_irq_vector(0);
1485         set_intr_gate(vector, interrupt[0]);
1486
1487         /*
1488          * Subtle, code in do_timer_interrupt() expects an AEOI
1489          * mode for the 8259A whenever interrupts are routed
1490          * through I/O APICs.  Also IRQ0 has to be enabled in
1491          * the 8259A which implies the virtual wire has to be
1492          * disabled in the local APIC.
1493          */
1494         apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1495         init_8259A(1);
1496         enable_8259A_irq(0);
1497
1498         pin1 = find_isa_irq_pin(0, mp_INT);
1499         pin2 = find_isa_irq_pin(0, mp_ExtINT);
1500
1501         printk(KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1502
1503         if (pin1 != -1) {
1504                 /*
1505                  * Ok, does IRQ0 through the IOAPIC work?
1506                  */
1507                 unmask_IO_APIC_irq(0);
1508                 if (timer_irq_works()) {
1509                         if (nmi_watchdog == NMI_IO_APIC) {
1510                                 disable_8259A_irq(0);
1511                                 setup_nmi();
1512                                 enable_8259A_irq(0);
1513                                 check_nmi_watchdog();
1514                         }
1515                         return;
1516                 }
1517                 clear_IO_APIC_pin(0, pin1);
1518                 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1519         }
1520
1521         printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1522         if (pin2 != -1) {
1523                 printk("\n..... (found pin %d) ...", pin2);
1524                 /*
1525                  * legacy devices should be connected to IO APIC #0
1526                  */
1527                 setup_ExtINT_IRQ0_pin(pin2, vector);
1528                 if (timer_irq_works()) {
1529                         printk("works.\n");
1530                         if (nmi_watchdog == NMI_IO_APIC) {
1531                                 setup_nmi();
1532                                 check_nmi_watchdog();
1533                         }
1534                         return;
1535                 }
1536                 /*
1537                  * Cleanup, just in case ...
1538                  */
1539                 clear_IO_APIC_pin(0, pin2);
1540         }
1541         printk(" failed.\n");
1542
1543         if (nmi_watchdog) {
1544                 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1545                 nmi_watchdog = 0;
1546         }
1547
1548         printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1549
1550         disable_8259A_irq(0);
1551         irq_desc[0].handler = &lapic_irq_type;
1552         apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector);   /* Fixed mode */
1553         enable_8259A_irq(0);
1554
1555         if (timer_irq_works()) {
1556                 printk(" works.\n");
1557                 return;
1558         }
1559         apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1560         printk(" failed.\n");
1561
1562         printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1563
1564         init_8259A(0);
1565         make_8259A_irq(0);
1566         apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1567
1568         unlock_ExtINT_logic();
1569
1570         if (timer_irq_works()) {
1571                 printk(" works.\n");
1572                 return;
1573         }
1574         printk(" failed :(.\n");
1575         panic("IO-APIC + timer doesn't work! pester mingo@redhat.com");
1576 }
1577
1578 /*
1579  *
1580  * IRQ's that are handled by the old PIC in all cases:
1581  * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1582  *   Linux doesn't really care, as it's not actually used
1583  *   for any interrupt handling anyway.
1584  * - There used to be IRQ13 here as well, but all
1585  *   MPS-compliant must not use it for FPU coupling and we
1586  *   want to use exception 16 anyway.  And there are
1587  *   systems who connect it to an I/O APIC for other uses.
1588  *   Thus we don't mark it special any longer.
1589  *
1590  * Additionally, something is definitely wrong with irq9
1591  * on PIIX4 boards.
1592  */
1593 #define PIC_IRQS        (1<<2)
1594
1595 void __init setup_IO_APIC(void)
1596 {
1597         enable_IO_APIC();
1598
1599         io_apic_irqs = ~PIC_IRQS;
1600         printk("ENABLING IO-APIC IRQs\n");
1601
1602         /*
1603          * Set up the IO-APIC IRQ routing table.
1604          */
1605         setup_ioapic_ids_from_mpc();
1606         sync_Arb_IDs();
1607         setup_IO_APIC_irqs();
1608         init_IO_APIC_traps();
1609         check_timer();
1610         print_IO_APIC();
1611 }
1612
1613 /* Ensure the ACPI SCI interrupt level is active low, edge-triggered */
1614
1615 void __init mp_config_ioapic_for_sci(int irq)
1616 {
1617 #if 0 /* fixme */
1618        int ioapic;
1619        int ioapic_pin;
1620
1621        ioapic = mp_find_ioapic(irq);
1622
1623        ioapic_pin = irq - mp_ioapic_routing[ioapic].irq_start;
1624
1625        io_apic_set_pci_routing(ioapic, ioapic_pin, irq);
1626 #endif
1627 }
1628
1629
1630 /* --------------------------------------------------------------------------
1631                           ACPI-based IOAPIC Configuration
1632    -------------------------------------------------------------------------- */
1633
1634 #ifdef CONFIG_ACPI_BOOT
1635
1636 #define IO_APIC_MAX_ID          15
1637
1638 int __init io_apic_get_unique_id (int ioapic, int apic_id)
1639 {
1640         union IO_APIC_reg_00 reg_00;
1641         static unsigned long apic_id_map = 0;
1642         unsigned long flags;
1643         int i = 0;
1644
1645         /*
1646          * The P4 platform supports up to 256 APIC IDs on two separate APIC 
1647          * buses (one for LAPICs, one for IOAPICs), where predecessors only 
1648          * supports up to 16 on one shared APIC bus.
1649          * 
1650          * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
1651          *      advantage of new APIC bus architecture.
1652          */
1653
1654         if (!apic_id_map)
1655                 apic_id_map = phys_cpu_present_map;
1656
1657         spin_lock_irqsave(&ioapic_lock, flags);
1658         reg_00.raw = io_apic_read(ioapic, 0);
1659         spin_unlock_irqrestore(&ioapic_lock, flags);
1660
1661         if (apic_id >= IO_APIC_MAX_ID) {
1662                 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
1663                         "%d\n", ioapic, apic_id, reg_00.bits.ID);
1664                 apic_id = reg_00.bits.ID;
1665         }
1666
1667         /*
1668          * Every APIC in a system must have a unique ID or we get lots of nice 
1669          * 'stuck on smp_invalidate_needed IPI wait' messages.
1670          */
1671         if (apic_id_map & (1 << apic_id)) {
1672
1673                 for (i = 0; i < IO_APIC_MAX_ID; i++) {
1674                         if (!(apic_id_map & (1 << i)))
1675                                 break;
1676                 }
1677
1678                 if (i == IO_APIC_MAX_ID)
1679                         panic("Max apic_id exceeded!\n");
1680
1681                 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
1682                         "trying %d\n", ioapic, apic_id, i);
1683
1684                 apic_id = i;
1685         } 
1686
1687         apic_id_map |= (1 << apic_id);
1688
1689         if (reg_00.bits.ID != apic_id) {
1690                 reg_00.bits.ID = apic_id;
1691
1692                 spin_lock_irqsave(&ioapic_lock, flags);
1693                 io_apic_write(ioapic, 0, reg_00.raw);
1694                 reg_00.raw = io_apic_read(ioapic, 0);
1695                 spin_unlock_irqrestore(&ioapic_lock, flags);
1696
1697                 /* Sanity check */
1698                 if (reg_00.bits.ID != apic_id)
1699                         panic("IOAPIC[%d]: Unable change apic_id!\n", ioapic);
1700         }
1701
1702         printk(KERN_INFO "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
1703
1704         return apic_id;
1705 }
1706
1707
1708 int __init io_apic_get_version (int ioapic)
1709 {
1710         union IO_APIC_reg_01    reg_01;
1711         unsigned long flags;
1712
1713         spin_lock_irqsave(&ioapic_lock, flags);
1714         reg_01.raw = io_apic_read(ioapic, 1);
1715         spin_unlock_irqrestore(&ioapic_lock, flags);
1716
1717         return reg_01.bits.version;
1718 }
1719
1720
1721 int __init io_apic_get_redir_entries (int ioapic)
1722 {
1723         union IO_APIC_reg_01    reg_01;
1724         unsigned long flags;
1725
1726         spin_lock_irqsave(&ioapic_lock, flags);
1727         reg_01.raw = io_apic_read(ioapic, 1);
1728         spin_unlock_irqrestore(&ioapic_lock, flags);
1729
1730         return reg_01.bits.entries;
1731 }
1732
1733
1734 int io_apic_set_pci_routing (int ioapic, int pin, int irq)
1735 {
1736         struct IO_APIC_route_entry entry;
1737         unsigned long flags;
1738
1739         if (!IO_APIC_IRQ(irq)) {
1740                 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0/n", 
1741                         ioapic);
1742                 return -EINVAL;
1743         }
1744
1745         /*
1746          * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1747          * Note that we mask (disable) IRQs now -- these get enabled when the
1748          * corresponding device driver registers for this IRQ.
1749          */
1750
1751         memset(&entry,0,sizeof(entry));
1752
1753         entry.delivery_mode = dest_LowestPrio;
1754         entry.dest_mode = INT_DELIVERY_MODE;
1755         entry.dest.logical.logical_dest = TARGET_CPUS;
1756         entry.mask = 1;                                  /* Disabled (masked) */
1757         entry.trigger = 1;                                 /* Level sensitive */
1758         entry.polarity = 1;                                     /* Low active */
1759
1760         add_pin_to_irq(irq, ioapic, pin);
1761
1762         entry.vector = assign_irq_vector(irq);
1763
1764         printk(KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1765                 "IRQ %d)\n", ioapic, 
1766                 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq);
1767
1768         irq_desc[irq].handler = &ioapic_level_irq_type;
1769
1770         set_intr_gate(entry.vector, interrupt[irq]);
1771
1772         if (!ioapic && (irq < 16))
1773                 disable_8259A_irq(irq);
1774
1775         spin_lock_irqsave(&ioapic_lock, flags);
1776         io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
1777         io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
1778         spin_unlock_irqrestore(&ioapic_lock, flags);
1779
1780         return entry.vector;
1781 }
1782
1783 #endif /*CONFIG_ACPI_BOOT*/
1784
1785 #ifndef CONFIG_SMP
1786 void send_IPI_self(int vector)
1787 {
1788         unsigned int cfg;
1789         
1790        /*
1791         * Wait for idle.
1792         */
1793         apic_wait_icr_idle();
1794         cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
1795
1796         /*
1797          * Send the IPI. The write to APIC_ICR fires this off.
1798          */
1799         apic_write_around(APIC_ICR, cfg);
1800 }
1801 #endif