author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 11:08:29 +0300 | |
changeset 247 | d8d70de2bd36 |
parent 201 | 43365a9b78a3 |
child 221 | 39b39e1a406e |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1998-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". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// e32\memmodel\epoc\mmubase\ramalloc.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
/** |
|
19 |
@file |
|
20 |
@internalComponent |
|
21 |
*/ |
|
22 |
//#define __VERIFY_LEASTMOVDIS |
|
23 |
||
24 |
#include <plat_priv.h> |
|
25 |
#include <ramalloc.h> |
|
26 |
#include <e32btrace.h> |
|
27 |
||
28 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
29 |
#include <mmubase.inl> |
|
30 |
#else |
|
31 |
#include "mdefrag.inl" |
|
32 |
#endif //__MEMMODEL_FLEXIBLE__ |
|
33 |
||
34 |
DRamAllocator* DRamAllocator::New() |
|
35 |
{ |
|
36 |
return new DRamAllocator; |
|
37 |
} |
|
38 |
||
39 |
DRamAllocator* DRamAllocator::New(const SRamInfo& aInfo, const SRamZone* aZoneInfo, TRamZoneCallback aZoneCallback) |
|
40 |
{ |
|
41 |
DRamAllocator* pA=New(); |
|
42 |
if (!pA) |
|
43 |
Panic(ECreateNoMemory); |
|
44 |
// If this fails then it won't return but panic |
|
45 |
pA->Create(aInfo,aZoneInfo, aZoneCallback); |
|
46 |
return pA; |
|
47 |
} |
|
48 |
||
49 |
void DRamAllocator::Panic(TPanic aPanic) |
|
50 |
{ |
|
51 |
Kern::Fault("RAM-ALLOC", aPanic); |
|
52 |
} |
|
53 |
||
54 |
#ifdef KMMU |
|
55 |
void HexDump32(const TAny* a, TInt n, const char* s) |
|
56 |
{ |
|
57 |
const TUint32* p=(const TUint32*)a; |
|
58 |
Kern::Printf(s); |
|
59 |
TInt i=0; |
|
60 |
while(n) |
|
61 |
{ |
|
62 |
TBuf8<80> b; |
|
63 |
b.AppendNumFixedWidth(i,EHex,4); |
|
64 |
b.Append(':'); |
|
65 |
TInt m=Min(n,4); |
|
66 |
n-=m; |
|
67 |
i+=m; |
|
68 |
while(m--) |
|
69 |
{ |
|
70 |
b.Append(' '); |
|
71 |
b.AppendNumFixedWidth(*p++,EHex,8); |
|
72 |
} |
|
73 |
Kern::Printf("%S",&b); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
void HexDump8(const TAny* a, TInt n, const char* s) |
|
78 |
{ |
|
79 |
const TUint8* p=(const TUint8*)a; |
|
80 |
Kern::Printf(s); |
|
81 |
TInt i=0; |
|
82 |
while(n) |
|
83 |
{ |
|
84 |
TBuf8<80> b; |
|
85 |
b.AppendNumFixedWidth(i,EHex,4); |
|
86 |
b.Append(':'); |
|
87 |
TInt m=Min(n,16); |
|
88 |
n-=m; |
|
89 |
i+=m; |
|
90 |
while(m--) |
|
91 |
{ |
|
92 |
b.Append(' '); |
|
93 |
b.AppendNumFixedWidth(*p++,EHex,2); |
|
94 |
} |
|
95 |
Kern::Printf("%S",&b); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
void DRamAllocator::DebugDump() |
|
100 |
{ |
|
101 |
Kern::Printf("PageSize=%08x PageShift=%d",KPageSize,KPageShift); |
|
102 |
Kern::Printf("Total Pages=%x Total Free=%x",iTotalRamPages,iTotalFreeRamPages); |
|
103 |
Kern::Printf("Number of zones=%d, PowerState=%016lx",iNumZones,iZonePwrState); |
|
104 |
Kern::Printf("PhysAddrBase=%08x, PhysAddrTop=%08x",iPhysAddrBase,iPhysAddrTop); |
|
105 |
||
106 |
TUint i = 0; |
|
107 |
Kern::Printf("Zone Info:"); |
|
108 |
for (; i<iNumZones; ++i) |
|
109 |
{ |
|
110 |
SZone& z=iZones[i]; |
|
111 |
TBitMapAllocator& b = *(z.iBma[KBmaAllPages]); |
|
112 |
Kern::Printf("%x: Avail %x Size %x Phys %08x PhysEnd %08x ID %08x FreePage %x Pref %02x",i,b.iAvail,b.iSize, |
|
113 |
z.iPhysBase, z.iPhysEnd, z.iId,z.iFreePages, z.iPref); |
|
114 |
Kern::Printf("Allocated Unknown %x Fixed %x Movable %x Discardable %x",iZones[i].iAllocPages[EPageUnknown],iZones[i].iAllocPages[EPageFixed], |
|
115 |
iZones[i].iAllocPages[EPageMovable],iZones[i].iAllocPages[EPageDiscard]); |
|
116 |
} |
|
117 |
||
118 |
Kern::Printf("Zone pref order:"); |
|
119 |
SDblQueLink* link = iZonePrefList.First(); |
|
120 |
for (; link != &iZonePrefList.iA; link = link->iNext) |
|
121 |
{ |
|
122 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
123 |
Kern::Printf("ID0x%x rank0x%x", zone.iId, zone.iPrefRank); |
|
124 |
} |
|
125 |
SZone& zone = *_LOFF(iZoneLeastMovDis, SZone, iPrefLink); |
|
126 |
Kern::Printf("iZoneLeastMovDis ID 0x%x rank 0x%x", zone.iId, iZoneLeastMovDisRank); |
|
127 |
} |
|
128 |
#endif |
|
129 |
||
130 |
TInt CountBanks(const SRamBank* aBankList) |
|
131 |
{ |
|
132 |
TInt banks=0; |
|
133 |
for (; aBankList->iSize; ++banks, ++aBankList); |
|
134 |
return banks; |
|
135 |
} |
|
136 |
||
137 |
TUint32 TotalBankSize(const SRamBank* aBankList) |
|
138 |
{ |
|
139 |
TUint32 size=0; |
|
140 |
for (; aBankList->iSize; ++aBankList) |
|
141 |
size+=aBankList->iSize; |
|
142 |
return size; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
Count how many zones have been specified and do some basic checks on their layout: |
|
147 |
Zones must be distinct, i.e. not overlap |
|
148 |
Zone ID must be unique |
|
149 |
Zones must be page size aligned |
|
150 |
Zones must be big enough to cover all of the allocatable RAM |
|
151 |
The end of the list is indicated by a SRamZone.iSize==0. |
|
152 |
@param aZones The list of RAM zones to be setup |
|
153 |
*/ |
|
154 |
void DRamAllocator::CountZones(const SRamZone* aZones) |
|
155 |
{ |
|
156 |
TUint32 totalSize = 0; |
|
157 |
TUint32 pageMask = KPageSize-1; |
|
158 |
// Check zones don't overlap each other and while running through the zones |
|
159 |
// calculate how many there are |
|
160 |
const SRamZone* pCurZ = aZones; |
|
161 |
for (; pCurZ->iSize != 0; pCurZ++) |
|
162 |
{ |
|
163 |
// Verify zone addresses and alignment |
|
164 |
TUint32 curEnd = pCurZ->iBase + pCurZ->iSize - 1; |
|
165 |
__KTRACE_OPT(KMMU,Kern::Printf("curBase %x curEnd %x pageMask %x",pCurZ->iBase,curEnd,pageMask)); |
|
166 |
if (curEnd <= pCurZ->iBase || (((curEnd + 1) | pCurZ->iBase) & pageMask)) |
|
167 |
{ |
|
168 |
Panic(EZonesAlignment); |
|
169 |
} |
|
170 |
||
171 |
if (pCurZ->iId == KRamZoneInvalidId) |
|
172 |
{ |
|
173 |
Panic(EZonesIDInvalid); |
|
174 |
} |
|
175 |
// Check the flags are not set to invalid values |
|
176 |
if (pCurZ->iFlags & KRamZoneFlagInvalid) |
|
177 |
{ |
|
178 |
Panic(EZonesFlagsInvalid); |
|
179 |
} |
|
180 |
||
181 |
iNumZones++; |
|
182 |
if (iNumZones > KMaxRamZones) |
|
183 |
{// Too many zones specified |
|
184 |
Panic(EZonesTooNumerousOrFew); |
|
185 |
} |
|
186 |
totalSize += pCurZ->iSize; |
|
187 |
||
188 |
// Verify this zone doesn't overlap any of the previous zones' address space |
|
189 |
const SRamZone* pTmpZ = aZones; |
|
190 |
for (; pTmpZ < pCurZ; pTmpZ++) |
|
191 |
{ |
|
192 |
TUint32 tmpEnd = pTmpZ->iBase + pTmpZ->iSize - 1; |
|
193 |
if (tmpEnd >= pCurZ->iBase && pTmpZ->iBase <= curEnd) |
|
194 |
{ |
|
195 |
Panic(EZonesNotDistinct); |
|
196 |
} |
|
197 |
if(pTmpZ->iId == pCurZ->iId) |
|
198 |
{ |
|
199 |
Panic(EZonesIDNotUnique); |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
__KTRACE_OPT(KMMU,Kern::Printf("iNumZones=%d, totalSize=%x",iNumZones,totalSize)); |
|
204 |
if (!iNumZones) |
|
205 |
{// no zones specified |
|
206 |
Panic(EZonesTooNumerousOrFew); |
|
207 |
} |
|
208 |
||
209 |
// Together all of the zones should cover the whole of the RAM |
|
210 |
if (totalSize>>KPageShift < iTotalRamPages) |
|
211 |
{ |
|
212 |
Panic(EZonesIncomplete); |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
||
217 |
/** |
|
218 |
Get the zone from the ID |
|
219 |
@param aId ID of zone to find |
|
220 |
@return Pointer to the zone if zone of matching ID found, NULL otherwise |
|
221 |
*/ |
|
222 |
SZone* DRamAllocator::ZoneFromId(TUint aId) const |
|
223 |
{ |
|
224 |
SZone* pZ = iZones; |
|
225 |
const SZone* const pEndZone = iZones + iNumZones; |
|
226 |
for (; pZ < pEndZone; pZ++) |
|
227 |
{ |
|
228 |
if (aId == pZ->iId) |
|
229 |
{ |
|
230 |
return pZ; |
|
231 |
} |
|
232 |
} |
|
233 |
return NULL; |
|
234 |
} |
|
235 |
||
236 |
/** Retrieve the physical base address and number of pages in the specified zone. |
|
237 |
||
238 |
@param aZoneId The ID of the zone |
|
239 |
@param aPhysBaseAddr Receives the base address of the zone |
|
240 |
@param aNumPages Receives the number of pages in the zone |
|
241 |
||
242 |
@return KErrNone if zone found, KErrArgument if zone couldn't be found |
|
243 |
*/ |
|
244 |
TInt DRamAllocator::GetZoneAddress(TUint aZoneId, TPhysAddr& aPhysBase, TUint& aNumPages) |
|
245 |
{ |
|
246 |
SZone* zone = ZoneFromId(aZoneId); |
|
247 |
if (zone == NULL) |
|
248 |
{ |
|
249 |
return KErrArgument; |
|
250 |
} |
|
251 |
aPhysBase = zone->iPhysBase; |
|
252 |
aNumPages = zone->iPhysPages; |
|
253 |
return KErrNone; |
|
254 |
} |
|
255 |
||
256 |
#ifdef __MEMMODEL_FLEXIBLE__ |
|
257 |
/** |
|
258 |
@param aAddr The address of page to find the zone of |
|
259 |
@param aOffset The page offset from the start of the zone that the page is in |
|
260 |
*/ |
|
261 |
SZone* DRamAllocator::GetZoneAndOffset(TPhysAddr aAddr, TInt& aOffset) |
|
262 |
{ |
|
263 |
// Get the zone from the SPageInfo of the page at aAddr |
|
264 |
SPageInfo* pageInfo = SPageInfo::SafeFromPhysAddr(aAddr); |
|
265 |
if (pageInfo == NULL) |
|
266 |
{ |
|
267 |
return NULL; |
|
268 |
} |
|
269 |
||
270 |
// Perform a binary search for the RAM zone, we know aAddr is within a RAM |
|
271 |
// zone as pageInfo != NULL. |
|
272 |
SZone* left = iZones; |
|
273 |
SZone* mid = iZones + (iNumZones>>1); |
|
274 |
SZone* top = iZones + iNumZones - 1; |
|
275 |
||
276 |
while (mid->iPhysEnd < aAddr || mid->iPhysBase > aAddr) |
|
277 |
{ |
|
278 |
if (mid->iPhysEnd < aAddr) |
|
279 |
left = mid + 1; |
|
280 |
else |
|
281 |
top = mid - 1; |
|
282 |
mid = left + ((top - left) >> 1); |
|
283 |
__ASSERT_DEBUG(left <= top && mid <= top && mid >= left, Panic(EAllocRamPagesInconsistent)); |
|
284 |
} |
|
285 |
__ASSERT_DEBUG(mid->iPhysBase <= aAddr && mid->iPhysEnd >= aAddr, Panic(EAllocRamPagesInconsistent)); |
|
286 |
aOffset = (aAddr - mid->iPhysBase) >> KPageShift; |
|
287 |
__ASSERT_DEBUG((TUint)aOffset < mid->iPhysPages, Panic(EAllocRamPagesInconsistent)); |
|
288 |
return mid; |
|
289 |
} |
|
290 |
#else |
|
291 |
/** |
|
292 |
@param aAddr The address of page to find the zone of |
|
293 |
@param aOffset The page offset from the start of the zone that the page is in |
|
294 |
*/ |
|
295 |
SZone* DRamAllocator::GetZoneAndOffset(TPhysAddr aAddr, TInt& aOffset) |
|
296 |
{ |
|
297 |
// Get the zone from the SPageInfo of the page at aAddr |
|
298 |
SPageInfo* pageInfo = SPageInfo::SafeFromPhysAddr(aAddr); |
|
299 |
if (pageInfo == NULL) |
|
300 |
{ |
|
301 |
return NULL; |
|
302 |
} |
|
303 |
SZone* z = iZones + pageInfo->Zone(); |
|
304 |
aOffset = (aAddr - z->iPhysBase) >> KPageShift; |
|
305 |
__ASSERT_DEBUG((TUint)aOffset < z->iPhysPages, Panic(EAllocRamPagesInconsistent)); |
|
306 |
return z; |
|
307 |
} |
|
308 |
#endif |
|
309 |
/** |
|
310 |
@param aId ID of zone to get page count for |
|
311 |
@param aPageData store for page counts |
|
312 |
@return KErrNone if zone found, KErrArgument otherwise |
|
313 |
*/ |
|
314 |
TInt DRamAllocator::GetZonePageCount(TUint aId, SRamZonePageCount& aPageData) |
|
315 |
{ |
|
316 |
// Search for the zone of ID aId |
|
317 |
const SZone* zone = ZoneFromId(aId); |
|
318 |
if (zone == NULL) |
|
319 |
{ |
|
320 |
return KErrArgument; |
|
321 |
} |
|
322 |
aPageData.iFreePages = zone->iFreePages; |
|
323 |
aPageData.iUnknownPages = zone->iAllocPages[EPageUnknown]; |
|
324 |
aPageData.iFixedPages = zone->iAllocPages[EPageFixed]; |
|
325 |
aPageData.iMovablePages = zone->iAllocPages[EPageMovable]; |
|
326 |
aPageData.iDiscardablePages = zone->iAllocPages[EPageDiscard]; |
|
327 |
||
328 |
return KErrNone; |
|
329 |
} |
|
330 |
||
331 |
||
332 |
/** Update the count of free and allocated pages for the zone with |
|
333 |
@param aZone The index of the zone whose counts are being updated |
|
334 |
@param aCount The no of pages being allocated |
|
335 |
@param aType The type of the pages being allocated |
|
336 |
*/ |
|
337 |
void DRamAllocator::ZoneAllocPages(SZone* aZone, TUint32 aCount, TZonePageType aType) |
|
338 |
{ |
|
339 |
#ifdef _DEBUG |
|
340 |
TUint32 free = aZone->iFreePages - aCount; |
|
341 |
TUint32 alloc = aZone->iAllocPages[aType] + aCount; |
|
342 |
TUint32 total_alloc = aZone->iAllocPages[EPageUnknown] + |
|
343 |
aZone->iAllocPages[EPageDiscard] + |
|
344 |
aZone->iAllocPages[EPageMovable] + |
|
345 |
aZone->iAllocPages[EPageFixed] + aCount; |
|
346 |
if (free > aZone->iFreePages || |
|
347 |
alloc < aZone->iAllocPages[aType] || |
|
348 |
free + total_alloc != aZone->iPhysPages || |
|
349 |
iTotalFreeRamPages > iTotalRamPages) |
|
350 |
{ |
|
351 |
__KTRACE_OPT(KMMU,Kern::Printf("TotalFree %x TotalPages %x",iTotalFreeRamPages, iTotalRamPages)); |
|
352 |
__KTRACE_OPT(KMMU,Kern::Printf("ZoneFreePages - aCount %x free %x, alloc %x",aCount,free,alloc)); // counts rolled over |
|
353 |
__KTRACE_OPT(KMMU,Kern::Printf("Alloc Unk %x Fx %x Mv %x Dis %x",aZone->iAllocPages[EPageUnknown], |
|
354 |
aZone->iAllocPages[EPageFixed], aZone->iAllocPages[EPageMovable],aZone->iAllocPages[EPageDiscard])); |
|
355 |
Panic(EZonesCountErr); |
|
356 |
} |
|
357 |
__KTRACE_OPT(KMMU2,Kern::Printf("ZoneFreePages - aCount %x free %x, alloc %x",aCount,free,alloc)); |
|
358 |
__KTRACE_OPT(KMMU2,Kern::Printf("Alloc Unk %x Fx %x Mv %x Dis %x",aZone->iAllocPages[EPageUnknown], |
|
359 |
aZone->iAllocPages[EPageFixed], aZone->iAllocPages[EPageMovable],aZone->iAllocPages[EPageDiscard])); |
|
360 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
361 |
if (!iContiguousReserved) |
0 | 362 |
{ |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
363 |
__ASSERT_DEBUG(free == (TUint32)aZone->iBma[KBmaAllPages]->iAvail, Panic(EAllocRamPagesInconsistent)); |
0 | 364 |
TBitMapAllocator& bmaType = *(aZone->iBma[(aType != EPageUnknown)? aType : EPageFixed]); |
365 |
TUint allocPages; |
|
366 |
if (aType == EPageFixed || aType == EPageUnknown) |
|
367 |
allocPages = aZone->iAllocPages[EPageUnknown] + aZone->iAllocPages[EPageFixed]; |
|
368 |
else |
|
369 |
allocPages = aZone->iAllocPages[aType]; |
|
370 |
allocPages += aCount; |
|
371 |
__NK_ASSERT_DEBUG(aZone->iPhysPages - bmaType.iAvail == allocPages); |
|
372 |
__NK_ASSERT_DEBUG((TUint)bmaType.iAvail >= aZone->iFreePages - aCount); |
|
373 |
||
374 |
//#define _FULL_VERIFY_TYPE_BMAS |
|
375 |
#ifdef _FULL_VERIFY_TYPE_BMAS |
|
376 |
TUint offset = 0; |
|
377 |
TUint matchedPages = 0; |
|
378 |
TInt r = KErrNone; |
|
379 |
while (offset < aZone->iPhysPages && r == KErrNone) |
|
380 |
{ |
|
381 |
r = NextAllocatedPage(aZone, offset, EPageTypes); |
|
382 |
if (bmaType.NotFree(offset, 1)) |
|
383 |
{ |
|
384 |
matchedPages++; |
|
385 |
} |
|
386 |
offset++; |
|
387 |
} |
|
388 |
__NK_ASSERT_DEBUG(matchedPages == allocPages); |
|
389 |
#endif |
|
390 |
} |
|
391 |
#endif |
|
392 |
||
393 |
// Update counts |
|
394 |
aZone->iAllocPages[aType] += aCount; |
|
395 |
aZone->iFreePages -= aCount; |
|
396 |
aZone->iFlags &= ~KRamZoneFlagMark; // clear the mark as this zone is active |
|
397 |
||
398 |
// Check if power state of zone needs to be changed |
|
399 |
if (iZonePowerFunc && !(iZonePwrState & (((TUint64)1) << aZone - iZones))) |
|
400 |
{//zone no longer empty so call variant to power RAM zone up if necessary |
|
401 |
iZonePwrState |= (((TUint64)1) << aZone - iZones); |
|
402 |
||
403 |
if (iZoneCallbackInitSent) |
|
404 |
{ |
|
405 |
TInt ret = (*iZonePowerFunc)(ERamZoneOp_PowerUp, (TAny*)aZone->iId, (TUint*)&iZonePwrState); |
|
406 |
if (ret != KErrNone && ret != KErrNotSupported) |
|
407 |
{ |
|
408 |
Panic(EZonesCallbackErr); |
|
409 |
} |
|
410 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL, "DRamAllocator::ZoneAllocPages"); |
|
411 |
} |
|
412 |
} |
|
413 |
||
414 |
// Re-order the zone preference list so that a RAM zone with more immovable pages |
|
415 |
// is more preferable and secondary to that a RAM zone that is not empty is more |
|
416 |
// preferable than one that is empty. |
|
417 |
while (&aZone->iPrefLink != iZonePrefList.First()) |
|
418 |
{ |
|
419 |
SZone* prevZ = _LOFF(aZone->iPrefLink.iPrev, SZone, iPrefLink); |
|
420 |
__NK_ASSERT_DEBUG(K::Initialising || prevZ->iPrefRank == aZone->iPrefRank - 1); |
|
421 |
if (prevZ->iPref == aZone->iPref && |
|
422 |
(prevZ->iAllocPages[EPageFixed] + prevZ->iAllocPages[EPageUnknown] < |
|
423 |
aZone->iAllocPages[EPageFixed] + aZone->iAllocPages[EPageUnknown] || |
|
424 |
prevZ->iFreePages == prevZ->iPhysPages)) |
|
425 |
{ |
|
426 |
__KTRACE_OPT(KMMU, Kern::Printf("a - Reorder aZone 0x%x free 0x%x before prevZ 0x%x free 0x%x", aZone->iId, aZone->iFreePages, prevZ->iId, prevZ->iFreePages)); |
|
427 |
// Make this RAM zone more preferable. |
|
428 |
aZone->iPrefLink.Deque(); |
|
429 |
aZone->iPrefLink.InsertBefore(&prevZ->iPrefLink); |
|
430 |
aZone->iPrefRank--; |
|
431 |
prevZ->iPrefRank++; |
|
432 |
||
433 |
if (iZoneLeastMovDis == &prevZ->iPrefLink) |
|
434 |
{// Ensure iZoneLeastMovDisRank is kept up to date. |
|
435 |
iZoneLeastMovDisRank = prevZ->iPrefRank; |
|
436 |
} |
|
437 |
if (iZoneLeastMovDis == &aZone->iPrefLink) |
|
438 |
{// Ensure iZoneLeastMovDisRank is kept up to date. |
|
439 |
iZoneLeastMovDisRank = aZone->iPrefRank; |
|
440 |
// aZone was the least preferable with movable and/or discardable so is it still? |
|
441 |
if (prevZ->iAllocPages[EPageMovable] || prevZ->iAllocPages[EPageDiscard]) |
|
442 |
{// prevZ is now the least preferable RAM zone with movable and/or discardable. |
|
443 |
iZoneLeastMovDis = &prevZ->iPrefLink; |
|
444 |
iZoneLeastMovDisRank = prevZ->iPrefRank; |
|
445 |
__KTRACE_OPT(KMMU, Kern::Printf("aa - iZoneleastInUse ID 0x%x", (_LOFF(iZoneLeastMovDis, SZone, iPrefLink))->iId)); |
|
446 |
} |
|
447 |
__KTRACE_OPT(KMMU, Kern::Printf("iZoneLeastMovDisRank 0x%x", iZoneLeastMovDisRank)); |
|
448 |
} |
|
449 |
} |
|
450 |
else |
|
451 |
{ |
|
452 |
break; |
|
453 |
} |
|
454 |
} |
|
455 |
||
456 |
// Now that the preference list has been re-ordered check whether |
|
457 |
// iZoneLeastMovDis needs updating. |
|
458 |
if (aType >= EPageMovable && iZoneLeastMovDisRank < aZone->iPrefRank) |
|
459 |
{ |
|
460 |
iZoneLeastMovDis = &aZone->iPrefLink; |
|
461 |
iZoneLeastMovDisRank = aZone->iPrefRank; |
|
462 |
__KTRACE_OPT(KMMU, Kern::Printf("a - iZoneleastInUse ID 0x%x", (_LOFF(iZoneLeastMovDis, SZone, iPrefLink))->iId)); |
|
463 |
} |
|
464 |
__NK_ASSERT_DEBUG( K::Initialising || |
|
465 |
iZoneLeastMovDisRank == _LOFF(iZoneLeastMovDis, SZone, iPrefLink)->iPrefRank); |
|
466 |
#ifdef __VERIFY_LEASTMOVDIS |
|
467 |
if (!K::Initialising) |
|
468 |
VerifyLeastPrefMovDis(); |
|
469 |
#endif |
|
470 |
} |
|
471 |
||
472 |
||
473 |
/** Update the count of free and allocated pages for the zone with |
|
474 |
@param aZone The index of the zone whose counts are being updated |
|
475 |
@param aCount The no of pages being freed |
|
476 |
@param aType The type of the pages being freed |
|
477 |
*/ |
|
478 |
void DRamAllocator::ZoneFreePages(SZone* aZone, TUint32 aCount, TZonePageType aType) |
|
479 |
{ |
|
480 |
#ifdef _DEBUG |
|
481 |
TUint32 alloc = aZone->iAllocPages[aType] - aCount; |
|
482 |
TUint32 free = aZone->iFreePages + aCount; |
|
483 |
TUint32 total_alloc = aZone->iAllocPages[EPageUnknown] + |
|
484 |
aZone->iAllocPages[EPageDiscard] + |
|
485 |
aZone->iAllocPages[EPageMovable] + |
|
486 |
aZone->iAllocPages[EPageFixed] - aCount; |
|
487 |
if (free < aZone->iFreePages || |
|
488 |
alloc > aZone->iAllocPages[aType] || |
|
489 |
free + total_alloc != aZone->iPhysPages || |
|
490 |
iTotalFreeRamPages > iTotalRamPages) |
|
491 |
{ |
|
492 |
__KTRACE_OPT(KMMU,Kern::Printf("TotalFree %x TotalPages %x",iTotalFreeRamPages, iTotalRamPages)); |
|
493 |
__KTRACE_OPT(KMMU,Kern::Printf("ZoneFreePages - aCount %x free %x, alloc %x",aCount,free,alloc)); // counts rolled over |
|
494 |
__KTRACE_OPT(KMMU,Kern::Printf("Alloc Unk %x Fx %x Mv %x Dis %x",aZone->iAllocPages[EPageUnknown], |
|
495 |
aZone->iAllocPages[EPageFixed], aZone->iAllocPages[EPageMovable],aZone->iAllocPages[EPageDiscard])); |
|
496 |
Panic(EZonesCountErr); |
|
497 |
} |
|
498 |
__KTRACE_OPT(KMMU2,Kern::Printf("ZoneFreePages - aCount %x free %x, alloc %x",aCount,free,alloc)); |
|
499 |
__KTRACE_OPT(KMMU2,Kern::Printf("Alloc Unk %x Fx %x Mv %x Dis %x",aZone->iAllocPages[EPageUnknown], |
|
500 |
aZone->iAllocPages[EPageFixed], aZone->iAllocPages[EPageMovable],aZone->iAllocPages[EPageDiscard])); |
|
501 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
502 |
if (!iContiguousReserved) |
0 | 503 |
{ |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
504 |
__ASSERT_DEBUG(free == (TUint32)aZone->iBma[KBmaAllPages]->iAvail, Panic(EAllocRamPagesInconsistent)); |
0 | 505 |
TBitMapAllocator& bmaType = *(aZone->iBma[(aType != EPageUnknown)? aType : EPageFixed]); |
506 |
TUint allocPages; |
|
507 |
if (aType == EPageFixed || aType == EPageUnknown) |
|
508 |
allocPages = aZone->iAllocPages[EPageUnknown] + aZone->iAllocPages[EPageFixed]; |
|
509 |
else |
|
510 |
allocPages = aZone->iAllocPages[aType]; |
|
511 |
allocPages -= aCount; |
|
512 |
__NK_ASSERT_DEBUG(aZone->iPhysPages - bmaType.iAvail == allocPages); |
|
513 |
__NK_ASSERT_DEBUG((TUint)bmaType.iAvail >= aZone->iFreePages + aCount); |
|
514 |
||
515 |
#ifdef _FULL_VERIFY_TYPE_BMAS |
|
516 |
TUint offset = 0; |
|
517 |
TUint matchedPages = 0; |
|
518 |
TInt r = KErrNone; |
|
519 |
while(offset < aZone->iPhysPages && r == KErrNone) |
|
520 |
{ |
|
521 |
r = NextAllocatedPage(aZone, offset, EPageTypes); |
|
522 |
if (bmaType.NotFree(offset, 1)) |
|
523 |
{ |
|
524 |
matchedPages++; |
|
525 |
} |
|
526 |
offset++; |
|
527 |
} |
|
528 |
__NK_ASSERT_DEBUG(matchedPages == allocPages); |
|
529 |
#endif |
|
530 |
} |
|
531 |
#endif |
|
532 |
||
533 |
// Update counts |
|
534 |
aZone->iAllocPages[aType] -= aCount; |
|
535 |
aZone->iFreePages += aCount; |
|
536 |
aZone->iFlags &= ~KRamZoneFlagMark; // clear the mark as this zone is active |
|
537 |
||
538 |
// Check if power state of zone needs to be changed. |
|
539 |
// Don't update iZonePwrState when a zone is being cleared to then be |
|
540 |
// claimed as it shouldn't be powered off as it's about to be used. |
|
541 |
if (iZonePowerFunc && !(aZone->iFlags & KRamZoneFlagClaiming) && |
|
542 |
aZone->iFreePages == aZone->iPhysPages) |
|
543 |
{// Zone is empty so call variant to power down RAM zone if desirable. |
|
544 |
TUint64 pwrMask = ~(((TUint64)1) << aZone - iZones); |
|
545 |
iZonePwrState &= pwrMask; |
|
546 |
||
547 |
// Don't invoke callback until Init callback sent. |
|
548 |
if (iZoneCallbackInitSent) |
|
549 |
{ |
|
550 |
TInt ret = (*iZonePowerFunc)(ERamZoneOp_PowerDown, (TAny*)aZone->iId, (TUint*)&iZonePwrState); |
|
551 |
if (ret != KErrNone && ret != KErrNotSupported) |
|
552 |
{ |
|
553 |
Panic(EZonesCallbackErr); |
|
554 |
} |
|
555 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL, "DRamAllocator::ZoneFreePages"); |
|
556 |
} |
|
557 |
} |
|
558 |
||
559 |
// Re-order the zone preference list so that a RAM zone with more immovable pages |
|
560 |
// is more preferable and secondary to that a RAM zone that is not empty is more |
|
561 |
// preferable than one that is empty. |
|
562 |
while (&aZone->iPrefLink != iZonePrefList.Last()) |
|
563 |
{ |
|
564 |
SZone* nextZ = _LOFF(aZone->iPrefLink.iNext, SZone, iPrefLink); |
|
565 |
__NK_ASSERT_DEBUG(K::Initialising || nextZ->iPrefRank == aZone->iPrefRank + 1); |
|
566 |
if (nextZ->iPref == aZone->iPref && |
|
567 |
(nextZ->iAllocPages[EPageFixed] + nextZ->iAllocPages[EPageUnknown] > |
|
568 |
aZone->iAllocPages[EPageFixed] + aZone->iAllocPages[EPageUnknown] || |
|
569 |
(nextZ->iFreePages != nextZ->iPhysPages && |
|
570 |
aZone->iFreePages == aZone->iPhysPages))) |
|
571 |
{ |
|
572 |
__KTRACE_OPT(KMMU, Kern::Printf("f - Reorder aZone 0x%x free 0x%x after nextZ 0x%x free 0x%x", aZone->iId, aZone->iFreePages, nextZ->iId, nextZ->iFreePages)); |
|
573 |
// Make this RAM zone less preferable. |
|
574 |
aZone->iPrefLink.Deque(); |
|
575 |
aZone->iPrefLink.InsertAfter(&nextZ->iPrefLink); |
|
576 |
aZone->iPrefRank++; |
|
577 |
nextZ->iPrefRank--; |
|
578 |
||
579 |
if (iZoneLeastMovDis == &aZone->iPrefLink) |
|
580 |
{// Ensure iZoneLeastMovDisRank is kept up to date. |
|
581 |
iZoneLeastMovDisRank = aZone->iPrefRank; |
|
582 |
} |
|
583 |
if (iZoneLeastMovDis == &nextZ->iPrefLink) |
|
584 |
{// Ensure iZoneLeastMovDisRank is kept up to date. |
|
585 |
iZoneLeastMovDisRank = nextZ->iPrefRank; |
|
586 |
if (aZone->iAllocPages[EPageMovable] || aZone->iAllocPages[EPageDiscard]) |
|
587 |
{// aZone is now the least preferable RAM zone with movable and/or discardable. |
|
588 |
iZoneLeastMovDis = &aZone->iPrefLink; |
|
589 |
iZoneLeastMovDisRank = aZone->iPrefRank; |
|
590 |
__KTRACE_OPT(KMMU, Kern::Printf("aa - iZoneleastInUse ID 0x%x", (_LOFF(iZoneLeastMovDis, SZone, iPrefLink))->iId)); |
|
591 |
} |
|
592 |
__KTRACE_OPT(KMMU, Kern::Printf("iZoneLeastMovDis Rank 0x%x", iZoneLeastMovDisRank)); |
|
593 |
} |
|
594 |
} |
|
595 |
else |
|
596 |
{ |
|
597 |
break; |
|
598 |
} |
|
599 |
} |
|
600 |
if (&aZone->iPrefLink == iZoneLeastMovDis && |
|
601 |
!aZone->iAllocPages[EPageMovable] && !aZone->iAllocPages[EPageDiscard]) |
|
602 |
{// This RAM zone no longer has movable or discardable and therefore it |
|
603 |
// is also no longer the least preferable RAM zone with movable and/or |
|
604 |
// discardable. |
|
605 |
SZone* zonePrev; |
|
606 |
do |
|
607 |
{ |
|
608 |
iZoneLeastMovDis = iZoneLeastMovDis->iPrev; |
|
609 |
iZoneLeastMovDisRank--; |
|
610 |
if (iZoneLeastMovDis == iZonePrefList.First()) |
|
611 |
{// This the most preferable RAM zone so can't go any further. |
|
612 |
break; |
|
613 |
} |
|
614 |
zonePrev = _LOFF(iZoneLeastMovDis, SZone, iPrefLink); |
|
615 |
__KTRACE_OPT(KMMU, Kern::Printf("f - iZoneLeastMovDis 0x%x", zonePrev->iId)); |
|
616 |
} |
|
617 |
while (!zonePrev->iAllocPages[EPageMovable] && !zonePrev->iAllocPages[EPageDiscard]); |
|
618 |
||
619 |
__NK_ASSERT_DEBUG( K::Initialising || |
|
620 |
iZoneLeastMovDisRank == _LOFF(iZoneLeastMovDis, SZone, iPrefLink)->iPrefRank); |
|
621 |
||
622 |
#ifdef __VERIFY_LEASTMOVDIS |
|
623 |
if (!K::Initialising) |
|
624 |
VerifyLeastPrefMovDis(); |
|
625 |
#endif |
|
626 |
} |
|
627 |
} |
|
628 |
||
629 |
||
630 |
/** Calculate the physical address order of the zones and temporally store |
|
631 |
the order in aZoneAddrOrder |
|
632 |
*/ |
|
633 |
inline void DRamAllocator::SortRamZones(const SRamZone* aZones, TUint8* aZoneAddrOrder) |
|
634 |
{ |
|
635 |
const SRamZone* const endZone = aZones + iNumZones; |
|
636 |
const SRamZone* zone = aZones; |
|
637 |
for (; zone < endZone; zone++) |
|
638 |
{ |
|
639 |
// zoneIdx is the number of zones that have a lower base address than the |
|
640 |
// current zone and therefore it is the address index of the current zone |
|
641 |
TInt zoneIdx = 0; |
|
642 |
// search for any zones of lower base address |
|
643 |
const SRamZone* zone2 = aZones; |
|
644 |
for (; zone2 < endZone; zone2++) |
|
645 |
{ |
|
646 |
if (zone2->iBase < zone->iBase) |
|
647 |
{ |
|
648 |
zoneIdx++; // have another zone of lower base address |
|
649 |
} |
|
650 |
} |
|
651 |
aZoneAddrOrder[zoneIdx] = zone - aZones; |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
||
656 |
/** Initialise SPageInfos for all pages in this zone with the |
|
657 |
index of the zone. |
|
658 |
@param aZone The zone the pages to be initialised are in |
|
659 |
*/ |
|
660 |
inline TUint DRamAllocator::InitSPageInfos(const SZone* aZone) |
|
661 |
{ |
|
662 |
TUint pagesUpdated = 0; |
|
663 |
if (aZone->iPhysBase > iPhysAddrTop || aZone->iPhysEnd < iPhysAddrBase) |
|
664 |
{// None of the zone is in allocatable RAM |
|
665 |
return pagesUpdated; |
|
666 |
} |
|
667 |
||
668 |
// Mark each allocatable page in this zone with the index of the zone |
|
669 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
670 |
TUint8 zoneIndex = aZone - iZones; |
|
671 |
#endif |
|
672 |
TPhysAddr addr = aZone->iPhysBase; |
|
673 |
for (; addr <= aZone->iPhysEnd; addr += KPageSize) |
|
674 |
{ |
|
675 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(addr); |
|
676 |
if (pi) |
|
677 |
{ |
|
678 |
#ifndef __MEMMODEL_FLEXIBLE__ // The FMM doesn't store zone indices in SPageInfos. |
|
679 |
pi->SetZone(zoneIndex); |
|
680 |
#endif |
|
681 |
pagesUpdated++; |
|
682 |
} |
|
683 |
} |
|
684 |
return pagesUpdated; |
|
685 |
} |
|
686 |
||
687 |
/** HAL Function for the RAM allocator. |
|
688 |
*/ |
|
689 |
TInt DRamAllocator::HalFunction(TInt aFunction, TAny* a1, TAny* a2) |
|
690 |
{ |
|
691 |
switch(aFunction) |
|
692 |
{ |
|
693 |
case ERamHalGetZoneCount: |
|
694 |
{ |
|
695 |
kumemput32(a1, &iNumZones, sizeof(iNumZones)); |
|
696 |
return KErrNone; |
|
697 |
} |
|
698 |
||
699 |
case ERamHalGetZoneConfig: |
|
700 |
{ |
|
701 |
TUint zoneIndex = (TUint)a1; |
|
702 |
if (zoneIndex < iNumZones) |
|
703 |
{ |
|
704 |
SZone* pZone = iZones + zoneIndex; |
|
705 |
struct SRamZoneConfig config; |
|
706 |
NKern::ThreadEnterCS(); |
|
707 |
M::RamAllocLock(); // get mutex to ensure consistent set of values are read... |
|
708 |
config.iZoneId = pZone->iId; |
|
709 |
config.iZoneIndex = zoneIndex; |
|
710 |
config.iPhysBase = pZone->iPhysBase; |
|
711 |
config.iPhysEnd = pZone->iPhysEnd; |
|
712 |
config.iPhysPages = pZone->iPhysPages; |
|
713 |
config.iPref = pZone->iPref; |
|
714 |
config.iFlags = pZone->iFlags; |
|
715 |
M::RamAllocUnlock(); |
|
716 |
NKern::ThreadLeaveCS(); |
|
717 |
kumemput32(a2,&config,sizeof(config)); |
|
718 |
return KErrNone; |
|
719 |
} |
|
720 |
return KErrNotFound; |
|
721 |
} |
|
722 |
||
723 |
case ERamHalGetZoneUtilisation: |
|
724 |
{ |
|
725 |
TUint zoneIndex = (TUint)a1; |
|
726 |
if (zoneIndex < iNumZones) |
|
727 |
{ |
|
728 |
SZone* pZone = iZones + zoneIndex; |
|
729 |
struct SRamZoneUtilisation config; |
|
730 |
NKern::ThreadEnterCS(); |
|
731 |
M::RamAllocLock(); // get mutex to ensure consistent set of values are read... |
|
732 |
config.iZoneId = pZone->iId; |
|
733 |
config.iZoneIndex = zoneIndex; |
|
734 |
config.iPhysPages = pZone->iPhysPages; |
|
735 |
config.iFreePages = pZone->iFreePages; |
|
736 |
config.iAllocUnknown = pZone->iAllocPages[EPageUnknown]; |
|
737 |
config.iAllocFixed = pZone->iAllocPages[EPageFixed]; |
|
738 |
config.iAllocMovable = pZone->iAllocPages[EPageMovable]; |
|
739 |
config.iAllocDiscardable = pZone->iAllocPages[EPageDiscard]; |
|
740 |
config.iAllocOther = 0; |
|
741 |
M::RamAllocUnlock(); |
|
742 |
NKern::ThreadLeaveCS(); |
|
743 |
kumemput32(a2,&config,sizeof(config)); |
|
744 |
return KErrNone; |
|
745 |
} |
|
746 |
return KErrNotFound; |
|
747 |
} |
|
748 |
||
749 |
default: |
|
750 |
{ |
|
751 |
return KErrNotSupported; |
|
752 |
} |
|
753 |
} |
|
754 |
} |
|
755 |
||
756 |
/** |
|
757 |
Setup the ram allocator with information of the RAM available in the system that |
|
758 |
comes from the bootstrap/superpage. This is intended to be called from |
|
759 |
DRamAllocator::New(). |
|
760 |
@internalComponent |
|
761 |
@see DRamAllocator::New() |
|
762 |
@param aInfo Two lists of SRamBanks for available and reserved banks in RAM, respectively |
|
763 |
@param aZones A list of the ram zones in the system and their configuration/preferences |
|
764 |
@param aZoneCallback Pointer to a base port call back function that will be invoked by this class |
|
765 |
*/ |
|
766 |
void DRamAllocator::Create(const SRamInfo& aInfo, const SRamZone* aZones, TRamZoneCallback aZoneCallback) |
|
767 |
{ |
|
768 |
__KTRACE_OPT(KMMU,Kern::Printf("DRamAllocator::Create")); |
|
769 |
||
770 |
// SZone::iBma array assumes this and KBmaAllPages can't be the same as any |
|
771 |
// allocatable page type. |
|
772 |
__ASSERT_COMPILE(EPageFixed < KPageImmovable && EPageUnknown < KPageImmovable && |
|
773 |
EPageDiscard >= KPageImmovable && EPageMovable >= KPageImmovable && |
|
774 |
KBmaAllPages != EPageFixed && KBmaAllPages != EPageMovable && |
|
775 |
KBmaAllPages != EPageDiscard); |
|
776 |
// NoAllocOfPageType() requires this |
|
777 |
__ASSERT_COMPILE( KRamZoneFlagNoFixed == 1 << (EPageFixed - KPageTypeAllocBase) && |
|
778 |
KRamZoneFlagNoMovable == 1 << (EPageMovable - KPageTypeAllocBase) && |
|
779 |
KRamZoneFlagNoDiscard == 1 << (EPageDiscard - KPageTypeAllocBase)); |
|
780 |
||
781 |
// SZone::iPhysEnd and iPhysAddrTop rely on this when checking contiguous zones etc. |
|
782 |
__ASSERT_COMPILE(KPageShift != 0); |
|
783 |
||
784 |
/////////////////////////////////////////////////////////////////////////// |
|
785 |
// Determine where all the allocatable RAM pages are, using the SRamBank |
|
786 |
// data passed to the kernel by the bootstrap |
|
787 |
////////////////////////////////////////////////////////////////////////// |
|
788 |
TUint num_boot_banks=CountBanks(aInfo.iBanks); |
|
789 |
TUint32 total_ram_size=TotalBankSize(aInfo.iBanks); |
|
790 |
__KTRACE_OPT(KMMU,Kern::Printf("#banks from bootstrap=%d",num_boot_banks)); |
|
791 |
__KTRACE_OPT(KMMU,Kern::Printf("Total size=%08x",total_ram_size)); |
|
792 |
iTotalRamPages=total_ram_size>>KPageShift; |
|
793 |
// Assume all pages are allocated as unknown for now |
|
794 |
iTotalFreeRamPages = 0; |
|
795 |
__KTRACE_OPT(KMMU,Kern::Printf("Total size=%08x, total pages=%08x",total_ram_size,iTotalRamPages)); |
|
796 |
||
797 |
iPhysAddrBase=aInfo.iBanks[0].iBase; |
|
798 |
const SRamBank& last_boot_bank=aInfo.iBanks[num_boot_banks-1]; |
|
799 |
iPhysAddrTop = last_boot_bank.iBase + last_boot_bank.iSize - 1; |
|
800 |
__KTRACE_OPT(KMMU,Kern::Printf("PA base=%08x, PA top=%08x",iPhysAddrBase,iPhysAddrTop)); |
|
801 |
||
802 |
__ASSERT_DEBUG(iPhysAddrTop > iPhysAddrBase, Panic(ECreateInvalidRamBanks)); |
|
803 |
||
804 |
||
805 |
/////////////////////////////////////////////////////////////////////////// |
|
806 |
// Determine how many zones are required and allocate all the |
|
807 |
// data structures that will be required, permanent one first then |
|
808 |
// temporary ones to avoid kernel heap fragmentation. |
|
809 |
/////////////////////////////////////////////////////////////////////////// |
|
810 |
// Stop any RAM zone callback operations until the initial one has been sent |
|
811 |
iZoneCallbackInitSent = EFalse; |
|
812 |
if (aZones) |
|
813 |
{ |
|
814 |
CountZones(aZones); |
|
815 |
iZonePowerFunc = aZoneCallback; |
|
816 |
} |
|
817 |
else |
|
818 |
{// maximum number of zone is number of non-coalesced boot banks |
|
819 |
iNumZones = num_boot_banks; |
|
820 |
// No zones specified so don't worry about invoking callback function |
|
821 |
iZonePowerFunc = NULL; |
|
822 |
} |
|
823 |
||
824 |
// Permenant heap allocation #1 - may be resized if no zones specified |
|
825 |
__KTRACE_OPT(KMMU,Kern::Printf("iNumZones=%d", iNumZones)); |
|
826 |
iZones = (SZone*)Kern::AllocZ(iNumZones*sizeof(SZone)); |
|
827 |
if (!iZones) |
|
828 |
{ |
|
829 |
Panic(ECreateNoMemory); |
|
830 |
} |
|
831 |
||
832 |
/////////////////////////////////////////////////////////////////////////// |
|
833 |
// Coalesce contiguous boot banks |
|
834 |
/////////////////////////////////////////////////////////////////////////// |
|
835 |
SRamBank* physBanks = (SRamBank*)Kern::Alloc(num_boot_banks*sizeof(SRamBank)); |
|
836 |
if (!physBanks) |
|
837 |
{ |
|
838 |
Panic(ECreateNoMemory); |
|
839 |
} |
|
840 |
SRamBank* coalescedBank = physBanks; |
|
841 |
const SRamBank* const lastBank = aInfo.iBanks + num_boot_banks; |
|
842 |
TPhysAddr currentBase = aInfo.iBanks->iBase; |
|
843 |
TPhysAddr currentEnd = aInfo.iBanks->iBase + aInfo.iBanks->iSize; |
|
844 |
const SRamBank* nextBank = aInfo.iBanks + 1; |
|
845 |
for (; nextBank <= lastBank; ++nextBank) |
|
846 |
{ |
|
847 |
// Create new bank if the next bank isn't contiguous or if |
|
848 |
// it is the last bank |
|
849 |
if (nextBank == lastBank || nextBank->iBase != currentEnd) |
|
850 |
{ |
|
851 |
coalescedBank->iBase = currentBase; |
|
852 |
coalescedBank->iSize = currentEnd - currentBase; |
|
853 |
// Mark all the SPageInfos for the pages in this bank as unused. |
|
854 |
// Needs to be done here to allow SPageInfo::SafeFromPhysAddr to work |
|
855 |
// which is used by InitSPageInfos() |
|
856 |
SPageInfo* pi = SPageInfo::FromPhysAddr(coalescedBank->iBase); |
|
857 |
SPageInfo* piBankEnd = pi + (coalescedBank->iSize >> KPageShift); |
|
858 |
for (; pi < piBankEnd; pi++) |
|
859 |
{ |
|
860 |
pi->SetUnused(); |
|
861 |
} |
|
862 |
++coalescedBank; |
|
863 |
__KTRACE_OPT(KMMU, Kern::Printf("Coalesced bank: %08x-%08x", currentBase, currentEnd)); |
|
864 |
currentBase = nextBank->iBase; |
|
865 |
currentEnd = currentBase + nextBank->iSize; |
|
866 |
} |
|
867 |
else |
|
868 |
{ |
|
869 |
currentEnd += nextBank->iSize; |
|
870 |
} |
|
871 |
} |
|
872 |
TUint num_coalesced_banks = coalescedBank - physBanks; |
|
873 |
__KTRACE_OPT(KMMU, Kern::Printf("#Coalesced banks: %d", num_coalesced_banks)); |
|
874 |
||
875 |
/////////////////////////////////////////////////////////////////////////// |
|
876 |
// Initialise the SZone objects and mark all the SPageInfos with the index |
|
877 |
// of zone they are in. |
|
878 |
////////////////////////////////////////////////////////////////////////// |
|
879 |
// Assume everything is off so base port will get notification every time the |
|
880 |
// a new zone is required during the rest of boot process. |
|
881 |
if (aZones != NULL) |
|
882 |
{ |
|
883 |
SZone* newZone = iZones; // pointer to zone being created |
|
884 |
||
885 |
// Create and fill zoneAddrOrder with address ordered indices to aZones |
|
886 |
TUint8* zoneAddrOrder = (TUint8*)Kern::Alloc(iNumZones); |
|
887 |
if (!zoneAddrOrder) |
|
888 |
{ |
|
889 |
Panic(ECreateNoMemory); |
|
890 |
} |
|
891 |
SortRamZones(aZones, zoneAddrOrder); |
|
892 |
||
893 |
// Now go through each SRamZone in address order initialising the SZone |
|
894 |
// objects. |
|
895 |
TUint i = 0; |
|
896 |
TUint totalZonePages = 0; |
|
897 |
for (; i < iNumZones; i++) |
|
898 |
{ |
|
899 |
const SRamZone& ramZone = *(aZones + zoneAddrOrder[i]); |
|
900 |
newZone->iPhysBase = ramZone.iBase; |
|
901 |
newZone->iPhysEnd = ramZone.iBase + ramZone.iSize - 1; |
|
902 |
newZone->iPhysPages = ramZone.iSize >> KPageShift; |
|
903 |
newZone->iAllocPages[EPageUnknown] = newZone->iPhysPages; |
|
904 |
newZone->iId = ramZone.iId; |
|
905 |
newZone->iPref = ramZone.iPref; |
|
906 |
newZone->iFlags = ramZone.iFlags; |
|
907 |
totalZonePages += InitSPageInfos(newZone); |
|
908 |
newZone++; |
|
909 |
} |
|
910 |
||
911 |
// iZones now points to all the SZone objects stored in address order |
|
912 |
Kern::Free(zoneAddrOrder); |
|
913 |
if (totalZonePages != iTotalRamPages) |
|
914 |
{// The zones don't cover all of the allocatable RAM. |
|
915 |
Panic(EZonesIncomplete); |
|
916 |
} |
|
917 |
} |
|
918 |
else |
|
919 |
{ |
|
920 |
iNumZones = num_coalesced_banks; |
|
921 |
iZones = (SZone*)Kern::ReAlloc((TAny*)iZones, iNumZones*sizeof(SZone)); |
|
922 |
if (iZones == NULL) |
|
923 |
{ |
|
924 |
Panic(ECreateNoMemory); |
|
925 |
} |
|
926 |
// Create a zone for each coalesced boot bank |
|
927 |
SRamBank* bank = physBanks; |
|
928 |
SRamBank* bankEnd = physBanks + num_coalesced_banks; |
|
929 |
SZone* zone = iZones; |
|
930 |
for (; bank < bankEnd; bank++, zone++) |
|
931 |
{ |
|
932 |
zone->iPhysBase = bank->iBase; |
|
933 |
zone->iPhysEnd = bank->iBase + bank->iSize - 1; |
|
934 |
zone->iPhysPages = bank->iSize >> KPageShift; |
|
935 |
zone->iAllocPages[EPageUnknown] = zone->iPhysPages; |
|
936 |
zone->iId = (TUint)bank; // doesn't matter what it is as long as it is unique |
|
937 |
InitSPageInfos(zone); |
|
938 |
} |
|
939 |
} |
|
940 |
// Delete the coalesced banks as no longer required |
|
941 |
Kern::Free(physBanks); |
|
942 |
||
943 |
////////////////////////////////////////////////////////////////////////// |
|
944 |
// Create each zones' bit map allocator now as no temporary heap |
|
945 |
// cells still allocated at this point. |
|
946 |
/////////////////////////////////////////////////////////////////////////// |
|
947 |
const SZone* const endZone = iZones + iNumZones; |
|
948 |
SZone* zone = iZones; |
|
949 |
for (; zone < endZone; zone++) |
|
950 |
{// Create each BMA with all pages allocated as unknown. |
|
951 |
for (TUint i = 0; i < EPageTypes; i++) |
|
952 |
{ |
|
953 |
// Only mark the all pages bma and fixed/unknown bma as allocated. |
|
954 |
TBool notAllocated = (i >= (TUint)EPageMovable); |
|
955 |
zone->iBma[i] = TBitMapAllocator::New(zone->iPhysPages, notAllocated); |
|
956 |
if (!zone->iBma[i]) |
|
957 |
{ |
|
958 |
Panic(ECreateNoMemory); |
|
959 |
} |
|
960 |
} |
|
961 |
} |
|
962 |
||
963 |
/////////////////////////////////////////////////////////////////////////// |
|
964 |
// Unallocate each page in each bank so that it can be allocated when required. |
|
965 |
// Any page that exists outside a bank will remain allocated as EPageUnknown |
|
966 |
// and will therefore not be touched by the allocator. |
|
967 |
////////////////////////////////////////////////////////////////////////// |
|
968 |
// Temporarily fill preference list so SetPhysicalRamState can succeed |
|
969 |
#ifdef _DEBUG |
|
970 |
// Block bma verificaitons as bma and alloc counts aren't consistent yet. |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
971 |
iContiguousReserved = 1; |
0 | 972 |
#endif |
973 |
const SZone* const lastZone = iZones + iNumZones; |
|
974 |
zone = iZones; |
|
975 |
for (; zone < lastZone; zone++) |
|
976 |
{ |
|
977 |
iZonePrefList.Add(&zone->iPrefLink); |
|
978 |
} |
|
979 |
const SRamBank* const lastPhysBank = aInfo.iBanks + num_boot_banks; |
|
980 |
const SRamBank* bank = aInfo.iBanks; |
|
981 |
for (; bank < lastPhysBank; bank++) |
|
982 |
{// Free all the pages in this bank. |
|
983 |
SetPhysicalRamState(bank->iBase, bank->iSize, ETrue, EPageUnknown); |
|
984 |
} |
|
985 |
#ifdef _DEBUG |
|
986 |
// Only now is it safe to enable bma verifications |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
987 |
iContiguousReserved = 0; |
0 | 988 |
#endif |
989 |
||
990 |
/////////////////////////////////////////////////////////////////////////// |
|
991 |
// Sort the zones by preference and create a preference ordered linked list |
|
992 |
/////////////////////////////////////////////////////////////////////////// |
|
993 |
zone = iZones; |
|
994 |
for (; zone < lastZone; zone++) |
|
995 |
{// clear all the zones from the preference list as not in preference order |
|
996 |
zone->iPrefLink.Deque(); |
|
997 |
} |
|
998 |
SZone** prefOrder = (SZone**)Kern::AllocZ(iNumZones * sizeof(SZone*)); |
|
999 |
if (!prefOrder) |
|
1000 |
{ |
|
1001 |
Panic(ECreateNoMemory); |
|
1002 |
} |
|
1003 |
zone = iZones; |
|
1004 |
for(; zone < lastZone; zone++) |
|
1005 |
{ |
|
1006 |
TInt lowerZones = 0; |
|
1007 |
// Find how many zones that have a lower preference than this one |
|
1008 |
const SZone* zone2 = iZones; |
|
1009 |
for (; zone2 < lastZone; zone2++) |
|
1010 |
{ |
|
1011 |
if (zone->iPref > zone2->iPref || |
|
1012 |
zone->iPref == zone2->iPref && zone->iFreePages > zone2->iFreePages) |
|
1013 |
{ |
|
1014 |
lowerZones++; |
|
1015 |
} |
|
1016 |
} |
|
1017 |
while (prefOrder[lowerZones] != 0) |
|
1018 |
{// Zone(s) of this preference and size already exist so |
|
1019 |
// place this one after it/them |
|
1020 |
lowerZones++; |
|
1021 |
} |
|
1022 |
prefOrder[lowerZones] = zone; |
|
1023 |
} |
|
1024 |
// Fill preference ordered linked list |
|
1025 |
SZone** const lastPref = prefOrder + iNumZones; |
|
1026 |
SZone** prefZone = prefOrder; |
|
1027 |
TUint prefRank = 0; |
|
1028 |
for (; prefZone < lastPref; prefZone++, prefRank++) |
|
1029 |
{ |
|
1030 |
SZone& zone = **prefZone; |
|
1031 |
iZonePrefList.Add(&zone.iPrefLink); |
|
1032 |
zone.iPrefRank = prefRank; |
|
1033 |
} |
|
1034 |
Kern::Free(prefOrder); // Remove temporary allocation |
|
1035 |
||
1036 |
/////////////////////////////////////////////////////////////////////////// |
|
1037 |
// Now mark any regions reserved by the base port as allocated and not |
|
1038 |
// for use by the RAM allocator. |
|
1039 |
/////////////////////////////////////////////////////////////////////////// |
|
1040 |
const SRamBank* pB = lastBank + 1; // first reserved block specifier |
|
1041 |
for (; pB->iSize; ++pB) |
|
1042 |
{ |
|
1043 |
__KTRACE_OPT(KMMU, Kern::Printf("Reserve physical block %08x+%x", pB->iBase, pB->iSize)); |
|
1044 |
TInt r = SetPhysicalRamState(pB->iBase, pB->iSize, EFalse, EPageFixed); |
|
1045 |
__KTRACE_OPT(KMMU, Kern::Printf("Reserve returns %d", r)); |
|
1046 |
if (r!=KErrNone) |
|
1047 |
{ |
|
1048 |
Panic(ECreateInvalidReserveBank); |
|
1049 |
} |
|
1050 |
#ifdef BTRACE_KERNEL_MEMORY |
|
1051 |
BTrace8(BTrace::EKernelMemory, BTrace::EKernelMemoryDrvPhysAlloc, pB->iSize, pB->iBase); |
|
1052 |
Epoc::DriverAllocdPhysRam += pB->iSize; |
|
1053 |
#endif |
|
1054 |
#ifndef __MEMMODEL_FLEXIBLE__ // Mmu::Init2Common() handles this in FMM. |
|
1055 |
// Synchronise the SPageInfo with any blocks that were reserved by |
|
1056 |
// marking any reserved regions as locked |
|
1057 |
TPhysAddr physAddrEnd = pB->iBase + pB->iSize; |
|
1058 |
TPhysAddr physAddr = pB->iBase; |
|
1059 |
for(; physAddr < physAddrEnd; physAddr += KPageSize) |
|
1060 |
{ |
|
1061 |
SPageInfo* pi = SPageInfo::FromPhysAddr(physAddr); |
|
1062 |
pi->Lock(); |
|
1063 |
} |
|
1064 |
#endif |
|
1065 |
} |
|
1066 |
||
1067 |
////////////////////////////////////////////////////////////////////////// |
|
1068 |
// Now that we have have the RAM zone preference list and know how many |
|
1069 |
// allocatable pages there are, set iZoneLeastMovDis to be the RAM zone |
|
1070 |
// that will be used when half of the RAM is in use. This a boot up |
|
1071 |
// optimisation to reduce the amount of moving and/or discarding fixed page |
|
1072 |
// allocations will have to make during boot. |
|
1073 |
////////////////////////////////////////////////////////////////////////// |
|
1074 |
TUint halfAllocatablePages = iTotalFreeRamPages >> 1; |
|
1075 |
TUint pages = 0; |
|
1076 |
SDblQueLink* link = &iZonePrefList.iA; |
|
1077 |
do |
|
1078 |
{ |
|
1079 |
link = link->iNext; |
|
1080 |
__NK_ASSERT_DEBUG(link != &iZonePrefList.iA); |
|
1081 |
SZone& zonePages = *_LOFF(link, SZone, iPrefLink); |
|
1082 |
pages += zonePages.iFreePages; |
|
1083 |
} |
|
1084 |
while(pages < halfAllocatablePages); |
|
1085 |
iZoneLeastMovDis = link; |
|
1086 |
iZoneLeastMovDisRank = _LOFF(link, SZone, iPrefLink)->iPrefRank; |
|
1087 |
||
1088 |
// Reset general defrag links. |
|
1089 |
iZoneGeneralPrefLink = NULL; |
|
1090 |
iZoneGeneralTmpLink = NULL; |
|
1091 |
||
1092 |
__KTRACE_OPT(KMMU,DebugDump()); |
|
1093 |
} |
|
1094 |
||
1095 |
||
1096 |
void DRamAllocator::MarkPagesAllocated(TPhysAddr aAddr, TInt aCount, TZonePageType aType) |
|
1097 |
{ |
|
1098 |
__KTRACE_OPT(KMMU,Kern::Printf("DRamAllocator::MarkPagesAllocated(%x+%x)",aAddr,aCount)); |
|
1099 |
||
1100 |
M::RamAllocIsLocked(); |
|
1101 |
||
1102 |
// Don't allow unknown pages to be allocated, saves extra 'if' when |
|
1103 |
// creating bmaType. |
|
1104 |
__NK_ASSERT_DEBUG(aType != EPageUnknown); |
|
1105 |
||
1106 |
__ASSERT_DEBUG( !(TUint32(aAddr) & (KPageSize - 1)) && |
|
1107 |
(TUint32(aAddr) < TUint32(iPhysAddrTop)) && |
|
1108 |
(TUint32(aAddr) >= TUint32(iPhysAddrBase))&& |
|
1109 |
(TUint32((aCount << KPageShift) -1 + aAddr) <= TUint32(iPhysAddrTop)), |
|
1110 |
Panic(EDoMarkPagesAllocated1)); |
|
1111 |
||
1112 |
iTotalFreeRamPages-=aCount; |
|
1113 |
// Find the 1st zone the 1st set of allocations belong to |
|
1114 |
TInt offset = 0; |
|
1115 |
SZone* pZ = GetZoneAndOffset(aAddr,offset); |
|
1116 |
if (pZ == NULL) |
|
1117 |
{//aAddr not in RAM |
|
1118 |
Panic(EDoMarkPagesAllocated1); |
|
1119 |
} |
|
1120 |
while(aCount) |
|
1121 |
{ |
|
1122 |
TBitMapAllocator& bmaAll = *(pZ->iBma[KBmaAllPages]); |
|
1123 |
TBitMapAllocator& bmaType = *(pZ->iBma[aType]); |
|
1124 |
TInt count = Min(bmaAll.iSize - offset, aCount); |
|
1125 |
bmaAll.Alloc(offset, count); |
|
1126 |
bmaType.Alloc(offset, count); |
|
1127 |
ZoneAllocPages(pZ, count, aType); |
|
1128 |
aCount -= count; |
|
1129 |
||
1130 |
// If spanning zones then ensure the next zone is contiguous. |
|
1131 |
__ASSERT_DEBUG(!aCount || ((pZ + 1)->iPhysBase != 0 && ((pZ + 1)->iPhysBase - 1) == pZ->iPhysEnd), Panic(EDoMarkPagesAllocated1)); |
|
1132 |
||
1133 |
pZ++; // zones in physical address order so move to next one |
|
1134 |
offset = 0; // and reset offset to start of the zone |
|
1135 |
} |
|
1136 |
} |
|
1137 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1138 |
|
0 | 1139 |
TInt DRamAllocator::MarkPageAllocated(TPhysAddr aAddr, TZonePageType aType) |
1140 |
{ |
|
1141 |
__KTRACE_OPT(KMMU,Kern::Printf("DRamAllocator::MarkPageAllocated %08x",aAddr)); |
|
1142 |
||
1143 |
M::RamAllocIsLocked(); |
|
1144 |
||
1145 |
// Don't allow unknown pages to be allocated, saves extra 'if' when |
|
1146 |
// creating bmaType. |
|
1147 |
__NK_ASSERT_DEBUG(aType != EPageUnknown); |
|
1148 |
||
1149 |
TInt n; |
|
1150 |
SZone* z=GetZoneAndOffset(aAddr,n); |
|
1151 |
if (!z) |
|
1152 |
{ |
|
1153 |
return KErrArgument; |
|
1154 |
} |
|
1155 |
__KTRACE_OPT(KMMU2,Kern::Printf("Zone index %d page index %04x",z-iZones,n)); |
|
1156 |
TBitMapAllocator& bmaAll = *(z->iBma[KBmaAllPages]); |
|
1157 |
TBitMapAllocator& bmaType = *(z->iBma[aType]); |
|
1158 |
if (bmaAll.NotFree(n,1)) |
|
1159 |
{ |
|
1160 |
__KTRACE_OPT(KMMU,Kern::Printf("Page already allocated")); |
|
1161 |
return KErrAlreadyExists; // page is already allocated |
|
1162 |
} |
|
1163 |
bmaAll.Alloc(n,1); |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1164 |
if (bmaType.NotAllocated(n,1)) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1165 |
bmaType.Alloc(n,1); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1166 |
#ifdef _DEBUG |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1167 |
else // Allow this page to already be reserved in bmaType as AllocContiguousRam() may have done this. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1168 |
__NK_ASSERT_DEBUG(aType == EPageFixed); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1169 |
#endif |
0 | 1170 |
--iTotalFreeRamPages; |
1171 |
ZoneAllocPages(z, 1, aType); |
|
1172 |
__KTRACE_OPT(KMMU,Kern::Printf("Total free RAM pages now = %d",iTotalFreeRamPages)); |
|
1173 |
||
1174 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
1175 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocMarkAllocated, aType, aAddr); |
|
1176 |
#endif |
|
1177 |
return KErrNone; |
|
1178 |
} |
|
1179 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1180 |
|
0 | 1181 |
TInt DRamAllocator::FreeRamPage(TPhysAddr aAddr, TZonePageType aType) |
1182 |
{ |
|
1183 |
__KTRACE_OPT(KMMU,Kern::Printf("FreeRamPage %08x",aAddr)); |
|
1184 |
||
1185 |
M::RamAllocIsLocked(); |
|
1186 |
||
1187 |
#ifdef _DEBUG |
|
1188 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1189 |
// Check lock counter of the page |
|
1190 |
if (aAddr != KPhysAddrInvalid) |
|
1191 |
{ |
|
1192 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(aAddr); |
|
1193 |
if(pi && pi->LockCount()) |
|
1194 |
Panic(EFreeingLockedPage); |
|
1195 |
} |
|
1196 |
#endif |
|
1197 |
// Don't allow unknown pages to be freed, saves extra 'if' when |
|
1198 |
// creating bmaType. |
|
1199 |
__NK_ASSERT_DEBUG(aType != EPageUnknown); |
|
1200 |
#endif |
|
1201 |
||
1202 |
TInt n; |
|
1203 |
SZone* z=GetZoneAndOffset(aAddr,n); |
|
1204 |
if (!z) |
|
1205 |
{ |
|
1206 |
return KErrArgument; |
|
1207 |
} |
|
1208 |
__KTRACE_OPT(KMMU2,Kern::Printf("Zone index %d page index %04x",z-iZones,n)); |
|
1209 |
TBitMapAllocator& bmaAll = *(z->iBma[KBmaAllPages]); |
|
1210 |
TBitMapAllocator& bmaType = *(z->iBma[aType]); |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1211 |
|
0 | 1212 |
bmaType.Free(n); |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1213 |
if (iContiguousReserved && aType != EPageFixed && z->iBma[EPageFixed]->NotFree(n, 1)) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1214 |
{// This page has been reserved by AllocContiguous() so don't free it |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1215 |
// but allocate it as fixed. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1216 |
ZoneFreePages(z, 1, aType); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1217 |
ZoneAllocPages(z, 1, EPageFixed); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1218 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1219 |
else |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1220 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1221 |
bmaAll.Free(n); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1222 |
++iTotalFreeRamPages; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1223 |
ZoneFreePages(z, 1, aType); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1224 |
} |
0 | 1225 |
#ifdef BTRACE_RAM_ALLOCATOR |
1226 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocFreePage, aType, aAddr); |
|
1227 |
#endif |
|
1228 |
return KErrNone; |
|
1229 |
} |
|
1230 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1231 |
|
0 | 1232 |
void DRamAllocator::FreeRamPages(TPhysAddr* aPageList, TInt aNumPages, TZonePageType aType) |
1233 |
{ |
|
1234 |
__KTRACE_OPT(KMMU,Kern::Printf("FreeRamPages count=%08x",aNumPages)); |
|
1235 |
||
1236 |
M::RamAllocIsLocked(); |
|
1237 |
||
1238 |
#if defined(_DEBUG) && !defined(__MEMMODEL_FLEXIBLE__) |
|
1239 |
// Check lock counter for each page that is about to be freed. |
|
1240 |
TInt pageNum = aNumPages; |
|
1241 |
TPhysAddr* pageList = aPageList; |
|
1242 |
while (pageNum--) |
|
1243 |
{ |
|
1244 |
TPhysAddr pa = *pageList++; |
|
1245 |
if (pa == KPhysAddrInvalid) |
|
1246 |
continue; |
|
1247 |
SPageInfo* pi = SPageInfo::SafeFromPhysAddr(pa); |
|
1248 |
if(pi && pi->LockCount()) |
|
1249 |
Panic(EFreeingLockedPage); |
|
1250 |
} |
|
1251 |
#endif |
|
1252 |
||
1253 |
while(aNumPages--) |
|
1254 |
{ |
|
1255 |
TPhysAddr first_pa = *aPageList++; |
|
1256 |
if (first_pa == KPhysAddrInvalid) |
|
1257 |
{ |
|
1258 |
continue; |
|
1259 |
} |
|
1260 |
TInt ix; |
|
1261 |
SZone* z = GetZoneAndOffset(first_pa,ix); |
|
1262 |
if (!z) |
|
1263 |
{ |
|
1264 |
continue; |
|
1265 |
} |
|
1266 |
TBitMapAllocator& bmaAll = *(z->iBma[KBmaAllPages]); |
|
1267 |
TInt zp_rem = bmaAll.iSize - ix; |
|
1268 |
__KTRACE_OPT(KMMU,Kern::Printf("1st PA=%08x Zone %d index %04x",first_pa,z-iZones,ix)); |
|
1269 |
TInt n = 1; |
|
1270 |
TPhysAddr pa = first_pa + KPageSize; |
|
1271 |
while (--zp_rem && aNumPages && *aPageList==pa) |
|
1272 |
{ |
|
1273 |
++n; |
|
1274 |
--aNumPages; |
|
1275 |
++aPageList; |
|
1276 |
pa += KPageSize; |
|
1277 |
} |
|
1278 |
__KTRACE_OPT(KMMU2,Kern::Printf("%d consecutive pages, zp_rem=%x, %d remaining pages",n,zp_rem,aNumPages)); |
|
1279 |
TBitMapAllocator& bmaType = *(z->iBma[aType]); |
|
1280 |
bmaType.Free(ix,n); |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1281 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1282 |
if (iContiguousReserved && aType != EPageFixed) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1283 |
{// See if a page has been reserved by AllocContiguous() in this range. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1284 |
TUint pagesFreed = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1285 |
TUint allocStart = ix; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1286 |
TUint freeOffset = ix; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1287 |
TUint endOffset = ix + n - 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1288 |
while (freeOffset <= endOffset) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1289 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1290 |
TUint runLength = NextAllocatedRun(z, allocStart, endOffset, EPageFixed); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1291 |
if (allocStart > freeOffset) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1292 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1293 |
TUint freed = allocStart - freeOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1294 |
bmaAll.Free(freeOffset, freed); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1295 |
pagesFreed += freed; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1296 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1297 |
allocStart += runLength; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1298 |
freeOffset = allocStart; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1299 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1300 |
iTotalFreeRamPages += pagesFreed; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1301 |
ZoneFreePages(z, n, aType); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1302 |
ZoneAllocPages(z, n - pagesFreed, EPageFixed); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1303 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1304 |
else |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1305 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1306 |
bmaAll.Free(ix,n); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1307 |
iTotalFreeRamPages += n; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1308 |
ZoneFreePages(z, n, aType); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1309 |
} |
0 | 1310 |
#ifdef BTRACE_RAM_ALLOCATOR |
1311 |
BTrace12(BTrace::ERamAllocator, BTrace::ERamAllocFreePages, aType, n, first_pa); |
|
1312 |
#endif |
|
1313 |
} |
|
1314 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
1315 |
BTrace0(BTrace::ERamAllocator, BTrace::ERamAllocFreePagesEnd); |
|
1316 |
#endif |
|
1317 |
} |
|
1318 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1319 |
|
0 | 1320 |
/** |
1321 |
Attempt to clear upto the required amount of discardable or movable pages |
|
1322 |
from the RAM zone. |
|
1323 |
||
1324 |
@param aZone The RAM zone to clear. |
|
1325 |
@param aRequiredPages The maximum number of pages to clear. |
|
1326 |
*/ |
|
1327 |
void DRamAllocator::ZoneClearPages(SZone& aZone, TUint aRequiredPages) |
|
1328 |
{ |
|
1329 |
__KTRACE_OPT(KMMU, |
|
1330 |
Kern::Printf("ZoneClearPages: ID 0x%x, req 0x%x", aZone.iId, aRequiredPages)); |
|
1331 |
// Discard the required number of discardable pages. |
|
1332 |
TUint offset = 0; |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1333 |
for (; aRequiredPages; offset++) |
0 | 1334 |
{ |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1335 |
TInt r = NextAllocatedPage(&aZone, offset, EPageDiscard); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1336 |
if (r != KErrNone) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1337 |
break; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1338 |
if (iContiguousReserved && aZone.iBma[EPageFixed]->NotFree(offset, 1)) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1339 |
{ |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1340 |
offset++; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1341 |
continue; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1342 |
} |
0 | 1343 |
TPhysAddr physAddr = (offset << KPageShift) + aZone.iPhysBase; |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1344 |
TInt discarded = M::DiscardPage(physAddr, aZone.iId, M::EMoveDisMoveDirty); |
0 | 1345 |
if (discarded == KErrNone) |
1346 |
{// The page was successfully discarded. |
|
1347 |
aRequiredPages--; |
|
1348 |
} |
|
1349 |
} |
|
1350 |
// Move the required number of movable pages. |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1351 |
for (offset = 0; aRequiredPages; offset++) |
0 | 1352 |
{ |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1353 |
TInt r = NextAllocatedPage(&aZone, offset, EPageMovable); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1354 |
if (r != KErrNone) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1355 |
break; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1356 |
if (iContiguousReserved && aZone.iBma[EPageFixed]->NotFree(offset, 1)) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1357 |
{ |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1358 |
offset++; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1359 |
continue; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1360 |
} |
0 | 1361 |
TPhysAddr physAddr = (offset << KPageShift) + aZone.iPhysBase; |
1362 |
TPhysAddr newAddr = KPhysAddrInvalid; |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1363 |
if (M::MovePage(physAddr, newAddr, aZone.iId, 0) == KErrNone) |
0 | 1364 |
{// The page was successfully moved. |
1365 |
#ifdef _DEBUG |
|
1366 |
TInt newOffset = 0; |
|
1367 |
SZone* newZone = GetZoneAndOffset(newAddr, newOffset); |
|
1368 |
__NK_ASSERT_DEBUG(newZone != &aZone); |
|
1369 |
#endif |
|
1370 |
aRequiredPages--; |
|
1371 |
} |
|
1372 |
} |
|
1373 |
} |
|
1374 |
||
1375 |
/** Attempt to allocate pages into a particular zone. Pages will not |
|
1376 |
always be contiguous. |
|
1377 |
||
1378 |
@param aPageList On return it will contain the addresses of any allocated pages |
|
1379 |
@param aZone The zone to allocate from |
|
1380 |
@param aNumPages The number of pages to allocate |
|
1381 |
@param aType The type of pages to allocate |
|
1382 |
@return The number of pages that were allocated |
|
1383 |
*/ |
|
1384 |
TUint32 DRamAllocator::ZoneFindPages(TPhysAddr*& aPageList, SZone& aZone, TUint32 aNumPages, TZonePageType aType) |
|
1385 |
{ |
|
1386 |
// Don't allow unknown pages to be allocated, saves extra 'if' when |
|
1387 |
// creating bmaType. |
|
1388 |
__NK_ASSERT_DEBUG(aType != EPageUnknown); |
|
1389 |
||
1390 |
TBitMapAllocator& bmaAll = *aZone.iBma[KBmaAllPages]; |
|
1391 |
TBitMapAllocator& bmaType = *(aZone.iBma[aType]); |
|
1392 |
TPhysAddr zpb = aZone.iPhysBase; |
|
1393 |
TInt got = bmaAll.AllocList(aNumPages, (TInt*)aPageList); |
|
1394 |
if (got) |
|
1395 |
{ |
|
1396 |
TPhysAddr* pE = aPageList + got; |
|
1397 |
while(aPageList < pE) |
|
1398 |
{ |
|
1399 |
TInt ix = *aPageList; |
|
1400 |
*aPageList++ = zpb + (ix << KPageShift); |
|
1401 |
__KTRACE_OPT(KMMU,Kern::Printf("Got page @%08x",zpb + (ix << KPageShift))); |
|
1402 |
||
1403 |
// Mark the page allocated on the page type bit map. |
|
1404 |
bmaType.Alloc(ix, 1); |
|
1405 |
} |
|
1406 |
ZoneAllocPages(&aZone, got, aType); |
|
1407 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
1408 |
BTrace12(BTrace::ERamAllocator, BTrace::ERamAllocRamPages, aType, got, *(pE-got)); |
|
1409 |
#endif |
|
1410 |
} |
|
1411 |
return got; |
|
1412 |
} |
|
1413 |
||
1414 |
/** |
|
1415 |
Allocate discontiguous pages. |
|
1416 |
||
1417 |
Fixed pages are always allocated into the most preferable RAM zone that has free, |
|
1418 |
movable or discardable pages in it. This is to avoid fixed pages being placed |
|
1419 |
in the less preferred RAM zones. |
|
1420 |
||
1421 |
Movable and discardable pages are allocated into the RAM zones currently in use. |
|
1422 |
An empty RAM zone will only be used (switched on) if there are not enough free |
|
1423 |
pages in the in use RAM zones. The pages will be allocated from the least |
|
1424 |
preferable RAM to be in use after the allocation to the more preferred RAM zones. |
|
1425 |
||
1426 |
If a valid zone is specified in aBlockedZoneId then that RAM zone will not be |
|
1427 |
allocated into. Also, if aBlockedZoneId and aBlockRest is set then the allocation |
|
1428 |
will stop if aBlockZoneId |
|
1429 |
||
1430 |
@param aPageList On success, will contain the address of each allocated page |
|
1431 |
@param aNumPages The number of the pages to allocate |
|
1432 |
@param aType The type of the pages to allocate |
|
1433 |
@param aBlockedZoneId The ID of the RAM zone that shouldn't be allocated into. |
|
1434 |
The default value has no effect. |
|
1435 |
@param aBlockRest Set to ETrue to stop this allocation using any currently empty |
|
1436 |
RAM zones, EFalse to allow empty RAM zones to be used. Only |
|
1437 |
effects movable and discardable allocations. |
|
1438 |
||
1439 |
@return 0 on success, the number of extra pages required to fulfill the request on failure. |
|
1440 |
*/ |
|
1441 |
TInt DRamAllocator::AllocRamPages(TPhysAddr* aPageList, TInt aNumPages, TZonePageType aType, TUint aBlockedZoneId, TBool aBlockRest) |
|
1442 |
{ |
|
1443 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocRamPages 0x%x type%d",aNumPages, aType)); |
|
1444 |
||
1445 |
M::RamAllocIsLocked(); |
|
1446 |
||
1447 |
// Should never allocate unknown pages. |
|
1448 |
__NK_ASSERT_DEBUG(aType != EPageUnknown); |
|
1449 |
||
1450 |
TPhysAddr* pageListBase = aPageList; |
|
1451 |
TUint32 numMissing = aNumPages; |
|
1452 |
||
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1453 |
if ((TUint)aNumPages > iTotalFreeRamPages) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1454 |
{// Not enough free pages to fulfill this request so return the amount required |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1455 |
return aNumPages - iTotalFreeRamPages; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1456 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1457 |
|
0 | 1458 |
if (aType == EPageFixed) |
1459 |
{// Currently only a general defrag operation should set this and it won't |
|
1460 |
// allocate fixed pages. |
|
1461 |
__NK_ASSERT_DEBUG(!aBlockRest); |
|
1462 |
||
1463 |
// Search through each zone in preference order until all pages allocated or |
|
1464 |
// have reached the end of the preference list |
|
1465 |
SDblQueLink* link = iZonePrefList.First(); |
|
1466 |
while (numMissing && link != &iZonePrefList.iA) |
|
1467 |
{ |
|
1468 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
1469 |
// Get the link to next zone before any potential reordering. |
|
1470 |
// Which would occur if previous zone is same preference and has |
|
1471 |
// more free space after this allocation. |
|
1472 |
link = link->iNext; |
|
1473 |
||
1474 |
if (zone.iId == aBlockedZoneId || NoAllocOfPageType(zone, aType)) |
|
1475 |
{// The flags disallow aType pages or all pages. |
|
1476 |
__KTRACE_OPT(KMMU2, Kern::Printf("ARP Flags 0x%08x", zone.iFlags)); |
|
1477 |
continue; |
|
1478 |
} |
|
1479 |
||
1480 |
numMissing -= ZoneFindPages(aPageList, zone, numMissing, aType); |
|
1481 |
__KTRACE_OPT(KMMU, Kern::Printf("zone.iId 0x%x", zone.iId)); |
|
1482 |
||
1483 |
if (numMissing && |
|
1484 |
(zone.iAllocPages[EPageMovable] || zone.iAllocPages[EPageDiscard])) |
|
1485 |
{// Not all the required pages where allocated and there are still some |
|
1486 |
// movable and discardable pages in this RAM zone. |
|
1487 |
ZoneClearPages(zone, numMissing); |
|
1488 |
||
1489 |
// Have discarded and moved everything required or possible so |
|
1490 |
// now allocate into the pages just freed. |
|
1491 |
numMissing -= ZoneFindPages(aPageList, zone, numMissing, aType); |
|
1492 |
} |
|
1493 |
} |
|
1494 |
} |
|
1495 |
else |
|
1496 |
{ |
|
1497 |
// Determine if there are enough free pages in the RAM zones in use. |
|
1498 |
TUint totalFreeInUse = 0; |
|
1499 |
SDblQueLink* link = iZoneLeastMovDis; |
|
1500 |
for(; link != &iZonePrefList.iA; link = link->iPrev) |
|
1501 |
{ |
|
1502 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
1503 |
if (zone.iId == aBlockedZoneId || NoAllocOfPageType(zone, aType) || |
|
1504 |
(aBlockRest && (zone.iFlags & KRamZoneFlagGenDefragBlock))) |
|
1505 |
{// The blocked RAM zone or flags disallow aType pages or all pages |
|
1506 |
__KTRACE_OPT(KMMU2, Kern::Printf("ARP Flags 0x%08x", zone.iFlags)); |
|
1507 |
continue; |
|
1508 |
} |
|
1509 |
totalFreeInUse += zone.iFreePages; |
|
1510 |
} |
|
1511 |
||
1512 |
if (aBlockRest && totalFreeInUse < (TUint)aNumPages) |
|
1513 |
{// Allocating as part of a general defragmentation and |
|
1514 |
// can't allocate without using a RAM zone less preferable than |
|
1515 |
// the current least prefeable RAM zone with movable and/or |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1516 |
// discardable. |
0 | 1517 |
__NK_ASSERT_DEBUG(numMissing); |
1518 |
goto exit; |
|
1519 |
} |
|
1520 |
||
1521 |
SDblQueLink* leastClearable = iZoneLeastMovDis; |
|
1522 |
while (totalFreeInUse < (TUint)aNumPages) |
|
1523 |
{// The amount of free pages in the RAM zones with movable |
|
1524 |
// and/or discardable isn't enough. |
|
1525 |
leastClearable = leastClearable->iNext; |
|
1526 |
if (leastClearable == &iZonePrefList.iA) |
|
1527 |
{// There are no more RAM zones to allocate into. |
|
1528 |
__NK_ASSERT_DEBUG(numMissing); |
|
1529 |
goto exit; |
|
1530 |
} |
|
1531 |
SZone& zone = *_LOFF(leastClearable, SZone, iPrefLink); |
|
1532 |
if (zone.iId == aBlockedZoneId || NoAllocOfPageType(zone, aType)) |
|
1533 |
{// The flags disallow aType pages or all pages |
|
1534 |
__KTRACE_OPT(KMMU2, Kern::Printf("ARP Flags 0x%08x", zone.iFlags)); |
|
1535 |
continue; |
|
1536 |
} |
|
1537 |
totalFreeInUse += zone.iFreePages; |
|
1538 |
} |
|
1539 |
// Now that we know exactly how many RAM zones will be required do |
|
1540 |
// the allocation. To reduce fixed allocations having to clear RAM |
|
1541 |
// zones, allocate from the least preferable RAM to be used |
|
1542 |
// to the most preferable RAM zone. |
|
1543 |
link = leastClearable; |
|
1544 |
while (numMissing) |
|
1545 |
{ |
|
1546 |
__NK_ASSERT_DEBUG(link != &iZonePrefList.iA); |
|
1547 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
1548 |
// Update the link before any reordering so we don't miss a RAM zone. |
|
1549 |
link = link->iPrev; |
|
1550 |
||
1551 |
if (zone.iId == aBlockedZoneId || NoAllocOfPageType(zone, aType) || |
|
1552 |
(aBlockRest && (zone.iFlags & KRamZoneFlagGenDefragBlock))) |
|
1553 |
{// The blocked RAM zone or flags disallow aType pages or all pages |
|
1554 |
__KTRACE_OPT(KMMU2, Kern::Printf("ARP Flags 0x%08x", zone.iFlags)); |
|
1555 |
continue; |
|
1556 |
} |
|
1557 |
||
1558 |
numMissing -= ZoneFindPages(aPageList, zone, numMissing, aType); |
|
1559 |
__KTRACE_OPT(KMMU, Kern::Printf("zone.iId 0x%x", zone.iId)); |
|
1560 |
} |
|
1561 |
__NK_ASSERT_DEBUG(!numMissing); |
|
1562 |
} |
|
1563 |
||
1564 |
exit: |
|
1565 |
// Update here so any call to FreeRamPages doesn't upset count |
|
1566 |
aNumPages -= numMissing; //set to number of pages that are allocated |
|
1567 |
iTotalFreeRamPages -= aNumPages; |
|
1568 |
||
1569 |
if (numMissing) |
|
1570 |
{// Couldn't allocate all required pages so free those that were allocated |
|
1571 |
FreeRamPages(pageListBase, aNumPages, aType); |
|
1572 |
} |
|
1573 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
1574 |
else |
|
1575 |
{ |
|
1576 |
BTrace0(BTrace::ERamAllocator, BTrace::ERamAllocRamPagesEnd); |
|
1577 |
} |
|
1578 |
#endif |
|
1579 |
return numMissing; |
|
1580 |
} |
|
1581 |
||
1582 |
||
1583 |
/** |
|
1584 |
Attempt to allocate discontiguous pages from the specified RAM zone. |
|
1585 |
||
1586 |
NOTE - This method only obeys the KRamZoneFlagNoAlloc and KRamZoneFlagClaiming |
|
1587 |
flags and not the others. |
|
1588 |
But as currently only EFixed pages will be allocated using this method that is |
|
1589 |
the desired behaviour. |
|
1590 |
||
1591 |
@param aZoneIdList An array of the IDs of the RAM zones to allocate from. |
|
1592 |
@param aZoneIdCount The number of IDs in aZoneIdList. |
|
1593 |
@param aPageList On success, will contain the address of each allocated page. |
|
1594 |
@param aNumPages The number of the pages to allocate. |
|
1595 |
@param aType The type of the pages to allocate. |
|
1596 |
||
1597 |
@return KErrNone on success, KErrNoMemory if allocation couldn't succeed or |
|
1598 |
the RAM zone has the KRamZoneFlagNoAlloc flag set, KErrArgument if a zone of |
|
1599 |
aZoneIdList doesn't exist or aNumPages is greater than the total pages in the zone. |
|
1600 |
*/ |
|
1601 |
TInt DRamAllocator::ZoneAllocRamPages(TUint* aZoneIdList, TUint aZoneIdCount, TPhysAddr* aPageList, TInt aNumPages, TZonePageType aType) |
|
1602 |
{ |
|
1603 |
M::RamAllocIsLocked(); |
|
1604 |
__NK_ASSERT_DEBUG(aType == EPageFixed); |
|
1605 |
||
1606 |
||
1607 |
__KTRACE_OPT(KMMU,Kern::Printf("ZoneAllocRamPages 0x%x zones 0x%x",aNumPages, aZoneIdCount)); |
|
1608 |
||
1609 |
TInt r = KErrNone; |
|
1610 |
TUint* zoneIdPtr = aZoneIdList; |
|
1611 |
TUint* zoneIdEnd = zoneIdPtr + aZoneIdCount; |
|
1612 |
TUint numMissing = aNumPages; |
|
1613 |
TUint physicalPages = 0; |
|
1614 |
TPhysAddr* pageListBase = aPageList; |
|
1615 |
||
1616 |
// Always loop through all the RAM zones so that if an invalid ID is specified |
|
1617 |
// it is always detected whether all the specified RAM zones were required |
|
1618 |
// for the allocation or not. |
|
1619 |
for(; zoneIdPtr < zoneIdEnd; zoneIdPtr++) |
|
1620 |
{ |
|
1621 |
SZone* zone = ZoneFromId(*zoneIdPtr); |
|
1622 |
||
1623 |
if (zone == NULL) |
|
1624 |
{// Invalid zone ID. |
|
1625 |
r = KErrArgument; |
|
1626 |
break; |
|
1627 |
} |
|
1628 |
||
1629 |
physicalPages += zone->iPhysPages; |
|
1630 |
||
1631 |
if (zone->iFlags & (KRamZoneFlagNoAlloc|KRamZoneFlagClaiming)) |
|
1632 |
{// If this RAM zone can't be allocated into then skip it. |
|
1633 |
continue; |
|
1634 |
} |
|
1635 |
||
1636 |
numMissing -= ZoneFindPages(aPageList, *zone, numMissing, aType); |
|
1637 |
||
1638 |
if (numMissing && aType == EPageFixed) |
|
1639 |
{// Remove up to required number of pages from the RAM zone |
|
1640 |
// and reattempt the allocation. |
|
1641 |
ZoneClearPages(*zone, numMissing); |
|
1642 |
numMissing -= ZoneFindPages(aPageList, *zone, numMissing, aType); |
|
1643 |
} |
|
1644 |
} |
|
1645 |
||
1646 |
// Update iTotalFreeRamPages here so that if allocation doesn't succeed then |
|
1647 |
// FreeRamPages() will keep it consistent. |
|
1648 |
TUint numAllocated = aNumPages - numMissing; |
|
1649 |
iTotalFreeRamPages -= numAllocated; |
|
1650 |
||
1651 |
if (r == KErrArgument || physicalPages < (TUint)aNumPages) |
|
1652 |
{// Invalid zone ID or the number of pages requested is too large. |
|
1653 |
// This should fail regardless of whether the allocation failed or not. |
|
1654 |
FreeRamPages(pageListBase, numAllocated, aType); |
|
1655 |
return KErrArgument; |
|
1656 |
} |
|
1657 |
||
1658 |
if (numMissing) |
|
1659 |
{// Couldn't allocate all required pages so free those that were allocated |
|
1660 |
FreeRamPages(pageListBase, numAllocated, aType); |
|
1661 |
return KErrNoMemory; |
|
1662 |
} |
|
1663 |
||
1664 |
// Have allocated all the required pages. |
|
1665 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
1666 |
BTrace0(BTrace::ERamAllocator, BTrace::ERamAllocZoneRamPagesEnd); |
|
1667 |
#endif |
|
1668 |
return KErrNone; |
|
1669 |
} |
|
1670 |
||
1671 |
||
1672 |
/** |
|
1673 |
Will return zones one at a time in the following search patterns until a suitable |
|
1674 |
zone has been found or it is determined that there is no suitable zone: |
|
1675 |
- preference order |
|
1676 |
- address order |
|
1677 |
Before the first call for a new search sequence must set: |
|
1678 |
iZoneTmpAddrIndex = -1; |
|
1679 |
iZoneTmpPrefLink = iZonePrefList.First(); |
|
1680 |
||
1681 |
@param aZone On return this will be a pointer to the next zone to search. |
|
1682 |
@param aState The current search state, i.e. which of the zone orderings to follow. |
|
1683 |
It will be updated if necessary by this function. |
|
1684 |
@param aType The type of page to be allocated. |
|
1685 |
@param aBlockedZoneId The ID of a RAM zone to not allocate into. |
|
1686 |
@param aBlockRest ETrue if allocation should fail as soon as a blocked zone is reached, |
|
1687 |
EFalse otherwise. (Currently not used) |
|
1688 |
@return ETrue a sutiable zone is found, EFalse when the allocation is not possible. |
|
1689 |
*/ |
|
1690 |
TBool DRamAllocator::NextAllocZone(SZone*& aZone, TZoneSearchState& aState, TZonePageType aType, TUint aBlockedZoneId, TBool aBlockRest) |
|
1691 |
{ |
|
1692 |
TUint currentState = aState; |
|
1693 |
TBool r = EFalse; |
|
1694 |
||
1695 |
for (; currentState < EZoneSearchEnd; currentState++) |
|
1696 |
{ |
|
1697 |
if (currentState == EZoneSearchAddr) |
|
1698 |
{ |
|
1699 |
iZoneTmpAddrIndex++; |
|
1700 |
for (; iZoneTmpAddrIndex < (TInt)iNumZones; iZoneTmpAddrIndex++) |
|
1701 |
{ |
|
1702 |
aZone = iZones + iZoneTmpAddrIndex; |
|
1703 |
if (aBlockedZoneId != aZone->iId && !NoAllocOfPageType(*aZone, aType)) |
|
1704 |
{ |
|
1705 |
r = ETrue; |
|
1706 |
goto exit; |
|
1707 |
} |
|
1708 |
} |
|
1709 |
} |
|
1710 |
else |
|
1711 |
{ |
|
1712 |
while(iZoneTmpPrefLink != &iZonePrefList.iA) |
|
1713 |
{ |
|
1714 |
aZone = _LOFF(iZoneTmpPrefLink, SZone, iPrefLink); |
|
1715 |
iZoneTmpPrefLink = iZoneTmpPrefLink->iNext; // Update before any re-ordering |
|
1716 |
if (aBlockedZoneId != aZone->iId && !NoAllocOfPageType(*aZone, aType)) |
|
1717 |
{ |
|
1718 |
r = ETrue; |
|
1719 |
goto exit; |
|
1720 |
} |
|
1721 |
} |
|
1722 |
} |
|
1723 |
} |
|
1724 |
exit: |
|
1725 |
__NK_ASSERT_DEBUG((r && currentState < EZoneSearchEnd) || (!r && currentState == EZoneSearchEnd)); |
|
1726 |
||
1727 |
aState = (TZoneSearchState)currentState; |
|
1728 |
return r; |
|
1729 |
} |
|
1730 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1731 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1732 |
#if !defined(__MEMMODEL_MULTIPLE__) && !defined(__MEMMODEL_MOVING__) |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1733 |
TUint DRamAllocator::BlockContiguousRegion(TPhysAddr aAddrBase, TUint aNumPages) |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1734 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1735 |
// Shouldn't be asked to block zero pages, addrEndPage would be wrong if we did. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1736 |
__NK_ASSERT_DEBUG(aNumPages); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1737 |
TPhysAddr addr = aAddrBase; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1738 |
TPhysAddr addrEndPage = aAddrBase + ((aNumPages - 1) << KPageShift); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1739 |
TInt tmpOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1740 |
SZone* endZone = GetZoneAndOffset(addrEndPage, tmpOffset); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1741 |
SZone* tmpZone; |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1742 |
TUint totalUnreserved = aNumPages; |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1743 |
do |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1744 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1745 |
tmpZone = GetZoneAndOffset(addr, tmpOffset); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1746 |
__NK_ASSERT_DEBUG(tmpZone != NULL); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1747 |
TUint runLength = (addrEndPage < tmpZone->iPhysEnd)? |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1748 |
((addrEndPage - addr) >> KPageShift) + 1: |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1749 |
tmpZone->iPhysPages - tmpOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1750 |
TUint reserved = tmpZone->iBma[KBmaAllPages]->SelectiveAlloc(tmpOffset, runLength); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1751 |
if (reserved) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1752 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1753 |
#ifdef _DEBUG |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1754 |
TUint runEnd = tmpOffset + runLength; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1755 |
TUint free = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1756 |
for (TUint i = tmpOffset; i < runEnd; i++) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1757 |
if (tmpZone->iBma[EPageMovable]->NotAllocated(i,1) && tmpZone->iBma[EPageDiscard]->NotAllocated(i,1)) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1758 |
free++; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1759 |
__NK_ASSERT_DEBUG(free == reserved); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1760 |
#endif |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1761 |
ZoneAllocPages(tmpZone, reserved, EPageFixed); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1762 |
iTotalFreeRamPages -= reserved; |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1763 |
totalUnreserved -= reserved; |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1764 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1765 |
tmpZone->iBma[EPageFixed]->Alloc(tmpOffset, runLength); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1766 |
addr = tmpZone->iPhysEnd + 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1767 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1768 |
while (tmpZone != endZone); |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1769 |
return totalUnreserved; |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1770 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1771 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1772 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1773 |
FORCE_INLINE void DRamAllocator::UnblockSetAllocRuns( TUint& aOffset1, TUint& aOffset2, |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1774 |
TUint aRunLength1, TUint aRunLength2, |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1775 |
TUint& aAllocLength, TUint& aAllocStart) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1776 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1777 |
aAllocStart = aOffset1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1778 |
aAllocLength = aRunLength1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1779 |
aOffset1 += aAllocLength; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1780 |
if (aOffset1 == aOffset2) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1781 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1782 |
aAllocLength += aRunLength2; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1783 |
aOffset2 += aRunLength2; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1784 |
aOffset1 = aOffset2; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1785 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1786 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1787 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1788 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1789 |
void DRamAllocator::UnblockContiguousRegion(TPhysAddr aAddrBase, TUint aNumPages) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1790 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1791 |
// Shouldn't be asked to unblock zero pages, addrEndPage would be wrong if we did. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1792 |
__NK_ASSERT_DEBUG(aNumPages); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1793 |
TPhysAddr addr = aAddrBase; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1794 |
TPhysAddr addrEndPage = aAddrBase + ((aNumPages - 1) << KPageShift); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1795 |
TInt tmpOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1796 |
SZone* endZone = GetZoneAndOffset(addrEndPage, tmpOffset); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1797 |
SZone* tmpZone; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1798 |
do |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1799 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1800 |
tmpZone = GetZoneAndOffset(addr, tmpOffset); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1801 |
__NK_ASSERT_DEBUG(tmpZone != NULL); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1802 |
TUint runLength = (addrEndPage < tmpZone->iPhysEnd)? |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1803 |
((addrEndPage - addr) >> KPageShift) + 1: |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1804 |
tmpZone->iPhysPages - tmpOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1805 |
TUint unreserved = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1806 |
TUint runEnd = tmpOffset + runLength - 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1807 |
TUint freeOffset = tmpOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1808 |
TUint discardOffset = freeOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1809 |
TUint movableOffset = freeOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1810 |
__KTRACE_OPT(KMMU2, Kern::Printf("freeOff %d, runEnd %d", freeOffset, runEnd)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1811 |
while (freeOffset <= runEnd) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1812 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1813 |
TUint discardRun; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1814 |
TUint movableRun; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1815 |
discardRun = NextAllocatedRun(tmpZone, discardOffset, runEnd, EPageDiscard); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1816 |
movableRun = NextAllocatedRun(tmpZone, movableOffset, runEnd, EPageMovable); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1817 |
TUint allocLength; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1818 |
TUint allocStart; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1819 |
__KTRACE_OPT(KMMU2, Kern::Printf("disOff %d len %d movOff %d len %d", discardOffset, discardRun, movableOffset, movableRun)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1820 |
if (discardOffset < movableOffset) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1821 |
UnblockSetAllocRuns(discardOffset, movableOffset, discardRun, movableRun, allocLength, allocStart); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1822 |
else |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1823 |
UnblockSetAllocRuns(movableOffset, discardOffset, movableRun, discardRun, allocLength, allocStart); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1824 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1825 |
if (allocStart > freeOffset) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1826 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1827 |
unreserved += allocStart - freeOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1828 |
tmpZone->iBma[KBmaAllPages]->Free(freeOffset, allocStart - freeOffset); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1829 |
__NK_ASSERT_DEBUG( !tmpZone->iBma[EPageMovable]->NotFree(freeOffset, allocStart - freeOffset) && |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1830 |
!tmpZone->iBma[EPageDiscard]->NotFree(freeOffset, allocStart - freeOffset)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1831 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1832 |
__KTRACE_OPT(KMMU2, Kern::Printf("disOff %d len %d movOff %d len %d start %d len %d", discardOffset, discardRun, movableOffset, movableRun, allocStart, allocLength)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1833 |
freeOffset = allocStart + allocLength; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1834 |
__KTRACE_OPT(KMMU2, Kern::Printf("freeOff %d", freeOffset)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1835 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1836 |
tmpZone->iBma[EPageFixed]->Free(tmpOffset, runLength); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1837 |
ZoneFreePages(tmpZone, unreserved, EPageFixed); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1838 |
iTotalFreeRamPages += unreserved; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1839 |
addr = tmpZone->iPhysEnd + 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1840 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1841 |
while (tmpZone != endZone); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1842 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1843 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1844 |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1845 |
TUint DRamAllocator::CountPagesInRun(TPhysAddr aAddrBase, TPhysAddr aAddrEndPage, TZonePageType aType) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1846 |
{ |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1847 |
__NK_ASSERT_DEBUG(aAddrBase <= aAddrEndPage); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1848 |
TUint totalAllocated = 0; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1849 |
TPhysAddr addr = aAddrBase; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1850 |
TUint tmpOffset; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1851 |
SZone* endZone = GetZoneAndOffset(aAddrEndPage, (TInt&)tmpOffset); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1852 |
SZone* tmpZone; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1853 |
do |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1854 |
{ |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1855 |
tmpZone = GetZoneAndOffset(addr, (TInt&)tmpOffset); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1856 |
__NK_ASSERT_DEBUG(tmpZone != NULL); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1857 |
TUint runLength = (aAddrEndPage < tmpZone->iPhysEnd)? |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1858 |
((aAddrEndPage - addr) >> KPageShift) + 1: |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1859 |
tmpZone->iPhysPages - tmpOffset; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1860 |
TUint runEnd = tmpOffset + runLength - 1; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1861 |
while (tmpOffset <= runEnd) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1862 |
{ |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1863 |
TUint run = NextAllocatedRun(tmpZone, tmpOffset, runEnd, aType); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1864 |
totalAllocated += run; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1865 |
tmpOffset += run; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1866 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1867 |
addr = tmpZone->iPhysEnd + 1; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1868 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1869 |
while (tmpZone != endZone); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1870 |
return totalAllocated; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1871 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1872 |
|
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1873 |
|
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1874 |
TInt DRamAllocator::ClearContiguousRegion(TPhysAddr aAddrBase, TPhysAddr aZoneBase, TUint aNumPages, TInt& aOffset, TUint aUnreservedPages) |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1875 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1876 |
TPhysAddr addr = aAddrBase; |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1877 |
TPhysAddr addrEndPage = aAddrBase + ((aNumPages -1 )<< KPageShift); |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1878 |
TInt contigOffset = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1879 |
SZone* contigZone = GetZoneAndOffset(addr, contigOffset); |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1880 |
TUint unreservedPages = aUnreservedPages; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1881 |
for (; addr <= addrEndPage; addr += KPageSize, contigOffset++) |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1882 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1883 |
if (contigZone->iPhysEnd < addr) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1884 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1885 |
contigZone = GetZoneAndOffset(addr, contigOffset); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1886 |
__NK_ASSERT_DEBUG(contigZone != NULL); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1887 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1888 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1889 |
__NK_ASSERT_DEBUG(contigZone != NULL); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1890 |
__NK_ASSERT_DEBUG(contigZone->iBma[EPageFixed]->NotFree(contigOffset, 1)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1891 |
__NK_ASSERT_DEBUG(SPageInfo::SafeFromPhysAddr(addr) != NULL); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1892 |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1893 |
if (unreservedPages > iTotalFreeRamPages) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1894 |
{// May need to discard some pages so there is free space for the |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1895 |
// pages in the contiguous run to be moved to. |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1896 |
TUint requiredPages = unreservedPages - iTotalFreeRamPages; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1897 |
if (requiredPages) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1898 |
{// Ask the pager to get free some pages. |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1899 |
M::GetFreePages(requiredPages); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1900 |
|
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1901 |
// The ram alloc lock may have been flashed so ensure that we still have |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1902 |
// enough free ram to complete the allocation. |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1903 |
TUint remainingPages = ((addrEndPage - addr) >> KPageShift) + 1; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1904 |
unreservedPages = remainingPages - CountPagesInRun(addr, addrEndPage, EPageFixed); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1905 |
if (unreservedPages > iTotalFreeRamPages + M::NumberOfFreeDpPages()) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1906 |
{// Not enough free space and not enough freeable pages. |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1907 |
return KErrNoMemory; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1908 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1909 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1910 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1911 |
|
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1912 |
TInt r = M::MoveAndAllocPage(addr, EPageFixed); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1913 |
if (r != KErrNone) |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1914 |
{// This page couldn't be moved or discarded so |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1915 |
// restart the search the page after this one. |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1916 |
__KTRACE_OPT(KMMU2, Kern::Printf("ContigMov fail contigOffset 0x%x r %d", contigOffset, r)); |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1917 |
aOffset = (addr < aZoneBase)? 0 : contigOffset + 1; |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1918 |
return r; |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1919 |
} |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1920 |
__NK_ASSERT_DEBUG(contigZone->iBma[EPageFixed]->NotFree(contigOffset, 1)); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1921 |
__NK_ASSERT_DEBUG(contigZone->iBma[KBmaAllPages]->NotFree(contigOffset, 1)); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1922 |
__NK_ASSERT_DEBUG(contigZone->iBma[EPageDiscard]->NotAllocated(contigOffset, 1)); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1923 |
__NK_ASSERT_DEBUG(contigZone->iBma[EPageMovable]->NotAllocated(contigOffset, 1)); |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1924 |
} |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1925 |
|
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1926 |
// Successfully cleared the contiguous run |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1927 |
return KErrNone; |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1928 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1929 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1930 |
|
0 | 1931 |
/** |
1932 |
Search through the zones for the requested contiguous RAM, first in preference |
|
1933 |
order then, if that fails, in address order. |
|
1934 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1935 |
No support for non-fixed pages as this will discard and move pages if required. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1936 |
|
0 | 1937 |
@param aNumPages The number of contiguous pages to find |
1938 |
@param aPhysAddr Will contain the base address of any contiguous run if found |
|
1939 |
@param aAlign Alignment specified as the alignment shift |
|
1940 |
in preference ordering. EFalse otherwise. |
|
1941 |
||
1942 |
@return KErrNone on success, KErrNoMemory otherwise |
|
1943 |
*/ |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1944 |
TInt DRamAllocator::AllocContiguousRam(TUint aNumPages, TPhysAddr& aPhysAddr, TInt aAlign) |
0 | 1945 |
{ |
1946 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocContiguousRam size %08x align %d",aNumPages,aAlign)); |
|
1947 |
||
1948 |
M::RamAllocIsLocked(); |
|
1949 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1950 |
if ((TUint)aNumPages > iTotalFreeRamPages + M::NumberOfFreeDpPages()) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1951 |
{// Not enough free space and not enough freeable pages. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1952 |
return KErrNoMemory; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1953 |
} |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1954 |
if (aNumPages > iTotalFreeRamPages) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1955 |
{// Need to discard some pages so there is free space for the pages in |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1956 |
// the contiguous run to be moved to. |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1957 |
TUint requiredPages = aNumPages - iTotalFreeRamPages; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1958 |
if (!M::GetFreePages(requiredPages)) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1959 |
return KErrNoMemory; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1960 |
} |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1961 |
|
0 | 1962 |
TInt alignWrtPage = Max(aAlign - KPageShift, 0); |
1963 |
TUint32 alignmask = (1u << alignWrtPage) - 1; |
|
1964 |
||
1965 |
// Attempt to find enough pages searching in preference order first then |
|
1966 |
// in address order |
|
1967 |
TZoneSearchState searchState = EZoneSearchPref; |
|
1968 |
SZone* zone; |
|
1969 |
SZone* prevZone = NULL; |
|
1970 |
TInt carryAll = 0; // Carry for all pages bma, clear to start new run. |
|
1971 |
TInt carryImmov = 0; // Carry for immovable pages bma, clear to start new run. |
|
1972 |
TInt base = 0; |
|
1973 |
TInt offset = 0; |
|
1974 |
iZoneTmpAddrIndex = -1; |
|
1975 |
iZoneTmpPrefLink = iZonePrefList.First(); |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1976 |
while (NextAllocZone(zone, searchState, EPageFixed, KRamZoneInvalidId, EFalse)) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1977 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1978 |
// Be sure to start from scratch if zone not contiguous with previous zone |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1979 |
if (prevZone && (zone->iPhysBase == 0 || (zone->iPhysBase - 1) != prevZone->iPhysEnd)) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1980 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1981 |
carryAll = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1982 |
carryImmov = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1983 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1984 |
prevZone = zone; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1985 |
TBitMapAllocator& bmaAll = *(zone->iBma[KBmaAllPages]); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1986 |
base = TInt(zone->iPhysBase >> KPageShift); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1987 |
TInt runLength; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1988 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: base=%08x carryAll=%08x offset=%08x", base, carryAll, offset)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1989 |
offset = bmaAll.AllocAligned(aNumPages, alignWrtPage, base, EFalse, carryAll, runLength); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1990 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: offset=%08x", offset)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1991 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1992 |
if (offset >= 0) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1993 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1994 |
// Have found enough contiguous pages so return address of physical page |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1995 |
// at the start of the region |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1996 |
aPhysAddr = TPhysAddr((base + offset - carryAll + alignmask) & ~alignmask) << KPageShift; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1997 |
MarkPagesAllocated(aPhysAddr, aNumPages, EPageFixed); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1998 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1999 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocContiguousRam returns %08x",aPhysAddr)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2000 |
#ifdef BTRACE_RAM_ALLOCATOR |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2001 |
BTrace12(BTrace::ERamAllocator, BTrace::ERamAllocContiguousRam, EPageFixed, aNumPages, aPhysAddr); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2002 |
#endif |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2003 |
return KErrNone; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2004 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2005 |
// No run found when looking in just the free pages so see if this |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2006 |
// RAM zone could be used if pages where moved or discarded. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2007 |
TBitMapAllocator& bmaImmov = *(zone->iBma[EPageFixed]); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2008 |
offset = 0; // Clear so searches whole of fixed BMA on the first pass. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2009 |
do |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2010 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2011 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: base=%08x carryImmov=%08x offset=%08x", base, carryImmov, offset)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2012 |
offset = bmaImmov.AllocAligned(aNumPages, alignWrtPage, base, EFalse, carryImmov, runLength, offset); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2013 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: offset=%08x", offset)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2014 |
if (offset >= 0) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2015 |
{// Have found a run in immovable page bma so attempt to clear |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2016 |
// it for the allocation. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2017 |
TPhysAddr addrBase = TPhysAddr((base + offset - carryImmov + alignmask) & ~alignmask) << KPageShift; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2018 |
__KTRACE_OPT(KMMU2, Kern::Printf(">AllocContig fix run 0x%08x - 0x%08x 0x%x", addrBase, addrBase + (aNumPages << KPageShift), TheCurrentThread)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2019 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2020 |
// Block the contiguous region from being allocated. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2021 |
iContiguousReserved++; |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2022 |
TUint unreservedPages = BlockContiguousRegion(addrBase, aNumPages); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2023 |
TInt clearRet = ClearContiguousRegion(addrBase, zone->iPhysBase, aNumPages, offset, unreservedPages); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2024 |
if (clearRet == KErrNone) |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2025 |
{// Cleared all the required pages. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2026 |
// Return address of physical page at the start of the region. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2027 |
iContiguousReserved--; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2028 |
aPhysAddr = addrBase; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2029 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocContiguousRam returns %08x",aPhysAddr)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2030 |
#ifdef BTRACE_RAM_ALLOCATOR |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2031 |
BTrace12(BTrace::ERamAllocator, BTrace::ERamAllocContiguousRam, EPageFixed, aNumPages, aPhysAddr); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2032 |
#endif |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2033 |
__KTRACE_OPT(KMMU2, Kern::Printf("<AllocContig suc run 0x%08x - 0x%08x 0x%x", addrBase, addrBase + (aNumPages << KPageShift), TheCurrentThread)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2034 |
return KErrNone; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2035 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2036 |
else |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2037 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2038 |
// Unblock the contiguous region. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2039 |
UnblockContiguousRegion(addrBase, aNumPages); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2040 |
iContiguousReserved--; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2041 |
__KTRACE_OPT(KMMU2, Kern::Printf("ContigMov fail offset 0x%x carryImmov %x", |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2042 |
offset, carryImmov)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2043 |
// Can't rely on RAM zone preference ordering being |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2044 |
// the same so clear carrys and restart search from |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2045 |
// within the current RAM zone or skip onto the next |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2046 |
// one if at the end of this one. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2047 |
carryImmov = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2048 |
carryAll = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2049 |
__KTRACE_OPT(KMMU2, Kern::Printf("<AllocContigfail run 0x%08x - 0x%08x 0x%x", addrBase, addrBase + (aNumPages << KPageShift), TheCurrentThread)); |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2050 |
if (clearRet == KErrNoMemory) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2051 |
{// There are no longer enough free or discardable pages to |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2052 |
// be able to fulfill this allocation. |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2053 |
return KErrNoMemory; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2054 |
} |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2055 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2056 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2057 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2058 |
// Keep searching immovable page bma of the current RAM zone until |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2059 |
// gone past end of RAM zone or no run can be found. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2060 |
while (offset >= 0 && (TUint)offset < zone->iPhysPages); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2061 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2062 |
return KErrNoMemory; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2063 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2064 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2065 |
#else |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2066 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2067 |
/** |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2068 |
Search through the zones for the requested contiguous RAM, first in preference |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2069 |
order then, if that fails, in address order. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2070 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2071 |
No support for non-fixed pages as this will discard and move pages if required. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2072 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2073 |
@param aNumPages The number of contiguous pages to find |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2074 |
@param aPhysAddr Will contain the base address of any contiguous run if found |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2075 |
@param aAlign Alignment specified as the alignment shift |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2076 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2077 |
@return KErrNone on success, KErrNoMemory otherwise |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2078 |
*/ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2079 |
TInt DRamAllocator::AllocContiguousRam(TUint aNumPages, TPhysAddr& aPhysAddr, TInt aAlign) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2080 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2081 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocContiguousRam size %08x align %d",aNumPages,aAlign)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2082 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2083 |
M::RamAllocIsLocked(); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2084 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2085 |
TInt alignWrtPage = Max(aAlign - KPageShift, 0); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2086 |
TUint32 alignmask = (1u << alignWrtPage) - 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2087 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2088 |
// Attempt to find enough pages searching in preference order first then |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2089 |
// in address order |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2090 |
TZoneSearchState searchState = EZoneSearchPref; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2091 |
SZone* zone; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2092 |
SZone* prevZone = NULL; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2093 |
TInt carryAll = 0; // Carry for all pages bma, clear to start new run. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2094 |
TInt carryImmov = 0; // Carry for immovable pages bma, clear to start new run. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2095 |
TInt base = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2096 |
TInt offset = 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2097 |
iZoneTmpAddrIndex = -1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2098 |
iZoneTmpPrefLink = iZonePrefList.First(); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2099 |
while (NextAllocZone(zone, searchState, EPageFixed, KRamZoneInvalidId, EFalse)) |
0 | 2100 |
{ |
2101 |
// Be sure to start from scratch if zone not contiguous with previous zone |
|
2102 |
if (prevZone && (zone->iPhysBase == 0 || (zone->iPhysBase - 1) != prevZone->iPhysEnd)) |
|
2103 |
{ |
|
2104 |
carryAll = 0; |
|
2105 |
carryImmov = 0; |
|
2106 |
} |
|
2107 |
prevZone = zone; |
|
2108 |
TBitMapAllocator& bmaAll = *(zone->iBma[KBmaAllPages]); |
|
2109 |
base = TInt(zone->iPhysBase >> KPageShift); |
|
2110 |
TInt runLength; |
|
2111 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: base=%08x carryAll=%08x offset=%08x", base, carryAll, offset)); |
|
2112 |
offset = bmaAll.AllocAligned(aNumPages, alignWrtPage, base, EFalse, carryAll, runLength); |
|
2113 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: offset=%08x", offset)); |
|
2114 |
||
2115 |
if (offset >= 0) |
|
2116 |
{// Have found enough contiguous pages so return address of physical page |
|
2117 |
// at the start of the region |
|
2118 |
aPhysAddr = TPhysAddr((base + offset - carryAll + alignmask) & ~alignmask) << KPageShift; |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2119 |
MarkPagesAllocated(aPhysAddr, aNumPages, EPageFixed); |
0 | 2120 |
|
2121 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocContiguousRam returns %08x",aPhysAddr)); |
|
2122 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2123 |
BTrace12(BTrace::ERamAllocator, BTrace::ERamAllocContiguousRam, EPageFixed, aNumPages, aPhysAddr); |
0 | 2124 |
#endif |
2125 |
return KErrNone; |
|
2126 |
} |
|
2127 |
else |
|
2128 |
{// No run found when looking in just the free pages so see if this |
|
2129 |
// RAM zone could be used if pages where moved or discarded. |
|
2130 |
if (aNumPages > KMaxFreeableContiguousPages) |
|
2131 |
{// Can't move or discard any pages so move on to next RAM zone |
|
2132 |
// taking any run at the end of this RAM zone into account. |
|
2133 |
carryImmov = 0; |
|
2134 |
continue; |
|
2135 |
} |
|
2136 |
TBitMapAllocator& bmaImmov = *(zone->iBma[EPageFixed]); |
|
2137 |
offset = 0; // Clear so searches whole of fixed BMA on the first pass. |
|
2138 |
do |
|
2139 |
{ |
|
2140 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: base=%08x carryImmov=%08x offset=%08x", base, carryImmov, offset)); |
|
2141 |
offset = bmaImmov.AllocAligned(aNumPages, alignWrtPage, base, EFalse, carryImmov, runLength, offset); |
|
2142 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: offset=%08x", offset)); |
|
2143 |
if (offset >= 0) |
|
2144 |
{// Have found a run in immovable page bma so attempt to clear |
|
2145 |
// it for the allocation. |
|
2146 |
TPhysAddr addrBase = TPhysAddr((base + offset - carryImmov + alignmask) & ~alignmask) << KPageShift; |
|
2147 |
TPhysAddr addrEnd = addrBase + (aNumPages << KPageShift); |
|
2148 |
||
2149 |
// Block the RAM zones containing the contiguous region |
|
2150 |
// from being allocated into when pages are moved or replaced. |
|
2151 |
TPhysAddr addr = addrBase; |
|
2152 |
TInt tmpOffset; |
|
2153 |
SZone* tmpZone = GetZoneAndOffset(addr, tmpOffset); |
|
2154 |
while (addr < addrEnd-1) |
|
2155 |
{ |
|
2156 |
tmpZone->iFlags |= KRamZoneFlagTmpBlockAlloc; |
|
2157 |
addr = tmpZone->iPhysEnd; |
|
2158 |
tmpZone++; |
|
2159 |
} |
|
2160 |
||
2161 |
addr = addrBase; |
|
2162 |
TInt contigOffset = 0; |
|
2163 |
SZone* contigZone = GetZoneAndOffset(addr, contigOffset); |
|
2164 |
for (; addr != addrEnd; addr += KPageSize, contigOffset++) |
|
2165 |
{ |
|
2166 |
if (contigZone->iPhysEnd < addr) |
|
2167 |
{ |
|
2168 |
contigZone = GetZoneAndOffset(addr, contigOffset); |
|
2169 |
__NK_ASSERT_DEBUG(contigZone != NULL); |
|
2170 |
} |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2171 |
// This page shouldn't be allocated as fixed, only movable or discardable. |
0 | 2172 |
__NK_ASSERT_DEBUG(contigZone != NULL); |
2173 |
__NK_ASSERT_DEBUG(contigZone->iBma[EPageFixed]->NotAllocated(contigOffset, 1)); |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2174 |
__NK_ASSERT_DEBUG(SPageInfo::SafeFromPhysAddr(addr) != NULL); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2175 |
|
0 | 2176 |
TPhysAddr newAddr; |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2177 |
TInt moveRet = M::MovePage(addr, newAddr, contigZone->iId, 0); |
0 | 2178 |
if (moveRet != KErrNone && moveRet != KErrNotFound) |
2179 |
{// This page couldn't be moved or discarded so |
|
2180 |
// restart the search the page after this one. |
|
2181 |
__KTRACE_OPT(KMMU2, |
|
2182 |
Kern::Printf("ContigMov fail offset %x moveRet %d addr %x carryImmov %x", |
|
2183 |
offset, moveRet, addr, carryImmov)); |
|
2184 |
// Can't rely on RAM zone preference ordering being |
|
2185 |
// the same so clear carrys and restart search from |
|
2186 |
// within the current RAM zone or skip onto the next |
|
2187 |
// one if at the end of this one. |
|
2188 |
carryImmov = 0; |
|
2189 |
carryAll = 0; |
|
2190 |
offset = (addr < zone->iPhysBase)? 0 : contigOffset + 1; |
|
2191 |
__KTRACE_OPT(KMMU2, Kern::Printf("ContigMov fail offset %x", offset)); |
|
2192 |
break; |
|
2193 |
} |
|
2194 |
} |
|
2195 |
// Unblock the RAM zones containing the contiguous region. |
|
2196 |
TPhysAddr flagAddr = addrBase; |
|
2197 |
tmpZone = GetZoneAndOffset(flagAddr, tmpOffset); |
|
2198 |
while (flagAddr < addrEnd-1) |
|
2199 |
{ |
|
2200 |
tmpZone->iFlags &= ~KRamZoneFlagTmpBlockAlloc; |
|
2201 |
flagAddr = tmpZone->iPhysEnd; |
|
2202 |
tmpZone++; |
|
2203 |
} |
|
2204 |
||
2205 |
if (addr == addrEnd) |
|
2206 |
{// Cleared all the required pages so allocate them. |
|
2207 |
// Return address of physical page at the start of the region. |
|
2208 |
aPhysAddr = addrBase; |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2209 |
MarkPagesAllocated(aPhysAddr, aNumPages, EPageFixed); |
0 | 2210 |
|
2211 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocContiguousRam returns %08x",aPhysAddr)); |
|
2212 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2213 |
BTrace12(BTrace::ERamAllocator, BTrace::ERamAllocContiguousRam, EPageFixed, aNumPages, aPhysAddr); |
0 | 2214 |
#endif |
2215 |
return KErrNone; |
|
2216 |
} |
|
2217 |
} |
|
2218 |
} |
|
2219 |
// Keep searching immovable page bma of the current RAM zone until |
|
2220 |
// gone past end of RAM zone or no run can be found. |
|
2221 |
while (offset >= 0 && (TUint)offset < zone->iPhysPages); |
|
2222 |
} |
|
2223 |
} |
|
2224 |
return KErrNoMemory; |
|
2225 |
} |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2226 |
#endif // !defined(__MEMODEL_MULTIPLE__) || !defined(__MEMODEL_MOVING__) |
0 | 2227 |
|
2228 |
||
2229 |
/** |
|
2230 |
Attempt to allocate the contiguous RAM from the specified zone. |
|
2231 |
||
2232 |
NOTE - This method only obeys the KRamZoneFlagNoAlloc and KRamZoneFlagClaiming |
|
2233 |
flags and not the others. |
|
2234 |
But as currently only EFixed pages will be allocated using this method that is |
|
2235 |
the desired behaviour. |
|
2236 |
||
2237 |
@param aZoneIdList An array of the IDs of the RAM zones to allocate from. |
|
2238 |
@param aZoneIdCount The number of the IDs listed by aZoneIdList. |
|
2239 |
@param aSize The number of contiguous bytes to find |
|
2240 |
@param aPhysAddr Will contain the base address of the contiguous run if found |
|
2241 |
@param aAlign Alignment specified as the alignment shift |
|
2242 |
||
2243 |
@return KErrNone on success, KErrNoMemory if allocation couldn't succeed or |
|
2244 |
the RAM zone has the KRamZoneFlagNoAlloc flag set. KErrArgument if a zone of |
|
2245 |
aZoneIdList exists or if aSize is larger than the size of the zone. |
|
2246 |
*/ |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2247 |
TInt DRamAllocator::ZoneAllocContiguousRam(TUint* aZoneIdList, TUint aZoneIdCount, TInt aSize, TPhysAddr& aPhysAddr, TInt aAlign) |
0 | 2248 |
{ |
2249 |
__KTRACE_OPT(KMMU,Kern::Printf("ZoneAllocContiguousRam zones 0x%x size 0x%08x align %d",aZoneIdCount, aSize, aAlign)); |
|
2250 |
||
2251 |
M::RamAllocIsLocked(); |
|
2252 |
||
2253 |
||
2254 |
TUint numPages = (aSize + KPageSize - 1) >> KPageShift; |
|
2255 |
TInt carry = 0; // must be zero as this is always the start of a new run |
|
2256 |
TInt alignWrtPage = Max(aAlign - KPageShift, 0); |
|
2257 |
TUint32 alignmask = (1u << alignWrtPage) - 1; |
|
2258 |
TInt offset = -1; |
|
2259 |
TInt base = 0; |
|
2260 |
||
2261 |
TUint physPages = 0; |
|
2262 |
TUint* zoneIdPtr = aZoneIdList; |
|
2263 |
TUint* zoneIdEnd = aZoneIdList + aZoneIdCount; |
|
2264 |
SZone* prevZone = NULL; |
|
2265 |
for (; zoneIdPtr < zoneIdEnd; zoneIdPtr++) |
|
2266 |
{ |
|
2267 |
SZone* zone = ZoneFromId(*zoneIdPtr); |
|
2268 |
if (zone == NULL) |
|
2269 |
{// Couldn't find zone of this ID or it isn't large enough |
|
2270 |
return KErrArgument; |
|
2271 |
} |
|
2272 |
physPages += zone->iPhysPages; |
|
2273 |
||
2274 |
if (offset >= 0 || |
|
2275 |
(zone->iFlags & (KRamZoneFlagNoAlloc|KRamZoneFlagClaiming))) |
|
2276 |
{// Keep searching through the RAM zones if the allocation |
|
2277 |
// has succeeded, to ensure the ID list is always fully verified or |
|
2278 |
// if this zone is currently blocked for further allocations. |
|
2279 |
continue; |
|
2280 |
} |
|
2281 |
||
2282 |
// Be sure to start from scratch if zone not contiguous with previous zone |
|
2283 |
if (prevZone && (zone->iPhysBase == 0 || (zone->iPhysBase - 1) != prevZone->iPhysEnd)) |
|
2284 |
{ |
|
2285 |
carry = 0; |
|
2286 |
} |
|
2287 |
prevZone = zone; |
|
2288 |
||
2289 |
TInt len; |
|
2290 |
TBitMapAllocator& bmaAll = *(zone->iBma[KBmaAllPages]); |
|
2291 |
base = TInt(zone->iPhysBase >> KPageShift); |
|
2292 |
||
2293 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: aBase=%08x aCarry=%08x", base, carry)); |
|
2294 |
offset = bmaAll.AllocAligned(numPages, alignWrtPage, base, EFalse, carry, len); |
|
2295 |
__KTRACE_OPT(KMMU,Kern::Printf("AllocAligned: offset=%08x", offset)); |
|
2296 |
} |
|
2297 |
||
2298 |
if (physPages < numPages) |
|
2299 |
{// The allocation requested is too large for the specified RAM zones. |
|
2300 |
return KErrArgument; |
|
2301 |
} |
|
2302 |
||
2303 |
if (offset < 0) |
|
2304 |
{// The allocation failed. |
|
2305 |
return KErrNoMemory; |
|
2306 |
} |
|
2307 |
||
2308 |
// Have found enough contiguous pages so mark the pages allocated and |
|
2309 |
// return address of physical page at the start of the region. |
|
2310 |
aPhysAddr = TPhysAddr((base + offset - carry + alignmask) & ~alignmask) << KPageShift; |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2311 |
MarkPagesAllocated(aPhysAddr, numPages, EPageFixed); |
0 | 2312 |
|
2313 |
__KTRACE_OPT(KMMU,Kern::Printf("ZoneAllocContiguousRam returns %08x",aPhysAddr)); |
|
2314 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2315 |
BTrace12(BTrace::ERamAllocator, BTrace::ERamAllocZoneContiguousRam, EPageFixed, numPages, aPhysAddr); |
0 | 2316 |
#endif |
2317 |
return KErrNone; |
|
2318 |
} |
|
2319 |
||
2320 |
||
2321 |
/** |
|
2322 |
Attempt to set the specified contiguous block of RAM pages to be either |
|
2323 |
allocated or free. |
|
2324 |
||
2325 |
@param aBase The base address of the RAM to update. |
|
2326 |
@param aSize The number of contiguous bytes of RAM to update. |
|
2327 |
@param aState Set to ETrue to free the RAM, EFalse to allocate the RAM. |
|
2328 |
@param aType The type of the pages being updated. |
|
2329 |
||
2330 |
@return KErrNone on success, KErrArgument if aBase is an invalid address, |
|
2331 |
KErrGeneral if a page being marked free is already free, |
|
2332 |
KErrInUse if the page being marked allocated is already allocated. |
|
2333 |
*/ |
|
2334 |
TInt DRamAllocator::SetPhysicalRamState(TPhysAddr aBase, TInt aSize, TBool aState, TZonePageType aType) |
|
2335 |
{ |
|
2336 |
M::RamAllocIsLocked(); |
|
2337 |
||
2338 |
__KTRACE_OPT(KMMU,Kern::Printf("SetPhysicalRamState(%08x,%x,%d)",aBase,aSize,aState?1:0)); |
|
2339 |
TUint32 pageMask = KPageSize-1; |
|
2340 |
aSize += (aBase & pageMask); |
|
2341 |
aBase &= ~pageMask; |
|
2342 |
TInt npages = (aSize + pageMask) >> KPageShift; |
|
2343 |
__KTRACE_OPT(KMMU,Kern::Printf("Rounded base %08x npages=%x",aBase,npages)); |
|
2344 |
TInt baseOffset; |
|
2345 |
SZone* baseZone = GetZoneAndOffset(aBase, baseOffset); |
|
2346 |
if (!baseZone || (TUint32)aSize > (iPhysAddrTop - aBase + 1)) |
|
2347 |
{ |
|
2348 |
return KErrArgument; |
|
2349 |
} |
|
2350 |
SZone* zone = baseZone; |
|
2351 |
SZone* zoneEnd = iZones + iNumZones; |
|
2352 |
TPhysAddr base = aBase; |
|
2353 |
TInt pagesLeft = npages; |
|
2354 |
TInt offset = baseOffset; |
|
2355 |
TInt pageCount = -1; |
|
2356 |
__KTRACE_OPT(KMMU2,Kern::Printf("Zone %x page index %x z=%08x zE=%08x n=%x base=%08x",zone->iId, offset, zone, zoneEnd, pagesLeft, base)); |
|
2357 |
for (; pagesLeft && zone < zoneEnd; ++zone) |
|
2358 |
{ |
|
2359 |
if (zone->iPhysBase + (offset << KPageShift) != base) |
|
2360 |
{// Zone not contiguous with current run of page, so have been |
|
2361 |
// asked to set the state of non-existent pages. |
|
2362 |
return KErrArgument; |
|
2363 |
} |
|
2364 |
||
2365 |
TBitMapAllocator& bmaAll = *(zone->iBma[KBmaAllPages]); |
|
2366 |
TInt zp_rem = bmaAll.iSize - offset; |
|
2367 |
pageCount = Min(pagesLeft, zp_rem); |
|
2368 |
__KTRACE_OPT(KMMU2,Kern::Printf("Zone %x pages %x+%x base %08x", zone->iId, offset, pageCount, base)); |
|
2369 |
if(aState) |
|
2370 |
{ |
|
2371 |
if(bmaAll.NotAllocated(offset, pageCount)) |
|
2372 |
{ |
|
2373 |
return KErrGeneral; |
|
2374 |
} |
|
2375 |
} |
|
2376 |
else |
|
2377 |
{ |
|
2378 |
if(bmaAll.NotFree(offset, pageCount)) |
|
2379 |
{ |
|
2380 |
return KErrInUse; |
|
2381 |
} |
|
2382 |
} |
|
2383 |
pagesLeft -= pageCount; |
|
2384 |
offset = 0; |
|
2385 |
base += (TPhysAddr(pageCount) << KPageShift); |
|
2386 |
} |
|
2387 |
if (pagesLeft) |
|
2388 |
{ |
|
2389 |
return KErrArgument; // not all of the specified range exists |
|
2390 |
} |
|
2391 |
||
2392 |
iTotalFreeRamPages += (aState ? npages : -npages); |
|
2393 |
zone = baseZone; |
|
2394 |
offset = baseOffset; |
|
2395 |
for (pagesLeft = npages; pagesLeft; pagesLeft -= pageCount) |
|
2396 |
{ |
|
2397 |
TBitMapAllocator& bmaAll = *(zone->iBma[KBmaAllPages]); |
|
2398 |
// Unknown and fixed pages share a bit map. |
|
2399 |
TBitMapAllocator& bmaType = *(zone->iBma[(aType != EPageUnknown)? aType : EPageFixed]); |
|
2400 |
TInt zp_rem = bmaAll.iSize - offset; |
|
2401 |
pageCount = Min(pagesLeft, zp_rem); |
|
2402 |
if (aState) |
|
2403 |
{ |
|
2404 |
bmaAll.Free(offset, pageCount); |
|
2405 |
bmaType.Free(offset, pageCount); |
|
2406 |
ZoneFreePages(zone, pageCount, aType); |
|
2407 |
} |
|
2408 |
else |
|
2409 |
{ |
|
2410 |
bmaAll.Alloc(offset, pageCount); |
|
2411 |
bmaType.Alloc(offset, pageCount); |
|
2412 |
ZoneAllocPages(zone, pageCount, aType); |
|
2413 |
} |
|
2414 |
__KTRACE_OPT(KMMU2,Kern::Printf("Zone %d pages %x+%x base %08x",zone-iZones, offset, pageCount, base)); |
|
2415 |
++zone; |
|
2416 |
offset = 0; |
|
2417 |
} |
|
2418 |
return KErrNone; |
|
2419 |
} |
|
2420 |
||
2421 |
/** Update the allocated page counts for the zone that is page is allocated into. |
|
2422 |
||
2423 |
@param aAddr The physical address of the page |
|
2424 |
@param aOldPageType The type the page was allocated as |
|
2425 |
@param aNewPageType The type the page is changing to |
|
2426 |
*/ |
|
2427 |
void DRamAllocator::ChangePageType(SPageInfo* aPageInfo, TZonePageType aOldType, TZonePageType aNewType) |
|
2428 |
{ |
|
2429 |
||
2430 |
TInt offset; |
|
2431 |
SZone* zone = GetZoneAndOffset(aPageInfo->PhysAddr(), offset); |
|
2432 |
#ifdef _DEBUG |
|
2433 |
// *********** System lock may be held while this is invoked so don't do******** |
|
2434 |
// *********** anything too slow and definitely don't call zone callback******** |
|
2435 |
M::RamAllocIsLocked(); |
|
2436 |
CHECK_PRECONDITIONS((MASK_THREAD_CRITICAL) & ~MASK_NO_FAST_MUTEX, "DRamAllocator::ChangePageType"); |
|
2437 |
||
2438 |
// Get zone page is in and on debug builds check that it is allocated |
|
2439 |
if (zone == NULL || zone->iBma[KBmaAllPages]->NotAllocated(offset, 1)) |
|
2440 |
{ |
|
2441 |
Panic(EAllocRamPagesInconsistent); |
|
2442 |
} |
|
2443 |
||
2444 |
// Check if adjusting counts is valid, i.e. won't cause a roll over |
|
2445 |
if (zone->iAllocPages[aOldType] - 1 > zone->iAllocPages[aOldType] || |
|
2446 |
zone->iAllocPages[aNewType] + 1 < zone->iAllocPages[aNewType]) |
|
2447 |
{ |
|
2448 |
__KTRACE_OPT(KMMU, Kern::Printf("ChangePageType Alloc Unk %x Fx %x Mv %x Dis %x",zone->iAllocPages[EPageUnknown], |
|
2449 |
zone->iAllocPages[EPageFixed], zone->iAllocPages[EPageMovable],zone->iAllocPages[EPageDiscard])); |
|
2450 |
Panic(EZonesCountErr); |
|
2451 |
} |
|
2452 |
#endif |
|
2453 |
||
2454 |
// Update the counts and bmas |
|
2455 |
zone->iAllocPages[aOldType]--; |
|
2456 |
zone->iBma[aOldType]->Free(offset); |
|
2457 |
zone->iAllocPages[aNewType]++; |
|
2458 |
zone->iBma[aNewType]->Alloc(offset, 1); |
|
2459 |
||
2460 |
__KTRACE_OPT(KMMU2, Kern::Printf("ChangePageType Alloc Unk %x Fx %x Mv %x Dis %x",zone->iAllocPages[EPageUnknown], |
|
2461 |
zone->iAllocPages[EPageFixed], zone->iAllocPages[EPageMovable],zone->iAllocPages[EPageDiscard])); |
|
2462 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
2463 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocChangePageType, aNewType, aPageInfo->PhysAddr()); |
|
2464 |
#endif |
|
2465 |
} |
|
2466 |
||
2467 |
/** |
|
2468 |
Get the next page in this zone that is allocated after this one. |
|
2469 |
||
2470 |
@param aZone The zone to find the next allocated page in. |
|
2471 |
@param aOffset On entry this is the offset from which the next allocated |
|
2472 |
page in the zone should be found, on return it will be the offset |
|
2473 |
of the next allocated page. |
|
2474 |
@return KErrNone if a next allocated page could be found, KErrNotFound if no more pages in |
|
2475 |
the zone after aOffset are allocated, KErrArgument if aOffset is outside the zone. |
|
2476 |
*/ |
|
2477 |
TInt DRamAllocator::NextAllocatedPage(SZone* aZone, TUint& aOffset, TZonePageType aType) const |
|
2478 |
{ |
|
2479 |
const TUint KWordAlignMask = KMaxTUint32 << 5; |
|
2480 |
||
2481 |
M::RamAllocIsLocked(); |
|
2482 |
||
2483 |
__NK_ASSERT_DEBUG(aZone - iZones < (TInt)iNumZones); |
|
2484 |
// Makes things simpler for bma selection. |
|
2485 |
__NK_ASSERT_DEBUG(aType != EPageUnknown); |
|
2486 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2487 |
TUint zoneEndOffset = aZone->iPhysPages - 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2488 |
if (aOffset > zoneEndOffset) |
0 | 2489 |
{// Starting point is outside the zone |
2490 |
return KErrArgument; |
|
2491 |
} |
|
2492 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2493 |
TUint wordIndex = aOffset >> 5; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2494 |
TUint endWordIndex = zoneEndOffset >> 5; |
0 | 2495 |
|
2496 |
// Select the BMA to search, |
|
2497 |
TUint bmaIndex = (aType == EPageTypes)? KBmaAllPages : aType; |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2498 |
TUint32* map = &(aZone->iBma[bmaIndex]->iMap[wordIndex]); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2499 |
TUint32* mapEnd = &(aZone->iBma[bmaIndex]->iMap[endWordIndex]); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2500 |
TUint32 bits = *map; |
0 | 2501 |
|
2502 |
// Set bits for pages before 'offset' (i.e. ones we want to ignore)... |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2503 |
bits |= ~(KMaxTUint32 >> (aOffset & ~KWordAlignMask)); |
0 | 2504 |
|
2505 |
// Find the first bit map word from aOffset in aZone with allocated pages |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2506 |
while (bits == KMaxTUint32 && map < mapEnd) |
0 | 2507 |
{ |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2508 |
bits = *++map; |
0 | 2509 |
} |
2510 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2511 |
if (map == mapEnd) |
0 | 2512 |
{// Have reached the last bit mask word so set the bits that are |
2513 |
// outside of the zone so that they are ignored. |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2514 |
bits |= (KMaxTUint32 >> (zoneEndOffset & ~KWordAlignMask)) >> 1; |
0 | 2515 |
} |
2516 |
||
2517 |
if (bits == KMaxTUint32) |
|
2518 |
{// No allocated pages found after aOffset in aZone. |
|
2519 |
return KErrNotFound; |
|
2520 |
} |
|
2521 |
||
2522 |
// Now we have bits with allocated pages in it so determine the exact |
|
2523 |
// offset of the next allocated page |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2524 |
TInt msOne = __e32_find_ms1_32(~bits); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2525 |
__NK_ASSERT_DEBUG(msOne >= 0); // Must have at least one allocated page in the word. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2526 |
TUint msOneOffset = 31 - msOne; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2527 |
aOffset = ((map - aZone->iBma[bmaIndex]->iMap) << 5) + msOneOffset; |
0 | 2528 |
return KErrNone; |
2529 |
} |
|
2530 |
||
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2531 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2532 |
/** |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2533 |
Get the next run of pages in this zone that are allocated after aOffset. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2534 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2535 |
@param aZone The zone to find the next allocated page in. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2536 |
@param aOffset On entry this is the offset from which the next allocated |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2537 |
page in the zone should be found, on return it will be the offset |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2538 |
of the next allocated page. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2539 |
@param aEndOffset The last offset within this RAM zone to check for allocated runs. |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
2540 |
@return The length of any run found. |
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2541 |
*/ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2542 |
TInt DRamAllocator::NextAllocatedRun(SZone* aZone, TUint& aOffset, TUint aEndOffset, TZonePageType aType) const |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2543 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2544 |
const TUint KWordAlignMask = KMaxTUint32 << 5; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2545 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2546 |
M::RamAllocIsLocked(); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2547 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2548 |
__NK_ASSERT_DEBUG(aZone - iZones < (TInt)iNumZones); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2549 |
// Makes things simpler for bma selection. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2550 |
__NK_ASSERT_DEBUG(aType != EPageUnknown); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2551 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2552 |
if (aOffset > aEndOffset) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2553 |
{// UnblockContiguous() has already searched the whole range for this page type. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2554 |
return 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2555 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2556 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2557 |
TUint wordIndex = aOffset >> 5; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2558 |
TUint endWordIndex = aEndOffset >> 5; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2559 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2560 |
// Select the BMA to search, |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2561 |
TUint bmaIndex = (aType == EPageTypes)? KBmaAllPages : aType; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2562 |
TUint32* map = &(aZone->iBma[bmaIndex]->iMap[wordIndex]); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2563 |
TUint32* mapEnd = &(aZone->iBma[bmaIndex]->iMap[endWordIndex]); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2564 |
TUint32 bits = *map; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2565 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2566 |
// Set bits for pages before 'offset' (i.e. ones we want to ignore)... |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2567 |
bits |= ~(KMaxTUint32 >> (aOffset & ~KWordAlignMask)); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2568 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2569 |
// Find the first bit map word from aOffset in aZone with allocated pages |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2570 |
while (bits == KMaxTUint32 && map < mapEnd) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2571 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2572 |
bits = *++map; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2573 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2574 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2575 |
if (map == mapEnd) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2576 |
{// Have reached the last bit mask word so set the bits that are |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2577 |
// outside of the range so that they are ignored. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2578 |
bits |= (KMaxTUint32 >> (aEndOffset & ~KWordAlignMask)) >> 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2579 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2580 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2581 |
if (bits == KMaxTUint32) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2582 |
{// No allocated pages found in the range. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2583 |
aOffset = aEndOffset + 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2584 |
return 0; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2585 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2586 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2587 |
// Now we have bits with allocated pages in it so determine the exact |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2588 |
// offset of the next allocated page |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2589 |
TInt msOne = __e32_find_ms1_32(~bits); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2590 |
__NK_ASSERT_DEBUG(msOne >= 0); // Must have at least one allocated page in the word. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2591 |
TUint msOneOffset = 31 - msOne; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2592 |
aOffset = ((map - aZone->iBma[bmaIndex]->iMap) << 5) + msOneOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2593 |
TUint32* runWord = map; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2594 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2595 |
if (map < mapEnd && __e32_bit_count_32(~bits) == msOne + 1) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2596 |
{// The whole of the region in this word is allocated. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2597 |
// Find the next word which isn't completely allocated within the range. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2598 |
do |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2599 |
{ |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2600 |
bits = *++map; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2601 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2602 |
while (!bits && map < mapEnd); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2603 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2604 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2605 |
// Clear any bits before the run so can get next free from __e32_find_msl_32(). |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2606 |
if (runWord == map) |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2607 |
bits &= KMaxTUint32 >> (aOffset & ~KWordAlignMask); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2608 |
TInt msFree = __e32_find_ms1_32(bits); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2609 |
__NK_ASSERT_DEBUG(msFree >= 0 || map == mapEnd); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2610 |
TUint msFreeOffset = (msFree >= 0)? 31 - msFree : 32; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2611 |
TUint endIndex = map - aZone->iBma[bmaIndex]->iMap; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2612 |
TUint runEnd = (endIndex << 5) + msFreeOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2613 |
if (runEnd > aEndOffset + 1) // Ensure we don't go past the range. |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2614 |
runEnd = aEndOffset + 1; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2615 |
__NK_ASSERT_DEBUG(runEnd > aOffset); |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2616 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2617 |
return runEnd - aOffset; |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2618 |
} |
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2619 |
|
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
2620 |
|
0 | 2621 |
/** |
2622 |
See if any of the least preferable RAM zones can be emptied. If they can then |
|
2623 |
initialise the allocator for a general defragmentation operation. |
|
2624 |
||
2625 |
Stage 0 of the general defrag is to ensure that there are enough free |
|
2626 |
pages in the more preferable RAM zones to be in use after the general defrag |
|
2627 |
for the movable page allocations. This is achieved by discarding the |
|
2628 |
required amount of discardable pages from the more preferable RAM zones |
|
2629 |
to be in use after the general defrag. |
|
2630 |
||
2631 |
||
2632 |
@parm aInitialStage On return this will contain the stage the general |
|
2633 |
defragmentation should begin at. I.e. if no RAM |
|
2634 |
zones can be cleared then just perform the final |
|
2635 |
tidying stage. |
|
2636 |
@param aRequiredToBeDiscarded On return this will contain the number of |
|
2637 |
discardable pages that need to be discarded |
|
2638 |
from the RAM zones to be in use after the |
|
2639 |
general defrag. |
|
2640 |
@return Pointer to the RAM zone object that may potentially have pages |
|
2641 |
discarded by the general defrag. This will be NULL if no suitable |
|
2642 |
RAM zone could be found. |
|
2643 |
*/ |
|
2644 |
SZone* DRamAllocator::GeneralDefragStart0(TGenDefragStage& aStage, TUint& aRequiredToBeDiscarded) |
|
2645 |
{ |
|
2646 |
#ifdef _DEBUG |
|
2647 |
if (!K::Initialising) |
|
2648 |
{ |
|
2649 |
M::RamAllocIsLocked(); |
|
2650 |
#ifdef __VERIFY_LEASTMOVDIS |
|
2651 |
VerifyLeastPrefMovDis(); |
|
2652 |
#endif |
|
2653 |
} |
|
2654 |
// Any previous general defrag operation must have ended. |
|
2655 |
__NK_ASSERT_DEBUG(iZoneGeneralPrefLink == NULL); |
|
2656 |
__NK_ASSERT_DEBUG(iZoneGeneralTmpLink == NULL); |
|
2657 |
#endif |
|
2658 |
||
2659 |
if (iNumZones == 1) |
|
2660 |
{ |
|
2661 |
// Only have one RAM zone so a defrag can't do anything. |
|
2662 |
return NULL; |
|
2663 |
} |
|
2664 |
||
2665 |
// Determine how many movable or discardable pages are required to be allocated. |
|
2666 |
TUint requiredPagesDis = 0; |
|
2667 |
TUint requiredPagesMov = 0; |
|
2668 |
TUint firstClearableInUseRank = 0; |
|
2669 |
SDblQueLink* link = iZoneLeastMovDis; |
|
2670 |
do |
|
2671 |
{ |
|
2672 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
2673 |
requiredPagesDis += zone.iAllocPages[EPageDiscard]; |
|
2674 |
requiredPagesMov += zone.iAllocPages[EPageMovable]; |
|
2675 |
||
2676 |
if (!firstClearableInUseRank && |
|
2677 |
(zone.iAllocPages[EPageMovable] || zone.iAllocPages[EPageDiscard]) && |
|
2678 |
!zone.iAllocPages[EPageFixed] && !zone.iAllocPages[EPageUnknown]) |
|
2679 |
{// This is the least preferable RAM zone that is has movable or |
|
2680 |
// discardable but may be clearable as it has no immovable pages. |
|
2681 |
firstClearableInUseRank = zone.iPrefRank; |
|
2682 |
} |
|
2683 |
||
2684 |
// Reset KRamZoneFlagGenDefrag flag bit for each RAM zone to be defraged. |
|
2685 |
zone.iFlags &= ~(KRamZoneFlagGenDefrag | KRamZoneFlagGenDefragBlock); |
|
2686 |
||
2687 |
link = link->iPrev; |
|
2688 |
} |
|
2689 |
while (link != &iZonePrefList.iA); |
|
2690 |
||
2691 |
// Adjust the number of discardable pages for those that are freeable. |
|
2692 |
// Dirty pages will be moved rather than discarded so they are not freeable |
|
2693 |
// and we must make sure that we have enough space in zones for these dirty |
|
2694 |
// paged pages. |
|
2695 |
__NK_ASSERT_DEBUG(requiredPagesDis >= (TUint)M::NumberOfFreeDpPages()); |
|
2696 |
requiredPagesDis -= M::NumberOfFreeDpPages(); |
|
2697 |
TUint totalDirtyPagesDis = M::NumberOfDirtyDpPages(); |
|
2698 |
if (requiredPagesDis < totalDirtyPagesDis) |
|
2699 |
requiredPagesDis = totalDirtyPagesDis; |
|
2700 |
||
2701 |
// Determine which is the least preferable RAM zone that needs to be |
|
2702 |
// in use for required number of movable and discardable pages. |
|
2703 |
TUint onlyPagesDis = 0; // Number of pages in RAM zones for discard only. |
|
2704 |
TUint onlyPagesMov = 0; // Number of pages in RAM zones for movable only. |
|
2705 |
TUint totalPagesDis = 0; // Total pages found so far for discardable pages. |
|
2706 |
TUint totalPagesMov = 0; // Total pages found so far for movable pages. |
|
2707 |
TUint totalCurrentDis = 0; // Number of allocated discardable pages found in |
|
2708 |
// RAM zones to be in use after the general defrag. |
|
2709 |
TUint totalCurrentMov = 0; // Number of allocated movable pages found in |
|
2710 |
// RAM zones to be in use after the general defrag. |
|
2711 |
TUint totalCurrentFree = 0; // The current number of free pages in the RAM zones |
|
2712 |
// to be in use after the general defrag. |
|
2713 |
iZoneGeneralPrefLink = &iZonePrefList.iA; |
|
2714 |
while (iZoneGeneralPrefLink != iZoneLeastMovDis && |
|
2715 |
(requiredPagesMov > totalPagesMov || |
|
2716 |
requiredPagesDis > totalPagesDis)) |
|
2717 |
{ |
|
2718 |
iZoneGeneralPrefLink = iZoneGeneralPrefLink->iNext; |
|
2719 |
SZone& zone = *_LOFF(iZoneGeneralPrefLink, SZone, iPrefLink); |
|
2720 |
// Update the current totals. |
|
2721 |
totalCurrentDis += zone.iAllocPages[EPageDiscard]; |
|
2722 |
totalCurrentMov += zone.iAllocPages[EPageMovable]; |
|
2723 |
totalCurrentFree += zone.iFreePages; |
|
2724 |
||
2725 |
TBool onlyAllocDis = NoAllocOfPageType(zone, EPageMovable); |
|
2726 |
TBool onlyAllocMov = NoAllocOfPageType(zone, EPageDiscard); |
|
2727 |
if (!onlyAllocMov || !onlyAllocDis) |
|
2728 |
{// Either movable, discardable or both can be allocated in this zone. |
|
2729 |
TUint zonePagesFree = zone.iFreePages; |
|
2730 |
TUint zonePagesDis = zone.iAllocPages[EPageDiscard]; |
|
2731 |
TUint zonePagesMov = zone.iAllocPages[EPageMovable]; |
|
2732 |
// Total pages in this RAM zone that can be used for either |
|
2733 |
// discardable or movable pages. |
|
2734 |
TUint zonePagesGen = zonePagesDis + zonePagesMov + zonePagesFree; |
|
2735 |
if (onlyAllocMov) |
|
2736 |
{ |
|
2737 |
if (requiredPagesDis > totalPagesDis) |
|
2738 |
{// No further discardable pages can be allocated into |
|
2739 |
// this RAM zone but consider any that already are. |
|
2740 |
TUint usedPages = Min( (TInt)zonePagesDis, |
|
2741 |
requiredPagesDis - totalPagesDis); |
|
2742 |
totalPagesDis += usedPages; |
|
2743 |
zonePagesDis -= usedPages; |
|
2744 |
} |
|
2745 |
TUint zoneOnlyMov = zonePagesDis + zonePagesMov + zonePagesFree; |
|
2746 |
onlyPagesMov += zoneOnlyMov; |
|
2747 |
totalPagesMov += zoneOnlyMov; |
|
2748 |
__KTRACE_OPT(KMMU2, Kern::Printf("onlyMov ID%x tot %x", |
|
2749 |
zone.iId, zoneOnlyMov)); |
|
2750 |
zonePagesGen = 0; // These pages aren't general purpose. |
|
2751 |
} |
|
2752 |
if (onlyAllocDis) |
|
2753 |
{ |
|
2754 |
if (requiredPagesMov > totalPagesMov) |
|
2755 |
{// No further movable pages can be allocated into |
|
2756 |
// this RAM zone but consider any that already are. |
|
2757 |
TUint usedPages = Min( (TInt)zonePagesMov, |
|
2758 |
requiredPagesMov - totalPagesMov); |
|
2759 |
totalPagesMov += usedPages; |
|
2760 |
zonePagesMov -= usedPages; |
|
2761 |
} |
|
2762 |
TUint zoneOnlyDis = zonePagesDis + zonePagesMov + zonePagesFree; |
|
2763 |
onlyPagesDis += zoneOnlyDis; |
|
2764 |
totalPagesDis += zoneOnlyDis; |
|
2765 |
__KTRACE_OPT(KMMU2, Kern::Printf("onlyDis ID%x tot %x", |
|
2766 |
zone.iId, zoneOnlyDis)); |
|
2767 |
zonePagesGen = 0; // These pages aren't general purpose. |
|
2768 |
} |
|
2769 |
||
2770 |
if (requiredPagesDis > totalPagesDis) |
|
2771 |
{// Need some discardable pages so first steal any spare |
|
2772 |
// movable pages for discardable allocations. |
|
2773 |
if (totalPagesMov > requiredPagesMov) |
|
2774 |
{// Use any spare movable pages that can also be |
|
2775 |
// used for discardable allocations for discardable. |
|
2776 |
__NK_ASSERT_DEBUG(onlyPagesMov); |
|
2777 |
TUint spareMovPages = Min((TInt)(totalPagesMov - onlyPagesMov), |
|
2778 |
totalPagesMov - requiredPagesMov); |
|
2779 |
totalPagesMov -= spareMovPages; |
|
2780 |
totalPagesDis += spareMovPages; |
|
2781 |
__KTRACE_OPT(KMMU2, Kern::Printf("genDis Mov ID%x used%x", |
|
2782 |
zone.iId, spareMovPages)); |
|
2783 |
} |
|
2784 |
if (requiredPagesDis > totalPagesDis) |
|
2785 |
{ |
|
2786 |
// Need more discardable pages but only grab those required. |
|
2787 |
TUint usedPages = Min( (TInt) zonePagesGen, |
|
2788 |
requiredPagesDis - totalPagesDis); |
|
2789 |
totalPagesDis += usedPages; |
|
2790 |
zonePagesGen -= usedPages; |
|
2791 |
__KTRACE_OPT(KMMU2, Kern::Printf("genDis ID%x used%x", |
|
2792 |
zone.iId, usedPages)); |
|
2793 |
} |
|
2794 |
} |
|
2795 |
if (requiredPagesMov > totalPagesMov) |
|
2796 |
{// Need some movable pages so first steal any spare |
|
2797 |
// discardable pages for movable allocations. |
|
2798 |
if (totalPagesDis > requiredPagesDis) |
|
2799 |
{// Use any spare discardable pages that can also be |
|
2800 |
// used for movable allocations for movable. |
|
2801 |
__NK_ASSERT_DEBUG(onlyPagesDis); |
|
2802 |
TUint spareDisPages = Min((TInt)(totalPagesDis - onlyPagesDis), |
|
2803 |
totalPagesDis - requiredPagesDis); |
|
2804 |
totalPagesDis -= spareDisPages; |
|
2805 |
totalPagesMov += spareDisPages; |
|
2806 |
__KTRACE_OPT(KMMU2, Kern::Printf("genMov Dis ID%x used%x", |
|
2807 |
zone.iId, spareDisPages)); |
|
2808 |
} |
|
2809 |
if (requiredPagesMov > totalPagesMov) |
|
2810 |
{// Still need some movable pages so grab them from this zone. |
|
2811 |
// Just grab all of the general pages left as discard pages will |
|
2812 |
// have already grabbed some if it had needed to. |
|
2813 |
totalPagesMov += zonePagesGen; |
|
2814 |
__KTRACE_OPT(KMMU2, Kern::Printf("genMov ID%x used%x", |
|
2815 |
zone.iId, zonePagesGen)); |
|
2816 |
} |
|
2817 |
} |
|
2818 |
} |
|
2819 |
} |
|
2820 |
||
2821 |
__KTRACE_OPT(KMMU, Kern::Printf("gen least in use ID 0x%x", |
|
2822 |
(_LOFF(iZoneGeneralPrefLink, SZone, iPrefLink))->iId)); |
|
2823 |
__NK_ASSERT_DEBUG(_LOFF(iZoneGeneralPrefLink, SZone, iPrefLink)->iPrefRank <= |
|
2824 |
iZoneLeastMovDisRank); |
|
2825 |
||
2826 |
if (iZoneGeneralPrefLink != iZoneLeastMovDis && |
|
2827 |
firstClearableInUseRank > _LOFF(iZoneGeneralPrefLink, SZone, iPrefLink)->iPrefRank) |
|
2828 |
{// We can reduce the number of RAM zones in use so block all the RAM |
|
2829 |
// zones not to be in use after the defrag from being allocated into |
|
2830 |
// by the general defrag. |
|
2831 |
link = iZoneLeastMovDis; |
|
2832 |
while (link != iZoneGeneralPrefLink) |
|
2833 |
{ |
|
2834 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
2835 |
zone.iFlags |= KRamZoneFlagGenDefragBlock; |
|
2836 |
link = link->iPrev; |
|
2837 |
} |
|
2838 |
||
2839 |
// Determine how many pages will need to be discarded to allow general |
|
2840 |
// defrag to succeed in using the minimum RAM zones required. |
|
2841 |
if (requiredPagesDis > totalCurrentDis) |
|
2842 |
{// Need to replace some discardable pages in RAM zones to be |
|
2843 |
// cleared with pages in the RAM zones to be in use after the |
|
2844 |
// general defrag. |
|
2845 |
__NK_ASSERT_DEBUG(totalCurrentFree >= requiredPagesDis - totalCurrentDis); |
|
2846 |
totalCurrentFree -= requiredPagesDis - totalCurrentDis; |
|
2847 |
} |
|
2848 |
TUint totalForMov = totalCurrentFree + totalCurrentMov; |
|
2849 |
if (requiredPagesMov > totalForMov) |
|
2850 |
{// Need to discard some pages from the least preferable RAM zone to be |
|
2851 |
// in use after the general for the movable pages to be moved to. |
|
2852 |
aRequiredToBeDiscarded = requiredPagesMov - totalForMov; |
|
2853 |
__NK_ASSERT_DEBUG(aRequiredToBeDiscarded <= totalCurrentDis); |
|
2854 |
__NK_ASSERT_DEBUG(totalCurrentDis - aRequiredToBeDiscarded >= requiredPagesDis); |
|
2855 |
} |
|
2856 |
||
2857 |
// This stage should discard pages from the least preferable RAM zones |
|
2858 |
// to be in use after the general defrag to save the pages having to |
|
2859 |
// be moved again by the final stage. |
|
2860 |
iZoneGeneralStage = EGenDefragStage0; |
|
2861 |
aStage = EGenDefragStage1; // Defrag::GeneralDefrag() requires this. |
|
2862 |
iZoneGeneralTmpLink = iZoneGeneralPrefLink; |
|
2863 |
return GeneralDefragNextZone0(); |
|
2864 |
} |
|
2865 |
||
2866 |
// General defrag can't clear any RAM zones so jump to tidying stage. |
|
2867 |
aStage = EGenDefragStage2; |
|
2868 |
iZoneGeneralStage = EGenDefragStage2; |
|
2869 |
return NULL; |
|
2870 |
} |
|
2871 |
||
2872 |
||
2873 |
/** |
|
2874 |
Find the next RAM zone that is suitable for stage 0 of a general defrag. |
|
2875 |
This should only be called after a preceeding call to |
|
2876 |
DRamAllocator::GeneralDefragStart0(). |
|
2877 |
||
2878 |
This goes through the RAM zones from the least preferable to be in use |
|
2879 |
after the general defrag to the most preferable RAM zone. It will |
|
2880 |
return each time it finds a RAM zone with discardable pages allocated into it. |
|
2881 |
||
2882 |
@return Pointer to the RAM zone object that may potentially have pages |
|
2883 |
discarded by the general defrag. This will be NULL if no suitable |
|
2884 |
RAM zone could be found. |
|
2885 |
*/ |
|
2886 |
SZone* DRamAllocator::GeneralDefragNextZone0() |
|
2887 |
{ |
|
2888 |
M::RamAllocIsLocked(); |
|
2889 |
// Any previous general defrag operation must have ended. |
|
2890 |
__NK_ASSERT_DEBUG(iZoneGeneralPrefLink != NULL); |
|
2891 |
__NK_ASSERT_DEBUG(iZoneGeneralTmpLink != NULL); |
|
2892 |
__NK_ASSERT_DEBUG(iZoneGeneralStage == EGenDefragStage0); |
|
2893 |
||
2894 |
while (iZoneGeneralTmpLink != &iZonePrefList.iA) |
|
2895 |
{ |
|
2896 |
SZone* zone = _LOFF(iZoneGeneralTmpLink, SZone, iPrefLink); |
|
2897 |
||
2898 |
// Save the RAM zone that is currently more preferable than this one |
|
2899 |
// before any reordering. |
|
2900 |
iZoneGeneralTmpLink = iZoneGeneralTmpLink->iPrev; |
|
2901 |
||
2902 |
if (zone->iFlags & KRamZoneFlagGenDefrag) |
|
2903 |
{// This zone has been selected for a general defrag already. |
|
2904 |
__KTRACE_OPT(KMMU, Kern::Printf("GenDefragNext0 zone ID 0x%x already defraged", |
|
2905 |
zone->iId)); |
|
2906 |
return NULL; |
|
2907 |
} |
|
2908 |
zone->iFlags |= KRamZoneFlagGenDefrag; |
|
2909 |
if (zone->iAllocPages[EPageDiscard]) |
|
2910 |
{ |
|
2911 |
// A RAM zone that may have pages discarded by a general defrag has been found. |
|
2912 |
__KTRACE_OPT(KMMU, Kern::Printf("GenDefragNext0 zone ID 0x%x", zone->iId)); |
|
2913 |
return zone; |
|
2914 |
} |
|
2915 |
} |
|
2916 |
return NULL; |
|
2917 |
} |
|
2918 |
||
2919 |
||
2920 |
/** |
|
2921 |
Initialise this stage of a general defrag operation which will attempt |
|
2922 |
to clear all the RAM zones not to be in use once the general defrag |
|
2923 |
has completed. |
|
2924 |
||
2925 |
@return Pointer to the RAM zone object that may potentially be cleared |
|
2926 |
by the general defrag. This will be NULL if no suitable |
|
2927 |
RAM zone could be found. |
|
2928 |
*/ |
|
2929 |
SZone* DRamAllocator::GeneralDefragStart1() |
|
2930 |
{ |
|
2931 |
M::RamAllocIsLocked(); |
|
2932 |
__NK_ASSERT_DEBUG(iNumZones == 1 || iZoneGeneralPrefLink != NULL); |
|
2933 |
||
2934 |
||
2935 |
if (iNumZones == 1) |
|
2936 |
{// On a device with one RAM zone can't do any defrag so return NULL. |
|
2937 |
return NULL; |
|
2938 |
} |
|
2939 |
||
2940 |
// Clear general defrag flags of each RAM zone to be defraged. |
|
2941 |
SDblQueLink* link = iZoneGeneralPrefLink; |
|
2942 |
for (; link != &iZonePrefList.iA; link = link->iPrev) |
|
2943 |
{ |
|
2944 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
2945 |
zone.iFlags &= ~KRamZoneFlagGenDefrag; |
|
2946 |
} |
|
2947 |
||
2948 |
// Flags cleared so now to start this stage from least preferable RAM zone |
|
2949 |
// currently in use. |
|
2950 |
iZoneGeneralTmpLink = iZoneLeastMovDis; |
|
2951 |
iZoneGeneralStage = EGenDefragStage1; |
|
2952 |
return GeneralDefragNextZone1(); |
|
2953 |
} |
|
2954 |
||
2955 |
||
2956 |
/** |
|
2957 |
Find the next RAM zone that is suitable for stage 1 of a general defrag. |
|
2958 |
This should only be called after a preceeding call to |
|
2959 |
DRamAllocator::GeneralDefragStart1(). |
|
2960 |
||
2961 |
This goes through the RAM zones from the least preferable currently |
|
2962 |
with movable or discardable pages allocated into it to the least |
|
2963 |
preferable RAM zone that is to be in use after the general defrag. |
|
2964 |
It will return each time it finds a RAM zone with movable and/or |
|
2965 |
discardable pages allocated into it. |
|
2966 |
||
2967 |
@return Pointer to the RAM zone object that may potentially be cleared by a |
|
2968 |
general defrag. This will be NULL if no suitable zone could be found. |
|
2969 |
*/ |
|
2970 |
SZone* DRamAllocator::GeneralDefragNextZone1() |
|
2971 |
{ |
|
2972 |
M::RamAllocIsLocked(); |
|
2973 |
// Any previous general defrag operation must have ended. |
|
2974 |
__NK_ASSERT_DEBUG(iZoneGeneralPrefLink != NULL); |
|
2975 |
__NK_ASSERT_DEBUG(iZoneGeneralTmpLink != NULL); |
|
2976 |
__NK_ASSERT_DEBUG(iZoneGeneralStage == EGenDefragStage1); |
|
2977 |
||
2978 |
||
2979 |
// If we hit the target least preferable RAM zone to be in use once |
|
2980 |
// the defrag has completed then stop this stage of the general defrag. |
|
2981 |
||
2982 |
// Should never skip past iZoneGeneralPrefLink. |
|
2983 |
__NK_ASSERT_DEBUG(iZoneGeneralTmpLink != &iZonePrefList.iA); |
|
2984 |
||
2985 |
while (iZoneGeneralTmpLink != iZoneGeneralPrefLink) |
|
2986 |
{ |
|
2987 |
SZone* zone = _LOFF(iZoneGeneralTmpLink, SZone, iPrefLink); |
|
2988 |
||
2989 |
// Save the RAM zone that is currently more preferable than this one |
|
2990 |
// before any reordering. |
|
2991 |
iZoneGeneralTmpLink = iZoneGeneralTmpLink->iPrev; |
|
2992 |
||
2993 |
if (zone->iFlags & KRamZoneFlagGenDefrag) |
|
2994 |
{// This zone has been selected for a general defrag already. |
|
2995 |
__KTRACE_OPT(KMMU, Kern::Printf("GenDefragNext1 zone ID 0x%x already defraged", |
|
2996 |
zone->iId)); |
|
2997 |
return NULL; |
|
2998 |
} |
|
2999 |
zone->iFlags |= KRamZoneFlagGenDefrag; |
|
3000 |
if (zone->iAllocPages[EPageMovable] || zone->iAllocPages[EPageDiscard]) |
|
3001 |
{ |
|
3002 |
// A RAM zone that may be cleared by a general defrag has been found. |
|
3003 |
__KTRACE_OPT(KMMU, Kern::Printf("GenDefragNext1 zone ID 0x%x", zone->iId)); |
|
3004 |
return zone; |
|
3005 |
} |
|
3006 |
} |
|
3007 |
__KTRACE_OPT(KMMU, Kern::Printf("GenDefragNext1 reached general target")); |
|
3008 |
return NULL; |
|
3009 |
} |
|
3010 |
||
3011 |
||
3012 |
/** |
|
3013 |
Initialise stage 2 of a general defrag operation. |
|
3014 |
||
3015 |
Stage 2 creates room for fixed pages allocations in the more preferable RAM |
|
3016 |
zones in use by moving pages into the least preferable RAM zones in use. |
|
3017 |
||
3018 |
@return Pointer to the RAM zone object that may potentially be cleared of |
|
3019 |
movable and discardable pages by the general defrag. This will be |
|
3020 |
NULL if no suitable zone could be found. |
|
3021 |
*/ |
|
3022 |
SZone* DRamAllocator::GeneralDefragStart2() |
|
3023 |
{ |
|
3024 |
M::RamAllocIsLocked(); |
|
3025 |
__NK_ASSERT_DEBUG(iNumZones == 1 || iZoneGeneralPrefLink != NULL); |
|
3026 |
||
3027 |
||
3028 |
if (iNumZones == 1) |
|
3029 |
{// On a device with one RAM zone can't do any defrag so return NULL. |
|
3030 |
return NULL; |
|
3031 |
} |
|
3032 |
||
3033 |
// Clear general defrag flags of each RAM zone to be defraged. |
|
3034 |
SDblQueLink* link = iZoneLeastMovDis; |
|
3035 |
for (; link != &iZonePrefList.iA; link = link->iPrev) |
|
3036 |
{ |
|
3037 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
3038 |
zone.iFlags &= ~(KRamZoneFlagGenDefrag | KRamZoneFlagGenDefragBlock); |
|
3039 |
} |
|
3040 |
||
3041 |
// Flags cleared so now to start 2nd stage from most preferable RAM zone. |
|
3042 |
iZoneGeneralTmpLink = iZonePrefList.First(); |
|
3043 |
iZoneGeneralStage = EGenDefragStage2; |
|
3044 |
return GeneralDefragNextZone2(); |
|
3045 |
} |
|
3046 |
||
3047 |
||
3048 |
/** |
|
3049 |
Find the next RAM zone that is suitable for this stage of general defrag. |
|
3050 |
This should only be called after a preceeding call to |
|
3051 |
DRamAllocator::GeneralDefragStart2(). |
|
3052 |
||
3053 |
This goes through the RAM zones from the most preferable to the least |
|
3054 |
preferable RAM zone that has movable and/or discardable pages allocated |
|
3055 |
into it. It will return each time it finds a RAM zone with movable and/or |
|
3056 |
discardable pages allocated into it. |
|
3057 |
||
3058 |
@return Pointer to the RAM zone object that may potentially be cleared of |
|
3059 |
movable and discardable pages by the general defrag. This will be |
|
3060 |
NULL if no suitable zone could be found. |
|
3061 |
*/ |
|
3062 |
SZone* DRamAllocator::GeneralDefragNextZone2() |
|
3063 |
{ |
|
3064 |
M::RamAllocIsLocked(); |
|
3065 |
__NK_ASSERT_DEBUG(iZoneGeneralTmpLink != NULL); |
|
3066 |
__NK_ASSERT_DEBUG(iZoneGeneralStage == EGenDefragStage2); |
|
3067 |
||
3068 |
||
3069 |
while (iZoneGeneralTmpLink != iZoneLeastMovDis) |
|
3070 |
{ |
|
3071 |
SZone* zone = _LOFF(iZoneGeneralTmpLink, SZone, iPrefLink); |
|
3072 |
||
3073 |
// Save the RAM zone that is currently less preferable than this one |
|
3074 |
// before any reordering. |
|
3075 |
iZoneGeneralTmpLink = iZoneGeneralTmpLink->iNext; |
|
3076 |
||
3077 |
if (zone->iFlags & KRamZoneFlagGenDefrag) |
|
3078 |
{// This zone has been selected for a general defrag already. |
|
3079 |
__KTRACE_OPT(KMMU, Kern::Printf("GenDefragNext2 zone ID 0x%x already defraged", zone->iId)); |
|
3080 |
return NULL; |
|
3081 |
} |
|
3082 |
zone->iFlags |= KRamZoneFlagGenDefrag | KRamZoneFlagGenDefragBlock; |
|
3083 |
if (zone->iAllocPages[EPageMovable] || zone->iAllocPages[EPageDiscard]) |
|
3084 |
{// A RAM zone that may be cleared by a general defrag has been found. |
|
3085 |
__KTRACE_OPT(KMMU, Kern::Printf("GenDefragNext2 zone ID 0x%x", zone->iId)); |
|
3086 |
return zone; |
|
3087 |
} |
|
3088 |
} |
|
3089 |
__KTRACE_OPT(KMMU, Kern::Printf("GenDefragNext2 reached general target")); |
|
3090 |
return NULL; |
|
3091 |
} |
|
3092 |
||
3093 |
/** |
|
3094 |
Inform the allocator that a general defragmentation operation has completed. |
|
3095 |
||
3096 |
*/ |
|
3097 |
void DRamAllocator::GeneralDefragEnd() |
|
3098 |
{ |
|
3099 |
#ifdef _DEBUG |
|
3100 |
if (!K::Initialising) |
|
3101 |
{ |
|
3102 |
M::RamAllocIsLocked(); |
|
3103 |
#ifdef __VERIFY_LEASTMOVDIS |
|
3104 |
VerifyLeastPrefMovDis(); |
|
3105 |
#endif |
|
3106 |
} |
|
3107 |
#endif |
|
3108 |
// Reset the general defrag preference link as it is no longer required. |
|
3109 |
iZoneGeneralPrefLink = NULL; |
|
3110 |
iZoneGeneralTmpLink = NULL; |
|
3111 |
} |
|
3112 |
||
3113 |
||
3114 |
/** |
|
3115 |
Calculate the number of free pages in all the RAM zones to be in use |
|
3116 |
once the general defragmentation operation has completed. |
|
3117 |
||
3118 |
@param aType The type of free pages to find in the higher priority zones. |
|
3119 |
@return The number of free pages in the RAM zones intended to be in use |
|
3120 |
after the general defrag operation has completed. |
|
3121 |
*/ |
|
3122 |
TUint DRamAllocator::GenDefragFreePages(TZonePageType aType) const |
|
3123 |
{ |
|
3124 |
M::RamAllocIsLocked(); |
|
3125 |
||
3126 |
if (iZoneGeneralStage == EGenDefragStage2) |
|
3127 |
{// Second stage of general defrag where don't have to empty the RAM zone. |
|
3128 |
return KMaxTUint; |
|
3129 |
} |
|
3130 |
TUint totalFree = 0; |
|
3131 |
SDblQueLink* link = iZoneGeneralPrefLink; |
|
3132 |
for (; link != &iZonePrefList.iA; link = link->iPrev) |
|
3133 |
{ |
|
3134 |
SZone& zone = *_LOFF(link, SZone, iPrefLink); |
|
3135 |
if (NoAllocOfPageType(zone, aType) || |
|
3136 |
zone.iFlags & KRamZoneFlagGenDefragBlock) |
|
3137 |
{ |
|
3138 |
continue; |
|
3139 |
} |
|
3140 |
// This zone has free space for this type of page |
|
3141 |
totalFree += zone.iFreePages; |
|
3142 |
} |
|
3143 |
return totalFree; |
|
3144 |
} |
|
3145 |
||
3146 |
||
3147 |
/** Mark the RAM zone as being claimed to stop any further allocations. |
|
3148 |
@param aZone The zone to stop allocations to. |
|
3149 |
||
3150 |
@pre RamAlloc mutex held. |
|
3151 |
@post RamAlloc mutex held. |
|
3152 |
*/ |
|
3153 |
void DRamAllocator::ZoneClaimStart(SZone& aZone) |
|
3154 |
{ |
|
3155 |
M::RamAllocIsLocked(); |
|
3156 |
__NK_ASSERT_DEBUG(!(aZone.iFlags & KRamZoneFlagClaiming)); |
|
3157 |
||
3158 |
aZone.iFlags |= KRamZoneFlagClaiming; |
|
3159 |
||
3160 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
3161 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocZoneFlagsModified, aZone.iId, aZone.iFlags); |
|
3162 |
#endif |
|
3163 |
} |
|
3164 |
||
3165 |
||
3166 |
/** Mark the RAM zone as not being claimed to allow allocations. |
|
3167 |
@param aZone The zone to allow allocations into. |
|
3168 |
||
3169 |
@pre RamAlloc mutex held. |
|
3170 |
@post RamAlloc mutex held. |
|
3171 |
*/ |
|
3172 |
void DRamAllocator::ZoneClaimEnd(SZone& aZone) |
|
3173 |
{ |
|
3174 |
M::RamAllocIsLocked(); |
|
3175 |
__NK_ASSERT_DEBUG(aZone.iFlags & KRamZoneFlagClaiming); |
|
3176 |
||
3177 |
aZone.iFlags &= ~KRamZoneFlagClaiming; |
|
3178 |
||
3179 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
3180 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocZoneFlagsModified, aZone.iId, aZone.iFlags); |
|
3181 |
#endif |
|
3182 |
} |
|
3183 |
||
3184 |
/** Mark the RAM zone so that any allocation or frees from it can be detected. |
|
3185 |
Useful for defragging. |
|
3186 |
@param aZone The zone to mark. |
|
3187 |
@pre RamAlloc mutex held |
|
3188 |
@post RamAlloc mutex held |
|
3189 |
*/ |
|
3190 |
void DRamAllocator::ZoneMark(SZone& aZone) |
|
3191 |
{ |
|
3192 |
M::RamAllocIsLocked(); |
|
3193 |
__NK_ASSERT_DEBUG(!(aZone.iFlags & KRamZoneFlagMark)); |
|
3194 |
||
3195 |
aZone.iFlags |= KRamZoneFlagMark; |
|
3196 |
||
3197 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
3198 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocZoneFlagsModified, aZone.iId, aZone.iFlags); |
|
3199 |
#endif |
|
3200 |
} |
|
3201 |
||
3202 |
/** Unmark the RAM zone. |
|
3203 |
Useful for defragging. |
|
3204 |
@param aZone The zone to mark. |
|
3205 |
@return ETrue if the RAM zone is inactive, EFalse otherwise. |
|
3206 |
@pre RamAlloc mutex held |
|
3207 |
@post RamAlloc mutex held |
|
3208 |
*/ |
|
3209 |
TBool DRamAllocator::ZoneUnmark(SZone& aZone) |
|
3210 |
{ |
|
3211 |
M::RamAllocIsLocked(); |
|
3212 |
||
3213 |
TInt r = aZone.iFlags & KRamZoneFlagMark; |
|
3214 |
aZone.iFlags &= ~KRamZoneFlagMark; |
|
3215 |
||
3216 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
3217 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocZoneFlagsModified, aZone.iId, aZone.iFlags); |
|
3218 |
#endif |
|
3219 |
return r; |
|
3220 |
} |
|
3221 |
||
3222 |
/** Determine whether it is OK to allocate the specified page type |
|
3223 |
to the RAM zone. |
|
3224 |
||
3225 |
This should be used by all functions that search through the zones when |
|
3226 |
attempting to allocate pages. |
|
3227 |
||
3228 |
@return ETrue if this page type shouldn't be allocated into the RAM zone, |
|
3229 |
EFalse if it is OK to allocate that page type into the RAM zone. |
|
3230 |
*/ |
|
3231 |
TBool DRamAllocator::NoAllocOfPageType(SZone& aZone, TZonePageType aType) const |
|
3232 |
{ |
|
3233 |
TUint8 flagMask = 1 << (aType - KPageTypeAllocBase); |
|
3234 |
return (aZone.iFlags & (KRamZoneFlagClaiming|KRamZoneFlagNoAlloc|KRamZoneFlagTmpBlockAlloc)) || |
|
3235 |
(aZone.iFlags & flagMask); |
|
3236 |
} |
|
3237 |
||
3238 |
||
3239 |
/** Updates the flags of the specified RAM zone. |
|
3240 |
||
3241 |
@param aId The ID of the RAM zone to modify. |
|
3242 |
@param aClearFlags The bit flags to clear. |
|
3243 |
@param aSetFlags The bit flags to set. |
|
3244 |
||
3245 |
@return KErrNone on success, KErrArgument if the RAM zone of aId not found or |
|
3246 |
aSetMask contains invalid flags. |
|
3247 |
||
3248 |
@pre RamAlloc mutex held |
|
3249 |
@post RamAlloc mutex held |
|
3250 |
*/ |
|
3251 |
TInt DRamAllocator::ModifyZoneFlags(TUint aId, TUint aClearMask, TUint aSetMask) |
|
3252 |
{ |
|
3253 |
M::RamAllocIsLocked(); |
|
3254 |
||
3255 |
SZone* zone = ZoneFromId(aId); |
|
3256 |
if (zone == NULL || (aSetMask & KRamZoneFlagInvalid)) |
|
3257 |
{// aId invalid or an invalid flag bit was requested to be set. |
|
3258 |
return KErrArgument; |
|
3259 |
} |
|
3260 |
zone->iFlags &= ~aClearMask; |
|
3261 |
zone->iFlags |= aSetMask; |
|
3262 |
||
3263 |
__KTRACE_OPT(KMMU, Kern::Printf("Zone %x Flags %x", zone->iId, zone->iFlags)); |
|
3264 |
||
3265 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
3266 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocZoneFlagsModified, zone->iId, zone->iFlags); |
|
3267 |
#endif |
|
3268 |
return KErrNone; |
|
3269 |
} |
|
3270 |
||
3271 |
||
3272 |
/** Invoke the RAM zone call back function to inform the variant of the RAM zones |
|
3273 |
in use so far by the system. |
|
3274 |
This is designed to only be invoked once during boot in MmuBase::Init2() |
|
3275 |
*/ |
|
3276 |
void DRamAllocator::InitialCallback() |
|
3277 |
{ |
|
3278 |
__NK_ASSERT_DEBUG(iZoneCallbackInitSent == EFalse); |
|
3279 |
if (iZonePowerFunc) |
|
3280 |
{ |
|
3281 |
TInt ret = (*iZonePowerFunc)(ERamZoneOp_Init, NULL, (TUint*)&iZonePwrState); |
|
3282 |
if (ret != KErrNone && ret != KErrNotSupported) |
|
3283 |
{ |
|
3284 |
Panic(EZonesCallbackErr); |
|
3285 |
} |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3286 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL, "DRamAllocator::InitialCallback"); |
0 | 3287 |
} |
3288 |
iZoneCallbackInitSent = ETrue; |
|
3289 |
} |
|
3290 |
||
3291 |
||
3292 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
3293 |
/** |
|
3294 |
Structure for outputing zone information to BTrace that couldn't be fit into first |
|
3295 |
2 words of the BTraceN call |
|
3296 |
*/ |
|
3297 |
struct TRamAllocBtraceZone |
|
3298 |
{ |
|
3299 |
TUint32 iId; |
|
3300 |
TUint8 iPref; |
|
3301 |
TUint8 iFlags; |
|
3302 |
TUint16 iReserved; |
|
3303 |
}; |
|
3304 |
||
3305 |
/** |
|
3306 |
This will be invoked when BTrace starts logging BTrace::ERamAllocator category |
|
3307 |
traces. |
|
3308 |
It outputs the zone configuration and the base addresses of any contiguous block |
|
3309 |
of allocated pages. |
|
3310 |
*/ |
|
102
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
3311 |
void DRamAllocator::DoBTracePrime(void) |
0 | 3312 |
{ |
3313 |
M::RamAllocIsLocked(); |
|
3314 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL, "DRamAllocator::SendInitialBtraceLogs"); |
|
3315 |
||
3316 |
// Output the zone information |
|
3317 |
TRamAllocBtraceZone bZone; |
|
3318 |
BTrace4(BTrace::ERamAllocator, BTrace::ERamAllocZoneCount, iNumZones); |
|
3319 |
const SZone* zone = iZones; |
|
3320 |
const SZone* const endZone = iZones + iNumZones; |
|
3321 |
for (; zone < endZone; zone++) |
|
3322 |
{ |
|
3323 |
bZone.iId = zone->iId; |
|
3324 |
bZone.iPref = zone->iPref; |
|
3325 |
bZone.iFlags = zone->iFlags; |
|
3326 |
BTraceN(BTrace::ERamAllocator, BTrace::ERamAllocZoneConfig, zone->iPhysPages, |
|
3327 |
zone->iPhysBase, &bZone, sizeof(TRamAllocBtraceZone)); |
|
3328 |
} |
|
3329 |
||
3330 |
// Search through zones and output each contiguous region of allocated pages |
|
3331 |
for (zone = iZones; zone < endZone; zone++) |
|
3332 |
{ |
|
3333 |
if (zone->iFreePages != zone->iPhysPages) |
|
3334 |
{ |
|
3335 |
TInt pageCount = 0; |
|
3336 |
TInt totalPages = 0; |
|
3337 |
TUint32 runStart = 0; |
|
3338 |
while ((TUint)totalPages != zone->iPhysPages - zone->iFreePages) |
|
3339 |
{ |
|
3340 |
// find set of contiguous pages that have been allocated |
|
3341 |
// runStart will be set to first page of allocated run if one found |
|
3342 |
for (;runStart < zone->iPhysPages && zone->iBma[KBmaAllPages]->NotAllocated(runStart,1); runStart++); |
|
3343 |
||
3344 |
// find last allocated page of this run |
|
3345 |
TUint32 runEnd = runStart + 1; |
|
3346 |
for (;runEnd < zone->iPhysPages && zone->iBma[KBmaAllPages]->NotFree(runEnd,1); runEnd++); |
|
3347 |
||
3348 |
pageCount = runEnd - runStart; |
|
3349 |
if (pageCount > 0) |
|
3350 |
{// have a run of allocated pages so output BTrace |
|
3351 |
TPhysAddr baseAddr = (runStart << KPageShift) + zone->iPhysBase; |
|
3352 |
__KTRACE_OPT(KMMU2, Kern::Printf("offset %x physBase %x pages %x baseAddr %08x",runStart, zone->iPhysBase, pageCount, baseAddr)); |
|
3353 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocBootAllocation, pageCount, baseAddr); |
|
3354 |
runStart += pageCount; |
|
3355 |
totalPages += pageCount; |
|
3356 |
} |
|
3357 |
} |
|
3358 |
} |
|
3359 |
} |
|
3360 |
BTrace0(BTrace::ERamAllocator, BTrace::ERamAllocBootAllocationEnd); |
|
3361 |
} |
|
3362 |
#endif // BTRACE_RAM_ALLOCATOR |
|
3363 |
||
3364 |
TInt DRamAllocator::ClaimPhysicalRam(TPhysAddr aBase, TInt aSize) |
|
3365 |
{ |
|
3366 |
TInt ret = SetPhysicalRamState(aBase,aSize,EFalse, EPageFixed); |
|
3367 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
3368 |
if (ret == KErrNone) |
|
3369 |
{ |
|
3370 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocClaimRam, aSize, aBase); |
|
3371 |
} |
|
3372 |
#endif |
|
3373 |
return ret; |
|
3374 |
} |
|
3375 |
||
3376 |
TInt DRamAllocator::FreePhysicalRam(TPhysAddr aBase, TInt aSize) |
|
3377 |
{ |
|
3378 |
TInt ret = SetPhysicalRamState(aBase,aSize,ETrue, EPageFixed); |
|
3379 |
#ifdef BTRACE_RAM_ALLOCATOR |
|
3380 |
if (ret == KErrNone) |
|
3381 |
{ |
|
3382 |
BTrace8(BTrace::ERamAllocator, BTrace::ERamAllocFreePhysical, aSize, aBase); |
|
3383 |
} |
|
3384 |
#endif |
|
3385 |
return ret; |
|
3386 |
} |
|
3387 |
||
3388 |
||
3389 |
TInt DRamAllocator::FreeRamInBytes() |
|
3390 |
{ |
|
3391 |
return iTotalFreeRamPages<<KPageShift; |
|
3392 |
} |
|
3393 |
||
3394 |
TUint DRamAllocator::FreeRamInPages() |
|
3395 |
{ |
|
3396 |
return iTotalFreeRamPages; |
|
3397 |
} |
|
3398 |
||
3399 |
TUint DRamAllocator::TotalPhysicalRamPages() |
|
3400 |
{ |
|
3401 |
return iTotalRamPages; |
|
3402 |
} |
|
3403 |
||
3404 |
#ifdef __VERIFY_LEASTMOVDIS |
|
3405 |
void DRamAllocator::VerifyLeastPrefMovDis() |
|
3406 |
{ |
|
3407 |
// Shouldn't have any movable or discardable pages in any RAM |
|
3408 |
// zone less preferable than iZoneLeastMovDis |
|
3409 |
SDblQueLink* tmpLink = iZoneLeastMovDis->iNext; |
|
3410 |
while (tmpLink != &iZonePrefList.iA) |
|
3411 |
{ |
|
3412 |
SZone& zone = *_LOFF(tmpLink, SZone, iPrefLink); |
|
3413 |
if (zone.iAllocPages[EPageMovable] != 0 || |
|
3414 |
zone.iAllocPages[EPageDiscard] != 0) |
|
3415 |
{ |
|
3416 |
DebugDump(); |
|
3417 |
__NK_ASSERT_DEBUG(0); |
|
3418 |
} |
|
3419 |
tmpLink = tmpLink->iNext; |
|
3420 |
} |
|
3421 |
} |
|
3422 |
#endif |