Home | History | Annotate | Line # | Download | only in hpcboot
      1  1.8  martin /* -*-C++-*-	$NetBSD: memory.h,v 1.8 2008/04/28 20:23:20 martin 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  *
     19  1.1     uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1     uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1     uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1     uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1     uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1     uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1     uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1     uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1     uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1     uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1     uch  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1     uch  */
     31  1.1     uch 
     32  1.1     uch #ifndef _HPCBOOT_MEMORY_H_
     33  1.5     uch #define	_HPCBOOT_MEMORY_H_
     34  1.1     uch 
     35  1.1     uch #undef MEMORY_MAP_DEBUG		// super verbose.
     36  1.1     uch 
     37  1.1     uch #include <hpcboot.h>
     38  1.1     uch class Console;
     39  1.1     uch 
     40  1.1     uch template <class T>
     41  1.1     uch inline T
     42  1.1     uch ROUND(const T v, const T x) {
     43  1.1     uch 	return((v + x - 1) / x) * x;
     44  1.1     uch }
     45  1.1     uch 
     46  1.1     uch template <class T>
     47  1.1     uch inline T
     48  1.1     uch TRUNC(const T v, const T x) {
     49  1.1     uch 	return(v / x) * x;
     50  1.1     uch }
     51  1.1     uch 
     52  1.5     uch #define	MAX_MEM_BANK	16
     53  1.1     uch class MemoryManager {
     54  1.1     uch private:
     55  1.1     uch 	struct bank {
     56  1.1     uch 		paddr_t addr;
     57  1.1     uch 		psize_t size;
     58  1.1     uch 	};
     59  1.1     uch 	// Windows CE application virtual memory size
     60  1.1     uch 	enum { WCE_VMEM_MAX = 32 * 1024 * 1024 };
     61  1.1     uch 	// Windows CE VirtualAlloc() boundary
     62  1.1     uch 	enum { WCE_REGION_SIZE = 64 * 1024 };
     63  1.1     uch 
     64  1.1     uch protected:
     65  1.1     uch 	// Console options.
     66  1.1     uch 	Console *&_cons;
     67  1.1     uch 	BOOL _debug;
     68  1.5     uch 
     69  1.1     uch 	enum { BLOCK_SIZE = WCE_REGION_SIZE * 64 }; // 4MByte
     70  1.1     uch 
     71  1.1     uch 	// Pagesize, D-RAM bank
     72  1.1     uch 	vsize_t _page_size;
     73  1.1     uch 	int _page_shift;
     74  1.1     uch 	int _page_per_region;
     75  1.1     uch 	struct bank _bank[MAX_MEM_BANK];
     76  1.1     uch 	int _nbank;
     77  1.1     uch 
     78  1.1     uch 	// Allocated memory
     79  1.1     uch 	vaddr_t _memory;
     80  1.1     uch 
     81  1.1     uch 	// Virtual <-> Physical table
     82  1.5     uch 	int _naddr_table;
     83  1.1     uch 	struct AddressTranslationTable {
     84  1.1     uch 		vaddr_t vaddr;
     85  1.1     uch 		paddr_t paddr;
     86  1.1     uch 	}
     87  1.1     uch 	*_addr_table;
     88  1.1     uch 	int _addr_table_idx;
     89  1.1     uch 
     90  1.1     uch public:
     91  1.1     uch 	vsize_t getPageSize(void) { return _page_size; }
     92  1.1     uch 
     93  1.1     uch 	vsize_t getTaggedPageSize(void)
     94  1.1     uch 		{ return _page_size - sizeof(struct PageTag); }
     95  1.1     uch 	vsize_t estimateTaggedPageSize(vsize_t sz) {
     96  1.1     uch 		vsize_t tsz = getTaggedPageSize();
     97  1.4     uch 		return ((sz + tsz - 1) / tsz) * _page_size;
     98  1.1     uch 	}
     99  1.7     uwe 	uint32_t roundPage(uint32_t v) { return ROUND(v, _page_size); }
    100  1.7     uwe 	uint32_t truncPage(uint32_t v) { return TRUNC(v, _page_size); }
    101  1.7     uwe 	uint32_t roundRegion(uint32_t v)
    102  1.7     uwe 		{ return ROUND(v, uint32_t(WCE_REGION_SIZE)); }
    103  1.7     uwe 	uint32_t truncRegion(uint32_t v)
    104  1.7     uwe 		{ return TRUNC(v, uint32_t(WCE_REGION_SIZE)); }
    105  1.1     uch 
    106  1.1     uch 	// Physical address ops.
    107  1.7     uwe 	vaddr_t mapPhysicalPage(paddr_t paddr, psize_t size, uint32_t flags);
    108  1.1     uch 	void unmapPhysicalPage(vaddr_t vaddr);
    109  1.7     uwe 	uint32_t readPhysical4(paddr_t paddr);
    110  1.1     uch 	// return physical address of coresspoing virtual address.
    111  1.1     uch 	virtual paddr_t searchPage(vaddr_t vaddr) = 0;
    112  1.1     uch 
    113  1.1     uch 	MemoryManager(Console *&cons, size_t pagesize);
    114  1.1     uch 	virtual ~MemoryManager(void);
    115  1.1     uch 	BOOL &setDebug(void) { return _debug; }
    116  1.1     uch 	virtual BOOL init(void) { return TRUE; }
    117  1.1     uch 
    118  1.1     uch 	// initialize page pool
    119  1.1     uch 	BOOL reservePage(vsize_t size, BOOL page_commit = FALSE);
    120  1.1     uch 	// register D-RAM banks
    121  1.1     uch 	void loadBank(paddr_t paddr, psize_t psize);
    122  1.1     uch 	// get 1 page from high address of pool.
    123  1.1     uch 	BOOL getPage(vaddr_t &vaddr, paddr_t &paddr);
    124  1.1     uch 	// get tagged page from low address of pool.
    125  1.1     uch 	BOOL getTaggedPage(vaddr_t &vaddr, paddr_t &paddr);
    126  1.1     uch 	BOOL getTaggedPage(vaddr_t &v, paddr_t &p, struct PageTag **pvec,
    127  1.3     uch 	    paddr_t &pvec_paddr);
    128  1.1     uch };
    129  1.1     uch 
    130  1.1     uch //
    131  1.1     uch //	Use LockPages()
    132  1.1     uch //
    133  1.1     uch class MemoryManager_LockPages : public MemoryManager {
    134  1.1     uch private:
    135  1.1     uch 	BOOL(*_lock_pages)(LPVOID, DWORD, PDWORD, int);
    136  1.1     uch 	BOOL(*_unlock_pages)(LPVOID, DWORD);
    137  1.1     uch 	int _shift;
    138  1.1     uch 
    139  1.1     uch public:
    140  1.1     uch 	MemoryManager_LockPages(BOOL(*)(LPVOID, DWORD, PDWORD, int),
    141  1.3     uch 	    BOOL(*)(LPVOID, DWORD), Console *&, size_t, int = 0);
    142  1.1     uch 	virtual ~MemoryManager_LockPages(void);
    143  1.1     uch 	paddr_t searchPage(vaddr_t vaddr);
    144  1.1     uch };
    145  1.1     uch 
    146  1.1     uch //
    147  1.1     uch //	Use VirtualCopy()
    148  1.1     uch //
    149  1.1     uch class MemoryManager_VirtualCopy : public MemoryManager {
    150  1.1     uch private:
    151  1.2     uch 	// search guess
    152  1.2     uch 	paddr_t _search_guess;
    153  1.5     uch 
    154  1.1     uch 	// Memory marker
    155  1.7     uwe 	uint32_t _magic0, _magic1;
    156  1.7     uwe 	volatile uint32_t *_magic_addr;
    157  1.1     uch 	enum {
    158  1.5     uch 		MAGIC_CHECK_DONE	= 0xac1dcafe,
    159  1.1     uch 		MAGIC_CHECK_DUMMY	= 0xa5a5a5a5
    160  1.1     uch 	};
    161  1.1     uch 	void setMagic(vaddr_t v) {
    162  1.7     uwe 		_magic_addr =(uint32_t *)v;
    163  1.1     uch 		while ((_magic0 = Random()) == MAGIC_CHECK_DONE)
    164  1.1     uch 			;
    165  1.1     uch 		while ((_magic1 = Random()) == MAGIC_CHECK_DONE)
    166  1.1     uch 			;
    167  1.1     uch 		_magic_addr[0] = _magic0;
    168  1.1     uch 		_magic_addr[1] = _magic1;
    169  1.1     uch 	}
    170  1.1     uch 	BOOL checkMagic(vaddr_t v) {
    171  1.7     uwe 		volatile uint32_t *marker =(uint32_t *)v;
    172  1.5     uch 		return (marker[0] == _magic0) && (marker[1] == _magic1);
    173  1.1     uch 	}
    174  1.1     uch 	void clearMagic(void) {
    175  1.1     uch 		_magic_addr[0] = MAGIC_CHECK_DONE;
    176  1.1     uch 		_magic_addr[1] = MAGIC_CHECK_DONE;
    177  1.1     uch 	}
    178  1.1     uch 	vaddr_t checkMagicRegion(vaddr_t start, vsize_t size,
    179  1.3     uch 	    vsize_t step = 8) {
    180  1.1     uch 		for (vaddr_t ref = start; ref < start + size; ref += step)
    181  1.1     uch 			if (checkMagic(ref))
    182  1.1     uch 				return ref - start;
    183  1.1     uch 		return ~0;
    184  1.1     uch 	}
    185  1.1     uch 	paddr_t searchBank(int banknum);
    186  1.1     uch 
    187  1.1     uch public:
    188  1.1     uch 	MemoryManager_VirtualCopy(Console *&, size_t);
    189  1.1     uch 	virtual ~MemoryManager_VirtualCopy(void);
    190  1.1     uch 	paddr_t searchPage(vaddr_t vaddr);
    191  1.1     uch };
    192  1.1     uch 
    193  1.1     uch #endif // _HPCBOOT_MEMORY_H_
    194