xref: /freebsd/sys/dev/xdma/xdma.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2016-2019 Ruslan Bukin <br@bsdpad.com>
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_platform.h"
36 #include <sys/param.h>
37 #include <sys/conf.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/queue.h>
41 #include <sys/kobj.h>
42 #include <sys/malloc.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 
49 #include <machine/bus.h>
50 
51 #ifdef FDT
52 #include <dev/fdt/fdt_common.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #endif
56 
57 #include <dev/xdma/xdma.h>
58 
59 #include <xdma_if.h>
60 
61 /*
62  * Multiple xDMA controllers may work with single DMA device,
63  * so we have global lock for physical channel management.
64  */
65 static struct mtx xdma_mtx;
66 
67 #define	XDMA_LOCK()			mtx_lock(&xdma_mtx)
68 #define	XDMA_UNLOCK()			mtx_unlock(&xdma_mtx)
69 #define	XDMA_ASSERT_LOCKED()		mtx_assert(&xdma_mtx, MA_OWNED)
70 
71 #define	FDT_REG_CELLS	4
72 
73 /*
74  * Allocate virtual xDMA channel.
75  */
76 xdma_channel_t *
77 xdma_channel_alloc(xdma_controller_t *xdma, uint32_t caps)
78 {
79 	xdma_channel_t *xchan;
80 	int ret;
81 
82 	xchan = malloc(sizeof(xdma_channel_t), M_XDMA, M_WAITOK | M_ZERO);
83 	xchan->xdma = xdma;
84 	xchan->caps = caps;
85 
86 	XDMA_LOCK();
87 
88 	/* Request a real channel from hardware driver. */
89 	ret = XDMA_CHANNEL_ALLOC(xdma->dma_dev, xchan);
90 	if (ret != 0) {
91 		device_printf(xdma->dev,
92 		    "%s: Can't request hardware channel.\n", __func__);
93 		XDMA_UNLOCK();
94 		free(xchan, M_XDMA);
95 
96 		return (NULL);
97 	}
98 
99 	TAILQ_INIT(&xchan->ie_handlers);
100 
101 	mtx_init(&xchan->mtx_lock, "xDMA chan", NULL, MTX_DEF);
102 	mtx_init(&xchan->mtx_qin_lock, "xDMA qin", NULL, MTX_DEF);
103 	mtx_init(&xchan->mtx_qout_lock, "xDMA qout", NULL, MTX_DEF);
104 	mtx_init(&xchan->mtx_bank_lock, "xDMA bank", NULL, MTX_DEF);
105 	mtx_init(&xchan->mtx_proc_lock, "xDMA proc", NULL, MTX_DEF);
106 
107 	TAILQ_INIT(&xchan->bank);
108 	TAILQ_INIT(&xchan->queue_in);
109 	TAILQ_INIT(&xchan->queue_out);
110 	TAILQ_INIT(&xchan->processing);
111 
112 	TAILQ_INSERT_TAIL(&xdma->channels, xchan, xchan_next);
113 
114 	XDMA_UNLOCK();
115 
116 	return (xchan);
117 }
118 
119 int
120 xdma_channel_free(xdma_channel_t *xchan)
121 {
122 	xdma_controller_t *xdma;
123 	int err;
124 
125 	xdma = xchan->xdma;
126 	KASSERT(xdma != NULL, ("xdma is NULL"));
127 
128 	XDMA_LOCK();
129 
130 	/* Free the real DMA channel. */
131 	err = XDMA_CHANNEL_FREE(xdma->dma_dev, xchan);
132 	if (err != 0) {
133 		device_printf(xdma->dev,
134 		    "%s: Can't free real hw channel.\n", __func__);
135 		XDMA_UNLOCK();
136 		return (-1);
137 	}
138 
139 	if (xchan->flags & XCHAN_TYPE_SG)
140 		xdma_channel_free_sg(xchan);
141 
142 	xdma_teardown_all_intr(xchan);
143 
144 	mtx_destroy(&xchan->mtx_lock);
145 	mtx_destroy(&xchan->mtx_qin_lock);
146 	mtx_destroy(&xchan->mtx_qout_lock);
147 	mtx_destroy(&xchan->mtx_bank_lock);
148 	mtx_destroy(&xchan->mtx_proc_lock);
149 
150 	TAILQ_REMOVE(&xdma->channels, xchan, xchan_next);
151 
152 	free(xchan, M_XDMA);
153 
154 	XDMA_UNLOCK();
155 
156 	return (0);
157 }
158 
159 int
160 xdma_setup_intr(xdma_channel_t *xchan,
161     int (*cb)(void *, xdma_transfer_status_t *),
162     void *arg, void **ihandler)
163 {
164 	struct xdma_intr_handler *ih;
165 	xdma_controller_t *xdma;
166 
167 	xdma = xchan->xdma;
168 	KASSERT(xdma != NULL, ("xdma is NULL"));
169 
170 	/* Sanity check. */
171 	if (cb == NULL) {
172 		device_printf(xdma->dev,
173 		    "%s: Can't setup interrupt handler.\n",
174 		    __func__);
175 
176 		return (-1);
177 	}
178 
179 	ih = malloc(sizeof(struct xdma_intr_handler),
180 	    M_XDMA, M_WAITOK | M_ZERO);
181 	ih->cb = cb;
182 	ih->cb_user = arg;
183 
184 	XCHAN_LOCK(xchan);
185 	TAILQ_INSERT_TAIL(&xchan->ie_handlers, ih, ih_next);
186 	XCHAN_UNLOCK(xchan);
187 
188 	if (ihandler != NULL)
189 		*ihandler = ih;
190 
191 	return (0);
192 }
193 
194 int
195 xdma_teardown_intr(xdma_channel_t *xchan, struct xdma_intr_handler *ih)
196 {
197 	xdma_controller_t *xdma;
198 
199 	xdma = xchan->xdma;
200 	KASSERT(xdma != NULL, ("xdma is NULL"));
201 
202 	/* Sanity check. */
203 	if (ih == NULL) {
204 		device_printf(xdma->dev,
205 		    "%s: Can't teardown interrupt.\n", __func__);
206 		return (-1);
207 	}
208 
209 	TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next);
210 	free(ih, M_XDMA);
211 
212 	return (0);
213 }
214 
215 int
216 xdma_teardown_all_intr(xdma_channel_t *xchan)
217 {
218 	struct xdma_intr_handler *ih_tmp;
219 	struct xdma_intr_handler *ih;
220 	xdma_controller_t *xdma;
221 
222 	xdma = xchan->xdma;
223 	KASSERT(xdma != NULL, ("xdma is NULL"));
224 
225 	TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp) {
226 		TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next);
227 		free(ih, M_XDMA);
228 	}
229 
230 	return (0);
231 }
232 
233 int
234 xdma_request(xdma_channel_t *xchan, struct xdma_request *req)
235 {
236 	xdma_controller_t *xdma;
237 	int ret;
238 
239 	xdma = xchan->xdma;
240 
241 	KASSERT(xdma != NULL, ("xdma is NULL"));
242 
243 	XCHAN_LOCK(xchan);
244 	ret = XDMA_CHANNEL_REQUEST(xdma->dma_dev, xchan, req);
245 	if (ret != 0) {
246 		device_printf(xdma->dev,
247 		    "%s: Can't request a transfer.\n", __func__);
248 		XCHAN_UNLOCK(xchan);
249 
250 		return (-1);
251 	}
252 	XCHAN_UNLOCK(xchan);
253 
254 	return (0);
255 }
256 
257 int
258 xdma_control(xdma_channel_t *xchan, enum xdma_command cmd)
259 {
260 	xdma_controller_t *xdma;
261 	int ret;
262 
263 	xdma = xchan->xdma;
264 	KASSERT(xdma != NULL, ("xdma is NULL"));
265 
266 	ret = XDMA_CHANNEL_CONTROL(xdma->dma_dev, xchan, cmd);
267 	if (ret != 0) {
268 		device_printf(xdma->dev,
269 		    "%s: Can't process command.\n", __func__);
270 		return (-1);
271 	}
272 
273 	return (0);
274 }
275 
276 void
277 xdma_callback(xdma_channel_t *xchan, xdma_transfer_status_t *status)
278 {
279 	struct xdma_intr_handler *ih_tmp;
280 	struct xdma_intr_handler *ih;
281 	xdma_controller_t *xdma;
282 
283 	xdma = xchan->xdma;
284 	KASSERT(xdma != NULL, ("xdma is NULL"));
285 
286 	TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp)
287 		if (ih->cb != NULL)
288 			ih->cb(ih->cb_user, status);
289 
290 	if (xchan->flags & XCHAN_TYPE_SG)
291 		xdma_queue_submit(xchan);
292 }
293 
294 #ifdef FDT
295 /*
296  * Notify the DMA driver we have machine-dependent data in FDT.
297  */
298 static int
299 xdma_ofw_md_data(xdma_controller_t *xdma, pcell_t *cells, int ncells)
300 {
301 	uint32_t ret;
302 
303 	ret = XDMA_OFW_MD_DATA(xdma->dma_dev,
304 	    cells, ncells, (void **)&xdma->data);
305 
306 	return (ret);
307 }
308 
309 static int
310 xdma_handle_mem_node(vmem_t *vmem, phandle_t memory)
311 {
312 	pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
313 	pcell_t *regp;
314 	int addr_cells, size_cells;
315 	int i, reg_len, ret, tuple_size, tuples;
316 	u_long mem_start, mem_size;
317 
318 	if ((ret = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
319 	    &size_cells)) != 0)
320 		return (ret);
321 
322 	if (addr_cells > 2)
323 		return (ERANGE);
324 
325 	tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
326 	reg_len = OF_getproplen(memory, "reg");
327 	if (reg_len <= 0 || reg_len > sizeof(reg))
328 		return (ERANGE);
329 
330 	if (OF_getprop(memory, "reg", reg, reg_len) <= 0)
331 		return (ENXIO);
332 
333 	tuples = reg_len / tuple_size;
334 	regp = (pcell_t *)&reg;
335 	for (i = 0; i < tuples; i++) {
336 		ret = fdt_data_to_res(regp, addr_cells, size_cells,
337 		    &mem_start, &mem_size);
338 		if (ret != 0)
339 			return (ret);
340 
341 		vmem_add(vmem, mem_start, mem_size, 0);
342 		regp += addr_cells + size_cells;
343 	}
344 
345 	return (0);
346 }
347 
348 vmem_t *
349 xdma_get_memory(device_t dev)
350 {
351 	phandle_t mem_node, node;
352 	pcell_t mem_handle;
353 	vmem_t *vmem;
354 
355 	node = ofw_bus_get_node(dev);
356 	if (node <= 0) {
357 		device_printf(dev,
358 		    "%s called on not ofw based device.\n", __func__);
359 		return (NULL);
360 	}
361 
362 	if (!OF_hasprop(node, "memory-region"))
363 		return (NULL);
364 
365 	if (OF_getencprop(node, "memory-region", (void *)&mem_handle,
366 	    sizeof(mem_handle)) <= 0)
367 		return (NULL);
368 
369 	vmem = vmem_create("xDMA vmem", 0, 0, PAGE_SIZE,
370 	    PAGE_SIZE, M_BESTFIT | M_WAITOK);
371 	if (vmem == NULL)
372 		return (NULL);
373 
374 	mem_node = OF_node_from_xref(mem_handle);
375 	if (xdma_handle_mem_node(vmem, mem_node) != 0) {
376 		vmem_destroy(vmem);
377 		return (NULL);
378 	}
379 
380 	return (vmem);
381 }
382 
383 void
384 xdma_put_memory(vmem_t *vmem)
385 {
386 
387 	vmem_destroy(vmem);
388 }
389 
390 void
391 xchan_set_memory(xdma_channel_t *xchan, vmem_t *vmem)
392 {
393 
394 	xchan->vmem = vmem;
395 }
396 
397 /*
398  * Allocate xdma controller.
399  */
400 xdma_controller_t *
401 xdma_ofw_get(device_t dev, const char *prop)
402 {
403 	phandle_t node, parent;
404 	xdma_controller_t *xdma;
405 	device_t dma_dev;
406 	pcell_t *cells;
407 	int ncells;
408 	int error;
409 	int ndmas;
410 	int idx;
411 
412 	node = ofw_bus_get_node(dev);
413 	if (node <= 0)
414 		device_printf(dev,
415 		    "%s called on not ofw based device.\n", __func__);
416 
417 	error = ofw_bus_parse_xref_list_get_length(node,
418 	    "dmas", "#dma-cells", &ndmas);
419 	if (error) {
420 		device_printf(dev,
421 		    "%s can't get dmas list.\n", __func__);
422 		return (NULL);
423 	}
424 
425 	if (ndmas == 0) {
426 		device_printf(dev,
427 		    "%s dmas list is empty.\n", __func__);
428 		return (NULL);
429 	}
430 
431 	error = ofw_bus_find_string_index(node, "dma-names", prop, &idx);
432 	if (error != 0) {
433 		device_printf(dev,
434 		    "%s can't find string index.\n", __func__);
435 		return (NULL);
436 	}
437 
438 	error = ofw_bus_parse_xref_list_alloc(node, "dmas", "#dma-cells",
439 	    idx, &parent, &ncells, &cells);
440 	if (error != 0) {
441 		device_printf(dev,
442 		    "%s can't get dma device xref.\n", __func__);
443 		return (NULL);
444 	}
445 
446 	dma_dev = OF_device_from_xref(parent);
447 	if (dma_dev == NULL) {
448 		device_printf(dev,
449 		    "%s can't get dma device.\n", __func__);
450 		return (NULL);
451 	}
452 
453 	xdma = malloc(sizeof(struct xdma_controller),
454 	    M_XDMA, M_WAITOK | M_ZERO);
455 	xdma->dev = dev;
456 	xdma->dma_dev = dma_dev;
457 
458 	TAILQ_INIT(&xdma->channels);
459 
460 	xdma_ofw_md_data(xdma, cells, ncells);
461 	free(cells, M_OFWPROP);
462 
463 	return (xdma);
464 }
465 #endif
466 
467 /*
468  * Free xDMA controller object.
469  */
470 int
471 xdma_put(xdma_controller_t *xdma)
472 {
473 
474 	XDMA_LOCK();
475 
476 	/* Ensure no channels allocated. */
477 	if (!TAILQ_EMPTY(&xdma->channels)) {
478 		device_printf(xdma->dev, "%s: Can't free xDMA\n", __func__);
479 		return (-1);
480 	}
481 
482 	free(xdma->data, M_DEVBUF);
483 	free(xdma, M_XDMA);
484 
485 	XDMA_UNLOCK();
486 
487 	return (0);
488 }
489 
490 static void
491 xdma_init(void)
492 {
493 
494 	mtx_init(&xdma_mtx, "xDMA", NULL, MTX_DEF);
495 }
496 
497 SYSINIT(xdma, SI_SUB_DRIVERS, SI_ORDER_FIRST, xdma_init, NULL);
498