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