Home | History | Annotate | Line # | Download | only in hpcboot
memory.cpp revision 1.2
      1 /*	$NetBSD: memory.cpp,v 1.2 2001/03/25 17:14:53 uch 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 	int idx = --_naddr_table;
    169 
    170 	if (idx < 0 || _addr_table == NULL)
    171 		return FALSE;
    172 
    173 	AddressTranslationTable *tab = &_addr_table[idx];
    174 	vaddr = tab->vaddr;
    175 	paddr = tab->paddr;
    176 
    177 	return TRUE;
    178 }
    179 
    180 BOOL
    181 MemoryManager::getTaggedPage(vaddr_t &vaddr, paddr_t &paddr)
    182 {
    183 	/* get tagged page from the bottom */
    184 	if (_addr_table_idx >= _naddr_table ||
    185 	    _addr_table == NULL) {
    186 		DPRINTF((TEXT("page insufficient.\n")));
    187 		return FALSE;
    188 	}
    189 	AddressTranslationTable *tab =
    190 		&_addr_table[_addr_table_idx++];
    191 	vaddr = tab->vaddr;
    192 	paddr = tab->paddr;
    193 
    194 	return TRUE;
    195 }
    196 
    197 BOOL
    198 MemoryManager::getTaggedPage(vaddr_t &v, paddr_t &p,
    199 			     struct PageTag **pvec, paddr_t &pvec_paddr)
    200 {
    201 	if (!getTaggedPage(v, p))
    202 		return FALSE;
    203 
    204 	*pvec =(struct PageTag *)v;
    205 	memset(*pvec, 0, sizeof(struct PageTag));
    206 	v += sizeof(struct PageTag);
    207 	pvec_paddr = p;
    208 	p += sizeof(struct PageTag);
    209 
    210 	return TRUE;
    211 }
    212 
    213 vaddr_t
    214 MemoryManager::mapPhysicalPage(paddr_t paddr, psize_t size, u_int32_t flags)
    215 {
    216 	paddr_t pstart = truncPage(paddr);
    217 	paddr_t pend = roundPage(paddr + size);
    218 	psize_t psize = pend - pstart;
    219 
    220 	LPVOID p = VirtualAlloc(0, psize, MEM_RESERVE, PAGE_NOACCESS);
    221 
    222 	int ok = VirtualCopy(p, LPVOID(pstart >> 8), psize,
    223 			     flags | PAGE_NOCACHE | PAGE_PHYSICAL);
    224 	if (!ok) {
    225 		DPRINTF((TEXT("can't map physical address 0x%08x\n"), paddr));
    226 		return ~0;
    227 	}
    228 #if 0
    229 	DPRINTF((TEXT("start=0x%08x end=0x%08x size=0x%08x return=0x%08x\n"),
    230 		 pstart, pend, psize, vaddr_t(p) + vaddr_t(paddr - pstart)));
    231 
    232 #endif
    233 
    234 	return vaddr_t(p) + vaddr_t(paddr - pstart);
    235 }
    236 
    237 void
    238 MemoryManager::unmapPhysicalPage(vaddr_t vaddr)
    239 {
    240 	int ok = VirtualFree(LPVOID(truncPage(vaddr)), 0, MEM_RELEASE);
    241 	if (!ok)
    242 		DPRINTF((TEXT("can't release memory\n")));
    243 }
    244 
    245 u_int32_t
    246 MemoryManager::readPhysical4(paddr_t paddr)
    247 {
    248 	vaddr_t v = mapPhysicalPage(paddr, 4, PAGE_READONLY);
    249 	u_int32_t val = *(u_int32_t *)v;
    250 	unmapPhysicalPage(v);
    251 	return val;
    252 }
    253 
    254 //
    255 //	Use LockPages()
    256 //
    257 MemoryManager_LockPages::MemoryManager_LockPages
    258 (BOOL(*lock_pages)(LPVOID, DWORD, PDWORD, int),
    259  BOOL(*unlock_pages)(LPVOID, DWORD),
    260  Console *&cons, size_t pagesize, int shift)
    261 	:  MemoryManager(cons, pagesize)
    262 {
    263 	_lock_pages	= lock_pages;
    264 	_unlock_pages	= unlock_pages;
    265 	_shift = shift;
    266 	DPRINTF((TEXT("use LockPages method.\n")));
    267 }
    268 
    269 MemoryManager_LockPages::~MemoryManager_LockPages(void)
    270 {
    271 }
    272 
    273 paddr_t
    274 MemoryManager_LockPages::searchPage(vaddr_t vaddr)
    275 {
    276 	paddr_t paddr = ~0;
    277 
    278 	if (!_lock_pages(LPVOID(vaddr), _page_size, PDWORD(&paddr), 1))
    279 		return paddr;
    280 
    281 	if (!_unlock_pages(LPVOID(vaddr), _page_size)) {
    282 		DPRINTF((TEXT("can't unlock pages\n")));
    283 	}
    284 
    285 	return(paddr >>(_page_shift - _shift)) << _page_shift;
    286 }
    287 
    288 //
    289 //	Use VirtualCopy()
    290 //
    291 MemoryManager_VirtualCopy::MemoryManager_VirtualCopy(Console *&cons,
    292 						     size_t pagesize)
    293 	: MemoryManager(cons, pagesize)
    294 {
    295 	_search_guess = 0;
    296 	DPRINTF((TEXT("use VirtualCopy method.\n")));
    297 }
    298 
    299 MemoryManager_VirtualCopy::~MemoryManager_VirtualCopy(void)
    300 {
    301 }
    302 
    303 paddr_t
    304 MemoryManager_VirtualCopy::searchPage(vaddr_t vaddr)
    305 {
    306 	paddr_t paddr = ~0;
    307 	int i;
    308 
    309 	// search all D-RAM bank.
    310 	setMagic(vaddr);
    311  retry:
    312 	for (i = 0; i < _nbank; i++) {
    313 		paddr = searchBank(i);
    314 		if (paddr != ~0)
    315 			break;
    316 	}
    317 	if (_search_guess != 0 && paddr == ~0) {
    318 		_search_guess = 0;
    319 		goto retry;
    320 	}
    321 
    322 	clearMagic();
    323 
    324 	return paddr;
    325 }
    326 
    327 paddr_t
    328 MemoryManager_VirtualCopy::searchBank(int banknum)
    329 {
    330 	LPVOID ref;
    331 	paddr_t paddr, pstart, pend, pfound = ~0;
    332 	paddr_t bstart, bend;
    333 	vaddr_t ofs;
    334 
    335 	bstart = _bank[banknum].addr;
    336 	bend = _bank[banknum].addr + _bank[banknum].size;
    337 
    338 	pstart = _search_guess ? _search_guess : bstart;
    339 	pend = bend;
    340 
    341 	if (pstart < bstart || pstart >= pend)
    342 		return pfound;
    343 
    344 	// reserve physical reference region
    345 	ref = VirtualAlloc(0, BLOCK_SIZE, MEM_RESERVE, PAGE_NOACCESS);
    346 	if (ref == 0) {
    347 		DPRINTF((TEXT("can't allocate virtual memory.\n")));
    348 		return pfound;
    349 	}
    350 
    351 	for (paddr = pstart; paddr < pend; paddr += BLOCK_SIZE) {
    352 		if (!VirtualCopy(ref, LPVOID(paddr >> 8), BLOCK_SIZE,
    353 				 PAGE_READONLY | PAGE_NOCACHE |
    354 				 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