blob: 93e42152d8d09e9a4331b3acb2fb680165c461f0 [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
Ian Campbell9a626612011-02-18 16:43:30 +000023static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi,
24 int trigger, int polarity)
Stefano Stabellini42a1de52010-06-24 16:42:04 +010025{
26 int rc, irq;
27 struct physdev_map_pirq map_irq;
28 int shareable = 0;
29 char *name;
30
31 if (!xen_hvm_domain())
32 return -1;
33
34 map_irq.domid = DOMID_SELF;
35 map_irq.type = MAP_PIRQ_TYPE_GSI;
36 map_irq.index = gsi;
37 map_irq.pirq = -1;
38
39 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
40 if (rc) {
41 printk(KERN_WARNING "xen map irq failed %d\n", rc);
42 return -1;
43 }
44
Ian Campbell9a626612011-02-18 16:43:30 +000045 if (trigger == ACPI_EDGE_SENSITIVE) {
Stefano Stabellini42a1de52010-06-24 16:42:04 +010046 shareable = 0;
47 name = "ioapic-edge";
48 } else {
49 shareable = 1;
50 name = "ioapic-level";
51 }
52
53 irq = xen_map_pirq_gsi(map_irq.pirq, gsi, shareable, name);
54
55 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
56
57 return irq;
58}
59#endif
60
Alex Nixonb5401a92010-03-18 16:31:34 -040061#if defined(CONFIG_PCI_MSI)
62#include <linux/msi.h>
Stefano Stabellini809f9262010-07-01 17:10:39 +010063#include <asm/msidef.h>
Alex Nixonb5401a92010-03-18 16:31:34 -040064
65struct xen_pci_frontend_ops *xen_pci_frontend;
66EXPORT_SYMBOL_GPL(xen_pci_frontend);
67
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000068#define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
69 MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
70
Stefano Stabellini809f9262010-07-01 17:10:39 +010071static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
72 struct msi_msg *msg)
73{
74 /* We set vector == 0 to tell the hypervisor we don't care about it,
75 * but we want a pirq setup instead.
76 * We use the dest_id field to pass the pirq that we want. */
77 msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq);
78 msg->address_lo =
79 MSI_ADDR_BASE_LO |
80 MSI_ADDR_DEST_MODE_PHYSICAL |
81 MSI_ADDR_REDIRECTION_CPU |
82 MSI_ADDR_DEST_ID(pirq);
83
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000084 msg->data = XEN_PIRQ_MSI_DATA;
Stefano Stabellini809f9262010-07-01 17:10:39 +010085}
86
87static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
88{
Ian Campbellbf480d92011-02-18 16:43:32 +000089 int irq, pirq;
Stefano Stabellini809f9262010-07-01 17:10:39 +010090 struct msi_desc *msidesc;
91 struct msi_msg msg;
92
93 list_for_each_entry(msidesc, &dev->msi_list, list) {
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000094 __read_msi_msg(msidesc, &msg);
95 pirq = MSI_ADDR_EXT_DEST_ID(msg.address_hi) |
96 ((msg.address_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xff);
Ian Campbellbf480d92011-02-18 16:43:32 +000097 if (msg.data != XEN_PIRQ_MSI_DATA ||
98 xen_irq_from_pirq(pirq) < 0) {
99 pirq = xen_allocate_pirq_msi(dev, msidesc);
100 if (pirq < 0)
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000101 goto error;
Ian Campbellbf480d92011-02-18 16:43:32 +0000102 xen_msi_compose_msg(dev, pirq, &msg);
103 __write_msi_msg(msidesc, &msg);
104 dev_dbg(&dev->dev, "xen: msi bound to pirq=%d\n", pirq);
105 } else {
106 dev_dbg(&dev->dev,
107 "xen: msi already bound to pirq=%d\n", pirq);
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000108 }
Ian Campbellbf480d92011-02-18 16:43:32 +0000109 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq,
110 (type == PCI_CAP_ID_MSIX) ?
111 "msi-x" : "msi");
112 if (irq < 0)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100113 goto error;
Ian Campbellbf480d92011-02-18 16:43:32 +0000114 dev_dbg(&dev->dev,
115 "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100116 }
117 return 0;
118
Stefano Stabellini809f9262010-07-01 17:10:39 +0100119error:
Ian Campbellbf480d92011-02-18 16:43:32 +0000120 dev_err(&dev->dev,
121 "Xen PCI frontend has not registered MSI/MSI-X support!\n");
122 return -ENODEV;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100123}
124
Alex Nixonb5401a92010-03-18 16:31:34 -0400125/*
126 * For MSI interrupts we have to use drivers/xen/event.s functions to
127 * allocate an irq_desc and setup the right */
128
129
130static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
131{
132 int irq, ret, i;
133 struct msi_desc *msidesc;
134 int *v;
135
136 v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
137 if (!v)
138 return -ENOMEM;
139
Qing Hef731e3ef2010-10-11 15:30:09 +0100140 if (type == PCI_CAP_ID_MSIX)
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500141 ret = xen_pci_frontend_enable_msix(dev, v, nvec);
Qing Hef731e3ef2010-10-11 15:30:09 +0100142 else
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500143 ret = xen_pci_frontend_enable_msi(dev, v);
Qing Hef731e3ef2010-10-11 15:30:09 +0100144 if (ret)
145 goto error;
Alex Nixonb5401a92010-03-18 16:31:34 -0400146 i = 0;
147 list_for_each_entry(msidesc, &dev->msi_list, list) {
Ian Campbellbf480d92011-02-18 16:43:32 +0000148 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i],
149 (type == PCI_CAP_ID_MSIX) ?
150 "pcifront-msi-x" :
151 "pcifront-msi");
152 if (irq < 0)
Jiri Slaby07cf2a62010-11-06 10:06:49 +0100153 goto free;
Alex Nixonb5401a92010-03-18 16:31:34 -0400154 i++;
155 }
156 kfree(v);
157 return 0;
158
Alex Nixonb5401a92010-03-18 16:31:34 -0400159error:
Ian Campbellbf480d92011-02-18 16:43:32 +0000160 dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n");
Jiri Slaby07cf2a62010-11-06 10:06:49 +0100161free:
Alex Nixonb5401a92010-03-18 16:31:34 -0400162 kfree(v);
163 return ret;
164}
165
166static void xen_teardown_msi_irqs(struct pci_dev *dev)
167{
Qing Hef731e3ef2010-10-11 15:30:09 +0100168 struct msi_desc *msidesc;
Alex Nixonb5401a92010-03-18 16:31:34 -0400169
Qing Hef731e3ef2010-10-11 15:30:09 +0100170 msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
171 if (msidesc->msi_attrib.is_msix)
172 xen_pci_frontend_disable_msix(dev);
173 else
174 xen_pci_frontend_disable_msi(dev);
Konrad Rzeszutek Wilk3d74a532011-02-17 16:12:51 -0500175
176 /* Free the IRQ's and the msidesc using the generic code. */
177 default_teardown_msi_irqs(dev);
Alex Nixonb5401a92010-03-18 16:31:34 -0400178}
179
180static void xen_teardown_msi_irq(unsigned int irq)
181{
182 xen_destroy_irq(irq);
183}
Qing Hef731e3ef2010-10-11 15:30:09 +0100184
Ian Campbell260a7d42011-02-18 16:43:26 +0000185#ifdef CONFIG_XEN_DOM0
Qing Hef731e3ef2010-10-11 15:30:09 +0100186static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
187{
188 int irq, ret;
189 struct msi_desc *msidesc;
190
191 list_for_each_entry(msidesc, &dev->msi_list, list) {
192 irq = xen_create_msi_irq(dev, msidesc, type);
193 if (irq < 0)
194 return -1;
195
196 ret = set_irq_msi(irq, msidesc);
197 if (ret)
198 goto error;
199 }
200 return 0;
201
202error:
203 xen_destroy_irq(irq);
204 return ret;
205}
Alex Nixonb5401a92010-03-18 16:31:34 -0400206#endif
Ian Campbell260a7d42011-02-18 16:43:26 +0000207#endif
Alex Nixonb5401a92010-03-18 16:31:34 -0400208
209static int xen_pcifront_enable_irq(struct pci_dev *dev)
210{
211 int rc;
212 int share = 1;
Ian Campbell3f2a2302011-01-11 17:20:13 +0000213 u8 gsi;
Alex Nixonb5401a92010-03-18 16:31:34 -0400214
Ian Campbell3f2a2302011-01-11 17:20:13 +0000215 rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
Alex Nixonb5401a92010-03-18 16:31:34 -0400216 if (rc < 0) {
Ian Campbell3f2a2302011-01-11 17:20:13 +0000217 dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
218 rc);
Alex Nixonb5401a92010-03-18 16:31:34 -0400219 return rc;
220 }
Ian Campbell3f2a2302011-01-11 17:20:13 +0000221
222 if (gsi < NR_IRQS_LEGACY)
223 share = 0;
224
225 rc = xen_allocate_pirq(gsi, share, "pcifront");
226 if (rc < 0) {
227 dev_warn(&dev->dev, "Xen PCI: failed to register GSI%d: %d\n",
228 gsi, rc);
229 return rc;
230 }
231
232 dev->irq = rc;
233 dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400234 return 0;
235}
236
237int __init pci_xen_init(void)
238{
239 if (!xen_pv_domain() || xen_initial_domain())
240 return -ENODEV;
241
242 printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
243
244 pcibios_set_cache_line_size();
245
246 pcibios_enable_irq = xen_pcifront_enable_irq;
247 pcibios_disable_irq = NULL;
248
249#ifdef CONFIG_ACPI
250 /* Keep ACPI out of the picture */
251 acpi_noirq = 1;
252#endif
253
Alex Nixonb5401a92010-03-18 16:31:34 -0400254#ifdef CONFIG_PCI_MSI
255 x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
256 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
257 x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
258#endif
259 return 0;
260}
Stefano Stabellini3942b742010-06-24 17:50:18 +0100261
262int __init pci_xen_hvm_init(void)
263{
264 if (!xen_feature(XENFEAT_hvm_pirqs))
265 return 0;
266
267#ifdef CONFIG_ACPI
268 /*
269 * We don't want to change the actual ACPI delivery model,
270 * just how GSIs get registered.
271 */
272 __acpi_register_gsi = acpi_register_gsi_xen_hvm;
273#endif
Stefano Stabellini809f9262010-07-01 17:10:39 +0100274
275#ifdef CONFIG_PCI_MSI
276 x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
277 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
278#endif
Stefano Stabellini3942b742010-06-24 17:50:18 +0100279 return 0;
280}
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100281
282#ifdef CONFIG_XEN_DOM0
283static int xen_register_pirq(u32 gsi, int triggering)
284{
285 int rc, irq;
286 struct physdev_map_pirq map_irq;
287 int shareable = 0;
288 char *name;
289
290 if (!xen_pv_domain())
291 return -1;
292
293 if (triggering == ACPI_EDGE_SENSITIVE) {
294 shareable = 0;
295 name = "ioapic-edge";
296 } else {
297 shareable = 1;
298 name = "ioapic-level";
299 }
300
301 irq = xen_allocate_pirq(gsi, shareable, name);
302
303 printk(KERN_DEBUG "xen: --> irq=%d\n", irq);
304
305 if (irq < 0)
306 goto out;
307
308 map_irq.domid = DOMID_SELF;
309 map_irq.type = MAP_PIRQ_TYPE_GSI;
310 map_irq.index = gsi;
311 map_irq.pirq = irq;
312
313 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
314 if (rc) {
315 printk(KERN_WARNING "xen map irq failed %d\n", rc);
316 return -1;
317 }
318
319out:
320 return irq;
321}
322
323static int xen_register_gsi(u32 gsi, int triggering, int polarity)
324{
325 int rc, irq;
326 struct physdev_setup_gsi setup_gsi;
327
328 if (!xen_pv_domain())
329 return -1;
330
331 printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
332 gsi, triggering, polarity);
333
334 irq = xen_register_pirq(gsi, triggering);
335
336 setup_gsi.gsi = gsi;
337 setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
338 setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
339
340 rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
341 if (rc == -EEXIST)
342 printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
343 else if (rc) {
344 printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
345 gsi, rc);
346 }
347
348 return irq;
349}
350
351static __init void xen_setup_acpi_sci(void)
352{
353 int rc;
354 int trigger, polarity;
355 int gsi = acpi_sci_override_gsi;
356
357 if (!gsi)
358 return;
359
360 rc = acpi_get_override_irq(gsi, &trigger, &polarity);
361 if (rc) {
362 printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi"
363 " sci, rc=%d\n", rc);
364 return;
365 }
366 trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
367 polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
368
369 printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d "
370 "polarity=%d\n", gsi, trigger, polarity);
371
372 gsi = xen_register_gsi(gsi, trigger, polarity);
373 printk(KERN_INFO "xen: acpi sci %d\n", gsi);
374
375 return;
376}
377
378static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
379 int trigger, int polarity)
380{
381 return xen_register_gsi(gsi, trigger, polarity);
382}
383
384static int __init pci_xen_initial_domain(void)
385{
Qing Hef731e3ef2010-10-11 15:30:09 +0100386#ifdef CONFIG_PCI_MSI
387 x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
388 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
389#endif
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100390 xen_setup_acpi_sci();
391 __acpi_register_gsi = acpi_register_gsi_xen;
392
393 return 0;
394}
395
396void __init xen_setup_pirqs(void)
397{
398 int irq;
399
400 pci_xen_initial_domain();
401
402 if (0 == nr_ioapics) {
403 for (irq = 0; irq < NR_IRQS_LEGACY; irq++)
404 xen_allocate_pirq(irq, 0, "xt-pic");
405 return;
406 }
407
408 /* Pre-allocate legacy irqs */
409 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
410 int trigger, polarity;
411
412 if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
413 continue;
414
415 xen_register_pirq(irq,
416 trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE);
417 }
418}
419#endif