fdt_dma.c revision 1.1 1 /* $NetBSD: fdt_dma.c,v 1.1 2017/04/29 11:00:56 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) 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.1 2017/04/29 11:00:56 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35
36 #include <libfdt.h>
37 #include <dev/fdt/fdtvar.h>
38
39 struct fdtbus_dma_controller {
40 device_t dma_dev;
41 int dma_phandle;
42 const struct fdtbus_dma_controller_func *dma_funcs;
43
44 struct fdtbus_dma_controller *dma_next;
45 };
46
47 static struct fdtbus_dma_controller *fdtbus_dma = NULL;
48
49 int
50 fdtbus_register_dma_controller(device_t dev, int phandle,
51 const struct fdtbus_dma_controller_func *funcs)
52 {
53 struct fdtbus_dma_controller *dma;
54
55 dma = kmem_alloc(sizeof(*dma), KM_SLEEP);
56 dma->dma_dev = dev;
57 dma->dma_phandle = phandle;
58 dma->dma_funcs = funcs;
59
60 dma->dma_next = fdtbus_dma;
61 fdtbus_dma = dma;
62
63 return 0;
64 }
65
66 static struct fdtbus_dma_controller *
67 fdtbus_get_dma_controller(int phandle)
68 {
69 struct fdtbus_dma_controller *dma;
70
71 for (dma = fdtbus_dma; dma; dma = dma->dma_next) {
72 if (dma->dma_phandle == phandle) {
73 return dma;
74 }
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