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