os/kernelhwsrv/kernel/eka/drivers/paging/emulated/emulated_data_paging.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32/drivers/paging/emulated/emulated_rom_paging.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <kernel/kern_priv.h>
sl@0
    19
#include <kernel/kernel.h>
sl@0
    20
#include <memmodel/epoc/platform.h>
sl@0
    21
sl@0
    22
class DEmulatedDataPagingDevice : public DPagingDevice
sl@0
    23
	{
sl@0
    24
public:
sl@0
    25
	static TInt Install();
sl@0
    26
private:
sl@0
    27
	TInt Construct();
sl@0
    28
	TInt Read(TThreadMessage* aReq,TLinAddr aBuffer,TUint aOffset,TUint aSize,TInt);
sl@0
    29
	TInt Write(TThreadMessage* aReq,TLinAddr aBuffer,TUint aOffset,TUint aSize,TBool aBackground);
sl@0
    30
sl@0
    31
	static void ReadTimerCallback(TAny* aSem);
sl@0
    32
	static void ReadDfcCallback(TAny* aReq);
sl@0
    33
	void ReadDfc();
sl@0
    34
sl@0
    35
	static void WriteTimerCallback(TAny* aSem);
sl@0
    36
	static void WriteDfcCallback(TAny* aReq);
sl@0
    37
	void WriteDfc();
sl@0
    38
private:
sl@0
    39
	TLinAddr iDataStore;
sl@0
    40
	TDfcQue* iDfcQue;
sl@0
    41
	DMutex* iMutex;
sl@0
    42
sl@0
    43
	struct TDataRequest
sl@0
    44
		{
sl@0
    45
		TLinAddr iBuffer;
sl@0
    46
		TLinAddr iOffset;
sl@0
    47
		TUint	 iSize;
sl@0
    48
		NFastSemaphore* iSemaphore;
sl@0
    49
		TInt iResult;
sl@0
    50
		};
sl@0
    51
sl@0
    52
	TDataRequest iReadRequest;
sl@0
    53
	TInt iAccumulatedReadDelay;
sl@0
    54
	TInt iReadPageDelay;
sl@0
    55
	TInt iReadPageCPUDelay;
sl@0
    56
sl@0
    57
	TDataRequest iWriteRequest;
sl@0
    58
	TInt iAccumulatedWriteDelay;
sl@0
    59
	TInt iWritePageDelay;
sl@0
    60
	TInt iWritePageCPUDelay;
sl@0
    61
	};
sl@0
    62
sl@0
    63
sl@0
    64
TInt DEmulatedDataPagingDevice::Install()
sl@0
    65
	{
sl@0
    66
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf(">DEmulatedDataPagingDevice::Install"));
sl@0
    67
	TInt r;
sl@0
    68
	DEmulatedDataPagingDevice* dataDevice = new DEmulatedDataPagingDevice;
sl@0
    69
	if(!dataDevice)
sl@0
    70
		r = KErrNoMemory;
sl@0
    71
	else
sl@0
    72
		{
sl@0
    73
		r = dataDevice->Construct();
sl@0
    74
		if(r==KErrNone)
sl@0
    75
			Kern::InstallPagingDevice(dataDevice);
sl@0
    76
		else
sl@0
    77
			delete dataDevice;
sl@0
    78
		}
sl@0
    79
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("<DEmulatedDataPagingDevice::Install returns %d",r));
sl@0
    80
	return r;
sl@0
    81
	}
sl@0
    82
sl@0
    83
sl@0
    84
TInt DEmulatedDataPagingDevice::Construct()
sl@0
    85
	{
sl@0
    86
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf(">DEmulatedDataPagingDevice::Construct"));
sl@0
    87
sl@0
    88
	// Initialise DPagingDevice base class
sl@0
    89
	iType = EData;
sl@0
    90
	iReadUnitShift = 9; // 512 byte units
sl@0
    91
	iName = "EmulatedDataPagingDevice";
sl@0
    92
sl@0
    93
	// Calculate swap size
sl@0
    94
	TInt free = Kern::FreeRamInBytes()>>20; // megabytes free
sl@0
    95
	TInt swapSize = free/2;
sl@0
    96
	if(swapSize>256)
sl@0
    97
		swapSize = 256;
sl@0
    98
	swapSize <<= 20;
sl@0
    99
	iSwapSize = swapSize>>iReadUnitShift;
sl@0
   100
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("DEmulatedDataPagingDevice::Construct swap size 0x%x",swapSize));
sl@0
   101
sl@0
   102
	TInt r = Kern::MutexCreate(iMutex, _L("EmulDataPageDev"), KMutexOrdNone);
sl@0
   103
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   104
	// Create a shared chunk to map the ROM pages
sl@0
   105
	TChunkCreateInfo info;
sl@0
   106
	info.iType = TChunkCreateInfo::ESharedKernelSingle;
sl@0
   107
	info.iMaxSize = swapSize;
sl@0
   108
	info.iMapAttr = EMapAttrFullyBlocking;
sl@0
   109
	info.iOwnsMemory = ETrue;
sl@0
   110
	DChunk* chunk;
sl@0
   111
	TUint32 mapAttr;
sl@0
   112
	r = Kern::ChunkCreate(info,chunk,iDataStore,mapAttr);
sl@0
   113
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   114
	r = Kern::ChunkCommit(chunk,0,swapSize);
sl@0
   115
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   116
sl@0
   117
	// create DFC thread for NAND read simulation
sl@0
   118
	_LIT8(KDemandPagingDelay,"DataPagingDelay");
sl@0
   119
	r = Kern::DfcQCreate(iDfcQue,24,&KDemandPagingDelay); // DFC thread of same priority as NAND driver
sl@0
   120
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   121
sl@0
   122
	// setup delays used for simulation
sl@0
   123
	SDemandPagingConfig config = Epoc::RomHeader().iDemandPagingConfig;
sl@0
   124
	iReadPageDelay = config.iSpare[0];
sl@0
   125
	iReadPageCPUDelay = config.iSpare[1];
sl@0
   126
	iWritePageDelay = config.iSpare[0]*2;
sl@0
   127
	iWritePageCPUDelay = config.iSpare[1];
sl@0
   128
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("DEmulatedDataPagingDevice::Construct emulated delays=%d,%d",iReadPageDelay,iReadPageCPUDelay));
sl@0
   129
sl@0
   130
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("<DEmulatedDataPagingDevice::Construct"));
sl@0
   131
	return KErrNone;
sl@0
   132
	}
sl@0
   133
sl@0
   134
sl@0
   135
TInt DEmulatedDataPagingDevice::Read(TThreadMessage* /*aReq*/,TLinAddr aBuffer,TUint aOffset,TUint aSize,TInt)
sl@0
   136
	{
sl@0
   137
	aOffset <<= iReadUnitShift;
sl@0
   138
	aSize <<= iReadUnitShift;
sl@0
   139
sl@0
   140
	if(iReadPageCPUDelay==0 || KDebugNum(KTESTFAST))
sl@0
   141
		{
sl@0
   142
		// don't do emulated NAND delay, just copy it immediately...
sl@0
   143
		memcpy((TAny*)aBuffer,(TAny*)(iDataStore+aOffset),aSize);
sl@0
   144
		return KErrNone;
sl@0
   145
		}
sl@0
   146
	
sl@0
   147
	// make sure we are single threaded when we use the DFC.
sl@0
   148
	Kern::MutexWait(*iMutex);
sl@0
   149
	
sl@0
   150
	// get a DFC to do the simulated read
sl@0
   151
	NFastSemaphore sem;
sl@0
   152
	NKern::FSSetOwner(&sem,NULL);
sl@0
   153
	iReadRequest.iBuffer = aBuffer;
sl@0
   154
	iReadRequest.iOffset = aOffset;
sl@0
   155
	iReadRequest.iSize = aSize;
sl@0
   156
	iReadRequest.iSemaphore = &sem;
sl@0
   157
	TDfc dfc(ReadDfcCallback,this,iDfcQue,0);
sl@0
   158
	dfc.Enque();
sl@0
   159
	NKern::FSWait(&sem);
sl@0
   160
	TInt result = iReadRequest.iResult;
sl@0
   161
	
sl@0
   162
	// let any other threads have a go.
sl@0
   163
	Kern::MutexSignal(*iMutex);
sl@0
   164
	return result;
sl@0
   165
	}
sl@0
   166
sl@0
   167
sl@0
   168
void DEmulatedDataPagingDevice::ReadTimerCallback(TAny* aSem)
sl@0
   169
	{
sl@0
   170
	NKern::FSSignal((NFastSemaphore*)aSem);
sl@0
   171
	}
sl@0
   172
sl@0
   173
sl@0
   174
void DEmulatedDataPagingDevice::ReadDfcCallback(TAny* aSelf)
sl@0
   175
	{
sl@0
   176
	((DEmulatedDataPagingDevice*)aSelf)->ReadDfc();
sl@0
   177
	}
sl@0
   178
sl@0
   179
sl@0
   180
void DEmulatedDataPagingDevice::ReadDfc()
sl@0
   181
	{
sl@0
   182
	// calculate number of ticks to stall to emulate elapsed time for page read request
sl@0
   183
	iAccumulatedReadDelay += iReadPageDelay;
sl@0
   184
	TInt tick = NKern::TickPeriod();
sl@0
   185
	TInt delay = 0;
sl@0
   186
	if(iAccumulatedReadDelay>tick)
sl@0
   187
		{
sl@0
   188
		delay = iAccumulatedReadDelay/tick;
sl@0
   189
		iAccumulatedReadDelay -= delay*tick+(tick>>1);
sl@0
   190
		}
sl@0
   191
	NFastSemaphore sem;
sl@0
   192
	NKern::FSSetOwner(&sem,NULL);
sl@0
   193
	NTimer timer(ReadTimerCallback,&sem);
sl@0
   194
	if(delay)
sl@0
   195
		timer.OneShot(delay,ETrue);
sl@0
   196
sl@0
   197
	// emulate CPU load for processing read
sl@0
   198
	Kern::NanoWait(iReadPageCPUDelay*1000);
sl@0
   199
sl@0
   200
	// get data using memcpy
sl@0
   201
	memcpy((TAny*)iReadRequest.iBuffer,(TAny*)(iDataStore+iReadRequest.iOffset),iReadRequest.iSize);
sl@0
   202
sl@0
   203
	// wait for delay timer
sl@0
   204
	if(delay)
sl@0
   205
		NKern::FSWait(&sem);
sl@0
   206
sl@0
   207
	// signal done
sl@0
   208
	iReadRequest.iResult = KErrNone;
sl@0
   209
	NKern::FSSignal(iReadRequest.iSemaphore);
sl@0
   210
	}
sl@0
   211
sl@0
   212
sl@0
   213
TInt DEmulatedDataPagingDevice::Write(TThreadMessage* /*aReq*/,TLinAddr aBuffer,TUint aOffset,TUint aSize,TBool aBackground)
sl@0
   214
	{
sl@0
   215
	aOffset <<= iReadUnitShift;
sl@0
   216
	aSize <<= iReadUnitShift;
sl@0
   217
sl@0
   218
	if(iWritePageCPUDelay==0 || KDebugNum(KTESTFAST))
sl@0
   219
		{
sl@0
   220
		// don't do emulated NAND delay, just copy it immediately...
sl@0
   221
		memcpy((TAny*)(iDataStore+aOffset),(TAny*)aBuffer,aSize);
sl@0
   222
		return KErrNone;
sl@0
   223
		}
sl@0
   224
	
sl@0
   225
	// make sure we are single threaded when we use the DFC.
sl@0
   226
	Kern::MutexWait(*iMutex);
sl@0
   227
	
sl@0
   228
	// get a DFC to do the simulated write
sl@0
   229
	NFastSemaphore sem;
sl@0
   230
	NKern::FSSetOwner(&sem,NULL);
sl@0
   231
	iWriteRequest.iBuffer = aBuffer;
sl@0
   232
	iWriteRequest.iOffset = aOffset;
sl@0
   233
	iWriteRequest.iSize = aSize;
sl@0
   234
	iWriteRequest.iSemaphore = &sem;
sl@0
   235
	TDfc dfc(WriteDfcCallback,this,iDfcQue,0);
sl@0
   236
	dfc.Enque();
sl@0
   237
	NKern::FSWait(&sem);
sl@0
   238
	TInt result = iWriteRequest.iResult;
sl@0
   239
	
sl@0
   240
	// let any other threads have a go.
sl@0
   241
	Kern::MutexSignal(*iMutex);
sl@0
   242
	return result;
sl@0
   243
	}
sl@0
   244
sl@0
   245
sl@0
   246
void DEmulatedDataPagingDevice::WriteTimerCallback(TAny* aSem)
sl@0
   247
	{
sl@0
   248
	NKern::FSSignal((NFastSemaphore*)aSem);
sl@0
   249
	}
sl@0
   250
sl@0
   251
sl@0
   252
void DEmulatedDataPagingDevice::WriteDfcCallback(TAny* aSelf)
sl@0
   253
	{
sl@0
   254
	((DEmulatedDataPagingDevice*)aSelf)->WriteDfc();
sl@0
   255
	}
sl@0
   256
sl@0
   257
sl@0
   258
void DEmulatedDataPagingDevice::WriteDfc()
sl@0
   259
	{
sl@0
   260
	// calculate number of ticks to stall to emulate elapsed time for page read request
sl@0
   261
	iAccumulatedWriteDelay += iWritePageDelay;
sl@0
   262
	TInt tick = NKern::TickPeriod();
sl@0
   263
	TInt delay = 0;
sl@0
   264
	if(iAccumulatedWriteDelay>tick)
sl@0
   265
		{
sl@0
   266
		delay = iAccumulatedWriteDelay/tick;
sl@0
   267
		iAccumulatedWriteDelay -= delay*tick+(tick>>1);
sl@0
   268
		}
sl@0
   269
	NFastSemaphore sem;
sl@0
   270
	NKern::FSSetOwner(&sem,NULL);
sl@0
   271
	NTimer timer(WriteTimerCallback,&sem);
sl@0
   272
	if(delay)
sl@0
   273
		timer.OneShot(delay,ETrue);
sl@0
   274
sl@0
   275
	// emulate CPU load for processing read
sl@0
   276
	Kern::NanoWait(iWritePageCPUDelay*1000);
sl@0
   277
sl@0
   278
	// write data using memcpy
sl@0
   279
	memcpy((TAny*)(iDataStore+iWriteRequest.iOffset),(TAny*)iWriteRequest.iBuffer,iWriteRequest.iSize);
sl@0
   280
sl@0
   281
	// wait for delay timer
sl@0
   282
	if(delay)
sl@0
   283
		NKern::FSWait(&sem);
sl@0
   284
sl@0
   285
	// signal done
sl@0
   286
	iWriteRequest.iResult = KErrNone;
sl@0
   287
	NKern::FSSignal(iWriteRequest.iSemaphore);
sl@0
   288
	}
sl@0
   289
sl@0
   290
sl@0
   291
DECLARE_STANDARD_EXTENSION()
sl@0
   292
	{
sl@0
   293
	return DEmulatedDataPagingDevice::Install();
sl@0
   294
	}