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