1/* $NetBSD: fdt_dma.c,v 1.4 2019/02/27 16:56:00 jakllsch 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.4 2019/02/27 16:56:00 jakllsch 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 40struct 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 48static LIST_HEAD(, fdtbus_dma_controller) fdtbus_dma_controllers = 49 LIST_HEAD_INITIALIZER(fdtbus_dma_controllers); 50 51int 52fdtbus_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 67static struct fdtbus_dma_controller * 68fdtbus_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 80struct fdtbus_dma * 81fdtbus_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 125done: 126 if (dmas) 127 kmem_free(dmas, len); 128 129 return dma; 130} 131 132struct fdtbus_dma * 133fdtbus_dma_get(int phandle, const char *name, void (*cb)(void *), void *cbarg) 134{ 135 u_int index; 136 int err; 137 138 err = fdtbus_get_index(phandle, "dma-names", name, &index); 139 if (err != 0) 140 return NULL; 141 142 return fdtbus_dma_get_index(phandle, index, cb, cbarg); 143} 144 145void 146fdtbus_dma_put(struct fdtbus_dma *dma) 147{ 148 struct fdtbus_dma_controller *dc = dma->dma_dc; 149 150 dc->dma_funcs->release(dc->dma_dev, dma->dma_priv); 151 kmem_free(dma, sizeof(*dma)); 152} 153 154int 155fdtbus_dma_transfer(struct fdtbus_dma *dma, struct fdtbus_dma_req *req) 156{ 157 struct fdtbus_dma_controller *dc = dma->dma_dc; 158 159 return dc->dma_funcs->transfer(dc->dma_dev, dma->dma_priv, req); 160} 161 162void 163fdtbus_dma_halt(struct fdtbus_dma *dma) 164{ 165 struct fdtbus_dma_controller *dc = dma->dma_dc; 166 167 return dc->dma_funcs->halt(dc->dma_dev, dma->dma_priv); 168} 169