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