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