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