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