blob: 7491f417a8e4bc255c78f37b0f1f11d0fe6c91ad [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>
Yinghai Lud4057bd2008-08-19 20:50:38 -070051#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/io.h>
53#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053054#include <asm/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070056#include <asm/proto.h>
57#include <asm/acpi.h>
58#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070060#include <asm/i8259.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070061#include <asm/setup.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070062#include <asm/irq_remapping.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +053063#include <asm/hw_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Ingo Molnar7b6aa332009-02-17 13:58:15 +010065#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jiang Liuf44d1692014-06-09 16:19:42 +080067#define for_each_ioapic(idx) \
68 for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
69#define for_each_ioapic_reverse(idx) \
70 for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--)
71#define for_each_pin(idx, pin) \
72 for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++)
73#define for_each_ioapic_pin(idx, pin) \
74 for_each_ioapic((idx)) \
75 for_each_pin((idx), (pin))
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +040076#define for_each_irq_pin(entry, head) \
Yinghai Lua178b872014-10-27 16:11:55 +080077 list_for_each_entry(entry, &head, list)
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010078
Thomas Gleixnerdade7712009-07-25 18:39:36 +020079static DEFINE_RAW_SPINLOCK(ioapic_lock);
Jiang Liud7f3d472014-06-09 16:19:52 +080080static DEFINE_MUTEX(ioapic_mutex);
Jiang Liu44767bf2014-06-09 16:19:53 +080081static unsigned int ioapic_dynirq_base;
Jiang Liub81975e2014-06-09 16:20:11 +080082static int ioapic_initialized;
Yinghai Luefa25592008-08-19 20:50:36 -070083
Jiang Liu44677152015-04-14 10:29:53 +080084struct irq_pin_list {
85 struct list_head list;
86 int apic, pin;
87};
88
Jiang Liu49c7e602015-04-13 14:11:55 +080089struct mp_chip_data {
Jiang Liu44677152015-04-14 10:29:53 +080090 struct list_head irq_2_pin;
Jiang Liu49c7e602015-04-13 14:11:55 +080091 struct IO_APIC_route_entry entry;
92 int trigger;
93 int polarity;
Jiang Liu96ed44b2015-04-13 14:11:58 +080094 u32 count;
Jiang Liu49c7e602015-04-13 14:11:55 +080095 bool isa_irq;
96};
97
Jiang Liu154d9e52015-04-14 10:29:56 +080098struct mp_ioapic_gsi {
99 u32 gsi_base;
100 u32 gsi_end;
101};
102
Suresh Siddhab69c6c32011-05-18 16:31:35 -0700103static struct ioapic {
104 /*
105 * # of IRQ routing registers
106 */
107 int nr_registers;
Suresh Siddha57a6f742011-05-18 16:31:36 -0700108 /*
109 * Saved state during suspend/resume, or while enabling intr-remap.
110 */
111 struct IO_APIC_route_entry *saved_registers;
Suresh Siddhad5371432011-05-18 16:31:37 -0700112 /* I/O APIC config */
113 struct mpc_ioapic mp_config;
Suresh Siddhac040aae2011-05-18 16:31:38 -0700114 /* IO APIC gsi routing info */
115 struct mp_ioapic_gsi gsi_config;
Jiang Liud7f3d472014-06-09 16:19:52 +0800116 struct ioapic_domain_cfg irqdomain_cfg;
117 struct irq_domain *irqdomain;
Jiang Liu15516a32014-10-27 13:21:46 +0800118 struct resource *iomem_res;
Suresh Siddhab69c6c32011-05-18 16:31:35 -0700119} ioapics[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Yinghai Lu6f50d452011-10-12 00:33:48 -0700121#define mpc_ioapic_ver(ioapic_idx) ioapics[ioapic_idx].mp_config.apicver
Suresh Siddhad5371432011-05-18 16:31:37 -0700122
Yinghai Lu6f50d452011-10-12 00:33:48 -0700123int mpc_ioapic_id(int ioapic_idx)
Suresh Siddhad5371432011-05-18 16:31:37 -0700124{
Yinghai Lu6f50d452011-10-12 00:33:48 -0700125 return ioapics[ioapic_idx].mp_config.apicid;
Suresh Siddhad5371432011-05-18 16:31:37 -0700126}
127
Yinghai Lu6f50d452011-10-12 00:33:48 -0700128unsigned int mpc_ioapic_addr(int ioapic_idx)
Suresh Siddhad5371432011-05-18 16:31:37 -0700129{
Yinghai Lu6f50d452011-10-12 00:33:48 -0700130 return ioapics[ioapic_idx].mp_config.apicaddr;
Suresh Siddhad5371432011-05-18 16:31:37 -0700131}
132
Jiang Liu154d9e52015-04-14 10:29:56 +0800133static inline struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
Suresh Siddhac040aae2011-05-18 16:31:38 -0700134{
Yinghai Lu6f50d452011-10-12 00:33:48 -0700135 return &ioapics[ioapic_idx].gsi_config;
Suresh Siddhac040aae2011-05-18 16:31:38 -0700136}
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +0400137
Jiang Liu18e48552014-06-09 16:19:45 +0800138static inline int mp_ioapic_pin_count(int ioapic)
139{
140 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
141
142 return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
143}
144
Jiang Liu154d9e52015-04-14 10:29:56 +0800145static inline u32 mp_pin_to_gsi(int ioapic, int pin)
Jiang Liu18e48552014-06-09 16:19:45 +0800146{
147 return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin;
148}
149
Jiang Liud32932d2015-04-13 14:11:59 +0800150static inline bool mp_is_legacy_irq(int irq)
151{
152 return irq >= 0 && irq < nr_legacy_irqs();
153}
154
Jiang Liu95d76ac2014-06-09 16:19:48 +0800155/*
156 * Initialize all legacy IRQs and all pins on the first IOAPIC
157 * if we have legacy interrupt controller. Kernel boot option "pirq="
158 * may rely on non-legacy pins on the first IOAPIC.
159 */
Jiang Liu18e48552014-06-09 16:19:45 +0800160static inline int mp_init_irq_at_boot(int ioapic, int irq)
161{
Jiang Liu95d76ac2014-06-09 16:19:48 +0800162 if (!nr_legacy_irqs())
163 return 0;
164
Jiang Liud32932d2015-04-13 14:11:59 +0800165 return ioapic == 0 || mp_is_legacy_irq(irq);
Jiang Liu18e48552014-06-09 16:19:45 +0800166}
167
Jiang Liud7f3d472014-06-09 16:19:52 +0800168static inline struct irq_domain *mp_ioapic_irqdomain(int ioapic)
169{
170 return ioapics[ioapic].irqdomain;
171}
172
Suresh Siddhac040aae2011-05-18 16:31:38 -0700173int nr_ioapics;
Feng Tang2a4ab642009-07-07 23:01:15 -0400174
Eric W. Biedermana4384df2010-06-08 11:44:32 -0700175/* The one past the highest gsi number used */
176u32 gsi_top;
Eric W. Biederman57773722010-03-30 01:07:10 -0700177
Alexey Starikovskiy584f7342008-04-04 23:41:32 +0400178/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530179struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +0400180
181/* # of MP IRQ source entries */
182int mp_irq_entries;
183
Paul Gortmakerbb8187d2012-05-17 19:06:13 -0400184#ifdef CONFIG_EISA
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +0400185int mp_bus_id_to_type[MAX_MP_BUSSES];
186#endif
187
188DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
189
Yinghai Luefa25592008-08-19 20:50:36 -0700190int skip_ioapic_setup;
191
Henrik Kretzschmar7167d082011-02-22 15:38:05 +0100192/**
193 * disable_ioapic_support() - disables ioapic support at runtime
194 */
195void disable_ioapic_support(void)
Ingo Molnar65a4e572009-01-31 03:36:17 +0100196{
197#ifdef CONFIG_PCI
198 noioapicquirk = 1;
199 noioapicreroute = -1;
200#endif
201 skip_ioapic_setup = 1;
202}
203
Ingo Molnar54168ed2008-08-20 09:07:45 +0200204static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700205{
206 /* disable IO-APIC */
Henrik Kretzschmar7167d082011-02-22 15:38:05 +0100207 disable_ioapic_support();
Yinghai Luefa25592008-08-19 20:50:36 -0700208 return 0;
209}
210early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200211
Feng Tang2d8009b2010-11-19 11:33:35 +0800212/* Will be called in mpparse/acpi/sfi codes for saving IRQ info */
213void mp_save_irq(struct mpc_intsrc *m)
214{
215 int i;
216
217 apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
218 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
219 m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
220 m->srcbusirq, m->dstapic, m->dstirq);
221
222 for (i = 0; i < mp_irq_entries; i++) {
Feng Tang0e3fa132010-12-08 15:18:57 +0800223 if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
Feng Tang2d8009b2010-11-19 11:33:35 +0800224 return;
225 }
226
Feng Tang0e3fa132010-12-08 15:18:57 +0800227 memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
Feng Tang2d8009b2010-11-19 11:33:35 +0800228 if (++mp_irq_entries == MAX_IRQ_SOURCES)
229 panic("Max # of irq sources exceeded!!\n");
230}
231
Yinghai Lu7e899412014-10-27 13:21:39 +0800232static void alloc_ioapic_saved_registers(int idx)
233{
234 size_t size;
235
236 if (ioapics[idx].saved_registers)
237 return;
238
239 size = sizeof(struct IO_APIC_route_entry) * ioapics[idx].nr_registers;
240 ioapics[idx].saved_registers = kzalloc(size, GFP_KERNEL);
241 if (!ioapics[idx].saved_registers)
242 pr_err("IOAPIC %d: suspend/resume impossible!\n", idx);
243}
244
Jiang Liu15516a32014-10-27 13:21:46 +0800245static void free_ioapic_saved_registers(int idx)
246{
247 kfree(ioapics[idx].saved_registers);
248 ioapics[idx].saved_registers = NULL;
249}
250
Jiang Liu11d686e2014-10-27 16:12:05 +0800251int __init arch_early_ioapic_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800252{
Jiang Liu13315322015-04-13 14:11:56 +0800253 int i;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800254
Jiang Liu95d76ac2014-06-09 16:19:48 +0800255 if (!nr_legacy_irqs())
Jacob Pan1f912332010-02-05 04:06:56 -0800256 io_apic_irqs = ~0UL;
Jacob Pan1f912332010-02-05 04:06:56 -0800257
Yinghai Lu7e899412014-10-27 13:21:39 +0800258 for_each_ioapic(i)
259 alloc_ioapic_saved_registers(i);
Suresh Siddha4c791852011-05-18 16:31:32 -0700260
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800261 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800262}
263
Linus Torvalds130fe052006-11-01 09:11:00 -0800264struct io_apic {
265 unsigned int index;
266 unsigned int unused[3];
267 unsigned int data;
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700268 unsigned int unused2[11];
269 unsigned int eoi;
Linus Torvalds130fe052006-11-01 09:11:00 -0800270};
271
272static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
273{
274 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Suresh Siddhad5371432011-05-18 16:31:37 -0700275 + (mpc_ioapic_addr(idx) & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800276}
277
Jiang Liuad66e1e2015-04-14 10:29:43 +0800278static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700279{
280 struct io_apic __iomem *io_apic = io_apic_base(apic);
281 writel(vector, &io_apic->eoi);
282}
283
Konrad Rzeszutek Wilk4a8e2a32012-03-28 12:37:36 -0400284unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
Linus Torvalds130fe052006-11-01 09:11:00 -0800285{
286 struct io_apic __iomem *io_apic = io_apic_base(apic);
287 writel(reg, &io_apic->index);
288 return readl(&io_apic->data);
289}
290
Jiang Liu9a93d472015-04-14 10:29:55 +0800291static void io_apic_write(unsigned int apic, unsigned int reg,
292 unsigned int value)
Linus Torvalds130fe052006-11-01 09:11:00 -0800293{
294 struct io_apic __iomem *io_apic = io_apic_base(apic);
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400295
Linus Torvalds130fe052006-11-01 09:11:00 -0800296 writel(reg, &io_apic->index);
297 writel(value, &io_apic->data);
298}
299
Andi Kleencf4c6a22006-09-26 10:52:30 +0200300union entry_union {
301 struct { u32 w1, w2; };
302 struct IO_APIC_route_entry entry;
303};
304
Suresh Siddhae57253a2011-08-25 12:01:12 -0700305static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
306{
307 union entry_union eu;
308
309 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
310 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400311
Suresh Siddhae57253a2011-08-25 12:01:12 -0700312 return eu.entry;
313}
314
Andi Kleencf4c6a22006-09-26 10:52:30 +0200315static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
316{
317 union entry_union eu;
318 unsigned long flags;
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400319
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200320 raw_spin_lock_irqsave(&ioapic_lock, flags);
Suresh Siddhae57253a2011-08-25 12:01:12 -0700321 eu.entry = __ioapic_read_entry(apic, pin);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200322 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400323
Andi Kleencf4c6a22006-09-26 10:52:30 +0200324 return eu.entry;
325}
326
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800327/*
328 * When we write a new IO APIC routing entry, we need to write the high
329 * word first! If the mask bit in the low word is clear, we will enable
330 * the interrupt, and we need to make sure the entry is fully populated
331 * before that happens.
332 */
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400333static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleend15512f2006-12-07 02:14:07 +0100334{
Figo.zhang50a8d4d2009-06-17 22:25:20 +0800335 union entry_union eu = {{0, 0}};
336
Andi Kleend15512f2006-12-07 02:14:07 +0100337 eu.entry = e;
338 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
339 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
340}
341
Thomas Gleixner1a8ce7f2010-10-04 21:08:56 +0200342static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleencf4c6a22006-09-26 10:52:30 +0200343{
344 unsigned long flags;
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400345
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200346 raw_spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100347 __ioapic_write_entry(apic, pin, e);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200348 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800349}
350
351/*
352 * When we mask an IO APIC routing entry, we need to write the low
353 * word first, in order to set the mask bit before we change the
354 * high bits!
355 */
356static void ioapic_mask_entry(int apic, int pin)
357{
358 unsigned long flags;
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800359 union entry_union eu = { .entry.mask = IOAPIC_MASKED };
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800360
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200361 raw_spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200362 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
363 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200364 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
368 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
369 * shared ISA-space IRQs, so we have to support them. We are super
370 * fast in the common case, and fast for shared ISA-space IRQs.
371 */
Jiang Liu44677152015-04-14 10:29:53 +0800372static int __add_pin_to_irq_node(struct mp_chip_data *data,
373 int node, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Yinghai Lua178b872014-10-27 16:11:55 +0800375 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Cyrill Gorcunov2977fb32009-08-01 11:47:59 +0400377 /* don't allow duplicates */
Jiang Liu44677152015-04-14 10:29:53 +0800378 for_each_irq_pin(entry, data->irq_2_pin)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700379 if (entry->apic == apic && entry->pin == pin)
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400380 return 0;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700381
Jiang Liu44677152015-04-14 10:29:53 +0800382 entry = kzalloc_node(sizeof(struct irq_pin_list), GFP_ATOMIC, node);
Cyrill Gorcunova7428cd2009-08-01 11:48:00 +0400383 if (!entry) {
Joe Perchesc767a542012-05-21 19:50:07 -0700384 pr_err("can not alloc irq_pin_list (%d,%d,%d)\n",
385 node, apic, pin);
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400386 return -ENOMEM;
Cyrill Gorcunova7428cd2009-08-01 11:48:00 +0400387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 entry->apic = apic;
389 entry->pin = pin;
Jiang Liu44677152015-04-14 10:29:53 +0800390 list_add_tail(&entry->list, &data->irq_2_pin);
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700391
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400392 return 0;
393}
394
Jiang Liu44677152015-04-14 10:29:53 +0800395static void __remove_pin_from_irq(struct mp_chip_data *data, int apic, int pin)
Jiang Liudf334be2014-06-09 16:20:06 +0800396{
Yinghai Lua178b872014-10-27 16:11:55 +0800397 struct irq_pin_list *tmp, *entry;
Jiang Liudf334be2014-06-09 16:20:06 +0800398
Jiang Liu44677152015-04-14 10:29:53 +0800399 list_for_each_entry_safe(entry, tmp, &data->irq_2_pin, list)
Jiang Liudf334be2014-06-09 16:20:06 +0800400 if (entry->apic == apic && entry->pin == pin) {
Yinghai Lua178b872014-10-27 16:11:55 +0800401 list_del(&entry->list);
Jiang Liudf334be2014-06-09 16:20:06 +0800402 kfree(entry);
403 return;
Jiang Liudf334be2014-06-09 16:20:06 +0800404 }
405}
406
Jiang Liu44677152015-04-14 10:29:53 +0800407static void add_pin_to_irq_node(struct mp_chip_data *data,
408 int node, int apic, int pin)
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400409{
Jiang Liu44677152015-04-14 10:29:53 +0800410 if (__add_pin_to_irq_node(data, node, apic, pin))
Cyrill Gorcunovf3d19152009-08-06 00:09:31 +0400411 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/*
415 * Reroute an IRQ to a different pin.
416 */
Jiang Liu44677152015-04-14 10:29:53 +0800417static void __init replace_pin_at_irq_node(struct mp_chip_data *data, int node,
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700418 int oldapic, int oldpin,
419 int newapic, int newpin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Jeremy Fitzhardinge535b6422009-06-08 03:29:26 -0700421 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jiang Liu44677152015-04-14 10:29:53 +0800423 for_each_irq_pin(entry, data->irq_2_pin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (entry->apic == oldapic && entry->pin == oldpin) {
425 entry->apic = newapic;
426 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700427 /* every one is different, right? */
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700428 return;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700431
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700432 /* old apic/pin didn't exist, so just add new ones */
Jiang Liu44677152015-04-14 10:29:53 +0800433 add_pin_to_irq_node(data, node, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Jiang Liu44677152015-04-14 10:29:53 +0800436static void io_apic_modify_irq(struct mp_chip_data *data,
Jeremy Fitzhardinge2f210de2009-06-08 02:55:22 -0700437 int mask_and, int mask_or,
438 void (*final)(struct irq_pin_list *entry))
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400439{
Jiang Liu0be275e2015-04-14 10:29:57 +0800440 union entry_union eu;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400441 struct irq_pin_list *entry;
442
Jiang Liu0be275e2015-04-14 10:29:57 +0800443 eu.entry = data->entry;
444 eu.w1 &= mask_and;
445 eu.w1 |= mask_or;
446 data->entry = eu.entry;
447
448 for_each_irq_pin(entry, data->irq_2_pin) {
449 io_apic_write(entry->apic, 0x10 + 2 * entry->pin, eu.w1);
450 if (final)
451 final(entry);
452 }
Suresh Siddhac29d9db2009-12-01 15:31:16 -0800453}
454
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530455static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700456{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400457 /*
458 * Synchronize the IO-APIC and the CPU by doing
459 * a dummy read from the IO-APIC
460 */
461 struct io_apic __iomem *io_apic;
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -0400462
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400463 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700464 readl(&io_apic->data);
465}
466
Jiang Liu44677152015-04-14 10:29:53 +0800467static void mask_ioapic_irq(struct irq_data *irq_data)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400468{
Jiang Liu44677152015-04-14 10:29:53 +0800469 struct mp_chip_data *data = irq_data->chip_data;
Thomas Gleixnerdd5f15e2010-09-28 15:18:35 +0200470 unsigned long flags;
471
472 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +0800473 io_apic_modify_irq(data, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200474 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Jiang Liu44677152015-04-14 10:29:53 +0800477static void __unmask_ioapic(struct mp_chip_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Jiang Liu44677152015-04-14 10:29:53 +0800479 io_apic_modify_irq(data, ~IO_APIC_REDIR_MASKED, 0, NULL);
Thomas Gleixnerdd5f15e2010-09-28 15:18:35 +0200480}
481
Jiang Liu44677152015-04-14 10:29:53 +0800482static void unmask_ioapic_irq(struct irq_data *irq_data)
Thomas Gleixnerdd5f15e2010-09-28 15:18:35 +0200483{
Jiang Liu44677152015-04-14 10:29:53 +0800484 struct mp_chip_data *data = irq_data->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned long flags;
486
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200487 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +0800488 __unmask_ioapic(data);
Thomas Gleixnerdade7712009-07-25 18:39:36 +0200489 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Suresh Siddhac0205702011-08-25 12:01:13 -0700492/*
493 * IO-APIC versions below 0x20 don't support EOI register.
494 * For the record, here is the information about various versions:
495 * 0Xh 82489DX
496 * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
497 * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
498 * 30h-FFh Reserved
499 *
500 * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
501 * version as 0x2. This is an error with documentation and these ICH chips
502 * use io-apic's of version 0x20.
503 *
504 * For IO-APIC's with EOI register, we use that to do an explicit EOI.
505 * Otherwise, we simulate the EOI message manually by changing the trigger
506 * mode to edge and then back to level, with RTE being masked during this.
507 */
Jiang Liuad66e1e2015-04-14 10:29:43 +0800508static void __eoi_ioapic_pin(int apic, int pin, int vector)
Suresh Siddhac0205702011-08-25 12:01:13 -0700509{
510 if (mpc_ioapic_ver(apic) >= 0x20) {
Joerg Roedelda165322012-09-26 12:44:50 +0200511 io_apic_eoi(apic, vector);
Suresh Siddhac0205702011-08-25 12:01:13 -0700512 } else {
513 struct IO_APIC_route_entry entry, entry1;
514
515 entry = entry1 = __ioapic_read_entry(apic, pin);
516
517 /*
518 * Mask the entry and change the trigger mode to edge.
519 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800520 entry1.mask = IOAPIC_MASKED;
Suresh Siddhac0205702011-08-25 12:01:13 -0700521 entry1.trigger = IOAPIC_EDGE;
522
523 __ioapic_write_entry(apic, pin, entry1);
524
525 /*
526 * Restore the previous level triggered entry.
527 */
528 __ioapic_write_entry(apic, pin, entry);
529 }
530}
531
Andy Shevchenko4faefda2015-10-09 17:24:45 +0300532static void eoi_ioapic_pin(int vector, struct mp_chip_data *data)
Jiang Liud32932d2015-04-13 14:11:59 +0800533{
534 unsigned long flags;
535 struct irq_pin_list *entry;
536
537 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +0800538 for_each_irq_pin(entry, data->irq_2_pin)
Jiang Liuad66e1e2015-04-14 10:29:43 +0800539 __eoi_ioapic_pin(entry->apic, entry->pin, vector);
Suresh Siddhac0205702011-08-25 12:01:13 -0700540 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
544{
545 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200548 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (entry.delivery_mode == dest_SMI)
550 return;
Suresh Siddha1e75b312011-08-25 12:01:11 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /*
Suresh Siddha1e75b312011-08-25 12:01:11 -0700553 * Make sure the entry is masked and re-read the contents to check
554 * if it is a level triggered pin and if the remote-IRR is set.
555 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800556 if (entry.mask == IOAPIC_UNMASKED) {
557 entry.mask = IOAPIC_MASKED;
Suresh Siddha1e75b312011-08-25 12:01:11 -0700558 ioapic_write_entry(apic, pin, entry);
559 entry = ioapic_read_entry(apic, pin);
560 }
561
562 if (entry.irr) {
Suresh Siddhac0205702011-08-25 12:01:13 -0700563 unsigned long flags;
564
Suresh Siddha1e75b312011-08-25 12:01:11 -0700565 /*
566 * Make sure the trigger mode is set to level. Explicit EOI
567 * doesn't clear the remote-IRR if the trigger mode is not
568 * set to level.
569 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800570 if (entry.trigger == IOAPIC_EDGE) {
Suresh Siddha1e75b312011-08-25 12:01:11 -0700571 entry.trigger = IOAPIC_LEVEL;
572 ioapic_write_entry(apic, pin, entry);
573 }
Suresh Siddhac0205702011-08-25 12:01:13 -0700574 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liuad66e1e2015-04-14 10:29:43 +0800575 __eoi_ioapic_pin(apic, pin, entry.vector);
Suresh Siddhac0205702011-08-25 12:01:13 -0700576 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Suresh Siddha1e75b312011-08-25 12:01:11 -0700577 }
578
579 /*
580 * Clear the rest of the bits in the IO-APIC RTE except for the mask
581 * bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800583 ioapic_mask_entry(apic, pin);
Suresh Siddha1e75b312011-08-25 12:01:11 -0700584 entry = ioapic_read_entry(apic, pin);
585 if (entry.irr)
Joe Perchesc767a542012-05-21 19:50:07 -0700586 pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
Suresh Siddha1e75b312011-08-25 12:01:11 -0700587 mpc_ioapic_id(apic), pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Ingo Molnar54168ed2008-08-20 09:07:45 +0200590static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 int apic, pin;
593
Jiang Liuf44d1692014-06-09 16:19:42 +0800594 for_each_ioapic_pin(apic, pin)
595 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Ingo Molnar54168ed2008-08-20 09:07:45 +0200598#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
601 * specific CPU-side IRQs.
602 */
603
604#define MAX_PIRQS 8
Yinghai Lu3bd25d02009-02-15 02:54:03 -0800605static int pirq_entries[MAX_PIRQS] = {
606 [0 ... MAX_PIRQS - 1] = -1
607};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609static int __init ioapic_pirq_setup(char *str)
610{
611 int i, max;
612 int ints[MAX_PIRQS+1];
613
614 get_options(str, ARRAY_SIZE(ints), ints);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 apic_printk(APIC_VERBOSE, KERN_INFO
617 "PIRQ redirection, working around broken MP-BIOS.\n");
618 max = MAX_PIRQS;
619 if (ints[0] < MAX_PIRQS)
620 max = ints[0];
621
622 for (i = 0; i < max; i++) {
623 apic_printk(APIC_VERBOSE, KERN_DEBUG
624 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
625 /*
626 * PIRQs are mapped upside down, usually.
627 */
628 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
629 }
630 return 1;
631}
632
633__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200634#endif /* CONFIG_X86_32 */
635
Ingo Molnar54168ed2008-08-20 09:07:45 +0200636/*
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700637 * Saves all the IO-APIC RTE's
Ingo Molnar54168ed2008-08-20 09:07:45 +0200638 */
Suresh Siddha31dce142011-05-18 16:31:33 -0700639int save_ioapic_entries(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200640{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200641 int apic, pin;
Suresh Siddha31dce142011-05-18 16:31:33 -0700642 int err = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200643
Jiang Liuf44d1692014-06-09 16:19:42 +0800644 for_each_ioapic(apic) {
Suresh Siddha57a6f742011-05-18 16:31:36 -0700645 if (!ioapics[apic].saved_registers) {
Suresh Siddha31dce142011-05-18 16:31:33 -0700646 err = -ENOMEM;
647 continue;
648 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200649
Jiang Liuf44d1692014-06-09 16:19:42 +0800650 for_each_pin(apic, pin)
Suresh Siddha57a6f742011-05-18 16:31:36 -0700651 ioapics[apic].saved_registers[pin] =
Ingo Molnar54168ed2008-08-20 09:07:45 +0200652 ioapic_read_entry(apic, pin);
Fenghua Yub24696b2009-03-27 14:22:44 -0700653 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400654
Suresh Siddha31dce142011-05-18 16:31:33 -0700655 return err;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200656}
657
Fenghua Yub24696b2009-03-27 14:22:44 -0700658/*
659 * Mask all IO APIC entries.
660 */
Suresh Siddha31dce142011-05-18 16:31:33 -0700661void mask_ioapic_entries(void)
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700662{
663 int apic, pin;
664
Jiang Liuf44d1692014-06-09 16:19:42 +0800665 for_each_ioapic(apic) {
Suresh Siddha2f344d22011-05-24 10:45:31 -0700666 if (!ioapics[apic].saved_registers)
Suresh Siddha31dce142011-05-18 16:31:33 -0700667 continue;
Fenghua Yub24696b2009-03-27 14:22:44 -0700668
Jiang Liuf44d1692014-06-09 16:19:42 +0800669 for_each_pin(apic, pin) {
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700670 struct IO_APIC_route_entry entry;
671
Suresh Siddha57a6f742011-05-18 16:31:36 -0700672 entry = ioapics[apic].saved_registers[pin];
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800673 if (entry.mask == IOAPIC_UNMASKED) {
674 entry.mask = IOAPIC_MASKED;
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700675 ioapic_write_entry(apic, pin, entry);
676 }
677 }
678 }
679}
680
Fenghua Yub24696b2009-03-27 14:22:44 -0700681/*
Suresh Siddha57a6f742011-05-18 16:31:36 -0700682 * Restore IO APIC entries which was saved in the ioapic structure.
Fenghua Yub24696b2009-03-27 14:22:44 -0700683 */
Suresh Siddha31dce142011-05-18 16:31:33 -0700684int restore_ioapic_entries(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200685{
686 int apic, pin;
687
Jiang Liuf44d1692014-06-09 16:19:42 +0800688 for_each_ioapic(apic) {
Suresh Siddha2f344d22011-05-24 10:45:31 -0700689 if (!ioapics[apic].saved_registers)
Suresh Siddha31dce142011-05-18 16:31:33 -0700690 continue;
Fenghua Yub24696b2009-03-27 14:22:44 -0700691
Jiang Liuf44d1692014-06-09 16:19:42 +0800692 for_each_pin(apic, pin)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200693 ioapic_write_entry(apic, pin,
Suresh Siddha57a6f742011-05-18 16:31:36 -0700694 ioapics[apic].saved_registers[pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400695 }
Fenghua Yub24696b2009-03-27 14:22:44 -0700696 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/*
700 * Find the IRQ entry number of a certain pin.
701 */
Yinghai Lu6f50d452011-10-12 00:33:48 -0700702static int find_irq_entry(int ioapic_idx, int pin, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 int i;
705
706 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530707 if (mp_irqs[i].irqtype == type &&
Yinghai Lu6f50d452011-10-12 00:33:48 -0700708 (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530709 mp_irqs[i].dstapic == MP_APIC_ALL) &&
710 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return i;
712
713 return -1;
714}
715
716/*
717 * Find the pin to which IRQ[irq] (ISA) is connected
718 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800719static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 int i;
722
723 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530724 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300726 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530727 (mp_irqs[i].irqtype == type) &&
728 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530730 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732 return -1;
733}
734
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800735static int __init find_isa_irq_apic(int irq, int type)
736{
737 int i;
738
739 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530740 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800741
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300742 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530743 (mp_irqs[i].irqtype == type) &&
744 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800745 break;
746 }
Yinghai Lu6f50d452011-10-12 00:33:48 -0700747
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800748 if (i < mp_irq_entries) {
Yinghai Lu6f50d452011-10-12 00:33:48 -0700749 int ioapic_idx;
750
Jiang Liuf44d1692014-06-09 16:19:42 +0800751 for_each_ioapic(ioapic_idx)
Yinghai Lu6f50d452011-10-12 00:33:48 -0700752 if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
753 return ioapic_idx;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800754 }
755
756 return -1;
757}
758
Paul Gortmakerbb8187d2012-05-17 19:06:13 -0400759#ifdef CONFIG_EISA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/*
761 * EISA Edge/Level control register, ELCR
762 */
763static int EISA_ELCR(unsigned int irq)
764{
Jiang Liu95d76ac2014-06-09 16:19:48 +0800765 if (irq < nr_legacy_irqs()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 unsigned int port = 0x4d0 + (irq >> 3);
767 return (inb(port) >> (irq & 7)) & 1;
768 }
769 apic_printk(APIC_VERBOSE, KERN_INFO
770 "Broken MPtable reports ISA irq %d\n", irq);
771 return 0;
772}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200773
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300774#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800776/* ISA interrupts are always active high edge triggered,
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300777 * when listed as conforming in the MP table. */
778
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800779#define default_ISA_trigger(idx) (IOAPIC_EDGE)
780#define default_ISA_polarity(idx) (IOAPIC_POL_HIGH)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/* EISA interrupts are always polarity zero and can be edge or level
783 * trigger depending on the ELCR value. If an interrupt is listed as
784 * EISA conforming in the MP table, that means its trigger type must
785 * be read in from the ELCR */
786
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530787#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300788#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800790/* PCI interrupts are always active low level triggered,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * when listed as conforming in the MP table. */
792
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800793#define default_PCI_trigger(idx) (IOAPIC_LEVEL)
794#define default_PCI_polarity(idx) (IOAPIC_POL_LOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Thomas Gleixnerb77cf6a2011-02-23 17:33:53 +0100796static int irq_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530798 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /*
801 * Determine IRQ line polarity (high active or low active):
802 */
Thomas Gleixnerab760852015-04-14 10:30:07 +0800803 switch (mp_irqs[idx].irqflag & 0x03) {
804 case 0:
805 /* conforms to spec, ie. bus-type dependent polarity */
806 if (test_bit(bus, mp_bus_not_pci))
807 return default_ISA_polarity(idx);
808 else
809 return default_PCI_polarity(idx);
810 case 1:
811 return IOAPIC_POL_HIGH;
812 case 2:
813 pr_warn("IOAPIC: Invalid polarity: 2, defaulting to low\n");
814 case 3:
815 default: /* Pointless default required due to do gcc stupidity */
816 return IOAPIC_POL_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Thomas Gleixnerab760852015-04-14 10:30:07 +0800820#ifdef CONFIG_EISA
821static int eisa_irq_trigger(int idx, int bus, int trigger)
822{
823 switch (mp_bus_id_to_type[bus]) {
824 case MP_BUS_PCI:
825 case MP_BUS_ISA:
826 return trigger;
827 case MP_BUS_EISA:
828 return default_EISA_trigger(idx);
829 }
830 pr_warn("IOAPIC: Invalid srcbus: %d defaulting to level\n", bus);
831 return IOAPIC_LEVEL;
832}
833#else
834static inline int eisa_irq_trigger(int idx, int bus, int trigger)
835{
836 return trigger;
837}
838#endif
839
Thomas Gleixnerb77cf6a2011-02-23 17:33:53 +0100840static int irq_trigger(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530842 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int trigger;
844
845 /*
846 * Determine IRQ trigger mode (edge or level sensitive):
847 */
Thomas Gleixnerab760852015-04-14 10:30:07 +0800848 switch ((mp_irqs[idx].irqflag >> 2) & 0x03) {
849 case 0:
850 /* conforms to spec, ie. bus-type dependent trigger mode */
851 if (test_bit(bus, mp_bus_not_pci))
852 trigger = default_ISA_trigger(idx);
853 else
854 trigger = default_PCI_trigger(idx);
855 /* Take EISA into account */
856 return eisa_irq_trigger(idx, bus, trigger);
857 case 1:
858 return IOAPIC_EDGE;
859 case 2:
860 pr_warn("IOAPIC: Invalid trigger mode 2 defaulting to level\n");
861 case 3:
862 default: /* Pointless default required due to do gcc stupidity */
863 return IOAPIC_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Jiang Liuc4d05a22015-04-13 14:11:54 +0800867void ioapic_set_alloc_attr(struct irq_alloc_info *info, int node,
868 int trigger, int polarity)
869{
870 init_irq_alloc_info(info, NULL);
871 info->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
872 info->ioapic_node = node;
873 info->ioapic_trigger = trigger;
874 info->ioapic_polarity = polarity;
875 info->ioapic_valid = 1;
876}
877
Jiang Liu96ed44b2015-04-13 14:11:58 +0800878#ifndef CONFIG_ACPI
879int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity);
880#endif
881
882static void ioapic_copy_alloc_attr(struct irq_alloc_info *dst,
883 struct irq_alloc_info *src,
884 u32 gsi, int ioapic_idx, int pin)
885{
886 int trigger, polarity;
887
888 copy_irq_alloc_info(dst, src);
889 dst->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
890 dst->ioapic_id = mpc_ioapic_id(ioapic_idx);
891 dst->ioapic_pin = pin;
892 dst->ioapic_valid = 1;
893 if (src && src->ioapic_valid) {
894 dst->ioapic_node = src->ioapic_node;
895 dst->ioapic_trigger = src->ioapic_trigger;
896 dst->ioapic_polarity = src->ioapic_polarity;
897 } else {
898 dst->ioapic_node = NUMA_NO_NODE;
899 if (acpi_get_override_irq(gsi, &trigger, &polarity) >= 0) {
900 dst->ioapic_trigger = trigger;
901 dst->ioapic_polarity = polarity;
902 } else {
903 /*
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800904 * PCI interrupts are always active low level
Jiang Liu96ed44b2015-04-13 14:11:58 +0800905 * triggered.
906 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +0800907 dst->ioapic_trigger = IOAPIC_LEVEL;
908 dst->ioapic_polarity = IOAPIC_POL_LOW;
Jiang Liu96ed44b2015-04-13 14:11:58 +0800909 }
910 }
911}
912
913static int ioapic_alloc_attr_node(struct irq_alloc_info *info)
914{
915 return (info && info->ioapic_valid) ? info->ioapic_node : NUMA_NO_NODE;
916}
917
Jiang Liu49c7e602015-04-13 14:11:55 +0800918static void mp_register_handler(unsigned int irq, unsigned long trigger)
919{
920 irq_flow_handler_t hdl;
921 bool fasteoi;
922
923 if (trigger) {
924 irq_set_status_flags(irq, IRQ_LEVEL);
925 fasteoi = true;
926 } else {
927 irq_clear_status_flags(irq, IRQ_LEVEL);
928 fasteoi = false;
929 }
930
931 hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
932 __irq_set_handler(irq, hdl, 0, fasteoi ? "fasteoi" : "edge");
933}
934
Jiang Liu96ed44b2015-04-13 14:11:58 +0800935static bool mp_check_pin_attr(int irq, struct irq_alloc_info *info)
936{
937 struct mp_chip_data *data = irq_get_chip_data(irq);
938
939 /*
940 * setup_IO_APIC_irqs() programs all legacy IRQs with default trigger
941 * and polarity attirbutes. So allow the first user to reprogram the
942 * pin with real trigger and polarity attributes.
943 */
944 if (irq < nr_legacy_irqs() && data->count == 1) {
945 if (info->ioapic_trigger != data->trigger)
Jiang Liu646c4b72015-07-30 15:51:32 +0800946 mp_register_handler(irq, info->ioapic_trigger);
Jiang Liu96ed44b2015-04-13 14:11:58 +0800947 data->entry.trigger = data->trigger = info->ioapic_trigger;
948 data->entry.polarity = data->polarity = info->ioapic_polarity;
949 }
950
951 return data->trigger == info->ioapic_trigger &&
952 data->polarity == info->ioapic_polarity;
953}
954
Jiang Liud32932d2015-04-13 14:11:59 +0800955static int alloc_irq_from_domain(struct irq_domain *domain, int ioapic, u32 gsi,
Jiang Liuc4d05a22015-04-13 14:11:54 +0800956 struct irq_alloc_info *info)
Jiang Liu6b9fb702014-06-10 14:13:25 +0800957{
Jiang Liud32932d2015-04-13 14:11:59 +0800958 bool legacy = false;
Jiang Liud7f3d472014-06-09 16:19:52 +0800959 int irq = -1;
Jiang Liud7f3d472014-06-09 16:19:52 +0800960 int type = ioapics[ioapic].irqdomain_cfg.type;
961
962 switch (type) {
963 case IOAPIC_DOMAIN_LEGACY:
964 /*
Jiang Liud32932d2015-04-13 14:11:59 +0800965 * Dynamically allocate IRQ number for non-ISA IRQs in the first
966 * 16 GSIs on some weird platforms.
Jiang Liud7f3d472014-06-09 16:19:52 +0800967 */
Jiang Liud32932d2015-04-13 14:11:59 +0800968 if (!ioapic_initialized || gsi >= nr_legacy_irqs())
Jiang Liud7f3d472014-06-09 16:19:52 +0800969 irq = gsi;
Jiang Liud32932d2015-04-13 14:11:59 +0800970 legacy = mp_is_legacy_irq(irq);
Jiang Liud7f3d472014-06-09 16:19:52 +0800971 break;
972 case IOAPIC_DOMAIN_STRICT:
Jiang Liud32932d2015-04-13 14:11:59 +0800973 irq = gsi;
Jiang Liud7f3d472014-06-09 16:19:52 +0800974 break;
975 case IOAPIC_DOMAIN_DYNAMIC:
Jiang Liud7f3d472014-06-09 16:19:52 +0800976 break;
977 default:
978 WARN(1, "ioapic: unknown irqdomain type %d\n", type);
Jiang Liud32932d2015-04-13 14:11:59 +0800979 return -1;
Jiang Liud7f3d472014-06-09 16:19:52 +0800980 }
981
Jiang Liud32932d2015-04-13 14:11:59 +0800982 return __irq_domain_alloc_irqs(domain, irq, 1,
983 ioapic_alloc_attr_node(info),
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900984 info, legacy, NULL);
Jiang Liud32932d2015-04-13 14:11:59 +0800985}
986
987/*
988 * Need special handling for ISA IRQs because there may be multiple IOAPIC pins
989 * sharing the same ISA IRQ number and irqdomain only supports 1:1 mapping
990 * between IOAPIC pin and IRQ number. A typical IOAPIC has 24 pins, pin 0-15 are
991 * used for legacy IRQs and pin 16-23 are used for PCI IRQs (PIRQ A-H).
992 * When ACPI is disabled, only legacy IRQ numbers (IRQ0-15) are available, and
993 * some BIOSes may use MP Interrupt Source records to override IRQ numbers for
994 * PIRQs instead of reprogramming the interrupt routing logic. Thus there may be
995 * multiple pins sharing the same legacy IRQ number when ACPI is disabled.
996 */
997static int alloc_isa_irq_from_domain(struct irq_domain *domain,
998 int irq, int ioapic, int pin,
999 struct irq_alloc_info *info)
1000{
1001 struct mp_chip_data *data;
1002 struct irq_data *irq_data = irq_get_irq_data(irq);
1003 int node = ioapic_alloc_attr_node(info);
1004
1005 /*
1006 * Legacy ISA IRQ has already been allocated, just add pin to
1007 * the pin list assoicated with this IRQ and program the IOAPIC
1008 * entry. The IOAPIC entry
1009 */
1010 if (irq_data && irq_data->parent_data) {
Jiang Liud32932d2015-04-13 14:11:59 +08001011 if (!mp_check_pin_attr(irq, info))
1012 return -EBUSY;
Jiang Liu44677152015-04-14 10:29:53 +08001013 if (__add_pin_to_irq_node(irq_data->chip_data, node, ioapic,
1014 info->ioapic_pin))
Jiang Liud32932d2015-04-13 14:11:59 +08001015 return -ENOMEM;
1016 } else {
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001017 irq = __irq_domain_alloc_irqs(domain, irq, 1, node, info, true,
1018 NULL);
Jiang Liud32932d2015-04-13 14:11:59 +08001019 if (irq >= 0) {
1020 irq_data = irq_domain_get_irq_data(domain, irq);
1021 data = irq_data->chip_data;
1022 data->isa_irq = true;
1023 }
1024 }
1025
1026 return irq;
Jiang Liu6b9fb702014-06-10 14:13:25 +08001027}
1028
Jiang Liud7f3d472014-06-09 16:19:52 +08001029static int mp_map_pin_to_irq(u32 gsi, int idx, int ioapic, int pin,
Jiang Liuc4d05a22015-04-13 14:11:54 +08001030 unsigned int flags, struct irq_alloc_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Eric W. Biedermand4642072010-03-30 01:07:13 -07001032 int irq;
Jiang Liud32932d2015-04-13 14:11:59 +08001033 bool legacy = false;
1034 struct irq_alloc_info tmp;
1035 struct mp_chip_data *data;
Jiang Liud7f3d472014-06-09 16:19:52 +08001036 struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
1037
Jiang Liub81975e2014-06-09 16:20:11 +08001038 if (!domain)
Jiang Liud32932d2015-04-13 14:11:59 +08001039 return -ENOSYS;
Jiang Liu16ee7b32014-06-09 16:20:04 +08001040
Jiang Liu16ee7b32014-06-09 16:20:04 +08001041 if (idx >= 0 && test_bit(mp_irqs[idx].srcbus, mp_bus_not_pci)) {
1042 irq = mp_irqs[idx].srcbusirq;
Jiang Liud32932d2015-04-13 14:11:59 +08001043 legacy = mp_is_legacy_irq(irq);
1044 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001045
Jiang Liud32932d2015-04-13 14:11:59 +08001046 mutex_lock(&ioapic_mutex);
1047 if (!(flags & IOAPIC_MAP_ALLOC)) {
1048 if (!legacy) {
1049 irq = irq_find_mapping(domain, pin);
Jiang Liu16ee7b32014-06-09 16:20:04 +08001050 if (irq == 0)
Jiang Liud32932d2015-04-13 14:11:59 +08001051 irq = -ENOENT;
Jiang Liu16ee7b32014-06-09 16:20:04 +08001052 }
1053 } else {
Jiang Liud32932d2015-04-13 14:11:59 +08001054 ioapic_copy_alloc_attr(&tmp, info, gsi, ioapic, pin);
1055 if (legacy)
1056 irq = alloc_isa_irq_from_domain(domain, irq,
1057 ioapic, pin, &tmp);
1058 else if ((irq = irq_find_mapping(domain, pin)) == 0)
1059 irq = alloc_irq_from_domain(domain, ioapic, gsi, &tmp);
1060 else if (!mp_check_pin_attr(irq, &tmp))
1061 irq = -EBUSY;
1062 if (irq >= 0) {
1063 data = irq_get_chip_data(irq);
1064 data->count++;
1065 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001066 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001067 mutex_unlock(&ioapic_mutex);
1068
Jiang Liud32932d2015-04-13 14:11:59 +08001069 return irq;
Jiang Liud7f3d472014-06-09 16:19:52 +08001070}
1071
1072static int pin_2_irq(int idx, int ioapic, int pin, unsigned int flags)
1073{
1074 u32 gsi = mp_pin_to_gsi(ioapic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /*
1077 * Debugging check, we are in big trouble if this message pops up!
1078 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301079 if (mp_irqs[idx].dstirq != pin)
Joe Perchesc767a542012-05-21 19:50:07 -07001080 pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Ingo Molnar54168ed2008-08-20 09:07:45 +02001082#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1085 */
1086 if ((pin >= 16) && (pin <= 23)) {
1087 if (pirq_entries[pin-16] != -1) {
1088 if (!pirq_entries[pin-16]) {
1089 apic_printk(APIC_VERBOSE, KERN_DEBUG
1090 "disabling PIRQ%d\n", pin-16);
1091 } else {
Jiang Liud7f3d472014-06-09 16:19:52 +08001092 int irq = pirq_entries[pin-16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 apic_printk(APIC_VERBOSE, KERN_DEBUG
1094 "using PIRQ%d -> IRQ %d\n",
1095 pin-16, irq);
Jiang Liu6b9fb702014-06-10 14:13:25 +08001096 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 }
1099 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001100#endif
1101
Jiang Liuc4d05a22015-04-13 14:11:54 +08001102 return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, NULL);
Jiang Liud7f3d472014-06-09 16:19:52 +08001103}
Jiang Liu6b9fb702014-06-10 14:13:25 +08001104
Jiang Liu154d9e52015-04-14 10:29:56 +08001105int mp_map_gsi_to_irq(u32 gsi, unsigned int flags, struct irq_alloc_info *info)
Jiang Liud7f3d472014-06-09 16:19:52 +08001106{
1107 int ioapic, pin, idx;
1108
1109 ioapic = mp_find_ioapic(gsi);
1110 if (ioapic < 0)
1111 return -1;
1112
1113 pin = mp_find_ioapic_pin(ioapic, gsi);
1114 idx = find_irq_entry(ioapic, pin, mp_INT);
1115 if ((flags & IOAPIC_MAP_CHECK) && idx < 0)
1116 return -1;
1117
Jiang Liuc4d05a22015-04-13 14:11:54 +08001118 return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Jiang Liudf334be2014-06-09 16:20:06 +08001121void mp_unmap_irq(int irq)
1122{
Jiang Liud32932d2015-04-13 14:11:59 +08001123 struct irq_data *irq_data = irq_get_irq_data(irq);
1124 struct mp_chip_data *data;
Jiang Liudf334be2014-06-09 16:20:06 +08001125
Jiang Liud32932d2015-04-13 14:11:59 +08001126 if (!irq_data || !irq_data->domain)
Jiang Liudf334be2014-06-09 16:20:06 +08001127 return;
1128
Jiang Liud32932d2015-04-13 14:11:59 +08001129 data = irq_data->chip_data;
1130 if (!data || data->isa_irq)
1131 return;
Jiang Liudf334be2014-06-09 16:20:06 +08001132
1133 mutex_lock(&ioapic_mutex);
Jiang Liud32932d2015-04-13 14:11:59 +08001134 if (--data->count == 0)
1135 irq_domain_free_irqs(irq, 1);
Jiang Liudf334be2014-06-09 16:20:06 +08001136 mutex_unlock(&ioapic_mutex);
1137}
1138
Yinghai Lue20c06f2009-05-06 10:08:22 -07001139/*
1140 * Find a specific PCI IRQ entry.
1141 * Not an __init, possibly needed by modules
1142 */
Jiang Liu25d0d352014-10-27 16:11:54 +08001143int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
Yinghai Lue20c06f2009-05-06 10:08:22 -07001144{
Jiang Liud7f3d472014-06-09 16:19:52 +08001145 int irq, i, best_ioapic = -1, best_idx = -1;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001146
1147 apic_printk(APIC_DEBUG,
1148 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1149 bus, slot, pin);
1150 if (test_bit(bus, mp_bus_not_pci)) {
1151 apic_printk(APIC_VERBOSE,
1152 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1153 return -1;
1154 }
Jiang Liu79598502014-06-09 16:19:44 +08001155
Yinghai Lue20c06f2009-05-06 10:08:22 -07001156 for (i = 0; i < mp_irq_entries; i++) {
1157 int lbus = mp_irqs[i].srcbus;
Jiang Liu79598502014-06-09 16:19:44 +08001158 int ioapic_idx, found = 0;
1159
1160 if (bus != lbus || mp_irqs[i].irqtype != mp_INT ||
1161 slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f))
1162 continue;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001163
Jiang Liuf44d1692014-06-09 16:19:42 +08001164 for_each_ioapic(ioapic_idx)
Yinghai Lu6f50d452011-10-12 00:33:48 -07001165 if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
Jiang Liu79598502014-06-09 16:19:44 +08001166 mp_irqs[i].dstapic == MP_APIC_ALL) {
1167 found = 1;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001168 break;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001169 }
Jiang Liu79598502014-06-09 16:19:44 +08001170 if (!found)
1171 continue;
1172
1173 /* Skip ISA IRQs */
Jiang Liud7f3d472014-06-09 16:19:52 +08001174 irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq, 0);
1175 if (irq > 0 && !IO_APIC_IRQ(irq))
Jiang Liu79598502014-06-09 16:19:44 +08001176 continue;
1177
1178 if (pin == (mp_irqs[i].srcbusirq & 3)) {
Jiang Liud7f3d472014-06-09 16:19:52 +08001179 best_idx = i;
1180 best_ioapic = ioapic_idx;
1181 goto out;
Jiang Liu79598502014-06-09 16:19:44 +08001182 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001183
Jiang Liu79598502014-06-09 16:19:44 +08001184 /*
1185 * Use the first all-but-pin matching entry as a
1186 * best-guess fuzzy result for broken mptables.
1187 */
Jiang Liud7f3d472014-06-09 16:19:52 +08001188 if (best_idx < 0) {
1189 best_idx = i;
1190 best_ioapic = ioapic_idx;
Yinghai Lue20c06f2009-05-06 10:08:22 -07001191 }
1192 }
Jiang Liud7f3d472014-06-09 16:19:52 +08001193 if (best_idx < 0)
1194 return -1;
1195
1196out:
Jiang Liu25d0d352014-10-27 16:11:54 +08001197 return pin_2_irq(best_idx, best_ioapic, mp_irqs[best_idx].dstirq,
1198 IOAPIC_MAP_ALLOC);
Yinghai Lue20c06f2009-05-06 10:08:22 -07001199}
1200EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1201
Jiang Liud32932d2015-04-13 14:11:59 +08001202static struct irq_chip ioapic_chip, ioapic_ir_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001204#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001205static inline int IO_APIC_irq_trigger(int irq)
1206{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001207 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001208
Jiang Liuf44d1692014-06-09 16:19:42 +08001209 for_each_ioapic_pin(apic, pin) {
1210 idx = find_irq_entry(apic, pin, mp_INT);
Jiang Liud7f3d472014-06-09 16:19:52 +08001211 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin, 0)))
Jiang Liuf44d1692014-06-09 16:19:42 +08001212 return irq_trigger(idx);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001213 }
1214 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001215 * nonexistent IRQs are edge default
1216 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001217 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001218}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001219#else
1220static inline int IO_APIC_irq_trigger(int irq)
1221{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001222 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001223}
1224#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001225
Thomas Gleixnered972cc2011-02-23 14:31:36 +01001226static void __init setup_IO_APIC_irqs(void)
1227{
Jiang Liu16ee7b32014-06-09 16:20:04 +08001228 unsigned int ioapic, pin;
1229 int idx;
Thomas Gleixnered972cc2011-02-23 14:31:36 +01001230
1231 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1232
Jiang Liu16ee7b32014-06-09 16:20:04 +08001233 for_each_ioapic_pin(ioapic, pin) {
1234 idx = find_irq_entry(ioapic, pin, mp_INT);
1235 if (idx < 0)
1236 apic_printk(APIC_VERBOSE,
1237 KERN_DEBUG " apic %d pin %d not connected\n",
1238 mpc_ioapic_id(ioapic), pin);
1239 else
1240 pin_2_irq(idx, ioapic, pin,
1241 ioapic ? 0 : IOAPIC_MAP_ALLOC);
1242 }
Thomas Gleixnered972cc2011-02-23 14:31:36 +01001243}
1244
Yoshihiro YUNOMAE17405452013-08-20 16:01:07 +09001245void ioapic_zap_locks(void)
1246{
1247 raw_spin_lock_init(&ioapic_lock);
1248}
1249
Jiang Liua44174e2015-04-13 14:11:57 +08001250static void io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
1251{
1252 int i;
1253 char buf[256];
1254 struct IO_APIC_route_entry entry;
1255 struct IR_IO_APIC_route_entry *ir_entry = (void *)&entry;
1256
1257 printk(KERN_DEBUG "IOAPIC %d:\n", apic);
1258 for (i = 0; i <= nr_entries; i++) {
1259 entry = ioapic_read_entry(apic, i);
1260 snprintf(buf, sizeof(buf),
1261 " pin%02x, %s, %s, %s, V(%02X), IRR(%1d), S(%1d)",
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001262 i,
1263 entry.mask == IOAPIC_MASKED ? "disabled" : "enabled ",
1264 entry.trigger == IOAPIC_LEVEL ? "level" : "edge ",
1265 entry.polarity == IOAPIC_POL_LOW ? "low " : "high",
Jiang Liua44174e2015-04-13 14:11:57 +08001266 entry.vector, entry.irr, entry.delivery_status);
1267 if (ir_entry->format)
1268 printk(KERN_DEBUG "%s, remapped, I(%04X), Z(%X)\n",
1269 buf, (ir_entry->index << 15) | ir_entry->index,
1270 ir_entry->zero);
1271 else
1272 printk(KERN_DEBUG "%s, %s, D(%02X), M(%1d)\n",
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001273 buf,
1274 entry.dest_mode == IOAPIC_DEST_MODE_LOGICAL ?
1275 "logical " : "physical",
Jiang Liua44174e2015-04-13 14:11:57 +08001276 entry.dest, entry.delivery_mode);
1277 }
1278}
1279
Jiang Liu74afab72014-10-27 16:12:00 +08001280static void __init print_IO_APIC(int ioapic_idx)
Joerg Roedelafcc8a42012-09-26 12:44:36 +02001281{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 union IO_APIC_reg_00 reg_00;
1283 union IO_APIC_reg_01 reg_01;
1284 union IO_APIC_reg_02 reg_02;
1285 union IO_APIC_reg_03 reg_03;
1286 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001288 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001289 reg_00.raw = io_apic_read(ioapic_idx, 0);
1290 reg_01.raw = io_apic_read(ioapic_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (reg_01.bits.version >= 0x10)
Yinghai Lu6f50d452011-10-12 00:33:48 -07001292 reg_02.raw = io_apic_read(ioapic_idx, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001293 if (reg_01.bits.version >= 0x20)
Yinghai Lu6f50d452011-10-12 00:33:48 -07001294 reg_03.raw = io_apic_read(ioapic_idx, 3);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001295 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Yinghai Lu6f50d452011-10-12 00:33:48 -07001297 printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1299 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1300 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1301 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Ingo Molnar54168ed2008-08-20 09:07:45 +02001303 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Naga Chumbalkarbd6a46e2011-07-08 18:46:36 +00001304 printk(KERN_DEBUG "....... : max redirection entries: %02X\n",
1305 reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
Naga Chumbalkarbd6a46e2011-07-08 18:46:36 +00001308 printk(KERN_DEBUG "....... : IO APIC version: %02X\n",
1309 reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /*
1312 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1313 * but the value of reg_02 is read as the previous read register
1314 * value, so ignore it if reg_02 == reg_01.
1315 */
1316 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1317 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1318 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
1321 /*
1322 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1323 * or reg_03, but the value of reg_0[23] is read as the previous read
1324 * register value, so ignore it if reg_03 == reg_0[12].
1325 */
1326 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1327 reg_03.raw != reg_01.raw) {
1328 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1329 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
1332 printk(KERN_DEBUG ".... IRQ redirection table:\n");
Jiang Liua44174e2015-04-13 14:11:57 +08001333 io_apic_print_entries(ioapic_idx, reg_01.bits.entries);
Yinghai Lucda417d2011-10-12 00:33:39 -07001334}
1335
Jiang Liu74afab72014-10-27 16:12:00 +08001336void __init print_IO_APICs(void)
Yinghai Lucda417d2011-10-12 00:33:39 -07001337{
Yinghai Lu6f50d452011-10-12 00:33:48 -07001338 int ioapic_idx;
Yinghai Lucda417d2011-10-12 00:33:39 -07001339 unsigned int irq;
1340
1341 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Jiang Liuf44d1692014-06-09 16:19:42 +08001342 for_each_ioapic(ioapic_idx)
Yinghai Lucda417d2011-10-12 00:33:39 -07001343 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001344 mpc_ioapic_id(ioapic_idx),
1345 ioapics[ioapic_idx].nr_registers);
Yinghai Lucda417d2011-10-12 00:33:39 -07001346
1347 /*
1348 * We are a bit conservative about what we expect. We have to
1349 * know about every hardware change ASAP.
1350 */
1351 printk(KERN_INFO "testing the IO APIC.......................\n");
1352
Jiang Liuf44d1692014-06-09 16:19:42 +08001353 for_each_ioapic(ioapic_idx)
Yinghai Lu6f50d452011-10-12 00:33:48 -07001354 print_IO_APIC(ioapic_idx);
Naga Chumbalkar42f0efc2011-07-12 21:17:35 +00001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Thomas Gleixnerad9f4332010-09-30 11:26:43 +02001357 for_each_active_irq(irq) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001358 struct irq_pin_list *entry;
Jiang Liu44677152015-04-14 10:29:53 +08001359 struct irq_chip *chip;
1360 struct mp_chip_data *data;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001361
Mathias Nyman6fd36ba2011-11-10 13:45:24 +00001362 chip = irq_get_chip(irq);
Jiang Liud32932d2015-04-13 14:11:59 +08001363 if (chip != &ioapic_chip && chip != &ioapic_ir_chip)
Mathias Nyman6fd36ba2011-11-10 13:45:24 +00001364 continue;
Jiang Liu44677152015-04-14 10:29:53 +08001365 data = irq_get_chip_data(irq);
1366 if (!data)
1367 continue;
1368 if (list_empty(&data->irq_2_pin))
1369 continue;
Mathias Nyman6fd36ba2011-11-10 13:45:24 +00001370
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001371 printk(KERN_DEBUG "IRQ%d ", irq);
Jiang Liu44677152015-04-14 10:29:53 +08001372 for_each_irq_pin(entry, data->irq_2_pin)
Joe Perchesc767a542012-05-21 19:50:07 -07001373 pr_cont("-> %d:%d", entry->apic, entry->pin);
1374 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
1377 printk(KERN_INFO ".................................... done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Yinghai Luefa25592008-08-19 20:50:36 -07001380/* Where if anywhere is the i8259 connect in external int mode */
1381static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1382
Ingo Molnar54168ed2008-08-20 09:07:45 +02001383void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001385 int i8259_apic, i8259_pin;
Jiang Liuf44d1692014-06-09 16:19:42 +08001386 int apic, pin;
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001387
Thomas Gleixnera46f5c82015-01-15 21:22:32 +00001388 if (skip_ioapic_setup)
1389 nr_ioapics = 0;
1390
1391 if (!nr_legacy_irqs() || !nr_ioapics)
Thomas Gleixnerbc078442009-08-29 18:09:57 +02001392 return;
1393
Jiang Liuf44d1692014-06-09 16:19:42 +08001394 for_each_ioapic_pin(apic, pin) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001395 /* See if any of the pins is in ExtINT mode */
Jiang Liuf44d1692014-06-09 16:19:42 +08001396 struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001397
Jiang Liuf44d1692014-06-09 16:19:42 +08001398 /* If the interrupt line is enabled and in ExtInt mode
1399 * I have found the pin where the i8259 is connected.
1400 */
1401 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1402 ioapic_i8259.apic = apic;
1403 ioapic_i8259.pin = pin;
1404 goto found_i8259;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001405 }
1406 }
1407 found_i8259:
1408 /* Look to see what if the MP table has reported the ExtINT */
1409 /* If we could not find the appropriate pin by looking at the ioapic
1410 * the i8259 probably is not connected the ioapic but give the
1411 * mptable a chance anyway.
1412 */
1413 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1414 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1415 /* Trust the MP table if nothing is setup in the hardware */
1416 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1417 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1418 ioapic_i8259.pin = i8259_pin;
1419 ioapic_i8259.apic = i8259_apic;
1420 }
1421 /* Complain if the MP table and the hardware disagree */
1422 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1423 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1424 {
1425 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
1428 /*
1429 * Do not trust the IO-APIC being empty at bootup
1430 */
1431 clear_IO_APIC();
1432}
1433
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001434void native_disable_io_apic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001437 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001438 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001439 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001440 */
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001441 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001442 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001443
1444 memset(&entry, 0, sizeof(entry));
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001445 entry.mask = IOAPIC_UNMASKED;
1446 entry.trigger = IOAPIC_EDGE;
1447 entry.polarity = IOAPIC_POL_HIGH;
1448 entry.dest_mode = IOAPIC_DEST_MODE_PHYSICAL;
1449 entry.delivery_mode = dest_ExtINT;
1450 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001451
1452 /*
1453 * Add it to the IO-APIC irq-routing table:
1454 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001455 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001456 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001457
Borislav Petkov93984fb2016-04-04 22:25:00 +02001458 if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config())
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001459 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001460}
1461
1462/*
1463 * Not an __init, needed by the reboot code
1464 */
1465void disable_IO_APIC(void)
1466{
1467 /*
1468 * Clear the IO-APIC before rebooting:
1469 */
1470 clear_IO_APIC();
1471
Jiang Liu95d76ac2014-06-09 16:19:48 +08001472 if (!nr_legacy_irqs())
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001473 return;
1474
1475 x86_io_apic_ops.disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Ingo Molnar54168ed2008-08-20 09:07:45 +02001478#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479/*
1480 * function to set the IO-APIC physical IDs based on the
1481 * values stored in the MPC table.
1482 *
1483 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1484 */
Sebastian Andrzej Siewiora38c5382010-11-26 17:50:20 +01001485void __init setup_ioapic_ids_from_mpc_nocheck(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 union IO_APIC_reg_00 reg_00;
1488 physid_mask_t phys_id_present_map;
Yinghai Lu6f50d452011-10-12 00:33:48 -07001489 int ioapic_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 int i;
1491 unsigned char old_id;
1492 unsigned long flags;
1493
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001494 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 * This is broken; anything with a real cpu count has to
1496 * circumvent this idiocy regardless.
1497 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03001498 apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 /*
1501 * Set the IOAPIC ID to the value stored in the MPC table.
1502 */
Jiang Liuf44d1692014-06-09 16:19:42 +08001503 for_each_ioapic(ioapic_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /* Read the register 0 value */
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001505 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001506 reg_00.raw = io_apic_read(ioapic_idx, 0);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001507 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001508
Yinghai Lu6f50d452011-10-12 00:33:48 -07001509 old_id = mpc_ioapic_id(ioapic_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Yinghai Lu6f50d452011-10-12 00:33:48 -07001511 if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001513 ioapic_idx, mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1515 reg_00.bits.ID);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001516 ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 /*
1520 * Sanity check, is the ID really free? Every APIC in a
1521 * system must have a unique ID or we get lots of nice
1522 * 'stuck on smp_invalidate_needed IPI wait' messages.
1523 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03001524 if (apic->check_apicid_used(&phys_id_present_map,
Yinghai Lu6f50d452011-10-12 00:33:48 -07001525 mpc_ioapic_id(ioapic_idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001527 ioapic_idx, mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 for (i = 0; i < get_physical_broadcast(); i++)
1529 if (!physid_isset(i, phys_id_present_map))
1530 break;
1531 if (i >= get_physical_broadcast())
1532 panic("Max APIC ID exceeded!\n");
1533 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1534 i);
1535 physid_set(i, phys_id_present_map);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001536 ioapics[ioapic_idx].mp_config.apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 } else {
1538 physid_mask_t tmp;
Yinghai Lu6f50d452011-10-12 00:33:48 -07001539 apic->apicid_to_cpu_present(mpc_ioapic_id(ioapic_idx),
Suresh Siddhad5371432011-05-18 16:31:37 -07001540 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 apic_printk(APIC_VERBOSE, "Setting %d in the "
1542 "phys_id_present_map\n",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001543 mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1545 }
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
1548 * We need to adjust the IRQ routing table
1549 * if the ID changed.
1550 */
Yinghai Lu6f50d452011-10-12 00:33:48 -07001551 if (old_id != mpc_ioapic_id(ioapic_idx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301553 if (mp_irqs[i].dstapic == old_id)
1554 mp_irqs[i].dstapic
Yinghai Lu6f50d452011-10-12 00:33:48 -07001555 = mpc_ioapic_id(ioapic_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 /*
Yinghai Lu60d79fd2010-12-07 00:59:49 -08001558 * Update the ID register according to the right value
1559 * from the MPC table if they are different.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001560 */
Yinghai Lu6f50d452011-10-12 00:33:48 -07001561 if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
Yinghai Lu60d79fd2010-12-07 00:59:49 -08001562 continue;
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 apic_printk(APIC_VERBOSE, KERN_INFO
1565 "...changing IO-APIC physical APIC ID to %d ...",
Yinghai Lu6f50d452011-10-12 00:33:48 -07001566 mpc_ioapic_id(ioapic_idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Yinghai Lu6f50d452011-10-12 00:33:48 -07001568 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001569 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001570 io_apic_write(ioapic_idx, 0, reg_00.raw);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001571 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /*
1574 * Sanity check
1575 */
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001576 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001577 reg_00.raw = io_apic_read(ioapic_idx, 0);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001578 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07001579 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
Joe Perchesc767a542012-05-21 19:50:07 -07001580 pr_cont("could not set ID!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 else
1582 apic_printk(APIC_VERBOSE, " ok.\n");
1583 }
1584}
Sebastian Andrzej Siewiora38c5382010-11-26 17:50:20 +01001585
1586void __init setup_ioapic_ids_from_mpc(void)
1587{
1588
1589 if (acpi_ioapic)
1590 return;
1591 /*
1592 * Don't check I/O APIC IDs for xAPIC systems. They have
1593 * no meaning without the serial APIC bus.
1594 */
1595 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1596 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
1597 return;
1598 setup_ioapic_ids_from_mpc_nocheck();
1599}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001602int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001603
1604static int __init notimercheck(char *s)
1605{
1606 no_timer_check = 1;
1607 return 1;
1608}
1609__setup("no_timer_check", notimercheck);
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/*
1612 * There is a nasty bug in some older SMP boards, their mptable lies
1613 * about the timer IRQ. We do the following to work around the situation:
1614 *
1615 * - timer IRQ defaults to IO-APIC IRQ
1616 * - if this function detects that timer IRQs are defunct, then we fall
1617 * back to ISA timer IRQs
1618 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001619static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001622 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Zachary Amsden8542b202006-12-07 02:14:09 +01001624 if (no_timer_check)
1625 return 1;
1626
Ingo Molnar4aae0702007-12-18 18:05:58 +01001627 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 local_irq_enable();
1629 /* Let ten ticks pass... */
1630 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001631 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /*
1634 * Expect a few ticks at least, to be sure some possible
1635 * glue logic does not lock up after one or two first
1636 * ticks in a non-ExtINT mode. Also the local APIC
1637 * might have cached one ExtINT interrupt. Finally, at
1638 * least one tick may be lost due to delays.
1639 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001640
1641 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01001642 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645}
1646
1647/*
1648 * In the SMP+IOAPIC case it might happen that there are an unspecified
1649 * number of pending IRQ events unhandled. These cases are very rare,
1650 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1651 * better to do it this way as thus we do not have to be aware of
1652 * 'pending' interrupts in the IRQ path, except at this point.
1653 */
1654/*
1655 * Edge triggered needs to resend any interrupt
1656 * that was delayed but this is now handled in the device
1657 * independent code.
1658 */
1659
1660/*
1661 * Starting up a edge-triggered IO-APIC interrupt is
1662 * nasty - we need to make sure that we get the edge.
1663 * If it is already asserted for some reason, we need
1664 * return 1 to indicate that is was pending.
1665 *
1666 * This is not complete - we should be able to fake
1667 * an edge even if it isn't on the 8259A...
1668 */
Thomas Gleixner61a38ce2010-09-28 16:00:34 +02001669static unsigned int startup_ioapic_irq(struct irq_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Thomas Gleixner61a38ce2010-09-28 16:00:34 +02001671 int was_pending = 0, irq = data->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 unsigned long flags;
1673
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001674 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu95d76ac2014-06-09 16:19:48 +08001675 if (irq < nr_legacy_irqs()) {
Thomas Gleixner4305df92010-09-28 15:01:33 +02001676 legacy_pic->mask(irq);
Jacob Panb81bb372009-11-09 11:27:04 -08001677 if (legacy_pic->irq_pending(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 was_pending = 1;
1679 }
Jiang Liu44677152015-04-14 10:29:53 +08001680 __unmask_ioapic(data->chip_data);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02001681 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 return was_pending;
1684}
1685
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001686atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001687
Alexander Gordeev4da70722012-03-20 15:19:36 +01001688#ifdef CONFIG_GENERIC_PENDING_IRQ
Jiang Liu44677152015-04-14 10:29:53 +08001689static bool io_apic_level_ack_pending(struct mp_chip_data *data)
Márton Némethd1ecad62012-05-08 00:24:20 -07001690{
1691 struct irq_pin_list *entry;
1692 unsigned long flags;
1693
1694 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +08001695 for_each_irq_pin(entry, data->irq_2_pin) {
Márton Némethd1ecad62012-05-08 00:24:20 -07001696 unsigned int reg;
1697 int pin;
1698
1699 pin = entry->pin;
1700 reg = io_apic_read(entry->apic, 0x10 + pin*2);
1701 /* Is the remote IRR bit set? */
1702 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
1703 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1704 return true;
1705 }
1706 }
1707 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1708
1709 return false;
1710}
1711
Jiang Liu44677152015-04-14 10:29:53 +08001712static inline bool ioapic_irqd_mask(struct irq_data *data)
Alexander Gordeev4da70722012-03-20 15:19:36 +01001713{
1714 /* If we are moving the irq we need to mask it */
1715 if (unlikely(irqd_is_setaffinity_pending(data))) {
Jiang Liu44677152015-04-14 10:29:53 +08001716 mask_ioapic_irq(data);
Alexander Gordeev4da70722012-03-20 15:19:36 +01001717 return true;
1718 }
1719 return false;
1720}
1721
Jiang Liu44677152015-04-14 10:29:53 +08001722static inline void ioapic_irqd_unmask(struct irq_data *data, bool masked)
Alexander Gordeev4da70722012-03-20 15:19:36 +01001723{
1724 if (unlikely(masked)) {
1725 /* Only migrate the irq if the ack has been received.
1726 *
1727 * On rare occasions the broadcast level triggered ack gets
1728 * delayed going to ioapics, and if we reprogram the
1729 * vector while Remote IRR is still set the irq will never
1730 * fire again.
1731 *
1732 * To prevent this scenario we read the Remote IRR bit
1733 * of the ioapic. This has two effects.
1734 * - On any sane system the read of the ioapic will
1735 * flush writes (and acks) going to the ioapic from
1736 * this cpu.
1737 * - We get to see if the ACK has actually been delivered.
1738 *
1739 * Based on failed experiments of reprogramming the
1740 * ioapic entry from outside of irq context starting
1741 * with masking the ioapic entry and then polling until
1742 * Remote IRR was clear before reprogramming the
1743 * ioapic I don't trust the Remote IRR bit to be
1744 * completey accurate.
1745 *
1746 * However there appears to be no other way to plug
1747 * this race, so if the Remote IRR bit is not
1748 * accurate and is causing problems then it is a hardware bug
1749 * and you can go talk to the chipset vendor about it.
1750 */
Jiang Liu44677152015-04-14 10:29:53 +08001751 if (!io_apic_level_ack_pending(data->chip_data))
Alexander Gordeev4da70722012-03-20 15:19:36 +01001752 irq_move_masked_irq(data);
Jiang Liu44677152015-04-14 10:29:53 +08001753 unmask_ioapic_irq(data);
Alexander Gordeev4da70722012-03-20 15:19:36 +01001754 }
1755}
1756#else
Jiang Liu44677152015-04-14 10:29:53 +08001757static inline bool ioapic_irqd_mask(struct irq_data *data)
Alexander Gordeev4da70722012-03-20 15:19:36 +01001758{
1759 return false;
1760}
Jiang Liu44677152015-04-14 10:29:53 +08001761static inline void ioapic_irqd_unmask(struct irq_data *data, bool masked)
Alexander Gordeev4da70722012-03-20 15:19:36 +01001762{
1763}
1764#endif
1765
Jiang Liu44677152015-04-14 10:29:53 +08001766static void ioapic_ack_level(struct irq_data *irq_data)
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001767{
Jiang Liu44677152015-04-14 10:29:53 +08001768 struct irq_cfg *cfg = irqd_cfg(irq_data);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001769 unsigned long v;
Alexander Gordeev4da70722012-03-20 15:19:36 +01001770 bool masked;
Jiang Liud32932d2015-04-13 14:11:59 +08001771 int i;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001772
Thomas Gleixnerdd5f15e2010-09-28 15:18:35 +02001773 irq_complete_move(cfg);
Jiang Liu44677152015-04-14 10:29:53 +08001774 masked = ioapic_irqd_mask(irq_data);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001775
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001776 /*
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07001777 * It appears there is an erratum which affects at least version 0x11
1778 * of I/O APIC (that's the 82093AA and cores integrated into various
1779 * chipsets). Under certain conditions a level-triggered interrupt is
1780 * erroneously delivered as edge-triggered one but the respective IRR
1781 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1782 * message but it will never arrive and further interrupts are blocked
1783 * from the source. The exact reason is so far unknown, but the
1784 * phenomenon was observed when two consecutive interrupt requests
1785 * from a given source get delivered to the same CPU and the source is
1786 * temporarily disabled in between.
1787 *
1788 * A workaround is to simulate an EOI message manually. We achieve it
1789 * by setting the trigger mode to edge and then to level when the edge
1790 * trigger mode gets detected in the TMR of a local APIC for a
1791 * level-triggered interrupt. We mask the source for the time of the
1792 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1793 * The idea is from Manfred Spraul. --macro
Suresh Siddha1c839952009-12-01 15:31:17 -08001794 *
1795 * Also in the case when cpu goes offline, fixup_irqs() will forward
1796 * any unhandled interrupt on the offlined cpu to the new cpu
1797 * destination that is handling the corresponding interrupt. This
1798 * interrupt forwarding is done via IPI's. Hence, in this case also
1799 * level-triggered io-apic interrupt will be seen as an edge
1800 * interrupt in the IRR. And we can't rely on the cpu's EOI
1801 * to be broadcasted to the IO-APIC's which will clear the remoteIRR
1802 * corresponding to the level-triggered interrupt. Hence on IO-APIC's
1803 * supporting EOI register, we do an explicit EOI to clear the
1804 * remote IRR and on IO-APIC's which don't have an EOI register,
1805 * we use the above logic (mask+edge followed by unmask+level) from
1806 * Manfred Spraul to clear the remote IRR.
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07001807 */
Yinghai Lu3145e942008-12-05 18:58:34 -08001808 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001809 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001810
Ingo Molnar54168ed2008-08-20 09:07:45 +02001811 /*
1812 * We must acknowledge the irq before we move it or the acknowledge will
1813 * not propagate properly.
1814 */
1815 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001816
Suresh Siddha1c839952009-12-01 15:31:17 -08001817 /*
1818 * Tail end of clearing remote IRR bit (either by delivering the EOI
1819 * message via io-apic EOI register write or simulating it using
1820 * mask+edge followed by unnask+level logic) manually when the
1821 * level triggered interrupt is seen as the edge triggered interrupt
1822 * at the cpu.
1823 */
Maciej W. Rozyckica64c472009-12-01 15:31:15 -08001824 if (!(v & (1 << (i & 0x1f)))) {
1825 atomic_inc(&irq_mis_count);
Jiang Liu44677152015-04-14 10:29:53 +08001826 eoi_ioapic_pin(cfg->vector, irq_data->chip_data);
Maciej W. Rozyckica64c472009-12-01 15:31:15 -08001827 }
1828
Jiang Liu44677152015-04-14 10:29:53 +08001829 ioapic_irqd_unmask(irq_data, masked);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07001830}
Yinghai Lu1d025192008-08-19 20:50:34 -07001831
Jiang Liud32932d2015-04-13 14:11:59 +08001832static void ioapic_ir_ack_level(struct irq_data *irq_data)
1833{
1834 struct mp_chip_data *data = irq_data->chip_data;
1835
1836 /*
1837 * Intr-remapping uses pin number as the virtual vector
1838 * in the RTE. Actual vector is programmed in
1839 * intr-remapping table entry. Hence for the io-apic
1840 * EOI we use the pin number.
1841 */
1842 ack_APIC_irq();
Jiang Liu44677152015-04-14 10:29:53 +08001843 eoi_ioapic_pin(data->entry.vector, data);
Jiang Liud32932d2015-04-13 14:11:59 +08001844}
1845
1846static int ioapic_set_affinity(struct irq_data *irq_data,
1847 const struct cpumask *mask, bool force)
1848{
1849 struct irq_data *parent = irq_data->parent_data;
1850 struct mp_chip_data *data = irq_data->chip_data;
Jiang Liu0be275e2015-04-14 10:29:57 +08001851 struct irq_pin_list *entry;
Jiang Liud32932d2015-04-13 14:11:59 +08001852 struct irq_cfg *cfg;
1853 unsigned long flags;
1854 int ret;
1855
1856 ret = parent->chip->irq_set_affinity(parent, mask, force);
1857 raw_spin_lock_irqsave(&ioapic_lock, flags);
1858 if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
1859 cfg = irqd_cfg(irq_data);
1860 data->entry.dest = cfg->dest_apicid;
1861 data->entry.vector = cfg->vector;
Jiang Liu0be275e2015-04-14 10:29:57 +08001862 for_each_irq_pin(entry, data->irq_2_pin)
1863 __ioapic_write_entry(entry->apic, entry->pin,
1864 data->entry);
Jiang Liud32932d2015-04-13 14:11:59 +08001865 }
1866 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1867
1868 return ret;
1869}
1870
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001871static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerf7e909e2010-10-08 21:40:23 +02001872 .name = "IO-APIC",
1873 .irq_startup = startup_ioapic_irq,
1874 .irq_mask = mask_ioapic_irq,
1875 .irq_unmask = unmask_ioapic_irq,
Jiang Liud32932d2015-04-13 14:11:59 +08001876 .irq_ack = irq_chip_ack_parent,
1877 .irq_eoi = ioapic_ack_level,
1878 .irq_set_affinity = ioapic_set_affinity,
1879 .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,
Rafael J. Wysocki56135702014-09-01 13:49:07 +02001890 .flags = IRQCHIP_SKIP_SET_WAKE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891};
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893static inline void init_IO_APIC_traps(void)
1894{
Yinghai Luda51a822008-08-19 20:50:25 -07001895 struct irq_cfg *cfg;
Thomas Gleixnerad9f4332010-09-30 11:26:43 +02001896 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Thomas Gleixnerad9f4332010-09-30 11:26:43 +02001898 for_each_active_irq(irq) {
Jiang Liu32f5ef52014-06-09 16:19:43 +08001899 cfg = irq_cfg(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001900 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /*
1902 * Hmm.. We don't have an entry for this,
1903 * so default to an old-fashioned 8259
1904 * interrupt if we can..
1905 */
Jiang Liu95d76ac2014-06-09 16:19:48 +08001906 if (irq < nr_legacy_irqs())
Jacob Panb81bb372009-11-09 11:27:04 -08001907 legacy_pic->make_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001908 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /* Strange. Oh, well.. */
Thomas Gleixner2c778652011-03-12 12:20:43 +01001910 irq_set_chip(irq, &no_irq_chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 }
1913}
1914
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001915/*
1916 * The local APIC irq-chip implementation:
1917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Thomas Gleixner90297c52010-09-28 16:03:54 +02001919static void mask_lapic_irq(struct irq_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 unsigned long v;
1922
1923 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001924 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Thomas Gleixner90297c52010-09-28 16:03:54 +02001927static void unmask_lapic_irq(struct irq_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001929 unsigned long v;
1930
1931 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001932 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Thomas Gleixner90297c52010-09-28 16:03:54 +02001935static void ack_lapic_irq(struct irq_data *data)
Yinghai Lu1d025192008-08-19 20:50:34 -07001936{
1937 ack_APIC_irq();
1938}
1939
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001940static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01001941 .name = "local-APIC",
Thomas Gleixner90297c52010-09-28 16:03:54 +02001942 .irq_mask = mask_lapic_irq,
1943 .irq_unmask = unmask_lapic_irq,
1944 .irq_ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945};
1946
Thomas Gleixner60c69942010-09-28 17:28:38 +02001947static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001948{
Thomas Gleixner60c69942010-09-28 17:28:38 +02001949 irq_clear_status_flags(irq, IRQ_LEVEL);
Thomas Gleixner2c778652011-03-12 12:20:43 +01001950 irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001951 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/*
1955 * This looks a bit hackish but it's about the only one way of sending
1956 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1957 * not support the ExtINT mode, unfortunately. We need to send these
1958 * cycles as some i82489DX-based boards have glue logic that keeps the
1959 * 8259A interrupt line asserted until INTA. --macro
1960 */
Jacek Luczak28acf282008-04-12 17:41:12 +02001961static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001963 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct IO_APIC_route_entry entry0, entry1;
1965 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001967 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01001968 if (pin == -1) {
1969 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01001971 }
1972 apic = find_isa_irq_apic(8, mp_INT);
1973 if (apic == -1) {
1974 WARN_ON_ONCE(1);
1975 return;
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Andi Kleencf4c6a22006-09-26 10:52:30 +02001978 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001979 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 memset(&entry1, 0, sizeof(entry1));
1982
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001983 entry1.dest_mode = IOAPIC_DEST_MODE_PHYSICAL;
1984 entry1.mask = IOAPIC_UNMASKED;
Yinghai Lud83e94a2008-08-19 20:50:33 -07001985 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 entry1.delivery_mode = dest_ExtINT;
1987 entry1.polarity = entry0.polarity;
Thomas Gleixner335efdf2015-04-14 10:30:06 +08001988 entry1.trigger = IOAPIC_EDGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 entry1.vector = 0;
1990
Andi Kleencf4c6a22006-09-26 10:52:30 +02001991 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 save_control = CMOS_READ(RTC_CONTROL);
1994 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1995 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1996 RTC_FREQ_SELECT);
1997 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1998
1999 i = 100;
2000 while (i-- > 0) {
2001 mdelay(10);
2002 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2003 i -= 10;
2004 }
2005
2006 CMOS_WRITE(save_control, RTC_CONTROL);
2007 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002008 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Andi Kleencf4c6a22006-09-26 10:52:30 +02002010 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Yinghai Luefa25592008-08-19 20:50:36 -07002013static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002014/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002015static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002016{
2017 disable_timer_pin_1 = 1;
2018 return 0;
2019}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002020early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002021
Jiang Liud32932d2015-04-13 14:11:59 +08002022static int mp_alloc_timer_irq(int ioapic, int pin)
2023{
2024 int irq = -1;
Jiang Liud32932d2015-04-13 14:11:59 +08002025 struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
2026
2027 if (domain) {
Jiang Liu44677152015-04-14 10:29:53 +08002028 struct irq_alloc_info info;
2029
Jiang Liud32932d2015-04-13 14:11:59 +08002030 ioapic_set_alloc_attr(&info, NUMA_NO_NODE, 0, 0);
2031 info.ioapic_id = mpc_ioapic_id(ioapic);
2032 info.ioapic_pin = pin;
2033 mutex_lock(&ioapic_mutex);
2034 irq = alloc_isa_irq_from_domain(domain, 0, ioapic, pin, &info);
2035 mutex_unlock(&ioapic_mutex);
2036 }
2037
2038 return irq;
2039}
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041/*
2042 * This code may look a bit paranoid, but it's supposed to cooperate with
2043 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2044 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2045 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002046 *
2047 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002049static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Jiang Liu44677152015-04-14 10:29:53 +08002051 struct irq_data *irq_data = irq_get_irq_data(0);
2052 struct mp_chip_data *data = irq_data->chip_data;
2053 struct irq_cfg *cfg = irqd_cfg(irq_data);
Robert Richterf6e9456c2010-07-21 19:03:58 +02002054 int node = cpu_to_node(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002055 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002056 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002057 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002058
2059 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
2062 * get/set the timer IRQ vector:
2063 */
Thomas Gleixner4305df92010-09-28 15:01:33 +02002064 legacy_pic->mask(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002067 * As IRQ0 is to be enabled in the 8259A, the virtual
2068 * wire has to be disabled in the local APIC. Also
2069 * timer interrupts need to be acknowledged manually in
2070 * the 8259A for the i82489DX when using the NMI
2071 * watchdog as that APIC treats NMIs as level-triggered.
2072 * The AEOI mode will finish them in the 8259A
2073 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002075 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Jacob Panb81bb372009-11-09 11:27:04 -08002076 legacy_pic->init(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002078 pin1 = find_isa_irq_pin(0, mp_INT);
2079 apic1 = find_isa_irq_apic(0, mp_INT);
2080 pin2 = ioapic_i8259.pin;
2081 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002083 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2084 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002085 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002087 /*
2088 * Some BIOS writers are clueless and report the ExtINTA
2089 * I/O APIC input from the cascaded 8259A as the timer
2090 * interrupt input. So just in case, if only one pin
2091 * was found above, try it both directly and through the
2092 * 8259A.
2093 */
2094 if (pin1 == -1) {
Joerg Roedel6a9f5de2012-09-26 12:44:41 +02002095 panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002096 pin1 = pin2;
2097 apic1 = apic2;
2098 no_pin1 = 1;
2099 } else if (pin2 == -1) {
2100 pin2 = pin1;
2101 apic2 = apic1;
2102 }
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (pin1 != -1) {
Jiang Liud32932d2015-04-13 14:11:59 +08002105 /* Ok, does IRQ0 through the IOAPIC work? */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002106 if (no_pin1) {
Jiang Liud32932d2015-04-13 14:11:59 +08002107 mp_alloc_timer_irq(apic1, pin1);
Yinghai Luf72dcca2009-02-08 16:18:03 -08002108 } else {
Jiang Liud32932d2015-04-13 14:11:59 +08002109 /*
2110 * for edge trigger, it's already unmasked,
Yinghai Luf72dcca2009-02-08 16:18:03 -08002111 * so only need to unmask if it is level-trigger
2112 * do we really have level trigger timer?
2113 */
2114 int idx;
2115 idx = find_irq_entry(apic1, pin1, mp_INT);
2116 if (idx != -1 && irq_trigger(idx))
Jiang Liu44677152015-04-14 10:29:53 +08002117 unmask_ioapic_irq(irq_get_chip_data(0));
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002118 }
Jiang Liu44677152015-04-14 10:29:53 +08002119 irq_domain_activate_irq(irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (timer_irq_works()) {
Chuck Ebbert66759a02005-09-12 18:49:25 +02002121 if (disable_timer_pin_1 > 0)
2122 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
Joerg Roedel6a9f5de2012-09-26 12:44:41 +02002125 panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
Yinghai Luf72dcca2009-02-08 16:18:03 -08002126 local_irq_disable();
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002127 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002128 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002129 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2130 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002132 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2133 "(IRQ0) through the 8259A ...\n");
2134 apic_printk(APIC_QUIET, KERN_INFO
2135 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
2137 * legacy devices should be connected to IO APIC #0
2138 */
Jiang Liu44677152015-04-14 10:29:53 +08002139 replace_pin_at_irq_node(data, node, apic1, pin1, apic2, pin2);
2140 irq_domain_activate_irq(irq_data);
Thomas Gleixner4305df92010-09-28 15:01:33 +02002141 legacy_pic->unmask(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002143 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002144 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146 /*
2147 * Cleanup, just in case ...
2148 */
Yinghai Luf72dcca2009-02-08 16:18:03 -08002149 local_irq_disable();
Thomas Gleixner4305df92010-09-28 15:01:33 +02002150 legacy_pic->mask(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002151 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002152 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002155 apic_printk(APIC_QUIET, KERN_INFO
2156 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Thomas Gleixner60c69942010-09-28 17:28:38 +02002158 lapic_register_intr(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002159 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Thomas Gleixner4305df92010-09-28 15:01:33 +02002160 legacy_pic->unmask(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002163 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002164 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002166 local_irq_disable();
Thomas Gleixner4305df92010-09-28 15:01:33 +02002167 legacy_pic->mask(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002168 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002169 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002171 apic_printk(APIC_QUIET, KERN_INFO
2172 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Jacob Panb81bb372009-11-09 11:27:04 -08002174 legacy_pic->init(0);
2175 legacy_pic->make_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002176 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 unlock_ExtINT_logic();
2179
2180 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002181 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002182 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002184 local_irq_disable();
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002185 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Thomas Gleixner2ca5b402015-01-15 21:22:14 +00002186 if (apic_is_x2apic_enabled())
Yinghai Lufb209bd2011-12-21 17:45:17 -08002187 apic_printk(APIC_QUIET, KERN_INFO
2188 "Perhaps problem with the pre-enabled x2apic mode\n"
2189 "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002191 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002192out:
2193 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
2196/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002197 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2198 * to devices. However there may be an I/O APIC pin available for
2199 * this interrupt regardless. The pin may be left unconnected, but
2200 * typically it will be reused as an ExtINT cascade interrupt for
2201 * the master 8259A. In the MPS case such a pin will normally be
2202 * reported as an ExtINT interrupt in the MP table. With ACPI
2203 * there is no provision for ExtINT interrupts, and in the absence
2204 * of an override it would be treated as an ordinary ISA I/O APIC
2205 * interrupt, that is edge-triggered and unmasked by default. We
2206 * used to do this, but it caused problems on some systems because
2207 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2208 * the same ExtINT cascade interrupt to drive the local APIC of the
2209 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2210 * the I/O APIC in all cases now. No actual device should request
2211 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 */
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002213#define PIC_IRQS (1UL << PIC_CASCADE_IR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Jiang Liu44767bf2014-06-09 16:19:53 +08002215static int mp_irqdomain_create(int ioapic)
2216{
Jiang Liud32932d2015-04-13 14:11:59 +08002217 struct irq_alloc_info info;
2218 struct irq_domain *parent;
Jiang Liu44767bf2014-06-09 16:19:53 +08002219 int hwirqs = mp_ioapic_pin_count(ioapic);
2220 struct ioapic *ip = &ioapics[ioapic];
2221 struct ioapic_domain_cfg *cfg = &ip->irqdomain_cfg;
2222 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2223
2224 if (cfg->type == IOAPIC_DOMAIN_INVALID)
2225 return 0;
2226
Jiang Liud32932d2015-04-13 14:11:59 +08002227 init_irq_alloc_info(&info, NULL);
2228 info.type = X86_IRQ_ALLOC_TYPE_IOAPIC;
2229 info.ioapic_id = mpc_ioapic_id(ioapic);
2230 parent = irq_remapping_get_ir_irq_domain(&info);
2231 if (!parent)
2232 parent = x86_vector_domain;
2233
Jiang Liu44767bf2014-06-09 16:19:53 +08002234 ip->irqdomain = irq_domain_add_linear(cfg->dev, hwirqs, cfg->ops,
2235 (void *)(long)ioapic);
Jiang Liub75e8182015-04-14 10:29:39 +08002236 if (!ip->irqdomain)
Jiang Liu44767bf2014-06-09 16:19:53 +08002237 return -ENOMEM;
Jiang Liub75e8182015-04-14 10:29:39 +08002238
2239 ip->irqdomain->parent = parent;
Jiang Liu44767bf2014-06-09 16:19:53 +08002240
2241 if (cfg->type == IOAPIC_DOMAIN_LEGACY ||
2242 cfg->type == IOAPIC_DOMAIN_STRICT)
2243 ioapic_dynirq_base = max(ioapic_dynirq_base,
2244 gsi_cfg->gsi_end + 1);
2245
Jiang Liu44767bf2014-06-09 16:19:53 +08002246 return 0;
2247}
2248
Jiang Liu15516a32014-10-27 13:21:46 +08002249static void ioapic_destroy_irqdomain(int idx)
2250{
2251 if (ioapics[idx].irqdomain) {
2252 irq_domain_remove(ioapics[idx].irqdomain);
2253 ioapics[idx].irqdomain = NULL;
2254 }
Jiang Liu15516a32014-10-27 13:21:46 +08002255}
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257void __init setup_IO_APIC(void)
2258{
Jiang Liu44767bf2014-06-09 16:19:53 +08002259 int ioapic;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002260
Thomas Gleixnera46f5c82015-01-15 21:22:32 +00002261 if (skip_ioapic_setup || !nr_ioapics)
2262 return;
2263
Jiang Liu95d76ac2014-06-09 16:19:48 +08002264 io_apic_irqs = nr_legacy_irqs() ? ~PIC_IRQS : ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Ingo Molnar54168ed2008-08-20 09:07:45 +02002266 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Jiang Liu44767bf2014-06-09 16:19:53 +08002267 for_each_ioapic(ioapic)
2268 BUG_ON(mp_irqdomain_create(ioapic));
2269
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002270 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002271 * Set up IO-APIC IRQ routing.
2272 */
Thomas Gleixnerde934102009-08-20 09:27:29 +02002273 x86_init.mpparse.setup_ioapic_ids();
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 sync_Arb_IDs();
2276 setup_IO_APIC_irqs();
2277 init_IO_APIC_traps();
Jiang Liu95d76ac2014-06-09 16:19:48 +08002278 if (nr_legacy_irqs())
Thomas Gleixnerbc078442009-08-29 18:09:57 +02002279 check_timer();
Jiang Liub81975e2014-06-09 16:20:11 +08002280
2281 ioapic_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Yinghai Lu6f50d452011-10-12 00:33:48 -07002284static void resume_ioapic_id(int ioapic_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 unsigned long flags;
2287 union IO_APIC_reg_00 reg_00;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002288
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002289 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu6f50d452011-10-12 00:33:48 -07002290 reg_00.raw = io_apic_read(ioapic_idx, 0);
2291 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
2292 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
2293 io_apic_write(ioapic_idx, 0, reg_00.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002295 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002298static void ioapic_resume(void)
2299{
Yinghai Lu6f50d452011-10-12 00:33:48 -07002300 int ioapic_idx;
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002301
Jiang Liuf44d1692014-06-09 16:19:42 +08002302 for_each_ioapic_reverse(ioapic_idx)
Yinghai Lu6f50d452011-10-12 00:33:48 -07002303 resume_ioapic_id(ioapic_idx);
Suresh Siddha15bac202011-05-18 16:31:34 -07002304
2305 restore_ioapic_entries();
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002306}
2307
2308static struct syscore_ops ioapic_syscore_ops = {
Suresh Siddha15bac202011-05-18 16:31:34 -07002309 .suspend = save_ioapic_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 .resume = ioapic_resume,
2311};
2312
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002313static int __init ioapic_init_ops(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002315 register_syscore_ops(&ioapic_syscore_ops);
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return 0;
2318}
2319
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002320device_initcall(ioapic_init_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Jiang Liu67dc5e72014-10-27 13:21:41 +08002322static int io_apic_get_redir_entries(int ioapic)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07002323{
2324 union IO_APIC_reg_01 reg_01;
2325 unsigned long flags;
2326
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002327 raw_spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07002328 reg_01.raw = io_apic_read(ioapic, 1);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002329 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07002330
Eric W. Biederman4b6b19a2010-03-30 01:07:08 -07002331 /* The register returns the maximum index redir index
2332 * supported, which is one less than the total number of redir
2333 * entries.
2334 */
2335 return reg_01.bits.entries + 1;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07002336}
2337
Thomas Gleixner62a08ae2014-04-24 09:50:53 +02002338unsigned int arch_dynirq_lower_bound(unsigned int from)
2339{
Jiang Liub81975e2014-06-09 16:20:11 +08002340 /*
2341 * dmar_alloc_hwirq() may be called before setup_IO_APIC(), so use
2342 * gsi_top if ioapic_dynirq_base hasn't been initialized yet.
2343 */
2344 return ioapic_initialized ? ioapic_dynirq_base : gsi_top;
Thomas Gleixner62a08ae2014-04-24 09:50:53 +02002345}
2346
Feng Tang2a4ab642009-07-07 23:01:15 -04002347#ifdef CONFIG_X86_32
Jiang Liu67dc5e72014-10-27 13:21:41 +08002348static int io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 union IO_APIC_reg_00 reg_00;
2351 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2352 physid_mask_t tmp;
2353 unsigned long flags;
2354 int i = 0;
2355
2356 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002357 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2358 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002360 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2362 * advantage of new APIC bus architecture.
2363 */
2364
2365 if (physids_empty(apic_id_map))
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002366 apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002368 raw_spin_lock_irqsave(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 reg_00.raw = io_apic_read(ioapic, 0);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002370 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 if (apic_id >= get_physical_broadcast()) {
2373 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2374 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2375 apic_id = reg_00.bits.ID;
2376 }
2377
2378 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002379 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 * 'stuck on smp_invalidate_needed IPI wait' messages.
2381 */
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002382 if (apic->check_apicid_used(&apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 for (i = 0; i < get_physical_broadcast(); i++) {
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002385 if (!apic->check_apicid_used(&apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 break;
2387 }
2388
2389 if (i == get_physical_broadcast())
2390 panic("Max apic_id exceeded!\n");
2391
2392 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2393 "trying %d\n", ioapic, apic_id, i);
2394
2395 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +03002398 apic->apicid_to_cpu_present(apic_id, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 physids_or(apic_id_map, apic_id_map, tmp);
2400
2401 if (reg_00.bits.ID != apic_id) {
2402 reg_00.bits.ID = apic_id;
2403
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002404 raw_spin_lock_irqsave(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 io_apic_write(ioapic, 0, reg_00.raw);
2406 reg_00.raw = io_apic_read(ioapic, 0);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002407 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002410 if (reg_00.bits.ID != apic_id) {
Joe Perchesc767a542012-05-21 19:50:07 -07002411 pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
2412 ioapic);
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002413 return -1;
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416
2417 apic_printk(APIC_VERBOSE, KERN_INFO
2418 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2419
2420 return apic_id;
2421}
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002422
Jiang Liu67dc5e72014-10-27 13:21:41 +08002423static u8 io_apic_unique_id(int idx, u8 id)
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002424{
2425 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
2426 !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Yinghai Lu5411dc42014-10-27 13:21:40 +08002427 return io_apic_get_unique_id(idx, id);
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002428 else
2429 return id;
2430}
2431#else
Jiang Liu67dc5e72014-10-27 13:21:41 +08002432static u8 io_apic_unique_id(int idx, u8 id)
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002433{
Yinghai Lu5411dc42014-10-27 13:21:40 +08002434 union IO_APIC_reg_00 reg_00;
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002435 DECLARE_BITMAP(used, 256);
Yinghai Lu5411dc42014-10-27 13:21:40 +08002436 unsigned long flags;
2437 u8 new_id;
2438 int i;
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002439
2440 bitmap_zero(used, 256);
Jiang Liuf44d1692014-06-09 16:19:42 +08002441 for_each_ioapic(i)
Suresh Siddhad5371432011-05-18 16:31:37 -07002442 __set_bit(mpc_ioapic_id(i), used);
Yinghai Lu5411dc42014-10-27 13:21:40 +08002443
2444 /* Hand out the requested id if available */
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002445 if (!test_bit(id, used))
2446 return id;
Yinghai Lu5411dc42014-10-27 13:21:40 +08002447
2448 /*
2449 * Read the current id from the ioapic and keep it if
2450 * available.
2451 */
2452 raw_spin_lock_irqsave(&ioapic_lock, flags);
2453 reg_00.raw = io_apic_read(idx, 0);
2454 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2455 new_id = reg_00.bits.ID;
2456 if (!test_bit(new_id, used)) {
2457 apic_printk(APIC_VERBOSE, KERN_INFO
2458 "IOAPIC[%d]: Using reg apic_id %d instead of %d\n",
2459 idx, new_id, id);
2460 return new_id;
2461 }
2462
2463 /*
2464 * Get the next free id and write it to the ioapic.
2465 */
2466 new_id = find_first_zero_bit(used, 256);
2467 reg_00.bits.ID = new_id;
2468 raw_spin_lock_irqsave(&ioapic_lock, flags);
2469 io_apic_write(idx, 0, reg_00.raw);
2470 reg_00.raw = io_apic_read(idx, 0);
2471 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2472 /* Sanity check */
2473 BUG_ON(reg_00.bits.ID != new_id);
2474
2475 return new_id;
Thomas Gleixner41098ff2011-02-23 16:08:03 +01002476}
Naga Chumbalkar58f892e2009-05-26 21:48:07 +00002477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Jiang Liu67dc5e72014-10-27 13:21:41 +08002479static int io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 union IO_APIC_reg_01 reg_01;
2482 unsigned long flags;
2483
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002484 raw_spin_lock_irqsave(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 reg_01.raw = io_apic_read(ioapic, 1);
Thomas Gleixnerdade7712009-07-25 18:39:36 +02002486 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 return reg_01.bits.version;
2489}
2490
Eric W. Biederman9a0a91b2010-03-30 01:07:03 -07002491int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002492{
Eric W. Biederman9a0a91b2010-03-30 01:07:03 -07002493 int ioapic, pin, idx;
Shaohua Li61fd47e2007-11-17 01:05:28 -05002494
2495 if (skip_ioapic_setup)
2496 return -1;
2497
Eric W. Biederman9a0a91b2010-03-30 01:07:03 -07002498 ioapic = mp_find_ioapic(gsi);
2499 if (ioapic < 0)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002500 return -1;
2501
Eric W. Biederman9a0a91b2010-03-30 01:07:03 -07002502 pin = mp_find_ioapic_pin(ioapic, gsi);
2503 if (pin < 0)
2504 return -1;
2505
2506 idx = find_irq_entry(ioapic, pin, mp_INT);
2507 if (idx < 0)
2508 return -1;
2509
2510 *trigger = irq_trigger(idx);
2511 *polarity = irq_polarity(idx);
Shaohua Li61fd47e2007-11-17 01:05:28 -05002512 return 0;
2513}
2514
Yinghai Lu497c9a12008-08-19 20:50:28 -07002515/*
2516 * This function currently is only a helper for the i386 smp boot process where
2517 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01002518 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07002519 */
2520#ifdef CONFIG_SMP
2521void __init setup_ioapic_dest(void)
2522{
Eric W. Biedermanfad53992010-02-28 01:06:34 -08002523 int pin, ioapic, irq, irq_entry;
Mike Travis22f65d32008-12-16 17:33:56 -08002524 const struct cpumask *mask;
Thomas Gleixnere23b2572016-01-14 08:43:38 +01002525 struct irq_desc *desc;
Thomas Gleixner5451ddc2011-02-05 15:35:51 +01002526 struct irq_data *idata;
Thomas Gleixner4857c912015-09-14 12:00:55 +02002527 struct irq_chip *chip;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002528
2529 if (skip_ioapic_setup == 1)
2530 return;
2531
Jiang Liuf44d1692014-06-09 16:19:42 +08002532 for_each_ioapic_pin(ioapic, pin) {
Yinghai Lub9c61b702009-05-06 10:10:06 -07002533 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2534 if (irq_entry == -1)
2535 continue;
Yinghai Lub9c61b702009-05-06 10:10:06 -07002536
Jiang Liud7f3d472014-06-09 16:19:52 +08002537 irq = pin_2_irq(irq_entry, ioapic, pin, 0);
2538 if (irq < 0 || !mp_init_irq_at_boot(ioapic, irq))
Eric W. Biedermanfad53992010-02-28 01:06:34 -08002539 continue;
2540
Thomas Gleixnere23b2572016-01-14 08:43:38 +01002541 desc = irq_to_desc(irq);
2542 raw_spin_lock_irq(&desc->lock);
2543 idata = irq_desc_get_irq_data(desc);
Yinghai Lub9c61b702009-05-06 10:10:06 -07002544
2545 /*
2546 * Honour affinities which have been set in early boot
2547 */
Thomas Gleixner5451ddc2011-02-05 15:35:51 +01002548 if (!irqd_can_balance(idata) || irqd_affinity_was_set(idata))
Jiang Liuc149e4c2015-06-03 11:46:22 +08002549 mask = irq_data_get_affinity_mask(idata);
Yinghai Lub9c61b702009-05-06 10:10:06 -07002550 else
2551 mask = apic->target_cpus();
2552
Thomas Gleixner4857c912015-09-14 12:00:55 +02002553 chip = irq_data_get_irq_chip(idata);
Werner Pawlitschkoababae42015-10-27 09:08:04 +09002554 /* Might be lapic_chip for irq 0 */
2555 if (chip->irq_set_affinity)
2556 chip->irq_set_affinity(idata, mask, false);
Thomas Gleixnere23b2572016-01-14 08:43:38 +01002557 raw_spin_unlock_irq(&desc->lock);
Yinghai Lub9c61b702009-05-06 10:10:06 -07002558 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002559}
2560#endif
2561
Ingo Molnar54168ed2008-08-20 09:07:45 +02002562#define IOAPIC_RESOURCE_NAME_SIZE 11
2563
2564static struct resource *ioapic_resources;
2565
Jiang Liuf44d1692014-06-09 16:19:42 +08002566static struct resource * __init ioapic_setup_resources(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002567{
2568 unsigned long n;
2569 struct resource *res;
2570 char *mem;
Rui Wang48555312016-06-08 14:59:53 +08002571 int i;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002572
Rui Wang48555312016-06-08 14:59:53 +08002573 if (nr_ioapics == 0)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002574 return NULL;
2575
2576 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
Rui Wang48555312016-06-08 14:59:53 +08002577 n *= nr_ioapics;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002578
2579 mem = alloc_bootmem(n);
2580 res = (void *)mem;
2581
Rui Wang48555312016-06-08 14:59:53 +08002582 mem += sizeof(struct resource) * nr_ioapics;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002583
Jiang Liuf44d1692014-06-09 16:19:42 +08002584 for_each_ioapic(i) {
Rui Wang48555312016-06-08 14:59:53 +08002585 res[i].name = mem;
2586 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Cyrill Gorcunov4343fe12009-11-08 18:54:31 +03002587 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04002588 mem += IOAPIC_RESOURCE_NAME_SIZE;
Rui Wang48555312016-06-08 14:59:53 +08002589 ioapics[i].iomem_res = &res[i];
Ingo Molnar54168ed2008-08-20 09:07:45 +02002590 }
2591
2592 ioapic_resources = res;
2593
2594 return res;
2595}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002596
Thomas Gleixnerca1b8862015-04-24 13:57:48 +02002597void __init io_apic_init_mappings(void)
Jeremy Fitzhardinge136d2492012-03-21 22:58:08 -04002598{
Yinghai Luf3294a32008-06-27 01:41:56 -07002599 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002600 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002601 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07002602
Jiang Liuf44d1692014-06-09 16:19:42 +08002603 ioapic_res = ioapic_setup_resources();
2604 for_each_ioapic(i) {
Yinghai Luf3294a32008-06-27 01:41:56 -07002605 if (smp_found_config) {
Suresh Siddhad5371432011-05-18 16:31:37 -07002606 ioapic_phys = mpc_ioapic_addr(i);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002607#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002608 if (!ioapic_phys) {
2609 printk(KERN_ERR
2610 "WARNING: bogus zero IO-APIC "
2611 "address found in MPTABLE, "
2612 "disabling IO/APIC support!\n");
2613 smp_found_config = 0;
2614 skip_ioapic_setup = 1;
2615 goto fake_ioapic_page;
2616 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002617#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07002618 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002619#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07002620fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02002621#endif
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03002622 ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07002623 ioapic_phys = __pa(ioapic_phys);
2624 }
2625 set_fixmap_nocache(idx, ioapic_phys);
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03002626 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
2627 __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
2628 ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07002629 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002630
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04002631 ioapic_res->start = ioapic_phys;
Cyrill Gorcunove79c65a2009-11-16 18:14:26 +03002632 ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
Cyrill Gorcunovffc43832009-08-24 21:53:39 +04002633 ioapic_res++;
Yinghai Luf3294a32008-06-27 01:41:56 -07002634 }
2635}
2636
Yinghai Lu857fdc52009-07-10 09:36:20 -07002637void __init ioapic_insert_resources(void)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002638{
2639 int i;
2640 struct resource *r = ioapic_resources;
2641
2642 if (!r) {
Yinghai Lu857fdc52009-07-10 09:36:20 -07002643 if (nr_ioapics > 0)
Bartlomiej Zolnierkiewicz04c93ce2009-03-20 21:02:55 +01002644 printk(KERN_ERR
2645 "IO APIC resources couldn't be allocated.\n");
Yinghai Lu857fdc52009-07-10 09:36:20 -07002646 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002647 }
2648
Jiang Liuf44d1692014-06-09 16:19:42 +08002649 for_each_ioapic(i) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002650 insert_resource(&iomem_resource, r);
2651 r++;
2652 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002653}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002654
Eric W. Biedermaneddb0c52010-03-30 01:07:09 -07002655int mp_find_ioapic(u32 gsi)
Feng Tang2a4ab642009-07-07 23:01:15 -04002656{
Jiang Liuf44d1692014-06-09 16:19:42 +08002657 int i;
Feng Tang2a4ab642009-07-07 23:01:15 -04002658
Paul Bolle678301e2011-02-14 22:52:38 +01002659 if (nr_ioapics == 0)
2660 return -1;
2661
Feng Tang2a4ab642009-07-07 23:01:15 -04002662 /* Find the IOAPIC that manages this GSI. */
Jiang Liuf44d1692014-06-09 16:19:42 +08002663 for_each_ioapic(i) {
Suresh Siddhac040aae2011-05-18 16:31:38 -07002664 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
Jiang Liuf44d1692014-06-09 16:19:42 +08002665 if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end)
Feng Tang2a4ab642009-07-07 23:01:15 -04002666 return i;
2667 }
2668
2669 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
2670 return -1;
2671}
2672
Eric W. Biedermaneddb0c52010-03-30 01:07:09 -07002673int mp_find_ioapic_pin(int ioapic, u32 gsi)
Feng Tang2a4ab642009-07-07 23:01:15 -04002674{
Suresh Siddhac040aae2011-05-18 16:31:38 -07002675 struct mp_ioapic_gsi *gsi_cfg;
2676
Jiang Liuf44d1692014-06-09 16:19:42 +08002677 if (WARN_ON(ioapic < 0))
Feng Tang2a4ab642009-07-07 23:01:15 -04002678 return -1;
Suresh Siddhac040aae2011-05-18 16:31:38 -07002679
2680 gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2681 if (WARN_ON(gsi > gsi_cfg->gsi_end))
Feng Tang2a4ab642009-07-07 23:01:15 -04002682 return -1;
2683
Suresh Siddhac040aae2011-05-18 16:31:38 -07002684 return gsi - gsi_cfg->gsi_base;
Feng Tang2a4ab642009-07-07 23:01:15 -04002685}
2686
Jiang Liu67dc5e72014-10-27 13:21:41 +08002687static int bad_ioapic_register(int idx)
Suresh Siddha73d63d02012-03-12 11:36:33 -07002688{
2689 union IO_APIC_reg_00 reg_00;
2690 union IO_APIC_reg_01 reg_01;
2691 union IO_APIC_reg_02 reg_02;
2692
2693 reg_00.raw = io_apic_read(idx, 0);
2694 reg_01.raw = io_apic_read(idx, 1);
2695 reg_02.raw = io_apic_read(idx, 2);
2696
2697 if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
2698 pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
2699 mpc_ioapic_addr(idx));
2700 return 1;
2701 }
2702
2703 return 0;
2704}
2705
Jiang Liu35ef9c92014-10-27 13:21:43 +08002706static int find_free_ioapic_entry(void)
Feng Tang2a4ab642009-07-07 23:01:15 -04002707{
Jiang Liu7db298c2014-10-27 13:21:45 +08002708 int idx;
2709
2710 for (idx = 0; idx < MAX_IO_APICS; idx++)
2711 if (ioapics[idx].nr_registers == 0)
2712 return idx;
2713
2714 return MAX_IO_APICS;
Jiang Liu35ef9c92014-10-27 13:21:43 +08002715}
2716
2717/**
2718 * mp_register_ioapic - Register an IOAPIC device
2719 * @id: hardware IOAPIC ID
2720 * @address: physical address of IOAPIC register area
2721 * @gsi_base: base of GSI associated with the IOAPIC
2722 * @cfg: configuration information for the IOAPIC
2723 */
2724int mp_register_ioapic(int id, u32 address, u32 gsi_base,
2725 struct ioapic_domain_cfg *cfg)
2726{
Jiang Liu7db298c2014-10-27 13:21:45 +08002727 bool hotplug = !!ioapic_initialized;
Suresh Siddhac040aae2011-05-18 16:31:38 -07002728 struct mp_ioapic_gsi *gsi_cfg;
Jiang Liu35ef9c92014-10-27 13:21:43 +08002729 int idx, ioapic, entries;
2730 u32 gsi_end;
Feng Tang2a4ab642009-07-07 23:01:15 -04002731
Jiang Liu35ef9c92014-10-27 13:21:43 +08002732 if (!address) {
2733 pr_warn("Bogus (zero) I/O APIC address found, skipping!\n");
2734 return -EINVAL;
2735 }
2736 for_each_ioapic(ioapic)
2737 if (ioapics[ioapic].mp_config.apicaddr == address) {
2738 pr_warn("address 0x%x conflicts with IOAPIC%d\n",
2739 address, ioapic);
2740 return -EEXIST;
2741 }
Feng Tang2a4ab642009-07-07 23:01:15 -04002742
Jiang Liu35ef9c92014-10-27 13:21:43 +08002743 idx = find_free_ioapic_entry();
2744 if (idx >= MAX_IO_APICS) {
2745 pr_warn("Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
2746 MAX_IO_APICS, idx);
2747 return -ENOSPC;
2748 }
Feng Tang2a4ab642009-07-07 23:01:15 -04002749
Suresh Siddhad5371432011-05-18 16:31:37 -07002750 ioapics[idx].mp_config.type = MP_IOAPIC;
2751 ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
2752 ioapics[idx].mp_config.apicaddr = address;
Feng Tang2a4ab642009-07-07 23:01:15 -04002753
2754 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
Suresh Siddha73d63d02012-03-12 11:36:33 -07002755 if (bad_ioapic_register(idx)) {
2756 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
Jiang Liu35ef9c92014-10-27 13:21:43 +08002757 return -ENODEV;
Suresh Siddha73d63d02012-03-12 11:36:33 -07002758 }
2759
Yinghai Lu5411dc42014-10-27 13:21:40 +08002760 ioapics[idx].mp_config.apicid = io_apic_unique_id(idx, id);
Suresh Siddhad5371432011-05-18 16:31:37 -07002761 ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
Feng Tang2a4ab642009-07-07 23:01:15 -04002762
2763 /*
2764 * Build basic GSI lookup table to facilitate gsi->io_apic lookups
2765 * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
2766 */
Eric W. Biederman7716a5c2010-03-30 01:07:12 -07002767 entries = io_apic_get_redir_entries(idx);
Jiang Liu35ef9c92014-10-27 13:21:43 +08002768 gsi_end = gsi_base + entries - 1;
2769 for_each_ioapic(ioapic) {
2770 gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2771 if ((gsi_base >= gsi_cfg->gsi_base &&
2772 gsi_base <= gsi_cfg->gsi_end) ||
2773 (gsi_end >= gsi_cfg->gsi_base &&
2774 gsi_end <= gsi_cfg->gsi_end)) {
2775 pr_warn("GSI range [%u-%u] for new IOAPIC conflicts with GSI[%u-%u]\n",
2776 gsi_base, gsi_end,
2777 gsi_cfg->gsi_base, gsi_cfg->gsi_end);
2778 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
2779 return -ENOSPC;
2780 }
2781 }
Suresh Siddhac040aae2011-05-18 16:31:38 -07002782 gsi_cfg = mp_ioapic_gsi_routing(idx);
2783 gsi_cfg->gsi_base = gsi_base;
Jiang Liu35ef9c92014-10-27 13:21:43 +08002784 gsi_cfg->gsi_end = gsi_end;
Eric W. Biederman7716a5c2010-03-30 01:07:12 -07002785
Jiang Liu35ef9c92014-10-27 13:21:43 +08002786 ioapics[idx].irqdomain = NULL;
2787 ioapics[idx].irqdomain_cfg = *cfg;
Feng Tang2a4ab642009-07-07 23:01:15 -04002788
Jiang Liu7db298c2014-10-27 13:21:45 +08002789 /*
2790 * If mp_register_ioapic() is called during early boot stage when
2791 * walking ACPI/SFI/DT tables, it's too early to create irqdomain,
2792 * we are still using bootmem allocator. So delay it to setup_IO_APIC().
2793 */
2794 if (hotplug) {
2795 if (mp_irqdomain_create(idx)) {
2796 clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
2797 return -ENOMEM;
2798 }
2799 alloc_ioapic_saved_registers(idx);
2800 }
2801
Suresh Siddhac040aae2011-05-18 16:31:38 -07002802 if (gsi_cfg->gsi_end >= gsi_top)
2803 gsi_top = gsi_cfg->gsi_end + 1;
Jiang Liu35ef9c92014-10-27 13:21:43 +08002804 if (nr_ioapics <= idx)
2805 nr_ioapics = idx + 1;
2806
2807 /* Set nr_registers to mark entry present */
2808 ioapics[idx].nr_registers = entries;
Feng Tang2a4ab642009-07-07 23:01:15 -04002809
Suresh Siddha73d63d02012-03-12 11:36:33 -07002810 pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
2811 idx, mpc_ioapic_id(idx),
2812 mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
2813 gsi_cfg->gsi_base, gsi_cfg->gsi_end);
Feng Tang2a4ab642009-07-07 23:01:15 -04002814
Jiang Liu35ef9c92014-10-27 13:21:43 +08002815 return 0;
Feng Tang2a4ab642009-07-07 23:01:15 -04002816}
Jacob Pan05ddafb2009-09-23 07:20:23 -07002817
Jiang Liu15516a32014-10-27 13:21:46 +08002818int mp_unregister_ioapic(u32 gsi_base)
2819{
2820 int ioapic, pin;
2821 int found = 0;
Jiang Liu15516a32014-10-27 13:21:46 +08002822
2823 for_each_ioapic(ioapic)
2824 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) {
2825 found = 1;
2826 break;
2827 }
2828 if (!found) {
2829 pr_warn("can't find IOAPIC for GSI %d\n", gsi_base);
2830 return -ENODEV;
2831 }
2832
2833 for_each_pin(ioapic, pin) {
Jiang Liud32932d2015-04-13 14:11:59 +08002834 u32 gsi = mp_pin_to_gsi(ioapic, pin);
2835 int irq = mp_map_gsi_to_irq(gsi, 0, NULL);
2836 struct mp_chip_data *data;
2837
2838 if (irq >= 0) {
2839 data = irq_get_chip_data(irq);
2840 if (data && data->count) {
2841 pr_warn("pin%d on IOAPIC%d is still in use.\n",
2842 pin, ioapic);
2843 return -EBUSY;
2844 }
Jiang Liu15516a32014-10-27 13:21:46 +08002845 }
2846 }
2847
2848 /* Mark entry not present */
2849 ioapics[ioapic].nr_registers = 0;
2850 ioapic_destroy_irqdomain(ioapic);
2851 free_ioapic_saved_registers(ioapic);
2852 if (ioapics[ioapic].iomem_res)
2853 release_resource(ioapics[ioapic].iomem_res);
2854 clear_fixmap(FIX_IO_APIC_BASE_0 + ioapic);
2855 memset(&ioapics[ioapic], 0, sizeof(ioapics[ioapic]));
2856
2857 return 0;
2858}
2859
Jiang Liue89900c2014-10-27 13:21:47 +08002860int mp_ioapic_registered(u32 gsi_base)
2861{
2862 int ioapic;
2863
2864 for_each_ioapic(ioapic)
2865 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base)
2866 return 1;
2867
2868 return 0;
2869}
2870
Jiang Liu49c7e602015-04-13 14:11:55 +08002871static void mp_irqdomain_get_attr(u32 gsi, struct mp_chip_data *data,
Jiang Liu5ad274d2015-04-14 10:29:38 +08002872 struct irq_alloc_info *info)
Jiang Liu49c7e602015-04-13 14:11:55 +08002873{
2874 if (info && info->ioapic_valid) {
2875 data->trigger = info->ioapic_trigger;
2876 data->polarity = info->ioapic_polarity;
2877 } else if (acpi_get_override_irq(gsi, &data->trigger,
2878 &data->polarity) < 0) {
Thomas Gleixner335efdf2015-04-14 10:30:06 +08002879 /* PCI interrupts are always active low level triggered. */
2880 data->trigger = IOAPIC_LEVEL;
2881 data->polarity = IOAPIC_POL_LOW;
Jiang Liu49c7e602015-04-13 14:11:55 +08002882 }
2883}
2884
2885static void mp_setup_entry(struct irq_cfg *cfg, struct mp_chip_data *data,
2886 struct IO_APIC_route_entry *entry)
2887{
2888 memset(entry, 0, sizeof(*entry));
2889 entry->delivery_mode = apic->irq_delivery_mode;
2890 entry->dest_mode = apic->irq_dest_mode;
2891 entry->dest = cfg->dest_apicid;
2892 entry->vector = cfg->vector;
Jiang Liu49c7e602015-04-13 14:11:55 +08002893 entry->trigger = data->trigger;
2894 entry->polarity = data->polarity;
2895 /*
Thomas Gleixner335efdf2015-04-14 10:30:06 +08002896 * Mask level triggered irqs. Edge triggered irqs are masked
2897 * by the irq core code in case they fire.
Jiang Liu49c7e602015-04-13 14:11:55 +08002898 */
Thomas Gleixner335efdf2015-04-14 10:30:06 +08002899 if (data->trigger == IOAPIC_LEVEL)
2900 entry->mask = IOAPIC_MASKED;
2901 else
2902 entry->mask = IOAPIC_UNMASKED;
Jiang Liu49c7e602015-04-13 14:11:55 +08002903}
2904
2905int mp_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
2906 unsigned int nr_irqs, void *arg)
2907{
2908 int ret, ioapic, pin;
2909 struct irq_cfg *cfg;
2910 struct irq_data *irq_data;
2911 struct mp_chip_data *data;
2912 struct irq_alloc_info *info = arg;
Vitaly Kuznetsovc0ff9712015-10-15 19:42:23 +02002913 unsigned long flags;
Jiang Liu49c7e602015-04-13 14:11:55 +08002914
2915 if (!info || nr_irqs > 1)
2916 return -EINVAL;
2917 irq_data = irq_domain_get_irq_data(domain, virq);
2918 if (!irq_data)
2919 return -EINVAL;
2920
2921 ioapic = mp_irqdomain_ioapic_idx(domain);
2922 pin = info->ioapic_pin;
2923 if (irq_find_mapping(domain, (irq_hw_number_t)pin) > 0)
2924 return -EEXIST;
2925
2926 data = kzalloc(sizeof(*data), GFP_KERNEL);
2927 if (!data)
2928 return -ENOMEM;
2929
2930 info->ioapic_entry = &data->entry;
2931 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, info);
2932 if (ret < 0) {
2933 kfree(data);
2934 return ret;
2935 }
2936
Jiang Liu44677152015-04-14 10:29:53 +08002937 INIT_LIST_HEAD(&data->irq_2_pin);
Jiang Liu49c7e602015-04-13 14:11:55 +08002938 irq_data->hwirq = info->ioapic_pin;
Jiang Liud32932d2015-04-13 14:11:59 +08002939 irq_data->chip = (domain->parent == x86_vector_domain) ?
2940 &ioapic_chip : &ioapic_ir_chip;
Jiang Liu49c7e602015-04-13 14:11:55 +08002941 irq_data->chip_data = data;
2942 mp_irqdomain_get_attr(mp_pin_to_gsi(ioapic, pin), data, info);
2943
2944 cfg = irqd_cfg(irq_data);
Jiang Liu44677152015-04-14 10:29:53 +08002945 add_pin_to_irq_node(data, ioapic_alloc_attr_node(info), ioapic, pin);
Vitaly Kuznetsovc0ff9712015-10-15 19:42:23 +02002946
2947 local_irq_save(flags);
Jiang Liu49c7e602015-04-13 14:11:55 +08002948 if (info->ioapic_entry)
2949 mp_setup_entry(cfg, data, info->ioapic_entry);
2950 mp_register_handler(virq, data->trigger);
2951 if (virq < nr_legacy_irqs())
2952 legacy_pic->mask(virq);
Vitaly Kuznetsovc0ff9712015-10-15 19:42:23 +02002953 local_irq_restore(flags);
Jiang Liu49c7e602015-04-13 14:11:55 +08002954
2955 apic_printk(APIC_VERBOSE, KERN_DEBUG
2956 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i Dest:%d)\n",
2957 ioapic, mpc_ioapic_id(ioapic), pin, cfg->vector,
2958 virq, data->trigger, data->polarity, cfg->dest_apicid);
2959
2960 return 0;
2961}
2962
2963void mp_irqdomain_free(struct irq_domain *domain, unsigned int virq,
2964 unsigned int nr_irqs)
2965{
Jiang Liu49c7e602015-04-13 14:11:55 +08002966 struct irq_data *irq_data;
Jiang Liu44677152015-04-14 10:29:53 +08002967 struct mp_chip_data *data;
Jiang Liu49c7e602015-04-13 14:11:55 +08002968
2969 BUG_ON(nr_irqs != 1);
2970 irq_data = irq_domain_get_irq_data(domain, virq);
2971 if (irq_data && irq_data->chip_data) {
Jiang Liu44677152015-04-14 10:29:53 +08002972 data = irq_data->chip_data;
2973 __remove_pin_from_irq(data, mp_irqdomain_ioapic_idx(domain),
Jiang Liu49c7e602015-04-13 14:11:55 +08002974 (int)irq_data->hwirq);
Jiang Liu44677152015-04-14 10:29:53 +08002975 WARN_ON(!list_empty(&data->irq_2_pin));
Jiang Liu49c7e602015-04-13 14:11:55 +08002976 kfree(irq_data->chip_data);
2977 }
2978 irq_domain_free_irqs_top(domain, virq, nr_irqs);
2979}
2980
2981void mp_irqdomain_activate(struct irq_domain *domain,
2982 struct irq_data *irq_data)
2983{
2984 unsigned long flags;
2985 struct irq_pin_list *entry;
2986 struct mp_chip_data *data = irq_data->chip_data;
Jiang Liu49c7e602015-04-13 14:11:55 +08002987
2988 raw_spin_lock_irqsave(&ioapic_lock, flags);
Jiang Liu44677152015-04-14 10:29:53 +08002989 for_each_irq_pin(entry, data->irq_2_pin)
Jiang Liu49c7e602015-04-13 14:11:55 +08002990 __ioapic_write_entry(entry->apic, entry->pin, data->entry);
2991 raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2992}
2993
2994void mp_irqdomain_deactivate(struct irq_domain *domain,
2995 struct irq_data *irq_data)
2996{
2997 /* It won't be called for IRQ with multiple IOAPIC pins associated */
2998 ioapic_mask_entry(mp_irqdomain_ioapic_idx(domain),
2999 (int)irq_data->hwirq);
3000}
3001
Jiang Liu49c7e602015-04-13 14:11:55 +08003002int mp_irqdomain_ioapic_idx(struct irq_domain *domain)
3003{
3004 return (int)(long)domain->host_data;
3005}
Thomas Gleixnerf7a0c782015-04-14 10:30:08 +08003006
3007const struct irq_domain_ops mp_ioapic_irqdomain_ops = {
3008 .alloc = mp_irqdomain_alloc,
3009 .free = mp_irqdomain_free,
3010 .activate = mp_irqdomain_activate,
3011 .deactivate = mp_irqdomain_deactivate,
3012};