isadma_bounce.c revision 1.2.8.2 1 1.2.8.2 jdolecek /* $NetBSD: isadma_bounce.c,v 1.2.8.2 2002/06/23 17:35:57 jdolecek Exp $ */
2 1.2.8.2 jdolecek
3 1.2.8.2 jdolecek /*-
4 1.2.8.2 jdolecek * Copyright (c) 1996, 1997, 1998, 2000, 2001 The NetBSD Foundation, Inc.
5 1.2.8.2 jdolecek * All rights reserved.
6 1.2.8.2 jdolecek *
7 1.2.8.2 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.2.8.2 jdolecek * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.2.8.2 jdolecek * NASA Ames Research Center.
10 1.2.8.2 jdolecek *
11 1.2.8.2 jdolecek * Redistribution and use in source and binary forms, with or without
12 1.2.8.2 jdolecek * modification, are permitted provided that the following conditions
13 1.2.8.2 jdolecek * are met:
14 1.2.8.2 jdolecek * 1. Redistributions of source code must retain the above copyright
15 1.2.8.2 jdolecek * notice, this list of conditions and the following disclaimer.
16 1.2.8.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
17 1.2.8.2 jdolecek * notice, this list of conditions and the following disclaimer in the
18 1.2.8.2 jdolecek * documentation and/or other materials provided with the distribution.
19 1.2.8.2 jdolecek * 3. All advertising materials mentioning features or use of this software
20 1.2.8.2 jdolecek * must display the following acknowledgement:
21 1.2.8.2 jdolecek * This product includes software developed by the NetBSD
22 1.2.8.2 jdolecek * Foundation, Inc. and its contributors.
23 1.2.8.2 jdolecek * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.2.8.2 jdolecek * contributors may be used to endorse or promote products derived
25 1.2.8.2 jdolecek * from this software without specific prior written permission.
26 1.2.8.2 jdolecek *
27 1.2.8.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.2.8.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.2.8.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.2.8.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.2.8.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.2.8.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.2.8.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.2.8.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.2.8.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.2.8.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.2.8.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
38 1.2.8.2 jdolecek */
39 1.2.8.2 jdolecek
40 1.2.8.2 jdolecek #include <sys/param.h>
41 1.2.8.2 jdolecek #include <sys/systm.h>
42 1.2.8.2 jdolecek #include <sys/syslog.h>
43 1.2.8.2 jdolecek #include <sys/device.h>
44 1.2.8.2 jdolecek #include <sys/malloc.h>
45 1.2.8.2 jdolecek #include <sys/proc.h>
46 1.2.8.2 jdolecek #include <sys/mbuf.h>
47 1.2.8.2 jdolecek
48 1.2.8.2 jdolecek #include <mips/cache.h>
49 1.2.8.2 jdolecek #define _MIPS_BUS_DMA_PRIVATE
50 1.2.8.2 jdolecek #include <machine/bus.h>
51 1.2.8.2 jdolecek #include <machine/locore.h>
52 1.2.8.2 jdolecek
53 1.2.8.2 jdolecek #include <dev/isa/isareg.h>
54 1.2.8.2 jdolecek #include <dev/isa/isavar.h>
55 1.2.8.2 jdolecek
56 1.2.8.2 jdolecek #include <uvm/uvm_extern.h>
57 1.2.8.2 jdolecek
58 1.2.8.2 jdolecek extern paddr_t avail_end;
59 1.2.8.2 jdolecek
60 1.2.8.2 jdolecek /*
61 1.2.8.2 jdolecek * ISA can only DMA to 0-16M.
62 1.2.8.2 jdolecek */
63 1.2.8.2 jdolecek #define ISA_DMA_BOUNCE_THRESHOLD (16 * 1024 * 1024)
64 1.2.8.2 jdolecek
65 1.2.8.2 jdolecek /*
66 1.2.8.2 jdolecek * Cookie used by bouncing ISA DMA. A pointer to one of these is stashed
67 1.2.8.2 jdolecek * in the DMA map.
68 1.2.8.2 jdolecek */
69 1.2.8.2 jdolecek struct isadma_bounce_cookie {
70 1.2.8.2 jdolecek int id_flags; /* flags; see below */
71 1.2.8.2 jdolecek
72 1.2.8.2 jdolecek /*
73 1.2.8.2 jdolecek * Information about the original buffer used during
74 1.2.8.2 jdolecek * DMA map syncs. Note that origbuflen is only used
75 1.2.8.2 jdolecek * for ID_BUFTYPE_LINEAR.
76 1.2.8.2 jdolecek */
77 1.2.8.2 jdolecek void *id_origbuf; /* pointer to orig buffer if
78 1.2.8.2 jdolecek bouncing */
79 1.2.8.2 jdolecek bus_size_t id_origbuflen; /* ...and size */
80 1.2.8.2 jdolecek int id_buftype; /* type of buffer */
81 1.2.8.2 jdolecek
82 1.2.8.2 jdolecek void *id_bouncebuf; /* pointer to the bounce buffer */
83 1.2.8.2 jdolecek bus_size_t id_bouncebuflen; /* ...and size */
84 1.2.8.2 jdolecek int id_nbouncesegs; /* number of valid bounce segs */
85 1.2.8.2 jdolecek bus_dma_segment_t id_bouncesegs[1]; /* array of bounce buffer
86 1.2.8.2 jdolecek physical memory segments */
87 1.2.8.2 jdolecek };
88 1.2.8.2 jdolecek
89 1.2.8.2 jdolecek /* id_flags */
90 1.2.8.2 jdolecek #define ID_MIGHT_NEED_BOUNCE 0x01 /* map could need bounce buffers */
91 1.2.8.2 jdolecek #define ID_HAS_BOUNCE 0x02 /* map currently has bounce buffers */
92 1.2.8.2 jdolecek #define ID_IS_BOUNCING 0x04 /* map is bouncing current xfer */
93 1.2.8.2 jdolecek
94 1.2.8.2 jdolecek /* id_buftype */
95 1.2.8.2 jdolecek #define ID_BUFTYPE_INVALID 0
96 1.2.8.2 jdolecek #define ID_BUFTYPE_LINEAR 1
97 1.2.8.2 jdolecek #define ID_BUFTYPE_MBUF 2
98 1.2.8.2 jdolecek #define ID_BUFTYPE_UIO 3
99 1.2.8.2 jdolecek #define ID_BUFTYPE_RAW 4
100 1.2.8.2 jdolecek
101 1.2.8.2 jdolecek int isadma_bounce_alloc_bouncebuf(bus_dma_tag_t, bus_dmamap_t,
102 1.2.8.2 jdolecek bus_size_t, int);
103 1.2.8.2 jdolecek void isadma_bounce_free_bouncebuf(bus_dma_tag_t, bus_dmamap_t);
104 1.2.8.2 jdolecek
105 1.2.8.2 jdolecek /*
106 1.2.8.2 jdolecek * Create an ISA DMA map.
107 1.2.8.2 jdolecek */
108 1.2.8.2 jdolecek int
109 1.2.8.2 jdolecek isadma_bounce_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
110 1.2.8.2 jdolecek bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
111 1.2.8.2 jdolecek {
112 1.2.8.2 jdolecek struct isadma_bounce_cookie *cookie;
113 1.2.8.2 jdolecek bus_dmamap_t map;
114 1.2.8.2 jdolecek int error, cookieflags;
115 1.2.8.2 jdolecek void *cookiestore;
116 1.2.8.2 jdolecek size_t cookiesize;
117 1.2.8.2 jdolecek
118 1.2.8.2 jdolecek /* Call common function to create the basic map. */
119 1.2.8.2 jdolecek error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
120 1.2.8.2 jdolecek flags, dmamp);
121 1.2.8.2 jdolecek if (error)
122 1.2.8.2 jdolecek return (error);
123 1.2.8.2 jdolecek
124 1.2.8.2 jdolecek map = *dmamp;
125 1.2.8.2 jdolecek map->_dm_cookie = NULL;
126 1.2.8.2 jdolecek
127 1.2.8.2 jdolecek cookiesize = sizeof(*cookie);
128 1.2.8.2 jdolecek
129 1.2.8.2 jdolecek /*
130 1.2.8.2 jdolecek * ISA only has 24-bits of address space. This means
131 1.2.8.2 jdolecek * we can't DMA to pages over 16M. In order to DMA to
132 1.2.8.2 jdolecek * arbitrary buffers, we use "bounce buffers" - pages
133 1.2.8.2 jdolecek * in memory below the 16M boundary. On DMA reads,
134 1.2.8.2 jdolecek * DMA happens to the bounce buffers, and is copied into
135 1.2.8.2 jdolecek * the caller's buffer. On writes, data is copied into
136 1.2.8.2 jdolecek * but bounce buffer, and the DMA happens from those
137 1.2.8.2 jdolecek * pages. To software using the DMA mapping interface,
138 1.2.8.2 jdolecek * this looks simply like a data cache.
139 1.2.8.2 jdolecek *
140 1.2.8.2 jdolecek * If we have more than 16M of RAM in the system, we may
141 1.2.8.2 jdolecek * need bounce buffers. We check and remember that here.
142 1.2.8.2 jdolecek *
143 1.2.8.2 jdolecek * ...or, there is an opposite case. The most segments
144 1.2.8.2 jdolecek * a transfer will require is (maxxfer / PAGE_SIZE) + 1. If
145 1.2.8.2 jdolecek * the caller can't handle that many segments (e.g. the
146 1.2.8.2 jdolecek * ISA DMA controller), we may have to bounce it as well.
147 1.2.8.2 jdolecek */
148 1.2.8.2 jdolecek cookieflags = 0;
149 1.2.8.2 jdolecek if (avail_end > (t->_wbase + t->_wsize) ||
150 1.2.8.2 jdolecek ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) {
151 1.2.8.2 jdolecek cookieflags |= ID_MIGHT_NEED_BOUNCE;
152 1.2.8.2 jdolecek cookiesize += (sizeof(bus_dma_segment_t) *
153 1.2.8.2 jdolecek (map->_dm_segcnt - 1));
154 1.2.8.2 jdolecek }
155 1.2.8.2 jdolecek
156 1.2.8.2 jdolecek /*
157 1.2.8.2 jdolecek * Allocate our cookie.
158 1.2.8.2 jdolecek */
159 1.2.8.2 jdolecek if ((cookiestore = malloc(cookiesize, M_DMAMAP,
160 1.2.8.2 jdolecek (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
161 1.2.8.2 jdolecek error = ENOMEM;
162 1.2.8.2 jdolecek goto out;
163 1.2.8.2 jdolecek }
164 1.2.8.2 jdolecek memset(cookiestore, 0, cookiesize);
165 1.2.8.2 jdolecek cookie = (struct isadma_bounce_cookie *)cookiestore;
166 1.2.8.2 jdolecek cookie->id_flags = cookieflags;
167 1.2.8.2 jdolecek map->_dm_cookie = cookie;
168 1.2.8.2 jdolecek
169 1.2.8.2 jdolecek if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
170 1.2.8.2 jdolecek /*
171 1.2.8.2 jdolecek * Allocate the bounce pages now if the caller
172 1.2.8.2 jdolecek * wishes us to do so.
173 1.2.8.2 jdolecek */
174 1.2.8.2 jdolecek if ((flags & BUS_DMA_ALLOCNOW) == 0)
175 1.2.8.2 jdolecek goto out;
176 1.2.8.2 jdolecek
177 1.2.8.2 jdolecek error = isadma_bounce_alloc_bouncebuf(t, map, size, flags);
178 1.2.8.2 jdolecek }
179 1.2.8.2 jdolecek
180 1.2.8.2 jdolecek out:
181 1.2.8.2 jdolecek if (error) {
182 1.2.8.2 jdolecek if (map->_dm_cookie != NULL)
183 1.2.8.2 jdolecek free(map->_dm_cookie, M_DMAMAP);
184 1.2.8.2 jdolecek _bus_dmamap_destroy(t, map);
185 1.2.8.2 jdolecek }
186 1.2.8.2 jdolecek return (error);
187 1.2.8.2 jdolecek }
188 1.2.8.2 jdolecek
189 1.2.8.2 jdolecek /*
190 1.2.8.2 jdolecek * Destroy an ISA DMA map.
191 1.2.8.2 jdolecek */
192 1.2.8.2 jdolecek void
193 1.2.8.2 jdolecek isadma_bounce_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
194 1.2.8.2 jdolecek {
195 1.2.8.2 jdolecek struct isadma_bounce_cookie *cookie = map->_dm_cookie;
196 1.2.8.2 jdolecek
197 1.2.8.2 jdolecek /*
198 1.2.8.2 jdolecek * Free any bounce pages this map might hold.
199 1.2.8.2 jdolecek */
200 1.2.8.2 jdolecek if (cookie->id_flags & ID_HAS_BOUNCE)
201 1.2.8.2 jdolecek isadma_bounce_free_bouncebuf(t, map);
202 1.2.8.2 jdolecek
203 1.2.8.2 jdolecek free(cookie, M_DMAMAP);
204 1.2.8.2 jdolecek _bus_dmamap_destroy(t, map);
205 1.2.8.2 jdolecek }
206 1.2.8.2 jdolecek
207 1.2.8.2 jdolecek /*
208 1.2.8.2 jdolecek * Load an ISA DMA map with a linear buffer.
209 1.2.8.2 jdolecek */
210 1.2.8.2 jdolecek int
211 1.2.8.2 jdolecek isadma_bounce_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
212 1.2.8.2 jdolecek bus_size_t buflen, struct proc *p, int flags)
213 1.2.8.2 jdolecek {
214 1.2.8.2 jdolecek struct isadma_bounce_cookie *cookie = map->_dm_cookie;
215 1.2.8.2 jdolecek int error;
216 1.2.8.2 jdolecek
217 1.2.8.2 jdolecek /*
218 1.2.8.2 jdolecek * Make sure that on error condition we return "no valid mappings."
219 1.2.8.2 jdolecek */
220 1.2.8.2 jdolecek map->dm_mapsize = 0;
221 1.2.8.2 jdolecek map->dm_nsegs = 0;
222 1.2.8.2 jdolecek
223 1.2.8.2 jdolecek /*
224 1.2.8.2 jdolecek * Try to load the map the normal way. If this errors out,
225 1.2.8.2 jdolecek * and we can bounce, we will.
226 1.2.8.2 jdolecek */
227 1.2.8.2 jdolecek error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
228 1.2.8.2 jdolecek if (error == 0 ||
229 1.2.8.2 jdolecek (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
230 1.2.8.2 jdolecek return (error);
231 1.2.8.2 jdolecek
232 1.2.8.2 jdolecek /*
233 1.2.8.2 jdolecek * First attempt failed; bounce it.
234 1.2.8.2 jdolecek */
235 1.2.8.2 jdolecek
236 1.2.8.2 jdolecek /*
237 1.2.8.2 jdolecek * Allocate bounce pages, if necessary.
238 1.2.8.2 jdolecek */
239 1.2.8.2 jdolecek if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
240 1.2.8.2 jdolecek error = isadma_bounce_alloc_bouncebuf(t, map, buflen, flags);
241 1.2.8.2 jdolecek if (error)
242 1.2.8.2 jdolecek return (error);
243 1.2.8.2 jdolecek }
244 1.2.8.2 jdolecek
245 1.2.8.2 jdolecek /*
246 1.2.8.2 jdolecek * Cache a pointer to the caller's buffer and load the DMA map
247 1.2.8.2 jdolecek * with the bounce buffer.
248 1.2.8.2 jdolecek */
249 1.2.8.2 jdolecek cookie->id_origbuf = buf;
250 1.2.8.2 jdolecek cookie->id_origbuflen = buflen;
251 1.2.8.2 jdolecek cookie->id_buftype = ID_BUFTYPE_LINEAR;
252 1.2.8.2 jdolecek error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
253 1.2.8.2 jdolecek p, flags);
254 1.2.8.2 jdolecek if (error) {
255 1.2.8.2 jdolecek /*
256 1.2.8.2 jdolecek * Free the bounce pages, unless our resources
257 1.2.8.2 jdolecek * are reserved for our exclusive use.
258 1.2.8.2 jdolecek */
259 1.2.8.2 jdolecek if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
260 1.2.8.2 jdolecek isadma_bounce_free_bouncebuf(t, map);
261 1.2.8.2 jdolecek return (error);
262 1.2.8.2 jdolecek }
263 1.2.8.2 jdolecek
264 1.2.8.2 jdolecek /* ...so isadma_bounce_dmamap_sync() knows we're bouncing */
265 1.2.8.2 jdolecek cookie->id_flags |= ID_IS_BOUNCING;
266 1.2.8.2 jdolecek return (0);
267 1.2.8.2 jdolecek }
268 1.2.8.2 jdolecek
269 1.2.8.2 jdolecek /*
270 1.2.8.2 jdolecek * Like isadma_bounce_dmamap_load(), but for mbufs.
271 1.2.8.2 jdolecek */
272 1.2.8.2 jdolecek int
273 1.2.8.2 jdolecek isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map,
274 1.2.8.2 jdolecek struct mbuf *m0, int flags)
275 1.2.8.2 jdolecek {
276 1.2.8.2 jdolecek struct isadma_bounce_cookie *cookie = map->_dm_cookie;
277 1.2.8.2 jdolecek int error;
278 1.2.8.2 jdolecek
279 1.2.8.2 jdolecek /*
280 1.2.8.2 jdolecek * Make sure on error condition we return "no valid mappings."
281 1.2.8.2 jdolecek */
282 1.2.8.2 jdolecek map->dm_mapsize = 0;
283 1.2.8.2 jdolecek map->dm_nsegs = 0;
284 1.2.8.2 jdolecek
285 1.2.8.2 jdolecek #ifdef DIAGNOSTIC
286 1.2.8.2 jdolecek if ((m0->m_flags & M_PKTHDR) == 0)
287 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_load_mbuf: no packet header");
288 1.2.8.2 jdolecek #endif
289 1.2.8.2 jdolecek
290 1.2.8.2 jdolecek if (m0->m_pkthdr.len > map->_dm_size)
291 1.2.8.2 jdolecek return (EINVAL);
292 1.2.8.2 jdolecek
293 1.2.8.2 jdolecek /*
294 1.2.8.2 jdolecek * Try to load the map the normal way. If this errors out,
295 1.2.8.2 jdolecek * and we can bounce, we will.
296 1.2.8.2 jdolecek */
297 1.2.8.2 jdolecek error = _bus_dmamap_load_mbuf(t, map, m0, flags);
298 1.2.8.2 jdolecek if (error == 0 ||
299 1.2.8.2 jdolecek (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
300 1.2.8.2 jdolecek return (error);
301 1.2.8.2 jdolecek
302 1.2.8.2 jdolecek /*
303 1.2.8.2 jdolecek * First attempt failed; bounce it.
304 1.2.8.2 jdolecek */
305 1.2.8.2 jdolecek
306 1.2.8.2 jdolecek /*
307 1.2.8.2 jdolecek * Allocate bounce pages, if necessary.
308 1.2.8.2 jdolecek */
309 1.2.8.2 jdolecek if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
310 1.2.8.2 jdolecek error = isadma_bounce_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
311 1.2.8.2 jdolecek flags);
312 1.2.8.2 jdolecek if (error)
313 1.2.8.2 jdolecek return (error);
314 1.2.8.2 jdolecek }
315 1.2.8.2 jdolecek
316 1.2.8.2 jdolecek /*
317 1.2.8.2 jdolecek * Cache a pointer to the caller's buffer and load the DMA map
318 1.2.8.2 jdolecek * with the bounce buffer.
319 1.2.8.2 jdolecek */
320 1.2.8.2 jdolecek cookie->id_origbuf = m0;
321 1.2.8.2 jdolecek cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
322 1.2.8.2 jdolecek cookie->id_buftype = ID_BUFTYPE_MBUF;
323 1.2.8.2 jdolecek error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
324 1.2.8.2 jdolecek m0->m_pkthdr.len, NULL, flags);
325 1.2.8.2 jdolecek if (error) {
326 1.2.8.2 jdolecek /*
327 1.2.8.2 jdolecek * Free the bounce pages, unless our resources
328 1.2.8.2 jdolecek * are reserved for our exclusive use.
329 1.2.8.2 jdolecek */
330 1.2.8.2 jdolecek if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
331 1.2.8.2 jdolecek isadma_bounce_free_bouncebuf(t, map);
332 1.2.8.2 jdolecek return (error);
333 1.2.8.2 jdolecek }
334 1.2.8.2 jdolecek
335 1.2.8.2 jdolecek /* ...so isadma_bounce_dmamap_sync() knows we're bouncing */
336 1.2.8.2 jdolecek cookie->id_flags |= ID_IS_BOUNCING;
337 1.2.8.2 jdolecek return (0);
338 1.2.8.2 jdolecek }
339 1.2.8.2 jdolecek
340 1.2.8.2 jdolecek /*
341 1.2.8.2 jdolecek * Like isadma_bounce_dmamap_load(), but for uios.
342 1.2.8.2 jdolecek */
343 1.2.8.2 jdolecek int
344 1.2.8.2 jdolecek isadma_bounce_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map,
345 1.2.8.2 jdolecek struct uio *uio, int flags)
346 1.2.8.2 jdolecek {
347 1.2.8.2 jdolecek
348 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_load_uio: not implemented");
349 1.2.8.2 jdolecek }
350 1.2.8.2 jdolecek
351 1.2.8.2 jdolecek /*
352 1.2.8.2 jdolecek * Like isadma_bounce_dmamap_load(), but for raw memory allocated with
353 1.2.8.2 jdolecek * bus_dmamem_alloc().
354 1.2.8.2 jdolecek */
355 1.2.8.2 jdolecek int
356 1.2.8.2 jdolecek isadma_bounce_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
357 1.2.8.2 jdolecek bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
358 1.2.8.2 jdolecek {
359 1.2.8.2 jdolecek
360 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_load_raw: not implemented");
361 1.2.8.2 jdolecek }
362 1.2.8.2 jdolecek
363 1.2.8.2 jdolecek /*
364 1.2.8.2 jdolecek * Unload an ISA DMA map.
365 1.2.8.2 jdolecek */
366 1.2.8.2 jdolecek void
367 1.2.8.2 jdolecek isadma_bounce_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
368 1.2.8.2 jdolecek {
369 1.2.8.2 jdolecek struct isadma_bounce_cookie *cookie = map->_dm_cookie;
370 1.2.8.2 jdolecek
371 1.2.8.2 jdolecek /*
372 1.2.8.2 jdolecek * If we have bounce pages, free them, unless they're
373 1.2.8.2 jdolecek * reserved for our exclusive use.
374 1.2.8.2 jdolecek */
375 1.2.8.2 jdolecek if ((cookie->id_flags & ID_HAS_BOUNCE) &&
376 1.2.8.2 jdolecek (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
377 1.2.8.2 jdolecek isadma_bounce_free_bouncebuf(t, map);
378 1.2.8.2 jdolecek
379 1.2.8.2 jdolecek cookie->id_flags &= ~ID_IS_BOUNCING;
380 1.2.8.2 jdolecek cookie->id_buftype = ID_BUFTYPE_INVALID;
381 1.2.8.2 jdolecek
382 1.2.8.2 jdolecek /*
383 1.2.8.2 jdolecek * Do the generic bits of the unload.
384 1.2.8.2 jdolecek */
385 1.2.8.2 jdolecek _bus_dmamap_unload(t, map);
386 1.2.8.2 jdolecek }
387 1.2.8.2 jdolecek
388 1.2.8.2 jdolecek /*
389 1.2.8.2 jdolecek * Synchronize an ISA DMA map.
390 1.2.8.2 jdolecek */
391 1.2.8.2 jdolecek void
392 1.2.8.2 jdolecek isadma_bounce_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
393 1.2.8.2 jdolecek bus_size_t len, int ops)
394 1.2.8.2 jdolecek {
395 1.2.8.2 jdolecek struct isadma_bounce_cookie *cookie = map->_dm_cookie;
396 1.2.8.2 jdolecek
397 1.2.8.2 jdolecek /*
398 1.2.8.2 jdolecek * Mixing PRE and POST operations is not allowed.
399 1.2.8.2 jdolecek */
400 1.2.8.2 jdolecek if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
401 1.2.8.2 jdolecek (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
402 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_sync: mix PRE and POST");
403 1.2.8.2 jdolecek
404 1.2.8.2 jdolecek #ifdef DIAGNOSTIC
405 1.2.8.2 jdolecek if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
406 1.2.8.2 jdolecek if (offset >= map->dm_mapsize)
407 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_sync: bad offset");
408 1.2.8.2 jdolecek if (len == 0 || (offset + len) > map->dm_mapsize)
409 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_sync: bad length");
410 1.2.8.2 jdolecek }
411 1.2.8.2 jdolecek #endif
412 1.2.8.2 jdolecek
413 1.2.8.2 jdolecek /*
414 1.2.8.2 jdolecek * If we're not bouncing, just do the normal sync operation
415 1.2.8.2 jdolecek * and return.
416 1.2.8.2 jdolecek */
417 1.2.8.2 jdolecek if ((cookie->id_flags & ID_IS_BOUNCING) == 0) {
418 1.2.8.2 jdolecek _bus_dmamap_sync(t, map, offset, len, ops);
419 1.2.8.2 jdolecek return;
420 1.2.8.2 jdolecek }
421 1.2.8.2 jdolecek
422 1.2.8.2 jdolecek /*
423 1.2.8.2 jdolecek * Flush data cache for PREREAD. This has the side-effect
424 1.2.8.2 jdolecek * of invalidating the cache. Done at PREREAD since it
425 1.2.8.2 jdolecek * causes the cache line(s) to be written back to memory.
426 1.2.8.2 jdolecek *
427 1.2.8.2 jdolecek * Copy the original buffer to the bounce buffer and flush
428 1.2.8.2 jdolecek * the data cache for PREWRITE, so that the contents
429 1.2.8.2 jdolecek * of the data buffer in memory reflect reality.
430 1.2.8.2 jdolecek *
431 1.2.8.2 jdolecek * Copy the bounce buffer to the original buffer in POSTREAD.
432 1.2.8.2 jdolecek */
433 1.2.8.2 jdolecek
434 1.2.8.2 jdolecek switch (cookie->id_buftype) {
435 1.2.8.2 jdolecek case ID_BUFTYPE_LINEAR:
436 1.2.8.2 jdolecek /*
437 1.2.8.2 jdolecek * Nothing to do for pre-read.
438 1.2.8.2 jdolecek */
439 1.2.8.2 jdolecek
440 1.2.8.2 jdolecek if (ops & BUS_DMASYNC_PREWRITE) {
441 1.2.8.2 jdolecek /*
442 1.2.8.2 jdolecek * Copy the caller's buffer to the bounce buffer.
443 1.2.8.2 jdolecek */
444 1.2.8.2 jdolecek memcpy((char *)cookie->id_bouncebuf + offset,
445 1.2.8.2 jdolecek (char *)cookie->id_origbuf + offset, len);
446 1.2.8.2 jdolecek wbflush();
447 1.2.8.2 jdolecek }
448 1.2.8.2 jdolecek
449 1.2.8.2 jdolecek if (ops & BUS_DMASYNC_POSTREAD) {
450 1.2.8.2 jdolecek /*
451 1.2.8.2 jdolecek * Copy the bounce buffer to the caller's buffer.
452 1.2.8.2 jdolecek */
453 1.2.8.2 jdolecek memcpy((char *)cookie->id_origbuf + offset,
454 1.2.8.2 jdolecek (char *)cookie->id_bouncebuf + offset, len);
455 1.2.8.2 jdolecek }
456 1.2.8.2 jdolecek
457 1.2.8.2 jdolecek /*
458 1.2.8.2 jdolecek * Nothing to do for post-write.
459 1.2.8.2 jdolecek */
460 1.2.8.2 jdolecek break;
461 1.2.8.2 jdolecek
462 1.2.8.2 jdolecek case ID_BUFTYPE_MBUF:
463 1.2.8.2 jdolecek {
464 1.2.8.2 jdolecek struct mbuf *m, *m0 = cookie->id_origbuf;
465 1.2.8.2 jdolecek bus_size_t minlen, moff;
466 1.2.8.2 jdolecek
467 1.2.8.2 jdolecek /*
468 1.2.8.2 jdolecek * Nothing to do for pre-read.
469 1.2.8.2 jdolecek */
470 1.2.8.2 jdolecek
471 1.2.8.2 jdolecek if (ops & BUS_DMASYNC_PREWRITE) {
472 1.2.8.2 jdolecek /*
473 1.2.8.2 jdolecek * Copy the caller's buffer to the bounce buffer.
474 1.2.8.2 jdolecek */
475 1.2.8.2 jdolecek m_copydata(m0, offset, len,
476 1.2.8.2 jdolecek (char *)cookie->id_bouncebuf + offset);
477 1.2.8.2 jdolecek }
478 1.2.8.2 jdolecek
479 1.2.8.2 jdolecek if (ops & BUS_DMASYNC_POSTREAD) {
480 1.2.8.2 jdolecek /*
481 1.2.8.2 jdolecek * Copy the bounce buffer to the caller's buffer.
482 1.2.8.2 jdolecek */
483 1.2.8.2 jdolecek for (moff = offset, m = m0; m != NULL && len != 0;
484 1.2.8.2 jdolecek m = m->m_next) {
485 1.2.8.2 jdolecek /* Find the beginning mbuf. */
486 1.2.8.2 jdolecek if (moff >= m->m_len) {
487 1.2.8.2 jdolecek moff -= m->m_len;
488 1.2.8.2 jdolecek continue;
489 1.2.8.2 jdolecek }
490 1.2.8.2 jdolecek
491 1.2.8.2 jdolecek /*
492 1.2.8.2 jdolecek * Now at the first mbuf to sync; nail
493 1.2.8.2 jdolecek * each one until we have exhausted the
494 1.2.8.2 jdolecek * length.
495 1.2.8.2 jdolecek */
496 1.2.8.2 jdolecek minlen = len < m->m_len - moff ?
497 1.2.8.2 jdolecek len : m->m_len - moff;
498 1.2.8.2 jdolecek
499 1.2.8.2 jdolecek memcpy(mtod(m, caddr_t) + moff,
500 1.2.8.2 jdolecek (char *)cookie->id_bouncebuf + offset,
501 1.2.8.2 jdolecek minlen);
502 1.2.8.2 jdolecek
503 1.2.8.2 jdolecek moff = 0;
504 1.2.8.2 jdolecek len -= minlen;
505 1.2.8.2 jdolecek offset += minlen;
506 1.2.8.2 jdolecek }
507 1.2.8.2 jdolecek }
508 1.2.8.2 jdolecek
509 1.2.8.2 jdolecek /*
510 1.2.8.2 jdolecek * Nothing to do for post-write.
511 1.2.8.2 jdolecek */
512 1.2.8.2 jdolecek break;
513 1.2.8.2 jdolecek }
514 1.2.8.2 jdolecek
515 1.2.8.2 jdolecek case ID_BUFTYPE_UIO:
516 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_UIO");
517 1.2.8.2 jdolecek break;
518 1.2.8.2 jdolecek
519 1.2.8.2 jdolecek case ID_BUFTYPE_RAW:
520 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_RAW");
521 1.2.8.2 jdolecek break;
522 1.2.8.2 jdolecek
523 1.2.8.2 jdolecek case ID_BUFTYPE_INVALID:
524 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_INVALID");
525 1.2.8.2 jdolecek break;
526 1.2.8.2 jdolecek
527 1.2.8.2 jdolecek default:
528 1.2.8.2 jdolecek printf("unknown buffer type %d\n", cookie->id_buftype);
529 1.2.8.2 jdolecek panic("isadma_bounce_dmamap_sync");
530 1.2.8.2 jdolecek }
531 1.2.8.2 jdolecek
532 1.2.8.2 jdolecek /* Drain the write buffer. */
533 1.2.8.2 jdolecek wbflush();
534 1.2.8.2 jdolecek
535 1.2.8.2 jdolecek /* XXXJRT */
536 1.2.8.2 jdolecek if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE))
537 1.2.8.2 jdolecek mips_dcache_wbinv_range((vaddr_t)cookie->id_bouncebuf + offset,
538 1.2.8.2 jdolecek len);
539 1.2.8.2 jdolecek }
540 1.2.8.2 jdolecek
541 1.2.8.2 jdolecek /*
542 1.2.8.2 jdolecek * Allocate memory safe for ISA DMA.
543 1.2.8.2 jdolecek */
544 1.2.8.2 jdolecek int
545 1.2.8.2 jdolecek isadma_bounce_dmamem_alloc(bus_dma_tag_t t, bus_size_t size,
546 1.2.8.2 jdolecek bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
547 1.2.8.2 jdolecek int nsegs, int *rsegs, int flags)
548 1.2.8.2 jdolecek {
549 1.2.8.2 jdolecek paddr_t high;
550 1.2.8.2 jdolecek
551 1.2.8.2 jdolecek if (avail_end > ISA_DMA_BOUNCE_THRESHOLD)
552 1.2.8.2 jdolecek high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD);
553 1.2.8.2 jdolecek else
554 1.2.8.2 jdolecek high = trunc_page(avail_end);
555 1.2.8.2 jdolecek
556 1.2.8.2 jdolecek return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
557 1.2.8.2 jdolecek segs, nsegs, rsegs, flags, 0, high));
558 1.2.8.2 jdolecek }
559 1.2.8.2 jdolecek
560 1.2.8.2 jdolecek /**********************************************************************
561 1.2.8.2 jdolecek * ISA DMA utility functions
562 1.2.8.2 jdolecek **********************************************************************/
563 1.2.8.2 jdolecek
564 1.2.8.2 jdolecek int
565 1.2.8.2 jdolecek isadma_bounce_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
566 1.2.8.2 jdolecek bus_size_t size, int flags)
567 1.2.8.2 jdolecek {
568 1.2.8.2 jdolecek struct isadma_bounce_cookie *cookie = map->_dm_cookie;
569 1.2.8.2 jdolecek int error = 0;
570 1.2.8.2 jdolecek
571 1.2.8.2 jdolecek cookie->id_bouncebuflen = round_page(size);
572 1.2.8.2 jdolecek error = isadma_bounce_dmamem_alloc(t, cookie->id_bouncebuflen,
573 1.2.8.2 jdolecek PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
574 1.2.8.2 jdolecek map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
575 1.2.8.2 jdolecek if (error)
576 1.2.8.2 jdolecek goto out;
577 1.2.8.2 jdolecek error = _bus_dmamem_map(t, cookie->id_bouncesegs,
578 1.2.8.2 jdolecek cookie->id_nbouncesegs, cookie->id_bouncebuflen,
579 1.2.8.2 jdolecek (caddr_t *)&cookie->id_bouncebuf, flags);
580 1.2.8.2 jdolecek
581 1.2.8.2 jdolecek out:
582 1.2.8.2 jdolecek if (error) {
583 1.2.8.2 jdolecek _bus_dmamem_free(t, cookie->id_bouncesegs,
584 1.2.8.2 jdolecek cookie->id_nbouncesegs);
585 1.2.8.2 jdolecek cookie->id_bouncebuflen = 0;
586 1.2.8.2 jdolecek cookie->id_nbouncesegs = 0;
587 1.2.8.2 jdolecek } else
588 1.2.8.2 jdolecek cookie->id_flags |= ID_HAS_BOUNCE;
589 1.2.8.2 jdolecek
590 1.2.8.2 jdolecek return (error);
591 1.2.8.2 jdolecek }
592 1.2.8.2 jdolecek
593 1.2.8.2 jdolecek void
594 1.2.8.2 jdolecek isadma_bounce_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
595 1.2.8.2 jdolecek {
596 1.2.8.2 jdolecek struct isadma_bounce_cookie *cookie = map->_dm_cookie;
597 1.2.8.2 jdolecek
598 1.2.8.2 jdolecek _bus_dmamem_unmap(t, cookie->id_bouncebuf,
599 1.2.8.2 jdolecek cookie->id_bouncebuflen);
600 1.2.8.2 jdolecek _bus_dmamem_free(t, cookie->id_bouncesegs,
601 1.2.8.2 jdolecek cookie->id_nbouncesegs);
602 1.2.8.2 jdolecek cookie->id_bouncebuflen = 0;
603 1.2.8.2 jdolecek cookie->id_nbouncesegs = 0;
604 1.2.8.2 jdolecek cookie->id_flags &= ~ID_HAS_BOUNCE;
605 1.2.8.2 jdolecek }
606