xen_nexus.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2013 Roger Pau Monné <roger.pau@citrix.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #include <sys/cdefs.h>
  27. __FBSDID("$FreeBSD$");
  28. #include <sys/param.h>
  29. #include <sys/bus.h>
  30. #include <sys/kernel.h>
  31. #include <sys/module.h>
  32. #include <sys/sysctl.h>
  33. #include <sys/systm.h>
  34. #include <sys/smp.h>
  35. #include <contrib/dev/acpica/include/acpi.h>
  36. #include <dev/acpica/acpivar.h>
  37. #include <x86/init.h>
  38. #include <machine/nexusvar.h>
  39. #include <machine/intr_machdep.h>
  40. #include <xen/xen-os.h>
  41. #include <xen/xen_intr.h>
  42. #include <xen/xen_msi.h>
  43. #include "pcib_if.h"
  44. /*
  45. * Xen nexus(4) driver.
  46. */
  47. static int
  48. nexus_xen_probe(device_t dev)
  49. {
  50. if (!xen_pv_domain())
  51. return (ENXIO);
  52. return (BUS_PROBE_SPECIFIC);
  53. }
  54. static int
  55. nexus_xen_attach(device_t dev)
  56. {
  57. int error;
  58. device_t acpi_dev = NULL;
  59. nexus_init_resources();
  60. bus_generic_probe(dev);
  61. if (xen_initial_domain()) {
  62. /* Disable some ACPI devices that are not usable by Dom0 */
  63. acpi_cpu_disabled = true;
  64. acpi_hpet_disabled = true;
  65. acpi_timer_disabled = true;
  66. acpi_dev = BUS_ADD_CHILD(dev, 10, "acpi", 0);
  67. if (acpi_dev == NULL)
  68. panic("Unable to add ACPI bus to Xen Dom0");
  69. }
  70. error = bus_generic_attach(dev);
  71. if (xen_initial_domain() && (error == 0))
  72. acpi_install_wakeup_handler(device_get_softc(acpi_dev));
  73. return (error);
  74. }
  75. static int
  76. nexus_xen_config_intr(device_t dev, int irq, enum intr_trigger trig,
  77. enum intr_polarity pol)
  78. {
  79. int ret;
  80. /*
  81. * ISA and PCI intline IRQs are not preregistered on Xen, so
  82. * intercept calls to configure those and register them on the fly.
  83. */
  84. if ((irq < first_msi_irq) && (intr_lookup_source(irq) == NULL)) {
  85. ret = xen_register_pirq(irq, trig, pol);
  86. if (ret != 0)
  87. return (ret);
  88. nexus_add_irq(irq);
  89. }
  90. return (intr_config_intr(irq, trig, pol));
  91. }
  92. static int
  93. nexus_xen_alloc_msix(device_t pcib, device_t dev, int *irq)
  94. {
  95. return (xen_msix_alloc(dev, irq));
  96. }
  97. static int
  98. nexus_xen_release_msix(device_t pcib, device_t dev, int irq)
  99. {
  100. return (xen_msix_release(irq));
  101. }
  102. static int
  103. nexus_xen_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
  104. {
  105. return (xen_msi_alloc(dev, count, maxcount, irqs));
  106. }
  107. static int
  108. nexus_xen_release_msi(device_t pcib, device_t dev, int count, int *irqs)
  109. {
  110. return (xen_msi_release(irqs, count));
  111. }
  112. static int
  113. nexus_xen_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
  114. {
  115. return (xen_msi_map(irq, addr, data));
  116. }
  117. static device_method_t nexus_xen_methods[] = {
  118. /* Device interface */
  119. DEVMETHOD(device_probe, nexus_xen_probe),
  120. DEVMETHOD(device_attach, nexus_xen_attach),
  121. /* INTR */
  122. DEVMETHOD(bus_config_intr, nexus_xen_config_intr),
  123. /* MSI */
  124. DEVMETHOD(pcib_alloc_msi, nexus_xen_alloc_msi),
  125. DEVMETHOD(pcib_release_msi, nexus_xen_release_msi),
  126. DEVMETHOD(pcib_alloc_msix, nexus_xen_alloc_msix),
  127. DEVMETHOD(pcib_release_msix, nexus_xen_release_msix),
  128. DEVMETHOD(pcib_map_msi, nexus_xen_map_msi),
  129. { 0, 0 }
  130. };
  131. DEFINE_CLASS_1(nexus, nexus_xen_driver, nexus_xen_methods, 1, nexus_driver);
  132. static devclass_t nexus_devclass;
  133. DRIVER_MODULE(nexus_xen, root, nexus_xen_driver, nexus_devclass, 0, 0);