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