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