xen.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  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. #include <linux/io.h>
  12. #include <asm/pci_x86.h>
  13. #include <asm/xen/hypervisor.h>
  14. #include <xen/events.h>
  15. #include <asm/xen/pci.h>
  16. #ifdef CONFIG_ACPI
  17. static int xen_hvm_register_pirq(u32 gsi, int triggering)
  18. {
  19. int rc, irq;
  20. struct physdev_map_pirq map_irq;
  21. int shareable = 0;
  22. char *name;
  23. if (!xen_hvm_domain())
  24. return -1;
  25. map_irq.domid = DOMID_SELF;
  26. map_irq.type = MAP_PIRQ_TYPE_GSI;
  27. map_irq.index = gsi;
  28. map_irq.pirq = -1;
  29. rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
  30. if (rc) {
  31. printk(KERN_WARNING "xen map irq failed %d\n", rc);
  32. return -1;
  33. }
  34. if (triggering == ACPI_EDGE_SENSITIVE) {
  35. shareable = 0;
  36. name = "ioapic-edge";
  37. } else {
  38. shareable = 1;
  39. name = "ioapic-level";
  40. }
  41. irq = xen_map_pirq_gsi(map_irq.pirq, gsi, shareable, name);
  42. printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
  43. return irq;
  44. }
  45. #endif
  46. #if defined(CONFIG_PCI_MSI)
  47. #include <linux/msi.h>
  48. struct xen_pci_frontend_ops *xen_pci_frontend;
  49. EXPORT_SYMBOL_GPL(xen_pci_frontend);
  50. /*
  51. * For MSI interrupts we have to use drivers/xen/event.s functions to
  52. * allocate an irq_desc and setup the right */
  53. static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  54. {
  55. int irq, ret, i;
  56. struct msi_desc *msidesc;
  57. int *v;
  58. v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
  59. if (!v)
  60. return -ENOMEM;
  61. if (!xen_initial_domain()) {
  62. if (type == PCI_CAP_ID_MSIX)
  63. ret = xen_pci_frontend_enable_msix(dev, &v, nvec);
  64. else
  65. ret = xen_pci_frontend_enable_msi(dev, &v);
  66. if (ret)
  67. goto error;
  68. }
  69. i = 0;
  70. list_for_each_entry(msidesc, &dev->msi_list, list) {
  71. irq = xen_allocate_pirq(v[i], 0, /* not sharable */
  72. (type == PCI_CAP_ID_MSIX) ?
  73. "pcifront-msi-x" : "pcifront-msi");
  74. if (irq < 0)
  75. return -1;
  76. ret = set_irq_msi(irq, msidesc);
  77. if (ret)
  78. goto error_while;
  79. i++;
  80. }
  81. kfree(v);
  82. return 0;
  83. error_while:
  84. unbind_from_irqhandler(irq, NULL);
  85. error:
  86. if (ret == -ENODEV)
  87. dev_err(&dev->dev, "Xen PCI frontend has not registered" \
  88. " MSI/MSI-X support!\n");
  89. kfree(v);
  90. return ret;
  91. }
  92. static void xen_teardown_msi_irqs(struct pci_dev *dev)
  93. {
  94. /* Only do this when were are in non-privileged mode.*/
  95. if (!xen_initial_domain()) {
  96. struct msi_desc *msidesc;
  97. msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
  98. if (msidesc->msi_attrib.is_msix)
  99. xen_pci_frontend_disable_msix(dev);
  100. else
  101. xen_pci_frontend_disable_msi(dev);
  102. }
  103. }
  104. static void xen_teardown_msi_irq(unsigned int irq)
  105. {
  106. xen_destroy_irq(irq);
  107. }
  108. #endif
  109. static int xen_pcifront_enable_irq(struct pci_dev *dev)
  110. {
  111. int rc;
  112. int share = 1;
  113. dev_info(&dev->dev, "Xen PCI enabling IRQ: %d\n", dev->irq);
  114. if (dev->irq < 0)
  115. return -EINVAL;
  116. if (dev->irq < NR_IRQS_LEGACY)
  117. share = 0;
  118. rc = xen_allocate_pirq(dev->irq, share, "pcifront");
  119. if (rc < 0) {
  120. dev_warn(&dev->dev, "Xen PCI IRQ: %d, failed to register:%d\n",
  121. dev->irq, rc);
  122. return rc;
  123. }
  124. return 0;
  125. }
  126. int __init pci_xen_init(void)
  127. {
  128. if (!xen_pv_domain() || xen_initial_domain())
  129. return -ENODEV;
  130. printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
  131. pcibios_set_cache_line_size();
  132. pcibios_enable_irq = xen_pcifront_enable_irq;
  133. pcibios_disable_irq = NULL;
  134. #ifdef CONFIG_ACPI
  135. /* Keep ACPI out of the picture */
  136. acpi_noirq = 1;
  137. #endif
  138. #ifdef CONFIG_PCI_MSI
  139. x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
  140. x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
  141. x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
  142. #endif
  143. return 0;
  144. }