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