Home | History | Annotate | Line # | Download | only in common
dvma.c revision 1.3.2.2
      1  1.3.2.2   he /*	$NetBSD: dvma.c,v 1.3.2.2 2000/07/23 16:13:49 he 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.3.2.1   he  * The easiest way to deal with the need for DVMA mappings is to just
     34  1.3.2.1   he  * map the entire megabyte of RAM where we are loaded into DVMA space.
     35  1.3.2.1   he  * That way, dvma_mapin can just compute the DVMA alias address, and
     36  1.3.2.1   he  * dvma_mapout does nothing.  Note that this assumes all standalone
     37  1.3.2.1   he  * 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.3.2.1   he 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.3.2.1   he 	u_int segva, dmava;
     63  1.3.2.1   he 	int nseg;
     64  1.3.2.1   he 	extern int start asm("start");
     65      1.1  mrg 
     66  1.3.2.1   he 	/* Align our address base with the DVMA segment */
     67      1.1  mrg 	dmava = DVMA_BASE;
     68  1.3.2.2   he 	base_va = segva = (((int)&start)  - 0x40000) & DVMA_BASE;
     69  1.3.2.1   he 
     70  1.3.2.1   he 	/* Then double-map the DVMA adresses */
     71  1.3.2.1   he 	nseg = (DVMA_MAPLEN + NBPSG - 1) >> SGSHIFT;
     72  1.3.2.1   he 	while (nseg-- > 0) {
     73      1.1  mrg 		setsegmap(dmava, getsegmap(segva));
     74  1.3.2.1   he 		segva += NBPSG;
     75      1.1  mrg 		dmava += NBPSG;
     76      1.1  mrg 	}
     77      1.1  mrg }
     78      1.1  mrg 
     79      1.1  mrg /*
     80      1.1  mrg  * Convert a local address to a DVMA address.
     81      1.1  mrg  */
     82      1.1  mrg char *
     83      1.3   pk dvma_mapin(addr, len)
     84      1.3   pk 	char *addr;
     85      1.3   pk 	size_t len;
     86      1.1  mrg {
     87      1.3   pk 	int va = (int)addr;
     88      1.1  mrg 
     89  1.3.2.1   he 	va -= base_va;
     90  1.3.2.1   he 
     91  1.3.2.1   he #ifndef BOOTXX
     92      1.1  mrg 	/* Make sure the address is in the DVMA map. */
     93  1.3.2.1   he 	if (va < 0 || va >= DVMA_MAPLEN)
     94      1.1  mrg 		panic("dvma_mapin");
     95  1.3.2.1   he #endif
     96      1.1  mrg 
     97  1.3.2.1   he 	va += DVMA_BASE;
     98      1.1  mrg 
     99      1.1  mrg 	return ((char *)va);
    100      1.1  mrg }
    101      1.1  mrg 
    102      1.1  mrg /*
    103      1.1  mrg  * Convert a DVMA address to a local address.
    104      1.1  mrg  */
    105      1.1  mrg char *
    106      1.3   pk dvma_mapout(addr, len)
    107      1.3   pk 	char *addr;
    108      1.3   pk 	size_t len;
    109      1.1  mrg {
    110      1.1  mrg 	int va = (int)addr;
    111      1.1  mrg 
    112  1.3.2.1   he 	va -= DVMA_BASE;
    113  1.3.2.1   he 
    114  1.3.2.1   he #ifndef BOOTXX
    115      1.1  mrg 	/* Make sure the address is in the DVMA map. */
    116  1.3.2.1   he 	if (va < 0 || va >= DVMA_MAPLEN)
    117      1.1  mrg 		panic("dvma_mapout");
    118  1.3.2.1   he #endif
    119      1.1  mrg 
    120  1.3.2.1   he 	va += base_va;
    121      1.1  mrg 
    122      1.1  mrg 	return ((char *)va);
    123      1.1  mrg }
    124      1.1  mrg 
    125      1.1  mrg char *
    126      1.3   pk dvma_alloc(len)
    127      1.3   pk 	int len;
    128      1.1  mrg {
    129      1.1  mrg 	char *mem;
    130      1.1  mrg 
    131      1.1  mrg 	mem = alloc(len);
    132      1.3   pk 	if (mem == NULL)
    133      1.1  mrg 		return (mem);
    134      1.1  mrg 	return (dvma_mapin(mem, len));
    135      1.1  mrg }
    136      1.1  mrg 
    137      1.1  mrg void
    138      1.3   pk dvma_free(dvma, len)
    139      1.3   pk 	char *dvma;
    140      1.3   pk 	int len;
    141      1.1  mrg {
    142      1.1  mrg 	char *mem;
    143      1.1  mrg 
    144      1.1  mrg 	mem = dvma_mapout(dvma, len);
    145      1.3   pk 	if (mem != NULL)
    146      1.1  mrg 		free(mem, len);
    147      1.1  mrg }
    148