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