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