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