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 107 return -ENOSYS; 108 109 return 0; 110 } 111 112 int __init irq_remapping_enable(void) 113 { 114 int ret; 115 116 if (!remap_ops->enable) 117 return -ENODEV; 118 119 ret = remap_ops->enable(); 120 121 if (irq_remapping_enabled) 122 irq_remapping_modify_x86_ops(); 123 124 return ret; 125 } 126 127 void irq_remapping_disable(void) 128 { 129 if (irq_remapping_enabled && remap_ops->disable) 130 remap_ops->disable(); 131 } 132 133 int irq_remapping_reenable(int mode) 134 { 135 if (irq_remapping_enabled && remap_ops->reenable) 136 return remap_ops->reenable(mode); 137 138 return 0; 139 } 140 141 int __init irq_remap_enable_fault_handling(void) 142 { 143 if (!irq_remapping_enabled) 144 return 0; 145 146 if (!remap_ops->enable_faulting) 147 return -ENODEV; 148 149 return remap_ops->enable_faulting(); 150 } 151 152 void panic_if_irq_remap(const char *msg) 153 { 154 if (irq_remapping_enabled) 155 panic(msg); 156 } 157 158 /** 159 * irq_remapping_get_ir_irq_domain - Get the irqdomain associated with the IOMMU 160 * device serving request @info 161 * @info: interrupt allocation information, used to identify the IOMMU device 162 * 163 * It's used to get parent irqdomain for HPET and IOAPIC irqdomains. 164 * Returns pointer to IRQ domain, or NULL on failure. 165 */ 166 struct irq_domain * 167 irq_remapping_get_ir_irq_domain(struct irq_alloc_info *info) 168 { 169 if (!remap_ops || !remap_ops->get_ir_irq_domain) 170 return NULL; 171 172 return remap_ops->get_ir_irq_domain(info); 173 } 174 175 /** 176 * irq_remapping_get_irq_domain - Get the irqdomain serving the request @info 177 * @info: interrupt allocation information, used to identify the IOMMU device 178 * 179 * There will be one PCI MSI/MSIX irqdomain associated with each interrupt 180 * remapping device, so this interface is used to retrieve the PCI MSI/MSIX 181 * irqdomain serving request @info. 182 * Returns pointer to IRQ domain, or NULL on failure. 183 */ 184 struct irq_domain * 185 irq_remapping_get_irq_domain(struct irq_alloc_info *info) 186 { 187 if (!remap_ops || !remap_ops->get_irq_domain) 188 return NULL; 189 190 return remap_ops->get_irq_domain(info); 191 } 192