x86, irq: Move local APIC related code from io_apic.c into vector.c
[cascardo/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/syscore_ops.h>
34 #include <linux/irqdomain.h>
35 #include <linux/msi.h>
36 #include <linux/htirq.h>
37 #include <linux/freezer.h>
38 #include <linux/kthread.h>
39 #include <linux/jiffies.h>      /* time_after() */
40 #include <linux/slab.h>
41 #include <linux/bootmem.h>
42 #include <linux/dmar.h>
43 #include <linux/hpet.h>
44
45 #include <asm/idle.h>
46 #include <asm/io.h>
47 #include <asm/smp.h>
48 #include <asm/cpu.h>
49 #include <asm/desc.h>
50 #include <asm/proto.h>
51 #include <asm/acpi.h>
52 #include <asm/dma.h>
53 #include <asm/timer.h>
54 #include <asm/i8259.h>
55 #include <asm/msidef.h>
56 #include <asm/hypertransport.h>
57 #include <asm/setup.h>
58 #include <asm/irq_remapping.h>
59 #include <asm/hpet.h>
60 #include <asm/hw_irq.h>
61
62 #include <asm/apic.h>
63
64 #define for_each_ioapic(idx)            \
65         for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
66 #define for_each_ioapic_reverse(idx)    \
67         for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--)
68 #define for_each_pin(idx, pin)          \
69         for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++)
70 #define for_each_ioapic_pin(idx, pin)   \
71         for_each_ioapic((idx))          \
72                 for_each_pin((idx), (pin))
73
74 #define for_each_irq_pin(entry, head) \
75         list_for_each_entry(entry, &head, list)
76
77 /*
78  *      Is the SiS APIC rmw bug present ?
79  *      -1 = don't know, 0 = no, 1 = yes
80  */
81 int sis_apic_bug = -1;
82
83 static DEFINE_RAW_SPINLOCK(ioapic_lock);
84 static DEFINE_MUTEX(ioapic_mutex);
85 static unsigned int ioapic_dynirq_base;
86 static int ioapic_initialized;
87
88 struct mp_pin_info {
89         int trigger;
90         int polarity;
91         int node;
92         int set;
93         u32 count;
94 };
95
96 static struct ioapic {
97         /*
98          * # of IRQ routing registers
99          */
100         int nr_registers;
101         /*
102          * Saved state during suspend/resume, or while enabling intr-remap.
103          */
104         struct IO_APIC_route_entry *saved_registers;
105         /* I/O APIC config */
106         struct mpc_ioapic mp_config;
107         /* IO APIC gsi routing info */
108         struct mp_ioapic_gsi  gsi_config;
109         struct ioapic_domain_cfg irqdomain_cfg;
110         struct irq_domain *irqdomain;
111         struct mp_pin_info *pin_info;
112         struct resource *iomem_res;
113 } ioapics[MAX_IO_APICS];
114
115 #define mpc_ioapic_ver(ioapic_idx)      ioapics[ioapic_idx].mp_config.apicver
116
117 int mpc_ioapic_id(int ioapic_idx)
118 {
119         return ioapics[ioapic_idx].mp_config.apicid;
120 }
121
122 unsigned int mpc_ioapic_addr(int ioapic_idx)
123 {
124         return ioapics[ioapic_idx].mp_config.apicaddr;
125 }
126
127 struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
128 {
129         return &ioapics[ioapic_idx].gsi_config;
130 }
131
132 static inline int mp_ioapic_pin_count(int ioapic)
133 {
134         struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
135
136         return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
137 }
138
139 u32 mp_pin_to_gsi(int ioapic, int pin)
140 {
141         return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin;
142 }
143
144 /*
145  * Initialize all legacy IRQs and all pins on the first IOAPIC
146  * if we have legacy interrupt controller. Kernel boot option "pirq="
147  * may rely on non-legacy pins on the first IOAPIC.
148  */
149 static inline int mp_init_irq_at_boot(int ioapic, int irq)
150 {
151         if (!nr_legacy_irqs())
152                 return 0;
153
154         return ioapic == 0 || (irq >= 0 && irq < nr_legacy_irqs());
155 }
156
157 static inline struct mp_pin_info *mp_pin_info(int ioapic_idx, int pin)
158 {
159         return ioapics[ioapic_idx].pin_info + pin;
160 }
161
162 static inline struct irq_domain *mp_ioapic_irqdomain(int ioapic)
163 {
164         return ioapics[ioapic].irqdomain;
165 }
166
167 int nr_ioapics;
168
169 /* The one past the highest gsi number used */
170 u32 gsi_top;
171
172 /* MP IRQ source entries */
173 struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
174
175 /* # of MP IRQ source entries */
176 int mp_irq_entries;
177
178 #ifdef CONFIG_EISA
179 int mp_bus_id_to_type[MAX_MP_BUSSES];
180 #endif
181
182 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
183
184 int skip_ioapic_setup;
185
186 /**
187  * disable_ioapic_support() - disables ioapic support at runtime
188  */
189 void disable_ioapic_support(void)
190 {
191 #ifdef CONFIG_PCI
192         noioapicquirk = 1;
193         noioapicreroute = -1;
194 #endif
195         skip_ioapic_setup = 1;
196 }
197
198 static int __init parse_noapic(char *str)
199 {
200         /* disable IO-APIC */
201         disable_ioapic_support();
202         return 0;
203 }
204 early_param("noapic", parse_noapic);
205
206 /* Will be called in mpparse/acpi/sfi codes for saving IRQ info */
207 void mp_save_irq(struct mpc_intsrc *m)
208 {
209         int i;
210
211         apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
212                 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
213                 m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
214                 m->srcbusirq, m->dstapic, m->dstirq);
215
216         for (i = 0; i < mp_irq_entries; i++) {
217                 if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
218                         return;
219         }
220
221         memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
222         if (++mp_irq_entries == MAX_IRQ_SOURCES)
223                 panic("Max # of irq sources exceeded!!\n");
224 }
225
226 struct irq_pin_list {
227         struct list_head list;
228         int apic, pin;
229 };
230
231 static struct irq_pin_list *alloc_irq_pin_list(int node)
232 {
233         return kzalloc_node(sizeof(struct irq_pin_list), GFP_KERNEL, node);
234 }
235
236 static void alloc_ioapic_saved_registers(int idx)
237 {
238         size_t size;
239
240         if (ioapics[idx].saved_registers)
241                 return;
242
243         size = sizeof(struct IO_APIC_route_entry) * ioapics[idx].nr_registers;
244         ioapics[idx].saved_registers = kzalloc(size, GFP_KERNEL);
245         if (!ioapics[idx].saved_registers)
246                 pr_err("IOAPIC %d: suspend/resume impossible!\n", idx);
247 }
248
249 static void free_ioapic_saved_registers(int idx)
250 {
251         kfree(ioapics[idx].saved_registers);
252         ioapics[idx].saved_registers = NULL;
253 }
254
255 int __init arch_early_irq_init(void)
256 {
257         struct irq_cfg *cfg;
258         int i, node = cpu_to_node(0);
259
260         if (!nr_legacy_irqs())
261                 io_apic_irqs = ~0UL;
262
263         for_each_ioapic(i)
264                 alloc_ioapic_saved_registers(i);
265
266         /*
267          * For legacy IRQ's, start with assigning irq0 to irq15 to
268          * IRQ0_VECTOR to IRQ15_VECTOR for all cpu's.
269          */
270         for (i = 0; i < nr_legacy_irqs(); i++) {
271                 cfg = alloc_irq_and_cfg_at(i, node);
272                 cfg->vector = IRQ0_VECTOR + i;
273                 cpumask_setall(cfg->domain);
274         }
275
276         return 0;
277 }
278
279 struct io_apic {
280         unsigned int index;
281         unsigned int unused[3];
282         unsigned int data;
283         unsigned int unused2[11];
284         unsigned int eoi;
285 };
286
287 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
288 {
289         return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
290                 + (mpc_ioapic_addr(idx) & ~PAGE_MASK);
291 }
292
293 void io_apic_eoi(unsigned int apic, unsigned int vector)
294 {
295         struct io_apic __iomem *io_apic = io_apic_base(apic);
296         writel(vector, &io_apic->eoi);
297 }
298
299 unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
300 {
301         struct io_apic __iomem *io_apic = io_apic_base(apic);
302         writel(reg, &io_apic->index);
303         return readl(&io_apic->data);
304 }
305
306 void native_io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
307 {
308         struct io_apic __iomem *io_apic = io_apic_base(apic);
309
310         writel(reg, &io_apic->index);
311         writel(value, &io_apic->data);
312 }
313
314 /*
315  * Re-write a value: to be used for read-modify-write
316  * cycles where the read already set up the index register.
317  *
318  * Older SiS APIC requires we rewrite the index register
319  */
320 void native_io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
321 {
322         struct io_apic __iomem *io_apic = io_apic_base(apic);
323
324         if (sis_apic_bug)
325                 writel(reg, &io_apic->index);
326         writel(value, &io_apic->data);
327 }
328
329 union entry_union {
330         struct { u32 w1, w2; };
331         struct IO_APIC_route_entry entry;
332 };
333
334 static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
335 {
336         union entry_union eu;
337
338         eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
339         eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
340
341         return eu.entry;
342 }
343
344 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
345 {
346         union entry_union eu;
347         unsigned long flags;
348
349         raw_spin_lock_irqsave(&ioapic_lock, flags);
350         eu.entry = __ioapic_read_entry(apic, pin);
351         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
352
353         return eu.entry;
354 }
355
356 /*
357  * When we write a new IO APIC routing entry, we need to write the high
358  * word first! If the mask bit in the low word is clear, we will enable
359  * the interrupt, and we need to make sure the entry is fully populated
360  * before that happens.
361  */
362 static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
363 {
364         union entry_union eu = {{0, 0}};
365
366         eu.entry = e;
367         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
368         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
369 }
370
371 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
372 {
373         unsigned long flags;
374
375         raw_spin_lock_irqsave(&ioapic_lock, flags);
376         __ioapic_write_entry(apic, pin, e);
377         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
378 }
379
380 /*
381  * When we mask an IO APIC routing entry, we need to write the low
382  * word first, in order to set the mask bit before we change the
383  * high bits!
384  */
385 static void ioapic_mask_entry(int apic, int pin)
386 {
387         unsigned long flags;
388         union entry_union eu = { .entry.mask = 1 };
389
390         raw_spin_lock_irqsave(&ioapic_lock, flags);
391         io_apic_write(apic, 0x10 + 2*pin, eu.w1);
392         io_apic_write(apic, 0x11 + 2*pin, eu.w2);
393         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
394 }
395
396 /*
397  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
398  * shared ISA-space IRQs, so we have to support them. We are super
399  * fast in the common case, and fast for shared ISA-space IRQs.
400  */
401 static int __add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
402 {
403         struct irq_pin_list *entry;
404
405         /* don't allow duplicates */
406         for_each_irq_pin(entry, cfg->irq_2_pin)
407                 if (entry->apic == apic && entry->pin == pin)
408                         return 0;
409
410         entry = alloc_irq_pin_list(node);
411         if (!entry) {
412                 pr_err("can not alloc irq_pin_list (%d,%d,%d)\n",
413                        node, apic, pin);
414                 return -ENOMEM;
415         }
416         entry->apic = apic;
417         entry->pin = pin;
418
419         list_add_tail(&entry->list, &cfg->irq_2_pin);
420         return 0;
421 }
422
423 static void __remove_pin_from_irq(struct irq_cfg *cfg, int apic, int pin)
424 {
425         struct irq_pin_list *tmp, *entry;
426
427         list_for_each_entry_safe(entry, tmp, &cfg->irq_2_pin, list)
428                 if (entry->apic == apic && entry->pin == pin) {
429                         list_del(&entry->list);
430                         kfree(entry);
431                         return;
432                 }
433 }
434
435 static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
436 {
437         if (__add_pin_to_irq_node(cfg, node, apic, pin))
438                 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
439 }
440
441 /*
442  * Reroute an IRQ to a different pin.
443  */
444 static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
445                                            int oldapic, int oldpin,
446                                            int newapic, int newpin)
447 {
448         struct irq_pin_list *entry;
449
450         for_each_irq_pin(entry, cfg->irq_2_pin) {
451                 if (entry->apic == oldapic && entry->pin == oldpin) {
452                         entry->apic = newapic;
453                         entry->pin = newpin;
454                         /* every one is different, right? */
455                         return;
456                 }
457         }
458
459         /* old apic/pin didn't exist, so just add new ones */
460         add_pin_to_irq_node(cfg, node, newapic, newpin);
461 }
462
463 static void __io_apic_modify_irq(struct irq_pin_list *entry,
464                                  int mask_and, int mask_or,
465                                  void (*final)(struct irq_pin_list *entry))
466 {
467         unsigned int reg, pin;
468
469         pin = entry->pin;
470         reg = io_apic_read(entry->apic, 0x10 + pin * 2);
471         reg &= mask_and;
472         reg |= mask_or;
473         io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
474         if (final)
475                 final(entry);
476 }
477
478 static void io_apic_modify_irq(struct irq_cfg *cfg,
479                                int mask_and, int mask_or,
480                                void (*final)(struct irq_pin_list *entry))
481 {
482         struct irq_pin_list *entry;
483
484         for_each_irq_pin(entry, cfg->irq_2_pin)
485                 __io_apic_modify_irq(entry, mask_and, mask_or, final);
486 }
487
488 static void io_apic_sync(struct irq_pin_list *entry)
489 {
490         /*
491          * Synchronize the IO-APIC and the CPU by doing
492          * a dummy read from the IO-APIC
493          */
494         struct io_apic __iomem *io_apic;
495
496         io_apic = io_apic_base(entry->apic);
497         readl(&io_apic->data);
498 }
499
500 static void mask_ioapic(struct irq_cfg *cfg)
501 {
502         unsigned long flags;
503
504         raw_spin_lock_irqsave(&ioapic_lock, flags);
505         io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
506         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
507 }
508
509 static void mask_ioapic_irq(struct irq_data *data)
510 {
511         mask_ioapic(data->chip_data);
512 }
513
514 static void __unmask_ioapic(struct irq_cfg *cfg)
515 {
516         io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
517 }
518
519 static void unmask_ioapic(struct irq_cfg *cfg)
520 {
521         unsigned long flags;
522
523         raw_spin_lock_irqsave(&ioapic_lock, flags);
524         __unmask_ioapic(cfg);
525         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
526 }
527
528 static void unmask_ioapic_irq(struct irq_data *data)
529 {
530         unmask_ioapic(data->chip_data);
531 }
532
533 /*
534  * IO-APIC versions below 0x20 don't support EOI register.
535  * For the record, here is the information about various versions:
536  *     0Xh     82489DX
537  *     1Xh     I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
538  *     2Xh     I/O(x)APIC which is PCI 2.2 Compliant
539  *     30h-FFh Reserved
540  *
541  * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
542  * version as 0x2. This is an error with documentation and these ICH chips
543  * use io-apic's of version 0x20.
544  *
545  * For IO-APIC's with EOI register, we use that to do an explicit EOI.
546  * Otherwise, we simulate the EOI message manually by changing the trigger
547  * mode to edge and then back to level, with RTE being masked during this.
548  */
549 void native_eoi_ioapic_pin(int apic, int pin, int vector)
550 {
551         if (mpc_ioapic_ver(apic) >= 0x20) {
552                 io_apic_eoi(apic, vector);
553         } else {
554                 struct IO_APIC_route_entry entry, entry1;
555
556                 entry = entry1 = __ioapic_read_entry(apic, pin);
557
558                 /*
559                  * Mask the entry and change the trigger mode to edge.
560                  */
561                 entry1.mask = 1;
562                 entry1.trigger = IOAPIC_EDGE;
563
564                 __ioapic_write_entry(apic, pin, entry1);
565
566                 /*
567                  * Restore the previous level triggered entry.
568                  */
569                 __ioapic_write_entry(apic, pin, entry);
570         }
571 }
572
573 void eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
574 {
575         struct irq_pin_list *entry;
576         unsigned long flags;
577
578         raw_spin_lock_irqsave(&ioapic_lock, flags);
579         for_each_irq_pin(entry, cfg->irq_2_pin)
580                 x86_io_apic_ops.eoi_ioapic_pin(entry->apic, entry->pin,
581                                                cfg->vector);
582         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
583 }
584
585 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
586 {
587         struct IO_APIC_route_entry entry;
588
589         /* Check delivery_mode to be sure we're not clearing an SMI pin */
590         entry = ioapic_read_entry(apic, pin);
591         if (entry.delivery_mode == dest_SMI)
592                 return;
593
594         /*
595          * Make sure the entry is masked and re-read the contents to check
596          * if it is a level triggered pin and if the remote-IRR is set.
597          */
598         if (!entry.mask) {
599                 entry.mask = 1;
600                 ioapic_write_entry(apic, pin, entry);
601                 entry = ioapic_read_entry(apic, pin);
602         }
603
604         if (entry.irr) {
605                 unsigned long flags;
606
607                 /*
608                  * Make sure the trigger mode is set to level. Explicit EOI
609                  * doesn't clear the remote-IRR if the trigger mode is not
610                  * set to level.
611                  */
612                 if (!entry.trigger) {
613                         entry.trigger = IOAPIC_LEVEL;
614                         ioapic_write_entry(apic, pin, entry);
615                 }
616
617                 raw_spin_lock_irqsave(&ioapic_lock, flags);
618                 x86_io_apic_ops.eoi_ioapic_pin(apic, pin, entry.vector);
619                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
620         }
621
622         /*
623          * Clear the rest of the bits in the IO-APIC RTE except for the mask
624          * bit.
625          */
626         ioapic_mask_entry(apic, pin);
627         entry = ioapic_read_entry(apic, pin);
628         if (entry.irr)
629                 pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
630                        mpc_ioapic_id(apic), pin);
631 }
632
633 static void clear_IO_APIC (void)
634 {
635         int apic, pin;
636
637         for_each_ioapic_pin(apic, pin)
638                 clear_IO_APIC_pin(apic, pin);
639 }
640
641 #ifdef CONFIG_X86_32
642 /*
643  * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
644  * specific CPU-side IRQs.
645  */
646
647 #define MAX_PIRQS 8
648 static int pirq_entries[MAX_PIRQS] = {
649         [0 ... MAX_PIRQS - 1] = -1
650 };
651
652 static int __init ioapic_pirq_setup(char *str)
653 {
654         int i, max;
655         int ints[MAX_PIRQS+1];
656
657         get_options(str, ARRAY_SIZE(ints), ints);
658
659         apic_printk(APIC_VERBOSE, KERN_INFO
660                         "PIRQ redirection, working around broken MP-BIOS.\n");
661         max = MAX_PIRQS;
662         if (ints[0] < MAX_PIRQS)
663                 max = ints[0];
664
665         for (i = 0; i < max; i++) {
666                 apic_printk(APIC_VERBOSE, KERN_DEBUG
667                                 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
668                 /*
669                  * PIRQs are mapped upside down, usually.
670                  */
671                 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
672         }
673         return 1;
674 }
675
676 __setup("pirq=", ioapic_pirq_setup);
677 #endif /* CONFIG_X86_32 */
678
679 /*
680  * Saves all the IO-APIC RTE's
681  */
682 int save_ioapic_entries(void)
683 {
684         int apic, pin;
685         int err = 0;
686
687         for_each_ioapic(apic) {
688                 if (!ioapics[apic].saved_registers) {
689                         err = -ENOMEM;
690                         continue;
691                 }
692
693                 for_each_pin(apic, pin)
694                         ioapics[apic].saved_registers[pin] =
695                                 ioapic_read_entry(apic, pin);
696         }
697
698         return err;
699 }
700
701 /*
702  * Mask all IO APIC entries.
703  */
704 void mask_ioapic_entries(void)
705 {
706         int apic, pin;
707
708         for_each_ioapic(apic) {
709                 if (!ioapics[apic].saved_registers)
710                         continue;
711
712                 for_each_pin(apic, pin) {
713                         struct IO_APIC_route_entry entry;
714
715                         entry = ioapics[apic].saved_registers[pin];
716                         if (!entry.mask) {
717                                 entry.mask = 1;
718                                 ioapic_write_entry(apic, pin, entry);
719                         }
720                 }
721         }
722 }
723
724 /*
725  * Restore IO APIC entries which was saved in the ioapic structure.
726  */
727 int restore_ioapic_entries(void)
728 {
729         int apic, pin;
730
731         for_each_ioapic(apic) {
732                 if (!ioapics[apic].saved_registers)
733                         continue;
734
735                 for_each_pin(apic, pin)
736                         ioapic_write_entry(apic, pin,
737                                            ioapics[apic].saved_registers[pin]);
738         }
739         return 0;
740 }
741
742 /*
743  * Find the IRQ entry number of a certain pin.
744  */
745 static int find_irq_entry(int ioapic_idx, int pin, int type)
746 {
747         int i;
748
749         for (i = 0; i < mp_irq_entries; i++)
750                 if (mp_irqs[i].irqtype == type &&
751                     (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
752                      mp_irqs[i].dstapic == MP_APIC_ALL) &&
753                     mp_irqs[i].dstirq == pin)
754                         return i;
755
756         return -1;
757 }
758
759 /*
760  * Find the pin to which IRQ[irq] (ISA) is connected
761  */
762 static int __init find_isa_irq_pin(int irq, int type)
763 {
764         int i;
765
766         for (i = 0; i < mp_irq_entries; i++) {
767                 int lbus = mp_irqs[i].srcbus;
768
769                 if (test_bit(lbus, mp_bus_not_pci) &&
770                     (mp_irqs[i].irqtype == type) &&
771                     (mp_irqs[i].srcbusirq == irq))
772
773                         return mp_irqs[i].dstirq;
774         }
775         return -1;
776 }
777
778 static int __init find_isa_irq_apic(int irq, int type)
779 {
780         int i;
781
782         for (i = 0; i < mp_irq_entries; i++) {
783                 int lbus = mp_irqs[i].srcbus;
784
785                 if (test_bit(lbus, mp_bus_not_pci) &&
786                     (mp_irqs[i].irqtype == type) &&
787                     (mp_irqs[i].srcbusirq == irq))
788                         break;
789         }
790
791         if (i < mp_irq_entries) {
792                 int ioapic_idx;
793
794                 for_each_ioapic(ioapic_idx)
795                         if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
796                                 return ioapic_idx;
797         }
798
799         return -1;
800 }
801
802 #ifdef CONFIG_EISA
803 /*
804  * EISA Edge/Level control register, ELCR
805  */
806 static int EISA_ELCR(unsigned int irq)
807 {
808         if (irq < nr_legacy_irqs()) {
809                 unsigned int port = 0x4d0 + (irq >> 3);
810                 return (inb(port) >> (irq & 7)) & 1;
811         }
812         apic_printk(APIC_VERBOSE, KERN_INFO
813                         "Broken MPtable reports ISA irq %d\n", irq);
814         return 0;
815 }
816
817 #endif
818
819 /* ISA interrupts are always polarity zero edge triggered,
820  * when listed as conforming in the MP table. */
821
822 #define default_ISA_trigger(idx)        (0)
823 #define default_ISA_polarity(idx)       (0)
824
825 /* EISA interrupts are always polarity zero and can be edge or level
826  * trigger depending on the ELCR value.  If an interrupt is listed as
827  * EISA conforming in the MP table, that means its trigger type must
828  * be read in from the ELCR */
829
830 #define default_EISA_trigger(idx)       (EISA_ELCR(mp_irqs[idx].srcbusirq))
831 #define default_EISA_polarity(idx)      default_ISA_polarity(idx)
832
833 /* PCI interrupts are always polarity one level triggered,
834  * when listed as conforming in the MP table. */
835
836 #define default_PCI_trigger(idx)        (1)
837 #define default_PCI_polarity(idx)       (1)
838
839 static int irq_polarity(int idx)
840 {
841         int bus = mp_irqs[idx].srcbus;
842         int polarity;
843
844         /*
845          * Determine IRQ line polarity (high active or low active):
846          */
847         switch (mp_irqs[idx].irqflag & 3)
848         {
849                 case 0: /* conforms, ie. bus-type dependent polarity */
850                         if (test_bit(bus, mp_bus_not_pci))
851                                 polarity = default_ISA_polarity(idx);
852                         else
853                                 polarity = default_PCI_polarity(idx);
854                         break;
855                 case 1: /* high active */
856                 {
857                         polarity = 0;
858                         break;
859                 }
860                 case 2: /* reserved */
861                 {
862                         pr_warn("broken BIOS!!\n");
863                         polarity = 1;
864                         break;
865                 }
866                 case 3: /* low active */
867                 {
868                         polarity = 1;
869                         break;
870                 }
871                 default: /* invalid */
872                 {
873                         pr_warn("broken BIOS!!\n");
874                         polarity = 1;
875                         break;
876                 }
877         }
878         return polarity;
879 }
880
881 static int irq_trigger(int idx)
882 {
883         int bus = mp_irqs[idx].srcbus;
884         int trigger;
885
886         /*
887          * Determine IRQ trigger mode (edge or level sensitive):
888          */
889         switch ((mp_irqs[idx].irqflag>>2) & 3)
890         {
891                 case 0: /* conforms, ie. bus-type dependent */
892                         if (test_bit(bus, mp_bus_not_pci))
893                                 trigger = default_ISA_trigger(idx);
894                         else
895                                 trigger = default_PCI_trigger(idx);
896 #ifdef CONFIG_EISA
897                         switch (mp_bus_id_to_type[bus]) {
898                                 case MP_BUS_ISA: /* ISA pin */
899                                 {
900                                         /* set before the switch */
901                                         break;
902                                 }
903                                 case MP_BUS_EISA: /* EISA pin */
904                                 {
905                                         trigger = default_EISA_trigger(idx);
906                                         break;
907                                 }
908                                 case MP_BUS_PCI: /* PCI pin */
909                                 {
910                                         /* set before the switch */
911                                         break;
912                                 }
913                                 default:
914                                 {
915                                         pr_warn("broken BIOS!!\n");
916                                         trigger = 1;
917                                         break;
918                                 }
919                         }
920 #endif
921                         break;
922                 case 1: /* edge */
923                 {
924                         trigger = 0;
925                         break;
926                 }
927                 case 2: /* reserved */
928                 {
929                         pr_warn("broken BIOS!!\n");
930                         trigger = 1;
931                         break;
932                 }
933                 case 3: /* level */
934                 {
935                         trigger = 1;
936                         break;
937                 }
938                 default: /* invalid */
939                 {
940                         pr_warn("broken BIOS!!\n");
941                         trigger = 0;
942                         break;
943                 }
944         }
945         return trigger;
946 }
947
948 static int alloc_irq_from_domain(struct irq_domain *domain, u32 gsi, int pin)
949 {
950         int irq = -1;
951         int ioapic = (int)(long)domain->host_data;
952         int type = ioapics[ioapic].irqdomain_cfg.type;
953
954         switch (type) {
955         case IOAPIC_DOMAIN_LEGACY:
956                 /*
957                  * Dynamically allocate IRQ number for non-ISA IRQs in the first 16
958                  * GSIs on some weird platforms.
959                  */
960                 if (gsi < nr_legacy_irqs())
961                         irq = irq_create_mapping(domain, pin);
962                 else if (irq_create_strict_mappings(domain, gsi, pin, 1) == 0)
963                         irq = gsi;
964                 break;
965         case IOAPIC_DOMAIN_STRICT:
966                 if (irq_create_strict_mappings(domain, gsi, pin, 1) == 0)
967                         irq = gsi;
968                 break;
969         case IOAPIC_DOMAIN_DYNAMIC:
970                 irq = irq_create_mapping(domain, pin);
971                 break;
972         default:
973                 WARN(1, "ioapic: unknown irqdomain type %d\n", type);
974                 break;
975         }
976
977         return irq > 0 ? irq : -1;
978 }
979
980 static int mp_map_pin_to_irq(u32 gsi, int idx, int ioapic, int pin,
981                              unsigned int flags)
982 {
983         int irq;
984         struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
985         struct mp_pin_info *info = mp_pin_info(ioapic, pin);
986
987         if (!domain)
988                 return -1;
989
990         mutex_lock(&ioapic_mutex);
991
992         /*
993          * Don't use irqdomain to manage ISA IRQs because there may be
994          * multiple IOAPIC pins sharing the same ISA IRQ number and
995          * irqdomain only supports 1:1 mapping between IOAPIC pin and
996          * IRQ number. A typical IOAPIC has 24 pins, pin 0-15 are used
997          * for legacy IRQs and pin 16-23 are used for PCI IRQs (PIRQ A-H).
998          * When ACPI is disabled, only legacy IRQ numbers (IRQ0-15) are
999          * available, and some BIOSes may use MP Interrupt Source records
1000          * to override IRQ numbers for PIRQs instead of reprogramming
1001          * the interrupt routing logic. Thus there may be multiple pins
1002          * sharing the same legacy IRQ number when ACPI is disabled.
1003          */
1004         if (idx >= 0 && test_bit(mp_irqs[idx].srcbus, mp_bus_not_pci)) {
1005                 irq = mp_irqs[idx].srcbusirq;
1006                 if (flags & IOAPIC_MAP_ALLOC) {
1007                         if (info->count == 0 &&
1008                             mp_irqdomain_map(domain, irq, pin) != 0)
1009                                 irq = -1;
1010
1011                         /* special handling for timer IRQ0 */
1012                         if (irq == 0)
1013                                 info->count++;
1014                 }
1015         } else {
1016                 irq = irq_find_mapping(domain, pin);
1017                 if (irq <= 0 && (flags & IOAPIC_MAP_ALLOC))
1018                         irq = alloc_irq_from_domain(domain, gsi, pin);
1019         }
1020
1021         if (flags & IOAPIC_MAP_ALLOC) {
1022                 /* special handling for legacy IRQs */
1023                 if (irq < nr_legacy_irqs() && info->count == 1 &&
1024                     mp_irqdomain_map(domain, irq, pin) != 0)
1025                         irq = -1;
1026
1027                 if (irq > 0)
1028                         info->count++;
1029                 else if (info->count == 0)
1030                         info->set = 0;
1031         }
1032
1033         mutex_unlock(&ioapic_mutex);
1034
1035         return irq > 0 ? irq : -1;
1036 }
1037
1038 static int pin_2_irq(int idx, int ioapic, int pin, unsigned int flags)
1039 {
1040         u32 gsi = mp_pin_to_gsi(ioapic, pin);
1041
1042         /*
1043          * Debugging check, we are in big trouble if this message pops up!
1044          */
1045         if (mp_irqs[idx].dstirq != pin)
1046                 pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
1047
1048 #ifdef CONFIG_X86_32
1049         /*
1050          * PCI IRQ command line redirection. Yes, limits are hardcoded.
1051          */
1052         if ((pin >= 16) && (pin <= 23)) {
1053                 if (pirq_entries[pin-16] != -1) {
1054                         if (!pirq_entries[pin-16]) {
1055                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
1056                                                 "disabling PIRQ%d\n", pin-16);
1057                         } else {
1058                                 int irq = pirq_entries[pin-16];
1059                                 apic_printk(APIC_VERBOSE, KERN_DEBUG
1060                                                 "using PIRQ%d -> IRQ %d\n",
1061                                                 pin-16, irq);
1062                                 return irq;
1063                         }
1064                 }
1065         }
1066 #endif
1067
1068         return  mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags);
1069 }
1070
1071 int mp_map_gsi_to_irq(u32 gsi, unsigned int flags)
1072 {
1073         int ioapic, pin, idx;
1074
1075         ioapic = mp_find_ioapic(gsi);
1076         if (ioapic < 0)
1077                 return -1;
1078
1079         pin = mp_find_ioapic_pin(ioapic, gsi);
1080         idx = find_irq_entry(ioapic, pin, mp_INT);
1081         if ((flags & IOAPIC_MAP_CHECK) && idx < 0)
1082                 return -1;
1083
1084         return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags);
1085 }
1086
1087 void mp_unmap_irq(int irq)
1088 {
1089         struct irq_data *data = irq_get_irq_data(irq);
1090         struct mp_pin_info *info;
1091         int ioapic, pin;
1092
1093         if (!data || !data->domain)
1094                 return;
1095
1096         ioapic = (int)(long)data->domain->host_data;
1097         pin = (int)data->hwirq;
1098         info = mp_pin_info(ioapic, pin);
1099
1100         mutex_lock(&ioapic_mutex);
1101         if (--info->count == 0) {
1102                 info->set = 0;
1103                 if (irq < nr_legacy_irqs() &&
1104                     ioapics[ioapic].irqdomain_cfg.type == IOAPIC_DOMAIN_LEGACY)
1105                         mp_irqdomain_unmap(data->domain, irq);
1106                 else
1107                         irq_dispose_mapping(irq);
1108         }
1109         mutex_unlock(&ioapic_mutex);
1110 }
1111
1112 /*
1113  * Find a specific PCI IRQ entry.
1114  * Not an __init, possibly needed by modules
1115  */
1116 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
1117 {
1118         int irq, i, best_ioapic = -1, best_idx = -1;
1119
1120         apic_printk(APIC_DEBUG,
1121                     "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1122                     bus, slot, pin);
1123         if (test_bit(bus, mp_bus_not_pci)) {
1124                 apic_printk(APIC_VERBOSE,
1125                             "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1126                 return -1;
1127         }
1128
1129         for (i = 0; i < mp_irq_entries; i++) {
1130                 int lbus = mp_irqs[i].srcbus;
1131                 int ioapic_idx, found = 0;
1132
1133                 if (bus != lbus || mp_irqs[i].irqtype != mp_INT ||
1134                     slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f))
1135                         continue;
1136
1137                 for_each_ioapic(ioapic_idx)
1138                         if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
1139                             mp_irqs[i].dstapic == MP_APIC_ALL) {
1140                                 found = 1;
1141                                 break;
1142                         }
1143                 if (!found)
1144                         continue;
1145
1146                 /* Skip ISA IRQs */
1147                 irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq, 0);
1148                 if (irq > 0 && !IO_APIC_IRQ(irq))
1149                         continue;
1150
1151                 if (pin == (mp_irqs[i].srcbusirq & 3)) {
1152                         best_idx = i;
1153                         best_ioapic = ioapic_idx;
1154                         goto out;
1155                 }
1156
1157                 /*
1158                  * Use the first all-but-pin matching entry as a
1159                  * best-guess fuzzy result for broken mptables.
1160                  */
1161                 if (best_idx < 0) {
1162                         best_idx = i;
1163                         best_ioapic = ioapic_idx;
1164                 }
1165         }
1166         if (best_idx < 0)
1167                 return -1;
1168
1169 out:
1170         return pin_2_irq(best_idx, best_ioapic, mp_irqs[best_idx].dstirq,
1171                          IOAPIC_MAP_ALLOC);
1172 }
1173 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1174
1175 static struct irq_chip ioapic_chip;
1176
1177 #ifdef CONFIG_X86_32
1178 static inline int IO_APIC_irq_trigger(int irq)
1179 {
1180         int apic, idx, pin;
1181
1182         for_each_ioapic_pin(apic, pin) {
1183                 idx = find_irq_entry(apic, pin, mp_INT);
1184                 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin, 0)))
1185                         return irq_trigger(idx);
1186         }
1187         /*
1188          * nonexistent IRQs are edge default
1189          */
1190         return 0;
1191 }
1192 #else
1193 static inline int IO_APIC_irq_trigger(int irq)
1194 {
1195         return 1;
1196 }
1197 #endif
1198
1199 static void ioapic_register_intr(unsigned int irq, struct irq_cfg *cfg,
1200                                  unsigned long trigger)
1201 {
1202         struct irq_chip *chip = &ioapic_chip;
1203         irq_flow_handler_t hdl;
1204         bool fasteoi;
1205
1206         if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1207             trigger == IOAPIC_LEVEL) {
1208                 irq_set_status_flags(irq, IRQ_LEVEL);
1209                 fasteoi = true;
1210         } else {
1211                 irq_clear_status_flags(irq, IRQ_LEVEL);
1212                 fasteoi = false;
1213         }
1214
1215         if (setup_remapped_irq(irq, cfg, chip))
1216                 fasteoi = trigger != 0;
1217
1218         hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
1219         irq_set_chip_and_handler_name(irq, chip, hdl,
1220                                       fasteoi ? "fasteoi" : "edge");
1221 }
1222
1223 int native_setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
1224                               unsigned int destination, int vector,
1225                               struct io_apic_irq_attr *attr)
1226 {
1227         memset(entry, 0, sizeof(*entry));
1228
1229         entry->delivery_mode = apic->irq_delivery_mode;
1230         entry->dest_mode     = apic->irq_dest_mode;
1231         entry->dest          = destination;
1232         entry->vector        = vector;
1233         entry->mask          = 0;                       /* enable IRQ */
1234         entry->trigger       = attr->trigger;
1235         entry->polarity      = attr->polarity;
1236
1237         /*
1238          * Mask level triggered irqs.
1239          * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1240          */
1241         if (attr->trigger)
1242                 entry->mask = 1;
1243
1244         return 0;
1245 }
1246
1247 static void setup_ioapic_irq(unsigned int irq, struct irq_cfg *cfg,
1248                                 struct io_apic_irq_attr *attr)
1249 {
1250         struct IO_APIC_route_entry entry;
1251         unsigned int dest;
1252
1253         if (!IO_APIC_IRQ(irq))
1254                 return;
1255
1256         if (assign_irq_vector(irq, cfg, apic->target_cpus()))
1257                 return;
1258
1259         if (apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus(),
1260                                          &dest)) {
1261                 pr_warn("Failed to obtain apicid for ioapic %d, pin %d\n",
1262                         mpc_ioapic_id(attr->ioapic), attr->ioapic_pin);
1263                 clear_irq_vector(irq, cfg);
1264
1265                 return;
1266         }
1267
1268         apic_printk(APIC_VERBOSE,KERN_DEBUG
1269                     "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1270                     "IRQ %d Mode:%i Active:%i Dest:%d)\n",
1271                     attr->ioapic, mpc_ioapic_id(attr->ioapic), attr->ioapic_pin,
1272                     cfg->vector, irq, attr->trigger, attr->polarity, dest);
1273
1274         if (x86_io_apic_ops.setup_entry(irq, &entry, dest, cfg->vector, attr)) {
1275                 pr_warn("Failed to setup ioapic entry for ioapic  %d, pin %d\n",
1276                         mpc_ioapic_id(attr->ioapic), attr->ioapic_pin);
1277                 clear_irq_vector(irq, cfg);
1278
1279                 return;
1280         }
1281
1282         ioapic_register_intr(irq, cfg, attr->trigger);
1283         if (irq < nr_legacy_irqs())
1284                 legacy_pic->mask(irq);
1285
1286         ioapic_write_entry(attr->ioapic, attr->ioapic_pin, entry);
1287 }
1288
1289 static void __init setup_IO_APIC_irqs(void)
1290 {
1291         unsigned int ioapic, pin;
1292         int idx;
1293
1294         apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1295
1296         for_each_ioapic_pin(ioapic, pin) {
1297                 idx = find_irq_entry(ioapic, pin, mp_INT);
1298                 if (idx < 0)
1299                         apic_printk(APIC_VERBOSE,
1300                                     KERN_DEBUG " apic %d pin %d not connected\n",
1301                                     mpc_ioapic_id(ioapic), pin);
1302                 else
1303                         pin_2_irq(idx, ioapic, pin,
1304                                   ioapic ? 0 : IOAPIC_MAP_ALLOC);
1305         }
1306 }
1307
1308 /*
1309  * Set up the timer pin, possibly with the 8259A-master behind.
1310  */
1311 static void __init setup_timer_IRQ0_pin(unsigned int ioapic_idx,
1312                                         unsigned int pin, int vector)
1313 {
1314         struct IO_APIC_route_entry entry;
1315         unsigned int dest;
1316
1317         memset(&entry, 0, sizeof(entry));
1318
1319         /*
1320          * We use logical delivery to get the timer IRQ
1321          * to the first CPU.
1322          */
1323         if (unlikely(apic->cpu_mask_to_apicid_and(apic->target_cpus(),
1324                                                   apic->target_cpus(), &dest)))
1325                 dest = BAD_APICID;
1326
1327         entry.dest_mode = apic->irq_dest_mode;
1328         entry.mask = 0;                 /* don't mask IRQ for edge */
1329         entry.dest = dest;
1330         entry.delivery_mode = apic->irq_delivery_mode;
1331         entry.polarity = 0;
1332         entry.trigger = 0;
1333         entry.vector = vector;
1334
1335         /*
1336          * The timer IRQ doesn't have to know that behind the
1337          * scene we may have a 8259A-master in AEOI mode ...
1338          */
1339         irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
1340                                       "edge");
1341
1342         /*
1343          * Add it to the IO-APIC irq-routing table:
1344          */
1345         ioapic_write_entry(ioapic_idx, pin, entry);
1346 }
1347
1348 void native_io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
1349 {
1350         int i;
1351
1352         pr_debug(" NR Dst Mask Trig IRR Pol Stat Dmod Deli Vect:\n");
1353
1354         for (i = 0; i <= nr_entries; i++) {
1355                 struct IO_APIC_route_entry entry;
1356
1357                 entry = ioapic_read_entry(apic, i);
1358
1359                 pr_debug(" %02x %02X  ", i, entry.dest);
1360                 pr_cont("%1d    %1d    %1d   %1d   %1d    "
1361                         "%1d    %1d    %02X\n",
1362                         entry.mask,
1363                         entry.trigger,
1364                         entry.irr,
1365                         entry.polarity,
1366                         entry.delivery_status,
1367                         entry.dest_mode,
1368                         entry.delivery_mode,
1369                         entry.vector);
1370         }
1371 }
1372
1373 void intel_ir_io_apic_print_entries(unsigned int apic,
1374                                     unsigned int nr_entries)
1375 {
1376         int i;
1377
1378         pr_debug(" NR Indx Fmt Mask Trig IRR Pol Stat Indx2 Zero Vect:\n");
1379
1380         for (i = 0; i <= nr_entries; i++) {
1381                 struct IR_IO_APIC_route_entry *ir_entry;
1382                 struct IO_APIC_route_entry entry;
1383
1384                 entry = ioapic_read_entry(apic, i);
1385
1386                 ir_entry = (struct IR_IO_APIC_route_entry *)&entry;
1387
1388                 pr_debug(" %02x %04X ", i, ir_entry->index);
1389                 pr_cont("%1d   %1d    %1d    %1d   %1d   "
1390                         "%1d    %1d     %X    %02X\n",
1391                         ir_entry->format,
1392                         ir_entry->mask,
1393                         ir_entry->trigger,
1394                         ir_entry->irr,
1395                         ir_entry->polarity,
1396                         ir_entry->delivery_status,
1397                         ir_entry->index2,
1398                         ir_entry->zero,
1399                         ir_entry->vector);
1400         }
1401 }
1402
1403 void ioapic_zap_locks(void)
1404 {
1405         raw_spin_lock_init(&ioapic_lock);
1406 }
1407
1408 static void __init print_IO_APIC(int ioapic_idx)
1409 {
1410         union IO_APIC_reg_00 reg_00;
1411         union IO_APIC_reg_01 reg_01;
1412         union IO_APIC_reg_02 reg_02;
1413         union IO_APIC_reg_03 reg_03;
1414         unsigned long flags;
1415
1416         raw_spin_lock_irqsave(&ioapic_lock, flags);
1417         reg_00.raw = io_apic_read(ioapic_idx, 0);
1418         reg_01.raw = io_apic_read(ioapic_idx, 1);
1419         if (reg_01.bits.version >= 0x10)
1420                 reg_02.raw = io_apic_read(ioapic_idx, 2);
1421         if (reg_01.bits.version >= 0x20)
1422                 reg_03.raw = io_apic_read(ioapic_idx, 3);
1423         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1424
1425         printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
1426         printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1427         printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
1428         printk(KERN_DEBUG ".......    : Delivery Type: %X\n", reg_00.bits.delivery_type);
1429         printk(KERN_DEBUG ".......    : LTS          : %X\n", reg_00.bits.LTS);
1430
1431         printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1432         printk(KERN_DEBUG ".......     : max redirection entries: %02X\n",
1433                 reg_01.bits.entries);
1434
1435         printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
1436         printk(KERN_DEBUG ".......     : IO APIC version: %02X\n",
1437                 reg_01.bits.version);
1438
1439         /*
1440          * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1441          * but the value of reg_02 is read as the previous read register
1442          * value, so ignore it if reg_02 == reg_01.
1443          */
1444         if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1445                 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1446                 printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
1447         }
1448
1449         /*
1450          * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1451          * or reg_03, but the value of reg_0[23] is read as the previous read
1452          * register value, so ignore it if reg_03 == reg_0[12].
1453          */
1454         if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1455             reg_03.raw != reg_01.raw) {
1456                 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1457                 printk(KERN_DEBUG ".......     : Boot DT    : %X\n", reg_03.bits.boot_DT);
1458         }
1459
1460         printk(KERN_DEBUG ".... IRQ redirection table:\n");
1461
1462         x86_io_apic_ops.print_entries(ioapic_idx, reg_01.bits.entries);
1463 }
1464
1465 void __init print_IO_APICs(void)
1466 {
1467         int ioapic_idx;
1468         struct irq_cfg *cfg;
1469         unsigned int irq;
1470         struct irq_chip *chip;
1471
1472         printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1473         for_each_ioapic(ioapic_idx)
1474                 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1475                        mpc_ioapic_id(ioapic_idx),
1476                        ioapics[ioapic_idx].nr_registers);
1477
1478         /*
1479          * We are a bit conservative about what we expect.  We have to
1480          * know about every hardware change ASAP.
1481          */
1482         printk(KERN_INFO "testing the IO APIC.......................\n");
1483
1484         for_each_ioapic(ioapic_idx)
1485                 print_IO_APIC(ioapic_idx);
1486
1487         printk(KERN_DEBUG "IRQ to pin mappings:\n");
1488         for_each_active_irq(irq) {
1489                 struct irq_pin_list *entry;
1490
1491                 chip = irq_get_chip(irq);
1492                 if (chip != &ioapic_chip)
1493                         continue;
1494
1495                 cfg = irq_cfg(irq);
1496                 if (!cfg)
1497                         continue;
1498                 if (list_empty(&cfg->irq_2_pin))
1499                         continue;
1500                 printk(KERN_DEBUG "IRQ%d ", irq);
1501                 for_each_irq_pin(entry, cfg->irq_2_pin)
1502                         pr_cont("-> %d:%d", entry->apic, entry->pin);
1503                 pr_cont("\n");
1504         }
1505
1506         printk(KERN_INFO ".................................... done.\n");
1507 }
1508
1509 /* Where if anywhere is the i8259 connect in external int mode */
1510 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1511
1512 void __init enable_IO_APIC(void)
1513 {
1514         int i8259_apic, i8259_pin;
1515         int apic, pin;
1516
1517         if (!nr_legacy_irqs())
1518                 return;
1519
1520         for_each_ioapic_pin(apic, pin) {
1521                 /* See if any of the pins is in ExtINT mode */
1522                 struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin);
1523
1524                 /* If the interrupt line is enabled and in ExtInt mode
1525                  * I have found the pin where the i8259 is connected.
1526                  */
1527                 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1528                         ioapic_i8259.apic = apic;
1529                         ioapic_i8259.pin  = pin;
1530                         goto found_i8259;
1531                 }
1532         }
1533  found_i8259:
1534         /* Look to see what if the MP table has reported the ExtINT */
1535         /* If we could not find the appropriate pin by looking at the ioapic
1536          * the i8259 probably is not connected the ioapic but give the
1537          * mptable a chance anyway.
1538          */
1539         i8259_pin  = find_isa_irq_pin(0, mp_ExtINT);
1540         i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1541         /* Trust the MP table if nothing is setup in the hardware */
1542         if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1543                 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1544                 ioapic_i8259.pin  = i8259_pin;
1545                 ioapic_i8259.apic = i8259_apic;
1546         }
1547         /* Complain if the MP table and the hardware disagree */
1548         if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1549                 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1550         {
1551                 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1552         }
1553
1554         /*
1555          * Do not trust the IO-APIC being empty at bootup
1556          */
1557         clear_IO_APIC();
1558 }
1559
1560 void native_disable_io_apic(void)
1561 {
1562         /*
1563          * If the i8259 is routed through an IOAPIC
1564          * Put that IOAPIC in virtual wire mode
1565          * so legacy interrupts can be delivered.
1566          */
1567         if (ioapic_i8259.pin != -1) {
1568                 struct IO_APIC_route_entry entry;
1569
1570                 memset(&entry, 0, sizeof(entry));
1571                 entry.mask            = 0; /* Enabled */
1572                 entry.trigger         = 0; /* Edge */
1573                 entry.irr             = 0;
1574                 entry.polarity        = 0; /* High */
1575                 entry.delivery_status = 0;
1576                 entry.dest_mode       = 0; /* Physical */
1577                 entry.delivery_mode   = dest_ExtINT; /* ExtInt */
1578                 entry.vector          = 0;
1579                 entry.dest            = read_apic_id();
1580
1581                 /*
1582                  * Add it to the IO-APIC irq-routing table:
1583                  */
1584                 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1585         }
1586
1587         if (cpu_has_apic || apic_from_smp_config())
1588                 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1589
1590 }
1591
1592 /*
1593  * Not an __init, needed by the reboot code
1594  */
1595 void disable_IO_APIC(void)
1596 {
1597         /*
1598          * Clear the IO-APIC before rebooting:
1599          */
1600         clear_IO_APIC();
1601
1602         if (!nr_legacy_irqs())
1603                 return;
1604
1605         x86_io_apic_ops.disable();
1606 }
1607
1608 #ifdef CONFIG_X86_32
1609 /*
1610  * function to set the IO-APIC physical IDs based on the
1611  * values stored in the MPC table.
1612  *
1613  * by Matt Domsch <Matt_Domsch@dell.com>  Tue Dec 21 12:25:05 CST 1999
1614  */
1615 void __init setup_ioapic_ids_from_mpc_nocheck(void)
1616 {
1617         union IO_APIC_reg_00 reg_00;
1618         physid_mask_t phys_id_present_map;
1619         int ioapic_idx;
1620         int i;
1621         unsigned char old_id;
1622         unsigned long flags;
1623
1624         /*
1625          * This is broken; anything with a real cpu count has to
1626          * circumvent this idiocy regardless.
1627          */
1628         apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
1629
1630         /*
1631          * Set the IOAPIC ID to the value stored in the MPC table.
1632          */
1633         for_each_ioapic(ioapic_idx) {
1634                 /* Read the register 0 value */
1635                 raw_spin_lock_irqsave(&ioapic_lock, flags);
1636                 reg_00.raw = io_apic_read(ioapic_idx, 0);
1637                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1638
1639                 old_id = mpc_ioapic_id(ioapic_idx);
1640
1641                 if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
1642                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1643                                 ioapic_idx, mpc_ioapic_id(ioapic_idx));
1644                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1645                                 reg_00.bits.ID);
1646                         ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
1647                 }
1648
1649                 /*
1650                  * Sanity check, is the ID really free? Every APIC in a
1651                  * system must have a unique ID or we get lots of nice
1652                  * 'stuck on smp_invalidate_needed IPI wait' messages.
1653                  */
1654                 if (apic->check_apicid_used(&phys_id_present_map,
1655                                             mpc_ioapic_id(ioapic_idx))) {
1656                         printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1657                                 ioapic_idx, mpc_ioapic_id(ioapic_idx));
1658                         for (i = 0; i < get_physical_broadcast(); i++)
1659                                 if (!physid_isset(i, phys_id_present_map))
1660                                         break;
1661                         if (i >= get_physical_broadcast())
1662                                 panic("Max APIC ID exceeded!\n");
1663                         printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1664                                 i);
1665                         physid_set(i, phys_id_present_map);
1666                         ioapics[ioapic_idx].mp_config.apicid = i;
1667                 } else {
1668                         physid_mask_t tmp;
1669                         apic->apicid_to_cpu_present(mpc_ioapic_id(ioapic_idx),
1670                                                     &tmp);
1671                         apic_printk(APIC_VERBOSE, "Setting %d in the "
1672                                         "phys_id_present_map\n",
1673                                         mpc_ioapic_id(ioapic_idx));
1674                         physids_or(phys_id_present_map, phys_id_present_map, tmp);
1675                 }
1676
1677                 /*
1678                  * We need to adjust the IRQ routing table
1679                  * if the ID changed.
1680                  */
1681                 if (old_id != mpc_ioapic_id(ioapic_idx))
1682                         for (i = 0; i < mp_irq_entries; i++)
1683                                 if (mp_irqs[i].dstapic == old_id)
1684                                         mp_irqs[i].dstapic
1685                                                 = mpc_ioapic_id(ioapic_idx);
1686
1687                 /*
1688                  * Update the ID register according to the right value
1689                  * from the MPC table if they are different.
1690                  */
1691                 if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
1692                         continue;
1693
1694                 apic_printk(APIC_VERBOSE, KERN_INFO
1695                         "...changing IO-APIC physical APIC ID to %d ...",
1696                         mpc_ioapic_id(ioapic_idx));
1697
1698                 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
1699                 raw_spin_lock_irqsave(&ioapic_lock, flags);
1700                 io_apic_write(ioapic_idx, 0, reg_00.raw);
1701                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1702
1703                 /*
1704                  * Sanity check
1705                  */
1706                 raw_spin_lock_irqsave(&ioapic_lock, flags);
1707                 reg_00.raw = io_apic_read(ioapic_idx, 0);
1708                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1709                 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
1710                         pr_cont("could not set ID!\n");
1711                 else
1712                         apic_printk(APIC_VERBOSE, " ok.\n");
1713         }
1714 }
1715
1716 void __init setup_ioapic_ids_from_mpc(void)
1717 {
1718
1719         if (acpi_ioapic)
1720                 return;
1721         /*
1722          * Don't check I/O APIC IDs for xAPIC systems.  They have
1723          * no meaning without the serial APIC bus.
1724          */
1725         if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1726                 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
1727                 return;
1728         setup_ioapic_ids_from_mpc_nocheck();
1729 }
1730 #endif
1731
1732 int no_timer_check __initdata;
1733
1734 static int __init notimercheck(char *s)
1735 {
1736         no_timer_check = 1;
1737         return 1;
1738 }
1739 __setup("no_timer_check", notimercheck);
1740
1741 /*
1742  * There is a nasty bug in some older SMP boards, their mptable lies
1743  * about the timer IRQ. We do the following to work around the situation:
1744  *
1745  *      - timer IRQ defaults to IO-APIC IRQ
1746  *      - if this function detects that timer IRQs are defunct, then we fall
1747  *        back to ISA timer IRQs
1748  */
1749 static int __init timer_irq_works(void)
1750 {
1751         unsigned long t1 = jiffies;
1752         unsigned long flags;
1753
1754         if (no_timer_check)
1755                 return 1;
1756
1757         local_save_flags(flags);
1758         local_irq_enable();
1759         /* Let ten ticks pass... */
1760         mdelay((10 * 1000) / HZ);
1761         local_irq_restore(flags);
1762
1763         /*
1764          * Expect a few ticks at least, to be sure some possible
1765          * glue logic does not lock up after one or two first
1766          * ticks in a non-ExtINT mode.  Also the local APIC
1767          * might have cached one ExtINT interrupt.  Finally, at
1768          * least one tick may be lost due to delays.
1769          */
1770
1771         /* jiffies wrap? */
1772         if (time_after(jiffies, t1 + 4))
1773                 return 1;
1774         return 0;
1775 }
1776
1777 /*
1778  * In the SMP+IOAPIC case it might happen that there are an unspecified
1779  * number of pending IRQ events unhandled. These cases are very rare,
1780  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1781  * better to do it this way as thus we do not have to be aware of
1782  * 'pending' interrupts in the IRQ path, except at this point.
1783  */
1784 /*
1785  * Edge triggered needs to resend any interrupt
1786  * that was delayed but this is now handled in the device
1787  * independent code.
1788  */
1789
1790 /*
1791  * Starting up a edge-triggered IO-APIC interrupt is
1792  * nasty - we need to make sure that we get the edge.
1793  * If it is already asserted for some reason, we need
1794  * return 1 to indicate that is was pending.
1795  *
1796  * This is not complete - we should be able to fake
1797  * an edge even if it isn't on the 8259A...
1798  */
1799
1800 static unsigned int startup_ioapic_irq(struct irq_data *data)
1801 {
1802         int was_pending = 0, irq = data->irq;
1803         unsigned long flags;
1804
1805         raw_spin_lock_irqsave(&ioapic_lock, flags);
1806         if (irq < nr_legacy_irqs()) {
1807                 legacy_pic->mask(irq);
1808                 if (legacy_pic->irq_pending(irq))
1809                         was_pending = 1;
1810         }
1811         __unmask_ioapic(data->chip_data);
1812         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1813
1814         return was_pending;
1815 }
1816
1817 /*
1818  * Level and edge triggered IO-APIC interrupts need different handling,
1819  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1820  * handled with the level-triggered descriptor, but that one has slightly
1821  * more overhead. Level-triggered interrupts cannot be handled with the
1822  * edge-triggered handler, without risking IRQ storms and other ugly
1823  * races.
1824  */
1825
1826 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
1827 {
1828         int apic, pin;
1829         struct irq_pin_list *entry;
1830         u8 vector = cfg->vector;
1831
1832         for_each_irq_pin(entry, cfg->irq_2_pin) {
1833                 unsigned int reg;
1834
1835                 apic = entry->apic;
1836                 pin = entry->pin;
1837
1838                 io_apic_write(apic, 0x11 + pin*2, dest);
1839                 reg = io_apic_read(apic, 0x10 + pin*2);
1840                 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
1841                 reg |= vector;
1842                 io_apic_modify(apic, 0x10 + pin*2, reg);
1843         }
1844 }
1845
1846 int native_ioapic_set_affinity(struct irq_data *data,
1847                                const struct cpumask *mask,
1848                                bool force)
1849 {
1850         unsigned int dest, irq = data->irq;
1851         unsigned long flags;
1852         int ret;
1853
1854         if (!config_enabled(CONFIG_SMP))
1855                 return -EPERM;
1856
1857         raw_spin_lock_irqsave(&ioapic_lock, flags);
1858         ret = apic_set_affinity(data, mask, &dest);
1859         if (!ret) {
1860                 /* Only the high 8 bits are valid. */
1861                 dest = SET_APIC_LOGICAL_ID(dest);
1862                 __target_IO_APIC_irq(irq, dest, data->chip_data);
1863                 ret = IRQ_SET_MASK_OK_NOCOPY;
1864         }
1865         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1866         return ret;
1867 }
1868
1869 atomic_t irq_mis_count;
1870
1871 #ifdef CONFIG_GENERIC_PENDING_IRQ
1872 static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
1873 {
1874         struct irq_pin_list *entry;
1875         unsigned long flags;
1876
1877         raw_spin_lock_irqsave(&ioapic_lock, flags);
1878         for_each_irq_pin(entry, cfg->irq_2_pin) {
1879                 unsigned int reg;
1880                 int pin;
1881
1882                 pin = entry->pin;
1883                 reg = io_apic_read(entry->apic, 0x10 + pin*2);
1884                 /* Is the remote IRR bit set? */
1885                 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
1886                         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1887                         return true;
1888                 }
1889         }
1890         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1891
1892         return false;
1893 }
1894
1895 static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
1896 {
1897         /* If we are moving the irq we need to mask it */
1898         if (unlikely(irqd_is_setaffinity_pending(data))) {
1899                 mask_ioapic(cfg);
1900                 return true;
1901         }
1902         return false;
1903 }
1904
1905 static inline void ioapic_irqd_unmask(struct irq_data *data,
1906                                       struct irq_cfg *cfg, bool masked)
1907 {
1908         if (unlikely(masked)) {
1909                 /* Only migrate the irq if the ack has been received.
1910                  *
1911                  * On rare occasions the broadcast level triggered ack gets
1912                  * delayed going to ioapics, and if we reprogram the
1913                  * vector while Remote IRR is still set the irq will never
1914                  * fire again.
1915                  *
1916                  * To prevent this scenario we read the Remote IRR bit
1917                  * of the ioapic.  This has two effects.
1918                  * - On any sane system the read of the ioapic will
1919                  *   flush writes (and acks) going to the ioapic from
1920                  *   this cpu.
1921                  * - We get to see if the ACK has actually been delivered.
1922                  *
1923                  * Based on failed experiments of reprogramming the
1924                  * ioapic entry from outside of irq context starting
1925                  * with masking the ioapic entry and then polling until
1926                  * Remote IRR was clear before reprogramming the
1927                  * ioapic I don't trust the Remote IRR bit to be
1928                  * completey accurate.
1929                  *
1930                  * However there appears to be no other way to plug
1931                  * this race, so if the Remote IRR bit is not
1932                  * accurate and is causing problems then it is a hardware bug
1933                  * and you can go talk to the chipset vendor about it.
1934                  */
1935                 if (!io_apic_level_ack_pending(cfg))
1936                         irq_move_masked_irq(data);
1937                 unmask_ioapic(cfg);
1938         }
1939 }
1940 #else
1941 static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
1942 {
1943         return false;
1944 }
1945 static inline void ioapic_irqd_unmask(struct irq_data *data,
1946                                       struct irq_cfg *cfg, bool masked)
1947 {
1948 }
1949 #endif
1950
1951 static void ack_ioapic_level(struct irq_data *data)
1952 {
1953         struct irq_cfg *cfg = data->chip_data;
1954         int i, irq = data->irq;
1955         unsigned long v;
1956         bool masked;
1957
1958         irq_complete_move(cfg);
1959         masked = ioapic_irqd_mask(data, cfg);
1960
1961         /*
1962          * It appears there is an erratum which affects at least version 0x11
1963          * of I/O APIC (that's the 82093AA and cores integrated into various
1964          * chipsets).  Under certain conditions a level-triggered interrupt is
1965          * erroneously delivered as edge-triggered one but the respective IRR
1966          * bit gets set nevertheless.  As a result the I/O unit expects an EOI
1967          * message but it will never arrive and further interrupts are blocked
1968          * from the source.  The exact reason is so far unknown, but the
1969          * phenomenon was observed when two consecutive interrupt requests
1970          * from a given source get delivered to the same CPU and the source is
1971          * temporarily disabled in between.
1972          *
1973          * A workaround is to simulate an EOI message manually.  We achieve it
1974          * by setting the trigger mode to edge and then to level when the edge
1975          * trigger mode gets detected in the TMR of a local APIC for a
1976          * level-triggered interrupt.  We mask the source for the time of the
1977          * operation to prevent an edge-triggered interrupt escaping meanwhile.
1978          * The idea is from Manfred Spraul.  --macro
1979          *
1980          * Also in the case when cpu goes offline, fixup_irqs() will forward
1981          * any unhandled interrupt on the offlined cpu to the new cpu
1982          * destination that is handling the corresponding interrupt. This
1983          * interrupt forwarding is done via IPI's. Hence, in this case also
1984          * level-triggered io-apic interrupt will be seen as an edge
1985          * interrupt in the IRR. And we can't rely on the cpu's EOI
1986          * to be broadcasted to the IO-APIC's which will clear the remoteIRR
1987          * corresponding to the level-triggered interrupt. Hence on IO-APIC's
1988          * supporting EOI register, we do an explicit EOI to clear the
1989          * remote IRR and on IO-APIC's which don't have an EOI register,
1990          * we use the above logic (mask+edge followed by unmask+level) from
1991          * Manfred Spraul to clear the remote IRR.
1992          */
1993         i = cfg->vector;
1994         v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1995
1996         /*
1997          * We must acknowledge the irq before we move it or the acknowledge will
1998          * not propagate properly.
1999          */
2000         ack_APIC_irq();
2001
2002         /*
2003          * Tail end of clearing remote IRR bit (either by delivering the EOI
2004          * message via io-apic EOI register write or simulating it using
2005          * mask+edge followed by unnask+level logic) manually when the
2006          * level triggered interrupt is seen as the edge triggered interrupt
2007          * at the cpu.
2008          */
2009         if (!(v & (1 << (i & 0x1f)))) {
2010                 atomic_inc(&irq_mis_count);
2011
2012                 eoi_ioapic_irq(irq, cfg);
2013         }
2014
2015         ioapic_irqd_unmask(data, cfg, masked);
2016 }
2017
2018 static struct irq_chip ioapic_chip __read_mostly = {
2019         .name                   = "IO-APIC",
2020         .irq_startup            = startup_ioapic_irq,
2021         .irq_mask               = mask_ioapic_irq,
2022         .irq_unmask             = unmask_ioapic_irq,
2023         .irq_ack                = apic_ack_edge,
2024         .irq_eoi                = ack_ioapic_level,
2025         .irq_set_affinity       = native_ioapic_set_affinity,
2026         .irq_retrigger          = apic_retrigger_irq,
2027         .flags                  = IRQCHIP_SKIP_SET_WAKE,
2028 };
2029
2030 static inline void init_IO_APIC_traps(void)
2031 {
2032         struct irq_cfg *cfg;
2033         unsigned int irq;
2034
2035         for_each_active_irq(irq) {
2036                 cfg = irq_cfg(irq);
2037                 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
2038                         /*
2039                          * Hmm.. We don't have an entry for this,
2040                          * so default to an old-fashioned 8259
2041                          * interrupt if we can..
2042                          */
2043                         if (irq < nr_legacy_irqs())
2044                                 legacy_pic->make_irq(irq);
2045                         else
2046                                 /* Strange. Oh, well.. */
2047                                 irq_set_chip(irq, &no_irq_chip);
2048                 }
2049         }
2050 }
2051
2052 /*
2053  * The local APIC irq-chip implementation:
2054  */
2055
2056 static void mask_lapic_irq(struct irq_data *data)
2057 {
2058         unsigned long v;
2059
2060         v = apic_read(APIC_LVT0);
2061         apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2062 }
2063
2064 static void unmask_lapic_irq(struct irq_data *data)
2065 {
2066         unsigned long v;
2067
2068         v = apic_read(APIC_LVT0);
2069         apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2070 }
2071
2072 static void ack_lapic_irq(struct irq_data *data)
2073 {
2074         ack_APIC_irq();
2075 }
2076
2077 static struct irq_chip lapic_chip __read_mostly = {
2078         .name           = "local-APIC",
2079         .irq_mask       = mask_lapic_irq,
2080         .irq_unmask     = unmask_lapic_irq,
2081         .irq_ack        = ack_lapic_irq,
2082 };
2083
2084 static void lapic_register_intr(int irq)
2085 {
2086         irq_clear_status_flags(irq, IRQ_LEVEL);
2087         irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2088                                       "edge");
2089 }
2090
2091 /*
2092  * This looks a bit hackish but it's about the only one way of sending
2093  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
2094  * not support the ExtINT mode, unfortunately.  We need to send these
2095  * cycles as some i82489DX-based boards have glue logic that keeps the
2096  * 8259A interrupt line asserted until INTA.  --macro
2097  */
2098 static inline void __init unlock_ExtINT_logic(void)
2099 {
2100         int apic, pin, i;
2101         struct IO_APIC_route_entry entry0, entry1;
2102         unsigned char save_control, save_freq_select;
2103
2104         pin  = find_isa_irq_pin(8, mp_INT);
2105         if (pin == -1) {
2106                 WARN_ON_ONCE(1);
2107                 return;
2108         }
2109         apic = find_isa_irq_apic(8, mp_INT);
2110         if (apic == -1) {
2111                 WARN_ON_ONCE(1);
2112                 return;
2113         }
2114
2115         entry0 = ioapic_read_entry(apic, pin);
2116         clear_IO_APIC_pin(apic, pin);
2117
2118         memset(&entry1, 0, sizeof(entry1));
2119
2120         entry1.dest_mode = 0;                   /* physical delivery */
2121         entry1.mask = 0;                        /* unmask IRQ now */
2122         entry1.dest = hard_smp_processor_id();
2123         entry1.delivery_mode = dest_ExtINT;
2124         entry1.polarity = entry0.polarity;
2125         entry1.trigger = 0;
2126         entry1.vector = 0;
2127
2128         ioapic_write_entry(apic, pin, entry1);
2129
2130         save_control = CMOS_READ(RTC_CONTROL);
2131         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2132         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2133                    RTC_FREQ_SELECT);
2134         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2135
2136         i = 100;
2137         while (i-- > 0) {
2138                 mdelay(10);
2139                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2140                         i -= 10;
2141         }
2142
2143         CMOS_WRITE(save_control, RTC_CONTROL);
2144         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2145         clear_IO_APIC_pin(apic, pin);
2146
2147         ioapic_write_entry(apic, pin, entry0);
2148 }
2149
2150 static int disable_timer_pin_1 __initdata;
2151 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
2152 static int __init disable_timer_pin_setup(char *arg)
2153 {
2154         disable_timer_pin_1 = 1;
2155         return 0;
2156 }
2157 early_param("disable_timer_pin_1", disable_timer_pin_setup);
2158
2159 /*
2160  * This code may look a bit paranoid, but it's supposed to cooperate with
2161  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
2162  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
2163  * fanatically on his truly buggy board.
2164  *
2165  * FIXME: really need to revamp this for all platforms.
2166  */
2167 static inline void __init check_timer(void)
2168 {
2169         struct irq_cfg *cfg = irq_cfg(0);
2170         int node = cpu_to_node(0);
2171         int apic1, pin1, apic2, pin2;
2172         unsigned long flags;
2173         int no_pin1 = 0;
2174
2175         local_irq_save(flags);
2176
2177         /*
2178          * get/set the timer IRQ vector:
2179          */
2180         legacy_pic->mask(0);
2181         assign_irq_vector(0, cfg, apic->target_cpus());
2182
2183         /*
2184          * As IRQ0 is to be enabled in the 8259A, the virtual
2185          * wire has to be disabled in the local APIC.  Also
2186          * timer interrupts need to be acknowledged manually in
2187          * the 8259A for the i82489DX when using the NMI
2188          * watchdog as that APIC treats NMIs as level-triggered.
2189          * The AEOI mode will finish them in the 8259A
2190          * automatically.
2191          */
2192         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2193         legacy_pic->init(1);
2194
2195         pin1  = find_isa_irq_pin(0, mp_INT);
2196         apic1 = find_isa_irq_apic(0, mp_INT);
2197         pin2  = ioapic_i8259.pin;
2198         apic2 = ioapic_i8259.apic;
2199
2200         apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2201                     "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2202                     cfg->vector, apic1, pin1, apic2, pin2);
2203
2204         /*
2205          * Some BIOS writers are clueless and report the ExtINTA
2206          * I/O APIC input from the cascaded 8259A as the timer
2207          * interrupt input.  So just in case, if only one pin
2208          * was found above, try it both directly and through the
2209          * 8259A.
2210          */
2211         if (pin1 == -1) {
2212                 panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
2213                 pin1 = pin2;
2214                 apic1 = apic2;
2215                 no_pin1 = 1;
2216         } else if (pin2 == -1) {
2217                 pin2 = pin1;
2218                 apic2 = apic1;
2219         }
2220
2221         if (pin1 != -1) {
2222                 /*
2223                  * Ok, does IRQ0 through the IOAPIC work?
2224                  */
2225                 if (no_pin1) {
2226                         add_pin_to_irq_node(cfg, node, apic1, pin1);
2227                         setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2228                 } else {
2229                         /* for edge trigger, setup_ioapic_irq already
2230                          * leave it unmasked.
2231                          * so only need to unmask if it is level-trigger
2232                          * do we really have level trigger timer?
2233                          */
2234                         int idx;
2235                         idx = find_irq_entry(apic1, pin1, mp_INT);
2236                         if (idx != -1 && irq_trigger(idx))
2237                                 unmask_ioapic(cfg);
2238                 }
2239                 if (timer_irq_works()) {
2240                         if (disable_timer_pin_1 > 0)
2241                                 clear_IO_APIC_pin(0, pin1);
2242                         goto out;
2243                 }
2244                 panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
2245                 local_irq_disable();
2246                 clear_IO_APIC_pin(apic1, pin1);
2247                 if (!no_pin1)
2248                         apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2249                                     "8254 timer not connected to IO-APIC\n");
2250
2251                 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2252                             "(IRQ0) through the 8259A ...\n");
2253                 apic_printk(APIC_QUIET, KERN_INFO
2254                             "..... (found apic %d pin %d) ...\n", apic2, pin2);
2255                 /*
2256                  * legacy devices should be connected to IO APIC #0
2257                  */
2258                 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
2259                 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2260                 legacy_pic->unmask(0);
2261                 if (timer_irq_works()) {
2262                         apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2263                         goto out;
2264                 }
2265                 /*
2266                  * Cleanup, just in case ...
2267                  */
2268                 local_irq_disable();
2269                 legacy_pic->mask(0);
2270                 clear_IO_APIC_pin(apic2, pin2);
2271                 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2272         }
2273
2274         apic_printk(APIC_QUIET, KERN_INFO
2275                     "...trying to set up timer as Virtual Wire IRQ...\n");
2276
2277         lapic_register_intr(0);
2278         apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector);     /* Fixed mode */
2279         legacy_pic->unmask(0);
2280
2281         if (timer_irq_works()) {
2282                 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2283                 goto out;
2284         }
2285         local_irq_disable();
2286         legacy_pic->mask(0);
2287         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2288         apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2289
2290         apic_printk(APIC_QUIET, KERN_INFO
2291                     "...trying to set up timer as ExtINT IRQ...\n");
2292
2293         legacy_pic->init(0);
2294         legacy_pic->make_irq(0);
2295         apic_write(APIC_LVT0, APIC_DM_EXTINT);
2296
2297         unlock_ExtINT_logic();
2298
2299         if (timer_irq_works()) {
2300                 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2301                 goto out;
2302         }
2303         local_irq_disable();
2304         apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2305         if (x2apic_preenabled)
2306                 apic_printk(APIC_QUIET, KERN_INFO
2307                             "Perhaps problem with the pre-enabled x2apic mode\n"
2308                             "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
2309         panic("IO-APIC + timer doesn't work!  Boot with apic=debug and send a "
2310                 "report.  Then try booting with the 'noapic' option.\n");
2311 out:
2312         local_irq_restore(flags);
2313 }
2314
2315 /*
2316  * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2317  * to devices.  However there may be an I/O APIC pin available for
2318  * this interrupt regardless.  The pin may be left unconnected, but
2319  * typically it will be reused as an ExtINT cascade interrupt for
2320  * the master 8259A.  In the MPS case such a pin will normally be
2321  * reported as an ExtINT interrupt in the MP table.  With ACPI
2322  * there is no provision for ExtINT interrupts, and in the absence
2323  * of an override it would be treated as an ordinary ISA I/O APIC
2324  * interrupt, that is edge-triggered and unmasked by default.  We
2325  * used to do this, but it caused problems on some systems because
2326  * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2327  * the same ExtINT cascade interrupt to drive the local APIC of the
2328  * bootstrap processor.  Therefore we refrain from routing IRQ2 to
2329  * the I/O APIC in all cases now.  No actual device should request
2330  * it anyway.  --macro
2331  */
2332 #define PIC_IRQS        (1UL << PIC_CASCADE_IR)
2333
2334 static int mp_irqdomain_create(int ioapic)
2335 {
2336         size_t size;
2337         int hwirqs = mp_ioapic_pin_count(ioapic);
2338         struct ioapic *ip = &ioapics[ioapic];
2339         struct ioapic_domain_cfg *cfg = &ip->irqdomain_cfg;
2340         struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2341
2342         size = sizeof(struct mp_pin_info) * mp_ioapic_pin_count(ioapic);
2343         ip->pin_info = kzalloc(size, GFP_KERNEL);
2344         if (!ip->pin_info)
2345                 return -ENOMEM;
2346
2347         if (cfg->type == IOAPIC_DOMAIN_INVALID)
2348                 return 0;
2349
2350         ip->irqdomain = irq_domain_add_linear(cfg->dev, hwirqs, cfg->ops,
2351                                               (void *)(long)ioapic);
2352         if(!ip->irqdomain) {
2353                 kfree(ip->pin_info);
2354                 ip->pin_info = NULL;
2355                 return -ENOMEM;
2356         }
2357
2358         if (cfg->type == IOAPIC_DOMAIN_LEGACY ||
2359             cfg->type == IOAPIC_DOMAIN_STRICT)
2360                 ioapic_dynirq_base = max(ioapic_dynirq_base,
2361                                          gsi_cfg->gsi_end + 1);
2362
2363         if (gsi_cfg->gsi_base == 0)
2364                 irq_set_default_host(ip->irqdomain);
2365
2366         return 0;
2367 }
2368
2369 static void ioapic_destroy_irqdomain(int idx)
2370 {
2371         if (ioapics[idx].irqdomain) {
2372                 irq_domain_remove(ioapics[idx].irqdomain);
2373                 ioapics[idx].irqdomain = NULL;
2374         }
2375         kfree(ioapics[idx].pin_info);
2376         ioapics[idx].pin_info = NULL;
2377 }
2378
2379 void __init setup_IO_APIC(void)
2380 {
2381         int ioapic;
2382
2383         /*
2384          * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2385          */
2386         io_apic_irqs = nr_legacy_irqs() ? ~PIC_IRQS : ~0UL;
2387
2388         apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2389         for_each_ioapic(ioapic)
2390                 BUG_ON(mp_irqdomain_create(ioapic));
2391
2392         /*
2393          * Set up IO-APIC IRQ routing.
2394          */
2395         x86_init.mpparse.setup_ioapic_ids();
2396
2397         sync_Arb_IDs();
2398         setup_IO_APIC_irqs();
2399         init_IO_APIC_traps();
2400         if (nr_legacy_irqs())
2401                 check_timer();
2402
2403         ioapic_initialized = 1;
2404 }
2405
2406 /*
2407  *      Called after all the initialization is done. If we didn't find any
2408  *      APIC bugs then we can allow the modify fast path
2409  */
2410
2411 static int __init io_apic_bug_finalize(void)
2412 {
2413         if (sis_apic_bug == -1)
2414                 sis_apic_bug = 0;
2415         return 0;
2416 }
2417
2418 late_initcall(io_apic_bug_finalize);
2419
2420 static void resume_ioapic_id(int ioapic_idx)
2421 {
2422         unsigned long flags;
2423         union IO_APIC_reg_00 reg_00;
2424
2425         raw_spin_lock_irqsave(&ioapic_lock, flags);
2426         reg_00.raw = io_apic_read(ioapic_idx, 0);
2427         if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
2428                 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
2429                 io_apic_write(ioapic_idx, 0, reg_00.raw);
2430         }
2431         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2432 }
2433
2434 static void ioapic_resume(void)
2435 {
2436         int ioapic_idx;
2437
2438         for_each_ioapic_reverse(ioapic_idx)
2439                 resume_ioapic_id(ioapic_idx);
2440
2441         restore_ioapic_entries();
2442 }
2443
2444 static struct syscore_ops ioapic_syscore_ops = {
2445         .suspend = save_ioapic_entries,
2446         .resume = ioapic_resume,
2447 };
2448
2449 static int __init ioapic_init_ops(void)
2450 {
2451         register_syscore_ops(&ioapic_syscore_ops);
2452
2453         return 0;
2454 }
2455
2456 device_initcall(ioapic_init_ops);
2457
2458 /*
2459  * MSI message composition
2460  */
2461 void native_compose_msi_msg(struct pci_dev *pdev,
2462                             unsigned int irq, unsigned int dest,
2463                             struct msi_msg *msg, u8 hpet_id)
2464 {
2465         struct irq_cfg *cfg = irq_cfg(irq);
2466
2467         msg->address_hi = MSI_ADDR_BASE_HI;
2468
2469         if (x2apic_enabled())
2470                 msg->address_hi |= MSI_ADDR_EXT_DEST_ID(dest);
2471
2472         msg->address_lo =
2473                 MSI_ADDR_BASE_LO |
2474                 ((apic->irq_dest_mode == 0) ?
2475                         MSI_ADDR_DEST_MODE_PHYSICAL:
2476                         MSI_ADDR_DEST_MODE_LOGICAL) |
2477                 ((apic->irq_delivery_mode != dest_LowestPrio) ?
2478                         MSI_ADDR_REDIRECTION_CPU:
2479                         MSI_ADDR_REDIRECTION_LOWPRI) |
2480                 MSI_ADDR_DEST_ID(dest);
2481
2482         msg->data =
2483                 MSI_DATA_TRIGGER_EDGE |
2484                 MSI_DATA_LEVEL_ASSERT |
2485                 ((apic->irq_delivery_mode != dest_LowestPrio) ?
2486                         MSI_DATA_DELIVERY_FIXED:
2487                         MSI_DATA_DELIVERY_LOWPRI) |
2488                 MSI_DATA_VECTOR(cfg->vector);
2489 }
2490
2491 #ifdef CONFIG_PCI_MSI
2492 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq,
2493                            struct msi_msg *msg, u8 hpet_id)
2494 {
2495         struct irq_cfg *cfg;
2496         int err;
2497         unsigned dest;
2498
2499         if (disable_apic)
2500                 return -ENXIO;
2501
2502         cfg = irq_cfg(irq);
2503         err = assign_irq_vector(irq, cfg, apic->target_cpus());
2504         if (err)
2505                 return err;
2506
2507         err = apic->cpu_mask_to_apicid_and(cfg->domain,
2508                                            apic->target_cpus(), &dest);
2509         if (err)
2510                 return err;
2511
2512         x86_msi.compose_msi_msg(pdev, irq, dest, msg, hpet_id);
2513
2514         return 0;
2515 }
2516
2517 static int
2518 msi_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
2519 {
2520         struct irq_cfg *cfg = data->chip_data;
2521         struct msi_msg msg;
2522         unsigned int dest;
2523         int ret;
2524
2525         ret = apic_set_affinity(data, mask, &dest);
2526         if (ret)
2527                 return ret;
2528
2529         __get_cached_msi_msg(data->msi_desc, &msg);
2530
2531         msg.data &= ~MSI_DATA_VECTOR_MASK;
2532         msg.data |= MSI_DATA_VECTOR(cfg->vector);
2533         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2534         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2535
2536         __pci_write_msi_msg(data->msi_desc, &msg);
2537
2538         return IRQ_SET_MASK_OK_NOCOPY;
2539 }
2540
2541 /*
2542  * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2543  * which implement the MSI or MSI-X Capability Structure.
2544  */
2545 static struct irq_chip msi_chip = {
2546         .name                   = "PCI-MSI",
2547         .irq_unmask             = pci_msi_unmask_irq,
2548         .irq_mask               = pci_msi_mask_irq,
2549         .irq_ack                = apic_ack_edge,
2550         .irq_set_affinity       = msi_set_affinity,
2551         .irq_retrigger          = apic_retrigger_irq,
2552         .flags                  = IRQCHIP_SKIP_SET_WAKE,
2553 };
2554
2555 int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc,
2556                   unsigned int irq_base, unsigned int irq_offset)
2557 {
2558         struct irq_chip *chip = &msi_chip;
2559         struct msi_msg msg;
2560         unsigned int irq = irq_base + irq_offset;
2561         int ret;
2562
2563         ret = msi_compose_msg(dev, irq, &msg, -1);
2564         if (ret < 0)
2565                 return ret;
2566
2567         irq_set_msi_desc_off(irq_base, irq_offset, msidesc);
2568
2569         /*
2570          * MSI-X message is written per-IRQ, the offset is always 0.
2571          * MSI message denotes a contiguous group of IRQs, written for 0th IRQ.
2572          */
2573         if (!irq_offset)
2574                 pci_write_msi_msg(irq, &msg);
2575
2576         setup_remapped_irq(irq, irq_cfg(irq), chip);
2577
2578         irq_set_chip_and_handler_name(irq, chip, handle_edge_irq, "edge");
2579
2580         dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
2581
2582         return 0;
2583 }
2584
2585 int native_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
2586 {
2587         struct msi_desc *msidesc;
2588         unsigned int irq;
2589         int node, ret;
2590
2591         /* Multiple MSI vectors only supported with interrupt remapping */
2592         if (type == PCI_CAP_ID_MSI && nvec > 1)
2593                 return 1;
2594
2595         node = dev_to_node(&dev->dev);
2596
2597         list_for_each_entry(msidesc, &dev->msi_list, list) {
2598                 irq = irq_alloc_hwirq(node);
2599                 if (!irq)
2600                         return -ENOSPC;
2601
2602                 ret = setup_msi_irq(dev, msidesc, irq, 0);
2603                 if (ret < 0) {
2604                         irq_free_hwirq(irq);
2605                         return ret;
2606                 }
2607
2608         }
2609         return 0;
2610 }
2611
2612 void native_teardown_msi_irq(unsigned int irq)
2613 {
2614         irq_free_hwirq(irq);
2615 }
2616
2617 #ifdef CONFIG_DMAR_TABLE
2618 static int
2619 dmar_msi_set_affinity(struct irq_data *data, const struct cpumask *mask,
2620                       bool force)
2621 {
2622         struct irq_cfg *cfg = data->chip_data;
2623         unsigned int dest, irq = data->irq;
2624         struct msi_msg msg;
2625         int ret;
2626
2627         ret = apic_set_affinity(data, mask, &dest);
2628         if (ret)
2629                 return ret;
2630
2631         dmar_msi_read(irq, &msg);
2632
2633         msg.data &= ~MSI_DATA_VECTOR_MASK;
2634         msg.data |= MSI_DATA_VECTOR(cfg->vector);
2635         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2636         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2637         msg.address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(dest);
2638
2639         dmar_msi_write(irq, &msg);
2640
2641         return IRQ_SET_MASK_OK_NOCOPY;
2642 }
2643
2644 static struct irq_chip dmar_msi_type = {
2645         .name                   = "DMAR_MSI",
2646         .irq_unmask             = dmar_msi_unmask,
2647         .irq_mask               = dmar_msi_mask,
2648         .irq_ack                = apic_ack_edge,
2649         .irq_set_affinity       = dmar_msi_set_affinity,
2650         .irq_retrigger          = apic_retrigger_irq,
2651         .flags                  = IRQCHIP_SKIP_SET_WAKE,
2652 };
2653
2654 int arch_setup_dmar_msi(unsigned int irq)
2655 {
2656         int ret;
2657         struct msi_msg msg;
2658
2659         ret = msi_compose_msg(NULL, irq, &msg, -1);
2660         if (ret < 0)
2661                 return ret;
2662         dmar_msi_write(irq, &msg);
2663         irq_set_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
2664                                       "edge");
2665         return 0;
2666 }
2667 #endif
2668
2669 #ifdef CONFIG_HPET_TIMER
2670
2671 static int hpet_msi_set_affinity(struct irq_data *data,
2672                                  const struct cpumask *mask, bool force)
2673 {
2674         struct irq_cfg *cfg = data->chip_data;
2675         struct msi_msg msg;
2676         unsigned int dest;
2677         int ret;
2678
2679         ret = apic_set_affinity(data, mask, &dest);
2680         if (ret)
2681                 return ret;
2682
2683         hpet_msi_read(data->handler_data, &msg);
2684
2685         msg.data &= ~MSI_DATA_VECTOR_MASK;
2686         msg.data |= MSI_DATA_VECTOR(cfg->vector);
2687         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2688         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2689
2690         hpet_msi_write(data->handler_data, &msg);
2691
2692         return IRQ_SET_MASK_OK_NOCOPY;
2693 }
2694
2695 static struct irq_chip hpet_msi_type = {
2696         .name = "HPET_MSI",
2697         .irq_unmask = hpet_msi_unmask,
2698         .irq_mask = hpet_msi_mask,
2699         .irq_ack = apic_ack_edge,
2700         .irq_set_affinity = hpet_msi_set_affinity,
2701         .irq_retrigger = apic_retrigger_irq,
2702         .flags = IRQCHIP_SKIP_SET_WAKE,
2703 };
2704
2705 int default_setup_hpet_msi(unsigned int irq, unsigned int id)
2706 {
2707         struct irq_chip *chip = &hpet_msi_type;
2708         struct msi_msg msg;
2709         int ret;
2710
2711         ret = msi_compose_msg(NULL, irq, &msg, id);
2712         if (ret < 0)
2713                 return ret;
2714
2715         hpet_msi_write(irq_get_handler_data(irq), &msg);
2716         irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
2717         setup_remapped_irq(irq, irq_cfg(irq), chip);
2718
2719         irq_set_chip_and_handler_name(irq, chip, handle_edge_irq, "edge");
2720         return 0;
2721 }
2722 #endif
2723
2724 #endif /* CONFIG_PCI_MSI */
2725 /*
2726  * Hypertransport interrupt support
2727  */
2728 #ifdef CONFIG_HT_IRQ
2729
2730 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2731 {
2732         struct ht_irq_msg msg;
2733         fetch_ht_irq_msg(irq, &msg);
2734
2735         msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2736         msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2737
2738         msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2739         msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2740
2741         write_ht_irq_msg(irq, &msg);
2742 }
2743
2744 static int
2745 ht_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
2746 {
2747         struct irq_cfg *cfg = data->chip_data;
2748         unsigned int dest;
2749         int ret;
2750
2751         ret = apic_set_affinity(data, mask, &dest);
2752         if (ret)
2753                 return ret;
2754
2755         target_ht_irq(data->irq, dest, cfg->vector);
2756         return IRQ_SET_MASK_OK_NOCOPY;
2757 }
2758
2759 static struct irq_chip ht_irq_chip = {
2760         .name                   = "PCI-HT",
2761         .irq_mask               = mask_ht_irq,
2762         .irq_unmask             = unmask_ht_irq,
2763         .irq_ack                = apic_ack_edge,
2764         .irq_set_affinity       = ht_set_affinity,
2765         .irq_retrigger          = apic_retrigger_irq,
2766         .flags                  = IRQCHIP_SKIP_SET_WAKE,
2767 };
2768
2769 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2770 {
2771         struct irq_cfg *cfg;
2772         struct ht_irq_msg msg;
2773         unsigned dest;
2774         int err;
2775
2776         if (disable_apic)
2777                 return -ENXIO;
2778
2779         cfg = irq_cfg(irq);
2780         err = assign_irq_vector(irq, cfg, apic->target_cpus());
2781         if (err)
2782                 return err;
2783
2784         err = apic->cpu_mask_to_apicid_and(cfg->domain,
2785                                            apic->target_cpus(), &dest);
2786         if (err)
2787                 return err;
2788
2789         msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2790
2791         msg.address_lo =
2792                 HT_IRQ_LOW_BASE |
2793                 HT_IRQ_LOW_DEST_ID(dest) |
2794                 HT_IRQ_LOW_VECTOR(cfg->vector) |
2795                 ((apic->irq_dest_mode == 0) ?
2796                         HT_IRQ_LOW_DM_PHYSICAL :
2797                         HT_IRQ_LOW_DM_LOGICAL) |
2798                 HT_IRQ_LOW_RQEOI_EDGE |
2799                 ((apic->irq_delivery_mode != dest_LowestPrio) ?
2800                         HT_IRQ_LOW_MT_FIXED :
2801                         HT_IRQ_LOW_MT_ARBITRATED) |
2802                 HT_IRQ_LOW_IRQ_MASKED;
2803
2804         write_ht_irq_msg(irq, &msg);
2805
2806         irq_set_chip_and_handler_name(irq, &ht_irq_chip,
2807                                       handle_edge_irq, "edge");
2808
2809         dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
2810
2811         return 0;
2812 }
2813 #endif /* CONFIG_HT_IRQ */
2814
2815 static int
2816 io_apic_setup_irq_pin(unsigned int irq, int node, struct io_apic_irq_attr *attr)
2817 {
2818         struct irq_cfg *cfg = alloc_irq_and_cfg_at(irq, node);
2819         int ret;
2820
2821         if (!cfg)
2822                 return -EINVAL;
2823         ret = __add_pin_to_irq_node(cfg, node, attr->ioapic, attr->ioapic_pin);
2824         if (!ret)
2825                 setup_ioapic_irq(irq, cfg, attr);
2826         return ret;
2827 }
2828
2829 static int io_apic_get_redir_entries(int ioapic)
2830 {
2831         union IO_APIC_reg_01    reg_01;
2832         unsigned long flags;
2833
2834         raw_spin_lock_irqsave(&ioapic_lock, flags);
2835         reg_01.raw = io_apic_read(ioapic, 1);
2836         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2837
2838         /* The register returns the maximum index redir index
2839          * supported, which is one less than the total number of redir
2840          * entries.
2841          */
2842         return reg_01.bits.entries + 1;
2843 }
2844
2845 unsigned int arch_dynirq_lower_bound(unsigned int from)
2846 {
2847         /*
2848          * dmar_alloc_hwirq() may be called before setup_IO_APIC(), so use
2849          * gsi_top if ioapic_dynirq_base hasn't been initialized yet.
2850          */
2851         return ioapic_initialized ? ioapic_dynirq_base : gsi_top;
2852 }
2853
2854 int __init arch_probe_nr_irqs(void)
2855 {
2856         int nr;
2857
2858         if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
2859                 nr_irqs = NR_VECTORS * nr_cpu_ids;
2860
2861         nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
2862 #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
2863         /*
2864          * for MSI and HT dyn irq
2865          */
2866         nr += gsi_top * 16;
2867 #endif
2868         if (nr < nr_irqs)
2869                 nr_irqs = nr;
2870
2871         return 0;
2872 }
2873
2874 #ifdef CONFIG_X86_32
2875 static int io_apic_get_unique_id(int ioapic, int apic_id)
2876 {
2877         union IO_APIC_reg_00 reg_00;
2878         static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2879         physid_mask_t tmp;
2880         unsigned long flags;
2881         int i = 0;
2882
2883         /*
2884          * The P4 platform supports up to 256 APIC IDs on two separate APIC
2885          * buses (one for LAPICs, one for IOAPICs), where predecessors only
2886          * supports up to 16 on one shared APIC bus.
2887          *
2888          * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2889          *      advantage of new APIC bus architecture.
2890          */
2891
2892         if (physids_empty(apic_id_map))
2893                 apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
2894
2895         raw_spin_lock_irqsave(&ioapic_lock, flags);
2896         reg_00.raw = io_apic_read(ioapic, 0);
2897         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2898
2899         if (apic_id >= get_physical_broadcast()) {
2900                 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2901                         "%d\n", ioapic, apic_id, reg_00.bits.ID);
2902                 apic_id = reg_00.bits.ID;
2903         }
2904
2905         /*
2906          * Every APIC in a system must have a unique ID or we get lots of nice
2907          * 'stuck on smp_invalidate_needed IPI wait' messages.
2908          */
2909         if (apic->check_apicid_used(&apic_id_map, apic_id)) {
2910
2911                 for (i = 0; i < get_physical_broadcast(); i++) {
2912                         if (!apic->check_apicid_used(&apic_id_map, i))
2913                                 break;
2914                 }
2915
2916                 if (i == get_physical_broadcast())
2917                         panic("Max apic_id exceeded!\n");
2918
2919                 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2920                         "trying %d\n", ioapic, apic_id, i);
2921
2922                 apic_id = i;
2923         }
2924
2925         apic->apicid_to_cpu_present(apic_id, &tmp);
2926         physids_or(apic_id_map, apic_id_map, tmp);
2927
2928         if (reg_00.bits.ID != apic_id) {
2929                 reg_00.bits.ID = apic_id;
2930
2931                 raw_spin_lock_irqsave(&ioapic_lock, flags);
2932                 io_apic_write(ioapic, 0, reg_00.raw);
2933                 reg_00.raw = io_apic_read(ioapic, 0);
2934                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2935
2936                 /* Sanity check */
2937                 if (reg_00.bits.ID != apic_id) {
2938                         pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
2939                                ioapic);
2940                         return -1;
2941                 }
2942         }
2943
2944         apic_printk(APIC_VERBOSE, KERN_INFO
2945                         "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2946
2947         return apic_id;
2948 }
2949
2950 static u8 io_apic_unique_id(int idx, u8 id)
2951 {
2952         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
2953             !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
2954                 return io_apic_get_unique_id(idx, id);
2955         else
2956                 return id;
2957 }
2958 #else
2959 static u8 io_apic_unique_id(int idx, u8 id)
2960 {
2961         union IO_APIC_reg_00 reg_00;
2962         DECLARE_BITMAP(used, 256);
2963         unsigned long flags;
2964         u8 new_id;
2965         int i;
2966
2967         bitmap_zero(used, 256);
2968         for_each_ioapic(i)
2969                 __set_bit(mpc_ioapic_id(i), used);
2970
2971         /* Hand out the requested id if available */
2972         if (!test_bit(id, used))
2973                 return id;
2974
2975         /*
2976          * Read the current id from the ioapic and keep it if
2977          * available.
2978          */
2979         raw_spin_lock_irqsave(&ioapic_lock, flags);
2980         reg_00.raw = io_apic_read(idx, 0);
2981         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2982         new_id = reg_00.bits.ID;
2983         if (!test_bit(new_id, used)) {
2984                 apic_printk(APIC_VERBOSE, KERN_INFO
2985                         "IOAPIC[%d]: Using reg apic_id %d instead of %d\n",
2986                          idx, new_id, id);
2987                 return new_id;
2988         }
2989
2990         /*
2991          * Get the next free id and write it to the ioapic.
2992          */
2993         new_id = find_first_zero_bit(used, 256);
2994         reg_00.bits.ID = new_id;
2995         raw_spin_lock_irqsave(&ioapic_lock, flags);
2996         io_apic_write(idx, 0, reg_00.raw);
2997         reg_00.raw = io_apic_read(idx, 0);
2998         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2999         /* Sanity check */
3000         BUG_ON(reg_00.bits.ID != new_id);
3001
3002         return new_id;
3003 }
3004 #endif
3005
3006 static int io_apic_get_version(int ioapic)
3007 {
3008         union IO_APIC_reg_01    reg_01;
3009         unsigned long flags;
3010
3011         raw_spin_lock_irqsave(&ioapic_lock, flags);
3012         reg_01.raw = io_apic_read(ioapic, 1);
3013         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3014
3015         return reg_01.bits.version;
3016 }
3017
3018 int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
3019 {
3020         int ioapic, pin, idx;
3021
3022         if (skip_ioapic_setup)
3023                 return -1;
3024
3025         ioapic = mp_find_ioapic(gsi);
3026         if (ioapic < 0)
3027                 return -1;
3028
3029         pin = mp_find_ioapic_pin(ioapic, gsi);
3030         if (pin < 0)
3031                 return -1;
3032
3033         idx = find_irq_entry(ioapic, pin, mp_INT);
3034         if (idx < 0)
3035                 return -1;
3036
3037         *trigger = irq_trigger(idx);
3038         *polarity = irq_polarity(idx);
3039         return 0;
3040 }
3041
3042 /*
3043  * This function currently is only a helper for the i386 smp boot process where
3044  * we need to reprogram the ioredtbls to cater for the cpus which have come online
3045  * so mask in all cases should simply be apic->target_cpus()
3046  */
3047 #ifdef CONFIG_SMP
3048 void __init setup_ioapic_dest(void)
3049 {
3050         int pin, ioapic, irq, irq_entry;
3051         const struct cpumask *mask;
3052         struct irq_data *idata;
3053
3054         if (skip_ioapic_setup == 1)
3055                 return;
3056
3057         for_each_ioapic_pin(ioapic, pin) {
3058                 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3059                 if (irq_entry == -1)
3060                         continue;
3061
3062                 irq = pin_2_irq(irq_entry, ioapic, pin, 0);
3063                 if (irq < 0 || !mp_init_irq_at_boot(ioapic, irq))
3064                         continue;
3065
3066                 idata = irq_get_irq_data(irq);
3067
3068                 /*
3069                  * Honour affinities which have been set in early boot
3070                  */
3071                 if (!irqd_can_balance(idata) || irqd_affinity_was_set(idata))
3072                         mask = idata->affinity;
3073                 else
3074                         mask = apic->target_cpus();
3075
3076                 x86_io_apic_ops.set_affinity(idata, mask, false);
3077         }
3078
3079 }
3080 #endif
3081
3082 #define IOAPIC_RESOURCE_NAME_SIZE 11
3083
3084 static struct resource *ioapic_resources;
3085
3086 static struct resource * __init ioapic_setup_resources(void)
3087 {
3088         unsigned long n;
3089         struct resource *res;
3090         char *mem;
3091         int i, num = 0;
3092
3093         for_each_ioapic(i)
3094                 num++;
3095         if (num == 0)
3096                 return NULL;
3097
3098         n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3099         n *= num;
3100
3101         mem = alloc_bootmem(n);
3102         res = (void *)mem;
3103
3104         mem += sizeof(struct resource) * num;
3105
3106         num = 0;
3107         for_each_ioapic(i) {
3108                 res[num].name = mem;
3109                 res[num].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3110                 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
3111                 mem += IOAPIC_RESOURCE_NAME_SIZE;
3112                 num++;
3113                 ioapics[i].iomem_res = res;
3114         }
3115
3116         ioapic_resources = res;
3117
3118         return res;
3119 }
3120
3121 void __init native_io_apic_init_mappings(void)
3122 {
3123         unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3124         struct resource *ioapic_res;
3125         int i;
3126
3127         ioapic_res = ioapic_setup_resources();
3128         for_each_ioapic(i) {
3129                 if (smp_found_config) {
3130                         ioapic_phys = mpc_ioapic_addr(i);
3131 #ifdef CONFIG_X86_32
3132                         if (!ioapic_phys) {
3133                                 printk(KERN_ERR
3134                                        "WARNING: bogus zero IO-APIC "
3135                                        "address found in MPTABLE, "
3136                                        "disabling IO/APIC support!\n");
3137                                 smp_found_config = 0;
3138                                 skip_ioapic_setup = 1;
3139                                 goto fake_ioapic_page;
3140                         }
3141 #endif
3142                 } else {
3143 #ifdef CONFIG_X86_32
3144 fake_ioapic_page:
3145 #endif
3146                         ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
3147                         ioapic_phys = __pa(ioapic_phys);
3148                 }
3149                 set_fixmap_nocache(idx, ioapic_phys);
3150                 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
3151                         __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
3152                         ioapic_phys);
3153                 idx++;
3154
3155                 ioapic_res->start = ioapic_phys;
3156                 ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
3157                 ioapic_res++;
3158         }
3159 }
3160
3161 void __init ioapic_insert_resources(void)
3162 {
3163         int i;
3164         struct resource *r = ioapic_resources;
3165
3166         if (!r) {
3167                 if (nr_ioapics > 0)
3168                         printk(KERN_ERR
3169                                 "IO APIC resources couldn't be allocated.\n");
3170                 return;
3171         }
3172
3173         for_each_ioapic(i) {
3174                 insert_resource(&iomem_resource, r);
3175                 r++;
3176         }
3177 }
3178
3179 int mp_find_ioapic(u32 gsi)
3180 {
3181         int i;
3182
3183         if (nr_ioapics == 0)
3184                 return -1;
3185
3186         /* Find the IOAPIC that manages this GSI. */
3187         for_each_ioapic(i) {
3188                 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
3189                 if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end)
3190                         return i;
3191         }
3192
3193         printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
3194         return -1;
3195 }
3196
3197 int mp_find_ioapic_pin(int ioapic, u32 gsi)
3198 {
3199         struct mp_ioapic_gsi *gsi_cfg;
3200
3201         if (WARN_ON(ioapic < 0))
3202                 return -1;
3203
3204         gsi_cfg = mp_ioapic_gsi_routing(ioapic);
3205         if (WARN_ON(gsi > gsi_cfg->gsi_end))
3206                 return -1;
3207
3208         return gsi - gsi_cfg->gsi_base;
3209 }
3210
3211 static int bad_ioapic_register(int idx)
3212 {
3213         union IO_APIC_reg_00 reg_00;
3214         union IO_APIC_reg_01 reg_01;
3215         union IO_APIC_reg_02 reg_02;
3216
3217         reg_00.raw = io_apic_read(idx, 0);
3218         reg_01.raw = io_apic_read(idx, 1);
3219         reg_02.raw = io_apic_read(idx, 2);
3220
3221         if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
3222                 pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
3223                         mpc_ioapic_addr(idx));
3224                 return 1;
3225         }
3226
3227         return 0;
3228 }
3229
3230 static int find_free_ioapic_entry(void)
3231 {
3232         int idx;
3233
3234         for (idx = 0; idx < MAX_IO_APICS; idx++)
3235                 if (ioapics[idx].nr_registers == 0)
3236                         return idx;
3237
3238         return MAX_IO_APICS;
3239 }
3240
3241 /**
3242  * mp_register_ioapic - Register an IOAPIC device
3243  * @id:         hardware IOAPIC ID
3244  * @address:    physical address of IOAPIC register area
3245  * @gsi_base:   base of GSI associated with the IOAPIC
3246  * @cfg:        configuration information for the IOAPIC
3247  */
3248 int mp_register_ioapic(int id, u32 address, u32 gsi_base,
3249                        struct ioapic_domain_cfg *cfg)
3250 {
3251         bool hotplug = !!ioapic_initialized;
3252         struct mp_ioapic_gsi *gsi_cfg;
3253         int idx, ioapic, entries;
3254         u32 gsi_end;
3255
3256         if (!address) {
3257                 pr_warn("Bogus (zero) I/O APIC address found, skipping!\n");
3258                 return -EINVAL;
3259         }
3260         for_each_ioapic(ioapic)
3261                 if (ioapics[ioapic].mp_config.apicaddr == address) {
3262                         pr_warn("address 0x%x conflicts with IOAPIC%d\n",
3263                                 address, ioapic);
3264                         return -EEXIST;
3265                 }
3266
3267         idx = find_free_ioapic_entry();
3268         if (idx >= MAX_IO_APICS) {
3269                 pr_warn("Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
3270                         MAX_IO_APICS, idx);
3271                 return -ENOSPC;
3272         }
3273
3274         ioapics[idx].mp_config.type = MP_IOAPIC;
3275         ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
3276         ioapics[idx].mp_config.apicaddr = address;
3277
3278         set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
3279         if (bad_ioapic_register(idx)) {
3280                 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
3281                 return -ENODEV;
3282         }
3283
3284         ioapics[idx].mp_config.apicid = io_apic_unique_id(idx, id);
3285         ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
3286
3287         /*
3288          * Build basic GSI lookup table to facilitate gsi->io_apic lookups
3289          * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
3290          */
3291         entries = io_apic_get_redir_entries(idx);
3292         gsi_end = gsi_base + entries - 1;
3293         for_each_ioapic(ioapic) {
3294                 gsi_cfg = mp_ioapic_gsi_routing(ioapic);
3295                 if ((gsi_base >= gsi_cfg->gsi_base &&
3296                      gsi_base <= gsi_cfg->gsi_end) ||
3297                     (gsi_end >= gsi_cfg->gsi_base &&
3298                      gsi_end <= gsi_cfg->gsi_end)) {
3299                         pr_warn("GSI range [%u-%u] for new IOAPIC conflicts with GSI[%u-%u]\n",
3300                                 gsi_base, gsi_end,
3301                                 gsi_cfg->gsi_base, gsi_cfg->gsi_end);
3302                         clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
3303                         return -ENOSPC;
3304                 }
3305         }
3306         gsi_cfg = mp_ioapic_gsi_routing(idx);
3307         gsi_cfg->gsi_base = gsi_base;
3308         gsi_cfg->gsi_end = gsi_end;
3309
3310         ioapics[idx].irqdomain = NULL;
3311         ioapics[idx].irqdomain_cfg = *cfg;
3312
3313         /*
3314          * If mp_register_ioapic() is called during early boot stage when
3315          * walking ACPI/SFI/DT tables, it's too early to create irqdomain,
3316          * we are still using bootmem allocator. So delay it to setup_IO_APIC().
3317          */
3318         if (hotplug) {
3319                 if (mp_irqdomain_create(idx)) {
3320                         clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
3321                         return -ENOMEM;
3322                 }
3323                 alloc_ioapic_saved_registers(idx);
3324         }
3325
3326         if (gsi_cfg->gsi_end >= gsi_top)
3327                 gsi_top = gsi_cfg->gsi_end + 1;
3328         if (nr_ioapics <= idx)
3329                 nr_ioapics = idx + 1;
3330
3331         /* Set nr_registers to mark entry present */
3332         ioapics[idx].nr_registers = entries;
3333
3334         pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
3335                 idx, mpc_ioapic_id(idx),
3336                 mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
3337                 gsi_cfg->gsi_base, gsi_cfg->gsi_end);
3338
3339         return 0;
3340 }
3341
3342 int mp_unregister_ioapic(u32 gsi_base)
3343 {
3344         int ioapic, pin;
3345         int found = 0;
3346         struct mp_pin_info *pin_info;
3347
3348         for_each_ioapic(ioapic)
3349                 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) {
3350                         found = 1;
3351                         break;
3352                 }
3353         if (!found) {
3354                 pr_warn("can't find IOAPIC for GSI %d\n", gsi_base);
3355                 return -ENODEV;
3356         }
3357
3358         for_each_pin(ioapic, pin) {
3359                 pin_info = mp_pin_info(ioapic, pin);
3360                 if (pin_info->count) {
3361                         pr_warn("pin%d on IOAPIC%d is still in use.\n",
3362                                 pin, ioapic);
3363                         return -EBUSY;
3364                 }
3365         }
3366
3367         /* Mark entry not present */
3368         ioapics[ioapic].nr_registers  = 0;
3369         ioapic_destroy_irqdomain(ioapic);
3370         free_ioapic_saved_registers(ioapic);
3371         if (ioapics[ioapic].iomem_res)
3372                 release_resource(ioapics[ioapic].iomem_res);
3373         clear_fixmap(FIX_IO_APIC_BASE_0 + ioapic);
3374         memset(&ioapics[ioapic], 0, sizeof(ioapics[ioapic]));
3375
3376         return 0;
3377 }
3378
3379 int mp_ioapic_registered(u32 gsi_base)
3380 {
3381         int ioapic;
3382
3383         for_each_ioapic(ioapic)
3384                 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base)
3385                         return 1;
3386
3387         return 0;
3388 }
3389
3390 int mp_irqdomain_map(struct irq_domain *domain, unsigned int virq,
3391                      irq_hw_number_t hwirq)
3392 {
3393         int ioapic = (int)(long)domain->host_data;
3394         struct mp_pin_info *info = mp_pin_info(ioapic, hwirq);
3395         struct io_apic_irq_attr attr;
3396
3397         /* Get default attribute if not set by caller yet */
3398         if (!info->set) {
3399                 u32 gsi = mp_pin_to_gsi(ioapic, hwirq);
3400
3401                 if (acpi_get_override_irq(gsi, &info->trigger,
3402                                           &info->polarity) < 0) {
3403                         /*
3404                          * PCI interrupts are always polarity one level
3405                          * triggered.
3406                          */
3407                         info->trigger = 1;
3408                         info->polarity = 1;
3409                 }
3410                 info->node = NUMA_NO_NODE;
3411
3412                 /*
3413                  * setup_IO_APIC_irqs() programs all legacy IRQs with default
3414                  * trigger and polarity attributes. Don't set the flag for that
3415                  * case so the first legacy IRQ user could reprogram the pin
3416                  * with real trigger and polarity attributes.
3417                  */
3418                 if (virq >= nr_legacy_irqs() || info->count)
3419                         info->set = 1;
3420         }
3421         set_io_apic_irq_attr(&attr, ioapic, hwirq, info->trigger,
3422                              info->polarity);
3423
3424         return io_apic_setup_irq_pin(virq, info->node, &attr);
3425 }
3426
3427 void mp_irqdomain_unmap(struct irq_domain *domain, unsigned int virq)
3428 {
3429         struct irq_data *data = irq_get_irq_data(virq);
3430         struct irq_cfg *cfg = irq_cfg(virq);
3431         int ioapic = (int)(long)domain->host_data;
3432         int pin = (int)data->hwirq;
3433
3434         ioapic_mask_entry(ioapic, pin);
3435         __remove_pin_from_irq(cfg, ioapic, pin);
3436         WARN_ON(!list_empty(&cfg->irq_2_pin));
3437         arch_teardown_hwirq(virq);
3438 }
3439
3440 int mp_set_gsi_attr(u32 gsi, int trigger, int polarity, int node)
3441 {
3442         int ret = 0;
3443         int ioapic, pin;
3444         struct mp_pin_info *info;
3445
3446         ioapic = mp_find_ioapic(gsi);
3447         if (ioapic < 0)
3448                 return -ENODEV;
3449
3450         pin = mp_find_ioapic_pin(ioapic, gsi);
3451         info = mp_pin_info(ioapic, pin);
3452         trigger = trigger ? 1 : 0;
3453         polarity = polarity ? 1 : 0;
3454
3455         mutex_lock(&ioapic_mutex);
3456         if (!info->set) {
3457                 info->trigger = trigger;
3458                 info->polarity = polarity;
3459                 info->node = node;
3460                 info->set = 1;
3461         } else if (info->trigger != trigger || info->polarity != polarity) {
3462                 ret = -EBUSY;
3463         }
3464         mutex_unlock(&ioapic_mutex);
3465
3466         return ret;
3467 }
3468
3469 /* Enable IOAPIC early just for system timer */
3470 void __init pre_init_apic_IRQ0(void)
3471 {
3472         struct io_apic_irq_attr attr = { 0, 0, 0, 0 };
3473
3474         printk(KERN_INFO "Early APIC setup for system timer0\n");
3475 #ifndef CONFIG_SMP
3476         physid_set_mask_of_physid(boot_cpu_physical_apicid,
3477                                          &phys_cpu_present_map);
3478 #endif
3479         setup_local_APIC();
3480
3481         io_apic_setup_irq_pin(0, 0, &attr);
3482         irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
3483                                       "edge");
3484 }