Home | History | Annotate | Line # | Download | only in vax
sgmap.c revision 1.4
      1 /* $NetBSD: sgmap.c,v 1.4 2000/03/07 00:04:13 matt Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 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 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/proc.h>
     44 #include <sys/malloc.h>
     45 
     46 #include <vm/vm.h>
     47 
     48 #include <machine/bus.h>
     49 #include <machine/sgmap.h>
     50 
     51 void
     52 vax_sgmap_init(t, sgmap, name, sgvabase, sgvasize, ptva, minptalign)
     53 	bus_dma_tag_t t;
     54 	struct vax_sgmap *sgmap;
     55 	const char *name;
     56 	bus_addr_t sgvabase;
     57 	bus_size_t sgvasize;
     58 	struct pte *ptva;
     59 	bus_size_t minptalign;
     60 {
     61 	bus_dma_segment_t seg;
     62 	size_t ptsize;
     63 	int rseg;
     64 
     65 	if (sgvasize & PGOFSET) {
     66 		printf("size botch for sgmap `%s'\n", name);
     67 		goto die;
     68 	}
     69 
     70 	sgmap->aps_sgvabase = sgvabase;
     71 	sgmap->aps_sgvasize = sgvasize;
     72 
     73 	if (ptva != NULL) {
     74 		/*
     75 		 * We already have a page table; this may be a system
     76 		 * where the page table resides in bridge-resident SRAM.
     77 		 */
     78 		sgmap->aps_pt = ptva;
     79 	} else {
     80 		/*
     81 		 * Compute the page table size and allocate it.  At minimum,
     82 		 * this must be aligned to the page table size.  However,
     83 		 * some platforms have more strict alignment reqirements.
     84 		 */
     85 		ptsize = (sgvasize / VAX_NBPG) * sizeof(struct pte);
     86 		if (minptalign != 0) {
     87 			if (minptalign < ptsize)
     88 				minptalign = ptsize;
     89 		} else
     90 			minptalign = ptsize;
     91 		if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg,
     92 		    BUS_DMA_NOWAIT)) {
     93 			panic("unable to allocate page table for sgmap `%s'\n",
     94 			    name);
     95 			goto die;
     96 		}
     97 		sgmap->aps_pt = (struct pte *)(seg.ds_addr | KERNBASE);
     98 	}
     99 
    100 	/*
    101 	 * Create the extent map used to manage the virtual address
    102 	 * space.
    103 	 */
    104 	sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
    105 	    M_DMAMAP, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
    106 	if (sgmap->aps_ex == NULL) {
    107 		printf("unable to create extent map for sgmap `%s'\n", name);
    108 		goto die;
    109 	}
    110 
    111 	return;
    112  die:
    113 	panic("vax_sgmap_init");
    114 }
    115 
    116 int
    117 vax_sgmap_alloc(map, origlen, sgmap, flags)
    118 	bus_dmamap_t map;
    119 	bus_size_t origlen;
    120 	struct vax_sgmap *sgmap;
    121 	int flags;
    122 {
    123 	int error;
    124 	bus_size_t len = origlen;
    125 
    126 #ifdef DIAGNOSTIC
    127 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    128 		panic("vax_sgmap_alloc: already have sgva space");
    129 #endif
    130 
    131 	map->_dm_sgvalen = vax_round_page(len);
    132 
    133 #if 0
    134 	printf("len %x -> %x, _dm_sgvalen %x _dm_boundary %x boundary %x -> ",
    135 	    origlen, len, map->_dm_sgvalen, map->_dm_boundary, boundary);
    136 #endif
    137 
    138 	error = extent_alloc(sgmap->aps_ex, map->_dm_sgvalen, VAX_NBPG,
    139 	    0, (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK,
    140 	    &map->_dm_sgva);
    141 #if 0
    142 	printf("error %d _dm_sgva %x\n", error, map->_dm_sgva);
    143 #endif
    144 
    145 	if (error == 0)
    146 		map->_dm_flags |= DMAMAP_HAS_SGMAP;
    147 	else
    148 		map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
    149 
    150 	return (error);
    151 }
    152 
    153 void
    154 vax_sgmap_free(map, sgmap)
    155 	bus_dmamap_t map;
    156 	struct vax_sgmap *sgmap;
    157 {
    158 
    159 #ifdef DIAGNOSTIC
    160 	if ((map->_dm_flags & DMAMAP_HAS_SGMAP) == 0)
    161 		panic("vax_sgmap_free: no sgva space to free");
    162 #endif
    163 
    164 	if (extent_free(sgmap->aps_ex, map->_dm_sgva, map->_dm_sgvalen,
    165 	    EX_NOWAIT))
    166 		panic("vax_sgmap_free");
    167 
    168 	map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
    169 }
    170 
    171 int
    172 vax_sgmap_load(t, map, buf, buflen, p, flags, sgmap)
    173 	bus_dma_tag_t t;
    174 	bus_dmamap_t map;
    175 	void *buf;
    176 	bus_size_t buflen;
    177 	struct proc *p;
    178 	int flags;
    179 	struct vax_sgmap *sgmap;
    180 {
    181 	vaddr_t endva, va = (vaddr_t)buf;
    182 	paddr_t pa;
    183 	bus_addr_t dmaoffset;
    184 	bus_size_t dmalen;
    185 	long *pte, *page_table = (long *)sgmap->aps_pt;
    186 	int pteidx, error;
    187 
    188 	/*
    189 	 * Make sure that on error condition we return "no valid mappings".
    190 	 */
    191 	map->dm_mapsize = 0;
    192 	map->dm_nsegs = 0;
    193 
    194 	if (buflen > map->_dm_size)
    195 		return (EINVAL);
    196 
    197 	/*
    198 	 * Remember the offset into the first page and the total
    199 	 * transfer length.
    200 	 */
    201 	dmaoffset = ((u_long)buf) & VAX_PGOFSET;
    202 	dmalen = buflen;
    203 
    204 
    205 	/*
    206 	 * Allocate the necessary virtual address space for the
    207 	 * mapping.  Round the size, since we deal with whole pages.
    208 	 */
    209 	endva = vax_round_page(va + buflen);
    210 	va = vax_trunc_page(va);
    211 	if ((map->_dm_flags & DMAMAP_HAS_SGMAP) == 0) {
    212 		error = vax_sgmap_alloc(map, (endva - va), sgmap, flags);
    213 		if (error)
    214 			return (error);
    215 	}
    216 
    217 	pteidx = map->_dm_sgva >> VAX_PGSHIFT;
    218 	pte = &page_table[pteidx];
    219 
    220 	/*
    221 	 * Generate the DMA address.
    222 	 */
    223 	map->dm_segs[0].ds_addr = map->_dm_sgva + dmaoffset;
    224 	map->dm_segs[0].ds_len = dmalen;
    225 
    226 
    227 	map->_dm_pteidx = pteidx;
    228 	map->_dm_ptecnt = 0;
    229 
    230 	/*
    231 	 * Create the bus-specific page tables.
    232 	 * Can be done much more efficient than this.
    233 	 */
    234 	for (; va < endva; va += VAX_NBPG, pte++, map->_dm_ptecnt++) {
    235 		/*
    236 		 * Get the physical address for this segment.
    237 		 */
    238 		if (p != NULL)
    239 			(void) pmap_extract(p->p_vmspace->vm_map.pmap, va, &pa);
    240 		else
    241 			pa = kvtophys(va);
    242 
    243 		/*
    244 		 * Load the current PTE with this page.
    245 		 */
    246 		*pte = (pa >> VAX_PGSHIFT) | PG_V;
    247 	}
    248 
    249 	map->dm_mapsize = buflen;
    250 	map->dm_nsegs = 1;
    251 	return (0);
    252 }
    253 
    254 int
    255 vax_sgmap_load_mbuf(t, map, m, flags, sgmap)
    256 	bus_dma_tag_t t;
    257 	bus_dmamap_t map;
    258 	struct mbuf *m;
    259 	int flags;
    260 	struct vax_sgmap *sgmap;
    261 {
    262 
    263 	panic("vax_sgmap_load_mbuf : not implemented");
    264 }
    265 
    266 int
    267 vax_sgmap_load_uio(t, map, uio, flags, sgmap)
    268 	bus_dma_tag_t t;
    269 	bus_dmamap_t map;
    270 	struct uio *uio;
    271 	int flags;
    272 	struct vax_sgmap *sgmap;
    273 {
    274 
    275 	panic("vax_sgmap_load_uio : not implemented");
    276 }
    277 
    278 int
    279 vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, sgmap)
    280 	bus_dma_tag_t t;
    281 	bus_dmamap_t map;
    282 	bus_dma_segment_t *segs;
    283 	int nsegs;
    284 	bus_size_t size;
    285 	int flags;
    286 	struct vax_sgmap *sgmap;
    287 {
    288 
    289 	panic("vax_sgmap_load_raw : not implemented");
    290 }
    291 
    292 void
    293 vax_sgmap_unload(t, map, sgmap)
    294 	bus_dma_tag_t t;
    295 	bus_dmamap_t map;
    296 	struct vax_sgmap *sgmap;
    297 {
    298 	long *pte, *page_table = (long *)sgmap->aps_pt;
    299 	int ptecnt;
    300 
    301 	/*
    302 	 * Invalidate the PTEs for the mapping.
    303 	 */
    304 	for (ptecnt = map->_dm_ptecnt, pte = &page_table[map->_dm_pteidx];
    305 		ptecnt-- != 0; ) {
    306 		*pte++ = 0;
    307 	}
    308 
    309 	/*
    310 	 * Free the virtual address space used by the mapping
    311 	 * if necessary.
    312 	 */
    313 	if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    314 		vax_sgmap_free(map, sgmap);
    315 	/*
    316 	 * Mark the mapping invalid.
    317 	 */
    318 	map->dm_mapsize = 0;
    319 	map->dm_nsegs = 0;
    320 }
    321