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