Angel Pons | ae59387 | 2020-04-04 18:50:57 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 2 | |
Richard Spiegel | 0ad74ac | 2017-12-08 16:53:29 -0700 | [diff] [blame] | 3 | #include <amdblocks/agesawrapper.h> |
Richard Spiegel | 0ad74ac | 2017-12-08 16:53:29 -0700 | [diff] [blame] | 4 | #include <amdblocks/BiosCallOuts.h> |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 5 | #include <cbmem.h> |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 6 | #include <string.h> |
| 7 | |
Marshall Dawson | 1dd7a11 | 2018-03-28 20:21:10 -0600 | [diff] [blame] | 8 | static void *agesa_heap_base(void) |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 9 | { |
Marshall Dawson | 8cc5fde | 2018-01-30 15:23:48 -0700 | [diff] [blame] | 10 | return cbmem_add(CBMEM_ID_RESUME_SCRATCH, BIOS_HEAP_SIZE); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 11 | } |
| 12 | |
Marshall Dawson | 6c74706 | 2017-09-21 12:21:14 -0600 | [diff] [blame] | 13 | static void EmptyHeap(int unused) |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 14 | { |
Marshall Dawson | 21c5e15 | 2017-12-15 11:13:54 -0700 | [diff] [blame] | 15 | void *BiosManagerPtr = agesa_heap_base(); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 16 | memset(BiosManagerPtr, 0, BIOS_HEAP_SIZE); |
| 17 | } |
| 18 | |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 19 | /* |
Marshall Dawson | 10b52e0 | 2018-05-07 08:51:04 -0600 | [diff] [blame] | 20 | * Name agesa_GetTempHeapBase |
| 21 | * Brief description Get the location for TempRam, the target location in |
| 22 | * memory where AmdInitPost copies the heap prior to CAR |
| 23 | * teardown. AmdInitEnv calls this function after |
| 24 | * teardown for the source address when relocation the |
| 25 | * heap to its final location. |
| 26 | * Input parameters |
| 27 | * Func Unused |
| 28 | * Data Unused |
| 29 | * ConfigPtr Pointer to type AGESA_TEMP_HEAP_BASE_PARAMS |
| 30 | * Output parameters |
| 31 | * Status Indicates whether TempHeapAddress was successfully |
| 32 | * set. |
| 33 | */ |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 34 | AGESA_STATUS agesa_GetTempHeapBase(uint32_t Func, uintptr_t Data, |
| 35 | void *ConfigPtr) |
Marshall Dawson | 10b52e0 | 2018-05-07 08:51:04 -0600 | [diff] [blame] | 36 | { |
| 37 | AGESA_TEMP_HEAP_BASE_PARAMS *pTempHeapBase; |
| 38 | |
| 39 | pTempHeapBase = (AGESA_TEMP_HEAP_BASE_PARAMS *)ConfigPtr; |
| 40 | pTempHeapBase->TempHeapAddress = CONFIG_PI_AGESA_TEMP_RAM_BASE; |
| 41 | |
| 42 | return AGESA_SUCCESS; |
| 43 | } |
| 44 | |
| 45 | /* |
Marshall Dawson | c150a57 | 2018-04-30 17:59:27 -0600 | [diff] [blame] | 46 | * Name agesa_HeapRebase |
| 47 | * Brief description AGESA may use internal hardcoded locations for its |
| 48 | * heap. Modern implementations allow the base to be |
| 49 | * overridden by calling agesa_HeapRebase. |
| 50 | * Input parameters |
| 51 | * Func Unused |
| 52 | * Data Unused |
| 53 | * ConfigPtr Pointer to type AGESA_REBASE_PARAMS |
| 54 | * Output parameters |
| 55 | * Status Indicates whether HeapAddress was successfully |
| 56 | * set. |
| 57 | */ |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 58 | AGESA_STATUS agesa_HeapRebase(uint32_t Func, uintptr_t Data, void *ConfigPtr) |
Marshall Dawson | c150a57 | 2018-04-30 17:59:27 -0600 | [diff] [blame] | 59 | { |
| 60 | AGESA_REBASE_PARAMS *Rebase; |
| 61 | |
| 62 | Rebase = (AGESA_REBASE_PARAMS *)ConfigPtr; |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 63 | Rebase->HeapAddress = (uintptr_t)agesa_heap_base(); |
Marshall Dawson | 418c2bb | 2018-06-27 10:45:33 -0600 | [diff] [blame] | 64 | if (!Rebase->HeapAddress) |
Marshall Dawson | c150a57 | 2018-04-30 17:59:27 -0600 | [diff] [blame] | 65 | Rebase->HeapAddress = CONFIG_PI_AGESA_CAR_HEAP_BASE; |
Marshall Dawson | c150a57 | 2018-04-30 17:59:27 -0600 | [diff] [blame] | 66 | |
| 67 | return AGESA_SUCCESS; |
| 68 | } |
| 69 | |
| 70 | /* |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 71 | * Name FindAllocatedNode |
| 72 | * Brief description Find an allocated node that matches the handle. |
| 73 | * Input parameter The desired handle. |
| 74 | * Output parameters |
| 75 | * pointer Here is returned either the found node or the last |
| 76 | * allocated node if the handle is not found. This is |
| 77 | * intentional, as the field NextNode of this node will |
| 78 | * have to be filled with the offset of the node being |
| 79 | * created in procedure agesa_AllocateBuffer(). |
| 80 | * Status Indicates if the node was or was not found. |
| 81 | */ |
| 82 | static AGESA_STATUS FindAllocatedNode(uint32_t handle, |
| 83 | BIOS_BUFFER_NODE **last_allocd_or_match) |
| 84 | { |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 85 | uint32_t AllocNodeOffset; |
| 86 | uint8_t *BiosHeapBaseAddr; |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 87 | BIOS_BUFFER_NODE *AllocNodePtr; |
| 88 | BIOS_HEAP_MANAGER *BiosHeapBasePtr; |
| 89 | AGESA_STATUS Status = AGESA_SUCCESS; |
| 90 | |
| 91 | BiosHeapBaseAddr = agesa_heap_base(); |
| 92 | BiosHeapBasePtr = (BIOS_HEAP_MANAGER *)BiosHeapBaseAddr; |
| 93 | |
| 94 | AllocNodeOffset = BiosHeapBasePtr->StartOfAllocatedNodes; |
| 95 | AllocNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr + AllocNodeOffset); |
| 96 | |
| 97 | while (handle != AllocNodePtr->BufferHandle) { |
| 98 | if (AllocNodePtr->NextNodeOffset == 0) { |
| 99 | Status = AGESA_BOUNDS_CHK; |
| 100 | break; |
| 101 | } |
| 102 | AllocNodeOffset = AllocNodePtr->NextNodeOffset; |
| 103 | AllocNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr + |
| 104 | AllocNodeOffset); |
| 105 | } |
| 106 | *last_allocd_or_match = AllocNodePtr; |
| 107 | return Status; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Name ConcatenateNodes |
| 112 | * Brief description Concatenates two adjacent nodes into a single node, |
| 113 | * this procedure is used by agesa_DeallocateBuffer(). |
| 114 | * Input parameters |
| 115 | * FirstNodePtr This node is in the front, its header will be |
| 116 | * maintained. |
| 117 | * SecondNodePtr This node is in the back, its header will be cleared. |
| 118 | */ |
| 119 | static void ConcatenateNodes(BIOS_BUFFER_NODE *FirstNodePtr, |
| 120 | BIOS_BUFFER_NODE *SecondNodePtr) |
| 121 | { |
| 122 | FirstNodePtr->BufferSize += SecondNodePtr->BufferSize + |
| 123 | sizeof(BIOS_BUFFER_NODE); |
| 124 | FirstNodePtr->NextNodeOffset = SecondNodePtr->NextNodeOffset; |
| 125 | |
| 126 | /* Zero out the SecondNode header */ |
| 127 | memset(SecondNodePtr, 0, sizeof(BIOS_BUFFER_NODE)); |
| 128 | } |
| 129 | |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 130 | CBMEM_CREATION_HOOK(EmptyHeap); |
Marshall Dawson | 6c74706 | 2017-09-21 12:21:14 -0600 | [diff] [blame] | 131 | |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 132 | AGESA_STATUS agesa_AllocateBuffer(uint32_t Func, uintptr_t Data, |
| 133 | void *ConfigPtr) |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 134 | { |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 135 | /* |
| 136 | * Size variables explanation: |
| 137 | * FreedNodeSize - the size of the buffer node being examined, |
| 138 | * will be copied to BestFitNodeSize if the node |
| 139 | * is selected as a possible best fit. |
| 140 | * BestFitNodeSize - the size qf the buffer of the node currently |
| 141 | * considered the best fit. |
| 142 | * MinimumSize - the requested size + sizeof(BIOS_BUFFER_NODE). |
| 143 | * Its the minimum size for the buffer to be broken |
| 144 | * down into 2 nodes, once a node is selected as |
| 145 | * the best fit. |
| 146 | */ |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 147 | uint32_t AvailableHeapSize; |
| 148 | uint8_t *BiosHeapBaseAddr; |
| 149 | uint32_t CurrNodeOffset; |
| 150 | uint32_t PrevNodeOffset; |
| 151 | uint32_t FreedNodeOffset; |
| 152 | uint32_t FreedNodeSize; |
| 153 | uint32_t BestFitNodeOffset; |
| 154 | uint32_t BestFitNodeSize; |
| 155 | uint32_t BestFitPrevNodeOffset; |
| 156 | uint32_t NextFreeOffset; |
| 157 | uint32_t MinimumSize; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 158 | BIOS_BUFFER_NODE *CurrNodePtr; |
| 159 | BIOS_BUFFER_NODE *FreedNodePtr; |
| 160 | BIOS_BUFFER_NODE *BestFitNodePtr; |
| 161 | BIOS_BUFFER_NODE *BestFitPrevNodePtr; |
| 162 | BIOS_BUFFER_NODE *NextFreePtr; |
| 163 | BIOS_HEAP_MANAGER *BiosHeapBasePtr; |
| 164 | AGESA_BUFFER_PARAMS *AllocParams; |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 165 | AGESA_STATUS Status; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 166 | |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 167 | AllocParams = ((AGESA_BUFFER_PARAMS *)ConfigPtr); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 168 | AllocParams->BufferPointer = NULL; |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 169 | MinimumSize = AllocParams->BufferLength + sizeof(BIOS_BUFFER_NODE); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 170 | |
| 171 | AvailableHeapSize = BIOS_HEAP_SIZE - sizeof(BIOS_HEAP_MANAGER); |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 172 | BestFitNodeSize = AvailableHeapSize; /* init with largest possible */ |
Marshall Dawson | 21c5e15 | 2017-12-15 11:13:54 -0700 | [diff] [blame] | 173 | BiosHeapBaseAddr = agesa_heap_base(); |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 174 | BiosHeapBasePtr = (BIOS_HEAP_MANAGER *)BiosHeapBaseAddr; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 175 | |
| 176 | if (BiosHeapBasePtr->StartOfAllocatedNodes == 0) { |
| 177 | /* First allocation */ |
| 178 | CurrNodeOffset = sizeof(BIOS_HEAP_MANAGER); |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 179 | CurrNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr |
| 180 | + CurrNodeOffset); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 181 | CurrNodePtr->BufferHandle = AllocParams->BufferHandle; |
| 182 | CurrNodePtr->BufferSize = AllocParams->BufferLength; |
| 183 | CurrNodePtr->NextNodeOffset = 0; |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 184 | AllocParams->BufferPointer = (uint8_t *)CurrNodePtr |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 185 | + sizeof(BIOS_BUFFER_NODE); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 186 | |
| 187 | /* Update the remaining free space */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 188 | FreedNodeOffset = CurrNodeOffset + CurrNodePtr->BufferSize |
| 189 | + sizeof(BIOS_BUFFER_NODE); |
| 190 | FreedNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr |
| 191 | + FreedNodeOffset); |
| 192 | FreedNodePtr->BufferSize = AvailableHeapSize |
Marc Jones | e6033ce | 2018-01-15 18:47:24 -0700 | [diff] [blame] | 193 | - (FreedNodeOffset - CurrNodeOffset) |
| 194 | - sizeof(BIOS_BUFFER_NODE); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 195 | FreedNodePtr->NextNodeOffset = 0; |
| 196 | |
| 197 | /* Update the offsets for Allocated and Freed nodes */ |
| 198 | BiosHeapBasePtr->StartOfAllocatedNodes = CurrNodeOffset; |
| 199 | BiosHeapBasePtr->StartOfFreedNodes = FreedNodeOffset; |
| 200 | } else { |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 201 | /* |
| 202 | * Find out whether BufferHandle has been allocated on the heap. |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 203 | * If it has, return AGESA_BOUNDS_CHK. |
| 204 | */ |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 205 | Status = FindAllocatedNode(AllocParams->BufferHandle, |
| 206 | &CurrNodePtr); |
| 207 | if (Status == AGESA_SUCCESS) |
| 208 | return AGESA_BOUNDS_CHK; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 209 | |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 210 | /* |
| 211 | * If status ditn't returned AGESA_SUCCESS, CurrNodePtr here |
| 212 | * points to the end of the allocated nodes list. |
| 213 | */ |
| 214 | |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 215 | /* Find the node that best fits the requested buffer size */ |
| 216 | FreedNodeOffset = BiosHeapBasePtr->StartOfFreedNodes; |
| 217 | PrevNodeOffset = FreedNodeOffset; |
| 218 | BestFitNodeOffset = 0; |
| 219 | BestFitPrevNodeOffset = 0; |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 220 | while (FreedNodeOffset != 0) { |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 221 | FreedNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr |
| 222 | + FreedNodeOffset); |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 223 | FreedNodeSize = FreedNodePtr->BufferSize; |
| 224 | if (FreedNodeSize >= MinimumSize) { |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 225 | if (BestFitNodeOffset == 0) { |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 226 | /* |
| 227 | * First node that fits the requested |
| 228 | * buffer size |
| 229 | */ |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 230 | BestFitNodeOffset = FreedNodeOffset; |
| 231 | BestFitPrevNodeOffset = PrevNodeOffset; |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 232 | BestFitNodeSize = FreedNodeSize; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 233 | } else { |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 234 | /* |
| 235 | * Find out whether current node is a |
| 236 | * betterfit than the previous nodes |
| 237 | */ |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 238 | if (BestFitNodeSize > FreedNodeSize) { |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 239 | BestFitNodeOffset = |
| 240 | FreedNodeOffset; |
| 241 | BestFitPrevNodeOffset = |
| 242 | PrevNodeOffset; |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 243 | BestFitNodeSize = FreedNodeSize; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | PrevNodeOffset = FreedNodeOffset; |
| 248 | FreedNodeOffset = FreedNodePtr->NextNodeOffset; |
| 249 | } /* end of while loop */ |
| 250 | |
| 251 | if (BestFitNodeOffset == 0) { |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 252 | /* |
| 253 | * If we could not find a node that fits the requested |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 254 | * buffer size, return AGESA_BOUNDS_CHK. |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 255 | */ |
| 256 | return AGESA_BOUNDS_CHK; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 257 | } |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 258 | |
| 259 | BestFitNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr |
| 260 | + BestFitNodeOffset); |
Elyes Haouas | 68fc51f | 2022-07-16 09:48:27 +0200 | [diff] [blame] | 261 | BestFitPrevNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr + |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 262 | BestFitPrevNodeOffset); |
| 263 | |
| 264 | /* |
Marc Jones | a273753 | 2018-01-12 16:52:29 +0000 | [diff] [blame] | 265 | * If BestFitNode is larger than the requested buffer, |
| 266 | * fragment the node further |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 267 | */ |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 268 | if (BestFitNodePtr->BufferSize > MinimumSize) { |
| 269 | NextFreeOffset = BestFitNodeOffset + MinimumSize; |
Elyes Haouas | 68fc51f | 2022-07-16 09:48:27 +0200 | [diff] [blame] | 270 | NextFreePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr + |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 271 | NextFreeOffset); |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 272 | NextFreePtr->BufferSize = BestFitNodeSize - MinimumSize; |
| 273 | |
| 274 | /* Remove BestFitNode from list of Freed nodes */ |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 275 | NextFreePtr->NextNodeOffset = |
| 276 | BestFitNodePtr->NextNodeOffset; |
| 277 | } else { |
| 278 | /* |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 279 | * Otherwise, next free node is NextNodeOffset of |
| 280 | * BestFitNode. Remove it from list of Freed nodes. |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 281 | */ |
| 282 | NextFreeOffset = BestFitNodePtr->NextNodeOffset; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * If BestFitNode is the first buffer in the list, then |
| 287 | * update StartOfFreedNodes to reflect new free node. |
| 288 | */ |
| 289 | if (BestFitNodeOffset == BiosHeapBasePtr->StartOfFreedNodes) |
| 290 | BiosHeapBasePtr->StartOfFreedNodes = NextFreeOffset; |
| 291 | else |
| 292 | BestFitPrevNodePtr->NextNodeOffset = NextFreeOffset; |
| 293 | |
| 294 | /* Add BestFitNode to the list of Allocated nodes */ |
| 295 | CurrNodePtr->NextNodeOffset = BestFitNodeOffset; |
| 296 | BestFitNodePtr->BufferSize = AllocParams->BufferLength; |
| 297 | BestFitNodePtr->BufferHandle = AllocParams->BufferHandle; |
| 298 | BestFitNodePtr->NextNodeOffset = 0; |
| 299 | |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 300 | AllocParams->BufferPointer = (uint8_t *)BestFitNodePtr + |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 301 | sizeof(BIOS_BUFFER_NODE); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return AGESA_SUCCESS; |
| 305 | } |
| 306 | |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 307 | AGESA_STATUS agesa_DeallocateBuffer(uint32_t Func, uintptr_t Data, |
| 308 | void *ConfigPtr) |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 309 | { |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 310 | uint8_t *BiosHeapBaseAddr; |
| 311 | uint32_t AllocNodeOffset; |
| 312 | uint32_t PrevNodeOffset; |
| 313 | uint32_t NextNodeOffset; |
| 314 | uint32_t FreedNodeOffset; |
| 315 | uint32_t EndNodeOffset; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 316 | BIOS_BUFFER_NODE *AllocNodePtr; |
| 317 | BIOS_BUFFER_NODE *PrevNodePtr; |
| 318 | BIOS_BUFFER_NODE *FreedNodePtr; |
| 319 | BIOS_BUFFER_NODE *NextNodePtr; |
| 320 | BIOS_HEAP_MANAGER *BiosHeapBasePtr; |
| 321 | AGESA_BUFFER_PARAMS *AllocParams; |
| 322 | |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 323 | AllocParams = (AGESA_BUFFER_PARAMS *)ConfigPtr; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 324 | |
Marshall Dawson | 21c5e15 | 2017-12-15 11:13:54 -0700 | [diff] [blame] | 325 | BiosHeapBaseAddr = agesa_heap_base(); |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 326 | BiosHeapBasePtr = (BIOS_HEAP_MANAGER *)BiosHeapBaseAddr; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 327 | |
| 328 | /* Find target node to deallocate in list of allocated nodes. |
| 329 | * Return AGESA_BOUNDS_CHK if the BufferHandle is not found. |
| 330 | */ |
| 331 | AllocNodeOffset = BiosHeapBasePtr->StartOfAllocatedNodes; |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 332 | AllocNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr + AllocNodeOffset); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 333 | PrevNodeOffset = AllocNodeOffset; |
| 334 | |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 335 | while (AllocNodePtr->BufferHandle != AllocParams->BufferHandle) { |
| 336 | if (AllocNodePtr->NextNodeOffset == 0) |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 337 | return AGESA_BOUNDS_CHK; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 338 | PrevNodeOffset = AllocNodeOffset; |
| 339 | AllocNodeOffset = AllocNodePtr->NextNodeOffset; |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 340 | AllocNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr |
| 341 | + AllocNodeOffset); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Remove target node from list of allocated nodes */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 345 | PrevNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr + PrevNodeOffset); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 346 | PrevNodePtr->NextNodeOffset = AllocNodePtr->NextNodeOffset; |
| 347 | |
| 348 | /* Zero out the buffer, and clear the BufferHandle */ |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 349 | memset((uint8_t *)AllocNodePtr + sizeof(BIOS_BUFFER_NODE), 0, |
Aaron Durbin | e3f7d44 | 2017-11-03 11:44:10 -0600 | [diff] [blame] | 350 | AllocNodePtr->BufferSize); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 351 | AllocNodePtr->BufferHandle = 0; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 352 | |
| 353 | /* Add deallocated node in order to the list of freed nodes */ |
| 354 | FreedNodeOffset = BiosHeapBasePtr->StartOfFreedNodes; |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 355 | FreedNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr + FreedNodeOffset); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 356 | |
Marc Jones | e6033ce | 2018-01-15 18:47:24 -0700 | [diff] [blame] | 357 | EndNodeOffset = AllocNodeOffset + AllocNodePtr->BufferSize + |
| 358 | sizeof(BIOS_BUFFER_NODE); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 359 | |
| 360 | if (AllocNodeOffset < FreedNodeOffset) { |
| 361 | /* Add to the start of the freed list */ |
| 362 | if (EndNodeOffset == FreedNodeOffset) { |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 363 | /* If the freed node is adjacent to the first node in |
| 364 | * the list, concatenate both nodes |
| 365 | */ |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 366 | ConcatenateNodes(AllocNodePtr, FreedNodePtr); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 367 | } else { |
| 368 | /* Otherwise, add freed node to the start of the list |
| 369 | * Update NextNodeOffset and BufferSize to include the |
| 370 | * size of BIOS_BUFFER_NODE. |
| 371 | */ |
| 372 | AllocNodePtr->NextNodeOffset = FreedNodeOffset; |
| 373 | } |
| 374 | /* Update StartOfFreedNodes to the new first node */ |
| 375 | BiosHeapBasePtr->StartOfFreedNodes = AllocNodeOffset; |
| 376 | } else { |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 377 | /* Traverse list of freed nodes to find where the deallocated |
| 378 | * node should be placed. |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 379 | */ |
| 380 | NextNodeOffset = FreedNodeOffset; |
| 381 | NextNodePtr = FreedNodePtr; |
| 382 | while (AllocNodeOffset > NextNodeOffset) { |
| 383 | PrevNodeOffset = NextNodeOffset; |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 384 | if (NextNodePtr->NextNodeOffset == 0) |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 385 | break; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 386 | NextNodeOffset = NextNodePtr->NextNodeOffset; |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 387 | NextNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr |
| 388 | + NextNodeOffset); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* If deallocated node is adjacent to the next node, |
| 392 | * concatenate both nodes. |
| 393 | */ |
| 394 | if (NextNodeOffset == EndNodeOffset) { |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 395 | NextNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr |
| 396 | + NextNodeOffset); |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 397 | ConcatenateNodes(AllocNodePtr, NextNodePtr); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 398 | } else { |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 399 | /*AllocNodePtr->NextNodeOffset = |
Elyes HAOUAS | 05498a2 | 2018-05-28 16:26:43 +0200 | [diff] [blame] | 400 | * FreedNodePtr->NextNodeOffset; */ |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 401 | AllocNodePtr->NextNodeOffset = NextNodeOffset; |
| 402 | } |
Richard Spiegel | fc51127 | 2017-12-11 16:23:58 -0700 | [diff] [blame] | 403 | /* |
| 404 | * If deallocated node is adjacent to the previous node, |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 405 | * concatenate both nodes. |
| 406 | */ |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 407 | PrevNodePtr = (BIOS_BUFFER_NODE *)(BiosHeapBaseAddr |
| 408 | + PrevNodeOffset); |
Marc Jones | e6033ce | 2018-01-15 18:47:24 -0700 | [diff] [blame] | 409 | EndNodeOffset = PrevNodeOffset + PrevNodePtr->BufferSize + |
| 410 | sizeof(BIOS_BUFFER_NODE); |
| 411 | |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 412 | if (AllocNodeOffset == EndNodeOffset) |
| 413 | ConcatenateNodes(PrevNodePtr, AllocNodePtr); |
| 414 | else |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 415 | PrevNodePtr->NextNodeOffset = AllocNodeOffset; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 416 | } |
| 417 | return AGESA_SUCCESS; |
| 418 | } |
| 419 | |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 420 | AGESA_STATUS agesa_LocateBuffer(uint32_t Func, uintptr_t Data, void *ConfigPtr) |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 421 | { |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 422 | BIOS_BUFFER_NODE *AllocNodePtr; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 423 | AGESA_BUFFER_PARAMS *AllocParams; |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 424 | AGESA_STATUS Status; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 425 | |
Marshall Dawson | f3dc71e | 2017-06-14 16:22:07 -0600 | [diff] [blame] | 426 | AllocParams = (AGESA_BUFFER_PARAMS *)ConfigPtr; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 427 | |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 428 | Status = FindAllocatedNode(AllocParams->BufferHandle, &AllocNodePtr); |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 429 | |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 430 | if (Status == AGESA_SUCCESS) { |
Richard Spiegel | 271b8a5 | 2018-11-06 16:32:28 -0700 | [diff] [blame] | 431 | AllocParams->BufferPointer = (uint8_t *)((uint8_t *)AllocNodePtr |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 432 | + sizeof(BIOS_BUFFER_NODE)); |
| 433 | AllocParams->BufferLength = AllocNodePtr->BufferSize; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 434 | } |
| 435 | |
Richard Spiegel | 51895d1 | 2018-04-17 14:25:26 -0700 | [diff] [blame] | 436 | return Status; |
Marc Jones | 21cde8b | 2017-05-07 16:47:36 -0600 | [diff] [blame] | 437 | } |