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