port.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2022 Intel Corporation. All rights reserved. */
  3. #include <linux/device.h>
  4. #include <linux/module.h>
  5. #include <linux/slab.h>
  6. #include "cxlmem.h"
  7. #include "cxlpci.h"
  8. /**
  9. * DOC: cxl port
  10. *
  11. * The port driver enumerates dport via PCI and scans for HDM
  12. * (Host-managed-Device-Memory) decoder resources via the
  13. * @component_reg_phys value passed in by the agent that registered the
  14. * port. All descendant ports of a CXL root port (described by platform
  15. * firmware) are managed in this drivers context. Each driver instance
  16. * is responsible for tearing down the driver context of immediate
  17. * descendant ports. The locking for this is validated by
  18. * CONFIG_PROVE_CXL_LOCKING.
  19. *
  20. * The primary service this driver provides is presenting APIs to other
  21. * drivers to utilize the decoders, and indicating to userspace (via bind
  22. * status) the connectivity of the CXL.mem protocol throughout the
  23. * PCIe topology.
  24. */
  25. static void schedule_detach(void *cxlmd)
  26. {
  27. schedule_cxl_memdev_detach(cxlmd);
  28. }
  29. static int discover_region(struct device *dev, void *root)
  30. {
  31. struct cxl_endpoint_decoder *cxled;
  32. int rc;
  33. if (!is_endpoint_decoder(dev))
  34. return 0;
  35. cxled = to_cxl_endpoint_decoder(dev);
  36. if ((cxled->cxld.flags & CXL_DECODER_F_ENABLE) == 0)
  37. return 0;
  38. if (cxled->state != CXL_DECODER_STATE_AUTO)
  39. return 0;
  40. /*
  41. * Region enumeration is opportunistic, if this add-event fails,
  42. * continue to the next endpoint decoder.
  43. */
  44. rc = cxl_add_to_region(root, cxled);
  45. if (rc)
  46. dev_dbg(dev, "failed to add to region: %#llx-%#llx\n",
  47. cxled->cxld.hpa_range.start, cxled->cxld.hpa_range.end);
  48. return 0;
  49. }
  50. static int cxl_switch_port_probe(struct cxl_port *port)
  51. {
  52. struct cxl_hdm *cxlhdm;
  53. int rc;
  54. /* Cache the data early to ensure is_visible() works */
  55. read_cdat_data(port);
  56. rc = devm_cxl_port_enumerate_dports(port);
  57. if (rc < 0)
  58. return rc;
  59. cxl_switch_parse_cdat(port);
  60. cxlhdm = devm_cxl_setup_hdm(port, NULL);
  61. if (!IS_ERR(cxlhdm))
  62. return devm_cxl_enumerate_decoders(cxlhdm, NULL);
  63. if (PTR_ERR(cxlhdm) != -ENODEV) {
  64. dev_err(&port->dev, "Failed to map HDM decoder capability\n");
  65. return PTR_ERR(cxlhdm);
  66. }
  67. if (rc == 1) {
  68. dev_dbg(&port->dev, "Fallback to passthrough decoder\n");
  69. return devm_cxl_add_passthrough_decoder(port);
  70. }
  71. dev_err(&port->dev, "HDM decoder capability not found\n");
  72. return -ENXIO;
  73. }
  74. static int cxl_endpoint_port_probe(struct cxl_port *port)
  75. {
  76. struct cxl_endpoint_dvsec_info info = { .port = port };
  77. struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
  78. struct cxl_dev_state *cxlds = cxlmd->cxlds;
  79. struct cxl_hdm *cxlhdm;
  80. struct cxl_port *root;
  81. int rc;
  82. rc = cxl_dvsec_rr_decode(cxlds->dev, port, &info);
  83. if (rc < 0)
  84. return rc;
  85. cxlhdm = devm_cxl_setup_hdm(port, &info);
  86. if (IS_ERR(cxlhdm)) {
  87. if (PTR_ERR(cxlhdm) == -ENODEV)
  88. dev_err(&port->dev, "HDM decoder registers not found\n");
  89. return PTR_ERR(cxlhdm);
  90. }
  91. /* Cache the data early to ensure is_visible() works */
  92. read_cdat_data(port);
  93. cxl_endpoint_parse_cdat(port);
  94. get_device(&cxlmd->dev);
  95. rc = devm_add_action_or_reset(&port->dev, schedule_detach, cxlmd);
  96. if (rc)
  97. return rc;
  98. rc = cxl_hdm_decode_init(cxlds, cxlhdm, &info);
  99. if (rc)
  100. return rc;
  101. rc = devm_cxl_enumerate_decoders(cxlhdm, &info);
  102. if (rc)
  103. return rc;
  104. /*
  105. * This can't fail in practice as CXL root exit unregisters all
  106. * descendant ports and that in turn synchronizes with cxl_port_probe()
  107. */
  108. struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
  109. root = &cxl_root->port;
  110. /*
  111. * Now that all endpoint decoders are successfully enumerated, try to
  112. * assemble regions from committed decoders
  113. */
  114. device_for_each_child(&port->dev, root, discover_region);
  115. return 0;
  116. }
  117. static int cxl_port_probe(struct device *dev)
  118. {
  119. struct cxl_port *port = to_cxl_port(dev);
  120. if (is_cxl_endpoint(port))
  121. return cxl_endpoint_port_probe(port);
  122. return cxl_switch_port_probe(port);
  123. }
  124. static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
  125. struct bin_attribute *bin_attr, char *buf,
  126. loff_t offset, size_t count)
  127. {
  128. struct device *dev = kobj_to_dev(kobj);
  129. struct cxl_port *port = to_cxl_port(dev);
  130. if (!port->cdat_available)
  131. return -ENXIO;
  132. if (!port->cdat.table)
  133. return 0;
  134. return memory_read_from_buffer(buf, count, &offset,
  135. port->cdat.table,
  136. port->cdat.length);
  137. }
  138. static BIN_ATTR_ADMIN_RO(CDAT, 0);
  139. static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
  140. struct bin_attribute *attr, int i)
  141. {
  142. struct device *dev = kobj_to_dev(kobj);
  143. struct cxl_port *port = to_cxl_port(dev);
  144. if ((attr == &bin_attr_CDAT) && port->cdat_available)
  145. return attr->attr.mode;
  146. return 0;
  147. }
  148. static struct bin_attribute *cxl_cdat_bin_attributes[] = {
  149. &bin_attr_CDAT,
  150. NULL,
  151. };
  152. static struct attribute_group cxl_cdat_attribute_group = {
  153. .bin_attrs = cxl_cdat_bin_attributes,
  154. .is_bin_visible = cxl_port_bin_attr_is_visible,
  155. };
  156. static const struct attribute_group *cxl_port_attribute_groups[] = {
  157. &cxl_cdat_attribute_group,
  158. NULL,
  159. };
  160. static struct cxl_driver cxl_port_driver = {
  161. .name = "cxl_port",
  162. .probe = cxl_port_probe,
  163. .id = CXL_DEVICE_PORT,
  164. .drv = {
  165. .dev_groups = cxl_port_attribute_groups,
  166. },
  167. };
  168. static int __init cxl_port_init(void)
  169. {
  170. return cxl_driver_register(&cxl_port_driver);
  171. }
  172. /*
  173. * Be ready to immediately enable ports emitted by the platform CXL root
  174. * (e.g. cxl_acpi) when CONFIG_CXL_PORT=y.
  175. */
  176. subsys_initcall(cxl_port_init);
  177. static void __exit cxl_port_exit(void)
  178. {
  179. cxl_driver_unregister(&cxl_port_driver);
  180. }
  181. module_exit(cxl_port_exit);
  182. MODULE_DESCRIPTION("CXL: Port enumeration and services");
  183. MODULE_LICENSE("GPL v2");
  184. MODULE_IMPORT_NS(CXL);
  185. MODULE_ALIAS_CXL(CXL_DEVICE_PORT);