fwdma.c revision 117126
1113584Ssimokawa/* 2113584Ssimokawa * Copyright (c) 2003 3113584Ssimokawa * Hidetoshi Shimokawa. All rights reserved. 4113584Ssimokawa * 5113584Ssimokawa * Redistribution and use in source and binary forms, with or without 6113584Ssimokawa * modification, are permitted provided that the following conditions 7113584Ssimokawa * are met: 8113584Ssimokawa * 1. Redistributions of source code must retain the above copyright 9113584Ssimokawa * notice, this list of conditions and the following disclaimer. 10113584Ssimokawa * 2. Redistributions in binary form must reproduce the above copyright 11113584Ssimokawa * notice, this list of conditions and the following disclaimer in the 12113584Ssimokawa * documentation and/or other materials provided with the distribution. 13113584Ssimokawa * 3. All advertising materials mentioning features or use of this software 14113584Ssimokawa * must display the following acknowledgement: 15113584Ssimokawa * 16113584Ssimokawa * This product includes software developed by Hidetoshi Shimokawa. 17113584Ssimokawa * 18113584Ssimokawa * 4. Neither the name of the author nor the names of its contributors 19113584Ssimokawa * may be used to endorse or promote products derived from this software 20113584Ssimokawa * without specific prior written permission. 21113584Ssimokawa * 22113584Ssimokawa * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23113584Ssimokawa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24113584Ssimokawa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25113584Ssimokawa * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26113584Ssimokawa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27113584Ssimokawa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28113584Ssimokawa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29113584Ssimokawa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30113584Ssimokawa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31113584Ssimokawa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32113584Ssimokawa * SUCH DAMAGE. 33113584Ssimokawa * 34113584Ssimokawa * $FreeBSD: head/sys/dev/firewire/fwdma.c 117126 2003-07-01 15:52:06Z scottl $ 35113584Ssimokawa */ 36113584Ssimokawa#include <sys/param.h> 37113584Ssimokawa#include <sys/systm.h> 38113584Ssimokawa#include <sys/types.h> 39113584Ssimokawa#include <sys/kernel.h> 40113584Ssimokawa#include <sys/malloc.h> 41117126Sscottl#include <sys/lock.h> 42117126Sscottl#include <sys/mutex.h> 43113584Ssimokawa 44113584Ssimokawa#include <sys/bus.h> 45113584Ssimokawa#include <machine/bus.h> 46113584Ssimokawa 47113584Ssimokawa#include <dev/firewire/firewire.h> 48113584Ssimokawa#include <dev/firewire/firewirereg.h> 49113584Ssimokawa#include <dev/firewire/fwdma.h> 50113584Ssimokawa 51113584Ssimokawastatic void 52113584Ssimokawafwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 53113584Ssimokawa{ 54113584Ssimokawa bus_addr_t *baddr; 55113584Ssimokawa 56113584Ssimokawa if (error) 57113584Ssimokawa printf("fwdma_map_cb: error=%d\n", error); 58113584Ssimokawa baddr = (bus_addr_t *)arg; 59113584Ssimokawa *baddr = segs->ds_addr; 60113584Ssimokawa} 61113584Ssimokawa 62113584Ssimokawavoid * 63113584Ssimokawafwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size, 64113584Ssimokawa struct fwdma_alloc *dma, int flag) 65113584Ssimokawa{ 66113584Ssimokawa int err; 67113584Ssimokawa 68113584Ssimokawa dma->v_addr = NULL; 69113584Ssimokawa err = bus_dma_tag_create( 70113584Ssimokawa /*parent*/ fc->dmat, 71113584Ssimokawa /*alignment*/ alignment, 72113584Ssimokawa /*boundary*/ 0, 73113584Ssimokawa /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT, 74113584Ssimokawa /*highaddr*/ BUS_SPACE_MAXADDR, 75113584Ssimokawa /*filter*/NULL, /*filterarg*/NULL, 76113584Ssimokawa /*maxsize*/ size, 77113584Ssimokawa /*nsegments*/ 1, 78113584Ssimokawa /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT, 79117126Sscottl /*flags*/ BUS_DMA_ALLOCNOW, 80117126Sscottl /*lockfunc*/busdma_lock_mutex,/*lockarg*/&Giant, &dma->dma_tag); 81113584Ssimokawa if (err) { 82113584Ssimokawa printf("fwdma_malloc: failed(1)\n"); 83113584Ssimokawa return(NULL); 84113584Ssimokawa } 85113584Ssimokawa 86113584Ssimokawa err = bus_dmamem_alloc(dma->dma_tag, &dma->v_addr, 87113584Ssimokawa flag, &dma->dma_map); 88113584Ssimokawa if (err) { 89113584Ssimokawa printf("fwdma_malloc: failed(2)\n"); 90113584Ssimokawa /* XXX destory tag */ 91113584Ssimokawa return(NULL); 92113584Ssimokawa } 93113584Ssimokawa 94113584Ssimokawa bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->v_addr, 95113584Ssimokawa size, fwdma_map_cb, &dma->bus_addr, /*flags*/0); 96113584Ssimokawa 97113584Ssimokawa return(dma->v_addr); 98113584Ssimokawa} 99113584Ssimokawa 100113584Ssimokawavoid 101113584Ssimokawafwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma) 102113584Ssimokawa{ 103113584Ssimokawa bus_dmamap_unload(dma->dma_tag, dma->dma_map); 104113584Ssimokawa bus_dmamem_free(dma->dma_tag, dma->v_addr, dma->dma_map); 105113584Ssimokawa bus_dma_tag_destroy(dma->dma_tag); 106113584Ssimokawa} 107113584Ssimokawa 108113584Ssimokawa 109113584Ssimokawavoid * 110113584Ssimokawafwdma_malloc_size(bus_dma_tag_t dmat, bus_dmamap_t *dmamap, 111113584Ssimokawa bus_size_t size, bus_addr_t *bus_addr, int flag) 112113584Ssimokawa{ 113113584Ssimokawa void *v_addr; 114113584Ssimokawa 115113584Ssimokawa if (bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) { 116113584Ssimokawa printf("fwdma_malloc_size: failed(1)\n"); 117113584Ssimokawa return(NULL); 118113584Ssimokawa } 119113584Ssimokawa bus_dmamap_load(dmat, *dmamap, v_addr, size, 120113584Ssimokawa fwdma_map_cb, bus_addr, /*flags*/0); 121113584Ssimokawa return(v_addr); 122113584Ssimokawa} 123113584Ssimokawa 124113584Ssimokawavoid 125113584Ssimokawafwdma_free_size(bus_dma_tag_t dmat, bus_dmamap_t dmamap, 126113584Ssimokawa void *vaddr, bus_size_t size) 127113584Ssimokawa{ 128113584Ssimokawa bus_dmamap_unload(dmat, dmamap); 129113584Ssimokawa bus_dmamem_free(dmat, vaddr, dmamap); 130113584Ssimokawa} 131113584Ssimokawa 132113584Ssimokawa/* 133113584Ssimokawa * Allocate multisegment dma buffers 134113584Ssimokawa * each segment size is eqaul to ssize except last segment. 135113584Ssimokawa */ 136113584Ssimokawastruct fwdma_alloc_multi * 137113584Ssimokawafwdma_malloc_multiseg(struct firewire_comm *fc, int alignment, 138113584Ssimokawa int esize, int n, int flag) 139113584Ssimokawa{ 140113584Ssimokawa struct fwdma_alloc_multi *am; 141113584Ssimokawa struct fwdma_seg *seg; 142113584Ssimokawa bus_size_t ssize; 143113584Ssimokawa int nseg; 144113584Ssimokawa 145113584Ssimokawa if (esize > PAGE_SIZE) { 146113584Ssimokawa /* round up to PAGE_SIZE */ 147113584Ssimokawa esize = ssize = roundup2(esize, PAGE_SIZE); 148113584Ssimokawa nseg = n; 149113584Ssimokawa } else { 150113584Ssimokawa /* allocate PAGE_SIZE segment for small elements */ 151113584Ssimokawa ssize = rounddown(PAGE_SIZE, esize); 152113584Ssimokawa nseg = howmany(n, ssize / esize); 153113584Ssimokawa } 154113584Ssimokawa am = (struct fwdma_alloc_multi *)malloc(sizeof(struct fwdma_alloc_multi) 155113584Ssimokawa + sizeof(struct fwdma_seg)*nseg, M_FW, M_WAITOK); 156113584Ssimokawa if (am == NULL) { 157113584Ssimokawa printf("fwdma_malloc_multiseg: malloc failed\n"); 158113584Ssimokawa return(NULL); 159113584Ssimokawa } 160113584Ssimokawa am->ssize = ssize; 161113584Ssimokawa am->esize = esize; 162113584Ssimokawa am->nseg = 0; 163113584Ssimokawa if (bus_dma_tag_create( 164113584Ssimokawa /*parent*/ fc->dmat, 165113584Ssimokawa /*alignment*/ alignment, 166113584Ssimokawa /*boundary*/ 0, 167113584Ssimokawa /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT, 168113584Ssimokawa /*highaddr*/ BUS_SPACE_MAXADDR, 169113584Ssimokawa /*filter*/NULL, /*filterarg*/NULL, 170113584Ssimokawa /*maxsize*/ ssize, 171113584Ssimokawa /*nsegments*/ 1, 172113584Ssimokawa /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT, 173117126Sscottl /*flags*/ BUS_DMA_ALLOCNOW, 174117126Sscottl /*lockfunc*/busdma_lock_mutex, 175117126Sscottl /*lockarg*/&Giant, &am->dma_tag)) { 176113584Ssimokawa printf("fwdma_malloc_multiseg: tag_create failed\n"); 177113584Ssimokawa free(am, M_FW); 178113584Ssimokawa return(NULL); 179113584Ssimokawa } 180113584Ssimokawa 181113584Ssimokawa#if 0 182113584Ssimokawa#if __FreeBSD_version < 500000 183113584Ssimokawa printf("malloc_multi: ssize=%d nseg=%d\n", ssize, nseg); 184113584Ssimokawa#else 185113584Ssimokawa printf("malloc_multi: ssize=%td nseg=%d\n", ssize, nseg); 186113584Ssimokawa#endif 187113584Ssimokawa#endif 188113584Ssimokawa for (seg = &am->seg[0]; nseg --; seg ++) { 189113584Ssimokawa seg->v_addr = fwdma_malloc_size(am->dma_tag, &seg->dma_map, 190113584Ssimokawa ssize, &seg->bus_addr, flag); 191113584Ssimokawa if (seg->v_addr == NULL) { 192113584Ssimokawa printf("fwdma_malloc_multi: malloc_size failed %d\n", 193113584Ssimokawa am->nseg); 194113584Ssimokawa fwdma_free_multiseg(am); 195113584Ssimokawa return(NULL); 196113584Ssimokawa } 197113584Ssimokawa am->nseg++; 198113584Ssimokawa } 199113584Ssimokawa return(am); 200113584Ssimokawa} 201113584Ssimokawa 202113584Ssimokawavoid 203113584Ssimokawafwdma_free_multiseg(struct fwdma_alloc_multi *am) 204113584Ssimokawa{ 205113584Ssimokawa struct fwdma_seg *seg; 206113584Ssimokawa 207113584Ssimokawa for (seg = &am->seg[0]; am->nseg --; seg ++) { 208113584Ssimokawa fwdma_free_size(am->dma_tag, seg->dma_map, 209113584Ssimokawa seg->v_addr, am->ssize); 210113584Ssimokawa } 211113584Ssimokawa bus_dma_tag_destroy(am->dma_tag); 212113584Ssimokawa free(am, M_FW); 213113584Ssimokawa} 214