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