xref: /openbmc/linux/drivers/iommu/irq_remapping.c (revision 151f4e2b)
1 #include <linux/cpumask.h>
2 #include <linux/kernel.h>
3 #include <linux/string.h>
4 #include <linux/errno.h>
5 #include <linux/msi.h>
6 #include <linux/irq.h>
7 #include <linux/pci.h>
8 #include <linux/irqdomain.h>
9 
10 #include <asm/hw_irq.h>
11 #include <asm/irq_remapping.h>
12 #include <asm/processor.h>
13 #include <asm/x86_init.h>
14 #include <asm/apic.h>
15 #include <asm/hpet.h>
16 
17 #include "irq_remapping.h"
18 
19 int irq_remapping_enabled;
20 int irq_remap_broken;
21 int disable_sourceid_checking;
22 int no_x2apic_optout;
23 
24 int disable_irq_post = 0;
25 
26 static int disable_irq_remap;
27 static struct irq_remap_ops *remap_ops;
28 
29 static void irq_remapping_restore_boot_irq_mode(void)
30 {
31 	/*
32 	 * With interrupt-remapping, for now we will use virtual wire A
33 	 * mode, as virtual wire B is little complex (need to configure
34 	 * both IOAPIC RTE as well as interrupt-remapping table entry).
35 	 * As this gets called during crash dump, keep this simple for
36 	 * now.
37 	 */
38 	if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config())
39 		disconnect_bsp_APIC(0);
40 }
41 
42 static void __init irq_remapping_modify_x86_ops(void)
43 {
44 	x86_apic_ops.restore = irq_remapping_restore_boot_irq_mode;
45 }
46 
47 static __init int setup_nointremap(char *str)
48 {
49 	disable_irq_remap = 1;
50 	return 0;
51 }
52 early_param("nointremap", setup_nointremap);
53 
54 static __init int setup_irqremap(char *str)
55 {
56 	if (!str)
57 		return -EINVAL;
58 
59 	while (*str) {
60 		if (!strncmp(str, "on", 2)) {
61 			disable_irq_remap = 0;
62 			disable_irq_post = 0;
63 		} else if (!strncmp(str, "off", 3)) {
64 			disable_irq_remap = 1;
65 			disable_irq_post = 1;
66 		} else if (!strncmp(str, "nosid", 5))
67 			disable_sourceid_checking = 1;
68 		else if (!strncmp(str, "no_x2apic_optout", 16))
69 			no_x2apic_optout = 1;
70 		else if (!strncmp(str, "nopost", 6))
71 			disable_irq_post = 1;
72 
73 		str += strcspn(str, ",");
74 		while (*str == ',')
75 			str++;
76 	}
77 
78 	return 0;
79 }
80 early_param("intremap", setup_irqremap);
81 
82 void set_irq_remapping_broken(void)
83 {
84 	irq_remap_broken = 1;
85 }
86 
87 bool irq_remapping_cap(enum irq_remap_cap cap)
88 {
89 	if (!remap_ops || disable_irq_post)
90 		return false;
91 
92 	return (remap_ops->capability & (1 << cap));
93 }
94 EXPORT_SYMBOL_GPL(irq_remapping_cap);
95 
96 int __init irq_remapping_prepare(void)
97 {
98 	if (disable_irq_remap)
99 		return -ENOSYS;
100 
101 	if (intel_irq_remap_ops.prepare() == 0)
102 		remap_ops = &intel_irq_remap_ops;
103 	else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
104 		 amd_iommu_irq_ops.prepare() == 0)
105 		remap_ops = &amd_iommu_irq_ops;
106 	else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) &&
107 		 hyperv_irq_remap_ops.prepare() == 0)
108 		remap_ops = &hyperv_irq_remap_ops;
109 	else
110 		return -ENOSYS;
111 
112 	return 0;
113 }
114 
115 int __init irq_remapping_enable(void)
116 {
117 	int ret;
118 
119 	if (!remap_ops->enable)
120 		return -ENODEV;
121 
122 	ret = remap_ops->enable();
123 
124 	if (irq_remapping_enabled)
125 		irq_remapping_modify_x86_ops();
126 
127 	return ret;
128 }
129 
130 void irq_remapping_disable(void)
131 {
132 	if (irq_remapping_enabled && remap_ops->disable)
133 		remap_ops->disable();
134 }
135 
136 int irq_remapping_reenable(int mode)
137 {
138 	if (irq_remapping_enabled && remap_ops->reenable)
139 		return remap_ops->reenable(mode);
140 
141 	return 0;
142 }
143 
144 int __init irq_remap_enable_fault_handling(void)
145 {
146 	if (!irq_remapping_enabled)
147 		return 0;
148 
149 	if (!remap_ops->enable_faulting)
150 		return -ENODEV;
151 
152 	return remap_ops->enable_faulting();
153 }
154 
155 void panic_if_irq_remap(const char *msg)
156 {
157 	if (irq_remapping_enabled)
158 		panic(msg);
159 }
160 
161 /**
162  * irq_remapping_get_ir_irq_domain - Get the irqdomain associated with the IOMMU
163  *				     device serving request @info
164  * @info: interrupt allocation information, used to identify the IOMMU device
165  *
166  * It's used to get parent irqdomain for HPET and IOAPIC irqdomains.
167  * Returns pointer to IRQ domain, or NULL on failure.
168  */
169 struct irq_domain *
170 irq_remapping_get_ir_irq_domain(struct irq_alloc_info *info)
171 {
172 	if (!remap_ops || !remap_ops->get_ir_irq_domain)
173 		return NULL;
174 
175 	return remap_ops->get_ir_irq_domain(info);
176 }
177 
178 /**
179  * irq_remapping_get_irq_domain - Get the irqdomain serving the request @info
180  * @info: interrupt allocation information, used to identify the IOMMU device
181  *
182  * There will be one PCI MSI/MSIX irqdomain associated with each interrupt
183  * remapping device, so this interface is used to retrieve the PCI MSI/MSIX
184  * irqdomain serving request @info.
185  * Returns pointer to IRQ domain, or NULL on failure.
186  */
187 struct irq_domain *
188 irq_remapping_get_irq_domain(struct irq_alloc_info *info)
189 {
190 	if (!remap_ops || !remap_ops->get_irq_domain)
191 		return NULL;
192 
193 	return remap_ops->get_irq_domain(info);
194 }
195