blob: b94ebd42edd851efd1bfce4e8c3c3a025e1a5a92 [file] [log] [blame]
Joerg Roedel1c4248c2012-09-26 12:44:35 +02001#include <linux/cpumask.h>
Joerg Roedel736baef2012-03-30 11:47:00 -07002#include <linux/kernel.h>
3#include <linux/string.h>
4#include <linux/errno.h>
Joerg Roedel98f1ad22012-07-06 13:28:37 +02005#include <linux/msi.h>
Joerg Roedel5afba622012-09-26 12:44:38 +02006#include <linux/irq.h>
7#include <linux/pci.h>
Jiang Liua62b32c2015-04-13 14:11:29 +08008#include <linux/irqdomain.h>
Joerg Roedel98f1ad22012-07-06 13:28:37 +02009
10#include <asm/hw_irq.h>
11#include <asm/irq_remapping.h>
Joerg Roedel1c4248c2012-09-26 12:44:35 +020012#include <asm/processor.h>
13#include <asm/x86_init.h>
14#include <asm/apic.h>
Yijing Wang5fc24d82014-09-17 17:32:19 +080015#include <asm/hpet.h>
Joerg Roedel736baef2012-03-30 11:47:00 -070016
Suresh Siddha8a8f4222012-03-30 11:47:08 -070017#include "irq_remapping.h"
Joerg Roedel736baef2012-03-30 11:47:00 -070018
Suresh Siddha95a02e92012-03-30 11:47:07 -070019int irq_remapping_enabled;
Neil Horman03bbcb22013-04-16 16:38:32 -040020int irq_remap_broken;
Joerg Roedel736baef2012-03-30 11:47:00 -070021int disable_sourceid_checking;
22int no_x2apic_optout;
23
Feng Wub7d20632015-09-18 22:29:56 +080024int disable_irq_post = 0;
Feng Wu3d9b98f2015-06-09 13:20:35 +080025
Jiang Liu7fa1c842015-01-07 15:31:42 +080026static int disable_irq_remap;
Joerg Roedel736baef2012-03-30 11:47:00 -070027static struct irq_remap_ops *remap_ops;
28
Baoquan He51b146c2018-02-14 13:46:55 +080029static void irq_remapping_restore_boot_irq_mode(void)
Joerg Roedel1c4248c2012-09-26 12:44:35 +020030{
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 Petkov93984fb2016-04-04 22:25:00 +020038 if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config())
Joerg Roedel1c4248c2012-09-26 12:44:35 +020039 disconnect_bsp_APIC(0);
40}
41
42static void __init irq_remapping_modify_x86_ops(void)
43{
Baoquan He51b146c2018-02-14 13:46:55 +080044 x86_apic_ops.restore = irq_remapping_restore_boot_irq_mode;
Joerg Roedel1c4248c2012-09-26 12:44:35 +020045}
46
Joerg Roedel736baef2012-03-30 11:47:00 -070047static __init int setup_nointremap(char *str)
48{
Suresh Siddha95a02e92012-03-30 11:47:07 -070049 disable_irq_remap = 1;
Joerg Roedel736baef2012-03-30 11:47:00 -070050 return 0;
51}
52early_param("nointremap", setup_nointremap);
53
Suresh Siddha95a02e92012-03-30 11:47:07 -070054static __init int setup_irqremap(char *str)
Joerg Roedel736baef2012-03-30 11:47:00 -070055{
56 if (!str)
57 return -EINVAL;
58
59 while (*str) {
Feng Wub7d20632015-09-18 22:29:56 +080060 if (!strncmp(str, "on", 2)) {
Suresh Siddha95a02e92012-03-30 11:47:07 -070061 disable_irq_remap = 0;
Feng Wub7d20632015-09-18 22:29:56 +080062 disable_irq_post = 0;
63 } else if (!strncmp(str, "off", 3)) {
Suresh Siddha95a02e92012-03-30 11:47:07 -070064 disable_irq_remap = 1;
Feng Wub7d20632015-09-18 22:29:56 +080065 disable_irq_post = 1;
66 } else if (!strncmp(str, "nosid", 5))
Joerg Roedel736baef2012-03-30 11:47:00 -070067 disable_sourceid_checking = 1;
68 else if (!strncmp(str, "no_x2apic_optout", 16))
69 no_x2apic_optout = 1;
Feng Wub7d20632015-09-18 22:29:56 +080070 else if (!strncmp(str, "nopost", 6))
71 disable_irq_post = 1;
Joerg Roedel736baef2012-03-30 11:47:00 -070072
73 str += strcspn(str, ",");
74 while (*str == ',')
75 str++;
76 }
77
78 return 0;
79}
Suresh Siddha95a02e92012-03-30 11:47:07 -070080early_param("intremap", setup_irqremap);
Joerg Roedel736baef2012-03-30 11:47:00 -070081
Neil Horman03bbcb22013-04-16 16:38:32 -040082void set_irq_remapping_broken(void)
83{
84 irq_remap_broken = 1;
85}
86
Feng Wu959c8702015-06-09 13:20:36 +080087bool irq_remapping_cap(enum irq_remap_cap cap)
88{
89 if (!remap_ops || disable_irq_post)
Joerg Roedel30e93762015-08-13 11:13:17 +020090 return false;
Feng Wu959c8702015-06-09 13:20:36 +080091
92 return (remap_ops->capability & (1 << cap));
93}
94EXPORT_SYMBOL_GPL(irq_remapping_cap);
95
Suresh Siddha95a02e92012-03-30 11:47:07 -070096int __init irq_remapping_prepare(void)
Joerg Roedel736baef2012-03-30 11:47:00 -070097{
Jiang Liuc392f562015-01-07 15:31:40 +080098 if (disable_irq_remap)
99 return -ENOSYS;
100
Jiang Liu30969e32015-01-07 15:31:41 +0800101 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 Gleixnera1dafe82015-01-07 15:31:28 +0800105 remap_ops = &amd_iommu_irq_ops;
Jiang Liu30969e32015-01-07 15:31:41 +0800106 else
107 return -ENOSYS;
108
109 return 0;
Joerg Roedel736baef2012-03-30 11:47:00 -0700110}
111
Suresh Siddha95a02e92012-03-30 11:47:07 -0700112int __init irq_remapping_enable(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700113{
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200114 int ret;
115
Jiang Liue9011762015-01-07 15:31:43 +0800116 if (!remap_ops->enable)
Joerg Roedel736baef2012-03-30 11:47:00 -0700117 return -ENODEV;
118
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200119 ret = remap_ops->enable();
120
121 if (irq_remapping_enabled)
122 irq_remapping_modify_x86_ops();
123
124 return ret;
Joerg Roedel736baef2012-03-30 11:47:00 -0700125}
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700126
Suresh Siddha95a02e92012-03-30 11:47:07 -0700127void irq_remapping_disable(void)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700128{
Jiang Liue9011762015-01-07 15:31:43 +0800129 if (irq_remapping_enabled && remap_ops->disable)
130 remap_ops->disable();
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700131}
132
Suresh Siddha95a02e92012-03-30 11:47:07 -0700133int irq_remapping_reenable(int mode)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700134{
Jiang Liue9011762015-01-07 15:31:43 +0800135 if (irq_remapping_enabled && remap_ops->reenable)
136 return remap_ops->reenable(mode);
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700137
Jiang Liue9011762015-01-07 15:31:43 +0800138 return 0;
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700139}
140
Suresh Siddha95a02e92012-03-30 11:47:07 -0700141int __init irq_remap_enable_fault_handling(void)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700142{
Joerg Roedel70733e02012-09-26 12:44:33 +0200143 if (!irq_remapping_enabled)
144 return 0;
145
Jiang Liue9011762015-01-07 15:31:43 +0800146 if (!remap_ops->enable_faulting)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700147 return -ENODEV;
148
149 return remap_ops->enable_faulting();
150}
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700151
Joerg Roedel6a9f5de2012-09-26 12:44:41 +0200152void panic_if_irq_remap(const char *msg)
153{
154 if (irq_remapping_enabled)
155 panic(msg);
156}
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200157
Jiang Liu947045a2015-04-13 14:11:30 +0800158/**
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 */
166struct irq_domain *
167irq_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 */
184struct irq_domain *
185irq_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}