]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/io_apic_64.c
x86: make read_apic_id return final apicid
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / io_apic_64.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/acpi.h>
31 #include <linux/sysdev.h>
32 #include <linux/msi.h>
33 #include <linux/htirq.h>
34 #include <linux/dmar.h>
35 #include <linux/jiffies.h>
36 #ifdef CONFIG_ACPI
37 #include <acpi/acpi_bus.h>
38 #endif
39 #include <linux/bootmem.h>
40 #include <linux/dmar.h>
41
42 #include <asm/idle.h>
43 #include <asm/io.h>
44 #include <asm/smp.h>
45 #include <asm/desc.h>
46 #include <asm/proto.h>
47 #include <asm/acpi.h>
48 #include <asm/dma.h>
49 #include <asm/nmi.h>
50 #include <asm/msidef.h>
51 #include <asm/hypertransport.h>
52 #include <asm/irq_remapping.h>
53
54 #include <mach_ipi.h>
55 #include <mach_apic.h>
56
57 struct irq_cfg {
58         cpumask_t domain;
59         cpumask_t old_domain;
60         unsigned move_cleanup_count;
61         u8 vector;
62         u8 move_in_progress : 1;
63 };
64
65 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
66 static struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
67         [0]  = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR,  },
68         [1]  = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR,  },
69         [2]  = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR,  },
70         [3]  = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR,  },
71         [4]  = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR,  },
72         [5]  = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR,  },
73         [6]  = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR,  },
74         [7]  = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR,  },
75         [8]  = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR,  },
76         [9]  = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR,  },
77         [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
78         [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
79         [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
80         [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
81         [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
82         [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
83 };
84
85 static int assign_irq_vector(int irq, cpumask_t mask);
86
87 int first_system_vector = 0xfe;
88
89 char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
90
91 #define __apicdebuginit  __init
92
93 int sis_apic_bug; /* not actually supported, dummy for compile */
94
95 static int no_timer_check;
96
97 static int disable_timer_pin_1 __initdata;
98
99 static bool mask_ioapic_irq_2 __initdata;
100
101 void __init force_mask_ioapic_irq_2(void)
102 {
103         mask_ioapic_irq_2 = true;
104 }
105
106 int timer_through_8259 __initdata;
107
108 /* Where if anywhere is the i8259 connect in external int mode */
109 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
110
111 static DEFINE_SPINLOCK(ioapic_lock);
112 DEFINE_SPINLOCK(vector_lock);
113
114 /*
115  * # of IRQ routing registers
116  */
117 int nr_ioapic_registers[MAX_IO_APICS];
118
119 /* I/O APIC RTE contents at the OS boot up */
120 struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
121
122 /* I/O APIC entries */
123 struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
124 int nr_ioapics;
125
126 /* MP IRQ source entries */
127 struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
128
129 /* # of MP IRQ source entries */
130 int mp_irq_entries;
131
132 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
133
134 /*
135  * Rough estimation of how many shared IRQs there are, can
136  * be changed anytime.
137  */
138 #define MAX_PLUS_SHARED_IRQS NR_IRQS
139 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
140
141 /*
142  * This is performance-critical, we want to do it O(1)
143  *
144  * the indexing order of this array favors 1:1 mappings
145  * between pins and IRQs.
146  */
147
148 static struct irq_pin_list {
149         short apic, pin, next;
150 } irq_2_pin[PIN_MAP_SIZE];
151
152 struct io_apic {
153         unsigned int index;
154         unsigned int unused[3];
155         unsigned int data;
156 };
157
158 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
159 {
160         return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
161                 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
162 }
163
164 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
165 {
166         struct io_apic __iomem *io_apic = io_apic_base(apic);
167         writel(reg, &io_apic->index);
168         return readl(&io_apic->data);
169 }
170
171 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
172 {
173         struct io_apic __iomem *io_apic = io_apic_base(apic);
174         writel(reg, &io_apic->index);
175         writel(value, &io_apic->data);
176 }
177
178 /*
179  * Re-write a value: to be used for read-modify-write
180  * cycles where the read already set up the index register.
181  */
182 static inline void io_apic_modify(unsigned int apic, unsigned int value)
183 {
184         struct io_apic __iomem *io_apic = io_apic_base(apic);
185         writel(value, &io_apic->data);
186 }
187
188 static bool io_apic_level_ack_pending(unsigned int irq)
189 {
190         struct irq_pin_list *entry;
191         unsigned long flags;
192
193         spin_lock_irqsave(&ioapic_lock, flags);
194         entry = irq_2_pin + irq;
195         for (;;) {
196                 unsigned int reg;
197                 int pin;
198
199                 pin = entry->pin;
200                 if (pin == -1)
201                         break;
202                 reg = io_apic_read(entry->apic, 0x10 + pin*2);
203                 /* Is the remote IRR bit set? */
204                 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
205                         spin_unlock_irqrestore(&ioapic_lock, flags);
206                         return true;
207                 }
208                 if (!entry->next)
209                         break;
210                 entry = irq_2_pin + entry->next;
211         }
212         spin_unlock_irqrestore(&ioapic_lock, flags);
213
214         return false;
215 }
216
217 /*
218  * Synchronize the IO-APIC and the CPU by doing
219  * a dummy read from the IO-APIC
220  */
221 static inline void io_apic_sync(unsigned int apic)
222 {
223         struct io_apic __iomem *io_apic = io_apic_base(apic);
224         readl(&io_apic->data);
225 }
226
227 #define __DO_ACTION(R, ACTION, FINAL)                                   \
228                                                                         \
229 {                                                                       \
230         int pin;                                                        \
231         struct irq_pin_list *entry = irq_2_pin + irq;                   \
232                                                                         \
233         BUG_ON(irq >= NR_IRQS);                                         \
234         for (;;) {                                                      \
235                 unsigned int reg;                                       \
236                 pin = entry->pin;                                       \
237                 if (pin == -1)                                          \
238                         break;                                          \
239                 reg = io_apic_read(entry->apic, 0x10 + R + pin*2);      \
240                 reg ACTION;                                             \
241                 io_apic_modify(entry->apic, reg);                       \
242                 FINAL;                                                  \
243                 if (!entry->next)                                       \
244                         break;                                          \
245                 entry = irq_2_pin + entry->next;                        \
246         }                                                               \
247 }
248
249 union entry_union {
250         struct { u32 w1, w2; };
251         struct IO_APIC_route_entry entry;
252 };
253
254 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
255 {
256         union entry_union eu;
257         unsigned long flags;
258         spin_lock_irqsave(&ioapic_lock, flags);
259         eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
260         eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
261         spin_unlock_irqrestore(&ioapic_lock, flags);
262         return eu.entry;
263 }
264
265 /*
266  * When we write a new IO APIC routing entry, we need to write the high
267  * word first! If the mask bit in the low word is clear, we will enable
268  * the interrupt, and we need to make sure the entry is fully populated
269  * before that happens.
270  */
271 static void
272 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
273 {
274         union entry_union eu;
275         eu.entry = e;
276         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
277         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
278 }
279
280 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
281 {
282         unsigned long flags;
283         spin_lock_irqsave(&ioapic_lock, flags);
284         __ioapic_write_entry(apic, pin, e);
285         spin_unlock_irqrestore(&ioapic_lock, flags);
286 }
287
288 /*
289  * When we mask an IO APIC routing entry, we need to write the low
290  * word first, in order to set the mask bit before we change the
291  * high bits!
292  */
293 static void ioapic_mask_entry(int apic, int pin)
294 {
295         unsigned long flags;
296         union entry_union eu = { .entry.mask = 1 };
297
298         spin_lock_irqsave(&ioapic_lock, flags);
299         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
300         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
301         spin_unlock_irqrestore(&ioapic_lock, flags);
302 }
303
304 #ifdef CONFIG_SMP
305 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
306 {
307         int apic, pin;
308         struct irq_pin_list *entry = irq_2_pin + irq;
309
310         BUG_ON(irq >= NR_IRQS);
311         for (;;) {
312                 unsigned int reg;
313                 apic = entry->apic;
314                 pin = entry->pin;
315                 if (pin == -1)
316                         break;
317                 /*
318                  * With interrupt-remapping, destination information comes
319                  * from interrupt-remapping table entry.
320                  */
321                 if (!irq_remapped(irq))
322                         io_apic_write(apic, 0x11 + pin*2, dest);
323                 reg = io_apic_read(apic, 0x10 + pin*2);
324                 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
325                 reg |= vector;
326                 io_apic_modify(apic, reg);
327                 if (!entry->next)
328                         break;
329                 entry = irq_2_pin + entry->next;
330         }
331 }
332
333 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
334 {
335         struct irq_cfg *cfg = irq_cfg + irq;
336         unsigned long flags;
337         unsigned int dest;
338         cpumask_t tmp;
339
340         cpus_and(tmp, mask, cpu_online_map);
341         if (cpus_empty(tmp))
342                 return;
343
344         if (assign_irq_vector(irq, mask))
345                 return;
346
347         cpus_and(tmp, cfg->domain, mask);
348         dest = cpu_mask_to_apicid(tmp);
349
350         /*
351          * Only the high 8 bits are valid.
352          */
353         dest = SET_APIC_LOGICAL_ID(dest);
354
355         spin_lock_irqsave(&ioapic_lock, flags);
356         __target_IO_APIC_irq(irq, dest, cfg->vector);
357         irq_desc[irq].affinity = mask;
358         spin_unlock_irqrestore(&ioapic_lock, flags);
359 }
360 #endif
361
362 /*
363  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
364  * shared ISA-space IRQs, so we have to support them. We are super
365  * fast in the common case, and fast for shared ISA-space IRQs.
366  */
367 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
368 {
369         static int first_free_entry = NR_IRQS;
370         struct irq_pin_list *entry = irq_2_pin + irq;
371
372         BUG_ON(irq >= NR_IRQS);
373         while (entry->next)
374                 entry = irq_2_pin + entry->next;
375
376         if (entry->pin != -1) {
377                 entry->next = first_free_entry;
378                 entry = irq_2_pin + entry->next;
379                 if (++first_free_entry >= PIN_MAP_SIZE)
380                         panic("io_apic.c: ran out of irq_2_pin entries!");
381         }
382         entry->apic = apic;
383         entry->pin = pin;
384 }
385
386 /*
387  * Reroute an IRQ to a different pin.
388  */
389 static void __init replace_pin_at_irq(unsigned int irq,
390                                       int oldapic, int oldpin,
391                                       int newapic, int newpin)
392 {
393         struct irq_pin_list *entry = irq_2_pin + irq;
394
395         while (1) {
396                 if (entry->apic == oldapic && entry->pin == oldpin) {
397                         entry->apic = newapic;
398                         entry->pin = newpin;
399                 }
400                 if (!entry->next)
401                         break;
402                 entry = irq_2_pin + entry->next;
403         }
404 }
405
406
407 #define DO_ACTION(name,R,ACTION, FINAL)                                 \
408                                                                         \
409         static void name##_IO_APIC_irq (unsigned int irq)               \
410         __DO_ACTION(R, ACTION, FINAL)
411
412 /* mask = 1 */
413 DO_ACTION(__mask,       0, |= IO_APIC_REDIR_MASKED, io_apic_sync(entry->apic))
414
415 /* mask = 0 */
416 DO_ACTION(__unmask,     0, &= ~IO_APIC_REDIR_MASKED, )
417
418 static void mask_IO_APIC_irq (unsigned int irq)
419 {
420         unsigned long flags;
421
422         spin_lock_irqsave(&ioapic_lock, flags);
423         __mask_IO_APIC_irq(irq);
424         spin_unlock_irqrestore(&ioapic_lock, flags);
425 }
426
427 static void unmask_IO_APIC_irq (unsigned int irq)
428 {
429         unsigned long flags;
430
431         spin_lock_irqsave(&ioapic_lock, flags);
432         __unmask_IO_APIC_irq(irq);
433         spin_unlock_irqrestore(&ioapic_lock, flags);
434 }
435
436 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
437 {
438         struct IO_APIC_route_entry entry;
439
440         /* Check delivery_mode to be sure we're not clearing an SMI pin */
441         entry = ioapic_read_entry(apic, pin);
442         if (entry.delivery_mode == dest_SMI)
443                 return;
444         /*
445          * Disable it in the IO-APIC irq-routing table:
446          */
447         ioapic_mask_entry(apic, pin);
448 }
449
450 static void clear_IO_APIC (void)
451 {
452         int apic, pin;
453
454         for (apic = 0; apic < nr_ioapics; apic++)
455                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
456                         clear_IO_APIC_pin(apic, pin);
457 }
458
459 /*
460  * Saves and masks all the unmasked IO-APIC RTE's
461  */
462 int save_mask_IO_APIC_setup(void)
463 {
464         union IO_APIC_reg_01 reg_01;
465         unsigned long flags;
466         int apic, pin;
467
468         /*
469          * The number of IO-APIC IRQ registers (== #pins):
470          */
471         for (apic = 0; apic < nr_ioapics; apic++) {
472                 spin_lock_irqsave(&ioapic_lock, flags);
473                 reg_01.raw = io_apic_read(apic, 1);
474                 spin_unlock_irqrestore(&ioapic_lock, flags);
475                 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
476         }
477
478         for (apic = 0; apic < nr_ioapics; apic++) {
479                 early_ioapic_entries[apic] =
480                         kzalloc(sizeof(struct IO_APIC_route_entry) *
481                                 nr_ioapic_registers[apic], GFP_KERNEL);
482                 if (!early_ioapic_entries[apic])
483                         return -ENOMEM;
484         }
485
486         for (apic = 0; apic < nr_ioapics; apic++)
487                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
488                         struct IO_APIC_route_entry entry;
489
490                         entry = early_ioapic_entries[apic][pin] =
491                                 ioapic_read_entry(apic, pin);
492                         if (!entry.mask) {
493                                 entry.mask = 1;
494                                 ioapic_write_entry(apic, pin, entry);
495                         }
496                 }
497         return 0;
498 }
499
500 void restore_IO_APIC_setup(void)
501 {
502         int apic, pin;
503
504         for (apic = 0; apic < nr_ioapics; apic++)
505                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
506                         ioapic_write_entry(apic, pin,
507                                            early_ioapic_entries[apic][pin]);
508 }
509
510 void reinit_intr_remapped_IO_APIC(int intr_remapping)
511 {
512         /*
513          * for now plain restore of previous settings.
514          * TBD: In the case of OS enabling interrupt-remapping,
515          * IO-APIC RTE's need to be setup to point to interrupt-remapping
516          * table entries. for now, do a plain restore, and wait for
517          * the setup_IO_APIC_irqs() to do proper initialization.
518          */
519         restore_IO_APIC_setup();
520 }
521
522 int skip_ioapic_setup;
523 int ioapic_force;
524
525 static int __init parse_noapic(char *str)
526 {
527         disable_ioapic_setup();
528         return 0;
529 }
530 early_param("noapic", parse_noapic);
531
532 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
533 static int __init disable_timer_pin_setup(char *arg)
534 {
535         disable_timer_pin_1 = 1;
536         return 1;
537 }
538 __setup("disable_timer_pin_1", disable_timer_pin_setup);
539
540
541 /*
542  * Find the IRQ entry number of a certain pin.
543  */
544 static int find_irq_entry(int apic, int pin, int type)
545 {
546         int i;
547
548         for (i = 0; i < mp_irq_entries; i++)
549                 if (mp_irqs[i].mp_irqtype == type &&
550                     (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
551                      mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
552                     mp_irqs[i].mp_dstirq == pin)
553                         return i;
554
555         return -1;
556 }
557
558 /*
559  * Find the pin to which IRQ[irq] (ISA) is connected
560  */
561 static int __init find_isa_irq_pin(int irq, int type)
562 {
563         int i;
564
565         for (i = 0; i < mp_irq_entries; i++) {
566                 int lbus = mp_irqs[i].mp_srcbus;
567
568                 if (test_bit(lbus, mp_bus_not_pci) &&
569                     (mp_irqs[i].mp_irqtype == type) &&
570                     (mp_irqs[i].mp_srcbusirq == irq))
571
572                         return mp_irqs[i].mp_dstirq;
573         }
574         return -1;
575 }
576
577 static int __init find_isa_irq_apic(int irq, int type)
578 {
579         int i;
580
581         for (i = 0; i < mp_irq_entries; i++) {
582                 int lbus = mp_irqs[i].mp_srcbus;
583
584                 if (test_bit(lbus, mp_bus_not_pci) &&
585                     (mp_irqs[i].mp_irqtype == type) &&
586                     (mp_irqs[i].mp_srcbusirq == irq))
587                         break;
588         }
589         if (i < mp_irq_entries) {
590                 int apic;
591                 for(apic = 0; apic < nr_ioapics; apic++) {
592                         if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
593                                 return apic;
594                 }
595         }
596
597         return -1;
598 }
599
600 /*
601  * Find a specific PCI IRQ entry.
602  * Not an __init, possibly needed by modules
603  */
604 static int pin_2_irq(int idx, int apic, int pin);
605
606 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
607 {
608         int apic, i, best_guess = -1;
609
610         apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
611                 bus, slot, pin);
612         if (test_bit(bus, mp_bus_not_pci)) {
613                 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
614                 return -1;
615         }
616         for (i = 0; i < mp_irq_entries; i++) {
617                 int lbus = mp_irqs[i].mp_srcbus;
618
619                 for (apic = 0; apic < nr_ioapics; apic++)
620                         if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
621                             mp_irqs[i].mp_dstapic == MP_APIC_ALL)
622                                 break;
623
624                 if (!test_bit(lbus, mp_bus_not_pci) &&
625                     !mp_irqs[i].mp_irqtype &&
626                     (bus == lbus) &&
627                     (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
628                         int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
629
630                         if (!(apic || IO_APIC_IRQ(irq)))
631                                 continue;
632
633                         if (pin == (mp_irqs[i].mp_srcbusirq & 3))
634                                 return irq;
635                         /*
636                          * Use the first all-but-pin matching entry as a
637                          * best-guess fuzzy result for broken mptables.
638                          */
639                         if (best_guess < 0)
640                                 best_guess = irq;
641                 }
642         }
643         BUG_ON(best_guess >= NR_IRQS);
644         return best_guess;
645 }
646
647 /* ISA interrupts are always polarity zero edge triggered,
648  * when listed as conforming in the MP table. */
649
650 #define default_ISA_trigger(idx)        (0)
651 #define default_ISA_polarity(idx)       (0)
652
653 /* PCI interrupts are always polarity one level triggered,
654  * when listed as conforming in the MP table. */
655
656 #define default_PCI_trigger(idx)        (1)
657 #define default_PCI_polarity(idx)       (1)
658
659 static int MPBIOS_polarity(int idx)
660 {
661         int bus = mp_irqs[idx].mp_srcbus;
662         int polarity;
663
664         /*
665          * Determine IRQ line polarity (high active or low active):
666          */
667         switch (mp_irqs[idx].mp_irqflag & 3)
668         {
669                 case 0: /* conforms, ie. bus-type dependent polarity */
670                         if (test_bit(bus, mp_bus_not_pci))
671                                 polarity = default_ISA_polarity(idx);
672                         else
673                                 polarity = default_PCI_polarity(idx);
674                         break;
675                 case 1: /* high active */
676                 {
677                         polarity = 0;
678                         break;
679                 }
680                 case 2: /* reserved */
681                 {
682                         printk(KERN_WARNING "broken BIOS!!\n");
683                         polarity = 1;
684                         break;
685                 }
686                 case 3: /* low active */
687                 {
688                         polarity = 1;
689                         break;
690                 }
691                 default: /* invalid */
692                 {
693                         printk(KERN_WARNING "broken BIOS!!\n");
694                         polarity = 1;
695                         break;
696                 }
697         }
698         return polarity;
699 }
700
701 static int MPBIOS_trigger(int idx)
702 {
703         int bus = mp_irqs[idx].mp_srcbus;
704         int trigger;
705
706         /*
707          * Determine IRQ trigger mode (edge or level sensitive):
708          */
709         switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
710         {
711                 case 0: /* conforms, ie. bus-type dependent */
712                         if (test_bit(bus, mp_bus_not_pci))
713                                 trigger = default_ISA_trigger(idx);
714                         else
715                                 trigger = default_PCI_trigger(idx);
716                         break;
717                 case 1: /* edge */
718                 {
719                         trigger = 0;
720                         break;
721                 }
722                 case 2: /* reserved */
723                 {
724                         printk(KERN_WARNING "broken BIOS!!\n");
725                         trigger = 1;
726                         break;
727                 }
728                 case 3: /* level */
729                 {
730                         trigger = 1;
731                         break;
732                 }
733                 default: /* invalid */
734                 {
735                         printk(KERN_WARNING "broken BIOS!!\n");
736                         trigger = 0;
737                         break;
738                 }
739         }
740         return trigger;
741 }
742
743 static inline int irq_polarity(int idx)
744 {
745         return MPBIOS_polarity(idx);
746 }
747
748 static inline int irq_trigger(int idx)
749 {
750         return MPBIOS_trigger(idx);
751 }
752
753 static int pin_2_irq(int idx, int apic, int pin)
754 {
755         int irq, i;
756         int bus = mp_irqs[idx].mp_srcbus;
757
758         /*
759          * Debugging check, we are in big trouble if this message pops up!
760          */
761         if (mp_irqs[idx].mp_dstirq != pin)
762                 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
763
764         if (test_bit(bus, mp_bus_not_pci)) {
765                 irq = mp_irqs[idx].mp_srcbusirq;
766         } else {
767                 /*
768                  * PCI IRQs are mapped in order
769                  */
770                 i = irq = 0;
771                 while (i < apic)
772                         irq += nr_ioapic_registers[i++];
773                 irq += pin;
774         }
775         BUG_ON(irq >= NR_IRQS);
776         return irq;
777 }
778
779 static int __assign_irq_vector(int irq, cpumask_t mask)
780 {
781         /*
782          * NOTE! The local APIC isn't very good at handling
783          * multiple interrupts at the same interrupt level.
784          * As the interrupt level is determined by taking the
785          * vector number and shifting that right by 4, we
786          * want to spread these out a bit so that they don't
787          * all fall in the same interrupt level.
788          *
789          * Also, we've got to be careful not to trash gate
790          * 0x80, because int 0x80 is hm, kind of importantish. ;)
791          */
792         static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
793         unsigned int old_vector;
794         int cpu;
795         struct irq_cfg *cfg;
796
797         BUG_ON((unsigned)irq >= NR_IRQS);
798         cfg = &irq_cfg[irq];
799
800         /* Only try and allocate irqs on cpus that are present */
801         cpus_and(mask, mask, cpu_online_map);
802
803         if ((cfg->move_in_progress) || cfg->move_cleanup_count)
804                 return -EBUSY;
805
806         old_vector = cfg->vector;
807         if (old_vector) {
808                 cpumask_t tmp;
809                 cpus_and(tmp, cfg->domain, mask);
810                 if (!cpus_empty(tmp))
811                         return 0;
812         }
813
814         for_each_cpu_mask(cpu, mask) {
815                 cpumask_t domain, new_mask;
816                 int new_cpu;
817                 int vector, offset;
818
819                 domain = vector_allocation_domain(cpu);
820                 cpus_and(new_mask, domain, cpu_online_map);
821
822                 vector = current_vector;
823                 offset = current_offset;
824 next:
825                 vector += 8;
826                 if (vector >= first_system_vector) {
827                         /* If we run out of vectors on large boxen, must share them. */
828                         offset = (offset + 1) % 8;
829                         vector = FIRST_DEVICE_VECTOR + offset;
830                 }
831                 if (unlikely(current_vector == vector))
832                         continue;
833                 if (vector == IA32_SYSCALL_VECTOR)
834                         goto next;
835                 for_each_cpu_mask(new_cpu, new_mask)
836                         if (per_cpu(vector_irq, new_cpu)[vector] != -1)
837                                 goto next;
838                 /* Found one! */
839                 current_vector = vector;
840                 current_offset = offset;
841                 if (old_vector) {
842                         cfg->move_in_progress = 1;
843                         cfg->old_domain = cfg->domain;
844                 }
845                 for_each_cpu_mask(new_cpu, new_mask)
846                         per_cpu(vector_irq, new_cpu)[vector] = irq;
847                 cfg->vector = vector;
848                 cfg->domain = domain;
849                 return 0;
850         }
851         return -ENOSPC;
852 }
853
854 static int assign_irq_vector(int irq, cpumask_t mask)
855 {
856         int err;
857         unsigned long flags;
858
859         spin_lock_irqsave(&vector_lock, flags);
860         err = __assign_irq_vector(irq, mask);
861         spin_unlock_irqrestore(&vector_lock, flags);
862         return err;
863 }
864
865 static void __clear_irq_vector(int irq)
866 {
867         struct irq_cfg *cfg;
868         cpumask_t mask;
869         int cpu, vector;
870
871         BUG_ON((unsigned)irq >= NR_IRQS);
872         cfg = &irq_cfg[irq];
873         BUG_ON(!cfg->vector);
874
875         vector = cfg->vector;
876         cpus_and(mask, cfg->domain, cpu_online_map);
877         for_each_cpu_mask(cpu, mask)
878                 per_cpu(vector_irq, cpu)[vector] = -1;
879
880         cfg->vector = 0;
881         cpus_clear(cfg->domain);
882 }
883
884 static void __setup_vector_irq(int cpu)
885 {
886         /* Initialize vector_irq on a new cpu */
887         /* This function must be called with vector_lock held */
888         int irq, vector;
889
890         /* Mark the inuse vectors */
891         for (irq = 0; irq < NR_IRQS; ++irq) {
892                 if (!cpu_isset(cpu, irq_cfg[irq].domain))
893                         continue;
894                 vector = irq_cfg[irq].vector;
895                 per_cpu(vector_irq, cpu)[vector] = irq;
896         }
897         /* Mark the free vectors */
898         for (vector = 0; vector < NR_VECTORS; ++vector) {
899                 irq = per_cpu(vector_irq, cpu)[vector];
900                 if (irq < 0)
901                         continue;
902                 if (!cpu_isset(cpu, irq_cfg[irq].domain))
903                         per_cpu(vector_irq, cpu)[vector] = -1;
904         }
905 }
906
907 void setup_vector_irq(int cpu)
908 {
909         spin_lock(&vector_lock);
910         __setup_vector_irq(smp_processor_id());
911         spin_unlock(&vector_lock);
912 }
913
914
915 static struct irq_chip ioapic_chip;
916 #ifdef CONFIG_INTR_REMAP
917 static struct irq_chip ir_ioapic_chip;
918 #endif
919
920 static void ioapic_register_intr(int irq, unsigned long trigger)
921 {
922         if (trigger)
923                 irq_desc[irq].status |= IRQ_LEVEL;
924         else
925                 irq_desc[irq].status &= ~IRQ_LEVEL;
926
927 #ifdef CONFIG_INTR_REMAP
928         if (irq_remapped(irq)) {
929                 irq_desc[irq].status |= IRQ_MOVE_PCNTXT;
930                 if (trigger)
931                         set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
932                                                       handle_fasteoi_irq,
933                                                      "fasteoi");
934                 else
935                         set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
936                                                       handle_edge_irq, "edge");
937                 return;
938         }
939 #endif
940         if (trigger)
941                 set_irq_chip_and_handler_name(irq, &ioapic_chip,
942                                               handle_fasteoi_irq,
943                                               "fasteoi");
944         else
945                 set_irq_chip_and_handler_name(irq, &ioapic_chip,
946                                               handle_edge_irq, "edge");
947 }
948
949 static int setup_ioapic_entry(int apic, int irq,
950                               struct IO_APIC_route_entry *entry,
951                               unsigned int destination, int trigger,
952                               int polarity, int vector)
953 {
954         /*
955          * add it to the IO-APIC irq-routing table:
956          */
957         memset(entry,0,sizeof(*entry));
958
959 #ifdef CONFIG_INTR_REMAP
960         if (intr_remapping_enabled) {
961                 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
962                 struct irte irte;
963                 struct IR_IO_APIC_route_entry *ir_entry =
964                         (struct IR_IO_APIC_route_entry *) entry;
965                 int index;
966
967                 if (!iommu)
968                         panic("No mapping iommu for ioapic %d\n", apic);
969
970                 index = alloc_irte(iommu, irq, 1);
971                 if (index < 0)
972                         panic("Failed to allocate IRTE for ioapic %d\n", apic);
973
974                 memset(&irte, 0, sizeof(irte));
975
976                 irte.present = 1;
977                 irte.dst_mode = INT_DEST_MODE;
978                 irte.trigger_mode = trigger;
979                 irte.dlvry_mode = INT_DELIVERY_MODE;
980                 irte.vector = vector;
981                 irte.dest_id = IRTE_DEST(destination);
982
983                 modify_irte(irq, &irte);
984
985                 ir_entry->index2 = (index >> 15) & 0x1;
986                 ir_entry->zero = 0;
987                 ir_entry->format = 1;
988                 ir_entry->index = (index & 0x7fff);
989         } else
990 #endif
991         {
992                 entry->delivery_mode = INT_DELIVERY_MODE;
993                 entry->dest_mode = INT_DEST_MODE;
994                 entry->dest = destination;
995         }
996
997         entry->mask = 0;                                /* enable IRQ */
998         entry->trigger = trigger;
999         entry->polarity = polarity;
1000         entry->vector = vector;
1001
1002         /* Mask level triggered irqs.
1003          * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1004          */
1005         if (trigger)
1006                 entry->mask = 1;
1007         return 0;
1008 }
1009
1010 static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
1011                               int trigger, int polarity)
1012 {
1013         struct irq_cfg *cfg = irq_cfg + irq;
1014         struct IO_APIC_route_entry entry;
1015         cpumask_t mask;
1016
1017         if (!IO_APIC_IRQ(irq))
1018                 return;
1019
1020         mask = TARGET_CPUS;
1021         if (assign_irq_vector(irq, mask))
1022                 return;
1023
1024         cpus_and(mask, cfg->domain, mask);
1025
1026         apic_printk(APIC_VERBOSE,KERN_DEBUG
1027                     "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1028                     "IRQ %d Mode:%i Active:%i)\n",
1029                     apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1030                     irq, trigger, polarity);
1031
1032
1033         if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1034                                cpu_mask_to_apicid(mask), trigger, polarity,
1035                                cfg->vector)) {
1036                 printk("Failed to setup ioapic entry for ioapic  %d, pin %d\n",
1037                        mp_ioapics[apic].mp_apicid, pin);
1038                 __clear_irq_vector(irq);
1039                 return;
1040         }
1041
1042         ioapic_register_intr(irq, trigger);
1043         if (irq < 16)
1044                 disable_8259A_irq(irq);
1045
1046         ioapic_write_entry(apic, pin, entry);
1047 }
1048
1049 static void __init setup_IO_APIC_irqs(void)
1050 {
1051         int apic, pin, idx, irq, first_notcon = 1;
1052
1053         apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1054
1055         for (apic = 0; apic < nr_ioapics; apic++) {
1056         for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1057
1058                 idx = find_irq_entry(apic,pin,mp_INT);
1059                 if (idx == -1) {
1060                         if (first_notcon) {
1061                                 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
1062                                 first_notcon = 0;
1063                         } else
1064                                 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
1065                         continue;
1066                 }
1067                 if (!first_notcon) {
1068                         apic_printk(APIC_VERBOSE, " not connected.\n");
1069                         first_notcon = 1;
1070                 }
1071
1072                 irq = pin_2_irq(idx, apic, pin);
1073                 add_pin_to_irq(irq, apic, pin);
1074
1075                 setup_IO_APIC_irq(apic, pin, irq,
1076                                   irq_trigger(idx), irq_polarity(idx));
1077         }
1078         }
1079
1080         if (!first_notcon)
1081                 apic_printk(APIC_VERBOSE, " not connected.\n");
1082 }
1083
1084 /*
1085  * Set up the timer pin, possibly with the 8259A-master behind.
1086  */
1087 static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1088                                         int vector)
1089 {
1090         struct IO_APIC_route_entry entry;
1091
1092         if (intr_remapping_enabled)
1093                 return;
1094
1095         memset(&entry, 0, sizeof(entry));
1096
1097         /*
1098          * We use logical delivery to get the timer IRQ
1099          * to the first CPU.
1100          */
1101         entry.dest_mode = INT_DEST_MODE;
1102         entry.mask = 1;                                 /* mask IRQ now */
1103         entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
1104         entry.delivery_mode = INT_DELIVERY_MODE;
1105         entry.polarity = 0;
1106         entry.trigger = 0;
1107         entry.vector = vector;
1108
1109         /*
1110          * The timer IRQ doesn't have to know that behind the
1111          * scene we may have a 8259A-master in AEOI mode ...
1112          */
1113         set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1114
1115         /*
1116          * Add it to the IO-APIC irq-routing table:
1117          */
1118         ioapic_write_entry(apic, pin, entry);
1119 }
1120
1121 void __apicdebuginit print_IO_APIC(void)
1122 {
1123         int apic, i;
1124         union IO_APIC_reg_00 reg_00;
1125         union IO_APIC_reg_01 reg_01;
1126         union IO_APIC_reg_02 reg_02;
1127         unsigned long flags;
1128
1129         if (apic_verbosity == APIC_QUIET)
1130                 return;
1131
1132         printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1133         for (i = 0; i < nr_ioapics; i++)
1134                 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1135                        mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
1136
1137         /*
1138          * We are a bit conservative about what we expect.  We have to
1139          * know about every hardware change ASAP.
1140          */
1141         printk(KERN_INFO "testing the IO APIC.......................\n");
1142
1143         for (apic = 0; apic < nr_ioapics; apic++) {
1144
1145         spin_lock_irqsave(&ioapic_lock, flags);
1146         reg_00.raw = io_apic_read(apic, 0);
1147         reg_01.raw = io_apic_read(apic, 1);
1148         if (reg_01.bits.version >= 0x10)
1149                 reg_02.raw = io_apic_read(apic, 2);
1150         spin_unlock_irqrestore(&ioapic_lock, flags);
1151
1152         printk("\n");
1153         printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
1154         printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1155         printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
1156
1157         printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1158         printk(KERN_DEBUG ".......     : max redirection entries: %04X\n", reg_01.bits.entries);
1159
1160         printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
1161         printk(KERN_DEBUG ".......     : IO APIC version: %04X\n", reg_01.bits.version);
1162
1163         if (reg_01.bits.version >= 0x10) {
1164                 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1165                 printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
1166         }
1167
1168         printk(KERN_DEBUG ".... IRQ redirection table:\n");
1169
1170         printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1171                           " Stat Dmod Deli Vect:   \n");
1172
1173         for (i = 0; i <= reg_01.bits.entries; i++) {
1174                 struct IO_APIC_route_entry entry;
1175
1176                 entry = ioapic_read_entry(apic, i);
1177
1178                 printk(KERN_DEBUG " %02x %03X ",
1179                         i,
1180                         entry.dest
1181                 );
1182
1183                 printk("%1d    %1d    %1d   %1d   %1d    %1d    %1d    %02X\n",
1184                         entry.mask,
1185                         entry.trigger,
1186                         entry.irr,
1187                         entry.polarity,
1188                         entry.delivery_status,
1189                         entry.dest_mode,
1190                         entry.delivery_mode,
1191                         entry.vector
1192                 );
1193         }
1194         }
1195         printk(KERN_DEBUG "IRQ to pin mappings:\n");
1196         for (i = 0; i < NR_IRQS; i++) {
1197                 struct irq_pin_list *entry = irq_2_pin + i;
1198                 if (entry->pin < 0)
1199                         continue;
1200                 printk(KERN_DEBUG "IRQ%d ", i);
1201                 for (;;) {
1202                         printk("-> %d:%d", entry->apic, entry->pin);
1203                         if (!entry->next)
1204                                 break;
1205                         entry = irq_2_pin + entry->next;
1206                 }
1207                 printk("\n");
1208         }
1209
1210         printk(KERN_INFO ".................................... done.\n");
1211
1212         return;
1213 }
1214
1215 #if 0
1216
1217 static __apicdebuginit void print_APIC_bitfield (int base)
1218 {
1219         unsigned int v;
1220         int i, j;
1221
1222         if (apic_verbosity == APIC_QUIET)
1223                 return;
1224
1225         printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1226         for (i = 0; i < 8; i++) {
1227                 v = apic_read(base + i*0x10);
1228                 for (j = 0; j < 32; j++) {
1229                         if (v & (1<<j))
1230                                 printk("1");
1231                         else
1232                                 printk("0");
1233                 }
1234                 printk("\n");
1235         }
1236 }
1237
1238 void __apicdebuginit print_local_APIC(void * dummy)
1239 {
1240         unsigned int v, ver, maxlvt;
1241         unsigned long icr;
1242
1243         if (apic_verbosity == APIC_QUIET)
1244                 return;
1245
1246         printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1247                 smp_processor_id(), hard_smp_processor_id());
1248         v = apic_read(APIC_ID);
1249         printk(KERN_INFO "... APIC ID:      %08x (%01x)\n", v, read_apic_id());
1250         v = apic_read(APIC_LVR);
1251         printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1252         ver = GET_APIC_VERSION(v);
1253         maxlvt = lapic_get_maxlvt();
1254
1255         v = apic_read(APIC_TASKPRI);
1256         printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1257
1258         v = apic_read(APIC_ARBPRI);
1259         printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1260                 v & APIC_ARBPRI_MASK);
1261         v = apic_read(APIC_PROCPRI);
1262         printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1263
1264         v = apic_read(APIC_EOI);
1265         printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1266         v = apic_read(APIC_RRR);
1267         printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1268         v = apic_read(APIC_LDR);
1269         printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1270         v = apic_read(APIC_DFR);
1271         printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1272         v = apic_read(APIC_SPIV);
1273         printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1274
1275         printk(KERN_DEBUG "... APIC ISR field:\n");
1276         print_APIC_bitfield(APIC_ISR);
1277         printk(KERN_DEBUG "... APIC TMR field:\n");
1278         print_APIC_bitfield(APIC_TMR);
1279         printk(KERN_DEBUG "... APIC IRR field:\n");
1280         print_APIC_bitfield(APIC_IRR);
1281
1282         v = apic_read(APIC_ESR);
1283         printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1284
1285         icr = apic_icr_read();
1286         printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
1287         printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
1288
1289         v = apic_read(APIC_LVTT);
1290         printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1291
1292         if (maxlvt > 3) {                       /* PC is LVT#4. */
1293                 v = apic_read(APIC_LVTPC);
1294                 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1295         }
1296         v = apic_read(APIC_LVT0);
1297         printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1298         v = apic_read(APIC_LVT1);
1299         printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1300
1301         if (maxlvt > 2) {                       /* ERR is LVT#3. */
1302                 v = apic_read(APIC_LVTERR);
1303                 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1304         }
1305
1306         v = apic_read(APIC_TMICT);
1307         printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1308         v = apic_read(APIC_TMCCT);
1309         printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1310         v = apic_read(APIC_TDCR);
1311         printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1312         printk("\n");
1313 }
1314
1315 void print_all_local_APICs (void)
1316 {
1317         on_each_cpu(print_local_APIC, NULL, 1, 1);
1318 }
1319
1320 void __apicdebuginit print_PIC(void)
1321 {
1322         unsigned int v;
1323         unsigned long flags;
1324
1325         if (apic_verbosity == APIC_QUIET)
1326                 return;
1327
1328         printk(KERN_DEBUG "\nprinting PIC contents\n");
1329
1330         spin_lock_irqsave(&i8259A_lock, flags);
1331
1332         v = inb(0xa1) << 8 | inb(0x21);
1333         printk(KERN_DEBUG "... PIC  IMR: %04x\n", v);
1334
1335         v = inb(0xa0) << 8 | inb(0x20);
1336         printk(KERN_DEBUG "... PIC  IRR: %04x\n", v);
1337
1338         outb(0x0b,0xa0);
1339         outb(0x0b,0x20);
1340         v = inb(0xa0) << 8 | inb(0x20);
1341         outb(0x0a,0xa0);
1342         outb(0x0a,0x20);
1343
1344         spin_unlock_irqrestore(&i8259A_lock, flags);
1345
1346         printk(KERN_DEBUG "... PIC  ISR: %04x\n", v);
1347
1348         v = inb(0x4d1) << 8 | inb(0x4d0);
1349         printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1350 }
1351
1352 #endif  /*  0  */
1353
1354 void __init enable_IO_APIC(void)
1355 {
1356         union IO_APIC_reg_01 reg_01;
1357         int i8259_apic, i8259_pin;
1358         int i, apic;
1359         unsigned long flags;
1360
1361         for (i = 0; i < PIN_MAP_SIZE; i++) {
1362                 irq_2_pin[i].pin = -1;
1363                 irq_2_pin[i].next = 0;
1364         }
1365
1366         /*
1367          * The number of IO-APIC IRQ registers (== #pins):
1368          */
1369         for (apic = 0; apic < nr_ioapics; apic++) {
1370                 spin_lock_irqsave(&ioapic_lock, flags);
1371                 reg_01.raw = io_apic_read(apic, 1);
1372                 spin_unlock_irqrestore(&ioapic_lock, flags);
1373                 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1374         }
1375         for(apic = 0; apic < nr_ioapics; apic++) {
1376                 int pin;
1377                 /* See if any of the pins is in ExtINT mode */
1378                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1379                         struct IO_APIC_route_entry entry;
1380                         entry = ioapic_read_entry(apic, pin);
1381
1382                         /* If the interrupt line is enabled and in ExtInt mode
1383                          * I have found the pin where the i8259 is connected.
1384                          */
1385                         if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1386                                 ioapic_i8259.apic = apic;
1387                                 ioapic_i8259.pin  = pin;
1388                                 goto found_i8259;
1389                         }
1390                 }
1391         }
1392  found_i8259:
1393         /* Look to see what if the MP table has reported the ExtINT */
1394         i8259_pin  = find_isa_irq_pin(0, mp_ExtINT);
1395         i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1396         /* Trust the MP table if nothing is setup in the hardware */
1397         if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1398                 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1399                 ioapic_i8259.pin  = i8259_pin;
1400                 ioapic_i8259.apic = i8259_apic;
1401         }
1402         /* Complain if the MP table and the hardware disagree */
1403         if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1404                 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1405         {
1406                 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1407         }
1408
1409         /*
1410          * Do not trust the IO-APIC being empty at bootup
1411          */
1412         clear_IO_APIC();
1413 }
1414
1415 /*
1416  * Not an __init, needed by the reboot code
1417  */
1418 void disable_IO_APIC(void)
1419 {
1420         /*
1421          * Clear the IO-APIC before rebooting:
1422          */
1423         clear_IO_APIC();
1424
1425         /*
1426          * If the i8259 is routed through an IOAPIC
1427          * Put that IOAPIC in virtual wire mode
1428          * so legacy interrupts can be delivered.
1429          */
1430         if (ioapic_i8259.pin != -1) {
1431                 struct IO_APIC_route_entry entry;
1432
1433                 memset(&entry, 0, sizeof(entry));
1434                 entry.mask            = 0; /* Enabled */
1435                 entry.trigger         = 0; /* Edge */
1436                 entry.irr             = 0;
1437                 entry.polarity        = 0; /* High */
1438                 entry.delivery_status = 0;
1439                 entry.dest_mode       = 0; /* Physical */
1440                 entry.delivery_mode   = dest_ExtINT; /* ExtInt */
1441                 entry.vector          = 0;
1442                 entry.dest            = read_apic_id();
1443
1444                 /*
1445                  * Add it to the IO-APIC irq-routing table:
1446                  */
1447                 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1448         }
1449
1450         disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1451 }
1452
1453 /*
1454  * There is a nasty bug in some older SMP boards, their mptable lies
1455  * about the timer IRQ. We do the following to work around the situation:
1456  *
1457  *      - timer IRQ defaults to IO-APIC IRQ
1458  *      - if this function detects that timer IRQs are defunct, then we fall
1459  *        back to ISA timer IRQs
1460  */
1461 static int __init timer_irq_works(void)
1462 {
1463         unsigned long t1 = jiffies;
1464         unsigned long flags;
1465
1466         local_save_flags(flags);
1467         local_irq_enable();
1468         /* Let ten ticks pass... */
1469         mdelay((10 * 1000) / HZ);
1470         local_irq_restore(flags);
1471
1472         /*
1473          * Expect a few ticks at least, to be sure some possible
1474          * glue logic does not lock up after one or two first
1475          * ticks in a non-ExtINT mode.  Also the local APIC
1476          * might have cached one ExtINT interrupt.  Finally, at
1477          * least one tick may be lost due to delays.
1478          */
1479
1480         /* jiffies wrap? */
1481         if (time_after(jiffies, t1 + 4))
1482                 return 1;
1483         return 0;
1484 }
1485
1486 /*
1487  * In the SMP+IOAPIC case it might happen that there are an unspecified
1488  * number of pending IRQ events unhandled. These cases are very rare,
1489  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1490  * better to do it this way as thus we do not have to be aware of
1491  * 'pending' interrupts in the IRQ path, except at this point.
1492  */
1493 /*
1494  * Edge triggered needs to resend any interrupt
1495  * that was delayed but this is now handled in the device
1496  * independent code.
1497  */
1498
1499 /*
1500  * Starting up a edge-triggered IO-APIC interrupt is
1501  * nasty - we need to make sure that we get the edge.
1502  * If it is already asserted for some reason, we need
1503  * return 1 to indicate that is was pending.
1504  *
1505  * This is not complete - we should be able to fake
1506  * an edge even if it isn't on the 8259A...
1507  */
1508
1509 static unsigned int startup_ioapic_irq(unsigned int irq)
1510 {
1511         int was_pending = 0;
1512         unsigned long flags;
1513
1514         spin_lock_irqsave(&ioapic_lock, flags);
1515         if (irq < 16) {
1516                 disable_8259A_irq(irq);
1517                 if (i8259A_irq_pending(irq))
1518                         was_pending = 1;
1519         }
1520         __unmask_IO_APIC_irq(irq);
1521         spin_unlock_irqrestore(&ioapic_lock, flags);
1522
1523         return was_pending;
1524 }
1525
1526 static int ioapic_retrigger_irq(unsigned int irq)
1527 {
1528         struct irq_cfg *cfg = &irq_cfg[irq];
1529         cpumask_t mask;
1530         unsigned long flags;
1531
1532         spin_lock_irqsave(&vector_lock, flags);
1533         mask = cpumask_of_cpu(first_cpu(cfg->domain));
1534         send_IPI_mask(mask, cfg->vector);
1535         spin_unlock_irqrestore(&vector_lock, flags);
1536
1537         return 1;
1538 }
1539
1540 /*
1541  * Level and edge triggered IO-APIC interrupts need different handling,
1542  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1543  * handled with the level-triggered descriptor, but that one has slightly
1544  * more overhead. Level-triggered interrupts cannot be handled with the
1545  * edge-triggered handler, without risking IRQ storms and other ugly
1546  * races.
1547  */
1548
1549 #ifdef CONFIG_SMP
1550
1551 #ifdef CONFIG_INTR_REMAP
1552 static void ir_irq_migration(struct work_struct *work);
1553
1554 static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
1555
1556 /*
1557  * Migrate the IO-APIC irq in the presence of intr-remapping.
1558  *
1559  * For edge triggered, irq migration is a simple atomic update(of vector
1560  * and cpu destination) of IRTE and flush the hardware cache.
1561  *
1562  * For level triggered, we need to modify the io-apic RTE aswell with the update
1563  * vector information, along with modifying IRTE with vector and destination.
1564  * So irq migration for level triggered is little  bit more complex compared to
1565  * edge triggered migration. But the good news is, we use the same algorithm
1566  * for level triggered migration as we have today, only difference being,
1567  * we now initiate the irq migration from process context instead of the
1568  * interrupt context.
1569  *
1570  * In future, when we do a directed EOI (combined with cpu EOI broadcast
1571  * suppression) to the IO-APIC, level triggered irq migration will also be
1572  * as simple as edge triggered migration and we can do the irq migration
1573  * with a simple atomic update to IO-APIC RTE.
1574  */
1575 static void migrate_ioapic_irq(int irq, cpumask_t mask)
1576 {
1577         struct irq_cfg *cfg = irq_cfg + irq;
1578         struct irq_desc *desc = irq_desc + irq;
1579         cpumask_t tmp, cleanup_mask;
1580         struct irte irte;
1581         int modify_ioapic_rte = desc->status & IRQ_LEVEL;
1582         unsigned int dest;
1583         unsigned long flags;
1584
1585         cpus_and(tmp, mask, cpu_online_map);
1586         if (cpus_empty(tmp))
1587                 return;
1588
1589         if (get_irte(irq, &irte))
1590                 return;
1591
1592         if (assign_irq_vector(irq, mask))
1593                 return;
1594
1595         cpus_and(tmp, cfg->domain, mask);
1596         dest = cpu_mask_to_apicid(tmp);
1597
1598         if (modify_ioapic_rte) {
1599                 spin_lock_irqsave(&ioapic_lock, flags);
1600                 __target_IO_APIC_irq(irq, dest, cfg->vector);
1601                 spin_unlock_irqrestore(&ioapic_lock, flags);
1602         }
1603
1604         irte.vector = cfg->vector;
1605         irte.dest_id = IRTE_DEST(dest);
1606
1607         /*
1608          * Modified the IRTE and flushes the Interrupt entry cache.
1609          */
1610         modify_irte(irq, &irte);
1611
1612         if (cfg->move_in_progress) {
1613                 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1614                 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1615                 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1616                 cfg->move_in_progress = 0;
1617         }
1618
1619         irq_desc[irq].affinity = mask;
1620 }
1621
1622 static int migrate_irq_remapped_level(int irq)
1623 {
1624         int ret = -1;
1625
1626         mask_IO_APIC_irq(irq);
1627
1628         if (io_apic_level_ack_pending(irq)) {
1629                 /*
1630                  * Interrupt in progress. Migrating irq now will change the
1631                  * vector information in the IO-APIC RTE and that will confuse
1632                  * the EOI broadcast performed by cpu.
1633                  * So, delay the irq migration to the next instance.
1634                  */
1635                 schedule_delayed_work(&ir_migration_work, 1);
1636                 goto unmask;
1637         }
1638
1639         /* everthing is clear. we have right of way */
1640         migrate_ioapic_irq(irq, irq_desc[irq].pending_mask);
1641
1642         ret = 0;
1643         irq_desc[irq].status &= ~IRQ_MOVE_PENDING;
1644         cpus_clear(irq_desc[irq].pending_mask);
1645
1646 unmask:
1647         unmask_IO_APIC_irq(irq);
1648         return ret;
1649 }
1650
1651 static void ir_irq_migration(struct work_struct *work)
1652 {
1653         int irq;
1654
1655         for (irq = 0; irq < NR_IRQS; irq++) {
1656                 struct irq_desc *desc = irq_desc + irq;
1657                 if (desc->status & IRQ_MOVE_PENDING) {
1658                         unsigned long flags;
1659
1660                         spin_lock_irqsave(&desc->lock, flags);
1661                         if (!desc->chip->set_affinity ||
1662                             !(desc->status & IRQ_MOVE_PENDING)) {
1663                                 desc->status &= ~IRQ_MOVE_PENDING;
1664                                 spin_unlock_irqrestore(&desc->lock, flags);
1665                                 continue;
1666                         }
1667
1668                         desc->chip->set_affinity(irq,
1669                                                  irq_desc[irq].pending_mask);
1670                         spin_unlock_irqrestore(&desc->lock, flags);
1671                 }
1672         }
1673 }
1674
1675 /*
1676  * Migrates the IRQ destination in the process context.
1677  */
1678 static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
1679 {
1680         if (irq_desc[irq].status & IRQ_LEVEL) {
1681                 irq_desc[irq].status |= IRQ_MOVE_PENDING;
1682                 irq_desc[irq].pending_mask = mask;
1683                 migrate_irq_remapped_level(irq);
1684                 return;
1685         }
1686
1687         migrate_ioapic_irq(irq, mask);
1688 }
1689 #endif
1690
1691 asmlinkage void smp_irq_move_cleanup_interrupt(void)
1692 {
1693         unsigned vector, me;
1694         ack_APIC_irq();
1695         exit_idle();
1696         irq_enter();
1697
1698         me = smp_processor_id();
1699         for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1700                 unsigned int irq;
1701                 struct irq_desc *desc;
1702                 struct irq_cfg *cfg;
1703                 irq = __get_cpu_var(vector_irq)[vector];
1704                 if (irq >= NR_IRQS)
1705                         continue;
1706
1707                 desc = irq_desc + irq;
1708                 cfg = irq_cfg + irq;
1709                 spin_lock(&desc->lock);
1710                 if (!cfg->move_cleanup_count)
1711                         goto unlock;
1712
1713                 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
1714                         goto unlock;
1715
1716                 __get_cpu_var(vector_irq)[vector] = -1;
1717                 cfg->move_cleanup_count--;
1718 unlock:
1719                 spin_unlock(&desc->lock);
1720         }
1721
1722         irq_exit();
1723 }
1724
1725 static void irq_complete_move(unsigned int irq)
1726 {
1727         struct irq_cfg *cfg = irq_cfg + irq;
1728         unsigned vector, me;
1729
1730         if (likely(!cfg->move_in_progress))
1731                 return;
1732
1733         vector = ~get_irq_regs()->orig_ax;
1734         me = smp_processor_id();
1735         if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
1736                 cpumask_t cleanup_mask;
1737
1738                 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1739                 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1740                 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1741                 cfg->move_in_progress = 0;
1742         }
1743 }
1744 #else
1745 static inline void irq_complete_move(unsigned int irq) {}
1746 #endif
1747 #ifdef CONFIG_INTR_REMAP
1748 static void ack_x2apic_level(unsigned int irq)
1749 {
1750         ack_x2APIC_irq();
1751 }
1752
1753 static void ack_x2apic_edge(unsigned int irq)
1754 {
1755         ack_x2APIC_irq();
1756 }
1757 #endif
1758
1759 static void ack_apic_edge(unsigned int irq)
1760 {
1761         irq_complete_move(irq);
1762         move_native_irq(irq);
1763         ack_APIC_irq();
1764 }
1765
1766 static void ack_apic_level(unsigned int irq)
1767 {
1768         int do_unmask_irq = 0;
1769
1770         irq_complete_move(irq);
1771 #ifdef CONFIG_GENERIC_PENDING_IRQ
1772         /* If we are moving the irq we need to mask it */
1773         if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1774                 do_unmask_irq = 1;
1775                 mask_IO_APIC_irq(irq);
1776         }
1777 #endif
1778
1779         /*
1780          * We must acknowledge the irq before we move it or the acknowledge will
1781          * not propagate properly.
1782          */
1783         ack_APIC_irq();
1784
1785         /* Now we can move and renable the irq */
1786         if (unlikely(do_unmask_irq)) {
1787                 /* Only migrate the irq if the ack has been received.
1788                  *
1789                  * On rare occasions the broadcast level triggered ack gets
1790                  * delayed going to ioapics, and if we reprogram the
1791                  * vector while Remote IRR is still set the irq will never
1792                  * fire again.
1793                  *
1794                  * To prevent this scenario we read the Remote IRR bit
1795                  * of the ioapic.  This has two effects.
1796                  * - On any sane system the read of the ioapic will
1797                  *   flush writes (and acks) going to the ioapic from
1798                  *   this cpu.
1799                  * - We get to see if the ACK has actually been delivered.
1800                  *
1801                  * Based on failed experiments of reprogramming the
1802                  * ioapic entry from outside of irq context starting
1803                  * with masking the ioapic entry and then polling until
1804                  * Remote IRR was clear before reprogramming the
1805                  * ioapic I don't trust the Remote IRR bit to be
1806                  * completey accurate.
1807                  *
1808                  * However there appears to be no other way to plug
1809                  * this race, so if the Remote IRR bit is not
1810                  * accurate and is causing problems then it is a hardware bug
1811                  * and you can go talk to the chipset vendor about it.
1812                  */
1813                 if (!io_apic_level_ack_pending(irq))
1814                         move_masked_irq(irq);
1815                 unmask_IO_APIC_irq(irq);
1816         }
1817 }
1818
1819 static struct irq_chip ioapic_chip __read_mostly = {
1820         .name           = "IO-APIC",
1821         .startup        = startup_ioapic_irq,
1822         .mask           = mask_IO_APIC_irq,
1823         .unmask         = unmask_IO_APIC_irq,
1824         .ack            = ack_apic_edge,
1825         .eoi            = ack_apic_level,
1826 #ifdef CONFIG_SMP
1827         .set_affinity   = set_ioapic_affinity_irq,
1828 #endif
1829         .retrigger      = ioapic_retrigger_irq,
1830 };
1831
1832 #ifdef CONFIG_INTR_REMAP
1833 static struct irq_chip ir_ioapic_chip __read_mostly = {
1834         .name           = "IR-IO-APIC",
1835         .startup        = startup_ioapic_irq,
1836         .mask           = mask_IO_APIC_irq,
1837         .unmask         = unmask_IO_APIC_irq,
1838         .ack            = ack_x2apic_edge,
1839         .eoi            = ack_x2apic_level,
1840 #ifdef CONFIG_SMP
1841         .set_affinity   = set_ir_ioapic_affinity_irq,
1842 #endif
1843         .retrigger      = ioapic_retrigger_irq,
1844 };
1845 #endif
1846
1847 static inline void init_IO_APIC_traps(void)
1848 {
1849         int irq;
1850
1851         /*
1852          * NOTE! The local APIC isn't very good at handling
1853          * multiple interrupts at the same interrupt level.
1854          * As the interrupt level is determined by taking the
1855          * vector number and shifting that right by 4, we
1856          * want to spread these out a bit so that they don't
1857          * all fall in the same interrupt level.
1858          *
1859          * Also, we've got to be careful not to trash gate
1860          * 0x80, because int 0x80 is hm, kind of importantish. ;)
1861          */
1862         for (irq = 0; irq < NR_IRQS ; irq++) {
1863                 if (IO_APIC_IRQ(irq) && !irq_cfg[irq].vector) {
1864                         /*
1865                          * Hmm.. We don't have an entry for this,
1866                          * so default to an old-fashioned 8259
1867                          * interrupt if we can..
1868                          */
1869                         if (irq < 16)
1870                                 make_8259A_irq(irq);
1871                         else
1872                                 /* Strange. Oh, well.. */
1873                                 irq_desc[irq].chip = &no_irq_chip;
1874                 }
1875         }
1876 }
1877
1878 static void unmask_lapic_irq(unsigned int irq)
1879 {
1880         unsigned long v;
1881
1882         v = apic_read(APIC_LVT0);
1883         apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1884 }
1885
1886 static void mask_lapic_irq(unsigned int irq)
1887 {
1888         unsigned long v;
1889
1890         v = apic_read(APIC_LVT0);
1891         apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1892 }
1893
1894 static void ack_lapic_irq (unsigned int irq)
1895 {
1896         ack_APIC_irq();
1897 }
1898
1899 static struct irq_chip lapic_chip __read_mostly = {
1900         .name           = "local-APIC",
1901         .mask           = mask_lapic_irq,
1902         .unmask         = unmask_lapic_irq,
1903         .ack            = ack_lapic_irq,
1904 };
1905
1906 static void lapic_register_intr(int irq)
1907 {
1908         irq_desc[irq].status &= ~IRQ_LEVEL;
1909         set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
1910                                       "edge");
1911 }
1912
1913 static void __init setup_nmi(void)
1914 {
1915         /*
1916          * Dirty trick to enable the NMI watchdog ...
1917          * We put the 8259A master into AEOI mode and
1918          * unmask on all local APICs LVT0 as NMI.
1919          *
1920          * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1921          * is from Maciej W. Rozycki - so we do not have to EOI from
1922          * the NMI handler or the timer interrupt.
1923          */ 
1924         printk(KERN_INFO "activating NMI Watchdog ...");
1925
1926         enable_NMI_through_LVT0();
1927
1928         printk(" done.\n");
1929 }
1930
1931 /*
1932  * This looks a bit hackish but it's about the only one way of sending
1933  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
1934  * not support the ExtINT mode, unfortunately.  We need to send these
1935  * cycles as some i82489DX-based boards have glue logic that keeps the
1936  * 8259A interrupt line asserted until INTA.  --macro
1937  */
1938 static inline void __init unlock_ExtINT_logic(void)
1939 {
1940         int apic, pin, i;
1941         struct IO_APIC_route_entry entry0, entry1;
1942         unsigned char save_control, save_freq_select;
1943
1944         pin  = find_isa_irq_pin(8, mp_INT);
1945         apic = find_isa_irq_apic(8, mp_INT);
1946         if (pin == -1)
1947                 return;
1948
1949         entry0 = ioapic_read_entry(apic, pin);
1950
1951         clear_IO_APIC_pin(apic, pin);
1952
1953         memset(&entry1, 0, sizeof(entry1));
1954
1955         entry1.dest_mode = 0;                   /* physical delivery */
1956         entry1.mask = 0;                        /* unmask IRQ now */
1957         entry1.dest = hard_smp_processor_id();
1958         entry1.delivery_mode = dest_ExtINT;
1959         entry1.polarity = entry0.polarity;
1960         entry1.trigger = 0;
1961         entry1.vector = 0;
1962
1963         ioapic_write_entry(apic, pin, entry1);
1964
1965         save_control = CMOS_READ(RTC_CONTROL);
1966         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1967         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1968                    RTC_FREQ_SELECT);
1969         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1970
1971         i = 100;
1972         while (i-- > 0) {
1973                 mdelay(10);
1974                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1975                         i -= 10;
1976         }
1977
1978         CMOS_WRITE(save_control, RTC_CONTROL);
1979         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1980         clear_IO_APIC_pin(apic, pin);
1981
1982         ioapic_write_entry(apic, pin, entry0);
1983 }
1984
1985 /*
1986  * This code may look a bit paranoid, but it's supposed to cooperate with
1987  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
1988  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
1989  * fanatically on his truly buggy board.
1990  *
1991  * FIXME: really need to revamp this for modern platforms only.
1992  */
1993 static inline void __init check_timer(void)
1994 {
1995         struct irq_cfg *cfg = irq_cfg + 0;
1996         int apic1, pin1, apic2, pin2;
1997         unsigned long flags;
1998         int no_pin1 = 0;
1999
2000         local_irq_save(flags);
2001
2002         /*
2003          * get/set the timer IRQ vector:
2004          */
2005         disable_8259A_irq(0);
2006         assign_irq_vector(0, TARGET_CPUS);
2007
2008         /*
2009          * As IRQ0 is to be enabled in the 8259A, the virtual
2010          * wire has to be disabled in the local APIC.
2011          */
2012         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2013         init_8259A(1);
2014
2015         pin1  = find_isa_irq_pin(0, mp_INT);
2016         apic1 = find_isa_irq_apic(0, mp_INT);
2017         pin2  = ioapic_i8259.pin;
2018         apic2 = ioapic_i8259.apic;
2019
2020         apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
2021                 cfg->vector, apic1, pin1, apic2, pin2);
2022
2023         if (mask_ioapic_irq_2)
2024                 mask_IO_APIC_irq(2);
2025
2026         /*
2027          * Some BIOS writers are clueless and report the ExtINTA
2028          * I/O APIC input from the cascaded 8259A as the timer
2029          * interrupt input.  So just in case, if only one pin
2030          * was found above, try it both directly and through the
2031          * 8259A.
2032          */
2033         if (pin1 == -1) {
2034                 if (intr_remapping_enabled)
2035                         panic("BIOS bug: timer not connected to IO-APIC");
2036                 pin1 = pin2;
2037                 apic1 = apic2;
2038                 no_pin1 = 1;
2039         } else if (pin2 == -1) {
2040                 pin2 = pin1;
2041                 apic2 = apic1;
2042         }
2043
2044         if (pin1 != -1) {
2045                 /*
2046                  * Ok, does IRQ0 through the IOAPIC work?
2047                  */
2048                 if (no_pin1) {
2049                         add_pin_to_irq(0, apic1, pin1);
2050                         setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2051                 }
2052                 unmask_IO_APIC_irq(0);
2053                 if (!no_timer_check && timer_irq_works()) {
2054                         if (nmi_watchdog == NMI_IO_APIC) {
2055                                 setup_nmi();
2056                                 enable_8259A_irq(0);
2057                         }
2058                         if (disable_timer_pin_1 > 0)
2059                                 clear_IO_APIC_pin(0, pin1);
2060                         goto out;
2061                 }
2062                 if (intr_remapping_enabled)
2063                         panic("timer doesn't work through Interrupt-remapped IO-APIC");
2064                 clear_IO_APIC_pin(apic1, pin1);
2065                 if (!no_pin1)
2066                         apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: "
2067                                     "8254 timer not connected to IO-APIC\n");
2068
2069                 apic_printk(APIC_VERBOSE,KERN_INFO
2070                         "...trying to set up timer (IRQ0) "
2071                         "through the 8259A ... ");
2072                 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
2073                         apic2, pin2);
2074                 /*
2075                  * legacy devices should be connected to IO APIC #0
2076                  */
2077                 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
2078                 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2079                 unmask_IO_APIC_irq(0);
2080                 enable_8259A_irq(0);
2081                 if (timer_irq_works()) {
2082                         apic_printk(APIC_VERBOSE," works.\n");
2083                         timer_through_8259 = 1;
2084                         if (nmi_watchdog == NMI_IO_APIC) {
2085                                 disable_8259A_irq(0);
2086                                 setup_nmi();
2087                                 enable_8259A_irq(0);
2088                         }
2089                         goto out;
2090                 }
2091                 /*
2092                  * Cleanup, just in case ...
2093                  */
2094                 disable_8259A_irq(0);
2095                 clear_IO_APIC_pin(apic2, pin2);
2096                 apic_printk(APIC_VERBOSE," failed.\n");
2097         }
2098
2099         if (nmi_watchdog == NMI_IO_APIC) {
2100                 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
2101                 nmi_watchdog = NMI_NONE;
2102         }
2103
2104         apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2105
2106         lapic_register_intr(0);
2107         apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector);     /* Fixed mode */
2108         enable_8259A_irq(0);
2109
2110         if (timer_irq_works()) {
2111                 apic_printk(APIC_VERBOSE," works.\n");
2112                 goto out;
2113         }
2114         disable_8259A_irq(0);
2115         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2116         apic_printk(APIC_VERBOSE," failed.\n");
2117
2118         apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2119
2120         init_8259A(0);
2121         make_8259A_irq(0);
2122         apic_write(APIC_LVT0, APIC_DM_EXTINT);
2123
2124         unlock_ExtINT_logic();
2125
2126         if (timer_irq_works()) {
2127                 apic_printk(APIC_VERBOSE," works.\n");
2128                 goto out;
2129         }
2130         apic_printk(APIC_VERBOSE," failed :(.\n");
2131         panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
2132 out:
2133         local_irq_restore(flags);
2134 }
2135
2136 static int __init notimercheck(char *s)
2137 {
2138         no_timer_check = 1;
2139         return 1;
2140 }
2141 __setup("no_timer_check", notimercheck);
2142
2143 /*
2144  * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2145  * to devices.  However there may be an I/O APIC pin available for
2146  * this interrupt regardless.  The pin may be left unconnected, but
2147  * typically it will be reused as an ExtINT cascade interrupt for
2148  * the master 8259A.  In the MPS case such a pin will normally be
2149  * reported as an ExtINT interrupt in the MP table.  With ACPI
2150  * there is no provision for ExtINT interrupts, and in the absence
2151  * of an override it would be treated as an ordinary ISA I/O APIC
2152  * interrupt, that is edge-triggered and unmasked by default.  We
2153  * used to do this, but it caused problems on some systems because
2154  * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2155  * the same ExtINT cascade interrupt to drive the local APIC of the
2156  * bootstrap processor.  Therefore we refrain from routing IRQ2 to
2157  * the I/O APIC in all cases now.  No actual device should request
2158  * it anyway.  --macro
2159  */
2160 #define PIC_IRQS        (1<<2)
2161
2162 void __init setup_IO_APIC(void)
2163 {
2164
2165         /*
2166          * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2167          */
2168
2169         io_apic_irqs = ~PIC_IRQS;
2170
2171         apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2172
2173         sync_Arb_IDs();
2174         setup_IO_APIC_irqs();
2175         init_IO_APIC_traps();
2176         check_timer();
2177         if (!acpi_ioapic)
2178                 print_IO_APIC();
2179 }
2180
2181 struct sysfs_ioapic_data {
2182         struct sys_device dev;
2183         struct IO_APIC_route_entry entry[0];
2184 };
2185 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2186
2187 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2188 {
2189         struct IO_APIC_route_entry *entry;
2190         struct sysfs_ioapic_data *data;
2191         int i;
2192
2193         data = container_of(dev, struct sysfs_ioapic_data, dev);
2194         entry = data->entry;
2195         for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2196                 *entry = ioapic_read_entry(dev->id, i);
2197
2198         return 0;
2199 }
2200
2201 static int ioapic_resume(struct sys_device *dev)
2202 {
2203         struct IO_APIC_route_entry *entry;
2204         struct sysfs_ioapic_data *data;
2205         unsigned long flags;
2206         union IO_APIC_reg_00 reg_00;
2207         int i;
2208
2209         data = container_of(dev, struct sysfs_ioapic_data, dev);
2210         entry = data->entry;
2211
2212         spin_lock_irqsave(&ioapic_lock, flags);
2213         reg_00.raw = io_apic_read(dev->id, 0);
2214         if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2215                 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
2216                 io_apic_write(dev->id, 0, reg_00.raw);
2217         }
2218         spin_unlock_irqrestore(&ioapic_lock, flags);
2219         for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
2220                 ioapic_write_entry(dev->id, i, entry[i]);
2221
2222         return 0;
2223 }
2224
2225 static struct sysdev_class ioapic_sysdev_class = {
2226         .name = "ioapic",
2227         .suspend = ioapic_suspend,
2228         .resume = ioapic_resume,
2229 };
2230
2231 static int __init ioapic_init_sysfs(void)
2232 {
2233         struct sys_device * dev;
2234         int i, size, error;
2235
2236         error = sysdev_class_register(&ioapic_sysdev_class);
2237         if (error)
2238                 return error;
2239
2240         for (i = 0; i < nr_ioapics; i++ ) {
2241                 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2242                         * sizeof(struct IO_APIC_route_entry);
2243                 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
2244                 if (!mp_ioapic_data[i]) {
2245                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2246                         continue;
2247                 }
2248                 dev = &mp_ioapic_data[i]->dev;
2249                 dev->id = i;
2250                 dev->cls = &ioapic_sysdev_class;
2251                 error = sysdev_register(dev);
2252                 if (error) {
2253                         kfree(mp_ioapic_data[i]);
2254                         mp_ioapic_data[i] = NULL;
2255                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2256                         continue;
2257                 }
2258         }
2259
2260         return 0;
2261 }
2262
2263 device_initcall(ioapic_init_sysfs);
2264
2265 /*
2266  * Dynamic irq allocate and deallocation
2267  */
2268 int create_irq(void)
2269 {
2270         /* Allocate an unused irq */
2271         int irq;
2272         int new;
2273         unsigned long flags;
2274
2275         irq = -ENOSPC;
2276         spin_lock_irqsave(&vector_lock, flags);
2277         for (new = (NR_IRQS - 1); new >= 0; new--) {
2278                 if (platform_legacy_irq(new))
2279                         continue;
2280                 if (irq_cfg[new].vector != 0)
2281                         continue;
2282                 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
2283                         irq = new;
2284                 break;
2285         }
2286         spin_unlock_irqrestore(&vector_lock, flags);
2287
2288         if (irq >= 0) {
2289                 dynamic_irq_init(irq);
2290         }
2291         return irq;
2292 }
2293
2294 void destroy_irq(unsigned int irq)
2295 {
2296         unsigned long flags;
2297
2298         dynamic_irq_cleanup(irq);
2299
2300 #ifdef CONFIG_INTR_REMAP
2301         free_irte(irq);
2302 #endif
2303         spin_lock_irqsave(&vector_lock, flags);
2304         __clear_irq_vector(irq);
2305         spin_unlock_irqrestore(&vector_lock, flags);
2306 }
2307
2308 /*
2309  * MSI message composition
2310  */
2311 #ifdef CONFIG_PCI_MSI
2312 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
2313 {
2314         struct irq_cfg *cfg = irq_cfg + irq;
2315         int err;
2316         unsigned dest;
2317         cpumask_t tmp;
2318
2319         tmp = TARGET_CPUS;
2320         err = assign_irq_vector(irq, tmp);
2321         if (err)
2322                 return err;
2323
2324         cpus_and(tmp, cfg->domain, tmp);
2325         dest = cpu_mask_to_apicid(tmp);
2326
2327 #ifdef CONFIG_INTR_REMAP
2328         if (irq_remapped(irq)) {
2329                 struct irte irte;
2330                 int ir_index;
2331                 u16 sub_handle;
2332
2333                 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
2334                 BUG_ON(ir_index == -1);
2335
2336                 memset (&irte, 0, sizeof(irte));
2337
2338                 irte.present = 1;
2339                 irte.dst_mode = INT_DEST_MODE;
2340                 irte.trigger_mode = 0; /* edge */
2341                 irte.dlvry_mode = INT_DELIVERY_MODE;
2342                 irte.vector = cfg->vector;
2343                 irte.dest_id = IRTE_DEST(dest);
2344
2345                 modify_irte(irq, &irte);
2346
2347                 msg->address_hi = MSI_ADDR_BASE_HI;
2348                 msg->data = sub_handle;
2349                 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
2350                                   MSI_ADDR_IR_SHV |
2351                                   MSI_ADDR_IR_INDEX1(ir_index) |
2352                                   MSI_ADDR_IR_INDEX2(ir_index);
2353         } else
2354 #endif
2355         {
2356                 msg->address_hi = MSI_ADDR_BASE_HI;
2357                 msg->address_lo =
2358                         MSI_ADDR_BASE_LO |
2359                         ((INT_DEST_MODE == 0) ?
2360                                 MSI_ADDR_DEST_MODE_PHYSICAL:
2361                                 MSI_ADDR_DEST_MODE_LOGICAL) |
2362                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2363                                 MSI_ADDR_REDIRECTION_CPU:
2364                                 MSI_ADDR_REDIRECTION_LOWPRI) |
2365                         MSI_ADDR_DEST_ID(dest);
2366
2367                 msg->data =
2368                         MSI_DATA_TRIGGER_EDGE |
2369                         MSI_DATA_LEVEL_ASSERT |
2370                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2371                                 MSI_DATA_DELIVERY_FIXED:
2372                                 MSI_DATA_DELIVERY_LOWPRI) |
2373                         MSI_DATA_VECTOR(cfg->vector);
2374         }
2375         return err;
2376 }
2377
2378 #ifdef CONFIG_SMP
2379 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2380 {
2381         struct irq_cfg *cfg = irq_cfg + irq;
2382         struct msi_msg msg;
2383         unsigned int dest;
2384         cpumask_t tmp;
2385
2386         cpus_and(tmp, mask, cpu_online_map);
2387         if (cpus_empty(tmp))
2388                 return;
2389
2390         if (assign_irq_vector(irq, mask))
2391                 return;
2392
2393         cpus_and(tmp, cfg->domain, mask);
2394         dest = cpu_mask_to_apicid(tmp);
2395
2396         read_msi_msg(irq, &msg);
2397
2398         msg.data &= ~MSI_DATA_VECTOR_MASK;
2399         msg.data |= MSI_DATA_VECTOR(cfg->vector);
2400         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2401         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2402
2403         write_msi_msg(irq, &msg);
2404         irq_desc[irq].affinity = mask;
2405 }
2406
2407 #ifdef CONFIG_INTR_REMAP
2408 /*
2409  * Migrate the MSI irq to another cpumask. This migration is
2410  * done in the process context using interrupt-remapping hardware.
2411  */
2412 static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2413 {
2414         struct irq_cfg *cfg = irq_cfg + irq;
2415         unsigned int dest;
2416         cpumask_t tmp, cleanup_mask;
2417         struct irte irte;
2418
2419         cpus_and(tmp, mask, cpu_online_map);
2420         if (cpus_empty(tmp))
2421                 return;
2422
2423         if (get_irte(irq, &irte))
2424                 return;
2425
2426         if (assign_irq_vector(irq, mask))
2427                 return;
2428
2429         cpus_and(tmp, cfg->domain, mask);
2430         dest = cpu_mask_to_apicid(tmp);
2431
2432         irte.vector = cfg->vector;
2433         irte.dest_id = IRTE_DEST(dest);
2434
2435         /*
2436          * atomically update the IRTE with the new destination and vector.
2437          */
2438         modify_irte(irq, &irte);
2439
2440         /*
2441          * After this point, all the interrupts will start arriving
2442          * at the new destination. So, time to cleanup the previous
2443          * vector allocation.
2444          */
2445         if (cfg->move_in_progress) {
2446                 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2447                 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2448                 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2449                 cfg->move_in_progress = 0;
2450         }
2451
2452         irq_desc[irq].affinity = mask;
2453 }
2454 #endif
2455 #endif /* CONFIG_SMP */
2456
2457 /*
2458  * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2459  * which implement the MSI or MSI-X Capability Structure.
2460  */
2461 static struct irq_chip msi_chip = {
2462         .name           = "PCI-MSI",
2463         .unmask         = unmask_msi_irq,
2464         .mask           = mask_msi_irq,
2465         .ack            = ack_apic_edge,
2466 #ifdef CONFIG_SMP
2467         .set_affinity   = set_msi_irq_affinity,
2468 #endif
2469         .retrigger      = ioapic_retrigger_irq,
2470 };
2471
2472 #ifdef CONFIG_INTR_REMAP
2473 static struct irq_chip msi_ir_chip = {
2474         .name           = "IR-PCI-MSI",
2475         .unmask         = unmask_msi_irq,
2476         .mask           = mask_msi_irq,
2477         .ack            = ack_x2apic_edge,
2478 #ifdef CONFIG_SMP
2479         .set_affinity   = ir_set_msi_irq_affinity,
2480 #endif
2481         .retrigger      = ioapic_retrigger_irq,
2482 };
2483
2484 /*
2485  * Map the PCI dev to the corresponding remapping hardware unit
2486  * and allocate 'nvec' consecutive interrupt-remapping table entries
2487  * in it.
2488  */
2489 static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
2490 {
2491         struct intel_iommu *iommu;
2492         int index;
2493
2494         iommu = map_dev_to_ir(dev);
2495         if (!iommu) {
2496                 printk(KERN_ERR
2497                        "Unable to map PCI %s to iommu\n", pci_name(dev));
2498                 return -ENOENT;
2499         }
2500
2501         index = alloc_irte(iommu, irq, nvec);
2502         if (index < 0) {
2503                 printk(KERN_ERR
2504                        "Unable to allocate %d IRTE for PCI %s\n", nvec,
2505                         pci_name(dev));
2506                 return -ENOSPC;
2507         }
2508         return index;
2509 }
2510 #endif
2511
2512 static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
2513 {
2514         int ret;
2515         struct msi_msg msg;
2516
2517         ret = msi_compose_msg(dev, irq, &msg);
2518         if (ret < 0)
2519                 return ret;
2520
2521         set_irq_msi(irq, desc);
2522         write_msi_msg(irq, &msg);
2523
2524 #ifdef CONFIG_INTR_REMAP
2525         if (irq_remapped(irq)) {
2526                 struct irq_desc *desc = irq_desc + irq;
2527                 /*
2528                  * irq migration in process context
2529                  */
2530                 desc->status |= IRQ_MOVE_PCNTXT;
2531                 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
2532         } else
2533 #endif
2534                 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
2535
2536         return 0;
2537 }
2538
2539 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
2540 {
2541         int irq, ret;
2542
2543         irq = create_irq();
2544         if (irq < 0)
2545                 return irq;
2546
2547 #ifdef CONFIG_INTR_REMAP
2548         if (!intr_remapping_enabled)
2549                 goto no_ir;
2550
2551         ret = msi_alloc_irte(dev, irq, 1);
2552         if (ret < 0)
2553                 goto error;
2554 no_ir:
2555 #endif
2556         ret = setup_msi_irq(dev, desc, irq);
2557         if (ret < 0) {
2558                 destroy_irq(irq);
2559                 return ret;
2560         }
2561         return 0;
2562
2563 #ifdef CONFIG_INTR_REMAP
2564 error:
2565         destroy_irq(irq);
2566         return ret;
2567 #endif
2568 }
2569
2570 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
2571 {
2572         int irq, ret, sub_handle;
2573         struct msi_desc *desc;
2574 #ifdef CONFIG_INTR_REMAP
2575         struct intel_iommu *iommu = 0;
2576         int index = 0;
2577 #endif
2578
2579         sub_handle = 0;
2580         list_for_each_entry(desc, &dev->msi_list, list) {
2581                 irq = create_irq();
2582                 if (irq < 0)
2583                         return irq;
2584 #ifdef CONFIG_INTR_REMAP
2585                 if (!intr_remapping_enabled)
2586                         goto no_ir;
2587
2588                 if (!sub_handle) {
2589                         /*
2590                          * allocate the consecutive block of IRTE's
2591                          * for 'nvec'
2592                          */
2593                         index = msi_alloc_irte(dev, irq, nvec);
2594                         if (index < 0) {
2595                                 ret = index;
2596                                 goto error;
2597                         }
2598                 } else {
2599                         iommu = map_dev_to_ir(dev);
2600                         if (!iommu) {
2601                                 ret = -ENOENT;
2602                                 goto error;
2603                         }
2604                         /*
2605                          * setup the mapping between the irq and the IRTE
2606                          * base index, the sub_handle pointing to the
2607                          * appropriate interrupt remap table entry.
2608                          */
2609                         set_irte_irq(irq, iommu, index, sub_handle);
2610                 }
2611 no_ir:
2612 #endif
2613                 ret = setup_msi_irq(dev, desc, irq);
2614                 if (ret < 0)
2615                         goto error;
2616                 sub_handle++;
2617         }
2618         return 0;
2619
2620 error:
2621         destroy_irq(irq);
2622         return ret;
2623 }
2624
2625 void arch_teardown_msi_irq(unsigned int irq)
2626 {
2627         destroy_irq(irq);
2628 }
2629
2630 #ifdef CONFIG_DMAR
2631 #ifdef CONFIG_SMP
2632 static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
2633 {
2634         struct irq_cfg *cfg = irq_cfg + irq;
2635         struct msi_msg msg;
2636         unsigned int dest;
2637         cpumask_t tmp;
2638
2639         cpus_and(tmp, mask, cpu_online_map);
2640         if (cpus_empty(tmp))
2641                 return;
2642
2643         if (assign_irq_vector(irq, mask))
2644                 return;
2645
2646         cpus_and(tmp, cfg->domain, mask);
2647         dest = cpu_mask_to_apicid(tmp);
2648
2649         dmar_msi_read(irq, &msg);
2650
2651         msg.data &= ~MSI_DATA_VECTOR_MASK;
2652         msg.data |= MSI_DATA_VECTOR(cfg->vector);
2653         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2654         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2655
2656         dmar_msi_write(irq, &msg);
2657         irq_desc[irq].affinity = mask;
2658 }
2659 #endif /* CONFIG_SMP */
2660
2661 struct irq_chip dmar_msi_type = {
2662         .name = "DMAR_MSI",
2663         .unmask = dmar_msi_unmask,
2664         .mask = dmar_msi_mask,
2665         .ack = ack_apic_edge,
2666 #ifdef CONFIG_SMP
2667         .set_affinity = dmar_msi_set_affinity,
2668 #endif
2669         .retrigger = ioapic_retrigger_irq,
2670 };
2671
2672 int arch_setup_dmar_msi(unsigned int irq)
2673 {
2674         int ret;
2675         struct msi_msg msg;
2676
2677         ret = msi_compose_msg(NULL, irq, &msg);
2678         if (ret < 0)
2679                 return ret;
2680         dmar_msi_write(irq, &msg);
2681         set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
2682                 "edge");
2683         return 0;
2684 }
2685 #endif
2686
2687 #endif /* CONFIG_PCI_MSI */
2688 /*
2689  * Hypertransport interrupt support
2690  */
2691 #ifdef CONFIG_HT_IRQ
2692
2693 #ifdef CONFIG_SMP
2694
2695 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2696 {
2697         struct ht_irq_msg msg;
2698         fetch_ht_irq_msg(irq, &msg);
2699
2700         msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2701         msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2702
2703         msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2704         msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2705
2706         write_ht_irq_msg(irq, &msg);
2707 }
2708
2709 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2710 {
2711         struct irq_cfg *cfg = irq_cfg + irq;
2712         unsigned int dest;
2713         cpumask_t tmp;
2714
2715         cpus_and(tmp, mask, cpu_online_map);
2716         if (cpus_empty(tmp))
2717                 return;
2718
2719         if (assign_irq_vector(irq, mask))
2720                 return;
2721
2722         cpus_and(tmp, cfg->domain, mask);
2723         dest = cpu_mask_to_apicid(tmp);
2724
2725         target_ht_irq(irq, dest, cfg->vector);
2726         irq_desc[irq].affinity = mask;
2727 }
2728 #endif
2729
2730 static struct irq_chip ht_irq_chip = {
2731         .name           = "PCI-HT",
2732         .mask           = mask_ht_irq,
2733         .unmask         = unmask_ht_irq,
2734         .ack            = ack_apic_edge,
2735 #ifdef CONFIG_SMP
2736         .set_affinity   = set_ht_irq_affinity,
2737 #endif
2738         .retrigger      = ioapic_retrigger_irq,
2739 };
2740
2741 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2742 {
2743         struct irq_cfg *cfg = irq_cfg + irq;
2744         int err;
2745         cpumask_t tmp;
2746
2747         tmp = TARGET_CPUS;
2748         err = assign_irq_vector(irq, tmp);
2749         if (!err) {
2750                 struct ht_irq_msg msg;
2751                 unsigned dest;
2752
2753                 cpus_and(tmp, cfg->domain, tmp);
2754                 dest = cpu_mask_to_apicid(tmp);
2755
2756                 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2757
2758                 msg.address_lo =
2759                         HT_IRQ_LOW_BASE |
2760                         HT_IRQ_LOW_DEST_ID(dest) |
2761                         HT_IRQ_LOW_VECTOR(cfg->vector) |
2762                         ((INT_DEST_MODE == 0) ?
2763                                 HT_IRQ_LOW_DM_PHYSICAL :
2764                                 HT_IRQ_LOW_DM_LOGICAL) |
2765                         HT_IRQ_LOW_RQEOI_EDGE |
2766                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2767                                 HT_IRQ_LOW_MT_FIXED :
2768                                 HT_IRQ_LOW_MT_ARBITRATED) |
2769                         HT_IRQ_LOW_IRQ_MASKED;
2770
2771                 write_ht_irq_msg(irq, &msg);
2772
2773                 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2774                                               handle_edge_irq, "edge");
2775         }
2776         return err;
2777 }
2778 #endif /* CONFIG_HT_IRQ */
2779
2780 /* --------------------------------------------------------------------------
2781                           ACPI-based IOAPIC Configuration
2782    -------------------------------------------------------------------------- */
2783
2784 #ifdef CONFIG_ACPI
2785
2786 #define IO_APIC_MAX_ID          0xFE
2787
2788 int __init io_apic_get_redir_entries (int ioapic)
2789 {
2790         union IO_APIC_reg_01    reg_01;
2791         unsigned long flags;
2792
2793         spin_lock_irqsave(&ioapic_lock, flags);
2794         reg_01.raw = io_apic_read(ioapic, 1);
2795         spin_unlock_irqrestore(&ioapic_lock, flags);
2796
2797         return reg_01.bits.entries;
2798 }
2799
2800
2801 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2802 {
2803         if (!IO_APIC_IRQ(irq)) {
2804                 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2805                         ioapic);
2806                 return -EINVAL;
2807         }
2808
2809         /*
2810          * IRQs < 16 are already in the irq_2_pin[] map
2811          */
2812         if (irq >= 16)
2813                 add_pin_to_irq(irq, ioapic, pin);
2814
2815         setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
2816
2817         return 0;
2818 }
2819
2820
2821 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2822 {
2823         int i;
2824
2825         if (skip_ioapic_setup)
2826                 return -1;
2827
2828         for (i = 0; i < mp_irq_entries; i++)
2829                 if (mp_irqs[i].mp_irqtype == mp_INT &&
2830                     mp_irqs[i].mp_srcbusirq == bus_irq)
2831                         break;
2832         if (i >= mp_irq_entries)
2833                 return -1;
2834
2835         *trigger = irq_trigger(i);
2836         *polarity = irq_polarity(i);
2837         return 0;
2838 }
2839
2840 #endif /* CONFIG_ACPI */
2841
2842 /*
2843  * This function currently is only a helper for the i386 smp boot process where
2844  * we need to reprogram the ioredtbls to cater for the cpus which have come online
2845  * so mask in all cases should simply be TARGET_CPUS
2846  */
2847 #ifdef CONFIG_SMP
2848 void __init setup_ioapic_dest(void)
2849 {
2850         int pin, ioapic, irq, irq_entry;
2851
2852         if (skip_ioapic_setup == 1)
2853                 return;
2854
2855         for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2856                 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2857                         irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2858                         if (irq_entry == -1)
2859                                 continue;
2860                         irq = pin_2_irq(irq_entry, ioapic, pin);
2861
2862                         /* setup_IO_APIC_irqs could fail to get vector for some device
2863                          * when you have too many devices, because at that time only boot
2864                          * cpu is online.
2865                          */
2866                         if (!irq_cfg[irq].vector)
2867                                 setup_IO_APIC_irq(ioapic, pin, irq,
2868                                                   irq_trigger(irq_entry),
2869                                                   irq_polarity(irq_entry));
2870 #ifdef CONFIG_INTR_REMAP
2871                         else if (intr_remapping_enabled)
2872                                 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
2873 #endif
2874                         else
2875                                 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2876                 }
2877
2878         }
2879 }
2880 #endif
2881
2882 #define IOAPIC_RESOURCE_NAME_SIZE 11
2883
2884 static struct resource *ioapic_resources;
2885
2886 static struct resource * __init ioapic_setup_resources(void)
2887 {
2888         unsigned long n;
2889         struct resource *res;
2890         char *mem;
2891         int i;
2892
2893         if (nr_ioapics <= 0)
2894                 return NULL;
2895
2896         n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
2897         n *= nr_ioapics;
2898
2899         mem = alloc_bootmem(n);
2900         res = (void *)mem;
2901
2902         if (mem != NULL) {
2903                 mem += sizeof(struct resource) * nr_ioapics;
2904
2905                 for (i = 0; i < nr_ioapics; i++) {
2906                         res[i].name = mem;
2907                         res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
2908                         sprintf(mem,  "IOAPIC %u", i);
2909                         mem += IOAPIC_RESOURCE_NAME_SIZE;
2910                 }
2911         }
2912
2913         ioapic_resources = res;
2914
2915         return res;
2916 }
2917
2918 void __init ioapic_init_mappings(void)
2919 {
2920         unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2921         struct resource *ioapic_res;
2922         int i;
2923
2924         ioapic_res = ioapic_setup_resources();
2925         for (i = 0; i < nr_ioapics; i++) {
2926                 if (smp_found_config) {
2927                         ioapic_phys = mp_ioapics[i].mp_apicaddr;
2928                 } else {
2929                         ioapic_phys = (unsigned long)
2930                                 alloc_bootmem_pages(PAGE_SIZE);
2931                         ioapic_phys = __pa(ioapic_phys);
2932                 }
2933                 set_fixmap_nocache(idx, ioapic_phys);
2934                 apic_printk(APIC_VERBOSE,
2935                             "mapped IOAPIC to %016lx (%016lx)\n",
2936                             __fix_to_virt(idx), ioapic_phys);
2937                 idx++;
2938
2939                 if (ioapic_res != NULL) {
2940                         ioapic_res->start = ioapic_phys;
2941                         ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
2942                         ioapic_res++;
2943                 }
2944         }
2945 }
2946
2947 static int __init ioapic_insert_resources(void)
2948 {
2949         int i;
2950         struct resource *r = ioapic_resources;
2951
2952         if (!r) {
2953                 printk(KERN_ERR
2954                        "IO APIC resources could be not be allocated.\n");
2955                 return -1;
2956         }
2957
2958         for (i = 0; i < nr_ioapics; i++) {
2959                 insert_resource(&iomem_resource, r);
2960                 r++;
2961         }
2962
2963         return 0;
2964 }
2965
2966 /* Insert the IO APIC resources after PCI initialization has occured to handle
2967  * IO APICS that are mapped in on a BAR in PCI space. */
2968 late_initcall(ioapic_insert_resources);
2969