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