memory.h revision 1.5 1 1.5 uch /* -*-C++-*- $NetBSD: memory.h,v 1.5 2004/08/06 18:33:09 uch Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch * 3. All advertising materials mentioning features or use of this software
19 1.1 uch * must display the following acknowledgement:
20 1.1 uch * This product includes software developed by the NetBSD
21 1.1 uch * Foundation, Inc. and its contributors.
22 1.1 uch * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 uch * contributors may be used to endorse or promote products derived
24 1.1 uch * from this software without specific prior written permission.
25 1.1 uch *
26 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
37 1.1 uch */
38 1.1 uch
39 1.1 uch #ifndef _HPCBOOT_MEMORY_H_
40 1.5 uch #define _HPCBOOT_MEMORY_H_
41 1.1 uch
42 1.1 uch #undef MEMORY_MAP_DEBUG // super verbose.
43 1.1 uch
44 1.1 uch #include <hpcboot.h>
45 1.1 uch class Console;
46 1.1 uch
47 1.1 uch template <class T>
48 1.1 uch inline T
49 1.1 uch ROUND(const T v, const T x) {
50 1.1 uch return((v + x - 1) / x) * x;
51 1.1 uch }
52 1.1 uch
53 1.1 uch template <class T>
54 1.1 uch inline T
55 1.1 uch TRUNC(const T v, const T x) {
56 1.1 uch return(v / x) * x;
57 1.1 uch }
58 1.1 uch
59 1.5 uch #define MAX_MEM_BANK 16
60 1.1 uch class MemoryManager {
61 1.1 uch private:
62 1.1 uch struct bank {
63 1.1 uch paddr_t addr;
64 1.1 uch psize_t size;
65 1.1 uch };
66 1.1 uch // Windows CE application virtual memory size
67 1.1 uch enum { WCE_VMEM_MAX = 32 * 1024 * 1024 };
68 1.1 uch // Windows CE VirtualAlloc() boundary
69 1.1 uch enum { WCE_REGION_SIZE = 64 * 1024 };
70 1.1 uch
71 1.1 uch protected:
72 1.1 uch // Console options.
73 1.1 uch Console *&_cons;
74 1.1 uch BOOL _debug;
75 1.5 uch
76 1.1 uch enum { BLOCK_SIZE = WCE_REGION_SIZE * 64 }; // 4MByte
77 1.1 uch
78 1.1 uch // Pagesize, D-RAM bank
79 1.1 uch vsize_t _page_size;
80 1.1 uch int _page_shift;
81 1.1 uch int _page_per_region;
82 1.1 uch struct bank _bank[MAX_MEM_BANK];
83 1.1 uch int _nbank;
84 1.1 uch
85 1.1 uch // Allocated memory
86 1.1 uch vaddr_t _memory;
87 1.1 uch
88 1.1 uch // Virtual <-> Physical table
89 1.5 uch int _naddr_table;
90 1.1 uch struct AddressTranslationTable {
91 1.1 uch vaddr_t vaddr;
92 1.1 uch paddr_t paddr;
93 1.1 uch }
94 1.1 uch *_addr_table;
95 1.1 uch int _addr_table_idx;
96 1.1 uch
97 1.1 uch public:
98 1.1 uch vsize_t getPageSize(void) { return _page_size; }
99 1.1 uch
100 1.1 uch vsize_t getTaggedPageSize(void)
101 1.1 uch { return _page_size - sizeof(struct PageTag); }
102 1.1 uch vsize_t estimateTaggedPageSize(vsize_t sz) {
103 1.1 uch vsize_t tsz = getTaggedPageSize();
104 1.4 uch return ((sz + tsz - 1) / tsz) * _page_size;
105 1.1 uch }
106 1.1 uch u_int32_t roundPage(u_int32_t v) { return ROUND(v, _page_size); }
107 1.1 uch u_int32_t truncPage(u_int32_t v) { return TRUNC(v, _page_size); }
108 1.1 uch u_int32_t roundRegion(u_int32_t v)
109 1.1 uch { return ROUND(v, u_int32_t(WCE_REGION_SIZE)); }
110 1.1 uch u_int32_t truncRegion(u_int32_t v)
111 1.1 uch { return TRUNC(v, u_int32_t(WCE_REGION_SIZE)); }
112 1.1 uch
113 1.1 uch // Physical address ops.
114 1.1 uch vaddr_t mapPhysicalPage(paddr_t paddr, psize_t size, u_int32_t flags);
115 1.1 uch void unmapPhysicalPage(vaddr_t vaddr);
116 1.1 uch u_int32_t readPhysical4(paddr_t paddr);
117 1.1 uch // return physical address of coresspoing virtual address.
118 1.1 uch virtual paddr_t searchPage(vaddr_t vaddr) = 0;
119 1.1 uch
120 1.1 uch MemoryManager(Console *&cons, size_t pagesize);
121 1.1 uch virtual ~MemoryManager(void);
122 1.1 uch BOOL &setDebug(void) { return _debug; }
123 1.1 uch virtual BOOL init(void) { return TRUE; }
124 1.1 uch
125 1.1 uch // initialize page pool
126 1.1 uch BOOL reservePage(vsize_t size, BOOL page_commit = FALSE);
127 1.1 uch // register D-RAM banks
128 1.1 uch void loadBank(paddr_t paddr, psize_t psize);
129 1.1 uch // get 1 page from high address of pool.
130 1.1 uch BOOL getPage(vaddr_t &vaddr, paddr_t &paddr);
131 1.1 uch // get tagged page from low address of pool.
132 1.1 uch BOOL getTaggedPage(vaddr_t &vaddr, paddr_t &paddr);
133 1.1 uch BOOL getTaggedPage(vaddr_t &v, paddr_t &p, struct PageTag **pvec,
134 1.3 uch paddr_t &pvec_paddr);
135 1.1 uch };
136 1.1 uch
137 1.1 uch //
138 1.1 uch // Use LockPages()
139 1.1 uch //
140 1.1 uch class MemoryManager_LockPages : public MemoryManager {
141 1.1 uch private:
142 1.1 uch BOOL(*_lock_pages)(LPVOID, DWORD, PDWORD, int);
143 1.1 uch BOOL(*_unlock_pages)(LPVOID, DWORD);
144 1.1 uch int _shift;
145 1.1 uch
146 1.1 uch public:
147 1.1 uch MemoryManager_LockPages(BOOL(*)(LPVOID, DWORD, PDWORD, int),
148 1.3 uch BOOL(*)(LPVOID, DWORD), Console *&, size_t, int = 0);
149 1.1 uch virtual ~MemoryManager_LockPages(void);
150 1.1 uch paddr_t searchPage(vaddr_t vaddr);
151 1.1 uch };
152 1.1 uch
153 1.1 uch //
154 1.1 uch // Use VirtualCopy()
155 1.1 uch //
156 1.1 uch class MemoryManager_VirtualCopy : public MemoryManager {
157 1.1 uch private:
158 1.2 uch // search guess
159 1.2 uch paddr_t _search_guess;
160 1.5 uch
161 1.1 uch // Memory marker
162 1.1 uch u_int32_t _magic0, _magic1;
163 1.1 uch volatile u_int32_t *_magic_addr;
164 1.1 uch enum {
165 1.5 uch MAGIC_CHECK_DONE = 0xac1dcafe,
166 1.1 uch MAGIC_CHECK_DUMMY = 0xa5a5a5a5
167 1.1 uch };
168 1.1 uch void setMagic(vaddr_t v) {
169 1.1 uch _magic_addr =(u_int32_t *)v;
170 1.1 uch while ((_magic0 = Random()) == MAGIC_CHECK_DONE)
171 1.1 uch ;
172 1.1 uch while ((_magic1 = Random()) == MAGIC_CHECK_DONE)
173 1.1 uch ;
174 1.1 uch _magic_addr[0] = _magic0;
175 1.1 uch _magic_addr[1] = _magic1;
176 1.1 uch }
177 1.1 uch BOOL checkMagic(vaddr_t v) {
178 1.1 uch volatile u_int32_t *marker =(u_int32_t *)v;
179 1.5 uch return (marker[0] == _magic0) && (marker[1] == _magic1);
180 1.1 uch }
181 1.1 uch void clearMagic(void) {
182 1.1 uch _magic_addr[0] = MAGIC_CHECK_DONE;
183 1.1 uch _magic_addr[1] = MAGIC_CHECK_DONE;
184 1.1 uch }
185 1.1 uch vaddr_t checkMagicRegion(vaddr_t start, vsize_t size,
186 1.3 uch vsize_t step = 8) {
187 1.1 uch for (vaddr_t ref = start; ref < start + size; ref += step)
188 1.1 uch if (checkMagic(ref))
189 1.1 uch return ref - start;
190 1.1 uch return ~0;
191 1.1 uch }
192 1.1 uch paddr_t searchBank(int banknum);
193 1.1 uch
194 1.1 uch public:
195 1.1 uch MemoryManager_VirtualCopy(Console *&, size_t);
196 1.1 uch virtual ~MemoryManager_VirtualCopy(void);
197 1.1 uch paddr_t searchPage(vaddr_t vaddr);
198 1.1 uch };
199 1.1 uch
200 1.1 uch #endif // _HPCBOOT_MEMORY_H_
201