bus_dma.c revision 1.37 1 1.37 simonb /* $NetBSD: bus_dma.c,v 1.37 2000/06/26 04:55:21 simonb Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.8 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.2 thorpej * NASA Ames Research Center.
10 1.2 thorpej *
11 1.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.2 thorpej * modification, are permitted provided that the following conditions
13 1.2 thorpej * are met:
14 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.2 thorpej * must display the following acknowledgement:
21 1.2 thorpej * This product includes software developed by the NetBSD
22 1.2 thorpej * Foundation, Inc. and its contributors.
23 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.2 thorpej * contributors may be used to endorse or promote products derived
25 1.2 thorpej * from this software without specific prior written permission.
26 1.2 thorpej *
27 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.2 thorpej */
39 1.2 thorpej
40 1.2 thorpej #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
41 1.2 thorpej
42 1.37 simonb __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.37 2000/06/26 04:55:21 simonb Exp $");
43 1.2 thorpej
44 1.2 thorpej #include <sys/param.h>
45 1.2 thorpej #include <sys/systm.h>
46 1.2 thorpej #include <sys/kernel.h>
47 1.2 thorpej #include <sys/device.h>
48 1.2 thorpej #include <sys/malloc.h>
49 1.2 thorpej #include <sys/proc.h>
50 1.9 thorpej #include <sys/mbuf.h>
51 1.2 thorpej
52 1.2 thorpej #include <vm/vm.h>
53 1.2 thorpej #include <vm/vm_kern.h>
54 1.28 mrg
55 1.16 thorpej #include <uvm/uvm_extern.h>
56 1.2 thorpej
57 1.2 thorpej #define _ALPHA_BUS_DMA_PRIVATE
58 1.2 thorpej #include <machine/bus.h>
59 1.3 thorpej #include <machine/intr.h>
60 1.2 thorpej
61 1.26 thorpej int _bus_dmamap_load_buffer_direct_common __P((bus_dma_tag_t,
62 1.26 thorpej bus_dmamap_t, void *, bus_size_t, struct proc *, int,
63 1.26 thorpej paddr_t *, int *, int));
64 1.9 thorpej
65 1.34 thorpej extern paddr_t avail_start, avail_end; /* from pmap.c */
66 1.34 thorpej
67 1.2 thorpej /*
68 1.2 thorpej * Common function for DMA map creation. May be called by bus-specific
69 1.2 thorpej * DMA map creation functions.
70 1.2 thorpej */
71 1.2 thorpej int
72 1.2 thorpej _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
73 1.2 thorpej bus_dma_tag_t t;
74 1.2 thorpej bus_size_t size;
75 1.2 thorpej int nsegments;
76 1.2 thorpej bus_size_t maxsegsz;
77 1.2 thorpej bus_size_t boundary;
78 1.2 thorpej int flags;
79 1.2 thorpej bus_dmamap_t *dmamp;
80 1.2 thorpej {
81 1.2 thorpej struct alpha_bus_dmamap *map;
82 1.2 thorpej void *mapstore;
83 1.2 thorpej size_t mapsize;
84 1.2 thorpej
85 1.2 thorpej /*
86 1.35 mjacob * Allocate and initialize the DMA map. The end of the map
87 1.2 thorpej * is a variable-sized array of segments, so we allocate enough
88 1.2 thorpej * room for them in one shot.
89 1.2 thorpej *
90 1.2 thorpej * Note we don't preserve the WAITOK or NOWAIT flags. Preservation
91 1.2 thorpej * of ALLOCNOW notifes others that we've reserved these resources,
92 1.2 thorpej * and they are not to be freed.
93 1.2 thorpej *
94 1.2 thorpej * The bus_dmamap_t includes one bus_dma_segment_t, hence
95 1.2 thorpej * the (nsegments - 1).
96 1.2 thorpej */
97 1.2 thorpej mapsize = sizeof(struct alpha_bus_dmamap) +
98 1.2 thorpej (sizeof(bus_dma_segment_t) * (nsegments - 1));
99 1.14 thorpej if ((mapstore = malloc(mapsize, M_DMAMAP,
100 1.2 thorpej (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
101 1.2 thorpej return (ENOMEM);
102 1.2 thorpej
103 1.2 thorpej bzero(mapstore, mapsize);
104 1.2 thorpej map = (struct alpha_bus_dmamap *)mapstore;
105 1.2 thorpej map->_dm_size = size;
106 1.2 thorpej map->_dm_segcnt = nsegments;
107 1.2 thorpej map->_dm_maxsegsz = maxsegsz;
108 1.23 thorpej if (t->_boundary != 0 && t->_boundary < boundary)
109 1.23 thorpej map->_dm_boundary = t->_boundary;
110 1.23 thorpej else
111 1.23 thorpej map->_dm_boundary = boundary;
112 1.2 thorpej map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
113 1.10 thorpej map->dm_mapsize = 0; /* no valid mappings */
114 1.10 thorpej map->dm_nsegs = 0;
115 1.2 thorpej
116 1.2 thorpej *dmamp = map;
117 1.2 thorpej return (0);
118 1.2 thorpej }
119 1.2 thorpej
120 1.2 thorpej /*
121 1.2 thorpej * Common function for DMA map destruction. May be called by bus-specific
122 1.2 thorpej * DMA map destruction functions.
123 1.2 thorpej */
124 1.2 thorpej void
125 1.2 thorpej _bus_dmamap_destroy(t, map)
126 1.2 thorpej bus_dma_tag_t t;
127 1.2 thorpej bus_dmamap_t map;
128 1.2 thorpej {
129 1.2 thorpej
130 1.14 thorpej free(map, M_DMAMAP);
131 1.2 thorpej }
132 1.2 thorpej
133 1.2 thorpej /*
134 1.9 thorpej * Utility function to load a linear buffer. lastaddrp holds state
135 1.9 thorpej * between invocations (for multiple-buffer loads). segp contains
136 1.9 thorpej * the starting segment on entrance, and the ending segment on exit.
137 1.9 thorpej * first indicates if this is the first invocation of this function.
138 1.2 thorpej */
139 1.2 thorpej int
140 1.26 thorpej _bus_dmamap_load_buffer_direct_common(t, map, buf, buflen, p, flags,
141 1.26 thorpej lastaddrp, segp, first)
142 1.26 thorpej bus_dma_tag_t t;
143 1.2 thorpej bus_dmamap_t map;
144 1.2 thorpej void *buf;
145 1.2 thorpej bus_size_t buflen;
146 1.2 thorpej struct proc *p;
147 1.2 thorpej int flags;
148 1.25 thorpej paddr_t *lastaddrp;
149 1.9 thorpej int *segp;
150 1.9 thorpej int first;
151 1.2 thorpej {
152 1.2 thorpej bus_size_t sgsize;
153 1.22 thorpej bus_addr_t curaddr, lastaddr, baddr, bmask;
154 1.25 thorpej vaddr_t vaddr = (vaddr_t)buf;
155 1.9 thorpej int seg;
156 1.2 thorpej
157 1.9 thorpej lastaddr = *lastaddrp;
158 1.21 matt bmask = ~(map->_dm_boundary - 1);
159 1.2 thorpej
160 1.20 matt for (seg = *segp; buflen > 0 ; ) {
161 1.2 thorpej /*
162 1.2 thorpej * Get the physical address for this segment.
163 1.2 thorpej */
164 1.2 thorpej if (p != NULL)
165 1.31 thorpej (void) pmap_extract(p->p_vmspace->vm_map.pmap,
166 1.31 thorpej vaddr, &curaddr);
167 1.2 thorpej else
168 1.2 thorpej curaddr = vtophys(vaddr);
169 1.2 thorpej
170 1.19 thorpej /*
171 1.19 thorpej * If we're beyond the current DMA window, indicate
172 1.19 thorpej * that and try to fall back into SGMAPs.
173 1.19 thorpej */
174 1.26 thorpej if (t->_wsize != 0 && curaddr >= t->_wsize)
175 1.19 thorpej return (EINVAL);
176 1.19 thorpej
177 1.26 thorpej curaddr |= t->_wbase;
178 1.2 thorpej
179 1.2 thorpej /*
180 1.2 thorpej * Compute the segment size, and adjust counts.
181 1.2 thorpej */
182 1.2 thorpej sgsize = NBPG - ((u_long)vaddr & PGOFSET);
183 1.2 thorpej if (buflen < sgsize)
184 1.2 thorpej sgsize = buflen;
185 1.22 thorpej
186 1.20 matt /*
187 1.22 thorpej * Make sure we don't cross any boundaries.
188 1.20 matt */
189 1.21 matt if (map->_dm_boundary > 0) {
190 1.20 matt baddr = (curaddr + map->_dm_boundary) & bmask;
191 1.22 thorpej if (sgsize > (baddr - curaddr))
192 1.22 thorpej sgsize = (baddr - curaddr);
193 1.20 matt }
194 1.2 thorpej
195 1.2 thorpej /*
196 1.2 thorpej * Insert chunk into a segment, coalescing with
197 1.2 thorpej * the previous segment if possible.
198 1.2 thorpej */
199 1.2 thorpej if (first) {
200 1.2 thorpej map->dm_segs[seg].ds_addr = curaddr;
201 1.2 thorpej map->dm_segs[seg].ds_len = sgsize;
202 1.2 thorpej first = 0;
203 1.2 thorpej } else {
204 1.36 thorpej if ((map->_dm_flags & DMAMAP_NO_COALESCE) == 0 &&
205 1.36 thorpej curaddr == lastaddr &&
206 1.2 thorpej (map->dm_segs[seg].ds_len + sgsize) <=
207 1.22 thorpej map->_dm_maxsegsz &&
208 1.20 matt (map->_dm_boundary == 0 ||
209 1.20 matt (map->dm_segs[seg].ds_addr & bmask) ==
210 1.21 matt (curaddr & bmask)))
211 1.2 thorpej map->dm_segs[seg].ds_len += sgsize;
212 1.2 thorpej else {
213 1.20 matt if (++seg >= map->_dm_segcnt)
214 1.20 matt break;
215 1.2 thorpej map->dm_segs[seg].ds_addr = curaddr;
216 1.2 thorpej map->dm_segs[seg].ds_len = sgsize;
217 1.2 thorpej }
218 1.2 thorpej }
219 1.2 thorpej
220 1.2 thorpej lastaddr = curaddr + sgsize;
221 1.2 thorpej vaddr += sgsize;
222 1.2 thorpej buflen -= sgsize;
223 1.2 thorpej }
224 1.2 thorpej
225 1.9 thorpej *segp = seg;
226 1.9 thorpej *lastaddrp = lastaddr;
227 1.9 thorpej
228 1.2 thorpej /*
229 1.2 thorpej * Did we fit?
230 1.2 thorpej */
231 1.2 thorpej if (buflen != 0) {
232 1.2 thorpej /*
233 1.19 thorpej * If there is a chained window, we will automatically
234 1.19 thorpej * fall back to it.
235 1.2 thorpej */
236 1.2 thorpej return (EFBIG); /* XXX better return value here? */
237 1.2 thorpej }
238 1.19 thorpej
239 1.9 thorpej return (0);
240 1.9 thorpej }
241 1.9 thorpej
242 1.9 thorpej /*
243 1.9 thorpej * Common function for loading a direct-mapped DMA map with a linear
244 1.9 thorpej * buffer. Called by bus-specific DMA map load functions with the
245 1.9 thorpej * OR value appropriate for indicating "direct-mapped" for that
246 1.9 thorpej * chipset.
247 1.9 thorpej */
248 1.9 thorpej int
249 1.18 thorpej _bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
250 1.9 thorpej bus_dma_tag_t t;
251 1.9 thorpej bus_dmamap_t map;
252 1.9 thorpej void *buf;
253 1.9 thorpej bus_size_t buflen;
254 1.9 thorpej struct proc *p;
255 1.9 thorpej int flags;
256 1.9 thorpej {
257 1.25 thorpej paddr_t lastaddr;
258 1.9 thorpej int seg, error;
259 1.9 thorpej
260 1.9 thorpej /*
261 1.9 thorpej * Make sure that on error condition we return "no valid mappings".
262 1.9 thorpej */
263 1.10 thorpej map->dm_mapsize = 0;
264 1.9 thorpej map->dm_nsegs = 0;
265 1.2 thorpej
266 1.9 thorpej if (buflen > map->_dm_size)
267 1.9 thorpej return (EINVAL);
268 1.9 thorpej
269 1.9 thorpej seg = 0;
270 1.26 thorpej error = _bus_dmamap_load_buffer_direct_common(t, map, buf, buflen,
271 1.26 thorpej p, flags, &lastaddr, &seg, 1);
272 1.10 thorpej if (error == 0) {
273 1.10 thorpej map->dm_mapsize = buflen;
274 1.9 thorpej map->dm_nsegs = seg + 1;
275 1.19 thorpej } else if (t->_next_window != NULL) {
276 1.19 thorpej /*
277 1.19 thorpej * Give the next window a chance.
278 1.19 thorpej */
279 1.19 thorpej error = bus_dmamap_load(t->_next_window, map, buf, buflen,
280 1.19 thorpej p, flags);
281 1.10 thorpej }
282 1.9 thorpej return (error);
283 1.2 thorpej }
284 1.2 thorpej
285 1.2 thorpej /*
286 1.2 thorpej * Like _bus_dmamap_load_direct_common(), but for mbufs.
287 1.2 thorpej */
288 1.2 thorpej int
289 1.18 thorpej _bus_dmamap_load_mbuf_direct(t, map, m0, flags)
290 1.2 thorpej bus_dma_tag_t t;
291 1.2 thorpej bus_dmamap_t map;
292 1.9 thorpej struct mbuf *m0;
293 1.2 thorpej int flags;
294 1.2 thorpej {
295 1.25 thorpej paddr_t lastaddr;
296 1.9 thorpej int seg, error, first;
297 1.9 thorpej struct mbuf *m;
298 1.9 thorpej
299 1.9 thorpej /*
300 1.9 thorpej * Make sure that on error condition we return "no valid mappings."
301 1.9 thorpej */
302 1.10 thorpej map->dm_mapsize = 0;
303 1.9 thorpej map->dm_nsegs = 0;
304 1.9 thorpej
305 1.9 thorpej #ifdef DIAGNOSTIC
306 1.9 thorpej if ((m0->m_flags & M_PKTHDR) == 0)
307 1.9 thorpej panic("_bus_dmamap_load_mbuf_direct_common: no packet header");
308 1.9 thorpej #endif
309 1.2 thorpej
310 1.9 thorpej if (m0->m_pkthdr.len > map->_dm_size)
311 1.9 thorpej return (EINVAL);
312 1.9 thorpej
313 1.9 thorpej first = 1;
314 1.9 thorpej seg = 0;
315 1.9 thorpej error = 0;
316 1.9 thorpej for (m = m0; m != NULL && error == 0; m = m->m_next) {
317 1.26 thorpej error = _bus_dmamap_load_buffer_direct_common(t, map,
318 1.26 thorpej m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
319 1.9 thorpej first = 0;
320 1.9 thorpej }
321 1.10 thorpej if (error == 0) {
322 1.10 thorpej map->dm_mapsize = m0->m_pkthdr.len;
323 1.9 thorpej map->dm_nsegs = seg + 1;
324 1.19 thorpej } else if (t->_next_window != NULL) {
325 1.19 thorpej /*
326 1.19 thorpej * Give the next window a chance.
327 1.19 thorpej */
328 1.19 thorpej error = bus_dmamap_load_mbuf(t->_next_window, map, m0, flags);
329 1.10 thorpej }
330 1.9 thorpej return (error);
331 1.2 thorpej }
332 1.2 thorpej
333 1.2 thorpej /*
334 1.2 thorpej * Like _bus_dmamap_load_direct_common(), but for uios.
335 1.2 thorpej */
336 1.2 thorpej int
337 1.18 thorpej _bus_dmamap_load_uio_direct(t, map, uio, flags)
338 1.2 thorpej bus_dma_tag_t t;
339 1.2 thorpej bus_dmamap_t map;
340 1.2 thorpej struct uio *uio;
341 1.2 thorpej int flags;
342 1.2 thorpej {
343 1.25 thorpej paddr_t lastaddr;
344 1.24 thorpej int seg, i, error, first;
345 1.24 thorpej bus_size_t minlen, resid;
346 1.24 thorpej struct proc *p = NULL;
347 1.24 thorpej struct iovec *iov;
348 1.24 thorpej caddr_t addr;
349 1.24 thorpej
350 1.24 thorpej /*
351 1.24 thorpej * Make sure that on error condition we return "no valid mappings."
352 1.24 thorpej */
353 1.24 thorpej map->dm_mapsize = 0;
354 1.24 thorpej map->dm_nsegs = 0;
355 1.24 thorpej
356 1.24 thorpej resid = uio->uio_resid;
357 1.24 thorpej iov = uio->uio_iov;
358 1.24 thorpej
359 1.24 thorpej if (uio->uio_segflg == UIO_USERSPACE) {
360 1.24 thorpej p = uio->uio_procp;
361 1.24 thorpej #ifdef DIAGNOSTIC
362 1.24 thorpej if (p == NULL)
363 1.24 thorpej panic("_bus_dmamap_load_direct_common: USERSPACE but no proc");
364 1.24 thorpej #endif
365 1.24 thorpej }
366 1.24 thorpej
367 1.24 thorpej first = 1;
368 1.24 thorpej seg = 0;
369 1.24 thorpej error = 0;
370 1.24 thorpej for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
371 1.24 thorpej /*
372 1.24 thorpej * Now at the first iovec to load. Load each iovec
373 1.24 thorpej * until we have exhausted the residual count.
374 1.24 thorpej */
375 1.27 thorpej minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
376 1.27 thorpej addr = (caddr_t)iov[i].iov_base;
377 1.24 thorpej
378 1.26 thorpej error = _bus_dmamap_load_buffer_direct_common(t, map,
379 1.26 thorpej addr, minlen, p, flags, &lastaddr, &seg, first);
380 1.24 thorpej first = 0;
381 1.24 thorpej
382 1.24 thorpej resid -= minlen;
383 1.24 thorpej }
384 1.24 thorpej if (error == 0) {
385 1.24 thorpej map->dm_mapsize = uio->uio_resid;
386 1.24 thorpej map->dm_nsegs = seg + 1;
387 1.24 thorpej } else if (t->_next_window != NULL) {
388 1.24 thorpej /*
389 1.24 thorpej * Give the next window a chance.
390 1.24 thorpej */
391 1.24 thorpej error = bus_dmamap_load_uio(t->_next_window, map, uio, flags);
392 1.24 thorpej }
393 1.24 thorpej return (error);
394 1.2 thorpej }
395 1.2 thorpej
396 1.2 thorpej /*
397 1.2 thorpej * Like _bus_dmamap_load_direct_common(), but for raw memory.
398 1.2 thorpej */
399 1.2 thorpej int
400 1.18 thorpej _bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
401 1.2 thorpej bus_dma_tag_t t;
402 1.2 thorpej bus_dmamap_t map;
403 1.2 thorpej bus_dma_segment_t *segs;
404 1.2 thorpej int nsegs;
405 1.2 thorpej bus_size_t size;
406 1.2 thorpej int flags;
407 1.2 thorpej {
408 1.2 thorpej
409 1.18 thorpej panic("_bus_dmamap_load_raw_direct: not implemented");
410 1.2 thorpej }
411 1.2 thorpej
412 1.2 thorpej /*
413 1.2 thorpej * Common function for unloading a DMA map. May be called by
414 1.2 thorpej * chipset-specific DMA map unload functions.
415 1.2 thorpej */
416 1.2 thorpej void
417 1.2 thorpej _bus_dmamap_unload(t, map)
418 1.2 thorpej bus_dma_tag_t t;
419 1.2 thorpej bus_dmamap_t map;
420 1.2 thorpej {
421 1.2 thorpej
422 1.2 thorpej /*
423 1.2 thorpej * No resources to free; just mark the mappings as
424 1.2 thorpej * invalid.
425 1.2 thorpej */
426 1.10 thorpej map->dm_mapsize = 0;
427 1.2 thorpej map->dm_nsegs = 0;
428 1.2 thorpej }
429 1.2 thorpej
430 1.2 thorpej /*
431 1.2 thorpej * Common function for DMA map synchronization. May be called
432 1.2 thorpej * by chipset-specific DMA map synchronization functions.
433 1.2 thorpej */
434 1.2 thorpej void
435 1.12 thorpej _bus_dmamap_sync(t, map, offset, len, ops)
436 1.2 thorpej bus_dma_tag_t t;
437 1.2 thorpej bus_dmamap_t map;
438 1.12 thorpej bus_addr_t offset;
439 1.12 thorpej bus_size_t len;
440 1.11 thorpej int ops;
441 1.2 thorpej {
442 1.2 thorpej
443 1.13 thorpej /*
444 1.13 thorpej * Flush the store buffer.
445 1.13 thorpej */
446 1.13 thorpej alpha_mb();
447 1.2 thorpej }
448 1.2 thorpej
449 1.2 thorpej /*
450 1.2 thorpej * Common function for DMA-safe memory allocation. May be called
451 1.2 thorpej * by bus-specific DMA memory allocation functions.
452 1.2 thorpej */
453 1.2 thorpej int
454 1.2 thorpej _bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
455 1.2 thorpej bus_dma_tag_t t;
456 1.2 thorpej bus_size_t size, alignment, boundary;
457 1.2 thorpej bus_dma_segment_t *segs;
458 1.2 thorpej int nsegs;
459 1.2 thorpej int *rsegs;
460 1.2 thorpej int flags;
461 1.2 thorpej {
462 1.34 thorpej
463 1.34 thorpej return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
464 1.34 thorpej segs, nsegs, rsegs, flags, 0, trunc_page(avail_end)));
465 1.34 thorpej }
466 1.34 thorpej
467 1.34 thorpej /*
468 1.34 thorpej * Allocate physical memory from the given physical address range.
469 1.34 thorpej * Called by DMA-safe memory allocation methods.
470 1.34 thorpej */
471 1.34 thorpej int
472 1.34 thorpej _bus_dmamem_alloc_range(t, size, alignment, boundary, segs, nsegs, rsegs,
473 1.34 thorpej flags, low, high)
474 1.34 thorpej bus_dma_tag_t t;
475 1.34 thorpej bus_size_t size, alignment, boundary;
476 1.34 thorpej bus_dma_segment_t *segs;
477 1.34 thorpej int nsegs;
478 1.34 thorpej int *rsegs;
479 1.34 thorpej int flags;
480 1.34 thorpej paddr_t low;
481 1.34 thorpej paddr_t high;
482 1.34 thorpej {
483 1.34 thorpej paddr_t curaddr, lastaddr;
484 1.2 thorpej vm_page_t m;
485 1.2 thorpej struct pglist mlist;
486 1.2 thorpej int curseg, error;
487 1.2 thorpej
488 1.2 thorpej /* Always round the size. */
489 1.2 thorpej size = round_page(size);
490 1.2 thorpej
491 1.7 thorpej high = avail_end - PAGE_SIZE;
492 1.2 thorpej
493 1.2 thorpej /*
494 1.2 thorpej * Allocate pages from the VM system.
495 1.2 thorpej */
496 1.2 thorpej TAILQ_INIT(&mlist);
497 1.34 thorpej error = uvm_pglistalloc(size, low, high, alignment, boundary,
498 1.16 thorpej &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
499 1.2 thorpej if (error)
500 1.2 thorpej return (error);
501 1.2 thorpej
502 1.2 thorpej /*
503 1.2 thorpej * Compute the location, size, and number of segments actually
504 1.2 thorpej * returned by the VM code.
505 1.2 thorpej */
506 1.2 thorpej m = mlist.tqh_first;
507 1.2 thorpej curseg = 0;
508 1.2 thorpej lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
509 1.2 thorpej segs[curseg].ds_len = PAGE_SIZE;
510 1.2 thorpej m = m->pageq.tqe_next;
511 1.2 thorpej
512 1.2 thorpej for (; m != NULL; m = m->pageq.tqe_next) {
513 1.2 thorpej curaddr = VM_PAGE_TO_PHYS(m);
514 1.2 thorpej #ifdef DIAGNOSTIC
515 1.7 thorpej if (curaddr < avail_start || curaddr >= high) {
516 1.2 thorpej printf("vm_page_alloc_memory returned non-sensical"
517 1.2 thorpej " address 0x%lx\n", curaddr);
518 1.2 thorpej panic("_bus_dmamem_alloc");
519 1.2 thorpej }
520 1.2 thorpej #endif
521 1.2 thorpej if (curaddr == (lastaddr + PAGE_SIZE))
522 1.2 thorpej segs[curseg].ds_len += PAGE_SIZE;
523 1.2 thorpej else {
524 1.2 thorpej curseg++;
525 1.2 thorpej segs[curseg].ds_addr = curaddr;
526 1.2 thorpej segs[curseg].ds_len = PAGE_SIZE;
527 1.2 thorpej }
528 1.2 thorpej lastaddr = curaddr;
529 1.2 thorpej }
530 1.2 thorpej
531 1.2 thorpej *rsegs = curseg + 1;
532 1.2 thorpej
533 1.2 thorpej return (0);
534 1.2 thorpej }
535 1.2 thorpej
536 1.2 thorpej /*
537 1.2 thorpej * Common function for freeing DMA-safe memory. May be called by
538 1.2 thorpej * bus-specific DMA memory free functions.
539 1.2 thorpej */
540 1.2 thorpej void
541 1.2 thorpej _bus_dmamem_free(t, segs, nsegs)
542 1.2 thorpej bus_dma_tag_t t;
543 1.2 thorpej bus_dma_segment_t *segs;
544 1.2 thorpej int nsegs;
545 1.2 thorpej {
546 1.2 thorpej vm_page_t m;
547 1.2 thorpej bus_addr_t addr;
548 1.2 thorpej struct pglist mlist;
549 1.2 thorpej int curseg;
550 1.2 thorpej
551 1.2 thorpej /*
552 1.2 thorpej * Build a list of pages to free back to the VM system.
553 1.2 thorpej */
554 1.2 thorpej TAILQ_INIT(&mlist);
555 1.2 thorpej for (curseg = 0; curseg < nsegs; curseg++) {
556 1.2 thorpej for (addr = segs[curseg].ds_addr;
557 1.2 thorpej addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
558 1.2 thorpej addr += PAGE_SIZE) {
559 1.2 thorpej m = PHYS_TO_VM_PAGE(addr);
560 1.2 thorpej TAILQ_INSERT_TAIL(&mlist, m, pageq);
561 1.2 thorpej }
562 1.2 thorpej }
563 1.2 thorpej
564 1.16 thorpej uvm_pglistfree(&mlist);
565 1.2 thorpej }
566 1.2 thorpej
567 1.2 thorpej /*
568 1.2 thorpej * Common function for mapping DMA-safe memory. May be called by
569 1.2 thorpej * bus-specific DMA memory map functions.
570 1.2 thorpej */
571 1.2 thorpej int
572 1.2 thorpej _bus_dmamem_map(t, segs, nsegs, size, kvap, flags)
573 1.2 thorpej bus_dma_tag_t t;
574 1.2 thorpej bus_dma_segment_t *segs;
575 1.2 thorpej int nsegs;
576 1.2 thorpej size_t size;
577 1.2 thorpej caddr_t *kvap;
578 1.2 thorpej int flags;
579 1.2 thorpej {
580 1.25 thorpej vaddr_t va;
581 1.2 thorpej bus_addr_t addr;
582 1.16 thorpej int curseg;
583 1.2 thorpej
584 1.8 thorpej /*
585 1.8 thorpej * If we're only mapping 1 segment, use K0SEG, to avoid
586 1.8 thorpej * TLB thrashing.
587 1.8 thorpej */
588 1.8 thorpej if (nsegs == 1) {
589 1.8 thorpej *kvap = (caddr_t)ALPHA_PHYS_TO_K0SEG(segs[0].ds_addr);
590 1.8 thorpej return (0);
591 1.8 thorpej }
592 1.8 thorpej
593 1.2 thorpej size = round_page(size);
594 1.3 thorpej
595 1.16 thorpej va = uvm_km_valloc(kernel_map, size);
596 1.3 thorpej
597 1.2 thorpej if (va == 0)
598 1.2 thorpej return (ENOMEM);
599 1.2 thorpej
600 1.2 thorpej *kvap = (caddr_t)va;
601 1.2 thorpej
602 1.2 thorpej for (curseg = 0; curseg < nsegs; curseg++) {
603 1.2 thorpej for (addr = segs[curseg].ds_addr;
604 1.2 thorpej addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
605 1.2 thorpej addr += NBPG, va += NBPG, size -= NBPG) {
606 1.2 thorpej if (size == 0)
607 1.2 thorpej panic("_bus_dmamem_map: size botch");
608 1.2 thorpej pmap_enter(pmap_kernel(), va, addr,
609 1.33 thorpej VM_PROT_READ | VM_PROT_WRITE,
610 1.33 thorpej PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
611 1.2 thorpej }
612 1.2 thorpej }
613 1.2 thorpej
614 1.2 thorpej return (0);
615 1.2 thorpej }
616 1.2 thorpej
617 1.2 thorpej /*
618 1.2 thorpej * Common function for unmapping DMA-safe memory. May be called by
619 1.2 thorpej * bus-specific DMA memory unmapping functions.
620 1.2 thorpej */
621 1.2 thorpej void
622 1.2 thorpej _bus_dmamem_unmap(t, kva, size)
623 1.2 thorpej bus_dma_tag_t t;
624 1.2 thorpej caddr_t kva;
625 1.2 thorpej size_t size;
626 1.2 thorpej {
627 1.2 thorpej
628 1.2 thorpej #ifdef DIAGNOSTIC
629 1.2 thorpej if ((u_long)kva & PGOFSET)
630 1.2 thorpej panic("_bus_dmamem_unmap");
631 1.2 thorpej #endif
632 1.8 thorpej
633 1.8 thorpej /*
634 1.8 thorpej * Nothing to do if we mapped it with K0SEG.
635 1.8 thorpej */
636 1.8 thorpej if (kva >= (caddr_t)ALPHA_K0SEG_BASE &&
637 1.8 thorpej kva <= (caddr_t)ALPHA_K0SEG_END)
638 1.8 thorpej return;
639 1.2 thorpej
640 1.2 thorpej size = round_page(size);
641 1.25 thorpej uvm_km_free(kernel_map, (vaddr_t)kva, size);
642 1.2 thorpej }
643 1.2 thorpej
644 1.2 thorpej /*
645 1.2 thorpej * Common functin for mmap(2)'ing DMA-safe memory. May be called by
646 1.2 thorpej * bus-specific DMA mmap(2)'ing functions.
647 1.2 thorpej */
648 1.37 simonb paddr_t
649 1.2 thorpej _bus_dmamem_mmap(t, segs, nsegs, off, prot, flags)
650 1.2 thorpej bus_dma_tag_t t;
651 1.2 thorpej bus_dma_segment_t *segs;
652 1.37 simonb int nsegs;
653 1.37 simonb off_t off;
654 1.37 simonb int prot, flags;
655 1.2 thorpej {
656 1.6 thorpej int i;
657 1.2 thorpej
658 1.6 thorpej for (i = 0; i < nsegs; i++) {
659 1.6 thorpej #ifdef DIAGNOSTIC
660 1.6 thorpej if (off & PGOFSET)
661 1.6 thorpej panic("_bus_dmamem_mmap: offset unaligned");
662 1.6 thorpej if (segs[i].ds_addr & PGOFSET)
663 1.6 thorpej panic("_bus_dmamem_mmap: segment unaligned");
664 1.6 thorpej if (segs[i].ds_len & PGOFSET)
665 1.6 thorpej panic("_bus_dmamem_mmap: segment size not multiple"
666 1.6 thorpej " of page size");
667 1.6 thorpej #endif
668 1.6 thorpej if (off >= segs[i].ds_len) {
669 1.6 thorpej off -= segs[i].ds_len;
670 1.6 thorpej continue;
671 1.6 thorpej }
672 1.6 thorpej
673 1.6 thorpej return (alpha_btop((caddr_t)segs[i].ds_addr + off));
674 1.6 thorpej }
675 1.6 thorpej
676 1.6 thorpej /* Page not found. */
677 1.6 thorpej return (-1);
678 1.2 thorpej }
679