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