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