Joerg Roedel | 1c4248c | 2012-09-26 12:44:35 +0200 | [diff] [blame] | 1 | #include <linux/cpumask.h> |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/string.h> |
| 4 | #include <linux/errno.h> |
Joerg Roedel | 98f1ad2 | 2012-07-06 13:28:37 +0200 | [diff] [blame] | 5 | #include <linux/msi.h> |
Joerg Roedel | 5afba62 | 2012-09-26 12:44:38 +0200 | [diff] [blame] | 6 | #include <linux/irq.h> |
| 7 | #include <linux/pci.h> |
Jiang Liu | a62b32c | 2015-04-13 14:11:29 +0800 | [diff] [blame] | 8 | #include <linux/irqdomain.h> |
Joerg Roedel | 98f1ad2 | 2012-07-06 13:28:37 +0200 | [diff] [blame] | 9 | |
| 10 | #include <asm/hw_irq.h> |
| 11 | #include <asm/irq_remapping.h> |
Joerg Roedel | 1c4248c | 2012-09-26 12:44:35 +0200 | [diff] [blame] | 12 | #include <asm/processor.h> |
| 13 | #include <asm/x86_init.h> |
| 14 | #include <asm/apic.h> |
Yijing Wang | 5fc24d8 | 2014-09-17 17:32:19 +0800 | [diff] [blame] | 15 | #include <asm/hpet.h> |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 16 | |
Suresh Siddha | 8a8f422 | 2012-03-30 11:47:08 -0700 | [diff] [blame] | 17 | #include "irq_remapping.h" |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 18 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 19 | int irq_remapping_enabled; |
Neil Horman | 03bbcb2 | 2013-04-16 16:38:32 -0400 | [diff] [blame] | 20 | int irq_remap_broken; |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 21 | int disable_sourceid_checking; |
| 22 | int no_x2apic_optout; |
| 23 | |
Feng Wu | b7d2063 | 2015-09-18 22:29:56 +0800 | [diff] [blame] | 24 | int disable_irq_post = 0; |
Feng Wu | 3d9b98f | 2015-06-09 13:20:35 +0800 | [diff] [blame] | 25 | |
Jiang Liu | 7fa1c84 | 2015-01-07 15:31:42 +0800 | [diff] [blame] | 26 | static int disable_irq_remap; |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 27 | static struct irq_remap_ops *remap_ops; |
| 28 | |
Baoquan He | 51b146c | 2018-02-14 13:46:55 +0800 | [diff] [blame] | 29 | static void irq_remapping_restore_boot_irq_mode(void) |
Joerg Roedel | 1c4248c | 2012-09-26 12:44:35 +0200 | [diff] [blame] | 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 | */ |
Borislav Petkov | 93984fb | 2016-04-04 22:25:00 +0200 | [diff] [blame] | 38 | if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config()) |
Joerg Roedel | 1c4248c | 2012-09-26 12:44:35 +0200 | [diff] [blame] | 39 | disconnect_bsp_APIC(0); |
| 40 | } |
| 41 | |
| 42 | static void __init irq_remapping_modify_x86_ops(void) |
| 43 | { |
Baoquan He | 51b146c | 2018-02-14 13:46:55 +0800 | [diff] [blame] | 44 | x86_apic_ops.restore = irq_remapping_restore_boot_irq_mode; |
Joerg Roedel | 1c4248c | 2012-09-26 12:44:35 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 47 | static __init int setup_nointremap(char *str) |
| 48 | { |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 49 | disable_irq_remap = 1; |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | early_param("nointremap", setup_nointremap); |
| 53 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 54 | static __init int setup_irqremap(char *str) |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 55 | { |
| 56 | if (!str) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | while (*str) { |
Feng Wu | b7d2063 | 2015-09-18 22:29:56 +0800 | [diff] [blame] | 60 | if (!strncmp(str, "on", 2)) { |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 61 | disable_irq_remap = 0; |
Feng Wu | b7d2063 | 2015-09-18 22:29:56 +0800 | [diff] [blame] | 62 | disable_irq_post = 0; |
| 63 | } else if (!strncmp(str, "off", 3)) { |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 64 | disable_irq_remap = 1; |
Feng Wu | b7d2063 | 2015-09-18 22:29:56 +0800 | [diff] [blame] | 65 | disable_irq_post = 1; |
| 66 | } else if (!strncmp(str, "nosid", 5)) |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 67 | disable_sourceid_checking = 1; |
| 68 | else if (!strncmp(str, "no_x2apic_optout", 16)) |
| 69 | no_x2apic_optout = 1; |
Feng Wu | b7d2063 | 2015-09-18 22:29:56 +0800 | [diff] [blame] | 70 | else if (!strncmp(str, "nopost", 6)) |
| 71 | disable_irq_post = 1; |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 72 | |
| 73 | str += strcspn(str, ","); |
| 74 | while (*str == ',') |
| 75 | str++; |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 80 | early_param("intremap", setup_irqremap); |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 81 | |
Neil Horman | 03bbcb2 | 2013-04-16 16:38:32 -0400 | [diff] [blame] | 82 | void set_irq_remapping_broken(void) |
| 83 | { |
| 84 | irq_remap_broken = 1; |
| 85 | } |
| 86 | |
Feng Wu | 959c870 | 2015-06-09 13:20:36 +0800 | [diff] [blame] | 87 | bool irq_remapping_cap(enum irq_remap_cap cap) |
| 88 | { |
| 89 | if (!remap_ops || disable_irq_post) |
Joerg Roedel | 30e9376 | 2015-08-13 11:13:17 +0200 | [diff] [blame] | 90 | return false; |
Feng Wu | 959c870 | 2015-06-09 13:20:36 +0800 | [diff] [blame] | 91 | |
| 92 | return (remap_ops->capability & (1 << cap)); |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(irq_remapping_cap); |
| 95 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 96 | int __init irq_remapping_prepare(void) |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 97 | { |
Jiang Liu | c392f56 | 2015-01-07 15:31:40 +0800 | [diff] [blame] | 98 | if (disable_irq_remap) |
| 99 | return -ENOSYS; |
| 100 | |
Jiang Liu | 30969e3 | 2015-01-07 15:31:41 +0800 | [diff] [blame] | 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) |
Thomas Gleixner | a1dafe8 | 2015-01-07 15:31:28 +0800 | [diff] [blame] | 105 | remap_ops = &amd_iommu_irq_ops; |
Jiang Liu | 30969e3 | 2015-01-07 15:31:41 +0800 | [diff] [blame] | 106 | else |
| 107 | return -ENOSYS; |
| 108 | |
| 109 | return 0; |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 112 | int __init irq_remapping_enable(void) |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 113 | { |
Joerg Roedel | 1c4248c | 2012-09-26 12:44:35 +0200 | [diff] [blame] | 114 | int ret; |
| 115 | |
Jiang Liu | e901176 | 2015-01-07 15:31:43 +0800 | [diff] [blame] | 116 | if (!remap_ops->enable) |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 117 | return -ENODEV; |
| 118 | |
Joerg Roedel | 1c4248c | 2012-09-26 12:44:35 +0200 | [diff] [blame] | 119 | ret = remap_ops->enable(); |
| 120 | |
| 121 | if (irq_remapping_enabled) |
| 122 | irq_remapping_modify_x86_ops(); |
| 123 | |
| 124 | return ret; |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 125 | } |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 126 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 127 | void irq_remapping_disable(void) |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 128 | { |
Jiang Liu | e901176 | 2015-01-07 15:31:43 +0800 | [diff] [blame] | 129 | if (irq_remapping_enabled && remap_ops->disable) |
| 130 | remap_ops->disable(); |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 133 | int irq_remapping_reenable(int mode) |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 134 | { |
Jiang Liu | e901176 | 2015-01-07 15:31:43 +0800 | [diff] [blame] | 135 | if (irq_remapping_enabled && remap_ops->reenable) |
| 136 | return remap_ops->reenable(mode); |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 137 | |
Jiang Liu | e901176 | 2015-01-07 15:31:43 +0800 | [diff] [blame] | 138 | return 0; |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 141 | int __init irq_remap_enable_fault_handling(void) |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 142 | { |
Joerg Roedel | 70733e0 | 2012-09-26 12:44:33 +0200 | [diff] [blame] | 143 | if (!irq_remapping_enabled) |
| 144 | return 0; |
| 145 | |
Jiang Liu | e901176 | 2015-01-07 15:31:43 +0800 | [diff] [blame] | 146 | if (!remap_ops->enable_faulting) |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 147 | return -ENODEV; |
| 148 | |
| 149 | return remap_ops->enable_faulting(); |
| 150 | } |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 151 | |
Joerg Roedel | 6a9f5de | 2012-09-26 12:44:41 +0200 | [diff] [blame] | 152 | void panic_if_irq_remap(const char *msg) |
| 153 | { |
| 154 | if (irq_remapping_enabled) |
| 155 | panic(msg); |
| 156 | } |
Joerg Roedel | 9b1b0e4 | 2012-09-26 12:44:45 +0200 | [diff] [blame] | 157 | |
Jiang Liu | 947045a | 2015-04-13 14:11:30 +0800 | [diff] [blame] | 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 | } |