blob: 2a12f3dbdd02c08745ef787968aa4b035d302e99 [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)
153 ret = xen_pci_frontend_enable_msix(dev, &v, nvec);
154 else
155 ret = xen_pci_frontend_enable_msi(dev, &v);
156 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) {
160 irq = xen_allocate_pirq(v[i], 0, /* not sharable */
161 (type == PCI_CAP_ID_MSIX) ?
162 "pcifront-msi-x" : "pcifront-msi");
Jiri Slaby07cf2a62010-11-06 10:06:49 +0100163 if (irq < 0) {
164 ret = -1;
165 goto free;
166 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400167
168 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);
Alex Nixonb5401a92010-03-18 16:31:34 -0400196}
197
198static void xen_teardown_msi_irq(unsigned int irq)
199{
200 xen_destroy_irq(irq);
201}
Qing Hef731e3ef2010-10-11 15:30:09 +0100202
203static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
204{
205 int irq, ret;
206 struct msi_desc *msidesc;
207
208 list_for_each_entry(msidesc, &dev->msi_list, list) {
209 irq = xen_create_msi_irq(dev, msidesc, type);
210 if (irq < 0)
211 return -1;
212
213 ret = set_irq_msi(irq, msidesc);
214 if (ret)
215 goto error;
216 }
217 return 0;
218
219error:
220 xen_destroy_irq(irq);
221 return ret;
222}
Alex Nixonb5401a92010-03-18 16:31:34 -0400223#endif
224
225static int xen_pcifront_enable_irq(struct pci_dev *dev)
226{
227 int rc;
228 int share = 1;
Ian Campbell3f2a2302011-01-11 17:20:13 +0000229 u8 gsi;
Alex Nixonb5401a92010-03-18 16:31:34 -0400230
Ian Campbell3f2a2302011-01-11 17:20:13 +0000231 rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
Alex Nixonb5401a92010-03-18 16:31:34 -0400232 if (rc < 0) {
Ian Campbell3f2a2302011-01-11 17:20:13 +0000233 dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
234 rc);
Alex Nixonb5401a92010-03-18 16:31:34 -0400235 return rc;
236 }
Ian Campbell3f2a2302011-01-11 17:20:13 +0000237
238 if (gsi < NR_IRQS_LEGACY)
239 share = 0;
240
241 rc = xen_allocate_pirq(gsi, share, "pcifront");
242 if (rc < 0) {
243 dev_warn(&dev->dev, "Xen PCI: failed to register GSI%d: %d\n",
244 gsi, rc);
245 return rc;
246 }
247
248 dev->irq = rc;
249 dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400250 return 0;
251}
252
253int __init pci_xen_init(void)
254{
255 if (!xen_pv_domain() || xen_initial_domain())
256 return -ENODEV;
257
258 printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
259
260 pcibios_set_cache_line_size();
261
262 pcibios_enable_irq = xen_pcifront_enable_irq;
263 pcibios_disable_irq = NULL;
264
265#ifdef CONFIG_ACPI
266 /* Keep ACPI out of the picture */
267 acpi_noirq = 1;
268#endif
269
Alex Nixonb5401a92010-03-18 16:31:34 -0400270#ifdef CONFIG_PCI_MSI
271 x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
272 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
273 x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
274#endif
275 return 0;
276}
Stefano Stabellini3942b742010-06-24 17:50:18 +0100277
278int __init pci_xen_hvm_init(void)
279{
280 if (!xen_feature(XENFEAT_hvm_pirqs))
281 return 0;
282
283#ifdef CONFIG_ACPI
284 /*
285 * We don't want to change the actual ACPI delivery model,
286 * just how GSIs get registered.
287 */
288 __acpi_register_gsi = acpi_register_gsi_xen_hvm;
289#endif
Stefano Stabellini809f9262010-07-01 17:10:39 +0100290
291#ifdef CONFIG_PCI_MSI
292 x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
293 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
294#endif
Stefano Stabellini3942b742010-06-24 17:50:18 +0100295 return 0;
296}
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100297
298#ifdef CONFIG_XEN_DOM0
299static int xen_register_pirq(u32 gsi, int triggering)
300{
301 int rc, irq;
302 struct physdev_map_pirq map_irq;
303 int shareable = 0;
304 char *name;
305
306 if (!xen_pv_domain())
307 return -1;
308
309 if (triggering == ACPI_EDGE_SENSITIVE) {
310 shareable = 0;
311 name = "ioapic-edge";
312 } else {
313 shareable = 1;
314 name = "ioapic-level";
315 }
316
317 irq = xen_allocate_pirq(gsi, shareable, name);
318
319 printk(KERN_DEBUG "xen: --> irq=%d\n", irq);
320
321 if (irq < 0)
322 goto out;
323
324 map_irq.domid = DOMID_SELF;
325 map_irq.type = MAP_PIRQ_TYPE_GSI;
326 map_irq.index = gsi;
327 map_irq.pirq = irq;
328
329 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
330 if (rc) {
331 printk(KERN_WARNING "xen map irq failed %d\n", rc);
332 return -1;
333 }
334
335out:
336 return irq;
337}
338
339static int xen_register_gsi(u32 gsi, int triggering, int polarity)
340{
341 int rc, irq;
342 struct physdev_setup_gsi setup_gsi;
343
344 if (!xen_pv_domain())
345 return -1;
346
347 printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
348 gsi, triggering, polarity);
349
350 irq = xen_register_pirq(gsi, triggering);
351
352 setup_gsi.gsi = gsi;
353 setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
354 setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
355
356 rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
357 if (rc == -EEXIST)
358 printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
359 else if (rc) {
360 printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
361 gsi, rc);
362 }
363
364 return irq;
365}
366
367static __init void xen_setup_acpi_sci(void)
368{
369 int rc;
370 int trigger, polarity;
371 int gsi = acpi_sci_override_gsi;
372
373 if (!gsi)
374 return;
375
376 rc = acpi_get_override_irq(gsi, &trigger, &polarity);
377 if (rc) {
378 printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi"
379 " sci, rc=%d\n", rc);
380 return;
381 }
382 trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
383 polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
384
385 printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d "
386 "polarity=%d\n", gsi, trigger, polarity);
387
388 gsi = xen_register_gsi(gsi, trigger, polarity);
389 printk(KERN_INFO "xen: acpi sci %d\n", gsi);
390
391 return;
392}
393
394static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
395 int trigger, int polarity)
396{
397 return xen_register_gsi(gsi, trigger, polarity);
398}
399
400static int __init pci_xen_initial_domain(void)
401{
Qing Hef731e3ef2010-10-11 15:30:09 +0100402#ifdef CONFIG_PCI_MSI
403 x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
404 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
405#endif
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100406 xen_setup_acpi_sci();
407 __acpi_register_gsi = acpi_register_gsi_xen;
408
409 return 0;
410}
411
412void __init xen_setup_pirqs(void)
413{
414 int irq;
415
416 pci_xen_initial_domain();
417
418 if (0 == nr_ioapics) {
419 for (irq = 0; irq < NR_IRQS_LEGACY; irq++)
420 xen_allocate_pirq(irq, 0, "xt-pic");
421 return;
422 }
423
424 /* Pre-allocate legacy irqs */
425 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
426 int trigger, polarity;
427
428 if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
429 continue;
430
431 xen_register_pirq(irq,
432 trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE);
433 }
434}
435#endif