os/kernelhwsrv/kernel/eka/include/memmodel/epoc/mmubase/ramcache.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/include/memmodel/epoc/mmubase/ramcache.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,252 @@
     1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32\include\memmodel\epoc\mmubase\ramcache.h
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#ifndef RAMCACHE_H
    1.22 +#define RAMCACHE_H
    1.23 +
    1.24 +#ifndef __MEMMODEL_FLEXIBLE__
    1.25 +#include <memmodel/epoc/mmubase/mmubase.h>
    1.26 +#else
    1.27 +class MmuBase;
    1.28 +#endif
    1.29 +
    1.30 +/**
    1.31 +Base class for the ram caching or demand paging implementation.
    1.32 +
    1.33 +This provides an interface between class Mmu and the implementation
    1.34 +of any form of dynamic use of the system's free memory, e.g. demand paging.
    1.35 +The chief functionality of this interface is for transferring ownership of
    1.36 +physical pages of RAM.
    1.37 +
    1.38 +@internalComponent
    1.39 +*/
    1.40 +class RamCacheBase
    1.41 +	{
    1.42 +public:
    1.43 +	/**
    1.44 +	Constructor
    1.45 +	*/
    1.46 +	RamCacheBase();
    1.47 +
    1.48 +	/**
    1.49 +	Intialisation called during MmuBase:Init2.
    1.50 +	*/
    1.51 +	virtual void Init2();
    1.52 +
    1.53 +	/**
    1.54 +	Initialisation called from M::DemandPagingInit.
    1.55 +	*/
    1.56 +	virtual TInt Init3()=0;
    1.57 +
    1.58 +	/**
    1.59 +	Remove RAM pages from the cache and return them to the system's free pool.
    1.60 +	(Free them.)
    1.61 +
    1.62 +	This is called by MmuBase when it requires more free RAM to meet an
    1.63 +	allocation request.
    1.64 +
    1.65 +	@param	aNumPages The number of pages to free up.
    1.66 +	@return	True if all pages could be freed, false otherwise
    1.67 +	@pre	RamAlloc mutex held.
    1.68 +	*/
    1.69 +	virtual TBool GetFreePages(TInt aNumPages)=0;
    1.70 +
    1.71 +	/**
    1.72 +	Give a RAM page to the cache system for managing.
    1.73 +	This page of RAM may be reused for any purpose.
    1.74 +	If the page has already been donated then no action is taken.
    1.75 +
    1.76 +	@param aPageInfo The page info for the donated page.
    1.77 +
    1.78 +	@see ReclaimRamCachePage.
    1.79 +
    1.80 +	@pre System Lock held
    1.81 +	@post System Lock left unchanged.
    1.82 +	*/
    1.83 +	virtual void DonateRamCachePage(SPageInfo* aPageInfo)=0;
    1.84 +
    1.85 +	/**
    1.86 +	Attempt to reclaim a RAM page given to the cache system with #DonateRamCachePage.
    1.87 +	If the RAM page has not been reused for other purposes then the page is
    1.88 +	removed from the cache system's management.
    1.89 +	If the page has not previousely been donated then no action is taken.
    1.90 +
    1.91 +	@param aPageInfo The page info for the page to reclaim.
    1.92 +
    1.93 +	@return True if page successfuly reclaimed, false otherwise.
    1.94 +
    1.95 +	@pre System Lock held
    1.96 +	@post System Lock left unchanged.
    1.97 +	*/
    1.98 +	virtual TBool ReclaimRamCachePage(SPageInfo* aPageInfo)=0;
    1.99 +
   1.100 +	/**
   1.101 +	Called by MMU class when a page is unmapped from a chunk.
   1.102 +
   1.103 +	@param aPageInfo The page info for the page being unmapped.
   1.104 +
   1.105 +	@return True is cache system doesn't claim ownership of this page, false if it does.
   1.106 +	*/
   1.107 +	virtual TBool PageUnmapped(SPageInfo* aPageInfo)=0;
   1.108 +
   1.109 +	/**
   1.110 +	Check whether the specified page can be discarded by the RAM cache.
   1.111 +
   1.112 +	@param aPageInfo The page info of the page being queried.
   1.113 +	@return ETrue when the page can be discarded, EFalse otherwise.
   1.114 +	@pre System lock held.
   1.115 +	@post System lock held.
   1.116 +	*/
   1.117 +	virtual TBool IsPageDiscardable(SPageInfo& aPageInfo) = 0;
   1.118 +
   1.119 +	/**
   1.120 +	Discard the specified page.
   1.121 +	Should only be called on a page if a previous call to IsPageDiscardable()
   1.122 +	returned ETrue and the system lock hasn't been released between the calls.
   1.123 +	If necessary a new page may be allocated to replace the one being
   1.124 +	discarded, however the new page should not be allocated into the RAM
   1.125 +	zone of ID==aBlockedZoneId.
   1.126 +	
   1.127 +	@param aPageInfo The page info of the page to be discarded
   1.128 +	@param aBlockZoneId The ID of the RAM zone that shouldn't be allocated into.
   1.129 +	@param aBlockRest Set to ETrue to stop allocation as soon as aBlockedZoneId is reached 
   1.130 +	in preference ordering.  EFalse otherwise.
   1.131 +	@return ETrue if the page could be discarded, EFalse otherwise.
   1.132 +	
   1.133 +	@pre System lock held.
   1.134 +	@post System lock held.
   1.135 +	*/
   1.136 +	virtual TBool DoDiscardPage(SPageInfo& aPageInfo, TUint aBlockedZoneId, TBool aBlockRest) = 0;
   1.137 +
   1.138 +
   1.139 +	/**
   1.140 +	First stage in discarding a list of pages.
   1.141 +
   1.142 +	Must ensure that the pages will still be discardable even if system lock 
   1.143 +	is released after this method has completed.
   1.144 +	To be used in conjunction with RamCacheBase::DoDiscardPages1().
   1.145 +
   1.146 +	@param aPageList A NULL terminated list of the pages to be discarded
   1.147 +	@return KErrNone on success.
   1.148 +
   1.149 +	@pre System lock held
   1.150 +	@post System lock held
   1.151 +	*/
   1.152 +	virtual TInt DoDiscardPages0(SPageInfo** aPageList) = 0;
   1.153 +
   1.154 +
   1.155 +	/**
   1.156 +	Final stage in discarding a list of page
   1.157 +	Finish discarding the pages previously removed by RamCacheBase::DoDiscardPages0().
   1.158 +
   1.159 +	@param aPageList A NULL terminated list of the pages to be discarded
   1.160 +	@return KErrNone on success.
   1.161 +
   1.162 +	@pre System lock held
   1.163 +	@post System lock held
   1.164 +	*/
   1.165 +	virtual TInt DoDiscardPages1(SPageInfo** aPageList) = 0;
   1.166 +
   1.167 +
   1.168 +	/**
   1.169 +	Flush (unmap) all the free memory which is currently cached.
   1.170 +	*/
   1.171 +	virtual void FlushAll() = 0;
   1.172 +
   1.173 +	/**
   1.174 +	Attempt to allocate the required contiguous region of pages, freeing
   1.175 +	RAM cache pages if required.
   1.176 +
   1.177 +	@param	aNumPages The number of pages to free up.
   1.178 +	@param	aAlign The alignment of the region to free-up, (a power-of-2).
   1.179 +	@param aBlockZoneId The ID of a zone that shouldn't be allocated into, when set to 
   1.180 +	KRamZoneInvalidId it will have no effect.
   1.181 +	@param aBlockRest Set to ETrue to stop allocation as soon as aBlockedZoneId is reached 
   1.182 +	in preference ordering.  EFalse otherwise.
   1.183 +
   1.184 +	@return	KErrNone on success, KErrNoMemory otherwise
   1.185 +	@pre	RamAlloc mutex held.
   1.186 +	*/
   1.187 +	TInt AllocFreeContiguousPages(TInt aNumPages, TInt aAlign, TZonePageType aType, TPhysAddr& aPhysAddr, TUint aBlockedZoneId, TBool aBlockRest);
   1.188 +
   1.189 +	/**
   1.190 +	Return the maximum number of RAM pages which could be obtained with #GetFreePages.
   1.191 +	This value is used in the calculation of the 'free' RAM in the system.
   1.192 +
   1.193 +	@return Number of free RAM pages.
   1.194 +	*/
   1.195 +	inline TInt NumberOfFreePages() { return iNumberOfFreePages; }
   1.196 +
   1.197 +	/**
   1.198 +	Put a page back on the system's free pool.
   1.199 +
   1.200 +	@pre RamAlloc mutex held.
   1.201 +	*/
   1.202 +	void ReturnToSystem(SPageInfo* aPageInfo);
   1.203 +
   1.204 +	/**
   1.205 +	Get a RAM page from the system's free pool.
   1.206 +	
   1.207 +	@param aBlockZoneId The ID of a zone that shouldn't be allocated into, when set to 
   1.208 +	KRamZoneInvalidId it will have no effect.
   1.209 +	@param aBlockRest Set to ETrue to stop allocation as soon as aBlockedZoneId is reached 
   1.210 +	in preference ordering.  EFalse otherwise.
   1.211 +
   1.212 + 	@pre	RamAlloc mutex held.
   1.213 +
   1.214 +	@return The page or NULL if no page is available.
   1.215 +	*/
   1.216 +	SPageInfo* GetPageFromSystem(TUint aBlockedZone=KRamZoneInvalidId, TBool aBlockRest=EFalse);
   1.217 +
   1.218 +
   1.219 +
   1.220 +public:
   1.221 +	MmuBase* iMmu;				/**< Copy of MmuBase::TheMmu */
   1.222 +	TInt iNumberOfFreePages;	/**< Number of pages which could be freed by GetFreePages.
   1.223 +									 This value is protected by the RamAlloc mutex. */
   1.224 +	static RamCacheBase* TheRamCache;
   1.225 +	};
   1.226 +
   1.227 +
   1.228 +class RamCache : public RamCacheBase
   1.229 +	{
   1.230 +public:
   1.231 +	// from RamCacheBase
   1.232 +	virtual void Init2();
   1.233 +	virtual TInt Init3();
   1.234 +	virtual TBool GetFreePages(TInt aNumPages);
   1.235 +	virtual void DonateRamCachePage(SPageInfo* aPageInfo);
   1.236 +	virtual TBool ReclaimRamCachePage(SPageInfo* aPageInfo);
   1.237 +	virtual TBool PageUnmapped(SPageInfo* aPageInfo);
   1.238 +	virtual TBool IsPageDiscardable(SPageInfo& aPageInfo);
   1.239 +	virtual TBool DoDiscardPage(SPageInfo& aPageInfo, TUint aBlockZoneId, TBool aBlockRest);
   1.240 +	virtual TInt DoDiscardPages0(SPageInfo** aPageList);
   1.241 +	virtual TInt DoDiscardPages1(SPageInfo** aPageList);
   1.242 +	virtual void FlushAll();
   1.243 +	// new
   1.244 +	virtual void SetFree(SPageInfo* aPageInfo);
   1.245 +	enum TFault
   1.246 +		{
   1.247 +		EUnexpectedPageType = 3,	/**< A page in the live page list had an unexpected type (SPageInfo::Attribs) */
   1.248 +		};
   1.249 +	static void Panic(TFault aFault);
   1.250 +	void RemovePage(SPageInfo& aPageInfo);
   1.251 +private:
   1.252 +	SDblQue iPageList;
   1.253 +	};
   1.254 +
   1.255 +#endif