Merge branch 'x86/asm' into x86/core, to prepare for new patch
[firefly-linux-kernel-4.4.55.git] / drivers / iommu / irq_remapping.c
1 #include <linux/seq_file.h>
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 static int disable_irq_remap;
26 static struct irq_remap_ops *remap_ops;
27
28 static void irq_remapping_disable_io_apic(void)
29 {
30         /*
31          * With interrupt-remapping, for now we will use virtual wire A
32          * mode, as virtual wire B is little complex (need to configure
33          * both IOAPIC RTE as well as interrupt-remapping table entry).
34          * As this gets called during crash dump, keep this simple for
35          * now.
36          */
37         if (cpu_has_apic || apic_from_smp_config())
38                 disconnect_bsp_APIC(0);
39 }
40
41 static void __init irq_remapping_modify_x86_ops(void)
42 {
43         x86_io_apic_ops.disable         = irq_remapping_disable_io_apic;
44 }
45
46 static __init int setup_nointremap(char *str)
47 {
48         disable_irq_remap = 1;
49         return 0;
50 }
51 early_param("nointremap", setup_nointremap);
52
53 static __init int setup_irqremap(char *str)
54 {
55         if (!str)
56                 return -EINVAL;
57
58         while (*str) {
59                 if (!strncmp(str, "on", 2))
60                         disable_irq_remap = 0;
61                 else if (!strncmp(str, "off", 3))
62                         disable_irq_remap = 1;
63                 else if (!strncmp(str, "nosid", 5))
64                         disable_sourceid_checking = 1;
65                 else if (!strncmp(str, "no_x2apic_optout", 16))
66                         no_x2apic_optout = 1;
67
68                 str += strcspn(str, ",");
69                 while (*str == ',')
70                         str++;
71         }
72
73         return 0;
74 }
75 early_param("intremap", setup_irqremap);
76
77 void set_irq_remapping_broken(void)
78 {
79         irq_remap_broken = 1;
80 }
81
82 int __init irq_remapping_prepare(void)
83 {
84         if (disable_irq_remap)
85                 return -ENOSYS;
86
87         if (intel_irq_remap_ops.prepare() == 0)
88                 remap_ops = &intel_irq_remap_ops;
89         else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
90                  amd_iommu_irq_ops.prepare() == 0)
91                 remap_ops = &amd_iommu_irq_ops;
92         else
93                 return -ENOSYS;
94
95         return 0;
96 }
97
98 int __init irq_remapping_enable(void)
99 {
100         int ret;
101
102         if (!remap_ops->enable)
103                 return -ENODEV;
104
105         ret = remap_ops->enable();
106
107         if (irq_remapping_enabled)
108                 irq_remapping_modify_x86_ops();
109
110         return ret;
111 }
112
113 void irq_remapping_disable(void)
114 {
115         if (irq_remapping_enabled && remap_ops->disable)
116                 remap_ops->disable();
117 }
118
119 int irq_remapping_reenable(int mode)
120 {
121         if (irq_remapping_enabled && remap_ops->reenable)
122                 return remap_ops->reenable(mode);
123
124         return 0;
125 }
126
127 int __init irq_remap_enable_fault_handling(void)
128 {
129         if (!irq_remapping_enabled)
130                 return 0;
131
132         if (!remap_ops->enable_faulting)
133                 return -ENODEV;
134
135         return remap_ops->enable_faulting();
136 }
137
138 void panic_if_irq_remap(const char *msg)
139 {
140         if (irq_remapping_enabled)
141                 panic(msg);
142 }
143
144 void ir_ack_apic_edge(struct irq_data *data)
145 {
146         ack_APIC_irq();
147 }
148
149 /**
150  * irq_remapping_get_ir_irq_domain - Get the irqdomain associated with the IOMMU
151  *                                   device serving request @info
152  * @info: interrupt allocation information, used to identify the IOMMU device
153  *
154  * It's used to get parent irqdomain for HPET and IOAPIC irqdomains.
155  * Returns pointer to IRQ domain, or NULL on failure.
156  */
157 struct irq_domain *
158 irq_remapping_get_ir_irq_domain(struct irq_alloc_info *info)
159 {
160         if (!remap_ops || !remap_ops->get_ir_irq_domain)
161                 return NULL;
162
163         return remap_ops->get_ir_irq_domain(info);
164 }
165
166 /**
167  * irq_remapping_get_irq_domain - Get the irqdomain serving the request @info
168  * @info: interrupt allocation information, used to identify the IOMMU device
169  *
170  * There will be one PCI MSI/MSIX irqdomain associated with each interrupt
171  * remapping device, so this interface is used to retrieve the PCI MSI/MSIX
172  * irqdomain serving request @info.
173  * Returns pointer to IRQ domain, or NULL on failure.
174  */
175 struct irq_domain *
176 irq_remapping_get_irq_domain(struct irq_alloc_info *info)
177 {
178         if (!remap_ops || !remap_ops->get_irq_domain)
179                 return NULL;
180
181         return remap_ops->get_irq_domain(info);
182 }