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