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