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