2 * SPDX-License-Identifier: BSD-4-Clause
5 * Hidetoshi Shimokawa. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
18 * This product includes software developed by Hidetoshi Shimokawa.
20 * 4. Neither the name of the author nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/mutex.h>
52 #include <machine/bus.h>
54 #include <dev/firewire/firewire.h>
55 #include <dev/firewire/firewirereg.h>
56 #include <dev/firewire/fwdma.h>
59 fwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
64 printf("fwdma_map_cb: error=%d\n", error);
65 baddr = (bus_addr_t *)arg;
66 *baddr = segs->ds_addr;
70 fwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size,
71 struct fwdma_alloc *dma, int flag)
76 err = bus_dma_tag_create(
78 /*alignment*/ alignment,
80 /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
81 /*highaddr*/ BUS_SPACE_MAXADDR,
82 /*filter*/NULL, /*filterarg*/NULL,
85 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
86 /*flags*/ BUS_DMA_ALLOCNOW,
87 /*lockfunc*/busdma_lock_mutex,
88 /*lockarg*/FW_GMTX(fc),
91 printf("fwdma_malloc: failed(1)\n");
95 err = bus_dmamem_alloc(dma->dma_tag, &dma->v_addr,
98 printf("fwdma_malloc: failed(2)\n");
103 bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->v_addr,
104 size, fwdma_map_cb, &dma->bus_addr, /*flags*/0);
106 return (dma->v_addr);
110 fwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma)
112 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
113 bus_dmamem_free(dma->dma_tag, dma->v_addr, dma->dma_map);
114 bus_dma_tag_destroy(dma->dma_tag);
119 fwdma_malloc_size(bus_dma_tag_t dmat, bus_dmamap_t *dmamap,
120 bus_size_t size, bus_addr_t *bus_addr, int flag)
124 if (bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) {
125 printf("fwdma_malloc_size: failed(1)\n");
128 bus_dmamap_load(dmat, *dmamap, v_addr, size,
129 fwdma_map_cb, bus_addr, /*flags*/0);
134 fwdma_free_size(bus_dma_tag_t dmat, bus_dmamap_t dmamap,
135 void *vaddr, bus_size_t size)
137 bus_dmamap_unload(dmat, dmamap);
138 bus_dmamem_free(dmat, vaddr, dmamap);
142 * Allocate multisegment dma buffers
143 * each segment size is eqaul to ssize except last segment.
145 struct fwdma_alloc_multi *
146 fwdma_malloc_multiseg(struct firewire_comm *fc, int alignment,
147 int esize, int n, int flag)
149 struct fwdma_alloc_multi *am;
150 struct fwdma_seg *seg;
154 if (esize > PAGE_SIZE) {
155 /* round up to PAGE_SIZE */
156 esize = ssize = roundup2(esize, PAGE_SIZE);
159 /* allocate PAGE_SIZE segment for small elements */
160 ssize = rounddown(PAGE_SIZE, esize);
161 nseg = howmany(n, ssize / esize);
163 am = (struct fwdma_alloc_multi *)malloc(sizeof(struct fwdma_alloc_multi)
164 + sizeof(struct fwdma_seg)*nseg, M_FW, M_WAITOK);
168 if (bus_dma_tag_create(
170 /*alignment*/ alignment,
172 /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
173 /*highaddr*/ BUS_SPACE_MAXADDR,
174 /*filter*/NULL, /*filterarg*/NULL,
177 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
178 /*flags*/ BUS_DMA_ALLOCNOW,
179 /*lockfunc*/busdma_lock_mutex,
180 /*lockarg*/FW_GMTX(fc),
182 printf("fwdma_malloc_multiseg: tag_create failed\n");
187 for (seg = &am->seg[0]; nseg--; seg++) {
188 seg->v_addr = fwdma_malloc_size(am->dma_tag, &seg->dma_map,
189 ssize, &seg->bus_addr, flag);
190 if (seg->v_addr == NULL) {
191 printf("fwdma_malloc_multi: malloc_size failed %d\n",
193 fwdma_free_multiseg(am);
202 fwdma_free_multiseg(struct fwdma_alloc_multi *am)
204 struct fwdma_seg *seg;
206 for (seg = &am->seg[0]; am->nseg--; seg++) {
207 fwdma_free_size(am->dma_tag, seg->dma_map,
208 seg->v_addr, am->ssize);
210 bus_dma_tag_destroy(am->dma_tag);