Update contrib.
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32/drivers/paging/emulated/emulated_rom_paging.cpp
18 #include <kernel/kern_priv.h>
19 #include <kernel/kernel.h>
20 #include <memmodel/epoc/platform.h>
22 class DEmulatedRomPagingDevice : public DPagingDevice
25 static TInt Install();
28 TInt Read(TThreadMessage* aReq,TLinAddr aBuffer,TUint aOffset,TUint aSize,TInt);
29 static void ReadTimerCallback(TAny* aSem);
30 static void ReadDfcCallback(TAny* aReq);
40 NFastSemaphore* iSemaphore;
44 TReadRequest iReadRequest;
46 TInt iAccumulatedDelay;
48 TInt iReadPageCPUDelay;
53 TInt DEmulatedRomPagingDevice::Install()
55 __KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf(">DEmulatedRomPagingDevice::Install"));
57 DEmulatedRomPagingDevice* romDevice = new DEmulatedRomPagingDevice;
62 r = romDevice->Construct();
64 Kern::InstallPagingDevice(romDevice);
68 __KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("<DEmulatedRomPagingDevice::Install returns %d",r));
73 TInt DEmulatedRomPagingDevice::Construct()
75 __KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf(">DEmulatedRomPagingDevice::Construct"));
77 // Initialise DPagingDevice base class
79 iReadUnitShift = 9; // 512 byte units
80 iName = "EmulatedRomPagingDevice";
85 TInt r=Kern::HalFunction(EHalGroupVM,EVMHalGetOriginalRomPages,&romPages,&numRomPages);
86 __KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("DEmulatedRomPagingDevice::Construct numRomPages=%08x",numRomPages));
87 __NK_ASSERT_ALWAYS(r==KErrNone);
89 r = Kern::MutexCreate(iMutex, _L("EmulRomPageDev"), KMutexOrdNone);
90 __NK_ASSERT_ALWAYS(r==KErrNone);
91 // Create a shared chunk to map the ROM pages
92 TInt pageSize = Kern::RoundToPageSize(1);
93 TChunkCreateInfo info;
94 info.iType = TChunkCreateInfo::ESharedKernelSingle;
95 info.iMaxSize = numRomPages*pageSize;
96 info.iMapAttr = EMapAttrFullyBlocking;
97 info.iOwnsMemory = EFalse;
100 r = Kern::ChunkCreate(info,chunk,iRomStore,mapAttr);
101 __NK_ASSERT_ALWAYS(r==KErrNone);
102 r = Kern::ChunkCommitPhysical(chunk,0,numRomPages*pageSize,romPages);
103 __NK_ASSERT_ALWAYS(r==KErrNone);
105 // create DFC thread for NAND read simulation
106 _LIT8(KDemandPagingDelay,"DemandPagingDelay");
107 r = Kern::DfcQCreate(iDfcQue,24,&KDemandPagingDelay); // DFC thread of same priority as NAND driver
108 __NK_ASSERT_ALWAYS(r==KErrNone);
110 // setup delays used for simulation
111 SDemandPagingConfig config = Epoc::RomHeader().iDemandPagingConfig;
112 iReadPageDelay = config.iSpare[0];
113 iReadPageCPUDelay = config.iSpare[1];
114 __KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("DEmulatedRomPagingDevice::Construct emulated delays=%d,%d",iReadPageDelay,iReadPageCPUDelay));
116 __KTRACE_OPT2(KPAGING,KBOOT,Kern::Printf("<DEmulatedRomPagingDevice::Construct"));
121 TInt DEmulatedRomPagingDevice::Read(TThreadMessage* /*aReq*/,TLinAddr aBuffer,TUint aOffset,TUint aSize,TInt)
123 aOffset <<= iReadUnitShift;
124 aSize <<= iReadUnitShift;
126 if(iReadPageCPUDelay==0 || KDebugNum(KTESTFAST))
128 // don't do emulated NAND delay, just copy it immediately...
129 memcpy((TAny*)aBuffer,(TAny*)(iRomStore+aOffset),aSize);
133 // make sure we are single threaded when we use the DFC.
134 Kern::MutexWait(*iMutex);
136 // get a DFC to do the simulated read
138 NKern::FSSetOwner(&sem,NULL);
139 iReadRequest.iBuffer = aBuffer;
140 iReadRequest.iOffset = aOffset;
141 iReadRequest.iSize = aSize;
142 iReadRequest.iSemaphore = &sem;
143 TDfc dfc(ReadDfcCallback,this,iDfcQue,0);
146 TInt result = iReadRequest.iResult;
148 // let any other threads have a go.
149 Kern::MutexSignal(*iMutex);
154 void DEmulatedRomPagingDevice::ReadTimerCallback(TAny* aSem)
156 NKern::FSSignal((NFastSemaphore*)aSem);
160 void DEmulatedRomPagingDevice::ReadDfcCallback(TAny* aSelf)
162 ((DEmulatedRomPagingDevice*)aSelf)->ReadDfc();
166 void DEmulatedRomPagingDevice::ReadDfc()
168 // calculate number of ticks to stall to emulate elapsed time for page read request
169 iAccumulatedDelay += iReadPageDelay;
170 TInt tick = NKern::TickPeriod();
172 if(iAccumulatedDelay>tick)
174 delay = iAccumulatedDelay/tick;
175 iAccumulatedDelay -= delay*tick+(tick>>1);
178 NKern::FSSetOwner(&sem,NULL);
179 NTimer timer(ReadTimerCallback,&sem);
181 timer.OneShot(delay,ETrue);
183 // emulate CPU load for processing read
184 Kern::NanoWait(iReadPageCPUDelay*1000);
186 // get data using memcpy
187 memcpy((TAny*)iReadRequest.iBuffer,(TAny*)(iRomStore+iReadRequest.iOffset),iReadRequest.iSize);
189 // wait for delay timer
194 iReadRequest.iResult = KErrNone;
195 NKern::FSSignal(iReadRequest.iSemaphore);
199 DECLARE_STANDARD_EXTENSION()
201 return DEmulatedRomPagingDevice::Install();