| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 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 | int disable_irq_post = 0; |
| 26 | |
| 27 | bool enable_posted_msi __ro_after_init; |
| 28 | |
| 29 | static int disable_irq_remap; |
| 30 | static struct irq_remap_ops *remap_ops; |
| 31 | |
| 32 | static void irq_remapping_restore_boot_irq_mode(void) |
| 33 | { |
| 34 | /* |
| 35 | * With interrupt-remapping, for now we will use virtual wire A |
| 36 | * mode, as virtual wire B is little complex (need to configure |
| 37 | * both IOAPIC RTE as well as interrupt-remapping table entry). |
| 38 | * As this gets called during crash dump, keep this simple for |
| 39 | * now. |
| 40 | */ |
| 41 | if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config()) |
| 42 | disconnect_bsp_APIC(0); |
| 43 | } |
| 44 | |
| 45 | static void __init irq_remapping_modify_x86_ops(void) |
| 46 | { |
| 47 | x86_apic_ops.restore = irq_remapping_restore_boot_irq_mode; |
| 48 | } |
| 49 | |
| 50 | static __init int setup_nointremap(char *str) |
| 51 | { |
| 52 | disable_irq_remap = 1; |
| 53 | return 0; |
| 54 | } |
| 55 | early_param("nointremap", setup_nointremap); |
| 56 | |
| 57 | static __init int setup_irqremap(char *str) |
| 58 | { |
| 59 | if (!str) |
| 60 | return -EINVAL; |
| 61 | |
| 62 | while (*str) { |
| 63 | if (!strncmp(str, "on", 2)) { |
| 64 | disable_irq_remap = 0; |
| 65 | disable_irq_post = 0; |
| 66 | } else if (!strncmp(str, "off", 3)) { |
| 67 | disable_irq_remap = 1; |
| 68 | disable_irq_post = 1; |
| 69 | } else if (!strncmp(str, "nosid", 5)) |
| 70 | disable_sourceid_checking = 1; |
| 71 | else if (!strncmp(str, "no_x2apic_optout", 16)) |
| 72 | no_x2apic_optout = 1; |
| 73 | else if (!strncmp(str, "nopost", 6)) |
| 74 | disable_irq_post = 1; |
| 75 | else if (IS_ENABLED(CONFIG_X86_POSTED_MSI) && !strncmp(str, "posted_msi", 10)) |
| 76 | enable_posted_msi = true; |
| 77 | str += strcspn(str, ","); |
| 78 | while (*str == ',') |
| 79 | str++; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | early_param("intremap", setup_irqremap); |
| 85 | |
| 86 | void set_irq_remapping_broken(void) |
| 87 | { |
| 88 | irq_remap_broken = 1; |
| 89 | } |
| 90 | |
| 91 | bool irq_remapping_cap(enum irq_remap_cap cap) |
| 92 | { |
| 93 | if (!remap_ops || disable_irq_post) |
| 94 | return false; |
| 95 | |
| 96 | return (remap_ops->capability & (1 << cap)); |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(irq_remapping_cap); |
| 99 | |
| 100 | int __init irq_remapping_prepare(void) |
| 101 | { |
| 102 | if (disable_irq_remap) |
| 103 | return -ENOSYS; |
| 104 | |
| 105 | if (IS_ENABLED(CONFIG_INTEL_IOMMU) && |
| 106 | intel_irq_remap_ops.prepare() == 0) |
| 107 | remap_ops = &intel_irq_remap_ops; |
| 108 | else if (IS_ENABLED(CONFIG_AMD_IOMMU) && |
| 109 | amd_iommu_irq_ops.prepare() == 0) |
| 110 | remap_ops = &amd_iommu_irq_ops; |
| 111 | else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) && |
| 112 | hyperv_irq_remap_ops.prepare() == 0) |
| 113 | remap_ops = &hyperv_irq_remap_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 | cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "dmar:enable_fault_handling", |
| 158 | remap_ops->enable_faulting, NULL); |
| 159 | |
| 160 | return remap_ops->enable_faulting(smp_processor_id()); |
| 161 | } |
| 162 | |
| 163 | void panic_if_irq_remap(const char *msg) |
| 164 | { |
| 165 | if (irq_remapping_enabled) |
| 166 | panic(msg); |
| 167 | } |