xref: /freebsd/sys/dev/dpaa2/dpaa2_mcp.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
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
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
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 struct dpaa2_cmd *
110 dpaa2_mcp_tk(struct dpaa2_cmd *cmd, uint16_t token)
111 {
112 	struct dpaa2_cmd_header *hdr;
113 	KASSERT(cmd != NULL, ("%s: cmd is NULL", __func__));
114 
115 	hdr = (struct dpaa2_cmd_header *) &cmd->header;
116 	hdr->token = token;
117 	return (cmd);
118 }
119 
120 struct dpaa2_cmd *
121 dpaa2_mcp_f(struct dpaa2_cmd *cmd, uint16_t flags)
122 {
123 	struct dpaa2_cmd_header *hdr;
124 	KASSERT(cmd != NULL, ("%s: cmd is NULL", __func__));
125 
126 	hdr = (struct dpaa2_cmd_header *) &cmd->header;
127 	hdr->flags_hw = DPAA2_CMD_DEF;
128 	hdr->flags_sw = DPAA2_CMD_DEF;
129 	if (flags & DPAA2_CMD_HIGH_PRIO) {
130 		hdr->flags_hw |= DPAA2_HW_FLAG_HIGH_PRIO;
131 	}
132 	if (flags & DPAA2_CMD_INTR_DIS) {
133 		hdr->flags_sw |= DPAA2_SW_FLAG_INTR_DIS;
134 	}
135 	return (cmd);
136 }
137 
138 static int
139 dpaa2_mcp_probe(device_t dev)
140 {
141 	/* DPMCP device will be added by the parent resource container. */
142 	device_set_desc(dev, "DPAA2 MC portal");
143 	return (BUS_PROBE_DEFAULT);
144 }
145 
146 static int
147 dpaa2_mcp_detach(device_t dev)
148 {
149 	return (0);
150 }
151 
152 static int
153 dpaa2_mcp_attach(device_t dev)
154 {
155 	device_t pdev = device_get_parent(dev);
156 	device_t child = dev;
157 	struct dpaa2_mcp_softc *sc = device_get_softc(dev);
158 	struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
159 	struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
160 	struct dpaa2_cmd cmd;
161 	struct dpaa2_mcp *portal;
162 	struct resource_map_request req;
163 	uint16_t rc_token, mcp_token;
164 	int error;
165 
166 	sc->dev = dev;
167 
168 	error = bus_alloc_resources(sc->dev, dpaa2_mcp_spec, sc->res);
169 	if (error) {
170 		device_printf(dev, "%s: failed to allocate resources\n",
171 		    __func__);
172 		goto err_exit;
173 	}
174 
175 	/* At least 64 bytes of the command portal should be available. */
176 	if (rman_get_size(sc->res[0]) < DPAA2_MCP_MEM_WIDTH) {
177 		device_printf(dev, "%s: MC portal memory region too small: "
178 		    "%jd\n", __func__, rman_get_size(sc->res[0]));
179 		goto err_exit;
180 	}
181 
182 	/* Map MC portal memory resource. */
183 	resource_init_map_request(&req);
184 	req.memattr = VM_MEMATTR_DEVICE;
185 	error = bus_map_resource(sc->dev, SYS_RES_MEMORY, sc->res[0], &req,
186 	    &sc->map[0]);
187 	if (error) {
188 		device_printf(dev, "%s: failed to map MC portal memory\n",
189 		    __func__);
190 		goto err_exit;
191 	}
192 
193 	/* Initialize portal to send commands to MC. */
194 	error = dpaa2_mcp_init_portal(&portal, sc->res[0], &sc->map[0],
195 	    DPAA2_PORTAL_DEF);
196 	if (error) {
197 		device_printf(dev, "%s: failed to initialize dpaa2_mcp: "
198 		    "error=%d\n", __func__, error);
199 		goto err_exit;
200 	}
201 
202 	DPAA2_CMD_INIT(&cmd);
203 
204 	/* Open resource container and DPMCP object. */
205 	error = DPAA2_CMD_RC_OPEN(dev, child, &cmd, rcinfo->id, &rc_token);
206 	if (error) {
207 		device_printf(dev, "%s: failed to open DPRC: error=%d\n",
208 		    __func__, error);
209 		goto err_exit;
210 	}
211 	error = DPAA2_CMD_MCP_OPEN(dev, child, &cmd, dinfo->id, &mcp_token);
212 	if (error) {
213 		device_printf(dev, "%s: failed to open DPMCP: id=%d, error=%d\n",
214 		    __func__, dinfo->id, error);
215 		goto close_rc;
216 	}
217 
218 	/* Prepare DPMCP object. */
219 	error = DPAA2_CMD_MCP_RESET(dev, child, &cmd);
220 	if (error) {
221 		device_printf(dev, "%s: failed to reset DPMCP: id=%d, "
222 		    "error=%d\n", __func__, dinfo->id, error);
223 		goto close_mcp;
224 	}
225 
226 	(void)DPAA2_CMD_MCP_CLOSE(dev, child, &cmd);
227 	(void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
228 	dinfo->portal = portal;
229 
230 	return (0);
231 
232 close_mcp:
233 	(void)DPAA2_CMD_MCP_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, mcp_token));
234 close_rc:
235 	(void)DPAA2_CMD_RC_CLOSE(dev, child, DPAA2_CMD_TK(&cmd, rc_token));
236 err_exit:
237 	dpaa2_mcp_detach(dev);
238 	return (ENXIO);
239 }
240 
241 static device_method_t dpaa2_mcp_methods[] = {
242 	/* Device interface */
243 	DEVMETHOD(device_probe,		dpaa2_mcp_probe),
244 	DEVMETHOD(device_attach,	dpaa2_mcp_attach),
245 	DEVMETHOD(device_detach,	dpaa2_mcp_detach),
246 
247 	DEVMETHOD_END
248 };
249 
250 static driver_t dpaa2_mcp_driver = {
251 	"dpaa2_mcp",
252 	dpaa2_mcp_methods,
253 	sizeof(struct dpaa2_mcp_softc),
254 };
255 
256 DRIVER_MODULE(dpaa2_mcp, dpaa2_rc, dpaa2_mcp_driver, 0, 0);
257