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