1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f62bae50SIngo Molnar /*
3f62bae50SIngo Molnar * Intel IO-APIC support for multi-Pentium hosts.
4f62bae50SIngo Molnar *
5f62bae50SIngo Molnar * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
6f62bae50SIngo Molnar *
7f62bae50SIngo Molnar * Many thanks to Stig Venaas for trying out countless experimental
8f62bae50SIngo Molnar * patches and reporting/debugging problems patiently!
9f62bae50SIngo Molnar *
10f62bae50SIngo Molnar * (c) 1999, Multiple IO-APIC support, developed by
11f62bae50SIngo Molnar * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
12f62bae50SIngo Molnar * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
13f62bae50SIngo Molnar * further tested and cleaned up by Zach Brown <zab@redhat.com>
14f62bae50SIngo Molnar * and Ingo Molnar <mingo@redhat.com>
15f62bae50SIngo Molnar *
16f62bae50SIngo Molnar * Fixes
17f62bae50SIngo Molnar * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
18f62bae50SIngo Molnar * thanks to Eric Gilmore
19f62bae50SIngo Molnar * and Rolf G. Tews
20f62bae50SIngo Molnar * for testing these extensively
21f62bae50SIngo Molnar * Paul Diefenbaugh : Added full ACPI support
221f934641SThomas Gleixner *
231f934641SThomas Gleixner * Historical information which is worth to be preserved:
241f934641SThomas Gleixner *
251f934641SThomas Gleixner * - SiS APIC rmw bug:
261f934641SThomas Gleixner *
271f934641SThomas Gleixner * We used to have a workaround for a bug in SiS chips which
281f934641SThomas Gleixner * required to rewrite the index register for a read-modify-write
291f934641SThomas Gleixner * operation as the chip lost the index information which was
301f934641SThomas Gleixner * setup for the read already. We cache the data now, so that
311f934641SThomas Gleixner * workaround has been removed.
32f62bae50SIngo Molnar */
33f62bae50SIngo Molnar
34f62bae50SIngo Molnar #include <linux/mm.h>
35f62bae50SIngo Molnar #include <linux/interrupt.h>
36447ae316SNicolai Stange #include <linux/irq.h>
37f62bae50SIngo Molnar #include <linux/init.h>
38f62bae50SIngo Molnar #include <linux/delay.h>
39f62bae50SIngo Molnar #include <linux/sched.h>
40f62bae50SIngo Molnar #include <linux/pci.h>
41f62bae50SIngo Molnar #include <linux/mc146818rtc.h>
42f62bae50SIngo Molnar #include <linux/compiler.h>
43f62bae50SIngo Molnar #include <linux/acpi.h>
44186f4360SPaul Gortmaker #include <linux/export.h>
45f3c6ea1bSRafael J. Wysocki #include <linux/syscore_ops.h>
46f62bae50SIngo Molnar #include <linux/freezer.h>
47f62bae50SIngo Molnar #include <linux/kthread.h>
48f62bae50SIngo Molnar #include <linux/jiffies.h> /* time_after() */
495a0e3ad6STejun Heo #include <linux/slab.h>
5057c8a661SMike Rapoport #include <linux/memblock.h>
515d5a9713SDavid Woodhouse #include <linux/msi.h>
52f62bae50SIngo Molnar
53f7a0c786SThomas Gleixner #include <asm/irqdomain.h>
54f62bae50SIngo Molnar #include <asm/io.h>
55f62bae50SIngo Molnar #include <asm/smp.h>
56f62bae50SIngo Molnar #include <asm/cpu.h>
57f62bae50SIngo Molnar #include <asm/desc.h>
58f62bae50SIngo Molnar #include <asm/proto.h>
59f62bae50SIngo Molnar #include <asm/acpi.h>
60f62bae50SIngo Molnar #include <asm/dma.h>
61f62bae50SIngo Molnar #include <asm/timer.h>
62c8c40767SThomas Gleixner #include <asm/time.h>
63f62bae50SIngo Molnar #include <asm/i8259.h>
64f62bae50SIngo Molnar #include <asm/setup.h>
658a8f422dSSuresh Siddha #include <asm/irq_remapping.h>
662c1b284eSJaswinder Singh Rajput #include <asm/hw_irq.h>
67f62bae50SIngo Molnar #include <asm/apic.h>
68f4c9361fSIsaku Yamahata #include <asm/pgtable.h>
6988e378d4SMichael Kelley #include <asm/x86_init.h>
70f62bae50SIngo Molnar
71f44d1692SJiang Liu #define for_each_ioapic(idx) \
72f44d1692SJiang Liu for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
73f44d1692SJiang Liu #define for_each_ioapic_reverse(idx) \
74f44d1692SJiang Liu for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--)
75f44d1692SJiang Liu #define for_each_pin(idx, pin) \
76f44d1692SJiang Liu for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++)
77f44d1692SJiang Liu #define for_each_ioapic_pin(idx, pin) \
78f44d1692SJiang Liu for_each_ioapic((idx)) \
79f44d1692SJiang Liu for_each_pin((idx), (pin))
802977fb3fSCyrill Gorcunov #define for_each_irq_pin(entry, head) \
81a178b87bSYinghai Lu list_for_each_entry(entry, &head, list)
82f62bae50SIngo Molnar
83dade7716SThomas Gleixner static DEFINE_RAW_SPINLOCK(ioapic_lock);
84d7f3d478SJiang Liu static DEFINE_MUTEX(ioapic_mutex);
8544767bfaSJiang Liu static unsigned int ioapic_dynirq_base;
86b81975eaSJiang Liu static int ioapic_initialized;
87f62bae50SIngo Molnar
884467715aSJiang Liu struct irq_pin_list {
894467715aSJiang Liu struct list_head list;
904467715aSJiang Liu int apic, pin;
914467715aSJiang Liu };
924467715aSJiang Liu
9349c7e600SJiang Liu struct mp_chip_data {
944467715aSJiang Liu struct list_head irq_2_pin;
9549c7e600SJiang Liu struct IO_APIC_route_entry entry;
96a27dca64SThomas Gleixner bool is_level;
97a27dca64SThomas Gleixner bool active_low;
9849c7e600SJiang Liu bool isa_irq;
99a27dca64SThomas Gleixner u32 count;
10049c7e600SJiang Liu };
10149c7e600SJiang Liu
102154d9e50SJiang Liu struct mp_ioapic_gsi {
103154d9e50SJiang Liu u32 gsi_base;
104154d9e50SJiang Liu u32 gsi_end;
105154d9e50SJiang Liu };
106154d9e50SJiang Liu
107b69c6c3bSSuresh Siddha static struct ioapic {
108f62bae50SIngo Molnar /*
109f62bae50SIngo Molnar * # of IRQ routing registers
110f62bae50SIngo Molnar */
111b69c6c3bSSuresh Siddha int nr_registers;
11257a6f740SSuresh Siddha /*
11357a6f740SSuresh Siddha * Saved state during suspend/resume, or while enabling intr-remap.
11457a6f740SSuresh Siddha */
11557a6f740SSuresh Siddha struct IO_APIC_route_entry *saved_registers;
116d5371430SSuresh Siddha /* I/O APIC config */
117d5371430SSuresh Siddha struct mpc_ioapic mp_config;
118c040aaebSSuresh Siddha /* IO APIC gsi routing info */
119c040aaebSSuresh Siddha struct mp_ioapic_gsi gsi_config;
120d7f3d478SJiang Liu struct ioapic_domain_cfg irqdomain_cfg;
121d7f3d478SJiang Liu struct irq_domain *irqdomain;
12215516a3bSJiang Liu struct resource *iomem_res;
123b69c6c3bSSuresh Siddha } ioapics[MAX_IO_APICS];
124f62bae50SIngo Molnar
1256f50d45fSYinghai Lu #define mpc_ioapic_ver(ioapic_idx) ioapics[ioapic_idx].mp_config.apicver
126d5371430SSuresh Siddha
mpc_ioapic_id(int ioapic_idx)1276f50d45fSYinghai Lu int mpc_ioapic_id(int ioapic_idx)
128d5371430SSuresh Siddha {
1296f50d45fSYinghai Lu return ioapics[ioapic_idx].mp_config.apicid;
130d5371430SSuresh Siddha }
131d5371430SSuresh Siddha
mpc_ioapic_addr(int ioapic_idx)1326f50d45fSYinghai Lu unsigned int mpc_ioapic_addr(int ioapic_idx)
133d5371430SSuresh Siddha {
1346f50d45fSYinghai Lu return ioapics[ioapic_idx].mp_config.apicaddr;
135d5371430SSuresh Siddha }
136d5371430SSuresh Siddha
mp_ioapic_gsi_routing(int ioapic_idx)137154d9e50SJiang Liu static inline struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
138c040aaebSSuresh Siddha {
1396f50d45fSYinghai Lu return &ioapics[ioapic_idx].gsi_config;
140c040aaebSSuresh Siddha }
141f62bae50SIngo Molnar
mp_ioapic_pin_count(int ioapic)14218e48551SJiang Liu static inline int mp_ioapic_pin_count(int ioapic)
14318e48551SJiang Liu {
14418e48551SJiang Liu struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
14518e48551SJiang Liu
14618e48551SJiang Liu return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
14718e48551SJiang Liu }
14818e48551SJiang Liu
mp_pin_to_gsi(int ioapic,int pin)149154d9e50SJiang Liu static inline u32 mp_pin_to_gsi(int ioapic, int pin)
15018e48551SJiang Liu {
15118e48551SJiang Liu return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin;
15218e48551SJiang Liu }
15318e48551SJiang Liu
mp_is_legacy_irq(int irq)154d32932d0SJiang Liu static inline bool mp_is_legacy_irq(int irq)
155d32932d0SJiang Liu {
156d32932d0SJiang Liu return irq >= 0 && irq < nr_legacy_irqs();
157d32932d0SJiang Liu }
158d32932d0SJiang Liu
mp_ioapic_irqdomain(int ioapic)159d7f3d478SJiang Liu static inline struct irq_domain *mp_ioapic_irqdomain(int ioapic)
160d7f3d478SJiang Liu {
161d7f3d478SJiang Liu return ioapics[ioapic].irqdomain;
162d7f3d478SJiang Liu }
163d7f3d478SJiang Liu
164c040aaebSSuresh Siddha int nr_ioapics;
1652a4ab640SFeng Tang
166a4384df3SEric W. Biederman /* The one past the highest gsi number used */
167a4384df3SEric W. Biederman u32 gsi_top;
1685777372aSEric W. Biederman
169f62bae50SIngo Molnar /* MP IRQ source entries */
170f62bae50SIngo Molnar struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
171f62bae50SIngo Molnar
172f62bae50SIngo Molnar /* # of MP IRQ source entries */
173f62bae50SIngo Molnar int mp_irq_entries;
174f62bae50SIngo Molnar
175bb8187d3SPaul Gortmaker #ifdef CONFIG_EISA
176f62bae50SIngo Molnar int mp_bus_id_to_type[MAX_MP_BUSSES];
177f62bae50SIngo Molnar #endif
178f62bae50SIngo Molnar
179f62bae50SIngo Molnar DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
180f62bae50SIngo Molnar
181ecf600f8SThomas Gleixner bool ioapic_is_disabled __ro_after_init;
182f62bae50SIngo Molnar
1837167d08eSHenrik Kretzschmar /**
1847167d08eSHenrik Kretzschmar * disable_ioapic_support() - disables ioapic support at runtime
1857167d08eSHenrik Kretzschmar */
disable_ioapic_support(void)1867167d08eSHenrik Kretzschmar void disable_ioapic_support(void)
187f62bae50SIngo Molnar {
188f62bae50SIngo Molnar #ifdef CONFIG_PCI
189f62bae50SIngo Molnar noioapicquirk = 1;
190f62bae50SIngo Molnar noioapicreroute = -1;
191f62bae50SIngo Molnar #endif
192ecf600f8SThomas Gleixner ioapic_is_disabled = true;
193f62bae50SIngo Molnar }
194f62bae50SIngo Molnar
parse_noapic(char * str)195f62bae50SIngo Molnar static int __init parse_noapic(char *str)
196f62bae50SIngo Molnar {
197f62bae50SIngo Molnar /* disable IO-APIC */
1987167d08eSHenrik Kretzschmar disable_ioapic_support();
199f62bae50SIngo Molnar return 0;
200f62bae50SIngo Molnar }
201f62bae50SIngo Molnar early_param("noapic", parse_noapic);
202f62bae50SIngo Molnar
2034590d98fSAndy Shevchenko /* Will be called in mpparse/ACPI codes for saving IRQ info */
mp_save_irq(struct mpc_intsrc * m)2042d8009baSFeng Tang void mp_save_irq(struct mpc_intsrc *m)
2052d8009baSFeng Tang {
2062d8009baSFeng Tang int i;
2072d8009baSFeng Tang
2082d8009baSFeng Tang apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
2092d8009baSFeng Tang " IRQ %02x, APIC ID %x, APIC INT %02x\n",
2102d8009baSFeng Tang m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
2112d8009baSFeng Tang m->srcbusirq, m->dstapic, m->dstirq);
2122d8009baSFeng Tang
2132d8009baSFeng Tang for (i = 0; i < mp_irq_entries; i++) {
2140e3fa13fSFeng Tang if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
2152d8009baSFeng Tang return;
2162d8009baSFeng Tang }
2172d8009baSFeng Tang
2180e3fa13fSFeng Tang memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
2192d8009baSFeng Tang if (++mp_irq_entries == MAX_IRQ_SOURCES)
2202d8009baSFeng Tang panic("Max # of irq sources exceeded!!\n");
2212d8009baSFeng Tang }
2222d8009baSFeng Tang
alloc_ioapic_saved_registers(int idx)2237e899419SYinghai Lu static void alloc_ioapic_saved_registers(int idx)
2247e899419SYinghai Lu {
2257e899419SYinghai Lu size_t size;
2267e899419SYinghai Lu
2277e899419SYinghai Lu if (ioapics[idx].saved_registers)
2287e899419SYinghai Lu return;
2297e899419SYinghai Lu
2307e899419SYinghai Lu size = sizeof(struct IO_APIC_route_entry) * ioapics[idx].nr_registers;
2317e899419SYinghai Lu ioapics[idx].saved_registers = kzalloc(size, GFP_KERNEL);
2327e899419SYinghai Lu if (!ioapics[idx].saved_registers)
2337e899419SYinghai Lu pr_err("IOAPIC %d: suspend/resume impossible!\n", idx);
2347e899419SYinghai Lu }
2357e899419SYinghai Lu
free_ioapic_saved_registers(int idx)23615516a3bSJiang Liu static void free_ioapic_saved_registers(int idx)
23715516a3bSJiang Liu {
23815516a3bSJiang Liu kfree(ioapics[idx].saved_registers);
23915516a3bSJiang Liu ioapics[idx].saved_registers = NULL;
24015516a3bSJiang Liu }
24115516a3bSJiang Liu
arch_early_ioapic_init(void)24211d686e9SJiang Liu int __init arch_early_ioapic_init(void)
243f62bae50SIngo Molnar {
24413315320SJiang Liu int i;
245f62bae50SIngo Molnar
24695d76accSJiang Liu if (!nr_legacy_irqs())
2471f91233cSJacob Pan io_apic_irqs = ~0UL;
2481f91233cSJacob Pan
2497e899419SYinghai Lu for_each_ioapic(i)
2507e899419SYinghai Lu alloc_ioapic_saved_registers(i);
2514c79185cSSuresh Siddha
252f62bae50SIngo Molnar return 0;
253f62bae50SIngo Molnar }
254f62bae50SIngo Molnar
255f62bae50SIngo Molnar struct io_apic {
256f62bae50SIngo Molnar unsigned int index;
257f62bae50SIngo Molnar unsigned int unused[3];
258f62bae50SIngo Molnar unsigned int data;
2590280f7c4SSuresh Siddha unsigned int unused2[11];
2600280f7c4SSuresh Siddha unsigned int eoi;
261f62bae50SIngo Molnar };
262f62bae50SIngo Molnar
io_apic_base(int idx)263f62bae50SIngo Molnar static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
264f62bae50SIngo Molnar {
265f62bae50SIngo Molnar return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
266d5371430SSuresh Siddha + (mpc_ioapic_addr(idx) & ~PAGE_MASK);
267f62bae50SIngo Molnar }
268f62bae50SIngo Molnar
io_apic_eoi(unsigned int apic,unsigned int vector)269ad66e1efSJiang Liu static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
2700280f7c4SSuresh Siddha {
2710280f7c4SSuresh Siddha struct io_apic __iomem *io_apic = io_apic_base(apic);
2720280f7c4SSuresh Siddha writel(vector, &io_apic->eoi);
2730280f7c4SSuresh Siddha }
2740280f7c4SSuresh Siddha
native_io_apic_read(unsigned int apic,unsigned int reg)2754a8e2a31SKonrad Rzeszutek Wilk unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
276f62bae50SIngo Molnar {
277f62bae50SIngo Molnar struct io_apic __iomem *io_apic = io_apic_base(apic);
278f62bae50SIngo Molnar writel(reg, &io_apic->index);
279f62bae50SIngo Molnar return readl(&io_apic->data);
280f62bae50SIngo Molnar }
281f62bae50SIngo Molnar
io_apic_write(unsigned int apic,unsigned int reg,unsigned int value)2829a93d473SJiang Liu static void io_apic_write(unsigned int apic, unsigned int reg,
2839a93d473SJiang Liu unsigned int value)
284f62bae50SIngo Molnar {
285f62bae50SIngo Molnar struct io_apic __iomem *io_apic = io_apic_base(apic);
286136d249eSJeremy Fitzhardinge
287f62bae50SIngo Molnar writel(reg, &io_apic->index);
288f62bae50SIngo Molnar writel(value, &io_apic->data);
289f62bae50SIngo Molnar }
290f62bae50SIngo Molnar
__ioapic_read_entry(int apic,int pin)291e57253a8SSuresh Siddha static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
292e57253a8SSuresh Siddha {
293341b4a72SThomas Gleixner struct IO_APIC_route_entry entry;
294e57253a8SSuresh Siddha
295341b4a72SThomas Gleixner entry.w1 = io_apic_read(apic, 0x10 + 2 * pin);
296341b4a72SThomas Gleixner entry.w2 = io_apic_read(apic, 0x11 + 2 * pin);
297136d249eSJeremy Fitzhardinge
298341b4a72SThomas Gleixner return entry;
299e57253a8SSuresh Siddha }
300e57253a8SSuresh Siddha
ioapic_read_entry(int apic,int pin)301f62bae50SIngo Molnar static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
302f62bae50SIngo Molnar {
303341b4a72SThomas Gleixner struct IO_APIC_route_entry entry;
304f62bae50SIngo Molnar unsigned long flags;
305136d249eSJeremy Fitzhardinge
306dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
307341b4a72SThomas Gleixner entry = __ioapic_read_entry(apic, pin);
308dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
309136d249eSJeremy Fitzhardinge
310341b4a72SThomas Gleixner return entry;
311f62bae50SIngo Molnar }
312f62bae50SIngo Molnar
313f62bae50SIngo Molnar /*
314f62bae50SIngo Molnar * When we write a new IO APIC routing entry, we need to write the high
315f62bae50SIngo Molnar * word first! If the mask bit in the low word is clear, we will enable
316f62bae50SIngo Molnar * the interrupt, and we need to make sure the entry is fully populated
317f62bae50SIngo Molnar * before that happens.
318f62bae50SIngo Molnar */
__ioapic_write_entry(int apic,int pin,struct IO_APIC_route_entry e)319136d249eSJeremy Fitzhardinge static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
320f62bae50SIngo Molnar {
321341b4a72SThomas Gleixner io_apic_write(apic, 0x11 + 2*pin, e.w2);
322341b4a72SThomas Gleixner io_apic_write(apic, 0x10 + 2*pin, e.w1);
323f62bae50SIngo Molnar }
324f62bae50SIngo Molnar
ioapic_write_entry(int apic,int pin,struct IO_APIC_route_entry e)3251a8ce7ffSThomas Gleixner static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
326f62bae50SIngo Molnar {
327f62bae50SIngo Molnar unsigned long flags;
328136d249eSJeremy Fitzhardinge
329dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
330f62bae50SIngo Molnar __ioapic_write_entry(apic, pin, e);
331dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
332f62bae50SIngo Molnar }
333f62bae50SIngo Molnar
334f62bae50SIngo Molnar /*
335f62bae50SIngo Molnar * When we mask an IO APIC routing entry, we need to write the low
336f62bae50SIngo Molnar * word first, in order to set the mask bit before we change the
337f62bae50SIngo Molnar * high bits!
338f62bae50SIngo Molnar */
ioapic_mask_entry(int apic,int pin)339f62bae50SIngo Molnar static void ioapic_mask_entry(int apic, int pin)
340f62bae50SIngo Molnar {
341341b4a72SThomas Gleixner struct IO_APIC_route_entry e = { .masked = true };
342f62bae50SIngo Molnar unsigned long flags;
343f62bae50SIngo Molnar
344dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
345341b4a72SThomas Gleixner io_apic_write(apic, 0x10 + 2*pin, e.w1);
346341b4a72SThomas Gleixner io_apic_write(apic, 0x11 + 2*pin, e.w2);
347dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
348f62bae50SIngo Molnar }
349f62bae50SIngo Molnar
350f62bae50SIngo Molnar /*
351f62bae50SIngo Molnar * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
352f62bae50SIngo Molnar * shared ISA-space IRQs, so we have to support them. We are super
353f62bae50SIngo Molnar * fast in the common case, and fast for shared ISA-space IRQs.
354f62bae50SIngo Molnar */
add_pin_to_irq_node(struct mp_chip_data * data,int node,int apic,int pin)355*077e1b7cSThomas Gleixner static bool add_pin_to_irq_node(struct mp_chip_data *data, int node, int apic, int pin)
356f62bae50SIngo Molnar {
357a178b87bSYinghai Lu struct irq_pin_list *entry;
358f62bae50SIngo Molnar
359*077e1b7cSThomas Gleixner /* Don't allow duplicates */
360*077e1b7cSThomas Gleixner for_each_irq_pin(entry, data->irq_2_pin) {
361f62bae50SIngo Molnar if (entry->apic == apic && entry->pin == pin)
362*077e1b7cSThomas Gleixner return true;
363*077e1b7cSThomas Gleixner }
364f62bae50SIngo Molnar
3654467715aSJiang Liu entry = kzalloc_node(sizeof(struct irq_pin_list), GFP_ATOMIC, node);
366a7428cd2SCyrill Gorcunov if (!entry) {
367*077e1b7cSThomas Gleixner pr_err("Cannot allocate irq_pin_list (%d,%d,%d)\n", node, apic, pin);
368*077e1b7cSThomas Gleixner return false;
369a7428cd2SCyrill Gorcunov }
370*077e1b7cSThomas Gleixner
371f62bae50SIngo Molnar entry->apic = apic;
372f62bae50SIngo Molnar entry->pin = pin;
3734467715aSJiang Liu list_add_tail(&entry->list, &data->irq_2_pin);
374*077e1b7cSThomas Gleixner return true;
375f3d1915aSCyrill Gorcunov }
376f3d1915aSCyrill Gorcunov
__remove_pin_from_irq(struct mp_chip_data * data,int apic,int pin)3774467715aSJiang Liu static void __remove_pin_from_irq(struct mp_chip_data *data, int apic, int pin)
378df334beaSJiang Liu {
379a178b87bSYinghai Lu struct irq_pin_list *tmp, *entry;
380df334beaSJiang Liu
3814467715aSJiang Liu list_for_each_entry_safe(entry, tmp, &data->irq_2_pin, list)
382df334beaSJiang Liu if (entry->apic == apic && entry->pin == pin) {
383a178b87bSYinghai Lu list_del(&entry->list);
384df334beaSJiang Liu kfree(entry);
385df334beaSJiang Liu return;
386df334beaSJiang Liu }
387df334beaSJiang Liu }
388df334beaSJiang Liu
389f62bae50SIngo Molnar /*
390f62bae50SIngo Molnar * Reroute an IRQ to a different pin.
391f62bae50SIngo Molnar */
replace_pin_at_irq_node(struct mp_chip_data * data,int node,int oldapic,int oldpin,int newapic,int newpin)3924467715aSJiang Liu static void __init replace_pin_at_irq_node(struct mp_chip_data *data, int node,
393f62bae50SIngo Molnar int oldapic, int oldpin,
394f62bae50SIngo Molnar int newapic, int newpin)
395f62bae50SIngo Molnar {
396535b6429SJeremy Fitzhardinge struct irq_pin_list *entry;
397f62bae50SIngo Molnar
3984467715aSJiang Liu for_each_irq_pin(entry, data->irq_2_pin) {
399f62bae50SIngo Molnar if (entry->apic == oldapic && entry->pin == oldpin) {
400f62bae50SIngo Molnar entry->apic = newapic;
401f62bae50SIngo Molnar entry->pin = newpin;
402f62bae50SIngo Molnar /* every one is different, right? */
4034eea6fffSJeremy Fitzhardinge return;
404f62bae50SIngo Molnar }
405f62bae50SIngo Molnar }
406f62bae50SIngo Molnar
4074eea6fffSJeremy Fitzhardinge /* old apic/pin didn't exist, so just add new ones */
4084467715aSJiang Liu add_pin_to_irq_node(data, node, newapic, newpin);
409f62bae50SIngo Molnar }
410f62bae50SIngo Molnar
io_apic_modify_irq(struct mp_chip_data * data,bool masked,void (* final)(struct irq_pin_list * entry))411341b4a72SThomas Gleixner static void io_apic_modify_irq(struct mp_chip_data *data, bool masked,
412c29d9db3SSuresh Siddha void (*final)(struct irq_pin_list *entry))
413c29d9db3SSuresh Siddha {
414c29d9db3SSuresh Siddha struct irq_pin_list *entry;
415c29d9db3SSuresh Siddha
416341b4a72SThomas Gleixner data->entry.masked = masked;
4170be275e3SJiang Liu
4180be275e3SJiang Liu for_each_irq_pin(entry, data->irq_2_pin) {
419341b4a72SThomas Gleixner io_apic_write(entry->apic, 0x10 + 2 * entry->pin, data->entry.w1);
4200be275e3SJiang Liu if (final)
4210be275e3SJiang Liu final(entry);
4220be275e3SJiang Liu }
423c29d9db3SSuresh Siddha }
424c29d9db3SSuresh Siddha
io_apic_sync(struct irq_pin_list * entry)425f62bae50SIngo Molnar static void io_apic_sync(struct irq_pin_list *entry)
426f62bae50SIngo Molnar {
427f62bae50SIngo Molnar /*
428f62bae50SIngo Molnar * Synchronize the IO-APIC and the CPU by doing
429f62bae50SIngo Molnar * a dummy read from the IO-APIC
430f62bae50SIngo Molnar */
431f62bae50SIngo Molnar struct io_apic __iomem *io_apic;
432136d249eSJeremy Fitzhardinge
433f62bae50SIngo Molnar io_apic = io_apic_base(entry->apic);
434f62bae50SIngo Molnar readl(&io_apic->data);
435f62bae50SIngo Molnar }
436f62bae50SIngo Molnar
mask_ioapic_irq(struct irq_data * irq_data)4374467715aSJiang Liu static void mask_ioapic_irq(struct irq_data *irq_data)
438f62bae50SIngo Molnar {
4394467715aSJiang Liu struct mp_chip_data *data = irq_data->chip_data;
440dd5f15e5SThomas Gleixner unsigned long flags;
441dd5f15e5SThomas Gleixner
442dd5f15e5SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
443341b4a72SThomas Gleixner io_apic_modify_irq(data, true, &io_apic_sync);
444dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
445f62bae50SIngo Molnar }
446f62bae50SIngo Molnar
__unmask_ioapic(struct mp_chip_data * data)4474467715aSJiang Liu static void __unmask_ioapic(struct mp_chip_data *data)
448f62bae50SIngo Molnar {
449341b4a72SThomas Gleixner io_apic_modify_irq(data, false, NULL);
450dd5f15e5SThomas Gleixner }
451dd5f15e5SThomas Gleixner
unmask_ioapic_irq(struct irq_data * irq_data)4524467715aSJiang Liu static void unmask_ioapic_irq(struct irq_data *irq_data)
453dd5f15e5SThomas Gleixner {
4544467715aSJiang Liu struct mp_chip_data *data = irq_data->chip_data;
455f62bae50SIngo Molnar unsigned long flags;
456f62bae50SIngo Molnar
457dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
4584467715aSJiang Liu __unmask_ioapic(data);
459dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
460f62bae50SIngo Molnar }
461f62bae50SIngo Molnar
462c0205701SSuresh Siddha /*
463c0205701SSuresh Siddha * IO-APIC versions below 0x20 don't support EOI register.
464c0205701SSuresh Siddha * For the record, here is the information about various versions:
465c0205701SSuresh Siddha * 0Xh 82489DX
466c0205701SSuresh Siddha * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
467c0205701SSuresh Siddha * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
468c0205701SSuresh Siddha * 30h-FFh Reserved
469c0205701SSuresh Siddha *
470c0205701SSuresh Siddha * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
471c0205701SSuresh Siddha * version as 0x2. This is an error with documentation and these ICH chips
472c0205701SSuresh Siddha * use io-apic's of version 0x20.
473c0205701SSuresh Siddha *
474c0205701SSuresh Siddha * For IO-APIC's with EOI register, we use that to do an explicit EOI.
475c0205701SSuresh Siddha * Otherwise, we simulate the EOI message manually by changing the trigger
476c0205701SSuresh Siddha * mode to edge and then back to level, with RTE being masked during this.
477c0205701SSuresh Siddha */
__eoi_ioapic_pin(int apic,int pin,int vector)478ad66e1efSJiang Liu static void __eoi_ioapic_pin(int apic, int pin, int vector)
479c0205701SSuresh Siddha {
480c0205701SSuresh Siddha if (mpc_ioapic_ver(apic) >= 0x20) {
481c0205701SSuresh Siddha io_apic_eoi(apic, vector);
482c0205701SSuresh Siddha } else {
483c0205701SSuresh Siddha struct IO_APIC_route_entry entry, entry1;
484c0205701SSuresh Siddha
485c0205701SSuresh Siddha entry = entry1 = __ioapic_read_entry(apic, pin);
486c0205701SSuresh Siddha
487c0205701SSuresh Siddha /*
488c0205701SSuresh Siddha * Mask the entry and change the trigger mode to edge.
489c0205701SSuresh Siddha */
490341b4a72SThomas Gleixner entry1.masked = true;
491341b4a72SThomas Gleixner entry1.is_level = false;
492c0205701SSuresh Siddha
493c0205701SSuresh Siddha __ioapic_write_entry(apic, pin, entry1);
494c0205701SSuresh Siddha
495c0205701SSuresh Siddha /*
496c0205701SSuresh Siddha * Restore the previous level triggered entry.
497c0205701SSuresh Siddha */
498c0205701SSuresh Siddha __ioapic_write_entry(apic, pin, entry);
499c0205701SSuresh Siddha }
500c0205701SSuresh Siddha }
501c0205701SSuresh Siddha
eoi_ioapic_pin(int vector,struct mp_chip_data * data)5024faefda9SAndy Shevchenko static void eoi_ioapic_pin(int vector, struct mp_chip_data *data)
503d32932d0SJiang Liu {
504d32932d0SJiang Liu unsigned long flags;
505d32932d0SJiang Liu struct irq_pin_list *entry;
506d32932d0SJiang Liu
507d32932d0SJiang Liu raw_spin_lock_irqsave(&ioapic_lock, flags);
5084467715aSJiang Liu for_each_irq_pin(entry, data->irq_2_pin)
509ad66e1efSJiang Liu __eoi_ioapic_pin(entry->apic, entry->pin, vector);
510c0205701SSuresh Siddha raw_spin_unlock_irqrestore(&ioapic_lock, flags);
511c0205701SSuresh Siddha }
512c0205701SSuresh Siddha
clear_IO_APIC_pin(unsigned int apic,unsigned int pin)513f62bae50SIngo Molnar static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
514f62bae50SIngo Molnar {
515f62bae50SIngo Molnar struct IO_APIC_route_entry entry;
516f62bae50SIngo Molnar
517f62bae50SIngo Molnar /* Check delivery_mode to be sure we're not clearing an SMI pin */
518f62bae50SIngo Molnar entry = ioapic_read_entry(apic, pin);
51972161299SThomas Gleixner if (entry.delivery_mode == APIC_DELIVERY_MODE_SMI)
520f62bae50SIngo Molnar return;
5211e75b31dSSuresh Siddha
522f62bae50SIngo Molnar /*
5231e75b31dSSuresh Siddha * Make sure the entry is masked and re-read the contents to check
5241e75b31dSSuresh Siddha * if it is a level triggered pin and if the remote-IRR is set.
5251e75b31dSSuresh Siddha */
526341b4a72SThomas Gleixner if (!entry.masked) {
527341b4a72SThomas Gleixner entry.masked = true;
5281e75b31dSSuresh Siddha ioapic_write_entry(apic, pin, entry);
5291e75b31dSSuresh Siddha entry = ioapic_read_entry(apic, pin);
5301e75b31dSSuresh Siddha }
5311e75b31dSSuresh Siddha
5321e75b31dSSuresh Siddha if (entry.irr) {
533c0205701SSuresh Siddha unsigned long flags;
534c0205701SSuresh Siddha
5351e75b31dSSuresh Siddha /*
5361e75b31dSSuresh Siddha * Make sure the trigger mode is set to level. Explicit EOI
5371e75b31dSSuresh Siddha * doesn't clear the remote-IRR if the trigger mode is not
5381e75b31dSSuresh Siddha * set to level.
5391e75b31dSSuresh Siddha */
540341b4a72SThomas Gleixner if (!entry.is_level) {
541341b4a72SThomas Gleixner entry.is_level = true;
5421e75b31dSSuresh Siddha ioapic_write_entry(apic, pin, entry);
5431e75b31dSSuresh Siddha }
5441e75b31dSSuresh Siddha raw_spin_lock_irqsave(&ioapic_lock, flags);
545ad66e1efSJiang Liu __eoi_ioapic_pin(apic, pin, entry.vector);
5461e75b31dSSuresh Siddha raw_spin_unlock_irqrestore(&ioapic_lock, flags);
5471e75b31dSSuresh Siddha }
5481e75b31dSSuresh Siddha
5491e75b31dSSuresh Siddha /*
5501e75b31dSSuresh Siddha * Clear the rest of the bits in the IO-APIC RTE except for the mask
5511e75b31dSSuresh Siddha * bit.
552f62bae50SIngo Molnar */
553f62bae50SIngo Molnar ioapic_mask_entry(apic, pin);
5541e75b31dSSuresh Siddha entry = ioapic_read_entry(apic, pin);
5551e75b31dSSuresh Siddha if (entry.irr)
556c767a54bSJoe Perches pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
5571e75b31dSSuresh Siddha mpc_ioapic_id(apic), pin);
558f62bae50SIngo Molnar }
559f62bae50SIngo Molnar
clear_IO_APIC(void)5603c9e76dbSBaoquan He void clear_IO_APIC (void)
561f62bae50SIngo Molnar {
562f62bae50SIngo Molnar int apic, pin;
563f62bae50SIngo Molnar
564f44d1692SJiang Liu for_each_ioapic_pin(apic, pin)
565f62bae50SIngo Molnar clear_IO_APIC_pin(apic, pin);
566f62bae50SIngo Molnar }
567f62bae50SIngo Molnar
568f62bae50SIngo Molnar #ifdef CONFIG_X86_32
569f62bae50SIngo Molnar /*
570f62bae50SIngo Molnar * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
571f62bae50SIngo Molnar * specific CPU-side IRQs.
572f62bae50SIngo Molnar */
573f62bae50SIngo Molnar
574f62bae50SIngo Molnar #define MAX_PIRQS 8
575f62bae50SIngo Molnar static int pirq_entries[MAX_PIRQS] = {
576f62bae50SIngo Molnar [0 ... MAX_PIRQS - 1] = -1
577f62bae50SIngo Molnar };
578f62bae50SIngo Molnar
ioapic_pirq_setup(char * str)579f62bae50SIngo Molnar static int __init ioapic_pirq_setup(char *str)
580f62bae50SIngo Molnar {
581f62bae50SIngo Molnar int i, max;
582f62bae50SIngo Molnar int ints[MAX_PIRQS+1];
583f62bae50SIngo Molnar
584f62bae50SIngo Molnar get_options(str, ARRAY_SIZE(ints), ints);
585f62bae50SIngo Molnar
586f62bae50SIngo Molnar apic_printk(APIC_VERBOSE, KERN_INFO
587f62bae50SIngo Molnar "PIRQ redirection, working around broken MP-BIOS.\n");
588f62bae50SIngo Molnar max = MAX_PIRQS;
589f62bae50SIngo Molnar if (ints[0] < MAX_PIRQS)
590f62bae50SIngo Molnar max = ints[0];
591f62bae50SIngo Molnar
592f62bae50SIngo Molnar for (i = 0; i < max; i++) {
593f62bae50SIngo Molnar apic_printk(APIC_VERBOSE, KERN_DEBUG
594f62bae50SIngo Molnar "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
595f62bae50SIngo Molnar /*
596f62bae50SIngo Molnar * PIRQs are mapped upside down, usually.
597f62bae50SIngo Molnar */
598f62bae50SIngo Molnar pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
599f62bae50SIngo Molnar }
600f62bae50SIngo Molnar return 1;
601f62bae50SIngo Molnar }
602f62bae50SIngo Molnar
603f62bae50SIngo Molnar __setup("pirq=", ioapic_pirq_setup);
604f62bae50SIngo Molnar #endif /* CONFIG_X86_32 */
605f62bae50SIngo Molnar
606f62bae50SIngo Molnar /*
60705c3dc2cSSuresh Siddha * Saves all the IO-APIC RTE's
608f62bae50SIngo Molnar */
save_ioapic_entries(void)60931dce14aSSuresh Siddha int save_ioapic_entries(void)
610f62bae50SIngo Molnar {
611f62bae50SIngo Molnar int apic, pin;
61231dce14aSSuresh Siddha int err = 0;
613f62bae50SIngo Molnar
614f44d1692SJiang Liu for_each_ioapic(apic) {
61557a6f740SSuresh Siddha if (!ioapics[apic].saved_registers) {
61631dce14aSSuresh Siddha err = -ENOMEM;
61731dce14aSSuresh Siddha continue;
61831dce14aSSuresh Siddha }
619f62bae50SIngo Molnar
620f44d1692SJiang Liu for_each_pin(apic, pin)
62157a6f740SSuresh Siddha ioapics[apic].saved_registers[pin] =
622f62bae50SIngo Molnar ioapic_read_entry(apic, pin);
623b24696bcSFenghua Yu }
624f62bae50SIngo Molnar
62531dce14aSSuresh Siddha return err;
626f62bae50SIngo Molnar }
627f62bae50SIngo Molnar
628b24696bcSFenghua Yu /*
629b24696bcSFenghua Yu * Mask all IO APIC entries.
630b24696bcSFenghua Yu */
mask_ioapic_entries(void)63131dce14aSSuresh Siddha void mask_ioapic_entries(void)
63205c3dc2cSSuresh Siddha {
63305c3dc2cSSuresh Siddha int apic, pin;
63405c3dc2cSSuresh Siddha
635f44d1692SJiang Liu for_each_ioapic(apic) {
6362f344d2eSSuresh Siddha if (!ioapics[apic].saved_registers)
63731dce14aSSuresh Siddha continue;
638b24696bcSFenghua Yu
639f44d1692SJiang Liu for_each_pin(apic, pin) {
64005c3dc2cSSuresh Siddha struct IO_APIC_route_entry entry;
64105c3dc2cSSuresh Siddha
64257a6f740SSuresh Siddha entry = ioapics[apic].saved_registers[pin];
643341b4a72SThomas Gleixner if (!entry.masked) {
644341b4a72SThomas Gleixner entry.masked = true;
64505c3dc2cSSuresh Siddha ioapic_write_entry(apic, pin, entry);
64605c3dc2cSSuresh Siddha }
64705c3dc2cSSuresh Siddha }
64805c3dc2cSSuresh Siddha }
64905c3dc2cSSuresh Siddha }
65005c3dc2cSSuresh Siddha
651b24696bcSFenghua Yu /*
65257a6f740SSuresh Siddha * Restore IO APIC entries which was saved in the ioapic structure.
653b24696bcSFenghua Yu */
restore_ioapic_entries(void)65431dce14aSSuresh Siddha int restore_ioapic_entries(void)
655f62bae50SIngo Molnar {
656f62bae50SIngo Molnar int apic, pin;
657f62bae50SIngo Molnar
658f44d1692SJiang Liu for_each_ioapic(apic) {
6592f344d2eSSuresh Siddha if (!ioapics[apic].saved_registers)
66031dce14aSSuresh Siddha continue;
661b24696bcSFenghua Yu
662f44d1692SJiang Liu for_each_pin(apic, pin)
663f62bae50SIngo Molnar ioapic_write_entry(apic, pin,
66457a6f740SSuresh Siddha ioapics[apic].saved_registers[pin]);
665f62bae50SIngo Molnar }
666b24696bcSFenghua Yu return 0;
667f62bae50SIngo Molnar }
668f62bae50SIngo Molnar
669f62bae50SIngo Molnar /*
670f62bae50SIngo Molnar * Find the IRQ entry number of a certain pin.
671f62bae50SIngo Molnar */
find_irq_entry(int ioapic_idx,int pin,int type)6726f50d45fSYinghai Lu static int find_irq_entry(int ioapic_idx, int pin, int type)
673f62bae50SIngo Molnar {
674f62bae50SIngo Molnar int i;
675f62bae50SIngo Molnar
676f62bae50SIngo Molnar for (i = 0; i < mp_irq_entries; i++)
677f62bae50SIngo Molnar if (mp_irqs[i].irqtype == type &&
6786f50d45fSYinghai Lu (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
679f62bae50SIngo Molnar mp_irqs[i].dstapic == MP_APIC_ALL) &&
680f62bae50SIngo Molnar mp_irqs[i].dstirq == pin)
681f62bae50SIngo Molnar return i;
682f62bae50SIngo Molnar
683f62bae50SIngo Molnar return -1;
684f62bae50SIngo Molnar }
685f62bae50SIngo Molnar
686f62bae50SIngo Molnar /*
687f62bae50SIngo Molnar * Find the pin to which IRQ[irq] (ISA) is connected
688f62bae50SIngo Molnar */
find_isa_irq_pin(int irq,int type)689f62bae50SIngo Molnar static int __init find_isa_irq_pin(int irq, int type)
690f62bae50SIngo Molnar {
691f62bae50SIngo Molnar int i;
692f62bae50SIngo Molnar
693f62bae50SIngo Molnar for (i = 0; i < mp_irq_entries; i++) {
694f62bae50SIngo Molnar int lbus = mp_irqs[i].srcbus;
695f62bae50SIngo Molnar
696f62bae50SIngo Molnar if (test_bit(lbus, mp_bus_not_pci) &&
697f62bae50SIngo Molnar (mp_irqs[i].irqtype == type) &&
698f62bae50SIngo Molnar (mp_irqs[i].srcbusirq == irq))
699f62bae50SIngo Molnar
700f62bae50SIngo Molnar return mp_irqs[i].dstirq;
701f62bae50SIngo Molnar }
702f62bae50SIngo Molnar return -1;
703f62bae50SIngo Molnar }
704f62bae50SIngo Molnar
find_isa_irq_apic(int irq,int type)705f62bae50SIngo Molnar static int __init find_isa_irq_apic(int irq, int type)
706f62bae50SIngo Molnar {
707f62bae50SIngo Molnar int i;
708f62bae50SIngo Molnar
709f62bae50SIngo Molnar for (i = 0; i < mp_irq_entries; i++) {
710f62bae50SIngo Molnar int lbus = mp_irqs[i].srcbus;
711f62bae50SIngo Molnar
712f62bae50SIngo Molnar if (test_bit(lbus, mp_bus_not_pci) &&
713f62bae50SIngo Molnar (mp_irqs[i].irqtype == type) &&
714f62bae50SIngo Molnar (mp_irqs[i].srcbusirq == irq))
715f62bae50SIngo Molnar break;
716f62bae50SIngo Molnar }
7176f50d45fSYinghai Lu
718f62bae50SIngo Molnar if (i < mp_irq_entries) {
7196f50d45fSYinghai Lu int ioapic_idx;
7206f50d45fSYinghai Lu
721f44d1692SJiang Liu for_each_ioapic(ioapic_idx)
7226f50d45fSYinghai Lu if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
7236f50d45fSYinghai Lu return ioapic_idx;
724f62bae50SIngo Molnar }
725f62bae50SIngo Molnar
726f62bae50SIngo Molnar return -1;
727f62bae50SIngo Molnar }
728f62bae50SIngo Molnar
irq_active_low(int idx)729a27dca64SThomas Gleixner static bool irq_active_low(int idx)
730f62bae50SIngo Molnar {
731f62bae50SIngo Molnar int bus = mp_irqs[idx].srcbus;
732f62bae50SIngo Molnar
733f62bae50SIngo Molnar /*
734f62bae50SIngo Molnar * Determine IRQ line polarity (high active or low active):
735f62bae50SIngo Molnar */
736a09c5ec0SJan Kiszka switch (mp_irqs[idx].irqflag & MP_IRQPOL_MASK) {
737a09c5ec0SJan Kiszka case MP_IRQPOL_DEFAULT:
738a27dca64SThomas Gleixner /*
739a27dca64SThomas Gleixner * Conforms to spec, ie. bus-type dependent polarity. PCI
740a27dca64SThomas Gleixner * defaults to low active. [E]ISA defaults to high active.
741a27dca64SThomas Gleixner */
742a27dca64SThomas Gleixner return !test_bit(bus, mp_bus_not_pci);
743a09c5ec0SJan Kiszka case MP_IRQPOL_ACTIVE_HIGH:
744a27dca64SThomas Gleixner return false;
745a09c5ec0SJan Kiszka case MP_IRQPOL_RESERVED:
746ab76085eSThomas Gleixner pr_warn("IOAPIC: Invalid polarity: 2, defaulting to low\n");
747df561f66SGustavo A. R. Silva fallthrough;
748a09c5ec0SJan Kiszka case MP_IRQPOL_ACTIVE_LOW:
749ab76085eSThomas Gleixner default: /* Pointless default required due to do gcc stupidity */
750a27dca64SThomas Gleixner return true;
751ab76085eSThomas Gleixner }
752ab76085eSThomas Gleixner }
753ab76085eSThomas Gleixner
754ab76085eSThomas Gleixner #ifdef CONFIG_EISA
755a27dca64SThomas Gleixner /*
756a27dca64SThomas Gleixner * EISA Edge/Level control register, ELCR
757a27dca64SThomas Gleixner */
EISA_ELCR(unsigned int irq)758a27dca64SThomas Gleixner static bool EISA_ELCR(unsigned int irq)
759a27dca64SThomas Gleixner {
760a27dca64SThomas Gleixner if (irq < nr_legacy_irqs()) {
761d2531661SMaciej W. Rozycki unsigned int port = PIC_ELCR1 + (irq >> 3);
762a27dca64SThomas Gleixner return (inb(port) >> (irq & 7)) & 1;
763a27dca64SThomas Gleixner }
764a27dca64SThomas Gleixner apic_printk(APIC_VERBOSE, KERN_INFO
765a27dca64SThomas Gleixner "Broken MPtable reports ISA irq %d\n", irq);
766a27dca64SThomas Gleixner return false;
767a27dca64SThomas Gleixner }
768a27dca64SThomas Gleixner
769a27dca64SThomas Gleixner /*
770a27dca64SThomas Gleixner * EISA interrupts are always active high and can be edge or level
771a27dca64SThomas Gleixner * triggered depending on the ELCR value. If an interrupt is listed as
772a27dca64SThomas Gleixner * EISA conforming in the MP table, that means its trigger type must be
773a27dca64SThomas Gleixner * read in from the ELCR.
774a27dca64SThomas Gleixner */
eisa_irq_is_level(int idx,int bus,bool level)775a27dca64SThomas Gleixner static bool eisa_irq_is_level(int idx, int bus, bool level)
776f62bae50SIngo Molnar {
777ab76085eSThomas Gleixner switch (mp_bus_id_to_type[bus]) {
778ab76085eSThomas Gleixner case MP_BUS_PCI:
779ab76085eSThomas Gleixner case MP_BUS_ISA:
780a27dca64SThomas Gleixner return level;
781ab76085eSThomas Gleixner case MP_BUS_EISA:
782a27dca64SThomas Gleixner return EISA_ELCR(mp_irqs[idx].srcbusirq);
783f62bae50SIngo Molnar }
784ab76085eSThomas Gleixner pr_warn("IOAPIC: Invalid srcbus: %d defaulting to level\n", bus);
785a27dca64SThomas Gleixner return true;
786ab76085eSThomas Gleixner }
787ab76085eSThomas Gleixner #else
eisa_irq_is_level(int idx,int bus,bool level)788a27dca64SThomas Gleixner static inline int eisa_irq_is_level(int idx, int bus, bool level)
789f62bae50SIngo Molnar {
790a27dca64SThomas Gleixner return level;
791f62bae50SIngo Molnar }
792ab76085eSThomas Gleixner #endif
793f62bae50SIngo Molnar
irq_is_level(int idx)794a27dca64SThomas Gleixner static bool irq_is_level(int idx)
795f62bae50SIngo Molnar {
796f62bae50SIngo Molnar int bus = mp_irqs[idx].srcbus;
797a27dca64SThomas Gleixner bool level;
798f62bae50SIngo Molnar
799f62bae50SIngo Molnar /*
800f62bae50SIngo Molnar * Determine IRQ trigger mode (edge or level sensitive):
801f62bae50SIngo Molnar */
802a09c5ec0SJan Kiszka switch (mp_irqs[idx].irqflag & MP_IRQTRIG_MASK) {
803a09c5ec0SJan Kiszka case MP_IRQTRIG_DEFAULT:
804a27dca64SThomas Gleixner /*
805a27dca64SThomas Gleixner * Conforms to spec, ie. bus-type dependent trigger
806aec8da04SThomas Gleixner * mode. PCI defaults to level, ISA to edge.
807a27dca64SThomas Gleixner */
808aec8da04SThomas Gleixner level = !test_bit(bus, mp_bus_not_pci);
809ab76085eSThomas Gleixner /* Take EISA into account */
810a27dca64SThomas Gleixner return eisa_irq_is_level(idx, bus, level);
811a09c5ec0SJan Kiszka case MP_IRQTRIG_EDGE:
812a27dca64SThomas Gleixner return false;
813a09c5ec0SJan Kiszka case MP_IRQTRIG_RESERVED:
814ab76085eSThomas Gleixner pr_warn("IOAPIC: Invalid trigger mode 2 defaulting to level\n");
815df561f66SGustavo A. R. Silva fallthrough;
816a09c5ec0SJan Kiszka case MP_IRQTRIG_LEVEL:
817ab76085eSThomas Gleixner default: /* Pointless default required due to do gcc stupidity */
818a27dca64SThomas Gleixner return true;
819f62bae50SIngo Molnar }
820f62bae50SIngo Molnar }
821f62bae50SIngo Molnar
__acpi_get_override_irq(u32 gsi,bool * trigger,bool * polarity)822a27dca64SThomas Gleixner static int __acpi_get_override_irq(u32 gsi, bool *trigger, bool *polarity)
823a27dca64SThomas Gleixner {
824a27dca64SThomas Gleixner int ioapic, pin, idx;
825a27dca64SThomas Gleixner
826ecf600f8SThomas Gleixner if (ioapic_is_disabled)
827a27dca64SThomas Gleixner return -1;
828a27dca64SThomas Gleixner
829a27dca64SThomas Gleixner ioapic = mp_find_ioapic(gsi);
830a27dca64SThomas Gleixner if (ioapic < 0)
831a27dca64SThomas Gleixner return -1;
832a27dca64SThomas Gleixner
833a27dca64SThomas Gleixner pin = mp_find_ioapic_pin(ioapic, gsi);
834a27dca64SThomas Gleixner if (pin < 0)
835a27dca64SThomas Gleixner return -1;
836a27dca64SThomas Gleixner
837a27dca64SThomas Gleixner idx = find_irq_entry(ioapic, pin, mp_INT);
838a27dca64SThomas Gleixner if (idx < 0)
839a27dca64SThomas Gleixner return -1;
840a27dca64SThomas Gleixner
841a27dca64SThomas Gleixner *trigger = irq_is_level(idx);
842a27dca64SThomas Gleixner *polarity = irq_active_low(idx);
843a27dca64SThomas Gleixner return 0;
844a27dca64SThomas Gleixner }
845a27dca64SThomas Gleixner
846a27dca64SThomas Gleixner #ifdef CONFIG_ACPI
acpi_get_override_irq(u32 gsi,int * is_level,int * active_low)847a27dca64SThomas Gleixner int acpi_get_override_irq(u32 gsi, int *is_level, int *active_low)
848a27dca64SThomas Gleixner {
849a27dca64SThomas Gleixner *is_level = *active_low = 0;
850a27dca64SThomas Gleixner return __acpi_get_override_irq(gsi, (bool *)is_level,
851a27dca64SThomas Gleixner (bool *)active_low);
852a27dca64SThomas Gleixner }
853a27dca64SThomas Gleixner #endif
854a27dca64SThomas Gleixner
ioapic_set_alloc_attr(struct irq_alloc_info * info,int node,int trigger,int polarity)855c4d05a2cSJiang Liu void ioapic_set_alloc_attr(struct irq_alloc_info *info, int node,
856c4d05a2cSJiang Liu int trigger, int polarity)
857c4d05a2cSJiang Liu {
858c4d05a2cSJiang Liu init_irq_alloc_info(info, NULL);
859c4d05a2cSJiang Liu info->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
86033a65ba4SThomas Gleixner info->ioapic.node = node;
861a27dca64SThomas Gleixner info->ioapic.is_level = trigger;
862a27dca64SThomas Gleixner info->ioapic.active_low = polarity;
86333a65ba4SThomas Gleixner info->ioapic.valid = 1;
864c4d05a2cSJiang Liu }
865c4d05a2cSJiang Liu
ioapic_copy_alloc_attr(struct irq_alloc_info * dst,struct irq_alloc_info * src,u32 gsi,int ioapic_idx,int pin)86696ed44b2SJiang Liu static void ioapic_copy_alloc_attr(struct irq_alloc_info *dst,
86796ed44b2SJiang Liu struct irq_alloc_info *src,
86896ed44b2SJiang Liu u32 gsi, int ioapic_idx, int pin)
86996ed44b2SJiang Liu {
870a27dca64SThomas Gleixner bool level, pol_low;
87196ed44b2SJiang Liu
87296ed44b2SJiang Liu copy_irq_alloc_info(dst, src);
87396ed44b2SJiang Liu dst->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
87433a65ba4SThomas Gleixner dst->devid = mpc_ioapic_id(ioapic_idx);
87533a65ba4SThomas Gleixner dst->ioapic.pin = pin;
87633a65ba4SThomas Gleixner dst->ioapic.valid = 1;
87733a65ba4SThomas Gleixner if (src && src->ioapic.valid) {
87833a65ba4SThomas Gleixner dst->ioapic.node = src->ioapic.node;
879a27dca64SThomas Gleixner dst->ioapic.is_level = src->ioapic.is_level;
880a27dca64SThomas Gleixner dst->ioapic.active_low = src->ioapic.active_low;
88196ed44b2SJiang Liu } else {
88233a65ba4SThomas Gleixner dst->ioapic.node = NUMA_NO_NODE;
883a27dca64SThomas Gleixner if (__acpi_get_override_irq(gsi, &level, &pol_low) >= 0) {
884a27dca64SThomas Gleixner dst->ioapic.is_level = level;
885a27dca64SThomas Gleixner dst->ioapic.active_low = pol_low;
88696ed44b2SJiang Liu } else {
88796ed44b2SJiang Liu /*
888335efdf5SThomas Gleixner * PCI interrupts are always active low level
88996ed44b2SJiang Liu * triggered.
89096ed44b2SJiang Liu */
891a27dca64SThomas Gleixner dst->ioapic.is_level = true;
892a27dca64SThomas Gleixner dst->ioapic.active_low = true;
89396ed44b2SJiang Liu }
89496ed44b2SJiang Liu }
89596ed44b2SJiang Liu }
89696ed44b2SJiang Liu
ioapic_alloc_attr_node(struct irq_alloc_info * info)89796ed44b2SJiang Liu static int ioapic_alloc_attr_node(struct irq_alloc_info *info)
89896ed44b2SJiang Liu {
89933a65ba4SThomas Gleixner return (info && info->ioapic.valid) ? info->ioapic.node : NUMA_NO_NODE;
90096ed44b2SJiang Liu }
90196ed44b2SJiang Liu
mp_register_handler(unsigned int irq,bool level)902a27dca64SThomas Gleixner static void mp_register_handler(unsigned int irq, bool level)
90349c7e600SJiang Liu {
90449c7e600SJiang Liu irq_flow_handler_t hdl;
90549c7e600SJiang Liu bool fasteoi;
90649c7e600SJiang Liu
907a27dca64SThomas Gleixner if (level) {
90849c7e600SJiang Liu irq_set_status_flags(irq, IRQ_LEVEL);
90949c7e600SJiang Liu fasteoi = true;
91049c7e600SJiang Liu } else {
91149c7e600SJiang Liu irq_clear_status_flags(irq, IRQ_LEVEL);
91249c7e600SJiang Liu fasteoi = false;
91349c7e600SJiang Liu }
91449c7e600SJiang Liu
91549c7e600SJiang Liu hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
91649c7e600SJiang Liu __irq_set_handler(irq, hdl, 0, fasteoi ? "fasteoi" : "edge");
91749c7e600SJiang Liu }
91849c7e600SJiang Liu
mp_check_pin_attr(int irq,struct irq_alloc_info * info)91996ed44b2SJiang Liu static bool mp_check_pin_attr(int irq, struct irq_alloc_info *info)
92096ed44b2SJiang Liu {
92196ed44b2SJiang Liu struct mp_chip_data *data = irq_get_chip_data(irq);
92296ed44b2SJiang Liu
92396ed44b2SJiang Liu /*
92496ed44b2SJiang Liu * setup_IO_APIC_irqs() programs all legacy IRQs with default trigger
925d9f6e12fSIngo Molnar * and polarity attributes. So allow the first user to reprogram the
92696ed44b2SJiang Liu * pin with real trigger and polarity attributes.
92796ed44b2SJiang Liu */
92896ed44b2SJiang Liu if (irq < nr_legacy_irqs() && data->count == 1) {
929a27dca64SThomas Gleixner if (info->ioapic.is_level != data->is_level)
930a27dca64SThomas Gleixner mp_register_handler(irq, info->ioapic.is_level);
931341b4a72SThomas Gleixner data->entry.is_level = data->is_level = info->ioapic.is_level;
932341b4a72SThomas Gleixner data->entry.active_low = data->active_low = info->ioapic.active_low;
93396ed44b2SJiang Liu }
93496ed44b2SJiang Liu
935a27dca64SThomas Gleixner return data->is_level == info->ioapic.is_level &&
936a27dca64SThomas Gleixner data->active_low == info->ioapic.active_low;
93796ed44b2SJiang Liu }
93896ed44b2SJiang Liu
alloc_irq_from_domain(struct irq_domain * domain,int ioapic,u32 gsi,struct irq_alloc_info * info)939d32932d0SJiang Liu static int alloc_irq_from_domain(struct irq_domain *domain, int ioapic, u32 gsi,
940c4d05a2cSJiang Liu struct irq_alloc_info *info)
9416b9fb708SJiang Liu {
942d32932d0SJiang Liu bool legacy = false;
943d7f3d478SJiang Liu int irq = -1;
944d7f3d478SJiang Liu int type = ioapics[ioapic].irqdomain_cfg.type;
945d7f3d478SJiang Liu
946d7f3d478SJiang Liu switch (type) {
947d7f3d478SJiang Liu case IOAPIC_DOMAIN_LEGACY:
9486b9fb708SJiang Liu /*
949d32932d0SJiang Liu * Dynamically allocate IRQ number for non-ISA IRQs in the first
950d32932d0SJiang Liu * 16 GSIs on some weird platforms.
951d7f3d478SJiang Liu */
952d32932d0SJiang Liu if (!ioapic_initialized || gsi >= nr_legacy_irqs())
953d7f3d478SJiang Liu irq = gsi;
954d32932d0SJiang Liu legacy = mp_is_legacy_irq(irq);
955d7f3d478SJiang Liu break;
956d7f3d478SJiang Liu case IOAPIC_DOMAIN_STRICT:
957d7f3d478SJiang Liu irq = gsi;
958d7f3d478SJiang Liu break;
959d7f3d478SJiang Liu case IOAPIC_DOMAIN_DYNAMIC:
960d7f3d478SJiang Liu break;
961d7f3d478SJiang Liu default:
962d7f3d478SJiang Liu WARN(1, "ioapic: unknown irqdomain type %d\n", type);
963d32932d0SJiang Liu return -1;
964d7f3d478SJiang Liu }
965d7f3d478SJiang Liu
966d32932d0SJiang Liu return __irq_domain_alloc_irqs(domain, irq, 1,
967d32932d0SJiang Liu ioapic_alloc_attr_node(info),
96806ee6d57SThomas Gleixner info, legacy, NULL);
969d32932d0SJiang Liu }
970d32932d0SJiang Liu
971d32932d0SJiang Liu /*
972d32932d0SJiang Liu * Need special handling for ISA IRQs because there may be multiple IOAPIC pins
973d32932d0SJiang Liu * sharing the same ISA IRQ number and irqdomain only supports 1:1 mapping
974d32932d0SJiang Liu * between IOAPIC pin and IRQ number. A typical IOAPIC has 24 pins, pin 0-15 are
975d32932d0SJiang Liu * used for legacy IRQs and pin 16-23 are used for PCI IRQs (PIRQ A-H).
976d32932d0SJiang Liu * When ACPI is disabled, only legacy IRQ numbers (IRQ0-15) are available, and
977d32932d0SJiang Liu * some BIOSes may use MP Interrupt Source records to override IRQ numbers for
978d32932d0SJiang Liu * PIRQs instead of reprogramming the interrupt routing logic. Thus there may be
979d32932d0SJiang Liu * multiple pins sharing the same legacy IRQ number when ACPI is disabled.
980d32932d0SJiang Liu */
alloc_isa_irq_from_domain(struct irq_domain * domain,int irq,int ioapic,int pin,struct irq_alloc_info * info)981d32932d0SJiang Liu static int alloc_isa_irq_from_domain(struct irq_domain *domain,
982d32932d0SJiang Liu int irq, int ioapic, int pin,
983d32932d0SJiang Liu struct irq_alloc_info *info)
984d32932d0SJiang Liu {
985d32932d0SJiang Liu struct mp_chip_data *data;
986d32932d0SJiang Liu struct irq_data *irq_data = irq_get_irq_data(irq);
987d32932d0SJiang Liu int node = ioapic_alloc_attr_node(info);
988d32932d0SJiang Liu
989d32932d0SJiang Liu /*
990d32932d0SJiang Liu * Legacy ISA IRQ has already been allocated, just add pin to
991d9f6e12fSIngo Molnar * the pin list associated with this IRQ and program the IOAPIC
992d32932d0SJiang Liu * entry. The IOAPIC entry
993d32932d0SJiang Liu */
994d32932d0SJiang Liu if (irq_data && irq_data->parent_data) {
995d32932d0SJiang Liu if (!mp_check_pin_attr(irq, info))
996d32932d0SJiang Liu return -EBUSY;
997*077e1b7cSThomas Gleixner if (!add_pin_to_irq_node(irq_data->chip_data, node, ioapic, info->ioapic.pin))
998d32932d0SJiang Liu return -ENOMEM;
999d32932d0SJiang Liu } else {
10003534be05SThomas Gleixner info->flags |= X86_IRQ_ALLOC_LEGACY;
100106ee6d57SThomas Gleixner irq = __irq_domain_alloc_irqs(domain, irq, 1, node, info, true,
100206ee6d57SThomas Gleixner NULL);
1003d32932d0SJiang Liu if (irq >= 0) {
1004d32932d0SJiang Liu irq_data = irq_domain_get_irq_data(domain, irq);
1005d32932d0SJiang Liu data = irq_data->chip_data;
1006d32932d0SJiang Liu data->isa_irq = true;
1007d32932d0SJiang Liu }
1008d32932d0SJiang Liu }
1009d32932d0SJiang Liu
1010d32932d0SJiang Liu return irq;
1011d7f3d478SJiang Liu }
1012d7f3d478SJiang Liu
mp_map_pin_to_irq(u32 gsi,int idx,int ioapic,int pin,unsigned int flags,struct irq_alloc_info * info)1013d7f3d478SJiang Liu static int mp_map_pin_to_irq(u32 gsi, int idx, int ioapic, int pin,
1014c4d05a2cSJiang Liu unsigned int flags, struct irq_alloc_info *info)
1015d7f3d478SJiang Liu {
1016d7f3d478SJiang Liu int irq;
1017d32932d0SJiang Liu bool legacy = false;
1018d32932d0SJiang Liu struct irq_alloc_info tmp;
1019d32932d0SJiang Liu struct mp_chip_data *data;
1020d7f3d478SJiang Liu struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
1021d7f3d478SJiang Liu
1022b81975eaSJiang Liu if (!domain)
1023d32932d0SJiang Liu return -ENOSYS;
102416ee7b3dSJiang Liu
102516ee7b3dSJiang Liu if (idx >= 0 && test_bit(mp_irqs[idx].srcbus, mp_bus_not_pci)) {
102616ee7b3dSJiang Liu irq = mp_irqs[idx].srcbusirq;
1027d32932d0SJiang Liu legacy = mp_is_legacy_irq(irq);
1028a501b048SThomas Gleixner /*
1029a501b048SThomas Gleixner * IRQ2 is unusable for historical reasons on systems which
1030a501b048SThomas Gleixner * have a legacy PIC. See the comment vs. IRQ2 further down.
1031a501b048SThomas Gleixner *
1032a501b048SThomas Gleixner * If this gets removed at some point then the related code
1033a501b048SThomas Gleixner * in lapic_assign_system_vectors() needs to be adjusted as
1034a501b048SThomas Gleixner * well.
1035a501b048SThomas Gleixner */
1036a501b048SThomas Gleixner if (legacy && irq == PIC_CASCADE_IR)
1037a501b048SThomas Gleixner return -EINVAL;
1038d32932d0SJiang Liu }
1039d7f3d478SJiang Liu
1040d32932d0SJiang Liu mutex_lock(&ioapic_mutex);
1041d32932d0SJiang Liu if (!(flags & IOAPIC_MAP_ALLOC)) {
1042d32932d0SJiang Liu if (!legacy) {
1043d32932d0SJiang Liu irq = irq_find_mapping(domain, pin);
104416ee7b3dSJiang Liu if (irq == 0)
1045d32932d0SJiang Liu irq = -ENOENT;
10466b9fb708SJiang Liu }
104716ee7b3dSJiang Liu } else {
1048d32932d0SJiang Liu ioapic_copy_alloc_attr(&tmp, info, gsi, ioapic, pin);
1049d32932d0SJiang Liu if (legacy)
1050d32932d0SJiang Liu irq = alloc_isa_irq_from_domain(domain, irq,
1051d32932d0SJiang Liu ioapic, pin, &tmp);
1052d32932d0SJiang Liu else if ((irq = irq_find_mapping(domain, pin)) == 0)
1053d32932d0SJiang Liu irq = alloc_irq_from_domain(domain, ioapic, gsi, &tmp);
1054d32932d0SJiang Liu else if (!mp_check_pin_attr(irq, &tmp))
1055d32932d0SJiang Liu irq = -EBUSY;
1056d32932d0SJiang Liu if (irq >= 0) {
1057d32932d0SJiang Liu data = irq_get_chip_data(irq);
1058d32932d0SJiang Liu data->count++;
105916ee7b3dSJiang Liu }
106015a3c7ccSJiang Liu }
1061d7f3d478SJiang Liu mutex_unlock(&ioapic_mutex);
1062d7f3d478SJiang Liu
1063d32932d0SJiang Liu return irq;
1064d7f3d478SJiang Liu }
1065d7f3d478SJiang Liu
pin_2_irq(int idx,int ioapic,int pin,unsigned int flags)1066d7f3d478SJiang Liu static int pin_2_irq(int idx, int ioapic, int pin, unsigned int flags)
1067f62bae50SIngo Molnar {
1068d7f3d478SJiang Liu u32 gsi = mp_pin_to_gsi(ioapic, pin);
1069f62bae50SIngo Molnar
1070f62bae50SIngo Molnar /*
1071f62bae50SIngo Molnar * Debugging check, we are in big trouble if this message pops up!
1072f62bae50SIngo Molnar */
1073f62bae50SIngo Molnar if (mp_irqs[idx].dstirq != pin)
1074c767a54bSJoe Perches pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
1075f62bae50SIngo Molnar
1076f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1077f62bae50SIngo Molnar /*
1078f62bae50SIngo Molnar * PCI IRQ command line redirection. Yes, limits are hardcoded.
1079f62bae50SIngo Molnar */
1080f62bae50SIngo Molnar if ((pin >= 16) && (pin <= 23)) {
1081f62bae50SIngo Molnar if (pirq_entries[pin-16] != -1) {
1082f62bae50SIngo Molnar if (!pirq_entries[pin-16]) {
1083f62bae50SIngo Molnar apic_printk(APIC_VERBOSE, KERN_DEBUG
1084f62bae50SIngo Molnar "disabling PIRQ%d\n", pin-16);
1085f62bae50SIngo Molnar } else {
1086d7f3d478SJiang Liu int irq = pirq_entries[pin-16];
1087f62bae50SIngo Molnar apic_printk(APIC_VERBOSE, KERN_DEBUG
1088f62bae50SIngo Molnar "using PIRQ%d -> IRQ %d\n",
1089f62bae50SIngo Molnar pin-16, irq);
10906b9fb708SJiang Liu return irq;
1091f62bae50SIngo Molnar }
1092f62bae50SIngo Molnar }
1093f62bae50SIngo Molnar }
1094f62bae50SIngo Molnar #endif
1095f62bae50SIngo Molnar
1096c4d05a2cSJiang Liu return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, NULL);
1097d7f3d478SJiang Liu }
10986b9fb708SJiang Liu
mp_map_gsi_to_irq(u32 gsi,unsigned int flags,struct irq_alloc_info * info)1099154d9e50SJiang Liu int mp_map_gsi_to_irq(u32 gsi, unsigned int flags, struct irq_alloc_info *info)
1100d7f3d478SJiang Liu {
1101d7f3d478SJiang Liu int ioapic, pin, idx;
1102d7f3d478SJiang Liu
1103d7f3d478SJiang Liu ioapic = mp_find_ioapic(gsi);
1104d7f3d478SJiang Liu if (ioapic < 0)
1105358e96deSAndy Shevchenko return -ENODEV;
1106d7f3d478SJiang Liu
1107d7f3d478SJiang Liu pin = mp_find_ioapic_pin(ioapic, gsi);
1108d7f3d478SJiang Liu idx = find_irq_entry(ioapic, pin, mp_INT);
1109d7f3d478SJiang Liu if ((flags & IOAPIC_MAP_CHECK) && idx < 0)
1110358e96deSAndy Shevchenko return -ENODEV;
1111d7f3d478SJiang Liu
1112c4d05a2cSJiang Liu return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, info);
1113f62bae50SIngo Molnar }
1114f62bae50SIngo Molnar
mp_unmap_irq(int irq)1115df334beaSJiang Liu void mp_unmap_irq(int irq)
1116df334beaSJiang Liu {
1117d32932d0SJiang Liu struct irq_data *irq_data = irq_get_irq_data(irq);
1118d32932d0SJiang Liu struct mp_chip_data *data;
1119df334beaSJiang Liu
1120d32932d0SJiang Liu if (!irq_data || !irq_data->domain)
1121df334beaSJiang Liu return;
1122df334beaSJiang Liu
1123d32932d0SJiang Liu data = irq_data->chip_data;
1124d32932d0SJiang Liu if (!data || data->isa_irq)
1125d32932d0SJiang Liu return;
1126df334beaSJiang Liu
1127df334beaSJiang Liu mutex_lock(&ioapic_mutex);
1128d32932d0SJiang Liu if (--data->count == 0)
1129d32932d0SJiang Liu irq_domain_free_irqs(irq, 1);
1130df334beaSJiang Liu mutex_unlock(&ioapic_mutex);
1131df334beaSJiang Liu }
1132df334beaSJiang Liu
1133e20c06fdSYinghai Lu /*
1134e20c06fdSYinghai Lu * Find a specific PCI IRQ entry.
1135e20c06fdSYinghai Lu * Not an __init, possibly needed by modules
1136e20c06fdSYinghai Lu */
IO_APIC_get_PCI_irq_vector(int bus,int slot,int pin)113725d0d35eSJiang Liu int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
1138e20c06fdSYinghai Lu {
1139d7f3d478SJiang Liu int irq, i, best_ioapic = -1, best_idx = -1;
1140e20c06fdSYinghai Lu
1141e20c06fdSYinghai Lu apic_printk(APIC_DEBUG,
1142e20c06fdSYinghai Lu "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1143e20c06fdSYinghai Lu bus, slot, pin);
1144e20c06fdSYinghai Lu if (test_bit(bus, mp_bus_not_pci)) {
1145e20c06fdSYinghai Lu apic_printk(APIC_VERBOSE,
1146e20c06fdSYinghai Lu "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1147e20c06fdSYinghai Lu return -1;
1148e20c06fdSYinghai Lu }
114979598505SJiang Liu
1150e20c06fdSYinghai Lu for (i = 0; i < mp_irq_entries; i++) {
1151e20c06fdSYinghai Lu int lbus = mp_irqs[i].srcbus;
115279598505SJiang Liu int ioapic_idx, found = 0;
115379598505SJiang Liu
115479598505SJiang Liu if (bus != lbus || mp_irqs[i].irqtype != mp_INT ||
115579598505SJiang Liu slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f))
115679598505SJiang Liu continue;
1157e20c06fdSYinghai Lu
1158f44d1692SJiang Liu for_each_ioapic(ioapic_idx)
11596f50d45fSYinghai Lu if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
116079598505SJiang Liu mp_irqs[i].dstapic == MP_APIC_ALL) {
116179598505SJiang Liu found = 1;
1162e20c06fdSYinghai Lu break;
116379598505SJiang Liu }
116479598505SJiang Liu if (!found)
116579598505SJiang Liu continue;
1166e20c06fdSYinghai Lu
116779598505SJiang Liu /* Skip ISA IRQs */
1168d7f3d478SJiang Liu irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq, 0);
1169d7f3d478SJiang Liu if (irq > 0 && !IO_APIC_IRQ(irq))
1170e20c06fdSYinghai Lu continue;
1171e20c06fdSYinghai Lu
1172e20c06fdSYinghai Lu if (pin == (mp_irqs[i].srcbusirq & 3)) {
1173d7f3d478SJiang Liu best_idx = i;
1174d7f3d478SJiang Liu best_ioapic = ioapic_idx;
1175d7f3d478SJiang Liu goto out;
1176e20c06fdSYinghai Lu }
1177d7f3d478SJiang Liu
1178e20c06fdSYinghai Lu /*
1179e20c06fdSYinghai Lu * Use the first all-but-pin matching entry as a
1180e20c06fdSYinghai Lu * best-guess fuzzy result for broken mptables.
1181e20c06fdSYinghai Lu */
1182d7f3d478SJiang Liu if (best_idx < 0) {
1183d7f3d478SJiang Liu best_idx = i;
1184d7f3d478SJiang Liu best_ioapic = ioapic_idx;
1185e20c06fdSYinghai Lu }
1186e20c06fdSYinghai Lu }
1187d7f3d478SJiang Liu if (best_idx < 0)
1188d7f3d478SJiang Liu return -1;
1189d7f3d478SJiang Liu
1190d7f3d478SJiang Liu out:
119125d0d35eSJiang Liu return pin_2_irq(best_idx, best_ioapic, mp_irqs[best_idx].dstirq,
1192d7f3d478SJiang Liu IOAPIC_MAP_ALLOC);
1193e20c06fdSYinghai Lu }
1194e20c06fdSYinghai Lu EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1195e20c06fdSYinghai Lu
1196d32932d0SJiang Liu static struct irq_chip ioapic_chip, ioapic_ir_chip;
1197f62bae50SIngo Molnar
setup_IO_APIC_irqs(void)1198ed972ccfSThomas Gleixner static void __init setup_IO_APIC_irqs(void)
1199ed972ccfSThomas Gleixner {
120016ee7b3dSJiang Liu unsigned int ioapic, pin;
120116ee7b3dSJiang Liu int idx;
1202ed972ccfSThomas Gleixner
1203ed972ccfSThomas Gleixner apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1204ed972ccfSThomas Gleixner
120516ee7b3dSJiang Liu for_each_ioapic_pin(ioapic, pin) {
120616ee7b3dSJiang Liu idx = find_irq_entry(ioapic, pin, mp_INT);
120716ee7b3dSJiang Liu if (idx < 0)
120816ee7b3dSJiang Liu apic_printk(APIC_VERBOSE,
120916ee7b3dSJiang Liu KERN_DEBUG " apic %d pin %d not connected\n",
121016ee7b3dSJiang Liu mpc_ioapic_id(ioapic), pin);
121116ee7b3dSJiang Liu else
121216ee7b3dSJiang Liu pin_2_irq(idx, ioapic, pin,
121316ee7b3dSJiang Liu ioapic ? 0 : IOAPIC_MAP_ALLOC);
121416ee7b3dSJiang Liu }
1215ed972ccfSThomas Gleixner }
1216ed972ccfSThomas Gleixner
ioapic_zap_locks(void)121717405453SYoshihiro YUNOMAE void ioapic_zap_locks(void)
121817405453SYoshihiro YUNOMAE {
121917405453SYoshihiro YUNOMAE raw_spin_lock_init(&ioapic_lock);
122017405453SYoshihiro YUNOMAE }
122117405453SYoshihiro YUNOMAE
io_apic_print_entries(unsigned int apic,unsigned int nr_entries)1222a44174eeSJiang Liu static void io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
1223a44174eeSJiang Liu {
1224a44174eeSJiang Liu struct IO_APIC_route_entry entry;
1225341b4a72SThomas Gleixner char buf[256];
1226341b4a72SThomas Gleixner int i;
1227a44174eeSJiang Liu
1228a44174eeSJiang Liu printk(KERN_DEBUG "IOAPIC %d:\n", apic);
1229a44174eeSJiang Liu for (i = 0; i <= nr_entries; i++) {
1230a44174eeSJiang Liu entry = ioapic_read_entry(apic, i);
1231a44174eeSJiang Liu snprintf(buf, sizeof(buf),
1232a44174eeSJiang Liu " pin%02x, %s, %s, %s, V(%02X), IRR(%1d), S(%1d)",
1233335efdf5SThomas Gleixner i,
1234341b4a72SThomas Gleixner entry.masked ? "disabled" : "enabled ",
1235341b4a72SThomas Gleixner entry.is_level ? "level" : "edge ",
1236341b4a72SThomas Gleixner entry.active_low ? "low " : "high",
1237a44174eeSJiang Liu entry.vector, entry.irr, entry.delivery_status);
1238341b4a72SThomas Gleixner if (entry.ir_format) {
1239a44174eeSJiang Liu printk(KERN_DEBUG "%s, remapped, I(%04X), Z(%X)\n",
1240335efdf5SThomas Gleixner buf,
1241341b4a72SThomas Gleixner (entry.ir_index_15 << 15) | entry.ir_index_0_14,
1242341b4a72SThomas Gleixner entry.ir_zero);
1243341b4a72SThomas Gleixner } else {
124451130d21SDavid Woodhouse printk(KERN_DEBUG "%s, %s, D(%02X%02X), M(%1d)\n", buf,
1245341b4a72SThomas Gleixner entry.dest_mode_logical ? "logical " : "physical",
124651130d21SDavid Woodhouse entry.virt_destid_8_14, entry.destid_0_7,
124751130d21SDavid Woodhouse entry.delivery_mode);
1248341b4a72SThomas Gleixner }
1249a44174eeSJiang Liu }
1250a44174eeSJiang Liu }
1251a44174eeSJiang Liu
print_IO_APIC(int ioapic_idx)125274afab7aSJiang Liu static void __init print_IO_APIC(int ioapic_idx)
1253afcc8a40SJoerg Roedel {
1254f62bae50SIngo Molnar union IO_APIC_reg_00 reg_00;
1255f62bae50SIngo Molnar union IO_APIC_reg_01 reg_01;
1256f62bae50SIngo Molnar union IO_APIC_reg_02 reg_02;
1257f62bae50SIngo Molnar union IO_APIC_reg_03 reg_03;
1258f62bae50SIngo Molnar unsigned long flags;
1259f62bae50SIngo Molnar
1260dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
12616f50d45fSYinghai Lu reg_00.raw = io_apic_read(ioapic_idx, 0);
12626f50d45fSYinghai Lu reg_01.raw = io_apic_read(ioapic_idx, 1);
1263f62bae50SIngo Molnar if (reg_01.bits.version >= 0x10)
12646f50d45fSYinghai Lu reg_02.raw = io_apic_read(ioapic_idx, 2);
1265f62bae50SIngo Molnar if (reg_01.bits.version >= 0x20)
12666f50d45fSYinghai Lu reg_03.raw = io_apic_read(ioapic_idx, 3);
1267dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1268f62bae50SIngo Molnar
12696f50d45fSYinghai Lu printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
1270f62bae50SIngo Molnar printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1271f62bae50SIngo Molnar printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1272f62bae50SIngo Molnar printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1273f62bae50SIngo Molnar printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1274f62bae50SIngo Molnar
1275f62bae50SIngo Molnar printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1276bd6a46e0SNaga Chumbalkar printk(KERN_DEBUG "....... : max redirection entries: %02X\n",
1277bd6a46e0SNaga Chumbalkar reg_01.bits.entries);
1278f62bae50SIngo Molnar
1279f62bae50SIngo Molnar printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1280bd6a46e0SNaga Chumbalkar printk(KERN_DEBUG "....... : IO APIC version: %02X\n",
1281bd6a46e0SNaga Chumbalkar reg_01.bits.version);
1282f62bae50SIngo Molnar
1283f62bae50SIngo Molnar /*
1284f62bae50SIngo Molnar * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1285f62bae50SIngo Molnar * but the value of reg_02 is read as the previous read register
1286f62bae50SIngo Molnar * value, so ignore it if reg_02 == reg_01.
1287f62bae50SIngo Molnar */
1288f62bae50SIngo Molnar if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1289f62bae50SIngo Molnar printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1290f62bae50SIngo Molnar printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1291f62bae50SIngo Molnar }
1292f62bae50SIngo Molnar
1293f62bae50SIngo Molnar /*
1294f62bae50SIngo Molnar * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1295f62bae50SIngo Molnar * or reg_03, but the value of reg_0[23] is read as the previous read
1296f62bae50SIngo Molnar * register value, so ignore it if reg_03 == reg_0[12].
1297f62bae50SIngo Molnar */
1298f62bae50SIngo Molnar if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1299f62bae50SIngo Molnar reg_03.raw != reg_01.raw) {
1300f62bae50SIngo Molnar printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1301f62bae50SIngo Molnar printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1302f62bae50SIngo Molnar }
1303f62bae50SIngo Molnar
1304f62bae50SIngo Molnar printk(KERN_DEBUG ".... IRQ redirection table:\n");
1305a44174eeSJiang Liu io_apic_print_entries(ioapic_idx, reg_01.bits.entries);
130642f0efc5SNaga Chumbalkar }
130742f0efc5SNaga Chumbalkar
print_IO_APICs(void)130874afab7aSJiang Liu void __init print_IO_APICs(void)
1309cda417ddSYinghai Lu {
13106f50d45fSYinghai Lu int ioapic_idx;
1311cda417ddSYinghai Lu unsigned int irq;
1312cda417ddSYinghai Lu
1313cda417ddSYinghai Lu printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1314f44d1692SJiang Liu for_each_ioapic(ioapic_idx)
1315cda417ddSYinghai Lu printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
13166f50d45fSYinghai Lu mpc_ioapic_id(ioapic_idx),
13176f50d45fSYinghai Lu ioapics[ioapic_idx].nr_registers);
1318cda417ddSYinghai Lu
1319cda417ddSYinghai Lu /*
1320cda417ddSYinghai Lu * We are a bit conservative about what we expect. We have to
1321cda417ddSYinghai Lu * know about every hardware change ASAP.
1322cda417ddSYinghai Lu */
1323cda417ddSYinghai Lu printk(KERN_INFO "testing the IO APIC.......................\n");
1324cda417ddSYinghai Lu
1325f44d1692SJiang Liu for_each_ioapic(ioapic_idx)
13266f50d45fSYinghai Lu print_IO_APIC(ioapic_idx);
1327cda417ddSYinghai Lu
1328f62bae50SIngo Molnar printk(KERN_DEBUG "IRQ to pin mappings:\n");
1329ad9f4334SThomas Gleixner for_each_active_irq(irq) {
1330f62bae50SIngo Molnar struct irq_pin_list *entry;
13314467715aSJiang Liu struct irq_chip *chip;
13324467715aSJiang Liu struct mp_chip_data *data;
1333f62bae50SIngo Molnar
13346fd36ba0SMathias Nyman chip = irq_get_chip(irq);
1335d32932d0SJiang Liu if (chip != &ioapic_chip && chip != &ioapic_ir_chip)
13366fd36ba0SMathias Nyman continue;
13374467715aSJiang Liu data = irq_get_chip_data(irq);
13384467715aSJiang Liu if (!data)
13394467715aSJiang Liu continue;
13404467715aSJiang Liu if (list_empty(&data->irq_2_pin))
13414467715aSJiang Liu continue;
13426fd36ba0SMathias Nyman
1343f62bae50SIngo Molnar printk(KERN_DEBUG "IRQ%d ", irq);
13444467715aSJiang Liu for_each_irq_pin(entry, data->irq_2_pin)
1345c767a54bSJoe Perches pr_cont("-> %d:%d", entry->apic, entry->pin);
1346c767a54bSJoe Perches pr_cont("\n");
1347f62bae50SIngo Molnar }
1348f62bae50SIngo Molnar
1349f62bae50SIngo Molnar printk(KERN_INFO ".................................... done.\n");
1350f62bae50SIngo Molnar }
1351f62bae50SIngo Molnar
1352f62bae50SIngo Molnar /* Where if anywhere is the i8259 connect in external int mode */
1353f62bae50SIngo Molnar static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1354f62bae50SIngo Molnar
enable_IO_APIC(void)1355f62bae50SIngo Molnar void __init enable_IO_APIC(void)
1356f62bae50SIngo Molnar {
1357f62bae50SIngo Molnar int i8259_apic, i8259_pin;
1358f44d1692SJiang Liu int apic, pin;
1359bc07844aSThomas Gleixner
1360ecf600f8SThomas Gleixner if (ioapic_is_disabled)
1361a46f5c89SThomas Gleixner nr_ioapics = 0;
1362a46f5c89SThomas Gleixner
1363a46f5c89SThomas Gleixner if (!nr_legacy_irqs() || !nr_ioapics)
1364bc07844aSThomas Gleixner return;
1365bc07844aSThomas Gleixner
1366f44d1692SJiang Liu for_each_ioapic_pin(apic, pin) {
1367f62bae50SIngo Molnar /* See if any of the pins is in ExtINT mode */
1368f44d1692SJiang Liu struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin);
1369f62bae50SIngo Molnar
1370f62bae50SIngo Molnar /* If the interrupt line is enabled and in ExtInt mode
1371f62bae50SIngo Molnar * I have found the pin where the i8259 is connected.
1372f62bae50SIngo Molnar */
1373341b4a72SThomas Gleixner if (!entry.masked &&
1374341b4a72SThomas Gleixner entry.delivery_mode == APIC_DELIVERY_MODE_EXTINT) {
1375f62bae50SIngo Molnar ioapic_i8259.apic = apic;
1376f62bae50SIngo Molnar ioapic_i8259.pin = pin;
1377f62bae50SIngo Molnar goto found_i8259;
1378f62bae50SIngo Molnar }
1379f62bae50SIngo Molnar }
1380f62bae50SIngo Molnar found_i8259:
1381f62bae50SIngo Molnar /* Look to see what if the MP table has reported the ExtINT */
1382f62bae50SIngo Molnar /* If we could not find the appropriate pin by looking at the ioapic
1383f62bae50SIngo Molnar * the i8259 probably is not connected the ioapic but give the
1384f62bae50SIngo Molnar * mptable a chance anyway.
1385f62bae50SIngo Molnar */
1386f62bae50SIngo Molnar i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1387f62bae50SIngo Molnar i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1388f62bae50SIngo Molnar /* Trust the MP table if nothing is setup in the hardware */
1389f62bae50SIngo Molnar if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1390f62bae50SIngo Molnar printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1391f62bae50SIngo Molnar ioapic_i8259.pin = i8259_pin;
1392f62bae50SIngo Molnar ioapic_i8259.apic = i8259_apic;
1393f62bae50SIngo Molnar }
1394f62bae50SIngo Molnar /* Complain if the MP table and the hardware disagree */
1395f62bae50SIngo Molnar if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1396f62bae50SIngo Molnar (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1397f62bae50SIngo Molnar {
1398f62bae50SIngo Molnar printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1399f62bae50SIngo Molnar }
1400f62bae50SIngo Molnar
1401f62bae50SIngo Molnar /*
1402f62bae50SIngo Molnar * Do not trust the IO-APIC being empty at bootup
1403f62bae50SIngo Molnar */
1404f62bae50SIngo Molnar clear_IO_APIC();
1405f62bae50SIngo Molnar }
1406f62bae50SIngo Molnar
native_restore_boot_irq_mode(void)140751b146c5SBaoquan He void native_restore_boot_irq_mode(void)
1408f62bae50SIngo Molnar {
1409f62bae50SIngo Molnar /*
1410f62bae50SIngo Molnar * If the i8259 is routed through an IOAPIC
1411f62bae50SIngo Molnar * Put that IOAPIC in virtual wire mode
1412f62bae50SIngo Molnar * so legacy interrupts can be delivered.
1413f62bae50SIngo Molnar */
14141c4248caSJoerg Roedel if (ioapic_i8259.pin != -1) {
1415f62bae50SIngo Molnar struct IO_APIC_route_entry entry;
141651130d21SDavid Woodhouse u32 apic_id = read_apic_id();
1417f62bae50SIngo Molnar
1418f62bae50SIngo Molnar memset(&entry, 0, sizeof(entry));
1419341b4a72SThomas Gleixner entry.masked = false;
1420341b4a72SThomas Gleixner entry.is_level = false;
1421341b4a72SThomas Gleixner entry.active_low = false;
1422341b4a72SThomas Gleixner entry.dest_mode_logical = false;
142372161299SThomas Gleixner entry.delivery_mode = APIC_DELIVERY_MODE_EXTINT;
142451130d21SDavid Woodhouse entry.destid_0_7 = apic_id & 0xFF;
142551130d21SDavid Woodhouse entry.virt_destid_8_14 = apic_id >> 8;
1426f62bae50SIngo Molnar
1427f62bae50SIngo Molnar /*
1428f62bae50SIngo Molnar * Add it to the IO-APIC irq-routing table:
1429f62bae50SIngo Molnar */
1430f62bae50SIngo Molnar ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1431f62bae50SIngo Molnar }
1432f62bae50SIngo Molnar
143393984fbdSBorislav Petkov if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config())
14341c4248caSJoerg Roedel disconnect_bsp_APIC(ioapic_i8259.pin != -1);
14351c4248caSJoerg Roedel }
14361c4248caSJoerg Roedel
restore_boot_irq_mode(void)1437ce279cdcSBaoquan He void restore_boot_irq_mode(void)
14381c4248caSJoerg Roedel {
143995d76accSJiang Liu if (!nr_legacy_irqs())
14401c4248caSJoerg Roedel return;
14411c4248caSJoerg Roedel
144251b146c5SBaoquan He x86_apic_ops.restore();
1443f62bae50SIngo Molnar }
1444f62bae50SIngo Molnar
1445f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1446f62bae50SIngo Molnar /*
1447f62bae50SIngo Molnar * function to set the IO-APIC physical IDs based on the
1448f62bae50SIngo Molnar * values stored in the MPC table.
1449f62bae50SIngo Molnar *
1450f62bae50SIngo Molnar * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1451f62bae50SIngo Molnar */
setup_ioapic_ids_from_mpc_nocheck(void)1452a38c5380SSebastian Andrzej Siewior void __init setup_ioapic_ids_from_mpc_nocheck(void)
1453f62bae50SIngo Molnar {
1454f62bae50SIngo Molnar union IO_APIC_reg_00 reg_00;
1455f62bae50SIngo Molnar physid_mask_t phys_id_present_map;
14566f50d45fSYinghai Lu int ioapic_idx;
1457f62bae50SIngo Molnar int i;
1458f62bae50SIngo Molnar unsigned char old_id;
1459f62bae50SIngo Molnar unsigned long flags;
1460f62bae50SIngo Molnar
1461f62bae50SIngo Molnar /*
1462f62bae50SIngo Molnar * This is broken; anything with a real cpu count has to
1463f62bae50SIngo Molnar * circumvent this idiocy regardless.
1464f62bae50SIngo Molnar */
14657abc0753SCyrill Gorcunov apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
1466f62bae50SIngo Molnar
1467f62bae50SIngo Molnar /*
1468f62bae50SIngo Molnar * Set the IOAPIC ID to the value stored in the MPC table.
1469f62bae50SIngo Molnar */
1470f44d1692SJiang Liu for_each_ioapic(ioapic_idx) {
1471f62bae50SIngo Molnar /* Read the register 0 value */
1472dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
14736f50d45fSYinghai Lu reg_00.raw = io_apic_read(ioapic_idx, 0);
1474dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1475f62bae50SIngo Molnar
14766f50d45fSYinghai Lu old_id = mpc_ioapic_id(ioapic_idx);
1477f62bae50SIngo Molnar
14786f50d45fSYinghai Lu if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
1479f62bae50SIngo Molnar printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
14806f50d45fSYinghai Lu ioapic_idx, mpc_ioapic_id(ioapic_idx));
1481f62bae50SIngo Molnar printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1482f62bae50SIngo Molnar reg_00.bits.ID);
14836f50d45fSYinghai Lu ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
1484f62bae50SIngo Molnar }
1485f62bae50SIngo Molnar
1486f62bae50SIngo Molnar /*
1487f62bae50SIngo Molnar * Sanity check, is the ID really free? Every APIC in a
1488f62bae50SIngo Molnar * system must have a unique ID or we get lots of nice
1489f62bae50SIngo Molnar * 'stuck on smp_invalidate_needed IPI wait' messages.
1490f62bae50SIngo Molnar */
14917abc0753SCyrill Gorcunov if (apic->check_apicid_used(&phys_id_present_map,
14926f50d45fSYinghai Lu mpc_ioapic_id(ioapic_idx))) {
1493f62bae50SIngo Molnar printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
14946f50d45fSYinghai Lu ioapic_idx, mpc_ioapic_id(ioapic_idx));
1495f62bae50SIngo Molnar for (i = 0; i < get_physical_broadcast(); i++)
1496f62bae50SIngo Molnar if (!physid_isset(i, phys_id_present_map))
1497f62bae50SIngo Molnar break;
1498f62bae50SIngo Molnar if (i >= get_physical_broadcast())
1499f62bae50SIngo Molnar panic("Max APIC ID exceeded!\n");
1500f62bae50SIngo Molnar printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1501f62bae50SIngo Molnar i);
1502f62bae50SIngo Molnar physid_set(i, phys_id_present_map);
15036f50d45fSYinghai Lu ioapics[ioapic_idx].mp_config.apicid = i;
1504f62bae50SIngo Molnar } else {
15059a2a637aSThomas Gleixner apic_printk(APIC_VERBOSE, "Setting %d in the phys_id_present_map\n",
15066f50d45fSYinghai Lu mpc_ioapic_id(ioapic_idx));
15079faee3ecSThomas Gleixner physid_set(mpc_ioapic_id(ioapic_idx), phys_id_present_map);
1508f62bae50SIngo Molnar }
1509f62bae50SIngo Molnar
1510f62bae50SIngo Molnar /*
1511f62bae50SIngo Molnar * We need to adjust the IRQ routing table
1512f62bae50SIngo Molnar * if the ID changed.
1513f62bae50SIngo Molnar */
15146f50d45fSYinghai Lu if (old_id != mpc_ioapic_id(ioapic_idx))
1515f62bae50SIngo Molnar for (i = 0; i < mp_irq_entries; i++)
1516f62bae50SIngo Molnar if (mp_irqs[i].dstapic == old_id)
1517f62bae50SIngo Molnar mp_irqs[i].dstapic
15186f50d45fSYinghai Lu = mpc_ioapic_id(ioapic_idx);
1519f62bae50SIngo Molnar
1520f62bae50SIngo Molnar /*
152160d79fd9SYinghai Lu * Update the ID register according to the right value
152260d79fd9SYinghai Lu * from the MPC table if they are different.
1523f62bae50SIngo Molnar */
15246f50d45fSYinghai Lu if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
152560d79fd9SYinghai Lu continue;
152660d79fd9SYinghai Lu
1527f62bae50SIngo Molnar apic_printk(APIC_VERBOSE, KERN_INFO
1528f62bae50SIngo Molnar "...changing IO-APIC physical APIC ID to %d ...",
15296f50d45fSYinghai Lu mpc_ioapic_id(ioapic_idx));
1530f62bae50SIngo Molnar
15316f50d45fSYinghai Lu reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
1532dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
15336f50d45fSYinghai Lu io_apic_write(ioapic_idx, 0, reg_00.raw);
1534dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1535f62bae50SIngo Molnar
1536f62bae50SIngo Molnar /*
1537f62bae50SIngo Molnar * Sanity check
1538f62bae50SIngo Molnar */
1539dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
15406f50d45fSYinghai Lu reg_00.raw = io_apic_read(ioapic_idx, 0);
1541dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
15426f50d45fSYinghai Lu if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
1543c767a54bSJoe Perches pr_cont("could not set ID!\n");
1544f62bae50SIngo Molnar else
1545f62bae50SIngo Molnar apic_printk(APIC_VERBOSE, " ok.\n");
1546f62bae50SIngo Molnar }
1547f62bae50SIngo Molnar }
1548a38c5380SSebastian Andrzej Siewior
setup_ioapic_ids_from_mpc(void)1549a38c5380SSebastian Andrzej Siewior void __init setup_ioapic_ids_from_mpc(void)
1550a38c5380SSebastian Andrzej Siewior {
1551a38c5380SSebastian Andrzej Siewior
1552a38c5380SSebastian Andrzej Siewior if (acpi_ioapic)
1553a38c5380SSebastian Andrzej Siewior return;
1554a38c5380SSebastian Andrzej Siewior /*
1555a38c5380SSebastian Andrzej Siewior * Don't check I/O APIC IDs for xAPIC systems. They have
1556a38c5380SSebastian Andrzej Siewior * no meaning without the serial APIC bus.
1557a38c5380SSebastian Andrzej Siewior */
1558a38c5380SSebastian Andrzej Siewior if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1559cff9ab2bSDenys Vlasenko || APIC_XAPIC(boot_cpu_apic_version))
1560a38c5380SSebastian Andrzej Siewior return;
1561a38c5380SSebastian Andrzej Siewior setup_ioapic_ids_from_mpc_nocheck();
1562a38c5380SSebastian Andrzej Siewior }
1563f62bae50SIngo Molnar #endif
1564f62bae50SIngo Molnar
1565f62bae50SIngo Molnar int no_timer_check __initdata;
1566f62bae50SIngo Molnar
notimercheck(char * s)1567f62bae50SIngo Molnar static int __init notimercheck(char *s)
1568f62bae50SIngo Molnar {
1569f62bae50SIngo Molnar no_timer_check = 1;
1570f62bae50SIngo Molnar return 1;
1571f62bae50SIngo Molnar }
1572f62bae50SIngo Molnar __setup("no_timer_check", notimercheck);
1573f62bae50SIngo Molnar
delay_with_tsc(void)1574ca7c6076SDou Liyang static void __init delay_with_tsc(void)
1575ca7c6076SDou Liyang {
1576ca7c6076SDou Liyang unsigned long long start, now;
1577ca7c6076SDou Liyang unsigned long end = jiffies + 4;
1578ca7c6076SDou Liyang
1579ca7c6076SDou Liyang start = rdtsc();
1580ca7c6076SDou Liyang
1581ca7c6076SDou Liyang /*
1582ca7c6076SDou Liyang * We don't know the TSC frequency yet, but waiting for
1583ca7c6076SDou Liyang * 40000000000/HZ TSC cycles is safe:
1584ca7c6076SDou Liyang * 4 GHz == 10 jiffies
1585ca7c6076SDou Liyang * 1 GHz == 40 jiffies
1586ca7c6076SDou Liyang */
1587ca7c6076SDou Liyang do {
1588ca7c6076SDou Liyang rep_nop();
1589ca7c6076SDou Liyang now = rdtsc();
15906262b6e7SJan Beulich } while ((now - start) < 40000000000ULL / HZ &&
1591ca7c6076SDou Liyang time_before_eq(jiffies, end));
1592ca7c6076SDou Liyang }
1593ca7c6076SDou Liyang
delay_without_tsc(void)1594ca7c6076SDou Liyang static void __init delay_without_tsc(void)
1595ca7c6076SDou Liyang {
1596ca7c6076SDou Liyang unsigned long end = jiffies + 4;
1597ca7c6076SDou Liyang int band = 1;
1598ca7c6076SDou Liyang
1599ca7c6076SDou Liyang /*
1600ca7c6076SDou Liyang * We don't know any frequency yet, but waiting for
1601ca7c6076SDou Liyang * 40940000000/HZ cycles is safe:
1602ca7c6076SDou Liyang * 4 GHz == 10 jiffies
1603ca7c6076SDou Liyang * 1 GHz == 40 jiffies
1604ca7c6076SDou Liyang * 1 << 1 + 1 << 2 +...+ 1 << 11 = 4094
1605ca7c6076SDou Liyang */
1606ca7c6076SDou Liyang do {
1607ca7c6076SDou Liyang __delay(((1U << band++) * 10000000UL) / HZ);
1608ca7c6076SDou Liyang } while (band < 12 && time_before_eq(jiffies, end));
1609ca7c6076SDou Liyang }
1610ca7c6076SDou Liyang
1611f62bae50SIngo Molnar /*
1612f62bae50SIngo Molnar * There is a nasty bug in some older SMP boards, their mptable lies
1613f62bae50SIngo Molnar * about the timer IRQ. We do the following to work around the situation:
1614f62bae50SIngo Molnar *
1615f62bae50SIngo Molnar * - timer IRQ defaults to IO-APIC IRQ
1616f62bae50SIngo Molnar * - if this function detects that timer IRQs are defunct, then we fall
1617f62bae50SIngo Molnar * back to ISA timer IRQs
1618f62bae50SIngo Molnar */
timer_irq_works(void)1619f62bae50SIngo Molnar static int __init timer_irq_works(void)
1620f62bae50SIngo Molnar {
1621f62bae50SIngo Molnar unsigned long t1 = jiffies;
1622f62bae50SIngo Molnar
1623f62bae50SIngo Molnar if (no_timer_check)
1624f62bae50SIngo Molnar return 1;
1625f62bae50SIngo Molnar
1626f62bae50SIngo Molnar local_irq_enable();
1627ca7c6076SDou Liyang if (boot_cpu_has(X86_FEATURE_TSC))
1628ca7c6076SDou Liyang delay_with_tsc();
1629ca7c6076SDou Liyang else
1630ca7c6076SDou Liyang delay_without_tsc();
1631ca7c6076SDou Liyang
1632f62bae50SIngo Molnar /*
1633f62bae50SIngo Molnar * Expect a few ticks at least, to be sure some possible
1634f62bae50SIngo Molnar * glue logic does not lock up after one or two first
1635f62bae50SIngo Molnar * ticks in a non-ExtINT mode. Also the local APIC
1636f62bae50SIngo Molnar * might have cached one ExtINT interrupt. Finally, at
1637f62bae50SIngo Molnar * least one tick may be lost due to delays.
1638f62bae50SIngo Molnar */
1639f62bae50SIngo Molnar
1640058df195SThomas Gleixner local_irq_disable();
1641058df195SThomas Gleixner
1642058df195SThomas Gleixner /* Did jiffies advance? */
1643058df195SThomas Gleixner return time_after(jiffies, t1 + 4);
1644f62bae50SIngo Molnar }
1645f62bae50SIngo Molnar
1646f62bae50SIngo Molnar /*
1647f62bae50SIngo Molnar * In the SMP+IOAPIC case it might happen that there are an unspecified
1648f62bae50SIngo Molnar * number of pending IRQ events unhandled. These cases are very rare,
1649f62bae50SIngo Molnar * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1650f62bae50SIngo Molnar * better to do it this way as thus we do not have to be aware of
1651f62bae50SIngo Molnar * 'pending' interrupts in the IRQ path, except at this point.
1652f62bae50SIngo Molnar */
1653f62bae50SIngo Molnar /*
1654f62bae50SIngo Molnar * Edge triggered needs to resend any interrupt
1655f62bae50SIngo Molnar * that was delayed but this is now handled in the device
1656f62bae50SIngo Molnar * independent code.
1657f62bae50SIngo Molnar */
1658f62bae50SIngo Molnar
1659f62bae50SIngo Molnar /*
1660f62bae50SIngo Molnar * Starting up a edge-triggered IO-APIC interrupt is
1661f62bae50SIngo Molnar * nasty - we need to make sure that we get the edge.
1662f62bae50SIngo Molnar * If it is already asserted for some reason, we need
1663f62bae50SIngo Molnar * return 1 to indicate that is was pending.
1664f62bae50SIngo Molnar *
1665f62bae50SIngo Molnar * This is not complete - we should be able to fake
1666f62bae50SIngo Molnar * an edge even if it isn't on the 8259A...
1667f62bae50SIngo Molnar */
startup_ioapic_irq(struct irq_data * data)166861a38ce3SThomas Gleixner static unsigned int startup_ioapic_irq(struct irq_data *data)
1669f62bae50SIngo Molnar {
167061a38ce3SThomas Gleixner int was_pending = 0, irq = data->irq;
1671f62bae50SIngo Molnar unsigned long flags;
1672f62bae50SIngo Molnar
1673dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
167495d76accSJiang Liu if (irq < nr_legacy_irqs()) {
16754305df94SThomas Gleixner legacy_pic->mask(irq);
1676b81bb373SJacob Pan if (legacy_pic->irq_pending(irq))
1677f62bae50SIngo Molnar was_pending = 1;
1678f62bae50SIngo Molnar }
16794467715aSJiang Liu __unmask_ioapic(data->chip_data);
1680dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1681f62bae50SIngo Molnar
1682f62bae50SIngo Molnar return was_pending;
1683f62bae50SIngo Molnar }
1684f62bae50SIngo Molnar
1685f62bae50SIngo Molnar atomic_t irq_mis_count;
1686f62bae50SIngo Molnar
16874da7072aSAlexander Gordeev #ifdef CONFIG_GENERIC_PENDING_IRQ
io_apic_level_ack_pending(struct mp_chip_data * data)16884467715aSJiang Liu static bool io_apic_level_ack_pending(struct mp_chip_data *data)
1689d1ecad6eSMárton Németh {
1690d1ecad6eSMárton Németh struct irq_pin_list *entry;
1691d1ecad6eSMárton Németh unsigned long flags;
1692d1ecad6eSMárton Németh
1693d1ecad6eSMárton Németh raw_spin_lock_irqsave(&ioapic_lock, flags);
16944467715aSJiang Liu for_each_irq_pin(entry, data->irq_2_pin) {
1695341b4a72SThomas Gleixner struct IO_APIC_route_entry e;
1696d1ecad6eSMárton Németh int pin;
1697d1ecad6eSMárton Németh
1698d1ecad6eSMárton Németh pin = entry->pin;
1699341b4a72SThomas Gleixner e.w1 = io_apic_read(entry->apic, 0x10 + pin*2);
1700d1ecad6eSMárton Németh /* Is the remote IRR bit set? */
1701341b4a72SThomas Gleixner if (e.irr) {
1702d1ecad6eSMárton Németh raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1703d1ecad6eSMárton Németh return true;
1704d1ecad6eSMárton Németh }
1705d1ecad6eSMárton Németh }
1706d1ecad6eSMárton Németh raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1707d1ecad6eSMárton Németh
1708d1ecad6eSMárton Németh return false;
1709d1ecad6eSMárton Németh }
1710d1ecad6eSMárton Németh
ioapic_prepare_move(struct irq_data * data)17112579a4eeSThomas Gleixner static inline bool ioapic_prepare_move(struct irq_data *data)
17124da7072aSAlexander Gordeev {
1713df439342SThomas Gleixner /* If we are moving the IRQ we need to mask it */
17144da7072aSAlexander Gordeev if (unlikely(irqd_is_setaffinity_pending(data))) {
1715df439342SThomas Gleixner if (!irqd_irq_masked(data))
17164467715aSJiang Liu mask_ioapic_irq(data);
17174da7072aSAlexander Gordeev return true;
17184da7072aSAlexander Gordeev }
17194da7072aSAlexander Gordeev return false;
17204da7072aSAlexander Gordeev }
17214da7072aSAlexander Gordeev
ioapic_finish_move(struct irq_data * data,bool moveit)17222579a4eeSThomas Gleixner static inline void ioapic_finish_move(struct irq_data *data, bool moveit)
17234da7072aSAlexander Gordeev {
17242579a4eeSThomas Gleixner if (unlikely(moveit)) {
17254da7072aSAlexander Gordeev /* Only migrate the irq if the ack has been received.
17264da7072aSAlexander Gordeev *
17274da7072aSAlexander Gordeev * On rare occasions the broadcast level triggered ack gets
17284da7072aSAlexander Gordeev * delayed going to ioapics, and if we reprogram the
17294da7072aSAlexander Gordeev * vector while Remote IRR is still set the irq will never
17304da7072aSAlexander Gordeev * fire again.
17314da7072aSAlexander Gordeev *
17324da7072aSAlexander Gordeev * To prevent this scenario we read the Remote IRR bit
17334da7072aSAlexander Gordeev * of the ioapic. This has two effects.
17344da7072aSAlexander Gordeev * - On any sane system the read of the ioapic will
17354da7072aSAlexander Gordeev * flush writes (and acks) going to the ioapic from
17364da7072aSAlexander Gordeev * this cpu.
17374da7072aSAlexander Gordeev * - We get to see if the ACK has actually been delivered.
17384da7072aSAlexander Gordeev *
17394da7072aSAlexander Gordeev * Based on failed experiments of reprogramming the
17404da7072aSAlexander Gordeev * ioapic entry from outside of irq context starting
17414da7072aSAlexander Gordeev * with masking the ioapic entry and then polling until
17424da7072aSAlexander Gordeev * Remote IRR was clear before reprogramming the
17434da7072aSAlexander Gordeev * ioapic I don't trust the Remote IRR bit to be
1744d9f6e12fSIngo Molnar * completely accurate.
17454da7072aSAlexander Gordeev *
17464da7072aSAlexander Gordeev * However there appears to be no other way to plug
17474da7072aSAlexander Gordeev * this race, so if the Remote IRR bit is not
17484da7072aSAlexander Gordeev * accurate and is causing problems then it is a hardware bug
17494da7072aSAlexander Gordeev * and you can go talk to the chipset vendor about it.
17504da7072aSAlexander Gordeev */
17514467715aSJiang Liu if (!io_apic_level_ack_pending(data->chip_data))
17524da7072aSAlexander Gordeev irq_move_masked_irq(data);
1753df439342SThomas Gleixner /* If the IRQ is masked in the core, leave it: */
1754df439342SThomas Gleixner if (!irqd_irq_masked(data))
17554467715aSJiang Liu unmask_ioapic_irq(data);
17564da7072aSAlexander Gordeev }
17574da7072aSAlexander Gordeev }
17584da7072aSAlexander Gordeev #else
ioapic_prepare_move(struct irq_data * data)17592579a4eeSThomas Gleixner static inline bool ioapic_prepare_move(struct irq_data *data)
17604da7072aSAlexander Gordeev {
17614da7072aSAlexander Gordeev return false;
17624da7072aSAlexander Gordeev }
ioapic_finish_move(struct irq_data * data,bool moveit)17632579a4eeSThomas Gleixner static inline void ioapic_finish_move(struct irq_data *data, bool moveit)
17644da7072aSAlexander Gordeev {
17654da7072aSAlexander Gordeev }
17664da7072aSAlexander Gordeev #endif
17674da7072aSAlexander Gordeev
ioapic_ack_level(struct irq_data * irq_data)17684467715aSJiang Liu static void ioapic_ack_level(struct irq_data *irq_data)
1769f62bae50SIngo Molnar {
17704467715aSJiang Liu struct irq_cfg *cfg = irqd_cfg(irq_data);
1771f62bae50SIngo Molnar unsigned long v;
17722579a4eeSThomas Gleixner bool moveit;
1773d32932d0SJiang Liu int i;
1774f62bae50SIngo Molnar
1775dd5f15e5SThomas Gleixner irq_complete_move(cfg);
17762579a4eeSThomas Gleixner moveit = ioapic_prepare_move(irq_data);
1777f62bae50SIngo Molnar
1778f62bae50SIngo Molnar /*
1779f62bae50SIngo Molnar * It appears there is an erratum which affects at least version 0x11
1780f62bae50SIngo Molnar * of I/O APIC (that's the 82093AA and cores integrated into various
1781f62bae50SIngo Molnar * chipsets). Under certain conditions a level-triggered interrupt is
1782f62bae50SIngo Molnar * erroneously delivered as edge-triggered one but the respective IRR
1783f62bae50SIngo Molnar * bit gets set nevertheless. As a result the I/O unit expects an EOI
1784f62bae50SIngo Molnar * message but it will never arrive and further interrupts are blocked
1785f62bae50SIngo Molnar * from the source. The exact reason is so far unknown, but the
1786f62bae50SIngo Molnar * phenomenon was observed when two consecutive interrupt requests
1787f62bae50SIngo Molnar * from a given source get delivered to the same CPU and the source is
1788f62bae50SIngo Molnar * temporarily disabled in between.
1789f62bae50SIngo Molnar *
1790f62bae50SIngo Molnar * A workaround is to simulate an EOI message manually. We achieve it
1791f62bae50SIngo Molnar * by setting the trigger mode to edge and then to level when the edge
1792f62bae50SIngo Molnar * trigger mode gets detected in the TMR of a local APIC for a
1793f62bae50SIngo Molnar * level-triggered interrupt. We mask the source for the time of the
1794f62bae50SIngo Molnar * operation to prevent an edge-triggered interrupt escaping meanwhile.
1795f62bae50SIngo Molnar * The idea is from Manfred Spraul. --macro
17961c83995bSSuresh Siddha *
17971c83995bSSuresh Siddha * Also in the case when cpu goes offline, fixup_irqs() will forward
17981c83995bSSuresh Siddha * any unhandled interrupt on the offlined cpu to the new cpu
17991c83995bSSuresh Siddha * destination that is handling the corresponding interrupt. This
18001c83995bSSuresh Siddha * interrupt forwarding is done via IPI's. Hence, in this case also
18011c83995bSSuresh Siddha * level-triggered io-apic interrupt will be seen as an edge
18021c83995bSSuresh Siddha * interrupt in the IRR. And we can't rely on the cpu's EOI
18031c83995bSSuresh Siddha * to be broadcasted to the IO-APIC's which will clear the remoteIRR
18041c83995bSSuresh Siddha * corresponding to the level-triggered interrupt. Hence on IO-APIC's
18051c83995bSSuresh Siddha * supporting EOI register, we do an explicit EOI to clear the
18061c83995bSSuresh Siddha * remote IRR and on IO-APIC's which don't have an EOI register,
18071c83995bSSuresh Siddha * we use the above logic (mask+edge followed by unmask+level) from
18081c83995bSSuresh Siddha * Manfred Spraul to clear the remote IRR.
1809f62bae50SIngo Molnar */
1810f62bae50SIngo Molnar i = cfg->vector;
1811f62bae50SIngo Molnar v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1812f62bae50SIngo Molnar
1813f62bae50SIngo Molnar /*
1814f62bae50SIngo Molnar * We must acknowledge the irq before we move it or the acknowledge will
1815f62bae50SIngo Molnar * not propagate properly.
1816f62bae50SIngo Molnar */
1817670c04adSDave Hansen apic_eoi();
1818f62bae50SIngo Molnar
18191c83995bSSuresh Siddha /*
18201c83995bSSuresh Siddha * Tail end of clearing remote IRR bit (either by delivering the EOI
18211c83995bSSuresh Siddha * message via io-apic EOI register write or simulating it using
1822d9f6e12fSIngo Molnar * mask+edge followed by unmask+level logic) manually when the
18231c83995bSSuresh Siddha * level triggered interrupt is seen as the edge triggered interrupt
18241c83995bSSuresh Siddha * at the cpu.
18251c83995bSSuresh Siddha */
1826ca64c47cSMaciej W. Rozycki if (!(v & (1 << (i & 0x1f)))) {
1827ca64c47cSMaciej W. Rozycki atomic_inc(&irq_mis_count);
18284467715aSJiang Liu eoi_ioapic_pin(cfg->vector, irq_data->chip_data);
1829ca64c47cSMaciej W. Rozycki }
1830ca64c47cSMaciej W. Rozycki
18312579a4eeSThomas Gleixner ioapic_finish_move(irq_data, moveit);
1832b3ec0a37SSuresh Siddha }
1833f62bae50SIngo Molnar
ioapic_ir_ack_level(struct irq_data * irq_data)1834d32932d0SJiang Liu static void ioapic_ir_ack_level(struct irq_data *irq_data)
1835d32932d0SJiang Liu {
1836d32932d0SJiang Liu struct mp_chip_data *data = irq_data->chip_data;
1837d32932d0SJiang Liu
1838d32932d0SJiang Liu /*
1839d32932d0SJiang Liu * Intr-remapping uses pin number as the virtual vector
1840d32932d0SJiang Liu * in the RTE. Actual vector is programmed in
1841d32932d0SJiang Liu * intr-remapping table entry. Hence for the io-apic
1842d32932d0SJiang Liu * EOI we use the pin number.
1843d32932d0SJiang Liu */
18442b04e46dSThomas Gleixner apic_ack_irq(irq_data);
18454467715aSJiang Liu eoi_ioapic_pin(data->entry.vector, data);
1846d32932d0SJiang Liu }
1847d32932d0SJiang Liu
18485d5a9713SDavid Woodhouse /*
18495d5a9713SDavid Woodhouse * The I/OAPIC is just a device for generating MSI messages from legacy
18505d5a9713SDavid Woodhouse * interrupt pins. Various fields of the RTE translate into bits of the
18515d5a9713SDavid Woodhouse * resulting MSI which had a historical meaning.
18525d5a9713SDavid Woodhouse *
18535d5a9713SDavid Woodhouse * With interrupt remapping, many of those bits have different meanings
18545d5a9713SDavid Woodhouse * in the underlying MSI, but the way that the I/OAPIC transforms them
18555d5a9713SDavid Woodhouse * from its RTE to the MSI message is the same. This function allows
18565d5a9713SDavid Woodhouse * the parent IRQ domain to compose the MSI message, then takes the
18575d5a9713SDavid Woodhouse * relevant bits to put them in the appropriate places in the RTE in
18585d5a9713SDavid Woodhouse * order to generate that message when the IRQ happens.
18595d5a9713SDavid Woodhouse *
18605d5a9713SDavid Woodhouse * The setup here relies on a preconfigured route entry (is_level,
18615d5a9713SDavid Woodhouse * active_low, masked) because the parent domain is merely composing the
18625d5a9713SDavid Woodhouse * generic message routing information which is used for the MSI.
18635d5a9713SDavid Woodhouse */
ioapic_setup_msg_from_msi(struct irq_data * irq_data,struct IO_APIC_route_entry * entry)18645d5a9713SDavid Woodhouse static void ioapic_setup_msg_from_msi(struct irq_data *irq_data,
18655d5a9713SDavid Woodhouse struct IO_APIC_route_entry *entry)
18665d5a9713SDavid Woodhouse {
18675d5a9713SDavid Woodhouse struct msi_msg msg;
18685d5a9713SDavid Woodhouse
18695d5a9713SDavid Woodhouse /* Let the parent domain compose the MSI message */
18705d5a9713SDavid Woodhouse irq_chip_compose_msi_msg(irq_data, &msg);
18715d5a9713SDavid Woodhouse
18725d5a9713SDavid Woodhouse /*
18735d5a9713SDavid Woodhouse * - Real vector
18745d5a9713SDavid Woodhouse * - DMAR/IR: 8bit subhandle (ioapic.pin)
18755d5a9713SDavid Woodhouse * - AMD/IR: 8bit IRTE index
18765d5a9713SDavid Woodhouse */
18775d5a9713SDavid Woodhouse entry->vector = msg.arch_data.vector;
18785d5a9713SDavid Woodhouse /* Delivery mode (for DMAR/IR all 0) */
18795d5a9713SDavid Woodhouse entry->delivery_mode = msg.arch_data.delivery_mode;
18805d5a9713SDavid Woodhouse /* Destination mode or DMAR/IR index bit 15 */
18815d5a9713SDavid Woodhouse entry->dest_mode_logical = msg.arch_addr_lo.dest_mode_logical;
18825d5a9713SDavid Woodhouse /* DMAR/IR: 1, 0 for all other modes */
18835d5a9713SDavid Woodhouse entry->ir_format = msg.arch_addr_lo.dmar_format;
18845d5a9713SDavid Woodhouse /*
188551130d21SDavid Woodhouse * - DMAR/IR: index bit 0-14.
188651130d21SDavid Woodhouse *
188751130d21SDavid Woodhouse * - Virt: If the host supports x2apic without a virtualized IR
188851130d21SDavid Woodhouse * unit then bit 0-6 of dmar_index_0_14 are providing bit
188951130d21SDavid Woodhouse * 8-14 of the destination id.
18905d5a9713SDavid Woodhouse *
18915d5a9713SDavid Woodhouse * All other modes have bit 0-6 of dmar_index_0_14 cleared and the
18925d5a9713SDavid Woodhouse * topmost 8 bits are destination id bit 0-7 (entry::destid_0_7).
18935d5a9713SDavid Woodhouse */
18945d5a9713SDavid Woodhouse entry->ir_index_0_14 = msg.arch_addr_lo.dmar_index_0_14;
18955d5a9713SDavid Woodhouse }
18965d5a9713SDavid Woodhouse
ioapic_configure_entry(struct irq_data * irqd)189790ad9e2dSThomas Gleixner static void ioapic_configure_entry(struct irq_data *irqd)
189890ad9e2dSThomas Gleixner {
189990ad9e2dSThomas Gleixner struct mp_chip_data *mpd = irqd->chip_data;
190090ad9e2dSThomas Gleixner struct irq_pin_list *entry;
190190ad9e2dSThomas Gleixner
19025d5a9713SDavid Woodhouse ioapic_setup_msg_from_msi(irqd, &mpd->entry);
19035d5a9713SDavid Woodhouse
190490ad9e2dSThomas Gleixner for_each_irq_pin(entry, mpd->irq_2_pin)
190590ad9e2dSThomas Gleixner __ioapic_write_entry(entry->apic, entry->pin, mpd->entry);
190690ad9e2dSThomas Gleixner }
190790ad9e2dSThomas Gleixner
ioapic_set_affinity(struct irq_data * irq_data,const struct cpumask * mask,bool force)1908d32932d0SJiang Liu static int ioapic_set_affinity(struct irq_data *irq_data,
1909d32932d0SJiang Liu const struct cpumask *mask, bool force)
1910d32932d0SJiang Liu {
1911d32932d0SJiang Liu struct irq_data *parent = irq_data->parent_data;
1912d32932d0SJiang Liu unsigned long flags;
1913d32932d0SJiang Liu int ret;
1914d32932d0SJiang Liu
1915d32932d0SJiang Liu ret = parent->chip->irq_set_affinity(parent, mask, force);
1916d32932d0SJiang Liu raw_spin_lock_irqsave(&ioapic_lock, flags);
191790ad9e2dSThomas Gleixner if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE)
191890ad9e2dSThomas Gleixner ioapic_configure_entry(irq_data);
1919d32932d0SJiang Liu raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1920d32932d0SJiang Liu
1921d32932d0SJiang Liu return ret;
1922d32932d0SJiang Liu }
1923d32932d0SJiang Liu
1924dfe0cf8bSThomas Gleixner /*
1925dfe0cf8bSThomas Gleixner * Interrupt shutdown masks the ioapic pin, but the interrupt might already
1926dfe0cf8bSThomas Gleixner * be in flight, but not yet serviced by the target CPU. That means
1927dfe0cf8bSThomas Gleixner * __synchronize_hardirq() would return and claim that everything is calmed
1928dfe0cf8bSThomas Gleixner * down. So free_irq() would proceed and deactivate the interrupt and free
1929dfe0cf8bSThomas Gleixner * resources.
1930dfe0cf8bSThomas Gleixner *
1931dfe0cf8bSThomas Gleixner * Once the target CPU comes around to service it it will find a cleared
1932dfe0cf8bSThomas Gleixner * vector and complain. While the spurious interrupt is harmless, the full
1933dfe0cf8bSThomas Gleixner * release of resources might prevent the interrupt from being acknowledged
1934dfe0cf8bSThomas Gleixner * which keeps the hardware in a weird state.
1935dfe0cf8bSThomas Gleixner *
1936dfe0cf8bSThomas Gleixner * Verify that the corresponding Remote-IRR bits are clear.
1937dfe0cf8bSThomas Gleixner */
ioapic_irq_get_chip_state(struct irq_data * irqd,enum irqchip_irq_state which,bool * state)1938dfe0cf8bSThomas Gleixner static int ioapic_irq_get_chip_state(struct irq_data *irqd,
1939dfe0cf8bSThomas Gleixner enum irqchip_irq_state which,
1940dfe0cf8bSThomas Gleixner bool *state)
1941dfe0cf8bSThomas Gleixner {
1942dfe0cf8bSThomas Gleixner struct mp_chip_data *mcd = irqd->chip_data;
1943dfe0cf8bSThomas Gleixner struct IO_APIC_route_entry rentry;
1944dfe0cf8bSThomas Gleixner struct irq_pin_list *p;
1945dfe0cf8bSThomas Gleixner
1946dfe0cf8bSThomas Gleixner if (which != IRQCHIP_STATE_ACTIVE)
1947dfe0cf8bSThomas Gleixner return -EINVAL;
1948dfe0cf8bSThomas Gleixner
1949dfe0cf8bSThomas Gleixner *state = false;
1950dfe0cf8bSThomas Gleixner raw_spin_lock(&ioapic_lock);
1951dfe0cf8bSThomas Gleixner for_each_irq_pin(p, mcd->irq_2_pin) {
1952dfe0cf8bSThomas Gleixner rentry = __ioapic_read_entry(p->apic, p->pin);
1953dfe0cf8bSThomas Gleixner /*
1954dfe0cf8bSThomas Gleixner * The remote IRR is only valid in level trigger mode. It's
1955dfe0cf8bSThomas Gleixner * meaning is undefined for edge triggered interrupts and
1956dfe0cf8bSThomas Gleixner * irrelevant because the IO-APIC treats them as fire and
1957dfe0cf8bSThomas Gleixner * forget.
1958dfe0cf8bSThomas Gleixner */
1959341b4a72SThomas Gleixner if (rentry.irr && rentry.is_level) {
1960dfe0cf8bSThomas Gleixner *state = true;
1961dfe0cf8bSThomas Gleixner break;
1962dfe0cf8bSThomas Gleixner }
1963dfe0cf8bSThomas Gleixner }
1964dfe0cf8bSThomas Gleixner raw_spin_unlock(&ioapic_lock);
1965dfe0cf8bSThomas Gleixner return 0;
1966dfe0cf8bSThomas Gleixner }
1967dfe0cf8bSThomas Gleixner
1968f62bae50SIngo Molnar static struct irq_chip ioapic_chip __read_mostly = {
1969f62bae50SIngo Molnar .name = "IO-APIC",
197061a38ce3SThomas Gleixner .irq_startup = startup_ioapic_irq,
197190297c5fSThomas Gleixner .irq_mask = mask_ioapic_irq,
197290297c5fSThomas Gleixner .irq_unmask = unmask_ioapic_irq,
1973d32932d0SJiang Liu .irq_ack = irq_chip_ack_parent,
1974d32932d0SJiang Liu .irq_eoi = ioapic_ack_level,
1975d32932d0SJiang Liu .irq_set_affinity = ioapic_set_affinity,
1976a9b4f087SRuslan Ruslichenko .irq_retrigger = irq_chip_retrigger_hierarchy,
1977dfe0cf8bSThomas Gleixner .irq_get_irqchip_state = ioapic_irq_get_chip_state,
19780c0e37dcSThomas Gleixner .flags = IRQCHIP_SKIP_SET_WAKE |
19790c0e37dcSThomas Gleixner IRQCHIP_AFFINITY_PRE_STARTUP,
1980d32932d0SJiang Liu };
1981d32932d0SJiang Liu
1982d32932d0SJiang Liu static struct irq_chip ioapic_ir_chip __read_mostly = {
1983d32932d0SJiang Liu .name = "IR-IO-APIC",
1984d32932d0SJiang Liu .irq_startup = startup_ioapic_irq,
1985d32932d0SJiang Liu .irq_mask = mask_ioapic_irq,
1986d32932d0SJiang Liu .irq_unmask = unmask_ioapic_irq,
1987d32932d0SJiang Liu .irq_ack = irq_chip_ack_parent,
1988d32932d0SJiang Liu .irq_eoi = ioapic_ir_ack_level,
1989d32932d0SJiang Liu .irq_set_affinity = ioapic_set_affinity,
1990a9b4f087SRuslan Ruslichenko .irq_retrigger = irq_chip_retrigger_hierarchy,
1991dfe0cf8bSThomas Gleixner .irq_get_irqchip_state = ioapic_irq_get_chip_state,
19920c0e37dcSThomas Gleixner .flags = IRQCHIP_SKIP_SET_WAKE |
19930c0e37dcSThomas Gleixner IRQCHIP_AFFINITY_PRE_STARTUP,
1994f62bae50SIngo Molnar };
1995f62bae50SIngo Molnar
init_IO_APIC_traps(void)1996f62bae50SIngo Molnar static inline void init_IO_APIC_traps(void)
1997f62bae50SIngo Molnar {
1998f62bae50SIngo Molnar struct irq_cfg *cfg;
1999ad9f4334SThomas Gleixner unsigned int irq;
2000f62bae50SIngo Molnar
2001ad9f4334SThomas Gleixner for_each_active_irq(irq) {
200232f5ef5dSJiang Liu cfg = irq_cfg(irq);
2003f62bae50SIngo Molnar if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
2004f62bae50SIngo Molnar /*
2005f62bae50SIngo Molnar * Hmm.. We don't have an entry for this,
2006f62bae50SIngo Molnar * so default to an old-fashioned 8259
2007f62bae50SIngo Molnar * interrupt if we can..
2008f62bae50SIngo Molnar */
200995d76accSJiang Liu if (irq < nr_legacy_irqs())
2010b81bb373SJacob Pan legacy_pic->make_irq(irq);
2011f62bae50SIngo Molnar else
2012f62bae50SIngo Molnar /* Strange. Oh, well.. */
20132c778651SThomas Gleixner irq_set_chip(irq, &no_irq_chip);
2014f62bae50SIngo Molnar }
2015f62bae50SIngo Molnar }
2016f62bae50SIngo Molnar }
2017f62bae50SIngo Molnar
2018f62bae50SIngo Molnar /*
2019f62bae50SIngo Molnar * The local APIC irq-chip implementation:
2020f62bae50SIngo Molnar */
2021f62bae50SIngo Molnar
mask_lapic_irq(struct irq_data * data)202290297c5fSThomas Gleixner static void mask_lapic_irq(struct irq_data *data)
2023f62bae50SIngo Molnar {
2024f62bae50SIngo Molnar unsigned long v;
2025f62bae50SIngo Molnar
2026f62bae50SIngo Molnar v = apic_read(APIC_LVT0);
2027f62bae50SIngo Molnar apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2028f62bae50SIngo Molnar }
2029f62bae50SIngo Molnar
unmask_lapic_irq(struct irq_data * data)203090297c5fSThomas Gleixner static void unmask_lapic_irq(struct irq_data *data)
2031f62bae50SIngo Molnar {
2032f62bae50SIngo Molnar unsigned long v;
2033f62bae50SIngo Molnar
2034f62bae50SIngo Molnar v = apic_read(APIC_LVT0);
2035f62bae50SIngo Molnar apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2036f62bae50SIngo Molnar }
2037f62bae50SIngo Molnar
ack_lapic_irq(struct irq_data * data)203890297c5fSThomas Gleixner static void ack_lapic_irq(struct irq_data *data)
2039f62bae50SIngo Molnar {
2040670c04adSDave Hansen apic_eoi();
2041f62bae50SIngo Molnar }
2042f62bae50SIngo Molnar
2043f62bae50SIngo Molnar static struct irq_chip lapic_chip __read_mostly = {
2044f62bae50SIngo Molnar .name = "local-APIC",
204590297c5fSThomas Gleixner .irq_mask = mask_lapic_irq,
204690297c5fSThomas Gleixner .irq_unmask = unmask_lapic_irq,
204790297c5fSThomas Gleixner .irq_ack = ack_lapic_irq,
2048f62bae50SIngo Molnar };
2049f62bae50SIngo Molnar
lapic_register_intr(int irq)205060c69948SThomas Gleixner static void lapic_register_intr(int irq)
2051f62bae50SIngo Molnar {
205260c69948SThomas Gleixner irq_clear_status_flags(irq, IRQ_LEVEL);
20532c778651SThomas Gleixner irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2054f62bae50SIngo Molnar "edge");
2055f62bae50SIngo Molnar }
2056f62bae50SIngo Molnar
2057f62bae50SIngo Molnar /*
2058f62bae50SIngo Molnar * This looks a bit hackish but it's about the only one way of sending
2059f62bae50SIngo Molnar * a few INTA cycles to 8259As and any associated glue logic. ICR does
2060f62bae50SIngo Molnar * not support the ExtINT mode, unfortunately. We need to send these
2061f62bae50SIngo Molnar * cycles as some i82489DX-based boards have glue logic that keeps the
2062f62bae50SIngo Molnar * 8259A interrupt line asserted until INTA. --macro
2063f62bae50SIngo Molnar */
unlock_ExtINT_logic(void)2064f62bae50SIngo Molnar static inline void __init unlock_ExtINT_logic(void)
2065f62bae50SIngo Molnar {
2066f62bae50SIngo Molnar int apic, pin, i;
2067f62bae50SIngo Molnar struct IO_APIC_route_entry entry0, entry1;
2068f62bae50SIngo Molnar unsigned char save_control, save_freq_select;
206951130d21SDavid Woodhouse u32 apic_id;
2070f62bae50SIngo Molnar
2071f62bae50SIngo Molnar pin = find_isa_irq_pin(8, mp_INT);
2072f62bae50SIngo Molnar if (pin == -1) {
2073f62bae50SIngo Molnar WARN_ON_ONCE(1);
2074f62bae50SIngo Molnar return;
2075f62bae50SIngo Molnar }
2076f62bae50SIngo Molnar apic = find_isa_irq_apic(8, mp_INT);
2077f62bae50SIngo Molnar if (apic == -1) {
2078f62bae50SIngo Molnar WARN_ON_ONCE(1);
2079f62bae50SIngo Molnar return;
2080f62bae50SIngo Molnar }
2081f62bae50SIngo Molnar
2082f62bae50SIngo Molnar entry0 = ioapic_read_entry(apic, pin);
2083f62bae50SIngo Molnar clear_IO_APIC_pin(apic, pin);
2084f62bae50SIngo Molnar
2085a6625b47SThomas Gleixner apic_id = read_apic_id();
2086f62bae50SIngo Molnar memset(&entry1, 0, sizeof(entry1));
2087f62bae50SIngo Molnar
2088341b4a72SThomas Gleixner entry1.dest_mode_logical = true;
2089341b4a72SThomas Gleixner entry1.masked = false;
209051130d21SDavid Woodhouse entry1.destid_0_7 = apic_id & 0xFF;
209151130d21SDavid Woodhouse entry1.virt_destid_8_14 = apic_id >> 8;
209272161299SThomas Gleixner entry1.delivery_mode = APIC_DELIVERY_MODE_EXTINT;
2093341b4a72SThomas Gleixner entry1.active_low = entry0.active_low;
2094341b4a72SThomas Gleixner entry1.is_level = false;
2095f62bae50SIngo Molnar entry1.vector = 0;
2096f62bae50SIngo Molnar
2097f62bae50SIngo Molnar ioapic_write_entry(apic, pin, entry1);
2098f62bae50SIngo Molnar
2099f62bae50SIngo Molnar save_control = CMOS_READ(RTC_CONTROL);
2100f62bae50SIngo Molnar save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2101f62bae50SIngo Molnar CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2102f62bae50SIngo Molnar RTC_FREQ_SELECT);
2103f62bae50SIngo Molnar CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2104f62bae50SIngo Molnar
2105f62bae50SIngo Molnar i = 100;
2106f62bae50SIngo Molnar while (i-- > 0) {
2107f62bae50SIngo Molnar mdelay(10);
2108f62bae50SIngo Molnar if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2109f62bae50SIngo Molnar i -= 10;
2110f62bae50SIngo Molnar }
2111f62bae50SIngo Molnar
2112f62bae50SIngo Molnar CMOS_WRITE(save_control, RTC_CONTROL);
2113f62bae50SIngo Molnar CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2114f62bae50SIngo Molnar clear_IO_APIC_pin(apic, pin);
2115f62bae50SIngo Molnar
2116f62bae50SIngo Molnar ioapic_write_entry(apic, pin, entry0);
2117f62bae50SIngo Molnar }
2118f62bae50SIngo Molnar
2119f62bae50SIngo Molnar static int disable_timer_pin_1 __initdata;
2120f62bae50SIngo Molnar /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
disable_timer_pin_setup(char * arg)2121f62bae50SIngo Molnar static int __init disable_timer_pin_setup(char *arg)
2122f62bae50SIngo Molnar {
2123f62bae50SIngo Molnar disable_timer_pin_1 = 1;
2124f62bae50SIngo Molnar return 0;
2125f62bae50SIngo Molnar }
2126f62bae50SIngo Molnar early_param("disable_timer_pin_1", disable_timer_pin_setup);
2127f62bae50SIngo Molnar
mp_alloc_timer_irq(int ioapic,int pin)2128d32932d0SJiang Liu static int mp_alloc_timer_irq(int ioapic, int pin)
2129d32932d0SJiang Liu {
2130d32932d0SJiang Liu int irq = -1;
2131d32932d0SJiang Liu struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
2132d32932d0SJiang Liu
2133d32932d0SJiang Liu if (domain) {
21344467715aSJiang Liu struct irq_alloc_info info;
21354467715aSJiang Liu
2136d32932d0SJiang Liu ioapic_set_alloc_attr(&info, NUMA_NO_NODE, 0, 0);
213733a65ba4SThomas Gleixner info.devid = mpc_ioapic_id(ioapic);
213833a65ba4SThomas Gleixner info.ioapic.pin = pin;
2139d32932d0SJiang Liu mutex_lock(&ioapic_mutex);
2140d32932d0SJiang Liu irq = alloc_isa_irq_from_domain(domain, 0, ioapic, pin, &info);
2141d32932d0SJiang Liu mutex_unlock(&ioapic_mutex);
2142d32932d0SJiang Liu }
2143d32932d0SJiang Liu
2144d32932d0SJiang Liu return irq;
2145d32932d0SJiang Liu }
2146d32932d0SJiang Liu
2147f62bae50SIngo Molnar /*
2148f62bae50SIngo Molnar * This code may look a bit paranoid, but it's supposed to cooperate with
2149f62bae50SIngo Molnar * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2150f62bae50SIngo Molnar * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2151f62bae50SIngo Molnar * fanatically on his truly buggy board.
2152f62bae50SIngo Molnar *
2153f62bae50SIngo Molnar * FIXME: really need to revamp this for all platforms.
2154f62bae50SIngo Molnar */
check_timer(void)2155f62bae50SIngo Molnar static inline void __init check_timer(void)
2156f62bae50SIngo Molnar {
21574467715aSJiang Liu struct irq_data *irq_data = irq_get_irq_data(0);
21584467715aSJiang Liu struct mp_chip_data *data = irq_data->chip_data;
21594467715aSJiang Liu struct irq_cfg *cfg = irqd_cfg(irq_data);
2160f6e9456cSRobert Richter int node = cpu_to_node(0);
2161f62bae50SIngo Molnar int apic1, pin1, apic2, pin2;
2162f62bae50SIngo Molnar int no_pin1 = 0;
2163f62bae50SIngo Molnar
2164c8c40767SThomas Gleixner if (!global_clock_event)
2165c8c40767SThomas Gleixner return;
2166c8c40767SThomas Gleixner
2167058df195SThomas Gleixner local_irq_disable();
2168f62bae50SIngo Molnar
2169f62bae50SIngo Molnar /*
2170f62bae50SIngo Molnar * get/set the timer IRQ vector:
2171f62bae50SIngo Molnar */
21724305df94SThomas Gleixner legacy_pic->mask(0);
2173f62bae50SIngo Molnar
2174f62bae50SIngo Molnar /*
2175f62bae50SIngo Molnar * As IRQ0 is to be enabled in the 8259A, the virtual
2176f62bae50SIngo Molnar * wire has to be disabled in the local APIC. Also
2177f62bae50SIngo Molnar * timer interrupts need to be acknowledged manually in
2178f62bae50SIngo Molnar * the 8259A for the i82489DX when using the NMI
2179f62bae50SIngo Molnar * watchdog as that APIC treats NMIs as level-triggered.
2180f62bae50SIngo Molnar * The AEOI mode will finish them in the 8259A
2181f62bae50SIngo Molnar * automatically.
2182f62bae50SIngo Molnar */
2183f62bae50SIngo Molnar apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2184b81bb373SJacob Pan legacy_pic->init(1);
2185f62bae50SIngo Molnar
2186f62bae50SIngo Molnar pin1 = find_isa_irq_pin(0, mp_INT);
2187f62bae50SIngo Molnar apic1 = find_isa_irq_apic(0, mp_INT);
2188f62bae50SIngo Molnar pin2 = ioapic_i8259.pin;
2189f62bae50SIngo Molnar apic2 = ioapic_i8259.apic;
2190f62bae50SIngo Molnar
2191f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2192f62bae50SIngo Molnar "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2193f62bae50SIngo Molnar cfg->vector, apic1, pin1, apic2, pin2);
2194f62bae50SIngo Molnar
2195f62bae50SIngo Molnar /*
2196f62bae50SIngo Molnar * Some BIOS writers are clueless and report the ExtINTA
2197f62bae50SIngo Molnar * I/O APIC input from the cascaded 8259A as the timer
2198f62bae50SIngo Molnar * interrupt input. So just in case, if only one pin
2199f62bae50SIngo Molnar * was found above, try it both directly and through the
2200f62bae50SIngo Molnar * 8259A.
2201f62bae50SIngo Molnar */
2202f62bae50SIngo Molnar if (pin1 == -1) {
22036a9f5de2SJoerg Roedel panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
2204f62bae50SIngo Molnar pin1 = pin2;
2205f62bae50SIngo Molnar apic1 = apic2;
2206f62bae50SIngo Molnar no_pin1 = 1;
2207f62bae50SIngo Molnar } else if (pin2 == -1) {
2208f62bae50SIngo Molnar pin2 = pin1;
2209f62bae50SIngo Molnar apic2 = apic1;
2210f62bae50SIngo Molnar }
2211f62bae50SIngo Molnar
2212f62bae50SIngo Molnar if (pin1 != -1) {
2213d32932d0SJiang Liu /* Ok, does IRQ0 through the IOAPIC work? */
2214f62bae50SIngo Molnar if (no_pin1) {
2215d32932d0SJiang Liu mp_alloc_timer_irq(apic1, pin1);
2216f62bae50SIngo Molnar } else {
2217d32932d0SJiang Liu /*
2218d32932d0SJiang Liu * for edge trigger, it's already unmasked,
2219f62bae50SIngo Molnar * so only need to unmask if it is level-trigger
2220f62bae50SIngo Molnar * do we really have level trigger timer?
2221f62bae50SIngo Molnar */
2222a27dca64SThomas Gleixner int idx = find_irq_entry(apic1, pin1, mp_INT);
2223a27dca64SThomas Gleixner
2224a27dca64SThomas Gleixner if (idx != -1 && irq_is_level(idx))
2225e708e35bSSeunghun Han unmask_ioapic_irq(irq_get_irq_data(0));
2226f62bae50SIngo Molnar }
2227aaaec6fcSThomas Gleixner irq_domain_deactivate_irq(irq_data);
222842e1cc2dSThomas Gleixner irq_domain_activate_irq(irq_data, false);
2229f62bae50SIngo Molnar if (timer_irq_works()) {
2230f62bae50SIngo Molnar if (disable_timer_pin_1 > 0)
2231f62bae50SIngo Molnar clear_IO_APIC_pin(0, pin1);
2232f62bae50SIngo Molnar goto out;
2233f62bae50SIngo Molnar }
22346a9f5de2SJoerg Roedel panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
2235f62bae50SIngo Molnar clear_IO_APIC_pin(apic1, pin1);
2236f62bae50SIngo Molnar if (!no_pin1)
2237f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2238f62bae50SIngo Molnar "8254 timer not connected to IO-APIC\n");
2239f62bae50SIngo Molnar
2240f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2241f62bae50SIngo Molnar "(IRQ0) through the 8259A ...\n");
2242f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO
2243f62bae50SIngo Molnar "..... (found apic %d pin %d) ...\n", apic2, pin2);
2244f62bae50SIngo Molnar /*
2245f62bae50SIngo Molnar * legacy devices should be connected to IO APIC #0
2246f62bae50SIngo Molnar */
22474467715aSJiang Liu replace_pin_at_irq_node(data, node, apic1, pin1, apic2, pin2);
2248aaaec6fcSThomas Gleixner irq_domain_deactivate_irq(irq_data);
224942e1cc2dSThomas Gleixner irq_domain_activate_irq(irq_data, false);
22504305df94SThomas Gleixner legacy_pic->unmask(0);
2251f62bae50SIngo Molnar if (timer_irq_works()) {
2252f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2253f62bae50SIngo Molnar goto out;
2254f62bae50SIngo Molnar }
2255f62bae50SIngo Molnar /*
2256f62bae50SIngo Molnar * Cleanup, just in case ...
2257f62bae50SIngo Molnar */
22584305df94SThomas Gleixner legacy_pic->mask(0);
2259f62bae50SIngo Molnar clear_IO_APIC_pin(apic2, pin2);
2260f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2261f62bae50SIngo Molnar }
2262f62bae50SIngo Molnar
2263f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO
2264f62bae50SIngo Molnar "...trying to set up timer as Virtual Wire IRQ...\n");
2265f62bae50SIngo Molnar
226660c69948SThomas Gleixner lapic_register_intr(0);
2267f62bae50SIngo Molnar apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
22684305df94SThomas Gleixner legacy_pic->unmask(0);
2269f62bae50SIngo Molnar
2270f62bae50SIngo Molnar if (timer_irq_works()) {
2271f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2272f62bae50SIngo Molnar goto out;
2273f62bae50SIngo Molnar }
22744305df94SThomas Gleixner legacy_pic->mask(0);
2275f62bae50SIngo Molnar apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2276f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2277f62bae50SIngo Molnar
2278f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO
2279f62bae50SIngo Molnar "...trying to set up timer as ExtINT IRQ...\n");
2280f62bae50SIngo Molnar
2281b81bb373SJacob Pan legacy_pic->init(0);
2282b81bb373SJacob Pan legacy_pic->make_irq(0);
2283f62bae50SIngo Molnar apic_write(APIC_LVT0, APIC_DM_EXTINT);
228486a82ae0SThomas Gleixner legacy_pic->unmask(0);
2285f62bae50SIngo Molnar
2286f62bae50SIngo Molnar unlock_ExtINT_logic();
2287f62bae50SIngo Molnar
2288f62bae50SIngo Molnar if (timer_irq_works()) {
2289f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2290f62bae50SIngo Molnar goto out;
2291f62bae50SIngo Molnar }
2292f62bae50SIngo Molnar apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
22932ca5b404SThomas Gleixner if (apic_is_x2apic_enabled())
2294fb209bd8SYinghai Lu apic_printk(APIC_QUIET, KERN_INFO
2295fb209bd8SYinghai Lu "Perhaps problem with the pre-enabled x2apic mode\n"
2296fb209bd8SYinghai Lu "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
2297f62bae50SIngo Molnar panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2298f62bae50SIngo Molnar "report. Then try booting with the 'noapic' option.\n");
2299f62bae50SIngo Molnar out:
2300058df195SThomas Gleixner local_irq_enable();
2301f62bae50SIngo Molnar }
2302f62bae50SIngo Molnar
2303f62bae50SIngo Molnar /*
2304f62bae50SIngo Molnar * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2305f62bae50SIngo Molnar * to devices. However there may be an I/O APIC pin available for
2306f62bae50SIngo Molnar * this interrupt regardless. The pin may be left unconnected, but
2307f62bae50SIngo Molnar * typically it will be reused as an ExtINT cascade interrupt for
2308f62bae50SIngo Molnar * the master 8259A. In the MPS case such a pin will normally be
2309f62bae50SIngo Molnar * reported as an ExtINT interrupt in the MP table. With ACPI
2310f62bae50SIngo Molnar * there is no provision for ExtINT interrupts, and in the absence
2311f62bae50SIngo Molnar * of an override it would be treated as an ordinary ISA I/O APIC
2312f62bae50SIngo Molnar * interrupt, that is edge-triggered and unmasked by default. We
2313f62bae50SIngo Molnar * used to do this, but it caused problems on some systems because
2314f62bae50SIngo Molnar * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2315f62bae50SIngo Molnar * the same ExtINT cascade interrupt to drive the local APIC of the
2316f62bae50SIngo Molnar * bootstrap processor. Therefore we refrain from routing IRQ2 to
2317f62bae50SIngo Molnar * the I/O APIC in all cases now. No actual device should request
2318f62bae50SIngo Molnar * it anyway. --macro
2319f62bae50SIngo Molnar */
2320bc07844aSThomas Gleixner #define PIC_IRQS (1UL << PIC_CASCADE_IR)
2321f62bae50SIngo Molnar
mp_irqdomain_create(int ioapic)232244767bfaSJiang Liu static int mp_irqdomain_create(int ioapic)
232344767bfaSJiang Liu {
2324d32932d0SJiang Liu struct irq_domain *parent;
232544767bfaSJiang Liu int hwirqs = mp_ioapic_pin_count(ioapic);
232644767bfaSJiang Liu struct ioapic *ip = &ioapics[ioapic];
232744767bfaSJiang Liu struct ioapic_domain_cfg *cfg = &ip->irqdomain_cfg;
232844767bfaSJiang Liu struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
23291b604745SThomas Gleixner struct fwnode_handle *fn;
2330b643128bSDavid Woodhouse struct irq_fwspec fwspec;
233144767bfaSJiang Liu
233244767bfaSJiang Liu if (cfg->type == IOAPIC_DOMAIN_INVALID)
233344767bfaSJiang Liu return 0;
233444767bfaSJiang Liu
23351b604745SThomas Gleixner /* Handle device tree enumerated APICs proper */
23361b604745SThomas Gleixner if (cfg->dev) {
23371b604745SThomas Gleixner fn = of_node_to_fwnode(cfg->dev);
23381b604745SThomas Gleixner } else {
2339f36a74b9SDavid Woodhouse fn = irq_domain_alloc_named_id_fwnode("IO-APIC", mpc_ioapic_id(ioapic));
23401b604745SThomas Gleixner if (!fn)
23411b604745SThomas Gleixner return -ENOMEM;
23421b604745SThomas Gleixner }
23431b604745SThomas Gleixner
2344b643128bSDavid Woodhouse fwspec.fwnode = fn;
2345b643128bSDavid Woodhouse fwspec.param_count = 1;
2346f36a74b9SDavid Woodhouse fwspec.param[0] = mpc_ioapic_id(ioapic);
2347b643128bSDavid Woodhouse
2348b643128bSDavid Woodhouse parent = irq_find_matching_fwspec(&fwspec, DOMAIN_BUS_ANY);
2349b643128bSDavid Woodhouse if (!parent) {
2350b643128bSDavid Woodhouse if (!cfg->dev)
2351b643128bSDavid Woodhouse irq_domain_free_fwnode(fn);
2352b643128bSDavid Woodhouse return -ENODEV;
2353b643128bSDavid Woodhouse }
2354b643128bSDavid Woodhouse
2355bc1bc1b3SJohan Hovold ip->irqdomain = irq_domain_create_hierarchy(parent, 0, hwirqs, fn, cfg->ops,
235644767bfaSJiang Liu (void *)(long)ioapic);
2357e3beca48SThomas Gleixner if (!ip->irqdomain) {
23581b604745SThomas Gleixner /* Release fw handle if it was allocated above */
23591b604745SThomas Gleixner if (!cfg->dev)
23601b604745SThomas Gleixner irq_domain_free_fwnode(fn);
236144767bfaSJiang Liu return -ENOMEM;
2362e3beca48SThomas Gleixner }
2363b75e818fSJiang Liu
236444767bfaSJiang Liu if (cfg->type == IOAPIC_DOMAIN_LEGACY ||
236544767bfaSJiang Liu cfg->type == IOAPIC_DOMAIN_STRICT)
236644767bfaSJiang Liu ioapic_dynirq_base = max(ioapic_dynirq_base,
236744767bfaSJiang Liu gsi_cfg->gsi_end + 1);
236844767bfaSJiang Liu
236944767bfaSJiang Liu return 0;
237044767bfaSJiang Liu }
237144767bfaSJiang Liu
ioapic_destroy_irqdomain(int idx)237215516a3bSJiang Liu static void ioapic_destroy_irqdomain(int idx)
237315516a3bSJiang Liu {
2374ec016089SJon Derrick struct ioapic_domain_cfg *cfg = &ioapics[idx].irqdomain_cfg;
2375ec016089SJon Derrick struct fwnode_handle *fn = ioapics[idx].irqdomain->fwnode;
2376ec016089SJon Derrick
237715516a3bSJiang Liu if (ioapics[idx].irqdomain) {
237815516a3bSJiang Liu irq_domain_remove(ioapics[idx].irqdomain);
2379ec016089SJon Derrick if (!cfg->dev)
2380ec016089SJon Derrick irq_domain_free_fwnode(fn);
238115516a3bSJiang Liu ioapics[idx].irqdomain = NULL;
238215516a3bSJiang Liu }
238315516a3bSJiang Liu }
238415516a3bSJiang Liu
setup_IO_APIC(void)2385f62bae50SIngo Molnar void __init setup_IO_APIC(void)
2386f62bae50SIngo Molnar {
238744767bfaSJiang Liu int ioapic;
2388f62bae50SIngo Molnar
2389ecf600f8SThomas Gleixner if (ioapic_is_disabled || !nr_ioapics)
2390a46f5c89SThomas Gleixner return;
2391a46f5c89SThomas Gleixner
239295d76accSJiang Liu io_apic_irqs = nr_legacy_irqs() ? ~PIC_IRQS : ~0UL;
2393f62bae50SIngo Molnar
2394f62bae50SIngo Molnar apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
239544767bfaSJiang Liu for_each_ioapic(ioapic)
239644767bfaSJiang Liu BUG_ON(mp_irqdomain_create(ioapic));
239744767bfaSJiang Liu
2398f62bae50SIngo Molnar /*
2399f62bae50SIngo Molnar * Set up IO-APIC IRQ routing.
2400f62bae50SIngo Molnar */
2401de934103SThomas Gleixner x86_init.mpparse.setup_ioapic_ids();
2402de934103SThomas Gleixner
2403f62bae50SIngo Molnar sync_Arb_IDs();
2404f62bae50SIngo Molnar setup_IO_APIC_irqs();
2405f62bae50SIngo Molnar init_IO_APIC_traps();
240695d76accSJiang Liu if (nr_legacy_irqs())
2407f62bae50SIngo Molnar check_timer();
2408b81975eaSJiang Liu
2409b81975eaSJiang Liu ioapic_initialized = 1;
2410f62bae50SIngo Molnar }
2411f62bae50SIngo Molnar
resume_ioapic_id(int ioapic_idx)24126f50d45fSYinghai Lu static void resume_ioapic_id(int ioapic_idx)
2413f62bae50SIngo Molnar {
2414f62bae50SIngo Molnar unsigned long flags;
2415f62bae50SIngo Molnar union IO_APIC_reg_00 reg_00;
2416f62bae50SIngo Molnar
2417dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
24186f50d45fSYinghai Lu reg_00.raw = io_apic_read(ioapic_idx, 0);
24196f50d45fSYinghai Lu if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
24206f50d45fSYinghai Lu reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
24216f50d45fSYinghai Lu io_apic_write(ioapic_idx, 0, reg_00.raw);
2422f62bae50SIngo Molnar }
2423dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2424f62bae50SIngo Molnar }
2425f62bae50SIngo Molnar
ioapic_resume(void)2426f3c6ea1bSRafael J. Wysocki static void ioapic_resume(void)
2427f3c6ea1bSRafael J. Wysocki {
24286f50d45fSYinghai Lu int ioapic_idx;
2429f3c6ea1bSRafael J. Wysocki
2430f44d1692SJiang Liu for_each_ioapic_reverse(ioapic_idx)
24316f50d45fSYinghai Lu resume_ioapic_id(ioapic_idx);
243215bac20bSSuresh Siddha
243315bac20bSSuresh Siddha restore_ioapic_entries();
2434f3c6ea1bSRafael J. Wysocki }
2435f3c6ea1bSRafael J. Wysocki
2436f3c6ea1bSRafael J. Wysocki static struct syscore_ops ioapic_syscore_ops = {
243715bac20bSSuresh Siddha .suspend = save_ioapic_entries,
2438f62bae50SIngo Molnar .resume = ioapic_resume,
2439f62bae50SIngo Molnar };
2440f62bae50SIngo Molnar
ioapic_init_ops(void)2441f3c6ea1bSRafael J. Wysocki static int __init ioapic_init_ops(void)
2442f62bae50SIngo Molnar {
2443f3c6ea1bSRafael J. Wysocki register_syscore_ops(&ioapic_syscore_ops);
2444f62bae50SIngo Molnar
2445f62bae50SIngo Molnar return 0;
2446f62bae50SIngo Molnar }
2447f62bae50SIngo Molnar
2448f3c6ea1bSRafael J. Wysocki device_initcall(ioapic_init_ops);
2449f62bae50SIngo Molnar
io_apic_get_redir_entries(int ioapic)245067dc5e70SJiang Liu static int io_apic_get_redir_entries(int ioapic)
2451f62bae50SIngo Molnar {
2452f62bae50SIngo Molnar union IO_APIC_reg_01 reg_01;
2453f62bae50SIngo Molnar unsigned long flags;
2454f62bae50SIngo Molnar
2455dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
2456f62bae50SIngo Molnar reg_01.raw = io_apic_read(ioapic, 1);
2457dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2458f62bae50SIngo Molnar
24594b6b19a1SEric W. Biederman /* The register returns the maximum index redir index
24604b6b19a1SEric W. Biederman * supported, which is one less than the total number of redir
24614b6b19a1SEric W. Biederman * entries.
24624b6b19a1SEric W. Biederman */
24634b6b19a1SEric W. Biederman return reg_01.bits.entries + 1;
2464f62bae50SIngo Molnar }
2465f62bae50SIngo Molnar
arch_dynirq_lower_bound(unsigned int from)246662a08ae2SThomas Gleixner unsigned int arch_dynirq_lower_bound(unsigned int from)
246762a08ae2SThomas Gleixner {
24685af507beSSaurabh Sengar unsigned int ret;
24695af507beSSaurabh Sengar
2470b81975eaSJiang Liu /*
2471b81975eaSJiang Liu * dmar_alloc_hwirq() may be called before setup_IO_APIC(), so use
2472b81975eaSJiang Liu * gsi_top if ioapic_dynirq_base hasn't been initialized yet.
2473b81975eaSJiang Liu */
24745af507beSSaurabh Sengar ret = ioapic_dynirq_base ? : gsi_top;
24755af507beSSaurabh Sengar
24763e5bedc2SThomas Gleixner /*
24775af507beSSaurabh Sengar * For DT enabled machines ioapic_dynirq_base is irrelevant and
24785af507beSSaurabh Sengar * always 0. gsi_top can be 0 if there is no IO/APIC registered.
24795af507beSSaurabh Sengar * 0 is an invalid interrupt number for dynamic allocations. Return
24805af507beSSaurabh Sengar * @from instead.
24813e5bedc2SThomas Gleixner */
24825af507beSSaurabh Sengar return ret ? : from;
248362a08ae2SThomas Gleixner }
248462a08ae2SThomas Gleixner
24852a4ab640SFeng Tang #ifdef CONFIG_X86_32
io_apic_get_unique_id(int ioapic,int apic_id)248667dc5e70SJiang Liu static int io_apic_get_unique_id(int ioapic, int apic_id)
2487f62bae50SIngo Molnar {
2488f62bae50SIngo Molnar union IO_APIC_reg_00 reg_00;
2489f62bae50SIngo Molnar static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2490f62bae50SIngo Molnar physid_mask_t tmp;
2491f62bae50SIngo Molnar unsigned long flags;
2492f62bae50SIngo Molnar int i = 0;
2493f62bae50SIngo Molnar
2494f62bae50SIngo Molnar /*
2495f62bae50SIngo Molnar * The P4 platform supports up to 256 APIC IDs on two separate APIC
2496f62bae50SIngo Molnar * buses (one for LAPICs, one for IOAPICs), where predecessors only
2497f62bae50SIngo Molnar * supports up to 16 on one shared APIC bus.
2498f62bae50SIngo Molnar *
2499f62bae50SIngo Molnar * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2500f62bae50SIngo Molnar * advantage of new APIC bus architecture.
2501f62bae50SIngo Molnar */
2502f62bae50SIngo Molnar
2503f62bae50SIngo Molnar if (physids_empty(apic_id_map))
25047abc0753SCyrill Gorcunov apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
2505f62bae50SIngo Molnar
2506dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
2507f62bae50SIngo Molnar reg_00.raw = io_apic_read(ioapic, 0);
2508dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2509f62bae50SIngo Molnar
2510f62bae50SIngo Molnar if (apic_id >= get_physical_broadcast()) {
2511f62bae50SIngo Molnar printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2512f62bae50SIngo Molnar "%d\n", ioapic, apic_id, reg_00.bits.ID);
2513f62bae50SIngo Molnar apic_id = reg_00.bits.ID;
2514f62bae50SIngo Molnar }
2515f62bae50SIngo Molnar
2516f62bae50SIngo Molnar /*
2517f62bae50SIngo Molnar * Every APIC in a system must have a unique ID or we get lots of nice
2518f62bae50SIngo Molnar * 'stuck on smp_invalidate_needed IPI wait' messages.
2519f62bae50SIngo Molnar */
25207abc0753SCyrill Gorcunov if (apic->check_apicid_used(&apic_id_map, apic_id)) {
2521f62bae50SIngo Molnar
2522f62bae50SIngo Molnar for (i = 0; i < get_physical_broadcast(); i++) {
25237abc0753SCyrill Gorcunov if (!apic->check_apicid_used(&apic_id_map, i))
2524f62bae50SIngo Molnar break;
2525f62bae50SIngo Molnar }
2526f62bae50SIngo Molnar
2527f62bae50SIngo Molnar if (i == get_physical_broadcast())
2528f62bae50SIngo Molnar panic("Max apic_id exceeded!\n");
2529f62bae50SIngo Molnar
2530f62bae50SIngo Molnar printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2531f62bae50SIngo Molnar "trying %d\n", ioapic, apic_id, i);
2532f62bae50SIngo Molnar
2533f62bae50SIngo Molnar apic_id = i;
2534f62bae50SIngo Molnar }
2535f62bae50SIngo Molnar
25369a2a637aSThomas Gleixner physid_set_mask_of_physid(apic_id, &tmp);
2537f62bae50SIngo Molnar physids_or(apic_id_map, apic_id_map, tmp);
2538f62bae50SIngo Molnar
2539f62bae50SIngo Molnar if (reg_00.bits.ID != apic_id) {
2540f62bae50SIngo Molnar reg_00.bits.ID = apic_id;
2541f62bae50SIngo Molnar
2542dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
2543f62bae50SIngo Molnar io_apic_write(ioapic, 0, reg_00.raw);
2544f62bae50SIngo Molnar reg_00.raw = io_apic_read(ioapic, 0);
2545dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2546f62bae50SIngo Molnar
2547f62bae50SIngo Molnar /* Sanity check */
2548f62bae50SIngo Molnar if (reg_00.bits.ID != apic_id) {
2549c767a54bSJoe Perches pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
2550c767a54bSJoe Perches ioapic);
2551f62bae50SIngo Molnar return -1;
2552f62bae50SIngo Molnar }
2553f62bae50SIngo Molnar }
2554f62bae50SIngo Molnar
2555f62bae50SIngo Molnar apic_printk(APIC_VERBOSE, KERN_INFO
2556f62bae50SIngo Molnar "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2557f62bae50SIngo Molnar
2558f62bae50SIngo Molnar return apic_id;
2559f62bae50SIngo Molnar }
256041098ffeSThomas Gleixner
io_apic_unique_id(int idx,u8 id)256167dc5e70SJiang Liu static u8 io_apic_unique_id(int idx, u8 id)
256241098ffeSThomas Gleixner {
256341098ffeSThomas Gleixner if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
2564cff9ab2bSDenys Vlasenko !APIC_XAPIC(boot_cpu_apic_version))
25655411dc4cSYinghai Lu return io_apic_get_unique_id(idx, id);
256641098ffeSThomas Gleixner else
256741098ffeSThomas Gleixner return id;
256841098ffeSThomas Gleixner }
256941098ffeSThomas Gleixner #else
io_apic_unique_id(int idx,u8 id)257067dc5e70SJiang Liu static u8 io_apic_unique_id(int idx, u8 id)
257141098ffeSThomas Gleixner {
25725411dc4cSYinghai Lu union IO_APIC_reg_00 reg_00;
257341098ffeSThomas Gleixner DECLARE_BITMAP(used, 256);
25745411dc4cSYinghai Lu unsigned long flags;
25755411dc4cSYinghai Lu u8 new_id;
25765411dc4cSYinghai Lu int i;
257741098ffeSThomas Gleixner
257841098ffeSThomas Gleixner bitmap_zero(used, 256);
2579f44d1692SJiang Liu for_each_ioapic(i)
2580d5371430SSuresh Siddha __set_bit(mpc_ioapic_id(i), used);
25815411dc4cSYinghai Lu
25825411dc4cSYinghai Lu /* Hand out the requested id if available */
258341098ffeSThomas Gleixner if (!test_bit(id, used))
258441098ffeSThomas Gleixner return id;
25855411dc4cSYinghai Lu
25865411dc4cSYinghai Lu /*
25875411dc4cSYinghai Lu * Read the current id from the ioapic and keep it if
25885411dc4cSYinghai Lu * available.
25895411dc4cSYinghai Lu */
25905411dc4cSYinghai Lu raw_spin_lock_irqsave(&ioapic_lock, flags);
25915411dc4cSYinghai Lu reg_00.raw = io_apic_read(idx, 0);
25925411dc4cSYinghai Lu raw_spin_unlock_irqrestore(&ioapic_lock, flags);
25935411dc4cSYinghai Lu new_id = reg_00.bits.ID;
25945411dc4cSYinghai Lu if (!test_bit(new_id, used)) {
25955411dc4cSYinghai Lu apic_printk(APIC_VERBOSE, KERN_INFO
25965411dc4cSYinghai Lu "IOAPIC[%d]: Using reg apic_id %d instead of %d\n",
25975411dc4cSYinghai Lu idx, new_id, id);
25985411dc4cSYinghai Lu return new_id;
25995411dc4cSYinghai Lu }
26005411dc4cSYinghai Lu
26015411dc4cSYinghai Lu /*
26025411dc4cSYinghai Lu * Get the next free id and write it to the ioapic.
26035411dc4cSYinghai Lu */
26045411dc4cSYinghai Lu new_id = find_first_zero_bit(used, 256);
26055411dc4cSYinghai Lu reg_00.bits.ID = new_id;
26065411dc4cSYinghai Lu raw_spin_lock_irqsave(&ioapic_lock, flags);
26075411dc4cSYinghai Lu io_apic_write(idx, 0, reg_00.raw);
26085411dc4cSYinghai Lu reg_00.raw = io_apic_read(idx, 0);
26095411dc4cSYinghai Lu raw_spin_unlock_irqrestore(&ioapic_lock, flags);
26105411dc4cSYinghai Lu /* Sanity check */
26115411dc4cSYinghai Lu BUG_ON(reg_00.bits.ID != new_id);
26125411dc4cSYinghai Lu
26135411dc4cSYinghai Lu return new_id;
261441098ffeSThomas Gleixner }
261558f892e0SNaga Chumbalkar #endif
2616f62bae50SIngo Molnar
io_apic_get_version(int ioapic)261767dc5e70SJiang Liu static int io_apic_get_version(int ioapic)
2618f62bae50SIngo Molnar {
2619f62bae50SIngo Molnar union IO_APIC_reg_01 reg_01;
2620f62bae50SIngo Molnar unsigned long flags;
2621f62bae50SIngo Molnar
2622dade7716SThomas Gleixner raw_spin_lock_irqsave(&ioapic_lock, flags);
2623f62bae50SIngo Molnar reg_01.raw = io_apic_read(ioapic, 1);
2624dade7716SThomas Gleixner raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2625f62bae50SIngo Molnar
2626f62bae50SIngo Molnar return reg_01.bits.version;
2627f62bae50SIngo Molnar }
2628f62bae50SIngo Molnar
2629f62bae50SIngo Molnar /*
2630c1d1ee9aSThomas Gleixner * This function updates target affinity of IOAPIC interrupts to include
2631c1d1ee9aSThomas Gleixner * the CPUs which came online during SMP bringup.
2632f62bae50SIngo Molnar */
2633f62bae50SIngo Molnar #define IOAPIC_RESOURCE_NAME_SIZE 11
2634f62bae50SIngo Molnar
2635f62bae50SIngo Molnar static struct resource *ioapic_resources;
2636f62bae50SIngo Molnar
ioapic_setup_resources(void)2637f44d1692SJiang Liu static struct resource * __init ioapic_setup_resources(void)
2638f62bae50SIngo Molnar {
2639f62bae50SIngo Molnar unsigned long n;
2640f62bae50SIngo Molnar struct resource *res;
2641f62bae50SIngo Molnar char *mem;
26424855531eSRui Wang int i;
2643f62bae50SIngo Molnar
26444855531eSRui Wang if (nr_ioapics == 0)
2645f62bae50SIngo Molnar return NULL;
2646f62bae50SIngo Molnar
2647f62bae50SIngo Molnar n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
26484855531eSRui Wang n *= nr_ioapics;
2649f62bae50SIngo Molnar
26507e1c4e27SMike Rapoport mem = memblock_alloc(n, SMP_CACHE_BYTES);
26518a7f97b9SMike Rapoport if (!mem)
26528a7f97b9SMike Rapoport panic("%s: Failed to allocate %lu bytes\n", __func__, n);
2653f62bae50SIngo Molnar res = (void *)mem;
2654f62bae50SIngo Molnar
26554855531eSRui Wang mem += sizeof(struct resource) * nr_ioapics;
2656f62bae50SIngo Molnar
2657f44d1692SJiang Liu for_each_ioapic(i) {
26584855531eSRui Wang res[i].name = mem;
26594855531eSRui Wang res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
26604343fe10SCyrill Gorcunov snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
2661f62bae50SIngo Molnar mem += IOAPIC_RESOURCE_NAME_SIZE;
26624855531eSRui Wang ioapics[i].iomem_res = &res[i];
2663f62bae50SIngo Molnar }
2664f62bae50SIngo Molnar
2665f62bae50SIngo Molnar ioapic_resources = res;
2666f62bae50SIngo Molnar
2667f62bae50SIngo Molnar return res;
2668f62bae50SIngo Molnar }
2669f62bae50SIngo Molnar
io_apic_set_fixmap(enum fixed_addresses idx,phys_addr_t phys)2670f4c9361fSIsaku Yamahata static void io_apic_set_fixmap(enum fixed_addresses idx, phys_addr_t phys)
2671f4c9361fSIsaku Yamahata {
2672f4c9361fSIsaku Yamahata pgprot_t flags = FIXMAP_PAGE_NOCACHE;
2673f4c9361fSIsaku Yamahata
2674f4c9361fSIsaku Yamahata /*
267588e378d4SMichael Kelley * Ensure fixmaps for IO-APIC MMIO respect memory encryption pgprot
2676f4c9361fSIsaku Yamahata * bits, just like normal ioremap():
2677f4c9361fSIsaku Yamahata */
267888e378d4SMichael Kelley if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
267988e378d4SMichael Kelley if (x86_platform.hyper.is_private_mmio(phys))
268088e378d4SMichael Kelley flags = pgprot_encrypted(flags);
268188e378d4SMichael Kelley else
2682f4c9361fSIsaku Yamahata flags = pgprot_decrypted(flags);
268388e378d4SMichael Kelley }
2684f4c9361fSIsaku Yamahata
2685f4c9361fSIsaku Yamahata __set_fixmap(idx, phys, flags);
2686f4c9361fSIsaku Yamahata }
2687f4c9361fSIsaku Yamahata
io_apic_init_mappings(void)2688ca1b8862SThomas Gleixner void __init io_apic_init_mappings(void)
2689136d249eSJeremy Fitzhardinge {
2690f62bae50SIngo Molnar unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2691f62bae50SIngo Molnar struct resource *ioapic_res;
2692f62bae50SIngo Molnar int i;
2693f62bae50SIngo Molnar
2694f44d1692SJiang Liu ioapic_res = ioapic_setup_resources();
2695f44d1692SJiang Liu for_each_ioapic(i) {
2696f62bae50SIngo Molnar if (smp_found_config) {
2697d5371430SSuresh Siddha ioapic_phys = mpc_ioapic_addr(i);
2698f62bae50SIngo Molnar #ifdef CONFIG_X86_32
2699f62bae50SIngo Molnar if (!ioapic_phys) {
2700f62bae50SIngo Molnar printk(KERN_ERR
2701f62bae50SIngo Molnar "WARNING: bogus zero IO-APIC "
2702f62bae50SIngo Molnar "address found in MPTABLE, "
2703f62bae50SIngo Molnar "disabling IO/APIC support!\n");
2704f62bae50SIngo Molnar smp_found_config = 0;
2705ecf600f8SThomas Gleixner ioapic_is_disabled = true;
2706f62bae50SIngo Molnar goto fake_ioapic_page;
2707f62bae50SIngo Molnar }
2708f62bae50SIngo Molnar #endif
2709f62bae50SIngo Molnar } else {
2710f62bae50SIngo Molnar #ifdef CONFIG_X86_32
2711f62bae50SIngo Molnar fake_ioapic_page:
2712f62bae50SIngo Molnar #endif
271315c3c114SMike Rapoport ioapic_phys = (unsigned long)memblock_alloc(PAGE_SIZE,
271415c3c114SMike Rapoport PAGE_SIZE);
27158a7f97b9SMike Rapoport if (!ioapic_phys)
27168a7f97b9SMike Rapoport panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
27178a7f97b9SMike Rapoport __func__, PAGE_SIZE, PAGE_SIZE);
2718f62bae50SIngo Molnar ioapic_phys = __pa(ioapic_phys);
2719f62bae50SIngo Molnar }
2720f4c9361fSIsaku Yamahata io_apic_set_fixmap(idx, ioapic_phys);
2721e79c65a9SCyrill Gorcunov apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
2722e79c65a9SCyrill Gorcunov __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
2723e79c65a9SCyrill Gorcunov ioapic_phys);
2724f62bae50SIngo Molnar idx++;
2725f62bae50SIngo Molnar
2726f62bae50SIngo Molnar ioapic_res->start = ioapic_phys;
2727e79c65a9SCyrill Gorcunov ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
2728f62bae50SIngo Molnar ioapic_res++;
2729f62bae50SIngo Molnar }
2730f62bae50SIngo Molnar }
2731f62bae50SIngo Molnar
ioapic_insert_resources(void)2732857fdc53SYinghai Lu void __init ioapic_insert_resources(void)
2733f62bae50SIngo Molnar {
2734f62bae50SIngo Molnar int i;
2735f62bae50SIngo Molnar struct resource *r = ioapic_resources;
2736f62bae50SIngo Molnar
2737f62bae50SIngo Molnar if (!r) {
2738857fdc53SYinghai Lu if (nr_ioapics > 0)
2739f62bae50SIngo Molnar printk(KERN_ERR
274004c93ce4SBartlomiej Zolnierkiewicz "IO APIC resources couldn't be allocated.\n");
2741857fdc53SYinghai Lu return;
274204c93ce4SBartlomiej Zolnierkiewicz }
2743f62bae50SIngo Molnar
2744f44d1692SJiang Liu for_each_ioapic(i) {
2745f62bae50SIngo Molnar insert_resource(&iomem_resource, r);
2746f62bae50SIngo Molnar r++;
2747f62bae50SIngo Molnar }
2748f62bae50SIngo Molnar }
2749f62bae50SIngo Molnar
mp_find_ioapic(u32 gsi)2750eddb0c55SEric W. Biederman int mp_find_ioapic(u32 gsi)
27512a4ab640SFeng Tang {
2752f44d1692SJiang Liu int i;
27532a4ab640SFeng Tang
2754678301ecSPaul Bolle if (nr_ioapics == 0)
2755678301ecSPaul Bolle return -1;
2756678301ecSPaul Bolle
27572a4ab640SFeng Tang /* Find the IOAPIC that manages this GSI. */
2758f44d1692SJiang Liu for_each_ioapic(i) {
2759c040aaebSSuresh Siddha struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
2760f44d1692SJiang Liu if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end)
27612a4ab640SFeng Tang return i;
27622a4ab640SFeng Tang }
27632a4ab640SFeng Tang
27642a4ab640SFeng Tang printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
27652a4ab640SFeng Tang return -1;
27662a4ab640SFeng Tang }
27672a4ab640SFeng Tang
mp_find_ioapic_pin(int ioapic,u32 gsi)2768eddb0c55SEric W. Biederman int mp_find_ioapic_pin(int ioapic, u32 gsi)
27692a4ab640SFeng Tang {
2770c040aaebSSuresh Siddha struct mp_ioapic_gsi *gsi_cfg;
2771c040aaebSSuresh Siddha
2772f44d1692SJiang Liu if (WARN_ON(ioapic < 0))
27732a4ab640SFeng Tang return -1;
2774c040aaebSSuresh Siddha
2775c040aaebSSuresh Siddha gsi_cfg = mp_ioapic_gsi_routing(ioapic);
2776c040aaebSSuresh Siddha if (WARN_ON(gsi > gsi_cfg->gsi_end))
27772a4ab640SFeng Tang return -1;
27782a4ab640SFeng Tang
2779c040aaebSSuresh Siddha return gsi - gsi_cfg->gsi_base;
27802a4ab640SFeng Tang }
27812a4ab640SFeng Tang
bad_ioapic_register(int idx)278267dc5e70SJiang Liu static int bad_ioapic_register(int idx)
278373d63d03SSuresh Siddha {
278473d63d03SSuresh Siddha union IO_APIC_reg_00 reg_00;
278573d63d03SSuresh Siddha union IO_APIC_reg_01 reg_01;
278673d63d03SSuresh Siddha union IO_APIC_reg_02 reg_02;
278773d63d03SSuresh Siddha
278873d63d03SSuresh Siddha reg_00.raw = io_apic_read(idx, 0);
278973d63d03SSuresh Siddha reg_01.raw = io_apic_read(idx, 1);
279073d63d03SSuresh Siddha reg_02.raw = io_apic_read(idx, 2);
279173d63d03SSuresh Siddha
279273d63d03SSuresh Siddha if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
279373d63d03SSuresh Siddha pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
279473d63d03SSuresh Siddha mpc_ioapic_addr(idx));
279573d63d03SSuresh Siddha return 1;
279673d63d03SSuresh Siddha }
279773d63d03SSuresh Siddha
279873d63d03SSuresh Siddha return 0;
279973d63d03SSuresh Siddha }
280073d63d03SSuresh Siddha
find_free_ioapic_entry(void)280135ef9c94SJiang Liu static int find_free_ioapic_entry(void)
280235ef9c94SJiang Liu {
28037db298cbSJiang Liu int idx;
28047db298cbSJiang Liu
28057db298cbSJiang Liu for (idx = 0; idx < MAX_IO_APICS; idx++)
28067db298cbSJiang Liu if (ioapics[idx].nr_registers == 0)
28077db298cbSJiang Liu return idx;
28087db298cbSJiang Liu
28097db298cbSJiang Liu return MAX_IO_APICS;
281035ef9c94SJiang Liu }
281135ef9c94SJiang Liu
281235ef9c94SJiang Liu /**
281335ef9c94SJiang Liu * mp_register_ioapic - Register an IOAPIC device
281435ef9c94SJiang Liu * @id: hardware IOAPIC ID
281535ef9c94SJiang Liu * @address: physical address of IOAPIC register area
281635ef9c94SJiang Liu * @gsi_base: base of GSI associated with the IOAPIC
281735ef9c94SJiang Liu * @cfg: configuration information for the IOAPIC
281835ef9c94SJiang Liu */
mp_register_ioapic(int id,u32 address,u32 gsi_base,struct ioapic_domain_cfg * cfg)281935ef9c94SJiang Liu int mp_register_ioapic(int id, u32 address, u32 gsi_base,
282044767bfaSJiang Liu struct ioapic_domain_cfg *cfg)
28212a4ab640SFeng Tang {
28227db298cbSJiang Liu bool hotplug = !!ioapic_initialized;
2823c040aaebSSuresh Siddha struct mp_ioapic_gsi *gsi_cfg;
282435ef9c94SJiang Liu int idx, ioapic, entries;
282535ef9c94SJiang Liu u32 gsi_end;
28262a4ab640SFeng Tang
282735ef9c94SJiang Liu if (!address) {
282835ef9c94SJiang Liu pr_warn("Bogus (zero) I/O APIC address found, skipping!\n");
282935ef9c94SJiang Liu return -EINVAL;
283035ef9c94SJiang Liu }
283135ef9c94SJiang Liu for_each_ioapic(ioapic)
283235ef9c94SJiang Liu if (ioapics[ioapic].mp_config.apicaddr == address) {
283335ef9c94SJiang Liu pr_warn("address 0x%x conflicts with IOAPIC%d\n",
283435ef9c94SJiang Liu address, ioapic);
283535ef9c94SJiang Liu return -EEXIST;
283635ef9c94SJiang Liu }
28372a4ab640SFeng Tang
283835ef9c94SJiang Liu idx = find_free_ioapic_entry();
283935ef9c94SJiang Liu if (idx >= MAX_IO_APICS) {
284035ef9c94SJiang Liu pr_warn("Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
284135ef9c94SJiang Liu MAX_IO_APICS, idx);
284235ef9c94SJiang Liu return -ENOSPC;
284335ef9c94SJiang Liu }
28442a4ab640SFeng Tang
2845d5371430SSuresh Siddha ioapics[idx].mp_config.type = MP_IOAPIC;
2846d5371430SSuresh Siddha ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
2847d5371430SSuresh Siddha ioapics[idx].mp_config.apicaddr = address;
28482a4ab640SFeng Tang
2849f4c9361fSIsaku Yamahata io_apic_set_fixmap(FIX_IO_APIC_BASE_0 + idx, address);
285073d63d03SSuresh Siddha if (bad_ioapic_register(idx)) {
285173d63d03SSuresh Siddha clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
285235ef9c94SJiang Liu return -ENODEV;
285373d63d03SSuresh Siddha }
285473d63d03SSuresh Siddha
28555411dc4cSYinghai Lu ioapics[idx].mp_config.apicid = io_apic_unique_id(idx, id);
2856d5371430SSuresh Siddha ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
28572a4ab640SFeng Tang
28582a4ab640SFeng Tang /*
28592a4ab640SFeng Tang * Build basic GSI lookup table to facilitate gsi->io_apic lookups
28602a4ab640SFeng Tang * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
28612a4ab640SFeng Tang */
28627716a5c4SEric W. Biederman entries = io_apic_get_redir_entries(idx);
286335ef9c94SJiang Liu gsi_end = gsi_base + entries - 1;
286435ef9c94SJiang Liu for_each_ioapic(ioapic) {
286535ef9c94SJiang Liu gsi_cfg = mp_ioapic_gsi_routing(ioapic);
286635ef9c94SJiang Liu if ((gsi_base >= gsi_cfg->gsi_base &&
286735ef9c94SJiang Liu gsi_base <= gsi_cfg->gsi_end) ||
286835ef9c94SJiang Liu (gsi_end >= gsi_cfg->gsi_base &&
286935ef9c94SJiang Liu gsi_end <= gsi_cfg->gsi_end)) {
287035ef9c94SJiang Liu pr_warn("GSI range [%u-%u] for new IOAPIC conflicts with GSI[%u-%u]\n",
287135ef9c94SJiang Liu gsi_base, gsi_end,
287235ef9c94SJiang Liu gsi_cfg->gsi_base, gsi_cfg->gsi_end);
287335ef9c94SJiang Liu clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
287435ef9c94SJiang Liu return -ENOSPC;
287535ef9c94SJiang Liu }
287635ef9c94SJiang Liu }
2877c040aaebSSuresh Siddha gsi_cfg = mp_ioapic_gsi_routing(idx);
2878c040aaebSSuresh Siddha gsi_cfg->gsi_base = gsi_base;
287935ef9c94SJiang Liu gsi_cfg->gsi_end = gsi_end;
28807716a5c4SEric W. Biederman
288135ef9c94SJiang Liu ioapics[idx].irqdomain = NULL;
288235ef9c94SJiang Liu ioapics[idx].irqdomain_cfg = *cfg;
28832a4ab640SFeng Tang
28847db298cbSJiang Liu /*
28857db298cbSJiang Liu * If mp_register_ioapic() is called during early boot stage when
28864590d98fSAndy Shevchenko * walking ACPI/DT tables, it's too early to create irqdomain,
28877db298cbSJiang Liu * we are still using bootmem allocator. So delay it to setup_IO_APIC().
28887db298cbSJiang Liu */
28897db298cbSJiang Liu if (hotplug) {
28907db298cbSJiang Liu if (mp_irqdomain_create(idx)) {
28917db298cbSJiang Liu clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
28927db298cbSJiang Liu return -ENOMEM;
28937db298cbSJiang Liu }
28947db298cbSJiang Liu alloc_ioapic_saved_registers(idx);
28957db298cbSJiang Liu }
28967db298cbSJiang Liu
2897c040aaebSSuresh Siddha if (gsi_cfg->gsi_end >= gsi_top)
2898c040aaebSSuresh Siddha gsi_top = gsi_cfg->gsi_end + 1;
289935ef9c94SJiang Liu if (nr_ioapics <= idx)
290035ef9c94SJiang Liu nr_ioapics = idx + 1;
290135ef9c94SJiang Liu
290235ef9c94SJiang Liu /* Set nr_registers to mark entry present */
290335ef9c94SJiang Liu ioapics[idx].nr_registers = entries;
29042a4ab640SFeng Tang
290573d63d03SSuresh Siddha pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
290673d63d03SSuresh Siddha idx, mpc_ioapic_id(idx),
2907d5371430SSuresh Siddha mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
2908c040aaebSSuresh Siddha gsi_cfg->gsi_base, gsi_cfg->gsi_end);
29092a4ab640SFeng Tang
291035ef9c94SJiang Liu return 0;
29112a4ab640SFeng Tang }
291205ddafb1SJacob Pan
mp_unregister_ioapic(u32 gsi_base)291315516a3bSJiang Liu int mp_unregister_ioapic(u32 gsi_base)
291415516a3bSJiang Liu {
291515516a3bSJiang Liu int ioapic, pin;
291615516a3bSJiang Liu int found = 0;
291715516a3bSJiang Liu
291815516a3bSJiang Liu for_each_ioapic(ioapic)
291915516a3bSJiang Liu if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) {
292015516a3bSJiang Liu found = 1;
292115516a3bSJiang Liu break;
292215516a3bSJiang Liu }
292315516a3bSJiang Liu if (!found) {
292415516a3bSJiang Liu pr_warn("can't find IOAPIC for GSI %d\n", gsi_base);
292515516a3bSJiang Liu return -ENODEV;
292615516a3bSJiang Liu }
292715516a3bSJiang Liu
292815516a3bSJiang Liu for_each_pin(ioapic, pin) {
2929d32932d0SJiang Liu u32 gsi = mp_pin_to_gsi(ioapic, pin);
2930d32932d0SJiang Liu int irq = mp_map_gsi_to_irq(gsi, 0, NULL);
2931d32932d0SJiang Liu struct mp_chip_data *data;
2932d32932d0SJiang Liu
2933d32932d0SJiang Liu if (irq >= 0) {
2934d32932d0SJiang Liu data = irq_get_chip_data(irq);
2935d32932d0SJiang Liu if (data && data->count) {
293615516a3bSJiang Liu pr_warn("pin%d on IOAPIC%d is still in use.\n",
293715516a3bSJiang Liu pin, ioapic);
293815516a3bSJiang Liu return -EBUSY;
293915516a3bSJiang Liu }
294015516a3bSJiang Liu }
2941d32932d0SJiang Liu }
294215516a3bSJiang Liu
294315516a3bSJiang Liu /* Mark entry not present */
294415516a3bSJiang Liu ioapics[ioapic].nr_registers = 0;
294515516a3bSJiang Liu ioapic_destroy_irqdomain(ioapic);
294615516a3bSJiang Liu free_ioapic_saved_registers(ioapic);
294715516a3bSJiang Liu if (ioapics[ioapic].iomem_res)
294815516a3bSJiang Liu release_resource(ioapics[ioapic].iomem_res);
294915516a3bSJiang Liu clear_fixmap(FIX_IO_APIC_BASE_0 + ioapic);
295015516a3bSJiang Liu memset(&ioapics[ioapic], 0, sizeof(ioapics[ioapic]));
295115516a3bSJiang Liu
295215516a3bSJiang Liu return 0;
295315516a3bSJiang Liu }
295415516a3bSJiang Liu
mp_ioapic_registered(u32 gsi_base)2955e89900c9SJiang Liu int mp_ioapic_registered(u32 gsi_base)
2956e89900c9SJiang Liu {
2957e89900c9SJiang Liu int ioapic;
2958e89900c9SJiang Liu
2959e89900c9SJiang Liu for_each_ioapic(ioapic)
2960e89900c9SJiang Liu if (ioapics[ioapic].gsi_config.gsi_base == gsi_base)
2961e89900c9SJiang Liu return 1;
2962e89900c9SJiang Liu
2963e89900c9SJiang Liu return 0;
2964e89900c9SJiang Liu }
2965e89900c9SJiang Liu
mp_irqdomain_get_attr(u32 gsi,struct mp_chip_data * data,struct irq_alloc_info * info)296649c7e600SJiang Liu static void mp_irqdomain_get_attr(u32 gsi, struct mp_chip_data *data,
296749c7e600SJiang Liu struct irq_alloc_info *info)
296849c7e600SJiang Liu {
296933a65ba4SThomas Gleixner if (info && info->ioapic.valid) {
2970a27dca64SThomas Gleixner data->is_level = info->ioapic.is_level;
2971a27dca64SThomas Gleixner data->active_low = info->ioapic.active_low;
2972a27dca64SThomas Gleixner } else if (__acpi_get_override_irq(gsi, &data->is_level,
2973a27dca64SThomas Gleixner &data->active_low) < 0) {
2974335efdf5SThomas Gleixner /* PCI interrupts are always active low level triggered. */
2975a27dca64SThomas Gleixner data->is_level = true;
2976a27dca64SThomas Gleixner data->active_low = true;
297749c7e600SJiang Liu }
297849c7e600SJiang Liu }
297949c7e600SJiang Liu
29805d5a9713SDavid Woodhouse /*
29815d5a9713SDavid Woodhouse * Configure the I/O-APIC specific fields in the routing entry.
29825d5a9713SDavid Woodhouse *
29835d5a9713SDavid Woodhouse * This is important to setup the I/O-APIC specific bits (is_level,
29845d5a9713SDavid Woodhouse * active_low, masked) because the underlying parent domain will only
29855d5a9713SDavid Woodhouse * provide the routing information and is oblivious of the I/O-APIC
29865d5a9713SDavid Woodhouse * specific bits.
29875d5a9713SDavid Woodhouse *
29885d5a9713SDavid Woodhouse * The entry is just preconfigured at this point and not written into the
29895d5a9713SDavid Woodhouse * RTE. This happens later during activation which will fill in the actual
29905d5a9713SDavid Woodhouse * routing information.
29915d5a9713SDavid Woodhouse */
mp_preconfigure_entry(struct mp_chip_data * data)29925d5a9713SDavid Woodhouse static void mp_preconfigure_entry(struct mp_chip_data *data)
299349c7e600SJiang Liu {
29945d5a9713SDavid Woodhouse struct IO_APIC_route_entry *entry = &data->entry;
29955d5a9713SDavid Woodhouse
299649c7e600SJiang Liu memset(entry, 0, sizeof(*entry));
2997341b4a72SThomas Gleixner entry->is_level = data->is_level;
2998341b4a72SThomas Gleixner entry->active_low = data->active_low;
299949c7e600SJiang Liu /*
3000335efdf5SThomas Gleixner * Mask level triggered irqs. Edge triggered irqs are masked
3001335efdf5SThomas Gleixner * by the irq core code in case they fire.
300249c7e600SJiang Liu */
3003341b4a72SThomas Gleixner entry->masked = data->is_level;
300449c7e600SJiang Liu }
300549c7e600SJiang Liu
mp_irqdomain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)300649c7e600SJiang Liu int mp_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
300749c7e600SJiang Liu unsigned int nr_irqs, void *arg)
300849c7e600SJiang Liu {
300949c7e600SJiang Liu struct irq_alloc_info *info = arg;
30105d5a9713SDavid Woodhouse struct mp_chip_data *data;
30115d5a9713SDavid Woodhouse struct irq_data *irq_data;
30125d5a9713SDavid Woodhouse int ret, ioapic, pin;
3013c0ff971eSVitaly Kuznetsov unsigned long flags;
301449c7e600SJiang Liu
301549c7e600SJiang Liu if (!info || nr_irqs > 1)
301649c7e600SJiang Liu return -EINVAL;
301749c7e600SJiang Liu irq_data = irq_domain_get_irq_data(domain, virq);
301849c7e600SJiang Liu if (!irq_data)
301949c7e600SJiang Liu return -EINVAL;
302049c7e600SJiang Liu
302149c7e600SJiang Liu ioapic = mp_irqdomain_ioapic_idx(domain);
302233a65ba4SThomas Gleixner pin = info->ioapic.pin;
302349c7e600SJiang Liu if (irq_find_mapping(domain, (irq_hw_number_t)pin) > 0)
302449c7e600SJiang Liu return -EEXIST;
302549c7e600SJiang Liu
302649c7e600SJiang Liu data = kzalloc(sizeof(*data), GFP_KERNEL);
302749c7e600SJiang Liu if (!data)
302849c7e600SJiang Liu return -ENOMEM;
302949c7e600SJiang Liu
303049c7e600SJiang Liu ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, info);
3031*077e1b7cSThomas Gleixner if (ret < 0)
3032*077e1b7cSThomas Gleixner goto free_data;
303349c7e600SJiang Liu
30344467715aSJiang Liu INIT_LIST_HEAD(&data->irq_2_pin);
303533a65ba4SThomas Gleixner irq_data->hwirq = info->ioapic.pin;
3036d32932d0SJiang Liu irq_data->chip = (domain->parent == x86_vector_domain) ?
3037d32932d0SJiang Liu &ioapic_chip : &ioapic_ir_chip;
303849c7e600SJiang Liu irq_data->chip_data = data;
303949c7e600SJiang Liu mp_irqdomain_get_attr(mp_pin_to_gsi(ioapic, pin), data, info);
304049c7e600SJiang Liu
3041*077e1b7cSThomas Gleixner if (!add_pin_to_irq_node(data, ioapic_alloc_attr_node(info), ioapic, pin)) {
3042*077e1b7cSThomas Gleixner ret = -ENOMEM;
3043*077e1b7cSThomas Gleixner goto free_irqs;
3044*077e1b7cSThomas Gleixner }
3045c0ff971eSVitaly Kuznetsov
30465d5a9713SDavid Woodhouse mp_preconfigure_entry(data);
3047a27dca64SThomas Gleixner mp_register_handler(virq, data->is_level);
30485d5a9713SDavid Woodhouse
30495d5a9713SDavid Woodhouse local_irq_save(flags);
305049c7e600SJiang Liu if (virq < nr_legacy_irqs())
305149c7e600SJiang Liu legacy_pic->mask(virq);
3052c0ff971eSVitaly Kuznetsov local_irq_restore(flags);
305349c7e600SJiang Liu
305449c7e600SJiang Liu apic_printk(APIC_VERBOSE, KERN_DEBUG
30555d5a9713SDavid Woodhouse "IOAPIC[%d]: Preconfigured routing entry (%d-%d -> IRQ %d Level:%i ActiveLow:%i)\n",
30565d5a9713SDavid Woodhouse ioapic, mpc_ioapic_id(ioapic), pin, virq,
30575d5a9713SDavid Woodhouse data->is_level, data->active_low);
305849c7e600SJiang Liu return 0;
3059*077e1b7cSThomas Gleixner
3060*077e1b7cSThomas Gleixner free_irqs:
3061*077e1b7cSThomas Gleixner irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3062*077e1b7cSThomas Gleixner free_data:
3063*077e1b7cSThomas Gleixner kfree(data);
3064*077e1b7cSThomas Gleixner return ret;
306549c7e600SJiang Liu }
306649c7e600SJiang Liu
mp_irqdomain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)306749c7e600SJiang Liu void mp_irqdomain_free(struct irq_domain *domain, unsigned int virq,
306849c7e600SJiang Liu unsigned int nr_irqs)
306949c7e600SJiang Liu {
307049c7e600SJiang Liu struct irq_data *irq_data;
30714467715aSJiang Liu struct mp_chip_data *data;
307249c7e600SJiang Liu
307349c7e600SJiang Liu BUG_ON(nr_irqs != 1);
307449c7e600SJiang Liu irq_data = irq_domain_get_irq_data(domain, virq);
307549c7e600SJiang Liu if (irq_data && irq_data->chip_data) {
30764467715aSJiang Liu data = irq_data->chip_data;
30774467715aSJiang Liu __remove_pin_from_irq(data, mp_irqdomain_ioapic_idx(domain),
307849c7e600SJiang Liu (int)irq_data->hwirq);
30794467715aSJiang Liu WARN_ON(!list_empty(&data->irq_2_pin));
308049c7e600SJiang Liu kfree(irq_data->chip_data);
308149c7e600SJiang Liu }
308249c7e600SJiang Liu irq_domain_free_irqs_top(domain, virq, nr_irqs);
308349c7e600SJiang Liu }
308449c7e600SJiang Liu
mp_irqdomain_activate(struct irq_domain * domain,struct irq_data * irq_data,bool reserve)308572491643SThomas Gleixner int mp_irqdomain_activate(struct irq_domain *domain,
3086702cb0a0SThomas Gleixner struct irq_data *irq_data, bool reserve)
308749c7e600SJiang Liu {
308849c7e600SJiang Liu unsigned long flags;
308949c7e600SJiang Liu
309049c7e600SJiang Liu raw_spin_lock_irqsave(&ioapic_lock, flags);
309190ad9e2dSThomas Gleixner ioapic_configure_entry(irq_data);
309249c7e600SJiang Liu raw_spin_unlock_irqrestore(&ioapic_lock, flags);
309372491643SThomas Gleixner return 0;
309449c7e600SJiang Liu }
309549c7e600SJiang Liu
mp_irqdomain_deactivate(struct irq_domain * domain,struct irq_data * irq_data)309649c7e600SJiang Liu void mp_irqdomain_deactivate(struct irq_domain *domain,
309749c7e600SJiang Liu struct irq_data *irq_data)
309849c7e600SJiang Liu {
309949c7e600SJiang Liu /* It won't be called for IRQ with multiple IOAPIC pins associated */
310049c7e600SJiang Liu ioapic_mask_entry(mp_irqdomain_ioapic_idx(domain),
310149c7e600SJiang Liu (int)irq_data->hwirq);
310249c7e600SJiang Liu }
310349c7e600SJiang Liu
mp_irqdomain_ioapic_idx(struct irq_domain * domain)310449c7e600SJiang Liu int mp_irqdomain_ioapic_idx(struct irq_domain *domain)
310549c7e600SJiang Liu {
310649c7e600SJiang Liu return (int)(long)domain->host_data;
310749c7e600SJiang Liu }
3108f7a0c786SThomas Gleixner
3109f7a0c786SThomas Gleixner const struct irq_domain_ops mp_ioapic_irqdomain_ops = {
3110f7a0c786SThomas Gleixner .alloc = mp_irqdomain_alloc,
3111f7a0c786SThomas Gleixner .free = mp_irqdomain_free,
3112f7a0c786SThomas Gleixner .activate = mp_irqdomain_activate,
3113f7a0c786SThomas Gleixner .deactivate = mp_irqdomain_deactivate,
3114f7a0c786SThomas Gleixner };
3115