2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2018-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/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/rwlock.h>
44 #include <machine/bus.h>
47 #include <vm/vm_extern.h>
48 #include <vm/vm_page.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>
60 struct seg_load_request {
61 struct bus_dma_segment *seg;
67 xchan_bufs_free_reserved(xdma_channel_t *xchan)
69 struct xdma_request *xr;
73 for (i = 0; i < xchan->xr_num; i++) {
74 xr = &xchan->xr_mem[i];
77 pmap_kremove_device(xr->buf.vaddr, size);
78 kva_free(xr->buf.vaddr, size);
82 vmem_free(xchan->vmem, xr->buf.paddr, size);
90 xchan_bufs_alloc_reserved(xdma_channel_t *xchan)
92 xdma_controller_t *xdma;
93 struct xdma_request *xr;
100 if (xchan->vmem == NULL)
103 for (i = 0; i < xchan->xr_num; i++) {
104 xr = &xchan->xr_mem[i];
105 size = round_page(xchan->maxsegsize);
106 if (vmem_alloc(xchan->vmem, size,
107 M_BESTFIT | M_NOWAIT, &addr)) {
108 device_printf(xdma->dev,
109 "%s: Can't allocate memory\n", __func__);
110 xchan_bufs_free_reserved(xchan);
115 xr->buf.paddr = addr;
116 xr->buf.vaddr = kva_alloc(size);
117 if (xr->buf.vaddr == 0) {
118 device_printf(xdma->dev,
119 "%s: Can't allocate KVA\n", __func__);
120 xchan_bufs_free_reserved(xchan);
123 pmap_kenter_device(xr->buf.vaddr, size, addr);
130 xchan_bufs_alloc_busdma(xdma_channel_t *xchan)
132 xdma_controller_t *xdma;
133 struct xdma_request *xr;
139 /* Create bus_dma tag */
140 err = bus_dma_tag_create(
141 bus_get_dma_tag(xdma->dev), /* Parent tag. */
142 xchan->alignment, /* alignment */
143 xchan->boundary, /* boundary */
144 xchan->lowaddr, /* lowaddr */
145 xchan->highaddr, /* highaddr */
146 NULL, NULL, /* filter, filterarg */
147 xchan->maxsegsize * xchan->maxnsegs, /* maxsize */
148 xchan->maxnsegs, /* nsegments */
149 xchan->maxsegsize, /* maxsegsize */
151 NULL, NULL, /* lockfunc, lockarg */
152 &xchan->dma_tag_bufs);
154 device_printf(xdma->dev,
155 "%s: Can't create bus_dma tag.\n", __func__);
159 for (i = 0; i < xchan->xr_num; i++) {
160 xr = &xchan->xr_mem[i];
161 err = bus_dmamap_create(xchan->dma_tag_bufs, 0,
164 device_printf(xdma->dev,
165 "%s: Can't create buf DMA map.\n", __func__);
168 bus_dma_tag_destroy(xchan->dma_tag_bufs);
178 xchan_bufs_alloc(xdma_channel_t *xchan)
180 xdma_controller_t *xdma;
186 device_printf(xdma->dev,
187 "%s: Channel was not allocated properly.\n", __func__);
191 if (xchan->caps & XCHAN_CAP_BUSDMA)
192 ret = xchan_bufs_alloc_busdma(xchan);
194 ret = xchan_bufs_alloc_reserved(xchan);
197 device_printf(xdma->dev,
198 "%s: Can't allocate bufs.\n", __func__);
202 xchan->flags |= XCHAN_BUFS_ALLOCATED;
208 xchan_bufs_free(xdma_channel_t *xchan)
210 struct xdma_request *xr;
214 if ((xchan->flags & XCHAN_BUFS_ALLOCATED) == 0)
217 if (xchan->caps & XCHAN_CAP_BUSDMA) {
218 for (i = 0; i < xchan->xr_num; i++) {
219 xr = &xchan->xr_mem[i];
221 bus_dmamap_destroy(xchan->dma_tag_bufs, b->map);
223 bus_dma_tag_destroy(xchan->dma_tag_bufs);
225 xchan_bufs_free_reserved(xchan);
227 xchan->flags &= ~XCHAN_BUFS_ALLOCATED;
233 xdma_channel_free_sg(xdma_channel_t *xchan)
236 xchan_bufs_free(xchan);
237 xchan_sglist_free(xchan);
238 xchan_bank_free(xchan);
242 * Prepare xchan for a scatter-gather transfer.
243 * xr_num - xdma requests queue size,
244 * maxsegsize - maximum allowed scatter-gather list element size in bytes
247 xdma_prep_sg(xdma_channel_t *xchan, uint32_t xr_num,
248 bus_size_t maxsegsize, bus_size_t maxnsegs,
249 bus_size_t alignment, bus_addr_t boundary,
250 bus_addr_t lowaddr, bus_addr_t highaddr)
252 xdma_controller_t *xdma;
257 KASSERT(xdma != NULL, ("xdma is NULL"));
259 if (xchan->flags & XCHAN_CONFIGURED) {
260 device_printf(xdma->dev,
261 "%s: Channel is already configured.\n", __func__);
265 xchan->xr_num = xr_num;
266 xchan->maxsegsize = maxsegsize;
267 xchan->maxnsegs = maxnsegs;
268 xchan->alignment = alignment;
269 xchan->boundary = boundary;
270 xchan->lowaddr = lowaddr;
271 xchan->highaddr = highaddr;
273 if (xchan->maxnsegs > XDMA_MAX_SEG) {
274 device_printf(xdma->dev, "%s: maxnsegs is too big\n",
279 xchan_bank_init(xchan);
281 /* Allocate sglist. */
282 ret = xchan_sglist_alloc(xchan);
284 device_printf(xdma->dev,
285 "%s: Can't allocate sglist.\n", __func__);
289 /* Allocate buffers if required. */
290 if ((xchan->caps & XCHAN_CAP_NOBUFS) == 0) {
291 ret = xchan_bufs_alloc(xchan);
293 device_printf(xdma->dev,
294 "%s: Can't allocate bufs.\n", __func__);
297 xchan_sglist_free(xchan);
298 xchan_bank_free(xchan);
304 xchan->flags |= (XCHAN_CONFIGURED | XCHAN_TYPE_SG);
307 ret = XDMA_CHANNEL_PREP_SG(xdma->dma_dev, xchan);
309 device_printf(xdma->dev,
310 "%s: Can't prepare SG transfer.\n", __func__);
321 xchan_seg_done(xdma_channel_t *xchan,
322 struct xdma_transfer_status *st)
324 struct xdma_request *xr;
325 xdma_controller_t *xdma;
330 xr = TAILQ_FIRST(&xchan->processing);
332 panic("request not found\n");
336 atomic_subtract_int(&b->nsegs_left, 1);
338 if (b->nsegs_left == 0) {
339 if (xchan->caps & XCHAN_CAP_BUSDMA) {
340 if (xr->direction == XDMA_MEM_TO_DEV)
341 bus_dmamap_sync(xchan->dma_tag_bufs, b->map,
342 BUS_DMASYNC_POSTWRITE);
344 bus_dmamap_sync(xchan->dma_tag_bufs, b->map,
345 BUS_DMASYNC_POSTREAD);
346 bus_dmamap_unload(xchan->dma_tag_bufs, b->map);
348 if (xr->req_type == XR_TYPE_MBUF &&
349 xr->direction == XDMA_DEV_TO_MEM)
350 m_copyback(xr->m, 0, st->transferred,
351 (void *)xr->buf.vaddr);
353 xr->status.error = st->error;
354 xr->status.transferred = st->transferred;
356 QUEUE_PROC_LOCK(xchan);
357 TAILQ_REMOVE(&xchan->processing, xr, xr_next);
358 QUEUE_PROC_UNLOCK(xchan);
360 QUEUE_OUT_LOCK(xchan);
361 TAILQ_INSERT_TAIL(&xchan->queue_out, xr, xr_next);
362 QUEUE_OUT_UNLOCK(xchan);
367 xdma_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
369 struct seg_load_request *slr;
370 struct bus_dma_segment *seg;
383 for (i = 0; i < nsegs; i++) {
384 seg[i].ds_addr = segs[i].ds_addr;
385 seg[i].ds_len = segs[i].ds_len;
390 _xdma_load_data_busdma(xdma_channel_t *xchan, struct xdma_request *xr,
391 struct bus_dma_segment *seg)
393 xdma_controller_t *xdma;
394 struct seg_load_request slr;
404 switch (xr->req_type) {
406 error = bus_dmamap_load_mbuf_sg(xchan->dma_tag_bufs,
407 xr->buf.map, xr->m, seg, &nsegs, BUS_DMA_NOWAIT);
413 error = bus_dmamap_load_bio(xchan->dma_tag_bufs,
414 xr->buf.map, xr->bp, xdma_dmamap_cb, &slr, BUS_DMA_NOWAIT);
415 if (slr.error != 0) {
416 device_printf(xdma->dma_dev,
417 "%s: bus_dmamap_load failed, err %d\n",
418 __func__, slr.error);
424 switch (xr->direction) {
425 case XDMA_MEM_TO_DEV:
426 addr = (void *)xr->src_addr;
428 case XDMA_DEV_TO_MEM:
429 addr = (void *)xr->dst_addr;
432 device_printf(xdma->dma_dev,
433 "%s: Direction is not supported\n", __func__);
439 error = bus_dmamap_load(xchan->dma_tag_bufs, xr->buf.map,
440 addr, (xr->block_len * xr->block_num),
441 xdma_dmamap_cb, &slr, BUS_DMA_NOWAIT);
442 if (slr.error != 0) {
443 device_printf(xdma->dma_dev,
444 "%s: bus_dmamap_load failed, err %d\n",
445 __func__, slr.error);
455 if (error == ENOMEM) {
457 * Out of memory. Try again later.
458 * TODO: count errors.
461 device_printf(xdma->dma_dev,
462 "%s: bus_dmamap_load failed with err %d\n",
467 if (xr->direction == XDMA_MEM_TO_DEV)
468 bus_dmamap_sync(xchan->dma_tag_bufs, xr->buf.map,
469 BUS_DMASYNC_PREWRITE);
471 bus_dmamap_sync(xchan->dma_tag_bufs, xr->buf.map,
472 BUS_DMASYNC_PREREAD);
478 _xdma_load_data(xdma_channel_t *xchan, struct xdma_request *xr,
479 struct bus_dma_segment *seg)
481 xdma_controller_t *xdma;
491 switch (xr->req_type) {
493 if ((xchan->caps & XCHAN_CAP_NOBUFS) == 0) {
494 if (xr->direction == XDMA_MEM_TO_DEV)
495 m_copydata(m, 0, m->m_pkthdr.len,
496 (void *)xr->buf.vaddr);
497 seg[0].ds_addr = (bus_addr_t)xr->buf.paddr;
499 seg[0].ds_addr = mtod(m, bus_addr_t);
500 seg[0].ds_len = m->m_pkthdr.len;
505 panic("implement me\n");
512 xdma_load_data(xdma_channel_t *xchan,
513 struct xdma_request *xr, struct bus_dma_segment *seg)
515 xdma_controller_t *xdma;
524 if (xchan->caps & XCHAN_CAP_BUSDMA)
525 nsegs = _xdma_load_data_busdma(xchan, xr, seg);
527 nsegs = _xdma_load_data(xchan, xr, seg);
529 return (0); /* Try again later. */
531 xr->buf.nsegs = nsegs;
532 xr->buf.nsegs_left = nsegs;
538 xdma_process(xdma_channel_t *xchan,
539 struct xdma_sglist *sg)
541 struct bus_dma_segment seg[XDMA_MAX_SEG];
542 struct xdma_request *xr;
543 struct xdma_request *xr_tmp;
544 xdma_controller_t *xdma;
551 XCHAN_ASSERT_LOCKED(xchan);
558 ret = XDMA_CHANNEL_CAPACITY(xdma->dma_dev, xchan, &capacity);
560 device_printf(xdma->dev,
561 "%s: Can't get DMA controller capacity.\n", __func__);
565 TAILQ_FOREACH_SAFE(xr, &xchan->queue_in, xr_next, xr_tmp) {
566 switch (xr->req_type) {
568 if ((xchan->caps & XCHAN_CAP_NOSEG) ||
569 (c > xchan->maxnsegs))
570 c = xdma_mbuf_defrag(xchan, xr);
578 if (capacity <= (c + n)) {
580 * No space yet available for the entire
581 * request in the DMA engine.
586 if ((c + n + xchan->maxnsegs) >= XDMA_SGLIST_MAXLEN) {
587 /* Sglist is full. */
591 nsegs = xdma_load_data(xchan, xr, seg);
595 xdma_sglist_add(&sg[n], seg, nsegs, xr);
598 QUEUE_IN_LOCK(xchan);
599 TAILQ_REMOVE(&xchan->queue_in, xr, xr_next);
600 QUEUE_IN_UNLOCK(xchan);
602 QUEUE_PROC_LOCK(xchan);
603 TAILQ_INSERT_TAIL(&xchan->processing, xr, xr_next);
604 QUEUE_PROC_UNLOCK(xchan);
611 xdma_queue_submit_sg(xdma_channel_t *xchan)
613 struct xdma_sglist *sg;
614 xdma_controller_t *xdma;
619 KASSERT(xdma != NULL, ("xdma is NULL"));
621 XCHAN_ASSERT_LOCKED(xchan);
625 if ((xchan->caps & XCHAN_CAP_NOBUFS) == 0 &&
626 (xchan->flags & XCHAN_BUFS_ALLOCATED) == 0) {
627 device_printf(xdma->dev,
628 "%s: Can't submit a transfer: no bufs\n",
633 sg_n = xdma_process(xchan, sg);
635 return (0); /* Nothing to submit */
637 /* Now submit sglist to DMA engine driver. */
638 ret = XDMA_CHANNEL_SUBMIT_SG(xdma->dma_dev, xchan, sg, sg_n);
640 device_printf(xdma->dev,
641 "%s: Can't submit an sglist.\n", __func__);