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