xref: /freebsd/sys/dev/xdma/xdma.c (revision b056a52947aa19921787a77db886a8162f2347b6)
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/sysctl.h>
46 #include <sys/systm.h>
47 
48 #include <machine/bus.h>
49 
50 #ifdef FDT
51 #include <dev/fdt/fdt_common.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 #endif
55 
56 #include <dev/xdma/xdma.h>
57 
58 #include <xdma_if.h>
59 
60 /*
61  * Multiple xDMA controllers may work with single DMA device,
62  * so we have global lock for physical channel management.
63  */
64 static struct mtx xdma_mtx;
65 
66 #define	XDMA_LOCK()			mtx_lock(&xdma_mtx)
67 #define	XDMA_UNLOCK()			mtx_unlock(&xdma_mtx)
68 #define	XDMA_ASSERT_LOCKED()		mtx_assert(&xdma_mtx, MA_OWNED)
69 
70 #define	FDT_REG_CELLS	4
71 
72 /*
73  * Allocate virtual xDMA channel.
74  */
75 xdma_channel_t *
76 xdma_channel_alloc(xdma_controller_t *xdma, uint32_t caps)
77 {
78 	xdma_channel_t *xchan;
79 	int ret;
80 
81 	xchan = malloc(sizeof(xdma_channel_t), M_XDMA, M_WAITOK | M_ZERO);
82 	xchan->xdma = xdma;
83 	xchan->caps = caps;
84 
85 	XDMA_LOCK();
86 
87 	/* Request a real channel from hardware driver. */
88 	ret = XDMA_CHANNEL_ALLOC(xdma->dma_dev, xchan);
89 	if (ret != 0) {
90 		device_printf(xdma->dev,
91 		    "%s: Can't request hardware channel.\n", __func__);
92 		XDMA_UNLOCK();
93 		free(xchan, M_XDMA);
94 
95 		return (NULL);
96 	}
97 
98 	TAILQ_INIT(&xchan->ie_handlers);
99 
100 	mtx_init(&xchan->mtx_lock, "xDMA chan", NULL, MTX_DEF);
101 	mtx_init(&xchan->mtx_qin_lock, "xDMA qin", NULL, MTX_DEF);
102 	mtx_init(&xchan->mtx_qout_lock, "xDMA qout", NULL, MTX_DEF);
103 	mtx_init(&xchan->mtx_bank_lock, "xDMA bank", NULL, MTX_DEF);
104 	mtx_init(&xchan->mtx_proc_lock, "xDMA proc", NULL, MTX_DEF);
105 
106 	TAILQ_INIT(&xchan->bank);
107 	TAILQ_INIT(&xchan->queue_in);
108 	TAILQ_INIT(&xchan->queue_out);
109 	TAILQ_INIT(&xchan->processing);
110 
111 	TAILQ_INSERT_TAIL(&xdma->channels, xchan, xchan_next);
112 
113 	XDMA_UNLOCK();
114 
115 	return (xchan);
116 }
117 
118 int
119 xdma_channel_free(xdma_channel_t *xchan)
120 {
121 	xdma_controller_t *xdma;
122 	int err;
123 
124 	xdma = xchan->xdma;
125 	KASSERT(xdma != NULL, ("xdma is NULL"));
126 
127 	XDMA_LOCK();
128 
129 	/* Free the real DMA channel. */
130 	err = XDMA_CHANNEL_FREE(xdma->dma_dev, xchan);
131 	if (err != 0) {
132 		device_printf(xdma->dev,
133 		    "%s: Can't free real hw channel.\n", __func__);
134 		XDMA_UNLOCK();
135 		return (-1);
136 	}
137 
138 	if (xchan->flags & XCHAN_TYPE_SG)
139 		xdma_channel_free_sg(xchan);
140 
141 	xdma_teardown_all_intr(xchan);
142 
143 	mtx_destroy(&xchan->mtx_lock);
144 	mtx_destroy(&xchan->mtx_qin_lock);
145 	mtx_destroy(&xchan->mtx_qout_lock);
146 	mtx_destroy(&xchan->mtx_bank_lock);
147 	mtx_destroy(&xchan->mtx_proc_lock);
148 
149 	TAILQ_REMOVE(&xdma->channels, xchan, xchan_next);
150 
151 	free(xchan, M_XDMA);
152 
153 	XDMA_UNLOCK();
154 
155 	return (0);
156 }
157 
158 int
159 xdma_setup_intr(xdma_channel_t *xchan,
160     int (*cb)(void *, xdma_transfer_status_t *),
161     void *arg, void **ihandler)
162 {
163 	struct xdma_intr_handler *ih;
164 	xdma_controller_t *xdma;
165 
166 	xdma = xchan->xdma;
167 	KASSERT(xdma != NULL, ("xdma is NULL"));
168 
169 	/* Sanity check. */
170 	if (cb == NULL) {
171 		device_printf(xdma->dev,
172 		    "%s: Can't setup interrupt handler.\n",
173 		    __func__);
174 
175 		return (-1);
176 	}
177 
178 	ih = malloc(sizeof(struct xdma_intr_handler),
179 	    M_XDMA, M_WAITOK | M_ZERO);
180 	ih->cb = cb;
181 	ih->cb_user = arg;
182 
183 	XCHAN_LOCK(xchan);
184 	TAILQ_INSERT_TAIL(&xchan->ie_handlers, ih, ih_next);
185 	XCHAN_UNLOCK(xchan);
186 
187 	if (ihandler != NULL)
188 		*ihandler = ih;
189 
190 	return (0);
191 }
192 
193 int
194 xdma_teardown_intr(xdma_channel_t *xchan, struct xdma_intr_handler *ih)
195 {
196 	xdma_controller_t *xdma;
197 
198 	xdma = xchan->xdma;
199 	KASSERT(xdma != NULL, ("xdma is NULL"));
200 
201 	/* Sanity check. */
202 	if (ih == NULL) {
203 		device_printf(xdma->dev,
204 		    "%s: Can't teardown interrupt.\n", __func__);
205 		return (-1);
206 	}
207 
208 	TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next);
209 	free(ih, M_XDMA);
210 
211 	return (0);
212 }
213 
214 int
215 xdma_teardown_all_intr(xdma_channel_t *xchan)
216 {
217 	struct xdma_intr_handler *ih_tmp;
218 	struct xdma_intr_handler *ih;
219 	xdma_controller_t *xdma;
220 
221 	xdma = xchan->xdma;
222 	KASSERT(xdma != NULL, ("xdma is NULL"));
223 
224 	TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp) {
225 		TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next);
226 		free(ih, M_XDMA);
227 	}
228 
229 	return (0);
230 }
231 
232 int
233 xdma_request(xdma_channel_t *xchan, struct xdma_request *req)
234 {
235 	xdma_controller_t *xdma;
236 	int ret;
237 
238 	xdma = xchan->xdma;
239 
240 	KASSERT(xdma != NULL, ("xdma is NULL"));
241 
242 	XCHAN_LOCK(xchan);
243 	ret = XDMA_CHANNEL_REQUEST(xdma->dma_dev, xchan, req);
244 	if (ret != 0) {
245 		device_printf(xdma->dev,
246 		    "%s: Can't request a transfer.\n", __func__);
247 		XCHAN_UNLOCK(xchan);
248 
249 		return (-1);
250 	}
251 	XCHAN_UNLOCK(xchan);
252 
253 	return (0);
254 }
255 
256 int
257 xdma_control(xdma_channel_t *xchan, enum xdma_command cmd)
258 {
259 	xdma_controller_t *xdma;
260 	int ret;
261 
262 	xdma = xchan->xdma;
263 	KASSERT(xdma != NULL, ("xdma is NULL"));
264 
265 	ret = XDMA_CHANNEL_CONTROL(xdma->dma_dev, xchan, cmd);
266 	if (ret != 0) {
267 		device_printf(xdma->dev,
268 		    "%s: Can't process command.\n", __func__);
269 		return (-1);
270 	}
271 
272 	return (0);
273 }
274 
275 void
276 xdma_callback(xdma_channel_t *xchan, xdma_transfer_status_t *status)
277 {
278 	struct xdma_intr_handler *ih_tmp;
279 	struct xdma_intr_handler *ih;
280 	xdma_controller_t *xdma;
281 
282 	xdma = xchan->xdma;
283 	KASSERT(xdma != NULL, ("xdma is NULL"));
284 
285 	TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp)
286 		if (ih->cb != NULL)
287 			ih->cb(ih->cb_user, status);
288 
289 	if (xchan->flags & XCHAN_TYPE_SG)
290 		xdma_queue_submit(xchan);
291 }
292 
293 #ifdef FDT
294 /*
295  * Notify the DMA driver we have machine-dependent data in FDT.
296  */
297 static int
298 xdma_ofw_md_data(xdma_controller_t *xdma, pcell_t *cells, int ncells)
299 {
300 	uint32_t ret;
301 
302 	ret = XDMA_OFW_MD_DATA(xdma->dma_dev,
303 	    cells, ncells, (void **)&xdma->data);
304 
305 	return (ret);
306 }
307 
308 static int
309 xdma_handle_mem_node(vmem_t *vmem, phandle_t memory)
310 {
311 	pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
312 	pcell_t *regp;
313 	int addr_cells, size_cells;
314 	int i, reg_len, ret, tuple_size, tuples;
315 	vmem_addr_t mem_start;
316 	vmem_size_t 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