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 int
112 dpaa2_mcp_init_command(struct dpaa2_cmd **cmd, uint16_t flags)
113 {
114 const int mflags = flags & DPAA2_CMD_NOWAIT_ALLOC
115 ? (M_NOWAIT | M_ZERO) : (M_WAITOK | M_ZERO);
116 struct dpaa2_cmd *c;
117 struct dpaa2_cmd_header *hdr;
118
119 if (!cmd)
120 return (DPAA2_CMD_STAT_EINVAL);
121
122 c = malloc(sizeof(struct dpaa2_cmd), M_DPAA2_MCP, mflags);
123 if (!c)
124 return (DPAA2_CMD_STAT_NO_MEMORY);
125
126 hdr = (struct dpaa2_cmd_header *) &c->header;
127 hdr->srcid = 0;
128 hdr->status = DPAA2_CMD_STAT_OK;
129 hdr->token = 0;
130 hdr->cmdid = 0;
131 hdr->flags_hw = DPAA2_CMD_DEF;
132 hdr->flags_sw = DPAA2_CMD_DEF;
133 if (flags & DPAA2_CMD_HIGH_PRIO)
134 hdr->flags_hw |= DPAA2_HW_FLAG_HIGH_PRIO;
135 if (flags & DPAA2_CMD_INTR_DIS)
136 hdr->flags_sw |= DPAA2_SW_FLAG_INTR_DIS;
137 for (uint32_t i = 0; i < DPAA2_CMD_PARAMS_N; i++)
138 c->params[i] = 0;
139 *cmd = c;
140
141 return (0);
142 }
143
144 void
145 dpaa2_mcp_free_command(struct dpaa2_cmd *cmd)
146 {
147 if (cmd != NULL)
148 free(cmd, M_DPAA2_MCP);
149 }
150
151 struct dpaa2_cmd *
152 dpaa2_mcp_tk(struct dpaa2_cmd *cmd, uint16_t token)
153 {
154 struct dpaa2_cmd_header *hdr;
155 if (cmd != NULL) {
156 hdr = (struct dpaa2_cmd_header *) &cmd->header;
157 hdr->token = token;
158 }
159 return (cmd);
160 }
161
162 struct dpaa2_cmd *
163 dpaa2_mcp_f(struct dpaa2_cmd *cmd, uint16_t flags)
164 {
165 struct dpaa2_cmd_header *hdr;
166 if (cmd) {
167 hdr = (struct dpaa2_cmd_header *) &cmd->header;
168 hdr->flags_hw = DPAA2_CMD_DEF;
169 hdr->flags_sw = DPAA2_CMD_DEF;
170
171 if (flags & DPAA2_CMD_HIGH_PRIO)
172 hdr->flags_hw |= DPAA2_HW_FLAG_HIGH_PRIO;
173 if (flags & DPAA2_CMD_INTR_DIS)
174 hdr->flags_sw |= DPAA2_SW_FLAG_INTR_DIS;
175 }
176 return (cmd);
177 }
178
179 static int
180 dpaa2_mcp_probe(device_t dev)
181 {
182 /* DPMCP device will be added by the parent resource container. */
183 device_set_desc(dev, "DPAA2 MC portal");
184 return (BUS_PROBE_DEFAULT);
185 }
186
187 static int
188 dpaa2_mcp_detach(device_t dev)
189 {
190 return (0);
191 }
192
193 static int
194 dpaa2_mcp_attach(device_t dev)
195 {
196 device_t pdev = device_get_parent(dev);
197 device_t child = dev;
198 struct dpaa2_mcp_softc *sc = device_get_softc(dev);
199 struct dpaa2_devinfo *rcinfo = device_get_ivars(pdev);
200 struct dpaa2_devinfo *dinfo = device_get_ivars(dev);
201 struct dpaa2_cmd *cmd;
202 struct dpaa2_mcp *portal;
203 struct resource_map_request req;
204 uint16_t rc_token, mcp_token;
205 int error;
206
207 sc->dev = dev;
208
209 error = bus_alloc_resources(sc->dev, dpaa2_mcp_spec, sc->res);
210 if (error) {
211 device_printf(dev, "%s: failed to allocate resources\n",
212 __func__);
213 goto err_exit;
214 }
215
216 /* At least 64 bytes of the command portal should be available. */
217 if (rman_get_size(sc->res[0]) < DPAA2_MCP_MEM_WIDTH) {
218 device_printf(dev, "%s: MC portal memory region too small: "
219 "%jd\n", __func__, rman_get_size(sc->res[0]));
220 goto err_exit;
221 }
222
223 /* Map MC portal memory resource. */
224 resource_init_map_request(&req);
225 req.memattr = VM_MEMATTR_DEVICE;
226 error = bus_map_resource(sc->dev, SYS_RES_MEMORY, sc->res[0], &req,
227 &sc->map[0]);
228 if (error) {
229 device_printf(dev, "%s: failed to map MC portal memory\n",
230 __func__);
231 goto err_exit;
232 }
233
234 /* Initialize portal to send commands to MC. */
235 error = dpaa2_mcp_init_portal(&portal, sc->res[0], &sc->map[0],
236 DPAA2_PORTAL_DEF);
237 if (error) {
238 device_printf(dev, "%s: failed to initialize dpaa2_mcp: "
239 "error=%d\n", __func__, error);
240 goto err_exit;
241 }
242
243 /* Allocate a command to send to MC hardware. */
244 error = dpaa2_mcp_init_command(&cmd, DPAA2_CMD_DEF);
245 if (error) {
246 device_printf(dev, "%s: failed to allocate dpaa2_cmd: "
247 "error=%d\n", __func__, error);
248 goto err_exit;
249 }
250
251 /* Open resource container and DPMCP object. */
252 error = DPAA2_CMD_RC_OPEN(dev, child, cmd, rcinfo->id, &rc_token);
253 if (error) {
254 device_printf(dev, "%s: failed to open DPRC: error=%d\n",
255 __func__, error);
256 goto err_free_cmd;
257 }
258 error = DPAA2_CMD_MCP_OPEN(dev, child, cmd, dinfo->id, &mcp_token);
259 if (error) {
260 device_printf(dev, "%s: failed to open DPMCP: id=%d, error=%d\n",
261 __func__, dinfo->id, error);
262 goto err_close_rc;
263 }
264
265 /* Prepare DPMCP object. */
266 error = DPAA2_CMD_MCP_RESET(dev, child, cmd);
267 if (error) {
268 device_printf(dev, "%s: failed to reset DPMCP: id=%d, "
269 "error=%d\n", __func__, dinfo->id, error);
270 goto err_close_mcp;
271 }
272
273 /* Close the DPMCP object and the resource container. */
274 error = DPAA2_CMD_MCP_CLOSE(dev, child, cmd);
275 if (error) {
276 device_printf(dev, "%s: failed to close DPMCP: id=%d, "
277 "error=%d\n", __func__, dinfo->id, error);
278 goto err_close_rc;
279 }
280 error = DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(cmd, rc_token));
281 if (error) {
282 device_printf(dev, "%s: failed to close DPRC: error=%d\n",
283 __func__, error);
284 goto err_free_cmd;
285 }
286
287 dpaa2_mcp_free_command(cmd);
288 dinfo->portal = portal;
289
290 return (0);
291
292 err_close_mcp:
293 DPAA2_CMD_MCP_CLOSE(dev, child, dpaa2_mcp_tk(cmd, mcp_token));
294 err_close_rc:
295 DPAA2_CMD_RC_CLOSE(dev, child, dpaa2_mcp_tk(cmd, rc_token));
296 err_free_cmd:
297 dpaa2_mcp_free_command(cmd);
298 err_exit:
299 dpaa2_mcp_detach(dev);
300 return (ENXIO);
301 }
302
303 static device_method_t dpaa2_mcp_methods[] = {
304 /* Device interface */
305 DEVMETHOD(device_probe, dpaa2_mcp_probe),
306 DEVMETHOD(device_attach, dpaa2_mcp_attach),
307 DEVMETHOD(device_detach, dpaa2_mcp_detach),
308
309 DEVMETHOD_END
310 };
311
312 static driver_t dpaa2_mcp_driver = {
313 "dpaa2_mcp",
314 dpaa2_mcp_methods,
315 sizeof(struct dpaa2_mcp_softc),
316 };
317
318 DRIVER_MODULE(dpaa2_mcp, dpaa2_rc, dpaa2_mcp_driver, 0, 0);
Cache object: d0732593d31cf6ee6b6be9d2e2a38655
|