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