xref: /netbsd-src/sys/arch/hpc/stand/hpcboot/memory.cpp (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: memory.cpp,v 1.4 2001/05/16 08:37:44 enami Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <memory.h>
40 #include <console.h>
41 
42 MemoryManager::MemoryManager(Console *&cons, size_t pagesize)
43 	: _cons(cons)
44 {
45 	_debug = FALSE;
46 	_page_size =pagesize;
47 
48 	int mask = _page_size;
49 	for (_page_shift = 0; !(mask & 1); _page_shift++)
50 		mask >>= 1;
51 
52 	_page_per_region = WCE_REGION_SIZE / _page_size;
53 	_nbank = 0;
54 	DPRINTF((TEXT("Page size %dbyte %dpages/region\n"),
55 	    _page_size , _page_per_region));
56 	_addr_table_idx = 0;
57 	_addr_table = 0;
58 	_memory = 0;
59 }
60 
61 MemoryManager::~MemoryManager(void)
62 {
63 	if (_memory)
64 		VirtualFree(LPVOID(_memory), 0, MEM_RELEASE);
65 }
66 
67 void
68 MemoryManager::loadBank(paddr_t paddr, psize_t psize)
69 {
70 	struct MemoryManager::bank *b = &_bank[_nbank++];
71 	b->addr = paddr;
72 	b->size = psize;
73 	DPRINTF((TEXT("Bank#%d 0x%08x size 0x%08x\n"), _nbank - 1,
74 	    b->addr, b->size));
75 }
76 
77 BOOL
78 MemoryManager::reservePage(vsize_t size, BOOL page_commit)
79 {
80 	// My virtual memory space
81 	vaddr_t vbase;
82 	vsize_t vsize;
83 
84 	int i, npage;
85 
86 	if (size == 0)
87 		return FALSE;
88 
89   // reserve all virtual memory.
90 	vsize = roundRegion(size);
91 	npage = roundPage(size) / _page_size;
92 
93 	size_t tabsz = sizeof(struct AddressTranslationTable) * npage;
94 	_addr_table = static_cast <struct AddressTranslationTable *>
95 	    (malloc(tabsz));
96 	if (_addr_table == NULL) {
97 		DPRINTF((TEXT("can't allocate memory for translation table.\n")));
98 		return FALSE;
99 	}
100 	DPRINTF((TEXT("address translation table %d pages.(%d byte)\n"), npage,
101 	    tabsz));
102 
103 	if (page_commit)
104 		vbase = vaddr_t(VirtualAlloc(0, vsize, MEM_RESERVE,
105 		    PAGE_NOACCESS));
106 	else
107 		vbase = vaddr_t(VirtualAlloc(0, vsize, MEM_COMMIT,
108 		    PAGE_READWRITE | PAGE_NOCACHE));
109 
110 	if (vbase == 0) {
111 		DPRINTF((TEXT("can't allocate memory\n")));
112 		return FALSE;
113 	}
114 	_memory = vbase;
115 
116   // find physical address of allocated page.
117 	AddressTranslationTable *tab = _addr_table;
118 	_naddr_table = 0;
119 	for (i = 0; i < npage; i++) {
120 		vaddr_t vaddr;
121 		paddr_t paddr = ~0;
122 
123 		if (page_commit)
124 			// now map to physical page.
125 			vaddr = vaddr_t(VirtualAlloc(
126 				LPVOID(vbase + _page_size * i),
127 				_page_size, MEM_COMMIT,
128 				PAGE_READWRITE | PAGE_NOCACHE));
129 		else
130 			vaddr = vbase + _page_size * i;
131 
132 		paddr = searchPage(vaddr);
133 
134 		if (paddr == ~0) {
135 			DPRINTF((TEXT("page#%d not found\n"), i));
136 			break;
137 		} else {
138 #ifdef MEMORY_MAP_DEBUG
139 			DPRINTF((TEXT("page %d vaddr=0x%08x paddr=0x%08x\n"),
140 			    _naddr_table, vaddr, paddr));
141 #endif
142 			tab->vaddr = vaddr;
143 			tab->paddr = paddr;
144 			++tab;
145 			++_naddr_table;
146 		}
147 	}
148 
149 #ifdef MEMORY_MAP_DEBUG
150 	// dump virtual <-> physical address table
151 	tab = _addr_table;
152 	for (i = 0; i < _naddr_table;) {
153 		for (int j = 0; j < 4; j++, i++, tab++)
154 			DPRINTF((TEXT("%08x=%08x "), tab->vaddr, tab->paddr));
155 		DPRINTF((TEXT("\n")));
156 	}
157 #endif
158 	DPRINTF((TEXT("allocated %d page. mapped %d page.\n"), npage,
159 	    _naddr_table));
160 
161 	return TRUE;
162 }
163 
164 BOOL
165 MemoryManager::getPage(vaddr_t &vaddr, paddr_t &paddr)
166 {
167 	/* get plain page from the top */
168 	if (_addr_table_idx >= _naddr_table ||
169 	    _addr_table == NULL)
170 		return FALSE;
171 
172 	int idx = --_naddr_table;
173 
174 	AddressTranslationTable *tab = &_addr_table[idx];
175 	vaddr = tab->vaddr;
176 	paddr = tab->paddr;
177 
178 	return TRUE;
179 }
180 
181 BOOL
182 MemoryManager::getTaggedPage(vaddr_t &vaddr, paddr_t &paddr)
183 {
184 	/* get tagged page from the bottom */
185 	if (_addr_table_idx >= _naddr_table ||
186 	    _addr_table == NULL) {
187 		DPRINTF((TEXT("page insufficient.\n")));
188 		return FALSE;
189 	}
190 	AddressTranslationTable *tab =
191 	    &_addr_table[_addr_table_idx++];
192 	vaddr = tab->vaddr;
193 	paddr = tab->paddr;
194 
195 	return TRUE;
196 }
197 
198 BOOL
199 MemoryManager::getTaggedPage(vaddr_t &v, paddr_t &p,
200     struct PageTag **pvec, paddr_t &pvec_paddr)
201 {
202 	if (!getTaggedPage(v, p))
203 		return FALSE;
204 
205 	*pvec =(struct PageTag *)v;
206 	memset(*pvec, 0, sizeof(struct PageTag));
207 	v += sizeof(struct PageTag);
208 	pvec_paddr = p;
209 	p += sizeof(struct PageTag);
210 
211 	return TRUE;
212 }
213 
214 vaddr_t
215 MemoryManager::mapPhysicalPage(paddr_t paddr, psize_t size, u_int32_t flags)
216 {
217 	paddr_t pstart = truncPage(paddr);
218 	paddr_t pend = roundPage(paddr + size);
219 	psize_t psize = pend - pstart;
220 
221 	LPVOID p = VirtualAlloc(0, psize, MEM_RESERVE, PAGE_NOACCESS);
222 
223 	int ok = VirtualCopy(p, LPVOID(pstart >> 8), psize,
224 	    flags | PAGE_NOCACHE | PAGE_PHYSICAL);
225 	if (!ok) {
226 		DPRINTF((TEXT("can't map physical address 0x%08x\n"), paddr));
227 		return ~0;
228 	}
229 #if 0
230 	DPRINTF((TEXT("start=0x%08x end=0x%08x size=0x%08x return=0x%08x\n"),
231 	    pstart, pend, psize, vaddr_t(p) + vaddr_t(paddr - pstart)));
232 
233 #endif
234 
235 	return vaddr_t(p) + vaddr_t(paddr - pstart);
236 }
237 
238 void
239 MemoryManager::unmapPhysicalPage(vaddr_t vaddr)
240 {
241 	int ok = VirtualFree(LPVOID(truncPage(vaddr)), 0, MEM_RELEASE);
242 	if (!ok)
243 		DPRINTF((TEXT("can't release memory\n")));
244 }
245 
246 u_int32_t
247 MemoryManager::readPhysical4(paddr_t paddr)
248 {
249 	vaddr_t v = mapPhysicalPage(paddr, 4, PAGE_READONLY);
250 	u_int32_t val = *(u_int32_t *)v;
251 	unmapPhysicalPage(v);
252 	return val;
253 }
254 
255 //
256 //	Use LockPages()
257 //
258 MemoryManager_LockPages::MemoryManager_LockPages
259 (BOOL(*lock_pages)(LPVOID, DWORD, PDWORD, int),
260     BOOL(*unlock_pages)(LPVOID, DWORD),
261     Console *&cons, size_t pagesize, int shift)
262 	:  MemoryManager(cons, pagesize)
263 {
264 	_lock_pages	= lock_pages;
265 	_unlock_pages	= unlock_pages;
266 	_shift = shift;
267 	DPRINTF((TEXT("use LockPages method.\n")));
268 }
269 
270 MemoryManager_LockPages::~MemoryManager_LockPages(void)
271 {
272 }
273 
274 paddr_t
275 MemoryManager_LockPages::searchPage(vaddr_t vaddr)
276 {
277 	paddr_t paddr = ~0;
278 
279 	if (!_lock_pages(LPVOID(vaddr), _page_size, PDWORD(&paddr), 1))
280 		return paddr;
281 
282 	if (!_unlock_pages(LPVOID(vaddr), _page_size)) {
283 		DPRINTF((TEXT("can't unlock pages\n")));
284 	}
285 
286 	return(paddr >>(_page_shift - _shift)) << _page_shift;
287 }
288 
289 //
290 //	Use VirtualCopy()
291 //
292 MemoryManager_VirtualCopy::MemoryManager_VirtualCopy(Console *&cons,
293     size_t pagesize)
294 	: MemoryManager(cons, pagesize)
295 {
296 	_search_guess = 0;
297 	DPRINTF((TEXT("use VirtualCopy method.\n")));
298 }
299 
300 MemoryManager_VirtualCopy::~MemoryManager_VirtualCopy(void)
301 {
302 }
303 
304 paddr_t
305 MemoryManager_VirtualCopy::searchPage(vaddr_t vaddr)
306 {
307 	paddr_t paddr = ~0;
308 	int i;
309 
310 	// search all D-RAM bank.
311 	setMagic(vaddr);
312  retry:
313 	for (i = 0; i < _nbank; i++) {
314 		paddr = searchBank(i);
315 		if (paddr != ~0)
316 			break;
317 	}
318 	if (_search_guess != 0 && paddr == ~0) {
319 		_search_guess = 0;
320 		goto retry;
321 	}
322 
323 	clearMagic();
324 
325 	return paddr;
326 }
327 
328 paddr_t
329 MemoryManager_VirtualCopy::searchBank(int banknum)
330 {
331 	LPVOID ref;
332 	paddr_t paddr, pstart, pend, pfound = ~0;
333 	paddr_t bstart, bend;
334 	vaddr_t ofs;
335 
336 	bstart = _bank[banknum].addr;
337 	bend = _bank[banknum].addr + _bank[banknum].size;
338 
339 	pstart = _search_guess ? _search_guess : bstart;
340 	pend = bend;
341 
342 	if (pstart < bstart || pstart >= pend)
343 		return pfound;
344 
345 	// reserve physical reference region
346 	ref = VirtualAlloc(0, BLOCK_SIZE, MEM_RESERVE, PAGE_NOACCESS);
347 	if (ref == 0) {
348 		DPRINTF((TEXT("can't allocate virtual memory.\n")));
349 		return pfound;
350 	}
351 
352 	for (paddr = pstart; paddr < pend; paddr += BLOCK_SIZE) {
353 		if (!VirtualCopy(ref, LPVOID(paddr >> 8), BLOCK_SIZE,
354 		    PAGE_READONLY | PAGE_NOCACHE | PAGE_PHYSICAL)) {
355 			DPRINTF((TEXT("can't map physical addr 0x%08x(->0x%08x)\n"),
356 			    ref, paddr));
357 			goto release;
358 		}
359 
360 		// search magic in this region.
361 		ofs = checkMagicRegion(vaddr_t(ref), BLOCK_SIZE, _page_size);
362 
363 		// decommit reference region.
364 		if (!VirtualFree(ref, BLOCK_SIZE, MEM_DECOMMIT)) {
365 			DPRINTF((TEXT("can't decommit addr 0x%08x(->0x%08x)\n"),
366 			    ref, paddr));
367 			goto release;
368 		}
369 
370 		if (ofs != ~0) {
371 			pfound = paddr + ofs;
372 			_search_guess = paddr;
373 			break;
374 		}
375 	}
376  release:
377 	if (!VirtualFree(ref, 0, MEM_RELEASE))
378 		DPRINTF((TEXT("can't release memory\n")));
379 
380 	return pfound;
381 }
382