1 #include <linux/cpumask.h>
2 #include <linux/kernel.h>
3 #include <linux/string.h>
4 #include <linux/cpumask.h>
5 #include <linux/errno.h>
10 #include <asm/hw_irq.h>
11 #include <asm/irq_remapping.h>
12 #include <asm/processor.h>
13 #include <asm/x86_init.h>
16 #include "irq_remapping.h"
18 int irq_remapping_enabled;
20 int disable_irq_remap;
21 int disable_sourceid_checking;
24 static struct irq_remap_ops *remap_ops;
26 static int msi_alloc_remapped_irq(struct pci_dev *pdev, int irq, int nvec);
27 static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
28 int index, int sub_handle);
30 static void irq_remapping_disable_io_apic(void)
33 * With interrupt-remapping, for now we will use virtual wire A
34 * mode, as virtual wire B is little complex (need to configure
35 * both IOAPIC RTE as well as interrupt-remapping table entry).
36 * As this gets called during crash dump, keep this simple for
39 if (cpu_has_apic || apic_from_smp_config())
40 disconnect_bsp_APIC(0);
43 static int do_setup_msi_irqs(struct pci_dev *dev, int nvec)
45 int node, ret, sub_handle, index = 0;
47 struct msi_desc *msidesc;
49 nvec = __roundup_pow_of_two(nvec);
51 WARN_ON(!list_is_singular(&dev->msi_list));
52 msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
53 WARN_ON(msidesc->irq);
54 WARN_ON(msidesc->msi_attrib.multiple);
56 node = dev_to_node(&dev->dev);
57 irq = __create_irqs(get_nr_irqs_gsi(), nvec, node);
61 msidesc->msi_attrib.multiple = ilog2(nvec);
62 for (sub_handle = 0; sub_handle < nvec; sub_handle++) {
64 index = msi_alloc_remapped_irq(dev, irq, nvec);
70 ret = msi_setup_remapped_irq(dev, irq + sub_handle,
75 ret = setup_msi_irq(dev, msidesc, irq, sub_handle);
82 destroy_irqs(irq, nvec);
85 * Restore altered MSI descriptor fields and prevent just destroyed
86 * IRQs from tearing down again in default_teardown_msi_irqs()
89 msidesc->msi_attrib.multiple = 0;
94 static int do_setup_msix_irqs(struct pci_dev *dev, int nvec)
96 int node, ret, sub_handle, index = 0;
97 struct msi_desc *msidesc;
100 node = dev_to_node(&dev->dev);
101 irq = get_nr_irqs_gsi();
104 list_for_each_entry(msidesc, &dev->msi_list, list) {
106 irq = create_irq_nr(irq, node);
111 ret = index = msi_alloc_remapped_irq(dev, irq, nvec);
113 ret = msi_setup_remapped_irq(dev, irq, index, sub_handle);
118 ret = setup_msi_irq(dev, msidesc, irq, 0);
133 static int irq_remapping_setup_msi_irqs(struct pci_dev *dev,
136 if (type == PCI_CAP_ID_MSI)
137 return do_setup_msi_irqs(dev, nvec);
139 return do_setup_msix_irqs(dev, nvec);
142 static void __init irq_remapping_modify_x86_ops(void)
144 x86_io_apic_ops.disable = irq_remapping_disable_io_apic;
145 x86_msi.setup_msi_irqs = irq_remapping_setup_msi_irqs;
146 x86_msi.setup_hpet_msi = setup_hpet_msi_remapped;
149 static __init int setup_nointremap(char *str)
151 disable_irq_remap = 1;
154 early_param("nointremap", setup_nointremap);
156 static __init int setup_irqremap(char *str)
162 if (!strncmp(str, "on", 2))
163 disable_irq_remap = 0;
164 else if (!strncmp(str, "off", 3))
165 disable_irq_remap = 1;
166 else if (!strncmp(str, "nosid", 5))
167 disable_sourceid_checking = 1;
168 else if (!strncmp(str, "no_x2apic_optout", 16))
169 no_x2apic_optout = 1;
171 str += strcspn(str, ",");
178 early_param("intremap", setup_irqremap);
180 void __init setup_irq_remapping_ops(void)
182 remap_ops = &intel_irq_remap_ops;
184 #ifdef CONFIG_AMD_IOMMU
185 if (amd_iommu_irq_ops.prepare() == 0)
186 remap_ops = &amd_iommu_irq_ops;
190 int irq_remapping_supported(void)
192 if (disable_irq_remap)
195 if (!remap_ops || !remap_ops->supported)
198 return remap_ops->supported();
201 int __init irq_remapping_prepare(void)
203 if (!remap_ops || !remap_ops->prepare)
206 return remap_ops->prepare();
209 int __init irq_remapping_enable(void)
213 if (!remap_ops || !remap_ops->enable)
216 ret = remap_ops->enable();
218 if (irq_remapping_enabled)
219 irq_remapping_modify_x86_ops();
224 void irq_remapping_disable(void)
226 if (!irq_remapping_enabled ||
231 remap_ops->disable();
234 int irq_remapping_reenable(int mode)
236 if (!irq_remapping_enabled ||
238 !remap_ops->reenable)
241 return remap_ops->reenable(mode);
244 int __init irq_remap_enable_fault_handling(void)
246 if (!irq_remapping_enabled)
249 if (!remap_ops || !remap_ops->enable_faulting)
252 return remap_ops->enable_faulting();
255 int setup_ioapic_remapped_entry(int irq,
256 struct IO_APIC_route_entry *entry,
257 unsigned int destination, int vector,
258 struct io_apic_irq_attr *attr)
260 if (!remap_ops || !remap_ops->setup_ioapic_entry)
263 return remap_ops->setup_ioapic_entry(irq, entry, destination,
267 int set_remapped_irq_affinity(struct irq_data *data, const struct cpumask *mask,
270 if (!config_enabled(CONFIG_SMP) || !remap_ops ||
271 !remap_ops->set_affinity)
274 return remap_ops->set_affinity(data, mask, force);
277 void free_remapped_irq(int irq)
279 if (!remap_ops || !remap_ops->free_irq)
282 remap_ops->free_irq(irq);
285 void compose_remapped_msi_msg(struct pci_dev *pdev,
286 unsigned int irq, unsigned int dest,
287 struct msi_msg *msg, u8 hpet_id)
289 if (!remap_ops || !remap_ops->compose_msi_msg)
292 remap_ops->compose_msi_msg(pdev, irq, dest, msg, hpet_id);
295 static int msi_alloc_remapped_irq(struct pci_dev *pdev, int irq, int nvec)
297 if (!remap_ops || !remap_ops->msi_alloc_irq)
300 return remap_ops->msi_alloc_irq(pdev, irq, nvec);
303 static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
304 int index, int sub_handle)
306 if (!remap_ops || !remap_ops->msi_setup_irq)
309 return remap_ops->msi_setup_irq(pdev, irq, index, sub_handle);
312 int setup_hpet_msi_remapped(unsigned int irq, unsigned int id)
314 if (!remap_ops || !remap_ops->setup_hpet_msi)
317 return remap_ops->setup_hpet_msi(irq, id);