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