xref: /freebsd-13-stable/sys/dev/dpaa2/dpaa2_mcp.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright © 2021-2022 Dmitry Salychev
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 /*
30  * DPAA2 MC command portal and helper routines.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/rman.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/condvar.h>
44 #include <sys/lock.h>
45 
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 
49 #include "pcib_if.h"
50 #include "pci_if.h"
51 
52 #include "dpaa2_mcp.h"
53 #include "dpaa2_mc.h"
54 #include "dpaa2_cmd_if.h"
55 
56 MALLOC_DEFINE(M_DPAA2_MCP, "dpaa2_mcp", "DPAA2 Management Complex Portal");
57 
58 static struct resource_spec dpaa2_mcp_spec[] = {
59 	{ SYS_RES_MEMORY, 0, RF_ACTIVE | RF_UNMAPPED },
60 	RESOURCE_SPEC_END
61 };
62 
63 int
dpaa2_mcp_init_portal(struct dpaa2_mcp ** mcp,struct resource * res,struct resource_map * map,uint16_t flags)64 dpaa2_mcp_init_portal(struct dpaa2_mcp **mcp, struct resource *res,
65     struct resource_map *map, uint16_t flags)
66 {
67 	const int mflags = flags & DPAA2_PORTAL_NOWAIT_ALLOC
68 	    ? (M_NOWAIT | M_ZERO) : (M_WAITOK | M_ZERO);
69 	struct dpaa2_mcp *p;
70 
71 	if (!mcp || !res || !map)
72 		return (DPAA2_CMD_STAT_EINVAL);
73 
74 	p = malloc(sizeof(struct dpaa2_mcp), M_DPAA2_MCP, mflags);
75 	if (p == NULL)
76 		return (DPAA2_CMD_STAT_NO_MEMORY);
77 
78 	mtx_init(&p->lock, "mcp_sleep_lock", NULL, MTX_DEF);
79 
80 	p->res = res;
81 	p->map = map;
82 	p->flags = flags;
83 	p->rc_api_major = 0; /* DPRC API version to be cached later. */
84 	p->rc_api_minor = 0;
85 
86 	*mcp = p;
87 
88 	return (0);
89 }
90 
91 void
dpaa2_mcp_free_portal(struct dpaa2_mcp * mcp)92 dpaa2_mcp_free_portal(struct dpaa2_mcp *mcp)
93 {
94 	uint16_t flags;
95 
96 	KASSERT(mcp != NULL, ("%s: mcp is NULL", __func__));
97 
98 	DPAA2_MCP_LOCK(mcp, &flags);
99 	mcp->flags |= DPAA2_PORTAL_DESTROYED;
100 	DPAA2_MCP_UNLOCK(mcp);
101 
102 	/* Let threads stop using this portal. */
103 	DELAY(DPAA2_PORTAL_TIMEOUT);
104 
105 	mtx_destroy(&mcp->lock);
106 	free(mcp, M_DPAA2_MCP);
107 }
108 
109 int
dpaa2_mcp_init_command(struct dpaa2_cmd ** cmd,uint16_t flags)110 dpaa2_mcp_init_command(struct dpaa2_cmd **cmd, uint16_t flags)
111 {
112 	const int mflags = flags & DPAA2_CMD_NOWAIT_ALLOC
113 	    ? (M_NOWAIT | M_ZERO) : (M_WAITOK | M_ZERO);
114 	struct dpaa2_cmd *c;
115 	struct dpaa2_cmd_header *hdr;
116 
117 	if (!cmd)
118 		return (DPAA2_CMD_STAT_EINVAL);
119 
120 	c = malloc(sizeof(struct dpaa2_cmd), M_DPAA2_MCP, mflags);
121 	if (!c)
122 		return (DPAA2_CMD_STAT_NO_MEMORY);
123 
124 	hdr = (struct dpaa2_cmd_header *) &c->header;
125 	hdr->srcid = 0;
126 	hdr->status = DPAA2_CMD_STAT_OK;
127 	hdr->token = 0;
128 	hdr->cmdid = 0;
129 	hdr->flags_hw = DPAA2_CMD_DEF;
130 	hdr->flags_sw = DPAA2_CMD_DEF;
131 	if (flags & DPAA2_CMD_HIGH_PRIO)
132 		hdr->flags_hw |= DPAA2_HW_FLAG_HIGH_PRIO;
133 	if (flags & DPAA2_CMD_INTR_DIS)
134 		hdr->flags_sw |= DPAA2_SW_FLAG_INTR_DIS;
135 	for (uint32_t i = 0; i < DPAA2_CMD_PARAMS_N; i++)
136 		c->params[i] = 0;
137 	*cmd = c;
138 
139 	return (0);
140 }
141 
142 void
dpaa2_mcp_free_command(struct dpaa2_cmd * cmd)143 dpaa2_mcp_free_command(struct dpaa2_cmd *cmd)
144 {
145 	if (cmd != NULL)
146 		free(cmd, M_DPAA2_MCP);
147 }
148 
149 struct dpaa2_cmd *
dpaa2_mcp_tk(struct dpaa2_cmd * cmd,uint16_t token)150 dpaa2_mcp_tk(struct dpaa2_cmd *cmd, uint16_t token)
151 {
152 	struct dpaa2_cmd_header *hdr;
153 	if (cmd != NULL) {
154 		hdr = (struct dpaa2_cmd_header *) &cmd->header;
155 		hdr->token = token;
156 	}
157 	return (cmd);
158 }
159 
160 struct dpaa2_cmd *
dpaa2_mcp_f(struct dpaa2_cmd * cmd,uint16_t flags)161 dpaa2_mcp_f(struct dpaa2_cmd *cmd, uint16_t flags)
162 {
163 	struct dpaa2_cmd_header *hdr;
164 	if (cmd) {
165 		hdr = (struct dpaa2_cmd_header *) &cmd->header;
166 		hdr->flags_hw = DPAA2_CMD_DEF;
167 		hdr->flags_sw = DPAA2_CMD_DEF;
168 
169 		if (flags & DPAA2_CMD_HIGH_PRIO)
170 			hdr->flags_hw |= DPAA2_HW_FLAG_HIGH_PRIO;
171 		if (flags & DPAA2_CMD_INTR_DIS)
172 			hdr->flags_sw |= DPAA2_SW_FLAG_INTR_DIS;
173 	}
174 	return (cmd);
175 }
176 
177 static int
dpaa2_mcp_probe(device_t dev)178 dpaa2_mcp_probe(device_t dev)
179 {
180 	/* DPMCP device will be added by the parent resource container. */
181 	device_set_desc(dev, "DPAA2 MC portal");
182 	return (BUS_PROBE_DEFAULT);
183 }
184 
185 static int
dpaa2_mcp_detach(device_t dev)186 dpaa2_mcp_detach(device_t dev)
187 {
188 	return (0);
189 }
190 
191 static int
dpaa2_mcp_attach(device_t dev)192 dpaa2_mcp_attach(device_t dev)
193 {
194 	device_t pdev = device_get_parent(dev);
195 	device_t child = dev;
196 	struct dpaa2_mcp_softc *sc = device_get_softc(dev);
197 	struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
198 	struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
199 	struct dpaa2_cmd *cmd;
200 	struct dpaa2_mcp *portal;
201 	struct resource_map_request req;
202 	uint16_t rc_token, mcp_token;
203 	int error;
204 
205 	sc->dev = dev;
206 
207 	error = bus_alloc_resources(sc->dev, dpaa2_mcp_spec, sc->res);
208 	if (error) {
209 		device_printf(dev, "%s: failed to allocate resources\n",
210 		    __func__);
211 		goto err_exit;
212 	}
213 
214 	/* At least 64 bytes of the command portal should be available. */
215 	if (rman_get_size(sc->res[0]) < DPAA2_MCP_MEM_WIDTH) {
216 		device_printf(dev, "%s: MC portal memory region too small: "
217 		    "%jd\n", __func__, rman_get_size(sc->res[0]));
218 		goto err_exit;
219 	}
220 
221 	/* Map MC portal memory resource. */
222 	resource_init_map_request(&req);
223 	req.memattr = VM_MEMATTR_DEVICE;
224 	error = bus_map_resource(sc->dev, SYS_RES_MEMORY, sc->res[0], &req,
225 	    &sc->map[0]);
226 	if (error) {
227 		device_printf(dev, "%s: failed to map MC portal memory\n",
228 		    __func__);
229 		goto err_exit;
230 	}
231 
232 	/* Initialize portal to send commands to MC. */
233 	error = dpaa2_mcp_init_portal(&portal, sc->res[0], &sc->map[0],
234 	    DPAA2_PORTAL_DEF);
235 	if (error) {
236 		device_printf(dev, "%s: failed to initialize dpaa2_mcp: "
237 		    "error=%d\n", __func__, error);
238 		goto err_exit;
239 	}
240 
241 	/* Allocate a command to send to MC hardware. */
242 	error = dpaa2_mcp_init_command(&cmd, DPAA2_CMD_DEF);
243 	if (error) {
244 		device_printf(dev, "%s: failed to allocate dpaa2_cmd: "
245 		    "error=%d\n", __func__, error);
246 		goto err_exit;
247 	}
248 
249 	/* Open resource container and DPMCP object. */
250 	error = DPAA2_CMD_RC_OPEN(dev, child, cmd, rcinfo->id, &rc_token);
251 	if (error) {
252 		device_printf(dev, "%s: failed to open DPRC: error=%d\n",
253 		    __func__, error);
254 		goto err_free_cmd;
255 	}
256 	error = DPAA2_CMD_MCP_OPEN(dev, child, cmd, dinfo->id, &mcp_token);
257 	if (error) {
258 		device_printf(dev, "%s: failed to open DPMCP: id=%d, error=%d\n",
259 		    __func__, dinfo->id, error);
260 		goto err_close_rc;
261 	}
262 
263 	/* Prepare DPMCP object. */
264 	error = DPAA2_CMD_MCP_RESET(dev, child, cmd);
265 	if (error) {
266 		device_printf(dev, "%s: failed to reset DPMCP: id=%d, "
267 		    "error=%d\n", __func__, dinfo->id, error);
268 		goto err_close_mcp;
269 	}
270 
271 	/* Close the DPMCP object and the resource container. */
272 	error = DPAA2_CMD_MCP_CLOSE(dev, child, cmd);
273 	if (error) {
274 		device_printf(dev, "%s: failed to close DPMCP: id=%d, "
275 		    "error=%d\n", __func__, dinfo->id, error);
276 		goto err_close_rc;
277 	}
278 	error = DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(cmd, rc_token));
279 	if (error) {
280 		device_printf(dev, "%s: failed to close DPRC: error=%d\n",
281 		    __func__, error);
282 		goto err_free_cmd;
283 	}
284 
285 	dpaa2_mcp_free_command(cmd);
286 	dinfo->portal = portal;
287 
288 	return (0);
289 
290 err_close_mcp:
291 	DPAA2_CMD_MCP_CLOSE(dev, child, dpaa2_mcp_tk(cmd, mcp_token));
292 err_close_rc:
293 	DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(cmd, rc_token));
294 err_free_cmd:
295 	dpaa2_mcp_free_command(cmd);
296 err_exit:
297 	dpaa2_mcp_detach(dev);
298 	return (ENXIO);
299 }
300 
301 static device_method_t dpaa2_mcp_methods[] = {
302 	/* Device interface */
303 	DEVMETHOD(device_probe,		dpaa2_mcp_probe),
304 	DEVMETHOD(device_attach,	dpaa2_mcp_attach),
305 	DEVMETHOD(device_detach,	dpaa2_mcp_detach),
306 
307 	DEVMETHOD_END
308 };
309 
310 static driver_t dpaa2_mcp_driver = {
311 	"dpaa2_mcp",
312 	dpaa2_mcp_methods,
313 	sizeof(struct dpaa2_mcp_softc),
314 };
315 
316 DRIVER_MODULE(dpaa2_mcp, dpaa2_rc, dpaa2_mcp_driver, 0, 0);
317