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