xref: /freebsd/sys/dev/firewire/fwdma.c (revision 118063fb70c3f74f16678dc7ddbc8528c483a528)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2003
5  * 	Hidetoshi Shimokawa. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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:
17  *
18  *	This product includes software developed by Hidetoshi Shimokawa.
19  *
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.
23  *
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
34  * SUCH DAMAGE.
35  *
36  */
37 
38 #ifdef __FreeBSD__
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 #endif
42 
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>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 
51 #include <sys/bus.h>
52 #include <machine/bus.h>
53 
54 #include <dev/firewire/firewire.h>
55 #include <dev/firewire/firewirereg.h>
56 #include <dev/firewire/fwdma.h>
57 
58 static void
59 fwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
60 {
61 	bus_addr_t *baddr;
62 
63 	if (error)
64 		printf("fwdma_map_cb: error=%d\n", error);
65 	baddr = (bus_addr_t *)arg;
66 	*baddr = segs->ds_addr;
67 }
68 
69 void *
70 fwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size,
71 	struct fwdma_alloc *dma, int flag)
72 {
73 	int err;
74 
75 	dma->v_addr = NULL;
76 	err = bus_dma_tag_create(
77 		/*parent*/ fc->dmat,
78 		/*alignment*/ alignment,
79 		/*boundary*/ 0,
80 		/*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
81 		/*highaddr*/ BUS_SPACE_MAXADDR,
82 		/*filter*/NULL, /*filterarg*/NULL,
83 		/*maxsize*/ size,
84 		/*nsegments*/ 1,
85 		/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
86 		/*flags*/ BUS_DMA_ALLOCNOW,
87 		/*lockfunc*/busdma_lock_mutex,
88 		/*lockarg*/FW_GMTX(fc),
89 		&dma->dma_tag);
90 	if (err) {
91 		printf("fwdma_malloc: failed(1)\n");
92 		return (NULL);
93 	}
94 
95 	err = bus_dmamem_alloc(dma->dma_tag, &dma->v_addr,
96 		flag, &dma->dma_map);
97 	if (err) {
98 		printf("fwdma_malloc: failed(2)\n");
99 		/* XXX destroy tag */
100 		return (NULL);
101 	}
102 
103 	bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->v_addr,
104 		size, fwdma_map_cb, &dma->bus_addr, /*flags*/0);
105 
106 	return (dma->v_addr);
107 }
108 
109 void
110 fwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma)
111 {
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);
115 }
116 
117 
118 void *
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)
121 {
122 	void *v_addr;
123 
124 	if (bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) {
125 		printf("fwdma_malloc_size: failed(1)\n");
126 		return (NULL);
127 	}
128 	bus_dmamap_load(dmat, *dmamap, v_addr, size,
129 			fwdma_map_cb, bus_addr, /*flags*/0);
130 	return (v_addr);
131 }
132 
133 void
134 fwdma_free_size(bus_dma_tag_t dmat, bus_dmamap_t dmamap,
135 		void *vaddr, bus_size_t size)
136 {
137 	bus_dmamap_unload(dmat, dmamap);
138 	bus_dmamem_free(dmat, vaddr, dmamap);
139 }
140 
141 /*
142  * Allocate multisegment dma buffers
143  * each segment size is eqaul to ssize except last segment.
144  */
145 struct fwdma_alloc_multi *
146 fwdma_malloc_multiseg(struct firewire_comm *fc, int alignment,
147 		int esize, int n, int flag)
148 {
149 	struct fwdma_alloc_multi *am;
150 	struct fwdma_seg *seg;
151 	bus_size_t ssize;
152 	int nseg;
153 
154 	if (esize > PAGE_SIZE) {
155 		/* round up to PAGE_SIZE */
156 		esize = ssize = roundup2(esize, PAGE_SIZE);
157 		nseg = n;
158 	} else {
159 		/* allocate PAGE_SIZE segment for small elements */
160 		ssize = rounddown(PAGE_SIZE, esize);
161 		nseg = howmany(n, ssize / esize);
162 	}
163 	am = (struct fwdma_alloc_multi *)malloc(sizeof(struct fwdma_alloc_multi)
164 			+ sizeof(struct fwdma_seg)*nseg, M_FW, M_WAITOK);
165 	am->ssize = ssize;
166 	am->esize = esize;
167 	am->nseg = 0;
168 	if (bus_dma_tag_create(
169 			/*parent*/ fc->dmat,
170 			/*alignment*/ alignment,
171 			/*boundary*/ 0,
172 			/*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
173 			/*highaddr*/ BUS_SPACE_MAXADDR,
174 			/*filter*/NULL, /*filterarg*/NULL,
175 			/*maxsize*/ ssize,
176 			/*nsegments*/ 1,
177 			/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
178 			/*flags*/ BUS_DMA_ALLOCNOW,
179 			/*lockfunc*/busdma_lock_mutex,
180 			/*lockarg*/FW_GMTX(fc),
181 			&am->dma_tag)) {
182 		printf("fwdma_malloc_multiseg: tag_create failed\n");
183 		free(am, M_FW);
184 		return (NULL);
185 	}
186 
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",
192 				am->nseg);
193 			fwdma_free_multiseg(am);
194 			return (NULL);
195 		}
196 		am->nseg++;
197 	}
198 	return (am);
199 }
200 
201 void
202 fwdma_free_multiseg(struct fwdma_alloc_multi *am)
203 {
204 	struct fwdma_seg *seg;
205 
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);
209 	}
210 	bus_dma_tag_destroy(am->dma_tag);
211 	free(am, M_FW);
212 }
213