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