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