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