[PATCH] genirq: x86_64 irq: Remove the msi assumption that irq == vector
[safe/jmp/linux-2.6] / 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/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/pci.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
33 #ifdef CONFIG_ACPI
34 #include <acpi/acpi_bus.h>
35 #endif
36
37 #include <asm/io.h>
38 #include <asm/smp.h>
39 #include <asm/desc.h>
40 #include <asm/proto.h>
41 #include <asm/mach_apic.h>
42 #include <asm/acpi.h>
43 #include <asm/dma.h>
44 #include <asm/nmi.h>
45 #include <asm/msidef.h>
46
47 static int assign_irq_vector(int irq);
48
49 #define __apicdebuginit  __init
50
51 int sis_apic_bug; /* not actually supported, dummy for compile */
52
53 static int no_timer_check;
54
55 static int disable_timer_pin_1 __initdata;
56
57 int timer_over_8254 __initdata = 0;
58
59 /* Where if anywhere is the i8259 connect in external int mode */
60 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
61
62 static DEFINE_SPINLOCK(ioapic_lock);
63 static DEFINE_SPINLOCK(vector_lock);
64
65 /*
66  * # of IRQ routing registers
67  */
68 int nr_ioapic_registers[MAX_IO_APICS];
69
70 /*
71  * Rough estimation of how many shared IRQs there are, can
72  * be changed anytime.
73  */
74 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
75 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
76
77 /*
78  * This is performance-critical, we want to do it O(1)
79  *
80  * the indexing order of this array favors 1:1 mappings
81  * between pins and IRQs.
82  */
83
84 static struct irq_pin_list {
85         short apic, pin, next;
86 } irq_2_pin[PIN_MAP_SIZE];
87
88 #define __DO_ACTION(R, ACTION, FINAL)                                   \
89                                                                         \
90 {                                                                       \
91         int pin;                                                        \
92         struct irq_pin_list *entry = irq_2_pin + irq;                   \
93                                                                         \
94         BUG_ON(irq >= NR_IRQS);                                         \
95         for (;;) {                                                      \
96                 unsigned int reg;                                       \
97                 pin = entry->pin;                                       \
98                 if (pin == -1)                                          \
99                         break;                                          \
100                 reg = io_apic_read(entry->apic, 0x10 + R + pin*2);      \
101                 reg ACTION;                                             \
102                 io_apic_modify(entry->apic, reg);                       \
103                 if (!entry->next)                                       \
104                         break;                                          \
105                 entry = irq_2_pin + entry->next;                        \
106         }                                                               \
107         FINAL;                                                          \
108 }
109
110 union entry_union {
111         struct { u32 w1, w2; };
112         struct IO_APIC_route_entry entry;
113 };
114
115 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
116 {
117         union entry_union eu;
118         unsigned long flags;
119         spin_lock_irqsave(&ioapic_lock, flags);
120         eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
121         eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
122         spin_unlock_irqrestore(&ioapic_lock, flags);
123         return eu.entry;
124 }
125
126 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
127 {
128         unsigned long flags;
129         union entry_union eu;
130         eu.entry = e;
131         spin_lock_irqsave(&ioapic_lock, flags);
132         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
133         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
134         spin_unlock_irqrestore(&ioapic_lock, flags);
135 }
136
137 #ifdef CONFIG_SMP
138 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
139 {
140         unsigned long flags;
141         unsigned int dest;
142         cpumask_t tmp;
143
144         cpus_and(tmp, mask, cpu_online_map);
145         if (cpus_empty(tmp))
146                 tmp = TARGET_CPUS;
147
148         cpus_and(mask, tmp, CPU_MASK_ALL);
149
150         dest = cpu_mask_to_apicid(mask);
151
152         /*
153          * Only the high 8 bits are valid.
154          */
155         dest = SET_APIC_LOGICAL_ID(dest);
156
157         spin_lock_irqsave(&ioapic_lock, flags);
158         __DO_ACTION(1, = dest, )
159         set_native_irq_info(irq, mask);
160         spin_unlock_irqrestore(&ioapic_lock, flags);
161 }
162 #endif
163
164 static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
165
166 /*
167  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
168  * shared ISA-space IRQs, so we have to support them. We are super
169  * fast in the common case, and fast for shared ISA-space IRQs.
170  */
171 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
172 {
173         static int first_free_entry = NR_IRQS;
174         struct irq_pin_list *entry = irq_2_pin + irq;
175
176         BUG_ON(irq >= NR_IRQS);
177         while (entry->next)
178                 entry = irq_2_pin + entry->next;
179
180         if (entry->pin != -1) {
181                 entry->next = first_free_entry;
182                 entry = irq_2_pin + entry->next;
183                 if (++first_free_entry >= PIN_MAP_SIZE)
184                         panic("io_apic.c: ran out of irq_2_pin entries!");
185         }
186         entry->apic = apic;
187         entry->pin = pin;
188 }
189
190
191 #define DO_ACTION(name,R,ACTION, FINAL)                                 \
192                                                                         \
193         static void name##_IO_APIC_irq (unsigned int irq)               \
194         __DO_ACTION(R, ACTION, FINAL)
195
196 DO_ACTION( __mask,             0, |= 0x00010000, io_apic_sync(entry->apic) )
197                                                 /* mask = 1 */
198 DO_ACTION( __unmask,           0, &= 0xfffeffff, )
199                                                 /* mask = 0 */
200
201 static void mask_IO_APIC_irq (unsigned int irq)
202 {
203         unsigned long flags;
204
205         spin_lock_irqsave(&ioapic_lock, flags);
206         __mask_IO_APIC_irq(irq);
207         spin_unlock_irqrestore(&ioapic_lock, flags);
208 }
209
210 static void unmask_IO_APIC_irq (unsigned int irq)
211 {
212         unsigned long flags;
213
214         spin_lock_irqsave(&ioapic_lock, flags);
215         __unmask_IO_APIC_irq(irq);
216         spin_unlock_irqrestore(&ioapic_lock, flags);
217 }
218
219 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
220 {
221         struct IO_APIC_route_entry entry;
222
223         /* Check delivery_mode to be sure we're not clearing an SMI pin */
224         entry = ioapic_read_entry(apic, pin);
225         if (entry.delivery_mode == dest_SMI)
226                 return;
227         /*
228          * Disable it in the IO-APIC irq-routing table:
229          */
230         memset(&entry, 0, sizeof(entry));
231         entry.mask = 1;
232         ioapic_write_entry(apic, pin, entry);
233 }
234
235 static void clear_IO_APIC (void)
236 {
237         int apic, pin;
238
239         for (apic = 0; apic < nr_ioapics; apic++)
240                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
241                         clear_IO_APIC_pin(apic, pin);
242 }
243
244 int skip_ioapic_setup;
245 int ioapic_force;
246
247 /* dummy parsing: see setup.c */
248
249 static int __init disable_ioapic_setup(char *str)
250 {
251         skip_ioapic_setup = 1;
252         return 0;
253 }
254 early_param("noapic", disable_ioapic_setup);
255
256 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
257 static int __init disable_timer_pin_setup(char *arg)
258 {
259         disable_timer_pin_1 = 1;
260         return 1;
261 }
262 __setup("disable_timer_pin_1", disable_timer_pin_setup);
263
264 static int __init setup_disable_8254_timer(char *s)
265 {
266         timer_over_8254 = -1;
267         return 1;
268 }
269 static int __init setup_enable_8254_timer(char *s)
270 {
271         timer_over_8254 = 2;
272         return 1;
273 }
274
275 __setup("disable_8254_timer", setup_disable_8254_timer);
276 __setup("enable_8254_timer", setup_enable_8254_timer);
277
278
279 /*
280  * Find the IRQ entry number of a certain pin.
281  */
282 static int find_irq_entry(int apic, int pin, int type)
283 {
284         int i;
285
286         for (i = 0; i < mp_irq_entries; i++)
287                 if (mp_irqs[i].mpc_irqtype == type &&
288                     (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
289                      mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
290                     mp_irqs[i].mpc_dstirq == pin)
291                         return i;
292
293         return -1;
294 }
295
296 /*
297  * Find the pin to which IRQ[irq] (ISA) is connected
298  */
299 static int __init find_isa_irq_pin(int irq, int type)
300 {
301         int i;
302
303         for (i = 0; i < mp_irq_entries; i++) {
304                 int lbus = mp_irqs[i].mpc_srcbus;
305
306                 if (test_bit(lbus, mp_bus_not_pci) &&
307                     (mp_irqs[i].mpc_irqtype == type) &&
308                     (mp_irqs[i].mpc_srcbusirq == irq))
309
310                         return mp_irqs[i].mpc_dstirq;
311         }
312         return -1;
313 }
314
315 static int __init find_isa_irq_apic(int irq, int type)
316 {
317         int i;
318
319         for (i = 0; i < mp_irq_entries; i++) {
320                 int lbus = mp_irqs[i].mpc_srcbus;
321
322                 if (test_bit(lbus, mp_bus_not_pci) &&
323                     (mp_irqs[i].mpc_irqtype == type) &&
324                     (mp_irqs[i].mpc_srcbusirq == irq))
325                         break;
326         }
327         if (i < mp_irq_entries) {
328                 int apic;
329                 for(apic = 0; apic < nr_ioapics; apic++) {
330                         if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
331                                 return apic;
332                 }
333         }
334
335         return -1;
336 }
337
338 /*
339  * Find a specific PCI IRQ entry.
340  * Not an __init, possibly needed by modules
341  */
342 static int pin_2_irq(int idx, int apic, int pin);
343
344 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
345 {
346         int apic, i, best_guess = -1;
347
348         apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
349                 bus, slot, pin);
350         if (mp_bus_id_to_pci_bus[bus] == -1) {
351                 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
352                 return -1;
353         }
354         for (i = 0; i < mp_irq_entries; i++) {
355                 int lbus = mp_irqs[i].mpc_srcbus;
356
357                 for (apic = 0; apic < nr_ioapics; apic++)
358                         if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
359                             mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
360                                 break;
361
362                 if (!test_bit(lbus, mp_bus_not_pci) &&
363                     !mp_irqs[i].mpc_irqtype &&
364                     (bus == lbus) &&
365                     (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
366                         int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
367
368                         if (!(apic || IO_APIC_IRQ(irq)))
369                                 continue;
370
371                         if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
372                                 return irq;
373                         /*
374                          * Use the first all-but-pin matching entry as a
375                          * best-guess fuzzy result for broken mptables.
376                          */
377                         if (best_guess < 0)
378                                 best_guess = irq;
379                 }
380         }
381         BUG_ON(best_guess >= NR_IRQS);
382         return best_guess;
383 }
384
385 /* ISA interrupts are always polarity zero edge triggered,
386  * when listed as conforming in the MP table. */
387
388 #define default_ISA_trigger(idx)        (0)
389 #define default_ISA_polarity(idx)       (0)
390
391 /* PCI interrupts are always polarity one level triggered,
392  * when listed as conforming in the MP table. */
393
394 #define default_PCI_trigger(idx)        (1)
395 #define default_PCI_polarity(idx)       (1)
396
397 static int __init MPBIOS_polarity(int idx)
398 {
399         int bus = mp_irqs[idx].mpc_srcbus;
400         int polarity;
401
402         /*
403          * Determine IRQ line polarity (high active or low active):
404          */
405         switch (mp_irqs[idx].mpc_irqflag & 3)
406         {
407                 case 0: /* conforms, ie. bus-type dependent polarity */
408                         if (test_bit(bus, mp_bus_not_pci))
409                                 polarity = default_ISA_polarity(idx);
410                         else
411                                 polarity = default_PCI_polarity(idx);
412                         break;
413                 case 1: /* high active */
414                 {
415                         polarity = 0;
416                         break;
417                 }
418                 case 2: /* reserved */
419                 {
420                         printk(KERN_WARNING "broken BIOS!!\n");
421                         polarity = 1;
422                         break;
423                 }
424                 case 3: /* low active */
425                 {
426                         polarity = 1;
427                         break;
428                 }
429                 default: /* invalid */
430                 {
431                         printk(KERN_WARNING "broken BIOS!!\n");
432                         polarity = 1;
433                         break;
434                 }
435         }
436         return polarity;
437 }
438
439 static int MPBIOS_trigger(int idx)
440 {
441         int bus = mp_irqs[idx].mpc_srcbus;
442         int trigger;
443
444         /*
445          * Determine IRQ trigger mode (edge or level sensitive):
446          */
447         switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
448         {
449                 case 0: /* conforms, ie. bus-type dependent */
450                         if (test_bit(bus, mp_bus_not_pci))
451                                 trigger = default_ISA_trigger(idx);
452                         else
453                                 trigger = default_PCI_trigger(idx);
454                         break;
455                 case 1: /* edge */
456                 {
457                         trigger = 0;
458                         break;
459                 }
460                 case 2: /* reserved */
461                 {
462                         printk(KERN_WARNING "broken BIOS!!\n");
463                         trigger = 1;
464                         break;
465                 }
466                 case 3: /* level */
467                 {
468                         trigger = 1;
469                         break;
470                 }
471                 default: /* invalid */
472                 {
473                         printk(KERN_WARNING "broken BIOS!!\n");
474                         trigger = 0;
475                         break;
476                 }
477         }
478         return trigger;
479 }
480
481 static inline int irq_polarity(int idx)
482 {
483         return MPBIOS_polarity(idx);
484 }
485
486 static inline int irq_trigger(int idx)
487 {
488         return MPBIOS_trigger(idx);
489 }
490
491 static int next_irq = 16;
492
493 /*
494  * gsi_irq_sharing -- Name overload!  "irq" can be either a legacy IRQ
495  * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
496  * from ACPI, which can reach 800 in large boxen.
497  *
498  * Compact the sparse GSI space into a sequential IRQ series and reuse
499  * vectors if possible.
500  */
501 int gsi_irq_sharing(int gsi)
502 {
503         int i, tries, vector;
504
505         BUG_ON(gsi >= NR_IRQ_VECTORS);
506
507         if (platform_legacy_irq(gsi))
508                 return gsi;
509
510         if (gsi_2_irq[gsi] != 0xFF)
511                 return (int)gsi_2_irq[gsi];
512
513         tries = NR_IRQS;
514   try_again:
515         vector = assign_irq_vector(gsi);
516
517         /*
518          * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
519          * use of vector and if found, return that IRQ.  However, we never want
520          * to share legacy IRQs, which usually have a different trigger mode
521          * than PCI.
522          */
523         for (i = 0; i < NR_IRQS; i++)
524                 if (IO_APIC_VECTOR(i) == vector)
525                         break;
526         if (platform_legacy_irq(i)) {
527                 if (--tries >= 0) {
528                         IO_APIC_VECTOR(i) = 0;
529                         goto try_again;
530                 }
531                 panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
532         }
533         if (i < NR_IRQS) {
534                 gsi_2_irq[gsi] = i;
535                 printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
536                                 gsi, vector, i);
537                 return i;
538         }
539
540         i = next_irq++;
541         BUG_ON(i >= NR_IRQS);
542         gsi_2_irq[gsi] = i;
543         IO_APIC_VECTOR(i) = vector;
544         printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
545                         gsi, vector, i);
546         return i;
547 }
548
549 static int pin_2_irq(int idx, int apic, int pin)
550 {
551         int irq, i;
552         int bus = mp_irqs[idx].mpc_srcbus;
553
554         /*
555          * Debugging check, we are in big trouble if this message pops up!
556          */
557         if (mp_irqs[idx].mpc_dstirq != pin)
558                 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
559
560         if (test_bit(bus, mp_bus_not_pci)) {
561                 irq = mp_irqs[idx].mpc_srcbusirq;
562         } else {
563                 /*
564                  * PCI IRQs are mapped in order
565                  */
566                 i = irq = 0;
567                 while (i < apic)
568                         irq += nr_ioapic_registers[i++];
569                 irq += pin;
570                 irq = gsi_irq_sharing(irq);
571         }
572         BUG_ON(irq >= NR_IRQS);
573         return irq;
574 }
575
576 static inline int IO_APIC_irq_trigger(int irq)
577 {
578         int apic, idx, pin;
579
580         for (apic = 0; apic < nr_ioapics; apic++) {
581                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
582                         idx = find_irq_entry(apic,pin,mp_INT);
583                         if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
584                                 return irq_trigger(idx);
585                 }
586         }
587         /*
588          * nonexistent IRQs are edge default
589          */
590         return 0;
591 }
592
593 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
594 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
595
596 static int __assign_irq_vector(int irq)
597 {
598         static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
599         int vector;
600
601         BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
602
603         if (IO_APIC_VECTOR(irq) > 0) {
604                 return IO_APIC_VECTOR(irq);
605         }
606 next:
607         current_vector += 8;
608         if (current_vector == IA32_SYSCALL_VECTOR)
609                 goto next;
610
611         if (current_vector >= FIRST_SYSTEM_VECTOR) {
612                 /* If we run out of vectors on large boxen, must share them. */
613                 offset = (offset + 1) % 8;
614                 current_vector = FIRST_DEVICE_VECTOR + offset;
615         }
616
617         vector = current_vector;
618         IO_APIC_VECTOR(irq) = vector;
619
620         return vector;
621 }
622
623 static int assign_irq_vector(int irq)
624 {
625         int vector;
626         unsigned long flags;
627
628         spin_lock_irqsave(&vector_lock, flags);
629         vector = __assign_irq_vector(irq);
630         spin_unlock_irqrestore(&vector_lock, flags);
631
632         return vector;
633 }
634
635 extern void (*interrupt[NR_IRQS])(void);
636
637 static struct irq_chip ioapic_chip;
638
639 #define IOAPIC_AUTO     -1
640 #define IOAPIC_EDGE     0
641 #define IOAPIC_LEVEL    1
642
643 static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
644 {
645         if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
646                         trigger == IOAPIC_LEVEL)
647                 set_irq_chip_and_handler(irq, &ioapic_chip,
648                                          handle_fasteoi_irq);
649         else
650                 set_irq_chip_and_handler(irq, &ioapic_chip,
651                                          handle_edge_irq);
652         set_intr_gate(vector, interrupt[irq]);
653 }
654
655 static void __init setup_IO_APIC_irqs(void)
656 {
657         struct IO_APIC_route_entry entry;
658         int apic, pin, idx, irq, first_notcon = 1, vector;
659         unsigned long flags;
660
661         apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
662
663         for (apic = 0; apic < nr_ioapics; apic++) {
664         for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
665
666                 /*
667                  * add it to the IO-APIC irq-routing table:
668                  */
669                 memset(&entry,0,sizeof(entry));
670
671                 entry.delivery_mode = INT_DELIVERY_MODE;
672                 entry.dest_mode = INT_DEST_MODE;
673                 entry.mask = 0;                         /* enable IRQ */
674                 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
675
676                 idx = find_irq_entry(apic,pin,mp_INT);
677                 if (idx == -1) {
678                         if (first_notcon) {
679                                 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
680                                 first_notcon = 0;
681                         } else
682                                 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
683                         continue;
684                 }
685
686                 entry.trigger = irq_trigger(idx);
687                 entry.polarity = irq_polarity(idx);
688
689                 if (irq_trigger(idx)) {
690                         entry.trigger = 1;
691                         entry.mask = 1;
692                         entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
693                 }
694
695                 irq = pin_2_irq(idx, apic, pin);
696                 add_pin_to_irq(irq, apic, pin);
697
698                 if (!apic && !IO_APIC_IRQ(irq))
699                         continue;
700
701                 if (IO_APIC_IRQ(irq)) {
702                         vector = assign_irq_vector(irq);
703                         entry.vector = vector;
704
705                         ioapic_register_intr(irq, vector, IOAPIC_AUTO);
706                         if (!apic && (irq < 16))
707                                 disable_8259A_irq(irq);
708                 }
709                 ioapic_write_entry(apic, pin, entry);
710
711                 spin_lock_irqsave(&ioapic_lock, flags);
712                 set_native_irq_info(irq, TARGET_CPUS);
713                 spin_unlock_irqrestore(&ioapic_lock, flags);
714         }
715         }
716
717         if (!first_notcon)
718                 apic_printk(APIC_VERBOSE," not connected.\n");
719 }
720
721 /*
722  * Set up the 8259A-master output pin as broadcast to all
723  * CPUs.
724  */
725 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
726 {
727         struct IO_APIC_route_entry entry;
728         unsigned long flags;
729
730         memset(&entry,0,sizeof(entry));
731
732         disable_8259A_irq(0);
733
734         /* mask LVT0 */
735         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
736
737         /*
738          * We use logical delivery to get the timer IRQ
739          * to the first CPU.
740          */
741         entry.dest_mode = INT_DEST_MODE;
742         entry.mask = 0;                                 /* unmask IRQ now */
743         entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
744         entry.delivery_mode = INT_DELIVERY_MODE;
745         entry.polarity = 0;
746         entry.trigger = 0;
747         entry.vector = vector;
748
749         /*
750          * The timer IRQ doesn't have to know that behind the
751          * scene we have a 8259A-master in AEOI mode ...
752          */
753         set_irq_chip_and_handler(0, &ioapic_chip, handle_edge_irq);
754
755         /*
756          * Add it to the IO-APIC irq-routing table:
757          */
758         spin_lock_irqsave(&ioapic_lock, flags);
759         io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
760         io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
761         spin_unlock_irqrestore(&ioapic_lock, flags);
762
763         enable_8259A_irq(0);
764 }
765
766 void __init UNEXPECTED_IO_APIC(void)
767 {
768 }
769
770 void __apicdebuginit print_IO_APIC(void)
771 {
772         int apic, i;
773         union IO_APIC_reg_00 reg_00;
774         union IO_APIC_reg_01 reg_01;
775         union IO_APIC_reg_02 reg_02;
776         unsigned long flags;
777
778         if (apic_verbosity == APIC_QUIET)
779                 return;
780
781         printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
782         for (i = 0; i < nr_ioapics; i++)
783                 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
784                        mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
785
786         /*
787          * We are a bit conservative about what we expect.  We have to
788          * know about every hardware change ASAP.
789          */
790         printk(KERN_INFO "testing the IO APIC.......................\n");
791
792         for (apic = 0; apic < nr_ioapics; apic++) {
793
794         spin_lock_irqsave(&ioapic_lock, flags);
795         reg_00.raw = io_apic_read(apic, 0);
796         reg_01.raw = io_apic_read(apic, 1);
797         if (reg_01.bits.version >= 0x10)
798                 reg_02.raw = io_apic_read(apic, 2);
799         spin_unlock_irqrestore(&ioapic_lock, flags);
800
801         printk("\n");
802         printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
803         printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
804         printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
805         if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
806                 UNEXPECTED_IO_APIC();
807
808         printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
809         printk(KERN_DEBUG ".......     : max redirection entries: %04X\n", reg_01.bits.entries);
810         if (    (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
811                 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
812                 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
813                 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
814                 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
815                 (reg_01.bits.entries != 0x2E) &&
816                 (reg_01.bits.entries != 0x3F) &&
817                 (reg_01.bits.entries != 0x03) 
818         )
819                 UNEXPECTED_IO_APIC();
820
821         printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
822         printk(KERN_DEBUG ".......     : IO APIC version: %04X\n", reg_01.bits.version);
823         if (    (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
824                 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
825                 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
826                 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
827                 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
828                 (reg_01.bits.version != 0x20)    /* Intel P64H (82806 AA) */
829         )
830                 UNEXPECTED_IO_APIC();
831         if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
832                 UNEXPECTED_IO_APIC();
833
834         if (reg_01.bits.version >= 0x10) {
835                 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
836                 printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
837                 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
838                         UNEXPECTED_IO_APIC();
839         }
840
841         printk(KERN_DEBUG ".... IRQ redirection table:\n");
842
843         printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
844                           " Stat Dest Deli Vect:   \n");
845
846         for (i = 0; i <= reg_01.bits.entries; i++) {
847                 struct IO_APIC_route_entry entry;
848
849                 entry = ioapic_read_entry(apic, i);
850
851                 printk(KERN_DEBUG " %02x %03X %02X  ",
852                         i,
853                         entry.dest.logical.logical_dest,
854                         entry.dest.physical.physical_dest
855                 );
856
857                 printk("%1d    %1d    %1d   %1d   %1d    %1d    %1d    %02X\n",
858                         entry.mask,
859                         entry.trigger,
860                         entry.irr,
861                         entry.polarity,
862                         entry.delivery_status,
863                         entry.dest_mode,
864                         entry.delivery_mode,
865                         entry.vector
866                 );
867         }
868         }
869         printk(KERN_DEBUG "IRQ to pin mappings:\n");
870         for (i = 0; i < NR_IRQS; i++) {
871                 struct irq_pin_list *entry = irq_2_pin + i;
872                 if (entry->pin < 0)
873                         continue;
874                 printk(KERN_DEBUG "IRQ%d ", i);
875                 for (;;) {
876                         printk("-> %d:%d", entry->apic, entry->pin);
877                         if (!entry->next)
878                                 break;
879                         entry = irq_2_pin + entry->next;
880                 }
881                 printk("\n");
882         }
883
884         printk(KERN_INFO ".................................... done.\n");
885
886         return;
887 }
888
889 #if 0
890
891 static __apicdebuginit void print_APIC_bitfield (int base)
892 {
893         unsigned int v;
894         int i, j;
895
896         if (apic_verbosity == APIC_QUIET)
897                 return;
898
899         printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
900         for (i = 0; i < 8; i++) {
901                 v = apic_read(base + i*0x10);
902                 for (j = 0; j < 32; j++) {
903                         if (v & (1<<j))
904                                 printk("1");
905                         else
906                                 printk("0");
907                 }
908                 printk("\n");
909         }
910 }
911
912 void __apicdebuginit print_local_APIC(void * dummy)
913 {
914         unsigned int v, ver, maxlvt;
915
916         if (apic_verbosity == APIC_QUIET)
917                 return;
918
919         printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
920                 smp_processor_id(), hard_smp_processor_id());
921         v = apic_read(APIC_ID);
922         printk(KERN_INFO "... APIC ID:      %08x (%01x)\n", v, GET_APIC_ID(v));
923         v = apic_read(APIC_LVR);
924         printk(KERN_INFO "... APIC VERSION: %08x\n", v);
925         ver = GET_APIC_VERSION(v);
926         maxlvt = get_maxlvt();
927
928         v = apic_read(APIC_TASKPRI);
929         printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
930
931         v = apic_read(APIC_ARBPRI);
932         printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
933                 v & APIC_ARBPRI_MASK);
934         v = apic_read(APIC_PROCPRI);
935         printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
936
937         v = apic_read(APIC_EOI);
938         printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
939         v = apic_read(APIC_RRR);
940         printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
941         v = apic_read(APIC_LDR);
942         printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
943         v = apic_read(APIC_DFR);
944         printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
945         v = apic_read(APIC_SPIV);
946         printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
947
948         printk(KERN_DEBUG "... APIC ISR field:\n");
949         print_APIC_bitfield(APIC_ISR);
950         printk(KERN_DEBUG "... APIC TMR field:\n");
951         print_APIC_bitfield(APIC_TMR);
952         printk(KERN_DEBUG "... APIC IRR field:\n");
953         print_APIC_bitfield(APIC_IRR);
954
955         v = apic_read(APIC_ESR);
956         printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
957
958         v = apic_read(APIC_ICR);
959         printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
960         v = apic_read(APIC_ICR2);
961         printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
962
963         v = apic_read(APIC_LVTT);
964         printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
965
966         if (maxlvt > 3) {                       /* PC is LVT#4. */
967                 v = apic_read(APIC_LVTPC);
968                 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
969         }
970         v = apic_read(APIC_LVT0);
971         printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
972         v = apic_read(APIC_LVT1);
973         printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
974
975         if (maxlvt > 2) {                       /* ERR is LVT#3. */
976                 v = apic_read(APIC_LVTERR);
977                 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
978         }
979
980         v = apic_read(APIC_TMICT);
981         printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
982         v = apic_read(APIC_TMCCT);
983         printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
984         v = apic_read(APIC_TDCR);
985         printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
986         printk("\n");
987 }
988
989 void print_all_local_APICs (void)
990 {
991         on_each_cpu(print_local_APIC, NULL, 1, 1);
992 }
993
994 void __apicdebuginit print_PIC(void)
995 {
996         unsigned int v;
997         unsigned long flags;
998
999         if (apic_verbosity == APIC_QUIET)
1000                 return;
1001
1002         printk(KERN_DEBUG "\nprinting PIC contents\n");
1003
1004         spin_lock_irqsave(&i8259A_lock, flags);
1005
1006         v = inb(0xa1) << 8 | inb(0x21);
1007         printk(KERN_DEBUG "... PIC  IMR: %04x\n", v);
1008
1009         v = inb(0xa0) << 8 | inb(0x20);
1010         printk(KERN_DEBUG "... PIC  IRR: %04x\n", v);
1011
1012         outb(0x0b,0xa0);
1013         outb(0x0b,0x20);
1014         v = inb(0xa0) << 8 | inb(0x20);
1015         outb(0x0a,0xa0);
1016         outb(0x0a,0x20);
1017
1018         spin_unlock_irqrestore(&i8259A_lock, flags);
1019
1020         printk(KERN_DEBUG "... PIC  ISR: %04x\n", v);
1021
1022         v = inb(0x4d1) << 8 | inb(0x4d0);
1023         printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1024 }
1025
1026 #endif  /*  0  */
1027
1028 static void __init enable_IO_APIC(void)
1029 {
1030         union IO_APIC_reg_01 reg_01;
1031         int i8259_apic, i8259_pin;
1032         int i, apic;
1033         unsigned long flags;
1034
1035         for (i = 0; i < PIN_MAP_SIZE; i++) {
1036                 irq_2_pin[i].pin = -1;
1037                 irq_2_pin[i].next = 0;
1038         }
1039
1040         /*
1041          * The number of IO-APIC IRQ registers (== #pins):
1042          */
1043         for (apic = 0; apic < nr_ioapics; apic++) {
1044                 spin_lock_irqsave(&ioapic_lock, flags);
1045                 reg_01.raw = io_apic_read(apic, 1);
1046                 spin_unlock_irqrestore(&ioapic_lock, flags);
1047                 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1048         }
1049         for(apic = 0; apic < nr_ioapics; apic++) {
1050                 int pin;
1051                 /* See if any of the pins is in ExtINT mode */
1052                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1053                         struct IO_APIC_route_entry entry;
1054                         entry = ioapic_read_entry(apic, pin);
1055
1056                         /* If the interrupt line is enabled and in ExtInt mode
1057                          * I have found the pin where the i8259 is connected.
1058                          */
1059                         if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1060                                 ioapic_i8259.apic = apic;
1061                                 ioapic_i8259.pin  = pin;
1062                                 goto found_i8259;
1063                         }
1064                 }
1065         }
1066  found_i8259:
1067         /* Look to see what if the MP table has reported the ExtINT */
1068         i8259_pin  = find_isa_irq_pin(0, mp_ExtINT);
1069         i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1070         /* Trust the MP table if nothing is setup in the hardware */
1071         if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1072                 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1073                 ioapic_i8259.pin  = i8259_pin;
1074                 ioapic_i8259.apic = i8259_apic;
1075         }
1076         /* Complain if the MP table and the hardware disagree */
1077         if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1078                 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1079         {
1080                 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1081         }
1082
1083         /*
1084          * Do not trust the IO-APIC being empty at bootup
1085          */
1086         clear_IO_APIC();
1087 }
1088
1089 /*
1090  * Not an __init, needed by the reboot code
1091  */
1092 void disable_IO_APIC(void)
1093 {
1094         /*
1095          * Clear the IO-APIC before rebooting:
1096          */
1097         clear_IO_APIC();
1098
1099         /*
1100          * If the i8259 is routed through an IOAPIC
1101          * Put that IOAPIC in virtual wire mode
1102          * so legacy interrupts can be delivered.
1103          */
1104         if (ioapic_i8259.pin != -1) {
1105                 struct IO_APIC_route_entry entry;
1106
1107                 memset(&entry, 0, sizeof(entry));
1108                 entry.mask            = 0; /* Enabled */
1109                 entry.trigger         = 0; /* Edge */
1110                 entry.irr             = 0;
1111                 entry.polarity        = 0; /* High */
1112                 entry.delivery_status = 0;
1113                 entry.dest_mode       = 0; /* Physical */
1114                 entry.delivery_mode   = dest_ExtINT; /* ExtInt */
1115                 entry.vector          = 0;
1116                 entry.dest.physical.physical_dest =
1117                                         GET_APIC_ID(apic_read(APIC_ID));
1118
1119                 /*
1120                  * Add it to the IO-APIC irq-routing table:
1121                  */
1122                 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1123         }
1124
1125         disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1126 }
1127
1128 /*
1129  * There is a nasty bug in some older SMP boards, their mptable lies
1130  * about the timer IRQ. We do the following to work around the situation:
1131  *
1132  *      - timer IRQ defaults to IO-APIC IRQ
1133  *      - if this function detects that timer IRQs are defunct, then we fall
1134  *        back to ISA timer IRQs
1135  */
1136 static int __init timer_irq_works(void)
1137 {
1138         unsigned long t1 = jiffies;
1139
1140         local_irq_enable();
1141         /* Let ten ticks pass... */
1142         mdelay((10 * 1000) / HZ);
1143
1144         /*
1145          * Expect a few ticks at least, to be sure some possible
1146          * glue logic does not lock up after one or two first
1147          * ticks in a non-ExtINT mode.  Also the local APIC
1148          * might have cached one ExtINT interrupt.  Finally, at
1149          * least one tick may be lost due to delays.
1150          */
1151
1152         /* jiffies wrap? */
1153         if (jiffies - t1 > 4)
1154                 return 1;
1155         return 0;
1156 }
1157
1158 /*
1159  * In the SMP+IOAPIC case it might happen that there are an unspecified
1160  * number of pending IRQ events unhandled. These cases are very rare,
1161  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1162  * better to do it this way as thus we do not have to be aware of
1163  * 'pending' interrupts in the IRQ path, except at this point.
1164  */
1165 /*
1166  * Edge triggered needs to resend any interrupt
1167  * that was delayed but this is now handled in the device
1168  * independent code.
1169  */
1170
1171 /*
1172  * Starting up a edge-triggered IO-APIC interrupt is
1173  * nasty - we need to make sure that we get the edge.
1174  * If it is already asserted for some reason, we need
1175  * return 1 to indicate that is was pending.
1176  *
1177  * This is not complete - we should be able to fake
1178  * an edge even if it isn't on the 8259A...
1179  */
1180
1181 static unsigned int startup_ioapic_irq(unsigned int irq)
1182 {
1183         int was_pending = 0;
1184         unsigned long flags;
1185
1186         spin_lock_irqsave(&ioapic_lock, flags);
1187         if (irq < 16) {
1188                 disable_8259A_irq(irq);
1189                 if (i8259A_irq_pending(irq))
1190                         was_pending = 1;
1191         }
1192         __unmask_IO_APIC_irq(irq);
1193         spin_unlock_irqrestore(&ioapic_lock, flags);
1194
1195         return was_pending;
1196 }
1197
1198 static int ioapic_retrigger_irq(unsigned int irq)
1199 {
1200         send_IPI_self(IO_APIC_VECTOR(irq));
1201
1202         return 1;
1203 }
1204
1205 /*
1206  * Level and edge triggered IO-APIC interrupts need different handling,
1207  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1208  * handled with the level-triggered descriptor, but that one has slightly
1209  * more overhead. Level-triggered interrupts cannot be handled with the
1210  * edge-triggered handler, without risking IRQ storms and other ugly
1211  * races.
1212  */
1213
1214 static void ack_apic_edge(unsigned int irq)
1215 {
1216         move_native_irq(irq);
1217         ack_APIC_irq();
1218 }
1219
1220 static void ack_apic_level(unsigned int irq)
1221 {
1222         int do_unmask_irq = 0;
1223
1224 #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
1225         /* If we are moving the irq we need to mask it */
1226         if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1227                 do_unmask_irq = 1;
1228                 mask_IO_APIC_irq(irq);
1229         }
1230 #endif
1231
1232         /*
1233          * We must acknowledge the irq before we move it or the acknowledge will
1234          * not propogate properly.
1235          */
1236         ack_APIC_irq();
1237
1238         /* Now we can move and renable the irq */
1239         move_masked_irq(irq);
1240         if (unlikely(do_unmask_irq))
1241                 unmask_IO_APIC_irq(irq);
1242 }
1243
1244 static struct irq_chip ioapic_chip __read_mostly = {
1245         .name           = "IO-APIC",
1246         .startup        = startup_ioapic_irq,
1247         .mask           = mask_IO_APIC_irq,
1248         .unmask         = unmask_IO_APIC_irq,
1249         .ack            = ack_apic_edge,
1250         .eoi            = ack_apic_level,
1251 #ifdef CONFIG_SMP
1252         .set_affinity   = set_ioapic_affinity_irq,
1253 #endif
1254         .retrigger      = ioapic_retrigger_irq,
1255 };
1256
1257 static inline void init_IO_APIC_traps(void)
1258 {
1259         int irq;
1260
1261         /*
1262          * NOTE! The local APIC isn't very good at handling
1263          * multiple interrupts at the same interrupt level.
1264          * As the interrupt level is determined by taking the
1265          * vector number and shifting that right by 4, we
1266          * want to spread these out a bit so that they don't
1267          * all fall in the same interrupt level.
1268          *
1269          * Also, we've got to be careful not to trash gate
1270          * 0x80, because int 0x80 is hm, kind of importantish. ;)
1271          */
1272         for (irq = 0; irq < NR_IRQS ; irq++) {
1273                 int tmp = irq;
1274                 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1275                         /*
1276                          * Hmm.. We don't have an entry for this,
1277                          * so default to an old-fashioned 8259
1278                          * interrupt if we can..
1279                          */
1280                         if (irq < 16)
1281                                 make_8259A_irq(irq);
1282                         else
1283                                 /* Strange. Oh, well.. */
1284                                 irq_desc[irq].chip = &no_irq_chip;
1285                 }
1286         }
1287 }
1288
1289 static void enable_lapic_irq (unsigned int irq)
1290 {
1291         unsigned long v;
1292
1293         v = apic_read(APIC_LVT0);
1294         apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1295 }
1296
1297 static void disable_lapic_irq (unsigned int irq)
1298 {
1299         unsigned long v;
1300
1301         v = apic_read(APIC_LVT0);
1302         apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1303 }
1304
1305 static void ack_lapic_irq (unsigned int irq)
1306 {
1307         ack_APIC_irq();
1308 }
1309
1310 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1311
1312 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1313         .typename = "local-APIC-edge",
1314         .startup = NULL, /* startup_irq() not used for IRQ0 */
1315         .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1316         .enable = enable_lapic_irq,
1317         .disable = disable_lapic_irq,
1318         .ack = ack_lapic_irq,
1319         .end = end_lapic_irq,
1320 };
1321
1322 static void setup_nmi (void)
1323 {
1324         /*
1325          * Dirty trick to enable the NMI watchdog ...
1326          * We put the 8259A master into AEOI mode and
1327          * unmask on all local APICs LVT0 as NMI.
1328          *
1329          * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1330          * is from Maciej W. Rozycki - so we do not have to EOI from
1331          * the NMI handler or the timer interrupt.
1332          */ 
1333         printk(KERN_INFO "activating NMI Watchdog ...");
1334
1335         enable_NMI_through_LVT0(NULL);
1336
1337         printk(" done.\n");
1338 }
1339
1340 /*
1341  * This looks a bit hackish but it's about the only one way of sending
1342  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
1343  * not support the ExtINT mode, unfortunately.  We need to send these
1344  * cycles as some i82489DX-based boards have glue logic that keeps the
1345  * 8259A interrupt line asserted until INTA.  --macro
1346  */
1347 static inline void unlock_ExtINT_logic(void)
1348 {
1349         int apic, pin, i;
1350         struct IO_APIC_route_entry entry0, entry1;
1351         unsigned char save_control, save_freq_select;
1352         unsigned long flags;
1353
1354         pin  = find_isa_irq_pin(8, mp_INT);
1355         apic = find_isa_irq_apic(8, mp_INT);
1356         if (pin == -1)
1357                 return;
1358
1359         spin_lock_irqsave(&ioapic_lock, flags);
1360         *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1361         *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1362         spin_unlock_irqrestore(&ioapic_lock, flags);
1363         clear_IO_APIC_pin(apic, pin);
1364
1365         memset(&entry1, 0, sizeof(entry1));
1366
1367         entry1.dest_mode = 0;                   /* physical delivery */
1368         entry1.mask = 0;                        /* unmask IRQ now */
1369         entry1.dest.physical.physical_dest = hard_smp_processor_id();
1370         entry1.delivery_mode = dest_ExtINT;
1371         entry1.polarity = entry0.polarity;
1372         entry1.trigger = 0;
1373         entry1.vector = 0;
1374
1375         spin_lock_irqsave(&ioapic_lock, flags);
1376         io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1377         io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1378         spin_unlock_irqrestore(&ioapic_lock, flags);
1379
1380         save_control = CMOS_READ(RTC_CONTROL);
1381         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1382         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1383                    RTC_FREQ_SELECT);
1384         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1385
1386         i = 100;
1387         while (i-- > 0) {
1388                 mdelay(10);
1389                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1390                         i -= 10;
1391         }
1392
1393         CMOS_WRITE(save_control, RTC_CONTROL);
1394         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1395         clear_IO_APIC_pin(apic, pin);
1396
1397         spin_lock_irqsave(&ioapic_lock, flags);
1398         io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1399         io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1400         spin_unlock_irqrestore(&ioapic_lock, flags);
1401 }
1402
1403 int timer_uses_ioapic_pin_0;
1404
1405 /*
1406  * This code may look a bit paranoid, but it's supposed to cooperate with
1407  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
1408  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
1409  * fanatically on his truly buggy board.
1410  *
1411  * FIXME: really need to revamp this for modern platforms only.
1412  */
1413 static inline void check_timer(void)
1414 {
1415         int apic1, pin1, apic2, pin2;
1416         int vector;
1417
1418         /*
1419          * get/set the timer IRQ vector:
1420          */
1421         disable_8259A_irq(0);
1422         vector = assign_irq_vector(0);
1423         set_intr_gate(vector, interrupt[0]);
1424
1425         /*
1426          * Subtle, code in do_timer_interrupt() expects an AEOI
1427          * mode for the 8259A whenever interrupts are routed
1428          * through I/O APICs.  Also IRQ0 has to be enabled in
1429          * the 8259A which implies the virtual wire has to be
1430          * disabled in the local APIC.
1431          */
1432         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1433         init_8259A(1);
1434         if (timer_over_8254 > 0)
1435                 enable_8259A_irq(0);
1436
1437         pin1  = find_isa_irq_pin(0, mp_INT);
1438         apic1 = find_isa_irq_apic(0, mp_INT);
1439         pin2  = ioapic_i8259.pin;
1440         apic2 = ioapic_i8259.apic;
1441
1442         if (pin1 == 0)
1443                 timer_uses_ioapic_pin_0 = 1;
1444
1445         apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1446                 vector, apic1, pin1, apic2, pin2);
1447
1448         if (pin1 != -1) {
1449                 /*
1450                  * Ok, does IRQ0 through the IOAPIC work?
1451                  */
1452                 unmask_IO_APIC_irq(0);
1453                 if (!no_timer_check && timer_irq_works()) {
1454                         nmi_watchdog_default();
1455                         if (nmi_watchdog == NMI_IO_APIC) {
1456                                 disable_8259A_irq(0);
1457                                 setup_nmi();
1458                                 enable_8259A_irq(0);
1459                         }
1460                         if (disable_timer_pin_1 > 0)
1461                                 clear_IO_APIC_pin(0, pin1);
1462                         return;
1463                 }
1464                 clear_IO_APIC_pin(apic1, pin1);
1465                 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1466                                 "connected to IO-APIC\n");
1467         }
1468
1469         apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1470                                 "through the 8259A ... ");
1471         if (pin2 != -1) {
1472                 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1473                         apic2, pin2);
1474                 /*
1475                  * legacy devices should be connected to IO APIC #0
1476                  */
1477                 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1478                 if (timer_irq_works()) {
1479                         apic_printk(APIC_VERBOSE," works.\n");
1480                         nmi_watchdog_default();
1481                         if (nmi_watchdog == NMI_IO_APIC) {
1482                                 setup_nmi();
1483                         }
1484                         return;
1485                 }
1486                 /*
1487                  * Cleanup, just in case ...
1488                  */
1489                 clear_IO_APIC_pin(apic2, pin2);
1490         }
1491         apic_printk(APIC_VERBOSE," failed.\n");
1492
1493         if (nmi_watchdog == NMI_IO_APIC) {
1494                 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1495                 nmi_watchdog = 0;
1496         }
1497
1498         apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1499
1500         disable_8259A_irq(0);
1501         irq_desc[0].chip = &lapic_irq_type;
1502         apic_write(APIC_LVT0, APIC_DM_FIXED | vector);  /* Fixed mode */
1503         enable_8259A_irq(0);
1504
1505         if (timer_irq_works()) {
1506                 apic_printk(APIC_VERBOSE," works.\n");
1507                 return;
1508         }
1509         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1510         apic_printk(APIC_VERBOSE," failed.\n");
1511
1512         apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1513
1514         init_8259A(0);
1515         make_8259A_irq(0);
1516         apic_write(APIC_LVT0, APIC_DM_EXTINT);
1517
1518         unlock_ExtINT_logic();
1519
1520         if (timer_irq_works()) {
1521                 apic_printk(APIC_VERBOSE," works.\n");
1522                 return;
1523         }
1524         apic_printk(APIC_VERBOSE," failed :(.\n");
1525         panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1526 }
1527
1528 static int __init notimercheck(char *s)
1529 {
1530         no_timer_check = 1;
1531         return 1;
1532 }
1533 __setup("no_timer_check", notimercheck);
1534
1535 /*
1536  *
1537  * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1538  * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1539  *   Linux doesn't really care, as it's not actually used
1540  *   for any interrupt handling anyway.
1541  */
1542 #define PIC_IRQS        (1<<2)
1543
1544 void __init setup_IO_APIC(void)
1545 {
1546         enable_IO_APIC();
1547
1548         if (acpi_ioapic)
1549                 io_apic_irqs = ~0;      /* all IRQs go through IOAPIC */
1550         else
1551                 io_apic_irqs = ~PIC_IRQS;
1552
1553         apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1554
1555         sync_Arb_IDs();
1556         setup_IO_APIC_irqs();
1557         init_IO_APIC_traps();
1558         check_timer();
1559         if (!acpi_ioapic)
1560                 print_IO_APIC();
1561 }
1562
1563 struct sysfs_ioapic_data {
1564         struct sys_device dev;
1565         struct IO_APIC_route_entry entry[0];
1566 };
1567 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1568
1569 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1570 {
1571         struct IO_APIC_route_entry *entry;
1572         struct sysfs_ioapic_data *data;
1573         int i;
1574
1575         data = container_of(dev, struct sysfs_ioapic_data, dev);
1576         entry = data->entry;
1577         for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
1578                 *entry = ioapic_read_entry(dev->id, i);
1579
1580         return 0;
1581 }
1582
1583 static int ioapic_resume(struct sys_device *dev)
1584 {
1585         struct IO_APIC_route_entry *entry;
1586         struct sysfs_ioapic_data *data;
1587         unsigned long flags;
1588         union IO_APIC_reg_00 reg_00;
1589         int i;
1590
1591         data = container_of(dev, struct sysfs_ioapic_data, dev);
1592         entry = data->entry;
1593
1594         spin_lock_irqsave(&ioapic_lock, flags);
1595         reg_00.raw = io_apic_read(dev->id, 0);
1596         if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1597                 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1598                 io_apic_write(dev->id, 0, reg_00.raw);
1599         }
1600         spin_unlock_irqrestore(&ioapic_lock, flags);
1601         for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
1602                 ioapic_write_entry(dev->id, i, entry[i]);
1603
1604         return 0;
1605 }
1606
1607 static struct sysdev_class ioapic_sysdev_class = {
1608         set_kset_name("ioapic"),
1609         .suspend = ioapic_suspend,
1610         .resume = ioapic_resume,
1611 };
1612
1613 static int __init ioapic_init_sysfs(void)
1614 {
1615         struct sys_device * dev;
1616         int i, size, error = 0;
1617
1618         error = sysdev_class_register(&ioapic_sysdev_class);
1619         if (error)
1620                 return error;
1621
1622         for (i = 0; i < nr_ioapics; i++ ) {
1623                 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1624                         * sizeof(struct IO_APIC_route_entry);
1625                 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1626                 if (!mp_ioapic_data[i]) {
1627                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1628                         continue;
1629                 }
1630                 memset(mp_ioapic_data[i], 0, size);
1631                 dev = &mp_ioapic_data[i]->dev;
1632                 dev->id = i;
1633                 dev->cls = &ioapic_sysdev_class;
1634                 error = sysdev_register(dev);
1635                 if (error) {
1636                         kfree(mp_ioapic_data[i]);
1637                         mp_ioapic_data[i] = NULL;
1638                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1639                         continue;
1640                 }
1641         }
1642
1643         return 0;
1644 }
1645
1646 device_initcall(ioapic_init_sysfs);
1647
1648 /*
1649  * Dynamic irq allocate and deallocation
1650  */
1651 int create_irq(void)
1652 {
1653         /* Allocate an unused irq */
1654         int irq;
1655         int new;
1656         int vector = 0;
1657         unsigned long flags;
1658
1659         irq = -ENOSPC;
1660         spin_lock_irqsave(&vector_lock, flags);
1661         for (new = (NR_IRQS - 1); new >= 0; new--) {
1662                 if (platform_legacy_irq(new))
1663                         continue;
1664                 if (irq_vector[new] != 0)
1665                         continue;
1666                 vector = __assign_irq_vector(new);
1667                 if (likely(vector > 0))
1668                         irq = new;
1669                 break;
1670         }
1671         spin_unlock_irqrestore(&vector_lock, flags);
1672
1673         if (irq >= 0) {
1674                 set_intr_gate(vector, interrupt[irq]);
1675                 dynamic_irq_init(irq);
1676         }
1677         return irq;
1678 }
1679
1680 void destroy_irq(unsigned int irq)
1681 {
1682         unsigned long flags;
1683
1684         dynamic_irq_cleanup(irq);
1685
1686         spin_lock_irqsave(&vector_lock, flags);
1687         irq_vector[irq] = 0;
1688         spin_unlock_irqrestore(&vector_lock, flags);
1689 }
1690
1691 /*
1692  * MSI mesage composition
1693  */
1694 #ifdef CONFIG_PCI_MSI
1695 static int msi_msg_setup(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
1696 {
1697         /* For now always this code always uses physical delivery
1698          * mode.
1699          */
1700         int vector;
1701         unsigned dest;
1702
1703         vector = assign_irq_vector(irq);
1704         if (vector >= 0) {
1705                 cpumask_t tmp;
1706
1707                 cpus_clear(tmp);
1708                 cpu_set(first_cpu(cpu_online_map), tmp);
1709                 dest = cpu_mask_to_apicid(tmp);
1710
1711                 msg->address_hi = MSI_ADDR_BASE_HI;
1712                 msg->address_lo =
1713                         MSI_ADDR_BASE_LO |
1714                         ((INT_DEST_MODE == 0) ?
1715                                 MSI_ADDR_DEST_MODE_PHYSICAL:
1716                                 MSI_ADDR_DEST_MODE_LOGICAL) |
1717                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1718                                 MSI_ADDR_REDIRECTION_CPU:
1719                                 MSI_ADDR_REDIRECTION_LOWPRI) |
1720                         MSI_ADDR_DEST_ID(dest);
1721
1722                 msg->data =
1723                         MSI_DATA_TRIGGER_EDGE |
1724                         MSI_DATA_LEVEL_ASSERT |
1725                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1726                                 MSI_DATA_DELIVERY_FIXED:
1727                                 MSI_DATA_DELIVERY_LOWPRI) |
1728                         MSI_DATA_VECTOR(vector);
1729         }
1730         return vector;
1731 }
1732
1733 static void msi_msg_teardown(unsigned int irq)
1734 {
1735         return;
1736 }
1737
1738 static void msi_msg_set_affinity(unsigned int irq, cpumask_t mask, struct msi_msg *msg)
1739 {
1740         int vector;
1741         unsigned dest;
1742
1743         vector = assign_irq_vector(irq);
1744         if (vector > 0) {
1745                 dest = cpu_mask_to_apicid(mask);
1746
1747                 msg->data &= ~MSI_DATA_VECTOR_MASK;
1748                 msg->data |= MSI_DATA_VECTOR(vector);
1749                 msg->address_lo &= ~MSI_ADDR_DEST_ID_MASK;
1750                 msg->address_lo |= MSI_ADDR_DEST_ID(dest);
1751         }
1752 }
1753
1754 struct msi_ops arch_msi_ops = {
1755         .needs_64bit_address = 0,
1756         .setup = msi_msg_setup,
1757         .teardown = msi_msg_teardown,
1758         .target = msi_msg_set_affinity,
1759 };
1760
1761 #endif
1762
1763 /* --------------------------------------------------------------------------
1764                           ACPI-based IOAPIC Configuration
1765    -------------------------------------------------------------------------- */
1766
1767 #ifdef CONFIG_ACPI
1768
1769 #define IO_APIC_MAX_ID          0xFE
1770
1771 int __init io_apic_get_redir_entries (int ioapic)
1772 {
1773         union IO_APIC_reg_01    reg_01;
1774         unsigned long flags;
1775
1776         spin_lock_irqsave(&ioapic_lock, flags);
1777         reg_01.raw = io_apic_read(ioapic, 1);
1778         spin_unlock_irqrestore(&ioapic_lock, flags);
1779
1780         return reg_01.bits.entries;
1781 }
1782
1783
1784 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
1785 {
1786         struct IO_APIC_route_entry entry;
1787         unsigned long flags;
1788
1789         if (!IO_APIC_IRQ(irq)) {
1790                 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1791                         ioapic);
1792                 return -EINVAL;
1793         }
1794
1795         /*
1796          * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1797          * Note that we mask (disable) IRQs now -- these get enabled when the
1798          * corresponding device driver registers for this IRQ.
1799          */
1800
1801         memset(&entry,0,sizeof(entry));
1802
1803         entry.delivery_mode = INT_DELIVERY_MODE;
1804         entry.dest_mode = INT_DEST_MODE;
1805         entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1806         entry.trigger = triggering;
1807         entry.polarity = polarity;
1808         entry.mask = 1;                                  /* Disabled (masked) */
1809
1810         irq = gsi_irq_sharing(irq);
1811         /*
1812          * IRQs < 16 are already in the irq_2_pin[] map
1813          */
1814         if (irq >= 16)
1815                 add_pin_to_irq(irq, ioapic, pin);
1816
1817         entry.vector = assign_irq_vector(irq);
1818
1819         apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1820                 "IRQ %d Mode:%i Active:%i)\n", ioapic, 
1821                mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
1822                triggering, polarity);
1823
1824         ioapic_register_intr(irq, entry.vector, triggering);
1825
1826         if (!ioapic && (irq < 16))
1827                 disable_8259A_irq(irq);
1828
1829         ioapic_write_entry(ioapic, pin, entry);
1830
1831         spin_lock_irqsave(&ioapic_lock, flags);
1832         set_native_irq_info(irq, TARGET_CPUS);
1833         spin_unlock_irqrestore(&ioapic_lock, flags);
1834
1835         return 0;
1836 }
1837
1838 #endif /* CONFIG_ACPI */
1839
1840
1841 /*
1842  * This function currently is only a helper for the i386 smp boot process where
1843  * we need to reprogram the ioredtbls to cater for the cpus which have come online
1844  * so mask in all cases should simply be TARGET_CPUS
1845  */
1846 #ifdef CONFIG_SMP
1847 void __init setup_ioapic_dest(void)
1848 {
1849         int pin, ioapic, irq, irq_entry;
1850
1851         if (skip_ioapic_setup == 1)
1852                 return;
1853
1854         for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
1855                 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
1856                         irq_entry = find_irq_entry(ioapic, pin, mp_INT);
1857                         if (irq_entry == -1)
1858                                 continue;
1859                         irq = pin_2_irq(irq_entry, ioapic, pin);
1860                         set_ioapic_affinity_irq(irq, TARGET_CPUS);
1861                 }
1862
1863         }
1864 }
1865 #endif