Home | History | Annotate | Line # | Download | only in ieee1394
fwdma.c revision 1.1
      1 /*	$NetBSD: fwdma.c,v 1.1 2005/07/11 15:29:05 kiyohara Exp $	*/
      2 /*-
      3  * Copyright (c) 2003
      4  * 	Hidetoshi Shimokawa. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *
     17  *	This product includes software developed by Hidetoshi Shimokawa.
     18  *
     19  * 4. Neither the name of the author nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifdef __FBSDID
     39 __FBSDID("$FreeBSD: /repoman/r/ncvs/src/sys/dev/firewire/fwdma.c,v 1.7 2005/01/06 01:42:41 imp Exp $");
     40 #endif
     41 
     42 #if defined(__FreeBSD__)
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/types.h>
     46 #include <sys/kernel.h>
     47 #include <sys/conf.h>
     48 #include <sys/malloc.h>
     49 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
     50 #include <sys/lock.h>
     51 #include <sys/mutex.h>
     52 #endif
     53 
     54 #include <sys/bus.h>
     55 #include <machine/bus.h>
     56 
     57 #ifdef __DragonFly__
     58 #include <bus/firewire/fw_port.h>
     59 #include <bus/firewire/firewire.h>
     60 #include <bus/firewire/firewirereg.h>
     61 #include <bus/firewire/fwdma.h>
     62 #else
     63 #include <dev/firewire/fw_port.h>
     64 #include <dev/firewire/firewire.h>
     65 #include <dev/firewire/firewirereg.h>
     66 #include <dev/firewire/fwdma.h>
     67 #endif
     68 #elif defined(__NetBSD__)
     69 #include <sys/param.h>
     70 #include <sys/device.h>
     71 #include <sys/systm.h>
     72 #include <sys/types.h>
     73 #include <sys/kernel.h>
     74 #include <sys/malloc.h>
     75 
     76 #include <machine/bus.h>
     77 #include <machine/vmparam.h>
     78 
     79 #include <dev/ieee1394/fw_port.h>
     80 #include <dev/ieee1394/firewire.h>
     81 #include <dev/ieee1394/firewirereg.h>
     82 #include <dev/ieee1394/fwdma.h>
     83 #endif
     84 
     85 static void
     86 fwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
     87 {
     88 	bus_addr_t *baddr;
     89 
     90 	if (error)
     91 		printf("fwdma_map_cb: error=%d\n", error);
     92 	baddr = (bus_addr_t *)arg;
     93 	*baddr = segs->ds_addr;
     94 }
     95 
     96 void *
     97 fwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size,
     98 	struct fwdma_alloc *dma, int flag)
     99 {
    100 	int err;
    101 
    102 	dma->v_addr = NULL;
    103 	err = fw_bus_dma_tag_create(
    104 		/*parent*/ fc->dmat,
    105 		/*alignment*/ alignment,
    106 		/*boundary*/ 0,
    107 		/*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
    108 		/*highaddr*/ BUS_SPACE_MAXADDR,
    109 		/*filter*/NULL, /*filterarg*/NULL,
    110 		/*maxsize*/ size,
    111 		/*nsegments*/ 1,
    112 		/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
    113 		/*flags*/ BUS_DMA_ALLOCNOW,
    114 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
    115 		/*lockfunc*/busdma_lock_mutex,
    116 		/*lockarg*/&Giant,
    117 #endif
    118 		&dma->fw_dma_tag);
    119 	if (err) {
    120 		printf("fwdma_malloc: failed(1)\n");
    121 		return(NULL);
    122 	}
    123 
    124 	err = fw_bus_dmamem_alloc(dma->fw_dma_tag, &dma->v_addr,
    125 		flag, &dma->dma_map);
    126 	if (err) {
    127 		printf("fwdma_malloc: failed(2)\n");
    128 		fw_bus_dma_tag_destroy(dma->fw_dma_tag);
    129 		return(NULL);
    130 	}
    131 
    132 	err = fw_bus_dmamap_load(dma->fw_dma_tag, dma->dma_map, dma->v_addr,
    133 		size, fwdma_map_cb, &dma->bus_addr, /*flags*/0);
    134 	if (err != 0) {
    135 		printf("fwdma_malloc: failed(3)\n");
    136 		fw_bus_dmamem_free(dma->fw_dma_tag, dma->v_addr, dma->dma_map);
    137 		fw_bus_dma_tag_destroy(dma->fw_dma_tag);
    138 		return(NULL);
    139 	}
    140 
    141 	return(dma->v_addr);
    142 }
    143 
    144 void
    145 fwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma)
    146 {
    147         fw_bus_dmamap_unload(dma->fw_dma_tag, dma->dma_map);
    148 	fw_bus_dmamem_free(dma->fw_dma_tag, dma->v_addr, dma->dma_map);
    149 	fw_bus_dma_tag_destroy(dma->fw_dma_tag);
    150 }
    151 
    152 
    153 void *
    154 fwdma_malloc_size(fw_bus_dma_tag_t dmat, bus_dmamap_t *dmamap,
    155 	bus_size_t size, bus_addr_t *bus_addr, int flag)
    156 {
    157 	void *v_addr;
    158 
    159 	if (fw_bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) {
    160 		printf("fwdma_malloc_size: failed(1)\n");
    161 		return(NULL);
    162 	}
    163 	if (fw_bus_dmamap_load(dmat, *dmamap, v_addr, size,
    164 			fwdma_map_cb, bus_addr, /*flags*/0)) {
    165 		printf("fwdma_malloc_size: failed(2)\n");
    166 		fw_bus_dmamem_free(dmat, v_addr, *dmamap);
    167 		return(NULL);
    168 	}
    169 	return(v_addr);
    170 }
    171 
    172 void
    173 fwdma_free_size(fw_bus_dma_tag_t dmat, bus_dmamap_t dmamap,
    174 		void *vaddr, bus_size_t size)
    175 {
    176 	fw_bus_dmamap_unload(dmat, dmamap);
    177 	fw_bus_dmamem_free(dmat, vaddr, dmamap);
    178 }
    179 
    180 /*
    181  * Allocate multisegment dma buffers
    182  * each segment size is eqaul to ssize except last segment.
    183  */
    184 struct fwdma_alloc_multi *
    185 fwdma_malloc_multiseg(struct firewire_comm *fc, int alignment,
    186 		int esize, int n, int flag)
    187 {
    188 	struct fwdma_alloc_multi *am;
    189 	struct fwdma_seg *seg;
    190 	bus_size_t ssize;
    191 	int nseg;
    192 
    193 	if (esize > PAGE_SIZE) {
    194 		/* round up to PAGE_SIZE */
    195 		esize = ssize = roundup2(esize, PAGE_SIZE);
    196 		nseg = n;
    197 	} else {
    198 		/* allocate PAGE_SIZE segment for small elements */
    199 		ssize = rounddown(PAGE_SIZE, esize);
    200 		nseg = howmany(n, ssize / esize);
    201 	}
    202 	am = (struct fwdma_alloc_multi *)malloc(sizeof(struct fwdma_alloc_multi)
    203 			+ sizeof(struct fwdma_seg)*nseg, M_FW, M_WAITOK);
    204 	if (am == NULL) {
    205 		printf("fwdma_malloc_multiseg: malloc failed\n");
    206 		return(NULL);
    207 	}
    208 	am->ssize = ssize;
    209 	am->esize = esize;
    210 	am->nseg = 0;
    211 	if (fw_bus_dma_tag_create(
    212 			/*parent*/ fc->dmat,
    213 			/*alignment*/ alignment,
    214 			/*boundary*/ 0,
    215 			/*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
    216 			/*highaddr*/ BUS_SPACE_MAXADDR,
    217 			/*filter*/NULL, /*filterarg*/NULL,
    218 			/*maxsize*/ ssize,
    219 			/*nsegments*/ 1,
    220 			/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
    221 			/*flags*/ BUS_DMA_ALLOCNOW,
    222 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
    223 			/*lockfunc*/busdma_lock_mutex,
    224 			/*lockarg*/&Giant,
    225 #endif
    226 			&am->fw_dma_tag)) {
    227 		printf("fwdma_malloc_multiseg: tag_create failed\n");
    228 		free(am, M_FW);
    229 		return(NULL);
    230 	}
    231 
    232 #if 0
    233 #if defined(__DragonFly__) || __FreeBSD_version < 500000
    234 	printf("malloc_multi: ssize=%d nseg=%d\n", ssize, nseg);
    235 #else
    236 	printf("malloc_multi: ssize=%td nseg=%d\n", ssize, nseg);
    237 #endif
    238 #endif
    239 	for (seg = &am->seg[0]; nseg --; seg ++) {
    240 		seg->v_addr = fwdma_malloc_size(am->fw_dma_tag, &seg->dma_map,
    241 			ssize, &seg->bus_addr, flag);
    242 		if (seg->v_addr == NULL) {
    243 			printf("fwdma_malloc_multi: malloc_size failed %d\n",
    244 				am->nseg);
    245 			fwdma_free_multiseg(am);
    246 			return(NULL);
    247 		}
    248 		am->nseg++;
    249 	}
    250 	return(am);
    251 }
    252 
    253 void
    254 fwdma_free_multiseg(struct fwdma_alloc_multi *am)
    255 {
    256 	struct fwdma_seg *seg;
    257 
    258 	for (seg = &am->seg[0]; am->nseg --; seg ++) {
    259 		fwdma_free_size(am->fw_dma_tag, seg->dma_map,
    260 			seg->v_addr, am->ssize);
    261 	}
    262 	fw_bus_dma_tag_destroy(am->fw_dma_tag);
    263 	free(am, M_FW);
    264 }
    265