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