plpar_pcibus.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2011 Nathan Whitehorn
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <sys/param.h>
  31. #include <sys/bus.h>
  32. #include <sys/kernel.h>
  33. #include <sys/libkern.h>
  34. #include <sys/module.h>
  35. #include <sys/pciio.h>
  36. #include <dev/ofw/openfirm.h>
  37. #include <dev/pci/pcivar.h>
  38. #include <dev/pci/pcireg.h>
  39. #include <dev/pci/pci_private.h>
  40. #include <machine/bus.h>
  41. #include <machine/rtas.h>
  42. #include <powerpc/ofw/ofw_pcibus.h>
  43. #include <powerpc/pseries/plpar_iommu.h>
  44. #include "pci_if.h"
  45. #include "iommu_if.h"
  46. static int plpar_pcibus_probe(device_t);
  47. static bus_dma_tag_t plpar_pcibus_get_dma_tag(device_t dev, device_t child);
  48. /*
  49. * Driver methods.
  50. */
  51. static device_method_t plpar_pcibus_methods[] = {
  52. /* Device interface */
  53. DEVMETHOD(device_probe, plpar_pcibus_probe),
  54. /* IOMMU functions */
  55. DEVMETHOD(bus_get_dma_tag, plpar_pcibus_get_dma_tag),
  56. DEVMETHOD(iommu_map, phyp_iommu_map),
  57. DEVMETHOD(iommu_unmap, phyp_iommu_unmap),
  58. DEVMETHOD_END
  59. };
  60. static devclass_t pci_devclass;
  61. DEFINE_CLASS_1(pci, plpar_pcibus_driver, plpar_pcibus_methods,
  62. sizeof(struct pci_softc), ofw_pcibus_driver);
  63. DRIVER_MODULE(plpar_pcibus, pcib, plpar_pcibus_driver, pci_devclass, 0, 0);
  64. static int
  65. plpar_pcibus_probe(device_t dev)
  66. {
  67. phandle_t rtas;
  68. if (ofw_bus_get_node(dev) == -1 || !rtas_exists())
  69. return (ENXIO);
  70. rtas = OF_finddevice("/rtas");
  71. if (!OF_hasprop(rtas, "ibm,hypertas-functions"))
  72. return (ENXIO);
  73. device_set_desc(dev, "POWER Hypervisor PCI bus");
  74. return (BUS_PROBE_SPECIFIC);
  75. }
  76. static bus_dma_tag_t
  77. plpar_pcibus_get_dma_tag(device_t dev, device_t child)
  78. {
  79. struct ofw_pcibus_devinfo *dinfo;
  80. while (device_get_parent(child) != dev)
  81. child = device_get_parent(child);
  82. dinfo = device_get_ivars(child);
  83. if (dinfo->opd_dma_tag != NULL)
  84. return (dinfo->opd_dma_tag);
  85. bus_dma_tag_create(bus_get_dma_tag(dev),
  86. 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
  87. NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
  88. BUS_SPACE_MAXSIZE, 0, NULL, NULL, &dinfo->opd_dma_tag);
  89. phyp_iommu_set_dma_tag(dev, child, dinfo->opd_dma_tag);
  90. return (dinfo->opd_dma_tag);
  91. }