ofw_pcib_pci.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2000 Michael Smith
  5. * Copyright (c) 2000 BSDi
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/module.h>
  34. #include <sys/bus.h>
  35. #include <sys/malloc.h>
  36. #include <sys/kernel.h>
  37. #include <dev/ofw/openfirm.h>
  38. #include <dev/ofw/ofw_pci.h>
  39. #include <dev/ofw/ofw_bus.h>
  40. #include <dev/ofw/ofw_bus_subr.h>
  41. #include <dev/pci/pcivar.h>
  42. #include <dev/pci/pcireg.h>
  43. #include <dev/pci/pcib_private.h>
  44. #include <machine/intr_machdep.h>
  45. #include "pcib_if.h"
  46. static int ofw_pcib_pci_probe(device_t bus);
  47. static int ofw_pcib_pci_attach(device_t bus);
  48. static phandle_t ofw_pcib_pci_get_node(device_t bus, device_t dev);
  49. static int ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev,
  50. int intpin);
  51. static device_method_t ofw_pcib_pci_methods[] = {
  52. /* Device interface */
  53. DEVMETHOD(device_probe, ofw_pcib_pci_probe),
  54. DEVMETHOD(device_attach, ofw_pcib_pci_attach),
  55. /* pcib interface */
  56. DEVMETHOD(pcib_route_interrupt, ofw_pcib_pci_route_interrupt),
  57. DEVMETHOD(pcib_request_feature, pcib_request_feature_allow),
  58. /* ofw_bus interface */
  59. DEVMETHOD(ofw_bus_get_node, ofw_pcib_pci_get_node),
  60. DEVMETHOD_END
  61. };
  62. static devclass_t pcib_devclass;
  63. struct ofw_pcib_softc {
  64. /*
  65. * This is here so that we can use pci bridge methods, too - the
  66. * generic routines only need the dev, secbus and subbus members
  67. * filled.
  68. */
  69. struct pcib_softc ops_pcib_sc;
  70. phandle_t ops_node;
  71. struct ofw_bus_iinfo ops_iinfo;
  72. };
  73. DEFINE_CLASS_1(pcib, ofw_pcib_pci_driver, ofw_pcib_pci_methods,
  74. sizeof(struct ofw_pcib_softc), pcib_driver);
  75. EARLY_DRIVER_MODULE(ofw_pcib, pci, ofw_pcib_pci_driver, pcib_devclass, 0, 0,
  76. BUS_PASS_BUS);
  77. static int
  78. ofw_pcib_pci_probe(device_t dev)
  79. {
  80. if ((pci_get_class(dev) != PCIC_BRIDGE) ||
  81. (pci_get_subclass(dev) != PCIS_BRIDGE_PCI)) {
  82. return (ENXIO);
  83. }
  84. if (ofw_bus_get_node(dev) == -1)
  85. return (ENXIO);
  86. device_set_desc(dev, "OFW PCI-PCI bridge");
  87. return (0);
  88. }
  89. static int
  90. ofw_pcib_pci_attach(device_t dev)
  91. {
  92. struct ofw_pcib_softc *sc;
  93. sc = device_get_softc(dev);
  94. sc->ops_pcib_sc.dev = dev;
  95. sc->ops_node = ofw_bus_get_node(dev);
  96. ofw_bus_setup_iinfo(sc->ops_node, &sc->ops_iinfo,
  97. sizeof(cell_t));
  98. pcib_attach_common(dev);
  99. return (pcib_attach_child(dev));
  100. }
  101. static phandle_t
  102. ofw_pcib_pci_get_node(device_t bridge, device_t dev)
  103. {
  104. /* We have only one child, the PCI bus, so pass it our node */
  105. return (ofw_bus_get_node(bridge));
  106. }
  107. static int
  108. ofw_pcib_pci_route_interrupt(device_t bridge, device_t dev, int intpin)
  109. {
  110. struct ofw_pcib_softc *sc;
  111. struct ofw_bus_iinfo *ii;
  112. struct ofw_pci_register reg;
  113. cell_t pintr, mintr[2];
  114. int intrcells;
  115. phandle_t iparent;
  116. sc = device_get_softc(bridge);
  117. ii = &sc->ops_iinfo;
  118. if (ii->opi_imapsz > 0) {
  119. pintr = intpin;
  120. /* Fabricate imap information if this isn't an OFW device */
  121. bzero(&reg, sizeof(reg));
  122. reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) |
  123. (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) |
  124. (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT);
  125. intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev), ii, &reg,
  126. sizeof(reg), &pintr, sizeof(pintr), mintr, sizeof(mintr),
  127. &iparent);
  128. if (intrcells) {
  129. /*
  130. * If we've found a mapping, return it and don't map
  131. * it again on higher levels - that causes problems
  132. * in some cases, and never seems to be required.
  133. */
  134. mintr[0] = ofw_bus_map_intr(dev, iparent, intrcells,
  135. mintr);
  136. return (mintr[0]);
  137. }
  138. } else if (intpin >= 1 && intpin <= 4) {
  139. /*
  140. * When an interrupt map is missing, we need to do the
  141. * standard PCI swizzle and continue mapping at the parent.
  142. */
  143. return (pcib_route_interrupt(bridge, dev, intpin));
  144. }
  145. return (PCIB_ROUTE_INTERRUPT(device_get_parent(device_get_parent(
  146. bridge)), bridge, intpin));
  147. }