Home | History | Annotate | Line # | Download | only in common
bus_dma.c revision 1.2.6.1
      1  1.2.6.1  thorpej /* $NetBSD: bus_dma.c,v 1.2.6.1 1997/08/27 21:41:59 thorpej Exp $ */
      2      1.2  thorpej 
      3      1.2  thorpej /*-
      4      1.2  thorpej  * Copyright (c) 1997 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 <machine/options.h>		/* Config options headers */
     41      1.2  thorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     42      1.2  thorpej 
     43  1.2.6.1  thorpej __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.2.6.1 1997/08/27 21:41:59 thorpej Exp $");
     44      1.2  thorpej 
     45      1.2  thorpej #include <sys/param.h>
     46      1.2  thorpej #include <sys/systm.h>
     47      1.2  thorpej #include <sys/kernel.h>
     48      1.2  thorpej #include <sys/device.h>
     49      1.2  thorpej #include <sys/malloc.h>
     50      1.2  thorpej #include <sys/proc.h>
     51      1.2  thorpej 
     52      1.2  thorpej #include <vm/vm.h>
     53      1.2  thorpej #include <vm/vm_kern.h>
     54      1.2  thorpej 
     55      1.2  thorpej #define _ALPHA_BUS_DMA_PRIVATE
     56      1.2  thorpej #include <machine/bus.h>
     57  1.2.6.1  thorpej #include <machine/intr.h>
     58      1.2  thorpej 
     59      1.2  thorpej /*
     60      1.2  thorpej  * Common function for DMA map creation.  May be called by bus-specific
     61      1.2  thorpej  * DMA map creation functions.
     62      1.2  thorpej  */
     63      1.2  thorpej int
     64      1.2  thorpej _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
     65      1.2  thorpej 	bus_dma_tag_t t;
     66      1.2  thorpej 	bus_size_t size;
     67      1.2  thorpej 	int nsegments;
     68      1.2  thorpej 	bus_size_t maxsegsz;
     69      1.2  thorpej 	bus_size_t boundary;
     70      1.2  thorpej 	int flags;
     71      1.2  thorpej 	bus_dmamap_t *dmamp;
     72      1.2  thorpej {
     73      1.2  thorpej 	struct alpha_bus_dmamap *map;
     74      1.2  thorpej 	void *mapstore;
     75      1.2  thorpej 	size_t mapsize;
     76      1.2  thorpej 
     77      1.2  thorpej 	/*
     78      1.2  thorpej 	 * Allcoate and initialize the DMA map.  The end of the map
     79      1.2  thorpej 	 * is a variable-sized array of segments, so we allocate enough
     80      1.2  thorpej 	 * room for them in one shot.
     81      1.2  thorpej 	 *
     82      1.2  thorpej 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
     83      1.2  thorpej 	 * of ALLOCNOW notifes others that we've reserved these resources,
     84      1.2  thorpej 	 * and they are not to be freed.
     85      1.2  thorpej 	 *
     86      1.2  thorpej 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
     87      1.2  thorpej 	 * the (nsegments - 1).
     88      1.2  thorpej 	 */
     89      1.2  thorpej 	mapsize = sizeof(struct alpha_bus_dmamap) +
     90      1.2  thorpej 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
     91      1.2  thorpej 	if ((mapstore = malloc(mapsize, M_DEVBUF,
     92      1.2  thorpej 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
     93      1.2  thorpej 		return (ENOMEM);
     94      1.2  thorpej 
     95      1.2  thorpej 	bzero(mapstore, mapsize);
     96      1.2  thorpej 	map = (struct alpha_bus_dmamap *)mapstore;
     97      1.2  thorpej 	map->_dm_size = size;
     98      1.2  thorpej 	map->_dm_segcnt = nsegments;
     99      1.2  thorpej 	map->_dm_maxsegsz = maxsegsz;
    100      1.2  thorpej 	map->_dm_boundary = boundary;
    101      1.2  thorpej 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    102      1.2  thorpej 	map->dm_nsegs = 0;		/* no valid mappings */
    103      1.2  thorpej 
    104      1.2  thorpej 	*dmamp = map;
    105      1.2  thorpej 	return (0);
    106      1.2  thorpej }
    107      1.2  thorpej 
    108      1.2  thorpej /*
    109      1.2  thorpej  * Common function for DMA map destruction.  May be called by bus-specific
    110      1.2  thorpej  * DMA map destruction functions.
    111      1.2  thorpej  */
    112      1.2  thorpej void
    113      1.2  thorpej _bus_dmamap_destroy(t, map)
    114      1.2  thorpej 	bus_dma_tag_t t;
    115      1.2  thorpej 	bus_dmamap_t map;
    116      1.2  thorpej {
    117      1.2  thorpej 
    118      1.2  thorpej 	free(map, M_DEVBUF);
    119      1.2  thorpej }
    120      1.2  thorpej 
    121      1.2  thorpej /*
    122      1.2  thorpej  * Common function for loading a direct-mapped DMA map with a linear
    123      1.2  thorpej  * buffer.  Called by bus-specific DMA map load functions with the
    124      1.2  thorpej  * OR value appropriate for indicating "direct-mapped" for that
    125      1.2  thorpej  * chipset.
    126      1.2  thorpej  */
    127      1.2  thorpej int
    128      1.2  thorpej _bus_dmamap_load_direct_common(t, map, buf, buflen, p, flags, wbase)
    129      1.2  thorpej 	bus_dma_tag_t t;
    130      1.2  thorpej 	bus_dmamap_t map;
    131      1.2  thorpej 	void *buf;
    132      1.2  thorpej 	bus_size_t buflen;
    133      1.2  thorpej 	struct proc *p;
    134      1.2  thorpej 	int flags;
    135      1.2  thorpej 	bus_addr_t wbase;
    136      1.2  thorpej {
    137      1.2  thorpej 	bus_size_t sgsize;
    138      1.2  thorpej 	vm_offset_t curaddr, lastaddr;
    139      1.2  thorpej 	vm_offset_t vaddr = (vm_offset_t)buf;
    140      1.2  thorpej 	int first, seg;
    141      1.2  thorpej 
    142      1.2  thorpej 	/*
    143      1.2  thorpej 	 * Make sure that on error condition we return "no valid mappings".
    144      1.2  thorpej 	 */
    145      1.2  thorpej 	map->dm_nsegs = 0;
    146      1.2  thorpej 
    147      1.2  thorpej 	if (buflen > map->_dm_size)
    148      1.2  thorpej 		return (EINVAL);
    149      1.2  thorpej 
    150      1.2  thorpej 	/*
    151      1.2  thorpej 	 * XXX Need to implement "don't dma across this boundry".
    152      1.2  thorpej 	 */
    153      1.2  thorpej 
    154      1.2  thorpej 	lastaddr = ~0;		/* XXX gcc */
    155      1.2  thorpej 	for (first = 1, seg = 0; buflen > 0 && seg < map->_dm_segcnt; ) {
    156      1.2  thorpej 		/*
    157      1.2  thorpej 		 * Get the physical address for this segment.
    158      1.2  thorpej 		 */
    159      1.2  thorpej 		if (p != NULL)
    160      1.2  thorpej 			curaddr = pmap_extract(&p->p_vmspace->vm_pmap, vaddr);
    161      1.2  thorpej 		else
    162      1.2  thorpej 			curaddr = vtophys(vaddr);
    163      1.2  thorpej 
    164      1.2  thorpej 		curaddr |= wbase;
    165      1.2  thorpej 
    166      1.2  thorpej 		/*
    167      1.2  thorpej 		 * Compute the segment size, and adjust counts.
    168      1.2  thorpej 		 */
    169      1.2  thorpej 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    170      1.2  thorpej 		if (buflen < sgsize)
    171      1.2  thorpej 			sgsize = buflen;
    172      1.2  thorpej 
    173      1.2  thorpej 		/*
    174      1.2  thorpej 		 * Insert chunk into a segment, coalescing with
    175      1.2  thorpej 		 * the previous segment if possible.
    176      1.2  thorpej 		 */
    177      1.2  thorpej 		if (first) {
    178      1.2  thorpej 			map->dm_segs[seg].ds_addr = curaddr;
    179      1.2  thorpej 			map->dm_segs[seg].ds_len = sgsize;
    180      1.2  thorpej 			first = 0;
    181      1.2  thorpej 		} else {
    182      1.2  thorpej 			if (curaddr == lastaddr &&
    183      1.2  thorpej 			    (map->dm_segs[seg].ds_len + sgsize) <=
    184      1.2  thorpej 			    map->_dm_maxsegsz)
    185      1.2  thorpej 				map->dm_segs[seg].ds_len += sgsize;
    186      1.2  thorpej 			else {
    187      1.2  thorpej 				seg++;
    188      1.2  thorpej 				map->dm_segs[seg].ds_addr = curaddr;
    189      1.2  thorpej 				map->dm_segs[seg].ds_len = sgsize;
    190      1.2  thorpej 			}
    191      1.2  thorpej 		}
    192      1.2  thorpej 
    193      1.2  thorpej 		lastaddr = curaddr + sgsize;
    194      1.2  thorpej 		vaddr += sgsize;
    195      1.2  thorpej 		buflen -= sgsize;
    196      1.2  thorpej 	}
    197      1.2  thorpej 
    198      1.2  thorpej 	/*
    199      1.2  thorpej 	 * Did we fit?
    200      1.2  thorpej 	 */
    201      1.2  thorpej 	if (buflen != 0) {
    202      1.2  thorpej 		/*
    203      1.2  thorpej 		 * XXX Should fall back on SGMAPs.
    204      1.2  thorpej 		 */
    205      1.2  thorpej 		return (EFBIG);		/* XXX better return value here? */
    206      1.2  thorpej 	}
    207      1.2  thorpej 
    208      1.2  thorpej 	map->dm_nsegs = seg + 1;
    209      1.2  thorpej 	return (0);
    210      1.2  thorpej }
    211      1.2  thorpej 
    212      1.2  thorpej /*
    213      1.2  thorpej  * Like _bus_dmamap_load_direct_common(), but for mbufs.
    214      1.2  thorpej  */
    215      1.2  thorpej int
    216      1.2  thorpej _bus_dmamap_load_mbuf_direct_common(t, map, m, flags, wbase)
    217      1.2  thorpej 	bus_dma_tag_t t;
    218      1.2  thorpej 	bus_dmamap_t map;
    219      1.2  thorpej 	struct mbuf *m;
    220      1.2  thorpej 	int flags;
    221      1.2  thorpej 	bus_addr_t wbase;
    222      1.2  thorpej {
    223      1.2  thorpej 
    224      1.2  thorpej 	panic("_bus_dmamap_load_mbuf_direct_common: not implemented");
    225      1.2  thorpej }
    226      1.2  thorpej 
    227      1.2  thorpej /*
    228      1.2  thorpej  * Like _bus_dmamap_load_direct_common(), but for uios.
    229      1.2  thorpej  */
    230      1.2  thorpej int
    231      1.2  thorpej _bus_dmamap_load_uio_direct_common(t, map, uio, flags, wbase)
    232      1.2  thorpej 	bus_dma_tag_t t;
    233      1.2  thorpej 	bus_dmamap_t map;
    234      1.2  thorpej 	struct uio *uio;
    235      1.2  thorpej 	int flags;
    236      1.2  thorpej 	bus_addr_t wbase;
    237      1.2  thorpej {
    238      1.2  thorpej 
    239      1.2  thorpej 	panic("_bus_dmamap_load_uio_direct_common: not implemented");
    240      1.2  thorpej }
    241      1.2  thorpej 
    242      1.2  thorpej /*
    243      1.2  thorpej  * Like _bus_dmamap_load_direct_common(), but for raw memory.
    244      1.2  thorpej  */
    245      1.2  thorpej int
    246      1.2  thorpej _bus_dmamap_load_raw_direct_common(t, map, segs, nsegs, size, flags, wbase)
    247      1.2  thorpej 	bus_dma_tag_t t;
    248      1.2  thorpej 	bus_dmamap_t map;
    249      1.2  thorpej 	bus_dma_segment_t *segs;
    250      1.2  thorpej 	int nsegs;
    251      1.2  thorpej 	bus_size_t size;
    252      1.2  thorpej 	int flags;
    253      1.2  thorpej 	bus_addr_t wbase;
    254      1.2  thorpej {
    255      1.2  thorpej 
    256      1.2  thorpej 	panic("_bus_dmamap_load_raw_direct_common: not implemented");
    257      1.2  thorpej }
    258      1.2  thorpej 
    259      1.2  thorpej /*
    260      1.2  thorpej  * Common function for unloading a DMA map.  May be called by
    261      1.2  thorpej  * chipset-specific DMA map unload functions.
    262      1.2  thorpej  */
    263      1.2  thorpej void
    264      1.2  thorpej _bus_dmamap_unload(t, map)
    265      1.2  thorpej 	bus_dma_tag_t t;
    266      1.2  thorpej 	bus_dmamap_t map;
    267      1.2  thorpej {
    268      1.2  thorpej 
    269      1.2  thorpej 	/*
    270      1.2  thorpej 	 * No resources to free; just mark the mappings as
    271      1.2  thorpej 	 * invalid.
    272      1.2  thorpej 	 */
    273      1.2  thorpej 	map->dm_nsegs = 0;
    274      1.2  thorpej }
    275      1.2  thorpej 
    276      1.2  thorpej /*
    277      1.2  thorpej  * Common function for DMA map synchronization.  May be called
    278      1.2  thorpej  * by chipset-specific DMA map synchronization functions.
    279      1.2  thorpej  */
    280      1.2  thorpej void
    281      1.2  thorpej _bus_dmamap_sync(t, map, op)
    282      1.2  thorpej 	bus_dma_tag_t t;
    283      1.2  thorpej 	bus_dmamap_t map;
    284      1.2  thorpej 	bus_dmasync_op_t op;
    285      1.2  thorpej {
    286      1.2  thorpej 
    287      1.2  thorpej 	/* Nothing to do. */
    288      1.2  thorpej }
    289      1.2  thorpej 
    290      1.2  thorpej /*
    291      1.2  thorpej  * Common function for DMA-safe memory allocation.  May be called
    292      1.2  thorpej  * by bus-specific DMA memory allocation functions.
    293      1.2  thorpej  */
    294      1.2  thorpej int
    295      1.2  thorpej _bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
    296      1.2  thorpej 	bus_dma_tag_t t;
    297      1.2  thorpej 	bus_size_t size, alignment, boundary;
    298      1.2  thorpej 	bus_dma_segment_t *segs;
    299      1.2  thorpej 	int nsegs;
    300      1.2  thorpej 	int *rsegs;
    301      1.2  thorpej 	int flags;
    302      1.2  thorpej {
    303      1.2  thorpej 	extern vm_offset_t vm_first_phys, vm_last_phys;
    304      1.2  thorpej 	vm_offset_t curaddr, lastaddr, high;
    305      1.2  thorpej 	vm_page_t m;
    306      1.2  thorpej 	struct pglist mlist;
    307      1.2  thorpej 	int curseg, error;
    308      1.2  thorpej 
    309      1.2  thorpej 	/* Always round the size. */
    310      1.2  thorpej 	size = round_page(size);
    311      1.2  thorpej 
    312      1.2  thorpej 	high = vm_last_phys - PAGE_SIZE;
    313      1.2  thorpej 
    314      1.2  thorpej 	/*
    315      1.2  thorpej 	 * Allocate pages from the VM system.
    316      1.2  thorpej 	 */
    317      1.2  thorpej 	TAILQ_INIT(&mlist);
    318      1.2  thorpej 	error = vm_page_alloc_memory(size, vm_first_phys, high,
    319      1.2  thorpej 	    alignment, boundary, &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    320      1.2  thorpej 	if (error)
    321      1.2  thorpej 		return (error);
    322      1.2  thorpej 
    323      1.2  thorpej 	/*
    324      1.2  thorpej 	 * Compute the location, size, and number of segments actually
    325      1.2  thorpej 	 * returned by the VM code.
    326      1.2  thorpej 	 */
    327      1.2  thorpej 	m = mlist.tqh_first;
    328      1.2  thorpej 	curseg = 0;
    329      1.2  thorpej 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
    330      1.2  thorpej 	segs[curseg].ds_len = PAGE_SIZE;
    331      1.2  thorpej 	m = m->pageq.tqe_next;
    332      1.2  thorpej 
    333      1.2  thorpej 	for (; m != NULL; m = m->pageq.tqe_next) {
    334      1.2  thorpej 		curaddr = VM_PAGE_TO_PHYS(m);
    335      1.2  thorpej #ifdef DIAGNOSTIC
    336      1.2  thorpej 		if (curaddr < vm_first_phys || curaddr >= high) {
    337      1.2  thorpej 			printf("vm_page_alloc_memory returned non-sensical"
    338      1.2  thorpej 			    " address 0x%lx\n", curaddr);
    339      1.2  thorpej 			panic("_bus_dmamem_alloc");
    340      1.2  thorpej 		}
    341      1.2  thorpej #endif
    342      1.2  thorpej 		if (curaddr == (lastaddr + PAGE_SIZE))
    343      1.2  thorpej 			segs[curseg].ds_len += PAGE_SIZE;
    344      1.2  thorpej 		else {
    345      1.2  thorpej 			curseg++;
    346      1.2  thorpej 			segs[curseg].ds_addr = curaddr;
    347      1.2  thorpej 			segs[curseg].ds_len = PAGE_SIZE;
    348      1.2  thorpej 		}
    349      1.2  thorpej 		lastaddr = curaddr;
    350      1.2  thorpej 	}
    351      1.2  thorpej 
    352      1.2  thorpej 	*rsegs = curseg + 1;
    353      1.2  thorpej 
    354      1.2  thorpej 	return (0);
    355      1.2  thorpej }
    356      1.2  thorpej 
    357      1.2  thorpej /*
    358      1.2  thorpej  * Common function for freeing DMA-safe memory.  May be called by
    359      1.2  thorpej  * bus-specific DMA memory free functions.
    360      1.2  thorpej  */
    361      1.2  thorpej void
    362      1.2  thorpej _bus_dmamem_free(t, segs, nsegs)
    363      1.2  thorpej 	bus_dma_tag_t t;
    364      1.2  thorpej 	bus_dma_segment_t *segs;
    365      1.2  thorpej 	int nsegs;
    366      1.2  thorpej {
    367      1.2  thorpej 	vm_page_t m;
    368      1.2  thorpej 	bus_addr_t addr;
    369      1.2  thorpej 	struct pglist mlist;
    370      1.2  thorpej 	int curseg;
    371      1.2  thorpej 
    372      1.2  thorpej 	/*
    373      1.2  thorpej 	 * Build a list of pages to free back to the VM system.
    374      1.2  thorpej 	 */
    375      1.2  thorpej 	TAILQ_INIT(&mlist);
    376      1.2  thorpej 	for (curseg = 0; curseg < nsegs; curseg++) {
    377      1.2  thorpej 		for (addr = segs[curseg].ds_addr;
    378      1.2  thorpej 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    379      1.2  thorpej 		    addr += PAGE_SIZE) {
    380      1.2  thorpej 			m = PHYS_TO_VM_PAGE(addr);
    381      1.2  thorpej 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
    382      1.2  thorpej 		}
    383      1.2  thorpej 	}
    384      1.2  thorpej 
    385      1.2  thorpej 	vm_page_free_memory(&mlist);
    386      1.2  thorpej }
    387      1.2  thorpej 
    388      1.2  thorpej /*
    389      1.2  thorpej  * Common function for mapping DMA-safe memory.  May be called by
    390      1.2  thorpej  * bus-specific DMA memory map functions.
    391      1.2  thorpej  */
    392      1.2  thorpej int
    393      1.2  thorpej _bus_dmamem_map(t, segs, nsegs, size, kvap, flags)
    394      1.2  thorpej 	bus_dma_tag_t t;
    395      1.2  thorpej 	bus_dma_segment_t *segs;
    396      1.2  thorpej 	int nsegs;
    397      1.2  thorpej 	size_t size;
    398      1.2  thorpej 	caddr_t *kvap;
    399      1.2  thorpej 	int flags;
    400      1.2  thorpej {
    401      1.2  thorpej 	vm_offset_t va;
    402      1.2  thorpej 	bus_addr_t addr;
    403  1.2.6.1  thorpej 	int curseg, s;
    404      1.2  thorpej 
    405      1.2  thorpej 	size = round_page(size);
    406  1.2.6.1  thorpej 
    407  1.2.6.1  thorpej 	s = splimp();
    408      1.2  thorpej 	va = kmem_alloc_pageable(kmem_map, size);
    409  1.2.6.1  thorpej 	splx(s);
    410  1.2.6.1  thorpej 
    411      1.2  thorpej 	if (va == 0)
    412      1.2  thorpej 		return (ENOMEM);
    413      1.2  thorpej 
    414      1.2  thorpej 	*kvap = (caddr_t)va;
    415      1.2  thorpej 
    416      1.2  thorpej 	for (curseg = 0; curseg < nsegs; curseg++) {
    417      1.2  thorpej 		for (addr = segs[curseg].ds_addr;
    418      1.2  thorpej 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    419      1.2  thorpej 		    addr += NBPG, va += NBPG, size -= NBPG) {
    420      1.2  thorpej 			if (size == 0)
    421      1.2  thorpej 				panic("_bus_dmamem_map: size botch");
    422      1.2  thorpej 			pmap_enter(pmap_kernel(), va, addr,
    423      1.2  thorpej 			    VM_PROT_READ | VM_PROT_WRITE, TRUE);
    424      1.2  thorpej #if 0
    425      1.2  thorpej 			if (flags & BUS_DMAMEM_NOSYNC)
    426      1.2  thorpej 				/* XXX make non-cacheable? */ ;
    427      1.2  thorpej #endif
    428      1.2  thorpej 		}
    429      1.2  thorpej 	}
    430      1.2  thorpej 
    431      1.2  thorpej 	return (0);
    432      1.2  thorpej }
    433      1.2  thorpej 
    434      1.2  thorpej /*
    435      1.2  thorpej  * Common function for unmapping DMA-safe memory.  May be called by
    436      1.2  thorpej  * bus-specific DMA memory unmapping functions.
    437      1.2  thorpej  */
    438      1.2  thorpej void
    439      1.2  thorpej _bus_dmamem_unmap(t, kva, size)
    440      1.2  thorpej 	bus_dma_tag_t t;
    441      1.2  thorpej 	caddr_t kva;
    442      1.2  thorpej 	size_t size;
    443      1.2  thorpej {
    444  1.2.6.1  thorpej 	int s;
    445      1.2  thorpej 
    446      1.2  thorpej #ifdef DIAGNOSTIC
    447      1.2  thorpej 	if ((u_long)kva & PGOFSET)
    448      1.2  thorpej 		panic("_bus_dmamem_unmap");
    449      1.2  thorpej #endif
    450      1.2  thorpej 
    451      1.2  thorpej 	size = round_page(size);
    452  1.2.6.1  thorpej 	s = splimp();
    453      1.2  thorpej 	kmem_free(kmem_map, (vm_offset_t)kva, size);
    454  1.2.6.1  thorpej 	splx(s);
    455      1.2  thorpej }
    456      1.2  thorpej 
    457      1.2  thorpej /*
    458      1.2  thorpej  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    459      1.2  thorpej  * bus-specific DMA mmap(2)'ing functions.
    460      1.2  thorpej  */
    461      1.2  thorpej int
    462      1.2  thorpej _bus_dmamem_mmap(t, segs, nsegs, off, prot, flags)
    463      1.2  thorpej 	bus_dma_tag_t t;
    464      1.2  thorpej 	bus_dma_segment_t *segs;
    465      1.2  thorpej 	int nsegs, off, prot, flags;
    466      1.2  thorpej {
    467      1.2  thorpej 
    468      1.2  thorpej 	panic("_bus_dmamem_mmap: not implemented");
    469      1.2  thorpej }
    470