3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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:
16 * This product includes software developed by Hidetoshi Shimokawa.
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.
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
34 * $FreeBSD: src/sys/dev/firewire/fwdma.c,v 1.5 2003/08/24 17:46:07 obrien Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/types.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
46 #include <sys/mutex.h>
49 #include <bus/firewire/firewire.h>
50 #include <bus/firewire/firewirereg.h>
51 #include <bus/firewire/fwdma.h>
54 fwdma_map_cb(void *arg
, bus_dma_segment_t
*segs
, int nseg
, int error
)
59 kprintf("fwdma_map_cb: error=%d\n", error
);
60 baddr
= (bus_addr_t
*)arg
;
61 *baddr
= segs
->ds_addr
;
65 fwdma_malloc(struct firewire_comm
*fc
, int alignment
, bus_size_t size
,
66 struct fwdma_alloc
*dma
, int flag
)
71 err
= bus_dma_tag_create(
73 /*alignment*/ alignment
,
75 /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT
,
76 /*highaddr*/ BUS_SPACE_MAXADDR
,
77 /*filter*/NULL
, /*filterarg*/NULL
,
80 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT
,
81 /*flags*/ BUS_DMA_ALLOCNOW
,
82 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
83 /*lockfunc*/busdma_lock_mutex
,
88 kprintf("fwdma_malloc: failed(1)\n");
92 err
= bus_dmamem_alloc(dma
->dma_tag
, &dma
->v_addr
,
95 kprintf("fwdma_malloc: failed(2)\n");
100 bus_dmamap_load(dma
->dma_tag
, dma
->dma_map
, dma
->v_addr
,
101 size
, fwdma_map_cb
, &dma
->bus_addr
, /*flags*/0);
107 fwdma_free(struct firewire_comm
*fc
, struct fwdma_alloc
*dma
)
109 bus_dmamap_unload(dma
->dma_tag
, dma
->dma_map
);
110 bus_dmamem_free(dma
->dma_tag
, dma
->v_addr
, dma
->dma_map
);
111 bus_dma_tag_destroy(dma
->dma_tag
);
116 fwdma_malloc_size(bus_dma_tag_t dmat
, bus_dmamap_t
*dmamap
,
117 bus_size_t size
, bus_addr_t
*bus_addr
, int flag
)
121 if (bus_dmamem_alloc(dmat
, &v_addr
, flag
, dmamap
)) {
122 kprintf("fwdma_malloc_size: failed(1)\n");
125 bus_dmamap_load(dmat
, *dmamap
, v_addr
, size
,
126 fwdma_map_cb
, bus_addr
, /*flags*/0);
131 fwdma_free_size(bus_dma_tag_t dmat
, bus_dmamap_t dmamap
,
132 void *vaddr
, bus_size_t size
)
134 bus_dmamap_unload(dmat
, dmamap
);
135 bus_dmamem_free(dmat
, vaddr
, dmamap
);
139 * Allocate multisegment dma buffers
140 * each segment size is eqaul to ssize except last segment.
142 struct fwdma_alloc_multi
*
143 fwdma_malloc_multiseg(struct firewire_comm
*fc
, int alignment
,
144 int esize
, int n
, int flag
)
146 struct fwdma_alloc_multi
*am
;
147 struct fwdma_seg
*seg
;
151 if (esize
> PAGE_SIZE
) {
152 /* round up to PAGE_SIZE */
153 esize
= ssize
= roundup2(esize
, PAGE_SIZE
);
156 /* allocate PAGE_SIZE segment for small elements */
157 ssize
= rounddown(PAGE_SIZE
, esize
);
158 nseg
= howmany(n
, ssize
/ esize
);
160 am
= (struct fwdma_alloc_multi
*)kmalloc(sizeof(struct fwdma_alloc_multi
)
161 + sizeof(struct fwdma_seg
)*nseg
, M_FW
, M_WAITOK
);
165 if (bus_dma_tag_create(
167 /*alignment*/ alignment
,
169 /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT
,
170 /*highaddr*/ BUS_SPACE_MAXADDR
,
171 /*filter*/NULL
, /*filterarg*/NULL
,
174 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT
,
175 /*flags*/ BUS_DMA_ALLOCNOW
,
176 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
177 /*lockfunc*/busdma_lock_mutex
,
181 kprintf("fwdma_malloc_multiseg: tag_create failed\n");
187 kprintf("malloc_multi: ssize=%ld nseg=%d\n", ssize
, nseg
);
189 for (seg
= &am
->seg
[0]; nseg
--; seg
++) {
190 seg
->v_addr
= fwdma_malloc_size(am
->dma_tag
, &seg
->dma_map
,
191 ssize
, &seg
->bus_addr
, flag
);
192 if (seg
->v_addr
== NULL
) {
193 kprintf("fwdma_malloc_multi: malloc_size failed %d\n",
195 fwdma_free_multiseg(am
);
204 fwdma_free_multiseg(struct fwdma_alloc_multi
*am
)
206 struct fwdma_seg
*seg
;
208 for (seg
= &am
->seg
[0]; am
->nseg
--; seg
++) {
209 fwdma_free_size(am
->dma_tag
, seg
->dma_map
,
210 seg
->v_addr
, am
->ssize
);
212 bus_dma_tag_destroy(am
->dma_tag
);