1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CDX host controller driver for AMD versal-net platform.
4  *
5  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
6  */
7 
8 #include <linux/mod_devicetable.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/cdx/cdx_bus.h>
12 
13 #include "cdx_controller.h"
14 #include "../cdx.h"
15 #include "mcdi_functions.h"
16 #include "mcdi.h"
17 
18 static unsigned int cdx_mcdi_rpc_timeout(struct cdx_mcdi *cdx, unsigned int cmd)
19 {
20 	return MCDI_RPC_TIMEOUT;
21 }
22 
23 static void cdx_mcdi_request(struct cdx_mcdi *cdx,
24 			     const struct cdx_dword *hdr, size_t hdr_len,
25 			     const struct cdx_dword *sdu, size_t sdu_len)
26 {
27 	if (cdx_rpmsg_send(cdx, hdr, hdr_len, sdu, sdu_len))
28 		dev_err(&cdx->rpdev->dev, "Failed to send rpmsg data\n");
29 }
30 
31 static const struct cdx_mcdi_ops mcdi_ops = {
32 	.mcdi_rpc_timeout = cdx_mcdi_rpc_timeout,
33 	.mcdi_request = cdx_mcdi_request,
34 };
35 
36 void cdx_rpmsg_post_probe(struct cdx_controller *cdx)
37 {
38 	/* Register CDX controller with CDX bus driver */
39 	if (cdx_register_controller(cdx))
40 		dev_err(cdx->dev, "Failed to register CDX controller\n");
41 }
42 
43 void cdx_rpmsg_pre_remove(struct cdx_controller *cdx)
44 {
45 	cdx_unregister_controller(cdx);
46 	cdx_mcdi_wait_for_quiescence(cdx->priv, MCDI_RPC_TIMEOUT);
47 }
48 
49 static int cdx_configure_device(struct cdx_controller *cdx,
50 				u8 bus_num, u8 dev_num,
51 				struct cdx_device_config *dev_config)
52 {
53 	int ret = 0;
54 
55 	switch (dev_config->type) {
56 	case CDX_DEV_RESET_CONF:
57 		ret = cdx_mcdi_reset_device(cdx->priv, bus_num, dev_num);
58 		break;
59 	default:
60 		ret = -EINVAL;
61 	}
62 
63 	return ret;
64 }
65 
66 static int cdx_scan_devices(struct cdx_controller *cdx)
67 {
68 	struct cdx_mcdi *cdx_mcdi = cdx->priv;
69 	u8 bus_num, dev_num, num_cdx_bus;
70 	int ret;
71 
72 	/* MCDI FW Read: Fetch the number of CDX buses on this controller */
73 	ret = cdx_mcdi_get_num_buses(cdx_mcdi);
74 	if (ret < 0) {
75 		dev_err(cdx->dev,
76 			"Get number of CDX buses failed: %d\n", ret);
77 		return ret;
78 	}
79 	num_cdx_bus = (u8)ret;
80 
81 	for (bus_num = 0; bus_num < num_cdx_bus; bus_num++) {
82 		u8 num_cdx_dev;
83 
84 		/* MCDI FW Read: Fetch the number of devices present */
85 		ret = cdx_mcdi_get_num_devs(cdx_mcdi, bus_num);
86 		if (ret < 0) {
87 			dev_err(cdx->dev,
88 				"Get devices on CDX bus %d failed: %d\n", bus_num, ret);
89 			continue;
90 		}
91 		num_cdx_dev = (u8)ret;
92 
93 		for (dev_num = 0; dev_num < num_cdx_dev; dev_num++) {
94 			struct cdx_dev_params dev_params;
95 
96 			/* MCDI FW: Get the device config */
97 			ret = cdx_mcdi_get_dev_config(cdx_mcdi, bus_num,
98 						      dev_num, &dev_params);
99 			if (ret) {
100 				dev_err(cdx->dev,
101 					"CDX device config get failed for %d(bus):%d(dev), %d\n",
102 					bus_num, dev_num, ret);
103 				continue;
104 			}
105 			dev_params.cdx = cdx;
106 
107 			/* Add the device to the cdx bus */
108 			ret = cdx_device_add(&dev_params);
109 			if (ret) {
110 				dev_err(cdx->dev, "registering cdx dev: %d failed: %d\n",
111 					dev_num, ret);
112 				continue;
113 			}
114 
115 			dev_dbg(cdx->dev, "CDX dev: %d on cdx bus: %d created\n",
116 				dev_num, bus_num);
117 		}
118 	}
119 
120 	return 0;
121 }
122 
123 static struct cdx_ops cdx_ops = {
124 	.scan		= cdx_scan_devices,
125 	.dev_configure	= cdx_configure_device,
126 };
127 
128 static int xlnx_cdx_probe(struct platform_device *pdev)
129 {
130 	struct cdx_controller *cdx;
131 	struct cdx_mcdi *cdx_mcdi;
132 	int ret;
133 
134 	cdx_mcdi = kzalloc(sizeof(*cdx_mcdi), GFP_KERNEL);
135 	if (!cdx_mcdi)
136 		return -ENOMEM;
137 
138 	/* Store the MCDI ops */
139 	cdx_mcdi->mcdi_ops = &mcdi_ops;
140 	/* MCDI FW: Initialize the FW path */
141 	ret = cdx_mcdi_init(cdx_mcdi);
142 	if (ret) {
143 		dev_err_probe(&pdev->dev, ret, "MCDI Initialization failed\n");
144 		goto mcdi_init_fail;
145 	}
146 
147 	cdx = kzalloc(sizeof(*cdx), GFP_KERNEL);
148 	if (!cdx) {
149 		ret = -ENOMEM;
150 		goto cdx_alloc_fail;
151 	}
152 	platform_set_drvdata(pdev, cdx);
153 
154 	cdx->dev = &pdev->dev;
155 	cdx->priv = cdx_mcdi;
156 	cdx->ops = &cdx_ops;
157 
158 	ret = cdx_setup_rpmsg(pdev);
159 	if (ret) {
160 		if (ret != -EPROBE_DEFER)
161 			dev_err(&pdev->dev, "Failed to register CDX RPMsg transport\n");
162 		goto cdx_rpmsg_fail;
163 	}
164 
165 	dev_info(&pdev->dev, "Successfully registered CDX controller with RPMsg as transport\n");
166 	return 0;
167 
168 cdx_rpmsg_fail:
169 	kfree(cdx);
170 cdx_alloc_fail:
171 	cdx_mcdi_finish(cdx_mcdi);
172 mcdi_init_fail:
173 	kfree(cdx_mcdi);
174 
175 	return ret;
176 }
177 
178 static int xlnx_cdx_remove(struct platform_device *pdev)
179 {
180 	struct cdx_controller *cdx = platform_get_drvdata(pdev);
181 	struct cdx_mcdi *cdx_mcdi = cdx->priv;
182 
183 	cdx_destroy_rpmsg(pdev);
184 
185 	kfree(cdx);
186 
187 	cdx_mcdi_finish(cdx_mcdi);
188 	kfree(cdx_mcdi);
189 
190 	return 0;
191 }
192 
193 static const struct of_device_id cdx_match_table[] = {
194 	{.compatible = "xlnx,versal-net-cdx",},
195 	{ },
196 };
197 
198 MODULE_DEVICE_TABLE(of, cdx_match_table);
199 
200 static struct platform_driver cdx_pdriver = {
201 	.driver = {
202 		   .name = "cdx-controller",
203 		   .pm = NULL,
204 		   .of_match_table = cdx_match_table,
205 		   },
206 	.probe = xlnx_cdx_probe,
207 	.remove = xlnx_cdx_remove,
208 };
209 
210 static int __init cdx_controller_init(void)
211 {
212 	int ret;
213 
214 	ret = platform_driver_register(&cdx_pdriver);
215 	if (ret)
216 		pr_err("platform_driver_register() failed: %d\n", ret);
217 
218 	return ret;
219 }
220 
221 static void __exit cdx_controller_exit(void)
222 {
223 	platform_driver_unregister(&cdx_pdriver);
224 }
225 
226 module_init(cdx_controller_init);
227 module_exit(cdx_controller_exit);
228 
229 MODULE_AUTHOR("AMD Inc.");
230 MODULE_DESCRIPTION("CDX controller for AMD devices");
231 MODULE_LICENSE("GPL");
232