blob: 74a1767c89b512fa09ef2698b918211451c8e8de [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>
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;
Joerg Roedel736baef2012-03-30 11:47:00 -070020
Suresh Siddha95a02e92012-03-30 11:47:07 -070021int disable_irq_remap;
Neil Horman03bbcb22013-04-16 16:38:32 -040022int irq_remap_broken;
Joerg Roedel736baef2012-03-30 11:47:00 -070023int disable_sourceid_checking;
24int no_x2apic_optout;
25
26static struct irq_remap_ops *remap_ops;
27
Joerg Roedel5afba622012-09-26 12:44:38 +020028static int msi_alloc_remapped_irq(struct pci_dev *pdev, int irq, int nvec);
29static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
30 int index, int sub_handle);
Joerg Roedel373dd7a2012-09-26 12:44:39 +020031static int set_remapped_irq_affinity(struct irq_data *data,
32 const struct cpumask *mask,
33 bool force);
Joerg Roedel5afba622012-09-26 12:44:38 +020034
Joerg Roedela1bb20c2012-09-26 12:44:51 +020035static bool irq_remapped(struct irq_cfg *cfg)
36{
37 return (cfg->remapped == 1);
38}
39
Joerg Roedel1c4248c2012-09-26 12:44:35 +020040static void irq_remapping_disable_io_apic(void)
41{
42 /*
43 * With interrupt-remapping, for now we will use virtual wire A
44 * mode, as virtual wire B is little complex (need to configure
45 * both IOAPIC RTE as well as interrupt-remapping table entry).
46 * As this gets called during crash dump, keep this simple for
47 * now.
48 */
49 if (cpu_has_apic || apic_from_smp_config())
50 disconnect_bsp_APIC(0);
51}
52
Joerg Roedel5afba622012-09-26 12:44:38 +020053static int do_setup_msi_irqs(struct pci_dev *dev, int nvec)
54{
Thomas Gleixnerd24a1352014-05-07 15:44:07 +000055 int ret, sub_handle, nvec_pow2, index = 0;
Joerg Roedel5afba622012-09-26 12:44:38 +020056 unsigned int irq;
57 struct msi_desc *msidesc;
58
Joerg Roedel5afba622012-09-26 12:44:38 +020059 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);
Alexander Gordeev5fec9452013-05-13 11:06:17 +020063 WARN_ON(msidesc->nvec_used);
Joerg Roedel5afba622012-09-26 12:44:38 +020064
Thomas Gleixnerd24a1352014-05-07 15:44:07 +000065 irq = irq_alloc_hwirqs(nvec, dev_to_node(&dev->dev));
Joerg Roedel5afba622012-09-26 12:44:38 +020066 if (irq == 0)
67 return -ENOSPC;
68
Alexander Gordeev5fec9452013-05-13 11:06:17 +020069 nvec_pow2 = __roundup_pow_of_two(nvec);
70 msidesc->nvec_used = nvec;
71 msidesc->msi_attrib.multiple = ilog2(nvec_pow2);
Joerg Roedel5afba622012-09-26 12:44:38 +020072 for (sub_handle = 0; sub_handle < nvec; sub_handle++) {
73 if (!sub_handle) {
Alexander Gordeev5fec9452013-05-13 11:06:17 +020074 index = msi_alloc_remapped_irq(dev, irq, nvec_pow2);
Joerg Roedel5afba622012-09-26 12:44:38 +020075 if (index < 0) {
76 ret = index;
77 goto error;
78 }
79 } else {
80 ret = msi_setup_remapped_irq(dev, irq + sub_handle,
81 index, sub_handle);
82 if (ret < 0)
83 goto error;
84 }
85 ret = setup_msi_irq(dev, msidesc, irq, sub_handle);
86 if (ret < 0)
87 goto error;
88 }
89 return 0;
90
91error:
Thomas Gleixnerd24a1352014-05-07 15:44:07 +000092 irq_free_hwirqs(irq, nvec);
Joerg Roedel5afba622012-09-26 12:44:38 +020093
94 /*
95 * Restore altered MSI descriptor fields and prevent just destroyed
96 * IRQs from tearing down again in default_teardown_msi_irqs()
97 */
98 msidesc->irq = 0;
Alexander Gordeev5fec9452013-05-13 11:06:17 +020099 msidesc->nvec_used = 0;
Joerg Roedel5afba622012-09-26 12:44:38 +0200100 msidesc->msi_attrib.multiple = 0;
101
102 return ret;
103}
104
105static int do_setup_msix_irqs(struct pci_dev *dev, int nvec)
106{
107 int node, ret, sub_handle, index = 0;
108 struct msi_desc *msidesc;
109 unsigned int irq;
110
111 node = dev_to_node(&dev->dev);
Joerg Roedel5afba622012-09-26 12:44:38 +0200112 sub_handle = 0;
113
114 list_for_each_entry(msidesc, &dev->msi_list, list) {
115
Thomas Gleixnerd24a1352014-05-07 15:44:07 +0000116 irq = irq_alloc_hwirq(node);
Joerg Roedel5afba622012-09-26 12:44:38 +0200117 if (irq == 0)
118 return -1;
119
120 if (sub_handle == 0)
121 ret = index = msi_alloc_remapped_irq(dev, irq, nvec);
122 else
123 ret = msi_setup_remapped_irq(dev, irq, index, sub_handle);
124
125 if (ret < 0)
126 goto error;
127
128 ret = setup_msi_irq(dev, msidesc, irq, 0);
129 if (ret < 0)
130 goto error;
131
132 sub_handle += 1;
133 irq += 1;
134 }
135
136 return 0;
137
138error:
Thomas Gleixnerd24a1352014-05-07 15:44:07 +0000139 irq_free_hwirq(irq);
Joerg Roedel5afba622012-09-26 12:44:38 +0200140 return ret;
141}
142
143static int irq_remapping_setup_msi_irqs(struct pci_dev *dev,
144 int nvec, int type)
145{
146 if (type == PCI_CAP_ID_MSI)
147 return do_setup_msi_irqs(dev, nvec);
148 else
149 return do_setup_msix_irqs(dev, nvec);
150}
151
Rashika Kheriad2d1e8f2013-12-18 12:06:45 +0530152static void eoi_ioapic_pin_remapped(int apic, int pin, int vector)
Joerg Roedelda165322012-09-26 12:44:50 +0200153{
154 /*
155 * Intr-remapping uses pin number as the virtual vector
156 * in the RTE. Actual vector is programmed in
157 * intr-remapping table entry. Hence for the io-apic
158 * EOI we use the pin number.
159 */
160 io_apic_eoi(apic, pin);
161}
162
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200163static void __init irq_remapping_modify_x86_ops(void)
164{
Joerg Roedel71054d82012-09-26 12:44:37 +0200165 x86_io_apic_ops.disable = irq_remapping_disable_io_apic;
Joerg Roedel373dd7a2012-09-26 12:44:39 +0200166 x86_io_apic_ops.set_affinity = set_remapped_irq_affinity;
Joerg Roedela6a25dd2012-09-26 12:44:40 +0200167 x86_io_apic_ops.setup_entry = setup_ioapic_remapped_entry;
Joerg Roedelda165322012-09-26 12:44:50 +0200168 x86_io_apic_ops.eoi_ioapic_pin = eoi_ioapic_pin_remapped;
Joerg Roedel5afba622012-09-26 12:44:38 +0200169 x86_msi.setup_msi_irqs = irq_remapping_setup_msi_irqs;
Joerg Roedel71054d82012-09-26 12:44:37 +0200170 x86_msi.setup_hpet_msi = setup_hpet_msi_remapped;
Joerg Roedel76013842012-09-26 12:44:49 +0200171 x86_msi.compose_msi_msg = compose_remapped_msi_msg;
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200172}
173
Joerg Roedel736baef2012-03-30 11:47:00 -0700174static __init int setup_nointremap(char *str)
175{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700176 disable_irq_remap = 1;
Joerg Roedel736baef2012-03-30 11:47:00 -0700177 return 0;
178}
179early_param("nointremap", setup_nointremap);
180
Suresh Siddha95a02e92012-03-30 11:47:07 -0700181static __init int setup_irqremap(char *str)
Joerg Roedel736baef2012-03-30 11:47:00 -0700182{
183 if (!str)
184 return -EINVAL;
185
186 while (*str) {
187 if (!strncmp(str, "on", 2))
Suresh Siddha95a02e92012-03-30 11:47:07 -0700188 disable_irq_remap = 0;
Joerg Roedel736baef2012-03-30 11:47:00 -0700189 else if (!strncmp(str, "off", 3))
Suresh Siddha95a02e92012-03-30 11:47:07 -0700190 disable_irq_remap = 1;
Joerg Roedel736baef2012-03-30 11:47:00 -0700191 else if (!strncmp(str, "nosid", 5))
192 disable_sourceid_checking = 1;
193 else if (!strncmp(str, "no_x2apic_optout", 16))
194 no_x2apic_optout = 1;
195
196 str += strcspn(str, ",");
197 while (*str == ',')
198 str++;
199 }
200
201 return 0;
202}
Suresh Siddha95a02e92012-03-30 11:47:07 -0700203early_param("intremap", setup_irqremap);
Joerg Roedel736baef2012-03-30 11:47:00 -0700204
Suresh Siddha95a02e92012-03-30 11:47:07 -0700205void __init setup_irq_remapping_ops(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700206{
207 remap_ops = &intel_irq_remap_ops;
Joerg Roedelc18d2382012-06-26 16:59:34 +0200208
209#ifdef CONFIG_AMD_IOMMU
210 if (amd_iommu_irq_ops.prepare() == 0)
211 remap_ops = &amd_iommu_irq_ops;
212#endif
Joerg Roedel736baef2012-03-30 11:47:00 -0700213}
214
Neil Horman03bbcb22013-04-16 16:38:32 -0400215void set_irq_remapping_broken(void)
216{
217 irq_remap_broken = 1;
218}
219
Suresh Siddha95a02e92012-03-30 11:47:07 -0700220int irq_remapping_supported(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700221{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700222 if (disable_irq_remap)
Joerg Roedel736baef2012-03-30 11:47:00 -0700223 return 0;
224
225 if (!remap_ops || !remap_ops->supported)
226 return 0;
227
228 return remap_ops->supported();
229}
230
Suresh Siddha95a02e92012-03-30 11:47:07 -0700231int __init irq_remapping_prepare(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700232{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700233 if (!remap_ops || !remap_ops->prepare)
Joerg Roedel736baef2012-03-30 11:47:00 -0700234 return -ENODEV;
235
Suresh Siddha95a02e92012-03-30 11:47:07 -0700236 return remap_ops->prepare();
Joerg Roedel736baef2012-03-30 11:47:00 -0700237}
238
Suresh Siddha95a02e92012-03-30 11:47:07 -0700239int __init irq_remapping_enable(void)
Joerg Roedel736baef2012-03-30 11:47:00 -0700240{
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200241 int ret;
242
Suresh Siddha95a02e92012-03-30 11:47:07 -0700243 if (!remap_ops || !remap_ops->enable)
Joerg Roedel736baef2012-03-30 11:47:00 -0700244 return -ENODEV;
245
Joerg Roedel1c4248c2012-09-26 12:44:35 +0200246 ret = remap_ops->enable();
247
248 if (irq_remapping_enabled)
249 irq_remapping_modify_x86_ops();
250
251 return ret;
Joerg Roedel736baef2012-03-30 11:47:00 -0700252}
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700253
Suresh Siddha95a02e92012-03-30 11:47:07 -0700254void irq_remapping_disable(void)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700255{
Joerg Roedel70733e02012-09-26 12:44:33 +0200256 if (!irq_remapping_enabled ||
257 !remap_ops ||
258 !remap_ops->disable)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700259 return;
260
Suresh Siddha95a02e92012-03-30 11:47:07 -0700261 remap_ops->disable();
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700262}
263
Suresh Siddha95a02e92012-03-30 11:47:07 -0700264int irq_remapping_reenable(int mode)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700265{
Joerg Roedel70733e02012-09-26 12:44:33 +0200266 if (!irq_remapping_enabled ||
267 !remap_ops ||
268 !remap_ops->reenable)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700269 return 0;
270
Suresh Siddha95a02e92012-03-30 11:47:07 -0700271 return remap_ops->reenable(mode);
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700272}
273
Suresh Siddha95a02e92012-03-30 11:47:07 -0700274int __init irq_remap_enable_fault_handling(void)
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700275{
Joerg Roedel70733e02012-09-26 12:44:33 +0200276 if (!irq_remapping_enabled)
277 return 0;
278
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700279 if (!remap_ops || !remap_ops->enable_faulting)
280 return -ENODEV;
281
282 return remap_ops->enable_faulting();
283}
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700284
Suresh Siddha95a02e92012-03-30 11:47:07 -0700285int setup_ioapic_remapped_entry(int irq,
286 struct IO_APIC_route_entry *entry,
287 unsigned int destination, int vector,
288 struct io_apic_irq_attr *attr)
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700289{
290 if (!remap_ops || !remap_ops->setup_ioapic_entry)
291 return -ENODEV;
292
293 return remap_ops->setup_ioapic_entry(irq, entry, destination,
294 vector, attr);
295}
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700296
Jiang Liub707cb02014-01-06 14:18:26 +0800297static int set_remapped_irq_affinity(struct irq_data *data,
298 const struct cpumask *mask, bool force)
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700299{
Suresh Siddha7eb9ae02012-06-14 18:28:49 -0700300 if (!config_enabled(CONFIG_SMP) || !remap_ops ||
301 !remap_ops->set_affinity)
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700302 return 0;
303
304 return remap_ops->set_affinity(data, mask, force);
305}
Joerg Roedel9d619f62012-03-30 11:47:04 -0700306
Suresh Siddha95a02e92012-03-30 11:47:07 -0700307void free_remapped_irq(int irq)
Joerg Roedel9d619f62012-03-30 11:47:04 -0700308{
Joerg Roedel11b4a1c2012-09-26 12:44:47 +0200309 struct irq_cfg *cfg = irq_get_chip_data(irq);
310
Joerg Roedel9d619f62012-03-30 11:47:04 -0700311 if (!remap_ops || !remap_ops->free_irq)
312 return;
313
Joerg Roedel11b4a1c2012-09-26 12:44:47 +0200314 if (irq_remapped(cfg))
315 remap_ops->free_irq(irq);
Joerg Roedel9d619f62012-03-30 11:47:04 -0700316}
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700317
Suresh Siddha95a02e92012-03-30 11:47:07 -0700318void compose_remapped_msi_msg(struct pci_dev *pdev,
319 unsigned int irq, unsigned int dest,
320 struct msi_msg *msg, u8 hpet_id)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700321{
Joerg Roedel76013842012-09-26 12:44:49 +0200322 struct irq_cfg *cfg = irq_get_chip_data(irq);
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700323
Joerg Roedel76013842012-09-26 12:44:49 +0200324 if (!irq_remapped(cfg))
325 native_compose_msi_msg(pdev, irq, dest, msg, hpet_id);
326 else if (remap_ops && remap_ops->compose_msi_msg)
327 remap_ops->compose_msi_msg(pdev, irq, dest, msg, hpet_id);
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700328}
329
Joerg Roedel5afba622012-09-26 12:44:38 +0200330static int msi_alloc_remapped_irq(struct pci_dev *pdev, int irq, int nvec)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700331{
332 if (!remap_ops || !remap_ops->msi_alloc_irq)
333 return -ENODEV;
334
335 return remap_ops->msi_alloc_irq(pdev, irq, nvec);
336}
337
Joerg Roedel5afba622012-09-26 12:44:38 +0200338static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
339 int index, int sub_handle)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700340{
341 if (!remap_ops || !remap_ops->msi_setup_irq)
342 return -ENODEV;
343
344 return remap_ops->msi_setup_irq(pdev, irq, index, sub_handle);
345}
346
Suresh Siddha95a02e92012-03-30 11:47:07 -0700347int setup_hpet_msi_remapped(unsigned int irq, unsigned int id)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700348{
Yijing Wang5fc24d82014-09-17 17:32:19 +0800349 int ret;
350
351 if (!remap_ops || !remap_ops->alloc_hpet_msi)
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700352 return -ENODEV;
353
Yijing Wang5fc24d82014-09-17 17:32:19 +0800354 ret = remap_ops->alloc_hpet_msi(irq, id);
355 if (ret)
356 return -EINVAL;
357
358 return default_setup_hpet_msi(irq, id);
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700359}
Joerg Roedel6a9f5de2012-09-26 12:44:41 +0200360
361void panic_if_irq_remap(const char *msg)
362{
363 if (irq_remapping_enabled)
364 panic(msg);
365}
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200366
367static void ir_ack_apic_edge(struct irq_data *data)
368{
369 ack_APIC_irq();
370}
371
372static void ir_ack_apic_level(struct irq_data *data)
373{
374 ack_APIC_irq();
375 eoi_ioapic_irq(data->irq, data->chip_data);
376}
377
378static void ir_print_prefix(struct irq_data *data, struct seq_file *p)
379{
380 seq_printf(p, " IR-%s", data->chip->name);
381}
382
383void irq_remap_modify_chip_defaults(struct irq_chip *chip)
384{
385 chip->irq_print_chip = ir_print_prefix;
386 chip->irq_ack = ir_ack_apic_edge;
387 chip->irq_eoi = ir_ack_apic_level;
388 chip->irq_set_affinity = x86_io_apic_ops.set_affinity;
389}
Joerg Roedel2976fd82012-09-26 12:44:48 +0200390
391bool setup_remapped_irq(int irq, struct irq_cfg *cfg, struct irq_chip *chip)
392{
393 if (!irq_remapped(cfg))
394 return false;
395 irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
396 irq_remap_modify_chip_defaults(chip);
397 return true;
398}