1 /* $NetBSD: dvma.c,v 1.15 2009/10/21 23:12:09 snj Exp $ */
2 /*
3 * Copyright (c) 1995 Gordon W. Ross
4 * All rights reserved.
5 *
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.
14 *
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.
25 */
26
27 /*
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'
33 */
34
35 #include <sys/param.h>
36 #include <sparc/sparc/asm.h>
37 #include <machine/pte.h>
38 #include <machine/ctlreg.h>
39
40 #include <lib/libsa/stand.h>
41 #include <sparc/stand/common/promdev.h>
42
43 #define DVMA_BASE 0xFFF00000
44 #define DVMA_MAPLEN 0xE0000 /* 1 MB - 128K (save MONSHORTSEG) */
45
46 static int base_va;
47
48 /*
49 * This module is only used on sun4, so:
50 */
51 #define getsegmap(va) (lduha(va, ASI_SEGMAP))
52 #define setsegmap(va, pmeg) do stha(va, ASI_SEGMAP, pmeg); while(0)
53
54 void
dvma_init(void)55 dvma_init(void)
56 {
57 u_int segva, dmava;
58 int nseg;
59 extern int start;
60
61 /*
62 * Align our address base with the DVMA segment.
63 * Allocate one DVMA segment to cover the stack, which
64 * grows downward from `start'.
65 */
66 dmava = DVMA_BASE;
67 base_va = segva = (((int)&start) & -NBPSG) - NBPSG;
68
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;
75 }
76 }
77
78 /*
79 * Convert a local address to a DVMA address.
80 */
81 char *
dvma_mapin(char * addr,size_t len)82 dvma_mapin(char *addr, size_t len)
83 {
84 int va = (int)addr;
85
86 va -= base_va;
87
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
93
94 va += DVMA_BASE;
95
96 return ((char *)va);
97 }
98
99 /*
100 * Convert a DVMA address to a local address.
101 */
102 char *
dvma_mapout(char * addr,size_t len)103 dvma_mapout(char *addr, size_t len)
104 {
105 int va = (int)addr;
106
107 va -= DVMA_BASE;
108
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
114
115 va += base_va;
116
117 return ((char *)va);
118 }
119
120 char *
dvma_alloc(int len)121 dvma_alloc(int len)
122 {
123 char *mem;
124
125 mem = alloc(len);
126 if (mem == NULL)
127 return (mem);
128 return (dvma_mapin(mem, len));
129 }
130
131 void
dvma_free(char * dvma,int len)132 dvma_free(char *dvma, int len)
133 {
134 char *mem;
135
136 mem = dvma_mapout(dvma, len);
137 if (mem != NULL)
138 dealloc(mem, len);
139 }
140