2 * Xen PCI Frontend Stub - puts some "dummy" functions in to the Linux
3 * x86 PCI core to support the Xen PCI Frontend
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
7 #include <linux/module.h>
8 #include <linux/init.h>
10 #include <linux/acpi.h>
13 #include <asm/io_apic.h>
14 #include <asm/pci_x86.h>
16 #include <asm/xen/hypervisor.h>
18 #include <xen/features.h>
19 #include <xen/events.h>
20 #include <asm/xen/pci.h>
22 static int xen_pcifront_enable_irq(struct pci_dev *dev)
29 rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
31 dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
36 rc = xen_allocate_pirq_gsi(gsi);
38 dev_warn(&dev->dev, "Xen PCI: failed to allocate a PIRQ for GSI%d: %d\n",
44 if (gsi < NR_IRQS_LEGACY)
47 rc = xen_bind_pirq_gsi_to_irq(gsi, pirq, share, "pcifront");
49 dev_warn(&dev->dev, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n",
55 dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
60 static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi,
61 int trigger, int polarity)
64 struct physdev_map_pirq map_irq;
68 if (!xen_hvm_domain())
71 map_irq.domid = DOMID_SELF;
72 map_irq.type = MAP_PIRQ_TYPE_GSI;
76 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
78 printk(KERN_WARNING "xen map irq failed %d\n", rc);
82 if (trigger == ACPI_EDGE_SENSITIVE) {
87 name = "ioapic-level";
90 irq = xen_bind_pirq_gsi_to_irq(gsi, map_irq.pirq, shareable, name);
92 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
98 #ifdef CONFIG_XEN_DOM0
99 static int xen_register_pirq(u32 gsi, int gsi_override, int triggering)
101 int rc, pirq, irq = -1;
102 struct physdev_map_pirq map_irq;
106 if (!xen_pv_domain())
109 if (triggering == ACPI_EDGE_SENSITIVE) {
111 name = "ioapic-edge";
114 name = "ioapic-level";
116 pirq = xen_allocate_pirq_gsi(gsi);
120 if (gsi_override >= 0)
121 irq = xen_bind_pirq_gsi_to_irq(gsi_override, pirq, shareable, name);
123 irq = xen_bind_pirq_gsi_to_irq(gsi, pirq, shareable, name);
127 printk(KERN_DEBUG "xen: --> pirq=%d -> irq=%d (gsi=%d)\n", pirq, irq, gsi);
129 map_irq.domid = DOMID_SELF;
130 map_irq.type = MAP_PIRQ_TYPE_GSI;
134 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
136 printk(KERN_WARNING "xen map irq failed %d\n", rc);
144 static int xen_register_gsi(u32 gsi, int gsi_override, int triggering, int polarity)
147 struct physdev_setup_gsi setup_gsi;
149 if (!xen_pv_domain())
152 printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
153 gsi, triggering, polarity);
155 irq = xen_register_pirq(gsi, gsi_override, triggering);
158 setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
159 setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
161 rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
163 printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
165 printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
172 static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
173 int trigger, int polarity)
175 return xen_register_gsi(gsi, -1 /* no GSI override */, trigger, polarity);
179 #if defined(CONFIG_PCI_MSI)
180 #include <linux/msi.h>
181 #include <asm/msidef.h>
183 struct xen_pci_frontend_ops *xen_pci_frontend;
184 EXPORT_SYMBOL_GPL(xen_pci_frontend);
187 * For MSI interrupts we have to use drivers/xen/event.s functions to
188 * allocate an irq_desc and setup the right */
189 static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
192 struct msi_desc *msidesc;
195 v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
199 if (type == PCI_CAP_ID_MSIX)
200 ret = xen_pci_frontend_enable_msix(dev, v, nvec);
202 ret = xen_pci_frontend_enable_msi(dev, v);
206 list_for_each_entry(msidesc, &dev->msi_list, list) {
207 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i], 0,
208 (type == PCI_CAP_ID_MSIX) ?
220 dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n");
226 #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
227 MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
229 static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
232 /* We set vector == 0 to tell the hypervisor we don't care about it,
233 * but we want a pirq setup instead.
234 * We use the dest_id field to pass the pirq that we want. */
235 msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq);
238 MSI_ADDR_DEST_MODE_PHYSICAL |
239 MSI_ADDR_REDIRECTION_CPU |
240 MSI_ADDR_DEST_ID(pirq);
242 msg->data = XEN_PIRQ_MSI_DATA;
245 static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
248 struct msi_desc *msidesc;
251 list_for_each_entry(msidesc, &dev->msi_list, list) {
252 __read_msi_msg(msidesc, &msg);
253 pirq = MSI_ADDR_EXT_DEST_ID(msg.address_hi) |
254 ((msg.address_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xff);
255 if (msg.data != XEN_PIRQ_MSI_DATA ||
256 xen_irq_from_pirq(pirq) < 0) {
257 pirq = xen_allocate_pirq_msi(dev, msidesc);
260 xen_msi_compose_msg(dev, pirq, &msg);
261 __write_msi_msg(msidesc, &msg);
262 dev_dbg(&dev->dev, "xen: msi bound to pirq=%d\n", pirq);
265 "xen: msi already bound to pirq=%d\n", pirq);
267 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq, 0,
268 (type == PCI_CAP_ID_MSIX) ?
274 "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq);
280 "Xen PCI frontend has not registered MSI/MSI-X support!\n");
284 #ifdef CONFIG_XEN_DOM0
285 static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
288 struct msi_desc *msidesc;
290 list_for_each_entry(msidesc, &dev->msi_list, list) {
291 struct physdev_map_pirq map_irq;
294 domid = ret = xen_find_device_domain_owner(dev);
295 /* N.B. Casting int's -ENODEV to uint16_t results in 0xFFED,
296 * hence check ret value for < 0. */
300 memset(&map_irq, 0, sizeof(map_irq));
301 map_irq.domid = domid;
302 map_irq.type = MAP_PIRQ_TYPE_MSI;
305 map_irq.bus = dev->bus->number;
306 map_irq.devfn = dev->devfn;
308 if (type == PCI_CAP_ID_MSIX) {
310 u32 table_offset, bir;
312 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
314 pci_read_config_dword(dev, pos + PCI_MSIX_TABLE,
316 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
318 map_irq.table_base = pci_resource_start(dev, bir);
319 map_irq.entry_nr = msidesc->msi_attrib.entry_nr;
322 ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
324 dev_warn(&dev->dev, "xen map irq failed %d for %d domain\n",
329 ret = xen_bind_pirq_msi_to_irq(dev, msidesc,
330 map_irq.pirq, map_irq.index,
331 (type == PCI_CAP_ID_MSIX) ?
343 static void xen_teardown_msi_irqs(struct pci_dev *dev)
345 struct msi_desc *msidesc;
347 msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
348 if (msidesc->msi_attrib.is_msix)
349 xen_pci_frontend_disable_msix(dev);
351 xen_pci_frontend_disable_msi(dev);
353 /* Free the IRQ's and the msidesc using the generic code. */
354 default_teardown_msi_irqs(dev);
357 static void xen_teardown_msi_irq(unsigned int irq)
359 xen_destroy_irq(irq);
365 int __init pci_xen_init(void)
367 if (!xen_pv_domain() || xen_initial_domain())
370 printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
372 pcibios_set_cache_line_size();
374 pcibios_enable_irq = xen_pcifront_enable_irq;
375 pcibios_disable_irq = NULL;
378 /* Keep ACPI out of the picture */
382 #ifdef CONFIG_PCI_MSI
383 x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
384 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
385 x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
390 int __init pci_xen_hvm_init(void)
392 if (!xen_feature(XENFEAT_hvm_pirqs))
397 * We don't want to change the actual ACPI delivery model,
398 * just how GSIs get registered.
400 __acpi_register_gsi = acpi_register_gsi_xen_hvm;
403 #ifdef CONFIG_PCI_MSI
404 x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
405 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
410 #ifdef CONFIG_XEN_DOM0
411 static __init void xen_setup_acpi_sci(void)
414 int trigger, polarity;
415 int gsi = acpi_sci_override_gsi;
417 int gsi_override = -1;
422 rc = acpi_get_override_irq(gsi, &trigger, &polarity);
424 printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi"
425 " sci, rc=%d\n", rc);
428 trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
429 polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
431 printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d "
432 "polarity=%d\n", gsi, trigger, polarity);
434 /* Before we bind the GSI to a Linux IRQ, check whether
435 * we need to override it with bus_irq (IRQ) value. Usually for
436 * IRQs below IRQ_LEGACY_IRQ this holds IRQ == GSI, as so:
437 * ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 9 low level)
438 * but there are oddballs where the IRQ != GSI:
439 * ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 20 low level)
440 * which ends up being: gsi_to_irq[9] == 20
441 * (which is what acpi_gsi_to_irq ends up calling when starting the
442 * the ACPI interpreter and keels over since IRQ 9 has not been
443 * setup as we had setup IRQ 20 for it).
445 /* Check whether the GSI != IRQ */
446 if (acpi_gsi_to_irq(gsi, &irq) == 0) {
447 if (irq >= 0 && irq != gsi)
448 /* Bugger, we MUST have that IRQ. */
452 gsi = xen_register_gsi(gsi, gsi_override, trigger, polarity);
453 printk(KERN_INFO "xen: acpi sci %d\n", gsi);
458 static int __init pci_xen_initial_domain(void)
460 #ifdef CONFIG_PCI_MSI
461 x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
462 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
464 xen_setup_acpi_sci();
465 __acpi_register_gsi = acpi_register_gsi_xen;
470 void __init xen_setup_pirqs(void)
474 pci_xen_initial_domain();
476 if (0 == nr_ioapics) {
477 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
478 pirq = xen_allocate_pirq_gsi(irq);
480 "Could not allocate PIRQ for legacy interrupt\n"))
482 irq = xen_bind_pirq_gsi_to_irq(irq, pirq, 0, "xt-pic");
487 /* Pre-allocate legacy irqs */
488 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
489 int trigger, polarity;
491 if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
494 xen_register_pirq(irq, -1 /* no GSI override */,
495 trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE);
499 struct xen_device_domain_owner {
502 struct list_head list;
505 static DEFINE_SPINLOCK(dev_domain_list_spinlock);
506 static struct list_head dev_domain_list = LIST_HEAD_INIT(dev_domain_list);
508 static struct xen_device_domain_owner *find_device(struct pci_dev *dev)
510 struct xen_device_domain_owner *owner;
512 list_for_each_entry(owner, &dev_domain_list, list) {
513 if (owner->dev == dev)
519 int xen_find_device_domain_owner(struct pci_dev *dev)
521 struct xen_device_domain_owner *owner;
522 int domain = -ENODEV;
524 spin_lock(&dev_domain_list_spinlock);
525 owner = find_device(dev);
527 domain = owner->domain;
528 spin_unlock(&dev_domain_list_spinlock);
531 EXPORT_SYMBOL_GPL(xen_find_device_domain_owner);
533 int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain)
535 struct xen_device_domain_owner *owner;
537 owner = kzalloc(sizeof(struct xen_device_domain_owner), GFP_KERNEL);
541 spin_lock(&dev_domain_list_spinlock);
542 if (find_device(dev)) {
543 spin_unlock(&dev_domain_list_spinlock);
547 owner->domain = domain;
549 list_add_tail(&owner->list, &dev_domain_list);
550 spin_unlock(&dev_domain_list_spinlock);
553 EXPORT_SYMBOL_GPL(xen_register_device_domain_owner);
555 int xen_unregister_device_domain_owner(struct pci_dev *dev)
557 struct xen_device_domain_owner *owner;
559 spin_lock(&dev_domain_list_spinlock);
560 owner = find_device(dev);
562 spin_unlock(&dev_domain_list_spinlock);
565 list_del(&owner->list);
566 spin_unlock(&dev_domain_list_spinlock);
570 EXPORT_SYMBOL_GPL(xen_unregister_device_domain_owner);