blob: 83314b9d8f38bfcf63652ddb63e5c4c05d4c7f93 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Joerg Roedel1c4248c2012-09-26 12:44:35 +02002#include <linux/cpumask.h>
Joerg Roedel736baef2012-03-30 11:47:00 -07003#include <linux/kernel.h>
4#include <linux/string.h>
5#include <linux/errno.h>
Joerg Roedel98f1ad22012-07-06 13:28:37 +02006#include <linux/msi.h>
Joerg Roedel5afba622012-09-26 12:44:38 +02007#include <linux/irq.h>
8#include <linux/pci.h>
Jiang Liua62b32c2015-04-13 14:11:29 +08009#include <linux/irqdomain.h>
Joerg Roedel98f1ad22012-07-06 13:28:37 +020010
11#include <asm/hw_irq.h>
12#include <asm/irq_remapping.h>
Joerg Roedel1c4248c2012-09-26 12:44:35 +020013#include <asm/processor.h>
14#include <asm/x86_init.h>
15#include <asm/apic.h>
Yijing Wang5fc24d82014-09-17 17:32:19 +080016#include <asm/hpet.h>
Joerg Roedel736baef2012-03-30 11:47:00 -070017
Suresh Siddha8a8f4222012-03-30 11:47:08 -070018#include "irq_remapping.h"
Joerg Roedel736baef2012-03-30 11:47:00 -070019
Suresh Siddha95a02e92012-03-30 11:47:07 -070020int irq_remapping_enabled;
Neil Horman03bbcb22013-04-16 16:38:32 -040021int irq_remap_broken;
Joerg Roedel736baef2012-03-30 11:47:00 -070022int disable_sourceid_checking;
23int no_x2apic_optout;
24
Feng Wub7d20632015-09-18 22:29:56 +080025int disable_irq_post = 0;
Feng Wu3d9b98f2015-06-09 13:20:35 +080026
Jiang Liu7fa1c842015-01-07 15:31:42 +080027static int disable_irq_remap;
Joerg Roedel736baef2012-03-30 11:47:00 -070028static struct irq_remap_ops *remap_ops;
29
Baoquan He51b146c2018-02-14 13:46:55 +080030static void irq_remapping_restore_boot_irq_mode(void)
Joerg Roedel1c4248c2012-09-26 12:44:35 +020031{
32 /*
33 * With interrupt-remapping, for now we will use virtual wire A
34 * mode, as virtual wire B is little complex (need to configure
35 * both IOAPIC RTE as well as interrupt-remapping table entry).
36 * As this gets called during crash dump, keep this simple for
37 * now.
38 */
Borislav Petkov93984fb2016-04-04 22:25:00 +020039 if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config())
Joerg Roedel1c4248c2012-09-26 12:44:35 +020040 disconnect_bsp_APIC(0);
41}
42
43static void __init irq_remapping_modify_x86_ops(void)
44{
Baoquan He51b146c2018-02-14 13:46:55 +080045 x86_apic_ops.restore = irq_remapping_restore_boot_irq_mode;
Joerg Roedel1c4248c2012-09-26 12:44:35 +020046}
47
Joerg Roedel736baef2012-03-30 11:47:00 -070048static __init int setup_nointremap(char *str)
49{
Suresh Siddha95a02e92012-03-30 11:47:07 -070050 disable_irq_remap = 1;
Joerg Roedel736baef2012-03-30 11:47:00 -070051 return 0;
52}
53early_param("nointremap", setup_nointremap);
54
Suresh Siddha95a02e92012-03-30 11:47:07 -070055static __init int setup_irqremap(char *str)
Joerg Roedel736baef2012-03-30 11:47:00 -070056{
57 if (!str)
58 return -EINVAL;
59
60 while (*str) {
Feng Wub7d20632015-09-18 22:29:56 +080061 if (!strncmp(str, "on", 2)) {
Suresh Siddha95a02e92012-03-30 11:47:07 -070062 disable_irq_remap = 0;
Feng Wub7d20632015-09-18 22:29:56 +080063 disable_irq_post = 0;
64 } else if (!strncmp(str, "off", 3)) {
Suresh Siddha95a02e92012-03-30 11:47:07 -070065 disable_irq_remap = 1;
Feng Wub7d20632015-09-18 22:29:56 +080066 disable_irq_post = 1;
67 } else if (!strncmp(str, "nosid", 5))
Joerg Roedel736baef2012-03-30 11:47:00 -070068 disable_sourceid_checking = 1;
69 else if (!strncmp(str, "no_x2apic_optout", 16))
70 no_x2apic_optout = 1;
Feng Wub7d20632015-09-18 22:29:56 +080071 else if (!strncmp(str, "nopost", 6))
72 disable_irq_post = 1;
Joerg Roedel736baef2012-03-30 11:47:00 -070073
74 str += strcspn(str, ",");
75 while (*str == ',')
76 str++;
77 }
78
79 return 0;
80}
Suresh Siddha95a02e92012-03-30 11:47:07 -070081early_param("intremap", setup_irqremap);
Joerg Roedel736baef2012-03-30 11:47:00 -070082
Neil Horman03bbcb22013-04-16 16:38:32 -040083void set_irq_remapping_broken(void)
84{
85 irq_remap_broken = 1;
86}
87
Feng Wu959c8702015-06-09 13:20:36 +080088bool irq_remapping_cap(enum irq_remap_cap cap)
89{
90 if (!remap_ops || disable_irq_post)
Joerg Roedel30e93762015-08-13 11:13:17 +020091 return false;
Feng Wu959c8702015-06-09 13:20:36 +080092
93 return (remap_ops->capability & (1 << cap));
94}
95EXPORT_SYMBOL_GPL(irq_remapping_cap);
96
Suresh Siddha95a02e92012-03-30 11:47:07 -070097int __init irq_remapping_prepare(void)
Joerg Roedel736baef2012-03-30 11:47:00 -070098{
Jiang Liuc392f562015-01-07 15:31:40 +080099 if (disable_irq_remap)
100 return -ENOSYS;
101
Jiang Liu30969e32015-01-07 15:31:41 +0800102 if (intel_irq_remap_ops.prepare() == 0)
103 remap_ops = &intel_irq_remap_ops;
104 else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
105 amd_iommu_irq_ops.prepare() == 0)
Thomas Gleixnera1dafe82015-01-07 15:31:28 +0800106 remap_ops = &amd_iommu_irq_ops;
Lan Tianyu29217a42019-02-27 22:54:04 +0800107 else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) &&
108 hyperv_irq_remap_ops.prepare() == 0)
109 remap_ops = &hyperv_irq_remap_ops;
Jiang Liu30969e32015-01-07 15:31:41 +0800110 else
111 return -ENOSYS;
112
113 return 0;
Joerg Roedel736baef2012-03-30 11:47:00 -0700114}
115
Suresh Siddha95a02e92012-03-30 11:47:07 -0700116int __init irq_remapping_enable(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700117{
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200118 int ret;
119
Jiang Liue9011762015-01-07 15:31:43 +0800120 if (!remap_ops->enable)
Joerg Roedel736baef2012-03-30 11:47:00 -0700121 return -ENODEV;
122
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200123 ret = remap_ops->enable();
124
125 if (irq_remapping_enabled)
126 irq_remapping_modify_x86_ops();
127
128 return ret;
Joerg Roedel736baef2012-03-30 11:47:00 -0700129}
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700130
Suresh Siddha95a02e92012-03-30 11:47:07 -0700131void irq_remapping_disable(void)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700132{
Jiang Liue9011762015-01-07 15:31:43 +0800133 if (irq_remapping_enabled && remap_ops->disable)
134 remap_ops->disable();
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700135}
136
Suresh Siddha95a02e92012-03-30 11:47:07 -0700137int irq_remapping_reenable(int mode)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700138{
Jiang Liue9011762015-01-07 15:31:43 +0800139 if (irq_remapping_enabled && remap_ops->reenable)
140 return remap_ops->reenable(mode);
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700141
Jiang Liue9011762015-01-07 15:31:43 +0800142 return 0;
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700143}
144
Suresh Siddha95a02e92012-03-30 11:47:07 -0700145int __init irq_remap_enable_fault_handling(void)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700146{
Joerg Roedel70733e02012-09-26 12:44:33 +0200147 if (!irq_remapping_enabled)
148 return 0;
149
Jiang Liue9011762015-01-07 15:31:43 +0800150 if (!remap_ops->enable_faulting)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700151 return -ENODEV;
152
153 return remap_ops->enable_faulting();
154}
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700155
Joerg Roedel6a9f5de2012-09-26 12:44:41 +0200156void panic_if_irq_remap(const char *msg)
157{
158 if (irq_remapping_enabled)
159 panic(msg);
160}