qpi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2010 Hudson River Trading LLC
  5. * Written by: John H. Baldwin <jhb@FreeBSD.org>
  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. /*
  30. * This driver provides a pseudo-bus to enumerate the PCI buses
  31. * present on a system using a QPI chipset. It creates a qpi0 bus that
  32. * is a child of nexus0 and then creates Host-PCI bridges as a
  33. * child of that.
  34. */
  35. #include <sys/cdefs.h>
  36. __FBSDID("$FreeBSD$");
  37. #include <sys/param.h>
  38. #include <sys/bus.h>
  39. #include <sys/kernel.h>
  40. #include <sys/malloc.h>
  41. #include <sys/module.h>
  42. #include <sys/rman.h>
  43. #include <sys/systm.h>
  44. #include <machine/cputypes.h>
  45. #include <machine/md_var.h>
  46. #include <x86/legacyvar.h>
  47. #include <x86/pci_cfgreg.h>
  48. #include <x86/specialreg.h>
  49. #include <dev/pci/pcireg.h>
  50. #include <dev/pci/pcivar.h>
  51. #include <dev/pci/pcib_private.h>
  52. #include "pcib_if.h"
  53. struct qpi_device {
  54. int qd_pcibus;
  55. };
  56. static MALLOC_DEFINE(M_QPI, "qpidrv", "qpi system device");
  57. static void
  58. qpi_identify(driver_t *driver, device_t parent)
  59. {
  60. int do_qpi;
  61. /* Check CPUID to ensure this is an i7 CPU of some sort. */
  62. if (cpu_vendor_id != CPU_VENDOR_INTEL ||
  63. CPUID_TO_FAMILY(cpu_id) != 0x6)
  64. return;
  65. /* Only discover buses with configuration devices if allowed by user */
  66. do_qpi = 0;
  67. TUNABLE_INT_FETCH("hw.attach_intel_csr_pci", &do_qpi);
  68. if (!do_qpi)
  69. return;
  70. /* PCI config register access is required. */
  71. if (pci_cfgregopen() == 0)
  72. return;
  73. /* Add a qpi bus device. */
  74. if (BUS_ADD_CHILD(parent, 20, "qpi", -1) == NULL)
  75. panic("Failed to add qpi bus");
  76. }
  77. static int
  78. qpi_probe(device_t dev)
  79. {
  80. device_set_desc(dev, "QPI system bus");
  81. return (BUS_PROBE_SPECIFIC);
  82. }
  83. /*
  84. * Look for a PCI bus with the specified bus address. If one is found,
  85. * add a pcib device and return 0. Otherwise, return an error code.
  86. */
  87. static int
  88. qpi_probe_pcib(device_t dev, int bus)
  89. {
  90. struct qpi_device *qdev;
  91. device_t child;
  92. uint32_t devid;
  93. int s;
  94. /*
  95. * If a PCI bus already exists for this bus number, then
  96. * fail.
  97. */
  98. if (pci_find_bsf(bus, 0, 0) != NULL)
  99. return (EEXIST);
  100. /*
  101. * Attempt to read the device id for every slot, function 0 on
  102. * the bus. If all read values are 0xffffffff this means that
  103. * the bus is not present.
  104. */
  105. for (s = 0; s <= PCI_SLOTMAX; s++) {
  106. devid = pci_cfgregread(bus, s, 0, PCIR_DEVVENDOR, 4);
  107. if (devid != 0xffffffff)
  108. break;
  109. }
  110. if (devid == 0xffffffff)
  111. return (ENOENT);
  112. if ((devid & 0xffff) != 0x8086) {
  113. if (bootverbose)
  114. device_printf(dev,
  115. "Device at pci%d.%d.0 has non-Intel vendor 0x%x\n",
  116. bus, s, devid & 0xffff);
  117. return (ENXIO);
  118. }
  119. child = BUS_ADD_CHILD(dev, 0, "pcib", -1);
  120. if (child == NULL)
  121. panic("%s: failed to add pci bus %d", device_get_nameunit(dev),
  122. bus);
  123. qdev = malloc(sizeof(struct qpi_device), M_QPI, M_WAITOK);
  124. qdev->qd_pcibus = bus;
  125. device_set_ivars(child, qdev);
  126. return (0);
  127. }
  128. static int
  129. qpi_attach(device_t dev)
  130. {
  131. int bus;
  132. /*
  133. * Each processor socket has a dedicated PCI bus, sometimes
  134. * not enumerated by ACPI. Probe all unattached buses from 0
  135. * to 255.
  136. */
  137. for (bus = PCI_BUSMAX; bus >= 0; bus--)
  138. qpi_probe_pcib(dev, bus);
  139. return (bus_generic_attach(dev));
  140. }
  141. static int
  142. qpi_print_child(device_t bus, device_t child)
  143. {
  144. struct qpi_device *qdev;
  145. int retval = 0;
  146. qdev = device_get_ivars(child);
  147. retval += bus_print_child_header(bus, child);
  148. if (qdev->qd_pcibus != -1)
  149. retval += printf(" pcibus %d", qdev->qd_pcibus);
  150. retval += bus_print_child_footer(bus, child);
  151. return (retval);
  152. }
  153. static int
  154. qpi_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
  155. {
  156. struct qpi_device *qdev;
  157. qdev = device_get_ivars(child);
  158. switch (which) {
  159. case PCIB_IVAR_BUS:
  160. *result = qdev->qd_pcibus;
  161. break;
  162. default:
  163. return (ENOENT);
  164. }
  165. return (0);
  166. }
  167. static device_method_t qpi_methods[] = {
  168. /* Device interface */
  169. DEVMETHOD(device_identify, qpi_identify),
  170. DEVMETHOD(device_probe, qpi_probe),
  171. DEVMETHOD(device_attach, qpi_attach),
  172. DEVMETHOD(device_shutdown, bus_generic_shutdown),
  173. DEVMETHOD(device_suspend, bus_generic_suspend),
  174. DEVMETHOD(device_resume, bus_generic_resume),
  175. /* Bus interface */
  176. DEVMETHOD(bus_print_child, qpi_print_child),
  177. DEVMETHOD(bus_add_child, bus_generic_add_child),
  178. DEVMETHOD(bus_read_ivar, qpi_read_ivar),
  179. DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
  180. DEVMETHOD(bus_release_resource, bus_generic_release_resource),
  181. DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
  182. DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
  183. DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
  184. DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
  185. { 0, 0 }
  186. };
  187. static devclass_t qpi_devclass;
  188. DEFINE_CLASS_0(qpi, qpi_driver, qpi_methods, 0);
  189. DRIVER_MODULE(qpi, nexus, qpi_driver, qpi_devclass, 0, 0);
  190. static int
  191. qpi_pcib_probe(device_t dev)
  192. {
  193. device_set_desc(dev, "QPI Host-PCI bridge");
  194. return (BUS_PROBE_SPECIFIC);
  195. }
  196. static int
  197. qpi_pcib_attach(device_t dev)
  198. {
  199. device_add_child(dev, "pci", -1);
  200. return (bus_generic_attach(dev));
  201. }
  202. static int
  203. qpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
  204. {
  205. switch (which) {
  206. case PCIB_IVAR_DOMAIN:
  207. *result = 0;
  208. return (0);
  209. case PCIB_IVAR_BUS:
  210. *result = pcib_get_bus(dev);
  211. return (0);
  212. default:
  213. return (ENOENT);
  214. }
  215. }
  216. #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
  217. static struct resource *
  218. qpi_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
  219. rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
  220. {
  221. if (type == PCI_RES_BUS)
  222. return (pci_domain_alloc_bus(0, child, rid, start, end, count,
  223. flags));
  224. return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
  225. count, flags));
  226. }
  227. #endif
  228. static int
  229. qpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
  230. uint32_t *data)
  231. {
  232. device_t bus;
  233. bus = device_get_parent(pcib);
  234. return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
  235. }
  236. static device_method_t qpi_pcib_methods[] = {
  237. /* Device interface */
  238. DEVMETHOD(device_probe, qpi_pcib_probe),
  239. DEVMETHOD(device_attach, qpi_pcib_attach),
  240. DEVMETHOD(device_shutdown, bus_generic_shutdown),
  241. DEVMETHOD(device_suspend, bus_generic_suspend),
  242. DEVMETHOD(device_resume, bus_generic_resume),
  243. /* Bus interface */
  244. DEVMETHOD(bus_read_ivar, qpi_pcib_read_ivar),
  245. #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
  246. DEVMETHOD(bus_alloc_resource, qpi_pcib_alloc_resource),
  247. DEVMETHOD(bus_adjust_resource, legacy_pcib_adjust_resource),
  248. DEVMETHOD(bus_release_resource, legacy_pcib_release_resource),
  249. #else
  250. DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
  251. DEVMETHOD(bus_release_resource, bus_generic_release_resource),
  252. #endif
  253. DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
  254. DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
  255. DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
  256. DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
  257. /* pcib interface */
  258. DEVMETHOD(pcib_maxslots, pcib_maxslots),
  259. DEVMETHOD(pcib_read_config, legacy_pcib_read_config),
  260. DEVMETHOD(pcib_write_config, legacy_pcib_write_config),
  261. DEVMETHOD(pcib_alloc_msi, legacy_pcib_alloc_msi),
  262. DEVMETHOD(pcib_release_msi, pcib_release_msi),
  263. DEVMETHOD(pcib_alloc_msix, legacy_pcib_alloc_msix),
  264. DEVMETHOD(pcib_release_msix, pcib_release_msix),
  265. DEVMETHOD(pcib_map_msi, qpi_pcib_map_msi),
  266. DEVMETHOD_END
  267. };
  268. static devclass_t qpi_pcib_devclass;
  269. DEFINE_CLASS_0(pcib, qpi_pcib_driver, qpi_pcib_methods, 0);
  270. DRIVER_MODULE(pcib, qpi, qpi_pcib_driver, qpi_pcib_devclass, 0, 0);