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