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