]> bbs.cooldavid.org Git - net-next-2.6.git/blob - arch/x86/kernel/apic/io_apic.c
x86: Convert remapped ioapic affinity setting to new irq chip function
[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 data->affinity to a valid value, and returns
2283  * ->cpu_mask_to_apicid of that in dest_id, or returns -1 and
2284  * leaves data->affinity untouched.
2285  */
2286 int __ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2287                           unsigned int *dest_id)
2288 {
2289         struct irq_cfg *cfg = data->chip_data;
2290
2291         if (!cpumask_intersects(mask, cpu_online_mask))
2292                 return -1;
2293
2294         if (assign_irq_vector(data->irq, data->chip_data, mask))
2295                 return -1;
2296
2297         cpumask_copy(data->affinity, mask);
2298
2299         *dest_id = apic->cpu_mask_to_apicid_and(mask, cfg->domain);
2300         return 0;
2301 }
2302
2303 static int
2304 ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2305                     bool force)
2306 {
2307         unsigned int dest, irq = data->irq;
2308         unsigned long flags;
2309         int ret;
2310
2311         raw_spin_lock_irqsave(&ioapic_lock, flags);
2312         ret = __ioapic_set_affinity(data, mask, &dest);
2313         if (!ret) {
2314                 /* Only the high 8 bits are valid. */
2315                 dest = SET_APIC_LOGICAL_ID(dest);
2316                 __target_IO_APIC_irq(irq, dest, data->chip_data);
2317         }
2318         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2319         return ret;
2320 }
2321
2322 #ifdef CONFIG_INTR_REMAP
2323
2324 /*
2325  * Migrate the IO-APIC irq in the presence of intr-remapping.
2326  *
2327  * For both level and edge triggered, irq migration is a simple atomic
2328  * update(of vector and cpu destination) of IRTE and flush the hardware cache.
2329  *
2330  * For level triggered, we eliminate the io-apic RTE modification (with the
2331  * updated vector information), by using a virtual vector (io-apic pin number).
2332  * Real vector that is used for interrupting cpu will be coming from
2333  * the interrupt-remapping table entry.
2334  */
2335 static int
2336 ir_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2337                        bool force)
2338 {
2339         struct irq_cfg *cfg = data->chip_data;
2340         unsigned int dest, irq = data->irq;
2341         struct irte irte;
2342
2343         if (!cpumask_intersects(mask, cpu_online_mask))
2344                 return -EINVAL;
2345
2346         if (get_irte(irq, &irte))
2347                 return -EBUSY;
2348
2349         if (assign_irq_vector(irq, cfg, mask))
2350                 return -EBUSY;
2351
2352         dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
2353
2354         irte.vector = cfg->vector;
2355         irte.dest_id = IRTE_DEST(dest);
2356
2357         /*
2358          * Modified the IRTE and flushes the Interrupt entry cache.
2359          */
2360         modify_irte(irq, &irte);
2361
2362         if (cfg->move_in_progress)
2363                 send_cleanup_vector(cfg);
2364
2365         cpumask_copy(data->affinity, mask);
2366         return 0;
2367 }
2368
2369 #else
2370 static inline int
2371 ir_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2372                        bool force)
2373 {
2374         return 0;
2375 }
2376 #endif
2377
2378 asmlinkage void smp_irq_move_cleanup_interrupt(void)
2379 {
2380         unsigned vector, me;
2381
2382         ack_APIC_irq();
2383         exit_idle();
2384         irq_enter();
2385
2386         me = smp_processor_id();
2387         for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2388                 unsigned int irq;
2389                 unsigned int irr;
2390                 struct irq_desc *desc;
2391                 struct irq_cfg *cfg;
2392                 irq = __get_cpu_var(vector_irq)[vector];
2393
2394                 if (irq == -1)
2395                         continue;
2396
2397                 desc = irq_to_desc(irq);
2398                 if (!desc)
2399                         continue;
2400
2401                 cfg = irq_cfg(irq);
2402                 raw_spin_lock(&desc->lock);
2403
2404                 /*
2405                  * Check if the irq migration is in progress. If so, we
2406                  * haven't received the cleanup request yet for this irq.
2407                  */
2408                 if (cfg->move_in_progress)
2409                         goto unlock;
2410
2411                 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2412                         goto unlock;
2413
2414                 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2415                 /*
2416                  * Check if the vector that needs to be cleanedup is
2417                  * registered at the cpu's IRR. If so, then this is not
2418                  * the best time to clean it up. Lets clean it up in the
2419                  * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2420                  * to myself.
2421                  */
2422                 if (irr  & (1 << (vector % 32))) {
2423                         apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2424                         goto unlock;
2425                 }
2426                 __get_cpu_var(vector_irq)[vector] = -1;
2427 unlock:
2428                 raw_spin_unlock(&desc->lock);
2429         }
2430
2431         irq_exit();
2432 }
2433
2434 static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
2435 {
2436         unsigned me;
2437
2438         if (likely(!cfg->move_in_progress))
2439                 return;
2440
2441         me = smp_processor_id();
2442
2443         if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2444                 send_cleanup_vector(cfg);
2445 }
2446
2447 static void irq_complete_move(struct irq_cfg *cfg)
2448 {
2449         __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
2450 }
2451
2452 void irq_force_complete_move(int irq)
2453 {
2454         struct irq_cfg *cfg = get_irq_chip_data(irq);
2455
2456         if (!cfg)
2457                 return;
2458
2459         __irq_complete_move(cfg, cfg->vector);
2460 }
2461 #else
2462 static inline void irq_complete_move(struct irq_cfg *cfg) { }
2463 #endif
2464
2465 static void ack_apic_edge(struct irq_data *data)
2466 {
2467         irq_complete_move(data->chip_data);
2468         move_native_irq(data->irq);
2469         ack_APIC_irq();
2470 }
2471
2472 atomic_t irq_mis_count;
2473
2474 /*
2475  * IO-APIC versions below 0x20 don't support EOI register.
2476  * For the record, here is the information about various versions:
2477  *     0Xh     82489DX
2478  *     1Xh     I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
2479  *     2Xh     I/O(x)APIC which is PCI 2.2 Compliant
2480  *     30h-FFh Reserved
2481  *
2482  * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
2483  * version as 0x2. This is an error with documentation and these ICH chips
2484  * use io-apic's of version 0x20.
2485  *
2486  * For IO-APIC's with EOI register, we use that to do an explicit EOI.
2487  * Otherwise, we simulate the EOI message manually by changing the trigger
2488  * mode to edge and then back to level, with RTE being masked during this.
2489 */
2490 static void eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2491 {
2492         struct irq_pin_list *entry;
2493         unsigned long flags;
2494
2495         raw_spin_lock_irqsave(&ioapic_lock, flags);
2496         for_each_irq_pin(entry, cfg->irq_2_pin) {
2497                 if (mp_ioapics[entry->apic].apicver >= 0x20) {
2498                         /*
2499                          * Intr-remapping uses pin number as the virtual vector
2500                          * in the RTE. Actual vector is programmed in
2501                          * intr-remapping table entry. Hence for the io-apic
2502                          * EOI we use the pin number.
2503                          */
2504                         if (irq_remapped(irq))
2505                                 io_apic_eoi(entry->apic, entry->pin);
2506                         else
2507                                 io_apic_eoi(entry->apic, cfg->vector);
2508                 } else {
2509                         __mask_and_edge_IO_APIC_irq(entry);
2510                         __unmask_and_level_IO_APIC_irq(entry);
2511                 }
2512         }
2513         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2514 }
2515
2516 static void ack_apic_level(struct irq_data *data)
2517 {
2518         struct irq_cfg *cfg = data->chip_data;
2519         int i, do_unmask_irq = 0, irq = data->irq;
2520         struct irq_desc *desc = irq_to_desc(irq);
2521         unsigned long v;
2522
2523         irq_complete_move(cfg);
2524 #ifdef CONFIG_GENERIC_PENDING_IRQ
2525         /* If we are moving the irq we need to mask it */
2526         if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
2527                 do_unmask_irq = 1;
2528                 mask_ioapic(cfg);
2529         }
2530 #endif
2531
2532         /*
2533          * It appears there is an erratum which affects at least version 0x11
2534          * of I/O APIC (that's the 82093AA and cores integrated into various
2535          * chipsets).  Under certain conditions a level-triggered interrupt is
2536          * erroneously delivered as edge-triggered one but the respective IRR
2537          * bit gets set nevertheless.  As a result the I/O unit expects an EOI
2538          * message but it will never arrive and further interrupts are blocked
2539          * from the source.  The exact reason is so far unknown, but the
2540          * phenomenon was observed when two consecutive interrupt requests
2541          * from a given source get delivered to the same CPU and the source is
2542          * temporarily disabled in between.
2543          *
2544          * A workaround is to simulate an EOI message manually.  We achieve it
2545          * by setting the trigger mode to edge and then to level when the edge
2546          * trigger mode gets detected in the TMR of a local APIC for a
2547          * level-triggered interrupt.  We mask the source for the time of the
2548          * operation to prevent an edge-triggered interrupt escaping meanwhile.
2549          * The idea is from Manfred Spraul.  --macro
2550          *
2551          * Also in the case when cpu goes offline, fixup_irqs() will forward
2552          * any unhandled interrupt on the offlined cpu to the new cpu
2553          * destination that is handling the corresponding interrupt. This
2554          * interrupt forwarding is done via IPI's. Hence, in this case also
2555          * level-triggered io-apic interrupt will be seen as an edge
2556          * interrupt in the IRR. And we can't rely on the cpu's EOI
2557          * to be broadcasted to the IO-APIC's which will clear the remoteIRR
2558          * corresponding to the level-triggered interrupt. Hence on IO-APIC's
2559          * supporting EOI register, we do an explicit EOI to clear the
2560          * remote IRR and on IO-APIC's which don't have an EOI register,
2561          * we use the above logic (mask+edge followed by unmask+level) from
2562          * Manfred Spraul to clear the remote IRR.
2563          */
2564         i = cfg->vector;
2565         v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2566
2567         /*
2568          * We must acknowledge the irq before we move it or the acknowledge will
2569          * not propagate properly.
2570          */
2571         ack_APIC_irq();
2572
2573         /*
2574          * Tail end of clearing remote IRR bit (either by delivering the EOI
2575          * message via io-apic EOI register write or simulating it using
2576          * mask+edge followed by unnask+level logic) manually when the
2577          * level triggered interrupt is seen as the edge triggered interrupt
2578          * at the cpu.
2579          */
2580         if (!(v & (1 << (i & 0x1f)))) {
2581                 atomic_inc(&irq_mis_count);
2582
2583                 eoi_ioapic_irq(irq, cfg);
2584         }
2585
2586         /* Now we can move and renable the irq */
2587         if (unlikely(do_unmask_irq)) {
2588                 /* Only migrate the irq if the ack has been received.
2589                  *
2590                  * On rare occasions the broadcast level triggered ack gets
2591                  * delayed going to ioapics, and if we reprogram the
2592                  * vector while Remote IRR is still set the irq will never
2593                  * fire again.
2594                  *
2595                  * To prevent this scenario we read the Remote IRR bit
2596                  * of the ioapic.  This has two effects.
2597                  * - On any sane system the read of the ioapic will
2598                  *   flush writes (and acks) going to the ioapic from
2599                  *   this cpu.
2600                  * - We get to see if the ACK has actually been delivered.
2601                  *
2602                  * Based on failed experiments of reprogramming the
2603                  * ioapic entry from outside of irq context starting
2604                  * with masking the ioapic entry and then polling until
2605                  * Remote IRR was clear before reprogramming the
2606                  * ioapic I don't trust the Remote IRR bit to be
2607                  * completey accurate.
2608                  *
2609                  * However there appears to be no other way to plug
2610                  * this race, so if the Remote IRR bit is not
2611                  * accurate and is causing problems then it is a hardware bug
2612                  * and you can go talk to the chipset vendor about it.
2613                  */
2614                 if (!io_apic_level_ack_pending(cfg))
2615                         move_masked_irq(irq);
2616                 unmask_ioapic(cfg);
2617         }
2618 }
2619
2620 #ifdef CONFIG_INTR_REMAP
2621 static void ir_ack_apic_edge(struct irq_data *data)
2622 {
2623         ack_APIC_irq();
2624 }
2625
2626 static void ir_ack_apic_level(struct irq_data *data)
2627 {
2628         ack_APIC_irq();
2629         eoi_ioapic_irq(data->irq, data->chip_data);
2630 }
2631 #endif /* CONFIG_INTR_REMAP */
2632
2633 static struct irq_chip ioapic_chip __read_mostly = {
2634         .name                   = "IO-APIC",
2635         .irq_startup            = startup_ioapic_irq,
2636         .irq_mask               = mask_ioapic_irq,
2637         .irq_unmask             = unmask_ioapic_irq,
2638         .irq_ack                = ack_apic_edge,
2639         .irq_eoi                = ack_apic_level,
2640 #ifdef CONFIG_SMP
2641         .irq_set_affinity       = ioapic_set_affinity,
2642 #endif
2643         .irq_retrigger          = ioapic_retrigger_irq,
2644 };
2645
2646 static struct irq_chip ir_ioapic_chip __read_mostly = {
2647         .name                   = "IR-IO-APIC",
2648         .irq_startup            = startup_ioapic_irq,
2649         .irq_mask               = mask_ioapic_irq,
2650         .irq_unmask             = unmask_ioapic_irq,
2651 #ifdef CONFIG_INTR_REMAP
2652         .irq_ack                = ir_ack_apic_edge,
2653         .irq_eoi                = ir_ack_apic_level,
2654 #ifdef CONFIG_SMP
2655         .irq_set_affinity       = ir_ioapic_set_affinity,
2656 #endif
2657 #endif
2658         .irq_retrigger          = ioapic_retrigger_irq,
2659 };
2660
2661 static inline void init_IO_APIC_traps(void)
2662 {
2663         int irq;
2664         struct irq_desc *desc;
2665         struct irq_cfg *cfg;
2666
2667         /*
2668          * NOTE! The local APIC isn't very good at handling
2669          * multiple interrupts at the same interrupt level.
2670          * As the interrupt level is determined by taking the
2671          * vector number and shifting that right by 4, we
2672          * want to spread these out a bit so that they don't
2673          * all fall in the same interrupt level.
2674          *
2675          * Also, we've got to be careful not to trash gate
2676          * 0x80, because int 0x80 is hm, kind of importantish. ;)
2677          */
2678         for_each_irq_desc(irq, desc) {
2679                 cfg = get_irq_desc_chip_data(desc);
2680                 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
2681                         /*
2682                          * Hmm.. We don't have an entry for this,
2683                          * so default to an old-fashioned 8259
2684                          * interrupt if we can..
2685                          */
2686                         if (irq < legacy_pic->nr_legacy_irqs)
2687                                 legacy_pic->make_irq(irq);
2688                         else
2689                                 /* Strange. Oh, well.. */
2690                                 desc->chip = &no_irq_chip;
2691                 }
2692         }
2693 }
2694
2695 /*
2696  * The local APIC irq-chip implementation:
2697  */
2698
2699 static void mask_lapic_irq(struct irq_data *data)
2700 {
2701         unsigned long v;
2702
2703         v = apic_read(APIC_LVT0);
2704         apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2705 }
2706
2707 static void unmask_lapic_irq(struct irq_data *data)
2708 {
2709         unsigned long v;
2710
2711         v = apic_read(APIC_LVT0);
2712         apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2713 }
2714
2715 static void ack_lapic_irq(struct irq_data *data)
2716 {
2717         ack_APIC_irq();
2718 }
2719
2720 static struct irq_chip lapic_chip __read_mostly = {
2721         .name           = "local-APIC",
2722         .irq_mask       = mask_lapic_irq,
2723         .irq_unmask     = unmask_lapic_irq,
2724         .irq_ack        = ack_lapic_irq,
2725 };
2726
2727 static void lapic_register_intr(int irq)
2728 {
2729         irq_clear_status_flags(irq, IRQ_LEVEL);
2730         set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2731                                       "edge");
2732 }
2733
2734 static void __init setup_nmi(void)
2735 {
2736         /*
2737          * Dirty trick to enable the NMI watchdog ...
2738          * We put the 8259A master into AEOI mode and
2739          * unmask on all local APICs LVT0 as NMI.
2740          *
2741          * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2742          * is from Maciej W. Rozycki - so we do not have to EOI from
2743          * the NMI handler or the timer interrupt.
2744          */
2745         apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2746
2747         enable_NMI_through_LVT0();
2748
2749         apic_printk(APIC_VERBOSE, " done.\n");
2750 }
2751
2752 /*
2753  * This looks a bit hackish but it's about the only one way of sending
2754  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
2755  * not support the ExtINT mode, unfortunately.  We need to send these
2756  * cycles as some i82489DX-based boards have glue logic that keeps the
2757  * 8259A interrupt line asserted until INTA.  --macro
2758  */
2759 static inline void __init unlock_ExtINT_logic(void)
2760 {
2761         int apic, pin, i;
2762         struct IO_APIC_route_entry entry0, entry1;
2763         unsigned char save_control, save_freq_select;
2764
2765         pin  = find_isa_irq_pin(8, mp_INT);
2766         if (pin == -1) {
2767                 WARN_ON_ONCE(1);
2768                 return;
2769         }
2770         apic = find_isa_irq_apic(8, mp_INT);
2771         if (apic == -1) {
2772                 WARN_ON_ONCE(1);
2773                 return;
2774         }
2775
2776         entry0 = ioapic_read_entry(apic, pin);
2777         clear_IO_APIC_pin(apic, pin);
2778
2779         memset(&entry1, 0, sizeof(entry1));
2780
2781         entry1.dest_mode = 0;                   /* physical delivery */
2782         entry1.mask = 0;                        /* unmask IRQ now */
2783         entry1.dest = hard_smp_processor_id();
2784         entry1.delivery_mode = dest_ExtINT;
2785         entry1.polarity = entry0.polarity;
2786         entry1.trigger = 0;
2787         entry1.vector = 0;
2788
2789         ioapic_write_entry(apic, pin, entry1);
2790
2791         save_control = CMOS_READ(RTC_CONTROL);
2792         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2793         CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2794                    RTC_FREQ_SELECT);
2795         CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2796
2797         i = 100;
2798         while (i-- > 0) {
2799                 mdelay(10);
2800                 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2801                         i -= 10;
2802         }
2803
2804         CMOS_WRITE(save_control, RTC_CONTROL);
2805         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2806         clear_IO_APIC_pin(apic, pin);
2807
2808         ioapic_write_entry(apic, pin, entry0);
2809 }
2810
2811 static int disable_timer_pin_1 __initdata;
2812 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
2813 static int __init disable_timer_pin_setup(char *arg)
2814 {
2815         disable_timer_pin_1 = 1;
2816         return 0;
2817 }
2818 early_param("disable_timer_pin_1", disable_timer_pin_setup);
2819
2820 int timer_through_8259 __initdata;
2821
2822 /*
2823  * This code may look a bit paranoid, but it's supposed to cooperate with
2824  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
2825  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
2826  * fanatically on his truly buggy board.
2827  *
2828  * FIXME: really need to revamp this for all platforms.
2829  */
2830 static inline void __init check_timer(void)
2831 {
2832         struct irq_cfg *cfg = get_irq_chip_data(0);
2833         int node = cpu_to_node(0);
2834         int apic1, pin1, apic2, pin2;
2835         unsigned long flags;
2836         int no_pin1 = 0;
2837
2838         local_irq_save(flags);
2839
2840         /*
2841          * get/set the timer IRQ vector:
2842          */
2843         legacy_pic->mask(0);
2844         assign_irq_vector(0, cfg, apic->target_cpus());
2845
2846         /*
2847          * As IRQ0 is to be enabled in the 8259A, the virtual
2848          * wire has to be disabled in the local APIC.  Also
2849          * timer interrupts need to be acknowledged manually in
2850          * the 8259A for the i82489DX when using the NMI
2851          * watchdog as that APIC treats NMIs as level-triggered.
2852          * The AEOI mode will finish them in the 8259A
2853          * automatically.
2854          */
2855         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2856         legacy_pic->init(1);
2857 #ifdef CONFIG_X86_32
2858         {
2859                 unsigned int ver;
2860
2861                 ver = apic_read(APIC_LVR);
2862                 ver = GET_APIC_VERSION(ver);
2863                 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2864         }
2865 #endif
2866
2867         pin1  = find_isa_irq_pin(0, mp_INT);
2868         apic1 = find_isa_irq_apic(0, mp_INT);
2869         pin2  = ioapic_i8259.pin;
2870         apic2 = ioapic_i8259.apic;
2871
2872         apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2873                     "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2874                     cfg->vector, apic1, pin1, apic2, pin2);
2875
2876         /*
2877          * Some BIOS writers are clueless and report the ExtINTA
2878          * I/O APIC input from the cascaded 8259A as the timer
2879          * interrupt input.  So just in case, if only one pin
2880          * was found above, try it both directly and through the
2881          * 8259A.
2882          */
2883         if (pin1 == -1) {
2884                 if (intr_remapping_enabled)
2885                         panic("BIOS bug: timer not connected to IO-APIC");
2886                 pin1 = pin2;
2887                 apic1 = apic2;
2888                 no_pin1 = 1;
2889         } else if (pin2 == -1) {
2890                 pin2 = pin1;
2891                 apic2 = apic1;
2892         }
2893
2894         if (pin1 != -1) {
2895                 /*
2896                  * Ok, does IRQ0 through the IOAPIC work?
2897                  */
2898                 if (no_pin1) {
2899                         add_pin_to_irq_node(cfg, node, apic1, pin1);
2900                         setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2901                 } else {
2902                         /* for edge trigger, setup_ioapic_irq already
2903                          * leave it unmasked.
2904                          * so only need to unmask if it is level-trigger
2905                          * do we really have level trigger timer?
2906                          */
2907                         int idx;
2908                         idx = find_irq_entry(apic1, pin1, mp_INT);
2909                         if (idx != -1 && irq_trigger(idx))
2910                                 unmask_ioapic(cfg);
2911                 }
2912                 if (timer_irq_works()) {
2913                         if (nmi_watchdog == NMI_IO_APIC) {
2914                                 setup_nmi();
2915                                 legacy_pic->unmask(0);
2916                         }
2917                         if (disable_timer_pin_1 > 0)
2918                                 clear_IO_APIC_pin(0, pin1);
2919                         goto out;
2920                 }
2921                 if (intr_remapping_enabled)
2922                         panic("timer doesn't work through Interrupt-remapped IO-APIC");
2923                 local_irq_disable();
2924                 clear_IO_APIC_pin(apic1, pin1);
2925                 if (!no_pin1)
2926                         apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2927                                     "8254 timer not connected to IO-APIC\n");
2928
2929                 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2930                             "(IRQ0) through the 8259A ...\n");
2931                 apic_printk(APIC_QUIET, KERN_INFO
2932                             "..... (found apic %d pin %d) ...\n", apic2, pin2);
2933                 /*
2934                  * legacy devices should be connected to IO APIC #0
2935                  */
2936                 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
2937                 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2938                 legacy_pic->unmask(0);
2939                 if (timer_irq_works()) {
2940                         apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2941                         timer_through_8259 = 1;
2942                         if (nmi_watchdog == NMI_IO_APIC) {
2943                                 legacy_pic->mask(0);
2944                                 setup_nmi();
2945                                 legacy_pic->unmask(0);
2946                         }
2947                         goto out;
2948                 }
2949                 /*
2950                  * Cleanup, just in case ...
2951                  */
2952                 local_irq_disable();
2953                 legacy_pic->mask(0);
2954                 clear_IO_APIC_pin(apic2, pin2);
2955                 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2956         }
2957
2958         if (nmi_watchdog == NMI_IO_APIC) {
2959                 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2960                             "through the IO-APIC - disabling NMI Watchdog!\n");
2961                 nmi_watchdog = NMI_NONE;
2962         }
2963 #ifdef CONFIG_X86_32
2964         timer_ack = 0;
2965 #endif
2966
2967         apic_printk(APIC_QUIET, KERN_INFO
2968                     "...trying to set up timer as Virtual Wire IRQ...\n");
2969
2970         lapic_register_intr(0);
2971         apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector);     /* Fixed mode */
2972         legacy_pic->unmask(0);
2973
2974         if (timer_irq_works()) {
2975                 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2976                 goto out;
2977         }
2978         local_irq_disable();
2979         legacy_pic->mask(0);
2980         apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2981         apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2982
2983         apic_printk(APIC_QUIET, KERN_INFO
2984                     "...trying to set up timer as ExtINT IRQ...\n");
2985
2986         legacy_pic->init(0);
2987         legacy_pic->make_irq(0);
2988         apic_write(APIC_LVT0, APIC_DM_EXTINT);
2989
2990         unlock_ExtINT_logic();
2991
2992         if (timer_irq_works()) {
2993                 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2994                 goto out;
2995         }
2996         local_irq_disable();
2997         apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2998         panic("IO-APIC + timer doesn't work!  Boot with apic=debug and send a "
2999                 "report.  Then try booting with the 'noapic' option.\n");
3000 out:
3001         local_irq_restore(flags);
3002 }
3003
3004 /*
3005  * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
3006  * to devices.  However there may be an I/O APIC pin available for
3007  * this interrupt regardless.  The pin may be left unconnected, but
3008  * typically it will be reused as an ExtINT cascade interrupt for
3009  * the master 8259A.  In the MPS case such a pin will normally be
3010  * reported as an ExtINT interrupt in the MP table.  With ACPI
3011  * there is no provision for ExtINT interrupts, and in the absence
3012  * of an override it would be treated as an ordinary ISA I/O APIC
3013  * interrupt, that is edge-triggered and unmasked by default.  We
3014  * used to do this, but it caused problems on some systems because
3015  * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3016  * the same ExtINT cascade interrupt to drive the local APIC of the
3017  * bootstrap processor.  Therefore we refrain from routing IRQ2 to
3018  * the I/O APIC in all cases now.  No actual device should request
3019  * it anyway.  --macro
3020  */
3021 #define PIC_IRQS        (1UL << PIC_CASCADE_IR)
3022
3023 void __init setup_IO_APIC(void)
3024 {
3025
3026         /*
3027          * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3028          */
3029         io_apic_irqs = legacy_pic->nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
3030
3031         apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
3032         /*
3033          * Set up IO-APIC IRQ routing.
3034          */
3035         x86_init.mpparse.setup_ioapic_ids();
3036
3037         sync_Arb_IDs();
3038         setup_IO_APIC_irqs();
3039         init_IO_APIC_traps();
3040         if (legacy_pic->nr_legacy_irqs)
3041                 check_timer();
3042 }
3043
3044 /*
3045  *      Called after all the initialization is done. If we didnt find any
3046  *      APIC bugs then we can allow the modify fast path
3047  */
3048
3049 static int __init io_apic_bug_finalize(void)
3050 {
3051         if (sis_apic_bug == -1)
3052                 sis_apic_bug = 0;
3053         return 0;
3054 }
3055
3056 late_initcall(io_apic_bug_finalize);
3057
3058 struct sysfs_ioapic_data {
3059         struct sys_device dev;
3060         struct IO_APIC_route_entry entry[0];
3061 };
3062 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
3063
3064 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
3065 {
3066         struct IO_APIC_route_entry *entry;
3067         struct sysfs_ioapic_data *data;
3068         int i;
3069
3070         data = container_of(dev, struct sysfs_ioapic_data, dev);
3071         entry = data->entry;
3072         for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3073                 *entry = ioapic_read_entry(dev->id, i);
3074
3075         return 0;
3076 }
3077
3078 static int ioapic_resume(struct sys_device *dev)
3079 {
3080         struct IO_APIC_route_entry *entry;
3081         struct sysfs_ioapic_data *data;
3082         unsigned long flags;
3083         union IO_APIC_reg_00 reg_00;
3084         int i;
3085
3086         data = container_of(dev, struct sysfs_ioapic_data, dev);
3087         entry = data->entry;
3088
3089         raw_spin_lock_irqsave(&ioapic_lock, flags);
3090         reg_00.raw = io_apic_read(dev->id, 0);
3091         if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3092                 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
3093                 io_apic_write(dev->id, 0, reg_00.raw);
3094         }
3095         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3096         for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
3097                 ioapic_write_entry(dev->id, i, entry[i]);
3098
3099         return 0;
3100 }
3101
3102 static struct sysdev_class ioapic_sysdev_class = {
3103         .name = "ioapic",
3104         .suspend = ioapic_suspend,
3105         .resume = ioapic_resume,
3106 };
3107
3108 static int __init ioapic_init_sysfs(void)
3109 {
3110         struct sys_device * dev;
3111         int i, size, error;
3112
3113         error = sysdev_class_register(&ioapic_sysdev_class);
3114         if (error)
3115                 return error;
3116
3117         for (i = 0; i < nr_ioapics; i++ ) {
3118                 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
3119                         * sizeof(struct IO_APIC_route_entry);
3120                 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
3121                 if (!mp_ioapic_data[i]) {
3122                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3123                         continue;
3124                 }
3125                 dev = &mp_ioapic_data[i]->dev;
3126                 dev->id = i;
3127                 dev->cls = &ioapic_sysdev_class;
3128                 error = sysdev_register(dev);
3129                 if (error) {
3130                         kfree(mp_ioapic_data[i]);
3131                         mp_ioapic_data[i] = NULL;
3132                         printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3133                         continue;
3134                 }
3135         }
3136
3137         return 0;
3138 }
3139
3140 device_initcall(ioapic_init_sysfs);
3141
3142 /*
3143  * Dynamic irq allocate and deallocation
3144  */
3145 unsigned int create_irq_nr(unsigned int irq_want, int node)
3146 {
3147         /* Allocate an unused irq */
3148         unsigned int irq;
3149         unsigned int new;
3150         unsigned long flags;
3151         struct irq_cfg *cfg_new = NULL;
3152         struct irq_desc *desc_new = NULL;
3153
3154         irq = 0;
3155         if (irq_want < nr_irqs_gsi)
3156                 irq_want = nr_irqs_gsi;
3157
3158         raw_spin_lock_irqsave(&vector_lock, flags);
3159         for (new = irq_want; new < nr_irqs; new++) {
3160                 desc_new = irq_to_desc_alloc_node(new, node);
3161                 if (!desc_new) {
3162                         printk(KERN_INFO "can not get irq_desc for %d\n", new);
3163                         continue;
3164                 }
3165                 cfg_new = get_irq_desc_chip_data(desc_new);
3166
3167                 if (cfg_new->vector != 0)
3168                         continue;
3169
3170                 desc_new = move_irq_desc(desc_new, node);
3171                 cfg_new = get_irq_desc_chip_data(desc_new);
3172
3173                 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
3174                         irq = new;
3175                 break;
3176         }
3177         raw_spin_unlock_irqrestore(&vector_lock, flags);
3178
3179         if (irq > 0)
3180                 dynamic_irq_init_keep_chip_data(irq);
3181
3182         return irq;
3183 }
3184
3185 int create_irq(void)
3186 {
3187         int node = cpu_to_node(0);
3188         unsigned int irq_want;
3189         int irq;
3190
3191         irq_want = nr_irqs_gsi;
3192         irq = create_irq_nr(irq_want, node);
3193
3194         if (irq == 0)
3195                 irq = -1;
3196
3197         return irq;
3198 }
3199
3200 void destroy_irq(unsigned int irq)
3201 {
3202         unsigned long flags;
3203
3204         dynamic_irq_cleanup_keep_chip_data(irq);
3205
3206         free_irte(irq);
3207         raw_spin_lock_irqsave(&vector_lock, flags);
3208         __clear_irq_vector(irq, get_irq_chip_data(irq));
3209         raw_spin_unlock_irqrestore(&vector_lock, flags);
3210 }
3211
3212 /*
3213  * MSI message composition
3214  */
3215 #ifdef CONFIG_PCI_MSI
3216 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq,
3217                            struct msi_msg *msg, u8 hpet_id)
3218 {
3219         struct irq_cfg *cfg;
3220         int err;
3221         unsigned dest;
3222
3223         if (disable_apic)
3224                 return -ENXIO;
3225
3226         cfg = irq_cfg(irq);
3227         err = assign_irq_vector(irq, cfg, apic->target_cpus());
3228         if (err)
3229                 return err;
3230
3231         dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
3232
3233         if (irq_remapped(irq)) {
3234                 struct irte irte;
3235                 int ir_index;
3236                 u16 sub_handle;
3237
3238                 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3239                 BUG_ON(ir_index == -1);
3240
3241                 prepare_irte(&irte, cfg->vector, dest);
3242
3243                 /* Set source-id of interrupt request */
3244                 if (pdev)
3245                         set_msi_sid(&irte, pdev);
3246                 else
3247                         set_hpet_sid(&irte, hpet_id);
3248
3249                 modify_irte(irq, &irte);
3250
3251                 msg->address_hi = MSI_ADDR_BASE_HI;
3252                 msg->data = sub_handle;
3253                 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3254                                   MSI_ADDR_IR_SHV |
3255                                   MSI_ADDR_IR_INDEX1(ir_index) |
3256                                   MSI_ADDR_IR_INDEX2(ir_index);
3257         } else {
3258                 if (x2apic_enabled())
3259                         msg->address_hi = MSI_ADDR_BASE_HI |
3260                                           MSI_ADDR_EXT_DEST_ID(dest);
3261                 else
3262                         msg->address_hi = MSI_ADDR_BASE_HI;
3263
3264                 msg->address_lo =
3265                         MSI_ADDR_BASE_LO |
3266                         ((apic->irq_dest_mode == 0) ?
3267                                 MSI_ADDR_DEST_MODE_PHYSICAL:
3268                                 MSI_ADDR_DEST_MODE_LOGICAL) |
3269                         ((apic->irq_delivery_mode != dest_LowestPrio) ?
3270                                 MSI_ADDR_REDIRECTION_CPU:
3271                                 MSI_ADDR_REDIRECTION_LOWPRI) |
3272                         MSI_ADDR_DEST_ID(dest);
3273
3274                 msg->data =
3275                         MSI_DATA_TRIGGER_EDGE |
3276                         MSI_DATA_LEVEL_ASSERT |
3277                         ((apic->irq_delivery_mode != dest_LowestPrio) ?
3278                                 MSI_DATA_DELIVERY_FIXED:
3279                                 MSI_DATA_DELIVERY_LOWPRI) |
3280                         MSI_DATA_VECTOR(cfg->vector);
3281         }
3282         return err;
3283 }
3284
3285 #ifdef CONFIG_SMP
3286 static int
3287 msi_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
3288 {
3289         struct irq_cfg *cfg = data->chip_data;
3290         struct msi_msg msg;
3291         unsigned int dest;
3292
3293         if (__ioapic_set_affinity(data, mask, &dest))
3294                 return -1;
3295
3296         __get_cached_msi_msg(data->msi_desc, &msg);
3297
3298         msg.data &= ~MSI_DATA_VECTOR_MASK;
3299         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3300         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3301         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3302
3303         __write_msi_msg(data->msi_desc, &msg);
3304
3305         return 0;
3306 }
3307 #ifdef CONFIG_INTR_REMAP
3308 /*
3309  * Migrate the MSI irq to another cpumask. This migration is
3310  * done in the process context using interrupt-remapping hardware.
3311  */
3312 static int
3313 ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
3314 {
3315         struct irq_desc *desc = irq_to_desc(irq);
3316         struct irq_cfg *cfg = get_irq_desc_chip_data(desc);
3317         unsigned int dest;
3318         struct irte irte;
3319
3320         if (get_irte(irq, &irte))
3321                 return -1;
3322
3323         if (__ioapic_set_affinity(&desc->irq_data, mask, &dest))
3324                 return -1;
3325
3326         irte.vector = cfg->vector;
3327         irte.dest_id = IRTE_DEST(dest);
3328
3329         /*
3330          * atomically update the IRTE with the new destination and vector.
3331          */
3332         modify_irte(irq, &irte);
3333
3334         /*
3335          * After this point, all the interrupts will start arriving
3336          * at the new destination. So, time to cleanup the previous
3337          * vector allocation.
3338          */
3339         if (cfg->move_in_progress)
3340                 send_cleanup_vector(cfg);
3341
3342         return 0;
3343 }
3344
3345 #endif
3346 #endif /* CONFIG_SMP */
3347
3348 /*
3349  * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3350  * which implement the MSI or MSI-X Capability Structure.
3351  */
3352 static struct irq_chip msi_chip = {
3353         .name                   = "PCI-MSI",
3354         .irq_unmask             = unmask_msi_irq,
3355         .irq_mask               = mask_msi_irq,
3356         .irq_ack                = ack_apic_edge,
3357 #ifdef CONFIG_SMP
3358         .irq_set_affinity       = msi_set_affinity,
3359 #endif
3360         .irq_retrigger          = ioapic_retrigger_irq,
3361 };
3362
3363 static struct irq_chip msi_ir_chip = {
3364         .name           = "IR-PCI-MSI",
3365         .irq_unmask     = unmask_msi_irq,
3366         .irq_mask       = mask_msi_irq,
3367 #ifdef CONFIG_INTR_REMAP
3368         .irq_ack        = ir_ack_apic_edge,
3369 #ifdef CONFIG_SMP
3370         .set_affinity   = ir_set_msi_irq_affinity,
3371 #endif
3372 #endif
3373         .irq_retrigger  = ioapic_retrigger_irq,
3374 };
3375
3376 /*
3377  * Map the PCI dev to the corresponding remapping hardware unit
3378  * and allocate 'nvec' consecutive interrupt-remapping table entries
3379  * in it.
3380  */
3381 static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3382 {
3383         struct intel_iommu *iommu;
3384         int index;
3385
3386         iommu = map_dev_to_ir(dev);
3387         if (!iommu) {
3388                 printk(KERN_ERR
3389                        "Unable to map PCI %s to iommu\n", pci_name(dev));
3390                 return -ENOENT;
3391         }
3392
3393         index = alloc_irte(iommu, irq, nvec);
3394         if (index < 0) {
3395                 printk(KERN_ERR
3396                        "Unable to allocate %d IRTE for PCI %s\n", nvec,
3397                        pci_name(dev));
3398                 return -ENOSPC;
3399         }
3400         return index;
3401 }
3402
3403 static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
3404 {
3405         struct msi_msg msg;
3406         int ret;
3407
3408         ret = msi_compose_msg(dev, irq, &msg, -1);
3409         if (ret < 0)
3410                 return ret;
3411
3412         set_irq_msi(irq, msidesc);
3413         write_msi_msg(irq, &msg);
3414
3415         if (irq_remapped(irq)) {
3416                 irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
3417                 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3418         } else
3419                 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
3420
3421         dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3422
3423         return 0;
3424 }
3425
3426 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3427 {
3428         int node, ret, sub_handle, index = 0;
3429         unsigned int irq, irq_want;
3430         struct msi_desc *msidesc;
3431         struct intel_iommu *iommu = NULL;
3432
3433         /* x86 doesn't support multiple MSI yet */
3434         if (type == PCI_CAP_ID_MSI && nvec > 1)
3435                 return 1;
3436
3437         node = dev_to_node(&dev->dev);
3438         irq_want = nr_irqs_gsi;
3439         sub_handle = 0;
3440         list_for_each_entry(msidesc, &dev->msi_list, list) {
3441                 irq = create_irq_nr(irq_want, node);
3442                 if (irq == 0)
3443                         return -1;
3444                 irq_want = irq + 1;
3445                 if (!intr_remapping_enabled)
3446                         goto no_ir;
3447
3448                 if (!sub_handle) {
3449                         /*
3450                          * allocate the consecutive block of IRTE's
3451                          * for 'nvec'
3452                          */
3453                         index = msi_alloc_irte(dev, irq, nvec);
3454                         if (index < 0) {
3455                                 ret = index;
3456                                 goto error;
3457                         }
3458                 } else {
3459                         iommu = map_dev_to_ir(dev);
3460                         if (!iommu) {
3461                                 ret = -ENOENT;
3462                                 goto error;
3463                         }
3464                         /*
3465                          * setup the mapping between the irq and the IRTE
3466                          * base index, the sub_handle pointing to the
3467                          * appropriate interrupt remap table entry.
3468                          */
3469                         set_irte_irq(irq, iommu, index, sub_handle);
3470                 }
3471 no_ir:
3472                 ret = setup_msi_irq(dev, msidesc, irq);
3473                 if (ret < 0)
3474                         goto error;
3475                 sub_handle++;
3476         }
3477         return 0;
3478
3479 error:
3480         destroy_irq(irq);
3481         return ret;
3482 }
3483
3484 void arch_teardown_msi_irq(unsigned int irq)
3485 {
3486         destroy_irq(irq);
3487 }
3488
3489 #if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
3490 #ifdef CONFIG_SMP
3491 static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
3492 {
3493         struct irq_desc *desc = irq_to_desc(irq);
3494         struct irq_cfg *cfg;
3495         struct msi_msg msg;
3496         unsigned int dest;
3497
3498         if (__ioapic_set_affinity(&desc->irq_data, mask, &dest))
3499                 return -1;
3500
3501         cfg = get_irq_desc_chip_data(desc);
3502
3503         dmar_msi_read(irq, &msg);
3504
3505         msg.data &= ~MSI_DATA_VECTOR_MASK;
3506         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3507         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3508         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3509
3510         dmar_msi_write(irq, &msg);
3511
3512         return 0;
3513 }
3514
3515 #endif /* CONFIG_SMP */
3516
3517 static struct irq_chip dmar_msi_type = {
3518         .name = "DMAR_MSI",
3519         .irq_unmask = dmar_msi_unmask,
3520         .irq_mask = dmar_msi_mask,
3521         .irq_ack = ack_apic_edge,
3522 #ifdef CONFIG_SMP
3523         .set_affinity = dmar_msi_set_affinity,
3524 #endif
3525         .irq_retrigger = ioapic_retrigger_irq,
3526 };
3527
3528 int arch_setup_dmar_msi(unsigned int irq)
3529 {
3530         int ret;
3531         struct msi_msg msg;
3532
3533         ret = msi_compose_msg(NULL, irq, &msg, -1);
3534         if (ret < 0)
3535                 return ret;
3536         dmar_msi_write(irq, &msg);
3537         set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3538                 "edge");
3539         return 0;
3540 }
3541 #endif
3542
3543 #ifdef CONFIG_HPET_TIMER
3544
3545 #ifdef CONFIG_SMP
3546 static int hpet_msi_set_affinity(struct irq_data *data,
3547                                  const struct cpumask *mask, bool force)
3548 {
3549         struct irq_desc *desc = irq_to_desc(data->irq);
3550         struct irq_cfg *cfg = data->chip_data;
3551         struct msi_msg msg;
3552         unsigned int dest;
3553
3554         if (__ioapic_set_affinity(&desc->irq_data, mask, &dest))
3555                 return -1;
3556
3557         hpet_msi_read(data->handler_data, &msg);
3558
3559         msg.data &= ~MSI_DATA_VECTOR_MASK;
3560         msg.data |= MSI_DATA_VECTOR(cfg->vector);
3561         msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3562         msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3563
3564         hpet_msi_write(data->handler_data, &msg);
3565
3566         return 0;
3567 }
3568
3569 #endif /* CONFIG_SMP */
3570
3571 static struct irq_chip ir_hpet_msi_type = {
3572         .name = "IR-HPET_MSI",
3573         .irq_unmask = hpet_msi_unmask,
3574         .irq_mask = hpet_msi_mask,
3575 #ifdef CONFIG_INTR_REMAP
3576         .irq_ack = ir_ack_apic_edge,
3577 #ifdef CONFIG_SMP
3578         .set_affinity = ir_set_msi_irq_affinity,
3579 #endif
3580 #endif
3581         .irq_retrigger = ioapic_retrigger_irq,
3582 };
3583
3584 static struct irq_chip hpet_msi_type = {
3585         .name = "HPET_MSI",
3586         .irq_unmask = hpet_msi_unmask,
3587         .irq_mask = hpet_msi_mask,
3588         .irq_ack = ack_apic_edge,
3589 #ifdef CONFIG_SMP
3590         .irq_set_affinity = hpet_msi_set_affinity,
3591 #endif
3592         .irq_retrigger = ioapic_retrigger_irq,
3593 };
3594
3595 int arch_setup_hpet_msi(unsigned int irq, unsigned int id)
3596 {
3597         struct msi_msg msg;
3598         int ret;
3599
3600         if (intr_remapping_enabled) {
3601                 struct intel_iommu *iommu = map_hpet_to_ir(id);
3602                 int index;
3603
3604                 if (!iommu)
3605                         return -1;
3606
3607                 index = alloc_irte(iommu, irq, 1);
3608                 if (index < 0)
3609                         return -1;
3610         }
3611
3612         ret = msi_compose_msg(NULL, irq, &msg, id);
3613         if (ret < 0)
3614                 return ret;
3615
3616         hpet_msi_write(get_irq_data(irq), &msg);
3617         irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
3618         if (irq_remapped(irq))
3619                 set_irq_chip_and_handler_name(irq, &ir_hpet_msi_type,
3620                                               handle_edge_irq, "edge");
3621         else
3622                 set_irq_chip_and_handler_name(irq, &hpet_msi_type,
3623                                               handle_edge_irq, "edge");
3624
3625         return 0;
3626 }
3627 #endif
3628
3629 #endif /* CONFIG_PCI_MSI */
3630 /*
3631  * Hypertransport interrupt support
3632  */
3633 #ifdef CONFIG_HT_IRQ
3634
3635 #ifdef CONFIG_SMP
3636
3637 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
3638 {
3639         struct ht_irq_msg msg;
3640         fetch_ht_irq_msg(irq, &msg);
3641
3642         msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
3643         msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
3644
3645         msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
3646         msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
3647
3648         write_ht_irq_msg(irq, &msg);
3649 }
3650
3651 static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
3652 {
3653         struct irq_desc *desc = irq_to_desc(irq);
3654         struct irq_cfg *cfg;
3655         unsigned int dest;
3656
3657         if (__ioapic_set_affinity(&desc->irq_data, mask, &dest))
3658                 return -1;
3659
3660         cfg = get_irq_desc_chip_data(desc);
3661
3662         target_ht_irq(irq, dest, cfg->vector);
3663
3664         return 0;
3665 }
3666
3667 #endif
3668
3669 static struct irq_chip ht_irq_chip = {
3670         .name           = "PCI-HT",
3671         .irq_mask       = mask_ht_irq,
3672         .irq_unmask     = unmask_ht_irq,
3673         .irq_ack        = ack_apic_edge,
3674 #ifdef CONFIG_SMP
3675         .set_affinity   = set_ht_irq_affinity,
3676 #endif
3677         .irq_retrigger  = ioapic_retrigger_irq,
3678 };
3679
3680 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3681 {
3682         struct irq_cfg *cfg;
3683         int err;
3684
3685         if (disable_apic)
3686                 return -ENXIO;
3687
3688         cfg = irq_cfg(irq);
3689         err = assign_irq_vector(irq, cfg, apic->target_cpus());
3690         if (!err) {
3691                 struct ht_irq_msg msg;
3692                 unsigned dest;
3693
3694                 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3695                                                     apic->target_cpus());
3696
3697                 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
3698
3699                 msg.address_lo =
3700                         HT_IRQ_LOW_BASE |
3701                         HT_IRQ_LOW_DEST_ID(dest) |
3702                         HT_IRQ_LOW_VECTOR(cfg->vector) |
3703                         ((apic->irq_dest_mode == 0) ?
3704                                 HT_IRQ_LOW_DM_PHYSICAL :
3705                                 HT_IRQ_LOW_DM_LOGICAL) |
3706                         HT_IRQ_LOW_RQEOI_EDGE |
3707                         ((apic->irq_delivery_mode != dest_LowestPrio) ?
3708                                 HT_IRQ_LOW_MT_FIXED :
3709                                 HT_IRQ_LOW_MT_ARBITRATED) |
3710                         HT_IRQ_LOW_IRQ_MASKED;
3711
3712                 write_ht_irq_msg(irq, &msg);
3713
3714                 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3715                                               handle_edge_irq, "edge");
3716
3717                 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
3718         }
3719         return err;
3720 }
3721 #endif /* CONFIG_HT_IRQ */
3722
3723 int __init io_apic_get_redir_entries (int ioapic)
3724 {
3725         union IO_APIC_reg_01    reg_01;
3726         unsigned long flags;
3727
3728         raw_spin_lock_irqsave(&ioapic_lock, flags);
3729         reg_01.raw = io_apic_read(ioapic, 1);
3730         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3731
3732         /* The register returns the maximum index redir index
3733          * supported, which is one less than the total number of redir
3734          * entries.
3735          */
3736         return reg_01.bits.entries + 1;
3737 }
3738
3739 void __init probe_nr_irqs_gsi(void)
3740 {
3741         int nr;
3742
3743         nr = gsi_top + NR_IRQS_LEGACY;
3744         if (nr > nr_irqs_gsi)
3745                 nr_irqs_gsi = nr;
3746
3747         printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
3748 }
3749
3750 #ifdef CONFIG_SPARSE_IRQ
3751 int __init arch_probe_nr_irqs(void)
3752 {
3753         int nr;
3754
3755         if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3756                 nr_irqs = NR_VECTORS * nr_cpu_ids;
3757
3758         nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3759 #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3760         /*
3761          * for MSI and HT dyn irq
3762          */
3763         nr += nr_irqs_gsi * 16;
3764 #endif
3765         if (nr < nr_irqs)
3766                 nr_irqs = nr;
3767
3768         return NR_IRQS_LEGACY;
3769 }
3770 #endif
3771
3772 static int __io_apic_set_pci_routing(struct device *dev, int irq,
3773                                 struct io_apic_irq_attr *irq_attr)
3774 {
3775         struct irq_desc *desc;
3776         struct irq_cfg *cfg;
3777         int node;
3778         int ioapic, pin;
3779         int trigger, polarity;
3780
3781         ioapic = irq_attr->ioapic;
3782         if (!IO_APIC_IRQ(irq)) {
3783                 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3784                         ioapic);
3785                 return -EINVAL;
3786         }
3787
3788         if (dev)
3789                 node = dev_to_node(dev);
3790         else
3791                 node = cpu_to_node(0);
3792
3793         desc = irq_to_desc_alloc_node(irq, node);
3794         if (!desc) {
3795                 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3796                 return 0;
3797         }
3798
3799         pin = irq_attr->ioapic_pin;
3800         trigger = irq_attr->trigger;
3801         polarity = irq_attr->polarity;
3802
3803         cfg = get_irq_desc_chip_data(desc);
3804
3805         /*
3806          * IRQs < 16 are already in the irq_2_pin[] map
3807          */
3808         if (irq >= legacy_pic->nr_legacy_irqs) {
3809                 if (add_pin_to_irq_node_nopanic(cfg, node, ioapic, pin)) {
3810                         printk(KERN_INFO "can not add pin %d for irq %d\n",
3811                                 pin, irq);
3812                         return 0;
3813                 }
3814         }
3815
3816         setup_ioapic_irq(ioapic, pin, irq, cfg, trigger, polarity);
3817
3818         return 0;
3819 }
3820
3821 int io_apic_set_pci_routing(struct device *dev, int irq,
3822                                 struct io_apic_irq_attr *irq_attr)
3823 {
3824         int ioapic, pin;
3825         /*
3826          * Avoid pin reprogramming.  PRTs typically include entries
3827          * with redundant pin->gsi mappings (but unique PCI devices);
3828          * we only program the IOAPIC on the first.
3829          */
3830         ioapic = irq_attr->ioapic;
3831         pin = irq_attr->ioapic_pin;
3832         if (test_bit(pin, mp_ioapic_routing[ioapic].pin_programmed)) {
3833                 pr_debug("Pin %d-%d already programmed\n",
3834                          mp_ioapics[ioapic].apicid, pin);
3835                 return 0;
3836         }
3837         set_bit(pin, mp_ioapic_routing[ioapic].pin_programmed);
3838
3839         return __io_apic_set_pci_routing(dev, irq, irq_attr);
3840 }
3841
3842 u8 __init io_apic_unique_id(u8 id)
3843 {
3844 #ifdef CONFIG_X86_32
3845         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
3846             !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
3847                 return io_apic_get_unique_id(nr_ioapics, id);
3848         else
3849                 return id;
3850 #else
3851         int i;
3852         DECLARE_BITMAP(used, 256);
3853
3854         bitmap_zero(used, 256);
3855         for (i = 0; i < nr_ioapics; i++) {
3856                 struct mpc_ioapic *ia = &mp_ioapics[i];
3857                 __set_bit(ia->apicid, used);
3858         }
3859         if (!test_bit(id, used))
3860                 return id;
3861         return find_first_zero_bit(used, 256);
3862 #endif
3863 }
3864
3865 #ifdef CONFIG_X86_32
3866 int __init io_apic_get_unique_id(int ioapic, int apic_id)
3867 {
3868         union IO_APIC_reg_00 reg_00;
3869         static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3870         physid_mask_t tmp;
3871         unsigned long flags;
3872         int i = 0;
3873
3874         /*
3875          * The P4 platform supports up to 256 APIC IDs on two separate APIC
3876          * buses (one for LAPICs, one for IOAPICs), where predecessors only
3877          * supports up to 16 on one shared APIC bus.
3878          *
3879          * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3880          *      advantage of new APIC bus architecture.
3881          */
3882
3883         if (physids_empty(apic_id_map))
3884                 apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
3885
3886         raw_spin_lock_irqsave(&ioapic_lock, flags);
3887         reg_00.raw = io_apic_read(ioapic, 0);
3888         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3889
3890         if (apic_id >= get_physical_broadcast()) {
3891                 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3892                         "%d\n", ioapic, apic_id, reg_00.bits.ID);
3893                 apic_id = reg_00.bits.ID;
3894         }
3895
3896         /*
3897          * Every APIC in a system must have a unique ID or we get lots of nice
3898          * 'stuck on smp_invalidate_needed IPI wait' messages.
3899          */
3900         if (apic->check_apicid_used(&apic_id_map, apic_id)) {
3901
3902                 for (i = 0; i < get_physical_broadcast(); i++) {
3903                         if (!apic->check_apicid_used(&apic_id_map, i))
3904                                 break;
3905                 }
3906
3907                 if (i == get_physical_broadcast())
3908                         panic("Max apic_id exceeded!\n");
3909
3910                 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3911                         "trying %d\n", ioapic, apic_id, i);
3912
3913                 apic_id = i;
3914         }
3915
3916         apic->apicid_to_cpu_present(apic_id, &tmp);
3917         physids_or(apic_id_map, apic_id_map, tmp);
3918
3919         if (reg_00.bits.ID != apic_id) {
3920                 reg_00.bits.ID = apic_id;
3921
3922                 raw_spin_lock_irqsave(&ioapic_lock, flags);
3923                 io_apic_write(ioapic, 0, reg_00.raw);
3924                 reg_00.raw = io_apic_read(ioapic, 0);
3925                 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3926
3927                 /* Sanity check */
3928                 if (reg_00.bits.ID != apic_id) {
3929                         printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3930                         return -1;
3931                 }
3932         }
3933
3934         apic_printk(APIC_VERBOSE, KERN_INFO
3935                         "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3936
3937         return apic_id;
3938 }
3939 #endif
3940
3941 int __init io_apic_get_version(int ioapic)
3942 {
3943         union IO_APIC_reg_01    reg_01;
3944         unsigned long flags;
3945
3946         raw_spin_lock_irqsave(&ioapic_lock, flags);
3947         reg_01.raw = io_apic_read(ioapic, 1);
3948         raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3949
3950         return reg_01.bits.version;
3951 }
3952
3953 int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
3954 {
3955         int ioapic, pin, idx;
3956
3957         if (skip_ioapic_setup)
3958                 return -1;
3959
3960         ioapic = mp_find_ioapic(gsi);
3961         if (ioapic < 0)
3962                 return -1;
3963
3964         pin = mp_find_ioapic_pin(ioapic, gsi);
3965         if (pin < 0)
3966                 return -1;
3967
3968         idx = find_irq_entry(ioapic, pin, mp_INT);
3969         if (idx < 0)
3970                 return -1;
3971
3972         *trigger = irq_trigger(idx);
3973         *polarity = irq_polarity(idx);
3974         return 0;
3975 }
3976
3977 /*
3978  * This function currently is only a helper for the i386 smp boot process where
3979  * we need to reprogram the ioredtbls to cater for the cpus which have come online
3980  * so mask in all cases should simply be apic->target_cpus()
3981  */
3982 #ifdef CONFIG_SMP
3983 void __init setup_ioapic_dest(void)
3984 {
3985         int pin, ioapic, irq, irq_entry;
3986         struct irq_desc *desc;
3987         const struct cpumask *mask;
3988
3989         if (skip_ioapic_setup == 1)
3990                 return;
3991
3992         for (ioapic = 0; ioapic < nr_ioapics; ioapic++)
3993         for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3994                 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3995                 if (irq_entry == -1)
3996                         continue;
3997                 irq = pin_2_irq(irq_entry, ioapic, pin);
3998
3999                 if ((ioapic > 0) && (irq > 16))
4000                         continue;
4001
4002                 desc = irq_to_desc(irq);
4003
4004                 /*
4005                  * Honour affinities which have been set in early boot
4006                  */
4007                 if (desc->status &
4008                     (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
4009                         mask = desc->irq_data.affinity;
4010                 else
4011                         mask = apic->target_cpus();
4012
4013                 if (intr_remapping_enabled)
4014                         ir_ioapic_set_affinity(&desc->irq_data, mask, false);
4015                 else
4016                         ioapic_set_affinity(&desc->irq_data, mask, false);
4017         }
4018
4019 }
4020 #endif
4021
4022 #define IOAPIC_RESOURCE_NAME_SIZE 11
4023
4024 static struct resource *ioapic_resources;
4025
4026 static struct resource * __init ioapic_setup_resources(int nr_ioapics)
4027 {
4028         unsigned long n;
4029         struct resource *res;
4030         char *mem;
4031         int i;
4032
4033         if (nr_ioapics <= 0)
4034                 return NULL;
4035
4036         n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4037         n *= nr_ioapics;
4038
4039         mem = alloc_bootmem(n);
4040         res = (void *)mem;
4041
4042         mem += sizeof(struct resource) * nr_ioapics;
4043
4044         for (i = 0; i < nr_ioapics; i++) {
4045                 res[i].name = mem;
4046                 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4047                 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
4048                 mem += IOAPIC_RESOURCE_NAME_SIZE;
4049         }
4050
4051         ioapic_resources = res;
4052
4053         return res;
4054 }
4055
4056 void __init ioapic_init_mappings(void)
4057 {
4058         unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
4059         struct resource *ioapic_res;
4060         int i;
4061
4062         ioapic_res = ioapic_setup_resources(nr_ioapics);
4063         for (i = 0; i < nr_ioapics; i++) {
4064                 if (smp_found_config) {
4065                         ioapic_phys = mp_ioapics[i].apicaddr;
4066 #ifdef CONFIG_X86_32
4067                         if (!ioapic_phys) {
4068                                 printk(KERN_ERR
4069                                        "WARNING: bogus zero IO-APIC "
4070                                        "address found in MPTABLE, "
4071                                        "disabling IO/APIC support!\n");
4072                                 smp_found_config = 0;
4073                                 skip_ioapic_setup = 1;
4074                                 goto fake_ioapic_page;
4075                         }
4076 #endif
4077                 } else {
4078 #ifdef CONFIG_X86_32
4079 fake_ioapic_page:
4080 #endif
4081                         ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
4082                         ioapic_phys = __pa(ioapic_phys);
4083                 }
4084                 set_fixmap_nocache(idx, ioapic_phys);
4085                 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
4086                         __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
4087                         ioapic_phys);
4088                 idx++;
4089
4090                 ioapic_res->start = ioapic_phys;
4091                 ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
4092                 ioapic_res++;
4093         }
4094 }
4095
4096 void __init ioapic_insert_resources(void)
4097 {
4098         int i;
4099         struct resource *r = ioapic_resources;
4100
4101         if (!r) {
4102                 if (nr_ioapics > 0)
4103                         printk(KERN_ERR
4104                                 "IO APIC resources couldn't be allocated.\n");
4105                 return;
4106         }
4107
4108         for (i = 0; i < nr_ioapics; i++) {
4109                 insert_resource(&iomem_resource, r);
4110                 r++;
4111         }
4112 }
4113
4114 int mp_find_ioapic(u32 gsi)
4115 {
4116         int i = 0;
4117
4118         /* Find the IOAPIC that manages this GSI. */
4119         for (i = 0; i < nr_ioapics; i++) {
4120                 if ((gsi >= mp_gsi_routing[i].gsi_base)
4121                     && (gsi <= mp_gsi_routing[i].gsi_end))
4122                         return i;
4123         }
4124
4125         printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
4126         return -1;
4127 }
4128
4129 int mp_find_ioapic_pin(int ioapic, u32 gsi)
4130 {
4131         if (WARN_ON(ioapic == -1))
4132                 return -1;
4133         if (WARN_ON(gsi > mp_gsi_routing[ioapic].gsi_end))
4134                 return -1;
4135
4136         return gsi - mp_gsi_routing[ioapic].gsi_base;
4137 }
4138
4139 static int bad_ioapic(unsigned long address)
4140 {
4141         if (nr_ioapics >= MAX_IO_APICS) {
4142                 printk(KERN_WARNING "WARING: Max # of I/O APICs (%d) exceeded "
4143                        "(found %d), skipping\n", MAX_IO_APICS, nr_ioapics);
4144                 return 1;
4145         }
4146         if (!address) {
4147                 printk(KERN_WARNING "WARNING: Bogus (zero) I/O APIC address"
4148                        " found in table, skipping!\n");
4149                 return 1;
4150         }
4151         return 0;
4152 }
4153
4154 void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
4155 {
4156         int idx = 0;
4157         int entries;
4158
4159         if (bad_ioapic(address))
4160                 return;
4161
4162         idx = nr_ioapics;
4163
4164         mp_ioapics[idx].type = MP_IOAPIC;
4165         mp_ioapics[idx].flags = MPC_APIC_USABLE;
4166         mp_ioapics[idx].apicaddr = address;
4167
4168         set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
4169         mp_ioapics[idx].apicid = io_apic_unique_id(id);
4170         mp_ioapics[idx].apicver = io_apic_get_version(idx);
4171
4172         /*
4173          * Build basic GSI lookup table to facilitate gsi->io_apic lookups
4174          * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
4175          */
4176         entries = io_apic_get_redir_entries(idx);
4177         mp_gsi_routing[idx].gsi_base = gsi_base;
4178         mp_gsi_routing[idx].gsi_end = gsi_base + entries - 1;
4179
4180         /*
4181          * The number of IO-APIC IRQ registers (== #pins):
4182          */
4183         nr_ioapic_registers[idx] = entries;
4184
4185         if (mp_gsi_routing[idx].gsi_end >= gsi_top)
4186                 gsi_top = mp_gsi_routing[idx].gsi_end + 1;
4187
4188         printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
4189                "GSI %d-%d\n", idx, mp_ioapics[idx].apicid,
4190                mp_ioapics[idx].apicver, mp_ioapics[idx].apicaddr,
4191                mp_gsi_routing[idx].gsi_base, mp_gsi_routing[idx].gsi_end);
4192
4193         nr_ioapics++;
4194 }
4195
4196 /* Enable IOAPIC early just for system timer */
4197 void __init pre_init_apic_IRQ0(void)
4198 {
4199         struct irq_cfg *cfg;
4200
4201         printk(KERN_INFO "Early APIC setup for system timer0\n");
4202 #ifndef CONFIG_SMP
4203         phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
4204 #endif
4205         irq_to_desc_alloc_node(0, 0);
4206
4207         setup_local_APIC();
4208
4209         cfg = irq_cfg(0);
4210         add_pin_to_irq_node(cfg, 0, 0, 0);
4211         set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
4212
4213         setup_ioapic_irq(0, 0, 0, cfg, 0, 0);
4214 }