Home | History | Annotate | Line # | Download | only in common
dvma.c revision 1.8.2.1
      1  1.8.2.1   tv /*	$NetBSD: dvma.c,v 1.8.2.1 2000/11/01 03:25:10 tv Exp $	*/
      2      1.1  mrg /*
      3      1.1  mrg  * Copyright (c) 1995 Gordon W. Ross
      4      1.1  mrg  * All rights reserved.
      5      1.1  mrg  *
      6      1.1  mrg  * Redistribution and use in source and binary forms, with or without
      7      1.1  mrg  * modification, are permitted provided that the following conditions
      8      1.1  mrg  * are met:
      9      1.1  mrg  * 1. Redistributions of source code must retain the above copyright
     10      1.1  mrg  *    notice, this list of conditions and the following disclaimer.
     11      1.1  mrg  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  mrg  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  mrg  *    documentation and/or other materials provided with the distribution.
     14      1.1  mrg  * 3. The name of the author may not be used to endorse or promote products
     15      1.1  mrg  *    derived from this software without specific prior written permission.
     16      1.1  mrg  * 4. All advertising materials mentioning features or use of this software
     17      1.1  mrg  *    must display the following acknowledgement:
     18      1.1  mrg  *      This product includes software developed by Gordon Ross
     19      1.1  mrg  *
     20      1.1  mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21      1.1  mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22      1.1  mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23      1.1  mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24      1.1  mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25      1.1  mrg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26      1.1  mrg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27      1.1  mrg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28      1.1  mrg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29      1.1  mrg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30      1.1  mrg  */
     31      1.1  mrg 
     32      1.1  mrg /*
     33      1.5   pk  * The easiest way to deal with the need for DVMA mappings is to just
     34      1.5   pk  * map the entire megabyte of RAM where we are loaded into DVMA space.
     35      1.5   pk  * That way, dvma_mapin can just compute the DVMA alias address, and
     36      1.5   pk  * dvma_mapout does nothing.  Note that this assumes all standalone
     37      1.5   pk  * programs stay in the range `base_va' .. `base_va + DVMA_MAPLEN'
     38      1.1  mrg  */
     39      1.1  mrg 
     40      1.1  mrg #include <sys/param.h>
     41      1.2   pk #include <sparc/sparc/asm.h>
     42      1.1  mrg #include <machine/pte.h>
     43      1.1  mrg #include <machine/ctlreg.h>
     44      1.1  mrg 
     45      1.3   pk #include <lib/libsa/stand.h>
     46      1.1  mrg #include <sparc/stand/common/promdev.h>
     47      1.1  mrg 
     48      1.1  mrg #define	DVMA_BASE	0xFFF00000
     49      1.1  mrg #define DVMA_MAPLEN	0xE0000	/* 1 MB - 128K (save MONSHORTSEG) */
     50      1.1  mrg 
     51      1.5   pk static int base_va;
     52      1.1  mrg 
     53      1.3   pk /*
     54      1.3   pk  * This module is only used on sun4, so:
     55      1.3   pk  */
     56      1.3   pk #define	getsegmap(va)		(lduha(va, ASI_SEGMAP))
     57      1.3   pk #define	setsegmap(va, pmeg)	do stha(va, ASI_SEGMAP, pmeg); while(0)
     58      1.3   pk 
     59      1.1  mrg void
     60      1.1  mrg dvma_init()
     61      1.1  mrg {
     62      1.6   pk 	u_int segva, dmava;
     63      1.6   pk 	int nseg;
     64      1.5   pk 	extern int start;
     65      1.1  mrg 
     66  1.8.2.1   tv 	/*
     67  1.8.2.1   tv 	 * Align our address base with the DVMA segment.
     68  1.8.2.1   tv 	 * Allocate one DVMA segment to cover the stack, which
     69  1.8.2.1   tv 	 * grows downward from `start'.
     70  1.8.2.1   tv 	 */
     71      1.6   pk 	dmava = DVMA_BASE;
     72  1.8.2.1   tv 	base_va = segva = (((int)&start) & -NBPSG) - NBPSG;
     73      1.5   pk 
     74      1.5   pk 	/* Then double-map the DVMA adresses */
     75      1.6   pk 	nseg = (DVMA_MAPLEN + NBPSG - 1) >> SGSHIFT;
     76      1.6   pk 	while (nseg-- > 0) {
     77      1.1  mrg 		setsegmap(dmava, getsegmap(segva));
     78      1.5   pk 		segva += NBPSG;
     79      1.1  mrg 		dmava += NBPSG;
     80      1.1  mrg 	}
     81      1.1  mrg }
     82      1.1  mrg 
     83      1.1  mrg /*
     84      1.1  mrg  * Convert a local address to a DVMA address.
     85      1.1  mrg  */
     86      1.1  mrg char *
     87      1.3   pk dvma_mapin(addr, len)
     88      1.3   pk 	char *addr;
     89      1.3   pk 	size_t len;
     90      1.1  mrg {
     91      1.3   pk 	int va = (int)addr;
     92      1.1  mrg 
     93      1.5   pk 	va -= base_va;
     94      1.5   pk 
     95      1.5   pk #ifndef BOOTXX
     96      1.1  mrg 	/* Make sure the address is in the DVMA map. */
     97      1.5   pk 	if (va < 0 || va >= DVMA_MAPLEN)
     98  1.8.2.1   tv 		panic("dvma_mapin: va %x (DMA base %x)", va+base_va, base_va);
     99      1.5   pk #endif
    100      1.1  mrg 
    101      1.5   pk 	va += DVMA_BASE;
    102      1.1  mrg 
    103      1.1  mrg 	return ((char *)va);
    104      1.1  mrg }
    105      1.1  mrg 
    106      1.1  mrg /*
    107      1.1  mrg  * Convert a DVMA address to a local address.
    108      1.1  mrg  */
    109      1.1  mrg char *
    110      1.3   pk dvma_mapout(addr, len)
    111      1.3   pk 	char *addr;
    112      1.3   pk 	size_t len;
    113      1.1  mrg {
    114      1.1  mrg 	int va = (int)addr;
    115      1.1  mrg 
    116      1.5   pk 	va -= DVMA_BASE;
    117      1.5   pk 
    118      1.5   pk #ifndef BOOTXX
    119      1.1  mrg 	/* Make sure the address is in the DVMA map. */
    120      1.5   pk 	if (va < 0 || va >= DVMA_MAPLEN)
    121  1.8.2.1   tv 		panic("dvma_mapout: va %x (DMA base %x)", va+base_va, base_va);
    122      1.5   pk #endif
    123      1.1  mrg 
    124      1.5   pk 	va += base_va;
    125      1.1  mrg 
    126      1.1  mrg 	return ((char *)va);
    127      1.1  mrg }
    128      1.1  mrg 
    129      1.1  mrg char *
    130      1.3   pk dvma_alloc(len)
    131      1.3   pk 	int len;
    132      1.1  mrg {
    133      1.1  mrg 	char *mem;
    134      1.1  mrg 
    135      1.1  mrg 	mem = alloc(len);
    136      1.3   pk 	if (mem == NULL)
    137      1.1  mrg 		return (mem);
    138      1.1  mrg 	return (dvma_mapin(mem, len));
    139      1.1  mrg }
    140      1.1  mrg 
    141      1.1  mrg void
    142      1.3   pk dvma_free(dvma, len)
    143      1.3   pk 	char *dvma;
    144      1.3   pk 	int len;
    145      1.1  mrg {
    146      1.1  mrg 	char *mem;
    147      1.1  mrg 
    148      1.1  mrg 	mem = dvma_mapout(dvma, len);
    149      1.3   pk 	if (mem != NULL)
    150      1.1  mrg 		free(mem, len);
    151      1.1  mrg }
    152