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