xref: /freebsd/sys/dev/firewire/fwdma.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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  */
35 
36 #ifdef __FreeBSD__
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/types.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 
49 #include <sys/bus.h>
50 #include <machine/bus.h>
51 
52 #include <dev/firewire/firewire.h>
53 #include <dev/firewire/firewirereg.h>
54 #include <dev/firewire/fwdma.h>
55 
56 static void
57 fwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
58 {
59 	bus_addr_t *baddr;
60 
61 	if (error)
62 		printf("fwdma_map_cb: error=%d\n", error);
63 	baddr = (bus_addr_t *)arg;
64 	*baddr = segs->ds_addr;
65 }
66 
67 void *
68 fwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size,
69 	struct fwdma_alloc *dma, int flag)
70 {
71 	int err;
72 
73 	dma->v_addr = NULL;
74 	err = bus_dma_tag_create(
75 		/*parent*/ fc->dmat,
76 		/*alignment*/ alignment,
77 		/*boundary*/ 0,
78 		/*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
79 		/*highaddr*/ BUS_SPACE_MAXADDR,
80 		/*filter*/NULL, /*filterarg*/NULL,
81 		/*maxsize*/ size,
82 		/*nsegments*/ 1,
83 		/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
84 		/*flags*/ BUS_DMA_ALLOCNOW,
85 		/*lockfunc*/busdma_lock_mutex,
86 		/*lockarg*/FW_GMTX(fc),
87 		&dma->dma_tag);
88 	if (err) {
89 		printf("fwdma_malloc: failed(1)\n");
90 		return (NULL);
91 	}
92 
93 	err = bus_dmamem_alloc(dma->dma_tag, &dma->v_addr,
94 		flag, &dma->dma_map);
95 	if (err) {
96 		printf("fwdma_malloc: failed(2)\n");
97 		/* XXX destroy tag */
98 		return (NULL);
99 	}
100 
101 	bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->v_addr,
102 		size, fwdma_map_cb, &dma->bus_addr, /*flags*/0);
103 
104 	return (dma->v_addr);
105 }
106 
107 void
108 fwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma)
109 {
110         bus_dmamap_unload(dma->dma_tag, dma->dma_map);
111 	bus_dmamem_free(dma->dma_tag, dma->v_addr, dma->dma_map);
112 	bus_dma_tag_destroy(dma->dma_tag);
113 }
114 
115 
116 void *
117 fwdma_malloc_size(bus_dma_tag_t dmat, bus_dmamap_t *dmamap,
118 	bus_size_t size, bus_addr_t *bus_addr, int flag)
119 {
120 	void *v_addr;
121 
122 	if (bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) {
123 		printf("fwdma_malloc_size: failed(1)\n");
124 		return (NULL);
125 	}
126 	bus_dmamap_load(dmat, *dmamap, v_addr, size,
127 			fwdma_map_cb, bus_addr, /*flags*/0);
128 	return (v_addr);
129 }
130 
131 void
132 fwdma_free_size(bus_dma_tag_t dmat, bus_dmamap_t dmamap,
133 		void *vaddr, bus_size_t size)
134 {
135 	bus_dmamap_unload(dmat, dmamap);
136 	bus_dmamem_free(dmat, vaddr, dmamap);
137 }
138 
139 /*
140  * Allocate multisegment dma buffers
141  * each segment size is eqaul to ssize except last segment.
142  */
143 struct fwdma_alloc_multi *
144 fwdma_malloc_multiseg(struct firewire_comm *fc, int alignment,
145 		int esize, int n, int flag)
146 {
147 	struct fwdma_alloc_multi *am;
148 	struct fwdma_seg *seg;
149 	bus_size_t ssize;
150 	int nseg;
151 
152 	if (esize > PAGE_SIZE) {
153 		/* round up to PAGE_SIZE */
154 		esize = ssize = roundup2(esize, PAGE_SIZE);
155 		nseg = n;
156 	} else {
157 		/* allocate PAGE_SIZE segment for small elements */
158 		ssize = rounddown(PAGE_SIZE, esize);
159 		nseg = howmany(n, ssize / esize);
160 	}
161 	am = (struct fwdma_alloc_multi *)malloc(sizeof(struct fwdma_alloc_multi)
162 			+ sizeof(struct fwdma_seg)*nseg, M_FW, M_WAITOK);
163 	if (am == NULL) {
164 		printf("fwdma_malloc_multiseg: malloc failed\n");
165 		return (NULL);
166 	}
167 	am->ssize = ssize;
168 	am->esize = esize;
169 	am->nseg = 0;
170 	if (bus_dma_tag_create(
171 			/*parent*/ fc->dmat,
172 			/*alignment*/ alignment,
173 			/*boundary*/ 0,
174 			/*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
175 			/*highaddr*/ BUS_SPACE_MAXADDR,
176 			/*filter*/NULL, /*filterarg*/NULL,
177 			/*maxsize*/ ssize,
178 			/*nsegments*/ 1,
179 			/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
180 			/*flags*/ BUS_DMA_ALLOCNOW,
181 			/*lockfunc*/busdma_lock_mutex,
182 			/*lockarg*/FW_GMTX(fc),
183 			&am->dma_tag)) {
184 		printf("fwdma_malloc_multiseg: tag_create failed\n");
185 		free(am, M_FW);
186 		return (NULL);
187 	}
188 
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 			printf("fwdma_malloc_multi: malloc_size failed %d\n",
194 				am->nseg);
195 			fwdma_free_multiseg(am);
196 			return (NULL);
197 		}
198 		am->nseg++;
199 	}
200 	return (am);
201 }
202 
203 void
204 fwdma_free_multiseg(struct fwdma_alloc_multi *am)
205 {
206 	struct fwdma_seg *seg;
207 
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);
211 	}
212 	bus_dma_tag_destroy(am->dma_tag);
213 	free(am, M_FW);
214 }
215