1 #include <linux/seq_file.h>
2 #include <linux/cpumask.h>
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/errno.h>
9 #include <linux/irqdomain.h>
11 #include <asm/hw_irq.h>
12 #include <asm/irq_remapping.h>
13 #include <asm/processor.h>
14 #include <asm/x86_init.h>
18 #include "irq_remapping.h"
20 int irq_remapping_enabled;
22 int disable_sourceid_checking;
25 static int disable_irq_remap;
26 static struct irq_remap_ops *remap_ops;
28 static int set_remapped_irq_affinity(struct irq_data *data,
29 const struct cpumask *mask,
32 static bool irq_remapped(struct irq_cfg *cfg)
34 return (cfg->remapped == 1);
37 static void irq_remapping_disable_io_apic(void)
40 * With interrupt-remapping, for now we will use virtual wire A
41 * mode, as virtual wire B is little complex (need to configure
42 * both IOAPIC RTE as well as interrupt-remapping table entry).
43 * As this gets called during crash dump, keep this simple for
46 if (cpu_has_apic || apic_from_smp_config())
47 disconnect_bsp_APIC(0);
50 static void eoi_ioapic_pin_remapped(int apic, int pin, int vector)
53 * Intr-remapping uses pin number as the virtual vector
54 * in the RTE. Actual vector is programmed in
55 * intr-remapping table entry. Hence for the io-apic
56 * EOI we use the pin number.
58 io_apic_eoi(apic, pin);
61 static void __init irq_remapping_modify_x86_ops(void)
63 x86_io_apic_ops.disable = irq_remapping_disable_io_apic;
64 x86_io_apic_ops.set_affinity = set_remapped_irq_affinity;
65 x86_io_apic_ops.eoi_ioapic_pin = eoi_ioapic_pin_remapped;
68 static __init int setup_nointremap(char *str)
70 disable_irq_remap = 1;
73 early_param("nointremap", setup_nointremap);
75 static __init int setup_irqremap(char *str)
81 if (!strncmp(str, "on", 2))
82 disable_irq_remap = 0;
83 else if (!strncmp(str, "off", 3))
84 disable_irq_remap = 1;
85 else if (!strncmp(str, "nosid", 5))
86 disable_sourceid_checking = 1;
87 else if (!strncmp(str, "no_x2apic_optout", 16))
90 str += strcspn(str, ",");
97 early_param("intremap", setup_irqremap);
99 void set_irq_remapping_broken(void)
101 irq_remap_broken = 1;
104 int __init irq_remapping_prepare(void)
106 if (disable_irq_remap)
109 if (intel_irq_remap_ops.prepare() == 0)
110 remap_ops = &intel_irq_remap_ops;
111 else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
112 amd_iommu_irq_ops.prepare() == 0)
113 remap_ops = &amd_iommu_irq_ops;
120 int __init irq_remapping_enable(void)
124 if (!remap_ops->enable)
127 ret = remap_ops->enable();
129 if (irq_remapping_enabled)
130 irq_remapping_modify_x86_ops();
135 void irq_remapping_disable(void)
137 if (irq_remapping_enabled && remap_ops->disable)
138 remap_ops->disable();
141 int irq_remapping_reenable(int mode)
143 if (irq_remapping_enabled && remap_ops->reenable)
144 return remap_ops->reenable(mode);
149 int __init irq_remap_enable_fault_handling(void)
151 if (!irq_remapping_enabled)
154 if (!remap_ops->enable_faulting)
157 return remap_ops->enable_faulting();
160 static int set_remapped_irq_affinity(struct irq_data *data,
161 const struct cpumask *mask, bool force)
163 if (!config_enabled(CONFIG_SMP) || !remap_ops->set_affinity)
166 return remap_ops->set_affinity(data, mask, force);
169 void free_remapped_irq(int irq)
171 struct irq_cfg *cfg = irq_cfg(irq);
173 if (irq_remapped(cfg) && remap_ops->free_irq)
174 remap_ops->free_irq(irq);
177 void panic_if_irq_remap(const char *msg)
179 if (irq_remapping_enabled)
183 void ir_ack_apic_edge(struct irq_data *data)
188 static void ir_ack_apic_level(struct irq_data *data)
191 eoi_ioapic_irq(data->irq, irqd_cfg(data));
194 static void ir_print_prefix(struct irq_data *data, struct seq_file *p)
196 seq_printf(p, " IR-%s", data->chip->name);
199 void irq_remap_modify_chip_defaults(struct irq_chip *chip)
201 chip->irq_print_chip = ir_print_prefix;
202 chip->irq_ack = ir_ack_apic_edge;
203 chip->irq_eoi = ir_ack_apic_level;
204 chip->irq_set_affinity = x86_io_apic_ops.set_affinity;
207 bool setup_remapped_irq(int irq, struct irq_cfg *cfg, struct irq_chip *chip)
209 if (!irq_remapped(cfg))
211 irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
212 irq_remap_modify_chip_defaults(chip);
217 * irq_remapping_get_ir_irq_domain - Get the irqdomain associated with the IOMMU
218 * device serving request @info
219 * @info: interrupt allocation information, used to identify the IOMMU device
221 * It's used to get parent irqdomain for HPET and IOAPIC irqdomains.
222 * Returns pointer to IRQ domain, or NULL on failure.
225 irq_remapping_get_ir_irq_domain(struct irq_alloc_info *info)
227 if (!remap_ops || !remap_ops->get_ir_irq_domain)
230 return remap_ops->get_ir_irq_domain(info);
234 * irq_remapping_get_irq_domain - Get the irqdomain serving the request @info
235 * @info: interrupt allocation information, used to identify the IOMMU device
237 * There will be one PCI MSI/MSIX irqdomain associated with each interrupt
238 * remapping device, so this interface is used to retrieve the PCI MSI/MSIX
239 * irqdomain serving request @info.
240 * Returns pointer to IRQ domain, or NULL on failure.
243 irq_remapping_get_irq_domain(struct irq_alloc_info *info)
245 if (!remap_ops || !remap_ops->get_irq_domain)
248 return remap_ops->get_irq_domain(info);