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