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