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