2 * Copyright (c) 2016-2018 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_platform.h"
35 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/queue.h>
41 #include <sys/malloc.h>
42 #include <sys/limits.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
48 #include <machine/bus.h>
51 #include <dev/fdt/fdt_common.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
56 #include <dev/xdma/xdma.h>
61 * Multiple xDMA controllers may work with single DMA device,
62 * so we have global lock for physical channel management.
64 static struct sx xdma_sx;
66 #define XDMA_LOCK() sx_xlock(&xdma_sx)
67 #define XDMA_UNLOCK() sx_xunlock(&xdma_sx)
68 #define XDMA_ASSERT_LOCKED() sx_xassert(&xdma_sx, MA_OWNED)
71 * Allocate virtual xDMA channel.
74 xdma_channel_alloc(xdma_controller_t *xdma, uint32_t caps)
76 xdma_channel_t *xchan;
79 xchan = malloc(sizeof(xdma_channel_t), M_XDMA, M_WAITOK | M_ZERO);
85 /* Request a real channel from hardware driver. */
86 ret = XDMA_CHANNEL_ALLOC(xdma->dma_dev, xchan);
88 device_printf(xdma->dev,
89 "%s: Can't request hardware channel.\n", __func__);
96 TAILQ_INIT(&xchan->ie_handlers);
98 sx_init(&xchan->sx_lock, "xDMA chan");
99 sx_init(&xchan->sx_qin_lock, "xDMA qin");
100 sx_init(&xchan->sx_qout_lock, "xDMA qout");
101 sx_init(&xchan->sx_bank_lock, "xDMA bank");
102 sx_init(&xchan->sx_proc_lock, "xDMA proc");
104 TAILQ_INIT(&xchan->bank);
105 TAILQ_INIT(&xchan->queue_in);
106 TAILQ_INIT(&xchan->queue_out);
107 TAILQ_INIT(&xchan->processing);
109 TAILQ_INSERT_TAIL(&xdma->channels, xchan, xchan_next);
117 xdma_channel_free(xdma_channel_t *xchan)
119 xdma_controller_t *xdma;
123 KASSERT(xdma != NULL, ("xdma is NULL"));
127 /* Free the real DMA channel. */
128 err = XDMA_CHANNEL_FREE(xdma->dma_dev, xchan);
130 device_printf(xdma->dev,
131 "%s: Can't free real hw channel.\n", __func__);
136 if (xchan->flags & XCHAN_TYPE_SG)
137 xdma_channel_free_sg(xchan);
139 xdma_teardown_all_intr(xchan);
141 sx_destroy(&xchan->sx_lock);
142 sx_destroy(&xchan->sx_qin_lock);
143 sx_destroy(&xchan->sx_qout_lock);
144 sx_destroy(&xchan->sx_bank_lock);
145 sx_destroy(&xchan->sx_proc_lock);
147 TAILQ_REMOVE(&xdma->channels, xchan, xchan_next);
157 xdma_setup_intr(xdma_channel_t *xchan,
158 int (*cb)(void *, xdma_transfer_status_t *),
159 void *arg, void **ihandler)
161 struct xdma_intr_handler *ih;
162 xdma_controller_t *xdma;
165 KASSERT(xdma != NULL, ("xdma is NULL"));
169 device_printf(xdma->dev,
170 "%s: Can't setup interrupt handler.\n",
176 ih = malloc(sizeof(struct xdma_intr_handler),
177 M_XDMA, M_WAITOK | M_ZERO);
182 TAILQ_INSERT_TAIL(&xchan->ie_handlers, ih, ih_next);
185 if (ihandler != NULL)
192 xdma_teardown_intr(xdma_channel_t *xchan, struct xdma_intr_handler *ih)
194 xdma_controller_t *xdma;
197 KASSERT(xdma != NULL, ("xdma is NULL"));
201 device_printf(xdma->dev,
202 "%s: Can't teardown interrupt.\n", __func__);
206 TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next);
213 xdma_teardown_all_intr(xdma_channel_t *xchan)
215 struct xdma_intr_handler *ih_tmp;
216 struct xdma_intr_handler *ih;
217 xdma_controller_t *xdma;
220 KASSERT(xdma != NULL, ("xdma is NULL"));
222 TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp) {
223 TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next);
231 xdma_request(xdma_channel_t *xchan, struct xdma_request *req)
233 xdma_controller_t *xdma;
238 KASSERT(xdma != NULL, ("xdma is NULL"));
241 ret = XDMA_CHANNEL_REQUEST(xdma->dma_dev, xchan, req);
243 device_printf(xdma->dev,
244 "%s: Can't request a transfer.\n", __func__);
255 xdma_control(xdma_channel_t *xchan, enum xdma_command cmd)
257 xdma_controller_t *xdma;
261 KASSERT(xdma != NULL, ("xdma is NULL"));
263 ret = XDMA_CHANNEL_CONTROL(xdma->dma_dev, xchan, cmd);
265 device_printf(xdma->dev,
266 "%s: Can't process command.\n", __func__);
274 xdma_callback(xdma_channel_t *xchan, xdma_transfer_status_t *status)
276 struct xdma_intr_handler *ih_tmp;
277 struct xdma_intr_handler *ih;
278 xdma_controller_t *xdma;
281 KASSERT(xdma != NULL, ("xdma is NULL"));
283 TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp)
285 ih->cb(ih->cb_user, status);
287 if (xchan->flags & XCHAN_TYPE_SG)
288 xdma_queue_submit(xchan);
293 * Notify the DMA driver we have machine-dependent data in FDT.
296 xdma_ofw_md_data(xdma_controller_t *xdma, pcell_t *cells, int ncells)
300 ret = XDMA_OFW_MD_DATA(xdma->dma_dev,
301 cells, ncells, (void **)&xdma->data);
307 * Allocate xdma controller.
310 xdma_ofw_get(device_t dev, const char *prop)
312 phandle_t node, parent;
313 xdma_controller_t *xdma;
321 node = ofw_bus_get_node(dev);
324 "%s called on not ofw based device.\n", __func__);
326 error = ofw_bus_parse_xref_list_get_length(node,
327 "dmas", "#dma-cells", &ndmas);
330 "%s can't get dmas list.\n", __func__);
336 "%s dmas list is empty.\n", __func__);
340 error = ofw_bus_find_string_index(node, "dma-names", prop, &idx);
343 "%s can't find string index.\n", __func__);
347 error = ofw_bus_parse_xref_list_alloc(node, "dmas", "#dma-cells",
348 idx, &parent, &ncells, &cells);
351 "%s can't get dma device xref.\n", __func__);
355 dma_dev = OF_device_from_xref(parent);
356 if (dma_dev == NULL) {
358 "%s can't get dma device.\n", __func__);
362 xdma = malloc(sizeof(struct xdma_controller),
363 M_XDMA, M_WAITOK | M_ZERO);
365 xdma->dma_dev = dma_dev;
367 TAILQ_INIT(&xdma->channels);
369 xdma_ofw_md_data(xdma, cells, ncells);
370 free(cells, M_OFWPROP);
377 * Free xDMA controller object.
380 xdma_put(xdma_controller_t *xdma)
385 /* Ensure no channels allocated. */
386 if (!TAILQ_EMPTY(&xdma->channels)) {
387 device_printf(xdma->dev, "%s: Can't free xDMA\n", __func__);
391 free(xdma->data, M_DEVBUF);
403 sx_init(&xdma_sx, "xDMA");
406 SYSINIT(xdma, SI_SUB_DRIVERS, SI_ORDER_FIRST, xdma_init, NULL);