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