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