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