1*febb7cceSsnj /* $NetBSD: dvma.c,v 1.15 2009/10/21 23:12:09 snj Exp $ */
27cedf69cSmrg /*
37cedf69cSmrg * Copyright (c) 1995 Gordon W. Ross
47cedf69cSmrg * All rights reserved.
57cedf69cSmrg *
67cedf69cSmrg * Redistribution and use in source and binary forms, with or without
77cedf69cSmrg * modification, are permitted provided that the following conditions
87cedf69cSmrg * are met:
97cedf69cSmrg * 1. Redistributions of source code must retain the above copyright
107cedf69cSmrg * notice, this list of conditions and the following disclaimer.
117cedf69cSmrg * 2. Redistributions in binary form must reproduce the above copyright
127cedf69cSmrg * notice, this list of conditions and the following disclaimer in the
137cedf69cSmrg * documentation and/or other materials provided with the distribution.
147cedf69cSmrg *
157cedf69cSmrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167cedf69cSmrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177cedf69cSmrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187cedf69cSmrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197cedf69cSmrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207cedf69cSmrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217cedf69cSmrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227cedf69cSmrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237cedf69cSmrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247cedf69cSmrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
257cedf69cSmrg */
267cedf69cSmrg
277cedf69cSmrg /*
2859742052Spk * The easiest way to deal with the need for DVMA mappings is to just
2959742052Spk * map the entire megabyte of RAM where we are loaded into DVMA space.
3059742052Spk * That way, dvma_mapin can just compute the DVMA alias address, and
3159742052Spk * dvma_mapout does nothing. Note that this assumes all standalone
3259742052Spk * programs stay in the range `base_va' .. `base_va + DVMA_MAPLEN'
337cedf69cSmrg */
347cedf69cSmrg
357cedf69cSmrg #include <sys/param.h>
365d124ce7Spk #include <sparc/sparc/asm.h>
377cedf69cSmrg #include <machine/pte.h>
387cedf69cSmrg #include <machine/ctlreg.h>
397cedf69cSmrg
40d292580fSpk #include <lib/libsa/stand.h>
417cedf69cSmrg #include <sparc/stand/common/promdev.h>
427cedf69cSmrg
437cedf69cSmrg #define DVMA_BASE 0xFFF00000
447cedf69cSmrg #define DVMA_MAPLEN 0xE0000 /* 1 MB - 128K (save MONSHORTSEG) */
457cedf69cSmrg
4659742052Spk static int base_va;
477cedf69cSmrg
48d292580fSpk /*
49d292580fSpk * This module is only used on sun4, so:
50d292580fSpk */
51d292580fSpk #define getsegmap(va) (lduha(va, ASI_SEGMAP))
52d292580fSpk #define setsegmap(va, pmeg) do stha(va, ASI_SEGMAP, pmeg); while(0)
53d292580fSpk
547cedf69cSmrg void
dvma_init(void)554c3c91e6Suwe dvma_init(void)
567cedf69cSmrg {
57cd955cb1Spk u_int segva, dmava;
58cd955cb1Spk int nseg;
5959742052Spk extern int start;
607cedf69cSmrg
614bddbd7eSpk /*
624bddbd7eSpk * Align our address base with the DVMA segment.
634bddbd7eSpk * Allocate one DVMA segment to cover the stack, which
644bddbd7eSpk * grows downward from `start'.
654bddbd7eSpk */
66cd955cb1Spk dmava = DVMA_BASE;
674bddbd7eSpk base_va = segva = (((int)&start) & -NBPSG) - NBPSG;
6859742052Spk
69ee1b4065Swiz /* Then double-map the DVMA addresses */
70cd955cb1Spk nseg = (DVMA_MAPLEN + NBPSG - 1) >> SGSHIFT;
71cd955cb1Spk while (nseg-- > 0) {
727cedf69cSmrg setsegmap(dmava, getsegmap(segva));
7359742052Spk segva += NBPSG;
747cedf69cSmrg dmava += NBPSG;
757cedf69cSmrg }
767cedf69cSmrg }
777cedf69cSmrg
787cedf69cSmrg /*
797cedf69cSmrg * Convert a local address to a DVMA address.
807cedf69cSmrg */
817cedf69cSmrg char *
dvma_mapin(char * addr,size_t len)824c3c91e6Suwe dvma_mapin(char *addr, size_t len)
837cedf69cSmrg {
84d292580fSpk int va = (int)addr;
857cedf69cSmrg
8659742052Spk va -= base_va;
877cedf69cSmrg
8859742052Spk #ifndef BOOTXX
8959742052Spk /* Make sure the address is in the DVMA map. */
9059742052Spk if (va < 0 || va >= DVMA_MAPLEN)
914bddbd7eSpk panic("dvma_mapin: va %x (DMA base %x)", va+base_va, base_va);
9259742052Spk #endif
9359742052Spk
9459742052Spk va += DVMA_BASE;
957cedf69cSmrg
967cedf69cSmrg return ((char *)va);
977cedf69cSmrg }
987cedf69cSmrg
997cedf69cSmrg /*
1007cedf69cSmrg * Convert a DVMA address to a local address.
1017cedf69cSmrg */
1027cedf69cSmrg char *
dvma_mapout(char * addr,size_t len)1034c3c91e6Suwe dvma_mapout(char *addr, size_t len)
1047cedf69cSmrg {
1057cedf69cSmrg int va = (int)addr;
1067cedf69cSmrg
10759742052Spk va -= DVMA_BASE;
1087cedf69cSmrg
10959742052Spk #ifndef BOOTXX
11059742052Spk /* Make sure the address is in the DVMA map. */
11159742052Spk if (va < 0 || va >= DVMA_MAPLEN)
1124bddbd7eSpk panic("dvma_mapout: va %x (DMA base %x)", va+base_va, base_va);
11359742052Spk #endif
11459742052Spk
11559742052Spk va += base_va;
1167cedf69cSmrg
1177cedf69cSmrg return ((char *)va);
1187cedf69cSmrg }
1197cedf69cSmrg
1207cedf69cSmrg char *
dvma_alloc(int len)1214c3c91e6Suwe dvma_alloc(int len)
1227cedf69cSmrg {
1237cedf69cSmrg char *mem;
1247cedf69cSmrg
1257cedf69cSmrg mem = alloc(len);
126d292580fSpk if (mem == NULL)
1277cedf69cSmrg return (mem);
1287cedf69cSmrg return (dvma_mapin(mem, len));
1297cedf69cSmrg }
1307cedf69cSmrg
1317cedf69cSmrg void
dvma_free(char * dvma,int len)1324c3c91e6Suwe dvma_free(char *dvma, int len)
1337cedf69cSmrg {
1347cedf69cSmrg char *mem;
1357cedf69cSmrg
1367cedf69cSmrg mem = dvma_mapout(dvma, len);
137d292580fSpk if (mem != NULL)
1386f95f80cSuwe dealloc(mem, len);
1397cedf69cSmrg }
140