blob: 8634e1b49c039fad57cb149e69e9a7b27116a50a [file] [log] [blame]
Alex Nixonb5401a92010-03-18 16:31:34 -04001/*
2 * Xen PCI Frontend Stub - puts some "dummy" functions in to the Linux
3 * x86 PCI core to support the Xen PCI Frontend
4 *
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 */
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/pci.h>
10#include <linux/acpi.h>
11
12#include <linux/io.h>
Stefano Stabellini0e058e52010-10-21 17:40:08 +010013#include <asm/io_apic.h>
Alex Nixonb5401a92010-03-18 16:31:34 -040014#include <asm/pci_x86.h>
15
16#include <asm/xen/hypervisor.h>
17
Stefano Stabellini3942b742010-06-24 17:50:18 +010018#include <xen/features.h>
Alex Nixonb5401a92010-03-18 16:31:34 -040019#include <xen/events.h>
20#include <asm/xen/pci.h>
21
Stefano Stabellini42a1de52010-06-24 16:42:04 +010022#ifdef CONFIG_ACPI
23static int xen_hvm_register_pirq(u32 gsi, int triggering)
24{
25 int rc, irq;
26 struct physdev_map_pirq map_irq;
27 int shareable = 0;
28 char *name;
29
30 if (!xen_hvm_domain())
31 return -1;
32
33 map_irq.domid = DOMID_SELF;
34 map_irq.type = MAP_PIRQ_TYPE_GSI;
35 map_irq.index = gsi;
36 map_irq.pirq = -1;
37
38 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
39 if (rc) {
40 printk(KERN_WARNING "xen map irq failed %d\n", rc);
41 return -1;
42 }
43
44 if (triggering == ACPI_EDGE_SENSITIVE) {
45 shareable = 0;
46 name = "ioapic-edge";
47 } else {
48 shareable = 1;
49 name = "ioapic-level";
50 }
51
52 irq = xen_map_pirq_gsi(map_irq.pirq, gsi, shareable, name);
53
54 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
55
56 return irq;
57}
Jeremy Fitzhardinge90f68812010-06-24 17:05:41 +010058
59static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi,
60 int trigger, int polarity)
61{
62 return xen_hvm_register_pirq(gsi, trigger);
63}
Stefano Stabellini42a1de52010-06-24 16:42:04 +010064#endif
65
Alex Nixonb5401a92010-03-18 16:31:34 -040066#if defined(CONFIG_PCI_MSI)
67#include <linux/msi.h>
Stefano Stabellini809f9262010-07-01 17:10:39 +010068#include <asm/msidef.h>
Alex Nixonb5401a92010-03-18 16:31:34 -040069
70struct xen_pci_frontend_ops *xen_pci_frontend;
71EXPORT_SYMBOL_GPL(xen_pci_frontend);
72
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000073#define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
74 MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
75
Stefano Stabellini809f9262010-07-01 17:10:39 +010076static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
77 struct msi_msg *msg)
78{
79 /* We set vector == 0 to tell the hypervisor we don't care about it,
80 * but we want a pirq setup instead.
81 * We use the dest_id field to pass the pirq that we want. */
82 msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq);
83 msg->address_lo =
84 MSI_ADDR_BASE_LO |
85 MSI_ADDR_DEST_MODE_PHYSICAL |
86 MSI_ADDR_REDIRECTION_CPU |
87 MSI_ADDR_DEST_ID(pirq);
88
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000089 msg->data = XEN_PIRQ_MSI_DATA;
Stefano Stabellini809f9262010-07-01 17:10:39 +010090}
91
92static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
93{
94 int irq, pirq, ret = 0;
95 struct msi_desc *msidesc;
96 struct msi_msg msg;
97
98 list_for_each_entry(msidesc, &dev->msi_list, list) {
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000099 __read_msi_msg(msidesc, &msg);
100 pirq = MSI_ADDR_EXT_DEST_ID(msg.address_hi) |
101 ((msg.address_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xff);
102 if (xen_irq_from_pirq(pirq) >= 0 && msg.data == XEN_PIRQ_MSI_DATA) {
103 xen_allocate_pirq_msi((type == PCI_CAP_ID_MSIX) ?
104 "msi-x" : "msi", &irq, &pirq, XEN_ALLOC_IRQ);
105 if (irq < 0)
106 goto error;
107 ret = set_irq_msi(irq, msidesc);
108 if (ret < 0)
109 goto error_while;
110 printk(KERN_DEBUG "xen: msi already setup: msi --> irq=%d"
111 " pirq=%d\n", irq, pirq);
112 return 0;
113 }
Stefano Stabellini809f9262010-07-01 17:10:39 +0100114 xen_allocate_pirq_msi((type == PCI_CAP_ID_MSIX) ?
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000115 "msi-x" : "msi", &irq, &pirq, (XEN_ALLOC_IRQ | XEN_ALLOC_PIRQ));
Stefano Stabellini809f9262010-07-01 17:10:39 +0100116 if (irq < 0 || pirq < 0)
117 goto error;
118 printk(KERN_DEBUG "xen: msi --> irq=%d, pirq=%d\n", irq, pirq);
119 xen_msi_compose_msg(dev, pirq, &msg);
120 ret = set_irq_msi(irq, msidesc);
121 if (ret < 0)
122 goto error_while;
123 write_msi_msg(irq, &msg);
124 }
125 return 0;
126
127error_while:
128 unbind_from_irqhandler(irq, NULL);
129error:
130 if (ret == -ENODEV)
131 dev_err(&dev->dev, "Xen PCI frontend has not registered" \
132 " MSI/MSI-X support!\n");
133
134 return ret;
135}
136
Alex Nixonb5401a92010-03-18 16:31:34 -0400137/*
138 * For MSI interrupts we have to use drivers/xen/event.s functions to
139 * allocate an irq_desc and setup the right */
140
141
142static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
143{
144 int irq, ret, i;
145 struct msi_desc *msidesc;
146 int *v;
147
148 v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
149 if (!v)
150 return -ENOMEM;
151
Qing Hef731e3ef2010-10-11 15:30:09 +0100152 if (type == PCI_CAP_ID_MSIX)
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500153 ret = xen_pci_frontend_enable_msix(dev, v, nvec);
Qing Hef731e3ef2010-10-11 15:30:09 +0100154 else
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500155 ret = xen_pci_frontend_enable_msi(dev, v);
Qing Hef731e3ef2010-10-11 15:30:09 +0100156 if (ret)
157 goto error;
Alex Nixonb5401a92010-03-18 16:31:34 -0400158 i = 0;
159 list_for_each_entry(msidesc, &dev->msi_list, list) {
Konrad Rzeszutek Wilk55cb8cd2011-02-16 13:43:04 -0500160 xen_allocate_pirq_msi(
Alex Nixonb5401a92010-03-18 16:31:34 -0400161 (type == PCI_CAP_ID_MSIX) ?
Konrad Rzeszutek Wilk55cb8cd2011-02-16 13:43:04 -0500162 "pcifront-msi-x" : "pcifront-msi",
163 &irq, &v[i], XEN_ALLOC_IRQ);
Jiri Slaby07cf2a62010-11-06 10:06:49 +0100164 if (irq < 0) {
165 ret = -1;
166 goto free;
167 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400168 ret = set_irq_msi(irq, msidesc);
169 if (ret)
170 goto error_while;
171 i++;
172 }
173 kfree(v);
174 return 0;
175
176error_while:
177 unbind_from_irqhandler(irq, NULL);
178error:
179 if (ret == -ENODEV)
180 dev_err(&dev->dev, "Xen PCI frontend has not registered" \
181 " MSI/MSI-X support!\n");
Jiri Slaby07cf2a62010-11-06 10:06:49 +0100182free:
Alex Nixonb5401a92010-03-18 16:31:34 -0400183 kfree(v);
184 return ret;
185}
186
187static void xen_teardown_msi_irqs(struct pci_dev *dev)
188{
Qing Hef731e3ef2010-10-11 15:30:09 +0100189 struct msi_desc *msidesc;
Alex Nixonb5401a92010-03-18 16:31:34 -0400190
Qing Hef731e3ef2010-10-11 15:30:09 +0100191 msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
192 if (msidesc->msi_attrib.is_msix)
193 xen_pci_frontend_disable_msix(dev);
194 else
195 xen_pci_frontend_disable_msi(dev);
Konrad Rzeszutek Wilk3d74a532011-02-17 16:12:51 -0500196
197 /* Free the IRQ's and the msidesc using the generic code. */
198 default_teardown_msi_irqs(dev);
Alex Nixonb5401a92010-03-18 16:31:34 -0400199}
200
201static void xen_teardown_msi_irq(unsigned int irq)
202{
203 xen_destroy_irq(irq);
204}
Qing Hef731e3ef2010-10-11 15:30:09 +0100205
206static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
207{
208 int irq, ret;
209 struct msi_desc *msidesc;
210
211 list_for_each_entry(msidesc, &dev->msi_list, list) {
212 irq = xen_create_msi_irq(dev, msidesc, type);
213 if (irq < 0)
214 return -1;
215
216 ret = set_irq_msi(irq, msidesc);
217 if (ret)
218 goto error;
219 }
220 return 0;
221
222error:
223 xen_destroy_irq(irq);
224 return ret;
225}
Alex Nixonb5401a92010-03-18 16:31:34 -0400226#endif
227
228static int xen_pcifront_enable_irq(struct pci_dev *dev)
229{
230 int rc;
231 int share = 1;
Ian Campbell3f2a2302011-01-11 17:20:13 +0000232 u8 gsi;
Alex Nixonb5401a92010-03-18 16:31:34 -0400233
Ian Campbell3f2a2302011-01-11 17:20:13 +0000234 rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
Alex Nixonb5401a92010-03-18 16:31:34 -0400235 if (rc < 0) {
Ian Campbell3f2a2302011-01-11 17:20:13 +0000236 dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
237 rc);
Alex Nixonb5401a92010-03-18 16:31:34 -0400238 return rc;
239 }
Ian Campbell3f2a2302011-01-11 17:20:13 +0000240
241 if (gsi < NR_IRQS_LEGACY)
242 share = 0;
243
244 rc = xen_allocate_pirq(gsi, share, "pcifront");
245 if (rc < 0) {
246 dev_warn(&dev->dev, "Xen PCI: failed to register GSI%d: %d\n",
247 gsi, rc);
248 return rc;
249 }
250
251 dev->irq = rc;
252 dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400253 return 0;
254}
255
256int __init pci_xen_init(void)
257{
258 if (!xen_pv_domain() || xen_initial_domain())
259 return -ENODEV;
260
261 printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
262
263 pcibios_set_cache_line_size();
264
265 pcibios_enable_irq = xen_pcifront_enable_irq;
266 pcibios_disable_irq = NULL;
267
268#ifdef CONFIG_ACPI
269 /* Keep ACPI out of the picture */
270 acpi_noirq = 1;
271#endif
272
Alex Nixonb5401a92010-03-18 16:31:34 -0400273#ifdef CONFIG_PCI_MSI
274 x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
275 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
276 x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
277#endif
278 return 0;
279}
Stefano Stabellini3942b742010-06-24 17:50:18 +0100280
281int __init pci_xen_hvm_init(void)
282{
283 if (!xen_feature(XENFEAT_hvm_pirqs))
284 return 0;
285
286#ifdef CONFIG_ACPI
287 /*
288 * We don't want to change the actual ACPI delivery model,
289 * just how GSIs get registered.
290 */
291 __acpi_register_gsi = acpi_register_gsi_xen_hvm;
292#endif
Stefano Stabellini809f9262010-07-01 17:10:39 +0100293
294#ifdef CONFIG_PCI_MSI
295 x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
296 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
297#endif
Stefano Stabellini3942b742010-06-24 17:50:18 +0100298 return 0;
299}
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100300
301#ifdef CONFIG_XEN_DOM0
302static int xen_register_pirq(u32 gsi, int triggering)
303{
304 int rc, irq;
305 struct physdev_map_pirq map_irq;
306 int shareable = 0;
307 char *name;
308
309 if (!xen_pv_domain())
310 return -1;
311
312 if (triggering == ACPI_EDGE_SENSITIVE) {
313 shareable = 0;
314 name = "ioapic-edge";
315 } else {
316 shareable = 1;
317 name = "ioapic-level";
318 }
319
320 irq = xen_allocate_pirq(gsi, shareable, name);
321
322 printk(KERN_DEBUG "xen: --> irq=%d\n", irq);
323
324 if (irq < 0)
325 goto out;
326
327 map_irq.domid = DOMID_SELF;
328 map_irq.type = MAP_PIRQ_TYPE_GSI;
329 map_irq.index = gsi;
330 map_irq.pirq = irq;
331
332 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
333 if (rc) {
334 printk(KERN_WARNING "xen map irq failed %d\n", rc);
335 return -1;
336 }
337
338out:
339 return irq;
340}
341
342static int xen_register_gsi(u32 gsi, int triggering, int polarity)
343{
344 int rc, irq;
345 struct physdev_setup_gsi setup_gsi;
346
347 if (!xen_pv_domain())
348 return -1;
349
350 printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
351 gsi, triggering, polarity);
352
353 irq = xen_register_pirq(gsi, triggering);
354
355 setup_gsi.gsi = gsi;
356 setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
357 setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
358
359 rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
360 if (rc == -EEXIST)
361 printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
362 else if (rc) {
363 printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
364 gsi, rc);
365 }
366
367 return irq;
368}
369
370static __init void xen_setup_acpi_sci(void)
371{
372 int rc;
373 int trigger, polarity;
374 int gsi = acpi_sci_override_gsi;
375
376 if (!gsi)
377 return;
378
379 rc = acpi_get_override_irq(gsi, &trigger, &polarity);
380 if (rc) {
381 printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi"
382 " sci, rc=%d\n", rc);
383 return;
384 }
385 trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
386 polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
387
388 printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d "
389 "polarity=%d\n", gsi, trigger, polarity);
390
391 gsi = xen_register_gsi(gsi, trigger, polarity);
392 printk(KERN_INFO "xen: acpi sci %d\n", gsi);
393
394 return;
395}
396
397static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
398 int trigger, int polarity)
399{
400 return xen_register_gsi(gsi, trigger, polarity);
401}
402
403static int __init pci_xen_initial_domain(void)
404{
Qing Hef731e3ef2010-10-11 15:30:09 +0100405#ifdef CONFIG_PCI_MSI
406 x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
407 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
408#endif
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100409 xen_setup_acpi_sci();
410 __acpi_register_gsi = acpi_register_gsi_xen;
411
412 return 0;
413}
414
415void __init xen_setup_pirqs(void)
416{
417 int irq;
418
419 pci_xen_initial_domain();
420
421 if (0 == nr_ioapics) {
422 for (irq = 0; irq < NR_IRQS_LEGACY; irq++)
423 xen_allocate_pirq(irq, 0, "xt-pic");
424 return;
425 }
426
427 /* Pre-allocate legacy irqs */
428 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
429 int trigger, polarity;
430
431 if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
432 continue;
433
434 xen_register_pirq(irq,
435 trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE);
436 }
437}
438#endif