os/kernelhwsrv/kernel/eka/drivers/paging/emulated/emulated_rom_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 DEmulatedRomPagingDevice : 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
	static void ReadTimerCallback(TAny* aSem);
sl@0
    30
	static void ReadDfcCallback(TAny* aReq);
sl@0
    31
	void ReadDfc();
sl@0
    32
private:
sl@0
    33
	TLinAddr iRomStore;
sl@0
    34
sl@0
    35
	struct TReadRequest
sl@0
    36
		{
sl@0
    37
		TLinAddr iBuffer;
sl@0
    38
		TLinAddr iOffset;
sl@0
    39
		TUint	 iSize;
sl@0
    40
		NFastSemaphore* iSemaphore;
sl@0
    41
		TInt iResult;
sl@0
    42
		};
sl@0
    43
sl@0
    44
	TReadRequest iReadRequest;
sl@0
    45
	TDfcQue* iDfcQue;
sl@0
    46
	TInt iAccumulatedDelay;
sl@0
    47
	TInt iReadPageDelay;
sl@0
    48
	TInt iReadPageCPUDelay;
sl@0
    49
	DMutex* iMutex;
sl@0
    50
	};
sl@0
    51
sl@0
    52
sl@0
    53
TInt DEmulatedRomPagingDevice::Install()
sl@0
    54
	{
sl@0
    55
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf(">DEmulatedRomPagingDevice::Install"));
sl@0
    56
	TInt r;
sl@0
    57
	DEmulatedRomPagingDevice* romDevice = new DEmulatedRomPagingDevice;
sl@0
    58
	if(!romDevice)
sl@0
    59
		r = KErrNoMemory;
sl@0
    60
	else
sl@0
    61
		{
sl@0
    62
		r = romDevice->Construct();
sl@0
    63
		if(r==KErrNone)
sl@0
    64
			Kern::InstallPagingDevice(romDevice);
sl@0
    65
		else
sl@0
    66
			delete romDevice;
sl@0
    67
		}
sl@0
    68
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("<DEmulatedRomPagingDevice::Install returns %d",r));
sl@0
    69
	return r;
sl@0
    70
	}
sl@0
    71
sl@0
    72
sl@0
    73
TInt DEmulatedRomPagingDevice::Construct()
sl@0
    74
	{
sl@0
    75
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf(">DEmulatedRomPagingDevice::Construct"));
sl@0
    76
sl@0
    77
	// Initialise DPagingDevice base class
sl@0
    78
	iType = ERom;
sl@0
    79
	iReadUnitShift = 9; // 512 byte units
sl@0
    80
	iName = "EmulatedRomPagingDevice";
sl@0
    81
sl@0
    82
	// Get info about ROM
sl@0
    83
	TPhysAddr* romPages;
sl@0
    84
	TInt numRomPages;
sl@0
    85
	TInt r=Kern::HalFunction(EHalGroupVM,EVMHalGetOriginalRomPages,&romPages,&numRomPages);
sl@0
    86
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("DEmulatedRomPagingDevice::Construct numRomPages=%08x",numRomPages));
sl@0
    87
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
    88
sl@0
    89
	r = Kern::MutexCreate(iMutex, _L("EmulRomPageDev"), KMutexOrdNone);
sl@0
    90
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
    91
	// Create a shared chunk to map the ROM pages
sl@0
    92
	TInt pageSize = Kern::RoundToPageSize(1);
sl@0
    93
	TChunkCreateInfo info;
sl@0
    94
	info.iType = TChunkCreateInfo::ESharedKernelSingle;
sl@0
    95
	info.iMaxSize = numRomPages*pageSize;
sl@0
    96
	info.iMapAttr = EMapAttrFullyBlocking;
sl@0
    97
	info.iOwnsMemory = EFalse;
sl@0
    98
	DChunk* chunk;
sl@0
    99
	TUint32 mapAttr;
sl@0
   100
	r = Kern::ChunkCreate(info,chunk,iRomStore,mapAttr);
sl@0
   101
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   102
	r = Kern::ChunkCommitPhysical(chunk,0,numRomPages*pageSize,romPages);
sl@0
   103
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   104
sl@0
   105
	// create DFC thread for NAND read simulation
sl@0
   106
	_LIT8(KDemandPagingDelay,"DemandPagingDelay");
sl@0
   107
	r = Kern::DfcQCreate(iDfcQue,24,&KDemandPagingDelay); // DFC thread of same priority as NAND driver
sl@0
   108
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   109
sl@0
   110
	// setup delays used for simulation
sl@0
   111
	SDemandPagingConfig config = Epoc::RomHeader().iDemandPagingConfig;
sl@0
   112
	iReadPageDelay = config.iSpare[0];
sl@0
   113
	iReadPageCPUDelay = config.iSpare[1];
sl@0
   114
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("DEmulatedRomPagingDevice::Construct emulated delays=%d,%d",iReadPageDelay,iReadPageCPUDelay));
sl@0
   115
sl@0
   116
	__KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("<DEmulatedRomPagingDevice::Construct"));
sl@0
   117
	return KErrNone;
sl@0
   118
	}
sl@0
   119
sl@0
   120
sl@0
   121
TInt DEmulatedRomPagingDevice::Read(TThreadMessage* /*aReq*/,TLinAddr aBuffer,TUint aOffset,TUint aSize,TInt)
sl@0
   122
	{
sl@0
   123
	aOffset <<= iReadUnitShift;
sl@0
   124
	aSize <<= iReadUnitShift;
sl@0
   125
sl@0
   126
	if(iReadPageCPUDelay==0 || KDebugNum(KTESTFAST))
sl@0
   127
		{
sl@0
   128
		// don't do emulated NAND delay, just copy it immediately...
sl@0
   129
		memcpy((TAny*)aBuffer,(TAny*)(iRomStore+aOffset),aSize);
sl@0
   130
		return KErrNone;
sl@0
   131
		}
sl@0
   132
	
sl@0
   133
	// make sure we are single threaded when we use the DFC.
sl@0
   134
	Kern::MutexWait(*iMutex);
sl@0
   135
	
sl@0
   136
	// get a DFC to do the simulated read
sl@0
   137
	NFastSemaphore sem;
sl@0
   138
	NKern::FSSetOwner(&sem,NULL);
sl@0
   139
	iReadRequest.iBuffer = aBuffer;
sl@0
   140
	iReadRequest.iOffset = aOffset;
sl@0
   141
	iReadRequest.iSize = aSize;
sl@0
   142
	iReadRequest.iSemaphore = &sem;
sl@0
   143
	TDfc dfc(ReadDfcCallback,this,iDfcQue,0);
sl@0
   144
	dfc.Enque();
sl@0
   145
	NKern::FSWait(&sem);
sl@0
   146
	TInt result = iReadRequest.iResult;
sl@0
   147
	
sl@0
   148
	// let any other threads have a go.
sl@0
   149
	Kern::MutexSignal(*iMutex);
sl@0
   150
	return result;
sl@0
   151
	}
sl@0
   152
sl@0
   153
sl@0
   154
void DEmulatedRomPagingDevice::ReadTimerCallback(TAny* aSem)
sl@0
   155
	{
sl@0
   156
	NKern::FSSignal((NFastSemaphore*)aSem);
sl@0
   157
	}
sl@0
   158
sl@0
   159
sl@0
   160
void DEmulatedRomPagingDevice::ReadDfcCallback(TAny* aSelf)
sl@0
   161
	{
sl@0
   162
	((DEmulatedRomPagingDevice*)aSelf)->ReadDfc();
sl@0
   163
	}
sl@0
   164
sl@0
   165
sl@0
   166
void DEmulatedRomPagingDevice::ReadDfc()
sl@0
   167
	{
sl@0
   168
	// calculate number of ticks to stall to emulate elapsed time for page read request
sl@0
   169
	iAccumulatedDelay += iReadPageDelay;
sl@0
   170
	TInt tick = NKern::TickPeriod();
sl@0
   171
	TInt delay = 0;
sl@0
   172
	if(iAccumulatedDelay>tick)
sl@0
   173
		{
sl@0
   174
		delay = iAccumulatedDelay/tick;
sl@0
   175
		iAccumulatedDelay -= delay*tick+(tick>>1);
sl@0
   176
		}
sl@0
   177
	NFastSemaphore sem;
sl@0
   178
	NKern::FSSetOwner(&sem,NULL);
sl@0
   179
	NTimer timer(ReadTimerCallback,&sem);
sl@0
   180
	if(delay)
sl@0
   181
		timer.OneShot(delay,ETrue);
sl@0
   182
sl@0
   183
	// emulate CPU load for processing read
sl@0
   184
	Kern::NanoWait(iReadPageCPUDelay*1000);
sl@0
   185
sl@0
   186
	// get data using memcpy
sl@0
   187
	memcpy((TAny*)iReadRequest.iBuffer,(TAny*)(iRomStore+iReadRequest.iOffset),iReadRequest.iSize);
sl@0
   188
sl@0
   189
	// wait for delay timer
sl@0
   190
	if(delay)
sl@0
   191
		NKern::FSWait(&sem);
sl@0
   192
sl@0
   193
	// signal done
sl@0
   194
	iReadRequest.iResult = KErrNone;
sl@0
   195
	NKern::FSSignal(iReadRequest.iSemaphore);
sl@0
   196
	}
sl@0
   197
sl@0
   198
sl@0
   199
DECLARE_STANDARD_EXTENSION()
sl@0
   200
	{
sl@0
   201
	return DEmulatedRomPagingDevice::Install();
sl@0
   202
	}