]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHcMem.c
d69e6e72272a608f9ca42086c3a20bfbba09dbd8
[mirror_edk2.git] / MdeModulePkg / Bus / Ufs / UfsBlockIoPei / UfsHcMem.c
1 /** @file
2
3 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
4
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions
7 of the BSD License which accompanies this distribution. The
8 full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UfsBlockIoPei.h"
17
18 /**
19 Allocate a block of memory to be used by the buffer pool.
20
21 @param Pages How many pages to allocate.
22
23 @return The allocated memory block or NULL if failed.
24
25 **/
26 UFS_PEIM_MEM_BLOCK *
27 UfsPeimAllocMemBlock (
28 IN UINTN Pages
29 )
30 {
31 UFS_PEIM_MEM_BLOCK *Block;
32 VOID *BufHost;
33 VOID *Mapping;
34 EFI_PHYSICAL_ADDRESS MappedAddr;
35 EFI_STATUS Status;
36 VOID *TempPtr;
37
38 TempPtr = NULL;
39 Block = NULL;
40
41 Status = PeiServicesAllocatePool (sizeof(UFS_PEIM_MEM_BLOCK), &TempPtr);
42 if (EFI_ERROR (Status)) {
43 return NULL;
44 }
45
46 ZeroMem ((VOID*)(UINTN)TempPtr, sizeof(UFS_PEIM_MEM_BLOCK));
47
48 //
49 // each bit in the bit array represents UFS_PEIM_MEM_UNIT
50 // bytes of memory in the memory block.
51 //
52 ASSERT (UFS_PEIM_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
53
54 Block = (UFS_PEIM_MEM_BLOCK*)(UINTN)TempPtr;
55 Block->BufLen = EFI_PAGES_TO_SIZE (Pages);
56 Block->BitsLen = Block->BufLen / (UFS_PEIM_MEM_UNIT * 8);
57
58 Status = PeiServicesAllocatePool (Block->BitsLen, &TempPtr);
59 if (EFI_ERROR (Status)) {
60 return NULL;
61 }
62
63 ZeroMem ((VOID*)(UINTN)TempPtr, Block->BitsLen);
64
65 Block->Bits = (UINT8*)(UINTN)TempPtr;
66
67 Status = IoMmuAllocateBuffer (
68 Pages,
69 &BufHost,
70 &MappedAddr,
71 &Mapping
72 );
73 if (EFI_ERROR (Status)) {
74 return NULL;
75 }
76
77 ZeroMem ((VOID*)(UINTN)BufHost, EFI_PAGES_TO_SIZE (Pages));
78
79 Block->BufHost = (UINT8 *) (UINTN) BufHost;
80 Block->Buf = (UINT8 *) (UINTN) MappedAddr;
81 Block->Mapping = Mapping;
82 Block->Next = NULL;
83
84 return Block;
85 }
86
87 /**
88 Free the memory block from the memory pool.
89
90 @param Pool The memory pool to free the block from.
91 @param Block The memory block to free.
92
93 **/
94 VOID
95 UfsPeimFreeMemBlock (
96 IN UFS_PEIM_MEM_POOL *Pool,
97 IN UFS_PEIM_MEM_BLOCK *Block
98 )
99 {
100 ASSERT ((Pool != NULL) && (Block != NULL));
101
102 IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost, Block->Mapping);
103 }
104
105 /**
106 Alloc some memory from the block.
107
108 @param Block The memory block to allocate memory from.
109 @param Units Number of memory units to allocate.
110
111 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
112 the return value is NULL.
113
114 **/
115 VOID *
116 UfsPeimAllocMemFromBlock (
117 IN UFS_PEIM_MEM_BLOCK *Block,
118 IN UINTN Units
119 )
120 {
121 UINTN Byte;
122 UINT8 Bit;
123 UINTN StartByte;
124 UINT8 StartBit;
125 UINTN Available;
126 UINTN Count;
127
128 ASSERT ((Block != 0) && (Units != 0));
129
130 StartByte = 0;
131 StartBit = 0;
132 Available = 0;
133
134 for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
135 //
136 // If current bit is zero, the corresponding memory unit is
137 // available, otherwise we need to restart our searching.
138 // Available counts the consective number of zero bit.
139 //
140 if (!UFS_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit)) {
141 Available++;
142
143 if (Available >= Units) {
144 break;
145 }
146
147 UFS_PEIM_NEXT_BIT (Byte, Bit);
148
149 } else {
150 UFS_PEIM_NEXT_BIT (Byte, Bit);
151
152 Available = 0;
153 StartByte = Byte;
154 StartBit = Bit;
155 }
156 }
157
158 if (Available < Units) {
159 return NULL;
160 }
161
162 //
163 // Mark the memory as allocated
164 //
165 Byte = StartByte;
166 Bit = StartBit;
167
168 for (Count = 0; Count < Units; Count++) {
169 ASSERT (!UFS_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit));
170
171 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] | (UINT8) UFS_PEIM_MEM_BIT (Bit));
172 UFS_PEIM_NEXT_BIT (Byte, Bit);
173 }
174
175 return Block->Buf + (StartByte * 8 + StartBit) * UFS_PEIM_MEM_UNIT;
176 }
177
178 /**
179 Insert the memory block to the pool's list of the blocks.
180
181 @param Head The head of the memory pool's block list.
182 @param Block The memory block to insert.
183
184 **/
185 VOID
186 UfsPeimInsertMemBlockToPool (
187 IN UFS_PEIM_MEM_BLOCK *Head,
188 IN UFS_PEIM_MEM_BLOCK *Block
189 )
190 {
191 ASSERT ((Head != NULL) && (Block != NULL));
192 Block->Next = Head->Next;
193 Head->Next = Block;
194 }
195
196 /**
197 Is the memory block empty?
198
199 @param Block The memory block to check.
200
201 @retval TRUE The memory block is empty.
202 @retval FALSE The memory block isn't empty.
203
204 **/
205 BOOLEAN
206 UfsPeimIsMemBlockEmpty (
207 IN UFS_PEIM_MEM_BLOCK *Block
208 )
209 {
210 UINTN Index;
211
212
213 for (Index = 0; Index < Block->BitsLen; Index++) {
214 if (Block->Bits[Index] != 0) {
215 return FALSE;
216 }
217 }
218
219 return TRUE;
220 }
221
222 /**
223 Unlink the memory block from the pool's list.
224
225 @param Head The block list head of the memory's pool.
226 @param BlockToUnlink The memory block to unlink.
227
228 **/
229 VOID
230 UfsPeimUnlinkMemBlock (
231 IN UFS_PEIM_MEM_BLOCK *Head,
232 IN UFS_PEIM_MEM_BLOCK *BlockToUnlink
233 )
234 {
235 UFS_PEIM_MEM_BLOCK *Block;
236
237 ASSERT ((Head != NULL) && (BlockToUnlink != NULL));
238
239 for (Block = Head; Block != NULL; Block = Block->Next) {
240 if (Block->Next == BlockToUnlink) {
241 Block->Next = BlockToUnlink->Next;
242 BlockToUnlink->Next = NULL;
243 break;
244 }
245 }
246 }
247
248 /**
249 Initialize the memory management pool for the host controller.
250
251 @param Private The Ufs Peim driver private data.
252
253 @retval EFI_SUCCESS The memory pool is initialized.
254 @retval Others Fail to init the memory pool.
255
256 **/
257 EFI_STATUS
258 UfsPeimInitMemPool (
259 IN UFS_PEIM_HC_PRIVATE_DATA *Private
260 )
261 {
262 UFS_PEIM_MEM_POOL *Pool;
263 EFI_STATUS Status;
264 VOID *TempPtr;
265
266 TempPtr = NULL;
267 Pool = NULL;
268
269 Status = PeiServicesAllocatePool (sizeof (UFS_PEIM_MEM_POOL), &TempPtr);
270 if (EFI_ERROR (Status)) {
271 return EFI_OUT_OF_RESOURCES;
272 }
273
274 ZeroMem ((VOID*)(UINTN)TempPtr, sizeof (UFS_PEIM_MEM_POOL));
275
276 Pool = (UFS_PEIM_MEM_POOL *)((UINTN)TempPtr);
277
278 Pool->Head = UfsPeimAllocMemBlock (UFS_PEIM_MEM_DEFAULT_PAGES);
279
280 if (Pool->Head == NULL) {
281 return EFI_OUT_OF_RESOURCES;
282 }
283
284 Private->Pool = Pool;
285 return EFI_SUCCESS;
286 }
287
288 /**
289 Release the memory management pool.
290
291 @param Pool The memory pool to free.
292
293 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
294 @retval EFI_SUCCESS The memory pool is freed.
295
296 **/
297 EFI_STATUS
298 UfsPeimFreeMemPool (
299 IN UFS_PEIM_MEM_POOL *Pool
300 )
301 {
302 UFS_PEIM_MEM_BLOCK *Block;
303
304 ASSERT (Pool->Head != NULL);
305
306 //
307 // Unlink all the memory blocks from the pool, then free them.
308 // UfsPeimUnlinkMemBlock can't be used to unlink and free the
309 // first block.
310 //
311 for (Block = Pool->Head->Next; Block != NULL; Block = Pool->Head->Next) {
312 UfsPeimFreeMemBlock (Pool, Block);
313 }
314
315 UfsPeimFreeMemBlock (Pool, Pool->Head);
316
317 return EFI_SUCCESS;
318 }
319
320 /**
321 Allocate some memory from the host controller's memory pool
322 which can be used to communicate with host controller.
323
324 @param Pool The host controller's memory pool.
325 @param Size Size of the memory to allocate.
326
327 @return The allocated memory or NULL.
328
329 **/
330 VOID *
331 UfsPeimAllocateMem (
332 IN UFS_PEIM_MEM_POOL *Pool,
333 IN UINTN Size
334 )
335 {
336 UFS_PEIM_MEM_BLOCK *Head;
337 UFS_PEIM_MEM_BLOCK *Block;
338 UFS_PEIM_MEM_BLOCK *NewBlock;
339 VOID *Mem;
340 UINTN AllocSize;
341 UINTN Pages;
342
343 Mem = NULL;
344 AllocSize = UFS_PEIM_MEM_ROUND (Size);
345 Head = Pool->Head;
346 ASSERT (Head != NULL);
347
348 //
349 // First check whether current memory blocks can satisfy the allocation.
350 //
351 for (Block = Head; Block != NULL; Block = Block->Next) {
352 Mem = UfsPeimAllocMemFromBlock (Block, AllocSize / UFS_PEIM_MEM_UNIT);
353
354 if (Mem != NULL) {
355 ZeroMem (Mem, Size);
356 break;
357 }
358 }
359
360 if (Mem != NULL) {
361 return Mem;
362 }
363
364 //
365 // Create a new memory block if there is not enough memory
366 // in the pool. If the allocation size is larger than the
367 // default page number, just allocate a large enough memory
368 // block. Otherwise allocate default pages.
369 //
370 if (AllocSize > EFI_PAGES_TO_SIZE (UFS_PEIM_MEM_DEFAULT_PAGES)) {
371 Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;
372 } else {
373 Pages = UFS_PEIM_MEM_DEFAULT_PAGES;
374 }
375
376 NewBlock = UfsPeimAllocMemBlock (Pages);
377 if (NewBlock == NULL) {
378 return NULL;
379 }
380
381 //
382 // Add the new memory block to the pool, then allocate memory from it
383 //
384 UfsPeimInsertMemBlockToPool (Head, NewBlock);
385 Mem = UfsPeimAllocMemFromBlock (NewBlock, AllocSize / UFS_PEIM_MEM_UNIT);
386
387 if (Mem != NULL) {
388 ZeroMem (Mem, Size);
389 }
390
391 return Mem;
392 }
393
394 /**
395 Free the allocated memory back to the memory pool.
396
397 @param Pool The memory pool of the host controller.
398 @param Mem The memory to free.
399 @param Size The size of the memory to free.
400
401 **/
402 VOID
403 UfsPeimFreeMem (
404 IN UFS_PEIM_MEM_POOL *Pool,
405 IN VOID *Mem,
406 IN UINTN Size
407 )
408 {
409 UFS_PEIM_MEM_BLOCK *Head;
410 UFS_PEIM_MEM_BLOCK *Block;
411 UINT8 *ToFree;
412 UINTN AllocSize;
413 UINTN Byte;
414 UINTN Bit;
415 UINTN Count;
416
417 Head = Pool->Head;
418 AllocSize = UFS_PEIM_MEM_ROUND (Size);
419 ToFree = (UINT8 *) Mem;
420
421 for (Block = Head; Block != NULL; Block = Block->Next) {
422 //
423 // scan the memory block list for the memory block that
424 // completely contains the memory to free.
425 //
426 if ((Block->Buf <= ToFree) && ((ToFree + AllocSize) <= (Block->Buf + Block->BufLen))) {
427 //
428 // compute the start byte and bit in the bit array
429 //
430 Byte = ((ToFree - Block->Buf) / UFS_PEIM_MEM_UNIT) / 8;
431 Bit = ((ToFree - Block->Buf) / UFS_PEIM_MEM_UNIT) % 8;
432
433 //
434 // reset associated bits in bit array
435 //
436 for (Count = 0; Count < (AllocSize / UFS_PEIM_MEM_UNIT); Count++) {
437 ASSERT (UFS_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit));
438
439 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] ^ UFS_PEIM_MEM_BIT (Bit));
440 UFS_PEIM_NEXT_BIT (Byte, Bit);
441 }
442
443 break;
444 }
445 }
446
447 //
448 // If Block == NULL, it means that the current memory isn't
449 // in the host controller's pool. This is critical because
450 // the caller has passed in a wrong memory point
451 //
452 ASSERT (Block != NULL);
453
454 //
455 // Release the current memory block if it is empty and not the head
456 //
457 if ((Block != Head) && UfsPeimIsMemBlockEmpty (Block)) {
458 UfsPeimFreeMemBlock (Pool, Block);
459 }
460
461 return ;
462 }