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