xref: /freebsd/sys/dev/dpaa2/dpaa2_mcp.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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 __FBSDID("$FreeBSD$");
30 
31 /*
32  * DPAA2 MC command portal and helper routines.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/rman.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/condvar.h>
46 #include <sys/lock.h>
47 
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 
51 #include "pcib_if.h"
52 #include "pci_if.h"
53 
54 #include "dpaa2_mcp.h"
55 #include "dpaa2_mc.h"
56 #include "dpaa2_cmd_if.h"
57 
58 MALLOC_DEFINE(M_DPAA2_MCP, "dpaa2_mcp", "DPAA2 Management Complex Portal");
59 
60 static struct resource_spec dpaa2_mcp_spec[] = {
61 	{ SYS_RES_MEMORY, 0, RF_ACTIVE | RF_UNMAPPED },
62 	RESOURCE_SPEC_END
63 };
64 
65 int
66 dpaa2_mcp_init_portal(struct dpaa2_mcp **mcp, struct resource *res,
67     struct resource_map *map, uint16_t flags)
68 {
69 	const int mflags = flags & DPAA2_PORTAL_NOWAIT_ALLOC
70 	    ? (M_NOWAIT | M_ZERO) : (M_WAITOK | M_ZERO);
71 	struct dpaa2_mcp *p;
72 
73 	if (!mcp || !res || !map)
74 		return (DPAA2_CMD_STAT_EINVAL);
75 
76 	p = malloc(sizeof(struct dpaa2_mcp), M_DPAA2_MCP, mflags);
77 	if (p == NULL)
78 		return (DPAA2_CMD_STAT_NO_MEMORY);
79 
80 	mtx_init(&p->lock, "mcp_sleep_lock", NULL, MTX_DEF);
81 
82 	p->res = res;
83 	p->map = map;
84 	p->flags = flags;
85 	p->rc_api_major = 0; /* DPRC API version to be cached later. */
86 	p->rc_api_minor = 0;
87 
88 	*mcp = p;
89 
90 	return (0);
91 }
92 
93 void
94 dpaa2_mcp_free_portal(struct dpaa2_mcp *mcp)
95 {
96 	uint16_t flags;
97 
98 	KASSERT(mcp != NULL, ("%s: mcp is NULL", __func__));
99 
100 	DPAA2_MCP_LOCK(mcp, &flags);
101 	mcp->flags |= DPAA2_PORTAL_DESTROYED;
102 	DPAA2_MCP_UNLOCK(mcp);
103 
104 	/* Let threads stop using this portal. */
105 	DELAY(DPAA2_PORTAL_TIMEOUT);
106 
107 	mtx_destroy(&mcp->lock);
108 	free(mcp, M_DPAA2_MCP);
109 }
110 
111 struct dpaa2_cmd *
112 dpaa2_mcp_tk(struct dpaa2_cmd *cmd, uint16_t token)
113 {
114 	struct dpaa2_cmd_header *hdr;
115 	KASSERT(cmd != NULL, ("%s: cmd is NULL", __func__));
116 
117 	hdr = (struct dpaa2_cmd_header *) &cmd->header;
118 	hdr->token = token;
119 	return (cmd);
120 }
121 
122 struct dpaa2_cmd *
123 dpaa2_mcp_f(struct dpaa2_cmd *cmd, uint16_t flags)
124 {
125 	struct dpaa2_cmd_header *hdr;
126 	KASSERT(cmd != NULL, ("%s: cmd is NULL", __func__));
127 
128 	hdr = (struct dpaa2_cmd_header *) &cmd->header;
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 	}
134 	if (flags & DPAA2_CMD_INTR_DIS) {
135 		hdr->flags_sw |= DPAA2_SW_FLAG_INTR_DIS;
136 	}
137 	return (cmd);
138 }
139 
140 static int
141 dpaa2_mcp_probe(device_t dev)
142 {
143 	/* DPMCP device will be added by the parent resource container. */
144 	device_set_desc(dev, "DPAA2 MC portal");
145 	return (BUS_PROBE_DEFAULT);
146 }
147 
148 static int
149 dpaa2_mcp_detach(device_t dev)
150 {
151 	return (0);
152 }
153 
154 static int
155 dpaa2_mcp_attach(device_t dev)
156 {
157 	device_t pdev = device_get_parent(dev);
158 	device_t child = dev;
159 	struct dpaa2_mcp_softc *sc = device_get_softc(dev);
160 	struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
161 	struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
162 	struct dpaa2_cmd cmd;
163 	struct dpaa2_mcp *portal;
164 	struct resource_map_request req;
165 	uint16_t rc_token, mcp_token;
166 	int error;
167 
168 	sc->dev = dev;
169 
170 	error = bus_alloc_resources(sc->dev, dpaa2_mcp_spec, sc->res);
171 	if (error) {
172 		device_printf(dev, "%s: failed to allocate resources\n",
173 		    __func__);
174 		goto err_exit;
175 	}
176 
177 	/* At least 64 bytes of the command portal should be available. */
178 	if (rman_get_size(sc->res[0]) < DPAA2_MCP_MEM_WIDTH) {
179 		device_printf(dev, "%s: MC portal memory region too small: "
180 		    "%jd\n", __func__, rman_get_size(sc->res[0]));
181 		goto err_exit;
182 	}
183 
184 	/* Map MC portal memory resource. */
185 	resource_init_map_request(&req);
186 	req.memattr = VM_MEMATTR_DEVICE;
187 	error = bus_map_resource(sc->dev, SYS_RES_MEMORY, sc->res[0], &req,
188 	    &sc->map[0]);
189 	if (error) {
190 		device_printf(dev, "%s: failed to map MC portal memory\n",
191 		    __func__);
192 		goto err_exit;
193 	}
194 
195 	/* Initialize portal to send commands to MC. */
196 	error = dpaa2_mcp_init_portal(&portal, sc->res[0], &sc->map[0],
197 	    DPAA2_PORTAL_DEF);
198 	if (error) {
199 		device_printf(dev, "%s: failed to initialize dpaa2_mcp: "
200 		    "error=%d\n", __func__, error);
201 		goto err_exit;
202 	}
203 
204 	DPAA2_CMD_INIT(&cmd);
205 
206 	/* Open resource container and DPMCP object. */
207 	error = DPAA2_CMD_RC_OPEN(dev, child, &cmd, rcinfo->id, &rc_token);
208 	if (error) {
209 		device_printf(dev, "%s: failed to open DPRC: error=%d\n",
210 		    __func__, error);
211 		goto err_exit;
212 	}
213 	error = DPAA2_CMD_MCP_OPEN(dev, child, &cmd, dinfo->id, &mcp_token);
214 	if (error) {
215 		device_printf(dev, "%s: failed to open DPMCP: id=%d, error=%d\n",
216 		    __func__, dinfo->id, error);
217 		goto close_rc;
218 	}
219 
220 	/* Prepare DPMCP object. */
221 	error = DPAA2_CMD_MCP_RESET(dev, child, &cmd);
222 	if (error) {
223 		device_printf(dev, "%s: failed to reset DPMCP: id=%d, "
224 		    "error=%d\n", __func__, dinfo->id, error);
225 		goto close_mcp;
226 	}
227 
228 	(void)DPAA2_CMD_MCP_CLOSE(dev, child, &cmd);
229 	(void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
230 	dinfo->portal = portal;
231 
232 	return (0);
233 
234 close_mcp:
235 	(void)DPAA2_CMD_MCP_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, mcp_token));
236 close_rc:
237 	(void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
238 err_exit:
239 	dpaa2_mcp_detach(dev);
240 	return (ENXIO);
241 }
242 
243 static device_method_t dpaa2_mcp_methods[] = {
244 	/* Device interface */
245 	DEVMETHOD(device_probe,		dpaa2_mcp_probe),
246 	DEVMETHOD(device_attach,	dpaa2_mcp_attach),
247 	DEVMETHOD(device_detach,	dpaa2_mcp_detach),
248 
249 	DEVMETHOD_END
250 };
251 
252 static driver_t dpaa2_mcp_driver = {
253 	"dpaa2_mcp",
254 	dpaa2_mcp_methods,
255 	sizeof(struct dpaa2_mcp_softc),
256 };
257 
258 DRIVER_MODULE(dpaa2_mcp, dpaa2_rc, dpaa2_mcp_driver, 0, 0);
259