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