xref: /openbmc/linux/drivers/cxl/port.c (revision bacf743e)
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 
7 #include "cxlmem.h"
8 #include "cxlpci.h"
9 
10 /**
11  * DOC: cxl port
12  *
13  * The port driver enumerates dport via PCI and scans for HDM
14  * (Host-managed-Device-Memory) decoder resources via the
15  * @component_reg_phys value passed in by the agent that registered the
16  * port. All descendant ports of a CXL root port (described by platform
17  * firmware) are managed in this drivers context. Each driver instance
18  * is responsible for tearing down the driver context of immediate
19  * descendant ports. The locking for this is validated by
20  * CONFIG_PROVE_CXL_LOCKING.
21  *
22  * The primary service this driver provides is presenting APIs to other
23  * drivers to utilize the decoders, and indicating to userspace (via bind
24  * status) the connectivity of the CXL.mem protocol throughout the
25  * PCIe topology.
26  */
27 
28 static void schedule_detach(void *cxlmd)
29 {
30 	schedule_cxl_memdev_detach(cxlmd);
31 }
32 
33 static int cxl_port_probe(struct device *dev)
34 {
35 	struct cxl_port *port = to_cxl_port(dev);
36 	struct cxl_hdm *cxlhdm;
37 	int rc;
38 
39 	if (is_cxl_endpoint(port)) {
40 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
41 
42 		get_device(&cxlmd->dev);
43 		rc = devm_add_action_or_reset(dev, schedule_detach, cxlmd);
44 		if (rc)
45 			return rc;
46 	} else {
47 		rc = devm_cxl_port_enumerate_dports(port);
48 		if (rc < 0)
49 			return rc;
50 		if (rc == 1)
51 			return devm_cxl_add_passthrough_decoder(port);
52 	}
53 
54 	cxlhdm = devm_cxl_setup_hdm(port);
55 	if (IS_ERR(cxlhdm))
56 		return PTR_ERR(cxlhdm);
57 
58 	rc = devm_cxl_enumerate_decoders(cxlhdm);
59 	if (rc) {
60 		dev_err(dev, "Couldn't enumerate decoders (%d)\n", rc);
61 		return rc;
62 	}
63 
64 	return 0;
65 }
66 
67 static struct cxl_driver cxl_port_driver = {
68 	.name = "cxl_port",
69 	.probe = cxl_port_probe,
70 	.id = CXL_DEVICE_PORT,
71 };
72 
73 module_cxl_driver(cxl_port_driver);
74 MODULE_LICENSE("GPL v2");
75 MODULE_IMPORT_NS(CXL);
76 MODULE_ALIAS_CXL(CXL_DEVICE_PORT);
77