fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / x86_64 / kernel / io_apic-xen.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/smp_lock.h>
29 #include <linux/pci.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
33 #include <linux/msi.h>
34 #include <linux/htirq.h>
35 #ifdef CONFIG_ACPI
36 #include <acpi/acpi_bus.h>
37 #endif
38
39 #include <asm/io.h>
40 #include <asm/smp.h>
41 #include <asm/desc.h>
42 #include <asm/proto.h>
43 #include <asm/mach_apic.h>
44 #include <asm/acpi.h>
45 #include <asm/dma.h>
46 #include <asm/nmi.h>
47 #include <asm/msidef.h>
48 #include <asm/hypertransport.h>
49
50 DEFINE_PER_CPU(vector_irq_t, vector_irq) = {
51         [0 ... FIRST_EXTERNAL_VECTOR - 1] = -1,
52         [FIRST_EXTERNAL_VECTOR + 0] = 0,
53         [FIRST_EXTERNAL_VECTOR + 1] = 1,
54         [FIRST_EXTERNAL_VECTOR + 2] = 2,
55         [FIRST_EXTERNAL_VECTOR + 3] = 3,
56         [FIRST_EXTERNAL_VECTOR + 4] = 4,
57         [FIRST_EXTERNAL_VECTOR + 5] = 5,
58         [FIRST_EXTERNAL_VECTOR + 6] = 6,
59         [FIRST_EXTERNAL_VECTOR + 7] = 7,
60         [FIRST_EXTERNAL_VECTOR + 8] = 8,
61         [FIRST_EXTERNAL_VECTOR + 9] = 9,
62         [FIRST_EXTERNAL_VECTOR + 10] = 10,
63         [FIRST_EXTERNAL_VECTOR + 11] = 11,
64         [FIRST_EXTERNAL_VECTOR + 12] = 12,
65         [FIRST_EXTERNAL_VECTOR + 13] = 13,
66         [FIRST_EXTERNAL_VECTOR + 14] = 14,
67         [FIRST_EXTERNAL_VECTOR + 15] = 15,
68         [FIRST_EXTERNAL_VECTOR + 16 ... NR_VECTORS - 1] = -1
69 };
70
71 static int assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result);
72
73 #define __apicdebuginit  __init
74
75 int sis_apic_bug; /* not actually supported, dummy for compile */
76
77 static int no_timer_check;
78
79 static int disable_timer_pin_1 __initdata;
80
81 int timer_over_8254 __initdata = 1;
82
83 #ifndef CONFIG_XEN
84 /* Where if anywhere is the i8259 connect in external int mode */
85 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
86 #endif
87
88 static DEFINE_SPINLOCK(ioapic_lock);
89 DEFINE_SPINLOCK(vector_lock);
90
91 /*
92  * # of IRQ routing registers
93  */
94 int nr_ioapic_registers[MAX_IO_APICS];
95
96 /*
97  * Rough estimation of how many shared IRQs there are, can
98  * be changed anytime.
99  */
100 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
101 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
102
103 /*
104  * This is performance-critical, we want to do it O(1)
105  *
106  * the indexing order of this array favors 1:1 mappings
107  * between pins and IRQs.
108  */
109
110 static struct irq_pin_list {
111         short apic, pin, next;
112 } irq_2_pin[PIN_MAP_SIZE];
113
114 struct io_apic {
115         unsigned int index;
116         unsigned int unused[3];
117         unsigned int data;
118 };
119
120 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
121 {
122         return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
123                 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
124 }
125
126 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
127 {
128         struct io_apic __iomem *io_apic = io_apic_base(apic);
129         writel(reg, &io_apic->index);
130         return readl(&io_apic->data);
131 }
132
133 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
134 {
135         struct io_apic __iomem *io_apic = io_apic_base(apic);
136         writel(reg, &io_apic->index);
137         writel(value, &io_apic->data);
138 }
139
140 /*
141  * Re-write a value: to be used for read-modify-write
142  * cycles where the read already set up the index register.
143  */
144 static inline void io_apic_modify(unsigned int apic, unsigned int value)
145 {
146         struct io_apic __iomem *io_apic = io_apic_base(apic);
147         writel(value, &io_apic->data);
148 }
149
150 /*
151  * Synchronize the IO-APIC and the CPU by doing
152  * a dummy read from the IO-APIC
153  */
154 static inline void io_apic_sync(unsigned int apic)
155 {
156         struct io_apic __iomem *io_apic = io_apic_base(apic);
157         readl(&io_apic->data);
158 }
159
160 #ifdef CONFIG_XEN
161
162 #include <xen/interface/xen.h>
163 #include <xen/interface/physdev.h>
164
165 /* Fake i8259 */
166 #define make_8259A_irq(_irq)     (io_apic_irqs &= ~(1UL<<(_irq)))
167 #define disable_8259A_irq(_irq)  ((void)0)
168 #define i8259A_irq_pending(_irq) (0)
169
170 unsigned long io_apic_irqs;
171
172 static inline unsigned int xen_io_apic_read(unsigned int apic, unsigned int reg)
173 {
174         struct physdev_apic apic_op;
175         int ret;
176
177         apic_op.apic_physbase = mp_ioapics[apic].mpc_apicaddr;
178         apic_op.reg = reg;
179         ret = HYPERVISOR_physdev_op(PHYSDEVOP_apic_read, &apic_op);
180         if (ret)
181                 return ret;
182         return apic_op.value;
183 }
184
185 static inline void xen_io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
186 {
187         struct physdev_apic apic_op;
188
189         apic_op.apic_physbase = mp_ioapics[apic].mpc_apicaddr;
190         apic_op.reg = reg;
191         apic_op.value = value;
192         HYPERVISOR_physdev_op(PHYSDEVOP_apic_write, &apic_op);
193 }
194
195 #define io_apic_read(a,r)    xen_io_apic_read(a,r)
196 #define io_apic_write(a,r,v) xen_io_apic_write(a,r,v)
197
198 #define clear_IO_APIC() ((void)0)
199 #endif
200
201 #ifndef CONFIG_XEN
202 #define __DO_ACTION(R, ACTION, FINAL)                                   \
203                                                                         \
204 {                                                                       \
205         int pin;                                                        \
206         struct irq_pin_list *entry = irq_2_pin + irq;                   \
207                                                                         \
208         BUG_ON(irq >= NR_IRQS);                                         \
209         for (;;) {                                                      \
210                 unsigned int reg;                                       \
211                 pin = entry->pin;                                       \
212                 if (pin == -1)                                          \
213                         break;                                          \
214                 reg = io_apic_read(entry->apic, 0x10 + R + pin*2);      \
215                 reg ACTION;                                             \
216                 io_apic_modify(entry->apic, reg);                       \
217                 if (!entry->next)                                       \
218                         break;                                          \
219                 entry = irq_2_pin + entry->next;                        \
220         }                                                               \
221         FINAL;                                                          \
222 }
223 #endif /* !CONFIG_XEN */
224
225 union entry_union {
226         struct { u32 w1, w2; };
227         struct IO_APIC_route_entry entry;
228 };
229
230 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
231 {
232         union entry_union eu;
233         unsigned long flags;
234         spin_lock_irqsave(&ioapic_lock, flags);
235         eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
236         eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
237         spin_unlock_irqrestore(&ioapic_lock, flags);
238         return eu.entry;
239 }
240
241 /*
242  * When we write a new IO APIC routing entry, we need to write the high
243  * word first! If the mask bit in the low word is clear, we will enable
244  * the interrupt, and we need to make sure the entry is fully populated
245  * before that happens.
246  */
247 static void
248 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
249 {
250         union entry_union eu;
251         eu.entry = e;
252         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
253         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
254 }
255
256 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
257 {
258         unsigned long flags;
259         spin_lock_irqsave(&ioapic_lock, flags);
260         __ioapic_write_entry(apic, pin, e);
261         spin_unlock_irqrestore(&ioapic_lock, flags);
262 }
263
264 /*
265  * When we mask an IO APIC routing entry, we need to write the low
266  * word first, in order to set the mask bit before we change the
267  * high bits!
268  */
269 #ifndef CONFIG_XEN
270 static void ioapic_mask_entry(int apic, int pin)
271 {
272         unsigned long flags;
273         union entry_union eu = { .entry.mask = 1 };
274
275         spin_lock_irqsave(&ioapic_lock, flags);
276         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
277         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
278         spin_unlock_irqrestore(&ioapic_lock, flags);
279 }
280
281 #ifdef CONFIG_SMP
282 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
283 {
284         int apic, pin;
285         struct irq_pin_list *entry = irq_2_pin + irq;
286
287         BUG_ON(irq >= NR_IRQS);
288         for (;;) {
289                 unsigned int reg;
290                 apic = entry->apic;
291                 pin = entry->pin;
292                 if (pin == -1)
293                         break;
294                 io_apic_write(apic, 0x11 + pin*2, dest);
295                 reg = io_apic_read(apic, 0x10 + pin*2);
296                 reg &= ~0x000000ff;
297                 reg |= vector;
298                 io_apic_modify(apic, reg);
299                 if (!entry->next)
300                         break;
301                 entry = irq_2_pin + entry->next;
302         }
303 }
304
305 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
306 {
307         unsigned long flags;
308         unsigned int dest;
309         cpumask_t tmp;
310         int vector;
311
312         cpus_and(tmp, mask, cpu_online_map);
313         if (cpus_empty(tmp))
314                 tmp = TARGET_CPUS;
315
316         cpus_and(mask, tmp, CPU_MASK_ALL);
317
318         vector = assign_irq_vector(irq, mask, &tmp);
319         if (vector < 0)
320                 return;
321
322         dest = cpu_mask_to_apicid(tmp);
323
324         /*
325          * Only the high 8 bits are valid.
326          */
327         dest = SET_APIC_LOGICAL_ID(dest);
328
329         spin_lock_irqsave(&ioapic_lock, flags);
330         __target_IO_APIC_irq(irq, dest, vector);
331         set_native_irq_info(irq, mask);
332         spin_unlock_irqrestore(&ioapic_lock, flags);
333 }
334 #endif
335 #endif /* !CONFIG_XEN */
336
337 /*
338  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
339  * shared ISA-space IRQs, so we have to support them. We are super
340  * fast in the common case, and fast for shared ISA-space IRQs.
341  */
342 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
343 {
344         static int first_free_entry = NR_IRQS;
345         struct irq_pin_list *entry = irq_2_pin + irq;
346
347         BUG_ON(irq >= NR_IRQS);
348         while (entry->next)
349                 entry = irq_2_pin + entry->next;
350
351         if (entry->pin != -1) {
352                 entry->next = first_free_entry;
353                 entry = irq_2_pin + entry->next;
354                 if (++first_free_entry >= PIN_MAP_SIZE)
355                         panic("io_apic.c: ran out of irq_2_pin entries!");
356         }
357         entry->apic = apic;
358         entry->pin = pin;
359 }
360
361 #ifndef CONFIG_XEN
362
363 #define DO_ACTION(name,R,ACTION, FINAL)                                 \
364                                                                         \
365         static void name##_IO_APIC_irq (unsigned int irq)               \
366         __DO_ACTION(R, ACTION, FINAL)
367
368 DO_ACTION( __mask,             0, |= 0x00010000, io_apic_sync(entry->apic) )
369                                                 /* mask = 1 */
370 DO_ACTION( __unmask,           0, &= 0xfffeffff, )
371                                                 /* mask = 0 */
372
373 static void mask_IO_APIC_irq (unsigned int irq)
374 {
375         unsigned long flags;
376
377         spin_lock_irqsave(&ioapic_lock, flags);
378         __mask_IO_APIC_irq(irq);
379         spin_unlock_irqrestore(&ioapic_lock, flags);
380 }
381
382 static void unmask_IO_APIC_irq (unsigned int irq)
383 {
384         unsigned long flags;
385
386         spin_lock_irqsave(&ioapic_lock, flags);
387         __unmask_IO_APIC_irq(irq);
388         spin_unlock_irqrestore(&ioapic_lock, flags);
389 }
390
391 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
392 {
393         struct IO_APIC_route_entry entry;
394
395         /* Check delivery_mode to be sure we're not clearing an SMI pin */
396         entry = ioapic_read_entry(apic, pin);
397         if (entry.delivery_mode == dest_SMI)
398                 return;
399         /*
400          * Disable it in the IO-APIC irq-routing table:
401          */
402         ioapic_mask_entry(apic, pin);
403 }
404
405 static void clear_IO_APIC (void)
406 {
407         int apic, pin;
408
409         for (apic = 0; apic < nr_ioapics; apic++)
410                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
411                         clear_IO_APIC_pin(apic, pin);
412 }
413
414 #endif /* !CONFIG_XEN */
415 int skip_ioapic_setup;
416 int ioapic_force;
417
418 /* dummy parsing: see setup.c */
419
420 static int __init disable_ioapic_setup(char *str)
421 {
422         skip_ioapic_setup = 1;
423         return 0;
424 }
425 early_param("noapic", disable_ioapic_setup);
426
427 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
428 static int __init disable_timer_pin_setup(char *arg)
429 {
430         disable_timer_pin_1 = 1;
431         return 1;
432 }
433 __setup("disable_timer_pin_1", disable_timer_pin_setup);
434
435 static int __init setup_disable_8254_timer(char *s)
436 {
437         timer_over_8254 = -1;
438         return 1;
439 }
440 static int __init setup_enable_8254_timer(char *s)
441 {
442         timer_over_8254 = 2;
443         return 1;
444 }
445
446 __setup("disable_8254_timer", setup_disable_8254_timer);
447 __setup("enable_8254_timer", setup_enable_8254_timer);
448
449
450 /*
451  * Find the IRQ entry number of a certain pin.
452  */
453 static int find_irq_entry(int apic, int pin, int type)
454 {
455         int i;
456
457         for (i = 0; i < mp_irq_entries; i++)
458                 if (mp_irqs[i].mpc_irqtype == type &&
459                     (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
460                      mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
461                     mp_irqs[i].mpc_dstirq == pin)
462                         return i;
463
464         return -1;
465 }
466
467 #ifndef CONFIG_XEN
468 /*
469  * Find the pin to which IRQ[irq] (ISA) is connected
470  */
471 static int __init find_isa_irq_pin(int irq, int type)
472 {
473         int i;
474
475         for (i = 0; i < mp_irq_entries; i++) {
476                 int lbus = mp_irqs[i].mpc_srcbus;
477
478                 if (test_bit(lbus, mp_bus_not_pci) &&
479                     (mp_irqs[i].mpc_irqtype == type) &&
480                     (mp_irqs[i].mpc_srcbusirq == irq))
481
482                         return mp_irqs[i].mpc_dstirq;
483         }
484         return -1;
485 }
486
487 static int __init find_isa_irq_apic(int irq, int type)
488 {
489         int i;
490
491         for (i = 0; i < mp_irq_entries; i++) {
492                 int lbus = mp_irqs[i].mpc_srcbus;
493
494                 if (test_bit(lbus, mp_bus_not_pci) &&
495                     (mp_irqs[i].mpc_irqtype == type) &&
496                     (mp_irqs[i].mpc_srcbusirq == irq))
497                         break;
498         }
499         if (i < mp_irq_entries) {
500                 int apic;
501                 for(apic = 0; apic < nr_ioapics; apic++) {
502                         if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
503                                 return apic;
504                 }
505         }
506
507         return -1;
508 }
509 #endif
510
511 /*
512  * Find a specific PCI IRQ entry.
513  * Not an __init, possibly needed by modules
514  */
515 static int pin_2_irq(int idx, int apic, int pin);
516
517 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
518 {
519         int apic, i, best_guess = -1;
520
521         apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
522                 bus, slot, pin);
523         if (mp_bus_id_to_pci_bus[bus] == -1) {
524                 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
525                 return -1;
526         }
527         for (i = 0; i < mp_irq_entries; i++) {
528                 int lbus = mp_irqs[i].mpc_srcbus;
529
530                 for (apic = 0; apic < nr_ioapics; apic++)
531                         if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
532                             mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
533                                 break;
534
535                 if (!test_bit(lbus, mp_bus_not_pci) &&
536                     !mp_irqs[i].mpc_irqtype &&
537                     (bus == lbus) &&
538                     (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
539                         int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
540
541                         if (!(apic || IO_APIC_IRQ(irq)))
542                                 continue;
543
544                         if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
545                                 return irq;
546                         /*
547                          * Use the first all-but-pin matching entry as a
548                          * best-guess fuzzy result for broken mptables.
549                          */
550                         if (best_guess < 0)
551                                 best_guess = irq;
552                 }
553         }
554         BUG_ON(best_guess >= NR_IRQS);
555         return best_guess;
556 }
557
558 /* ISA interrupts are always polarity zero edge triggered,
559  * when listed as conforming in the MP table. */
560
561 #define default_ISA_trigger(idx)        (0)
562 #define default_ISA_polarity(idx)       (0)
563
564 /* PCI interrupts are always polarity one level triggered,
565  * when listed as conforming in the MP table. */
566
567 #define default_PCI_trigger(idx)        (1)
568 #define default_PCI_polarity(idx)       (1)
569
570 static int __init MPBIOS_polarity(int idx)
571 {
572         int bus = mp_irqs[idx].mpc_srcbus;
573         int polarity;
574
575         /*
576          * Determine IRQ line polarity (high active or low active):
577          */
578         switch (mp_irqs[idx].mpc_irqflag & 3)
579         {
580                 case 0: /* conforms, ie. bus-type dependent polarity */
581                         if (test_bit(bus, mp_bus_not_pci))
582                                 polarity = default_ISA_polarity(idx);
583                         else
584                                 polarity = default_PCI_polarity(idx);
585                         break;
586                 case 1: /* high active */
587                 {
588                         polarity = 0;
589                         break;
590                 }
591                 case 2: /* reserved */
592                 {
593                         printk(KERN_WARNING "broken BIOS!!\n");
594                         polarity = 1;
595                         break;
596                 }
597                 case 3: /* low active */
598                 {
599                         polarity = 1;
600                         break;
601                 }
602                 default: /* invalid */
603                 {
604                         printk(KERN_WARNING "broken BIOS!!\n");
605                         polarity = 1;
606                         break;
607                 }
608         }
609         return polarity;
610 }
611
612 static int MPBIOS_trigger(int idx)
613 {
614         int bus = mp_irqs[idx].mpc_srcbus;
615         int trigger;
616
617         /*
618          * Determine IRQ trigger mode (edge or level sensitive):
619          */
620         switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
621         {
622                 case 0: /* conforms, ie. bus-type dependent */
623                         if (test_bit(bus, mp_bus_not_pci))
624                                 trigger = default_ISA_trigger(idx);
625                         else
626                                 trigger = default_PCI_trigger(idx);
627                         break;
628                 case 1: /* edge */
629                 {
630                         trigger = 0;
631                         break;
632                 }
633                 case 2: /* reserved */
634                 {
635                         printk(KERN_WARNING "broken BIOS!!\n");
636                         trigger = 1;
637                         break;
638                 }
639                 case 3: /* level */
640                 {
641                         trigger = 1;
642                         break;
643                 }
644                 default: /* invalid */
645                 {
646                         printk(KERN_WARNING "broken BIOS!!\n");
647                         trigger = 0;
648                         break;
649                 }
650         }
651         return trigger;
652 }
653
654 static inline int irq_polarity(int idx)
655 {
656         return MPBIOS_polarity(idx);
657 }
658
659 static inline int irq_trigger(int idx)
660 {
661         return MPBIOS_trigger(idx);
662 }
663
664 static int pin_2_irq(int idx, int apic, int pin)
665 {
666         int irq, i;
667         int bus = mp_irqs[idx].mpc_srcbus;
668
669         /*
670          * Debugging check, we are in big trouble if this message pops up!
671          */
672         if (mp_irqs[idx].mpc_dstirq != pin)
673                 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
674
675         if (test_bit(bus, mp_bus_not_pci)) {
676                 irq = mp_irqs[idx].mpc_srcbusirq;
677         } else {
678                 /*
679                  * PCI IRQs are mapped in order
680                  */
681                 i = irq = 0;
682                 while (i < apic)
683                         irq += nr_ioapic_registers[i++];
684                 irq += pin;
685         }
686         BUG_ON(irq >= NR_IRQS);
687         return irq;
688 }
689
690 static inline int IO_APIC_irq_trigger(int irq)
691 {
692         int apic, idx, pin;
693
694         for (apic = 0; apic < nr_ioapics; apic++) {
695                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
696                         idx = find_irq_entry(apic,pin,mp_INT);
697                         if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
698                                 return irq_trigger(idx);
699                 }
700         }
701         /*
702          * nonexistent IRQs are edge default
703          */
704         return 0;
705 }
706
707 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
708 static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly;
709
710 static cpumask_t irq_domain[NR_IRQ_VECTORS] __read_mostly = {
711         [0] = CPU_MASK_ALL,
712         [1] = CPU_MASK_ALL,
713         [2] = CPU_MASK_ALL,
714         [3] = CPU_MASK_ALL,
715         [4] = CPU_MASK_ALL,
716         [5] = CPU_MASK_ALL,
717         [6] = CPU_MASK_ALL,
718         [7] = CPU_MASK_ALL,
719         [8] = CPU_MASK_ALL,
720         [9] = CPU_MASK_ALL,
721         [10] = CPU_MASK_ALL,
722         [11] = CPU_MASK_ALL,
723         [12] = CPU_MASK_ALL,
724         [13] = CPU_MASK_ALL,
725         [14] = CPU_MASK_ALL,
726         [15] = CPU_MASK_ALL,
727 };
728
729
730 static int __assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result)
731 {
732         struct physdev_irq irq_op;
733         int vector;
734
735         BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
736
737         if (irq_vector[irq] > 0) {
738                 cpus_and(*result, irq_domain[irq], mask);
739                 return irq_vector[irq];
740         }
741         irq_op.irq = irq;
742         if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op))
743                 return -ENOSPC;
744
745         vector = irq_op.vector;
746         per_cpu(vector_irq,0)[vector] = irq;
747         irq_vector[irq] = vector;
748         cpus_and(*result, irq_domain[irq], mask);
749
750         return vector;
751 }
752
753 static int assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result)
754 {
755         int vector;
756         unsigned long flags;
757
758         spin_lock_irqsave(&vector_lock, flags);
759         vector = __assign_irq_vector(irq, mask, result);
760         spin_unlock_irqrestore(&vector_lock, flags);
761         return vector;
762 }
763
764 static void __clear_irq_vector(int irq)
765 {
766         cpumask_t mask;
767         int cpu, vector;
768
769         BUG_ON(!irq_vector[irq]);
770
771         vector = irq_vector[irq];
772         cpus_and(mask, irq_domain[irq], cpu_online_map);
773         for_each_cpu_mask(cpu, mask)
774                 per_cpu(vector_irq, cpu)[vector] = -1;
775
776         irq_vector[irq] = 0;
777         irq_domain[irq] = CPU_MASK_NONE;
778 }
779
780 void __setup_vector_irq(int cpu)
781 {
782         /* Initialize vector_irq on a new cpu */
783         /* This function must be called with vector_lock held */
784         int irq, vector;
785
786         /* Mark the inuse vectors */
787         for (irq = 0; irq < NR_IRQ_VECTORS; ++irq) {
788                 if (!cpu_isset(cpu, irq_domain[irq]))
789                         continue;
790                 vector = irq_vector[irq];
791                 per_cpu(vector_irq, cpu)[vector] = irq;
792         }
793         /* Mark the free vectors */
794         for (vector = 0; vector < NR_VECTORS; ++vector) {
795                 irq = per_cpu(vector_irq, cpu)[vector];
796                 if (irq < 0)
797                         continue;
798                 if (!cpu_isset(cpu, irq_domain[irq]))
799                         per_cpu(vector_irq, cpu)[vector] = -1;
800         }
801 }
802
803
804 extern void (*interrupt[NR_IRQS])(void);
805
806 #ifndef CONFIG_XEN
807 static struct irq_chip ioapic_chip;
808
809 #define IOAPIC_AUTO     -1
810 #define IOAPIC_EDGE     0
811 #define IOAPIC_LEVEL    1
812
813 static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
814 {
815         if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
816                         trigger == IOAPIC_LEVEL)
817                 set_irq_chip_and_handler_name(irq, &ioapic_chip,
818                                               handle_fasteoi_irq, "fasteoi");
819         else {
820                 irq_desc[irq].status |= IRQ_DELAYED_DISABLE;
821                 set_irq_chip_and_handler_name(irq, &ioapic_chip,
822                                               handle_edge_irq, "edge");
823         }
824 }
825 #else
826 #define ioapic_register_intr(_irq,_vector,_trigger) ((void)0)
827 #endif /* !CONFIG_XEN */
828
829 static void __init setup_IO_APIC_irq(int apic, int pin, int idx, int irq)
830 {
831         struct IO_APIC_route_entry entry;
832         int vector;
833         unsigned long flags;
834
835
836         /*
837          * add it to the IO-APIC irq-routing table:
838          */
839         memset(&entry,0,sizeof(entry));
840
841         entry.delivery_mode = INT_DELIVERY_MODE;
842         entry.dest_mode = INT_DEST_MODE;
843         entry.mask = 0;                         /* enable IRQ */
844         entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
845
846         entry.trigger = irq_trigger(idx);
847         entry.polarity = irq_polarity(idx);
848
849         if (irq_trigger(idx)) {
850                 entry.trigger = 1;
851                 entry.mask = 1;
852                 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
853         }
854
855         if (!apic && !IO_APIC_IRQ(irq))
856                 return;
857
858         if (IO_APIC_IRQ(irq)) {
859                 cpumask_t mask;
860                 vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
861                 if (vector < 0)
862                         return;
863
864                 entry.dest = cpu_mask_to_apicid(mask);
865                 entry.vector = vector;
866
867                 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
868                 if (!apic && (irq < 16))
869                         disable_8259A_irq(irq);
870         }
871
872         ioapic_write_entry(apic, pin, entry);
873
874         spin_lock_irqsave(&ioapic_lock, flags);
875         set_native_irq_info(irq, TARGET_CPUS);
876         spin_unlock_irqrestore(&ioapic_lock, flags);
877
878 }
879
880 static void __init setup_IO_APIC_irqs(void)
881 {
882         int apic, pin, idx, irq, first_notcon = 1;
883
884         apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
885
886         for (apic = 0; apic < nr_ioapics; apic++) {
887         for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
888
889                 idx = find_irq_entry(apic,pin,mp_INT);
890                 if (idx == -1) {
891                         if (first_notcon) {
892                                 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
893                                 first_notcon = 0;
894                         } else
895                                 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
896                         continue;
897                 }
898
899                 irq = pin_2_irq(idx, apic, pin);
900                 add_pin_to_irq(irq, apic, pin);
901
902                 setup_IO_APIC_irq(apic, pin, idx, irq);
903
904         }
905         }
906
907         if (!first_notcon)
908                 apic_printk(APIC_VERBOSE," not connected.\n");
909 }
910
911 #ifndef CONFIG_XEN
912 /*
913  * Set up the 8259A-master output pin as broadcast to all
914  * CPUs.
915  */
916 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
917 {
918         struct IO_APIC_route_entry entry;
919         unsigned long flags;
920
921         memset(&entry,0,sizeof(entry));
922
923         disable_8259A_irq(0);
924
925         /* mask LVT0 */
926         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
927
928         /*
929          * We use logical delivery to get the timer IRQ
930          * to the first CPU.
931          */
932         entry.dest_mode = INT_DEST_MODE;
933         entry.mask = 0;                                 /* unmask IRQ now */
934         entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
935         entry.delivery_mode = INT_DELIVERY_MODE;
936         entry.polarity = 0;
937         entry.trigger = 0;
938         entry.vector = vector;
939
940         /*
941          * The timer IRQ doesn't have to know that behind the
942          * scene we have a 8259A-master in AEOI mode ...
943          */
944         set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
945
946         /*
947          * Add it to the IO-APIC irq-routing table:
948          */
949         spin_lock_irqsave(&ioapic_lock, flags);
950         io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
951         io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
952         spin_unlock_irqrestore(&ioapic_lock, flags);
953
954         enable_8259A_irq(0);
955 }
956
957 void __init UNEXPECTED_IO_APIC(void)
958 {
959 }
960
961 void __apicdebuginit print_IO_APIC(void)
962 {
963         int apic, i;
964         union IO_APIC_reg_00 reg_00;
965         union IO_APIC_reg_01 reg_01;
966         union IO_APIC_reg_02 reg_02;
967         unsigned long flags;
968
969         if (apic_verbosity == APIC_QUIET)
970                 return;
971
972         printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
973         for (i = 0; i < nr_ioapics; i++)
974                 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
975                        mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
976
977         /*
978          * We are a bit conservative about what we expect.  We have to
979          * know about every hardware change ASAP.
980          */
981         printk(KERN_INFO "testing the IO APIC.......................\n");
982
983         for (apic = 0; apic < nr_ioapics; apic++) {
984
985         spin_lock_irqsave(&ioapic_lock, flags);
986         reg_00.raw = io_apic_read(apic, 0);
987         reg_01.raw = io_apic_read(apic, 1);
988         if (reg_01.bits.version >= 0x10)
989                 reg_02.raw = io_apic_read(apic, 2);
990         spin_unlock_irqrestore(&ioapic_lock, flags);
991
992         printk("\n");
993         printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
994         printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
995         printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
996         if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
997                 UNEXPECTED_IO_APIC();
998
999         printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1000         printk(KERN_DEBUG ".......     : max redirection entries: %04X\n", reg_01.bits.entries);
1001         if (    (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
1002                 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
1003                 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
1004                 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1005                 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1006                 (reg_01.bits.entries != 0x2E) &&
1007                 (reg_01.bits.entries != 0x3F) &&
1008                 (reg_01.bits.entries != 0x03) 
1009         )
1010                 UNEXPECTED_IO_APIC();
1011
1012         printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
1013         printk(KERN_DEBUG ".......     : IO APIC version: %04X\n", reg_01.bits.version);
1014         if (    (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1015                 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
1016                 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1017                 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1018                 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1019                 (reg_01.bits.version != 0x20)    /* Intel P64H (82806 AA) */
1020         )
1021                 UNEXPECTED_IO_APIC();
1022         if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1023                 UNEXPECTED_IO_APIC();
1024
1025         if (reg_01.bits.version >= 0x10) {
1026                 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1027                 printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
1028                 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1029                         UNEXPECTED_IO_APIC();
1030         }
1031
1032         printk(KERN_DEBUG ".... IRQ redirection table:\n");
1033
1034         printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1035                           " Stat Dmod Deli Vect:   \n");
1036
1037         for (i = 0; i <= reg_01.bits.entries; i++) {
1038                 struct IO_APIC_route_entry entry;
1039
1040                 entry = ioapic_read_entry(apic, i);
1041
1042                 printk(KERN_DEBUG " %02x %03X ",
1043                         i,
1044                         entry.dest
1045                 );
1046
1047                 printk("%1d    %1d    %1d   %1d   %1d    %1d    %1d    %02X\n",
1048                         entry.mask,
1049                         entry.trigger,
1050                         entry.irr,
1051                         entry.polarity,
1052                         entry.delivery_status,
1053                         entry.dest_mode,
1054                         entry.delivery_mode,
1055                         entry.vector
1056                 );
1057         }
1058         }
1059         printk(KERN_DEBUG "IRQ to pin mappings:\n");
1060         for (i = 0; i < NR_IRQS; i++) {
1061                 struct irq_pin_list *entry = irq_2_pin + i;
1062                 if (entry->pin < 0)
1063                         continue;
1064                 printk(KERN_DEBUG "IRQ%d ", i);
1065                 for (;;) {
1066                         printk("-> %d:%d", entry->apic, entry->pin);
1067                         if (!entry->next)
1068                                 break;
1069                         entry = irq_2_pin + entry->next;
1070                 }
1071                 printk("\n");
1072         }
1073
1074         printk(KERN_INFO ".................................... done.\n");
1075
1076         return;
1077 }
1078
1079 #if 0
1080
1081 static __apicdebuginit void print_APIC_bitfield (int base)
1082 {
1083         unsigned int v;
1084         int i, j;
1085
1086         if (apic_verbosity == APIC_QUIET)
1087                 return;
1088
1089         printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1090         for (i = 0; i < 8; i++) {
1091                 v = apic_read(base + i*0x10);
1092                 for (j = 0; j < 32; j++) {
1093                         if (v & (1<<j))
1094                                 printk("1");
1095                         else
1096                                 printk("0");
1097                 }
1098                 printk("\n");
1099         }
1100 }
1101
1102 void __apicdebuginit print_local_APIC(void * dummy)
1103 {
1104         unsigned int v, ver, maxlvt;
1105
1106         if (apic_verbosity == APIC_QUIET)
1107                 return;
1108
1109         printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1110                 smp_processor_id(), hard_smp_processor_id());
1111         v = apic_read(APIC_ID);
1112         printk(KERN_INFO "... APIC ID:      %08x (%01x)\n", v, GET_APIC_ID(v));
1113         v = apic_read(APIC_LVR);
1114         printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1115         ver = GET_APIC_VERSION(v);
1116         maxlvt = get_maxlvt();
1117
1118         v = apic_read(APIC_TASKPRI);
1119         printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1120
1121         v = apic_read(APIC_ARBPRI);
1122         printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1123                 v & APIC_ARBPRI_MASK);
1124         v = apic_read(APIC_PROCPRI);
1125         printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1126
1127         v = apic_read(APIC_EOI);
1128         printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1129         v = apic_read(APIC_RRR);
1130         printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1131         v = apic_read(APIC_LDR);
1132         printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1133         v = apic_read(APIC_DFR);
1134         printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1135         v = apic_read(APIC_SPIV);
1136         printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1137
1138         printk(KERN_DEBUG "... APIC ISR field:\n");
1139         print_APIC_bitfield(APIC_ISR);
1140         printk(KERN_DEBUG "... APIC TMR field:\n");
1141         print_APIC_bitfield(APIC_TMR);
1142         printk(KERN_DEBUG "... APIC IRR field:\n");
1143         print_APIC_bitfield(APIC_IRR);
1144
1145         v = apic_read(APIC_ESR);
1146         printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1147
1148         v = apic_read(APIC_ICR);
1149         printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1150         v = apic_read(APIC_ICR2);
1151         printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1152
1153         v = apic_read(APIC_LVTT);
1154         printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1155
1156         if (maxlvt > 3) {                       /* PC is LVT#4. */
1157                 v = apic_read(APIC_LVTPC);
1158                 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1159         }
1160         v = apic_read(APIC_LVT0);
1161         printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1162         v = apic_read(APIC_LVT1);
1163         printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1164
1165         if (maxlvt > 2) {                       /* ERR is LVT#3. */
1166                 v = apic_read(APIC_LVTERR);
1167                 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1168         }
1169
1170         v = apic_read(APIC_TMICT);
1171         printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1172         v = apic_read(APIC_TMCCT);
1173         printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1174         v = apic_read(APIC_TDCR);
1175         printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1176         printk("\n");
1177 }
1178
1179 void print_all_local_APICs (void)
1180 {
1181         on_each_cpu(print_local_APIC, NULL, 1, 1);
1182 }
1183
1184 void __apicdebuginit print_PIC(void)
1185 {
1186         unsigned int v;
1187         unsigned long flags;
1188
1189         if (apic_verbosity == APIC_QUIET)
1190                 return;
1191
1192         printk(KERN_DEBUG "\nprinting PIC contents\n");
1193
1194         spin_lock_irqsave(&i8259A_lock, flags);
1195
1196         v = inb(0xa1) << 8 | inb(0x21);
1197         printk(KERN_DEBUG "... PIC  IMR: %04x\n", v);
1198
1199         v = inb(0xa0) << 8 | inb(0x20);
1200         printk(KERN_DEBUG "... PIC  IRR: %04x\n", v);
1201
1202         outb(0x0b,0xa0);
1203         outb(0x0b,0x20);
1204         v = inb(0xa0) << 8 | inb(0x20);
1205         outb(0x0a,0xa0);
1206         outb(0x0a,0x20);
1207
1208         spin_unlock_irqrestore(&i8259A_lock, flags);
1209
1210         printk(KERN_DEBUG "... PIC  ISR: %04x\n", v);
1211
1212         v = inb(0x4d1) << 8 | inb(0x4d0);
1213         printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1214 }
1215
1216 #endif  /*  0  */
1217
1218 #else
1219 void __init print_IO_APIC(void) { }
1220 #endif /* !CONFIG_XEN */
1221
1222 static void __init enable_IO_APIC(void)
1223 {
1224         union IO_APIC_reg_01 reg_01;
1225 #ifndef CONFIG_XEN
1226         int i8259_apic, i8259_pin;
1227 #endif
1228         int i, apic;
1229         unsigned long flags;
1230
1231         for (i = 0; i < PIN_MAP_SIZE; i++) {
1232                 irq_2_pin[i].pin = -1;
1233                 irq_2_pin[i].next = 0;
1234         }
1235
1236         /*
1237          * The number of IO-APIC IRQ registers (== #pins):
1238          */
1239         for (apic = 0; apic < nr_ioapics; apic++) {
1240                 spin_lock_irqsave(&ioapic_lock, flags);
1241                 reg_01.raw = io_apic_read(apic, 1);
1242                 spin_unlock_irqrestore(&ioapic_lock, flags);
1243                 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1244         }
1245 #ifndef CONFIG_XEN
1246         for(apic = 0; apic < nr_ioapics; apic++) {
1247                 int pin;
1248                 /* See if any of the pins is in ExtINT mode */
1249                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1250                         struct IO_APIC_route_entry entry;
1251                         entry = ioapic_read_entry(apic, pin);
1252
1253                         /* If the interrupt line is enabled and in ExtInt mode
1254                          * I have found the pin where the i8259 is connected.
1255                          */
1256                         if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1257                                 ioapic_i8259.apic = apic;
1258                                 ioapic_i8259.pin  = pin;
1259                                 goto found_i8259;
1260                         }
1261                 }
1262         }
1263  found_i8259:
1264         /* Look to see what if the MP table has reported the ExtINT */
1265         i8259_pin  = find_isa_irq_pin(0, mp_ExtINT);
1266         i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1267         /* Trust the MP table if nothing is setup in the hardware */
1268         if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1269                 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1270                 ioapic_i8259.pin  = i8259_pin;
1271                 ioapic_i8259.apic = i8259_apic;
1272         }
1273         /* Complain if the MP table and the hardware disagree */
1274         if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1275                 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1276         {
1277                 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1278         }
1279 #endif
1280
1281         /*
1282          * Do not trust the IO-APIC being empty at bootup
1283          */
1284         clear_IO_APIC();
1285 }
1286
1287 /*
1288  * Not an __init, needed by the reboot code
1289  */
1290 void disable_IO_APIC(void)
1291 {
1292         /*
1293          * Clear the IO-APIC before rebooting:
1294          */
1295         clear_IO_APIC();
1296
1297 #ifndef CONFIG_XEN
1298         /*
1299          * If the i8259 is routed through an IOAPIC
1300          * Put that IOAPIC in virtual wire mode
1301          * so legacy interrupts can be delivered.
1302          */
1303         if (ioapic_i8259.pin != -1) {
1304                 struct IO_APIC_route_entry entry;
1305
1306                 memset(&entry, 0, sizeof(entry));
1307                 entry.mask            = 0; /* Enabled */
1308                 entry.trigger         = 0; /* Edge */
1309                 entry.irr             = 0;
1310                 entry.polarity        = 0; /* High */
1311                 entry.delivery_status = 0;
1312                 entry.dest_mode       = 0; /* Physical */
1313                 entry.delivery_mode   = dest_ExtINT; /* ExtInt */
1314                 entry.vector          = 0;
1315                 entry.dest            =
1316                                         GET_APIC_ID(apic_read(APIC_ID));
1317
1318                 /*
1319                  * Add it to the IO-APIC irq-routing table:
1320                  */
1321                 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1322         }
1323
1324         disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1325 #endif
1326 }
1327
1328 /*
1329  * There is a nasty bug in some older SMP boards, their mptable lies
1330  * about the timer IRQ. We do the following to work around the situation:
1331  *
1332  *      - timer IRQ defaults to IO-APIC IRQ
1333  *      - if this function detects that timer IRQs are defunct, then we fall
1334  *        back to ISA timer IRQs
1335  */
1336 #ifndef CONFIG_XEN
1337 static int __init timer_irq_works(void)
1338 {
1339         unsigned long t1 = jiffies;
1340
1341         local_irq_enable();
1342         /* Let ten ticks pass... */
1343         mdelay((10 * 1000) / HZ);
1344
1345         /*
1346          * Expect a few ticks at least, to be sure some possible
1347          * glue logic does not lock up after one or two first
1348          * ticks in a non-ExtINT mode.  Also the local APIC
1349          * might have cached one ExtINT interrupt.  Finally, at
1350          * least one tick may be lost due to delays.
1351          */
1352
1353         /* jiffies wrap? */
1354         if (jiffies - t1 > 4)
1355                 return 1;
1356         return 0;
1357 }
1358
1359 /*
1360  * In the SMP+IOAPIC case it might happen that there are an unspecified
1361  * number of pending IRQ events unhandled. These cases are very rare,
1362  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1363  * better to do it this way as thus we do not have to be aware of
1364  * 'pending' interrupts in the IRQ path, except at this point.
1365  */
1366 /*
1367  * Edge triggered needs to resend any interrupt
1368  * that was delayed but this is now handled in the device
1369  * independent code.
1370  */
1371
1372 /*
1373  * Starting up a edge-triggered IO-APIC interrupt is
1374  * nasty - we need to make sure that we get the edge.
1375  * If it is already asserted for some reason, we need
1376  * return 1 to indicate that is was pending.
1377  *
1378  * This is not complete - we should be able to fake
1379  * an edge even if it isn't on the 8259A...
1380  */
1381
1382 static unsigned int startup_ioapic_irq(unsigned int irq)
1383 {
1384         int was_pending = 0;
1385         unsigned long flags;
1386
1387         spin_lock_irqsave(&ioapic_lock, flags);
1388         if (irq < 16) {
1389                 disable_8259A_irq(irq);
1390                 if (i8259A_irq_pending(irq))
1391                         was_pending = 1;
1392         }
1393         __unmask_IO_APIC_irq(irq);
1394         spin_unlock_irqrestore(&ioapic_lock, flags);
1395
1396         return was_pending;
1397 }
1398
1399 static int ioapic_retrigger_irq(unsigned int irq)
1400 {
1401         cpumask_t mask;
1402         unsigned vector;
1403         unsigned long flags;
1404
1405         spin_lock_irqsave(&vector_lock, flags);
1406         vector = irq_vector[irq];
1407         cpus_clear(mask);
1408         cpu_set(first_cpu(irq_domain[irq]), mask);
1409
1410         send_IPI_mask(mask, vector);
1411         spin_unlock_irqrestore(&vector_lock, flags);
1412
1413         return 1;
1414 }
1415
1416 /*
1417  * Level and edge triggered IO-APIC interrupts need different handling,
1418  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1419  * handled with the level-triggered descriptor, but that one has slightly
1420  * more overhead. Level-triggered interrupts cannot be handled with the
1421  * edge-triggered handler, without risking IRQ storms and other ugly
1422  * races.
1423  */
1424
1425 static void ack_apic_edge(unsigned int irq)
1426 {
1427         move_native_irq(irq);
1428         ack_APIC_irq();
1429 }
1430
1431 static void ack_apic_level(unsigned int irq)
1432 {
1433         int do_unmask_irq = 0;
1434
1435 #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
1436         /* If we are moving the irq we need to mask it */
1437         if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1438                 do_unmask_irq = 1;
1439                 mask_IO_APIC_irq(irq);
1440         }
1441 #endif
1442
1443         /*
1444          * We must acknowledge the irq before we move it or the acknowledge will
1445          * not propogate properly.
1446          */
1447         ack_APIC_irq();
1448
1449         /* Now we can move and renable the irq */
1450         move_masked_irq(irq);
1451         if (unlikely(do_unmask_irq))
1452                 unmask_IO_APIC_irq(irq);
1453 }
1454
1455 static struct irq_chip ioapic_chip __read_mostly = {
1456         .name           = "IO-APIC",
1457         .startup        = startup_ioapic_irq,
1458         .mask           = mask_IO_APIC_irq,
1459         .unmask         = unmask_IO_APIC_irq,
1460         .ack            = ack_apic_edge,
1461         .eoi            = ack_apic_level,
1462 #ifdef CONFIG_SMP
1463         .set_affinity   = set_ioapic_affinity_irq,
1464 #endif
1465         .retrigger      = ioapic_retrigger_irq,
1466 };
1467 #endif /* !CONFIG_XEN */
1468
1469 static inline void init_IO_APIC_traps(void)
1470 {
1471         int irq;
1472
1473         /*
1474          * NOTE! The local APIC isn't very good at handling
1475          * multiple interrupts at the same interrupt level.
1476          * As the interrupt level is determined by taking the
1477          * vector number and shifting that right by 4, we
1478          * want to spread these out a bit so that they don't
1479          * all fall in the same interrupt level.
1480          *
1481          * Also, we've got to be careful not to trash gate
1482          * 0x80, because int 0x80 is hm, kind of importantish. ;)
1483          */
1484         for (irq = 0; irq < NR_IRQS ; irq++) {
1485                 int tmp = irq;
1486                 if (IO_APIC_IRQ(tmp) && !irq_vector[tmp]) {
1487                         /*
1488                          * Hmm.. We don't have an entry for this,
1489                          * so default to an old-fashioned 8259
1490                          * interrupt if we can..
1491                          */
1492                         if (irq < 16)
1493                                 make_8259A_irq(irq);
1494 #ifndef CONFIG_XEN
1495                         else
1496                                 /* Strange. Oh, well.. */
1497                                 irq_desc[irq].chip = &no_irq_chip;
1498 #endif
1499                 }
1500         }
1501 }
1502
1503 #ifndef CONFIG_XEN
1504 static void enable_lapic_irq (unsigned int irq)
1505 {
1506         unsigned long v;
1507
1508         v = apic_read(APIC_LVT0);
1509         apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1510 }
1511
1512 static void disable_lapic_irq (unsigned int irq)
1513 {
1514         unsigned long v;
1515
1516         v = apic_read(APIC_LVT0);
1517         apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1518 }
1519
1520 static void ack_lapic_irq (unsigned int irq)
1521 {
1522         ack_APIC_irq();
1523 }
1524
1525 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1526
1527 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1528         .typename = "local-APIC-edge",
1529         .startup = NULL, /* startup_irq() not used for IRQ0 */
1530         .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1531         .enable = enable_lapic_irq,
1532         .disable = disable_lapic_irq,
1533         .ack = ack_lapic_irq,
1534         .end = end_lapic_irq,
1535 };
1536
1537 static void setup_nmi (void)
1538 {
1539         /*
1540          * Dirty trick to enable the NMI watchdog ...
1541          * We put the 8259A master into AEOI mode and
1542          * unmask on all local APICs LVT0 as NMI.
1543          *
1544          * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1545          * is from Maciej W. Rozycki - so we do not have to EOI from
1546          * the NMI handler or the timer interrupt.
1547          */ 
1548         printk(KERN_INFO "activating NMI Watchdog ...");
1549
1550         enable_NMI_through_LVT0(NULL);
1551
1552         printk(" done.\n");
1553 }
1554
1555 /*
1556  * This looks a bit hackish but it's about the only one way of sending
1557  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
1558  * not support the ExtINT mode, unfortunately.  We need to send these
1559  * cycles as some i82489DX-based boards have glue logic that keeps the
1560  * 8259A interrupt line asserted until INTA.  --macro
1561  */
1562 static inline void unlock_ExtINT_logic(void)
1563 {
1564         int apic, pin, i;
1565         struct IO_APIC_route_entry entry0, entry1;
1566         unsigned char save_control, save_freq_select;
1567         unsigned long flags;
1568
1569         pin  = find_isa_irq_pin(8, mp_INT);
1570         apic = find_isa_irq_apic(8, mp_INT);
1571         if (pin == -1)
1572                 return;
1573
1574         spin_lock_irqsave(&ioapic_lock, flags);
1575         *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1576         *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1577         spin_unlock_irqrestore(&ioapic_lock, flags);
1578         clear_IO_APIC_pin(apic, pin);
1579
1580         memset(&entry1, 0, sizeof(entry1));
1581
1582         entry1.dest_mode = 0;                   /* physical delivery */
1583         entry1.mask = 0;                        /* unmask IRQ now */
1584         entry1.dest = hard_smp_processor_id();
1585         entry1.delivery_mode = dest_ExtINT;
1586         entry1.polarity = entry0.polarity;
1587         entry1.trigger = 0;
1588         entry1.vector = 0;
1589
1590         spin_lock_irqsave(&ioapic_lock, flags);
1591         io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1592         io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1593         spin_unlock_irqrestore(&ioapic_lock, flags);
1594
1595         save_control = CMOS_READ(RTC_CONTROL);
1596         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1597         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1598                    RTC_FREQ_SELECT);
1599         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1600
1601         i = 100;
1602         while (i-- > 0) {
1603                 mdelay(10);
1604                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1605                         i -= 10;
1606         }
1607
1608         CMOS_WRITE(save_control, RTC_CONTROL);
1609         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1610         clear_IO_APIC_pin(apic, pin);
1611
1612         spin_lock_irqsave(&ioapic_lock, flags);
1613         io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1614         io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1615         spin_unlock_irqrestore(&ioapic_lock, flags);
1616 }
1617
1618 /*
1619  * This code may look a bit paranoid, but it's supposed to cooperate with
1620  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
1621  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
1622  * fanatically on his truly buggy board.
1623  *
1624  * FIXME: really need to revamp this for modern platforms only.
1625  */
1626 static inline void check_timer(void)
1627 {
1628         int apic1, pin1, apic2, pin2;
1629         int vector;
1630         cpumask_t mask;
1631
1632         /*
1633          * get/set the timer IRQ vector:
1634          */
1635         disable_8259A_irq(0);
1636         vector = assign_irq_vector(0, TARGET_CPUS, &mask);
1637
1638         /*
1639          * Subtle, code in do_timer_interrupt() expects an AEOI
1640          * mode for the 8259A whenever interrupts are routed
1641          * through I/O APICs.  Also IRQ0 has to be enabled in
1642          * the 8259A which implies the virtual wire has to be
1643          * disabled in the local APIC.
1644          */
1645         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1646         init_8259A(1);
1647         if (timer_over_8254 > 0)
1648                 enable_8259A_irq(0);
1649
1650         pin1  = find_isa_irq_pin(0, mp_INT);
1651         apic1 = find_isa_irq_apic(0, mp_INT);
1652         pin2  = ioapic_i8259.pin;
1653         apic2 = ioapic_i8259.apic;
1654
1655         apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1656                 vector, apic1, pin1, apic2, pin2);
1657
1658         if (pin1 != -1) {
1659                 /*
1660                  * Ok, does IRQ0 through the IOAPIC work?
1661                  */
1662                 unmask_IO_APIC_irq(0);
1663                 if (!no_timer_check && timer_irq_works()) {
1664                         nmi_watchdog_default();
1665                         if (nmi_watchdog == NMI_IO_APIC) {
1666                                 disable_8259A_irq(0);
1667                                 setup_nmi();
1668                                 enable_8259A_irq(0);
1669                         }
1670                         if (disable_timer_pin_1 > 0)
1671                                 clear_IO_APIC_pin(0, pin1);
1672                         return;
1673                 }
1674                 clear_IO_APIC_pin(apic1, pin1);
1675                 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1676                                 "connected to IO-APIC\n");
1677         }
1678
1679         apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1680                                 "through the 8259A ... ");
1681         if (pin2 != -1) {
1682                 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1683                         apic2, pin2);
1684                 /*
1685                  * legacy devices should be connected to IO APIC #0
1686                  */
1687                 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1688                 if (timer_irq_works()) {
1689                         apic_printk(APIC_VERBOSE," works.\n");
1690                         nmi_watchdog_default();
1691                         if (nmi_watchdog == NMI_IO_APIC) {
1692                                 setup_nmi();
1693                         }
1694                         return;
1695                 }
1696                 /*
1697                  * Cleanup, just in case ...
1698                  */
1699                 clear_IO_APIC_pin(apic2, pin2);
1700         }
1701         apic_printk(APIC_VERBOSE," failed.\n");
1702
1703         if (nmi_watchdog == NMI_IO_APIC) {
1704                 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1705                 nmi_watchdog = 0;
1706         }
1707
1708         apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1709
1710         disable_8259A_irq(0);
1711         irq_desc[0].chip = &lapic_irq_type;
1712         apic_write(APIC_LVT0, APIC_DM_FIXED | vector);  /* Fixed mode */
1713         enable_8259A_irq(0);
1714
1715         if (timer_irq_works()) {
1716                 apic_printk(APIC_VERBOSE," works.\n");
1717                 return;
1718         }
1719         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1720         apic_printk(APIC_VERBOSE," failed.\n");
1721
1722         apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1723
1724         init_8259A(0);
1725         make_8259A_irq(0);
1726         apic_write(APIC_LVT0, APIC_DM_EXTINT);
1727
1728         unlock_ExtINT_logic();
1729
1730         if (timer_irq_works()) {
1731                 apic_printk(APIC_VERBOSE," works.\n");
1732                 return;
1733         }
1734         apic_printk(APIC_VERBOSE," failed :(.\n");
1735         panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1736 }
1737 #else
1738 #define check_timer() ((void)0)
1739 #endif /* !CONFIG_XEN */
1740
1741 static int __init notimercheck(char *s)
1742 {
1743         no_timer_check = 1;
1744         return 1;
1745 }
1746 __setup("no_timer_check", notimercheck);
1747
1748 /*
1749  *
1750  * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1751  * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1752  *   Linux doesn't really care, as it's not actually used
1753  *   for any interrupt handling anyway.
1754  */
1755 #define PIC_IRQS        (1<<2)
1756
1757 void __init setup_IO_APIC(void)
1758 {
1759         enable_IO_APIC();
1760
1761         if (acpi_ioapic)
1762                 io_apic_irqs = ~0;      /* all IRQs go through IOAPIC */
1763         else
1764                 io_apic_irqs = ~PIC_IRQS;
1765
1766         apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1767
1768 #ifndef CONFIG_XEN
1769         sync_Arb_IDs();
1770 #endif /* !CONFIG_XEN */
1771         setup_IO_APIC_irqs();
1772         init_IO_APIC_traps();
1773         check_timer();
1774         if (!acpi_ioapic)
1775                 print_IO_APIC();
1776 }
1777
1778 struct sysfs_ioapic_data {
1779         struct sys_device dev;
1780         struct IO_APIC_route_entry entry[0];
1781 };
1782 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1783
1784 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1785 {
1786         struct IO_APIC_route_entry *entry;
1787         struct sysfs_ioapic_data *data;
1788         int i;
1789
1790         data = container_of(dev, struct sysfs_ioapic_data, dev);
1791         entry = data->entry;
1792         for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
1793                 *entry = ioapic_read_entry(dev->id, i);
1794
1795         return 0;
1796 }
1797
1798 static int ioapic_resume(struct sys_device *dev)
1799 {
1800         struct IO_APIC_route_entry *entry;
1801         struct sysfs_ioapic_data *data;
1802         unsigned long flags;
1803         union IO_APIC_reg_00 reg_00;
1804         int i;
1805
1806         data = container_of(dev, struct sysfs_ioapic_data, dev);
1807         entry = data->entry;
1808
1809         spin_lock_irqsave(&ioapic_lock, flags);
1810         reg_00.raw = io_apic_read(dev->id, 0);
1811         if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1812                 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1813                 io_apic_write(dev->id, 0, reg_00.raw);
1814         }
1815         spin_unlock_irqrestore(&ioapic_lock, flags);
1816         for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
1817                 ioapic_write_entry(dev->id, i, entry[i]);
1818
1819         return 0;
1820 }
1821
1822 static struct sysdev_class ioapic_sysdev_class = {
1823         set_kset_name("ioapic"),
1824         .suspend = ioapic_suspend,
1825         .resume = ioapic_resume,
1826 };
1827
1828 static int __init ioapic_init_sysfs(void)
1829 {
1830         struct sys_device * dev;
1831         int i, size, error = 0;
1832
1833         error = sysdev_class_register(&ioapic_sysdev_class);
1834         if (error)
1835                 return error;
1836
1837         for (i = 0; i < nr_ioapics; i++ ) {
1838                 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1839                         * sizeof(struct IO_APIC_route_entry);
1840                 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1841                 if (!mp_ioapic_data[i]) {
1842                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1843                         continue;
1844                 }
1845                 memset(mp_ioapic_data[i], 0, size);
1846                 dev = &mp_ioapic_data[i]->dev;
1847                 dev->id = i;
1848                 dev->cls = &ioapic_sysdev_class;
1849                 error = sysdev_register(dev);
1850                 if (error) {
1851                         kfree(mp_ioapic_data[i]);
1852                         mp_ioapic_data[i] = NULL;
1853                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1854                         continue;
1855                 }
1856         }
1857
1858         return 0;
1859 }
1860
1861 device_initcall(ioapic_init_sysfs);
1862
1863 /*
1864  * Dynamic irq allocate and deallocation
1865  */
1866 int create_irq(void)
1867 {
1868         /* Allocate an unused irq */
1869         int irq;
1870         int new;
1871         int vector = 0;
1872         unsigned long flags;
1873         cpumask_t mask;
1874
1875         irq = -ENOSPC;
1876         spin_lock_irqsave(&vector_lock, flags);
1877         for (new = (NR_IRQS - 1); new >= 0; new--) {
1878                 if (platform_legacy_irq(new))
1879                         continue;
1880                 if (irq_vector[new] != 0)
1881                         continue;
1882                 vector = __assign_irq_vector(new, TARGET_CPUS, &mask);
1883                 if (likely(vector > 0))
1884                         irq = new;
1885                 break;
1886         }
1887         spin_unlock_irqrestore(&vector_lock, flags);
1888
1889         if (irq >= 0) {
1890                 dynamic_irq_init(irq);
1891         }
1892         return irq;
1893 }
1894
1895 void destroy_irq(unsigned int irq)
1896 {
1897         unsigned long flags;
1898
1899         dynamic_irq_cleanup(irq);
1900
1901         spin_lock_irqsave(&vector_lock, flags);
1902         __clear_irq_vector(irq);
1903         spin_unlock_irqrestore(&vector_lock, flags);
1904 }
1905
1906 /*
1907  * MSI mesage composition
1908  */
1909 #ifndef CONFIG_XEN
1910 #ifdef CONFIG_PCI_MSI
1911 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
1912 {
1913         int vector;
1914         unsigned dest;
1915         cpumask_t tmp;
1916
1917         vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
1918         if (vector >= 0) {
1919                 dest = cpu_mask_to_apicid(tmp);
1920
1921                 msg->address_hi = MSI_ADDR_BASE_HI;
1922                 msg->address_lo =
1923                         MSI_ADDR_BASE_LO |
1924                         ((INT_DEST_MODE == 0) ?
1925                                 MSI_ADDR_DEST_MODE_PHYSICAL:
1926                                 MSI_ADDR_DEST_MODE_LOGICAL) |
1927                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1928                                 MSI_ADDR_REDIRECTION_CPU:
1929                                 MSI_ADDR_REDIRECTION_LOWPRI) |
1930                         MSI_ADDR_DEST_ID(dest);
1931
1932                 msg->data =
1933                         MSI_DATA_TRIGGER_EDGE |
1934                         MSI_DATA_LEVEL_ASSERT |
1935                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1936                                 MSI_DATA_DELIVERY_FIXED:
1937                                 MSI_DATA_DELIVERY_LOWPRI) |
1938                         MSI_DATA_VECTOR(vector);
1939         }
1940         return vector;
1941 }
1942
1943 #ifdef CONFIG_SMP
1944 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
1945 {
1946         struct msi_msg msg;
1947         unsigned int dest;
1948         cpumask_t tmp;
1949         int vector;
1950
1951         cpus_and(tmp, mask, cpu_online_map);
1952         if (cpus_empty(tmp))
1953                 tmp = TARGET_CPUS;
1954
1955         cpus_and(mask, tmp, CPU_MASK_ALL);
1956
1957         vector = assign_irq_vector(irq, mask, &tmp);
1958         if (vector < 0)
1959                 return;
1960
1961         dest = cpu_mask_to_apicid(tmp);
1962
1963         read_msi_msg(irq, &msg);
1964
1965         msg.data &= ~MSI_DATA_VECTOR_MASK;
1966         msg.data |= MSI_DATA_VECTOR(vector);
1967         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
1968         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
1969
1970         write_msi_msg(irq, &msg);
1971         set_native_irq_info(irq, mask);
1972 }
1973 #endif /* CONFIG_SMP */
1974
1975 /*
1976  * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
1977  * which implement the MSI or MSI-X Capability Structure.
1978  */
1979 static struct irq_chip msi_chip = {
1980         .name           = "PCI-MSI",
1981         .unmask         = unmask_msi_irq,
1982         .mask           = mask_msi_irq,
1983         .ack            = ack_apic_edge,
1984 #ifdef CONFIG_SMP
1985         .set_affinity   = set_msi_irq_affinity,
1986 #endif
1987         .retrigger      = ioapic_retrigger_irq,
1988 };
1989
1990 int arch_setup_msi_irq(unsigned int irq, struct pci_dev *dev)
1991 {
1992         struct msi_msg msg;
1993         int ret;
1994         ret = msi_compose_msg(dev, irq, &msg);
1995         if (ret < 0)
1996                 return ret;
1997
1998         write_msi_msg(irq, &msg);
1999
2000         set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
2001
2002         return 0;
2003 }
2004
2005 void arch_teardown_msi_irq(unsigned int irq)
2006 {
2007         return;
2008 }
2009
2010 #endif /* CONFIG_PCI_MSI */
2011 #endif /* !CONFIG_XEN */
2012 /*
2013  * Hypertransport interrupt support
2014  */
2015 #ifdef CONFIG_HT_IRQ
2016
2017 #ifdef CONFIG_SMP
2018
2019 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2020 {
2021         struct ht_irq_msg msg;
2022         fetch_ht_irq_msg(irq, &msg);
2023
2024         msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2025         msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2026
2027         msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2028         msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2029
2030         write_ht_irq_msg(irq, &msg);
2031 }
2032
2033 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2034 {
2035         unsigned int dest;
2036         cpumask_t tmp;
2037         int vector;
2038
2039         cpus_and(tmp, mask, cpu_online_map);
2040         if (cpus_empty(tmp))
2041                 tmp = TARGET_CPUS;
2042
2043         cpus_and(mask, tmp, CPU_MASK_ALL);
2044
2045         vector = assign_irq_vector(irq, mask, &tmp);
2046         if (vector < 0)
2047                 return;
2048
2049         dest = cpu_mask_to_apicid(tmp);
2050
2051         target_ht_irq(irq, dest, vector);
2052         set_native_irq_info(irq, mask);
2053 }
2054 #endif
2055
2056 static struct irq_chip ht_irq_chip = {
2057         .name           = "PCI-HT",
2058         .mask           = mask_ht_irq,
2059         .unmask         = unmask_ht_irq,
2060         .ack            = ack_apic_edge,
2061 #ifdef CONFIG_SMP
2062         .set_affinity   = set_ht_irq_affinity,
2063 #endif
2064         .retrigger      = ioapic_retrigger_irq,
2065 };
2066
2067 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2068 {
2069         int vector;
2070         cpumask_t tmp;
2071
2072         vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
2073         if (vector >= 0) {
2074                 struct ht_irq_msg msg;
2075                 unsigned dest;
2076
2077                 dest = cpu_mask_to_apicid(tmp);
2078
2079                 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2080
2081                 msg.address_lo =
2082                         HT_IRQ_LOW_BASE |
2083                         HT_IRQ_LOW_DEST_ID(dest) |
2084                         HT_IRQ_LOW_VECTOR(vector) |
2085                         ((INT_DEST_MODE == 0) ?
2086                                 HT_IRQ_LOW_DM_PHYSICAL :
2087                                 HT_IRQ_LOW_DM_LOGICAL) |
2088                         HT_IRQ_LOW_RQEOI_EDGE |
2089                         ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2090                                 HT_IRQ_LOW_MT_FIXED :
2091                                 HT_IRQ_LOW_MT_ARBITRATED) |
2092                         HT_IRQ_LOW_IRQ_MASKED;
2093
2094                 write_ht_irq_msg(irq, &msg);
2095
2096                 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2097                                               handle_edge_irq, "edge");
2098         }
2099         return vector;
2100 }
2101 #endif /* CONFIG_HT_IRQ */
2102
2103 /* --------------------------------------------------------------------------
2104                           ACPI-based IOAPIC Configuration
2105    -------------------------------------------------------------------------- */
2106
2107 #ifdef CONFIG_ACPI
2108
2109 #define IO_APIC_MAX_ID          0xFE
2110
2111 int __init io_apic_get_redir_entries (int ioapic)
2112 {
2113         union IO_APIC_reg_01    reg_01;
2114         unsigned long flags;
2115
2116         spin_lock_irqsave(&ioapic_lock, flags);
2117         reg_01.raw = io_apic_read(ioapic, 1);
2118         spin_unlock_irqrestore(&ioapic_lock, flags);
2119
2120         return reg_01.bits.entries;
2121 }
2122
2123
2124 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2125 {
2126         struct IO_APIC_route_entry entry;
2127         unsigned long flags;
2128         int vector;
2129         cpumask_t mask;
2130
2131         if (!IO_APIC_IRQ(irq)) {
2132                 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2133                         ioapic);
2134                 return -EINVAL;
2135         }
2136
2137         /*
2138          * IRQs < 16 are already in the irq_2_pin[] map
2139          */
2140         if (irq >= 16)
2141                 add_pin_to_irq(irq, ioapic, pin);
2142
2143
2144         vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
2145         if (vector < 0)
2146                 return vector;
2147
2148         /*
2149          * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2150          * Note that we mask (disable) IRQs now -- these get enabled when the
2151          * corresponding device driver registers for this IRQ.
2152          */
2153
2154         memset(&entry,0,sizeof(entry));
2155
2156         entry.delivery_mode = INT_DELIVERY_MODE;
2157         entry.dest_mode = INT_DEST_MODE;
2158         entry.dest = cpu_mask_to_apicid(mask);
2159         entry.trigger = triggering;
2160         entry.polarity = polarity;
2161         entry.mask = 1;                                  /* Disabled (masked) */
2162         entry.vector = vector & 0xff;
2163
2164         apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2165                 "IRQ %d Mode:%i Active:%i)\n", ioapic, 
2166                mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2167                triggering, polarity);
2168
2169         ioapic_register_intr(irq, entry.vector, triggering);
2170
2171         if (!ioapic && (irq < 16))
2172                 disable_8259A_irq(irq);
2173
2174         ioapic_write_entry(ioapic, pin, entry);
2175
2176         spin_lock_irqsave(&ioapic_lock, flags);
2177         set_native_irq_info(irq, TARGET_CPUS);
2178         spin_unlock_irqrestore(&ioapic_lock, flags);
2179
2180         return 0;
2181 }
2182
2183 #endif /* CONFIG_ACPI */
2184
2185
2186 #ifndef CONFIG_XEN
2187 /*
2188  * This function currently is only a helper for the i386 smp boot process where
2189  * we need to reprogram the ioredtbls to cater for the cpus which have come online
2190  * so mask in all cases should simply be TARGET_CPUS
2191  */
2192 #ifdef CONFIG_SMP
2193 void __init setup_ioapic_dest(void)
2194 {
2195         int pin, ioapic, irq, irq_entry;
2196
2197         if (skip_ioapic_setup == 1)
2198                 return;
2199
2200         for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2201                 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2202                         irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2203                         if (irq_entry == -1)
2204                                 continue;
2205                         irq = pin_2_irq(irq_entry, ioapic, pin);
2206
2207                         /* setup_IO_APIC_irqs could fail to get vector for some device
2208                          * when you have too many devices, because at that time only boot
2209                          * cpu is online.
2210                          */
2211                         if(!irq_vector[irq])
2212                                 setup_IO_APIC_irq(ioapic, pin, irq_entry, irq);
2213                         else
2214                                 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2215                 }
2216
2217         }
2218 }
2219 #endif
2220 #endif /* !CONFIG_XEN */