]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/kernel/apic/io_apic.c
x86: ioapic: Use irq_data->state
[karo-tx-linux.git] / arch / x86 / kernel / apic / io_apic.c
1 /*
2  *      Intel IO-APIC support for multi-Pentium hosts.
3  *
4  *      Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
5  *
6  *      Many thanks to Stig Venaas for trying out countless experimental
7  *      patches and reporting/debugging problems patiently!
8  *
9  *      (c) 1999, Multiple IO-APIC support, developed by
10  *      Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11  *      Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12  *      further tested and cleaned up by Zach Brown <zab@redhat.com>
13  *      and Ingo Molnar <mingo@redhat.com>
14  *
15  *      Fixes
16  *      Maciej W. Rozycki       :       Bits for genuine 82489DX APICs;
17  *                                      thanks to Eric Gilmore
18  *                                      and Rolf G. Tews
19  *                                      for testing these extensively
20  *      Paul Diefenbaugh        :       Added full ACPI support
21  */
22
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/pci.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/compiler.h>
31 #include <linux/acpi.h>
32 #include <linux/module.h>
33 #include <linux/sysdev.h>
34 #include <linux/msi.h>
35 #include <linux/htirq.h>
36 #include <linux/freezer.h>
37 #include <linux/kthread.h>
38 #include <linux/jiffies.h>      /* time_after() */
39 #include <linux/slab.h>
40 #ifdef CONFIG_ACPI
41 #include <acpi/acpi_bus.h>
42 #endif
43 #include <linux/bootmem.h>
44 #include <linux/dmar.h>
45 #include <linux/hpet.h>
46
47 #include <asm/idle.h>
48 #include <asm/io.h>
49 #include <asm/smp.h>
50 #include <asm/cpu.h>
51 #include <asm/desc.h>
52 #include <asm/proto.h>
53 #include <asm/acpi.h>
54 #include <asm/dma.h>
55 #include <asm/timer.h>
56 #include <asm/i8259.h>
57 #include <asm/msidef.h>
58 #include <asm/hypertransport.h>
59 #include <asm/setup.h>
60 #include <asm/irq_remapping.h>
61 #include <asm/hpet.h>
62 #include <asm/hw_irq.h>
63
64 #include <asm/apic.h>
65
66 #define __apicdebuginit(type) static type __init
67 #define for_each_irq_pin(entry, head) \
68         for (entry = head; entry; entry = entry->next)
69
70 /*
71  *      Is the SiS APIC rmw bug present ?
72  *      -1 = don't know, 0 = no, 1 = yes
73  */
74 int sis_apic_bug = -1;
75
76 static DEFINE_RAW_SPINLOCK(ioapic_lock);
77 static DEFINE_RAW_SPINLOCK(vector_lock);
78
79 /*
80  * # of IRQ routing registers
81  */
82 int nr_ioapic_registers[MAX_IO_APICS];
83
84 /* I/O APIC entries */
85 struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
86 int nr_ioapics;
87
88 /* IO APIC gsi routing info */
89 struct mp_ioapic_gsi  mp_gsi_routing[MAX_IO_APICS];
90
91 /* The one past the highest gsi number used */
92 u32 gsi_top;
93
94 /* MP IRQ source entries */
95 struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
96
97 /* # of MP IRQ source entries */
98 int mp_irq_entries;
99
100 /* GSI interrupts */
101 static int nr_irqs_gsi = NR_IRQS_LEGACY;
102
103 #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
104 int mp_bus_id_to_type[MAX_MP_BUSSES];
105 #endif
106
107 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
108
109 int skip_ioapic_setup;
110
111 /**
112  * disable_ioapic_support() - disables ioapic support at runtime
113  */
114 void disable_ioapic_support(void)
115 {
116 #ifdef CONFIG_PCI
117         noioapicquirk = 1;
118         noioapicreroute = -1;
119 #endif
120         skip_ioapic_setup = 1;
121 }
122
123 static int __init parse_noapic(char *str)
124 {
125         /* disable IO-APIC */
126         disable_ioapic_support();
127         return 0;
128 }
129 early_param("noapic", parse_noapic);
130
131 static int io_apic_setup_irq_pin_once(unsigned int irq, int node,
132                                       struct io_apic_irq_attr *attr);
133
134 /* Will be called in mpparse/acpi/sfi codes for saving IRQ info */
135 void mp_save_irq(struct mpc_intsrc *m)
136 {
137         int i;
138
139         apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
140                 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
141                 m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
142                 m->srcbusirq, m->dstapic, m->dstirq);
143
144         for (i = 0; i < mp_irq_entries; i++) {
145                 if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
146                         return;
147         }
148
149         memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
150         if (++mp_irq_entries == MAX_IRQ_SOURCES)
151                 panic("Max # of irq sources exceeded!!\n");
152 }
153
154 struct irq_pin_list {
155         int apic, pin;
156         struct irq_pin_list *next;
157 };
158
159 static struct irq_pin_list *alloc_irq_pin_list(int node)
160 {
161         return kzalloc_node(sizeof(struct irq_pin_list), GFP_KERNEL, node);
162 }
163
164
165 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
166 #ifdef CONFIG_SPARSE_IRQ
167 static struct irq_cfg irq_cfgx[NR_IRQS_LEGACY];
168 #else
169 static struct irq_cfg irq_cfgx[NR_IRQS];
170 #endif
171
172 int __init arch_early_irq_init(void)
173 {
174         struct irq_cfg *cfg;
175         int count, node, i;
176
177         if (!legacy_pic->nr_legacy_irqs) {
178                 nr_irqs_gsi = 0;
179                 io_apic_irqs = ~0UL;
180         }
181
182         cfg = irq_cfgx;
183         count = ARRAY_SIZE(irq_cfgx);
184         node = cpu_to_node(0);
185
186         /* Make sure the legacy interrupts are marked in the bitmap */
187         irq_reserve_irqs(0, legacy_pic->nr_legacy_irqs);
188
189         for (i = 0; i < count; i++) {
190                 irq_set_chip_data(i, &cfg[i]);
191                 zalloc_cpumask_var_node(&cfg[i].domain, GFP_KERNEL, node);
192                 zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_KERNEL, node);
193                 /*
194                  * For legacy IRQ's, start with assigning irq0 to irq15 to
195                  * IRQ0_VECTOR to IRQ15_VECTOR on cpu 0.
196                  */
197                 if (i < legacy_pic->nr_legacy_irqs) {
198                         cfg[i].vector = IRQ0_VECTOR + i;
199                         cpumask_set_cpu(0, cfg[i].domain);
200                 }
201         }
202
203         return 0;
204 }
205
206 #ifdef CONFIG_SPARSE_IRQ
207 static struct irq_cfg *irq_cfg(unsigned int irq)
208 {
209         return irq_get_chip_data(irq);
210 }
211
212 static struct irq_cfg *alloc_irq_cfg(unsigned int irq, int node)
213 {
214         struct irq_cfg *cfg;
215
216         cfg = kzalloc_node(sizeof(*cfg), GFP_KERNEL, node);
217         if (!cfg)
218                 return NULL;
219         if (!zalloc_cpumask_var_node(&cfg->domain, GFP_KERNEL, node))
220                 goto out_cfg;
221         if (!zalloc_cpumask_var_node(&cfg->old_domain, GFP_KERNEL, node))
222                 goto out_domain;
223         return cfg;
224 out_domain:
225         free_cpumask_var(cfg->domain);
226 out_cfg:
227         kfree(cfg);
228         return NULL;
229 }
230
231 static void free_irq_cfg(unsigned int at, struct irq_cfg *cfg)
232 {
233         if (!cfg)
234                 return;
235         irq_set_chip_data(at, NULL);
236         free_cpumask_var(cfg->domain);
237         free_cpumask_var(cfg->old_domain);
238         kfree(cfg);
239 }
240
241 #else
242
243 struct irq_cfg *irq_cfg(unsigned int irq)
244 {
245         return irq < nr_irqs ? irq_cfgx + irq : NULL;
246 }
247
248 static struct irq_cfg *alloc_irq_cfg(unsigned int irq, int node)
249 {
250         return irq_cfgx + irq;
251 }
252
253 static inline void free_irq_cfg(unsigned int at, struct irq_cfg *cfg) { }
254
255 #endif
256
257 static struct irq_cfg *alloc_irq_and_cfg_at(unsigned int at, int node)
258 {
259         int res = irq_alloc_desc_at(at, node);
260         struct irq_cfg *cfg;
261
262         if (res < 0) {
263                 if (res != -EEXIST)
264                         return NULL;
265                 cfg = irq_get_chip_data(at);
266                 if (cfg)
267                         return cfg;
268         }
269
270         cfg = alloc_irq_cfg(at, node);
271         if (cfg)
272                 irq_set_chip_data(at, cfg);
273         else
274                 irq_free_desc(at);
275         return cfg;
276 }
277
278 static int alloc_irq_from(unsigned int from, int node)
279 {
280         return irq_alloc_desc_from(from, node);
281 }
282
283 static void free_irq_at(unsigned int at, struct irq_cfg *cfg)
284 {
285         free_irq_cfg(at, cfg);
286         irq_free_desc(at);
287 }
288
289 struct io_apic {
290         unsigned int index;
291         unsigned int unused[3];
292         unsigned int data;
293         unsigned int unused2[11];
294         unsigned int eoi;
295 };
296
297 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
298 {
299         return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
300                 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
301 }
302
303 static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
304 {
305         struct io_apic __iomem *io_apic = io_apic_base(apic);
306         writel(vector, &io_apic->eoi);
307 }
308
309 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
310 {
311         struct io_apic __iomem *io_apic = io_apic_base(apic);
312         writel(reg, &io_apic->index);
313         return readl(&io_apic->data);
314 }
315
316 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
317 {
318         struct io_apic __iomem *io_apic = io_apic_base(apic);
319         writel(reg, &io_apic->index);
320         writel(value, &io_apic->data);
321 }
322
323 /*
324  * Re-write a value: to be used for read-modify-write
325  * cycles where the read already set up the index register.
326  *
327  * Older SiS APIC requires we rewrite the index register
328  */
329 static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
330 {
331         struct io_apic __iomem *io_apic = io_apic_base(apic);
332
333         if (sis_apic_bug)
334                 writel(reg, &io_apic->index);
335         writel(value, &io_apic->data);
336 }
337
338 static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
339 {
340         struct irq_pin_list *entry;
341         unsigned long flags;
342
343         raw_spin_lock_irqsave(&ioapic_lock, flags);
344         for_each_irq_pin(entry, cfg->irq_2_pin) {
345                 unsigned int reg;
346                 int pin;
347
348                 pin = entry->pin;
349                 reg = io_apic_read(entry->apic, 0x10 + pin*2);
350                 /* Is the remote IRR bit set? */
351                 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
352                         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
353                         return true;
354                 }
355         }
356         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
357
358         return false;
359 }
360
361 union entry_union {
362         struct { u32 w1, w2; };
363         struct IO_APIC_route_entry entry;
364 };
365
366 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
367 {
368         union entry_union eu;
369         unsigned long flags;
370         raw_spin_lock_irqsave(&ioapic_lock, flags);
371         eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
372         eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
373         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
374         return eu.entry;
375 }
376
377 /*
378  * When we write a new IO APIC routing entry, we need to write the high
379  * word first! If the mask bit in the low word is clear, we will enable
380  * the interrupt, and we need to make sure the entry is fully populated
381  * before that happens.
382  */
383 static void
384 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
385 {
386         union entry_union eu = {{0, 0}};
387
388         eu.entry = e;
389         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
390         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
391 }
392
393 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
394 {
395         unsigned long flags;
396         raw_spin_lock_irqsave(&ioapic_lock, flags);
397         __ioapic_write_entry(apic, pin, e);
398         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
399 }
400
401 /*
402  * When we mask an IO APIC routing entry, we need to write the low
403  * word first, in order to set the mask bit before we change the
404  * high bits!
405  */
406 static void ioapic_mask_entry(int apic, int pin)
407 {
408         unsigned long flags;
409         union entry_union eu = { .entry.mask = 1 };
410
411         raw_spin_lock_irqsave(&ioapic_lock, flags);
412         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
413         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
414         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
415 }
416
417 /*
418  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
419  * shared ISA-space IRQs, so we have to support them. We are super
420  * fast in the common case, and fast for shared ISA-space IRQs.
421  */
422 static int
423 __add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
424 {
425         struct irq_pin_list **last, *entry;
426
427         /* don't allow duplicates */
428         last = &cfg->irq_2_pin;
429         for_each_irq_pin(entry, cfg->irq_2_pin) {
430                 if (entry->apic == apic && entry->pin == pin)
431                         return 0;
432                 last = &entry->next;
433         }
434
435         entry = alloc_irq_pin_list(node);
436         if (!entry) {
437                 printk(KERN_ERR "can not alloc irq_pin_list (%d,%d,%d)\n",
438                                 node, apic, pin);
439                 return -ENOMEM;
440         }
441         entry->apic = apic;
442         entry->pin = pin;
443
444         *last = entry;
445         return 0;
446 }
447
448 static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
449 {
450         if (__add_pin_to_irq_node(cfg, node, apic, pin))
451                 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
452 }
453
454 /*
455  * Reroute an IRQ to a different pin.
456  */
457 static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
458                                            int oldapic, int oldpin,
459                                            int newapic, int newpin)
460 {
461         struct irq_pin_list *entry;
462
463         for_each_irq_pin(entry, cfg->irq_2_pin) {
464                 if (entry->apic == oldapic && entry->pin == oldpin) {
465                         entry->apic = newapic;
466                         entry->pin = newpin;
467                         /* every one is different, right? */
468                         return;
469                 }
470         }
471
472         /* old apic/pin didn't exist, so just add new ones */
473         add_pin_to_irq_node(cfg, node, newapic, newpin);
474 }
475
476 static void __io_apic_modify_irq(struct irq_pin_list *entry,
477                                  int mask_and, int mask_or,
478                                  void (*final)(struct irq_pin_list *entry))
479 {
480         unsigned int reg, pin;
481
482         pin = entry->pin;
483         reg = io_apic_read(entry->apic, 0x10 + pin * 2);
484         reg &= mask_and;
485         reg |= mask_or;
486         io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
487         if (final)
488                 final(entry);
489 }
490
491 static void io_apic_modify_irq(struct irq_cfg *cfg,
492                                int mask_and, int mask_or,
493                                void (*final)(struct irq_pin_list *entry))
494 {
495         struct irq_pin_list *entry;
496
497         for_each_irq_pin(entry, cfg->irq_2_pin)
498                 __io_apic_modify_irq(entry, mask_and, mask_or, final);
499 }
500
501 static void __mask_and_edge_IO_APIC_irq(struct irq_pin_list *entry)
502 {
503         __io_apic_modify_irq(entry, ~IO_APIC_REDIR_LEVEL_TRIGGER,
504                              IO_APIC_REDIR_MASKED, NULL);
505 }
506
507 static void __unmask_and_level_IO_APIC_irq(struct irq_pin_list *entry)
508 {
509         __io_apic_modify_irq(entry, ~IO_APIC_REDIR_MASKED,
510                              IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
511 }
512
513 static void io_apic_sync(struct irq_pin_list *entry)
514 {
515         /*
516          * Synchronize the IO-APIC and the CPU by doing
517          * a dummy read from the IO-APIC
518          */
519         struct io_apic __iomem *io_apic;
520         io_apic = io_apic_base(entry->apic);
521         readl(&io_apic->data);
522 }
523
524 static void mask_ioapic(struct irq_cfg *cfg)
525 {
526         unsigned long flags;
527
528         raw_spin_lock_irqsave(&ioapic_lock, flags);
529         io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
530         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
531 }
532
533 static void mask_ioapic_irq(struct irq_data *data)
534 {
535         mask_ioapic(data->chip_data);
536 }
537
538 static void __unmask_ioapic(struct irq_cfg *cfg)
539 {
540         io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
541 }
542
543 static void unmask_ioapic(struct irq_cfg *cfg)
544 {
545         unsigned long flags;
546
547         raw_spin_lock_irqsave(&ioapic_lock, flags);
548         __unmask_ioapic(cfg);
549         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
550 }
551
552 static void unmask_ioapic_irq(struct irq_data *data)
553 {
554         unmask_ioapic(data->chip_data);
555 }
556
557 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
558 {
559         struct IO_APIC_route_entry entry;
560
561         /* Check delivery_mode to be sure we're not clearing an SMI pin */
562         entry = ioapic_read_entry(apic, pin);
563         if (entry.delivery_mode == dest_SMI)
564                 return;
565         /*
566          * Disable it in the IO-APIC irq-routing table:
567          */
568         ioapic_mask_entry(apic, pin);
569 }
570
571 static void clear_IO_APIC (void)
572 {
573         int apic, pin;
574
575         for (apic = 0; apic < nr_ioapics; apic++)
576                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
577                         clear_IO_APIC_pin(apic, pin);
578 }
579
580 #ifdef CONFIG_X86_32
581 /*
582  * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
583  * specific CPU-side IRQs.
584  */
585
586 #define MAX_PIRQS 8
587 static int pirq_entries[MAX_PIRQS] = {
588         [0 ... MAX_PIRQS - 1] = -1
589 };
590
591 static int __init ioapic_pirq_setup(char *str)
592 {
593         int i, max;
594         int ints[MAX_PIRQS+1];
595
596         get_options(str, ARRAY_SIZE(ints), ints);
597
598         apic_printk(APIC_VERBOSE, KERN_INFO
599                         "PIRQ redirection, working around broken MP-BIOS.\n");
600         max = MAX_PIRQS;
601         if (ints[0] < MAX_PIRQS)
602                 max = ints[0];
603
604         for (i = 0; i < max; i++) {
605                 apic_printk(APIC_VERBOSE, KERN_DEBUG
606                                 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
607                 /*
608                  * PIRQs are mapped upside down, usually.
609                  */
610                 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
611         }
612         return 1;
613 }
614
615 __setup("pirq=", ioapic_pirq_setup);
616 #endif /* CONFIG_X86_32 */
617
618 struct IO_APIC_route_entry **alloc_ioapic_entries(void)
619 {
620         int apic;
621         struct IO_APIC_route_entry **ioapic_entries;
622
623         ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
624                                 GFP_KERNEL);
625         if (!ioapic_entries)
626                 return 0;
627
628         for (apic = 0; apic < nr_ioapics; apic++) {
629                 ioapic_entries[apic] =
630                         kzalloc(sizeof(struct IO_APIC_route_entry) *
631                                 nr_ioapic_registers[apic], GFP_KERNEL);
632                 if (!ioapic_entries[apic])
633                         goto nomem;
634         }
635
636         return ioapic_entries;
637
638 nomem:
639         while (--apic >= 0)
640                 kfree(ioapic_entries[apic]);
641         kfree(ioapic_entries);
642
643         return 0;
644 }
645
646 /*
647  * Saves all the IO-APIC RTE's
648  */
649 int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
650 {
651         int apic, pin;
652
653         if (!ioapic_entries)
654                 return -ENOMEM;
655
656         for (apic = 0; apic < nr_ioapics; apic++) {
657                 if (!ioapic_entries[apic])
658                         return -ENOMEM;
659
660                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
661                         ioapic_entries[apic][pin] =
662                                 ioapic_read_entry(apic, pin);
663         }
664
665         return 0;
666 }
667
668 /*
669  * Mask all IO APIC entries.
670  */
671 void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
672 {
673         int apic, pin;
674
675         if (!ioapic_entries)
676                 return;
677
678         for (apic = 0; apic < nr_ioapics; apic++) {
679                 if (!ioapic_entries[apic])
680                         break;
681
682                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
683                         struct IO_APIC_route_entry entry;
684
685                         entry = ioapic_entries[apic][pin];
686                         if (!entry.mask) {
687                                 entry.mask = 1;
688                                 ioapic_write_entry(apic, pin, entry);
689                         }
690                 }
691         }
692 }
693
694 /*
695  * Restore IO APIC entries which was saved in ioapic_entries.
696  */
697 int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
698 {
699         int apic, pin;
700
701         if (!ioapic_entries)
702                 return -ENOMEM;
703
704         for (apic = 0; apic < nr_ioapics; apic++) {
705                 if (!ioapic_entries[apic])
706                         return -ENOMEM;
707
708                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
709                         ioapic_write_entry(apic, pin,
710                                         ioapic_entries[apic][pin]);
711         }
712         return 0;
713 }
714
715 void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
716 {
717         int apic;
718
719         for (apic = 0; apic < nr_ioapics; apic++)
720                 kfree(ioapic_entries[apic]);
721
722         kfree(ioapic_entries);
723 }
724
725 /*
726  * Find the IRQ entry number of a certain pin.
727  */
728 static int find_irq_entry(int apic, int pin, int type)
729 {
730         int i;
731
732         for (i = 0; i < mp_irq_entries; i++)
733                 if (mp_irqs[i].irqtype == type &&
734                     (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
735                      mp_irqs[i].dstapic == MP_APIC_ALL) &&
736                     mp_irqs[i].dstirq == pin)
737                         return i;
738
739         return -1;
740 }
741
742 /*
743  * Find the pin to which IRQ[irq] (ISA) is connected
744  */
745 static int __init find_isa_irq_pin(int irq, int type)
746 {
747         int i;
748
749         for (i = 0; i < mp_irq_entries; i++) {
750                 int lbus = mp_irqs[i].srcbus;
751
752                 if (test_bit(lbus, mp_bus_not_pci) &&
753                     (mp_irqs[i].irqtype == type) &&
754                     (mp_irqs[i].srcbusirq == irq))
755
756                         return mp_irqs[i].dstirq;
757         }
758         return -1;
759 }
760
761 static int __init find_isa_irq_apic(int irq, int type)
762 {
763         int i;
764
765         for (i = 0; i < mp_irq_entries; i++) {
766                 int lbus = mp_irqs[i].srcbus;
767
768                 if (test_bit(lbus, mp_bus_not_pci) &&
769                     (mp_irqs[i].irqtype == type) &&
770                     (mp_irqs[i].srcbusirq == irq))
771                         break;
772         }
773         if (i < mp_irq_entries) {
774                 int apic;
775                 for(apic = 0; apic < nr_ioapics; apic++) {
776                         if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
777                                 return apic;
778                 }
779         }
780
781         return -1;
782 }
783
784 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
785 /*
786  * EISA Edge/Level control register, ELCR
787  */
788 static int EISA_ELCR(unsigned int irq)
789 {
790         if (irq < legacy_pic->nr_legacy_irqs) {
791                 unsigned int port = 0x4d0 + (irq >> 3);
792                 return (inb(port) >> (irq & 7)) & 1;
793         }
794         apic_printk(APIC_VERBOSE, KERN_INFO
795                         "Broken MPtable reports ISA irq %d\n", irq);
796         return 0;
797 }
798
799 #endif
800
801 /* ISA interrupts are always polarity zero edge triggered,
802  * when listed as conforming in the MP table. */
803
804 #define default_ISA_trigger(idx)        (0)
805 #define default_ISA_polarity(idx)       (0)
806
807 /* EISA interrupts are always polarity zero and can be edge or level
808  * trigger depending on the ELCR value.  If an interrupt is listed as
809  * EISA conforming in the MP table, that means its trigger type must
810  * be read in from the ELCR */
811
812 #define default_EISA_trigger(idx)       (EISA_ELCR(mp_irqs[idx].srcbusirq))
813 #define default_EISA_polarity(idx)      default_ISA_polarity(idx)
814
815 /* PCI interrupts are always polarity one level triggered,
816  * when listed as conforming in the MP table. */
817
818 #define default_PCI_trigger(idx)        (1)
819 #define default_PCI_polarity(idx)       (1)
820
821 /* MCA interrupts are always polarity zero level triggered,
822  * when listed as conforming in the MP table. */
823
824 #define default_MCA_trigger(idx)        (1)
825 #define default_MCA_polarity(idx)       default_ISA_polarity(idx)
826
827 static int irq_polarity(int idx)
828 {
829         int bus = mp_irqs[idx].srcbus;
830         int polarity;
831
832         /*
833          * Determine IRQ line polarity (high active or low active):
834          */
835         switch (mp_irqs[idx].irqflag & 3)
836         {
837                 case 0: /* conforms, ie. bus-type dependent polarity */
838                         if (test_bit(bus, mp_bus_not_pci))
839                                 polarity = default_ISA_polarity(idx);
840                         else
841                                 polarity = default_PCI_polarity(idx);
842                         break;
843                 case 1: /* high active */
844                 {
845                         polarity = 0;
846                         break;
847                 }
848                 case 2: /* reserved */
849                 {
850                         printk(KERN_WARNING "broken BIOS!!\n");
851                         polarity = 1;
852                         break;
853                 }
854                 case 3: /* low active */
855                 {
856                         polarity = 1;
857                         break;
858                 }
859                 default: /* invalid */
860                 {
861                         printk(KERN_WARNING "broken BIOS!!\n");
862                         polarity = 1;
863                         break;
864                 }
865         }
866         return polarity;
867 }
868
869 static int irq_trigger(int idx)
870 {
871         int bus = mp_irqs[idx].srcbus;
872         int trigger;
873
874         /*
875          * Determine IRQ trigger mode (edge or level sensitive):
876          */
877         switch ((mp_irqs[idx].irqflag>>2) & 3)
878         {
879                 case 0: /* conforms, ie. bus-type dependent */
880                         if (test_bit(bus, mp_bus_not_pci))
881                                 trigger = default_ISA_trigger(idx);
882                         else
883                                 trigger = default_PCI_trigger(idx);
884 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
885                         switch (mp_bus_id_to_type[bus]) {
886                                 case MP_BUS_ISA: /* ISA pin */
887                                 {
888                                         /* set before the switch */
889                                         break;
890                                 }
891                                 case MP_BUS_EISA: /* EISA pin */
892                                 {
893                                         trigger = default_EISA_trigger(idx);
894                                         break;
895                                 }
896                                 case MP_BUS_PCI: /* PCI pin */
897                                 {
898                                         /* set before the switch */
899                                         break;
900                                 }
901                                 case MP_BUS_MCA: /* MCA pin */
902                                 {
903                                         trigger = default_MCA_trigger(idx);
904                                         break;
905                                 }
906                                 default:
907                                 {
908                                         printk(KERN_WARNING "broken BIOS!!\n");
909                                         trigger = 1;
910                                         break;
911                                 }
912                         }
913 #endif
914                         break;
915                 case 1: /* edge */
916                 {
917                         trigger = 0;
918                         break;
919                 }
920                 case 2: /* reserved */
921                 {
922                         printk(KERN_WARNING "broken BIOS!!\n");
923                         trigger = 1;
924                         break;
925                 }
926                 case 3: /* level */
927                 {
928                         trigger = 1;
929                         break;
930                 }
931                 default: /* invalid */
932                 {
933                         printk(KERN_WARNING "broken BIOS!!\n");
934                         trigger = 0;
935                         break;
936                 }
937         }
938         return trigger;
939 }
940
941 static int pin_2_irq(int idx, int apic, int pin)
942 {
943         int irq;
944         int bus = mp_irqs[idx].srcbus;
945
946         /*
947          * Debugging check, we are in big trouble if this message pops up!
948          */
949         if (mp_irqs[idx].dstirq != pin)
950                 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
951
952         if (test_bit(bus, mp_bus_not_pci)) {
953                 irq = mp_irqs[idx].srcbusirq;
954         } else {
955                 u32 gsi = mp_gsi_routing[apic].gsi_base + pin;
956
957                 if (gsi >= NR_IRQS_LEGACY)
958                         irq = gsi;
959                 else
960                         irq = gsi_top + gsi;
961         }
962
963 #ifdef CONFIG_X86_32
964         /*
965          * PCI IRQ command line redirection. Yes, limits are hardcoded.
966          */
967         if ((pin >= 16) && (pin <= 23)) {
968                 if (pirq_entries[pin-16] != -1) {
969                         if (!pirq_entries[pin-16]) {
970                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
971                                                 "disabling PIRQ%d\n", pin-16);
972                         } else {
973                                 irq = pirq_entries[pin-16];
974                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
975                                                 "using PIRQ%d -> IRQ %d\n",
976                                                 pin-16, irq);
977                         }
978                 }
979         }
980 #endif
981
982         return irq;
983 }
984
985 /*
986  * Find a specific PCI IRQ entry.
987  * Not an __init, possibly needed by modules
988  */
989 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
990                                 struct io_apic_irq_attr *irq_attr)
991 {
992         int apic, i, best_guess = -1;
993
994         apic_printk(APIC_DEBUG,
995                     "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
996                     bus, slot, pin);
997         if (test_bit(bus, mp_bus_not_pci)) {
998                 apic_printk(APIC_VERBOSE,
999                             "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1000                 return -1;
1001         }
1002         for (i = 0; i < mp_irq_entries; i++) {
1003                 int lbus = mp_irqs[i].srcbus;
1004
1005                 for (apic = 0; apic < nr_ioapics; apic++)
1006                         if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1007                             mp_irqs[i].dstapic == MP_APIC_ALL)
1008                                 break;
1009
1010                 if (!test_bit(lbus, mp_bus_not_pci) &&
1011                     !mp_irqs[i].irqtype &&
1012                     (bus == lbus) &&
1013                     (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1014                         int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1015
1016                         if (!(apic || IO_APIC_IRQ(irq)))
1017                                 continue;
1018
1019                         if (pin == (mp_irqs[i].srcbusirq & 3)) {
1020                                 set_io_apic_irq_attr(irq_attr, apic,
1021                                                      mp_irqs[i].dstirq,
1022                                                      irq_trigger(i),
1023                                                      irq_polarity(i));
1024                                 return irq;
1025                         }
1026                         /*
1027                          * Use the first all-but-pin matching entry as a
1028                          * best-guess fuzzy result for broken mptables.
1029                          */
1030                         if (best_guess < 0) {
1031                                 set_io_apic_irq_attr(irq_attr, apic,
1032                                                      mp_irqs[i].dstirq,
1033                                                      irq_trigger(i),
1034                                                      irq_polarity(i));
1035                                 best_guess = irq;
1036                         }
1037                 }
1038         }
1039         return best_guess;
1040 }
1041 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1042
1043 void lock_vector_lock(void)
1044 {
1045         /* Used to the online set of cpus does not change
1046          * during assign_irq_vector.
1047          */
1048         raw_spin_lock(&vector_lock);
1049 }
1050
1051 void unlock_vector_lock(void)
1052 {
1053         raw_spin_unlock(&vector_lock);
1054 }
1055
1056 static int
1057 __assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
1058 {
1059         /*
1060          * NOTE! The local APIC isn't very good at handling
1061          * multiple interrupts at the same interrupt level.
1062          * As the interrupt level is determined by taking the
1063          * vector number and shifting that right by 4, we
1064          * want to spread these out a bit so that they don't
1065          * all fall in the same interrupt level.
1066          *
1067          * Also, we've got to be careful not to trash gate
1068          * 0x80, because int 0x80 is hm, kind of importantish. ;)
1069          */
1070         static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
1071         static int current_offset = VECTOR_OFFSET_START % 8;
1072         unsigned int old_vector;
1073         int cpu, err;
1074         cpumask_var_t tmp_mask;
1075
1076         if (cfg->move_in_progress)
1077                 return -EBUSY;
1078
1079         if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1080                 return -ENOMEM;
1081
1082         old_vector = cfg->vector;
1083         if (old_vector) {
1084                 cpumask_and(tmp_mask, mask, cpu_online_mask);
1085                 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1086                 if (!cpumask_empty(tmp_mask)) {
1087                         free_cpumask_var(tmp_mask);
1088                         return 0;
1089                 }
1090         }
1091
1092         /* Only try and allocate irqs on cpus that are present */
1093         err = -ENOSPC;
1094         for_each_cpu_and(cpu, mask, cpu_online_mask) {
1095                 int new_cpu;
1096                 int vector, offset;
1097
1098                 apic->vector_allocation_domain(cpu, tmp_mask);
1099
1100                 vector = current_vector;
1101                 offset = current_offset;
1102 next:
1103                 vector += 8;
1104                 if (vector >= first_system_vector) {
1105                         /* If out of vectors on large boxen, must share them. */
1106                         offset = (offset + 1) % 8;
1107                         vector = FIRST_EXTERNAL_VECTOR + offset;
1108                 }
1109                 if (unlikely(current_vector == vector))
1110                         continue;
1111
1112                 if (test_bit(vector, used_vectors))
1113                         goto next;
1114
1115                 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
1116                         if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1117                                 goto next;
1118                 /* Found one! */
1119                 current_vector = vector;
1120                 current_offset = offset;
1121                 if (old_vector) {
1122                         cfg->move_in_progress = 1;
1123                         cpumask_copy(cfg->old_domain, cfg->domain);
1124                 }
1125                 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
1126                         per_cpu(vector_irq, new_cpu)[vector] = irq;
1127                 cfg->vector = vector;
1128                 cpumask_copy(cfg->domain, tmp_mask);
1129                 err = 0;
1130                 break;
1131         }
1132         free_cpumask_var(tmp_mask);
1133         return err;
1134 }
1135
1136 int assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
1137 {
1138         int err;
1139         unsigned long flags;
1140
1141         raw_spin_lock_irqsave(&vector_lock, flags);
1142         err = __assign_irq_vector(irq, cfg, mask);
1143         raw_spin_unlock_irqrestore(&vector_lock, flags);
1144         return err;
1145 }
1146
1147 static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
1148 {
1149         int cpu, vector;
1150
1151         BUG_ON(!cfg->vector);
1152
1153         vector = cfg->vector;
1154         for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
1155                 per_cpu(vector_irq, cpu)[vector] = -1;
1156
1157         cfg->vector = 0;
1158         cpumask_clear(cfg->domain);
1159
1160         if (likely(!cfg->move_in_progress))
1161                 return;
1162         for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
1163                 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1164                                                                 vector++) {
1165                         if (per_cpu(vector_irq, cpu)[vector] != irq)
1166                                 continue;
1167                         per_cpu(vector_irq, cpu)[vector] = -1;
1168                         break;
1169                 }
1170         }
1171         cfg->move_in_progress = 0;
1172 }
1173
1174 void __setup_vector_irq(int cpu)
1175 {
1176         /* Initialize vector_irq on a new cpu */
1177         int irq, vector;
1178         struct irq_cfg *cfg;
1179
1180         /*
1181          * vector_lock will make sure that we don't run into irq vector
1182          * assignments that might be happening on another cpu in parallel,
1183          * while we setup our initial vector to irq mappings.
1184          */
1185         raw_spin_lock(&vector_lock);
1186         /* Mark the inuse vectors */
1187         for_each_active_irq(irq) {
1188                 cfg = irq_get_chip_data(irq);
1189                 if (!cfg)
1190                         continue;
1191                 /*
1192                  * If it is a legacy IRQ handled by the legacy PIC, this cpu
1193                  * will be part of the irq_cfg's domain.
1194                  */
1195                 if (irq < legacy_pic->nr_legacy_irqs && !IO_APIC_IRQ(irq))
1196                         cpumask_set_cpu(cpu, cfg->domain);
1197
1198                 if (!cpumask_test_cpu(cpu, cfg->domain))
1199                         continue;
1200                 vector = cfg->vector;
1201                 per_cpu(vector_irq, cpu)[vector] = irq;
1202         }
1203         /* Mark the free vectors */
1204         for (vector = 0; vector < NR_VECTORS; ++vector) {
1205                 irq = per_cpu(vector_irq, cpu)[vector];
1206                 if (irq < 0)
1207                         continue;
1208
1209                 cfg = irq_cfg(irq);
1210                 if (!cpumask_test_cpu(cpu, cfg->domain))
1211                         per_cpu(vector_irq, cpu)[vector] = -1;
1212         }
1213         raw_spin_unlock(&vector_lock);
1214 }
1215
1216 static struct irq_chip ioapic_chip;
1217 static struct irq_chip ir_ioapic_chip;
1218
1219 #ifdef CONFIG_X86_32
1220 static inline int IO_APIC_irq_trigger(int irq)
1221 {
1222         int apic, idx, pin;
1223
1224         for (apic = 0; apic < nr_ioapics; apic++) {
1225                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1226                         idx = find_irq_entry(apic, pin, mp_INT);
1227                         if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1228                                 return irq_trigger(idx);
1229                 }
1230         }
1231         /*
1232          * nonexistent IRQs are edge default
1233          */
1234         return 0;
1235 }
1236 #else
1237 static inline int IO_APIC_irq_trigger(int irq)
1238 {
1239         return 1;
1240 }
1241 #endif
1242
1243 static void ioapic_register_intr(unsigned int irq, unsigned long trigger)
1244 {
1245         struct irq_chip *chip = &ioapic_chip;
1246         irq_flow_handler_t hdl;
1247         bool fasteoi;
1248
1249         if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1250             trigger == IOAPIC_LEVEL) {
1251                 irq_set_status_flags(irq, IRQ_LEVEL);
1252                 fasteoi = true;
1253         } else {
1254                 irq_clear_status_flags(irq, IRQ_LEVEL);
1255                 fasteoi = false;
1256         }
1257
1258         if (irq_remapped(irq_get_chip_data(irq))) {
1259                 irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
1260                 chip = &ir_ioapic_chip;
1261                 fasteoi = trigger != 0;
1262         }
1263
1264         hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
1265         irq_set_chip_and_handler_name(irq, chip, hdl,
1266                                       fasteoi ? "fasteoi" : "edge");
1267 }
1268
1269 static int setup_ioapic_entry(int apic_id, int irq,
1270                               struct IO_APIC_route_entry *entry,
1271                               unsigned int destination, int trigger,
1272                               int polarity, int vector, int pin)
1273 {
1274         /*
1275          * add it to the IO-APIC irq-routing table:
1276          */
1277         memset(entry,0,sizeof(*entry));
1278
1279         if (intr_remapping_enabled) {
1280                 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
1281                 struct irte irte;
1282                 struct IR_IO_APIC_route_entry *ir_entry =
1283                         (struct IR_IO_APIC_route_entry *) entry;
1284                 int index;
1285
1286                 if (!iommu)
1287                         panic("No mapping iommu for ioapic %d\n", apic_id);
1288
1289                 index = alloc_irte(iommu, irq, 1);
1290                 if (index < 0)
1291                         panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
1292
1293                 prepare_irte(&irte, vector, destination);
1294
1295                 /* Set source-id of interrupt request */
1296                 set_ioapic_sid(&irte, apic_id);
1297
1298                 modify_irte(irq, &irte);
1299
1300                 ir_entry->index2 = (index >> 15) & 0x1;
1301                 ir_entry->zero = 0;
1302                 ir_entry->format = 1;
1303                 ir_entry->index = (index & 0x7fff);
1304                 /*
1305                  * IO-APIC RTE will be configured with virtual vector.
1306                  * irq handler will do the explicit EOI to the io-apic.
1307                  */
1308                 ir_entry->vector = pin;
1309         } else {
1310                 entry->delivery_mode = apic->irq_delivery_mode;
1311                 entry->dest_mode = apic->irq_dest_mode;
1312                 entry->dest = destination;
1313                 entry->vector = vector;
1314         }
1315
1316         entry->mask = 0;                                /* enable IRQ */
1317         entry->trigger = trigger;
1318         entry->polarity = polarity;
1319
1320         /* Mask level triggered irqs.
1321          * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1322          */
1323         if (trigger)
1324                 entry->mask = 1;
1325         return 0;
1326 }
1327
1328 static void setup_ioapic_irq(int apic_id, int pin, unsigned int irq,
1329                              struct irq_cfg *cfg, int trigger, int polarity)
1330 {
1331         struct IO_APIC_route_entry entry;
1332         unsigned int dest;
1333
1334         if (!IO_APIC_IRQ(irq))
1335                 return;
1336         /*
1337          * For legacy irqs, cfg->domain starts with cpu 0 for legacy
1338          * controllers like 8259. Now that IO-APIC can handle this irq, update
1339          * the cfg->domain.
1340          */
1341         if (irq < legacy_pic->nr_legacy_irqs && cpumask_test_cpu(0, cfg->domain))
1342                 apic->vector_allocation_domain(0, cfg->domain);
1343
1344         if (assign_irq_vector(irq, cfg, apic->target_cpus()))
1345                 return;
1346
1347         dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
1348
1349         apic_printk(APIC_VERBOSE,KERN_DEBUG
1350                     "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1351                     "IRQ %d Mode:%i Active:%i)\n",
1352                     apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
1353                     irq, trigger, polarity);
1354
1355
1356         if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
1357                                dest, trigger, polarity, cfg->vector, pin)) {
1358                 printk("Failed to setup ioapic entry for ioapic  %d, pin %d\n",
1359                        mp_ioapics[apic_id].apicid, pin);
1360                 __clear_irq_vector(irq, cfg);
1361                 return;
1362         }
1363
1364         ioapic_register_intr(irq, trigger);
1365         if (irq < legacy_pic->nr_legacy_irqs)
1366                 legacy_pic->mask(irq);
1367
1368         ioapic_write_entry(apic_id, pin, entry);
1369 }
1370
1371 static struct {
1372         DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
1373 } mp_ioapic_routing[MAX_IO_APICS];
1374
1375 static bool __init io_apic_pin_not_connected(int idx, int apic_id, int pin)
1376 {
1377         if (idx != -1)
1378                 return false;
1379
1380         apic_printk(APIC_VERBOSE, KERN_DEBUG " apic %d pin %d not connected\n",
1381                     mp_ioapics[apic_id].apicid, pin);
1382         return true;
1383 }
1384
1385 static void __init __io_apic_setup_irqs(unsigned int apic_id)
1386 {
1387         int idx, node = cpu_to_node(0);
1388         struct io_apic_irq_attr attr;
1389         unsigned int pin, irq;
1390
1391         for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
1392                 idx = find_irq_entry(apic_id, pin, mp_INT);
1393                 if (io_apic_pin_not_connected(idx, apic_id, pin))
1394                         continue;
1395
1396                 irq = pin_2_irq(idx, apic_id, pin);
1397
1398                 if ((apic_id > 0) && (irq > 16))
1399                         continue;
1400
1401                 /*
1402                  * Skip the timer IRQ if there's a quirk handler
1403                  * installed and if it returns 1:
1404                  */
1405                 if (apic->multi_timer_check &&
1406                     apic->multi_timer_check(apic_id, irq))
1407                         continue;
1408
1409                 set_io_apic_irq_attr(&attr, apic_id, pin, irq_trigger(idx),
1410                                      irq_polarity(idx));
1411
1412                 io_apic_setup_irq_pin(irq, node, &attr);
1413         }
1414 }
1415
1416 static void __init setup_IO_APIC_irqs(void)
1417 {
1418         unsigned int apic_id;
1419
1420         apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1421
1422         for (apic_id = 0; apic_id < nr_ioapics; apic_id++)
1423                 __io_apic_setup_irqs(apic_id);
1424 }
1425
1426 /*
1427  * for the gsit that is not in first ioapic
1428  * but could not use acpi_register_gsi()
1429  * like some special sci in IBM x3330
1430  */
1431 void setup_IO_APIC_irq_extra(u32 gsi)
1432 {
1433         int apic_id = 0, pin, idx, irq, node = cpu_to_node(0);
1434         struct io_apic_irq_attr attr;
1435
1436         /*
1437          * Convert 'gsi' to 'ioapic.pin'.
1438          */
1439         apic_id = mp_find_ioapic(gsi);
1440         if (apic_id < 0)
1441                 return;
1442
1443         pin = mp_find_ioapic_pin(apic_id, gsi);
1444         idx = find_irq_entry(apic_id, pin, mp_INT);
1445         if (idx == -1)
1446                 return;
1447
1448         irq = pin_2_irq(idx, apic_id, pin);
1449
1450         /* Only handle the non legacy irqs on secondary ioapics */
1451         if (apic_id == 0 || irq < NR_IRQS_LEGACY)
1452                 return;
1453
1454         set_io_apic_irq_attr(&attr, apic_id, pin, irq_trigger(idx),
1455                              irq_polarity(idx));
1456
1457         io_apic_setup_irq_pin_once(irq, node, &attr);
1458 }
1459
1460 /*
1461  * Set up the timer pin, possibly with the 8259A-master behind.
1462  */
1463 static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
1464                                         int vector)
1465 {
1466         struct IO_APIC_route_entry entry;
1467
1468         if (intr_remapping_enabled)
1469                 return;
1470
1471         memset(&entry, 0, sizeof(entry));
1472
1473         /*
1474          * We use logical delivery to get the timer IRQ
1475          * to the first CPU.
1476          */
1477         entry.dest_mode = apic->irq_dest_mode;
1478         entry.mask = 0;                 /* don't mask IRQ for edge */
1479         entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
1480         entry.delivery_mode = apic->irq_delivery_mode;
1481         entry.polarity = 0;
1482         entry.trigger = 0;
1483         entry.vector = vector;
1484
1485         /*
1486          * The timer IRQ doesn't have to know that behind the
1487          * scene we may have a 8259A-master in AEOI mode ...
1488          */
1489         irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
1490                                       "edge");
1491
1492         /*
1493          * Add it to the IO-APIC irq-routing table:
1494          */
1495         ioapic_write_entry(apic_id, pin, entry);
1496 }
1497
1498
1499 __apicdebuginit(void) print_IO_APIC(void)
1500 {
1501         int apic, i;
1502         union IO_APIC_reg_00 reg_00;
1503         union IO_APIC_reg_01 reg_01;
1504         union IO_APIC_reg_02 reg_02;
1505         union IO_APIC_reg_03 reg_03;
1506         unsigned long flags;
1507         struct irq_cfg *cfg;
1508         unsigned int irq;
1509
1510         printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1511         for (i = 0; i < nr_ioapics; i++)
1512                 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1513                        mp_ioapics[i].apicid, nr_ioapic_registers[i]);
1514
1515         /*
1516          * We are a bit conservative about what we expect.  We have to
1517          * know about every hardware change ASAP.
1518          */
1519         printk(KERN_INFO "testing the IO APIC.......................\n");
1520
1521         for (apic = 0; apic < nr_ioapics; apic++) {
1522
1523         raw_spin_lock_irqsave(&ioapic_lock, flags);
1524         reg_00.raw = io_apic_read(apic, 0);
1525         reg_01.raw = io_apic_read(apic, 1);
1526         if (reg_01.bits.version >= 0x10)
1527                 reg_02.raw = io_apic_read(apic, 2);
1528         if (reg_01.bits.version >= 0x20)
1529                 reg_03.raw = io_apic_read(apic, 3);
1530         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1531
1532         printk("\n");
1533         printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
1534         printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1535         printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
1536         printk(KERN_DEBUG ".......    : Delivery Type: %X\n", reg_00.bits.delivery_type);
1537         printk(KERN_DEBUG ".......    : LTS          : %X\n", reg_00.bits.LTS);
1538
1539         printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1540         printk(KERN_DEBUG ".......     : max redirection entries: %04X\n", reg_01.bits.entries);
1541
1542         printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
1543         printk(KERN_DEBUG ".......     : IO APIC version: %04X\n", reg_01.bits.version);
1544
1545         /*
1546          * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1547          * but the value of reg_02 is read as the previous read register
1548          * value, so ignore it if reg_02 == reg_01.
1549          */
1550         if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1551                 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1552                 printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
1553         }
1554
1555         /*
1556          * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1557          * or reg_03, but the value of reg_0[23] is read as the previous read
1558          * register value, so ignore it if reg_03 == reg_0[12].
1559          */
1560         if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1561             reg_03.raw != reg_01.raw) {
1562                 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1563                 printk(KERN_DEBUG ".......     : Boot DT    : %X\n", reg_03.bits.boot_DT);
1564         }
1565
1566         printk(KERN_DEBUG ".... IRQ redirection table:\n");
1567
1568         printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1569                           " Stat Dmod Deli Vect:\n");
1570
1571         for (i = 0; i <= reg_01.bits.entries; i++) {
1572                 struct IO_APIC_route_entry entry;
1573
1574                 entry = ioapic_read_entry(apic, i);
1575
1576                 printk(KERN_DEBUG " %02x %03X ",
1577                         i,
1578                         entry.dest
1579                 );
1580
1581                 printk("%1d    %1d    %1d   %1d   %1d    %1d    %1d    %02X\n",
1582                         entry.mask,
1583                         entry.trigger,
1584                         entry.irr,
1585                         entry.polarity,
1586                         entry.delivery_status,
1587                         entry.dest_mode,
1588                         entry.delivery_mode,
1589                         entry.vector
1590                 );
1591         }
1592         }
1593         printk(KERN_DEBUG "IRQ to pin mappings:\n");
1594         for_each_active_irq(irq) {
1595                 struct irq_pin_list *entry;
1596
1597                 cfg = irq_get_chip_data(irq);
1598                 if (!cfg)
1599                         continue;
1600                 entry = cfg->irq_2_pin;
1601                 if (!entry)
1602                         continue;
1603                 printk(KERN_DEBUG "IRQ%d ", irq);
1604                 for_each_irq_pin(entry, cfg->irq_2_pin)
1605                         printk("-> %d:%d", entry->apic, entry->pin);
1606                 printk("\n");
1607         }
1608
1609         printk(KERN_INFO ".................................... done.\n");
1610
1611         return;
1612 }
1613
1614 __apicdebuginit(void) print_APIC_field(int base)
1615 {
1616         int i;
1617
1618         printk(KERN_DEBUG);
1619
1620         for (i = 0; i < 8; i++)
1621                 printk(KERN_CONT "%08x", apic_read(base + i*0x10));
1622
1623         printk(KERN_CONT "\n");
1624 }
1625
1626 __apicdebuginit(void) print_local_APIC(void *dummy)
1627 {
1628         unsigned int i, v, ver, maxlvt;
1629         u64 icr;
1630
1631         printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1632                 smp_processor_id(), hard_smp_processor_id());
1633         v = apic_read(APIC_ID);
1634         printk(KERN_INFO "... APIC ID:      %08x (%01x)\n", v, read_apic_id());
1635         v = apic_read(APIC_LVR);
1636         printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1637         ver = GET_APIC_VERSION(v);
1638         maxlvt = lapic_get_maxlvt();
1639
1640         v = apic_read(APIC_TASKPRI);
1641         printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1642
1643         if (APIC_INTEGRATED(ver)) {                     /* !82489DX */
1644                 if (!APIC_XAPIC(ver)) {
1645                         v = apic_read(APIC_ARBPRI);
1646                         printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1647                                v & APIC_ARBPRI_MASK);
1648                 }
1649                 v = apic_read(APIC_PROCPRI);
1650                 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1651         }
1652
1653         /*
1654          * Remote read supported only in the 82489DX and local APIC for
1655          * Pentium processors.
1656          */
1657         if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1658                 v = apic_read(APIC_RRR);
1659                 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1660         }
1661
1662         v = apic_read(APIC_LDR);
1663         printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1664         if (!x2apic_enabled()) {
1665                 v = apic_read(APIC_DFR);
1666                 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1667         }
1668         v = apic_read(APIC_SPIV);
1669         printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1670
1671         printk(KERN_DEBUG "... APIC ISR field:\n");
1672         print_APIC_field(APIC_ISR);
1673         printk(KERN_DEBUG "... APIC TMR field:\n");
1674         print_APIC_field(APIC_TMR);
1675         printk(KERN_DEBUG "... APIC IRR field:\n");
1676         print_APIC_field(APIC_IRR);
1677
1678         if (APIC_INTEGRATED(ver)) {             /* !82489DX */
1679                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
1680                         apic_write(APIC_ESR, 0);
1681
1682                 v = apic_read(APIC_ESR);
1683                 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1684         }
1685
1686         icr = apic_icr_read();
1687         printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1688         printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
1689
1690         v = apic_read(APIC_LVTT);
1691         printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1692
1693         if (maxlvt > 3) {                       /* PC is LVT#4. */
1694                 v = apic_read(APIC_LVTPC);
1695                 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1696         }
1697         v = apic_read(APIC_LVT0);
1698         printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1699         v = apic_read(APIC_LVT1);
1700         printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1701
1702         if (maxlvt > 2) {                       /* ERR is LVT#3. */
1703                 v = apic_read(APIC_LVTERR);
1704                 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1705         }
1706
1707         v = apic_read(APIC_TMICT);
1708         printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1709         v = apic_read(APIC_TMCCT);
1710         printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1711         v = apic_read(APIC_TDCR);
1712         printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1713
1714         if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1715                 v = apic_read(APIC_EFEAT);
1716                 maxlvt = (v >> 16) & 0xff;
1717                 printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1718                 v = apic_read(APIC_ECTRL);
1719                 printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1720                 for (i = 0; i < maxlvt; i++) {
1721                         v = apic_read(APIC_EILVTn(i));
1722                         printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1723                 }
1724         }
1725         printk("\n");
1726 }
1727
1728 __apicdebuginit(void) print_local_APICs(int maxcpu)
1729 {
1730         int cpu;
1731
1732         if (!maxcpu)
1733                 return;
1734
1735         preempt_disable();
1736         for_each_online_cpu(cpu) {
1737                 if (cpu >= maxcpu)
1738                         break;
1739                 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1740         }
1741         preempt_enable();
1742 }
1743
1744 __apicdebuginit(void) print_PIC(void)
1745 {
1746         unsigned int v;
1747         unsigned long flags;
1748
1749         if (!legacy_pic->nr_legacy_irqs)
1750                 return;
1751
1752         printk(KERN_DEBUG "\nprinting PIC contents\n");
1753
1754         raw_spin_lock_irqsave(&i8259A_lock, flags);
1755
1756         v = inb(0xa1) << 8 | inb(0x21);
1757         printk(KERN_DEBUG "... PIC  IMR: %04x\n", v);
1758
1759         v = inb(0xa0) << 8 | inb(0x20);
1760         printk(KERN_DEBUG "... PIC  IRR: %04x\n", v);
1761
1762         outb(0x0b,0xa0);
1763         outb(0x0b,0x20);
1764         v = inb(0xa0) << 8 | inb(0x20);
1765         outb(0x0a,0xa0);
1766         outb(0x0a,0x20);
1767
1768         raw_spin_unlock_irqrestore(&i8259A_lock, flags);
1769
1770         printk(KERN_DEBUG "... PIC  ISR: %04x\n", v);
1771
1772         v = inb(0x4d1) << 8 | inb(0x4d0);
1773         printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1774 }
1775
1776 static int __initdata show_lapic = 1;
1777 static __init int setup_show_lapic(char *arg)
1778 {
1779         int num = -1;
1780
1781         if (strcmp(arg, "all") == 0) {
1782                 show_lapic = CONFIG_NR_CPUS;
1783         } else {
1784                 get_option(&arg, &num);
1785                 if (num >= 0)
1786                         show_lapic = num;
1787         }
1788
1789         return 1;
1790 }
1791 __setup("show_lapic=", setup_show_lapic);
1792
1793 __apicdebuginit(int) print_ICs(void)
1794 {
1795         if (apic_verbosity == APIC_QUIET)
1796                 return 0;
1797
1798         print_PIC();
1799
1800         /* don't print out if apic is not there */
1801         if (!cpu_has_apic && !apic_from_smp_config())
1802                 return 0;
1803
1804         print_local_APICs(show_lapic);
1805         print_IO_APIC();
1806
1807         return 0;
1808 }
1809
1810 fs_initcall(print_ICs);
1811
1812
1813 /* Where if anywhere is the i8259 connect in external int mode */
1814 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1815
1816 void __init enable_IO_APIC(void)
1817 {
1818         int i8259_apic, i8259_pin;
1819         int apic;
1820
1821         if (!legacy_pic->nr_legacy_irqs)
1822                 return;
1823
1824         for(apic = 0; apic < nr_ioapics; apic++) {
1825                 int pin;
1826                 /* See if any of the pins is in ExtINT mode */
1827                 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1828                         struct IO_APIC_route_entry entry;
1829                         entry = ioapic_read_entry(apic, pin);
1830
1831                         /* If the interrupt line is enabled and in ExtInt mode
1832                          * I have found the pin where the i8259 is connected.
1833                          */
1834                         if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1835                                 ioapic_i8259.apic = apic;
1836                                 ioapic_i8259.pin  = pin;
1837                                 goto found_i8259;
1838                         }
1839                 }
1840         }
1841  found_i8259:
1842         /* Look to see what if the MP table has reported the ExtINT */
1843         /* If we could not find the appropriate pin by looking at the ioapic
1844          * the i8259 probably is not connected the ioapic but give the
1845          * mptable a chance anyway.
1846          */
1847         i8259_pin  = find_isa_irq_pin(0, mp_ExtINT);
1848         i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1849         /* Trust the MP table if nothing is setup in the hardware */
1850         if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1851                 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1852                 ioapic_i8259.pin  = i8259_pin;
1853                 ioapic_i8259.apic = i8259_apic;
1854         }
1855         /* Complain if the MP table and the hardware disagree */
1856         if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1857                 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1858         {
1859                 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1860         }
1861
1862         /*
1863          * Do not trust the IO-APIC being empty at bootup
1864          */
1865         clear_IO_APIC();
1866 }
1867
1868 /*
1869  * Not an __init, needed by the reboot code
1870  */
1871 void disable_IO_APIC(void)
1872 {
1873         /*
1874          * Clear the IO-APIC before rebooting:
1875          */
1876         clear_IO_APIC();
1877
1878         if (!legacy_pic->nr_legacy_irqs)
1879                 return;
1880
1881         /*
1882          * If the i8259 is routed through an IOAPIC
1883          * Put that IOAPIC in virtual wire mode
1884          * so legacy interrupts can be delivered.
1885          *
1886          * With interrupt-remapping, for now we will use virtual wire A mode,
1887          * as virtual wire B is little complex (need to configure both
1888          * IOAPIC RTE aswell as interrupt-remapping table entry).
1889          * As this gets called during crash dump, keep this simple for now.
1890          */
1891         if (ioapic_i8259.pin != -1 && !intr_remapping_enabled) {
1892                 struct IO_APIC_route_entry entry;
1893
1894                 memset(&entry, 0, sizeof(entry));
1895                 entry.mask            = 0; /* Enabled */
1896                 entry.trigger         = 0; /* Edge */
1897                 entry.irr             = 0;
1898                 entry.polarity        = 0; /* High */
1899                 entry.delivery_status = 0;
1900                 entry.dest_mode       = 0; /* Physical */
1901                 entry.delivery_mode   = dest_ExtINT; /* ExtInt */
1902                 entry.vector          = 0;
1903                 entry.dest            = read_apic_id();
1904
1905                 /*
1906                  * Add it to the IO-APIC irq-routing table:
1907                  */
1908                 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1909         }
1910
1911         /*
1912          * Use virtual wire A mode when interrupt remapping is enabled.
1913          */
1914         if (cpu_has_apic || apic_from_smp_config())
1915                 disconnect_bsp_APIC(!intr_remapping_enabled &&
1916                                 ioapic_i8259.pin != -1);
1917 }
1918
1919 #ifdef CONFIG_X86_32
1920 /*
1921  * function to set the IO-APIC physical IDs based on the
1922  * values stored in the MPC table.
1923  *
1924  * by Matt Domsch <Matt_Domsch@dell.com>  Tue Dec 21 12:25:05 CST 1999
1925  */
1926 void __init setup_ioapic_ids_from_mpc_nocheck(void)
1927 {
1928         union IO_APIC_reg_00 reg_00;
1929         physid_mask_t phys_id_present_map;
1930         int apic_id;
1931         int i;
1932         unsigned char old_id;
1933         unsigned long flags;
1934
1935         /*
1936          * This is broken; anything with a real cpu count has to
1937          * circumvent this idiocy regardless.
1938          */
1939         apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
1940
1941         /*
1942          * Set the IOAPIC ID to the value stored in the MPC table.
1943          */
1944         for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
1945
1946                 /* Read the register 0 value */
1947                 raw_spin_lock_irqsave(&ioapic_lock, flags);
1948                 reg_00.raw = io_apic_read(apic_id, 0);
1949                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1950
1951                 old_id = mp_ioapics[apic_id].apicid;
1952
1953                 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
1954                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1955                                 apic_id, mp_ioapics[apic_id].apicid);
1956                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1957                                 reg_00.bits.ID);
1958                         mp_ioapics[apic_id].apicid = reg_00.bits.ID;
1959                 }
1960
1961                 /*
1962                  * Sanity check, is the ID really free? Every APIC in a
1963                  * system must have a unique ID or we get lots of nice
1964                  * 'stuck on smp_invalidate_needed IPI wait' messages.
1965                  */
1966                 if (apic->check_apicid_used(&phys_id_present_map,
1967                                         mp_ioapics[apic_id].apicid)) {
1968                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1969                                 apic_id, mp_ioapics[apic_id].apicid);
1970                         for (i = 0; i < get_physical_broadcast(); i++)
1971                                 if (!physid_isset(i, phys_id_present_map))
1972                                         break;
1973                         if (i >= get_physical_broadcast())
1974                                 panic("Max APIC ID exceeded!\n");
1975                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1976                                 i);
1977                         physid_set(i, phys_id_present_map);
1978                         mp_ioapics[apic_id].apicid = i;
1979                 } else {
1980                         physid_mask_t tmp;
1981                         apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid, &tmp);
1982                         apic_printk(APIC_VERBOSE, "Setting %d in the "
1983                                         "phys_id_present_map\n",
1984                                         mp_ioapics[apic_id].apicid);
1985                         physids_or(phys_id_present_map, phys_id_present_map, tmp);
1986                 }
1987
1988                 /*
1989                  * We need to adjust the IRQ routing table
1990                  * if the ID changed.
1991                  */
1992                 if (old_id != mp_ioapics[apic_id].apicid)
1993                         for (i = 0; i < mp_irq_entries; i++)
1994                                 if (mp_irqs[i].dstapic == old_id)
1995                                         mp_irqs[i].dstapic
1996                                                 = mp_ioapics[apic_id].apicid;
1997
1998                 /*
1999                  * Update the ID register according to the right value
2000                  * from the MPC table if they are different.
2001                  */
2002                 if (mp_ioapics[apic_id].apicid == reg_00.bits.ID)
2003                         continue;
2004
2005                 apic_printk(APIC_VERBOSE, KERN_INFO
2006                         "...changing IO-APIC physical APIC ID to %d ...",
2007                         mp_ioapics[apic_id].apicid);
2008
2009                 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
2010                 raw_spin_lock_irqsave(&ioapic_lock, flags);
2011                 io_apic_write(apic_id, 0, reg_00.raw);
2012                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2013
2014                 /*
2015                  * Sanity check
2016                  */
2017                 raw_spin_lock_irqsave(&ioapic_lock, flags);
2018                 reg_00.raw = io_apic_read(apic_id, 0);
2019                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2020                 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
2021                         printk("could not set ID!\n");
2022                 else
2023                         apic_printk(APIC_VERBOSE, " ok.\n");
2024         }
2025 }
2026
2027 void __init setup_ioapic_ids_from_mpc(void)
2028 {
2029
2030         if (acpi_ioapic)
2031                 return;
2032         /*
2033          * Don't check I/O APIC IDs for xAPIC systems.  They have
2034          * no meaning without the serial APIC bus.
2035          */
2036         if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2037                 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
2038                 return;
2039         setup_ioapic_ids_from_mpc_nocheck();
2040 }
2041 #endif
2042
2043 int no_timer_check __initdata;
2044
2045 static int __init notimercheck(char *s)
2046 {
2047         no_timer_check = 1;
2048         return 1;
2049 }
2050 __setup("no_timer_check", notimercheck);
2051
2052 /*
2053  * There is a nasty bug in some older SMP boards, their mptable lies
2054  * about the timer IRQ. We do the following to work around the situation:
2055  *
2056  *      - timer IRQ defaults to IO-APIC IRQ
2057  *      - if this function detects that timer IRQs are defunct, then we fall
2058  *        back to ISA timer IRQs
2059  */
2060 static int __init timer_irq_works(void)
2061 {
2062         unsigned long t1 = jiffies;
2063         unsigned long flags;
2064
2065         if (no_timer_check)
2066                 return 1;
2067
2068         local_save_flags(flags);
2069         local_irq_enable();
2070         /* Let ten ticks pass... */
2071         mdelay((10 * 1000) / HZ);
2072         local_irq_restore(flags);
2073
2074         /*
2075          * Expect a few ticks at least, to be sure some possible
2076          * glue logic does not lock up after one or two first
2077          * ticks in a non-ExtINT mode.  Also the local APIC
2078          * might have cached one ExtINT interrupt.  Finally, at
2079          * least one tick may be lost due to delays.
2080          */
2081
2082         /* jiffies wrap? */
2083         if (time_after(jiffies, t1 + 4))
2084                 return 1;
2085         return 0;
2086 }
2087
2088 /*
2089  * In the SMP+IOAPIC case it might happen that there are an unspecified
2090  * number of pending IRQ events unhandled. These cases are very rare,
2091  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2092  * better to do it this way as thus we do not have to be aware of
2093  * 'pending' interrupts in the IRQ path, except at this point.
2094  */
2095 /*
2096  * Edge triggered needs to resend any interrupt
2097  * that was delayed but this is now handled in the device
2098  * independent code.
2099  */
2100
2101 /*
2102  * Starting up a edge-triggered IO-APIC interrupt is
2103  * nasty - we need to make sure that we get the edge.
2104  * If it is already asserted for some reason, we need
2105  * return 1 to indicate that is was pending.
2106  *
2107  * This is not complete - we should be able to fake
2108  * an edge even if it isn't on the 8259A...
2109  */
2110
2111 static unsigned int startup_ioapic_irq(struct irq_data *data)
2112 {
2113         int was_pending = 0, irq = data->irq;
2114         unsigned long flags;
2115
2116         raw_spin_lock_irqsave(&ioapic_lock, flags);
2117         if (irq < legacy_pic->nr_legacy_irqs) {
2118                 legacy_pic->mask(irq);
2119                 if (legacy_pic->irq_pending(irq))
2120                         was_pending = 1;
2121         }
2122         __unmask_ioapic(data->chip_data);
2123         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2124
2125         return was_pending;
2126 }
2127
2128 static int ioapic_retrigger_irq(struct irq_data *data)
2129 {
2130         struct irq_cfg *cfg = data->chip_data;
2131         unsigned long flags;
2132
2133         raw_spin_lock_irqsave(&vector_lock, flags);
2134         apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
2135         raw_spin_unlock_irqrestore(&vector_lock, flags);
2136
2137         return 1;
2138 }
2139
2140 /*
2141  * Level and edge triggered IO-APIC interrupts need different handling,
2142  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2143  * handled with the level-triggered descriptor, but that one has slightly
2144  * more overhead. Level-triggered interrupts cannot be handled with the
2145  * edge-triggered handler, without risking IRQ storms and other ugly
2146  * races.
2147  */
2148
2149 #ifdef CONFIG_SMP
2150 void send_cleanup_vector(struct irq_cfg *cfg)
2151 {
2152         cpumask_var_t cleanup_mask;
2153
2154         if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
2155                 unsigned int i;
2156                 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2157                         apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2158         } else {
2159                 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
2160                 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2161                 free_cpumask_var(cleanup_mask);
2162         }
2163         cfg->move_in_progress = 0;
2164 }
2165
2166 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
2167 {
2168         int apic, pin;
2169         struct irq_pin_list *entry;
2170         u8 vector = cfg->vector;
2171
2172         for_each_irq_pin(entry, cfg->irq_2_pin) {
2173                 unsigned int reg;
2174
2175                 apic = entry->apic;
2176                 pin = entry->pin;
2177                 /*
2178                  * With interrupt-remapping, destination information comes
2179                  * from interrupt-remapping table entry.
2180                  */
2181                 if (!irq_remapped(cfg))
2182                         io_apic_write(apic, 0x11 + pin*2, dest);
2183                 reg = io_apic_read(apic, 0x10 + pin*2);
2184                 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2185                 reg |= vector;
2186                 io_apic_modify(apic, 0x10 + pin*2, reg);
2187         }
2188 }
2189
2190 /*
2191  * Either sets data->affinity to a valid value, and returns
2192  * ->cpu_mask_to_apicid of that in dest_id, or returns -1 and
2193  * leaves data->affinity untouched.
2194  */
2195 int __ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2196                           unsigned int *dest_id)
2197 {
2198         struct irq_cfg *cfg = data->chip_data;
2199
2200         if (!cpumask_intersects(mask, cpu_online_mask))
2201                 return -1;
2202
2203         if (assign_irq_vector(data->irq, data->chip_data, mask))
2204                 return -1;
2205
2206         cpumask_copy(data->affinity, mask);
2207
2208         *dest_id = apic->cpu_mask_to_apicid_and(mask, cfg->domain);
2209         return 0;
2210 }
2211
2212 static int
2213 ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2214                     bool force)
2215 {
2216         unsigned int dest, irq = data->irq;
2217         unsigned long flags;
2218         int ret;
2219
2220         raw_spin_lock_irqsave(&ioapic_lock, flags);
2221         ret = __ioapic_set_affinity(data, mask, &dest);
2222         if (!ret) {
2223                 /* Only the high 8 bits are valid. */
2224                 dest = SET_APIC_LOGICAL_ID(dest);
2225                 __target_IO_APIC_irq(irq, dest, data->chip_data);
2226         }
2227         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2228         return ret;
2229 }
2230
2231 #ifdef CONFIG_INTR_REMAP
2232
2233 /*
2234  * Migrate the IO-APIC irq in the presence of intr-remapping.
2235  *
2236  * For both level and edge triggered, irq migration is a simple atomic
2237  * update(of vector and cpu destination) of IRTE and flush the hardware cache.
2238  *
2239  * For level triggered, we eliminate the io-apic RTE modification (with the
2240  * updated vector information), by using a virtual vector (io-apic pin number).
2241  * Real vector that is used for interrupting cpu will be coming from
2242  * the interrupt-remapping table entry.
2243  */
2244 static int
2245 ir_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2246                        bool force)
2247 {
2248         struct irq_cfg *cfg = data->chip_data;
2249         unsigned int dest, irq = data->irq;
2250         struct irte irte;
2251
2252         if (!cpumask_intersects(mask, cpu_online_mask))
2253                 return -EINVAL;
2254
2255         if (get_irte(irq, &irte))
2256                 return -EBUSY;
2257
2258         if (assign_irq_vector(irq, cfg, mask))
2259                 return -EBUSY;
2260
2261         dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
2262
2263         irte.vector = cfg->vector;
2264         irte.dest_id = IRTE_DEST(dest);
2265
2266         /*
2267          * Modified the IRTE and flushes the Interrupt entry cache.
2268          */
2269         modify_irte(irq, &irte);
2270
2271         if (cfg->move_in_progress)
2272                 send_cleanup_vector(cfg);
2273
2274         cpumask_copy(data->affinity, mask);
2275         return 0;
2276 }
2277
2278 #else
2279 static inline int
2280 ir_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2281                        bool force)
2282 {
2283         return 0;
2284 }
2285 #endif
2286
2287 asmlinkage void smp_irq_move_cleanup_interrupt(void)
2288 {
2289         unsigned vector, me;
2290
2291         ack_APIC_irq();
2292         exit_idle();
2293         irq_enter();
2294
2295         me = smp_processor_id();
2296         for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2297                 unsigned int irq;
2298                 unsigned int irr;
2299                 struct irq_desc *desc;
2300                 struct irq_cfg *cfg;
2301                 irq = __this_cpu_read(vector_irq[vector]);
2302
2303                 if (irq == -1)
2304                         continue;
2305
2306                 desc = irq_to_desc(irq);
2307                 if (!desc)
2308                         continue;
2309
2310                 cfg = irq_cfg(irq);
2311                 raw_spin_lock(&desc->lock);
2312
2313                 /*
2314                  * Check if the irq migration is in progress. If so, we
2315                  * haven't received the cleanup request yet for this irq.
2316                  */
2317                 if (cfg->move_in_progress)
2318                         goto unlock;
2319
2320                 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2321                         goto unlock;
2322
2323                 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2324                 /*
2325                  * Check if the vector that needs to be cleanedup is
2326                  * registered at the cpu's IRR. If so, then this is not
2327                  * the best time to clean it up. Lets clean it up in the
2328                  * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2329                  * to myself.
2330                  */
2331                 if (irr  & (1 << (vector % 32))) {
2332                         apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2333                         goto unlock;
2334                 }
2335                 __this_cpu_write(vector_irq[vector], -1);
2336 unlock:
2337                 raw_spin_unlock(&desc->lock);
2338         }
2339
2340         irq_exit();
2341 }
2342
2343 static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
2344 {
2345         unsigned me;
2346
2347         if (likely(!cfg->move_in_progress))
2348                 return;
2349
2350         me = smp_processor_id();
2351
2352         if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2353                 send_cleanup_vector(cfg);
2354 }
2355
2356 static void irq_complete_move(struct irq_cfg *cfg)
2357 {
2358         __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
2359 }
2360
2361 void irq_force_complete_move(int irq)
2362 {
2363         struct irq_cfg *cfg = irq_get_chip_data(irq);
2364
2365         if (!cfg)
2366                 return;
2367
2368         __irq_complete_move(cfg, cfg->vector);
2369 }
2370 #else
2371 static inline void irq_complete_move(struct irq_cfg *cfg) { }
2372 #endif
2373
2374 static void ack_apic_edge(struct irq_data *data)
2375 {
2376         irq_complete_move(data->chip_data);
2377         move_native_irq(data->irq);
2378         ack_APIC_irq();
2379 }
2380
2381 atomic_t irq_mis_count;
2382
2383 /*
2384  * IO-APIC versions below 0x20 don't support EOI register.
2385  * For the record, here is the information about various versions:
2386  *     0Xh     82489DX
2387  *     1Xh     I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
2388  *     2Xh     I/O(x)APIC which is PCI 2.2 Compliant
2389  *     30h-FFh Reserved
2390  *
2391  * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
2392  * version as 0x2. This is an error with documentation and these ICH chips
2393  * use io-apic's of version 0x20.
2394  *
2395  * For IO-APIC's with EOI register, we use that to do an explicit EOI.
2396  * Otherwise, we simulate the EOI message manually by changing the trigger
2397  * mode to edge and then back to level, with RTE being masked during this.
2398 */
2399 static void eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2400 {
2401         struct irq_pin_list *entry;
2402         unsigned long flags;
2403
2404         raw_spin_lock_irqsave(&ioapic_lock, flags);
2405         for_each_irq_pin(entry, cfg->irq_2_pin) {
2406                 if (mp_ioapics[entry->apic].apicver >= 0x20) {
2407                         /*
2408                          * Intr-remapping uses pin number as the virtual vector
2409                          * in the RTE. Actual vector is programmed in
2410                          * intr-remapping table entry. Hence for the io-apic
2411                          * EOI we use the pin number.
2412                          */
2413                         if (irq_remapped(cfg))
2414                                 io_apic_eoi(entry->apic, entry->pin);
2415                         else
2416                                 io_apic_eoi(entry->apic, cfg->vector);
2417                 } else {
2418                         __mask_and_edge_IO_APIC_irq(entry);
2419                         __unmask_and_level_IO_APIC_irq(entry);
2420                 }
2421         }
2422         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2423 }
2424
2425 static void ack_apic_level(struct irq_data *data)
2426 {
2427         struct irq_cfg *cfg = data->chip_data;
2428         int i, do_unmask_irq = 0, irq = data->irq;
2429         unsigned long v;
2430
2431         irq_complete_move(cfg);
2432 #ifdef CONFIG_GENERIC_PENDING_IRQ
2433         /* If we are moving the irq we need to mask it */
2434         if (unlikely(irqd_is_setaffinity_pending(data))) {
2435                 do_unmask_irq = 1;
2436                 mask_ioapic(cfg);
2437         }
2438 #endif
2439
2440         /*
2441          * It appears there is an erratum which affects at least version 0x11
2442          * of I/O APIC (that's the 82093AA and cores integrated into various
2443          * chipsets).  Under certain conditions a level-triggered interrupt is
2444          * erroneously delivered as edge-triggered one but the respective IRR
2445          * bit gets set nevertheless.  As a result the I/O unit expects an EOI
2446          * message but it will never arrive and further interrupts are blocked
2447          * from the source.  The exact reason is so far unknown, but the
2448          * phenomenon was observed when two consecutive interrupt requests
2449          * from a given source get delivered to the same CPU and the source is
2450          * temporarily disabled in between.
2451          *
2452          * A workaround is to simulate an EOI message manually.  We achieve it
2453          * by setting the trigger mode to edge and then to level when the edge
2454          * trigger mode gets detected in the TMR of a local APIC for a
2455          * level-triggered interrupt.  We mask the source for the time of the
2456          * operation to prevent an edge-triggered interrupt escaping meanwhile.
2457          * The idea is from Manfred Spraul.  --macro
2458          *
2459          * Also in the case when cpu goes offline, fixup_irqs() will forward
2460          * any unhandled interrupt on the offlined cpu to the new cpu
2461          * destination that is handling the corresponding interrupt. This
2462          * interrupt forwarding is done via IPI's. Hence, in this case also
2463          * level-triggered io-apic interrupt will be seen as an edge
2464          * interrupt in the IRR. And we can't rely on the cpu's EOI
2465          * to be broadcasted to the IO-APIC's which will clear the remoteIRR
2466          * corresponding to the level-triggered interrupt. Hence on IO-APIC's
2467          * supporting EOI register, we do an explicit EOI to clear the
2468          * remote IRR and on IO-APIC's which don't have an EOI register,
2469          * we use the above logic (mask+edge followed by unmask+level) from
2470          * Manfred Spraul to clear the remote IRR.
2471          */
2472         i = cfg->vector;
2473         v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2474
2475         /*
2476          * We must acknowledge the irq before we move it or the acknowledge will
2477          * not propagate properly.
2478          */
2479         ack_APIC_irq();
2480
2481         /*
2482          * Tail end of clearing remote IRR bit (either by delivering the EOI
2483          * message via io-apic EOI register write or simulating it using
2484          * mask+edge followed by unnask+level logic) manually when the
2485          * level triggered interrupt is seen as the edge triggered interrupt
2486          * at the cpu.
2487          */
2488         if (!(v & (1 << (i & 0x1f)))) {
2489                 atomic_inc(&irq_mis_count);
2490
2491                 eoi_ioapic_irq(irq, cfg);
2492         }
2493
2494         /* Now we can move and renable the irq */
2495         if (unlikely(do_unmask_irq)) {
2496                 /* Only migrate the irq if the ack has been received.
2497                  *
2498                  * On rare occasions the broadcast level triggered ack gets
2499                  * delayed going to ioapics, and if we reprogram the
2500                  * vector while Remote IRR is still set the irq will never
2501                  * fire again.
2502                  *
2503                  * To prevent this scenario we read the Remote IRR bit
2504                  * of the ioapic.  This has two effects.
2505                  * - On any sane system the read of the ioapic will
2506                  *   flush writes (and acks) going to the ioapic from
2507                  *   this cpu.
2508                  * - We get to see if the ACK has actually been delivered.
2509                  *
2510                  * Based on failed experiments of reprogramming the
2511                  * ioapic entry from outside of irq context starting
2512                  * with masking the ioapic entry and then polling until
2513                  * Remote IRR was clear before reprogramming the
2514                  * ioapic I don't trust the Remote IRR bit to be
2515                  * completey accurate.
2516                  *
2517                  * However there appears to be no other way to plug
2518                  * this race, so if the Remote IRR bit is not
2519                  * accurate and is causing problems then it is a hardware bug
2520                  * and you can go talk to the chipset vendor about it.
2521                  */
2522                 if (!io_apic_level_ack_pending(cfg))
2523                         move_masked_irq(irq);
2524                 unmask_ioapic(cfg);
2525         }
2526 }
2527
2528 #ifdef CONFIG_INTR_REMAP
2529 static void ir_ack_apic_edge(struct irq_data *data)
2530 {
2531         ack_APIC_irq();
2532 }
2533
2534 static void ir_ack_apic_level(struct irq_data *data)
2535 {
2536         ack_APIC_irq();
2537         eoi_ioapic_irq(data->irq, data->chip_data);
2538 }
2539 #endif /* CONFIG_INTR_REMAP */
2540
2541 static struct irq_chip ioapic_chip __read_mostly = {
2542         .name                   = "IO-APIC",
2543         .irq_startup            = startup_ioapic_irq,
2544         .irq_mask               = mask_ioapic_irq,
2545         .irq_unmask             = unmask_ioapic_irq,
2546         .irq_ack                = ack_apic_edge,
2547         .irq_eoi                = ack_apic_level,
2548 #ifdef CONFIG_SMP
2549         .irq_set_affinity       = ioapic_set_affinity,
2550 #endif
2551         .irq_retrigger          = ioapic_retrigger_irq,
2552 };
2553
2554 static struct irq_chip ir_ioapic_chip __read_mostly = {
2555         .name                   = "IR-IO-APIC",
2556         .irq_startup            = startup_ioapic_irq,
2557         .irq_mask               = mask_ioapic_irq,
2558         .irq_unmask             = unmask_ioapic_irq,
2559 #ifdef CONFIG_INTR_REMAP
2560         .irq_ack                = ir_ack_apic_edge,
2561         .irq_eoi                = ir_ack_apic_level,
2562 #ifdef CONFIG_SMP
2563         .irq_set_affinity       = ir_ioapic_set_affinity,
2564 #endif
2565 #endif
2566         .irq_retrigger          = ioapic_retrigger_irq,
2567 };
2568
2569 static inline void init_IO_APIC_traps(void)
2570 {
2571         struct irq_cfg *cfg;
2572         unsigned int irq;
2573
2574         /*
2575          * NOTE! The local APIC isn't very good at handling
2576          * multiple interrupts at the same interrupt level.
2577          * As the interrupt level is determined by taking the
2578          * vector number and shifting that right by 4, we
2579          * want to spread these out a bit so that they don't
2580          * all fall in the same interrupt level.
2581          *
2582          * Also, we've got to be careful not to trash gate
2583          * 0x80, because int 0x80 is hm, kind of importantish. ;)
2584          */
2585         for_each_active_irq(irq) {
2586                 cfg = irq_get_chip_data(irq);
2587                 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
2588                         /*
2589                          * Hmm.. We don't have an entry for this,
2590                          * so default to an old-fashioned 8259
2591                          * interrupt if we can..
2592                          */
2593                         if (irq < legacy_pic->nr_legacy_irqs)
2594                                 legacy_pic->make_irq(irq);
2595                         else
2596                                 /* Strange. Oh, well.. */
2597                                 irq_set_chip(irq, &no_irq_chip);
2598                 }
2599         }
2600 }
2601
2602 /*
2603  * The local APIC irq-chip implementation:
2604  */
2605
2606 static void mask_lapic_irq(struct irq_data *data)
2607 {
2608         unsigned long v;
2609
2610         v = apic_read(APIC_LVT0);
2611         apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2612 }
2613
2614 static void unmask_lapic_irq(struct irq_data *data)
2615 {
2616         unsigned long v;
2617
2618         v = apic_read(APIC_LVT0);
2619         apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2620 }
2621
2622 static void ack_lapic_irq(struct irq_data *data)
2623 {
2624         ack_APIC_irq();
2625 }
2626
2627 static struct irq_chip lapic_chip __read_mostly = {
2628         .name           = "local-APIC",
2629         .irq_mask       = mask_lapic_irq,
2630         .irq_unmask     = unmask_lapic_irq,
2631         .irq_ack        = ack_lapic_irq,
2632 };
2633
2634 static void lapic_register_intr(int irq)
2635 {
2636         irq_clear_status_flags(irq, IRQ_LEVEL);
2637         irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2638                                       "edge");
2639 }
2640
2641 /*
2642  * This looks a bit hackish but it's about the only one way of sending
2643  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
2644  * not support the ExtINT mode, unfortunately.  We need to send these
2645  * cycles as some i82489DX-based boards have glue logic that keeps the
2646  * 8259A interrupt line asserted until INTA.  --macro
2647  */
2648 static inline void __init unlock_ExtINT_logic(void)
2649 {
2650         int apic, pin, i;
2651         struct IO_APIC_route_entry entry0, entry1;
2652         unsigned char save_control, save_freq_select;
2653
2654         pin  = find_isa_irq_pin(8, mp_INT);
2655         if (pin == -1) {
2656                 WARN_ON_ONCE(1);
2657                 return;
2658         }
2659         apic = find_isa_irq_apic(8, mp_INT);
2660         if (apic == -1) {
2661                 WARN_ON_ONCE(1);
2662                 return;
2663         }
2664
2665         entry0 = ioapic_read_entry(apic, pin);
2666         clear_IO_APIC_pin(apic, pin);
2667
2668         memset(&entry1, 0, sizeof(entry1));
2669
2670         entry1.dest_mode = 0;                   /* physical delivery */
2671         entry1.mask = 0;                        /* unmask IRQ now */
2672         entry1.dest = hard_smp_processor_id();
2673         entry1.delivery_mode = dest_ExtINT;
2674         entry1.polarity = entry0.polarity;
2675         entry1.trigger = 0;
2676         entry1.vector = 0;
2677
2678         ioapic_write_entry(apic, pin, entry1);
2679
2680         save_control = CMOS_READ(RTC_CONTROL);
2681         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2682         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2683                    RTC_FREQ_SELECT);
2684         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2685
2686         i = 100;
2687         while (i-- > 0) {
2688                 mdelay(10);
2689                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2690                         i -= 10;
2691         }
2692
2693         CMOS_WRITE(save_control, RTC_CONTROL);
2694         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2695         clear_IO_APIC_pin(apic, pin);
2696
2697         ioapic_write_entry(apic, pin, entry0);
2698 }
2699
2700 static int disable_timer_pin_1 __initdata;
2701 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
2702 static int __init disable_timer_pin_setup(char *arg)
2703 {
2704         disable_timer_pin_1 = 1;
2705         return 0;
2706 }
2707 early_param("disable_timer_pin_1", disable_timer_pin_setup);
2708
2709 int timer_through_8259 __initdata;
2710
2711 /*
2712  * This code may look a bit paranoid, but it's supposed to cooperate with
2713  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
2714  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
2715  * fanatically on his truly buggy board.
2716  *
2717  * FIXME: really need to revamp this for all platforms.
2718  */
2719 static inline void __init check_timer(void)
2720 {
2721         struct irq_cfg *cfg = irq_get_chip_data(0);
2722         int node = cpu_to_node(0);
2723         int apic1, pin1, apic2, pin2;
2724         unsigned long flags;
2725         int no_pin1 = 0;
2726
2727         local_irq_save(flags);
2728
2729         /*
2730          * get/set the timer IRQ vector:
2731          */
2732         legacy_pic->mask(0);
2733         assign_irq_vector(0, cfg, apic->target_cpus());
2734
2735         /*
2736          * As IRQ0 is to be enabled in the 8259A, the virtual
2737          * wire has to be disabled in the local APIC.  Also
2738          * timer interrupts need to be acknowledged manually in
2739          * the 8259A for the i82489DX when using the NMI
2740          * watchdog as that APIC treats NMIs as level-triggered.
2741          * The AEOI mode will finish them in the 8259A
2742          * automatically.
2743          */
2744         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2745         legacy_pic->init(1);
2746
2747         pin1  = find_isa_irq_pin(0, mp_INT);
2748         apic1 = find_isa_irq_apic(0, mp_INT);
2749         pin2  = ioapic_i8259.pin;
2750         apic2 = ioapic_i8259.apic;
2751
2752         apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2753                     "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2754                     cfg->vector, apic1, pin1, apic2, pin2);
2755
2756         /*
2757          * Some BIOS writers are clueless and report the ExtINTA
2758          * I/O APIC input from the cascaded 8259A as the timer
2759          * interrupt input.  So just in case, if only one pin
2760          * was found above, try it both directly and through the
2761          * 8259A.
2762          */
2763         if (pin1 == -1) {
2764                 if (intr_remapping_enabled)
2765                         panic("BIOS bug: timer not connected to IO-APIC");
2766                 pin1 = pin2;
2767                 apic1 = apic2;
2768                 no_pin1 = 1;
2769         } else if (pin2 == -1) {
2770                 pin2 = pin1;
2771                 apic2 = apic1;
2772         }
2773
2774         if (pin1 != -1) {
2775                 /*
2776                  * Ok, does IRQ0 through the IOAPIC work?
2777                  */
2778                 if (no_pin1) {
2779                         add_pin_to_irq_node(cfg, node, apic1, pin1);
2780                         setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2781                 } else {
2782                         /* for edge trigger, setup_ioapic_irq already
2783                          * leave it unmasked.
2784                          * so only need to unmask if it is level-trigger
2785                          * do we really have level trigger timer?
2786                          */
2787                         int idx;
2788                         idx = find_irq_entry(apic1, pin1, mp_INT);
2789                         if (idx != -1 && irq_trigger(idx))
2790                                 unmask_ioapic(cfg);
2791                 }
2792                 if (timer_irq_works()) {
2793                         if (disable_timer_pin_1 > 0)
2794                                 clear_IO_APIC_pin(0, pin1);
2795                         goto out;
2796                 }
2797                 if (intr_remapping_enabled)
2798                         panic("timer doesn't work through Interrupt-remapped IO-APIC");
2799                 local_irq_disable();
2800                 clear_IO_APIC_pin(apic1, pin1);
2801                 if (!no_pin1)
2802                         apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2803                                     "8254 timer not connected to IO-APIC\n");
2804
2805                 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2806                             "(IRQ0) through the 8259A ...\n");
2807                 apic_printk(APIC_QUIET, KERN_INFO
2808                             "..... (found apic %d pin %d) ...\n", apic2, pin2);
2809                 /*
2810                  * legacy devices should be connected to IO APIC #0
2811                  */
2812                 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
2813                 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2814                 legacy_pic->unmask(0);
2815                 if (timer_irq_works()) {
2816                         apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2817                         timer_through_8259 = 1;
2818                         goto out;
2819                 }
2820                 /*
2821                  * Cleanup, just in case ...
2822                  */
2823                 local_irq_disable();
2824                 legacy_pic->mask(0);
2825                 clear_IO_APIC_pin(apic2, pin2);
2826                 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2827         }
2828
2829         apic_printk(APIC_QUIET, KERN_INFO
2830                     "...trying to set up timer as Virtual Wire IRQ...\n");
2831
2832         lapic_register_intr(0);
2833         apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector);     /* Fixed mode */
2834         legacy_pic->unmask(0);
2835
2836         if (timer_irq_works()) {
2837                 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2838                 goto out;
2839         }
2840         local_irq_disable();
2841         legacy_pic->mask(0);
2842         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2843         apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2844
2845         apic_printk(APIC_QUIET, KERN_INFO
2846                     "...trying to set up timer as ExtINT IRQ...\n");
2847
2848         legacy_pic->init(0);
2849         legacy_pic->make_irq(0);
2850         apic_write(APIC_LVT0, APIC_DM_EXTINT);
2851
2852         unlock_ExtINT_logic();
2853
2854         if (timer_irq_works()) {
2855                 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2856                 goto out;
2857         }
2858         local_irq_disable();
2859         apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2860         panic("IO-APIC + timer doesn't work!  Boot with apic=debug and send a "
2861                 "report.  Then try booting with the 'noapic' option.\n");
2862 out:
2863         local_irq_restore(flags);
2864 }
2865
2866 /*
2867  * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2868  * to devices.  However there may be an I/O APIC pin available for
2869  * this interrupt regardless.  The pin may be left unconnected, but
2870  * typically it will be reused as an ExtINT cascade interrupt for
2871  * the master 8259A.  In the MPS case such a pin will normally be
2872  * reported as an ExtINT interrupt in the MP table.  With ACPI
2873  * there is no provision for ExtINT interrupts, and in the absence
2874  * of an override it would be treated as an ordinary ISA I/O APIC
2875  * interrupt, that is edge-triggered and unmasked by default.  We
2876  * used to do this, but it caused problems on some systems because
2877  * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2878  * the same ExtINT cascade interrupt to drive the local APIC of the
2879  * bootstrap processor.  Therefore we refrain from routing IRQ2 to
2880  * the I/O APIC in all cases now.  No actual device should request
2881  * it anyway.  --macro
2882  */
2883 #define PIC_IRQS        (1UL << PIC_CASCADE_IR)
2884
2885 void __init setup_IO_APIC(void)
2886 {
2887
2888         /*
2889          * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2890          */
2891         io_apic_irqs = legacy_pic->nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
2892
2893         apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2894         /*
2895          * Set up IO-APIC IRQ routing.
2896          */
2897         x86_init.mpparse.setup_ioapic_ids();
2898
2899         sync_Arb_IDs();
2900         setup_IO_APIC_irqs();
2901         init_IO_APIC_traps();
2902         if (legacy_pic->nr_legacy_irqs)
2903                 check_timer();
2904 }
2905
2906 /*
2907  *      Called after all the initialization is done. If we didnt find any
2908  *      APIC bugs then we can allow the modify fast path
2909  */
2910
2911 static int __init io_apic_bug_finalize(void)
2912 {
2913         if (sis_apic_bug == -1)
2914                 sis_apic_bug = 0;
2915         return 0;
2916 }
2917
2918 late_initcall(io_apic_bug_finalize);
2919
2920 struct sysfs_ioapic_data {
2921         struct sys_device dev;
2922         struct IO_APIC_route_entry entry[0];
2923 };
2924 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2925
2926 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2927 {
2928         struct IO_APIC_route_entry *entry;
2929         struct sysfs_ioapic_data *data;
2930         int i;
2931
2932         data = container_of(dev, struct sysfs_ioapic_data, dev);
2933         entry = data->entry;
2934         for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2935                 *entry = ioapic_read_entry(dev->id, i);
2936
2937         return 0;
2938 }
2939
2940 static int ioapic_resume(struct sys_device *dev)
2941 {
2942         struct IO_APIC_route_entry *entry;
2943         struct sysfs_ioapic_data *data;
2944         unsigned long flags;
2945         union IO_APIC_reg_00 reg_00;
2946         int i;
2947
2948         data = container_of(dev, struct sysfs_ioapic_data, dev);
2949         entry = data->entry;
2950
2951         raw_spin_lock_irqsave(&ioapic_lock, flags);
2952         reg_00.raw = io_apic_read(dev->id, 0);
2953         if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
2954                 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
2955                 io_apic_write(dev->id, 0, reg_00.raw);
2956         }
2957         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2958         for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
2959                 ioapic_write_entry(dev->id, i, entry[i]);
2960
2961         return 0;
2962 }
2963
2964 static struct sysdev_class ioapic_sysdev_class = {
2965         .name = "ioapic",
2966         .suspend = ioapic_suspend,
2967         .resume = ioapic_resume,
2968 };
2969
2970 static int __init ioapic_init_sysfs(void)
2971 {
2972         struct sys_device * dev;
2973         int i, size, error;
2974
2975         error = sysdev_class_register(&ioapic_sysdev_class);
2976         if (error)
2977                 return error;
2978
2979         for (i = 0; i < nr_ioapics; i++ ) {
2980                 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2981                         * sizeof(struct IO_APIC_route_entry);
2982                 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
2983                 if (!mp_ioapic_data[i]) {
2984                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2985                         continue;
2986                 }
2987                 dev = &mp_ioapic_data[i]->dev;
2988                 dev->id = i;
2989                 dev->cls = &ioapic_sysdev_class;
2990                 error = sysdev_register(dev);
2991                 if (error) {
2992                         kfree(mp_ioapic_data[i]);
2993                         mp_ioapic_data[i] = NULL;
2994                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2995                         continue;
2996                 }
2997         }
2998
2999         return 0;
3000 }
3001
3002 device_initcall(ioapic_init_sysfs);
3003
3004 /*
3005  * Dynamic irq allocate and deallocation
3006  */
3007 unsigned int create_irq_nr(unsigned int from, int node)
3008 {
3009         struct irq_cfg *cfg;
3010         unsigned long flags;
3011         unsigned int ret = 0;
3012         int irq;
3013
3014         if (from < nr_irqs_gsi)
3015                 from = nr_irqs_gsi;
3016
3017         irq = alloc_irq_from(from, node);
3018         if (irq < 0)
3019                 return 0;
3020         cfg = alloc_irq_cfg(irq, node);
3021         if (!cfg) {
3022                 free_irq_at(irq, NULL);
3023                 return 0;
3024         }
3025
3026         raw_spin_lock_irqsave(&vector_lock, flags);
3027         if (!__assign_irq_vector(irq, cfg, apic->target_cpus()))
3028                 ret = irq;
3029         raw_spin_unlock_irqrestore(&vector_lock, flags);
3030
3031         if (ret) {
3032                 irq_set_chip_data(irq, cfg);
3033                 irq_clear_status_flags(irq, IRQ_NOREQUEST);
3034         } else {
3035                 free_irq_at(irq, cfg);
3036         }
3037         return ret;
3038 }
3039
3040 int create_irq(void)
3041 {
3042         int node = cpu_to_node(0);
3043         unsigned int irq_want;
3044         int irq;
3045
3046         irq_want = nr_irqs_gsi;
3047         irq = create_irq_nr(irq_want, node);
3048
3049         if (irq == 0)
3050                 irq = -1;
3051
3052         return irq;
3053 }
3054
3055 void destroy_irq(unsigned int irq)
3056 {
3057         struct irq_cfg *cfg = irq_get_chip_data(irq);
3058         unsigned long flags;
3059
3060         irq_set_status_flags(irq, IRQ_NOREQUEST|IRQ_NOPROBE);
3061
3062         if (irq_remapped(cfg))
3063                 free_irte(irq);
3064         raw_spin_lock_irqsave(&vector_lock, flags);
3065         __clear_irq_vector(irq, cfg);
3066         raw_spin_unlock_irqrestore(&vector_lock, flags);
3067         free_irq_at(irq, cfg);
3068 }
3069
3070 /*
3071  * MSI message composition
3072  */
3073 #ifdef CONFIG_PCI_MSI
3074 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq,
3075                            struct msi_msg *msg, u8 hpet_id)
3076 {
3077         struct irq_cfg *cfg;
3078         int err;
3079         unsigned dest;
3080
3081         if (disable_apic)
3082                 return -ENXIO;
3083
3084         cfg = irq_cfg(irq);
3085         err = assign_irq_vector(irq, cfg, apic->target_cpus());
3086         if (err)
3087                 return err;
3088
3089         dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
3090
3091         if (irq_remapped(irq_get_chip_data(irq))) {
3092                 struct irte irte;
3093                 int ir_index;
3094                 u16 sub_handle;
3095
3096                 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3097                 BUG_ON(ir_index == -1);
3098
3099                 prepare_irte(&irte, cfg->vector, dest);
3100
3101                 /* Set source-id of interrupt request */
3102                 if (pdev)
3103                         set_msi_sid(&irte, pdev);
3104                 else
3105                         set_hpet_sid(&irte, hpet_id);
3106
3107                 modify_irte(irq, &irte);
3108
3109                 msg->address_hi = MSI_ADDR_BASE_HI;
3110                 msg->data = sub_handle;
3111                 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3112                                   MSI_ADDR_IR_SHV |
3113                                   MSI_ADDR_IR_INDEX1(ir_index) |
3114                                   MSI_ADDR_IR_INDEX2(ir_index);
3115         } else {
3116                 if (x2apic_enabled())
3117                         msg->address_hi = MSI_ADDR_BASE_HI |
3118                                           MSI_ADDR_EXT_DEST_ID(dest);
3119                 else
3120                         msg->address_hi = MSI_ADDR_BASE_HI;
3121
3122                 msg->address_lo =
3123                         MSI_ADDR_BASE_LO |
3124                         ((apic->irq_dest_mode == 0) ?
3125                                 MSI_ADDR_DEST_MODE_PHYSICAL:
3126                                 MSI_ADDR_DEST_MODE_LOGICAL) |
3127                         ((apic->irq_delivery_mode != dest_LowestPrio) ?
3128                                 MSI_ADDR_REDIRECTION_CPU:
3129                                 MSI_ADDR_REDIRECTION_LOWPRI) |
3130                         MSI_ADDR_DEST_ID(dest);
3131
3132                 msg->data =
3133                         MSI_DATA_TRIGGER_EDGE |
3134                         MSI_DATA_LEVEL_ASSERT |
3135                         ((apic->irq_delivery_mode != dest_LowestPrio) ?
3136                                 MSI_DATA_DELIVERY_FIXED:
3137                                 MSI_DATA_DELIVERY_LOWPRI) |
3138                         MSI_DATA_VECTOR(cfg->vector);
3139         }
3140         return err;
3141 }
3142
3143 #ifdef CONFIG_SMP
3144 static int
3145 msi_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
3146 {
3147         struct irq_cfg *cfg = data->chip_data;
3148         struct msi_msg msg;
3149         unsigned int dest;
3150
3151         if (__ioapic_set_affinity(data, mask, &dest))
3152                 return -1;
3153
3154         __get_cached_msi_msg(data->msi_desc, &msg);
3155
3156         msg.data &= ~MSI_DATA_VECTOR_MASK;
3157         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3158         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3159         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3160
3161         __write_msi_msg(data->msi_desc, &msg);
3162
3163         return 0;
3164 }
3165 #ifdef CONFIG_INTR_REMAP
3166 /*
3167  * Migrate the MSI irq to another cpumask. This migration is
3168  * done in the process context using interrupt-remapping hardware.
3169  */
3170 static int
3171 ir_msi_set_affinity(struct irq_data *data, const struct cpumask *mask,
3172                     bool force)
3173 {
3174         struct irq_cfg *cfg = data->chip_data;
3175         unsigned int dest, irq = data->irq;
3176         struct irte irte;
3177
3178         if (get_irte(irq, &irte))
3179                 return -1;
3180
3181         if (__ioapic_set_affinity(data, mask, &dest))
3182                 return -1;
3183
3184         irte.vector = cfg->vector;
3185         irte.dest_id = IRTE_DEST(dest);
3186
3187         /*
3188          * atomically update the IRTE with the new destination and vector.
3189          */
3190         modify_irte(irq, &irte);
3191
3192         /*
3193          * After this point, all the interrupts will start arriving
3194          * at the new destination. So, time to cleanup the previous
3195          * vector allocation.
3196          */
3197         if (cfg->move_in_progress)
3198                 send_cleanup_vector(cfg);
3199
3200         return 0;
3201 }
3202
3203 #endif
3204 #endif /* CONFIG_SMP */
3205
3206 /*
3207  * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3208  * which implement the MSI or MSI-X Capability Structure.
3209  */
3210 static struct irq_chip msi_chip = {
3211         .name                   = "PCI-MSI",
3212         .irq_unmask             = unmask_msi_irq,
3213         .irq_mask               = mask_msi_irq,
3214         .irq_ack                = ack_apic_edge,
3215 #ifdef CONFIG_SMP
3216         .irq_set_affinity       = msi_set_affinity,
3217 #endif
3218         .irq_retrigger          = ioapic_retrigger_irq,
3219 };
3220
3221 static struct irq_chip msi_ir_chip = {
3222         .name                   = "IR-PCI-MSI",
3223         .irq_unmask             = unmask_msi_irq,
3224         .irq_mask               = mask_msi_irq,
3225 #ifdef CONFIG_INTR_REMAP
3226         .irq_ack                = ir_ack_apic_edge,
3227 #ifdef CONFIG_SMP
3228         .irq_set_affinity       = ir_msi_set_affinity,
3229 #endif
3230 #endif
3231         .irq_retrigger          = ioapic_retrigger_irq,
3232 };
3233
3234 /*
3235  * Map the PCI dev to the corresponding remapping hardware unit
3236  * and allocate 'nvec' consecutive interrupt-remapping table entries
3237  * in it.
3238  */
3239 static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3240 {
3241         struct intel_iommu *iommu;
3242         int index;
3243
3244         iommu = map_dev_to_ir(dev);
3245         if (!iommu) {
3246                 printk(KERN_ERR
3247                        "Unable to map PCI %s to iommu\n", pci_name(dev));
3248                 return -ENOENT;
3249         }
3250
3251         index = alloc_irte(iommu, irq, nvec);
3252         if (index < 0) {
3253                 printk(KERN_ERR
3254                        "Unable to allocate %d IRTE for PCI %s\n", nvec,
3255                        pci_name(dev));
3256                 return -ENOSPC;
3257         }
3258         return index;
3259 }
3260
3261 static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
3262 {
3263         struct irq_chip *chip = &msi_chip;
3264         struct msi_msg msg;
3265         int ret;
3266
3267         ret = msi_compose_msg(dev, irq, &msg, -1);
3268         if (ret < 0)
3269                 return ret;
3270
3271         irq_set_msi_desc(irq, msidesc);
3272         write_msi_msg(irq, &msg);
3273
3274         if (irq_remapped(irq_get_chip_data(irq))) {
3275                 irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
3276                 chip = &msi_ir_chip;
3277         }
3278
3279         irq_set_chip_and_handler_name(irq, chip, handle_edge_irq, "edge");
3280
3281         dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3282
3283         return 0;
3284 }
3285
3286 int native_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3287 {
3288         int node, ret, sub_handle, index = 0;
3289         unsigned int irq, irq_want;
3290         struct msi_desc *msidesc;
3291         struct intel_iommu *iommu = NULL;
3292
3293         /* x86 doesn't support multiple MSI yet */
3294         if (type == PCI_CAP_ID_MSI && nvec > 1)
3295                 return 1;
3296
3297         node = dev_to_node(&dev->dev);
3298         irq_want = nr_irqs_gsi;
3299         sub_handle = 0;
3300         list_for_each_entry(msidesc, &dev->msi_list, list) {
3301                 irq = create_irq_nr(irq_want, node);
3302                 if (irq == 0)
3303                         return -1;
3304                 irq_want = irq + 1;
3305                 if (!intr_remapping_enabled)
3306                         goto no_ir;
3307
3308                 if (!sub_handle) {
3309                         /*
3310                          * allocate the consecutive block of IRTE's
3311                          * for 'nvec'
3312                          */
3313                         index = msi_alloc_irte(dev, irq, nvec);
3314                         if (index < 0) {
3315                                 ret = index;
3316                                 goto error;
3317                         }
3318                 } else {
3319                         iommu = map_dev_to_ir(dev);
3320                         if (!iommu) {
3321                                 ret = -ENOENT;
3322                                 goto error;
3323                         }
3324                         /*
3325                          * setup the mapping between the irq and the IRTE
3326                          * base index, the sub_handle pointing to the
3327                          * appropriate interrupt remap table entry.
3328                          */
3329                         set_irte_irq(irq, iommu, index, sub_handle);
3330                 }
3331 no_ir:
3332                 ret = setup_msi_irq(dev, msidesc, irq);
3333                 if (ret < 0)
3334                         goto error;
3335                 sub_handle++;
3336         }
3337         return 0;
3338
3339 error:
3340         destroy_irq(irq);
3341         return ret;
3342 }
3343
3344 void native_teardown_msi_irq(unsigned int irq)
3345 {
3346         destroy_irq(irq);
3347 }
3348
3349 #if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
3350 #ifdef CONFIG_SMP
3351 static int
3352 dmar_msi_set_affinity(struct irq_data *data, const struct cpumask *mask,
3353                       bool force)
3354 {
3355         struct irq_cfg *cfg = data->chip_data;
3356         unsigned int dest, irq = data->irq;
3357         struct msi_msg msg;
3358
3359         if (__ioapic_set_affinity(data, mask, &dest))
3360                 return -1;
3361
3362         dmar_msi_read(irq, &msg);
3363
3364         msg.data &= ~MSI_DATA_VECTOR_MASK;
3365         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3366         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3367         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3368         msg.address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(dest);
3369
3370         dmar_msi_write(irq, &msg);
3371
3372         return 0;
3373 }
3374
3375 #endif /* CONFIG_SMP */
3376
3377 static struct irq_chip dmar_msi_type = {
3378         .name                   = "DMAR_MSI",
3379         .irq_unmask             = dmar_msi_unmask,
3380         .irq_mask               = dmar_msi_mask,
3381         .irq_ack                = ack_apic_edge,
3382 #ifdef CONFIG_SMP
3383         .irq_set_affinity       = dmar_msi_set_affinity,
3384 #endif
3385         .irq_retrigger          = ioapic_retrigger_irq,
3386 };
3387
3388 int arch_setup_dmar_msi(unsigned int irq)
3389 {
3390         int ret;
3391         struct msi_msg msg;
3392
3393         ret = msi_compose_msg(NULL, irq, &msg, -1);
3394         if (ret < 0)
3395                 return ret;
3396         dmar_msi_write(irq, &msg);
3397         irq_set_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3398                                       "edge");
3399         return 0;
3400 }
3401 #endif
3402
3403 #ifdef CONFIG_HPET_TIMER
3404
3405 #ifdef CONFIG_SMP
3406 static int hpet_msi_set_affinity(struct irq_data *data,
3407                                  const struct cpumask *mask, bool force)
3408 {
3409         struct irq_cfg *cfg = data->chip_data;
3410         struct msi_msg msg;
3411         unsigned int dest;
3412
3413         if (__ioapic_set_affinity(data, mask, &dest))
3414                 return -1;
3415
3416         hpet_msi_read(data->handler_data, &msg);
3417
3418         msg.data &= ~MSI_DATA_VECTOR_MASK;
3419         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3420         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3421         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3422
3423         hpet_msi_write(data->handler_data, &msg);
3424
3425         return 0;
3426 }
3427
3428 #endif /* CONFIG_SMP */
3429
3430 static struct irq_chip ir_hpet_msi_type = {
3431         .name                   = "IR-HPET_MSI",
3432         .irq_unmask             = hpet_msi_unmask,
3433         .irq_mask               = hpet_msi_mask,
3434 #ifdef CONFIG_INTR_REMAP
3435         .irq_ack                = ir_ack_apic_edge,
3436 #ifdef CONFIG_SMP
3437         .irq_set_affinity       = ir_msi_set_affinity,
3438 #endif
3439 #endif
3440         .irq_retrigger          = ioapic_retrigger_irq,
3441 };
3442
3443 static struct irq_chip hpet_msi_type = {
3444         .name = "HPET_MSI",
3445         .irq_unmask = hpet_msi_unmask,
3446         .irq_mask = hpet_msi_mask,
3447         .irq_ack = ack_apic_edge,
3448 #ifdef CONFIG_SMP
3449         .irq_set_affinity = hpet_msi_set_affinity,
3450 #endif
3451         .irq_retrigger = ioapic_retrigger_irq,
3452 };
3453
3454 int arch_setup_hpet_msi(unsigned int irq, unsigned int id)
3455 {
3456         struct irq_chip *chip = &hpet_msi_type;
3457         struct msi_msg msg;
3458         int ret;
3459
3460         if (intr_remapping_enabled) {
3461                 struct intel_iommu *iommu = map_hpet_to_ir(id);
3462                 int index;
3463
3464                 if (!iommu)
3465                         return -1;
3466
3467                 index = alloc_irte(iommu, irq, 1);
3468                 if (index < 0)
3469                         return -1;
3470         }
3471
3472         ret = msi_compose_msg(NULL, irq, &msg, id);
3473         if (ret < 0)
3474                 return ret;
3475
3476         hpet_msi_write(irq_get_handler_data(irq), &msg);
3477         irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
3478         if (irq_remapped(irq_get_chip_data(irq)))
3479                 chip = &ir_hpet_msi_type;
3480
3481         irq_set_chip_and_handler_name(irq, chip, handle_edge_irq, "edge");
3482         return 0;
3483 }
3484 #endif
3485
3486 #endif /* CONFIG_PCI_MSI */
3487 /*
3488  * Hypertransport interrupt support
3489  */
3490 #ifdef CONFIG_HT_IRQ
3491
3492 #ifdef CONFIG_SMP
3493
3494 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
3495 {
3496         struct ht_irq_msg msg;
3497         fetch_ht_irq_msg(irq, &msg);
3498
3499         msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
3500         msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
3501
3502         msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
3503         msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
3504
3505         write_ht_irq_msg(irq, &msg);
3506 }
3507
3508 static int
3509 ht_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
3510 {
3511         struct irq_cfg *cfg = data->chip_data;
3512         unsigned int dest;
3513
3514         if (__ioapic_set_affinity(data, mask, &dest))
3515                 return -1;
3516
3517         target_ht_irq(data->irq, dest, cfg->vector);
3518         return 0;
3519 }
3520
3521 #endif
3522
3523 static struct irq_chip ht_irq_chip = {
3524         .name                   = "PCI-HT",
3525         .irq_mask               = mask_ht_irq,
3526         .irq_unmask             = unmask_ht_irq,
3527         .irq_ack                = ack_apic_edge,
3528 #ifdef CONFIG_SMP
3529         .irq_set_affinity       = ht_set_affinity,
3530 #endif
3531         .irq_retrigger          = ioapic_retrigger_irq,
3532 };
3533
3534 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3535 {
3536         struct irq_cfg *cfg;
3537         int err;
3538
3539         if (disable_apic)
3540                 return -ENXIO;
3541
3542         cfg = irq_cfg(irq);
3543         err = assign_irq_vector(irq, cfg, apic->target_cpus());
3544         if (!err) {
3545                 struct ht_irq_msg msg;
3546                 unsigned dest;
3547
3548                 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3549                                                     apic->target_cpus());
3550
3551                 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
3552
3553                 msg.address_lo =
3554                         HT_IRQ_LOW_BASE |
3555                         HT_IRQ_LOW_DEST_ID(dest) |
3556                         HT_IRQ_LOW_VECTOR(cfg->vector) |
3557                         ((apic->irq_dest_mode == 0) ?
3558                                 HT_IRQ_LOW_DM_PHYSICAL :
3559                                 HT_IRQ_LOW_DM_LOGICAL) |
3560                         HT_IRQ_LOW_RQEOI_EDGE |
3561                         ((apic->irq_delivery_mode != dest_LowestPrio) ?
3562                                 HT_IRQ_LOW_MT_FIXED :
3563                                 HT_IRQ_LOW_MT_ARBITRATED) |
3564                         HT_IRQ_LOW_IRQ_MASKED;
3565
3566                 write_ht_irq_msg(irq, &msg);
3567
3568                 irq_set_chip_and_handler_name(irq, &ht_irq_chip,
3569                                               handle_edge_irq, "edge");
3570
3571                 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
3572         }
3573         return err;
3574 }
3575 #endif /* CONFIG_HT_IRQ */
3576
3577 int
3578 io_apic_setup_irq_pin(unsigned int irq, int node, struct io_apic_irq_attr *attr)
3579 {
3580         struct irq_cfg *cfg = alloc_irq_and_cfg_at(irq, node);
3581         int ret;
3582
3583         if (!cfg)
3584                 return -EINVAL;
3585         ret = __add_pin_to_irq_node(cfg, node, attr->ioapic, attr->ioapic_pin);
3586         if (!ret)
3587                 setup_ioapic_irq(attr->ioapic, attr->ioapic_pin, irq, cfg,
3588                                  attr->trigger, attr->polarity);
3589         return ret;
3590 }
3591
3592 static int io_apic_setup_irq_pin_once(unsigned int irq, int node,
3593                                       struct io_apic_irq_attr *attr)
3594 {
3595         unsigned int id = attr->ioapic, pin = attr->ioapic_pin;
3596         int ret;
3597
3598         /* Avoid redundant programming */
3599         if (test_bit(pin, mp_ioapic_routing[id].pin_programmed)) {
3600                 pr_debug("Pin %d-%d already programmed\n",
3601                          mp_ioapics[id].apicid, pin);
3602                 return 0;
3603         }
3604         ret = io_apic_setup_irq_pin(irq, node, attr);
3605         if (!ret)
3606                 set_bit(pin, mp_ioapic_routing[id].pin_programmed);
3607         return ret;
3608 }
3609
3610 static int __init io_apic_get_redir_entries(int ioapic)
3611 {
3612         union IO_APIC_reg_01    reg_01;
3613         unsigned long flags;
3614
3615         raw_spin_lock_irqsave(&ioapic_lock, flags);
3616         reg_01.raw = io_apic_read(ioapic, 1);
3617         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3618
3619         /* The register returns the maximum index redir index
3620          * supported, which is one less than the total number of redir
3621          * entries.
3622          */
3623         return reg_01.bits.entries + 1;
3624 }
3625
3626 static void __init probe_nr_irqs_gsi(void)
3627 {
3628         int nr;
3629
3630         nr = gsi_top + NR_IRQS_LEGACY;
3631         if (nr > nr_irqs_gsi)
3632                 nr_irqs_gsi = nr;
3633
3634         printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
3635 }
3636
3637 int get_nr_irqs_gsi(void)
3638 {
3639         return nr_irqs_gsi;
3640 }
3641
3642 #ifdef CONFIG_SPARSE_IRQ
3643 int __init arch_probe_nr_irqs(void)
3644 {
3645         int nr;
3646
3647         if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3648                 nr_irqs = NR_VECTORS * nr_cpu_ids;
3649
3650         nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3651 #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3652         /*
3653          * for MSI and HT dyn irq
3654          */
3655         nr += nr_irqs_gsi * 16;
3656 #endif
3657         if (nr < nr_irqs)
3658                 nr_irqs = nr;
3659
3660         return NR_IRQS_LEGACY;
3661 }
3662 #endif
3663
3664 int io_apic_set_pci_routing(struct device *dev, int irq,
3665                             struct io_apic_irq_attr *irq_attr)
3666 {
3667         int node;
3668
3669         if (!IO_APIC_IRQ(irq)) {
3670                 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3671                             irq_attr->ioapic);
3672                 return -EINVAL;
3673         }
3674
3675         node = dev ? dev_to_node(dev) : cpu_to_node(0);
3676
3677         return io_apic_setup_irq_pin_once(irq, node, irq_attr);
3678 }
3679
3680 #ifdef CONFIG_X86_32
3681 static int __init io_apic_get_unique_id(int ioapic, int apic_id)
3682 {
3683         union IO_APIC_reg_00 reg_00;
3684         static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3685         physid_mask_t tmp;
3686         unsigned long flags;
3687         int i = 0;
3688
3689         /*
3690          * The P4 platform supports up to 256 APIC IDs on two separate APIC
3691          * buses (one for LAPICs, one for IOAPICs), where predecessors only
3692          * supports up to 16 on one shared APIC bus.
3693          *
3694          * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3695          *      advantage of new APIC bus architecture.
3696          */
3697
3698         if (physids_empty(apic_id_map))
3699                 apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
3700
3701         raw_spin_lock_irqsave(&ioapic_lock, flags);
3702         reg_00.raw = io_apic_read(ioapic, 0);
3703         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3704
3705         if (apic_id >= get_physical_broadcast()) {
3706                 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3707                         "%d\n", ioapic, apic_id, reg_00.bits.ID);
3708                 apic_id = reg_00.bits.ID;
3709         }
3710
3711         /*
3712          * Every APIC in a system must have a unique ID or we get lots of nice
3713          * 'stuck on smp_invalidate_needed IPI wait' messages.
3714          */
3715         if (apic->check_apicid_used(&apic_id_map, apic_id)) {
3716
3717                 for (i = 0; i < get_physical_broadcast(); i++) {
3718                         if (!apic->check_apicid_used(&apic_id_map, i))
3719                                 break;
3720                 }
3721
3722                 if (i == get_physical_broadcast())
3723                         panic("Max apic_id exceeded!\n");
3724
3725                 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3726                         "trying %d\n", ioapic, apic_id, i);
3727
3728                 apic_id = i;
3729         }
3730
3731         apic->apicid_to_cpu_present(apic_id, &tmp);
3732         physids_or(apic_id_map, apic_id_map, tmp);
3733
3734         if (reg_00.bits.ID != apic_id) {
3735                 reg_00.bits.ID = apic_id;
3736
3737                 raw_spin_lock_irqsave(&ioapic_lock, flags);
3738                 io_apic_write(ioapic, 0, reg_00.raw);
3739                 reg_00.raw = io_apic_read(ioapic, 0);
3740                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3741
3742                 /* Sanity check */
3743                 if (reg_00.bits.ID != apic_id) {
3744                         printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3745                         return -1;
3746                 }
3747         }
3748
3749         apic_printk(APIC_VERBOSE, KERN_INFO
3750                         "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3751
3752         return apic_id;
3753 }
3754
3755 static u8 __init io_apic_unique_id(u8 id)
3756 {
3757         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
3758             !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
3759                 return io_apic_get_unique_id(nr_ioapics, id);
3760         else
3761                 return id;
3762 }
3763 #else
3764 static u8 __init io_apic_unique_id(u8 id)
3765 {
3766         int i;
3767         DECLARE_BITMAP(used, 256);
3768
3769         bitmap_zero(used, 256);
3770         for (i = 0; i < nr_ioapics; i++) {
3771                 struct mpc_ioapic *ia = &mp_ioapics[i];
3772                 __set_bit(ia->apicid, used);
3773         }
3774         if (!test_bit(id, used))
3775                 return id;
3776         return find_first_zero_bit(used, 256);
3777 }
3778 #endif
3779
3780 static int __init io_apic_get_version(int ioapic)
3781 {
3782         union IO_APIC_reg_01    reg_01;
3783         unsigned long flags;
3784
3785         raw_spin_lock_irqsave(&ioapic_lock, flags);
3786         reg_01.raw = io_apic_read(ioapic, 1);
3787         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3788
3789         return reg_01.bits.version;
3790 }
3791
3792 int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
3793 {
3794         int ioapic, pin, idx;
3795
3796         if (skip_ioapic_setup)
3797                 return -1;
3798
3799         ioapic = mp_find_ioapic(gsi);
3800         if (ioapic < 0)
3801                 return -1;
3802
3803         pin = mp_find_ioapic_pin(ioapic, gsi);
3804         if (pin < 0)
3805                 return -1;
3806
3807         idx = find_irq_entry(ioapic, pin, mp_INT);
3808         if (idx < 0)
3809                 return -1;
3810
3811         *trigger = irq_trigger(idx);
3812         *polarity = irq_polarity(idx);
3813         return 0;
3814 }
3815
3816 /*
3817  * This function currently is only a helper for the i386 smp boot process where
3818  * we need to reprogram the ioredtbls to cater for the cpus which have come online
3819  * so mask in all cases should simply be apic->target_cpus()
3820  */
3821 #ifdef CONFIG_SMP
3822 void __init setup_ioapic_dest(void)
3823 {
3824         int pin, ioapic, irq, irq_entry;
3825         const struct cpumask *mask;
3826         struct irq_data *idata;
3827
3828         if (skip_ioapic_setup == 1)
3829                 return;
3830
3831         for (ioapic = 0; ioapic < nr_ioapics; ioapic++)
3832         for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3833                 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3834                 if (irq_entry == -1)
3835                         continue;
3836                 irq = pin_2_irq(irq_entry, ioapic, pin);
3837
3838                 if ((ioapic > 0) && (irq > 16))
3839                         continue;
3840
3841                 idata = irq_get_irq_data(irq);
3842
3843                 /*
3844                  * Honour affinities which have been set in early boot
3845                  */
3846                 if (!irqd_can_balance(idata) || irqd_affinity_was_set(idata))
3847                         mask = idata->affinity;
3848                 else
3849                         mask = apic->target_cpus();
3850
3851                 if (intr_remapping_enabled)
3852                         ir_ioapic_set_affinity(idata, mask, false);
3853                 else
3854                         ioapic_set_affinity(idata, mask, false);
3855         }
3856
3857 }
3858 #endif
3859
3860 #define IOAPIC_RESOURCE_NAME_SIZE 11
3861
3862 static struct resource *ioapic_resources;
3863
3864 static struct resource * __init ioapic_setup_resources(int nr_ioapics)
3865 {
3866         unsigned long n;
3867         struct resource *res;
3868         char *mem;
3869         int i;
3870
3871         if (nr_ioapics <= 0)
3872                 return NULL;
3873
3874         n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3875         n *= nr_ioapics;
3876
3877         mem = alloc_bootmem(n);
3878         res = (void *)mem;
3879
3880         mem += sizeof(struct resource) * nr_ioapics;
3881
3882         for (i = 0; i < nr_ioapics; i++) {
3883                 res[i].name = mem;
3884                 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3885                 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
3886                 mem += IOAPIC_RESOURCE_NAME_SIZE;
3887         }
3888
3889         ioapic_resources = res;
3890
3891         return res;
3892 }
3893
3894 void __init ioapic_and_gsi_init(void)
3895 {
3896         unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3897         struct resource *ioapic_res;
3898         int i;
3899
3900         ioapic_res = ioapic_setup_resources(nr_ioapics);
3901         for (i = 0; i < nr_ioapics; i++) {
3902                 if (smp_found_config) {
3903                         ioapic_phys = mp_ioapics[i].apicaddr;
3904 #ifdef CONFIG_X86_32
3905                         if (!ioapic_phys) {
3906                                 printk(KERN_ERR
3907                                        "WARNING: bogus zero IO-APIC "
3908                                        "address found in MPTABLE, "
3909                                        "disabling IO/APIC support!\n");
3910                                 smp_found_config = 0;
3911                                 skip_ioapic_setup = 1;
3912                                 goto fake_ioapic_page;
3913                         }
3914 #endif
3915                 } else {
3916 #ifdef CONFIG_X86_32
3917 fake_ioapic_page:
3918 #endif
3919                         ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
3920                         ioapic_phys = __pa(ioapic_phys);
3921                 }
3922                 set_fixmap_nocache(idx, ioapic_phys);
3923                 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
3924                         __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
3925                         ioapic_phys);
3926                 idx++;
3927
3928                 ioapic_res->start = ioapic_phys;
3929                 ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
3930                 ioapic_res++;
3931         }
3932
3933         probe_nr_irqs_gsi();
3934 }
3935
3936 void __init ioapic_insert_resources(void)
3937 {
3938         int i;
3939         struct resource *r = ioapic_resources;
3940
3941         if (!r) {
3942                 if (nr_ioapics > 0)
3943                         printk(KERN_ERR
3944                                 "IO APIC resources couldn't be allocated.\n");
3945                 return;
3946         }
3947
3948         for (i = 0; i < nr_ioapics; i++) {
3949                 insert_resource(&iomem_resource, r);
3950                 r++;
3951         }
3952 }
3953
3954 int mp_find_ioapic(u32 gsi)
3955 {
3956         int i = 0;
3957
3958         if (nr_ioapics == 0)
3959                 return -1;
3960
3961         /* Find the IOAPIC that manages this GSI. */
3962         for (i = 0; i < nr_ioapics; i++) {
3963                 if ((gsi >= mp_gsi_routing[i].gsi_base)
3964                     && (gsi <= mp_gsi_routing[i].gsi_end))
3965                         return i;
3966         }
3967
3968         printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
3969         return -1;
3970 }
3971
3972 int mp_find_ioapic_pin(int ioapic, u32 gsi)
3973 {
3974         if (WARN_ON(ioapic == -1))
3975                 return -1;
3976         if (WARN_ON(gsi > mp_gsi_routing[ioapic].gsi_end))
3977                 return -1;
3978
3979         return gsi - mp_gsi_routing[ioapic].gsi_base;
3980 }
3981
3982 static __init int bad_ioapic(unsigned long address)
3983 {
3984         if (nr_ioapics >= MAX_IO_APICS) {
3985                 printk(KERN_WARNING "WARING: Max # of I/O APICs (%d) exceeded "
3986                        "(found %d), skipping\n", MAX_IO_APICS, nr_ioapics);
3987                 return 1;
3988         }
3989         if (!address) {
3990                 printk(KERN_WARNING "WARNING: Bogus (zero) I/O APIC address"
3991                        " found in table, skipping!\n");
3992                 return 1;
3993         }
3994         return 0;
3995 }
3996
3997 void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
3998 {
3999         int idx = 0;
4000         int entries;
4001
4002         if (bad_ioapic(address))
4003                 return;
4004
4005         idx = nr_ioapics;
4006
4007         mp_ioapics[idx].type = MP_IOAPIC;
4008         mp_ioapics[idx].flags = MPC_APIC_USABLE;
4009         mp_ioapics[idx].apicaddr = address;
4010
4011         set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
4012         mp_ioapics[idx].apicid = io_apic_unique_id(id);
4013         mp_ioapics[idx].apicver = io_apic_get_version(idx);
4014
4015         /*
4016          * Build basic GSI lookup table to facilitate gsi->io_apic lookups
4017          * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
4018          */
4019         entries = io_apic_get_redir_entries(idx);
4020         mp_gsi_routing[idx].gsi_base = gsi_base;
4021         mp_gsi_routing[idx].gsi_end = gsi_base + entries - 1;
4022
4023         /*
4024          * The number of IO-APIC IRQ registers (== #pins):
4025          */
4026         nr_ioapic_registers[idx] = entries;
4027
4028         if (mp_gsi_routing[idx].gsi_end >= gsi_top)
4029                 gsi_top = mp_gsi_routing[idx].gsi_end + 1;
4030
4031         printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
4032                "GSI %d-%d\n", idx, mp_ioapics[idx].apicid,
4033                mp_ioapics[idx].apicver, mp_ioapics[idx].apicaddr,
4034                mp_gsi_routing[idx].gsi_base, mp_gsi_routing[idx].gsi_end);
4035
4036         nr_ioapics++;
4037 }
4038
4039 /* Enable IOAPIC early just for system timer */
4040 void __init pre_init_apic_IRQ0(void)
4041 {
4042         struct io_apic_irq_attr attr = { 0, 0, 0, 0 };
4043
4044         printk(KERN_INFO "Early APIC setup for system timer0\n");
4045 #ifndef CONFIG_SMP
4046         physid_set_mask_of_physid(boot_cpu_physical_apicid,
4047                                          &phys_cpu_present_map);
4048 #endif
4049         setup_local_APIC();
4050
4051         io_apic_setup_irq_pin(0, 0, &attr);
4052         irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
4053                                       "edge");
4054 }