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