2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2016-2019 Ruslan Bukin <br@bsdpad.com>
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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_platform.h"
36 #include <sys/param.h>
39 #include <sys/epoch.h>
40 #include <sys/kernel.h>
41 #include <sys/queue.h>
43 #include <sys/malloc.h>
44 #include <sys/limits.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
50 #include <machine/bus.h>
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
58 #include <dev/xdma/xdma.h>
63 * Multiple xDMA controllers may work with single DMA device,
64 * so we have global lock for physical channel management.
66 static struct mtx xdma_mtx;
68 #define XDMA_LOCK() mtx_lock(&xdma_mtx)
69 #define XDMA_UNLOCK() mtx_unlock(&xdma_mtx)
70 #define XDMA_ASSERT_LOCKED() mtx_assert(&xdma_mtx, MA_OWNED)
72 #define FDT_REG_CELLS 4
76 xdma_get_iommu_fdt(xdma_controller_t *xdma, xdma_channel_t *xchan)
78 struct xdma_iommu *xio;
83 node = ofw_bus_get_node(xdma->dma_dev);
84 if (OF_getproplen(node, "xdma,iommu") <= 0)
87 len = OF_getencprop(node, "xdma,iommu", &prop, sizeof(prop));
88 if (len != sizeof(prop)) {
89 device_printf(xdma->dev,
90 "%s: Can't get iommu device node\n", __func__);
95 xio->dev = OF_device_from_xref(prop);
96 if (xio->dev == NULL) {
97 device_printf(xdma->dev,
98 "%s: Can't get iommu device\n", __func__);
108 * Allocate virtual xDMA channel.
111 xdma_channel_alloc(xdma_controller_t *xdma, uint32_t caps)
113 xdma_channel_t *xchan;
116 xchan = malloc(sizeof(xdma_channel_t), M_XDMA, M_WAITOK | M_ZERO);
120 /* Check if this DMA controller supports IOMMU. */
121 if (xdma_get_iommu_fdt(xdma, xchan))
122 caps |= XCHAN_CAP_IOMMU | XCHAN_CAP_NOSEG;
129 /* Request a real channel from hardware driver. */
130 ret = XDMA_CHANNEL_ALLOC(xdma->dma_dev, xchan);
132 device_printf(xdma->dev,
133 "%s: Can't request hardware channel.\n", __func__);
140 TAILQ_INIT(&xchan->ie_handlers);
142 mtx_init(&xchan->mtx_lock, "xDMA chan", NULL, MTX_DEF);
143 mtx_init(&xchan->mtx_qin_lock, "xDMA qin", NULL, MTX_DEF);
144 mtx_init(&xchan->mtx_qout_lock, "xDMA qout", NULL, MTX_DEF);
145 mtx_init(&xchan->mtx_bank_lock, "xDMA bank", NULL, MTX_DEF);
146 mtx_init(&xchan->mtx_proc_lock, "xDMA proc", NULL, MTX_DEF);
148 TAILQ_INIT(&xchan->bank);
149 TAILQ_INIT(&xchan->queue_in);
150 TAILQ_INIT(&xchan->queue_out);
151 TAILQ_INIT(&xchan->processing);
153 if (xchan->caps & XCHAN_CAP_IOMMU)
154 xdma_iommu_init(&xchan->xio);
156 TAILQ_INSERT_TAIL(&xdma->channels, xchan, xchan_next);
164 xdma_channel_free(xdma_channel_t *xchan)
166 xdma_controller_t *xdma;
170 KASSERT(xdma != NULL, ("xdma is NULL"));
174 /* Free the real DMA channel. */
175 err = XDMA_CHANNEL_FREE(xdma->dma_dev, xchan);
177 device_printf(xdma->dev,
178 "%s: Can't free real hw channel.\n", __func__);
183 if (xchan->flags & XCHAN_TYPE_SG)
184 xdma_channel_free_sg(xchan);
186 if (xchan->caps & XCHAN_CAP_IOMMU)
187 xdma_iommu_release(&xchan->xio);
189 xdma_teardown_all_intr(xchan);
191 mtx_destroy(&xchan->mtx_lock);
192 mtx_destroy(&xchan->mtx_qin_lock);
193 mtx_destroy(&xchan->mtx_qout_lock);
194 mtx_destroy(&xchan->mtx_bank_lock);
195 mtx_destroy(&xchan->mtx_proc_lock);
197 TAILQ_REMOVE(&xdma->channels, xchan, xchan_next);
207 xdma_setup_intr(xdma_channel_t *xchan, int flags,
208 int (*cb)(void *, xdma_transfer_status_t *),
209 void *arg, void **ihandler)
211 struct xdma_intr_handler *ih;
212 xdma_controller_t *xdma;
215 KASSERT(xdma != NULL, ("xdma is NULL"));
219 device_printf(xdma->dev,
220 "%s: Can't setup interrupt handler.\n",
226 ih = malloc(sizeof(struct xdma_intr_handler),
227 M_XDMA, M_WAITOK | M_ZERO);
233 TAILQ_INSERT_TAIL(&xchan->ie_handlers, ih, ih_next);
236 if (ihandler != NULL)
243 xdma_teardown_intr(xdma_channel_t *xchan, struct xdma_intr_handler *ih)
245 xdma_controller_t *xdma;
248 KASSERT(xdma != NULL, ("xdma is NULL"));
252 device_printf(xdma->dev,
253 "%s: Can't teardown interrupt.\n", __func__);
257 TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next);
264 xdma_teardown_all_intr(xdma_channel_t *xchan)
266 struct xdma_intr_handler *ih_tmp;
267 struct xdma_intr_handler *ih;
268 xdma_controller_t *xdma;
271 KASSERT(xdma != NULL, ("xdma is NULL"));
273 TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp) {
274 TAILQ_REMOVE(&xchan->ie_handlers, ih, ih_next);
282 xdma_request(xdma_channel_t *xchan, struct xdma_request *req)
284 xdma_controller_t *xdma;
289 KASSERT(xdma != NULL, ("xdma is NULL"));
292 ret = XDMA_CHANNEL_REQUEST(xdma->dma_dev, xchan, req);
294 device_printf(xdma->dev,
295 "%s: Can't request a transfer.\n", __func__);
306 xdma_control(xdma_channel_t *xchan, enum xdma_command cmd)
308 xdma_controller_t *xdma;
312 KASSERT(xdma != NULL, ("xdma is NULL"));
314 ret = XDMA_CHANNEL_CONTROL(xdma->dma_dev, xchan, cmd);
316 device_printf(xdma->dev,
317 "%s: Can't process command.\n", __func__);
325 xdma_callback(xdma_channel_t *xchan, xdma_transfer_status_t *status)
327 struct xdma_intr_handler *ih_tmp;
328 struct xdma_intr_handler *ih;
329 xdma_controller_t *xdma;
330 struct epoch_tracker et;
333 KASSERT(xdma != NULL, ("xdma is NULL"));
335 TAILQ_FOREACH_SAFE(ih, &xchan->ie_handlers, ih_next, ih_tmp) {
336 if (ih->cb != NULL) {
337 if (ih->flags & XDMA_INTR_NET)
339 ih->cb(ih->cb_user, status);
340 if (ih->flags & XDMA_INTR_NET)
345 if (xchan->flags & XCHAN_TYPE_SG)
346 xdma_queue_submit(xchan);
351 * Notify the DMA driver we have machine-dependent data in FDT.
354 xdma_ofw_md_data(xdma_controller_t *xdma, pcell_t *cells, int ncells)
358 ret = XDMA_OFW_MD_DATA(xdma->dma_dev,
359 cells, ncells, (void **)&xdma->data);
365 xdma_handle_mem_node(vmem_t *vmem, phandle_t memory)
367 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
369 int addr_cells, size_cells;
370 int i, reg_len, ret, tuple_size, tuples;
371 u_long mem_start, mem_size;
373 if ((ret = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
380 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
381 reg_len = OF_getproplen(memory, "reg");
382 if (reg_len <= 0 || reg_len > sizeof(reg))
385 if (OF_getprop(memory, "reg", reg, reg_len) <= 0)
388 tuples = reg_len / tuple_size;
389 regp = (pcell_t *)®
390 for (i = 0; i < tuples; i++) {
391 ret = fdt_data_to_res(regp, addr_cells, size_cells,
392 &mem_start, &mem_size);
396 vmem_add(vmem, mem_start, mem_size, 0);
397 regp += addr_cells + size_cells;
404 xdma_get_memory(device_t dev)
406 phandle_t mem_node, node;
410 node = ofw_bus_get_node(dev);
413 "%s called on not ofw based device.\n", __func__);
417 if (!OF_hasprop(node, "memory-region"))
420 if (OF_getencprop(node, "memory-region", (void *)&mem_handle,
421 sizeof(mem_handle)) <= 0)
424 vmem = vmem_create("xDMA vmem", 0, 0, PAGE_SIZE,
425 PAGE_SIZE, M_BESTFIT | M_WAITOK);
429 mem_node = OF_node_from_xref(mem_handle);
430 if (xdma_handle_mem_node(vmem, mem_node) != 0) {
439 xdma_put_memory(vmem_t *vmem)
446 xchan_set_memory(xdma_channel_t *xchan, vmem_t *vmem)
453 * Allocate xdma controller.
456 xdma_ofw_get(device_t dev, const char *prop)
458 phandle_t node, parent;
459 xdma_controller_t *xdma;
467 node = ofw_bus_get_node(dev);
470 "%s called on not ofw based device.\n", __func__);
472 error = ofw_bus_parse_xref_list_get_length(node,
473 "dmas", "#dma-cells", &ndmas);
476 "%s can't get dmas list.\n", __func__);
482 "%s dmas list is empty.\n", __func__);
486 error = ofw_bus_find_string_index(node, "dma-names", prop, &idx);
489 "%s can't find string index.\n", __func__);
493 error = ofw_bus_parse_xref_list_alloc(node, "dmas", "#dma-cells",
494 idx, &parent, &ncells, &cells);
497 "%s can't get dma device xref.\n", __func__);
501 dma_dev = OF_device_from_xref(parent);
502 if (dma_dev == NULL) {
504 "%s can't get dma device.\n", __func__);
508 xdma = malloc(sizeof(struct xdma_controller),
509 M_XDMA, M_WAITOK | M_ZERO);
511 xdma->dma_dev = dma_dev;
513 TAILQ_INIT(&xdma->channels);
515 xdma_ofw_md_data(xdma, cells, ncells);
516 free(cells, M_OFWPROP);
523 * Allocate xdma controller.
526 xdma_get(device_t dev, device_t dma_dev)
528 xdma_controller_t *xdma;
530 xdma = malloc(sizeof(struct xdma_controller),
531 M_XDMA, M_WAITOK | M_ZERO);
533 xdma->dma_dev = dma_dev;
535 TAILQ_INIT(&xdma->channels);
541 * Free xDMA controller object.
544 xdma_put(xdma_controller_t *xdma)
549 /* Ensure no channels allocated. */
550 if (!TAILQ_EMPTY(&xdma->channels)) {
551 device_printf(xdma->dev, "%s: Can't free xDMA\n", __func__);
555 free(xdma->data, M_DEVBUF);
567 mtx_init(&xdma_mtx, "xDMA", NULL, MTX_DEF);
570 SYSINIT(xdma, SI_SUB_DRIVERS, SI_ORDER_FIRST, xdma_init, NULL);