]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/io_apic.c
x86: io-apic - interrupt remapping fix
[linux-2.6-omap-h63xx.git] / arch / x86 / 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/pci.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/compiler.h>
31 #include <linux/acpi.h>
32 #include <linux/module.h>
33 #include <linux/sysdev.h>
34 #include <linux/msi.h>
35 #include <linux/htirq.h>
36 #include <linux/freezer.h>
37 #include <linux/kthread.h>
38 #include <linux/jiffies.h>      /* time_after() */
39 #ifdef CONFIG_ACPI
40 #include <acpi/acpi_bus.h>
41 #endif
42 #include <linux/bootmem.h>
43 #include <linux/dmar.h>
44 #include <linux/hpet.h>
45
46 #include <asm/idle.h>
47 #include <asm/io.h>
48 #include <asm/smp.h>
49 #include <asm/desc.h>
50 #include <asm/proto.h>
51 #include <asm/acpi.h>
52 #include <asm/dma.h>
53 #include <asm/timer.h>
54 #include <asm/i8259.h>
55 #include <asm/nmi.h>
56 #include <asm/msidef.h>
57 #include <asm/hypertransport.h>
58 #include <asm/setup.h>
59 #include <asm/irq_remapping.h>
60 #include <asm/hpet.h>
61
62 #include <mach_ipi.h>
63 #include <mach_apic.h>
64 #include <mach_apicdef.h>
65
66 #define __apicdebuginit(type) static type __init
67
68 /*
69  *      Is the SiS APIC rmw bug present ?
70  *      -1 = don't know, 0 = no, 1 = yes
71  */
72 int sis_apic_bug = -1;
73
74 static DEFINE_SPINLOCK(ioapic_lock);
75 static DEFINE_SPINLOCK(vector_lock);
76
77 /*
78  * # of IRQ routing registers
79  */
80 int nr_ioapic_registers[MAX_IO_APICS];
81
82 /* I/O APIC entries */
83 struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
84 int nr_ioapics;
85
86 /* MP IRQ source entries */
87 struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
88
89 /* # of MP IRQ source entries */
90 int mp_irq_entries;
91
92 #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
93 int mp_bus_id_to_type[MAX_MP_BUSSES];
94 #endif
95
96 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
97
98 int skip_ioapic_setup;
99
100 static int __init parse_noapic(char *str)
101 {
102         /* disable IO-APIC */
103         disable_ioapic_setup();
104         return 0;
105 }
106 early_param("noapic", parse_noapic);
107
108 struct irq_cfg;
109 struct irq_pin_list;
110 struct irq_cfg {
111         unsigned int irq;
112 #ifdef CONFIG_HAVE_SPARSE_IRQ
113         struct irq_cfg *next;
114 #endif
115         struct irq_pin_list *irq_2_pin;
116         cpumask_t domain;
117         cpumask_t old_domain;
118         unsigned move_cleanup_count;
119         u8 vector;
120         u8 move_in_progress : 1;
121 };
122
123 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
124 static struct irq_cfg irq_cfg_legacy[] __initdata = {
125         [0]  = { .irq =  0, .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR,  },
126         [1]  = { .irq =  1, .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR,  },
127         [2]  = { .irq =  2, .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR,  },
128         [3]  = { .irq =  3, .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR,  },
129         [4]  = { .irq =  4, .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR,  },
130         [5]  = { .irq =  5, .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR,  },
131         [6]  = { .irq =  6, .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR,  },
132         [7]  = { .irq =  7, .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR,  },
133         [8]  = { .irq =  8, .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR,  },
134         [9]  = { .irq =  9, .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR,  },
135         [10] = { .irq = 10, .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
136         [11] = { .irq = 11, .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
137         [12] = { .irq = 12, .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
138         [13] = { .irq = 13, .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
139         [14] = { .irq = 14, .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
140         [15] = { .irq = 15, .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
141 };
142
143 static struct irq_cfg irq_cfg_init = { .irq =  -1U, };
144
145 static void init_one_irq_cfg(struct irq_cfg *cfg)
146 {
147         memcpy(cfg, &irq_cfg_init, sizeof(struct irq_cfg));
148 }
149
150 static struct irq_cfg *irq_cfgx;
151
152 #ifdef CONFIG_HAVE_SPARSE_IRQ
153 /*
154  * Protect the irq_cfgx_free freelist:
155  */
156 static DEFINE_SPINLOCK(irq_cfg_lock);
157
158 static struct irq_cfg *irq_cfgx_free;
159 #endif
160
161 static void __init init_work(void *data)
162 {
163         struct dyn_array *da = data;
164         struct irq_cfg *cfg;
165         int legacy_count;
166         int i;
167
168         cfg = *da->name;
169
170         memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
171
172         legacy_count = ARRAY_SIZE(irq_cfg_legacy);
173         for (i = legacy_count; i < *da->nr; i++)
174                 init_one_irq_cfg(&cfg[i]);
175
176 #ifdef CONFIG_HAVE_SPARSE_IRQ
177         for (i = 1; i < *da->nr; i++)
178                 cfg[i-1].next = &cfg[i];
179
180         irq_cfgx_free = &irq_cfgx[legacy_count];
181         irq_cfgx[legacy_count - 1].next = NULL;
182 #endif
183 }
184
185 #ifdef CONFIG_HAVE_SPARSE_IRQ
186 /* need to be biger than size of irq_cfg_legacy */
187 static int nr_irq_cfg = 32;
188
189 static int __init parse_nr_irq_cfg(char *arg)
190 {
191         if (arg) {
192                 nr_irq_cfg = simple_strtoul(arg, NULL, 0);
193                 if (nr_irq_cfg < 32)
194                         nr_irq_cfg = 32;
195         }
196         return 0;
197 }
198
199 early_param("nr_irq_cfg", parse_nr_irq_cfg);
200
201 #define for_each_irq_cfg(irqX, cfg)           \
202         for (cfg = irq_cfgx, irqX = cfg->irq; cfg; cfg = cfg->next, irqX = cfg ? cfg->irq : -1U)
203
204
205 DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irq_cfg, PAGE_SIZE, init_work);
206
207 static struct irq_cfg *irq_cfg(unsigned int irq)
208 {
209         struct irq_cfg *cfg;
210
211         cfg = irq_cfgx;
212         while (cfg) {
213                 if (cfg->irq == irq)
214                         return cfg;
215
216                 cfg = cfg->next;
217         }
218
219         return NULL;
220 }
221
222 static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
223 {
224         struct irq_cfg *cfg, *cfg_pri;
225         unsigned long flags;
226         int count = 0;
227         int i;
228
229         cfg_pri = cfg = irq_cfgx;
230         while (cfg) {
231                 if (cfg->irq == irq)
232                         return cfg;
233
234                 cfg_pri = cfg;
235                 cfg = cfg->next;
236                 count++;
237         }
238
239         spin_lock_irqsave(&irq_cfg_lock, flags);
240         if (!irq_cfgx_free) {
241                 unsigned long phys;
242                 unsigned long total_bytes;
243                 /*
244                  *  we run out of pre-allocate ones, allocate more
245                  */
246                 printk(KERN_DEBUG "try to get more irq_cfg %d\n", nr_irq_cfg);
247
248                 total_bytes = sizeof(struct irq_cfg) * nr_irq_cfg;
249                 if (after_bootmem)
250                         cfg = kzalloc(total_bytes, GFP_ATOMIC);
251                 else
252                         cfg = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
253
254                 if (!cfg)
255                         panic("please boot with nr_irq_cfg= %d\n", count * 2);
256
257                 phys = __pa(cfg);
258                 printk(KERN_DEBUG "irq_irq ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
259
260                 for (i = 0; i < nr_irq_cfg; i++)
261                         init_one_irq_cfg(&cfg[i]);
262
263                 for (i = 1; i < nr_irq_cfg; i++)
264                         cfg[i-1].next = &cfg[i];
265
266                 irq_cfgx_free = cfg;
267         }
268
269         cfg = irq_cfgx_free;
270         irq_cfgx_free = irq_cfgx_free->next;
271         cfg->next = NULL;
272         if (cfg_pri)
273                 cfg_pri->next = cfg;
274         else
275                 irq_cfgx = cfg;
276         cfg->irq = irq;
277
278         spin_unlock_irqrestore(&irq_cfg_lock, flags);
279
280         return cfg;
281 }
282 #else
283
284 #define for_each_irq_cfg(irq, cfg)              \
285         for (irq = 0, cfg = &irq_cfgx[irq]; irq < nr_irqs; irq++, cfg = &irq_cfgx[irq])
286
287 DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irqs, PAGE_SIZE, init_work);
288
289 struct irq_cfg *irq_cfg(unsigned int irq)
290 {
291         if (irq < nr_irqs)
292                 return &irq_cfgx[irq];
293
294         return NULL;
295 }
296 struct irq_cfg *irq_cfg_alloc(unsigned int irq)
297 {
298         return irq_cfg(irq);
299 }
300
301 #endif
302 /*
303  * This is performance-critical, we want to do it O(1)
304  *
305  * the indexing order of this array favors 1:1 mappings
306  * between pins and IRQs.
307  */
308
309 struct irq_pin_list {
310         int apic, pin;
311         struct irq_pin_list *next;
312 };
313
314 static struct irq_pin_list *irq_2_pin_head;
315 /* fill one page ? */
316 static int nr_irq_2_pin = 0x100;
317 static struct irq_pin_list *irq_2_pin_ptr;
318 static void __init irq_2_pin_init_work(void *data)
319 {
320         struct dyn_array *da = data;
321         struct irq_pin_list *pin;
322         int i;
323
324         pin = *da->name;
325
326         for (i = 1; i < *da->nr; i++)
327                 pin[i-1].next = &pin[i];
328
329         irq_2_pin_ptr = &pin[0];
330 }
331 DEFINE_DYN_ARRAY(irq_2_pin_head, sizeof(struct irq_pin_list), nr_irq_2_pin, PAGE_SIZE, irq_2_pin_init_work);
332
333 static struct irq_pin_list *get_one_free_irq_2_pin(void)
334 {
335         struct irq_pin_list *pin;
336         int i;
337
338         pin = irq_2_pin_ptr;
339
340         if (pin) {
341                 irq_2_pin_ptr = pin->next;
342                 pin->next = NULL;
343                 return pin;
344         }
345
346         /*
347          *  we run out of pre-allocate ones, allocate more
348          */
349         printk(KERN_DEBUG "try to get more irq_2_pin %d\n", nr_irq_2_pin);
350
351         if (after_bootmem)
352                 pin = kzalloc(sizeof(struct irq_pin_list)*nr_irq_2_pin,
353                                  GFP_ATOMIC);
354         else
355                 pin = __alloc_bootmem_nopanic(sizeof(struct irq_pin_list) *
356                                 nr_irq_2_pin, PAGE_SIZE, 0);
357
358         if (!pin)
359                 panic("can not get more irq_2_pin\n");
360
361         for (i = 1; i < nr_irq_2_pin; i++)
362                 pin[i-1].next = &pin[i];
363
364         irq_2_pin_ptr = pin->next;
365         pin->next = NULL;
366
367         return pin;
368 }
369
370 struct io_apic {
371         unsigned int index;
372         unsigned int unused[3];
373         unsigned int data;
374 };
375
376 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
377 {
378         return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
379                 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
380 }
381
382 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
383 {
384         struct io_apic __iomem *io_apic = io_apic_base(apic);
385         writel(reg, &io_apic->index);
386         return readl(&io_apic->data);
387 }
388
389 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
390 {
391         struct io_apic __iomem *io_apic = io_apic_base(apic);
392         writel(reg, &io_apic->index);
393         writel(value, &io_apic->data);
394 }
395
396 /*
397  * Re-write a value: to be used for read-modify-write
398  * cycles where the read already set up the index register.
399  *
400  * Older SiS APIC requires we rewrite the index register
401  */
402 static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
403 {
404         struct io_apic __iomem *io_apic = io_apic_base(apic);
405         if (sis_apic_bug)
406                 writel(reg, &io_apic->index);
407         writel(value, &io_apic->data);
408 }
409
410 static bool io_apic_level_ack_pending(unsigned int irq)
411 {
412         struct irq_pin_list *entry;
413         unsigned long flags;
414         struct irq_cfg *cfg = irq_cfg(irq);
415
416         spin_lock_irqsave(&ioapic_lock, flags);
417         entry = cfg->irq_2_pin;
418         for (;;) {
419                 unsigned int reg;
420                 int pin;
421
422                 if (!entry)
423                         break;
424                 pin = entry->pin;
425                 reg = io_apic_read(entry->apic, 0x10 + pin*2);
426                 /* Is the remote IRR bit set? */
427                 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
428                         spin_unlock_irqrestore(&ioapic_lock, flags);
429                         return true;
430                 }
431                 if (!entry->next)
432                         break;
433                 entry = entry->next;
434         }
435         spin_unlock_irqrestore(&ioapic_lock, flags);
436
437         return false;
438 }
439
440 union entry_union {
441         struct { u32 w1, w2; };
442         struct IO_APIC_route_entry entry;
443 };
444
445 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
446 {
447         union entry_union eu;
448         unsigned long flags;
449         spin_lock_irqsave(&ioapic_lock, flags);
450         eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
451         eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
452         spin_unlock_irqrestore(&ioapic_lock, flags);
453         return eu.entry;
454 }
455
456 /*
457  * When we write a new IO APIC routing entry, we need to write the high
458  * word first! If the mask bit in the low word is clear, we will enable
459  * the interrupt, and we need to make sure the entry is fully populated
460  * before that happens.
461  */
462 static void
463 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
464 {
465         union entry_union eu;
466         eu.entry = e;
467         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
468         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
469 }
470
471 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
472 {
473         unsigned long flags;
474         spin_lock_irqsave(&ioapic_lock, flags);
475         __ioapic_write_entry(apic, pin, e);
476         spin_unlock_irqrestore(&ioapic_lock, flags);
477 }
478
479 /*
480  * When we mask an IO APIC routing entry, we need to write the low
481  * word first, in order to set the mask bit before we change the
482  * high bits!
483  */
484 static void ioapic_mask_entry(int apic, int pin)
485 {
486         unsigned long flags;
487         union entry_union eu = { .entry.mask = 1 };
488
489         spin_lock_irqsave(&ioapic_lock, flags);
490         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
491         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
492         spin_unlock_irqrestore(&ioapic_lock, flags);
493 }
494
495 #ifdef CONFIG_SMP
496 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
497 {
498         int apic, pin;
499         struct irq_cfg *cfg;
500         struct irq_pin_list *entry;
501
502         cfg = irq_cfg(irq);
503         entry = cfg->irq_2_pin;
504         for (;;) {
505                 unsigned int reg;
506
507                 if (!entry)
508                         break;
509
510                 apic = entry->apic;
511                 pin = entry->pin;
512 #ifdef CONFIG_INTR_REMAP
513                 /*
514                  * With interrupt-remapping, destination information comes
515                  * from interrupt-remapping table entry.
516                  */
517                 if (!irq_remapped(irq))
518                         io_apic_write(apic, 0x11 + pin*2, dest);
519 #else
520                 io_apic_write(apic, 0x11 + pin*2, dest);
521 #endif
522                 reg = io_apic_read(apic, 0x10 + pin*2);
523                 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
524                 reg |= vector;
525                 io_apic_modify(apic, 0x10 + pin*2, reg);
526                 if (!entry->next)
527                         break;
528                 entry = entry->next;
529         }
530 }
531
532 static int assign_irq_vector(int irq, cpumask_t mask);
533
534 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
535 {
536         struct irq_cfg *cfg;
537         unsigned long flags;
538         unsigned int dest;
539         cpumask_t tmp;
540         struct irq_desc *desc;
541
542         cpus_and(tmp, mask, cpu_online_map);
543         if (cpus_empty(tmp))
544                 return;
545
546         cfg = irq_cfg(irq);
547         if (assign_irq_vector(irq, mask))
548                 return;
549
550         cpus_and(tmp, cfg->domain, mask);
551         dest = cpu_mask_to_apicid(tmp);
552         /*
553          * Only the high 8 bits are valid.
554          */
555         dest = SET_APIC_LOGICAL_ID(dest);
556
557         desc = irq_to_desc(irq);
558         spin_lock_irqsave(&ioapic_lock, flags);
559         __target_IO_APIC_irq(irq, dest, cfg->vector);
560         desc->affinity = mask;
561         spin_unlock_irqrestore(&ioapic_lock, flags);
562 }
563 #endif /* CONFIG_SMP */
564
565 /*
566  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
567  * shared ISA-space IRQs, so we have to support them. We are super
568  * fast in the common case, and fast for shared ISA-space IRQs.
569  */
570 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
571 {
572         struct irq_cfg *cfg;
573         struct irq_pin_list *entry;
574
575         /* first time to refer irq_cfg, so with new */
576         cfg = irq_cfg_alloc(irq);
577         entry = cfg->irq_2_pin;
578         if (!entry) {
579                 entry = get_one_free_irq_2_pin();
580                 cfg->irq_2_pin = entry;
581                 entry->apic = apic;
582                 entry->pin = pin;
583                 return;
584         }
585
586         while (entry->next) {
587                 /* not again, please */
588                 if (entry->apic == apic && entry->pin == pin)
589                         return;
590
591                 entry = entry->next;
592         }
593
594         entry->next = get_one_free_irq_2_pin();
595         entry = entry->next;
596         entry->apic = apic;
597         entry->pin = pin;
598 }
599
600 /*
601  * Reroute an IRQ to a different pin.
602  */
603 static void __init replace_pin_at_irq(unsigned int irq,
604                                       int oldapic, int oldpin,
605                                       int newapic, int newpin)
606 {
607         struct irq_cfg *cfg = irq_cfg(irq);
608         struct irq_pin_list *entry = cfg->irq_2_pin;
609         int replaced = 0;
610
611         while (entry) {
612                 if (entry->apic == oldapic && entry->pin == oldpin) {
613                         entry->apic = newapic;
614                         entry->pin = newpin;
615                         replaced = 1;
616                         /* every one is different, right? */
617                         break;
618                 }
619                 entry = entry->next;
620         }
621
622         /* why? call replace before add? */
623         if (!replaced)
624                 add_pin_to_irq(irq, newapic, newpin);
625 }
626
627 static inline void io_apic_modify_irq(unsigned int irq,
628                                 int mask_and, int mask_or,
629                                 void (*final)(struct irq_pin_list *entry))
630 {
631         int pin;
632         struct irq_cfg *cfg;
633         struct irq_pin_list *entry;
634
635         cfg = irq_cfg(irq);
636         for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
637                 unsigned int reg;
638                 pin = entry->pin;
639                 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
640                 reg &= mask_and;
641                 reg |= mask_or;
642                 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
643                 if (final)
644                         final(entry);
645         }
646 }
647
648 static void __unmask_IO_APIC_irq(unsigned int irq)
649 {
650         io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED, 0, NULL);
651 }
652
653 #ifdef CONFIG_X86_64
654 void io_apic_sync(struct irq_pin_list *entry)
655 {
656         /*
657          * Synchronize the IO-APIC and the CPU by doing
658          * a dummy read from the IO-APIC
659          */
660         struct io_apic __iomem *io_apic;
661         io_apic = io_apic_base(entry->apic);
662         readl(&io_apic->data);
663 }
664
665 static void __mask_IO_APIC_irq(unsigned int irq)
666 {
667         io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
668 }
669 #else /* CONFIG_X86_32 */
670 static void __mask_IO_APIC_irq(unsigned int irq)
671 {
672         io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, NULL);
673 }
674
675 static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
676 {
677         io_apic_modify_irq(irq, ~IO_APIC_REDIR_LEVEL_TRIGGER,
678                         IO_APIC_REDIR_MASKED, NULL);
679 }
680
681 static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
682 {
683         io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED,
684                         IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
685 }
686 #endif /* CONFIG_X86_32 */
687
688 static void mask_IO_APIC_irq (unsigned int irq)
689 {
690         unsigned long flags;
691
692         spin_lock_irqsave(&ioapic_lock, flags);
693         __mask_IO_APIC_irq(irq);
694         spin_unlock_irqrestore(&ioapic_lock, flags);
695 }
696
697 static void unmask_IO_APIC_irq (unsigned int irq)
698 {
699         unsigned long flags;
700
701         spin_lock_irqsave(&ioapic_lock, flags);
702         __unmask_IO_APIC_irq(irq);
703         spin_unlock_irqrestore(&ioapic_lock, flags);
704 }
705
706 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
707 {
708         struct IO_APIC_route_entry entry;
709
710         /* Check delivery_mode to be sure we're not clearing an SMI pin */
711         entry = ioapic_read_entry(apic, pin);
712         if (entry.delivery_mode == dest_SMI)
713                 return;
714         /*
715          * Disable it in the IO-APIC irq-routing table:
716          */
717         ioapic_mask_entry(apic, pin);
718 }
719
720 static void clear_IO_APIC (void)
721 {
722         int apic, pin;
723
724         for (apic = 0; apic < nr_ioapics; apic++)
725                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
726                         clear_IO_APIC_pin(apic, pin);
727 }
728
729 #if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
730 void send_IPI_self(int vector)
731 {
732         unsigned int cfg;
733
734         /*
735          * Wait for idle.
736          */
737         apic_wait_icr_idle();
738         cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
739         /*
740          * Send the IPI. The write to APIC_ICR fires this off.
741          */
742         apic_write(APIC_ICR, cfg);
743 }
744 #endif /* !CONFIG_SMP && CONFIG_X86_32*/
745
746 #ifdef CONFIG_X86_32
747 /*
748  * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
749  * specific CPU-side IRQs.
750  */
751
752 #define MAX_PIRQS 8
753 static int pirq_entries [MAX_PIRQS];
754 static int pirqs_enabled;
755
756 static int __init ioapic_pirq_setup(char *str)
757 {
758         int i, max;
759         int ints[MAX_PIRQS+1];
760
761         get_options(str, ARRAY_SIZE(ints), ints);
762
763         for (i = 0; i < MAX_PIRQS; i++)
764                 pirq_entries[i] = -1;
765
766         pirqs_enabled = 1;
767         apic_printk(APIC_VERBOSE, KERN_INFO
768                         "PIRQ redirection, working around broken MP-BIOS.\n");
769         max = MAX_PIRQS;
770         if (ints[0] < MAX_PIRQS)
771                 max = ints[0];
772
773         for (i = 0; i < max; i++) {
774                 apic_printk(APIC_VERBOSE, KERN_DEBUG
775                                 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
776                 /*
777                  * PIRQs are mapped upside down, usually.
778                  */
779                 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
780         }
781         return 1;
782 }
783
784 __setup("pirq=", ioapic_pirq_setup);
785 #endif /* CONFIG_X86_32 */
786
787 #ifdef CONFIG_INTR_REMAP
788 /* I/O APIC RTE contents at the OS boot up */
789 static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
790
791 /*
792  * Saves and masks all the unmasked IO-APIC RTE's
793  */
794 int save_mask_IO_APIC_setup(void)
795 {
796         union IO_APIC_reg_01 reg_01;
797         unsigned long flags;
798         int apic, pin;
799
800         /*
801          * The number of IO-APIC IRQ registers (== #pins):
802          */
803         for (apic = 0; apic < nr_ioapics; apic++) {
804                 spin_lock_irqsave(&ioapic_lock, flags);
805                 reg_01.raw = io_apic_read(apic, 1);
806                 spin_unlock_irqrestore(&ioapic_lock, flags);
807                 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
808         }
809
810         for (apic = 0; apic < nr_ioapics; apic++) {
811                 early_ioapic_entries[apic] =
812                         kzalloc(sizeof(struct IO_APIC_route_entry) *
813                                 nr_ioapic_registers[apic], GFP_KERNEL);
814                 if (!early_ioapic_entries[apic])
815                         goto nomem;
816         }
817
818         for (apic = 0; apic < nr_ioapics; apic++)
819                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
820                         struct IO_APIC_route_entry entry;
821
822                         entry = early_ioapic_entries[apic][pin] =
823                                 ioapic_read_entry(apic, pin);
824                         if (!entry.mask) {
825                                 entry.mask = 1;
826                                 ioapic_write_entry(apic, pin, entry);
827                         }
828                 }
829
830         return 0;
831
832 nomem:
833         while (apic >= 0)
834                 kfree(early_ioapic_entries[apic--]);
835         memset(early_ioapic_entries, 0,
836                 ARRAY_SIZE(early_ioapic_entries));
837
838         return -ENOMEM;
839 }
840
841 void restore_IO_APIC_setup(void)
842 {
843         int apic, pin;
844
845         for (apic = 0; apic < nr_ioapics; apic++) {
846                 if (!early_ioapic_entries[apic])
847                         break;
848                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
849                         ioapic_write_entry(apic, pin,
850                                            early_ioapic_entries[apic][pin]);
851                 kfree(early_ioapic_entries[apic]);
852                 early_ioapic_entries[apic] = NULL;
853         }
854 }
855
856 void reinit_intr_remapped_IO_APIC(int intr_remapping)
857 {
858         /*
859          * for now plain restore of previous settings.
860          * TBD: In the case of OS enabling interrupt-remapping,
861          * IO-APIC RTE's need to be setup to point to interrupt-remapping
862          * table entries. for now, do a plain restore, and wait for
863          * the setup_IO_APIC_irqs() to do proper initialization.
864          */
865         restore_IO_APIC_setup();
866 }
867 #endif
868
869 /*
870  * Find the IRQ entry number of a certain pin.
871  */
872 static int find_irq_entry(int apic, int pin, int type)
873 {
874         int i;
875
876         for (i = 0; i < mp_irq_entries; i++)
877                 if (mp_irqs[i].mp_irqtype == type &&
878                     (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
879                      mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
880                     mp_irqs[i].mp_dstirq == pin)
881                         return i;
882
883         return -1;
884 }
885
886 /*
887  * Find the pin to which IRQ[irq] (ISA) is connected
888  */
889 static int __init find_isa_irq_pin(int irq, int type)
890 {
891         int i;
892
893         for (i = 0; i < mp_irq_entries; i++) {
894                 int lbus = mp_irqs[i].mp_srcbus;
895
896                 if (test_bit(lbus, mp_bus_not_pci) &&
897                     (mp_irqs[i].mp_irqtype == type) &&
898                     (mp_irqs[i].mp_srcbusirq == irq))
899
900                         return mp_irqs[i].mp_dstirq;
901         }
902         return -1;
903 }
904
905 static int __init find_isa_irq_apic(int irq, int type)
906 {
907         int i;
908
909         for (i = 0; i < mp_irq_entries; i++) {
910                 int lbus = mp_irqs[i].mp_srcbus;
911
912                 if (test_bit(lbus, mp_bus_not_pci) &&
913                     (mp_irqs[i].mp_irqtype == type) &&
914                     (mp_irqs[i].mp_srcbusirq == irq))
915                         break;
916         }
917         if (i < mp_irq_entries) {
918                 int apic;
919                 for(apic = 0; apic < nr_ioapics; apic++) {
920                         if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
921                                 return apic;
922                 }
923         }
924
925         return -1;
926 }
927
928 /*
929  * Find a specific PCI IRQ entry.
930  * Not an __init, possibly needed by modules
931  */
932 static int pin_2_irq(int idx, int apic, int pin);
933
934 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
935 {
936         int apic, i, best_guess = -1;
937
938         apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
939                 bus, slot, pin);
940         if (test_bit(bus, mp_bus_not_pci)) {
941                 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
942                 return -1;
943         }
944         for (i = 0; i < mp_irq_entries; i++) {
945                 int lbus = mp_irqs[i].mp_srcbus;
946
947                 for (apic = 0; apic < nr_ioapics; apic++)
948                         if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
949                             mp_irqs[i].mp_dstapic == MP_APIC_ALL)
950                                 break;
951
952                 if (!test_bit(lbus, mp_bus_not_pci) &&
953                     !mp_irqs[i].mp_irqtype &&
954                     (bus == lbus) &&
955                     (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
956                         int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
957
958                         if (!(apic || IO_APIC_IRQ(irq)))
959                                 continue;
960
961                         if (pin == (mp_irqs[i].mp_srcbusirq & 3))
962                                 return irq;
963                         /*
964                          * Use the first all-but-pin matching entry as a
965                          * best-guess fuzzy result for broken mptables.
966                          */
967                         if (best_guess < 0)
968                                 best_guess = irq;
969                 }
970         }
971         return best_guess;
972 }
973
974 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
975
976 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
977 /*
978  * EISA Edge/Level control register, ELCR
979  */
980 static int EISA_ELCR(unsigned int irq)
981 {
982         if (irq < 16) {
983                 unsigned int port = 0x4d0 + (irq >> 3);
984                 return (inb(port) >> (irq & 7)) & 1;
985         }
986         apic_printk(APIC_VERBOSE, KERN_INFO
987                         "Broken MPtable reports ISA irq %d\n", irq);
988         return 0;
989 }
990
991 #endif
992
993 /* ISA interrupts are always polarity zero edge triggered,
994  * when listed as conforming in the MP table. */
995
996 #define default_ISA_trigger(idx)        (0)
997 #define default_ISA_polarity(idx)       (0)
998
999 /* EISA interrupts are always polarity zero and can be edge or level
1000  * trigger depending on the ELCR value.  If an interrupt is listed as
1001  * EISA conforming in the MP table, that means its trigger type must
1002  * be read in from the ELCR */
1003
1004 #define default_EISA_trigger(idx)       (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
1005 #define default_EISA_polarity(idx)      default_ISA_polarity(idx)
1006
1007 /* PCI interrupts are always polarity one level triggered,
1008  * when listed as conforming in the MP table. */
1009
1010 #define default_PCI_trigger(idx)        (1)
1011 #define default_PCI_polarity(idx)       (1)
1012
1013 /* MCA interrupts are always polarity zero level triggered,
1014  * when listed as conforming in the MP table. */
1015
1016 #define default_MCA_trigger(idx)        (1)
1017 #define default_MCA_polarity(idx)       default_ISA_polarity(idx)
1018
1019 static int MPBIOS_polarity(int idx)
1020 {
1021         int bus = mp_irqs[idx].mp_srcbus;
1022         int polarity;
1023
1024         /*
1025          * Determine IRQ line polarity (high active or low active):
1026          */
1027         switch (mp_irqs[idx].mp_irqflag & 3)
1028         {
1029                 case 0: /* conforms, ie. bus-type dependent polarity */
1030                         if (test_bit(bus, mp_bus_not_pci))
1031                                 polarity = default_ISA_polarity(idx);
1032                         else
1033                                 polarity = default_PCI_polarity(idx);
1034                         break;
1035                 case 1: /* high active */
1036                 {
1037                         polarity = 0;
1038                         break;
1039                 }
1040                 case 2: /* reserved */
1041                 {
1042                         printk(KERN_WARNING "broken BIOS!!\n");
1043                         polarity = 1;
1044                         break;
1045                 }
1046                 case 3: /* low active */
1047                 {
1048                         polarity = 1;
1049                         break;
1050                 }
1051                 default: /* invalid */
1052                 {
1053                         printk(KERN_WARNING "broken BIOS!!\n");
1054                         polarity = 1;
1055                         break;
1056                 }
1057         }
1058         return polarity;
1059 }
1060
1061 static int MPBIOS_trigger(int idx)
1062 {
1063         int bus = mp_irqs[idx].mp_srcbus;
1064         int trigger;
1065
1066         /*
1067          * Determine IRQ trigger mode (edge or level sensitive):
1068          */
1069         switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
1070         {
1071                 case 0: /* conforms, ie. bus-type dependent */
1072                         if (test_bit(bus, mp_bus_not_pci))
1073                                 trigger = default_ISA_trigger(idx);
1074                         else
1075                                 trigger = default_PCI_trigger(idx);
1076 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
1077                         switch (mp_bus_id_to_type[bus]) {
1078                                 case MP_BUS_ISA: /* ISA pin */
1079                                 {
1080                                         /* set before the switch */
1081                                         break;
1082                                 }
1083                                 case MP_BUS_EISA: /* EISA pin */
1084                                 {
1085                                         trigger = default_EISA_trigger(idx);
1086                                         break;
1087                                 }
1088                                 case MP_BUS_PCI: /* PCI pin */
1089                                 {
1090                                         /* set before the switch */
1091                                         break;
1092                                 }
1093                                 case MP_BUS_MCA: /* MCA pin */
1094                                 {
1095                                         trigger = default_MCA_trigger(idx);
1096                                         break;
1097                                 }
1098                                 default:
1099                                 {
1100                                         printk(KERN_WARNING "broken BIOS!!\n");
1101                                         trigger = 1;
1102                                         break;
1103                                 }
1104                         }
1105 #endif
1106                         break;
1107                 case 1: /* edge */
1108                 {
1109                         trigger = 0;
1110                         break;
1111                 }
1112                 case 2: /* reserved */
1113                 {
1114                         printk(KERN_WARNING "broken BIOS!!\n");
1115                         trigger = 1;
1116                         break;
1117                 }
1118                 case 3: /* level */
1119                 {
1120                         trigger = 1;
1121                         break;
1122                 }
1123                 default: /* invalid */
1124                 {
1125                         printk(KERN_WARNING "broken BIOS!!\n");
1126                         trigger = 0;
1127                         break;
1128                 }
1129         }
1130         return trigger;
1131 }
1132
1133 static inline int irq_polarity(int idx)
1134 {
1135         return MPBIOS_polarity(idx);
1136 }
1137
1138 static inline int irq_trigger(int idx)
1139 {
1140         return MPBIOS_trigger(idx);
1141 }
1142
1143 int (*ioapic_renumber_irq)(int ioapic, int irq);
1144 static int pin_2_irq(int idx, int apic, int pin)
1145 {
1146         int irq, i;
1147         int bus = mp_irqs[idx].mp_srcbus;
1148
1149         /*
1150          * Debugging check, we are in big trouble if this message pops up!
1151          */
1152         if (mp_irqs[idx].mp_dstirq != pin)
1153                 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1154
1155         if (test_bit(bus, mp_bus_not_pci)) {
1156                 irq = mp_irqs[idx].mp_srcbusirq;
1157         } else {
1158                 /*
1159                  * PCI IRQs are mapped in order
1160                  */
1161                 i = irq = 0;
1162                 while (i < apic)
1163                         irq += nr_ioapic_registers[i++];
1164                 irq += pin;
1165                 /*
1166                  * For MPS mode, so far only needed by ES7000 platform
1167                  */
1168                 if (ioapic_renumber_irq)
1169                         irq = ioapic_renumber_irq(apic, irq);
1170         }
1171
1172 #ifdef CONFIG_X86_32
1173         /*
1174          * PCI IRQ command line redirection. Yes, limits are hardcoded.
1175          */
1176         if ((pin >= 16) && (pin <= 23)) {
1177                 if (pirq_entries[pin-16] != -1) {
1178                         if (!pirq_entries[pin-16]) {
1179                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
1180                                                 "disabling PIRQ%d\n", pin-16);
1181                         } else {
1182                                 irq = pirq_entries[pin-16];
1183                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
1184                                                 "using PIRQ%d -> IRQ %d\n",
1185                                                 pin-16, irq);
1186                         }
1187                 }
1188         }
1189 #endif
1190
1191         return irq;
1192 }
1193
1194 void lock_vector_lock(void)
1195 {
1196         /* Used to the online set of cpus does not change
1197          * during assign_irq_vector.
1198          */
1199         spin_lock(&vector_lock);
1200 }
1201
1202 void unlock_vector_lock(void)
1203 {
1204         spin_unlock(&vector_lock);
1205 }
1206
1207 static int __assign_irq_vector(int irq, cpumask_t mask)
1208 {
1209         /*
1210          * NOTE! The local APIC isn't very good at handling
1211          * multiple interrupts at the same interrupt level.
1212          * As the interrupt level is determined by taking the
1213          * vector number and shifting that right by 4, we
1214          * want to spread these out a bit so that they don't
1215          * all fall in the same interrupt level.
1216          *
1217          * Also, we've got to be careful not to trash gate
1218          * 0x80, because int 0x80 is hm, kind of importantish. ;)
1219          */
1220         static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1221         unsigned int old_vector;
1222         int cpu;
1223         struct irq_cfg *cfg;
1224
1225         cfg = irq_cfg(irq);
1226
1227         /* Only try and allocate irqs on cpus that are present */
1228         cpus_and(mask, mask, cpu_online_map);
1229
1230         if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1231                 return -EBUSY;
1232
1233         old_vector = cfg->vector;
1234         if (old_vector) {
1235                 cpumask_t tmp;
1236                 cpus_and(tmp, cfg->domain, mask);
1237                 if (!cpus_empty(tmp))
1238                         return 0;
1239         }
1240
1241         for_each_cpu_mask_nr(cpu, mask) {
1242                 cpumask_t domain, new_mask;
1243                 int new_cpu;
1244                 int vector, offset;
1245
1246                 domain = vector_allocation_domain(cpu);
1247                 cpus_and(new_mask, domain, cpu_online_map);
1248
1249                 vector = current_vector;
1250                 offset = current_offset;
1251 next:
1252                 vector += 8;
1253                 if (vector >= first_system_vector) {
1254                         /* If we run out of vectors on large boxen, must share them. */
1255                         offset = (offset + 1) % 8;
1256                         vector = FIRST_DEVICE_VECTOR + offset;
1257                 }
1258                 if (unlikely(current_vector == vector))
1259                         continue;
1260 #ifdef CONFIG_X86_64
1261                 if (vector == IA32_SYSCALL_VECTOR)
1262                         goto next;
1263 #else
1264                 if (vector == SYSCALL_VECTOR)
1265                         goto next;
1266 #endif
1267                 for_each_cpu_mask_nr(new_cpu, new_mask)
1268                         if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1269                                 goto next;
1270                 /* Found one! */
1271                 current_vector = vector;
1272                 current_offset = offset;
1273                 if (old_vector) {
1274                         cfg->move_in_progress = 1;
1275                         cfg->old_domain = cfg->domain;
1276                 }
1277                 for_each_cpu_mask_nr(new_cpu, new_mask)
1278                         per_cpu(vector_irq, new_cpu)[vector] = irq;
1279                 cfg->vector = vector;
1280                 cfg->domain = domain;
1281                 return 0;
1282         }
1283         return -ENOSPC;
1284 }
1285
1286 static int assign_irq_vector(int irq, cpumask_t mask)
1287 {
1288         int err;
1289         unsigned long flags;
1290
1291         spin_lock_irqsave(&vector_lock, flags);
1292         err = __assign_irq_vector(irq, mask);
1293         spin_unlock_irqrestore(&vector_lock, flags);
1294         return err;
1295 }
1296
1297 static void __clear_irq_vector(int irq)
1298 {
1299         struct irq_cfg *cfg;
1300         cpumask_t mask;
1301         int cpu, vector;
1302
1303         cfg = irq_cfg(irq);
1304         BUG_ON(!cfg->vector);
1305
1306         vector = cfg->vector;
1307         cpus_and(mask, cfg->domain, cpu_online_map);
1308         for_each_cpu_mask_nr(cpu, mask)
1309                 per_cpu(vector_irq, cpu)[vector] = -1;
1310
1311         cfg->vector = 0;
1312         cpus_clear(cfg->domain);
1313 }
1314
1315 void __setup_vector_irq(int cpu)
1316 {
1317         /* Initialize vector_irq on a new cpu */
1318         /* This function must be called with vector_lock held */
1319         int irq, vector;
1320         struct irq_cfg *cfg;
1321
1322         /* Mark the inuse vectors */
1323         for_each_irq_cfg(irq, cfg) {
1324                 if (!cpu_isset(cpu, cfg->domain))
1325                         continue;
1326                 vector = cfg->vector;
1327                 per_cpu(vector_irq, cpu)[vector] = irq;
1328         }
1329         /* Mark the free vectors */
1330         for (vector = 0; vector < NR_VECTORS; ++vector) {
1331                 irq = per_cpu(vector_irq, cpu)[vector];
1332                 if (irq < 0)
1333                         continue;
1334
1335                 cfg = irq_cfg(irq);
1336                 if (!cpu_isset(cpu, cfg->domain))
1337                         per_cpu(vector_irq, cpu)[vector] = -1;
1338         }
1339 }
1340
1341 static struct irq_chip ioapic_chip;
1342 #ifdef CONFIG_INTR_REMAP
1343 static struct irq_chip ir_ioapic_chip;
1344 #endif
1345
1346 #define IOAPIC_AUTO     -1
1347 #define IOAPIC_EDGE     0
1348 #define IOAPIC_LEVEL    1
1349
1350 #ifdef CONFIG_X86_32
1351 static inline int IO_APIC_irq_trigger(int irq)
1352 {
1353         int apic, idx, pin;
1354
1355         for (apic = 0; apic < nr_ioapics; apic++) {
1356                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1357                         idx = find_irq_entry(apic, pin, mp_INT);
1358                         if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1359                                 return irq_trigger(idx);
1360                 }
1361         }
1362         /*
1363          * nonexistent IRQs are edge default
1364          */
1365         return 0;
1366 }
1367 #else
1368 static inline int IO_APIC_irq_trigger(int irq)
1369 {
1370         return 1;
1371 }
1372 #endif
1373
1374 static void ioapic_register_intr(int irq, unsigned long trigger)
1375 {
1376         struct irq_desc *desc;
1377
1378         /* first time to use this irq_desc */
1379         if (irq < 16)
1380                 desc = irq_to_desc(irq);
1381         else
1382                 desc = irq_to_desc_alloc(irq);
1383
1384         if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1385             trigger == IOAPIC_LEVEL)
1386                 desc->status |= IRQ_LEVEL;
1387         else
1388                 desc->status &= ~IRQ_LEVEL;
1389
1390 #ifdef CONFIG_INTR_REMAP
1391         if (irq_remapped(irq)) {
1392                 desc->status |= IRQ_MOVE_PCNTXT;
1393                 if (trigger)
1394                         set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1395                                                       handle_fasteoi_irq,
1396                                                      "fasteoi");
1397                 else
1398                         set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1399                                                       handle_edge_irq, "edge");
1400                 return;
1401         }
1402 #endif
1403         if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1404             trigger == IOAPIC_LEVEL)
1405                 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1406                                               handle_fasteoi_irq,
1407                                               "fasteoi");
1408         else
1409                 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1410                                               handle_edge_irq, "edge");
1411 }
1412
1413 static int setup_ioapic_entry(int apic, int irq,
1414                               struct IO_APIC_route_entry *entry,
1415                               unsigned int destination, int trigger,
1416                               int polarity, int vector)
1417 {
1418         /*
1419          * add it to the IO-APIC irq-routing table:
1420          */
1421         memset(entry,0,sizeof(*entry));
1422
1423 #ifdef CONFIG_INTR_REMAP
1424         if (intr_remapping_enabled) {
1425                 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1426                 struct irte irte;
1427                 struct IR_IO_APIC_route_entry *ir_entry =
1428                         (struct IR_IO_APIC_route_entry *) entry;
1429                 int index;
1430
1431                 if (!iommu)
1432                         panic("No mapping iommu for ioapic %d\n", apic);
1433
1434                 index = alloc_irte(iommu, irq, 1);
1435                 if (index < 0)
1436                         panic("Failed to allocate IRTE for ioapic %d\n", apic);
1437
1438                 memset(&irte, 0, sizeof(irte));
1439
1440                 irte.present = 1;
1441                 irte.dst_mode = INT_DEST_MODE;
1442                 irte.trigger_mode = trigger;
1443                 irte.dlvry_mode = INT_DELIVERY_MODE;
1444                 irte.vector = vector;
1445                 irte.dest_id = IRTE_DEST(destination);
1446
1447                 modify_irte(irq, &irte);
1448
1449                 ir_entry->index2 = (index >> 15) & 0x1;
1450                 ir_entry->zero = 0;
1451                 ir_entry->format = 1;
1452                 ir_entry->index = (index & 0x7fff);
1453         } else
1454 #endif
1455         {
1456                 entry->delivery_mode = INT_DELIVERY_MODE;
1457                 entry->dest_mode = INT_DEST_MODE;
1458                 entry->dest = destination;
1459         }
1460
1461         entry->mask = 0;                                /* enable IRQ */
1462         entry->trigger = trigger;
1463         entry->polarity = polarity;
1464         entry->vector = vector;
1465
1466         /* Mask level triggered irqs.
1467          * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1468          */
1469         if (trigger)
1470                 entry->mask = 1;
1471         return 0;
1472 }
1473
1474 static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
1475                               int trigger, int polarity)
1476 {
1477         struct irq_cfg *cfg;
1478         struct IO_APIC_route_entry entry;
1479         cpumask_t mask;
1480
1481         if (!IO_APIC_IRQ(irq))
1482                 return;
1483
1484         cfg = irq_cfg(irq);
1485
1486         mask = TARGET_CPUS;
1487         if (assign_irq_vector(irq, mask))
1488                 return;
1489
1490         cpus_and(mask, cfg->domain, mask);
1491
1492         apic_printk(APIC_VERBOSE,KERN_DEBUG
1493                     "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1494                     "IRQ %d Mode:%i Active:%i)\n",
1495                     apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1496                     irq, trigger, polarity);
1497
1498
1499         if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1500                                cpu_mask_to_apicid(mask), trigger, polarity,
1501                                cfg->vector)) {
1502                 printk("Failed to setup ioapic entry for ioapic  %d, pin %d\n",
1503                        mp_ioapics[apic].mp_apicid, pin);
1504                 __clear_irq_vector(irq);
1505                 return;
1506         }
1507
1508         ioapic_register_intr(irq, trigger);
1509         if (irq < 16)
1510                 disable_8259A_irq(irq);
1511
1512         ioapic_write_entry(apic, pin, entry);
1513 }
1514
1515 static void __init setup_IO_APIC_irqs(void)
1516 {
1517         int apic, pin, idx, irq;
1518         int notcon = 0;
1519
1520         apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1521
1522         for (apic = 0; apic < nr_ioapics; apic++) {
1523                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1524
1525                         idx = find_irq_entry(apic, pin, mp_INT);
1526                         if (idx == -1) {
1527                                 if (!notcon) {
1528                                         notcon = 1;
1529                                         apic_printk(APIC_VERBOSE,
1530                                                 KERN_DEBUG " %d-%d",
1531                                                 mp_ioapics[apic].mp_apicid,
1532                                                 pin);
1533                                 } else
1534                                         apic_printk(APIC_VERBOSE, " %d-%d",
1535                                                 mp_ioapics[apic].mp_apicid,
1536                                                 pin);
1537                                 continue;
1538                         }
1539                         if (notcon) {
1540                                 apic_printk(APIC_VERBOSE,
1541                                         " (apicid-pin) not connected\n");
1542                                 notcon = 0;
1543                         }
1544
1545                         irq = pin_2_irq(idx, apic, pin);
1546 #ifdef CONFIG_X86_32
1547                         if (multi_timer_check(apic, irq))
1548                                 continue;
1549 #endif
1550                         add_pin_to_irq(irq, apic, pin);
1551
1552                         setup_IO_APIC_irq(apic, pin, irq,
1553                                         irq_trigger(idx), irq_polarity(idx));
1554                 }
1555         }
1556
1557         if (notcon)
1558                 apic_printk(APIC_VERBOSE,
1559                         " (apicid-pin) not connected\n");
1560 }
1561
1562 /*
1563  * Set up the timer pin, possibly with the 8259A-master behind.
1564  */
1565 static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1566                                         int vector)
1567 {
1568         struct IO_APIC_route_entry entry;
1569
1570 #ifdef CONFIG_INTR_REMAP
1571         if (intr_remapping_enabled)
1572                 return;
1573 #endif
1574
1575         memset(&entry, 0, sizeof(entry));
1576
1577         /*
1578          * We use logical delivery to get the timer IRQ
1579          * to the first CPU.
1580          */
1581         entry.dest_mode = INT_DEST_MODE;
1582         entry.mask = 1;                                 /* mask IRQ now */
1583         entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
1584         entry.delivery_mode = INT_DELIVERY_MODE;
1585         entry.polarity = 0;
1586         entry.trigger = 0;
1587         entry.vector = vector;
1588
1589         /*
1590          * The timer IRQ doesn't have to know that behind the
1591          * scene we may have a 8259A-master in AEOI mode ...
1592          */
1593         set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1594
1595         /*
1596          * Add it to the IO-APIC irq-routing table:
1597          */
1598         ioapic_write_entry(apic, pin, entry);
1599 }
1600
1601
1602 __apicdebuginit(void) print_IO_APIC(void)
1603 {
1604         int apic, i;
1605         union IO_APIC_reg_00 reg_00;
1606         union IO_APIC_reg_01 reg_01;
1607         union IO_APIC_reg_02 reg_02;
1608         union IO_APIC_reg_03 reg_03;
1609         unsigned long flags;
1610         struct irq_cfg *cfg;
1611         unsigned int irq;
1612
1613         if (apic_verbosity == APIC_QUIET)
1614                 return;
1615
1616         printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1617         for (i = 0; i < nr_ioapics; i++)
1618                 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1619                        mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
1620
1621         /*
1622          * We are a bit conservative about what we expect.  We have to
1623          * know about every hardware change ASAP.
1624          */
1625         printk(KERN_INFO "testing the IO APIC.......................\n");
1626
1627         for (apic = 0; apic < nr_ioapics; apic++) {
1628
1629         spin_lock_irqsave(&ioapic_lock, flags);
1630         reg_00.raw = io_apic_read(apic, 0);
1631         reg_01.raw = io_apic_read(apic, 1);
1632         if (reg_01.bits.version >= 0x10)
1633                 reg_02.raw = io_apic_read(apic, 2);
1634         if (reg_01.bits.version >= 0x20)
1635                 reg_03.raw = io_apic_read(apic, 3);
1636         spin_unlock_irqrestore(&ioapic_lock, flags);
1637
1638         printk("\n");
1639         printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
1640         printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1641         printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
1642         printk(KERN_DEBUG ".......    : Delivery Type: %X\n", reg_00.bits.delivery_type);
1643         printk(KERN_DEBUG ".......    : LTS          : %X\n", reg_00.bits.LTS);
1644
1645         printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1646         printk(KERN_DEBUG ".......     : max redirection entries: %04X\n", reg_01.bits.entries);
1647
1648         printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
1649         printk(KERN_DEBUG ".......     : IO APIC version: %04X\n", reg_01.bits.version);
1650
1651         /*
1652          * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1653          * but the value of reg_02 is read as the previous read register
1654          * value, so ignore it if reg_02 == reg_01.
1655          */
1656         if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1657                 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1658                 printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
1659         }
1660
1661         /*
1662          * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1663          * or reg_03, but the value of reg_0[23] is read as the previous read
1664          * register value, so ignore it if reg_03 == reg_0[12].
1665          */
1666         if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1667             reg_03.raw != reg_01.raw) {
1668                 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1669                 printk(KERN_DEBUG ".......     : Boot DT    : %X\n", reg_03.bits.boot_DT);
1670         }
1671
1672         printk(KERN_DEBUG ".... IRQ redirection table:\n");
1673
1674         printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1675                           " Stat Dmod Deli Vect:   \n");
1676
1677         for (i = 0; i <= reg_01.bits.entries; i++) {
1678                 struct IO_APIC_route_entry entry;
1679
1680                 entry = ioapic_read_entry(apic, i);
1681
1682                 printk(KERN_DEBUG " %02x %03X ",
1683                         i,
1684                         entry.dest
1685                 );
1686
1687                 printk("%1d    %1d    %1d   %1d   %1d    %1d    %1d    %02X\n",
1688                         entry.mask,
1689                         entry.trigger,
1690                         entry.irr,
1691                         entry.polarity,
1692                         entry.delivery_status,
1693                         entry.dest_mode,
1694                         entry.delivery_mode,
1695                         entry.vector
1696                 );
1697         }
1698         }
1699         printk(KERN_DEBUG "IRQ to pin mappings:\n");
1700         for_each_irq_cfg(irq, cfg) {
1701                 struct irq_pin_list *entry = cfg->irq_2_pin;
1702                 if (!entry)
1703                         continue;
1704                 printk(KERN_DEBUG "IRQ%d ", irq);
1705                 for (;;) {
1706                         printk("-> %d:%d", entry->apic, entry->pin);
1707                         if (!entry->next)
1708                                 break;
1709                         entry = entry->next;
1710                 }
1711                 printk("\n");
1712         }
1713
1714         printk(KERN_INFO ".................................... done.\n");
1715
1716         return;
1717 }
1718
1719 __apicdebuginit(void) print_APIC_bitfield(int base)
1720 {
1721         unsigned int v;
1722         int i, j;
1723
1724         if (apic_verbosity == APIC_QUIET)
1725                 return;
1726
1727         printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1728         for (i = 0; i < 8; i++) {
1729                 v = apic_read(base + i*0x10);
1730                 for (j = 0; j < 32; j++) {
1731                         if (v & (1<<j))
1732                                 printk("1");
1733                         else
1734                                 printk("0");
1735                 }
1736                 printk("\n");
1737         }
1738 }
1739
1740 __apicdebuginit(void) print_local_APIC(void *dummy)
1741 {
1742         unsigned int v, ver, maxlvt;
1743         u64 icr;
1744
1745         if (apic_verbosity == APIC_QUIET)
1746                 return;
1747
1748         printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1749                 smp_processor_id(), hard_smp_processor_id());
1750         v = apic_read(APIC_ID);
1751         printk(KERN_INFO "... APIC ID:      %08x (%01x)\n", v, read_apic_id());
1752         v = apic_read(APIC_LVR);
1753         printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1754         ver = GET_APIC_VERSION(v);
1755         maxlvt = lapic_get_maxlvt();
1756
1757         v = apic_read(APIC_TASKPRI);
1758         printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1759
1760         if (APIC_INTEGRATED(ver)) {                     /* !82489DX */
1761                 if (!APIC_XAPIC(ver)) {
1762                         v = apic_read(APIC_ARBPRI);
1763                         printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1764                                v & APIC_ARBPRI_MASK);
1765                 }
1766                 v = apic_read(APIC_PROCPRI);
1767                 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1768         }
1769
1770         /*
1771          * Remote read supported only in the 82489DX and local APIC for
1772          * Pentium processors.
1773          */
1774         if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1775                 v = apic_read(APIC_RRR);
1776                 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1777         }
1778
1779         v = apic_read(APIC_LDR);
1780         printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1781         if (!x2apic_enabled()) {
1782                 v = apic_read(APIC_DFR);
1783                 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1784         }
1785         v = apic_read(APIC_SPIV);
1786         printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1787
1788         printk(KERN_DEBUG "... APIC ISR field:\n");
1789         print_APIC_bitfield(APIC_ISR);
1790         printk(KERN_DEBUG "... APIC TMR field:\n");
1791         print_APIC_bitfield(APIC_TMR);
1792         printk(KERN_DEBUG "... APIC IRR field:\n");
1793         print_APIC_bitfield(APIC_IRR);
1794
1795         if (APIC_INTEGRATED(ver)) {             /* !82489DX */
1796                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
1797                         apic_write(APIC_ESR, 0);
1798
1799                 v = apic_read(APIC_ESR);
1800                 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1801         }
1802
1803         icr = apic_icr_read();
1804         printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1805         printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
1806
1807         v = apic_read(APIC_LVTT);
1808         printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1809
1810         if (maxlvt > 3) {                       /* PC is LVT#4. */
1811                 v = apic_read(APIC_LVTPC);
1812                 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1813         }
1814         v = apic_read(APIC_LVT0);
1815         printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1816         v = apic_read(APIC_LVT1);
1817         printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1818
1819         if (maxlvt > 2) {                       /* ERR is LVT#3. */
1820                 v = apic_read(APIC_LVTERR);
1821                 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1822         }
1823
1824         v = apic_read(APIC_TMICT);
1825         printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1826         v = apic_read(APIC_TMCCT);
1827         printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1828         v = apic_read(APIC_TDCR);
1829         printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1830         printk("\n");
1831 }
1832
1833 __apicdebuginit(void) print_all_local_APICs(void)
1834 {
1835         int cpu;
1836
1837         preempt_disable();
1838         for_each_online_cpu(cpu)
1839                 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1840         preempt_enable();
1841 }
1842
1843 __apicdebuginit(void) print_PIC(void)
1844 {
1845         unsigned int v;
1846         unsigned long flags;
1847
1848         if (apic_verbosity == APIC_QUIET)
1849                 return;
1850
1851         printk(KERN_DEBUG "\nprinting PIC contents\n");
1852
1853         spin_lock_irqsave(&i8259A_lock, flags);
1854
1855         v = inb(0xa1) << 8 | inb(0x21);
1856         printk(KERN_DEBUG "... PIC  IMR: %04x\n", v);
1857
1858         v = inb(0xa0) << 8 | inb(0x20);
1859         printk(KERN_DEBUG "... PIC  IRR: %04x\n", v);
1860
1861         outb(0x0b,0xa0);
1862         outb(0x0b,0x20);
1863         v = inb(0xa0) << 8 | inb(0x20);
1864         outb(0x0a,0xa0);
1865         outb(0x0a,0x20);
1866
1867         spin_unlock_irqrestore(&i8259A_lock, flags);
1868
1869         printk(KERN_DEBUG "... PIC  ISR: %04x\n", v);
1870
1871         v = inb(0x4d1) << 8 | inb(0x4d0);
1872         printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1873 }
1874
1875 __apicdebuginit(int) print_all_ICs(void)
1876 {
1877         print_PIC();
1878         print_all_local_APICs();
1879         print_IO_APIC();
1880
1881         return 0;
1882 }
1883
1884 fs_initcall(print_all_ICs);
1885
1886
1887 /* Where if anywhere is the i8259 connect in external int mode */
1888 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1889
1890 void __init enable_IO_APIC(void)
1891 {
1892         union IO_APIC_reg_01 reg_01;
1893         int i8259_apic, i8259_pin;
1894         int apic;
1895         unsigned long flags;
1896
1897 #ifdef CONFIG_X86_32
1898         int i;
1899         if (!pirqs_enabled)
1900                 for (i = 0; i < MAX_PIRQS; i++)
1901                         pirq_entries[i] = -1;
1902 #endif
1903
1904         /*
1905          * The number of IO-APIC IRQ registers (== #pins):
1906          */
1907         for (apic = 0; apic < nr_ioapics; apic++) {
1908                 spin_lock_irqsave(&ioapic_lock, flags);
1909                 reg_01.raw = io_apic_read(apic, 1);
1910                 spin_unlock_irqrestore(&ioapic_lock, flags);
1911                 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1912         }
1913         for(apic = 0; apic < nr_ioapics; apic++) {
1914                 int pin;
1915                 /* See if any of the pins is in ExtINT mode */
1916                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1917                         struct IO_APIC_route_entry entry;
1918                         entry = ioapic_read_entry(apic, pin);
1919
1920                         /* If the interrupt line is enabled and in ExtInt mode
1921                          * I have found the pin where the i8259 is connected.
1922                          */
1923                         if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1924                                 ioapic_i8259.apic = apic;
1925                                 ioapic_i8259.pin  = pin;
1926                                 goto found_i8259;
1927                         }
1928                 }
1929         }
1930  found_i8259:
1931         /* Look to see what if the MP table has reported the ExtINT */
1932         /* If we could not find the appropriate pin by looking at the ioapic
1933          * the i8259 probably is not connected the ioapic but give the
1934          * mptable a chance anyway.
1935          */
1936         i8259_pin  = find_isa_irq_pin(0, mp_ExtINT);
1937         i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1938         /* Trust the MP table if nothing is setup in the hardware */
1939         if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1940                 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1941                 ioapic_i8259.pin  = i8259_pin;
1942                 ioapic_i8259.apic = i8259_apic;
1943         }
1944         /* Complain if the MP table and the hardware disagree */
1945         if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1946                 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1947         {
1948                 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1949         }
1950
1951         /*
1952          * Do not trust the IO-APIC being empty at bootup
1953          */
1954         clear_IO_APIC();
1955 }
1956
1957 /*
1958  * Not an __init, needed by the reboot code
1959  */
1960 void disable_IO_APIC(void)
1961 {
1962         /*
1963          * Clear the IO-APIC before rebooting:
1964          */
1965         clear_IO_APIC();
1966
1967         /*
1968          * If the i8259 is routed through an IOAPIC
1969          * Put that IOAPIC in virtual wire mode
1970          * so legacy interrupts can be delivered.
1971          */
1972         if (ioapic_i8259.pin != -1) {
1973                 struct IO_APIC_route_entry entry;
1974
1975                 memset(&entry, 0, sizeof(entry));
1976                 entry.mask            = 0; /* Enabled */
1977                 entry.trigger         = 0; /* Edge */
1978                 entry.irr             = 0;
1979                 entry.polarity        = 0; /* High */
1980                 entry.delivery_status = 0;
1981                 entry.dest_mode       = 0; /* Physical */
1982                 entry.delivery_mode   = dest_ExtINT; /* ExtInt */
1983                 entry.vector          = 0;
1984                 entry.dest            = read_apic_id();
1985
1986                 /*
1987                  * Add it to the IO-APIC irq-routing table:
1988                  */
1989                 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1990         }
1991
1992         disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1993 }
1994
1995 #ifdef CONFIG_X86_32
1996 /*
1997  * function to set the IO-APIC physical IDs based on the
1998  * values stored in the MPC table.
1999  *
2000  * by Matt Domsch <Matt_Domsch@dell.com>  Tue Dec 21 12:25:05 CST 1999
2001  */
2002
2003 static void __init setup_ioapic_ids_from_mpc(void)
2004 {
2005         union IO_APIC_reg_00 reg_00;
2006         physid_mask_t phys_id_present_map;
2007         int apic;
2008         int i;
2009         unsigned char old_id;
2010         unsigned long flags;
2011
2012         if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
2013                 return;
2014
2015         /*
2016          * Don't check I/O APIC IDs for xAPIC systems.  They have
2017          * no meaning without the serial APIC bus.
2018          */
2019         if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2020                 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
2021                 return;
2022         /*
2023          * This is broken; anything with a real cpu count has to
2024          * circumvent this idiocy regardless.
2025          */
2026         phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2027
2028         /*
2029          * Set the IOAPIC ID to the value stored in the MPC table.
2030          */
2031         for (apic = 0; apic < nr_ioapics; apic++) {
2032
2033                 /* Read the register 0 value */
2034                 spin_lock_irqsave(&ioapic_lock, flags);
2035                 reg_00.raw = io_apic_read(apic, 0);
2036                 spin_unlock_irqrestore(&ioapic_lock, flags);
2037
2038                 old_id = mp_ioapics[apic].mp_apicid;
2039
2040                 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
2041                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
2042                                 apic, mp_ioapics[apic].mp_apicid);
2043                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2044                                 reg_00.bits.ID);
2045                         mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
2046                 }
2047
2048                 /*
2049                  * Sanity check, is the ID really free? Every APIC in a
2050                  * system must have a unique ID or we get lots of nice
2051                  * 'stuck on smp_invalidate_needed IPI wait' messages.
2052                  */
2053                 if (check_apicid_used(phys_id_present_map,
2054                                         mp_ioapics[apic].mp_apicid)) {
2055                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
2056                                 apic, mp_ioapics[apic].mp_apicid);
2057                         for (i = 0; i < get_physical_broadcast(); i++)
2058                                 if (!physid_isset(i, phys_id_present_map))
2059                                         break;
2060                         if (i >= get_physical_broadcast())
2061                                 panic("Max APIC ID exceeded!\n");
2062                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2063                                 i);
2064                         physid_set(i, phys_id_present_map);
2065                         mp_ioapics[apic].mp_apicid = i;
2066                 } else {
2067                         physid_mask_t tmp;
2068                         tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
2069                         apic_printk(APIC_VERBOSE, "Setting %d in the "
2070                                         "phys_id_present_map\n",
2071                                         mp_ioapics[apic].mp_apicid);
2072                         physids_or(phys_id_present_map, phys_id_present_map, tmp);
2073                 }
2074
2075
2076                 /*
2077                  * We need to adjust the IRQ routing table
2078                  * if the ID changed.
2079                  */
2080                 if (old_id != mp_ioapics[apic].mp_apicid)
2081                         for (i = 0; i < mp_irq_entries; i++)
2082                                 if (mp_irqs[i].mp_dstapic == old_id)
2083                                         mp_irqs[i].mp_dstapic
2084                                                 = mp_ioapics[apic].mp_apicid;
2085
2086                 /*
2087                  * Read the right value from the MPC table and
2088                  * write it into the ID register.
2089                  */
2090                 apic_printk(APIC_VERBOSE, KERN_INFO
2091                         "...changing IO-APIC physical APIC ID to %d ...",
2092                         mp_ioapics[apic].mp_apicid);
2093
2094                 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
2095                 spin_lock_irqsave(&ioapic_lock, flags);
2096                 io_apic_write(apic, 0, reg_00.raw);
2097                 spin_unlock_irqrestore(&ioapic_lock, flags);
2098
2099                 /*
2100                  * Sanity check
2101                  */
2102                 spin_lock_irqsave(&ioapic_lock, flags);
2103                 reg_00.raw = io_apic_read(apic, 0);
2104                 spin_unlock_irqrestore(&ioapic_lock, flags);
2105                 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
2106                         printk("could not set ID!\n");
2107                 else
2108                         apic_printk(APIC_VERBOSE, " ok.\n");
2109         }
2110 }
2111 #endif
2112
2113 int no_timer_check __initdata;
2114
2115 static int __init notimercheck(char *s)
2116 {
2117         no_timer_check = 1;
2118         return 1;
2119 }
2120 __setup("no_timer_check", notimercheck);
2121
2122 /*
2123  * There is a nasty bug in some older SMP boards, their mptable lies
2124  * about the timer IRQ. We do the following to work around the situation:
2125  *
2126  *      - timer IRQ defaults to IO-APIC IRQ
2127  *      - if this function detects that timer IRQs are defunct, then we fall
2128  *        back to ISA timer IRQs
2129  */
2130 static int __init timer_irq_works(void)
2131 {
2132         unsigned long t1 = jiffies;
2133         unsigned long flags;
2134
2135         if (no_timer_check)
2136                 return 1;
2137
2138         local_save_flags(flags);
2139         local_irq_enable();
2140         /* Let ten ticks pass... */
2141         mdelay((10 * 1000) / HZ);
2142         local_irq_restore(flags);
2143
2144         /*
2145          * Expect a few ticks at least, to be sure some possible
2146          * glue logic does not lock up after one or two first
2147          * ticks in a non-ExtINT mode.  Also the local APIC
2148          * might have cached one ExtINT interrupt.  Finally, at
2149          * least one tick may be lost due to delays.
2150          */
2151
2152         /* jiffies wrap? */
2153         if (time_after(jiffies, t1 + 4))
2154                 return 1;
2155         return 0;
2156 }
2157
2158 /*
2159  * In the SMP+IOAPIC case it might happen that there are an unspecified
2160  * number of pending IRQ events unhandled. These cases are very rare,
2161  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2162  * better to do it this way as thus we do not have to be aware of
2163  * 'pending' interrupts in the IRQ path, except at this point.
2164  */
2165 /*
2166  * Edge triggered needs to resend any interrupt
2167  * that was delayed but this is now handled in the device
2168  * independent code.
2169  */
2170
2171 /*
2172  * Starting up a edge-triggered IO-APIC interrupt is
2173  * nasty - we need to make sure that we get the edge.
2174  * If it is already asserted for some reason, we need
2175  * return 1 to indicate that is was pending.
2176  *
2177  * This is not complete - we should be able to fake
2178  * an edge even if it isn't on the 8259A...
2179  */
2180
2181 static unsigned int startup_ioapic_irq(unsigned int irq)
2182 {
2183         int was_pending = 0;
2184         unsigned long flags;
2185
2186         spin_lock_irqsave(&ioapic_lock, flags);
2187         if (irq < 16) {
2188                 disable_8259A_irq(irq);
2189                 if (i8259A_irq_pending(irq))
2190                         was_pending = 1;
2191         }
2192         __unmask_IO_APIC_irq(irq);
2193         spin_unlock_irqrestore(&ioapic_lock, flags);
2194
2195         return was_pending;
2196 }
2197
2198 #ifdef CONFIG_X86_64
2199 static int ioapic_retrigger_irq(unsigned int irq)
2200 {
2201
2202         struct irq_cfg *cfg = irq_cfg(irq);
2203         unsigned long flags;
2204
2205         spin_lock_irqsave(&vector_lock, flags);
2206         send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2207         spin_unlock_irqrestore(&vector_lock, flags);
2208
2209         return 1;
2210 }
2211 #else
2212 static int ioapic_retrigger_irq(unsigned int irq)
2213 {
2214         send_IPI_self(irq_cfg(irq)->vector);
2215
2216         return 1;
2217 }
2218 #endif
2219
2220 /*
2221  * Level and edge triggered IO-APIC interrupts need different handling,
2222  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2223  * handled with the level-triggered descriptor, but that one has slightly
2224  * more overhead. Level-triggered interrupts cannot be handled with the
2225  * edge-triggered handler, without risking IRQ storms and other ugly
2226  * races.
2227  */
2228
2229 #ifdef CONFIG_SMP
2230
2231 #ifdef CONFIG_INTR_REMAP
2232 static void ir_irq_migration(struct work_struct *work);
2233
2234 static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2235
2236 /*
2237  * Migrate the IO-APIC irq in the presence of intr-remapping.
2238  *
2239  * For edge triggered, irq migration is a simple atomic update(of vector
2240  * and cpu destination) of IRTE and flush the hardware cache.
2241  *
2242  * For level triggered, we need to modify the io-apic RTE aswell with the update
2243  * vector information, along with modifying IRTE with vector and destination.
2244  * So irq migration for level triggered is little  bit more complex compared to
2245  * edge triggered migration. But the good news is, we use the same algorithm
2246  * for level triggered migration as we have today, only difference being,
2247  * we now initiate the irq migration from process context instead of the
2248  * interrupt context.
2249  *
2250  * In future, when we do a directed EOI (combined with cpu EOI broadcast
2251  * suppression) to the IO-APIC, level triggered irq migration will also be
2252  * as simple as edge triggered migration and we can do the irq migration
2253  * with a simple atomic update to IO-APIC RTE.
2254  */
2255 static void migrate_ioapic_irq(int irq, cpumask_t mask)
2256 {
2257         struct irq_cfg *cfg;
2258         struct irq_desc *desc;
2259         cpumask_t tmp, cleanup_mask;
2260         struct irte irte;
2261         int modify_ioapic_rte;
2262         unsigned int dest;
2263         unsigned long flags;
2264
2265         cpus_and(tmp, mask, cpu_online_map);
2266         if (cpus_empty(tmp))
2267                 return;
2268
2269         if (get_irte(irq, &irte))
2270                 return;
2271
2272         if (assign_irq_vector(irq, mask))
2273                 return;
2274
2275         cfg = irq_cfg(irq);
2276         cpus_and(tmp, cfg->domain, mask);
2277         dest = cpu_mask_to_apicid(tmp);
2278
2279         desc = irq_to_desc(irq);
2280         modify_ioapic_rte = desc->status & IRQ_LEVEL;
2281         if (modify_ioapic_rte) {
2282                 spin_lock_irqsave(&ioapic_lock, flags);
2283                 __target_IO_APIC_irq(irq, dest, cfg->vector);
2284                 spin_unlock_irqrestore(&ioapic_lock, flags);
2285         }
2286
2287         irte.vector = cfg->vector;
2288         irte.dest_id = IRTE_DEST(dest);
2289
2290         /*
2291          * Modified the IRTE and flushes the Interrupt entry cache.
2292          */
2293         modify_irte(irq, &irte);
2294
2295         if (cfg->move_in_progress) {
2296                 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2297                 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2298                 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2299                 cfg->move_in_progress = 0;
2300         }
2301
2302         desc->affinity = mask;
2303 }
2304
2305 static int migrate_irq_remapped_level(int irq)
2306 {
2307         int ret = -1;
2308         struct irq_desc *desc = irq_to_desc(irq);
2309
2310         mask_IO_APIC_irq(irq);
2311
2312         if (io_apic_level_ack_pending(irq)) {
2313                 /*
2314                  * Interrupt in progress. Migrating irq now will change the
2315                  * vector information in the IO-APIC RTE and that will confuse
2316                  * the EOI broadcast performed by cpu.
2317                  * So, delay the irq migration to the next instance.
2318                  */
2319                 schedule_delayed_work(&ir_migration_work, 1);
2320                 goto unmask;
2321         }
2322
2323         /* everthing is clear. we have right of way */
2324         migrate_ioapic_irq(irq, desc->pending_mask);
2325
2326         ret = 0;
2327         desc->status &= ~IRQ_MOVE_PENDING;
2328         cpus_clear(desc->pending_mask);
2329
2330 unmask:
2331         unmask_IO_APIC_irq(irq);
2332         return ret;
2333 }
2334
2335 static void ir_irq_migration(struct work_struct *work)
2336 {
2337         unsigned int irq;
2338         struct irq_desc *desc;
2339
2340         for_each_irq_desc(irq, desc) {
2341                 if (desc->status & IRQ_MOVE_PENDING) {
2342                         unsigned long flags;
2343
2344                         spin_lock_irqsave(&desc->lock, flags);
2345                         if (!desc->chip->set_affinity ||
2346                             !(desc->status & IRQ_MOVE_PENDING)) {
2347                                 desc->status &= ~IRQ_MOVE_PENDING;
2348                                 spin_unlock_irqrestore(&desc->lock, flags);
2349                                 continue;
2350                         }
2351
2352                         desc->chip->set_affinity(irq, desc->pending_mask);
2353                         spin_unlock_irqrestore(&desc->lock, flags);
2354                 }
2355         }
2356 }
2357
2358 /*
2359  * Migrates the IRQ destination in the process context.
2360  */
2361 static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2362 {
2363         struct irq_desc *desc = irq_to_desc(irq);
2364
2365         if (desc->status & IRQ_LEVEL) {
2366                 desc->status |= IRQ_MOVE_PENDING;
2367                 desc->pending_mask = mask;
2368                 migrate_irq_remapped_level(irq);
2369                 return;
2370         }
2371
2372         migrate_ioapic_irq(irq, mask);
2373 }
2374 #endif
2375
2376 asmlinkage void smp_irq_move_cleanup_interrupt(void)
2377 {
2378         unsigned vector, me;
2379         ack_APIC_irq();
2380 #ifdef CONFIG_X86_64
2381         exit_idle();
2382 #endif
2383         irq_enter();
2384
2385         me = smp_processor_id();
2386         for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2387                 unsigned int irq;
2388                 struct irq_desc *desc;
2389                 struct irq_cfg *cfg;
2390                 irq = __get_cpu_var(vector_irq)[vector];
2391
2392                 desc = irq_to_desc(irq);
2393                 if (!desc)
2394                         continue;
2395
2396                 cfg = irq_cfg(irq);
2397                 spin_lock(&desc->lock);
2398                 if (!cfg->move_cleanup_count)
2399                         goto unlock;
2400
2401                 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2402                         goto unlock;
2403
2404                 __get_cpu_var(vector_irq)[vector] = -1;
2405                 cfg->move_cleanup_count--;
2406 unlock:
2407                 spin_unlock(&desc->lock);
2408         }
2409
2410         irq_exit();
2411 }
2412
2413 static void irq_complete_move(unsigned int irq)
2414 {
2415         struct irq_cfg *cfg = irq_cfg(irq);
2416         unsigned vector, me;
2417
2418         if (likely(!cfg->move_in_progress))
2419                 return;
2420
2421         vector = ~get_irq_regs()->orig_ax;
2422         me = smp_processor_id();
2423         if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2424                 cpumask_t cleanup_mask;
2425
2426                 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2427                 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2428                 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2429                 cfg->move_in_progress = 0;
2430         }
2431 }
2432 #else
2433 static inline void irq_complete_move(unsigned int irq) {}
2434 #endif
2435 #ifdef CONFIG_INTR_REMAP
2436 static void ack_x2apic_level(unsigned int irq)
2437 {
2438         ack_x2APIC_irq();
2439 }
2440
2441 static void ack_x2apic_edge(unsigned int irq)
2442 {
2443         ack_x2APIC_irq();
2444 }
2445 #endif
2446
2447 static void ack_apic_edge(unsigned int irq)
2448 {
2449         irq_complete_move(irq);
2450         move_native_irq(irq);
2451         ack_APIC_irq();
2452 }
2453
2454 #ifdef CONFIG_X86_32
2455 atomic_t irq_mis_count;
2456 #endif
2457
2458 static void ack_apic_level(unsigned int irq)
2459 {
2460 #ifdef CONFIG_X86_32
2461         unsigned long v;
2462         int i;
2463 #endif
2464         int do_unmask_irq = 0;
2465
2466         irq_complete_move(irq);
2467 #ifdef CONFIG_GENERIC_PENDING_IRQ
2468         /* If we are moving the irq we need to mask it */
2469         if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2470                 do_unmask_irq = 1;
2471                 mask_IO_APIC_irq(irq);
2472         }
2473 #endif
2474
2475 #ifdef CONFIG_X86_32
2476         /*
2477         * It appears there is an erratum which affects at least version 0x11
2478         * of I/O APIC (that's the 82093AA and cores integrated into various
2479         * chipsets).  Under certain conditions a level-triggered interrupt is
2480         * erroneously delivered as edge-triggered one but the respective IRR
2481         * bit gets set nevertheless.  As a result the I/O unit expects an EOI
2482         * message but it will never arrive and further interrupts are blocked
2483         * from the source.  The exact reason is so far unknown, but the
2484         * phenomenon was observed when two consecutive interrupt requests
2485         * from a given source get delivered to the same CPU and the source is
2486         * temporarily disabled in between.
2487         *
2488         * A workaround is to simulate an EOI message manually.  We achieve it
2489         * by setting the trigger mode to edge and then to level when the edge
2490         * trigger mode gets detected in the TMR of a local APIC for a
2491         * level-triggered interrupt.  We mask the source for the time of the
2492         * operation to prevent an edge-triggered interrupt escaping meanwhile.
2493         * The idea is from Manfred Spraul.  --macro
2494         */
2495         i = irq_cfg(irq)->vector;
2496
2497         v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2498 #endif
2499
2500         /*
2501          * We must acknowledge the irq before we move it or the acknowledge will
2502          * not propagate properly.
2503          */
2504         ack_APIC_irq();
2505
2506         /* Now we can move and renable the irq */
2507         if (unlikely(do_unmask_irq)) {
2508                 /* Only migrate the irq if the ack has been received.
2509                  *
2510                  * On rare occasions the broadcast level triggered ack gets
2511                  * delayed going to ioapics, and if we reprogram the
2512                  * vector while Remote IRR is still set the irq will never
2513                  * fire again.
2514                  *
2515                  * To prevent this scenario we read the Remote IRR bit
2516                  * of the ioapic.  This has two effects.
2517                  * - On any sane system the read of the ioapic will
2518                  *   flush writes (and acks) going to the ioapic from
2519                  *   this cpu.
2520                  * - We get to see if the ACK has actually been delivered.
2521                  *
2522                  * Based on failed experiments of reprogramming the
2523                  * ioapic entry from outside of irq context starting
2524                  * with masking the ioapic entry and then polling until
2525                  * Remote IRR was clear before reprogramming the
2526                  * ioapic I don't trust the Remote IRR bit to be
2527                  * completey accurate.
2528                  *
2529                  * However there appears to be no other way to plug
2530                  * this race, so if the Remote IRR bit is not
2531                  * accurate and is causing problems then it is a hardware bug
2532                  * and you can go talk to the chipset vendor about it.
2533                  */
2534                 if (!io_apic_level_ack_pending(irq))
2535                         move_masked_irq(irq);
2536                 unmask_IO_APIC_irq(irq);
2537         }
2538
2539 #ifdef CONFIG_X86_32
2540         if (!(v & (1 << (i & 0x1f)))) {
2541                 atomic_inc(&irq_mis_count);
2542                 spin_lock(&ioapic_lock);
2543                 __mask_and_edge_IO_APIC_irq(irq);
2544                 __unmask_and_level_IO_APIC_irq(irq);
2545                 spin_unlock(&ioapic_lock);
2546         }
2547 #endif
2548 }
2549
2550 static struct irq_chip ioapic_chip __read_mostly = {
2551         .name           = "IO-APIC",
2552         .startup        = startup_ioapic_irq,
2553         .mask           = mask_IO_APIC_irq,
2554         .unmask         = unmask_IO_APIC_irq,
2555         .ack            = ack_apic_edge,
2556         .eoi            = ack_apic_level,
2557 #ifdef CONFIG_SMP
2558         .set_affinity   = set_ioapic_affinity_irq,
2559 #endif
2560         .retrigger      = ioapic_retrigger_irq,
2561 };
2562
2563 #ifdef CONFIG_INTR_REMAP
2564 static struct irq_chip ir_ioapic_chip __read_mostly = {
2565         .name           = "IR-IO-APIC",
2566         .startup        = startup_ioapic_irq,
2567         .mask           = mask_IO_APIC_irq,
2568         .unmask         = unmask_IO_APIC_irq,
2569         .ack            = ack_x2apic_edge,
2570         .eoi            = ack_x2apic_level,
2571 #ifdef CONFIG_SMP
2572         .set_affinity   = set_ir_ioapic_affinity_irq,
2573 #endif
2574         .retrigger      = ioapic_retrigger_irq,
2575 };
2576 #endif
2577
2578 static inline void init_IO_APIC_traps(void)
2579 {
2580         int irq;
2581         struct irq_desc *desc;
2582         struct irq_cfg *cfg;
2583
2584         /*
2585          * NOTE! The local APIC isn't very good at handling
2586          * multiple interrupts at the same interrupt level.
2587          * As the interrupt level is determined by taking the
2588          * vector number and shifting that right by 4, we
2589          * want to spread these out a bit so that they don't
2590          * all fall in the same interrupt level.
2591          *
2592          * Also, we've got to be careful not to trash gate
2593          * 0x80, because int 0x80 is hm, kind of importantish. ;)
2594          */
2595         for_each_irq_cfg(irq, cfg) {
2596                 if (IO_APIC_IRQ(irq) && !cfg->vector) {
2597                         /*
2598                          * Hmm.. We don't have an entry for this,
2599                          * so default to an old-fashioned 8259
2600                          * interrupt if we can..
2601                          */
2602                         if (irq < 16)
2603                                 make_8259A_irq(irq);
2604                         else {
2605                                 desc = irq_to_desc(irq);
2606                                 /* Strange. Oh, well.. */
2607                                 desc->chip = &no_irq_chip;
2608                         }
2609                 }
2610         }
2611 }
2612
2613 /*
2614  * The local APIC irq-chip implementation:
2615  */
2616
2617 static void mask_lapic_irq(unsigned int irq)
2618 {
2619         unsigned long v;
2620
2621         v = apic_read(APIC_LVT0);
2622         apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2623 }
2624
2625 static void unmask_lapic_irq(unsigned int irq)
2626 {
2627         unsigned long v;
2628
2629         v = apic_read(APIC_LVT0);
2630         apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2631 }
2632
2633 static void ack_lapic_irq (unsigned int irq)
2634 {
2635         ack_APIC_irq();
2636 }
2637
2638 static struct irq_chip lapic_chip __read_mostly = {
2639         .name           = "local-APIC",
2640         .mask           = mask_lapic_irq,
2641         .unmask         = unmask_lapic_irq,
2642         .ack            = ack_lapic_irq,
2643 };
2644
2645 static void lapic_register_intr(int irq)
2646 {
2647         struct irq_desc *desc;
2648
2649         desc = irq_to_desc(irq);
2650         desc->status &= ~IRQ_LEVEL;
2651         set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2652                                       "edge");
2653 }
2654
2655 static void __init setup_nmi(void)
2656 {
2657         /*
2658          * Dirty trick to enable the NMI watchdog ...
2659          * We put the 8259A master into AEOI mode and
2660          * unmask on all local APICs LVT0 as NMI.
2661          *
2662          * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2663          * is from Maciej W. Rozycki - so we do not have to EOI from
2664          * the NMI handler or the timer interrupt.
2665          */
2666         apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2667
2668         enable_NMI_through_LVT0();
2669
2670         apic_printk(APIC_VERBOSE, " done.\n");
2671 }
2672
2673 /*
2674  * This looks a bit hackish but it's about the only one way of sending
2675  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
2676  * not support the ExtINT mode, unfortunately.  We need to send these
2677  * cycles as some i82489DX-based boards have glue logic that keeps the
2678  * 8259A interrupt line asserted until INTA.  --macro
2679  */
2680 static inline void __init unlock_ExtINT_logic(void)
2681 {
2682         int apic, pin, i;
2683         struct IO_APIC_route_entry entry0, entry1;
2684         unsigned char save_control, save_freq_select;
2685
2686         pin  = find_isa_irq_pin(8, mp_INT);
2687         if (pin == -1) {
2688                 WARN_ON_ONCE(1);
2689                 return;
2690         }
2691         apic = find_isa_irq_apic(8, mp_INT);
2692         if (apic == -1) {
2693                 WARN_ON_ONCE(1);
2694                 return;
2695         }
2696
2697         entry0 = ioapic_read_entry(apic, pin);
2698         clear_IO_APIC_pin(apic, pin);
2699
2700         memset(&entry1, 0, sizeof(entry1));
2701
2702         entry1.dest_mode = 0;                   /* physical delivery */
2703         entry1.mask = 0;                        /* unmask IRQ now */
2704         entry1.dest = hard_smp_processor_id();
2705         entry1.delivery_mode = dest_ExtINT;
2706         entry1.polarity = entry0.polarity;
2707         entry1.trigger = 0;
2708         entry1.vector = 0;
2709
2710         ioapic_write_entry(apic, pin, entry1);
2711
2712         save_control = CMOS_READ(RTC_CONTROL);
2713         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2714         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2715                    RTC_FREQ_SELECT);
2716         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2717
2718         i = 100;
2719         while (i-- > 0) {
2720                 mdelay(10);
2721                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2722                         i -= 10;
2723         }
2724
2725         CMOS_WRITE(save_control, RTC_CONTROL);
2726         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2727         clear_IO_APIC_pin(apic, pin);
2728
2729         ioapic_write_entry(apic, pin, entry0);
2730 }
2731
2732 static int disable_timer_pin_1 __initdata;
2733 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
2734 static int __init disable_timer_pin_setup(char *arg)
2735 {
2736         disable_timer_pin_1 = 1;
2737         return 0;
2738 }
2739 early_param("disable_timer_pin_1", disable_timer_pin_setup);
2740
2741 int timer_through_8259 __initdata;
2742
2743 /*
2744  * This code may look a bit paranoid, but it's supposed to cooperate with
2745  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
2746  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
2747  * fanatically on his truly buggy board.
2748  *
2749  * FIXME: really need to revamp this for all platforms.
2750  */
2751 static inline void __init check_timer(void)
2752 {
2753         struct irq_cfg *cfg = irq_cfg(0);
2754         int apic1, pin1, apic2, pin2;
2755         unsigned long flags;
2756         unsigned int ver;
2757         int no_pin1 = 0;
2758
2759         local_irq_save(flags);
2760
2761         ver = apic_read(APIC_LVR);
2762         ver = GET_APIC_VERSION(ver);
2763
2764         /*
2765          * get/set the timer IRQ vector:
2766          */
2767         disable_8259A_irq(0);
2768         assign_irq_vector(0, TARGET_CPUS);
2769
2770         /*
2771          * As IRQ0 is to be enabled in the 8259A, the virtual
2772          * wire has to be disabled in the local APIC.  Also
2773          * timer interrupts need to be acknowledged manually in
2774          * the 8259A for the i82489DX when using the NMI
2775          * watchdog as that APIC treats NMIs as level-triggered.
2776          * The AEOI mode will finish them in the 8259A
2777          * automatically.
2778          */
2779         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2780         init_8259A(1);
2781 #ifdef CONFIG_X86_32
2782         timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2783 #endif
2784
2785         pin1  = find_isa_irq_pin(0, mp_INT);
2786         apic1 = find_isa_irq_apic(0, mp_INT);
2787         pin2  = ioapic_i8259.pin;
2788         apic2 = ioapic_i8259.apic;
2789
2790         apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2791                     "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2792                     cfg->vector, apic1, pin1, apic2, pin2);
2793
2794         /*
2795          * Some BIOS writers are clueless and report the ExtINTA
2796          * I/O APIC input from the cascaded 8259A as the timer
2797          * interrupt input.  So just in case, if only one pin
2798          * was found above, try it both directly and through the
2799          * 8259A.
2800          */
2801         if (pin1 == -1) {
2802 #ifdef CONFIG_INTR_REMAP
2803                 if (intr_remapping_enabled)
2804                         panic("BIOS bug: timer not connected to IO-APIC");
2805 #endif
2806                 pin1 = pin2;
2807                 apic1 = apic2;
2808                 no_pin1 = 1;
2809         } else if (pin2 == -1) {
2810                 pin2 = pin1;
2811                 apic2 = apic1;
2812         }
2813
2814         if (pin1 != -1) {
2815                 /*
2816                  * Ok, does IRQ0 through the IOAPIC work?
2817                  */
2818                 if (no_pin1) {
2819                         add_pin_to_irq(0, apic1, pin1);
2820                         setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2821                 }
2822                 unmask_IO_APIC_irq(0);
2823                 if (timer_irq_works()) {
2824                         if (nmi_watchdog == NMI_IO_APIC) {
2825                                 setup_nmi();
2826                                 enable_8259A_irq(0);
2827                         }
2828                         if (disable_timer_pin_1 > 0)
2829                                 clear_IO_APIC_pin(0, pin1);
2830                         goto out;
2831                 }
2832 #ifdef CONFIG_INTR_REMAP
2833                 if (intr_remapping_enabled)
2834                         panic("timer doesn't work through Interrupt-remapped IO-APIC");
2835 #endif
2836                 clear_IO_APIC_pin(apic1, pin1);
2837                 if (!no_pin1)
2838                         apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2839                                     "8254 timer not connected to IO-APIC\n");
2840
2841                 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2842                             "(IRQ0) through the 8259A ...\n");
2843                 apic_printk(APIC_QUIET, KERN_INFO
2844                             "..... (found apic %d pin %d) ...\n", apic2, pin2);
2845                 /*
2846                  * legacy devices should be connected to IO APIC #0
2847                  */
2848                 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
2849                 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2850                 unmask_IO_APIC_irq(0);
2851                 enable_8259A_irq(0);
2852                 if (timer_irq_works()) {
2853                         apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2854                         timer_through_8259 = 1;
2855                         if (nmi_watchdog == NMI_IO_APIC) {
2856                                 disable_8259A_irq(0);
2857                                 setup_nmi();
2858                                 enable_8259A_irq(0);
2859                         }
2860                         goto out;
2861                 }
2862                 /*
2863                  * Cleanup, just in case ...
2864                  */
2865                 disable_8259A_irq(0);
2866                 clear_IO_APIC_pin(apic2, pin2);
2867                 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2868         }
2869
2870         if (nmi_watchdog == NMI_IO_APIC) {
2871                 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2872                             "through the IO-APIC - disabling NMI Watchdog!\n");
2873                 nmi_watchdog = NMI_NONE;
2874         }
2875 #ifdef CONFIG_X86_32
2876         timer_ack = 0;
2877 #endif
2878
2879         apic_printk(APIC_QUIET, KERN_INFO
2880                     "...trying to set up timer as Virtual Wire IRQ...\n");
2881
2882         lapic_register_intr(0);
2883         apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector);     /* Fixed mode */
2884         enable_8259A_irq(0);
2885
2886         if (timer_irq_works()) {
2887                 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2888                 goto out;
2889         }
2890         disable_8259A_irq(0);
2891         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2892         apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2893
2894         apic_printk(APIC_QUIET, KERN_INFO
2895                     "...trying to set up timer as ExtINT IRQ...\n");
2896
2897         init_8259A(0);
2898         make_8259A_irq(0);
2899         apic_write(APIC_LVT0, APIC_DM_EXTINT);
2900
2901         unlock_ExtINT_logic();
2902
2903         if (timer_irq_works()) {
2904                 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2905                 goto out;
2906         }
2907         apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2908         panic("IO-APIC + timer doesn't work!  Boot with apic=debug and send a "
2909                 "report.  Then try booting with the 'noapic' option.\n");
2910 out:
2911         local_irq_restore(flags);
2912 }
2913
2914 /*
2915  * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2916  * to devices.  However there may be an I/O APIC pin available for
2917  * this interrupt regardless.  The pin may be left unconnected, but
2918  * typically it will be reused as an ExtINT cascade interrupt for
2919  * the master 8259A.  In the MPS case such a pin will normally be
2920  * reported as an ExtINT interrupt in the MP table.  With ACPI
2921  * there is no provision for ExtINT interrupts, and in the absence
2922  * of an override it would be treated as an ordinary ISA I/O APIC
2923  * interrupt, that is edge-triggered and unmasked by default.  We
2924  * used to do this, but it caused problems on some systems because
2925  * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2926  * the same ExtINT cascade interrupt to drive the local APIC of the
2927  * bootstrap processor.  Therefore we refrain from routing IRQ2 to
2928  * the I/O APIC in all cases now.  No actual device should request
2929  * it anyway.  --macro
2930  */
2931 #define PIC_IRQS        (1 << PIC_CASCADE_IR)
2932
2933 void __init setup_IO_APIC(void)
2934 {
2935
2936 #ifdef CONFIG_X86_32
2937         enable_IO_APIC();
2938 #else
2939         /*
2940          * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2941          */
2942 #endif
2943
2944         io_apic_irqs = ~PIC_IRQS;
2945
2946         apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2947         /*
2948          * Set up IO-APIC IRQ routing.
2949          */
2950 #ifdef CONFIG_X86_32
2951         if (!acpi_ioapic)
2952                 setup_ioapic_ids_from_mpc();
2953 #endif
2954         sync_Arb_IDs();
2955         setup_IO_APIC_irqs();
2956         init_IO_APIC_traps();
2957         check_timer();
2958 }
2959
2960 /*
2961  *      Called after all the initialization is done. If we didnt find any
2962  *      APIC bugs then we can allow the modify fast path
2963  */
2964
2965 static int __init io_apic_bug_finalize(void)
2966 {
2967         if (sis_apic_bug == -1)
2968                 sis_apic_bug = 0;
2969         return 0;
2970 }
2971
2972 late_initcall(io_apic_bug_finalize);
2973
2974 struct sysfs_ioapic_data {
2975         struct sys_device dev;
2976         struct IO_APIC_route_entry entry[0];
2977 };
2978 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2979
2980 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2981 {
2982         struct IO_APIC_route_entry *entry;
2983         struct sysfs_ioapic_data *data;
2984         int i;
2985
2986         data = container_of(dev, struct sysfs_ioapic_data, dev);
2987         entry = data->entry;
2988         for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2989                 *entry = ioapic_read_entry(dev->id, i);
2990
2991         return 0;
2992 }
2993
2994 static int ioapic_resume(struct sys_device *dev)
2995 {
2996         struct IO_APIC_route_entry *entry;
2997         struct sysfs_ioapic_data *data;
2998         unsigned long flags;
2999         union IO_APIC_reg_00 reg_00;
3000         int i;
3001
3002         data = container_of(dev, struct sysfs_ioapic_data, dev);
3003         entry = data->entry;
3004
3005         spin_lock_irqsave(&ioapic_lock, flags);
3006         reg_00.raw = io_apic_read(dev->id, 0);
3007         if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
3008                 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
3009                 io_apic_write(dev->id, 0, reg_00.raw);
3010         }
3011         spin_unlock_irqrestore(&ioapic_lock, flags);
3012         for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
3013                 ioapic_write_entry(dev->id, i, entry[i]);
3014
3015         return 0;
3016 }
3017
3018 static struct sysdev_class ioapic_sysdev_class = {
3019         .name = "ioapic",
3020         .suspend = ioapic_suspend,
3021         .resume = ioapic_resume,
3022 };
3023
3024 static int __init ioapic_init_sysfs(void)
3025 {
3026         struct sys_device * dev;
3027         int i, size, error;
3028
3029         error = sysdev_class_register(&ioapic_sysdev_class);
3030         if (error)
3031                 return error;
3032
3033         for (i = 0; i < nr_ioapics; i++ ) {
3034                 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
3035                         * sizeof(struct IO_APIC_route_entry);
3036                 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
3037                 if (!mp_ioapic_data[i]) {
3038                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3039                         continue;
3040                 }
3041                 dev = &mp_ioapic_data[i]->dev;
3042                 dev->id = i;
3043                 dev->cls = &ioapic_sysdev_class;
3044                 error = sysdev_register(dev);
3045                 if (error) {
3046                         kfree(mp_ioapic_data[i]);
3047                         mp_ioapic_data[i] = NULL;
3048                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3049                         continue;
3050                 }
3051         }
3052
3053         return 0;
3054 }
3055
3056 device_initcall(ioapic_init_sysfs);
3057
3058 /*
3059  * Dynamic irq allocate and deallocation
3060  */
3061 unsigned int create_irq_nr(unsigned int irq_want)
3062 {
3063         /* Allocate an unused irq */
3064         unsigned int irq;
3065         unsigned int new;
3066         unsigned long flags;
3067         struct irq_cfg *cfg_new;
3068
3069 #ifndef CONFIG_HAVE_SPARSE_IRQ
3070         irq_want = nr_irqs - 1;
3071 #endif
3072
3073         irq = 0;
3074         spin_lock_irqsave(&vector_lock, flags);
3075         for (new = irq_want; new > 0; new--) {
3076                 if (platform_legacy_irq(new))
3077                         continue;
3078                 cfg_new = irq_cfg(new);
3079                 if (cfg_new && cfg_new->vector != 0)
3080                         continue;
3081                 /* check if need to create one */
3082                 if (!cfg_new)
3083                         cfg_new = irq_cfg_alloc(new);
3084                 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
3085                         irq = new;
3086                 break;
3087         }
3088         spin_unlock_irqrestore(&vector_lock, flags);
3089
3090         if (irq > 0) {
3091                 dynamic_irq_init(irq);
3092         }
3093         return irq;
3094 }
3095
3096 int create_irq(void)
3097 {
3098         int irq;
3099
3100         irq = create_irq_nr(nr_irqs - 1);
3101
3102         if (irq == 0)
3103                 irq = -1;
3104
3105         return irq;
3106 }
3107
3108 void destroy_irq(unsigned int irq)
3109 {
3110         unsigned long flags;
3111
3112         dynamic_irq_cleanup(irq);
3113
3114 #ifdef CONFIG_INTR_REMAP
3115         free_irte(irq);
3116 #endif
3117         spin_lock_irqsave(&vector_lock, flags);
3118         __clear_irq_vector(irq);
3119         spin_unlock_irqrestore(&vector_lock, flags);
3120 }
3121
3122 /*
3123  * MSI message composition
3124  */
3125 #ifdef CONFIG_PCI_MSI
3126 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
3127 {
3128         struct irq_cfg *cfg;
3129         int err;
3130         unsigned dest;
3131         cpumask_t tmp;
3132
3133         tmp = TARGET_CPUS;
3134         err = assign_irq_vector(irq, tmp);
3135         if (err)
3136                 return err;
3137
3138         cfg = irq_cfg(irq);
3139         cpus_and(tmp, cfg->domain, tmp);
3140         dest = cpu_mask_to_apicid(tmp);
3141
3142 #ifdef CONFIG_INTR_REMAP
3143         if (irq_remapped(irq)) {
3144                 struct irte irte;
3145                 int ir_index;
3146                 u16 sub_handle;
3147
3148                 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3149                 BUG_ON(ir_index == -1);
3150
3151                 memset (&irte, 0, sizeof(irte));
3152
3153                 irte.present = 1;
3154                 irte.dst_mode = INT_DEST_MODE;
3155                 irte.trigger_mode = 0; /* edge */
3156                 irte.dlvry_mode = INT_DELIVERY_MODE;
3157                 irte.vector = cfg->vector;
3158                 irte.dest_id = IRTE_DEST(dest);
3159
3160                 modify_irte(irq, &irte);
3161
3162                 msg->address_hi = MSI_ADDR_BASE_HI;
3163                 msg->data = sub_handle;
3164                 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3165                                   MSI_ADDR_IR_SHV |
3166                                   MSI_ADDR_IR_INDEX1(ir_index) |
3167                                   MSI_ADDR_IR_INDEX2(ir_index);
3168         } else
3169 #endif
3170         {
3171                 msg->address_hi = MSI_ADDR_BASE_HI;
3172                 msg->address_lo =
3173                         MSI_ADDR_BASE_LO |
3174                         ((INT_DEST_MODE == 0) ?
3175                                 MSI_ADDR_DEST_MODE_PHYSICAL:
3176                                 MSI_ADDR_DEST_MODE_LOGICAL) |
3177                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3178                                 MSI_ADDR_REDIRECTION_CPU:
3179                                 MSI_ADDR_REDIRECTION_LOWPRI) |
3180                         MSI_ADDR_DEST_ID(dest);
3181
3182                 msg->data =
3183                         MSI_DATA_TRIGGER_EDGE |
3184                         MSI_DATA_LEVEL_ASSERT |
3185                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3186                                 MSI_DATA_DELIVERY_FIXED:
3187                                 MSI_DATA_DELIVERY_LOWPRI) |
3188                         MSI_DATA_VECTOR(cfg->vector);
3189         }
3190         return err;
3191 }
3192
3193 #ifdef CONFIG_SMP
3194 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3195 {
3196         struct irq_cfg *cfg;
3197         struct msi_msg msg;
3198         unsigned int dest;
3199         cpumask_t tmp;
3200         struct irq_desc *desc;
3201
3202         cpus_and(tmp, mask, cpu_online_map);
3203         if (cpus_empty(tmp))
3204                 return;
3205
3206         if (assign_irq_vector(irq, mask))
3207                 return;
3208
3209         cfg = irq_cfg(irq);
3210         cpus_and(tmp, cfg->domain, mask);
3211         dest = cpu_mask_to_apicid(tmp);
3212
3213         read_msi_msg(irq, &msg);
3214
3215         msg.data &= ~MSI_DATA_VECTOR_MASK;
3216         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3217         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3218         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3219
3220         write_msi_msg(irq, &msg);
3221         desc = irq_to_desc(irq);
3222         desc->affinity = mask;
3223 }
3224
3225 #ifdef CONFIG_INTR_REMAP
3226 /*
3227  * Migrate the MSI irq to another cpumask. This migration is
3228  * done in the process context using interrupt-remapping hardware.
3229  */
3230 static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3231 {
3232         struct irq_cfg *cfg;
3233         unsigned int dest;
3234         cpumask_t tmp, cleanup_mask;
3235         struct irte irte;
3236         struct irq_desc *desc;
3237
3238         cpus_and(tmp, mask, cpu_online_map);
3239         if (cpus_empty(tmp))
3240                 return;
3241
3242         if (get_irte(irq, &irte))
3243                 return;
3244
3245         if (assign_irq_vector(irq, mask))
3246                 return;
3247
3248         cfg = irq_cfg(irq);
3249         cpus_and(tmp, cfg->domain, mask);
3250         dest = cpu_mask_to_apicid(tmp);
3251
3252         irte.vector = cfg->vector;
3253         irte.dest_id = IRTE_DEST(dest);
3254
3255         /*
3256          * atomically update the IRTE with the new destination and vector.
3257          */
3258         modify_irte(irq, &irte);
3259
3260         /*
3261          * After this point, all the interrupts will start arriving
3262          * at the new destination. So, time to cleanup the previous
3263          * vector allocation.
3264          */
3265         if (cfg->move_in_progress) {
3266                 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3267                 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3268                 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3269                 cfg->move_in_progress = 0;
3270         }
3271
3272         desc = irq_to_desc(irq);
3273         desc->affinity = mask;
3274 }
3275 #endif
3276 #endif /* CONFIG_SMP */
3277
3278 /*
3279  * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3280  * which implement the MSI or MSI-X Capability Structure.
3281  */
3282 static struct irq_chip msi_chip = {
3283         .name           = "PCI-MSI",
3284         .unmask         = unmask_msi_irq,
3285         .mask           = mask_msi_irq,
3286         .ack            = ack_apic_edge,
3287 #ifdef CONFIG_SMP
3288         .set_affinity   = set_msi_irq_affinity,
3289 #endif
3290         .retrigger      = ioapic_retrigger_irq,
3291 };
3292
3293 #ifdef CONFIG_INTR_REMAP
3294 static struct irq_chip msi_ir_chip = {
3295         .name           = "IR-PCI-MSI",
3296         .unmask         = unmask_msi_irq,
3297         .mask           = mask_msi_irq,
3298         .ack            = ack_x2apic_edge,
3299 #ifdef CONFIG_SMP
3300         .set_affinity   = ir_set_msi_irq_affinity,
3301 #endif
3302         .retrigger      = ioapic_retrigger_irq,
3303 };
3304
3305 /*
3306  * Map the PCI dev to the corresponding remapping hardware unit
3307  * and allocate 'nvec' consecutive interrupt-remapping table entries
3308  * in it.
3309  */
3310 static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3311 {
3312         struct intel_iommu *iommu;
3313         int index;
3314
3315         iommu = map_dev_to_ir(dev);
3316         if (!iommu) {
3317                 printk(KERN_ERR
3318                        "Unable to map PCI %s to iommu\n", pci_name(dev));
3319                 return -ENOENT;
3320         }
3321
3322         index = alloc_irte(iommu, irq, nvec);
3323         if (index < 0) {
3324                 printk(KERN_ERR
3325                        "Unable to allocate %d IRTE for PCI %s\n", nvec,
3326                         pci_name(dev));
3327                 return -ENOSPC;
3328         }
3329         return index;
3330 }
3331 #endif
3332
3333 static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3334 {
3335         int ret;
3336         struct msi_msg msg;
3337
3338         ret = msi_compose_msg(dev, irq, &msg);
3339         if (ret < 0)
3340                 return ret;
3341
3342         set_irq_msi(irq, desc);
3343         write_msi_msg(irq, &msg);
3344
3345 #ifdef CONFIG_INTR_REMAP
3346         if (irq_remapped(irq)) {
3347                 struct irq_desc *desc = irq_to_desc(irq);
3348                 /*
3349                  * irq migration in process context
3350                  */
3351                 desc->status |= IRQ_MOVE_PCNTXT;
3352                 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3353         } else
3354 #endif
3355                 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
3356
3357         return 0;
3358 }
3359
3360 static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3361 {
3362         unsigned int irq;
3363
3364         irq = dev->bus->number;
3365         irq <<= 8;
3366         irq |= dev->devfn;
3367         irq <<= 12;
3368
3369         return irq;
3370 }
3371
3372 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
3373 {
3374         unsigned int irq;
3375         int ret;
3376         unsigned int irq_want;
3377
3378         irq_want = build_irq_for_pci_dev(dev) + 0x100;
3379
3380         irq = create_irq_nr(irq_want);
3381         if (irq == 0)
3382                 return -1;
3383
3384 #ifdef CONFIG_INTR_REMAP
3385         if (!intr_remapping_enabled)
3386                 goto no_ir;
3387
3388         ret = msi_alloc_irte(dev, irq, 1);
3389         if (ret < 0)
3390                 goto error;
3391 no_ir:
3392 #endif
3393         ret = setup_msi_irq(dev, desc, irq);
3394         if (ret < 0) {
3395                 destroy_irq(irq);
3396                 return ret;
3397         }
3398         return 0;
3399
3400 #ifdef CONFIG_INTR_REMAP
3401 error:
3402         destroy_irq(irq);
3403         return ret;
3404 #endif
3405 }
3406
3407 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3408 {
3409         unsigned int irq;
3410         int ret, sub_handle;
3411         struct msi_desc *desc;
3412         unsigned int irq_want;
3413
3414 #ifdef CONFIG_INTR_REMAP
3415         struct intel_iommu *iommu = 0;
3416         int index = 0;
3417 #endif
3418
3419         irq_want = build_irq_for_pci_dev(dev) + 0x100;
3420         sub_handle = 0;
3421         list_for_each_entry(desc, &dev->msi_list, list) {
3422                 irq = create_irq_nr(irq_want--);
3423                 if (irq == 0)
3424                         return -1;
3425 #ifdef CONFIG_INTR_REMAP
3426                 if (!intr_remapping_enabled)
3427                         goto no_ir;
3428
3429                 if (!sub_handle) {
3430                         /*
3431                          * allocate the consecutive block of IRTE's
3432                          * for 'nvec'
3433                          */
3434                         index = msi_alloc_irte(dev, irq, nvec);
3435                         if (index < 0) {
3436                                 ret = index;
3437                                 goto error;
3438                         }
3439                 } else {
3440                         iommu = map_dev_to_ir(dev);
3441                         if (!iommu) {
3442                                 ret = -ENOENT;
3443                                 goto error;
3444                         }
3445                         /*
3446                          * setup the mapping between the irq and the IRTE
3447                          * base index, the sub_handle pointing to the
3448                          * appropriate interrupt remap table entry.
3449                          */
3450                         set_irte_irq(irq, iommu, index, sub_handle);
3451                 }
3452 no_ir:
3453 #endif
3454                 ret = setup_msi_irq(dev, desc, irq);
3455                 if (ret < 0)
3456                         goto error;
3457                 sub_handle++;
3458         }
3459         return 0;
3460
3461 error:
3462         destroy_irq(irq);
3463         return ret;
3464 }
3465
3466 void arch_teardown_msi_irq(unsigned int irq)
3467 {
3468         destroy_irq(irq);
3469 }
3470
3471 #ifdef CONFIG_DMAR
3472 #ifdef CONFIG_SMP
3473 static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3474 {
3475         struct irq_cfg *cfg;
3476         struct msi_msg msg;
3477         unsigned int dest;
3478         cpumask_t tmp;
3479         struct irq_desc *desc;
3480
3481         cpus_and(tmp, mask, cpu_online_map);
3482         if (cpus_empty(tmp))
3483                 return;
3484
3485         if (assign_irq_vector(irq, mask))
3486                 return;
3487
3488         cfg = irq_cfg(irq);
3489         cpus_and(tmp, cfg->domain, mask);
3490         dest = cpu_mask_to_apicid(tmp);
3491
3492         dmar_msi_read(irq, &msg);
3493
3494         msg.data &= ~MSI_DATA_VECTOR_MASK;
3495         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3496         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3497         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3498
3499         dmar_msi_write(irq, &msg);
3500         desc = irq_to_desc(irq);
3501         desc->affinity = mask;
3502 }
3503 #endif /* CONFIG_SMP */
3504
3505 struct irq_chip dmar_msi_type = {
3506         .name = "DMAR_MSI",
3507         .unmask = dmar_msi_unmask,
3508         .mask = dmar_msi_mask,
3509         .ack = ack_apic_edge,
3510 #ifdef CONFIG_SMP
3511         .set_affinity = dmar_msi_set_affinity,
3512 #endif
3513         .retrigger = ioapic_retrigger_irq,
3514 };
3515
3516 int arch_setup_dmar_msi(unsigned int irq)
3517 {
3518         int ret;
3519         struct msi_msg msg;
3520
3521         ret = msi_compose_msg(NULL, irq, &msg);
3522         if (ret < 0)
3523                 return ret;
3524         dmar_msi_write(irq, &msg);
3525         set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3526                 "edge");
3527         return 0;
3528 }
3529 #endif
3530
3531 #ifdef CONFIG_HPET_TIMER
3532
3533 #ifdef CONFIG_SMP
3534 static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
3535 {
3536         struct irq_cfg *cfg;
3537         struct irq_desc *desc;
3538         struct msi_msg msg;
3539         unsigned int dest;
3540         cpumask_t tmp;
3541
3542         cpus_and(tmp, mask, cpu_online_map);
3543         if (cpus_empty(tmp))
3544                 return;
3545
3546         if (assign_irq_vector(irq, mask))
3547                 return;
3548
3549         cfg = irq_cfg(irq);
3550         cpus_and(tmp, cfg->domain, mask);
3551         dest = cpu_mask_to_apicid(tmp);
3552
3553         hpet_msi_read(irq, &msg);
3554
3555         msg.data &= ~MSI_DATA_VECTOR_MASK;
3556         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3557         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3558         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3559
3560         hpet_msi_write(irq, &msg);
3561         desc = irq_to_desc(irq);
3562         desc->affinity = mask;
3563 }
3564 #endif /* CONFIG_SMP */
3565
3566 struct irq_chip hpet_msi_type = {
3567         .name = "HPET_MSI",
3568         .unmask = hpet_msi_unmask,
3569         .mask = hpet_msi_mask,
3570         .ack = ack_apic_edge,
3571 #ifdef CONFIG_SMP
3572         .set_affinity = hpet_msi_set_affinity,
3573 #endif
3574         .retrigger = ioapic_retrigger_irq,
3575 };
3576
3577 int arch_setup_hpet_msi(unsigned int irq)
3578 {
3579         int ret;
3580         struct msi_msg msg;
3581
3582         ret = msi_compose_msg(NULL, irq, &msg);
3583         if (ret < 0)
3584                 return ret;
3585
3586         hpet_msi_write(irq, &msg);
3587         set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3588                 "edge");
3589         return 0;
3590 }
3591 #endif
3592
3593 #endif /* CONFIG_PCI_MSI */
3594 /*
3595  * Hypertransport interrupt support
3596  */
3597 #ifdef CONFIG_HT_IRQ
3598
3599 #ifdef CONFIG_SMP
3600
3601 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
3602 {
3603         struct ht_irq_msg msg;
3604         fetch_ht_irq_msg(irq, &msg);
3605
3606         msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
3607         msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
3608
3609         msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
3610         msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
3611
3612         write_ht_irq_msg(irq, &msg);
3613 }
3614
3615 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3616 {
3617         struct irq_cfg *cfg;
3618         unsigned int dest;
3619         cpumask_t tmp;
3620         struct irq_desc *desc;
3621
3622         cpus_and(tmp, mask, cpu_online_map);
3623         if (cpus_empty(tmp))
3624                 return;
3625
3626         if (assign_irq_vector(irq, mask))
3627                 return;
3628
3629         cfg = irq_cfg(irq);
3630         cpus_and(tmp, cfg->domain, mask);
3631         dest = cpu_mask_to_apicid(tmp);
3632
3633         target_ht_irq(irq, dest, cfg->vector);
3634         desc = irq_to_desc(irq);
3635         desc->affinity = mask;
3636 }
3637 #endif
3638
3639 static struct irq_chip ht_irq_chip = {
3640         .name           = "PCI-HT",
3641         .mask           = mask_ht_irq,
3642         .unmask         = unmask_ht_irq,
3643         .ack            = ack_apic_edge,
3644 #ifdef CONFIG_SMP
3645         .set_affinity   = set_ht_irq_affinity,
3646 #endif
3647         .retrigger      = ioapic_retrigger_irq,
3648 };
3649
3650 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3651 {
3652         struct irq_cfg *cfg;
3653         int err;
3654         cpumask_t tmp;
3655
3656         tmp = TARGET_CPUS;
3657         err = assign_irq_vector(irq, tmp);
3658         if (!err) {
3659                 struct ht_irq_msg msg;
3660                 unsigned dest;
3661
3662                 cfg = irq_cfg(irq);
3663                 cpus_and(tmp, cfg->domain, tmp);
3664                 dest = cpu_mask_to_apicid(tmp);
3665
3666                 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
3667
3668                 msg.address_lo =
3669                         HT_IRQ_LOW_BASE |
3670                         HT_IRQ_LOW_DEST_ID(dest) |
3671                         HT_IRQ_LOW_VECTOR(cfg->vector) |
3672                         ((INT_DEST_MODE == 0) ?
3673                                 HT_IRQ_LOW_DM_PHYSICAL :
3674                                 HT_IRQ_LOW_DM_LOGICAL) |
3675                         HT_IRQ_LOW_RQEOI_EDGE |
3676                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3677                                 HT_IRQ_LOW_MT_FIXED :
3678                                 HT_IRQ_LOW_MT_ARBITRATED) |
3679                         HT_IRQ_LOW_IRQ_MASKED;
3680
3681                 write_ht_irq_msg(irq, &msg);
3682
3683                 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3684                                               handle_edge_irq, "edge");
3685         }
3686         return err;
3687 }
3688 #endif /* CONFIG_HT_IRQ */
3689
3690 int __init io_apic_get_redir_entries (int ioapic)
3691 {
3692         union IO_APIC_reg_01    reg_01;
3693         unsigned long flags;
3694
3695         spin_lock_irqsave(&ioapic_lock, flags);
3696         reg_01.raw = io_apic_read(ioapic, 1);
3697         spin_unlock_irqrestore(&ioapic_lock, flags);
3698
3699         return reg_01.bits.entries;
3700 }
3701
3702 int __init probe_nr_irqs(void)
3703 {
3704         int idx;
3705         int nr = 0;
3706 #ifndef CONFIG_XEN
3707         int nr_min = 32;
3708 #else
3709         int nr_min = NR_IRQS;
3710 #endif
3711
3712         for (idx = 0; idx < nr_ioapics; idx++)
3713                 nr += io_apic_get_redir_entries(idx) + 1;
3714
3715         /* double it for hotplug and msi and nmi */
3716         nr <<= 1;
3717
3718         /* something wrong ? */
3719         if (nr < nr_min)
3720                 nr = nr_min;
3721
3722         return nr;
3723 }
3724
3725 /* --------------------------------------------------------------------------
3726                           ACPI-based IOAPIC Configuration
3727    -------------------------------------------------------------------------- */
3728
3729 #ifdef CONFIG_ACPI
3730
3731 #ifdef CONFIG_X86_32
3732 int __init io_apic_get_unique_id(int ioapic, int apic_id)
3733 {
3734         union IO_APIC_reg_00 reg_00;
3735         static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3736         physid_mask_t tmp;
3737         unsigned long flags;
3738         int i = 0;
3739
3740         /*
3741          * The P4 platform supports up to 256 APIC IDs on two separate APIC
3742          * buses (one for LAPICs, one for IOAPICs), where predecessors only
3743          * supports up to 16 on one shared APIC bus.
3744          *
3745          * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3746          *      advantage of new APIC bus architecture.
3747          */
3748
3749         if (physids_empty(apic_id_map))
3750                 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3751
3752         spin_lock_irqsave(&ioapic_lock, flags);
3753         reg_00.raw = io_apic_read(ioapic, 0);
3754         spin_unlock_irqrestore(&ioapic_lock, flags);
3755
3756         if (apic_id >= get_physical_broadcast()) {
3757                 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3758                         "%d\n", ioapic, apic_id, reg_00.bits.ID);
3759                 apic_id = reg_00.bits.ID;
3760         }
3761
3762         /*
3763          * Every APIC in a system must have a unique ID or we get lots of nice
3764          * 'stuck on smp_invalidate_needed IPI wait' messages.
3765          */
3766         if (check_apicid_used(apic_id_map, apic_id)) {
3767
3768                 for (i = 0; i < get_physical_broadcast(); i++) {
3769                         if (!check_apicid_used(apic_id_map, i))
3770                                 break;
3771                 }
3772
3773                 if (i == get_physical_broadcast())
3774                         panic("Max apic_id exceeded!\n");
3775
3776                 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3777                         "trying %d\n", ioapic, apic_id, i);
3778
3779                 apic_id = i;
3780         }
3781
3782         tmp = apicid_to_cpu_present(apic_id);
3783         physids_or(apic_id_map, apic_id_map, tmp);
3784
3785         if (reg_00.bits.ID != apic_id) {
3786                 reg_00.bits.ID = apic_id;
3787
3788                 spin_lock_irqsave(&ioapic_lock, flags);
3789                 io_apic_write(ioapic, 0, reg_00.raw);
3790                 reg_00.raw = io_apic_read(ioapic, 0);
3791                 spin_unlock_irqrestore(&ioapic_lock, flags);
3792
3793                 /* Sanity check */
3794                 if (reg_00.bits.ID != apic_id) {
3795                         printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3796                         return -1;
3797                 }
3798         }
3799
3800         apic_printk(APIC_VERBOSE, KERN_INFO
3801                         "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3802
3803         return apic_id;
3804 }
3805
3806 int __init io_apic_get_version(int ioapic)
3807 {
3808         union IO_APIC_reg_01    reg_01;
3809         unsigned long flags;
3810
3811         spin_lock_irqsave(&ioapic_lock, flags);
3812         reg_01.raw = io_apic_read(ioapic, 1);
3813         spin_unlock_irqrestore(&ioapic_lock, flags);
3814
3815         return reg_01.bits.version;
3816 }
3817 #endif
3818
3819 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
3820 {
3821         if (!IO_APIC_IRQ(irq)) {
3822                 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3823                         ioapic);
3824                 return -EINVAL;
3825         }
3826
3827         /*
3828          * IRQs < 16 are already in the irq_2_pin[] map
3829          */
3830         if (irq >= 16)
3831                 add_pin_to_irq(irq, ioapic, pin);
3832
3833         setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
3834
3835         return 0;
3836 }
3837
3838
3839 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3840 {
3841         int i;
3842
3843         if (skip_ioapic_setup)
3844                 return -1;
3845
3846         for (i = 0; i < mp_irq_entries; i++)
3847                 if (mp_irqs[i].mp_irqtype == mp_INT &&
3848                     mp_irqs[i].mp_srcbusirq == bus_irq)
3849                         break;
3850         if (i >= mp_irq_entries)
3851                 return -1;
3852
3853         *trigger = irq_trigger(i);
3854         *polarity = irq_polarity(i);
3855         return 0;
3856 }
3857
3858 #endif /* CONFIG_ACPI */
3859
3860 /*
3861  * This function currently is only a helper for the i386 smp boot process where
3862  * we need to reprogram the ioredtbls to cater for the cpus which have come online
3863  * so mask in all cases should simply be TARGET_CPUS
3864  */
3865 #ifdef CONFIG_SMP
3866 void __init setup_ioapic_dest(void)
3867 {
3868         int pin, ioapic, irq, irq_entry;
3869         struct irq_cfg *cfg;
3870
3871         if (skip_ioapic_setup == 1)
3872                 return;
3873
3874         for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3875                 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3876                         irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3877                         if (irq_entry == -1)
3878                                 continue;
3879                         irq = pin_2_irq(irq_entry, ioapic, pin);
3880
3881                         /* setup_IO_APIC_irqs could fail to get vector for some device
3882                          * when you have too many devices, because at that time only boot
3883                          * cpu is online.
3884                          */
3885                         cfg = irq_cfg(irq);
3886                         if (!cfg->vector)
3887                                 setup_IO_APIC_irq(ioapic, pin, irq,
3888                                                   irq_trigger(irq_entry),
3889                                                   irq_polarity(irq_entry));
3890 #ifdef CONFIG_INTR_REMAP
3891                         else if (intr_remapping_enabled)
3892                                 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
3893 #endif
3894                         else
3895                                 set_ioapic_affinity_irq(irq, TARGET_CPUS);
3896                 }
3897
3898         }
3899 }
3900 #endif
3901
3902 #define IOAPIC_RESOURCE_NAME_SIZE 11
3903
3904 static struct resource *ioapic_resources;
3905
3906 static struct resource * __init ioapic_setup_resources(void)
3907 {
3908         unsigned long n;
3909         struct resource *res;
3910         char *mem;
3911         int i;
3912
3913         if (nr_ioapics <= 0)
3914                 return NULL;
3915
3916         n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3917         n *= nr_ioapics;
3918
3919         mem = alloc_bootmem(n);
3920         res = (void *)mem;
3921
3922         if (mem != NULL) {
3923                 mem += sizeof(struct resource) * nr_ioapics;
3924
3925                 for (i = 0; i < nr_ioapics; i++) {
3926                         res[i].name = mem;
3927                         res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3928                         sprintf(mem,  "IOAPIC %u", i);
3929                         mem += IOAPIC_RESOURCE_NAME_SIZE;
3930                 }
3931         }
3932
3933         ioapic_resources = res;
3934
3935         return res;
3936 }
3937
3938 void __init ioapic_init_mappings(void)
3939 {
3940         unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3941         int i;
3942         struct resource *ioapic_res;
3943
3944         ioapic_res = ioapic_setup_resources();
3945         for (i = 0; i < nr_ioapics; i++) {
3946                 if (smp_found_config) {
3947                         ioapic_phys = mp_ioapics[i].mp_apicaddr;
3948 #ifdef CONFIG_X86_32
3949                         if (!ioapic_phys) {
3950                                 printk(KERN_ERR
3951                                        "WARNING: bogus zero IO-APIC "
3952                                        "address found in MPTABLE, "
3953                                        "disabling IO/APIC support!\n");
3954                                 smp_found_config = 0;
3955                                 skip_ioapic_setup = 1;
3956                                 goto fake_ioapic_page;
3957                         }
3958 #endif
3959                 } else {
3960 #ifdef CONFIG_X86_32
3961 fake_ioapic_page:
3962 #endif
3963                         ioapic_phys = (unsigned long)
3964                                 alloc_bootmem_pages(PAGE_SIZE);
3965                         ioapic_phys = __pa(ioapic_phys);
3966                 }
3967                 set_fixmap_nocache(idx, ioapic_phys);
3968                 apic_printk(APIC_VERBOSE,
3969                             "mapped IOAPIC to %08lx (%08lx)\n",
3970                             __fix_to_virt(idx), ioapic_phys);
3971                 idx++;
3972
3973                 if (ioapic_res != NULL) {
3974                         ioapic_res->start = ioapic_phys;
3975                         ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3976                         ioapic_res++;
3977                 }
3978         }
3979 }
3980
3981 static int __init ioapic_insert_resources(void)
3982 {
3983         int i;
3984         struct resource *r = ioapic_resources;
3985
3986         if (!r) {
3987                 printk(KERN_ERR
3988                        "IO APIC resources could be not be allocated.\n");
3989                 return -1;
3990         }
3991
3992         for (i = 0; i < nr_ioapics; i++) {
3993                 insert_resource(&iomem_resource, r);
3994                 r++;
3995         }
3996
3997         return 0;
3998 }
3999
4000 /* Insert the IO APIC resources after PCI initialization has occured to handle
4001  * IO APICS that are mapped in on a BAR in PCI space. */
4002 late_initcall(ioapic_insert_resources);