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