Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1c4248ca | 2 | #include <linux/cpumask.h> |
736baef4 JR |
3 | #include <linux/kernel.h> |
4 | #include <linux/string.h> | |
5 | #include <linux/errno.h> | |
98f1ad25 | 6 | #include <linux/msi.h> |
5afba62c JR |
7 | #include <linux/irq.h> |
8 | #include <linux/pci.h> | |
a62b32cd | 9 | #include <linux/irqdomain.h> |
98f1ad25 JR |
10 | |
11 | #include <asm/hw_irq.h> | |
12 | #include <asm/irq_remapping.h> | |
1c4248ca JR |
13 | #include <asm/processor.h> |
14 | #include <asm/x86_init.h> | |
15 | #include <asm/apic.h> | |
5fc24d8c | 16 | #include <asm/hpet.h> |
736baef4 | 17 | |
8a8f422d | 18 | #include "irq_remapping.h" |
736baef4 | 19 | |
95a02e97 | 20 | int irq_remapping_enabled; |
03bbcb2e | 21 | int irq_remap_broken; |
736baef4 JR |
22 | int disable_sourceid_checking; |
23 | int no_x2apic_optout; | |
24 | ||
b7d20631 | 25 | int disable_irq_post = 0; |
3d9b98f4 | 26 | |
be9be07b JP |
27 | bool enable_posted_msi __ro_after_init; |
28 | ||
7fa1c842 | 29 | static int disable_irq_remap; |
736baef4 JR |
30 | static struct irq_remap_ops *remap_ops; |
31 | ||
51b146c5 | 32 | static void irq_remapping_restore_boot_irq_mode(void) |
1c4248ca JR |
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 | */ | |
93984fbd | 41 | if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config()) |
1c4248ca JR |
42 | disconnect_bsp_APIC(0); |
43 | } | |
44 | ||
45 | static void __init irq_remapping_modify_x86_ops(void) | |
46 | { | |
51b146c5 | 47 | x86_apic_ops.restore = irq_remapping_restore_boot_irq_mode; |
1c4248ca JR |
48 | } |
49 | ||
736baef4 JR |
50 | static __init int setup_nointremap(char *str) |
51 | { | |
95a02e97 | 52 | disable_irq_remap = 1; |
736baef4 JR |
53 | return 0; |
54 | } | |
55 | early_param("nointremap", setup_nointremap); | |
56 | ||
95a02e97 | 57 | static __init int setup_irqremap(char *str) |
736baef4 JR |
58 | { |
59 | if (!str) | |
60 | return -EINVAL; | |
61 | ||
62 | while (*str) { | |
b7d20631 | 63 | if (!strncmp(str, "on", 2)) { |
95a02e97 | 64 | disable_irq_remap = 0; |
b7d20631 FW |
65 | disable_irq_post = 0; |
66 | } else if (!strncmp(str, "off", 3)) { | |
95a02e97 | 67 | disable_irq_remap = 1; |
b7d20631 FW |
68 | disable_irq_post = 1; |
69 | } else if (!strncmp(str, "nosid", 5)) | |
736baef4 JR |
70 | disable_sourceid_checking = 1; |
71 | else if (!strncmp(str, "no_x2apic_optout", 16)) | |
72 | no_x2apic_optout = 1; | |
b7d20631 FW |
73 | else if (!strncmp(str, "nopost", 6)) |
74 | disable_irq_post = 1; | |
be9be07b JP |
75 | else if (IS_ENABLED(CONFIG_X86_POSTED_MSI) && !strncmp(str, "posted_msi", 10)) |
76 | enable_posted_msi = true; | |
736baef4 JR |
77 | str += strcspn(str, ","); |
78 | while (*str == ',') | |
79 | str++; | |
80 | } | |
81 | ||
82 | return 0; | |
83 | } | |
95a02e97 | 84 | early_param("intremap", setup_irqremap); |
736baef4 | 85 | |
03bbcb2e NH |
86 | void set_irq_remapping_broken(void) |
87 | { | |
88 | irq_remap_broken = 1; | |
89 | } | |
90 | ||
959c870f FW |
91 | bool irq_remapping_cap(enum irq_remap_cap cap) |
92 | { | |
93 | if (!remap_ops || disable_irq_post) | |
30e93761 | 94 | return false; |
959c870f FW |
95 | |
96 | return (remap_ops->capability & (1 << cap)); | |
97 | } | |
98 | EXPORT_SYMBOL_GPL(irq_remapping_cap); | |
99 | ||
c392f56c | 100 | int __init irq_remapping_prepare(void) |
736baef4 | 101 | { |
95a02e97 | 102 | if (disable_irq_remap) |
c392f56c | 103 | return -ENOSYS; |
736baef4 | 104 | |
70bad345 BK |
105 | if (IS_ENABLED(CONFIG_INTEL_IOMMU) && |
106 | intel_irq_remap_ops.prepare() == 0) | |
30969e34 JL |
107 | remap_ops = &intel_irq_remap_ops; |
108 | else if (IS_ENABLED(CONFIG_AMD_IOMMU) && | |
109 | amd_iommu_irq_ops.prepare() == 0) | |
a1dafe85 | 110 | remap_ops = &amd_iommu_irq_ops; |
29217a47 LT |
111 | else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) && |
112 | hyperv_irq_remap_ops.prepare() == 0) | |
113 | remap_ops = &hyperv_irq_remap_ops; | |
30969e34 JL |
114 | else |
115 | return -ENOSYS; | |
116 | ||
117 | return 0; | |
736baef4 JR |
118 | } |
119 | ||
95a02e97 | 120 | int __init irq_remapping_enable(void) |
736baef4 | 121 | { |
1c4248ca JR |
122 | int ret; |
123 | ||
e9011760 | 124 | if (!remap_ops->enable) |
736baef4 JR |
125 | return -ENODEV; |
126 | ||
1c4248ca JR |
127 | ret = remap_ops->enable(); |
128 | ||
129 | if (irq_remapping_enabled) | |
130 | irq_remapping_modify_x86_ops(); | |
131 | ||
132 | return ret; | |
736baef4 | 133 | } |
4f3d8b67 | 134 | |
95a02e97 | 135 | void irq_remapping_disable(void) |
4f3d8b67 | 136 | { |
e9011760 JL |
137 | if (irq_remapping_enabled && remap_ops->disable) |
138 | remap_ops->disable(); | |
4f3d8b67 JR |
139 | } |
140 | ||
95a02e97 | 141 | int irq_remapping_reenable(int mode) |
4f3d8b67 | 142 | { |
e9011760 JL |
143 | if (irq_remapping_enabled && remap_ops->reenable) |
144 | return remap_ops->reenable(mode); | |
4f3d8b67 | 145 | |
e9011760 | 146 | return 0; |
4f3d8b67 JR |
147 | } |
148 | ||
95a02e97 | 149 | int __init irq_remap_enable_fault_handling(void) |
4f3d8b67 | 150 | { |
70733e0c JR |
151 | if (!irq_remapping_enabled) |
152 | return 0; | |
153 | ||
e9011760 | 154 | if (!remap_ops->enable_faulting) |
4f3d8b67 JR |
155 | return -ENODEV; |
156 | ||
d74169ce DS |
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()); | |
4f3d8b67 | 161 | } |
0c3f173a | 162 | |
6a9f5de2 JR |
163 | void panic_if_irq_remap(const char *msg) |
164 | { | |
165 | if (irq_remapping_enabled) | |
166 | panic(msg); | |
167 | } |