Home | History | Annotate | Line # | Download | only in common
bus_dma.c revision 1.17
      1  1.17  thorpej /* $NetBSD: bus_dma.c,v 1.17 1998/03/17 04:59:36 thorpej 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.16  thorpej #include "opt_uvm.h"
     41  1.17  thorpej #include "opt_pmap_new.h"
     42  1.16  thorpej 
     43   1.2  thorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     44   1.2  thorpej 
     45  1.17  thorpej __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.17 1998/03/17 04:59:36 thorpej Exp $");
     46   1.2  thorpej 
     47   1.2  thorpej #include <sys/param.h>
     48   1.2  thorpej #include <sys/systm.h>
     49   1.2  thorpej #include <sys/kernel.h>
     50   1.2  thorpej #include <sys/device.h>
     51   1.2  thorpej #include <sys/malloc.h>
     52   1.2  thorpej #include <sys/proc.h>
     53   1.9  thorpej #include <sys/mbuf.h>
     54   1.2  thorpej 
     55   1.2  thorpej #include <vm/vm.h>
     56   1.2  thorpej #include <vm/vm_kern.h>
     57  1.16  thorpej #if defined(UVM)
     58  1.16  thorpej #include <uvm/uvm_extern.h>
     59  1.16  thorpej #endif
     60   1.2  thorpej 
     61   1.2  thorpej #define _ALPHA_BUS_DMA_PRIVATE
     62   1.2  thorpej #include <machine/bus.h>
     63   1.3  thorpej #include <machine/intr.h>
     64   1.2  thorpej 
     65   1.9  thorpej int	_bus_dmamap_load_buffer_direct_common __P((bus_dmamap_t,
     66   1.9  thorpej 	    void *, bus_size_t, struct proc *, int, bus_addr_t,
     67   1.9  thorpej 	    vm_offset_t *, int *, int));
     68   1.9  thorpej 
     69   1.2  thorpej /*
     70   1.2  thorpej  * Common function for DMA map creation.  May be called by bus-specific
     71   1.2  thorpej  * DMA map creation functions.
     72   1.2  thorpej  */
     73   1.2  thorpej int
     74   1.2  thorpej _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
     75   1.2  thorpej 	bus_dma_tag_t t;
     76   1.2  thorpej 	bus_size_t size;
     77   1.2  thorpej 	int nsegments;
     78   1.2  thorpej 	bus_size_t maxsegsz;
     79   1.2  thorpej 	bus_size_t boundary;
     80   1.2  thorpej 	int flags;
     81   1.2  thorpej 	bus_dmamap_t *dmamp;
     82   1.2  thorpej {
     83   1.2  thorpej 	struct alpha_bus_dmamap *map;
     84   1.2  thorpej 	void *mapstore;
     85   1.2  thorpej 	size_t mapsize;
     86   1.2  thorpej 
     87   1.2  thorpej 	/*
     88   1.2  thorpej 	 * Allcoate and initialize the DMA map.  The end of the map
     89   1.2  thorpej 	 * is a variable-sized array of segments, so we allocate enough
     90   1.2  thorpej 	 * room for them in one shot.
     91   1.2  thorpej 	 *
     92   1.2  thorpej 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
     93   1.2  thorpej 	 * of ALLOCNOW notifes others that we've reserved these resources,
     94   1.2  thorpej 	 * and they are not to be freed.
     95   1.2  thorpej 	 *
     96   1.2  thorpej 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
     97   1.2  thorpej 	 * the (nsegments - 1).
     98   1.2  thorpej 	 */
     99   1.2  thorpej 	mapsize = sizeof(struct alpha_bus_dmamap) +
    100   1.2  thorpej 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
    101  1.14  thorpej 	if ((mapstore = malloc(mapsize, M_DMAMAP,
    102   1.2  thorpej 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
    103   1.2  thorpej 		return (ENOMEM);
    104   1.2  thorpej 
    105   1.2  thorpej 	bzero(mapstore, mapsize);
    106   1.2  thorpej 	map = (struct alpha_bus_dmamap *)mapstore;
    107   1.2  thorpej 	map->_dm_size = size;
    108   1.2  thorpej 	map->_dm_segcnt = nsegments;
    109   1.2  thorpej 	map->_dm_maxsegsz = maxsegsz;
    110   1.2  thorpej 	map->_dm_boundary = boundary;
    111   1.2  thorpej 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    112  1.10  thorpej 	map->dm_mapsize = 0;		/* no valid mappings */
    113  1.10  thorpej 	map->dm_nsegs = 0;
    114   1.2  thorpej 
    115   1.2  thorpej 	*dmamp = map;
    116   1.2  thorpej 	return (0);
    117   1.2  thorpej }
    118   1.2  thorpej 
    119   1.2  thorpej /*
    120   1.2  thorpej  * Common function for DMA map destruction.  May be called by bus-specific
    121   1.2  thorpej  * DMA map destruction functions.
    122   1.2  thorpej  */
    123   1.2  thorpej void
    124   1.2  thorpej _bus_dmamap_destroy(t, map)
    125   1.2  thorpej 	bus_dma_tag_t t;
    126   1.2  thorpej 	bus_dmamap_t map;
    127   1.2  thorpej {
    128   1.2  thorpej 
    129  1.14  thorpej 	free(map, M_DMAMAP);
    130   1.2  thorpej }
    131   1.2  thorpej 
    132   1.2  thorpej /*
    133   1.9  thorpej  * Utility function to load a linear buffer.  lastaddrp holds state
    134   1.9  thorpej  * between invocations (for multiple-buffer loads).  segp contains
    135   1.9  thorpej  * the starting segment on entrance, and the ending segment on exit.
    136   1.9  thorpej  * first indicates if this is the first invocation of this function.
    137   1.2  thorpej  */
    138   1.2  thorpej int
    139   1.9  thorpej _bus_dmamap_load_buffer_direct_common(map, buf, buflen, p, flags, wbase,
    140   1.9  thorpej     lastaddrp, segp, first)
    141   1.2  thorpej 	bus_dmamap_t map;
    142   1.2  thorpej 	void *buf;
    143   1.2  thorpej 	bus_size_t buflen;
    144   1.2  thorpej 	struct proc *p;
    145   1.2  thorpej 	int flags;
    146   1.2  thorpej 	bus_addr_t wbase;
    147   1.9  thorpej 	vm_offset_t *lastaddrp;
    148   1.9  thorpej 	int *segp;
    149   1.9  thorpej 	int first;
    150   1.2  thorpej {
    151   1.2  thorpej 	bus_size_t sgsize;
    152   1.2  thorpej 	vm_offset_t curaddr, lastaddr;
    153   1.2  thorpej 	vm_offset_t vaddr = (vm_offset_t)buf;
    154   1.9  thorpej 	int seg;
    155   1.2  thorpej 
    156   1.9  thorpej 	lastaddr = *lastaddrp;
    157   1.2  thorpej 
    158   1.9  thorpej 	for (seg = *segp; buflen > 0 && seg < map->_dm_segcnt; ) {
    159   1.2  thorpej 		/*
    160   1.2  thorpej 		 * Get the physical address for this segment.
    161   1.2  thorpej 		 */
    162   1.2  thorpej 		if (p != NULL)
    163   1.5  thorpej 			curaddr = pmap_extract(p->p_vmspace->vm_map.pmap,
    164   1.5  thorpej 			    vaddr);
    165   1.2  thorpej 		else
    166   1.2  thorpej 			curaddr = vtophys(vaddr);
    167   1.2  thorpej 
    168   1.2  thorpej 		curaddr |= wbase;
    169   1.2  thorpej 
    170   1.2  thorpej 		/*
    171   1.2  thorpej 		 * Compute the segment size, and adjust counts.
    172   1.2  thorpej 		 */
    173   1.2  thorpej 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    174   1.2  thorpej 		if (buflen < sgsize)
    175   1.2  thorpej 			sgsize = buflen;
    176   1.2  thorpej 
    177   1.2  thorpej 		/*
    178   1.2  thorpej 		 * Insert chunk into a segment, coalescing with
    179   1.2  thorpej 		 * the previous segment if possible.
    180   1.2  thorpej 		 */
    181   1.2  thorpej 		if (first) {
    182   1.2  thorpej 			map->dm_segs[seg].ds_addr = curaddr;
    183   1.2  thorpej 			map->dm_segs[seg].ds_len = sgsize;
    184   1.2  thorpej 			first = 0;
    185   1.2  thorpej 		} else {
    186   1.2  thorpej 			if (curaddr == lastaddr &&
    187   1.2  thorpej 			    (map->dm_segs[seg].ds_len + sgsize) <=
    188   1.2  thorpej 			    map->_dm_maxsegsz)
    189   1.2  thorpej 				map->dm_segs[seg].ds_len += sgsize;
    190   1.2  thorpej 			else {
    191   1.2  thorpej 				seg++;
    192   1.2  thorpej 				map->dm_segs[seg].ds_addr = curaddr;
    193   1.2  thorpej 				map->dm_segs[seg].ds_len = sgsize;
    194   1.2  thorpej 			}
    195   1.2  thorpej 		}
    196   1.2  thorpej 
    197   1.2  thorpej 		lastaddr = curaddr + sgsize;
    198   1.2  thorpej 		vaddr += sgsize;
    199   1.2  thorpej 		buflen -= sgsize;
    200   1.2  thorpej 	}
    201   1.2  thorpej 
    202   1.9  thorpej 	*segp = seg;
    203   1.9  thorpej 	*lastaddrp = lastaddr;
    204   1.9  thorpej 
    205   1.2  thorpej 	/*
    206   1.2  thorpej 	 * Did we fit?
    207   1.2  thorpej 	 */
    208   1.2  thorpej 	if (buflen != 0) {
    209   1.2  thorpej 		/*
    210   1.2  thorpej 		 * XXX Should fall back on SGMAPs.
    211   1.2  thorpej 		 */
    212   1.2  thorpej 		return (EFBIG);		/* XXX better return value here? */
    213   1.2  thorpej 	}
    214   1.9  thorpej 	return (0);
    215   1.9  thorpej }
    216   1.9  thorpej 
    217   1.9  thorpej /*
    218   1.9  thorpej  * Common function for loading a direct-mapped DMA map with a linear
    219   1.9  thorpej  * buffer.  Called by bus-specific DMA map load functions with the
    220   1.9  thorpej  * OR value appropriate for indicating "direct-mapped" for that
    221   1.9  thorpej  * chipset.
    222   1.9  thorpej  */
    223   1.9  thorpej int
    224   1.9  thorpej _bus_dmamap_load_direct_common(t, map, buf, buflen, p, flags, wbase)
    225   1.9  thorpej 	bus_dma_tag_t t;
    226   1.9  thorpej 	bus_dmamap_t map;
    227   1.9  thorpej 	void *buf;
    228   1.9  thorpej 	bus_size_t buflen;
    229   1.9  thorpej 	struct proc *p;
    230   1.9  thorpej 	int flags;
    231   1.9  thorpej 	bus_addr_t wbase;
    232   1.9  thorpej {
    233   1.9  thorpej 	vm_offset_t lastaddr;
    234   1.9  thorpej 	int seg, error;
    235   1.9  thorpej 
    236   1.9  thorpej 	/*
    237   1.9  thorpej 	 * Make sure that on error condition we return "no valid mappings".
    238   1.9  thorpej 	 */
    239  1.10  thorpej 	map->dm_mapsize = 0;
    240   1.9  thorpej 	map->dm_nsegs = 0;
    241   1.2  thorpej 
    242   1.9  thorpej 	if (buflen > map->_dm_size)
    243   1.9  thorpej 		return (EINVAL);
    244   1.9  thorpej 
    245   1.9  thorpej 	seg = 0;
    246   1.9  thorpej 	error = _bus_dmamap_load_buffer_direct_common(map, buf, buflen,
    247   1.9  thorpej 	    p, flags, wbase, &lastaddr, &seg, 1);
    248  1.10  thorpej 	if (error == 0) {
    249  1.10  thorpej 		map->dm_mapsize = buflen;
    250   1.9  thorpej 		map->dm_nsegs = seg + 1;
    251  1.10  thorpej 	}
    252   1.9  thorpej 	return (error);
    253   1.2  thorpej }
    254   1.2  thorpej 
    255   1.2  thorpej /*
    256   1.2  thorpej  * Like _bus_dmamap_load_direct_common(), but for mbufs.
    257   1.2  thorpej  */
    258   1.2  thorpej int
    259   1.9  thorpej _bus_dmamap_load_mbuf_direct_common(t, map, m0, flags, wbase)
    260   1.2  thorpej 	bus_dma_tag_t t;
    261   1.2  thorpej 	bus_dmamap_t map;
    262   1.9  thorpej 	struct mbuf *m0;
    263   1.2  thorpej 	int flags;
    264   1.2  thorpej 	bus_addr_t wbase;
    265   1.2  thorpej {
    266   1.9  thorpej 	vm_offset_t lastaddr;
    267   1.9  thorpej 	int seg, error, first;
    268   1.9  thorpej 	struct mbuf *m;
    269   1.9  thorpej 
    270   1.9  thorpej 	/*
    271   1.9  thorpej 	 * Make sure that on error condition we return "no valid mappings."
    272   1.9  thorpej 	 */
    273  1.10  thorpej 	map->dm_mapsize = 0;
    274   1.9  thorpej 	map->dm_nsegs = 0;
    275   1.9  thorpej 
    276   1.9  thorpej #ifdef DIAGNOSTIC
    277   1.9  thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    278   1.9  thorpej 		panic("_bus_dmamap_load_mbuf_direct_common: no packet header");
    279   1.9  thorpej #endif
    280   1.2  thorpej 
    281   1.9  thorpej 	if (m0->m_pkthdr.len > map->_dm_size)
    282   1.9  thorpej 		return (EINVAL);
    283   1.9  thorpej 
    284   1.9  thorpej 	first = 1;
    285   1.9  thorpej 	seg = 0;
    286   1.9  thorpej 	error = 0;
    287   1.9  thorpej 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
    288   1.9  thorpej 		error = _bus_dmamap_load_buffer_direct_common(map,
    289   1.9  thorpej 		    m->m_data, m->m_len, NULL, flags, wbase, &lastaddr,
    290   1.9  thorpej 		    &seg, first);
    291   1.9  thorpej 		first = 0;
    292   1.9  thorpej 	}
    293  1.10  thorpej 	if (error == 0) {
    294  1.10  thorpej 		map->dm_mapsize = m0->m_pkthdr.len;
    295   1.9  thorpej 		map->dm_nsegs = seg + 1;
    296  1.10  thorpej 	}
    297   1.9  thorpej 	return (error);
    298   1.2  thorpej }
    299   1.2  thorpej 
    300   1.2  thorpej /*
    301   1.2  thorpej  * Like _bus_dmamap_load_direct_common(), but for uios.
    302   1.2  thorpej  */
    303   1.2  thorpej int
    304   1.2  thorpej _bus_dmamap_load_uio_direct_common(t, map, uio, flags, wbase)
    305   1.2  thorpej 	bus_dma_tag_t t;
    306   1.2  thorpej 	bus_dmamap_t map;
    307   1.2  thorpej 	struct uio *uio;
    308   1.2  thorpej 	int flags;
    309   1.2  thorpej 	bus_addr_t wbase;
    310   1.2  thorpej {
    311   1.2  thorpej 
    312   1.2  thorpej 	panic("_bus_dmamap_load_uio_direct_common: not implemented");
    313   1.2  thorpej }
    314   1.2  thorpej 
    315   1.2  thorpej /*
    316   1.2  thorpej  * Like _bus_dmamap_load_direct_common(), but for raw memory.
    317   1.2  thorpej  */
    318   1.2  thorpej int
    319   1.2  thorpej _bus_dmamap_load_raw_direct_common(t, map, segs, nsegs, size, flags, wbase)
    320   1.2  thorpej 	bus_dma_tag_t t;
    321   1.2  thorpej 	bus_dmamap_t map;
    322   1.2  thorpej 	bus_dma_segment_t *segs;
    323   1.2  thorpej 	int nsegs;
    324   1.2  thorpej 	bus_size_t size;
    325   1.2  thorpej 	int flags;
    326   1.2  thorpej 	bus_addr_t wbase;
    327   1.2  thorpej {
    328   1.2  thorpej 
    329   1.2  thorpej 	panic("_bus_dmamap_load_raw_direct_common: not implemented");
    330   1.2  thorpej }
    331   1.2  thorpej 
    332   1.2  thorpej /*
    333   1.2  thorpej  * Common function for unloading a DMA map.  May be called by
    334   1.2  thorpej  * chipset-specific DMA map unload functions.
    335   1.2  thorpej  */
    336   1.2  thorpej void
    337   1.2  thorpej _bus_dmamap_unload(t, map)
    338   1.2  thorpej 	bus_dma_tag_t t;
    339   1.2  thorpej 	bus_dmamap_t map;
    340   1.2  thorpej {
    341   1.2  thorpej 
    342   1.2  thorpej 	/*
    343   1.2  thorpej 	 * No resources to free; just mark the mappings as
    344   1.2  thorpej 	 * invalid.
    345   1.2  thorpej 	 */
    346  1.10  thorpej 	map->dm_mapsize = 0;
    347   1.2  thorpej 	map->dm_nsegs = 0;
    348   1.2  thorpej }
    349   1.2  thorpej 
    350   1.2  thorpej /*
    351   1.2  thorpej  * Common function for DMA map synchronization.  May be called
    352   1.2  thorpej  * by chipset-specific DMA map synchronization functions.
    353   1.2  thorpej  */
    354   1.2  thorpej void
    355  1.12  thorpej _bus_dmamap_sync(t, map, offset, len, ops)
    356   1.2  thorpej 	bus_dma_tag_t t;
    357   1.2  thorpej 	bus_dmamap_t map;
    358  1.12  thorpej 	bus_addr_t offset;
    359  1.12  thorpej 	bus_size_t len;
    360  1.11  thorpej 	int ops;
    361   1.2  thorpej {
    362   1.2  thorpej 
    363  1.13  thorpej 	/*
    364  1.13  thorpej 	 * Flush the store buffer.
    365  1.13  thorpej 	 */
    366  1.13  thorpej 	alpha_mb();
    367   1.2  thorpej }
    368   1.2  thorpej 
    369   1.2  thorpej /*
    370   1.2  thorpej  * Common function for DMA-safe memory allocation.  May be called
    371   1.2  thorpej  * by bus-specific DMA memory allocation functions.
    372   1.2  thorpej  */
    373   1.2  thorpej int
    374   1.2  thorpej _bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
    375   1.2  thorpej 	bus_dma_tag_t t;
    376   1.2  thorpej 	bus_size_t size, alignment, boundary;
    377   1.2  thorpej 	bus_dma_segment_t *segs;
    378   1.2  thorpej 	int nsegs;
    379   1.2  thorpej 	int *rsegs;
    380   1.2  thorpej 	int flags;
    381   1.2  thorpej {
    382   1.7  thorpej 	extern vm_offset_t avail_start, avail_end;
    383   1.2  thorpej 	vm_offset_t curaddr, lastaddr, high;
    384   1.2  thorpej 	vm_page_t m;
    385   1.2  thorpej 	struct pglist mlist;
    386   1.2  thorpej 	int curseg, error;
    387   1.2  thorpej 
    388   1.2  thorpej 	/* Always round the size. */
    389   1.2  thorpej 	size = round_page(size);
    390   1.2  thorpej 
    391   1.7  thorpej 	high = avail_end - PAGE_SIZE;
    392   1.2  thorpej 
    393   1.2  thorpej 	/*
    394   1.2  thorpej 	 * Allocate pages from the VM system.
    395   1.2  thorpej 	 */
    396   1.2  thorpej 	TAILQ_INIT(&mlist);
    397  1.16  thorpej #if defined(UVM)
    398  1.16  thorpej 	error = uvm_pglistalloc(size, avail_start, high, alignment, boundary,
    399  1.16  thorpej 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    400  1.16  thorpej #else
    401   1.7  thorpej 	error = vm_page_alloc_memory(size, avail_start, high,
    402   1.2  thorpej 	    alignment, boundary, &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    403  1.16  thorpej #endif
    404   1.2  thorpej 	if (error)
    405   1.2  thorpej 		return (error);
    406   1.2  thorpej 
    407   1.2  thorpej 	/*
    408   1.2  thorpej 	 * Compute the location, size, and number of segments actually
    409   1.2  thorpej 	 * returned by the VM code.
    410   1.2  thorpej 	 */
    411   1.2  thorpej 	m = mlist.tqh_first;
    412   1.2  thorpej 	curseg = 0;
    413   1.2  thorpej 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
    414   1.2  thorpej 	segs[curseg].ds_len = PAGE_SIZE;
    415   1.2  thorpej 	m = m->pageq.tqe_next;
    416   1.2  thorpej 
    417   1.2  thorpej 	for (; m != NULL; m = m->pageq.tqe_next) {
    418   1.2  thorpej 		curaddr = VM_PAGE_TO_PHYS(m);
    419   1.2  thorpej #ifdef DIAGNOSTIC
    420   1.7  thorpej 		if (curaddr < avail_start || curaddr >= high) {
    421   1.2  thorpej 			printf("vm_page_alloc_memory returned non-sensical"
    422   1.2  thorpej 			    " address 0x%lx\n", curaddr);
    423   1.2  thorpej 			panic("_bus_dmamem_alloc");
    424   1.2  thorpej 		}
    425   1.2  thorpej #endif
    426   1.2  thorpej 		if (curaddr == (lastaddr + PAGE_SIZE))
    427   1.2  thorpej 			segs[curseg].ds_len += PAGE_SIZE;
    428   1.2  thorpej 		else {
    429   1.2  thorpej 			curseg++;
    430   1.2  thorpej 			segs[curseg].ds_addr = curaddr;
    431   1.2  thorpej 			segs[curseg].ds_len = PAGE_SIZE;
    432   1.2  thorpej 		}
    433   1.2  thorpej 		lastaddr = curaddr;
    434   1.2  thorpej 	}
    435   1.2  thorpej 
    436   1.2  thorpej 	*rsegs = curseg + 1;
    437   1.2  thorpej 
    438   1.2  thorpej 	return (0);
    439   1.2  thorpej }
    440   1.2  thorpej 
    441   1.2  thorpej /*
    442   1.2  thorpej  * Common function for freeing DMA-safe memory.  May be called by
    443   1.2  thorpej  * bus-specific DMA memory free functions.
    444   1.2  thorpej  */
    445   1.2  thorpej void
    446   1.2  thorpej _bus_dmamem_free(t, segs, nsegs)
    447   1.2  thorpej 	bus_dma_tag_t t;
    448   1.2  thorpej 	bus_dma_segment_t *segs;
    449   1.2  thorpej 	int nsegs;
    450   1.2  thorpej {
    451   1.2  thorpej 	vm_page_t m;
    452   1.2  thorpej 	bus_addr_t addr;
    453   1.2  thorpej 	struct pglist mlist;
    454   1.2  thorpej 	int curseg;
    455   1.2  thorpej 
    456   1.2  thorpej 	/*
    457   1.2  thorpej 	 * Build a list of pages to free back to the VM system.
    458   1.2  thorpej 	 */
    459   1.2  thorpej 	TAILQ_INIT(&mlist);
    460   1.2  thorpej 	for (curseg = 0; curseg < nsegs; curseg++) {
    461   1.2  thorpej 		for (addr = segs[curseg].ds_addr;
    462   1.2  thorpej 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    463   1.2  thorpej 		    addr += PAGE_SIZE) {
    464   1.2  thorpej 			m = PHYS_TO_VM_PAGE(addr);
    465   1.2  thorpej 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
    466   1.2  thorpej 		}
    467   1.2  thorpej 	}
    468   1.2  thorpej 
    469  1.16  thorpej #if defined(UVM)
    470  1.16  thorpej 	uvm_pglistfree(&mlist);
    471  1.16  thorpej #else
    472   1.2  thorpej 	vm_page_free_memory(&mlist);
    473  1.16  thorpej #endif
    474   1.2  thorpej }
    475   1.2  thorpej 
    476   1.2  thorpej /*
    477   1.2  thorpej  * Common function for mapping DMA-safe memory.  May be called by
    478   1.2  thorpej  * bus-specific DMA memory map functions.
    479   1.2  thorpej  */
    480   1.2  thorpej int
    481   1.2  thorpej _bus_dmamem_map(t, segs, nsegs, size, kvap, flags)
    482   1.2  thorpej 	bus_dma_tag_t t;
    483   1.2  thorpej 	bus_dma_segment_t *segs;
    484   1.2  thorpej 	int nsegs;
    485   1.2  thorpej 	size_t size;
    486   1.2  thorpej 	caddr_t *kvap;
    487   1.2  thorpej 	int flags;
    488   1.2  thorpej {
    489   1.2  thorpej 	vm_offset_t va;
    490   1.2  thorpej 	bus_addr_t addr;
    491  1.16  thorpej 	int curseg;
    492   1.2  thorpej 
    493   1.8  thorpej 	/*
    494   1.8  thorpej 	 * If we're only mapping 1 segment, use K0SEG, to avoid
    495   1.8  thorpej 	 * TLB thrashing.
    496   1.8  thorpej 	 */
    497   1.8  thorpej 	if (nsegs == 1) {
    498   1.8  thorpej 		*kvap = (caddr_t)ALPHA_PHYS_TO_K0SEG(segs[0].ds_addr);
    499   1.8  thorpej 		return (0);
    500   1.8  thorpej 	}
    501   1.8  thorpej 
    502   1.2  thorpej 	size = round_page(size);
    503   1.3  thorpej 
    504  1.16  thorpej #if defined(UVM)
    505  1.16  thorpej 	va = uvm_km_valloc(kernel_map, size);
    506  1.16  thorpej #else
    507  1.15  thorpej 	va = kmem_alloc_pageable(kernel_map, size);
    508  1.16  thorpej #endif
    509   1.3  thorpej 
    510   1.2  thorpej 	if (va == 0)
    511   1.2  thorpej 		return (ENOMEM);
    512   1.2  thorpej 
    513   1.2  thorpej 	*kvap = (caddr_t)va;
    514   1.2  thorpej 
    515   1.2  thorpej 	for (curseg = 0; curseg < nsegs; curseg++) {
    516   1.2  thorpej 		for (addr = segs[curseg].ds_addr;
    517   1.2  thorpej 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    518   1.2  thorpej 		    addr += NBPG, va += NBPG, size -= NBPG) {
    519   1.2  thorpej 			if (size == 0)
    520   1.2  thorpej 				panic("_bus_dmamem_map: size botch");
    521  1.17  thorpej #if defined(PMAP_NEW)
    522  1.17  thorpej 			pmap_kenter_pa(va, addr, VM_PROT_READ | VM_PROT_WRITE);
    523  1.17  thorpej #else
    524   1.2  thorpej 			pmap_enter(pmap_kernel(), va, addr,
    525   1.2  thorpej 			    VM_PROT_READ | VM_PROT_WRITE, TRUE);
    526  1.17  thorpej #endif
    527   1.2  thorpej 		}
    528   1.2  thorpej 	}
    529   1.2  thorpej 
    530   1.2  thorpej 	return (0);
    531   1.2  thorpej }
    532   1.2  thorpej 
    533   1.2  thorpej /*
    534   1.2  thorpej  * Common function for unmapping DMA-safe memory.  May be called by
    535   1.2  thorpej  * bus-specific DMA memory unmapping functions.
    536   1.2  thorpej  */
    537   1.2  thorpej void
    538   1.2  thorpej _bus_dmamem_unmap(t, kva, size)
    539   1.2  thorpej 	bus_dma_tag_t t;
    540   1.2  thorpej 	caddr_t kva;
    541   1.2  thorpej 	size_t size;
    542   1.2  thorpej {
    543   1.2  thorpej 
    544   1.2  thorpej #ifdef DIAGNOSTIC
    545   1.2  thorpej 	if ((u_long)kva & PGOFSET)
    546   1.2  thorpej 		panic("_bus_dmamem_unmap");
    547   1.2  thorpej #endif
    548   1.8  thorpej 
    549   1.8  thorpej 	/*
    550   1.8  thorpej 	 * Nothing to do if we mapped it with K0SEG.
    551   1.8  thorpej 	 */
    552   1.8  thorpej 	if (kva >= (caddr_t)ALPHA_K0SEG_BASE &&
    553   1.8  thorpej 	    kva <= (caddr_t)ALPHA_K0SEG_END)
    554   1.8  thorpej 		return;
    555   1.2  thorpej 
    556   1.2  thorpej 	size = round_page(size);
    557  1.16  thorpej #if defined(UVM)
    558  1.16  thorpej 	uvm_km_free(kernel_map, (vm_offset_t)kva, size);
    559  1.16  thorpej #else
    560  1.15  thorpej 	kmem_free(kernel_map, (vm_offset_t)kva, size);
    561  1.16  thorpej #endif
    562   1.2  thorpej }
    563   1.2  thorpej 
    564   1.2  thorpej /*
    565   1.2  thorpej  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    566   1.2  thorpej  * bus-specific DMA mmap(2)'ing functions.
    567   1.2  thorpej  */
    568   1.2  thorpej int
    569   1.2  thorpej _bus_dmamem_mmap(t, segs, nsegs, off, prot, flags)
    570   1.2  thorpej 	bus_dma_tag_t t;
    571   1.2  thorpej 	bus_dma_segment_t *segs;
    572   1.2  thorpej 	int nsegs, off, prot, flags;
    573   1.2  thorpej {
    574   1.6  thorpej 	int i;
    575   1.2  thorpej 
    576   1.6  thorpej 	for (i = 0; i < nsegs; i++) {
    577   1.6  thorpej #ifdef DIAGNOSTIC
    578   1.6  thorpej 		if (off & PGOFSET)
    579   1.6  thorpej 			panic("_bus_dmamem_mmap: offset unaligned");
    580   1.6  thorpej 		if (segs[i].ds_addr & PGOFSET)
    581   1.6  thorpej 			panic("_bus_dmamem_mmap: segment unaligned");
    582   1.6  thorpej 		if (segs[i].ds_len & PGOFSET)
    583   1.6  thorpej 			panic("_bus_dmamem_mmap: segment size not multiple"
    584   1.6  thorpej 			    " of page size");
    585   1.6  thorpej #endif
    586   1.6  thorpej 		if (off >= segs[i].ds_len) {
    587   1.6  thorpej 			off -= segs[i].ds_len;
    588   1.6  thorpej 			continue;
    589   1.6  thorpej 		}
    590   1.6  thorpej 
    591   1.6  thorpej 		return (alpha_btop((caddr_t)segs[i].ds_addr + off));
    592   1.6  thorpej 	}
    593   1.6  thorpej 
    594   1.6  thorpej 	/* Page not found. */
    595   1.6  thorpej 	return (-1);
    596   1.2  thorpej }
    597