]> git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/DiskCache.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / DiskCache.c
1 /** @file
2 Cache implementation for EFI FAT File system driver.
3
4 Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "Fat.h"
10
11 /**
12
13 This function is used by the Data Cache.
14
15 When this function is called by write command, all entries in this range
16 are older than the contents in disk, so they are invalid; just mark them invalid.
17
18 When this function is called by read command, if any entry in this range
19 is dirty, it means that the relative info directly read from media is older than
20 than the info in the cache; So need to update the relative info in the Buffer.
21
22 @param Volume - FAT file system volume.
23 @param IoMode - This function is called by read command or write command
24 @param StartPageNo - First PageNo to be checked in the cache.
25 @param EndPageNo - Last PageNo to be checked in the cache.
26 @param Buffer - The user buffer need to update. Only when doing the read command
27 and there is dirty cache in the cache range, this parameter will be used.
28
29 **/
30 STATIC
31 VOID
32 FatFlushDataCacheRange (
33 IN FAT_VOLUME *Volume,
34 IN IO_MODE IoMode,
35 IN UINTN StartPageNo,
36 IN UINTN EndPageNo,
37 OUT UINT8 *Buffer
38 )
39 {
40 UINTN PageNo;
41 UINTN GroupNo;
42 UINTN GroupMask;
43 UINTN PageSize;
44 UINT8 PageAlignment;
45 DISK_CACHE *DiskCache;
46 CACHE_TAG *CacheTag;
47 UINT8 *BaseAddress;
48
49 DiskCache = &Volume->DiskCache[CacheData];
50 BaseAddress = DiskCache->CacheBase;
51 GroupMask = DiskCache->GroupMask;
52 PageAlignment = DiskCache->PageAlignment;
53 PageSize = (UINTN)1 << PageAlignment;
54
55 for (PageNo = StartPageNo; PageNo < EndPageNo; PageNo++) {
56 GroupNo = PageNo & GroupMask;
57 CacheTag = &DiskCache->CacheTag[GroupNo];
58 if ((CacheTag->RealSize > 0) && (CacheTag->PageNo == PageNo)) {
59 //
60 // When reading data form disk directly, if some dirty data
61 // in cache is in this rang, this data in the Buffer need to
62 // be updated with the cache's dirty data.
63 //
64 if (IoMode == ReadDisk) {
65 if (CacheTag->Dirty) {
66 CopyMem (
67 Buffer + ((PageNo - StartPageNo) << PageAlignment),
68 BaseAddress + (GroupNo << PageAlignment),
69 PageSize
70 );
71 }
72 } else {
73 //
74 // Make all valid entries in this range invalid.
75 //
76 CacheTag->RealSize = 0;
77 }
78 }
79 }
80 }
81
82 /**
83
84 Exchange the cache page with the image on the disk
85
86 @param Volume - FAT file system volume.
87 @param DataType - Indicate the cache type.
88 @param IoMode - Indicate whether to load this page from disk or store this page to disk.
89 @param CacheTag - The Cache Tag for the current cache page.
90 @param Task point to task instance.
91
92 @retval EFI_SUCCESS - Cache page exchanged successfully.
93 @return Others - An error occurred when exchanging cache page.
94
95 **/
96 STATIC
97 EFI_STATUS
98 FatExchangeCachePage (
99 IN FAT_VOLUME *Volume,
100 IN CACHE_DATA_TYPE DataType,
101 IN IO_MODE IoMode,
102 IN CACHE_TAG *CacheTag,
103 IN FAT_TASK *Task
104 )
105 {
106 EFI_STATUS Status;
107 UINTN GroupNo;
108 UINTN PageNo;
109 UINTN WriteCount;
110 UINTN RealSize;
111 UINT64 EntryPos;
112 UINT64 MaxSize;
113 DISK_CACHE *DiskCache;
114 VOID *PageAddress;
115 UINT8 PageAlignment;
116
117 DiskCache = &Volume->DiskCache[DataType];
118 PageNo = CacheTag->PageNo;
119 GroupNo = PageNo & DiskCache->GroupMask;
120 PageAlignment = DiskCache->PageAlignment;
121 PageAddress = DiskCache->CacheBase + (GroupNo << PageAlignment);
122 EntryPos = DiskCache->BaseAddress + LShiftU64 (PageNo, PageAlignment);
123 RealSize = CacheTag->RealSize;
124 if (IoMode == ReadDisk) {
125 RealSize = (UINTN)1 << PageAlignment;
126 MaxSize = DiskCache->LimitAddress - EntryPos;
127 if (MaxSize < RealSize) {
128 DEBUG ((DEBUG_INFO, "FatDiskIo: Cache Page OutBound occurred! \n"));
129 RealSize = (UINTN)MaxSize;
130 }
131 }
132
133 WriteCount = 1;
134 if ((DataType == CacheFat) && (IoMode == WriteDisk)) {
135 WriteCount = Volume->NumFats;
136 }
137
138 do {
139 //
140 // Only fat table writing will execute more than once
141 //
142 Status = FatDiskIo (Volume, IoMode, EntryPos, RealSize, PageAddress, Task);
143 if (EFI_ERROR (Status)) {
144 return Status;
145 }
146
147 EntryPos += Volume->FatSize;
148 } while (--WriteCount > 0);
149
150 CacheTag->Dirty = FALSE;
151 CacheTag->RealSize = RealSize;
152 return EFI_SUCCESS;
153 }
154
155 /**
156
157 Get one cache page by specified PageNo.
158
159 @param Volume - FAT file system volume.
160 @param CacheDataType - The cache type: CACHE_FAT or CACHE_DATA.
161 @param PageNo - PageNo to match with the cache.
162 @param CacheTag - The Cache Tag for the current cache page.
163
164 @retval EFI_SUCCESS - Get the cache page successfully.
165 @return other - An error occurred when accessing data.
166
167 **/
168 STATIC
169 EFI_STATUS
170 FatGetCachePage (
171 IN FAT_VOLUME *Volume,
172 IN CACHE_DATA_TYPE CacheDataType,
173 IN UINTN PageNo,
174 IN CACHE_TAG *CacheTag
175 )
176 {
177 EFI_STATUS Status;
178 UINTN OldPageNo;
179
180 OldPageNo = CacheTag->PageNo;
181 if ((CacheTag->RealSize > 0) && (OldPageNo == PageNo)) {
182 //
183 // Cache Hit occurred
184 //
185 return EFI_SUCCESS;
186 }
187
188 //
189 // Write dirty cache page back to disk
190 //
191 if ((CacheTag->RealSize > 0) && CacheTag->Dirty) {
192 Status = FatExchangeCachePage (Volume, CacheDataType, WriteDisk, CacheTag, NULL);
193 if (EFI_ERROR (Status)) {
194 return Status;
195 }
196 }
197
198 //
199 // Load new data from disk;
200 //
201 CacheTag->PageNo = PageNo;
202 Status = FatExchangeCachePage (Volume, CacheDataType, ReadDisk, CacheTag, NULL);
203
204 return Status;
205 }
206
207 /**
208
209 Read Length bytes from the position of Offset into Buffer, or
210 write Length bytes from Buffer into the position of Offset.
211
212 @param Volume - FAT file system volume.
213 @param CacheDataType - The type of cache: CACHE_DATA or CACHE_FAT.
214 @param IoMode - Indicate the type of disk access.
215 @param PageNo - The number of unaligned cache page.
216 @param Offset - The starting byte of cache page.
217 @param Length - The number of bytes that is read or written
218 @param Buffer - Buffer containing cache data.
219
220 @retval EFI_SUCCESS - The data was accessed correctly.
221 @return Others - An error occurred when accessing unaligned cache page.
222
223 **/
224 STATIC
225 EFI_STATUS
226 FatAccessUnalignedCachePage (
227 IN FAT_VOLUME *Volume,
228 IN CACHE_DATA_TYPE CacheDataType,
229 IN IO_MODE IoMode,
230 IN UINTN PageNo,
231 IN UINTN Offset,
232 IN UINTN Length,
233 IN OUT VOID *Buffer
234 )
235 {
236 EFI_STATUS Status;
237 VOID *Source;
238 VOID *Destination;
239 DISK_CACHE *DiskCache;
240 CACHE_TAG *CacheTag;
241 UINTN GroupNo;
242
243 DiskCache = &Volume->DiskCache[CacheDataType];
244 GroupNo = PageNo & DiskCache->GroupMask;
245 CacheTag = &DiskCache->CacheTag[GroupNo];
246 Status = FatGetCachePage (Volume, CacheDataType, PageNo, CacheTag);
247 if (!EFI_ERROR (Status)) {
248 Source = DiskCache->CacheBase + (GroupNo << DiskCache->PageAlignment) + Offset;
249 Destination = Buffer;
250 if (IoMode != ReadDisk) {
251 CacheTag->Dirty = TRUE;
252 DiskCache->Dirty = TRUE;
253 Destination = Source;
254 Source = Buffer;
255 }
256
257 CopyMem (Destination, Source, Length);
258 }
259
260 return Status;
261 }
262
263 /**
264
265 Read BufferSize bytes from the position of Offset into Buffer,
266 or write BufferSize bytes from Buffer into the position of Offset.
267
268 Base on the parameter of CACHE_DATA_TYPE, the data access will be divided into
269 the access of FAT cache (CACHE_FAT) and the access of Data cache (CACHE_DATA):
270
271 1. Access of FAT cache (CACHE_FAT): Access the data in the FAT cache, if there is cache
272 page hit, just return the cache page; else update the related cache page and return
273 the right cache page.
274 2. Access of Data cache (CACHE_DATA):
275 The access data will be divided into UnderRun data, Aligned data and OverRun data;
276 The UnderRun data and OverRun data will be accessed by the Data cache,
277 but the Aligned data will be accessed with disk directly.
278
279 @param Volume - FAT file system volume.
280 @param CacheDataType - The type of cache: CACHE_DATA or CACHE_FAT.
281 @param IoMode - Indicate the type of disk access.
282 @param Offset - The starting byte offset to read from.
283 @param BufferSize - Size of Buffer.
284 @param Buffer - Buffer containing cache data.
285 @param Task point to task instance.
286
287 @retval EFI_SUCCESS - The data was accessed correctly.
288 @retval EFI_MEDIA_CHANGED - The MediaId does not match the current device.
289 @return Others - An error occurred when accessing cache.
290
291 **/
292 EFI_STATUS
293 FatAccessCache (
294 IN FAT_VOLUME *Volume,
295 IN CACHE_DATA_TYPE CacheDataType,
296 IN IO_MODE IoMode,
297 IN UINT64 Offset,
298 IN UINTN BufferSize,
299 IN OUT UINT8 *Buffer,
300 IN FAT_TASK *Task
301 )
302 {
303 EFI_STATUS Status;
304 UINTN PageSize;
305 UINTN UnderRun;
306 UINTN OverRun;
307 UINTN AlignedSize;
308 UINTN Length;
309 UINTN PageNo;
310 UINTN AlignedPageCount;
311 UINTN OverRunPageNo;
312 DISK_CACHE *DiskCache;
313 UINT64 EntryPos;
314 UINT8 PageAlignment;
315
316 ASSERT (Volume->CacheBuffer != NULL);
317
318 Status = EFI_SUCCESS;
319 DiskCache = &Volume->DiskCache[CacheDataType];
320 EntryPos = Offset - DiskCache->BaseAddress;
321 PageAlignment = DiskCache->PageAlignment;
322 PageSize = (UINTN)1 << PageAlignment;
323 PageNo = (UINTN)RShiftU64 (EntryPos, PageAlignment);
324 UnderRun = ((UINTN)EntryPos) & (PageSize - 1);
325
326 if (UnderRun > 0) {
327 Length = PageSize - UnderRun;
328 if (Length > BufferSize) {
329 Length = BufferSize;
330 }
331
332 Status = FatAccessUnalignedCachePage (Volume, CacheDataType, IoMode, PageNo, UnderRun, Length, Buffer);
333 if (EFI_ERROR (Status)) {
334 return Status;
335 }
336
337 Buffer += Length;
338 BufferSize -= Length;
339 PageNo++;
340 }
341
342 AlignedPageCount = BufferSize >> PageAlignment;
343 OverRunPageNo = PageNo + AlignedPageCount;
344 //
345 // The access of the Aligned data
346 //
347 if (AlignedPageCount > 0) {
348 //
349 // Accessing fat table cannot have alignment data
350 //
351 ASSERT (CacheDataType == CacheData);
352
353 EntryPos = Volume->RootPos + LShiftU64 (PageNo, PageAlignment);
354 AlignedSize = AlignedPageCount << PageAlignment;
355 Status = FatDiskIo (Volume, IoMode, EntryPos, AlignedSize, Buffer, Task);
356 if (EFI_ERROR (Status)) {
357 return Status;
358 }
359
360 //
361 // If these access data over laps the relative cache range, these cache pages need
362 // to be updated.
363 //
364 FatFlushDataCacheRange (Volume, IoMode, PageNo, OverRunPageNo, Buffer);
365 Buffer += AlignedSize;
366 BufferSize -= AlignedSize;
367 }
368
369 //
370 // The access of the OverRun data
371 //
372 OverRun = BufferSize;
373 if (OverRun > 0) {
374 //
375 // Last read is not a complete page
376 //
377 Status = FatAccessUnalignedCachePage (Volume, CacheDataType, IoMode, OverRunPageNo, 0, OverRun, Buffer);
378 }
379
380 return Status;
381 }
382
383 /**
384
385 Flush all the dirty cache back, include the FAT cache and the Data cache.
386
387 @param Volume - FAT file system volume.
388 @param Task point to task instance.
389
390 @retval EFI_SUCCESS - Flush all the dirty cache back successfully
391 @return other - An error occurred when writing the data into the disk
392
393 **/
394 EFI_STATUS
395 FatVolumeFlushCache (
396 IN FAT_VOLUME *Volume,
397 IN FAT_TASK *Task
398 )
399 {
400 EFI_STATUS Status;
401 CACHE_DATA_TYPE CacheDataType;
402 UINTN GroupIndex;
403 UINTN GroupMask;
404 DISK_CACHE *DiskCache;
405 CACHE_TAG *CacheTag;
406
407 for (CacheDataType = (CACHE_DATA_TYPE)0; CacheDataType < CacheMaxType; CacheDataType++) {
408 DiskCache = &Volume->DiskCache[CacheDataType];
409 if (DiskCache->Dirty) {
410 //
411 // Data cache or fat cache is dirty, write the dirty data back
412 //
413 GroupMask = DiskCache->GroupMask;
414 for (GroupIndex = 0; GroupIndex <= GroupMask; GroupIndex++) {
415 CacheTag = &DiskCache->CacheTag[GroupIndex];
416 if ((CacheTag->RealSize > 0) && CacheTag->Dirty) {
417 //
418 // Write back all Dirty Data Cache Page to disk
419 //
420 Status = FatExchangeCachePage (Volume, CacheDataType, WriteDisk, CacheTag, Task);
421 if (EFI_ERROR (Status)) {
422 return Status;
423 }
424 }
425 }
426
427 DiskCache->Dirty = FALSE;
428 }
429 }
430
431 //
432 // Flush the block device.
433 //
434 Status = Volume->BlockIo->FlushBlocks (Volume->BlockIo);
435 return Status;
436 }
437
438 /**
439
440 Initialize the disk cache according to Volume's FatType.
441
442 @param Volume - FAT file system volume.
443
444 @retval EFI_SUCCESS - The disk cache is successfully initialized.
445 @retval EFI_OUT_OF_RESOURCES - Not enough memory to allocate disk cache.
446
447 **/
448 EFI_STATUS
449 FatInitializeDiskCache (
450 IN FAT_VOLUME *Volume
451 )
452 {
453 DISK_CACHE *DiskCache;
454 UINTN FatCacheGroupCount;
455 UINTN DataCacheSize;
456 UINTN FatCacheSize;
457 UINT8 *CacheBuffer;
458
459 DiskCache = Volume->DiskCache;
460 //
461 // Configure the parameters of disk cache
462 //
463 if (Volume->FatType == Fat12) {
464 FatCacheGroupCount = FAT_FATCACHE_GROUP_MIN_COUNT;
465 DiskCache[CacheFat].PageAlignment = FAT_FATCACHE_PAGE_MIN_ALIGNMENT;
466 DiskCache[CacheData].PageAlignment = FAT_DATACACHE_PAGE_MIN_ALIGNMENT;
467 } else {
468 FatCacheGroupCount = FAT_FATCACHE_GROUP_MAX_COUNT;
469 DiskCache[CacheFat].PageAlignment = FAT_FATCACHE_PAGE_MAX_ALIGNMENT;
470 DiskCache[CacheData].PageAlignment = FAT_DATACACHE_PAGE_MAX_ALIGNMENT;
471 }
472
473 DiskCache[CacheData].GroupMask = FAT_DATACACHE_GROUP_COUNT - 1;
474 DiskCache[CacheData].BaseAddress = Volume->RootPos;
475 DiskCache[CacheData].LimitAddress = Volume->VolumeSize;
476 DiskCache[CacheFat].GroupMask = FatCacheGroupCount - 1;
477 DiskCache[CacheFat].BaseAddress = Volume->FatPos;
478 DiskCache[CacheFat].LimitAddress = Volume->FatPos + Volume->FatSize;
479 FatCacheSize = FatCacheGroupCount << DiskCache[CacheFat].PageAlignment;
480 DataCacheSize = FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment;
481 //
482 // Allocate the Fat Cache buffer
483 //
484 CacheBuffer = AllocateZeroPool (FatCacheSize + DataCacheSize);
485 if (CacheBuffer == NULL) {
486 return EFI_OUT_OF_RESOURCES;
487 }
488
489 Volume->CacheBuffer = CacheBuffer;
490 DiskCache[CacheFat].CacheBase = CacheBuffer;
491 DiskCache[CacheData].CacheBase = CacheBuffer + FatCacheSize;
492 return EFI_SUCCESS;
493 }