Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / sparc / stand / common / dvma.c
blobbe5448a6413dc0323e141fe4f1e32cd116c93309
1 /* $NetBSD: dvma.c,v 1.14 2006/07/13 20:03:34 uwe Exp $ */
2 /*
3 * Copyright (c) 1995 Gordon W. Ross
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The easiest way to deal with the need for DVMA mappings is to just
29 * map the entire megabyte of RAM where we are loaded into DVMA space.
30 * That way, dvma_mapin can just compute the DVMA alias address, and
31 * dvma_mapout does nothing. Note that this assumes all standalone
32 * programs stay in the range `base_va' .. `base_va + DVMA_MAPLEN'
35 #include <sys/param.h>
36 #include <sparc/sparc/asm.h>
37 #include <machine/pte.h>
38 #include <machine/ctlreg.h>
40 #include <lib/libsa/stand.h>
41 #include <sparc/stand/common/promdev.h>
43 #define DVMA_BASE 0xFFF00000
44 #define DVMA_MAPLEN 0xE0000 /* 1 MB - 128K (save MONSHORTSEG) */
46 static int base_va;
49 * This module is only used on sun4, so:
51 #define getsegmap(va) (lduha(va, ASI_SEGMAP))
52 #define setsegmap(va, pmeg) do stha(va, ASI_SEGMAP, pmeg); while(0)
54 void
55 dvma_init(void)
57 u_int segva, dmava;
58 int nseg;
59 extern int start;
62 * Align our address base with the DVMA segment.
63 * Allocate one DVMA segment to cover the stack, which
64 * grows downward from `start'.
66 dmava = DVMA_BASE;
67 base_va = segva = (((int)&start) & -NBPSG) - NBPSG;
69 /* Then double-map the DVMA addresses */
70 nseg = (DVMA_MAPLEN + NBPSG - 1) >> SGSHIFT;
71 while (nseg-- > 0) {
72 setsegmap(dmava, getsegmap(segva));
73 segva += NBPSG;
74 dmava += NBPSG;
79 * Convert a local address to a DVMA address.
81 char *
82 dvma_mapin(char *addr, size_t len)
84 int va = (int)addr;
86 va -= base_va;
88 #ifndef BOOTXX
89 /* Make sure the address is in the DVMA map. */
90 if (va < 0 || va >= DVMA_MAPLEN)
91 panic("dvma_mapin: va %x (DMA base %x)", va+base_va, base_va);
92 #endif
94 va += DVMA_BASE;
96 return ((char *)va);
100 * Convert a DVMA address to a local address.
102 char *
103 dvma_mapout(char *addr, size_t len)
105 int va = (int)addr;
107 va -= DVMA_BASE;
109 #ifndef BOOTXX
110 /* Make sure the address is in the DVMA map. */
111 if (va < 0 || va >= DVMA_MAPLEN)
112 panic("dvma_mapout: va %x (DMA base %x)", va+base_va, base_va);
113 #endif
115 va += base_va;
117 return ((char *)va);
120 char *
121 dvma_alloc(int len)
123 char *mem;
125 mem = alloc(len);
126 if (mem == NULL)
127 return (mem);
128 return (dvma_mapin(mem, len));
131 void
132 dvma_free(char *dvma, int len)
134 char *mem;
136 mem = dvma_mapout(dvma, len);
137 if (mem != NULL)
138 dealloc(mem, len);