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