OSDN Git Service

5bb1a04c91f5f1f9b8eec10a876b311587a4d6f7
[uclinux-h8/linux.git] / drivers / iommu / irq_remapping.c
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>
6 #include <linux/msi.h>
7 #include <linux/irq.h>
8 #include <linux/pci.h>
9 #include <linux/irqdomain.h>
10
11 #include <asm/hw_irq.h>
12 #include <asm/irq_remapping.h>
13 #include <asm/processor.h>
14 #include <asm/x86_init.h>
15 #include <asm/apic.h>
16 #include <asm/hpet.h>
17
18 #include "irq_remapping.h"
19
20 int irq_remapping_enabled;
21 int irq_remap_broken;
22 int disable_sourceid_checking;
23 int no_x2apic_optout;
24
25 static int disable_irq_remap;
26 static struct irq_remap_ops *remap_ops;
27
28 static int set_remapped_irq_affinity(struct irq_data *data,
29                                      const struct cpumask *mask,
30                                      bool force);
31
32 static bool irq_remapped(struct irq_cfg *cfg)
33 {
34         return (cfg->remapped == 1);
35 }
36
37 static void irq_remapping_disable_io_apic(void)
38 {
39         /*
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
44          * now.
45          */
46         if (cpu_has_apic || apic_from_smp_config())
47                 disconnect_bsp_APIC(0);
48 }
49
50 static void eoi_ioapic_pin_remapped(int apic, int pin, int vector)
51 {
52         /*
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.
57          */
58         io_apic_eoi(apic, pin);
59 }
60
61 static void __init irq_remapping_modify_x86_ops(void)
62 {
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;
66 }
67
68 static __init int setup_nointremap(char *str)
69 {
70         disable_irq_remap = 1;
71         return 0;
72 }
73 early_param("nointremap", setup_nointremap);
74
75 static __init int setup_irqremap(char *str)
76 {
77         if (!str)
78                 return -EINVAL;
79
80         while (*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))
88                         no_x2apic_optout = 1;
89
90                 str += strcspn(str, ",");
91                 while (*str == ',')
92                         str++;
93         }
94
95         return 0;
96 }
97 early_param("intremap", setup_irqremap);
98
99 void set_irq_remapping_broken(void)
100 {
101         irq_remap_broken = 1;
102 }
103
104 int __init irq_remapping_prepare(void)
105 {
106         if (disable_irq_remap)
107                 return -ENOSYS;
108
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;
114         else
115                 return -ENOSYS;
116
117         return 0;
118 }
119
120 int __init irq_remapping_enable(void)
121 {
122         int ret;
123
124         if (!remap_ops->enable)
125                 return -ENODEV;
126
127         ret = remap_ops->enable();
128
129         if (irq_remapping_enabled)
130                 irq_remapping_modify_x86_ops();
131
132         return ret;
133 }
134
135 void irq_remapping_disable(void)
136 {
137         if (irq_remapping_enabled && remap_ops->disable)
138                 remap_ops->disable();
139 }
140
141 int irq_remapping_reenable(int mode)
142 {
143         if (irq_remapping_enabled && remap_ops->reenable)
144                 return remap_ops->reenable(mode);
145
146         return 0;
147 }
148
149 int __init irq_remap_enable_fault_handling(void)
150 {
151         if (!irq_remapping_enabled)
152                 return 0;
153
154         if (!remap_ops->enable_faulting)
155                 return -ENODEV;
156
157         return remap_ops->enable_faulting();
158 }
159
160 static int set_remapped_irq_affinity(struct irq_data *data,
161                                      const struct cpumask *mask, bool force)
162 {
163         if (!config_enabled(CONFIG_SMP) || !remap_ops->set_affinity)
164                 return 0;
165
166         return remap_ops->set_affinity(data, mask, force);
167 }
168
169 void free_remapped_irq(int irq)
170 {
171         struct irq_cfg *cfg = irq_cfg(irq);
172
173         if (irq_remapped(cfg) && remap_ops->free_irq)
174                 remap_ops->free_irq(irq);
175 }
176
177 void panic_if_irq_remap(const char *msg)
178 {
179         if (irq_remapping_enabled)
180                 panic(msg);
181 }
182
183 void ir_ack_apic_edge(struct irq_data *data)
184 {
185         ack_APIC_irq();
186 }
187
188 static void ir_ack_apic_level(struct irq_data *data)
189 {
190         ack_APIC_irq();
191         eoi_ioapic_irq(data->irq, irqd_cfg(data));
192 }
193
194 static void ir_print_prefix(struct irq_data *data, struct seq_file *p)
195 {
196         seq_printf(p, " IR-%s", data->chip->name);
197 }
198
199 void irq_remap_modify_chip_defaults(struct irq_chip *chip)
200 {
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;
205 }
206
207 bool setup_remapped_irq(int irq, struct irq_cfg *cfg, struct irq_chip *chip)
208 {
209         if (!irq_remapped(cfg))
210                 return false;
211         irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
212         irq_remap_modify_chip_defaults(chip);
213         return true;
214 }
215
216 /**
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
220  *
221  * It's used to get parent irqdomain for HPET and IOAPIC irqdomains.
222  * Returns pointer to IRQ domain, or NULL on failure.
223  */
224 struct irq_domain *
225 irq_remapping_get_ir_irq_domain(struct irq_alloc_info *info)
226 {
227         if (!remap_ops || !remap_ops->get_ir_irq_domain)
228                 return NULL;
229
230         return remap_ops->get_ir_irq_domain(info);
231 }
232
233 /**
234  * irq_remapping_get_irq_domain - Get the irqdomain serving the request @info
235  * @info: interrupt allocation information, used to identify the IOMMU device
236  *
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.
241  */
242 struct irq_domain *
243 irq_remapping_get_irq_domain(struct irq_alloc_info *info)
244 {
245         if (!remap_ops || !remap_ops->get_irq_domain)
246                 return NULL;
247
248         return remap_ops->get_irq_domain(info);
249 }