blob: 7c11ff368d07f88dc357dd3f6e22c9fbe2ed6cc1 [file] [log] [blame]
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02001#include <linux/seq_file.h>
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>
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>
Joerg Roedel736baef2012-03-30 11:47:00 -070015
Suresh Siddha8a8f4222012-03-30 11:47:08 -070016#include "irq_remapping.h"
Joerg Roedel736baef2012-03-30 11:47:00 -070017
Suresh Siddha95a02e92012-03-30 11:47:07 -070018int irq_remapping_enabled;
Joerg Roedel736baef2012-03-30 11:47:00 -070019
Suresh Siddha95a02e92012-03-30 11:47:07 -070020int disable_irq_remap;
Joerg Roedel736baef2012-03-30 11:47:00 -070021int disable_sourceid_checking;
22int no_x2apic_optout;
23
24static struct irq_remap_ops *remap_ops;
25
Joerg Roedel5afba622012-09-26 12:44:38 +020026static int msi_alloc_remapped_irq(struct pci_dev *pdev, int irq, int nvec);
27static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
28 int index, int sub_handle);
Joerg Roedel373dd7a2012-09-26 12:44:39 +020029static int set_remapped_irq_affinity(struct irq_data *data,
30 const struct cpumask *mask,
31 bool force);
Joerg Roedel5afba622012-09-26 12:44:38 +020032
Joerg Roedela1bb20c2012-09-26 12:44:51 +020033static bool irq_remapped(struct irq_cfg *cfg)
34{
35 return (cfg->remapped == 1);
36}
37
Joerg Roedel1c4248c2012-09-26 12:44:35 +020038static void irq_remapping_disable_io_apic(void)
39{
40 /*
41 * With interrupt-remapping, for now we will use virtual wire A
42 * mode, as virtual wire B is little complex (need to configure
43 * both IOAPIC RTE as well as interrupt-remapping table entry).
44 * As this gets called during crash dump, keep this simple for
45 * now.
46 */
47 if (cpu_has_apic || apic_from_smp_config())
48 disconnect_bsp_APIC(0);
49}
50
Joerg Roedel5afba622012-09-26 12:44:38 +020051static int do_setup_msi_irqs(struct pci_dev *dev, int nvec)
52{
53 int node, ret, sub_handle, index = 0;
54 unsigned int irq;
55 struct msi_desc *msidesc;
56
57 nvec = __roundup_pow_of_two(nvec);
58
59 WARN_ON(!list_is_singular(&dev->msi_list));
60 msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
61 WARN_ON(msidesc->irq);
62 WARN_ON(msidesc->msi_attrib.multiple);
63
64 node = dev_to_node(&dev->dev);
65 irq = __create_irqs(get_nr_irqs_gsi(), nvec, node);
66 if (irq == 0)
67 return -ENOSPC;
68
69 msidesc->msi_attrib.multiple = ilog2(nvec);
70 for (sub_handle = 0; sub_handle < nvec; sub_handle++) {
71 if (!sub_handle) {
72 index = msi_alloc_remapped_irq(dev, irq, nvec);
73 if (index < 0) {
74 ret = index;
75 goto error;
76 }
77 } else {
78 ret = msi_setup_remapped_irq(dev, irq + sub_handle,
79 index, sub_handle);
80 if (ret < 0)
81 goto error;
82 }
83 ret = setup_msi_irq(dev, msidesc, irq, sub_handle);
84 if (ret < 0)
85 goto error;
86 }
87 return 0;
88
89error:
90 destroy_irqs(irq, nvec);
91
92 /*
93 * Restore altered MSI descriptor fields and prevent just destroyed
94 * IRQs from tearing down again in default_teardown_msi_irqs()
95 */
96 msidesc->irq = 0;
97 msidesc->msi_attrib.multiple = 0;
98
99 return ret;
100}
101
102static int do_setup_msix_irqs(struct pci_dev *dev, int nvec)
103{
104 int node, ret, sub_handle, index = 0;
105 struct msi_desc *msidesc;
106 unsigned int irq;
107
108 node = dev_to_node(&dev->dev);
109 irq = get_nr_irqs_gsi();
110 sub_handle = 0;
111
112 list_for_each_entry(msidesc, &dev->msi_list, list) {
113
114 irq = create_irq_nr(irq, node);
115 if (irq == 0)
116 return -1;
117
118 if (sub_handle == 0)
119 ret = index = msi_alloc_remapped_irq(dev, irq, nvec);
120 else
121 ret = msi_setup_remapped_irq(dev, irq, index, sub_handle);
122
123 if (ret < 0)
124 goto error;
125
126 ret = setup_msi_irq(dev, msidesc, irq, 0);
127 if (ret < 0)
128 goto error;
129
130 sub_handle += 1;
131 irq += 1;
132 }
133
134 return 0;
135
136error:
137 destroy_irq(irq);
138 return ret;
139}
140
141static int irq_remapping_setup_msi_irqs(struct pci_dev *dev,
142 int nvec, int type)
143{
144 if (type == PCI_CAP_ID_MSI)
145 return do_setup_msi_irqs(dev, nvec);
146 else
147 return do_setup_msix_irqs(dev, nvec);
148}
149
Joerg Roedelda165322012-09-26 12:44:50 +0200150void eoi_ioapic_pin_remapped(int apic, int pin, int vector)
151{
152 /*
153 * Intr-remapping uses pin number as the virtual vector
154 * in the RTE. Actual vector is programmed in
155 * intr-remapping table entry. Hence for the io-apic
156 * EOI we use the pin number.
157 */
158 io_apic_eoi(apic, pin);
159}
160
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200161static void __init irq_remapping_modify_x86_ops(void)
162{
Joerg Roedel71054d82012-09-26 12:44:37 +0200163 x86_io_apic_ops.disable = irq_remapping_disable_io_apic;
Joerg Roedel373dd7a2012-09-26 12:44:39 +0200164 x86_io_apic_ops.set_affinity = set_remapped_irq_affinity;
Joerg Roedela6a25dd2012-09-26 12:44:40 +0200165 x86_io_apic_ops.setup_entry = setup_ioapic_remapped_entry;
Joerg Roedelda165322012-09-26 12:44:50 +0200166 x86_io_apic_ops.eoi_ioapic_pin = eoi_ioapic_pin_remapped;
Joerg Roedel5afba622012-09-26 12:44:38 +0200167 x86_msi.setup_msi_irqs = irq_remapping_setup_msi_irqs;
Joerg Roedel71054d82012-09-26 12:44:37 +0200168 x86_msi.setup_hpet_msi = setup_hpet_msi_remapped;
Joerg Roedel76013842012-09-26 12:44:49 +0200169 x86_msi.compose_msi_msg = compose_remapped_msi_msg;
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200170}
171
Joerg Roedel736baef2012-03-30 11:47:00 -0700172static __init int setup_nointremap(char *str)
173{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700174 disable_irq_remap = 1;
Joerg Roedel736baef2012-03-30 11:47:00 -0700175 return 0;
176}
177early_param("nointremap", setup_nointremap);
178
Suresh Siddha95a02e92012-03-30 11:47:07 -0700179static __init int setup_irqremap(char *str)
Joerg Roedel736baef2012-03-30 11:47:00 -0700180{
181 if (!str)
182 return -EINVAL;
183
184 while (*str) {
185 if (!strncmp(str, "on", 2))
Suresh Siddha95a02e92012-03-30 11:47:07 -0700186 disable_irq_remap = 0;
Joerg Roedel736baef2012-03-30 11:47:00 -0700187 else if (!strncmp(str, "off", 3))
Suresh Siddha95a02e92012-03-30 11:47:07 -0700188 disable_irq_remap = 1;
Joerg Roedel736baef2012-03-30 11:47:00 -0700189 else if (!strncmp(str, "nosid", 5))
190 disable_sourceid_checking = 1;
191 else if (!strncmp(str, "no_x2apic_optout", 16))
192 no_x2apic_optout = 1;
193
194 str += strcspn(str, ",");
195 while (*str == ',')
196 str++;
197 }
198
199 return 0;
200}
Suresh Siddha95a02e92012-03-30 11:47:07 -0700201early_param("intremap", setup_irqremap);
Joerg Roedel736baef2012-03-30 11:47:00 -0700202
Suresh Siddha95a02e92012-03-30 11:47:07 -0700203void __init setup_irq_remapping_ops(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700204{
205 remap_ops = &intel_irq_remap_ops;
Joerg Roedelc18d2382012-06-26 16:59:34 +0200206
207#ifdef CONFIG_AMD_IOMMU
208 if (amd_iommu_irq_ops.prepare() == 0)
209 remap_ops = &amd_iommu_irq_ops;
210#endif
Joerg Roedel736baef2012-03-30 11:47:00 -0700211}
212
Suresh Siddha95a02e92012-03-30 11:47:07 -0700213int irq_remapping_supported(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700214{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700215 if (disable_irq_remap)
Joerg Roedel736baef2012-03-30 11:47:00 -0700216 return 0;
217
218 if (!remap_ops || !remap_ops->supported)
219 return 0;
220
221 return remap_ops->supported();
222}
223
Suresh Siddha95a02e92012-03-30 11:47:07 -0700224int __init irq_remapping_prepare(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700225{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700226 if (!remap_ops || !remap_ops->prepare)
Joerg Roedel736baef2012-03-30 11:47:00 -0700227 return -ENODEV;
228
Suresh Siddha95a02e92012-03-30 11:47:07 -0700229 return remap_ops->prepare();
Joerg Roedel736baef2012-03-30 11:47:00 -0700230}
231
Suresh Siddha95a02e92012-03-30 11:47:07 -0700232int __init irq_remapping_enable(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700233{
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200234 int ret;
235
Suresh Siddha95a02e92012-03-30 11:47:07 -0700236 if (!remap_ops || !remap_ops->enable)
Joerg Roedel736baef2012-03-30 11:47:00 -0700237 return -ENODEV;
238
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200239 ret = remap_ops->enable();
240
241 if (irq_remapping_enabled)
242 irq_remapping_modify_x86_ops();
243
244 return ret;
Joerg Roedel736baef2012-03-30 11:47:00 -0700245}
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700246
Suresh Siddha95a02e92012-03-30 11:47:07 -0700247void irq_remapping_disable(void)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700248{
Joerg Roedel70733e02012-09-26 12:44:33 +0200249 if (!irq_remapping_enabled ||
250 !remap_ops ||
251 !remap_ops->disable)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700252 return;
253
Suresh Siddha95a02e92012-03-30 11:47:07 -0700254 remap_ops->disable();
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700255}
256
Suresh Siddha95a02e92012-03-30 11:47:07 -0700257int irq_remapping_reenable(int mode)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700258{
Joerg Roedel70733e02012-09-26 12:44:33 +0200259 if (!irq_remapping_enabled ||
260 !remap_ops ||
261 !remap_ops->reenable)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700262 return 0;
263
Suresh Siddha95a02e92012-03-30 11:47:07 -0700264 return remap_ops->reenable(mode);
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700265}
266
Suresh Siddha95a02e92012-03-30 11:47:07 -0700267int __init irq_remap_enable_fault_handling(void)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700268{
Joerg Roedel70733e02012-09-26 12:44:33 +0200269 if (!irq_remapping_enabled)
270 return 0;
271
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700272 if (!remap_ops || !remap_ops->enable_faulting)
273 return -ENODEV;
274
275 return remap_ops->enable_faulting();
276}
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700277
Suresh Siddha95a02e92012-03-30 11:47:07 -0700278int setup_ioapic_remapped_entry(int irq,
279 struct IO_APIC_route_entry *entry,
280 unsigned int destination, int vector,
281 struct io_apic_irq_attr *attr)
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700282{
283 if (!remap_ops || !remap_ops->setup_ioapic_entry)
284 return -ENODEV;
285
286 return remap_ops->setup_ioapic_entry(irq, entry, destination,
287 vector, attr);
288}
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700289
Suresh Siddha95a02e92012-03-30 11:47:07 -0700290int set_remapped_irq_affinity(struct irq_data *data, const struct cpumask *mask,
291 bool force)
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700292{
Suresh Siddha7eb9ae02012-06-14 18:28:49 -0700293 if (!config_enabled(CONFIG_SMP) || !remap_ops ||
294 !remap_ops->set_affinity)
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700295 return 0;
296
297 return remap_ops->set_affinity(data, mask, force);
298}
Joerg Roedel9d619f62012-03-30 11:47:04 -0700299
Suresh Siddha95a02e92012-03-30 11:47:07 -0700300void free_remapped_irq(int irq)
Joerg Roedel9d619f62012-03-30 11:47:04 -0700301{
Joerg Roedel11b4a1c2012-09-26 12:44:47 +0200302 struct irq_cfg *cfg = irq_get_chip_data(irq);
303
Joerg Roedel9d619f62012-03-30 11:47:04 -0700304 if (!remap_ops || !remap_ops->free_irq)
305 return;
306
Joerg Roedel11b4a1c2012-09-26 12:44:47 +0200307 if (irq_remapped(cfg))
308 remap_ops->free_irq(irq);
Joerg Roedel9d619f62012-03-30 11:47:04 -0700309}
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700310
Suresh Siddha95a02e92012-03-30 11:47:07 -0700311void compose_remapped_msi_msg(struct pci_dev *pdev,
312 unsigned int irq, unsigned int dest,
313 struct msi_msg *msg, u8 hpet_id)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700314{
Joerg Roedel76013842012-09-26 12:44:49 +0200315 struct irq_cfg *cfg = irq_get_chip_data(irq);
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700316
Joerg Roedel76013842012-09-26 12:44:49 +0200317 if (!irq_remapped(cfg))
318 native_compose_msi_msg(pdev, irq, dest, msg, hpet_id);
319 else if (remap_ops && remap_ops->compose_msi_msg)
320 remap_ops->compose_msi_msg(pdev, irq, dest, msg, hpet_id);
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700321}
322
Joerg Roedel5afba622012-09-26 12:44:38 +0200323static int msi_alloc_remapped_irq(struct pci_dev *pdev, int irq, int nvec)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700324{
325 if (!remap_ops || !remap_ops->msi_alloc_irq)
326 return -ENODEV;
327
328 return remap_ops->msi_alloc_irq(pdev, irq, nvec);
329}
330
Joerg Roedel5afba622012-09-26 12:44:38 +0200331static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
332 int index, int sub_handle)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700333{
334 if (!remap_ops || !remap_ops->msi_setup_irq)
335 return -ENODEV;
336
337 return remap_ops->msi_setup_irq(pdev, irq, index, sub_handle);
338}
339
Suresh Siddha95a02e92012-03-30 11:47:07 -0700340int setup_hpet_msi_remapped(unsigned int irq, unsigned int id)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700341{
342 if (!remap_ops || !remap_ops->setup_hpet_msi)
343 return -ENODEV;
344
345 return remap_ops->setup_hpet_msi(irq, id);
346}
Joerg Roedel6a9f5de2012-09-26 12:44:41 +0200347
348void panic_if_irq_remap(const char *msg)
349{
350 if (irq_remapping_enabled)
351 panic(msg);
352}
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200353
354static void ir_ack_apic_edge(struct irq_data *data)
355{
356 ack_APIC_irq();
357}
358
359static void ir_ack_apic_level(struct irq_data *data)
360{
361 ack_APIC_irq();
362 eoi_ioapic_irq(data->irq, data->chip_data);
363}
364
365static void ir_print_prefix(struct irq_data *data, struct seq_file *p)
366{
367 seq_printf(p, " IR-%s", data->chip->name);
368}
369
370void irq_remap_modify_chip_defaults(struct irq_chip *chip)
371{
372 chip->irq_print_chip = ir_print_prefix;
373 chip->irq_ack = ir_ack_apic_edge;
374 chip->irq_eoi = ir_ack_apic_level;
375 chip->irq_set_affinity = x86_io_apic_ops.set_affinity;
376}
Joerg Roedel2976fd82012-09-26 12:44:48 +0200377
378bool setup_remapped_irq(int irq, struct irq_cfg *cfg, struct irq_chip *chip)
379{
380 if (!irq_remapped(cfg))
381 return false;
382 irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
383 irq_remap_modify_chip_defaults(chip);
384 return true;
385}