1 /* $NetBSD: fdt_dma.c,v 1.2 2018/06/30 20:34:43 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: fdt_dma.c,v 1.2 2018/06/30 20:34:43 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/kmem.h> 35 #include <sys/queue.h> 36 37 #include <libfdt.h> 38 #include <dev/fdt/fdtvar.h> 39 40 struct fdtbus_dma_controller { 41 device_t dma_dev; 42 int dma_phandle; 43 const struct fdtbus_dma_controller_func *dma_funcs; 44 45 LIST_ENTRY(fdtbus_dma_controller) dma_next; 46 }; 47 48 static LIST_HEAD(, fdtbus_dma_controller) fdtbus_dma_controllers = 49 LIST_HEAD_INITIALIZER(fdtbus_dma_controllers); 50 51 int 52 fdtbus_register_dma_controller(device_t dev, int phandle, 53 const struct fdtbus_dma_controller_func *funcs) 54 { 55 struct fdtbus_dma_controller *dma; 56 57 dma = kmem_alloc(sizeof(*dma), KM_SLEEP); 58 dma->dma_dev = dev; 59 dma->dma_phandle = phandle; 60 dma->dma_funcs = funcs; 61 62 LIST_INSERT_HEAD(&fdtbus_dma_controllers, dma, dma_next); 63 64 return 0; 65 } 66 67 static struct fdtbus_dma_controller * 68 fdtbus_get_dma_controller(int phandle) 69 { 70 struct fdtbus_dma_controller *dma; 71 72 LIST_FOREACH(dma, &fdtbus_dma_controllers, dma_next) { 73 if (dma->dma_phandle == phandle) 74 return dma; 75 } 76 77 return NULL; 78 } 79 80 struct fdtbus_dma * 81 fdtbus_dma_get_index(int phandle, u_int index, void (*cb)(void *), void *cbarg) 82 { 83 struct fdtbus_dma_controller *dc; 84 struct fdtbus_dma *dma = NULL; 85 void *dma_priv = NULL; 86 uint32_t *dmas = NULL; 87 uint32_t *p; 88 u_int n, dma_cells; 89 int len, resid; 90 91 len = OF_getproplen(phandle, "dmas"); 92 if (len <= 0) 93 return NULL; 94 95 dmas = kmem_alloc(len, KM_SLEEP); 96 if (OF_getprop(phandle, "dmas", dmas, len) != len) { 97 kmem_free(dmas, len); 98 return NULL; 99 } 100 101 p = dmas; 102 for (n = 0, resid = len; resid > 0; n++) { 103 const int dc_phandle = 104 fdtbus_get_phandle_from_native(be32toh(p[0])); 105 if (of_getprop_uint32(dc_phandle, "#dma-cells", &dma_cells)) 106 break; 107 if (n == index) { 108 dc = fdtbus_get_dma_controller(dc_phandle); 109 if (dc == NULL) 110 goto done; 111 dma_priv = dc->dma_funcs->acquire(dc->dma_dev, 112 dma_cells > 0 ? &p[1] : NULL, dma_cells * 4, 113 cb, cbarg); 114 if (dma_priv) { 115 dma = kmem_alloc(sizeof(*dma), KM_SLEEP); 116 dma->dma_dc = dc; 117 dma->dma_priv = dma_priv; 118 } 119 break; 120 } 121 resid -= (dma_cells + 1) * 4; 122 p += dma_cells + 1; 123 } 124 125 done: 126 if (dmas) 127 kmem_free(dmas, len); 128 129 return dma; 130 } 131 132 struct fdtbus_dma * 133 fdtbus_dma_get(int phandle, const char *name, void (*cb)(void *), void *cbarg) 134 { 135 struct fdtbus_dma *dma = NULL; 136 char *dma_names = NULL; 137 const char *p; 138 u_int index; 139 int len, resid; 140 141 len = OF_getproplen(phandle, "dma-names"); 142 if (len <= 0) 143 return NULL; 144 145 dma_names = kmem_alloc(len, KM_SLEEP); 146 if (OF_getprop(phandle, "dma-names", dma_names, len) != len) { 147 kmem_free(dma_names, len); 148 return NULL; 149 } 150 151 p = dma_names; 152 for (index = 0, resid = len; resid > 0; index++) { 153 if (strcmp(p, name) == 0) { 154 dma = fdtbus_dma_get_index(phandle, index, cb, cbarg); 155 break; 156 } 157 resid -= strlen(p); 158 p += strlen(p) + 1; 159 } 160 161 if (dma_names) 162 kmem_free(dma_names, len); 163 164 return dma; 165 } 166 167 void 168 fdtbus_dma_put(struct fdtbus_dma *dma) 169 { 170 struct fdtbus_dma_controller *dc = dma->dma_dc; 171 172 dc->dma_funcs->release(dc->dma_dev, dma->dma_priv); 173 kmem_free(dma, sizeof(*dma)); 174 } 175 176 int 177 fdtbus_dma_transfer(struct fdtbus_dma *dma, struct fdtbus_dma_req *req) 178 { 179 struct fdtbus_dma_controller *dc = dma->dma_dc; 180 181 return dc->dma_funcs->transfer(dc->dma_dev, dma->dma_priv, req); 182 } 183 184 void 185 fdtbus_dma_halt(struct fdtbus_dma *dma) 186 { 187 struct fdtbus_dma_controller *dc = dma->dma_dc; 188 189 return dc->dma_funcs->halt(dc->dma_dev, dma->dma_priv); 190 } 191