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