UfsHcMem.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /** @file
  2. Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
  3. Copyright (c) 1985 - 2022, American Megatrends International LLC. <BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "UfsBlockIoPei.h"
  7. /**
  8. Allocate a block of memory to be used by the buffer pool.
  9. @param Pages How many pages to allocate.
  10. @return The allocated memory block or NULL if failed.
  11. **/
  12. UFS_PEIM_MEM_BLOCK *
  13. UfsPeimAllocMemBlock (
  14. IN UINTN Pages
  15. )
  16. {
  17. UFS_PEIM_MEM_BLOCK *Block;
  18. VOID *BufHost;
  19. VOID *Mapping;
  20. EFI_PHYSICAL_ADDRESS MappedAddr;
  21. EFI_STATUS Status;
  22. VOID *TempPtr;
  23. TempPtr = NULL;
  24. Block = NULL;
  25. Status = PeiServicesAllocatePool (sizeof (UFS_PEIM_MEM_BLOCK), &TempPtr);
  26. if (EFI_ERROR (Status)) {
  27. return NULL;
  28. }
  29. ZeroMem ((VOID *)(UINTN)TempPtr, sizeof (UFS_PEIM_MEM_BLOCK));
  30. //
  31. // each bit in the bit array represents UFS_PEIM_MEM_UNIT
  32. // bytes of memory in the memory block.
  33. //
  34. ASSERT (UFS_PEIM_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
  35. Block = (UFS_PEIM_MEM_BLOCK *)(UINTN)TempPtr;
  36. Block->BufLen = EFI_PAGES_TO_SIZE (Pages);
  37. Block->BitsLen = Block->BufLen / (UFS_PEIM_MEM_UNIT * 8);
  38. Status = PeiServicesAllocatePool (Block->BitsLen, &TempPtr);
  39. if (EFI_ERROR (Status)) {
  40. return NULL;
  41. }
  42. ZeroMem ((VOID *)(UINTN)TempPtr, Block->BitsLen);
  43. Block->Bits = (UINT8 *)(UINTN)TempPtr;
  44. Status = IoMmuAllocateBuffer (
  45. Pages,
  46. &BufHost,
  47. &MappedAddr,
  48. &Mapping
  49. );
  50. if (EFI_ERROR (Status)) {
  51. return NULL;
  52. }
  53. ZeroMem ((VOID *)(UINTN)BufHost, EFI_PAGES_TO_SIZE (Pages));
  54. Block->BufHost = (UINT8 *)(UINTN)BufHost;
  55. Block->Buf = (UINT8 *)(UINTN)MappedAddr;
  56. Block->Mapping = Mapping;
  57. Block->Next = NULL;
  58. return Block;
  59. }
  60. /**
  61. Free the memory block from the memory pool.
  62. @param Pool The memory pool to free the block from.
  63. @param Block The memory block to free.
  64. **/
  65. VOID
  66. UfsPeimFreeMemBlock (
  67. IN UFS_PEIM_MEM_POOL *Pool,
  68. IN UFS_PEIM_MEM_BLOCK *Block
  69. )
  70. {
  71. ASSERT ((Pool != NULL) && (Block != NULL));
  72. IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost, Block->Mapping);
  73. }
  74. /**
  75. Alloc some memory from the block.
  76. @param Block The memory block to allocate memory from.
  77. @param Units Number of memory units to allocate.
  78. @return The pointer to the allocated memory. If couldn't allocate the needed memory,
  79. the return value is NULL.
  80. **/
  81. VOID *
  82. UfsPeimAllocMemFromBlock (
  83. IN UFS_PEIM_MEM_BLOCK *Block,
  84. IN UINTN Units
  85. )
  86. {
  87. UINTN Byte;
  88. UINT8 Bit;
  89. UINTN StartByte;
  90. UINT8 StartBit;
  91. UINTN Available;
  92. UINTN Count;
  93. ASSERT ((Block != 0) && (Units != 0));
  94. StartByte = 0;
  95. StartBit = 0;
  96. Available = 0;
  97. for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
  98. //
  99. // If current bit is zero, the corresponding memory unit is
  100. // available, otherwise we need to restart our searching.
  101. // Available counts the consective number of zero bit.
  102. //
  103. if (!UFS_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit)) {
  104. Available++;
  105. if (Available >= Units) {
  106. break;
  107. }
  108. UFS_PEIM_NEXT_BIT (Byte, Bit);
  109. } else {
  110. UFS_PEIM_NEXT_BIT (Byte, Bit);
  111. Available = 0;
  112. StartByte = Byte;
  113. StartBit = Bit;
  114. }
  115. }
  116. if (Available < Units) {
  117. return NULL;
  118. }
  119. //
  120. // Mark the memory as allocated
  121. //
  122. Byte = StartByte;
  123. Bit = StartBit;
  124. for (Count = 0; Count < Units; Count++) {
  125. ASSERT (!UFS_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit));
  126. Block->Bits[Byte] = (UINT8)(Block->Bits[Byte] | (UINT8)UFS_PEIM_MEM_BIT (Bit));
  127. UFS_PEIM_NEXT_BIT (Byte, Bit);
  128. }
  129. return Block->Buf + (StartByte * 8 + StartBit) * UFS_PEIM_MEM_UNIT;
  130. }
  131. /**
  132. Insert the memory block to the pool's list of the blocks.
  133. @param Head The head of the memory pool's block list.
  134. @param Block The memory block to insert.
  135. **/
  136. VOID
  137. UfsPeimInsertMemBlockToPool (
  138. IN UFS_PEIM_MEM_BLOCK *Head,
  139. IN UFS_PEIM_MEM_BLOCK *Block
  140. )
  141. {
  142. ASSERT ((Head != NULL) && (Block != NULL));
  143. Block->Next = Head->Next;
  144. Head->Next = Block;
  145. }
  146. /**
  147. Is the memory block empty?
  148. @param Block The memory block to check.
  149. @retval TRUE The memory block is empty.
  150. @retval FALSE The memory block isn't empty.
  151. **/
  152. BOOLEAN
  153. UfsPeimIsMemBlockEmpty (
  154. IN UFS_PEIM_MEM_BLOCK *Block
  155. )
  156. {
  157. UINTN Index;
  158. for (Index = 0; Index < Block->BitsLen; Index++) {
  159. if (Block->Bits[Index] != 0) {
  160. return FALSE;
  161. }
  162. }
  163. return TRUE;
  164. }
  165. /**
  166. Initialize the memory management pool for the host controller.
  167. @param Private The Ufs Peim driver private data.
  168. @retval EFI_SUCCESS The memory pool is initialized.
  169. @retval Others Fail to init the memory pool.
  170. **/
  171. EFI_STATUS
  172. UfsPeimInitMemPool (
  173. IN UFS_PEIM_HC_PRIVATE_DATA *Private
  174. )
  175. {
  176. UFS_PEIM_MEM_POOL *Pool;
  177. EFI_STATUS Status;
  178. VOID *TempPtr;
  179. TempPtr = NULL;
  180. Pool = NULL;
  181. Status = PeiServicesAllocatePool (sizeof (UFS_PEIM_MEM_POOL), &TempPtr);
  182. if (EFI_ERROR (Status)) {
  183. return EFI_OUT_OF_RESOURCES;
  184. }
  185. ZeroMem ((VOID *)(UINTN)TempPtr, sizeof (UFS_PEIM_MEM_POOL));
  186. Pool = (UFS_PEIM_MEM_POOL *)((UINTN)TempPtr);
  187. Pool->Head = UfsPeimAllocMemBlock (UFS_PEIM_MEM_DEFAULT_PAGES);
  188. if (Pool->Head == NULL) {
  189. return EFI_OUT_OF_RESOURCES;
  190. }
  191. Private->Pool = Pool;
  192. return EFI_SUCCESS;
  193. }
  194. /**
  195. Release the memory management pool.
  196. @param Pool The memory pool to free.
  197. @retval EFI_DEVICE_ERROR Fail to free the memory pool.
  198. @retval EFI_SUCCESS The memory pool is freed.
  199. **/
  200. EFI_STATUS
  201. UfsPeimFreeMemPool (
  202. IN UFS_PEIM_MEM_POOL *Pool
  203. )
  204. {
  205. UFS_PEIM_MEM_BLOCK *Block;
  206. ASSERT (Pool->Head != NULL);
  207. //
  208. // Unlink all the memory blocks from the pool, then free them.
  209. //
  210. for (Block = Pool->Head->Next; Block != NULL; Block = Pool->Head->Next) {
  211. UfsPeimFreeMemBlock (Pool, Block);
  212. }
  213. UfsPeimFreeMemBlock (Pool, Pool->Head);
  214. return EFI_SUCCESS;
  215. }
  216. /**
  217. Allocate some memory from the host controller's memory pool
  218. which can be used to communicate with host controller.
  219. @param Pool The host controller's memory pool.
  220. @param Size Size of the memory to allocate.
  221. @return The allocated memory or NULL.
  222. **/
  223. VOID *
  224. UfsPeimAllocateMem (
  225. IN UFS_PEIM_MEM_POOL *Pool,
  226. IN UINTN Size
  227. )
  228. {
  229. UFS_PEIM_MEM_BLOCK *Head;
  230. UFS_PEIM_MEM_BLOCK *Block;
  231. UFS_PEIM_MEM_BLOCK *NewBlock;
  232. VOID *Mem;
  233. UINTN AllocSize;
  234. UINTN Pages;
  235. Mem = NULL;
  236. AllocSize = UFS_PEIM_MEM_ROUND (Size);
  237. Head = Pool->Head;
  238. ASSERT (Head != NULL);
  239. //
  240. // First check whether current memory blocks can satisfy the allocation.
  241. //
  242. for (Block = Head; Block != NULL; Block = Block->Next) {
  243. Mem = UfsPeimAllocMemFromBlock (Block, AllocSize / UFS_PEIM_MEM_UNIT);
  244. if (Mem != NULL) {
  245. ZeroMem (Mem, Size);
  246. break;
  247. }
  248. }
  249. if (Mem != NULL) {
  250. return Mem;
  251. }
  252. //
  253. // Create a new memory block if there is not enough memory
  254. // in the pool. If the allocation size is larger than the
  255. // default page number, just allocate a large enough memory
  256. // block. Otherwise allocate default pages.
  257. //
  258. if (AllocSize > EFI_PAGES_TO_SIZE (UFS_PEIM_MEM_DEFAULT_PAGES)) {
  259. Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;
  260. } else {
  261. Pages = UFS_PEIM_MEM_DEFAULT_PAGES;
  262. }
  263. NewBlock = UfsPeimAllocMemBlock (Pages);
  264. if (NewBlock == NULL) {
  265. return NULL;
  266. }
  267. //
  268. // Add the new memory block to the pool, then allocate memory from it
  269. //
  270. UfsPeimInsertMemBlockToPool (Head, NewBlock);
  271. Mem = UfsPeimAllocMemFromBlock (NewBlock, AllocSize / UFS_PEIM_MEM_UNIT);
  272. if (Mem != NULL) {
  273. ZeroMem (Mem, Size);
  274. }
  275. return Mem;
  276. }
  277. /**
  278. Free the allocated memory back to the memory pool.
  279. @param Pool The memory pool of the host controller.
  280. @param Mem The memory to free.
  281. @param Size The size of the memory to free.
  282. **/
  283. VOID
  284. UfsPeimFreeMem (
  285. IN UFS_PEIM_MEM_POOL *Pool,
  286. IN VOID *Mem,
  287. IN UINTN Size
  288. )
  289. {
  290. UFS_PEIM_MEM_BLOCK *Head;
  291. UFS_PEIM_MEM_BLOCK *Block;
  292. UINT8 *ToFree;
  293. UINTN AllocSize;
  294. UINTN Byte;
  295. UINTN Bit;
  296. UINTN Count;
  297. Head = Pool->Head;
  298. AllocSize = UFS_PEIM_MEM_ROUND (Size);
  299. ToFree = (UINT8 *)Mem;
  300. for (Block = Head; Block != NULL; Block = Block->Next) {
  301. //
  302. // scan the memory block list for the memory block that
  303. // completely contains the memory to free.
  304. //
  305. if ((Block->Buf <= ToFree) && ((ToFree + AllocSize) <= (Block->Buf + Block->BufLen))) {
  306. //
  307. // compute the start byte and bit in the bit array
  308. //
  309. Byte = ((ToFree - Block->Buf) / UFS_PEIM_MEM_UNIT) / 8;
  310. Bit = ((ToFree - Block->Buf) / UFS_PEIM_MEM_UNIT) % 8;
  311. //
  312. // reset associated bits in bit array
  313. //
  314. for (Count = 0; Count < (AllocSize / UFS_PEIM_MEM_UNIT); Count++) {
  315. ASSERT (UFS_PEIM_MEM_BIT_IS_SET (Block->Bits[Byte], Bit));
  316. Block->Bits[Byte] = (UINT8)(Block->Bits[Byte] ^ UFS_PEIM_MEM_BIT (Bit));
  317. UFS_PEIM_NEXT_BIT (Byte, Bit);
  318. }
  319. break;
  320. }
  321. }
  322. //
  323. // If Block == NULL, it means that the current memory isn't
  324. // in the host controller's pool. This is critical because
  325. // the caller has passed in a wrong memory point
  326. //
  327. ASSERT (Block != NULL);
  328. if (Block == NULL) {
  329. return;
  330. }
  331. //
  332. // Release the current memory block if it is empty and not the head
  333. //
  334. if ((Block != Head) && UfsPeimIsMemBlockEmpty (Block)) {
  335. UfsPeimFreeMemBlock (Pool, Block);
  336. }
  337. return;
  338. }