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