blob: 444ae9281216c4a08ba655a5f1563810512ff4e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
Thomas Gleixner1f934642015-04-14 10:29:58 +080021 *
22 * Historical information which is worth to be preserved:
23 *
24 * - SiS APIC rmw bug:
25 *
26 * We used to have a workaround for a bug in SiS chips which
27 * required to rewrite the index register for a read-modify-write
28 * operation as the chip lost the index information which was
29 * setup for the read already. We cache the data now, so that
30 * workaround has been removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/interrupt.h>
35#include <linux/init.h>
36#include <linux/delay.h>
37#include <linux/sched.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070038#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mc146818rtc.h>
40#include <linux/compiler.h>
41#include <linux/acpi.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040042#include <linux/export.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010043#include <linux/syscore_ops.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080044#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020045#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020046#include <linux/jiffies.h> /* time_after() */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070048#include <linux/bootmem.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070049
Thomas Gleixnerf7a0c782015-04-14 10:30:08 +080050#include <asm/irqdomain.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/io.h>
52#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053053#include <asm/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070055#include <asm/proto.h>
56#include <asm/acpi.h>
57#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070059#include <asm/i8259.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070060#include <asm/setup.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070061#include <asm/irq_remapping.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +053062#include <asm/hw_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Ingo Molnar7b6aa332009-02-17 13:58:15 +010064#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jiang Liuf44d1692014-06-09 16:19:42 +080066#define for_each_ioapic(idx) \
67 for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
68#define for_each_ioapic_reverse(idx) \
69 for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--)
70#define for_each_pin(idx, pin) \
71 for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++)
72#define for_each_ioapic_pin(idx, pin) \
73 for_each_ioapic((idx)) \
74 for_each_pin((idx), (pin))
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +040075#define for_each_irq_pin(entry, head) \
Yinghai Lua178b872014-10-27 16:11:55 +080076 list_for_each_entry(entry, &head, list)
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010077
Thomas Gleixnerdade7712009-07-25 18:39:36 +020078static DEFINE_RAW_SPINLOCK(ioapic_lock);
Jiang Liud7f3d472014-06-09 16:19:52 +080079static DEFINE_MUTEX(ioapic_mutex);
Jiang Liu44767bf2014-06-09 16:19:53 +080080static unsigned int ioapic_dynirq_base;
Jiang Liub81975e2014-06-09 16:20:11 +080081static int ioapic_initialized;
Yinghai Luefa25592008-08-19 20:50:36 -070082
Jiang Liu44677152015-04-14 10:29:53 +080083struct irq_pin_list {
84 struct list_head list;
85 int apic, pin;
86};
87
Jiang Liu49c7e602015-04-13 14:11:55 +080088struct mp_chip_data {
Jiang Liu44677152015-04-14 10:29:53 +080089 struct list_head irq_2_pin;
Jiang Liu49c7e602015-04-13 14:11:55 +080090 struct IO_APIC_route_entry entry;
91 int trigger;
92 int polarity;
Jiang Liu96ed44b2015-04-13 14:11:58 +080093 u32 count;
Jiang Liu49c7e602015-04-13 14:11:55 +080094 bool isa_irq;
95};
96
Jiang Liu154d9e52015-04-14 10:29:56 +080097struct mp_ioapic_gsi {
98 u32 gsi_base;
99 u32 gsi_end;
100};
101
Suresh Siddhab69c6c32011-05-18 16:31:35 -0700102static struct ioapic {
103 /*
104 * # of IRQ routing registers
105 */
106 int nr_registers;
Suresh Siddha57a6f742011-05-18 16:31:36 -0700107 /*
108 * Saved state during suspend/resume, or while enabling intr-remap.
109 */
110 struct IO_APIC_route_entry *saved_registers;
Suresh Siddhad5371432011-05-18 16:31:37 -0700111 /* I/O APIC config */
112 struct mpc_ioapic mp_config;
Suresh Siddhac040aae2011-05-18 16:31:38 -0700113 /* IO APIC gsi routing info */
114 struct mp_ioapic_gsi gsi_config;
Jiang Liud7f3d472014-06-09 16:19:52 +0800115 struct ioapic_domain_cfg irqdomain_cfg;
116 struct irq_domain *irqdomain;
Jiang Liu15516a32014-10-27 13:21:46 +0800117 struct resource *iomem_res;
Suresh Siddhab69c6c32011-05-18 16:31:35 -0700118} ioapics[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Yinghai Lu6f50d452011-10-12 00:33:48 -0700120#define mpc_ioapic_ver(ioapic_idx) ioapics[ioapic_idx].mp_config.apicver
Suresh Siddhad5371432011-05-18 16:31:37 -0700121
Yinghai Lu6f50d452011-10-12 00:33:48 -0700122int mpc_ioapic_id(int ioapic_idx)
Suresh Siddhad5371432011-05-18 16:31:37 -0700123{
Yinghai Lu6f50d452011-10-12 00:33:48 -0700124 return ioapics[ioapic_idx].mp_config.apicid;
Suresh Siddhad5371432011-05-18 16:31:37 -0700125}
126
Yinghai Lu6f50d452011-10-12 00:33:48 -0700127unsigned int mpc_ioapic_addr(int ioapic_idx)
Suresh Siddhad5371432011-05-18 16:31:37 -0700128{
Yinghai Lu6f50d452011-10-12 00:33:48 -0700129 return ioapics[ioapic_idx].mp_config.apicaddr;
Suresh Siddhad5371432011-05-18 16:31:37 -0700130}
131
Jiang Liu154d9e52015-04-14 10:29:56 +0800132static inline struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
Suresh Siddhac040aae2011-05-18 16:31:38 -0700133{
Yinghai Lu6f50d452011-10-12 00:33:48 -0700134 return &ioapics[ioapic_idx].gsi_config;
Suresh Siddhac040aae2011-05-18 16:31:38 -0700135}
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +0400136
Jiang Liu18e48552014-06-09 16:19:45 +0800137static inline int mp_ioapic_pin_count(int ioapic)
138{
139 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
140
141 return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
142}
143
Jiang Liu154d9e52015-04-14 10:29:56 +0800144static inline u32 mp_pin_to_gsi(int ioapic, int pin)
Jiang Liu18e48552014-06-09 16:19:45 +0800145{
146 return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin;
147}
148
Jiang Liud32932d2015-04-13 14:11:59 +0800149static inline bool mp_is_legacy_irq(int irq)
150{
151 return irq >= 0 && irq < nr_legacy_irqs();
152}
153
Jiang Liu95d76ac2014-06-09 16:19:48 +0800154/*
155 * Initialize all legacy IRQs and all pins on the first IOAPIC
156 * if we have legacy interrupt controller. Kernel boot option "pirq="
157 * may rely on non-legacy pins on the first IOAPIC.
158 */
Jiang Liu18e48552014-06-09 16:19:45 +0800159static inline int mp_init_irq_at_boot(int ioapic, int irq)
160{
Jiang Liu95d76ac2014-06-09 16:19:48 +0800161 if (!nr_legacy_irqs())
162 return 0;
163
Jiang Liud32932d2015-04-13 14:11:59 +0800164 return ioapic == 0 || mp_is_legacy_irq(irq);
Jiang Liu18e48552014-06-09 16:19:45 +0800165}
166
Jiang Liud7f3d472014-06-09 16:19:52 +0800167static inline struct irq_domain *mp_ioapic_irqdomain(int ioapic)
168{
169 return ioapics[ioapic].irqdomain;
170}
171
Suresh Siddhac040aae2011-05-18 16:31:38 -0700172int nr_ioapics;
Feng Tang2a4ab642009-07-07 23:01:15 -0400173
Eric W. Biedermana4384df2010-06-08 11:44:32 -0700174/* The one past the highest gsi number used */
175u32 gsi_top;
Eric W. Biederman57773722010-03-30 01:07:10 -0700176
Alexey Starikovskiy584f7342008-04-04 23:41:32 +0400177/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530178struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +0400179
180/* # of MP IRQ source entries */
181int mp_irq_entries;
182
Paul Gortmakerbb8187d2012-05-17 19:06:13 -0400183#ifdef CONFIG_EISA
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +0400184int mp_bus_id_to_type[MAX_MP_BUSSES];
185#endif
186
187DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
188
Yinghai Luefa25592008-08-19 20:50:36 -0700189int skip_ioapic_setup;
190
Henrik Kretzschmar7167d082011-02-22 15:38:05 +0100191/**
192 * disable_ioapic_support() - disables ioapic support at runtime
193 */
194void disable_ioapic_support(void)
Ingo Molnar65a4e572009-01-31 03:36:17 +0100195{
196#ifdef CONFIG_PCI
197 noioapicquirk = 1;
198 noioapicreroute = -1;
199#endif
200 skip_ioapic_setup = 1;
201}
202
Ingo Molnar54168ed2008-08-20 09:07:45 +0200203static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700204{
205 /* disable IO-APIC */
Henrik Kretzschmar7167d082011-02-22 15:38:05 +0100206 disable_ioapic_support();
Yinghai Luefa25592008-08-19 20:50:36 -0700207 return 0;
208}
209early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200210
Feng Tang2d8009b2010-11-19 11:33:35 +0800211/* Will be called in mpparse/acpi/sfi codes for saving IRQ info */
212void mp_save_irq(struct mpc_intsrc *m)
213{
214 int i;
215
216 apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
217 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
218 m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
219 m->srcbusirq, m->dstapic, m->dstirq);
220
221 for (i = 0; i < mp_irq_entries; i++) {
Feng Tang0e3fa132010-12-08 15:18:57 +0800222 if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
Feng Tang2d8009b2010-11-19 11:33:35 +0800223 return;
224 }
225
Feng Tang0e3fa132010-12-08 15:18:57 +0800226 memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
Feng Tang2d8009b2010-11-19 11:33:35 +0800227 if (++mp_irq_entries == MAX_IRQ_SOURCES)
228 panic("Max # of irq sources exceeded!!\n");
229}
230
Yinghai Lu7e899412014-10-27 13:21:39 +0800231static void alloc_ioapic_saved_registers(int idx)
232{
233 size_t size;
234
235 if (ioapics[idx].saved_registers)
236 return;
237
238 size = sizeof(struct IO_APIC_route_entry) * ioapics[idx].nr_registers;
239 ioapics[idx].saved_registers = kzalloc(size, GFP_KERNEL);
240 if (!ioapics[idx].saved_registers)
241 pr_err("IOAPIC %d: suspend/resume impossible!\n", idx);
242}
243
Jiang Liu15516a32014-10-27 13:21:46 +0800244static void free_ioapic_saved_registers(int idx)
245{
246 kfree(ioapics[idx].saved_registers);
247 ioapics[idx].saved_registers = NULL;
248}
249
Jiang Liu11d686e2014-10-27 16:12:05 +0800250int __init arch_early_ioapic_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800251{
Jiang Liu13315322015-04-13 14:11:56 +0800252 int i;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800253
Jiang Liu95d76ac2014-06-09 16:19:48 +0800254 if (!nr_legacy_irqs())
Jacob Pan1f912332010-02-05 04:06:56 -0800255 io_apic_irqs = ~0UL;
Jacob Pan1f912332010-02-05 04:06:56 -0800256
Yinghai Lu7e899412014-10-27 13:21:39 +0800257 for_each_ioapic(i)
258 alloc_ioapic_saved_registers(i);
Suresh Siddha4c791852011-05-18 16:31:32 -0700259
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800260 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800261}
262
Linus Torvalds130fe052006-11-01 09:11:00 -0800263struct io_apic {
264 unsigned int index;
265 unsigned int unused[3];
266 unsigned int data;
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700267 unsigned int unused2[11];
268 unsigned int eoi;
Linus Torvalds130fe052006-11-01 09:11:00 -0800269};
270
271static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
272{
273 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Suresh Siddhad5371432011-05-18 16:31:37 -0700274 + (mpc_ioapic_addr(idx) & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800275}
276
Jiang Liuad66e1e2015-04-14 10:29:43 +0800277static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700278{
279 struct io_apic __iomem *io_apic = io_apic_base(apic);
280 writel(vector, &io_apic->eoi);
281}
282
Konrad Rzeszutek Wilk4a8e2a32012-03-28 12:37:36 -0400283unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
Linus Torvalds130fe052006-11-01 09:11:00 -0800284{
285 struct io_apic __iomem *io_apic = io_apic_base(apic);
286 writel(reg, &io_apic->index);
287 return readl(&io_apic->data);
288}
289
Jiang Liu9a93d472015-04-14 10:29:55 +0800290static void io_apic_write(unsigned int apic, unsigned int reg,
291 unsigned int value)
Linus Torvalds130fe052006-11-01 09:11:00 -0800292{
293 struct io_apic __iomem *io_apic = io_apic_base(apic);
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400294
Linus Torvalds130fe052006-11-01 09:11:00 -0800295 writel(reg, &io_apic->index);
296 writel(value, &io_apic->data);
297}
298
Andi Kleencf4c6a22006-09-26 10:52:30 +0200299union entry_union {
300 struct { u32 w1, w2; };
301 struct IO_APIC_route_entry entry;
302};
303
Suresh Siddhae57253a2011-08-25 12:01:12 -0700304static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
305{
306 union entry_union eu;
307
308 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
309 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400310
Suresh Siddhae57253a2011-08-25 12:01:12 -0700311 return eu.entry;
312}
313
Andi Kleencf4c6a22006-09-26 10:52:30 +0200314static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
315{
316 union entry_union eu;
317 unsigned long flags;
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400318
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200319 raw_spin_lock_irqsave(&ioapic_lock, flags);
Suresh Siddhae57253a2011-08-25 12:01:12 -0700320 eu.entry = __ioapic_read_entry(apic, pin);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200321 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400322
Andi Kleencf4c6a22006-09-26 10:52:30 +0200323 return eu.entry;
324}
325
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800326/*
327 * When we write a new IO APIC routing entry, we need to write the high
328 * word first! If the mask bit in the low word is clear, we will enable
329 * the interrupt, and we need to make sure the entry is fully populated
330 * before that happens.
331 */
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400332static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleend15512f2006-12-07 02:14:07 +0100333{
Figo.zhang50a8d4d22009-06-17 22:25:20 +0800334 union entry_union eu = {{0, 0}};
335
Andi Kleend15512f2006-12-07 02:14:07 +0100336 eu.entry = e;
337 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
338 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
339}
340
Thomas Gleixner1a8ce7f2010-10-04 21:08:56 +0200341static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleencf4c6a22006-09-26 10:52:30 +0200342{
343 unsigned long flags;
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400344
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200345 raw_spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100346 __ioapic_write_entry(apic, pin, e);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200347 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800348}
349
350/*
351 * When we mask an IO APIC routing entry, we need to write the low
352 * word first, in order to set the mask bit before we change the
353 * high bits!
354 */
355static void ioapic_mask_entry(int apic, int pin)
356{
357 unsigned long flags;
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800358 union entry_union eu = { .entry.mask = IOAPIC_MASKED };
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800359
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200360 raw_spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200361 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
362 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200363 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
368 * shared ISA-space IRQs, so we have to support them. We are super
369 * fast in the common case, and fast for shared ISA-space IRQs.
370 */
Jiang Liu44677152015-04-14 10:29:53 +0800371static int __add_pin_to_irq_node(struct mp_chip_data *data,
372 int node, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Yinghai Lua178b872014-10-27 16:11:55 +0800374 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400376 /* don't allow duplicates */
Jiang Liu44677152015-04-14 10:29:53 +0800377 for_each_irq_pin(entry, data->irq_2_pin)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700378 if (entry->apic == apic && entry->pin == pin)
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400379 return 0;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700380
Jiang Liu44677152015-04-14 10:29:53 +0800381 entry = kzalloc_node(sizeof(struct irq_pin_list), GFP_ATOMIC, node);
Cyrill Gorcunova7428cd2009-08-01 11:48:00 +0400382 if (!entry) {
Joe Perchesc767a542012-05-21 19:50:07 -0700383 pr_err("can not alloc irq_pin_list (%d,%d,%d)\n",
384 node, apic, pin);
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400385 return -ENOMEM;
Cyrill Gorcunova7428cd2009-08-01 11:48:00 +0400386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 entry->apic = apic;
388 entry->pin = pin;
Jiang Liu44677152015-04-14 10:29:53 +0800389 list_add_tail(&entry->list, &data->irq_2_pin);
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700390
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400391 return 0;
392}
393
Jiang Liu44677152015-04-14 10:29:53 +0800394static void __remove_pin_from_irq(struct mp_chip_data *data, int apic, int pin)
Jiang Liudf334be2014-06-09 16:20:06 +0800395{
Yinghai Lua178b872014-10-27 16:11:55 +0800396 struct irq_pin_list *tmp, *entry;
Jiang Liudf334be2014-06-09 16:20:06 +0800397
Jiang Liu44677152015-04-14 10:29:53 +0800398 list_for_each_entry_safe(entry, tmp, &data->irq_2_pin, list)
Jiang Liudf334be2014-06-09 16:20:06 +0800399 if (entry->apic == apic && entry->pin == pin) {
Yinghai Lua178b872014-10-27 16:11:55 +0800400 list_del(&entry->list);
Jiang Liudf334be2014-06-09 16:20:06 +0800401 kfree(entry);
402 return;
Jiang Liudf334be2014-06-09 16:20:06 +0800403 }
404}
405
Jiang Liu44677152015-04-14 10:29:53 +0800406static void add_pin_to_irq_node(struct mp_chip_data *data,
407 int node, int apic, int pin)
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400408{
Jiang Liu44677152015-04-14 10:29:53 +0800409 if (__add_pin_to_irq_node(data, node, apic, pin))
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400410 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413/*
414 * Reroute an IRQ to a different pin.
415 */
Jiang Liu44677152015-04-14 10:29:53 +0800416static void __init replace_pin_at_irq_node(struct mp_chip_data *data, int node,
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700417 int oldapic, int oldpin,
418 int newapic, int newpin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Jeremy Fitzhardinge535b6422009-06-08 03:29:26 -0700420 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Jiang Liu44677152015-04-14 10:29:53 +0800422 for_each_irq_pin(entry, data->irq_2_pin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (entry->apic == oldapic && entry->pin == oldpin) {
424 entry->apic = newapic;
425 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700426 /* every one is different, right? */
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700427 return;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700430
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700431 /* old apic/pin didn't exist, so just add new ones */
Jiang Liu44677152015-04-14 10:29:53 +0800432 add_pin_to_irq_node(data, node, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Jiang Liu44677152015-04-14 10:29:53 +0800435static void io_apic_modify_irq(struct mp_chip_data *data,
Jeremy Fitzhardinge2f210de2009-06-08 02:55:22 -0700436 int mask_and, int mask_or,
437 void (*final)(struct irq_pin_list *entry))
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400438{
Jiang Liu0be275e2015-04-14 10:29:57 +0800439 union entry_union eu;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400440 struct irq_pin_list *entry;
441
Jiang Liu0be275e2015-04-14 10:29:57 +0800442 eu.entry = data->entry;
443 eu.w1 &= mask_and;
444 eu.w1 |= mask_or;
445 data->entry = eu.entry;
446
447 for_each_irq_pin(entry, data->irq_2_pin) {
448 io_apic_write(entry->apic, 0x10 + 2 * entry->pin, eu.w1);
449 if (final)
450 final(entry);
451 }
Suresh Siddhac29d9db2009-12-01 15:31:16 -0800452}
453
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530454static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700455{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400456 /*
457 * Synchronize the IO-APIC and the CPU by doing
458 * a dummy read from the IO-APIC
459 */
460 struct io_apic __iomem *io_apic;
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400461
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400462 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700463 readl(&io_apic->data);
464}
465
Jiang Liu44677152015-04-14 10:29:53 +0800466static void mask_ioapic_irq(struct irq_data *irq_data)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400467{
Jiang Liu44677152015-04-14 10:29:53 +0800468 struct mp_chip_data *data = irq_data->chip_data;
Thomas Gleixnerdd5f15e2010-09-28 15:18:35 +0200469 unsigned long flags;
470
471 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +0800472 io_apic_modify_irq(data, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200473 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Jiang Liu44677152015-04-14 10:29:53 +0800476static void __unmask_ioapic(struct mp_chip_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Jiang Liu44677152015-04-14 10:29:53 +0800478 io_apic_modify_irq(data, ~IO_APIC_REDIR_MASKED, 0, NULL);
Thomas Gleixnerdd5f15e2010-09-28 15:18:35 +0200479}
480
Jiang Liu44677152015-04-14 10:29:53 +0800481static void unmask_ioapic_irq(struct irq_data *irq_data)
Thomas Gleixnerdd5f15e2010-09-28 15:18:35 +0200482{
Jiang Liu44677152015-04-14 10:29:53 +0800483 struct mp_chip_data *data = irq_data->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 unsigned long flags;
485
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200486 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +0800487 __unmask_ioapic(data);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200488 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Suresh Siddhac0205702011-08-25 12:01:13 -0700491/*
492 * IO-APIC versions below 0x20 don't support EOI register.
493 * For the record, here is the information about various versions:
494 * 0Xh 82489DX
495 * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
496 * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
497 * 30h-FFh Reserved
498 *
499 * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
500 * version as 0x2. This is an error with documentation and these ICH chips
501 * use io-apic's of version 0x20.
502 *
503 * For IO-APIC's with EOI register, we use that to do an explicit EOI.
504 * Otherwise, we simulate the EOI message manually by changing the trigger
505 * mode to edge and then back to level, with RTE being masked during this.
506 */
Jiang Liuad66e1e2015-04-14 10:29:43 +0800507static void __eoi_ioapic_pin(int apic, int pin, int vector)
Suresh Siddhac0205702011-08-25 12:01:13 -0700508{
509 if (mpc_ioapic_ver(apic) >= 0x20) {
Joerg Roedelda165322012-09-26 12:44:50 +0200510 io_apic_eoi(apic, vector);
Suresh Siddhac0205702011-08-25 12:01:13 -0700511 } else {
512 struct IO_APIC_route_entry entry, entry1;
513
514 entry = entry1 = __ioapic_read_entry(apic, pin);
515
516 /*
517 * Mask the entry and change the trigger mode to edge.
518 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800519 entry1.mask = IOAPIC_MASKED;
Suresh Siddhac0205702011-08-25 12:01:13 -0700520 entry1.trigger = IOAPIC_EDGE;
521
522 __ioapic_write_entry(apic, pin, entry1);
523
524 /*
525 * Restore the previous level triggered entry.
526 */
527 __ioapic_write_entry(apic, pin, entry);
528 }
529}
530
Andy Shevchenko4faefda2015-10-09 17:24:45 +0300531static void eoi_ioapic_pin(int vector, struct mp_chip_data *data)
Jiang Liud32932d2015-04-13 14:11:59 +0800532{
533 unsigned long flags;
534 struct irq_pin_list *entry;
535
536 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +0800537 for_each_irq_pin(entry, data->irq_2_pin)
Jiang Liuad66e1e2015-04-14 10:29:43 +0800538 __eoi_ioapic_pin(entry->apic, entry->pin, vector);
Suresh Siddhac0205702011-08-25 12:01:13 -0700539 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
543{
544 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200547 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (entry.delivery_mode == dest_SMI)
549 return;
Suresh Siddha1e75b312011-08-25 12:01:11 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
Suresh Siddha1e75b312011-08-25 12:01:11 -0700552 * Make sure the entry is masked and re-read the contents to check
553 * if it is a level triggered pin and if the remote-IRR is set.
554 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800555 if (entry.mask == IOAPIC_UNMASKED) {
556 entry.mask = IOAPIC_MASKED;
Suresh Siddha1e75b312011-08-25 12:01:11 -0700557 ioapic_write_entry(apic, pin, entry);
558 entry = ioapic_read_entry(apic, pin);
559 }
560
561 if (entry.irr) {
Suresh Siddhac0205702011-08-25 12:01:13 -0700562 unsigned long flags;
563
Suresh Siddha1e75b312011-08-25 12:01:11 -0700564 /*
565 * Make sure the trigger mode is set to level. Explicit EOI
566 * doesn't clear the remote-IRR if the trigger mode is not
567 * set to level.
568 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800569 if (entry.trigger == IOAPIC_EDGE) {
Suresh Siddha1e75b312011-08-25 12:01:11 -0700570 entry.trigger = IOAPIC_LEVEL;
571 ioapic_write_entry(apic, pin, entry);
572 }
Suresh Siddhac0205702011-08-25 12:01:13 -0700573 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liuad66e1e2015-04-14 10:29:43 +0800574 __eoi_ioapic_pin(apic, pin, entry.vector);
Suresh Siddhac0205702011-08-25 12:01:13 -0700575 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Suresh Siddha1e75b312011-08-25 12:01:11 -0700576 }
577
578 /*
579 * Clear the rest of the bits in the IO-APIC RTE except for the mask
580 * bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800582 ioapic_mask_entry(apic, pin);
Suresh Siddha1e75b312011-08-25 12:01:11 -0700583 entry = ioapic_read_entry(apic, pin);
584 if (entry.irr)
Joe Perchesc767a542012-05-21 19:50:07 -0700585 pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
Suresh Siddha1e75b312011-08-25 12:01:11 -0700586 mpc_ioapic_id(apic), pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Ingo Molnar54168ed2008-08-20 09:07:45 +0200589static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 int apic, pin;
592
Jiang Liuf44d1692014-06-09 16:19:42 +0800593 for_each_ioapic_pin(apic, pin)
594 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Ingo Molnar54168ed2008-08-20 09:07:45 +0200597#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
599 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
600 * specific CPU-side IRQs.
601 */
602
603#define MAX_PIRQS 8
Yinghai Lu3bd25d02009-02-15 02:54:03 -0800604static int pirq_entries[MAX_PIRQS] = {
605 [0 ... MAX_PIRQS - 1] = -1
606};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static int __init ioapic_pirq_setup(char *str)
609{
610 int i, max;
611 int ints[MAX_PIRQS+1];
612
613 get_options(str, ARRAY_SIZE(ints), ints);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 apic_printk(APIC_VERBOSE, KERN_INFO
616 "PIRQ redirection, working around broken MP-BIOS.\n");
617 max = MAX_PIRQS;
618 if (ints[0] < MAX_PIRQS)
619 max = ints[0];
620
621 for (i = 0; i < max; i++) {
622 apic_printk(APIC_VERBOSE, KERN_DEBUG
623 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
624 /*
625 * PIRQs are mapped upside down, usually.
626 */
627 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
628 }
629 return 1;
630}
631
632__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200633#endif /* CONFIG_X86_32 */
634
Ingo Molnar54168ed2008-08-20 09:07:45 +0200635/*
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700636 * Saves all the IO-APIC RTE's
Ingo Molnar54168ed2008-08-20 09:07:45 +0200637 */
Suresh Siddha31dce142011-05-18 16:31:33 -0700638int save_ioapic_entries(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200639{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200640 int apic, pin;
Suresh Siddha31dce142011-05-18 16:31:33 -0700641 int err = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200642
Jiang Liuf44d1692014-06-09 16:19:42 +0800643 for_each_ioapic(apic) {
Suresh Siddha57a6f742011-05-18 16:31:36 -0700644 if (!ioapics[apic].saved_registers) {
Suresh Siddha31dce142011-05-18 16:31:33 -0700645 err = -ENOMEM;
646 continue;
647 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200648
Jiang Liuf44d1692014-06-09 16:19:42 +0800649 for_each_pin(apic, pin)
Suresh Siddha57a6f742011-05-18 16:31:36 -0700650 ioapics[apic].saved_registers[pin] =
Ingo Molnar54168ed2008-08-20 09:07:45 +0200651 ioapic_read_entry(apic, pin);
Fenghua Yub24696b2009-03-27 14:22:44 -0700652 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400653
Suresh Siddha31dce142011-05-18 16:31:33 -0700654 return err;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200655}
656
Fenghua Yub24696b2009-03-27 14:22:44 -0700657/*
658 * Mask all IO APIC entries.
659 */
Suresh Siddha31dce142011-05-18 16:31:33 -0700660void mask_ioapic_entries(void)
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700661{
662 int apic, pin;
663
Jiang Liuf44d1692014-06-09 16:19:42 +0800664 for_each_ioapic(apic) {
Suresh Siddha2f344d22011-05-24 10:45:31 -0700665 if (!ioapics[apic].saved_registers)
Suresh Siddha31dce142011-05-18 16:31:33 -0700666 continue;
Fenghua Yub24696b2009-03-27 14:22:44 -0700667
Jiang Liuf44d1692014-06-09 16:19:42 +0800668 for_each_pin(apic, pin) {
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700669 struct IO_APIC_route_entry entry;
670
Suresh Siddha57a6f742011-05-18 16:31:36 -0700671 entry = ioapics[apic].saved_registers[pin];
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800672 if (entry.mask == IOAPIC_UNMASKED) {
673 entry.mask = IOAPIC_MASKED;
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700674 ioapic_write_entry(apic, pin, entry);
675 }
676 }
677 }
678}
679
Fenghua Yub24696b2009-03-27 14:22:44 -0700680/*
Suresh Siddha57a6f742011-05-18 16:31:36 -0700681 * Restore IO APIC entries which was saved in the ioapic structure.
Fenghua Yub24696b2009-03-27 14:22:44 -0700682 */
Suresh Siddha31dce142011-05-18 16:31:33 -0700683int restore_ioapic_entries(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200684{
685 int apic, pin;
686
Jiang Liuf44d1692014-06-09 16:19:42 +0800687 for_each_ioapic(apic) {
Suresh Siddha2f344d22011-05-24 10:45:31 -0700688 if (!ioapics[apic].saved_registers)
Suresh Siddha31dce142011-05-18 16:31:33 -0700689 continue;
Fenghua Yub24696b2009-03-27 14:22:44 -0700690
Jiang Liuf44d1692014-06-09 16:19:42 +0800691 for_each_pin(apic, pin)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200692 ioapic_write_entry(apic, pin,
Suresh Siddha57a6f742011-05-18 16:31:36 -0700693 ioapics[apic].saved_registers[pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400694 }
Fenghua Yub24696b2009-03-27 14:22:44 -0700695 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/*
699 * Find the IRQ entry number of a certain pin.
700 */
Yinghai Lu6f50d452011-10-12 00:33:48 -0700701static int find_irq_entry(int ioapic_idx, int pin, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 int i;
704
705 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530706 if (mp_irqs[i].irqtype == type &&
Yinghai Lu6f50d452011-10-12 00:33:48 -0700707 (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530708 mp_irqs[i].dstapic == MP_APIC_ALL) &&
709 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return i;
711
712 return -1;
713}
714
715/*
716 * Find the pin to which IRQ[irq] (ISA) is connected
717 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800718static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 int i;
721
722 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530723 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300725 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530726 (mp_irqs[i].irqtype == type) &&
727 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530729 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 return -1;
732}
733
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800734static int __init find_isa_irq_apic(int irq, int type)
735{
736 int i;
737
738 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530739 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800740
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300741 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530742 (mp_irqs[i].irqtype == type) &&
743 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800744 break;
745 }
Yinghai Lu6f50d452011-10-12 00:33:48 -0700746
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800747 if (i < mp_irq_entries) {
Yinghai Lu6f50d452011-10-12 00:33:48 -0700748 int ioapic_idx;
749
Jiang Liuf44d1692014-06-09 16:19:42 +0800750 for_each_ioapic(ioapic_idx)
Yinghai Lu6f50d452011-10-12 00:33:48 -0700751 if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
752 return ioapic_idx;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800753 }
754
755 return -1;
756}
757
Paul Gortmakerbb8187d2012-05-17 19:06:13 -0400758#ifdef CONFIG_EISA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
760 * EISA Edge/Level control register, ELCR
761 */
762static int EISA_ELCR(unsigned int irq)
763{
Jiang Liu95d76ac2014-06-09 16:19:48 +0800764 if (irq < nr_legacy_irqs()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 unsigned int port = 0x4d0 + (irq >> 3);
766 return (inb(port) >> (irq & 7)) & 1;
767 }
768 apic_printk(APIC_VERBOSE, KERN_INFO
769 "Broken MPtable reports ISA irq %d\n", irq);
770 return 0;
771}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200772
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300773#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800775/* ISA interrupts are always active high edge triggered,
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300776 * when listed as conforming in the MP table. */
777
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800778#define default_ISA_trigger(idx) (IOAPIC_EDGE)
779#define default_ISA_polarity(idx) (IOAPIC_POL_HIGH)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781/* EISA interrupts are always polarity zero and can be edge or level
782 * trigger depending on the ELCR value. If an interrupt is listed as
783 * EISA conforming in the MP table, that means its trigger type must
784 * be read in from the ELCR */
785
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530786#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300787#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800789/* PCI interrupts are always active low level triggered,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * when listed as conforming in the MP table. */
791
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800792#define default_PCI_trigger(idx) (IOAPIC_LEVEL)
793#define default_PCI_polarity(idx) (IOAPIC_POL_LOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Thomas Gleixnerb77cf6a2011-02-23 17:33:53 +0100795static int irq_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530797 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /*
800 * Determine IRQ line polarity (high active or low active):
801 */
Thomas Gleixnerab760852015-04-14 10:30:07 +0800802 switch (mp_irqs[idx].irqflag & 0x03) {
803 case 0:
804 /* conforms to spec, ie. bus-type dependent polarity */
805 if (test_bit(bus, mp_bus_not_pci))
806 return default_ISA_polarity(idx);
807 else
808 return default_PCI_polarity(idx);
809 case 1:
810 return IOAPIC_POL_HIGH;
811 case 2:
812 pr_warn("IOAPIC: Invalid polarity: 2, defaulting to low\n");
813 case 3:
814 default: /* Pointless default required due to do gcc stupidity */
815 return IOAPIC_POL_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Thomas Gleixnerab760852015-04-14 10:30:07 +0800819#ifdef CONFIG_EISA
820static int eisa_irq_trigger(int idx, int bus, int trigger)
821{
822 switch (mp_bus_id_to_type[bus]) {
823 case MP_BUS_PCI:
824 case MP_BUS_ISA:
825 return trigger;
826 case MP_BUS_EISA:
827 return default_EISA_trigger(idx);
828 }
829 pr_warn("IOAPIC: Invalid srcbus: %d defaulting to level\n", bus);
830 return IOAPIC_LEVEL;
831}
832#else
833static inline int eisa_irq_trigger(int idx, int bus, int trigger)
834{
835 return trigger;
836}
837#endif
838
Thomas Gleixnerb77cf6a2011-02-23 17:33:53 +0100839static int irq_trigger(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530841 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int trigger;
843
844 /*
845 * Determine IRQ trigger mode (edge or level sensitive):
846 */
Thomas Gleixnerab760852015-04-14 10:30:07 +0800847 switch ((mp_irqs[idx].irqflag >> 2) & 0x03) {
848 case 0:
849 /* conforms to spec, ie. bus-type dependent trigger mode */
850 if (test_bit(bus, mp_bus_not_pci))
851 trigger = default_ISA_trigger(idx);
852 else
853 trigger = default_PCI_trigger(idx);
854 /* Take EISA into account */
855 return eisa_irq_trigger(idx, bus, trigger);
856 case 1:
857 return IOAPIC_EDGE;
858 case 2:
859 pr_warn("IOAPIC: Invalid trigger mode 2 defaulting to level\n");
860 case 3:
861 default: /* Pointless default required due to do gcc stupidity */
862 return IOAPIC_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Jiang Liuc4d05a22015-04-13 14:11:54 +0800866void ioapic_set_alloc_attr(struct irq_alloc_info *info, int node,
867 int trigger, int polarity)
868{
869 init_irq_alloc_info(info, NULL);
870 info->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
871 info->ioapic_node = node;
872 info->ioapic_trigger = trigger;
873 info->ioapic_polarity = polarity;
874 info->ioapic_valid = 1;
875}
876
Jiang Liu96ed44b2015-04-13 14:11:58 +0800877#ifndef CONFIG_ACPI
878int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity);
879#endif
880
881static void ioapic_copy_alloc_attr(struct irq_alloc_info *dst,
882 struct irq_alloc_info *src,
883 u32 gsi, int ioapic_idx, int pin)
884{
885 int trigger, polarity;
886
887 copy_irq_alloc_info(dst, src);
888 dst->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
889 dst->ioapic_id = mpc_ioapic_id(ioapic_idx);
890 dst->ioapic_pin = pin;
891 dst->ioapic_valid = 1;
892 if (src && src->ioapic_valid) {
893 dst->ioapic_node = src->ioapic_node;
894 dst->ioapic_trigger = src->ioapic_trigger;
895 dst->ioapic_polarity = src->ioapic_polarity;
896 } else {
897 dst->ioapic_node = NUMA_NO_NODE;
898 if (acpi_get_override_irq(gsi, &trigger, &polarity) >= 0) {
899 dst->ioapic_trigger = trigger;
900 dst->ioapic_polarity = polarity;
901 } else {
902 /*
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800903 * PCI interrupts are always active low level
Jiang Liu96ed44b2015-04-13 14:11:58 +0800904 * triggered.
905 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800906 dst->ioapic_trigger = IOAPIC_LEVEL;
907 dst->ioapic_polarity = IOAPIC_POL_LOW;
Jiang Liu96ed44b2015-04-13 14:11:58 +0800908 }
909 }
910}
911
912static int ioapic_alloc_attr_node(struct irq_alloc_info *info)
913{
914 return (info && info->ioapic_valid) ? info->ioapic_node : NUMA_NO_NODE;
915}
916
Jiang Liu49c7e602015-04-13 14:11:55 +0800917static void mp_register_handler(unsigned int irq, unsigned long trigger)
918{
919 irq_flow_handler_t hdl;
920 bool fasteoi;
921
922 if (trigger) {
923 irq_set_status_flags(irq, IRQ_LEVEL);
924 fasteoi = true;
925 } else {
926 irq_clear_status_flags(irq, IRQ_LEVEL);
927 fasteoi = false;
928 }
929
930 hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
931 __irq_set_handler(irq, hdl, 0, fasteoi ? "fasteoi" : "edge");
932}
933
Jiang Liu96ed44b2015-04-13 14:11:58 +0800934static bool mp_check_pin_attr(int irq, struct irq_alloc_info *info)
935{
936 struct mp_chip_data *data = irq_get_chip_data(irq);
937
938 /*
939 * setup_IO_APIC_irqs() programs all legacy IRQs with default trigger
940 * and polarity attirbutes. So allow the first user to reprogram the
941 * pin with real trigger and polarity attributes.
942 */
943 if (irq < nr_legacy_irqs() && data->count == 1) {
944 if (info->ioapic_trigger != data->trigger)
Jiang Liu646c4b72015-07-30 15:51:32 +0800945 mp_register_handler(irq, info->ioapic_trigger);
Jiang Liu96ed44b2015-04-13 14:11:58 +0800946 data->entry.trigger = data->trigger = info->ioapic_trigger;
947 data->entry.polarity = data->polarity = info->ioapic_polarity;
948 }
949
950 return data->trigger == info->ioapic_trigger &&
951 data->polarity == info->ioapic_polarity;
952}
953
Jiang Liud32932d2015-04-13 14:11:59 +0800954static int alloc_irq_from_domain(struct irq_domain *domain, int ioapic, u32 gsi,
Jiang Liuc4d05a22015-04-13 14:11:54 +0800955 struct irq_alloc_info *info)
Jiang Liu6b9fb702014-06-10 14:13:25 +0800956{
Jiang Liud32932d2015-04-13 14:11:59 +0800957 bool legacy = false;
Jiang Liud7f3d472014-06-09 16:19:52 +0800958 int irq = -1;
Jiang Liud7f3d472014-06-09 16:19:52 +0800959 int type = ioapics[ioapic].irqdomain_cfg.type;
960
961 switch (type) {
962 case IOAPIC_DOMAIN_LEGACY:
963 /*
Jiang Liud32932d2015-04-13 14:11:59 +0800964 * Dynamically allocate IRQ number for non-ISA IRQs in the first
965 * 16 GSIs on some weird platforms.
Jiang Liud7f3d472014-06-09 16:19:52 +0800966 */
Jiang Liud32932d2015-04-13 14:11:59 +0800967 if (!ioapic_initialized || gsi >= nr_legacy_irqs())
Jiang Liud7f3d472014-06-09 16:19:52 +0800968 irq = gsi;
Jiang Liud32932d2015-04-13 14:11:59 +0800969 legacy = mp_is_legacy_irq(irq);
Jiang Liud7f3d472014-06-09 16:19:52 +0800970 break;
971 case IOAPIC_DOMAIN_STRICT:
Jiang Liud32932d2015-04-13 14:11:59 +0800972 irq = gsi;
Jiang Liud7f3d472014-06-09 16:19:52 +0800973 break;
974 case IOAPIC_DOMAIN_DYNAMIC:
Jiang Liud7f3d472014-06-09 16:19:52 +0800975 break;
976 default:
977 WARN(1, "ioapic: unknown irqdomain type %d\n", type);
Jiang Liud32932d2015-04-13 14:11:59 +0800978 return -1;
Jiang Liud7f3d472014-06-09 16:19:52 +0800979 }
980
Jiang Liud32932d2015-04-13 14:11:59 +0800981 return __irq_domain_alloc_irqs(domain, irq, 1,
982 ioapic_alloc_attr_node(info),
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900983 info, legacy, NULL);
Jiang Liud32932d2015-04-13 14:11:59 +0800984}
985
986/*
987 * Need special handling for ISA IRQs because there may be multiple IOAPIC pins
988 * sharing the same ISA IRQ number and irqdomain only supports 1:1 mapping
989 * between IOAPIC pin and IRQ number. A typical IOAPIC has 24 pins, pin 0-15 are
990 * used for legacy IRQs and pin 16-23 are used for PCI IRQs (PIRQ A-H).
991 * When ACPI is disabled, only legacy IRQ numbers (IRQ0-15) are available, and
992 * some BIOSes may use MP Interrupt Source records to override IRQ numbers for
993 * PIRQs instead of reprogramming the interrupt routing logic. Thus there may be
994 * multiple pins sharing the same legacy IRQ number when ACPI is disabled.
995 */
996static int alloc_isa_irq_from_domain(struct irq_domain *domain,
997 int irq, int ioapic, int pin,
998 struct irq_alloc_info *info)
999{
1000 struct mp_chip_data *data;
1001 struct irq_data *irq_data = irq_get_irq_data(irq);
1002 int node = ioapic_alloc_attr_node(info);
1003
1004 /*
1005 * Legacy ISA IRQ has already been allocated, just add pin to
1006 * the pin list assoicated with this IRQ and program the IOAPIC
1007 * entry. The IOAPIC entry
1008 */
1009 if (irq_data && irq_data->parent_data) {
Jiang Liud32932d2015-04-13 14:11:59 +08001010 if (!mp_check_pin_attr(irq, info))
1011 return -EBUSY;
Jiang Liu44677152015-04-14 10:29:53 +08001012 if (__add_pin_to_irq_node(irq_data->chip_data, node, ioapic,
1013 info->ioapic_pin))
Jiang Liud32932d2015-04-13 14:11:59 +08001014 return -ENOMEM;
1015 } else {
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001016 irq = __irq_domain_alloc_irqs(domain, irq, 1, node, info, true,
1017 NULL);
Jiang Liud32932d2015-04-13 14:11:59 +08001018 if (irq >= 0) {
1019 irq_data = irq_domain_get_irq_data(domain, irq);
1020 data = irq_data->chip_data;
1021 data->isa_irq = true;
1022 }
1023 }
1024
1025 return irq;
Jiang Liu6b9fb702014-06-10 14:13:25 +08001026}
1027
Jiang Liud7f3d472014-06-09 16:19:52 +08001028static int mp_map_pin_to_irq(u32 gsi, int idx, int ioapic, int pin,
Jiang Liuc4d05a22015-04-13 14:11:54 +08001029 unsigned int flags, struct irq_alloc_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Eric W. Biedermand4642072010-03-30 01:07:13 -07001031 int irq;
Jiang Liud32932d2015-04-13 14:11:59 +08001032 bool legacy = false;
1033 struct irq_alloc_info tmp;
1034 struct mp_chip_data *data;
Jiang Liud7f3d472014-06-09 16:19:52 +08001035 struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
1036
Jiang Liub81975e2014-06-09 16:20:11 +08001037 if (!domain)
Jiang Liud32932d2015-04-13 14:11:59 +08001038 return -ENOSYS;
Jiang Liu16ee7b32014-06-09 16:20:04 +08001039
Jiang Liu16ee7b32014-06-09 16:20:04 +08001040 if (idx >= 0 && test_bit(mp_irqs[idx].srcbus, mp_bus_not_pci)) {
1041 irq = mp_irqs[idx].srcbusirq;
Jiang Liud32932d2015-04-13 14:11:59 +08001042 legacy = mp_is_legacy_irq(irq);
1043 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001044
Jiang Liud32932d2015-04-13 14:11:59 +08001045 mutex_lock(&ioapic_mutex);
1046 if (!(flags & IOAPIC_MAP_ALLOC)) {
1047 if (!legacy) {
1048 irq = irq_find_mapping(domain, pin);
Jiang Liu16ee7b32014-06-09 16:20:04 +08001049 if (irq == 0)
Jiang Liud32932d2015-04-13 14:11:59 +08001050 irq = -ENOENT;
Jiang Liu16ee7b32014-06-09 16:20:04 +08001051 }
1052 } else {
Jiang Liud32932d2015-04-13 14:11:59 +08001053 ioapic_copy_alloc_attr(&tmp, info, gsi, ioapic, pin);
1054 if (legacy)
1055 irq = alloc_isa_irq_from_domain(domain, irq,
1056 ioapic, pin, &tmp);
1057 else if ((irq = irq_find_mapping(domain, pin)) == 0)
1058 irq = alloc_irq_from_domain(domain, ioapic, gsi, &tmp);
1059 else if (!mp_check_pin_attr(irq, &tmp))
1060 irq = -EBUSY;
1061 if (irq >= 0) {
1062 data = irq_get_chip_data(irq);
1063 data->count++;
1064 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001065 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001066 mutex_unlock(&ioapic_mutex);
1067
Jiang Liud32932d2015-04-13 14:11:59 +08001068 return irq;
Jiang Liud7f3d472014-06-09 16:19:52 +08001069}
1070
1071static int pin_2_irq(int idx, int ioapic, int pin, unsigned int flags)
1072{
1073 u32 gsi = mp_pin_to_gsi(ioapic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 /*
1076 * Debugging check, we are in big trouble if this message pops up!
1077 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301078 if (mp_irqs[idx].dstirq != pin)
Joe Perchesc767a542012-05-21 19:50:07 -07001079 pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Ingo Molnar54168ed2008-08-20 09:07:45 +02001081#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /*
1083 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1084 */
1085 if ((pin >= 16) && (pin <= 23)) {
1086 if (pirq_entries[pin-16] != -1) {
1087 if (!pirq_entries[pin-16]) {
1088 apic_printk(APIC_VERBOSE, KERN_DEBUG
1089 "disabling PIRQ%d\n", pin-16);
1090 } else {
Jiang Liud7f3d472014-06-09 16:19:52 +08001091 int irq = pirq_entries[pin-16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 apic_printk(APIC_VERBOSE, KERN_DEBUG
1093 "using PIRQ%d -> IRQ %d\n",
1094 pin-16, irq);
Jiang Liu6b9fb702014-06-10 14:13:25 +08001095 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 }
1098 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001099#endif
1100
Jiang Liuc4d05a22015-04-13 14:11:54 +08001101 return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, NULL);
Jiang Liud7f3d472014-06-09 16:19:52 +08001102}
Jiang Liu6b9fb702014-06-10 14:13:25 +08001103
Jiang Liu154d9e52015-04-14 10:29:56 +08001104int mp_map_gsi_to_irq(u32 gsi, unsigned int flags, struct irq_alloc_info *info)
Jiang Liud7f3d472014-06-09 16:19:52 +08001105{
1106 int ioapic, pin, idx;
1107
1108 ioapic = mp_find_ioapic(gsi);
1109 if (ioapic < 0)
Andy Shevchenko358e96d2017-01-19 21:24:22 +02001110 return -ENODEV;
Jiang Liud7f3d472014-06-09 16:19:52 +08001111
1112 pin = mp_find_ioapic_pin(ioapic, gsi);
1113 idx = find_irq_entry(ioapic, pin, mp_INT);
1114 if ((flags & IOAPIC_MAP_CHECK) && idx < 0)
Andy Shevchenko358e96d2017-01-19 21:24:22 +02001115 return -ENODEV;
Jiang Liud7f3d472014-06-09 16:19:52 +08001116
Jiang Liuc4d05a22015-04-13 14:11:54 +08001117 return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Jiang Liudf334be2014-06-09 16:20:06 +08001120void mp_unmap_irq(int irq)
1121{
Jiang Liud32932d2015-04-13 14:11:59 +08001122 struct irq_data *irq_data = irq_get_irq_data(irq);
1123 struct mp_chip_data *data;
Jiang Liudf334be2014-06-09 16:20:06 +08001124
Jiang Liud32932d2015-04-13 14:11:59 +08001125 if (!irq_data || !irq_data->domain)
Jiang Liudf334be2014-06-09 16:20:06 +08001126 return;
1127
Jiang Liud32932d2015-04-13 14:11:59 +08001128 data = irq_data->chip_data;
1129 if (!data || data->isa_irq)
1130 return;
Jiang Liudf334be2014-06-09 16:20:06 +08001131
1132 mutex_lock(&ioapic_mutex);
Jiang Liud32932d2015-04-13 14:11:59 +08001133 if (--data->count == 0)
1134 irq_domain_free_irqs(irq, 1);
Jiang Liudf334be2014-06-09 16:20:06 +08001135 mutex_unlock(&ioapic_mutex);
1136}
1137
Yinghai Lue20c06f2009-05-06 10:08:22 -07001138/*
1139 * Find a specific PCI IRQ entry.
1140 * Not an __init, possibly needed by modules
1141 */
Jiang Liu25d0d352014-10-27 16:11:54 +08001142int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
Yinghai Lue20c06f2009-05-06 10:08:22 -07001143{
Jiang Liud7f3d472014-06-09 16:19:52 +08001144 int irq, i, best_ioapic = -1, best_idx = -1;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001145
1146 apic_printk(APIC_DEBUG,
1147 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1148 bus, slot, pin);
1149 if (test_bit(bus, mp_bus_not_pci)) {
1150 apic_printk(APIC_VERBOSE,
1151 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1152 return -1;
1153 }
Jiang Liu79598502014-06-09 16:19:44 +08001154
Yinghai Lue20c06f2009-05-06 10:08:22 -07001155 for (i = 0; i < mp_irq_entries; i++) {
1156 int lbus = mp_irqs[i].srcbus;
Jiang Liu79598502014-06-09 16:19:44 +08001157 int ioapic_idx, found = 0;
1158
1159 if (bus != lbus || mp_irqs[i].irqtype != mp_INT ||
1160 slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f))
1161 continue;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001162
Jiang Liuf44d1692014-06-09 16:19:42 +08001163 for_each_ioapic(ioapic_idx)
Yinghai Lu6f50d452011-10-12 00:33:48 -07001164 if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
Jiang Liu79598502014-06-09 16:19:44 +08001165 mp_irqs[i].dstapic == MP_APIC_ALL) {
1166 found = 1;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001167 break;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001168 }
Jiang Liu79598502014-06-09 16:19:44 +08001169 if (!found)
1170 continue;
1171
1172 /* Skip ISA IRQs */
Jiang Liud7f3d472014-06-09 16:19:52 +08001173 irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq, 0);
1174 if (irq > 0 && !IO_APIC_IRQ(irq))
Jiang Liu79598502014-06-09 16:19:44 +08001175 continue;
1176
1177 if (pin == (mp_irqs[i].srcbusirq & 3)) {
Jiang Liud7f3d472014-06-09 16:19:52 +08001178 best_idx = i;
1179 best_ioapic = ioapic_idx;
1180 goto out;
Jiang Liu79598502014-06-09 16:19:44 +08001181 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001182
Jiang Liu79598502014-06-09 16:19:44 +08001183 /*
1184 * Use the first all-but-pin matching entry as a
1185 * best-guess fuzzy result for broken mptables.
1186 */
Jiang Liud7f3d472014-06-09 16:19:52 +08001187 if (best_idx < 0) {
1188 best_idx = i;
1189 best_ioapic = ioapic_idx;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001190 }
1191 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001192 if (best_idx < 0)
1193 return -1;
1194
1195out:
Jiang Liu25d0d352014-10-27 16:11:54 +08001196 return pin_2_irq(best_idx, best_ioapic, mp_irqs[best_idx].dstirq,
1197 IOAPIC_MAP_ALLOC);
Yinghai Lue20c06f2009-05-06 10:08:22 -07001198}
1199EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1200
Jiang Liud32932d2015-04-13 14:11:59 +08001201static struct irq_chip ioapic_chip, ioapic_ir_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001203#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001204static inline int IO_APIC_irq_trigger(int irq)
1205{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001206 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001207
Jiang Liuf44d1692014-06-09 16:19:42 +08001208 for_each_ioapic_pin(apic, pin) {
1209 idx = find_irq_entry(apic, pin, mp_INT);
Jiang Liud7f3d472014-06-09 16:19:52 +08001210 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin, 0)))
Jiang Liuf44d1692014-06-09 16:19:42 +08001211 return irq_trigger(idx);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001212 }
1213 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001214 * nonexistent IRQs are edge default
1215 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001216 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001217}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001218#else
1219static inline int IO_APIC_irq_trigger(int irq)
1220{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001221 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001222}
1223#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001224
Thomas Gleixnered972cc2011-02-23 14:31:36 +01001225static void __init setup_IO_APIC_irqs(void)
1226{
Jiang Liu16ee7b32014-06-09 16:20:04 +08001227 unsigned int ioapic, pin;
1228 int idx;
Thomas Gleixnered972cc2011-02-23 14:31:36 +01001229
1230 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1231
Jiang Liu16ee7b32014-06-09 16:20:04 +08001232 for_each_ioapic_pin(ioapic, pin) {
1233 idx = find_irq_entry(ioapic, pin, mp_INT);
1234 if (idx < 0)
1235 apic_printk(APIC_VERBOSE,
1236 KERN_DEBUG " apic %d pin %d not connected\n",
1237 mpc_ioapic_id(ioapic), pin);
1238 else
1239 pin_2_irq(idx, ioapic, pin,
1240 ioapic ? 0 : IOAPIC_MAP_ALLOC);
1241 }
Thomas Gleixnered972cc2011-02-23 14:31:36 +01001242}
1243
Yoshihiro YUNOMAE17405452013-08-20 16:01:07 +09001244void ioapic_zap_locks(void)
1245{
1246 raw_spin_lock_init(&ioapic_lock);
1247}
1248
Jiang Liua44174e2015-04-13 14:11:57 +08001249static void io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
1250{
1251 int i;
1252 char buf[256];
1253 struct IO_APIC_route_entry entry;
1254 struct IR_IO_APIC_route_entry *ir_entry = (void *)&entry;
1255
1256 printk(KERN_DEBUG "IOAPIC %d:\n", apic);
1257 for (i = 0; i <= nr_entries; i++) {
1258 entry = ioapic_read_entry(apic, i);
1259 snprintf(buf, sizeof(buf),
1260 " pin%02x, %s, %s, %s, V(%02X), IRR(%1d), S(%1d)",
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001261 i,
1262 entry.mask == IOAPIC_MASKED ? "disabled" : "enabled ",
1263 entry.trigger == IOAPIC_LEVEL ? "level" : "edge ",
1264 entry.polarity == IOAPIC_POL_LOW ? "low " : "high",
Jiang Liua44174e2015-04-13 14:11:57 +08001265 entry.vector, entry.irr, entry.delivery_status);
1266 if (ir_entry->format)
1267 printk(KERN_DEBUG "%s, remapped, I(%04X), Z(%X)\n",
1268 buf, (ir_entry->index << 15) | ir_entry->index,
1269 ir_entry->zero);
1270 else
1271 printk(KERN_DEBUG "%s, %s, D(%02X), M(%1d)\n",
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001272 buf,
1273 entry.dest_mode == IOAPIC_DEST_MODE_LOGICAL ?
1274 "logical " : "physical",
Jiang Liua44174e2015-04-13 14:11:57 +08001275 entry.dest, entry.delivery_mode);
1276 }
1277}
1278
Jiang Liu74afab72014-10-27 16:12:00 +08001279static void __init print_IO_APIC(int ioapic_idx)
Joerg Roedelafcc8a42012-09-26 12:44:36 +02001280{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 union IO_APIC_reg_00 reg_00;
1282 union IO_APIC_reg_01 reg_01;
1283 union IO_APIC_reg_02 reg_02;
1284 union IO_APIC_reg_03 reg_03;
1285 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001287 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001288 reg_00.raw = io_apic_read(ioapic_idx, 0);
1289 reg_01.raw = io_apic_read(ioapic_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (reg_01.bits.version >= 0x10)
Yinghai Lu6f50d452011-10-12 00:33:48 -07001291 reg_02.raw = io_apic_read(ioapic_idx, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001292 if (reg_01.bits.version >= 0x20)
Yinghai Lu6f50d452011-10-12 00:33:48 -07001293 reg_03.raw = io_apic_read(ioapic_idx, 3);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001294 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Yinghai Lu6f50d452011-10-12 00:33:48 -07001296 printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1298 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1299 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1300 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Ingo Molnar54168ed2008-08-20 09:07:45 +02001302 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Naga Chumbalkarbd6a46e2011-07-08 18:46:36 +00001303 printk(KERN_DEBUG "....... : max redirection entries: %02X\n",
1304 reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
Naga Chumbalkarbd6a46e2011-07-08 18:46:36 +00001307 printk(KERN_DEBUG "....... : IO APIC version: %02X\n",
1308 reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /*
1311 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1312 * but the value of reg_02 is read as the previous read register
1313 * value, so ignore it if reg_02 == reg_01.
1314 */
1315 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1316 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1317 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
1320 /*
1321 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1322 * or reg_03, but the value of reg_0[23] is read as the previous read
1323 * register value, so ignore it if reg_03 == reg_0[12].
1324 */
1325 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1326 reg_03.raw != reg_01.raw) {
1327 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1328 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330
1331 printk(KERN_DEBUG ".... IRQ redirection table:\n");
Jiang Liua44174e2015-04-13 14:11:57 +08001332 io_apic_print_entries(ioapic_idx, reg_01.bits.entries);
Yinghai Lucda417d2011-10-12 00:33:39 -07001333}
1334
Jiang Liu74afab72014-10-27 16:12:00 +08001335void __init print_IO_APICs(void)
Yinghai Lucda417d2011-10-12 00:33:39 -07001336{
Yinghai Lu6f50d452011-10-12 00:33:48 -07001337 int ioapic_idx;
Yinghai Lucda417d2011-10-12 00:33:39 -07001338 unsigned int irq;
1339
1340 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Jiang Liuf44d1692014-06-09 16:19:42 +08001341 for_each_ioapic(ioapic_idx)
Yinghai Lucda417d2011-10-12 00:33:39 -07001342 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001343 mpc_ioapic_id(ioapic_idx),
1344 ioapics[ioapic_idx].nr_registers);
Yinghai Lucda417d2011-10-12 00:33:39 -07001345
1346 /*
1347 * We are a bit conservative about what we expect. We have to
1348 * know about every hardware change ASAP.
1349 */
1350 printk(KERN_INFO "testing the IO APIC.......................\n");
1351
Jiang Liuf44d1692014-06-09 16:19:42 +08001352 for_each_ioapic(ioapic_idx)
Yinghai Lu6f50d452011-10-12 00:33:48 -07001353 print_IO_APIC(ioapic_idx);
Naga Chumbalkar42f0efc2011-07-12 21:17:35 +00001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Thomas Gleixnerad9f4332010-09-30 11:26:43 +02001356 for_each_active_irq(irq) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001357 struct irq_pin_list *entry;
Jiang Liu44677152015-04-14 10:29:53 +08001358 struct irq_chip *chip;
1359 struct mp_chip_data *data;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001360
Mathias Nyman6fd36ba2011-11-10 13:45:24 +00001361 chip = irq_get_chip(irq);
Jiang Liud32932d2015-04-13 14:11:59 +08001362 if (chip != &ioapic_chip && chip != &ioapic_ir_chip)
Mathias Nyman6fd36ba2011-11-10 13:45:24 +00001363 continue;
Jiang Liu44677152015-04-14 10:29:53 +08001364 data = irq_get_chip_data(irq);
1365 if (!data)
1366 continue;
1367 if (list_empty(&data->irq_2_pin))
1368 continue;
Mathias Nyman6fd36ba2011-11-10 13:45:24 +00001369
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001370 printk(KERN_DEBUG "IRQ%d ", irq);
Jiang Liu44677152015-04-14 10:29:53 +08001371 for_each_irq_pin(entry, data->irq_2_pin)
Joe Perchesc767a542012-05-21 19:50:07 -07001372 pr_cont("-> %d:%d", entry->apic, entry->pin);
1373 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375
1376 printk(KERN_INFO ".................................... done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Yinghai Luefa25592008-08-19 20:50:36 -07001379/* Where if anywhere is the i8259 connect in external int mode */
1380static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1381
Ingo Molnar54168ed2008-08-20 09:07:45 +02001382void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001384 int i8259_apic, i8259_pin;
Jiang Liuf44d1692014-06-09 16:19:42 +08001385 int apic, pin;
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001386
Thomas Gleixnera46f5c82015-01-15 21:22:32 +00001387 if (skip_ioapic_setup)
1388 nr_ioapics = 0;
1389
1390 if (!nr_legacy_irqs() || !nr_ioapics)
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001391 return;
1392
Jiang Liuf44d1692014-06-09 16:19:42 +08001393 for_each_ioapic_pin(apic, pin) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001394 /* See if any of the pins is in ExtINT mode */
Jiang Liuf44d1692014-06-09 16:19:42 +08001395 struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001396
Jiang Liuf44d1692014-06-09 16:19:42 +08001397 /* If the interrupt line is enabled and in ExtInt mode
1398 * I have found the pin where the i8259 is connected.
1399 */
1400 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1401 ioapic_i8259.apic = apic;
1402 ioapic_i8259.pin = pin;
1403 goto found_i8259;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001404 }
1405 }
1406 found_i8259:
1407 /* Look to see what if the MP table has reported the ExtINT */
1408 /* If we could not find the appropriate pin by looking at the ioapic
1409 * the i8259 probably is not connected the ioapic but give the
1410 * mptable a chance anyway.
1411 */
1412 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1413 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1414 /* Trust the MP table if nothing is setup in the hardware */
1415 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1416 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1417 ioapic_i8259.pin = i8259_pin;
1418 ioapic_i8259.apic = i8259_apic;
1419 }
1420 /* Complain if the MP table and the hardware disagree */
1421 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1422 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1423 {
1424 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426
1427 /*
1428 * Do not trust the IO-APIC being empty at bootup
1429 */
1430 clear_IO_APIC();
1431}
1432
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001433void native_disable_io_apic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001436 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001437 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001438 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001439 */
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001440 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001441 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001442
1443 memset(&entry, 0, sizeof(entry));
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001444 entry.mask = IOAPIC_UNMASKED;
1445 entry.trigger = IOAPIC_EDGE;
1446 entry.polarity = IOAPIC_POL_HIGH;
1447 entry.dest_mode = IOAPIC_DEST_MODE_PHYSICAL;
1448 entry.delivery_mode = dest_ExtINT;
1449 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001450
1451 /*
1452 * Add it to the IO-APIC irq-routing table:
1453 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001454 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001455 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001456
Borislav Petkov93984fb2016-04-04 22:25:00 +02001457 if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config())
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001458 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001459}
1460
1461/*
1462 * Not an __init, needed by the reboot code
1463 */
1464void disable_IO_APIC(void)
1465{
1466 /*
1467 * Clear the IO-APIC before rebooting:
1468 */
1469 clear_IO_APIC();
1470
Jiang Liu95d76ac2014-06-09 16:19:48 +08001471 if (!nr_legacy_irqs())
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001472 return;
1473
1474 x86_io_apic_ops.disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Ingo Molnar54168ed2008-08-20 09:07:45 +02001477#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478/*
1479 * function to set the IO-APIC physical IDs based on the
1480 * values stored in the MPC table.
1481 *
1482 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1483 */
Sebastian Andrzej Siewiora38c5382010-11-26 17:50:20 +01001484void __init setup_ioapic_ids_from_mpc_nocheck(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 union IO_APIC_reg_00 reg_00;
1487 physid_mask_t phys_id_present_map;
Yinghai Lu6f50d452011-10-12 00:33:48 -07001488 int ioapic_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 int i;
1490 unsigned char old_id;
1491 unsigned long flags;
1492
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001493 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * This is broken; anything with a real cpu count has to
1495 * circumvent this idiocy regardless.
1496 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03001497 apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /*
1500 * Set the IOAPIC ID to the value stored in the MPC table.
1501 */
Jiang Liuf44d1692014-06-09 16:19:42 +08001502 for_each_ioapic(ioapic_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /* Read the register 0 value */
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001504 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001505 reg_00.raw = io_apic_read(ioapic_idx, 0);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001506 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001507
Yinghai Lu6f50d452011-10-12 00:33:48 -07001508 old_id = mpc_ioapic_id(ioapic_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Yinghai Lu6f50d452011-10-12 00:33:48 -07001510 if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001512 ioapic_idx, mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1514 reg_00.bits.ID);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001515 ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 /*
1519 * Sanity check, is the ID really free? Every APIC in a
1520 * system must have a unique ID or we get lots of nice
1521 * 'stuck on smp_invalidate_needed IPI wait' messages.
1522 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03001523 if (apic->check_apicid_used(&phys_id_present_map,
Yinghai Lu6f50d452011-10-12 00:33:48 -07001524 mpc_ioapic_id(ioapic_idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001526 ioapic_idx, mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 for (i = 0; i < get_physical_broadcast(); i++)
1528 if (!physid_isset(i, phys_id_present_map))
1529 break;
1530 if (i >= get_physical_broadcast())
1531 panic("Max APIC ID exceeded!\n");
1532 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1533 i);
1534 physid_set(i, phys_id_present_map);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001535 ioapics[ioapic_idx].mp_config.apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 } else {
1537 physid_mask_t tmp;
Yinghai Lu6f50d452011-10-12 00:33:48 -07001538 apic->apicid_to_cpu_present(mpc_ioapic_id(ioapic_idx),
Suresh Siddhad5371432011-05-18 16:31:37 -07001539 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 apic_printk(APIC_VERBOSE, "Setting %d in the "
1541 "phys_id_present_map\n",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001542 mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1544 }
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /*
1547 * We need to adjust the IRQ routing table
1548 * if the ID changed.
1549 */
Yinghai Lu6f50d452011-10-12 00:33:48 -07001550 if (old_id != mpc_ioapic_id(ioapic_idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301552 if (mp_irqs[i].dstapic == old_id)
1553 mp_irqs[i].dstapic
Yinghai Lu6f50d452011-10-12 00:33:48 -07001554 = mpc_ioapic_id(ioapic_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 /*
Yinghai Lu60d79fd2010-12-07 00:59:49 -08001557 * Update the ID register according to the right value
1558 * from the MPC table if they are different.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001559 */
Yinghai Lu6f50d452011-10-12 00:33:48 -07001560 if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
Yinghai Lu60d79fd2010-12-07 00:59:49 -08001561 continue;
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 apic_printk(APIC_VERBOSE, KERN_INFO
1564 "...changing IO-APIC physical APIC ID to %d ...",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001565 mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Yinghai Lu6f50d452011-10-12 00:33:48 -07001567 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001568 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001569 io_apic_write(ioapic_idx, 0, reg_00.raw);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001570 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /*
1573 * Sanity check
1574 */
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001575 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001576 reg_00.raw = io_apic_read(ioapic_idx, 0);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001577 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001578 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
Joe Perchesc767a542012-05-21 19:50:07 -07001579 pr_cont("could not set ID!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 else
1581 apic_printk(APIC_VERBOSE, " ok.\n");
1582 }
1583}
Sebastian Andrzej Siewiora38c5382010-11-26 17:50:20 +01001584
1585void __init setup_ioapic_ids_from_mpc(void)
1586{
1587
1588 if (acpi_ioapic)
1589 return;
1590 /*
1591 * Don't check I/O APIC IDs for xAPIC systems. They have
1592 * no meaning without the serial APIC bus.
1593 */
1594 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
Denys Vlasenkocff9ab22016-09-13 20:12:32 +02001595 || APIC_XAPIC(boot_cpu_apic_version))
Sebastian Andrzej Siewiora38c5382010-11-26 17:50:20 +01001596 return;
1597 setup_ioapic_ids_from_mpc_nocheck();
1598}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001599#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001601int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001602
1603static int __init notimercheck(char *s)
1604{
1605 no_timer_check = 1;
1606 return 1;
1607}
1608__setup("no_timer_check", notimercheck);
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610/*
1611 * There is a nasty bug in some older SMP boards, their mptable lies
1612 * about the timer IRQ. We do the following to work around the situation:
1613 *
1614 * - timer IRQ defaults to IO-APIC IRQ
1615 * - if this function detects that timer IRQs are defunct, then we fall
1616 * back to ISA timer IRQs
1617 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001618static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001621 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Zachary Amsden8542b202006-12-07 02:14:09 +01001623 if (no_timer_check)
1624 return 1;
1625
Ingo Molnar4aae0702007-12-18 18:05:58 +01001626 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 local_irq_enable();
1628 /* Let ten ticks pass... */
1629 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001630 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
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 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001639
1640 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01001641 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return 0;
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 */
Thomas Gleixner61a38ce2010-09-28 16:00:34 +02001668static unsigned int startup_ioapic_irq(struct irq_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Thomas Gleixner61a38ce2010-09-28 16:00:34 +02001670 int was_pending = 0, irq = data->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 unsigned long flags;
1672
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001673 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu95d76ac2014-06-09 16:19:48 +08001674 if (irq < nr_legacy_irqs()) {
Thomas Gleixner4305df92010-09-28 15:01:33 +02001675 legacy_pic->mask(irq);
Jacob Panb81bb372009-11-09 11:27:04 -08001676 if (legacy_pic->irq_pending(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 was_pending = 1;
1678 }
Jiang Liu44677152015-04-14 10:29:53 +08001679 __unmask_ioapic(data->chip_data);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001680 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 return was_pending;
1683}
1684
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001685atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001686
Alexander Gordeev4da70722012-03-20 15:19:36 +01001687#ifdef CONFIG_GENERIC_PENDING_IRQ
Jiang Liu44677152015-04-14 10:29:53 +08001688static bool io_apic_level_ack_pending(struct mp_chip_data *data)
Márton Némethd1ecad62012-05-08 00:24:20 -07001689{
1690 struct irq_pin_list *entry;
1691 unsigned long flags;
1692
1693 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +08001694 for_each_irq_pin(entry, data->irq_2_pin) {
Márton Némethd1ecad62012-05-08 00:24:20 -07001695 unsigned int reg;
1696 int pin;
1697
1698 pin = entry->pin;
1699 reg = io_apic_read(entry->apic, 0x10 + pin*2);
1700 /* Is the remote IRR bit set? */
1701 if (reg & IO_APIC_REDIR_REMOTE_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
Jiang Liu44677152015-04-14 10:29:53 +08001711static inline bool ioapic_irqd_mask(struct irq_data *data)
Alexander Gordeev4da70722012-03-20 15:19:36 +01001712{
1713 /* If we are moving the irq we need to mask it */
1714 if (unlikely(irqd_is_setaffinity_pending(data))) {
Jiang Liu44677152015-04-14 10:29:53 +08001715 mask_ioapic_irq(data);
Alexander Gordeev4da70722012-03-20 15:19:36 +01001716 return true;
1717 }
1718 return false;
1719}
1720
Jiang Liu44677152015-04-14 10:29:53 +08001721static inline void ioapic_irqd_unmask(struct irq_data *data, bool masked)
Alexander Gordeev4da70722012-03-20 15:19:36 +01001722{
1723 if (unlikely(masked)) {
1724 /* Only migrate the irq if the ack has been received.
1725 *
1726 * On rare occasions the broadcast level triggered ack gets
1727 * delayed going to ioapics, and if we reprogram the
1728 * vector while Remote IRR is still set the irq will never
1729 * fire again.
1730 *
1731 * To prevent this scenario we read the Remote IRR bit
1732 * of the ioapic. This has two effects.
1733 * - On any sane system the read of the ioapic will
1734 * flush writes (and acks) going to the ioapic from
1735 * this cpu.
1736 * - We get to see if the ACK has actually been delivered.
1737 *
1738 * Based on failed experiments of reprogramming the
1739 * ioapic entry from outside of irq context starting
1740 * with masking the ioapic entry and then polling until
1741 * Remote IRR was clear before reprogramming the
1742 * ioapic I don't trust the Remote IRR bit to be
1743 * completey accurate.
1744 *
1745 * However there appears to be no other way to plug
1746 * this race, so if the Remote IRR bit is not
1747 * accurate and is causing problems then it is a hardware bug
1748 * and you can go talk to the chipset vendor about it.
1749 */
Jiang Liu44677152015-04-14 10:29:53 +08001750 if (!io_apic_level_ack_pending(data->chip_data))
Alexander Gordeev4da70722012-03-20 15:19:36 +01001751 irq_move_masked_irq(data);
Jiang Liu44677152015-04-14 10:29:53 +08001752 unmask_ioapic_irq(data);
Alexander Gordeev4da70722012-03-20 15:19:36 +01001753 }
1754}
1755#else
Jiang Liu44677152015-04-14 10:29:53 +08001756static inline bool ioapic_irqd_mask(struct irq_data *data)
Alexander Gordeev4da70722012-03-20 15:19:36 +01001757{
1758 return false;
1759}
Jiang Liu44677152015-04-14 10:29:53 +08001760static inline void ioapic_irqd_unmask(struct irq_data *data, bool masked)
Alexander Gordeev4da70722012-03-20 15:19:36 +01001761{
1762}
1763#endif
1764
Jiang Liu44677152015-04-14 10:29:53 +08001765static void ioapic_ack_level(struct irq_data *irq_data)
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001766{
Jiang Liu44677152015-04-14 10:29:53 +08001767 struct irq_cfg *cfg = irqd_cfg(irq_data);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001768 unsigned long v;
Alexander Gordeev4da70722012-03-20 15:19:36 +01001769 bool masked;
Jiang Liud32932d2015-04-13 14:11:59 +08001770 int i;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001771
Thomas Gleixnerdd5f15e2010-09-28 15:18:35 +02001772 irq_complete_move(cfg);
Jiang Liu44677152015-04-14 10:29:53 +08001773 masked = ioapic_irqd_mask(irq_data);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001774
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001775 /*
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07001776 * It appears there is an erratum which affects at least version 0x11
1777 * of I/O APIC (that's the 82093AA and cores integrated into various
1778 * chipsets). Under certain conditions a level-triggered interrupt is
1779 * erroneously delivered as edge-triggered one but the respective IRR
1780 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1781 * message but it will never arrive and further interrupts are blocked
1782 * from the source. The exact reason is so far unknown, but the
1783 * phenomenon was observed when two consecutive interrupt requests
1784 * from a given source get delivered to the same CPU and the source is
1785 * temporarily disabled in between.
1786 *
1787 * A workaround is to simulate an EOI message manually. We achieve it
1788 * by setting the trigger mode to edge and then to level when the edge
1789 * trigger mode gets detected in the TMR of a local APIC for a
1790 * level-triggered interrupt. We mask the source for the time of the
1791 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1792 * The idea is from Manfred Spraul. --macro
Suresh Siddha1c839952009-12-01 15:31:17 -08001793 *
1794 * Also in the case when cpu goes offline, fixup_irqs() will forward
1795 * any unhandled interrupt on the offlined cpu to the new cpu
1796 * destination that is handling the corresponding interrupt. This
1797 * interrupt forwarding is done via IPI's. Hence, in this case also
1798 * level-triggered io-apic interrupt will be seen as an edge
1799 * interrupt in the IRR. And we can't rely on the cpu's EOI
1800 * to be broadcasted to the IO-APIC's which will clear the remoteIRR
1801 * corresponding to the level-triggered interrupt. Hence on IO-APIC's
1802 * supporting EOI register, we do an explicit EOI to clear the
1803 * remote IRR and on IO-APIC's which don't have an EOI register,
1804 * we use the above logic (mask+edge followed by unmask+level) from
1805 * Manfred Spraul to clear the remote IRR.
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07001806 */
Yinghai Lu3145e942008-12-05 18:58:34 -08001807 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001808 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001809
Ingo Molnar54168ed2008-08-20 09:07:45 +02001810 /*
1811 * We must acknowledge the irq before we move it or the acknowledge will
1812 * not propagate properly.
1813 */
1814 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001815
Suresh Siddha1c839952009-12-01 15:31:17 -08001816 /*
1817 * Tail end of clearing remote IRR bit (either by delivering the EOI
1818 * message via io-apic EOI register write or simulating it using
1819 * mask+edge followed by unnask+level logic) manually when the
1820 * level triggered interrupt is seen as the edge triggered interrupt
1821 * at the cpu.
1822 */
Maciej W. Rozyckica64c472009-12-01 15:31:15 -08001823 if (!(v & (1 << (i & 0x1f)))) {
1824 atomic_inc(&irq_mis_count);
Jiang Liu44677152015-04-14 10:29:53 +08001825 eoi_ioapic_pin(cfg->vector, irq_data->chip_data);
Maciej W. Rozyckica64c472009-12-01 15:31:15 -08001826 }
1827
Jiang Liu44677152015-04-14 10:29:53 +08001828 ioapic_irqd_unmask(irq_data, masked);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001829}
Yinghai Lu1d025192008-08-19 20:50:34 -07001830
Jiang Liud32932d2015-04-13 14:11:59 +08001831static void ioapic_ir_ack_level(struct irq_data *irq_data)
1832{
1833 struct mp_chip_data *data = irq_data->chip_data;
1834
1835 /*
1836 * Intr-remapping uses pin number as the virtual vector
1837 * in the RTE. Actual vector is programmed in
1838 * intr-remapping table entry. Hence for the io-apic
1839 * EOI we use the pin number.
1840 */
1841 ack_APIC_irq();
Jiang Liu44677152015-04-14 10:29:53 +08001842 eoi_ioapic_pin(data->entry.vector, data);
Jiang Liud32932d2015-04-13 14:11:59 +08001843}
1844
1845static int ioapic_set_affinity(struct irq_data *irq_data,
1846 const struct cpumask *mask, bool force)
1847{
1848 struct irq_data *parent = irq_data->parent_data;
1849 struct mp_chip_data *data = irq_data->chip_data;
Jiang Liu0be275e2015-04-14 10:29:57 +08001850 struct irq_pin_list *entry;
Jiang Liud32932d2015-04-13 14:11:59 +08001851 struct irq_cfg *cfg;
1852 unsigned long flags;
1853 int ret;
1854
1855 ret = parent->chip->irq_set_affinity(parent, mask, force);
1856 raw_spin_lock_irqsave(&ioapic_lock, flags);
1857 if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
1858 cfg = irqd_cfg(irq_data);
1859 data->entry.dest = cfg->dest_apicid;
1860 data->entry.vector = cfg->vector;
Jiang Liu0be275e2015-04-14 10:29:57 +08001861 for_each_irq_pin(entry, data->irq_2_pin)
1862 __ioapic_write_entry(entry->apic, entry->pin,
1863 data->entry);
Jiang Liud32932d2015-04-13 14:11:59 +08001864 }
1865 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1866
1867 return ret;
1868}
1869
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001870static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerf7e909e2010-10-08 21:40:23 +02001871 .name = "IO-APIC",
1872 .irq_startup = startup_ioapic_irq,
1873 .irq_mask = mask_ioapic_irq,
1874 .irq_unmask = unmask_ioapic_irq,
Jiang Liud32932d2015-04-13 14:11:59 +08001875 .irq_ack = irq_chip_ack_parent,
1876 .irq_eoi = ioapic_ack_level,
1877 .irq_set_affinity = ioapic_set_affinity,
Ruslan Ruslichenkoa9b4f082017-01-17 16:13:52 +02001878 .irq_retrigger = irq_chip_retrigger_hierarchy,
Jiang Liud32932d2015-04-13 14:11:59 +08001879 .flags = IRQCHIP_SKIP_SET_WAKE,
1880};
1881
1882static struct irq_chip ioapic_ir_chip __read_mostly = {
1883 .name = "IR-IO-APIC",
1884 .irq_startup = startup_ioapic_irq,
1885 .irq_mask = mask_ioapic_irq,
1886 .irq_unmask = unmask_ioapic_irq,
1887 .irq_ack = irq_chip_ack_parent,
1888 .irq_eoi = ioapic_ir_ack_level,
1889 .irq_set_affinity = ioapic_set_affinity,
Ruslan Ruslichenkoa9b4f082017-01-17 16:13:52 +02001890 .irq_retrigger = irq_chip_retrigger_hierarchy,
Rafael J. Wysocki56135702014-09-01 13:49:07 +02001891 .flags = IRQCHIP_SKIP_SET_WAKE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892};
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894static inline void init_IO_APIC_traps(void)
1895{
Yinghai Luda51a822008-08-19 20:50:25 -07001896 struct irq_cfg *cfg;
Thomas Gleixnerad9f4332010-09-30 11:26:43 +02001897 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Thomas Gleixnerad9f4332010-09-30 11:26:43 +02001899 for_each_active_irq(irq) {
Jiang Liu32f5ef52014-06-09 16:19:43 +08001900 cfg = irq_cfg(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001901 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
1903 * Hmm.. We don't have an entry for this,
1904 * so default to an old-fashioned 8259
1905 * interrupt if we can..
1906 */
Jiang Liu95d76ac2014-06-09 16:19:48 +08001907 if (irq < nr_legacy_irqs())
Jacob Panb81bb372009-11-09 11:27:04 -08001908 legacy_pic->make_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001909 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /* Strange. Oh, well.. */
Thomas Gleixner2c778652011-03-12 12:20:43 +01001911 irq_set_chip(irq, &no_irq_chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 }
1914}
1915
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001916/*
1917 * The local APIC irq-chip implementation:
1918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Thomas Gleixner90297c52010-09-28 16:03:54 +02001920static void mask_lapic_irq(struct irq_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 unsigned long v;
1923
1924 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001925 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Thomas Gleixner90297c52010-09-28 16:03:54 +02001928static void unmask_lapic_irq(struct irq_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001930 unsigned long v;
1931
1932 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001933 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Thomas Gleixner90297c52010-09-28 16:03:54 +02001936static void ack_lapic_irq(struct irq_data *data)
Yinghai Lu1d025192008-08-19 20:50:34 -07001937{
1938 ack_APIC_irq();
1939}
1940
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001941static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01001942 .name = "local-APIC",
Thomas Gleixner90297c52010-09-28 16:03:54 +02001943 .irq_mask = mask_lapic_irq,
1944 .irq_unmask = unmask_lapic_irq,
1945 .irq_ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946};
1947
Thomas Gleixner60c69942010-09-28 17:28:38 +02001948static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001949{
Thomas Gleixner60c69942010-09-28 17:28:38 +02001950 irq_clear_status_flags(irq, IRQ_LEVEL);
Thomas Gleixner2c778652011-03-12 12:20:43 +01001951 irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001952 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955/*
1956 * This looks a bit hackish but it's about the only one way of sending
1957 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1958 * not support the ExtINT mode, unfortunately. We need to send these
1959 * cycles as some i82489DX-based boards have glue logic that keeps the
1960 * 8259A interrupt line asserted until INTA. --macro
1961 */
Jacek Luczak28acf282008-04-12 17:41:12 +02001962static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001964 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct IO_APIC_route_entry entry0, entry1;
1966 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001968 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01001969 if (pin == -1) {
1970 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01001972 }
1973 apic = find_isa_irq_apic(8, mp_INT);
1974 if (apic == -1) {
1975 WARN_ON_ONCE(1);
1976 return;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Andi Kleencf4c6a22006-09-26 10:52:30 +02001979 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001980 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 memset(&entry1, 0, sizeof(entry1));
1983
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001984 entry1.dest_mode = IOAPIC_DEST_MODE_PHYSICAL;
1985 entry1.mask = IOAPIC_UNMASKED;
Yinghai Lud83e94a2008-08-19 20:50:33 -07001986 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 entry1.delivery_mode = dest_ExtINT;
1988 entry1.polarity = entry0.polarity;
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001989 entry1.trigger = IOAPIC_EDGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 entry1.vector = 0;
1991
Andi Kleencf4c6a22006-09-26 10:52:30 +02001992 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 save_control = CMOS_READ(RTC_CONTROL);
1995 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1996 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1997 RTC_FREQ_SELECT);
1998 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1999
2000 i = 100;
2001 while (i-- > 0) {
2002 mdelay(10);
2003 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2004 i -= 10;
2005 }
2006
2007 CMOS_WRITE(save_control, RTC_CONTROL);
2008 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002009 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Andi Kleencf4c6a22006-09-26 10:52:30 +02002011 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Yinghai Luefa25592008-08-19 20:50:36 -07002014static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002015/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002016static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002017{
2018 disable_timer_pin_1 = 1;
2019 return 0;
2020}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002021early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002022
Jiang Liud32932d2015-04-13 14:11:59 +08002023static int mp_alloc_timer_irq(int ioapic, int pin)
2024{
2025 int irq = -1;
Jiang Liud32932d2015-04-13 14:11:59 +08002026 struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
2027
2028 if (domain) {
Jiang Liu44677152015-04-14 10:29:53 +08002029 struct irq_alloc_info info;
2030
Jiang Liud32932d2015-04-13 14:11:59 +08002031 ioapic_set_alloc_attr(&info, NUMA_NO_NODE, 0, 0);
2032 info.ioapic_id = mpc_ioapic_id(ioapic);
2033 info.ioapic_pin = pin;
2034 mutex_lock(&ioapic_mutex);
2035 irq = alloc_isa_irq_from_domain(domain, 0, ioapic, pin, &info);
2036 mutex_unlock(&ioapic_mutex);
2037 }
2038
2039 return irq;
2040}
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042/*
2043 * This code may look a bit paranoid, but it's supposed to cooperate with
2044 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2045 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2046 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002047 *
2048 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002050static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Jiang Liu44677152015-04-14 10:29:53 +08002052 struct irq_data *irq_data = irq_get_irq_data(0);
2053 struct mp_chip_data *data = irq_data->chip_data;
2054 struct irq_cfg *cfg = irqd_cfg(irq_data);
Robert Richterf6e9456c2010-07-21 19:03:58 +02002055 int node = cpu_to_node(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002056 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002057 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002058 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002059
2060 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
2063 * get/set the timer IRQ vector:
2064 */
Thomas Gleixner4305df92010-09-28 15:01:33 +02002065 legacy_pic->mask(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002068 * As IRQ0 is to be enabled in the 8259A, the virtual
2069 * wire has to be disabled in the local APIC. Also
2070 * timer interrupts need to be acknowledged manually in
2071 * the 8259A for the i82489DX when using the NMI
2072 * watchdog as that APIC treats NMIs as level-triggered.
2073 * The AEOI mode will finish them in the 8259A
2074 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002076 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Jacob Panb81bb372009-11-09 11:27:04 -08002077 legacy_pic->init(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002079 pin1 = find_isa_irq_pin(0, mp_INT);
2080 apic1 = find_isa_irq_apic(0, mp_INT);
2081 pin2 = ioapic_i8259.pin;
2082 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002084 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2085 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002086 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002088 /*
2089 * Some BIOS writers are clueless and report the ExtINTA
2090 * I/O APIC input from the cascaded 8259A as the timer
2091 * interrupt input. So just in case, if only one pin
2092 * was found above, try it both directly and through the
2093 * 8259A.
2094 */
2095 if (pin1 == -1) {
Joerg Roedel6a9f5de2012-09-26 12:44:41 +02002096 panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002097 pin1 = pin2;
2098 apic1 = apic2;
2099 no_pin1 = 1;
2100 } else if (pin2 == -1) {
2101 pin2 = pin1;
2102 apic2 = apic1;
2103 }
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (pin1 != -1) {
Jiang Liud32932d2015-04-13 14:11:59 +08002106 /* Ok, does IRQ0 through the IOAPIC work? */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002107 if (no_pin1) {
Jiang Liud32932d2015-04-13 14:11:59 +08002108 mp_alloc_timer_irq(apic1, pin1);
Yinghai Luf72dcca2009-02-08 16:18:03 -08002109 } else {
Jiang Liud32932d2015-04-13 14:11:59 +08002110 /*
2111 * for edge trigger, it's already unmasked,
Yinghai Luf72dcca2009-02-08 16:18:03 -08002112 * so only need to unmask if it is level-trigger
2113 * do we really have level trigger timer?
2114 */
2115 int idx;
2116 idx = find_irq_entry(apic1, pin1, mp_INT);
2117 if (idx != -1 && irq_trigger(idx))
Jiang Liu44677152015-04-14 10:29:53 +08002118 unmask_ioapic_irq(irq_get_chip_data(0));
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002119 }
Thomas Gleixneraaaec6f2017-01-31 19:03:21 +01002120 irq_domain_deactivate_irq(irq_data);
Jiang Liu44677152015-04-14 10:29:53 +08002121 irq_domain_activate_irq(irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (timer_irq_works()) {
Chuck Ebbert66759a02005-09-12 18:49:25 +02002123 if (disable_timer_pin_1 > 0)
2124 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
Joerg Roedel6a9f5de2012-09-26 12:44:41 +02002127 panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
Yinghai Luf72dcca2009-02-08 16:18:03 -08002128 local_irq_disable();
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002129 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002130 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002131 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2132 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002134 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2135 "(IRQ0) through the 8259A ...\n");
2136 apic_printk(APIC_QUIET, KERN_INFO
2137 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /*
2139 * legacy devices should be connected to IO APIC #0
2140 */
Jiang Liu44677152015-04-14 10:29:53 +08002141 replace_pin_at_irq_node(data, node, apic1, pin1, apic2, pin2);
Thomas Gleixneraaaec6f2017-01-31 19:03:21 +01002142 irq_domain_deactivate_irq(irq_data);
Jiang Liu44677152015-04-14 10:29:53 +08002143 irq_domain_activate_irq(irq_data);
Thomas Gleixner4305df92010-09-28 15:01:33 +02002144 legacy_pic->unmask(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002146 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149 /*
2150 * Cleanup, just in case ...
2151 */
Yinghai Luf72dcca2009-02-08 16:18:03 -08002152 local_irq_disable();
Thomas Gleixner4305df92010-09-28 15:01:33 +02002153 legacy_pic->mask(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002154 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002155 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002158 apic_printk(APIC_QUIET, KERN_INFO
2159 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Thomas Gleixner60c69942010-09-28 17:28:38 +02002161 lapic_register_intr(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002162 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Thomas Gleixner4305df92010-09-28 15:01:33 +02002163 legacy_pic->unmask(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002166 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002167 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002169 local_irq_disable();
Thomas Gleixner4305df92010-09-28 15:01:33 +02002170 legacy_pic->mask(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002171 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002172 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002174 apic_printk(APIC_QUIET, KERN_INFO
2175 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Jacob Panb81bb372009-11-09 11:27:04 -08002177 legacy_pic->init(0);
2178 legacy_pic->make_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002179 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 unlock_ExtINT_logic();
2182
2183 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002184 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002187 local_irq_disable();
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002188 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Thomas Gleixner2ca5b402015-01-15 21:22:14 +00002189 if (apic_is_x2apic_enabled())
Yinghai Lufb209bd2011-12-21 17:45:17 -08002190 apic_printk(APIC_QUIET, KERN_INFO
2191 "Perhaps problem with the pre-enabled x2apic mode\n"
2192 "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002194 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002195out:
2196 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
2199/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002200 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2201 * to devices. However there may be an I/O APIC pin available for
2202 * this interrupt regardless. The pin may be left unconnected, but
2203 * typically it will be reused as an ExtINT cascade interrupt for
2204 * the master 8259A. In the MPS case such a pin will normally be
2205 * reported as an ExtINT interrupt in the MP table. With ACPI
2206 * there is no provision for ExtINT interrupts, and in the absence
2207 * of an override it would be treated as an ordinary ISA I/O APIC
2208 * interrupt, that is edge-triggered and unmasked by default. We
2209 * used to do this, but it caused problems on some systems because
2210 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2211 * the same ExtINT cascade interrupt to drive the local APIC of the
2212 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2213 * the I/O APIC in all cases now. No actual device should request
2214 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002216#define PIC_IRQS (1UL << PIC_CASCADE_IR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Jiang Liu44767bf2014-06-09 16:19:53 +08002218static int mp_irqdomain_create(int ioapic)
2219{
Jiang Liud32932d2015-04-13 14:11:59 +08002220 struct irq_alloc_info info;
2221 struct irq_domain *parent;
Jiang Liu44767bf2014-06-09 16:19:53 +08002222 int hwirqs = mp_ioapic_pin_count(ioapic);
2223 struct ioapic *ip = &ioapics[ioapic];
2224 struct ioapic_domain_cfg *cfg = &ip->irqdomain_cfg;
2225 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
Thomas Gleixner1b604742017-06-20 01:37:07 +02002226 struct fwnode_handle *fn;
2227 char *name = "IO-APIC";
Jiang Liu44767bf2014-06-09 16:19:53 +08002228
2229 if (cfg->type == IOAPIC_DOMAIN_INVALID)
2230 return 0;
2231
Jiang Liud32932d2015-04-13 14:11:59 +08002232 init_irq_alloc_info(&info, NULL);
2233 info.type = X86_IRQ_ALLOC_TYPE_IOAPIC;
2234 info.ioapic_id = mpc_ioapic_id(ioapic);
2235 parent = irq_remapping_get_ir_irq_domain(&info);
2236 if (!parent)
2237 parent = x86_vector_domain;
Thomas Gleixner1b604742017-06-20 01:37:07 +02002238 else
2239 name = "IO-APIC-IR";
Jiang Liud32932d2015-04-13 14:11:59 +08002240
Thomas Gleixner1b604742017-06-20 01:37:07 +02002241 /* Handle device tree enumerated APICs proper */
2242 if (cfg->dev) {
2243 fn = of_node_to_fwnode(cfg->dev);
2244 } else {
2245 fn = irq_domain_alloc_named_id_fwnode(name, ioapic);
2246 if (!fn)
2247 return -ENOMEM;
2248 }
2249
2250 ip->irqdomain = irq_domain_create_linear(fn, hwirqs, cfg->ops,
2251 (void *)(long)ioapic);
2252
2253 /* Release fw handle if it was allocated above */
2254 if (!cfg->dev)
2255 irq_domain_free_fwnode(fn);
2256
Jiang Liub75e8182015-04-14 10:29:39 +08002257 if (!ip->irqdomain)
Jiang Liu44767bf2014-06-09 16:19:53 +08002258 return -ENOMEM;
Jiang Liub75e8182015-04-14 10:29:39 +08002259
2260 ip->irqdomain->parent = parent;
Jiang Liu44767bf2014-06-09 16:19:53 +08002261
2262 if (cfg->type == IOAPIC_DOMAIN_LEGACY ||
2263 cfg->type == IOAPIC_DOMAIN_STRICT)
2264 ioapic_dynirq_base = max(ioapic_dynirq_base,
2265 gsi_cfg->gsi_end + 1);
2266
Jiang Liu44767bf2014-06-09 16:19:53 +08002267 return 0;
2268}
2269
Jiang Liu15516a32014-10-27 13:21:46 +08002270static void ioapic_destroy_irqdomain(int idx)
2271{
2272 if (ioapics[idx].irqdomain) {
2273 irq_domain_remove(ioapics[idx].irqdomain);
2274 ioapics[idx].irqdomain = NULL;
2275 }
Jiang Liu15516a32014-10-27 13:21:46 +08002276}
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278void __init setup_IO_APIC(void)
2279{
Jiang Liu44767bf2014-06-09 16:19:53 +08002280 int ioapic;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002281
Thomas Gleixnera46f5c82015-01-15 21:22:32 +00002282 if (skip_ioapic_setup || !nr_ioapics)
2283 return;
2284
Jiang Liu95d76ac2014-06-09 16:19:48 +08002285 io_apic_irqs = nr_legacy_irqs() ? ~PIC_IRQS : ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Ingo Molnar54168ed2008-08-20 09:07:45 +02002287 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Jiang Liu44767bf2014-06-09 16:19:53 +08002288 for_each_ioapic(ioapic)
2289 BUG_ON(mp_irqdomain_create(ioapic));
2290
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002291 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002292 * Set up IO-APIC IRQ routing.
2293 */
Thomas Gleixnerde934102009-08-20 09:27:29 +02002294 x86_init.mpparse.setup_ioapic_ids();
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 sync_Arb_IDs();
2297 setup_IO_APIC_irqs();
2298 init_IO_APIC_traps();
Jiang Liu95d76ac2014-06-09 16:19:48 +08002299 if (nr_legacy_irqs())
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002300 check_timer();
Jiang Liub81975e2014-06-09 16:20:11 +08002301
2302 ioapic_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Yinghai Lu6f50d452011-10-12 00:33:48 -07002305static void resume_ioapic_id(int ioapic_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 unsigned long flags;
2308 union IO_APIC_reg_00 reg_00;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002309
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002310 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07002311 reg_00.raw = io_apic_read(ioapic_idx, 0);
2312 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
2313 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
2314 io_apic_write(ioapic_idx, 0, reg_00.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002316 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002319static void ioapic_resume(void)
2320{
Yinghai Lu6f50d452011-10-12 00:33:48 -07002321 int ioapic_idx;
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002322
Jiang Liuf44d1692014-06-09 16:19:42 +08002323 for_each_ioapic_reverse(ioapic_idx)
Yinghai Lu6f50d452011-10-12 00:33:48 -07002324 resume_ioapic_id(ioapic_idx);
Suresh Siddha15bac202011-05-18 16:31:34 -07002325
2326 restore_ioapic_entries();
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002327}
2328
2329static struct syscore_ops ioapic_syscore_ops = {
Suresh Siddha15bac202011-05-18 16:31:34 -07002330 .suspend = save_ioapic_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 .resume = ioapic_resume,
2332};
2333
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002334static int __init ioapic_init_ops(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002336 register_syscore_ops(&ioapic_syscore_ops);
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return 0;
2339}
2340
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002341device_initcall(ioapic_init_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Jiang Liu67dc5e72014-10-27 13:21:41 +08002343static int io_apic_get_redir_entries(int ioapic)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07002344{
2345 union IO_APIC_reg_01 reg_01;
2346 unsigned long flags;
2347
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002348 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07002349 reg_01.raw = io_apic_read(ioapic, 1);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002350 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07002351
Eric W. Biederman4b6b19a2010-03-30 01:07:08 -07002352 /* The register returns the maximum index redir index
2353 * supported, which is one less than the total number of redir
2354 * entries.
2355 */
2356 return reg_01.bits.entries + 1;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07002357}
2358
Thomas Gleixner62a08ae2014-04-24 09:50:53 +02002359unsigned int arch_dynirq_lower_bound(unsigned int from)
2360{
Jiang Liub81975e2014-06-09 16:20:11 +08002361 /*
2362 * dmar_alloc_hwirq() may be called before setup_IO_APIC(), so use
2363 * gsi_top if ioapic_dynirq_base hasn't been initialized yet.
2364 */
2365 return ioapic_initialized ? ioapic_dynirq_base : gsi_top;
Thomas Gleixner62a08ae2014-04-24 09:50:53 +02002366}
2367
Feng Tang2a4ab642009-07-07 23:01:15 -04002368#ifdef CONFIG_X86_32
Jiang Liu67dc5e72014-10-27 13:21:41 +08002369static int io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
2371 union IO_APIC_reg_00 reg_00;
2372 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2373 physid_mask_t tmp;
2374 unsigned long flags;
2375 int i = 0;
2376
2377 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002378 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2379 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002381 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2383 * advantage of new APIC bus architecture.
2384 */
2385
2386 if (physids_empty(apic_id_map))
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002387 apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002389 raw_spin_lock_irqsave(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 reg_00.raw = io_apic_read(ioapic, 0);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002391 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 if (apic_id >= get_physical_broadcast()) {
2394 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2395 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2396 apic_id = reg_00.bits.ID;
2397 }
2398
2399 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002400 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 * 'stuck on smp_invalidate_needed IPI wait' messages.
2402 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002403 if (apic->check_apicid_used(&apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 for (i = 0; i < get_physical_broadcast(); i++) {
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002406 if (!apic->check_apicid_used(&apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 break;
2408 }
2409
2410 if (i == get_physical_broadcast())
2411 panic("Max apic_id exceeded!\n");
2412
2413 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2414 "trying %d\n", ioapic, apic_id, i);
2415
2416 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002419 apic->apicid_to_cpu_present(apic_id, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 physids_or(apic_id_map, apic_id_map, tmp);
2421
2422 if (reg_00.bits.ID != apic_id) {
2423 reg_00.bits.ID = apic_id;
2424
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002425 raw_spin_lock_irqsave(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 io_apic_write(ioapic, 0, reg_00.raw);
2427 reg_00.raw = io_apic_read(ioapic, 0);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002428 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002431 if (reg_00.bits.ID != apic_id) {
Joe Perchesc767a542012-05-21 19:50:07 -07002432 pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
2433 ioapic);
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002434 return -1;
2435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437
2438 apic_printk(APIC_VERBOSE, KERN_INFO
2439 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2440
2441 return apic_id;
2442}
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002443
Jiang Liu67dc5e72014-10-27 13:21:41 +08002444static u8 io_apic_unique_id(int idx, u8 id)
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002445{
2446 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
Denys Vlasenkocff9ab22016-09-13 20:12:32 +02002447 !APIC_XAPIC(boot_cpu_apic_version))
Yinghai Lu5411dc42014-10-27 13:21:40 +08002448 return io_apic_get_unique_id(idx, id);
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002449 else
2450 return id;
2451}
2452#else
Jiang Liu67dc5e72014-10-27 13:21:41 +08002453static u8 io_apic_unique_id(int idx, u8 id)
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002454{
Yinghai Lu5411dc42014-10-27 13:21:40 +08002455 union IO_APIC_reg_00 reg_00;
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002456 DECLARE_BITMAP(used, 256);
Yinghai Lu5411dc42014-10-27 13:21:40 +08002457 unsigned long flags;
2458 u8 new_id;
2459 int i;
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002460
2461 bitmap_zero(used, 256);
Jiang Liuf44d1692014-06-09 16:19:42 +08002462 for_each_ioapic(i)
Suresh Siddhad5371432011-05-18 16:31:37 -07002463 __set_bit(mpc_ioapic_id(i), used);
Yinghai Lu5411dc42014-10-27 13:21:40 +08002464
2465 /* Hand out the requested id if available */
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002466 if (!test_bit(id, used))
2467 return id;
Yinghai Lu5411dc42014-10-27 13:21:40 +08002468
2469 /*
2470 * Read the current id from the ioapic and keep it if
2471 * available.
2472 */
2473 raw_spin_lock_irqsave(&ioapic_lock, flags);
2474 reg_00.raw = io_apic_read(idx, 0);
2475 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2476 new_id = reg_00.bits.ID;
2477 if (!test_bit(new_id, used)) {
2478 apic_printk(APIC_VERBOSE, KERN_INFO
2479 "IOAPIC[%d]: Using reg apic_id %d instead of %d\n",
2480 idx, new_id, id);
2481 return new_id;
2482 }
2483
2484 /*
2485 * Get the next free id and write it to the ioapic.
2486 */
2487 new_id = find_first_zero_bit(used, 256);
2488 reg_00.bits.ID = new_id;
2489 raw_spin_lock_irqsave(&ioapic_lock, flags);
2490 io_apic_write(idx, 0, reg_00.raw);
2491 reg_00.raw = io_apic_read(idx, 0);
2492 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2493 /* Sanity check */
2494 BUG_ON(reg_00.bits.ID != new_id);
2495
2496 return new_id;
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002497}
Naga Chumbalkar58f892e2009-05-26 21:48:07 +00002498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Jiang Liu67dc5e72014-10-27 13:21:41 +08002500static int io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 union IO_APIC_reg_01 reg_01;
2503 unsigned long flags;
2504
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002505 raw_spin_lock_irqsave(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 reg_01.raw = io_apic_read(ioapic, 1);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002507 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 return reg_01.bits.version;
2510}
2511
Eric W. Biederman9a0a91b2010-03-30 01:07:03 -07002512int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002513{
Eric W. Biederman9a0a91b2010-03-30 01:07:03 -07002514 int ioapic, pin, idx;
Shaohua Li61fd47e2007-11-17 01:05:28 -05002515
2516 if (skip_ioapic_setup)
2517 return -1;
2518
Eric W. Biederman9a0a91b2010-03-30 01:07:03 -07002519 ioapic = mp_find_ioapic(gsi);
2520 if (ioapic < 0)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002521 return -1;
2522
Eric W. Biederman9a0a91b2010-03-30 01:07:03 -07002523 pin = mp_find_ioapic_pin(ioapic, gsi);
2524 if (pin < 0)
2525 return -1;
2526
2527 idx = find_irq_entry(ioapic, pin, mp_INT);
2528 if (idx < 0)
2529 return -1;
2530
2531 *trigger = irq_trigger(idx);
2532 *polarity = irq_polarity(idx);
Shaohua Li61fd47e2007-11-17 01:05:28 -05002533 return 0;
2534}
2535
Yinghai Lu497c9a12008-08-19 20:50:28 -07002536/*
2537 * This function currently is only a helper for the i386 smp boot process where
2538 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01002539 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07002540 */
2541#ifdef CONFIG_SMP
2542void __init setup_ioapic_dest(void)
2543{
Eric W. Biedermanfad53992010-02-28 01:06:34 -08002544 int pin, ioapic, irq, irq_entry;
Mike Travis22f65d32008-12-16 17:33:56 -08002545 const struct cpumask *mask;
Thomas Gleixnere23b2572016-01-14 08:43:38 +01002546 struct irq_desc *desc;
Thomas Gleixner5451ddc2011-02-05 15:35:51 +01002547 struct irq_data *idata;
Thomas Gleixner4857c912015-09-14 12:00:55 +02002548 struct irq_chip *chip;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002549
2550 if (skip_ioapic_setup == 1)
2551 return;
2552
Jiang Liuf44d1692014-06-09 16:19:42 +08002553 for_each_ioapic_pin(ioapic, pin) {
Yinghai Lub9c61b702009-05-06 10:10:06 -07002554 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2555 if (irq_entry == -1)
2556 continue;
Yinghai Lub9c61b702009-05-06 10:10:06 -07002557
Jiang Liud7f3d472014-06-09 16:19:52 +08002558 irq = pin_2_irq(irq_entry, ioapic, pin, 0);
2559 if (irq < 0 || !mp_init_irq_at_boot(ioapic, irq))
Eric W. Biedermanfad53992010-02-28 01:06:34 -08002560 continue;
2561
Thomas Gleixnere23b2572016-01-14 08:43:38 +01002562 desc = irq_to_desc(irq);
2563 raw_spin_lock_irq(&desc->lock);
2564 idata = irq_desc_get_irq_data(desc);
Yinghai Lub9c61b702009-05-06 10:10:06 -07002565
2566 /*
2567 * Honour affinities which have been set in early boot
2568 */
Thomas Gleixner5451ddc2011-02-05 15:35:51 +01002569 if (!irqd_can_balance(idata) || irqd_affinity_was_set(idata))
Jiang Liuc149e4c2015-06-03 11:46:22 +08002570 mask = irq_data_get_affinity_mask(idata);
Yinghai Lub9c61b702009-05-06 10:10:06 -07002571 else
2572 mask = apic->target_cpus();
2573
Thomas Gleixner4857c912015-09-14 12:00:55 +02002574 chip = irq_data_get_irq_chip(idata);
Werner Pawlitschkoababae42015-10-27 09:08:04 +09002575 /* Might be lapic_chip for irq 0 */
2576 if (chip->irq_set_affinity)
2577 chip->irq_set_affinity(idata, mask, false);
Thomas Gleixnere23b2572016-01-14 08:43:38 +01002578 raw_spin_unlock_irq(&desc->lock);
Yinghai Lub9c61b702009-05-06 10:10:06 -07002579 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002580}
2581#endif
2582
Ingo Molnar54168ed2008-08-20 09:07:45 +02002583#define IOAPIC_RESOURCE_NAME_SIZE 11
2584
2585static struct resource *ioapic_resources;
2586
Jiang Liuf44d1692014-06-09 16:19:42 +08002587static struct resource * __init ioapic_setup_resources(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002588{
2589 unsigned long n;
2590 struct resource *res;
2591 char *mem;
Rui Wang48555312016-06-08 14:59:53 +08002592 int i;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002593
Rui Wang48555312016-06-08 14:59:53 +08002594 if (nr_ioapics == 0)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002595 return NULL;
2596
2597 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
Rui Wang48555312016-06-08 14:59:53 +08002598 n *= nr_ioapics;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002599
2600 mem = alloc_bootmem(n);
2601 res = (void *)mem;
2602
Rui Wang48555312016-06-08 14:59:53 +08002603 mem += sizeof(struct resource) * nr_ioapics;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002604
Jiang Liuf44d1692014-06-09 16:19:42 +08002605 for_each_ioapic(i) {
Rui Wang48555312016-06-08 14:59:53 +08002606 res[i].name = mem;
2607 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Cyrill Gorcunov4343fe12009-11-08 18:54:31 +03002608 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04002609 mem += IOAPIC_RESOURCE_NAME_SIZE;
Rui Wang48555312016-06-08 14:59:53 +08002610 ioapics[i].iomem_res = &res[i];
Ingo Molnar54168ed2008-08-20 09:07:45 +02002611 }
2612
2613 ioapic_resources = res;
2614
2615 return res;
2616}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002617
Thomas Gleixnerca1b8862015-04-24 13:57:48 +02002618void __init io_apic_init_mappings(void)
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -04002619{
Yinghai Luf3294a32008-06-27 01:41:56 -07002620 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002621 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002622 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07002623
Jiang Liuf44d1692014-06-09 16:19:42 +08002624 ioapic_res = ioapic_setup_resources();
2625 for_each_ioapic(i) {
Yinghai Luf3294a32008-06-27 01:41:56 -07002626 if (smp_found_config) {
Suresh Siddhad5371432011-05-18 16:31:37 -07002627 ioapic_phys = mpc_ioapic_addr(i);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002628#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002629 if (!ioapic_phys) {
2630 printk(KERN_ERR
2631 "WARNING: bogus zero IO-APIC "
2632 "address found in MPTABLE, "
2633 "disabling IO/APIC support!\n");
2634 smp_found_config = 0;
2635 skip_ioapic_setup = 1;
2636 goto fake_ioapic_page;
2637 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002638#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07002639 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002640#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07002641fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02002642#endif
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03002643 ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07002644 ioapic_phys = __pa(ioapic_phys);
2645 }
2646 set_fixmap_nocache(idx, ioapic_phys);
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03002647 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
2648 __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
2649 ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07002650 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002651
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04002652 ioapic_res->start = ioapic_phys;
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03002653 ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04002654 ioapic_res++;
Yinghai Luf3294a32008-06-27 01:41:56 -07002655 }
2656}
2657
Yinghai Lu857fdc52009-07-10 09:36:20 -07002658void __init ioapic_insert_resources(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002659{
2660 int i;
2661 struct resource *r = ioapic_resources;
2662
2663 if (!r) {
Yinghai Lu857fdc52009-07-10 09:36:20 -07002664 if (nr_ioapics > 0)
Bartlomiej Zolnierkiewicz04c93ce2009-03-20 21:02:55 +01002665 printk(KERN_ERR
2666 "IO APIC resources couldn't be allocated.\n");
Yinghai Lu857fdc52009-07-10 09:36:20 -07002667 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002668 }
2669
Jiang Liuf44d1692014-06-09 16:19:42 +08002670 for_each_ioapic(i) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002671 insert_resource(&iomem_resource, r);
2672 r++;
2673 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002674}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002675
Eric W. Biedermaneddb0c52010-03-30 01:07:09 -07002676int mp_find_ioapic(u32 gsi)
Feng Tang2a4ab642009-07-07 23:01:15 -04002677{
Jiang Liuf44d1692014-06-09 16:19:42 +08002678 int i;
Feng Tang2a4ab642009-07-07 23:01:15 -04002679
Paul Bolle678301e2011-02-14 22:52:38 +01002680 if (nr_ioapics == 0)
2681 return -1;
2682
Feng Tang2a4ab642009-07-07 23:01:15 -04002683 /* Find the IOAPIC that manages this GSI. */
Jiang Liuf44d1692014-06-09 16:19:42 +08002684 for_each_ioapic(i) {
Suresh Siddhac040aae2011-05-18 16:31:38 -07002685 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
Jiang Liuf44d1692014-06-09 16:19:42 +08002686 if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end)
Feng Tang2a4ab642009-07-07 23:01:15 -04002687 return i;
2688 }
2689
2690 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
2691 return -1;
2692}
2693
Eric W. Biedermaneddb0c52010-03-30 01:07:09 -07002694int mp_find_ioapic_pin(int ioapic, u32 gsi)
Feng Tang2a4ab642009-07-07 23:01:15 -04002695{
Suresh Siddhac040aae2011-05-18 16:31:38 -07002696 struct mp_ioapic_gsi *gsi_cfg;
2697
Jiang Liuf44d1692014-06-09 16:19:42 +08002698 if (WARN_ON(ioapic < 0))
Feng Tang2a4ab642009-07-07 23:01:15 -04002699 return -1;
Suresh Siddhac040aae2011-05-18 16:31:38 -07002700
2701 gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2702 if (WARN_ON(gsi > gsi_cfg->gsi_end))
Feng Tang2a4ab642009-07-07 23:01:15 -04002703 return -1;
2704
Suresh Siddhac040aae2011-05-18 16:31:38 -07002705 return gsi - gsi_cfg->gsi_base;
Feng Tang2a4ab642009-07-07 23:01:15 -04002706}
2707
Jiang Liu67dc5e72014-10-27 13:21:41 +08002708static int bad_ioapic_register(int idx)
Suresh Siddha73d63d02012-03-12 11:36:33 -07002709{
2710 union IO_APIC_reg_00 reg_00;
2711 union IO_APIC_reg_01 reg_01;
2712 union IO_APIC_reg_02 reg_02;
2713
2714 reg_00.raw = io_apic_read(idx, 0);
2715 reg_01.raw = io_apic_read(idx, 1);
2716 reg_02.raw = io_apic_read(idx, 2);
2717
2718 if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
2719 pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
2720 mpc_ioapic_addr(idx));
2721 return 1;
2722 }
2723
2724 return 0;
2725}
2726
Jiang Liu35ef9c92014-10-27 13:21:43 +08002727static int find_free_ioapic_entry(void)
Feng Tang2a4ab642009-07-07 23:01:15 -04002728{
Jiang Liu7db298c2014-10-27 13:21:45 +08002729 int idx;
2730
2731 for (idx = 0; idx < MAX_IO_APICS; idx++)
2732 if (ioapics[idx].nr_registers == 0)
2733 return idx;
2734
2735 return MAX_IO_APICS;
Jiang Liu35ef9c92014-10-27 13:21:43 +08002736}
2737
2738/**
2739 * mp_register_ioapic - Register an IOAPIC device
2740 * @id: hardware IOAPIC ID
2741 * @address: physical address of IOAPIC register area
2742 * @gsi_base: base of GSI associated with the IOAPIC
2743 * @cfg: configuration information for the IOAPIC
2744 */
2745int mp_register_ioapic(int id, u32 address, u32 gsi_base,
2746 struct ioapic_domain_cfg *cfg)
2747{
Jiang Liu7db298c2014-10-27 13:21:45 +08002748 bool hotplug = !!ioapic_initialized;
Suresh Siddhac040aae2011-05-18 16:31:38 -07002749 struct mp_ioapic_gsi *gsi_cfg;
Jiang Liu35ef9c92014-10-27 13:21:43 +08002750 int idx, ioapic, entries;
2751 u32 gsi_end;
Feng Tang2a4ab642009-07-07 23:01:15 -04002752
Jiang Liu35ef9c92014-10-27 13:21:43 +08002753 if (!address) {
2754 pr_warn("Bogus (zero) I/O APIC address found, skipping!\n");
2755 return -EINVAL;
2756 }
2757 for_each_ioapic(ioapic)
2758 if (ioapics[ioapic].mp_config.apicaddr == address) {
2759 pr_warn("address 0x%x conflicts with IOAPIC%d\n",
2760 address, ioapic);
2761 return -EEXIST;
2762 }
Feng Tang2a4ab642009-07-07 23:01:15 -04002763
Jiang Liu35ef9c92014-10-27 13:21:43 +08002764 idx = find_free_ioapic_entry();
2765 if (idx >= MAX_IO_APICS) {
2766 pr_warn("Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
2767 MAX_IO_APICS, idx);
2768 return -ENOSPC;
2769 }
Feng Tang2a4ab642009-07-07 23:01:15 -04002770
Suresh Siddhad5371432011-05-18 16:31:37 -07002771 ioapics[idx].mp_config.type = MP_IOAPIC;
2772 ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
2773 ioapics[idx].mp_config.apicaddr = address;
Feng Tang2a4ab642009-07-07 23:01:15 -04002774
2775 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
Suresh Siddha73d63d02012-03-12 11:36:33 -07002776 if (bad_ioapic_register(idx)) {
2777 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
Jiang Liu35ef9c92014-10-27 13:21:43 +08002778 return -ENODEV;
Suresh Siddha73d63d02012-03-12 11:36:33 -07002779 }
2780
Yinghai Lu5411dc42014-10-27 13:21:40 +08002781 ioapics[idx].mp_config.apicid = io_apic_unique_id(idx, id);
Suresh Siddhad5371432011-05-18 16:31:37 -07002782 ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
Feng Tang2a4ab642009-07-07 23:01:15 -04002783
2784 /*
2785 * Build basic GSI lookup table to facilitate gsi->io_apic lookups
2786 * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
2787 */
Eric W. Biederman7716a5c2010-03-30 01:07:12 -07002788 entries = io_apic_get_redir_entries(idx);
Jiang Liu35ef9c92014-10-27 13:21:43 +08002789 gsi_end = gsi_base + entries - 1;
2790 for_each_ioapic(ioapic) {
2791 gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2792 if ((gsi_base >= gsi_cfg->gsi_base &&
2793 gsi_base <= gsi_cfg->gsi_end) ||
2794 (gsi_end >= gsi_cfg->gsi_base &&
2795 gsi_end <= gsi_cfg->gsi_end)) {
2796 pr_warn("GSI range [%u-%u] for new IOAPIC conflicts with GSI[%u-%u]\n",
2797 gsi_base, gsi_end,
2798 gsi_cfg->gsi_base, gsi_cfg->gsi_end);
2799 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
2800 return -ENOSPC;
2801 }
2802 }
Suresh Siddhac040aae2011-05-18 16:31:38 -07002803 gsi_cfg = mp_ioapic_gsi_routing(idx);
2804 gsi_cfg->gsi_base = gsi_base;
Jiang Liu35ef9c92014-10-27 13:21:43 +08002805 gsi_cfg->gsi_end = gsi_end;
Eric W. Biederman7716a5c2010-03-30 01:07:12 -07002806
Jiang Liu35ef9c92014-10-27 13:21:43 +08002807 ioapics[idx].irqdomain = NULL;
2808 ioapics[idx].irqdomain_cfg = *cfg;
Feng Tang2a4ab642009-07-07 23:01:15 -04002809
Jiang Liu7db298c2014-10-27 13:21:45 +08002810 /*
2811 * If mp_register_ioapic() is called during early boot stage when
2812 * walking ACPI/SFI/DT tables, it's too early to create irqdomain,
2813 * we are still using bootmem allocator. So delay it to setup_IO_APIC().
2814 */
2815 if (hotplug) {
2816 if (mp_irqdomain_create(idx)) {
2817 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
2818 return -ENOMEM;
2819 }
2820 alloc_ioapic_saved_registers(idx);
2821 }
2822
Suresh Siddhac040aae2011-05-18 16:31:38 -07002823 if (gsi_cfg->gsi_end >= gsi_top)
2824 gsi_top = gsi_cfg->gsi_end + 1;
Jiang Liu35ef9c92014-10-27 13:21:43 +08002825 if (nr_ioapics <= idx)
2826 nr_ioapics = idx + 1;
2827
2828 /* Set nr_registers to mark entry present */
2829 ioapics[idx].nr_registers = entries;
Feng Tang2a4ab642009-07-07 23:01:15 -04002830
Suresh Siddha73d63d02012-03-12 11:36:33 -07002831 pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
2832 idx, mpc_ioapic_id(idx),
2833 mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
2834 gsi_cfg->gsi_base, gsi_cfg->gsi_end);
Feng Tang2a4ab642009-07-07 23:01:15 -04002835
Jiang Liu35ef9c92014-10-27 13:21:43 +08002836 return 0;
Feng Tang2a4ab642009-07-07 23:01:15 -04002837}
Jacob Pan05ddafb2009-09-23 07:20:23 -07002838
Jiang Liu15516a32014-10-27 13:21:46 +08002839int mp_unregister_ioapic(u32 gsi_base)
2840{
2841 int ioapic, pin;
2842 int found = 0;
Jiang Liu15516a32014-10-27 13:21:46 +08002843
2844 for_each_ioapic(ioapic)
2845 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) {
2846 found = 1;
2847 break;
2848 }
2849 if (!found) {
2850 pr_warn("can't find IOAPIC for GSI %d\n", gsi_base);
2851 return -ENODEV;
2852 }
2853
2854 for_each_pin(ioapic, pin) {
Jiang Liud32932d2015-04-13 14:11:59 +08002855 u32 gsi = mp_pin_to_gsi(ioapic, pin);
2856 int irq = mp_map_gsi_to_irq(gsi, 0, NULL);
2857 struct mp_chip_data *data;
2858
2859 if (irq >= 0) {
2860 data = irq_get_chip_data(irq);
2861 if (data && data->count) {
2862 pr_warn("pin%d on IOAPIC%d is still in use.\n",
2863 pin, ioapic);
2864 return -EBUSY;
2865 }
Jiang Liu15516a32014-10-27 13:21:46 +08002866 }
2867 }
2868
2869 /* Mark entry not present */
2870 ioapics[ioapic].nr_registers = 0;
2871 ioapic_destroy_irqdomain(ioapic);
2872 free_ioapic_saved_registers(ioapic);
2873 if (ioapics[ioapic].iomem_res)
2874 release_resource(ioapics[ioapic].iomem_res);
2875 clear_fixmap(FIX_IO_APIC_BASE_0 + ioapic);
2876 memset(&ioapics[ioapic], 0, sizeof(ioapics[ioapic]));
2877
2878 return 0;
2879}
2880
Jiang Liue89900c2014-10-27 13:21:47 +08002881int mp_ioapic_registered(u32 gsi_base)
2882{
2883 int ioapic;
2884
2885 for_each_ioapic(ioapic)
2886 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base)
2887 return 1;
2888
2889 return 0;
2890}
2891
Jiang Liu49c7e602015-04-13 14:11:55 +08002892static void mp_irqdomain_get_attr(u32 gsi, struct mp_chip_data *data,
Jiang Liu5ad274d2015-04-14 10:29:38 +08002893 struct irq_alloc_info *info)
Jiang Liu49c7e602015-04-13 14:11:55 +08002894{
2895 if (info && info->ioapic_valid) {
2896 data->trigger = info->ioapic_trigger;
2897 data->polarity = info->ioapic_polarity;
2898 } else if (acpi_get_override_irq(gsi, &data->trigger,
2899 &data->polarity) < 0) {
Thomas Gleixner335efdf2015-04-14 10:30:06 +08002900 /* PCI interrupts are always active low level triggered. */
2901 data->trigger = IOAPIC_LEVEL;
2902 data->polarity = IOAPIC_POL_LOW;
Jiang Liu49c7e602015-04-13 14:11:55 +08002903 }
2904}
2905
2906static void mp_setup_entry(struct irq_cfg *cfg, struct mp_chip_data *data,
2907 struct IO_APIC_route_entry *entry)
2908{
2909 memset(entry, 0, sizeof(*entry));
2910 entry->delivery_mode = apic->irq_delivery_mode;
2911 entry->dest_mode = apic->irq_dest_mode;
2912 entry->dest = cfg->dest_apicid;
2913 entry->vector = cfg->vector;
Jiang Liu49c7e602015-04-13 14:11:55 +08002914 entry->trigger = data->trigger;
2915 entry->polarity = data->polarity;
2916 /*
Thomas Gleixner335efdf2015-04-14 10:30:06 +08002917 * Mask level triggered irqs. Edge triggered irqs are masked
2918 * by the irq core code in case they fire.
Jiang Liu49c7e602015-04-13 14:11:55 +08002919 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +08002920 if (data->trigger == IOAPIC_LEVEL)
2921 entry->mask = IOAPIC_MASKED;
2922 else
2923 entry->mask = IOAPIC_UNMASKED;
Jiang Liu49c7e602015-04-13 14:11:55 +08002924}
2925
2926int mp_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
2927 unsigned int nr_irqs, void *arg)
2928{
2929 int ret, ioapic, pin;
2930 struct irq_cfg *cfg;
2931 struct irq_data *irq_data;
2932 struct mp_chip_data *data;
2933 struct irq_alloc_info *info = arg;
Vitaly Kuznetsovc0ff9712015-10-15 19:42:23 +02002934 unsigned long flags;
Jiang Liu49c7e602015-04-13 14:11:55 +08002935
2936 if (!info || nr_irqs > 1)
2937 return -EINVAL;
2938 irq_data = irq_domain_get_irq_data(domain, virq);
2939 if (!irq_data)
2940 return -EINVAL;
2941
2942 ioapic = mp_irqdomain_ioapic_idx(domain);
2943 pin = info->ioapic_pin;
2944 if (irq_find_mapping(domain, (irq_hw_number_t)pin) > 0)
2945 return -EEXIST;
2946
2947 data = kzalloc(sizeof(*data), GFP_KERNEL);
2948 if (!data)
2949 return -ENOMEM;
2950
2951 info->ioapic_entry = &data->entry;
2952 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, info);
2953 if (ret < 0) {
2954 kfree(data);
2955 return ret;
2956 }
2957
Jiang Liu44677152015-04-14 10:29:53 +08002958 INIT_LIST_HEAD(&data->irq_2_pin);
Jiang Liu49c7e602015-04-13 14:11:55 +08002959 irq_data->hwirq = info->ioapic_pin;
Jiang Liud32932d2015-04-13 14:11:59 +08002960 irq_data->chip = (domain->parent == x86_vector_domain) ?
2961 &ioapic_chip : &ioapic_ir_chip;
Jiang Liu49c7e602015-04-13 14:11:55 +08002962 irq_data->chip_data = data;
2963 mp_irqdomain_get_attr(mp_pin_to_gsi(ioapic, pin), data, info);
2964
2965 cfg = irqd_cfg(irq_data);
Jiang Liu44677152015-04-14 10:29:53 +08002966 add_pin_to_irq_node(data, ioapic_alloc_attr_node(info), ioapic, pin);
Vitaly Kuznetsovc0ff9712015-10-15 19:42:23 +02002967
2968 local_irq_save(flags);
Jiang Liu49c7e602015-04-13 14:11:55 +08002969 if (info->ioapic_entry)
2970 mp_setup_entry(cfg, data, info->ioapic_entry);
2971 mp_register_handler(virq, data->trigger);
2972 if (virq < nr_legacy_irqs())
2973 legacy_pic->mask(virq);
Vitaly Kuznetsovc0ff9712015-10-15 19:42:23 +02002974 local_irq_restore(flags);
Jiang Liu49c7e602015-04-13 14:11:55 +08002975
2976 apic_printk(APIC_VERBOSE, KERN_DEBUG
2977 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i Dest:%d)\n",
2978 ioapic, mpc_ioapic_id(ioapic), pin, cfg->vector,
2979 virq, data->trigger, data->polarity, cfg->dest_apicid);
2980
2981 return 0;
2982}
2983
2984void mp_irqdomain_free(struct irq_domain *domain, unsigned int virq,
2985 unsigned int nr_irqs)
2986{
Jiang Liu49c7e602015-04-13 14:11:55 +08002987 struct irq_data *irq_data;
Jiang Liu44677152015-04-14 10:29:53 +08002988 struct mp_chip_data *data;
Jiang Liu49c7e602015-04-13 14:11:55 +08002989
2990 BUG_ON(nr_irqs != 1);
2991 irq_data = irq_domain_get_irq_data(domain, virq);
2992 if (irq_data && irq_data->chip_data) {
Jiang Liu44677152015-04-14 10:29:53 +08002993 data = irq_data->chip_data;
2994 __remove_pin_from_irq(data, mp_irqdomain_ioapic_idx(domain),
Jiang Liu49c7e602015-04-13 14:11:55 +08002995 (int)irq_data->hwirq);
Jiang Liu44677152015-04-14 10:29:53 +08002996 WARN_ON(!list_empty(&data->irq_2_pin));
Jiang Liu49c7e602015-04-13 14:11:55 +08002997 kfree(irq_data->chip_data);
2998 }
2999 irq_domain_free_irqs_top(domain, virq, nr_irqs);
3000}
3001
3002void mp_irqdomain_activate(struct irq_domain *domain,
3003 struct irq_data *irq_data)
3004{
3005 unsigned long flags;
3006 struct irq_pin_list *entry;
3007 struct mp_chip_data *data = irq_data->chip_data;
Jiang Liu49c7e602015-04-13 14:11:55 +08003008
3009 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +08003010 for_each_irq_pin(entry, data->irq_2_pin)
Jiang Liu49c7e602015-04-13 14:11:55 +08003011 __ioapic_write_entry(entry->apic, entry->pin, data->entry);
3012 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3013}
3014
3015void mp_irqdomain_deactivate(struct irq_domain *domain,
3016 struct irq_data *irq_data)
3017{
3018 /* It won't be called for IRQ with multiple IOAPIC pins associated */
3019 ioapic_mask_entry(mp_irqdomain_ioapic_idx(domain),
3020 (int)irq_data->hwirq);
3021}
3022
Jiang Liu49c7e602015-04-13 14:11:55 +08003023int mp_irqdomain_ioapic_idx(struct irq_domain *domain)
3024{
3025 return (int)(long)domain->host_data;
3026}
Thomas Gleixnerf7a0c782015-04-14 10:30:08 +08003027
3028const struct irq_domain_ops mp_ioapic_irqdomain_ops = {
3029 .alloc = mp_irqdomain_alloc,
3030 .free = mp_irqdomain_free,
3031 .activate = mp_irqdomain_activate,
3032 .deactivate = mp_irqdomain_deactivate,
3033};