]>
Commit | Line | Data |
---|---|---|
0a6653bc | 1 | /** @file\r |
2 | *\r | |
7e91decd | 3 | * Copyright (c) 2011-2013, ARM Limited. All rights reserved.\r |
0a6653bc | 4 | *\r |
7e91decd | 5 | * This program and the accompanying materials\r |
6 | * are licensed and made available under the terms and conditions of the BSD License\r | |
7 | * which accompanies this distribution. The full text of the license may be found at\r | |
8 | * http://opensource.org/licenses/bsd-license.php\r | |
9 | *\r | |
10 | * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
0a6653bc | 12 | *\r |
13 | **/\r | |
14 | \r | |
bc87b507 | 15 | #include <Library/ArmSmcLib.h>\r |
0a6653bc | 16 | #include <Library/PcdLib.h>\r |
17 | #include <libfdt.h>\r | |
18 | \r | |
bc87b507 | 19 | #include <IndustryStandard/ArmSmc.h>\r |
20 | \r | |
0a6653bc | 21 | #include "BdsInternal.h"\r |
22 | #include "BdsLinuxLoader.h"\r | |
23 | \r | |
24 | #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))\r | |
25 | #define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))\r | |
26 | #define GET_CELL(p) (p += 4, *((const UINT32 *)(p-4)))\r | |
27 | \r | |
b2ce4a39 | 28 | STATIC\r |
8255c569 | 29 | UINTN\r |
30 | cpu_to_fdtn (UINTN x) {\r | |
31 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
32 | return cpu_to_fdt32 (x);\r | |
33 | } else {\r | |
34 | return cpu_to_fdt64 (x);\r | |
35 | }\r | |
36 | }\r | |
37 | \r | |
cc9f2157 | 38 | typedef struct {\r |
39 | UINTN Base;\r | |
40 | UINTN Size;\r | |
41 | } FdtRegion;\r | |
42 | \r | |
43 | \r | |
0a6653bc | 44 | STATIC\r |
45 | UINTN\r | |
46 | IsPrintableString (\r | |
47 | IN CONST VOID* data,\r | |
48 | IN UINTN len\r | |
49 | )\r | |
50 | {\r | |
51 | CONST CHAR8 *s = data;\r | |
52 | CONST CHAR8 *ss;\r | |
53 | \r | |
54 | // Zero length is not\r | |
55 | if (len == 0) {\r | |
56 | return 0;\r | |
57 | }\r | |
58 | \r | |
59 | // Must terminate with zero\r | |
60 | if (s[len - 1] != '\0') {\r | |
61 | return 0;\r | |
62 | }\r | |
63 | \r | |
64 | ss = s;\r | |
65 | while (*s/* && isprint(*s)*/) {\r | |
66 | s++;\r | |
67 | }\r | |
68 | \r | |
69 | // Not zero, or not done yet\r | |
70 | if (*s != '\0' || (s + 1 - ss) < len) {\r | |
71 | return 0;\r | |
72 | }\r | |
73 | \r | |
74 | return 1;\r | |
75 | }\r | |
76 | \r | |
77 | STATIC\r | |
78 | VOID\r | |
79 | PrintData (\r | |
80 | IN CONST CHAR8* data,\r | |
81 | IN UINTN len\r | |
82 | )\r | |
83 | {\r | |
84 | UINTN i;\r | |
85 | CONST CHAR8 *p = data;\r | |
86 | \r | |
87 | // No data, don't print\r | |
88 | if (len == 0)\r | |
89 | return;\r | |
90 | \r | |
91 | if (IsPrintableString (data, len)) {\r | |
92 | Print(L" = \"%a\"", (const char *)data);\r | |
93 | } else if ((len % 4) == 0) {\r | |
94 | Print(L" = <");\r | |
95 | for (i = 0; i < len; i += 4) {\r | |
96 | Print(L"0x%08x%a", fdt32_to_cpu(GET_CELL(p)),i < (len - 4) ? " " : "");\r | |
97 | }\r | |
98 | Print(L">");\r | |
99 | } else {\r | |
100 | Print(L" = [");\r | |
101 | for (i = 0; i < len; i++)\r | |
102 | Print(L"%02x%a", *p++, i < len - 1 ? " " : "");\r | |
103 | Print(L"]");\r | |
104 | }\r | |
105 | }\r | |
106 | \r | |
107 | VOID\r | |
108 | DebugDumpFdt (\r | |
109 | IN VOID* FdtBlob\r | |
110 | )\r | |
111 | {\r | |
112 | struct fdt_header *bph;\r | |
113 | UINT32 off_dt;\r | |
114 | UINT32 off_str;\r | |
115 | CONST CHAR8* p_struct;\r | |
116 | CONST CHAR8* p_strings;\r | |
117 | CONST CHAR8* p;\r | |
118 | CONST CHAR8* s;\r | |
119 | CONST CHAR8* t;\r | |
120 | UINT32 tag;\r | |
121 | UINTN sz;\r | |
122 | UINTN depth;\r | |
123 | UINTN shift;\r | |
124 | UINT32 version;\r | |
125 | \r | |
a01e042d | 126 | {\r |
127 | // Can 'memreserve' be printed by below code?\r | |
128 | INTN num = fdt_num_mem_rsv(FdtBlob);\r | |
129 | INTN i, err;\r | |
130 | UINT64 addr = 0,size = 0;\r | |
131 | \r | |
132 | for (i = 0; i < num; i++) {\r | |
133 | err = fdt_get_mem_rsv(FdtBlob, i, &addr, &size);\r | |
134 | if (err) {\r | |
135 | DEBUG((EFI_D_ERROR, "Error (%d) : Cannot get memreserve section (%d)\n", err, i));\r | |
136 | }\r | |
137 | else {\r | |
138 | Print(L"/memreserve/ \t0x%lx \t0x%lx;\n",addr,size);\r | |
139 | }\r | |
140 | }\r | |
141 | }\r | |
142 | \r | |
0a6653bc | 143 | depth = 0;\r |
144 | shift = 4;\r | |
145 | \r | |
146 | bph = FdtBlob;\r | |
147 | off_dt = fdt32_to_cpu(bph->off_dt_struct);\r | |
148 | off_str = fdt32_to_cpu(bph->off_dt_strings);\r | |
149 | p_struct = (CONST CHAR8*)FdtBlob + off_dt;\r | |
150 | p_strings = (CONST CHAR8*)FdtBlob + off_str;\r | |
151 | version = fdt32_to_cpu(bph->version);\r | |
152 | \r | |
153 | p = p_struct;\r | |
154 | while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {\r | |
155 | if (tag == FDT_BEGIN_NODE) {\r | |
156 | s = p;\r | |
157 | p = PALIGN(p + AsciiStrLen (s) + 1, 4);\r | |
158 | \r | |
159 | if (*s == '\0')\r | |
160 | s = "/";\r | |
161 | \r | |
162 | Print(L"%*s%a {\n", depth * shift, L" ", s);\r | |
163 | \r | |
164 | depth++;\r | |
165 | continue;\r | |
166 | }\r | |
167 | \r | |
168 | if (tag == FDT_END_NODE) {\r | |
169 | depth--;\r | |
170 | \r | |
171 | Print(L"%*s};\n", depth * shift, L" ");\r | |
172 | continue;\r | |
173 | }\r | |
174 | \r | |
175 | if (tag == FDT_NOP) {\r | |
176 | Print(L"%*s// [NOP]\n", depth * shift, L" ");\r | |
177 | continue;\r | |
178 | }\r | |
179 | \r | |
180 | if (tag != FDT_PROP) {\r | |
181 | Print(L"%*s ** Unknown tag 0x%08x\n", depth * shift, L" ", tag);\r | |
182 | break;\r | |
183 | }\r | |
184 | sz = fdt32_to_cpu(GET_CELL(p));\r | |
185 | s = p_strings + fdt32_to_cpu(GET_CELL(p));\r | |
186 | if (version < 16 && sz >= 8)\r | |
187 | p = PALIGN(p, 8);\r | |
188 | t = p;\r | |
189 | \r | |
190 | p = PALIGN(p + sz, 4);\r | |
191 | \r | |
192 | Print(L"%*s%a", depth * shift, L" ", s);\r | |
193 | PrintData(t, sz);\r | |
194 | Print(L";\n");\r | |
195 | }\r | |
196 | }\r | |
197 | \r | |
a01e042d | 198 | STATIC\r |
199 | BOOLEAN\r | |
200 | IsLinuxReservedRegion (\r | |
201 | IN EFI_MEMORY_TYPE MemoryType\r | |
202 | )\r | |
203 | {\r | |
204 | switch(MemoryType) {\r | |
205 | case EfiRuntimeServicesCode:\r | |
206 | case EfiRuntimeServicesData:\r | |
207 | case EfiUnusableMemory:\r | |
208 | case EfiACPIReclaimMemory:\r | |
209 | case EfiACPIMemoryNVS:\r | |
b83a92b3 | 210 | case EfiReservedMemoryType:\r |
a01e042d | 211 | return TRUE;\r |
212 | default:\r | |
213 | return FALSE;\r | |
214 | }\r | |
215 | }\r | |
216 | \r | |
217 | \r | |
cc9f2157 | 218 | STATIC\r |
219 | BOOLEAN\r | |
220 | IsPsciSmcSupported (\r | |
221 | VOID\r | |
222 | )\r | |
223 | {\r | |
224 | BOOLEAN PsciSmcSupported;\r | |
225 | UINTN Rx;\r | |
226 | \r | |
227 | PsciSmcSupported = FALSE;\r | |
228 | \r | |
229 | // Check the SMC response to the Presence SMC\r | |
230 | Rx = ARM_SMC_ID_PRESENCE;\r | |
231 | ArmCallSmc (&Rx);\r | |
232 | if (Rx == 1) {\r | |
233 | // Check the SMC UID\r | |
234 | Rx = ARM_SMC_ID_UID;\r | |
235 | ArmCallSmc (&Rx);\r | |
236 | if (Rx == ARM_TRUSTZONE_UID_4LETTERID) {\r | |
237 | Rx = ARM_SMC_ID_UID + 1;\r | |
238 | ArmCallSmc (&Rx);\r | |
239 | if (Rx == ARM_TRUSTZONE_ARM_UID) {\r | |
240 | PsciSmcSupported = TRUE;\r | |
241 | }\r | |
242 | }\r | |
243 | }\r | |
244 | \r | |
245 | return PsciSmcSupported;\r | |
246 | }\r | |
247 | \r | |
248 | \r | |
249 | /**\r | |
250 | ** Relocate the FDT blob to a more appropriate location for the Linux kernel.\r | |
251 | ** This function will allocate memory for the relocated FDT blob.\r | |
252 | **\r | |
253 | ** @retval EFI_SUCCESS on success.\r | |
254 | ** @retval EFI_OUT_OF_RESOURCES or EFI_INVALID_PARAMETER on failure.\r | |
255 | */\r | |
256 | STATIC\r | |
257 | EFI_STATUS\r | |
258 | RelocateFdt (\r | |
259 | EFI_PHYSICAL_ADDRESS OriginalFdt,\r | |
260 | UINTN OriginalFdtSize,\r | |
261 | EFI_PHYSICAL_ADDRESS *RelocatedFdt,\r | |
262 | UINTN *RelocatedFdtSize,\r | |
263 | EFI_PHYSICAL_ADDRESS *RelocatedFdtAlloc\r | |
264 | )\r | |
265 | {\r | |
266 | EFI_STATUS Status;\r | |
267 | INTN Error;\r | |
ff58c914 | 268 | UINT64 FdtAlignment;\r |
cc9f2157 | 269 | \r |
270 | *RelocatedFdtSize = OriginalFdtSize + FDT_ADDITIONAL_ENTRIES_SIZE;\r | |
271 | \r | |
272 | // If FDT load address needs to be aligned, allocate more space.\r | |
273 | FdtAlignment = PcdGet32 (PcdArmLinuxFdtAlignment);\r | |
274 | if (FdtAlignment != 0) {\r | |
275 | *RelocatedFdtSize += FdtAlignment;\r | |
276 | }\r | |
277 | \r | |
278 | // Try below a watermark address.\r | |
279 | Status = EFI_NOT_FOUND;\r | |
280 | if (PcdGet32 (PcdArmLinuxFdtMaxOffset) != 0) {\r | |
281 | *RelocatedFdt = LINUX_FDT_MAX_OFFSET;\r | |
282 | Status = gBS->AllocatePages (AllocateMaxAddress, EfiBootServicesData,\r | |
283 | EFI_SIZE_TO_PAGES (*RelocatedFdtSize), RelocatedFdt);\r | |
284 | if (EFI_ERROR (Status)) {\r | |
285 | DEBUG ((EFI_D_WARN, "Warning: Failed to load FDT below address 0x%lX (%r). Will try again at a random address anywhere.\n", *RelocatedFdt, Status));\r | |
286 | }\r | |
287 | }\r | |
288 | \r | |
289 | // Try anywhere there is available space.\r | |
290 | if (EFI_ERROR (Status)) {\r | |
291 | Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,\r | |
292 | EFI_SIZE_TO_PAGES (*RelocatedFdtSize), RelocatedFdt);\r | |
293 | if (EFI_ERROR (Status)) {\r | |
294 | ASSERT_EFI_ERROR (Status);\r | |
295 | return EFI_OUT_OF_RESOURCES;\r | |
296 | } else {\r | |
297 | DEBUG ((EFI_D_WARN, "WARNING: Loaded FDT at random address 0x%lX.\nWARNING: There is a risk of accidental overwriting by other code/data.\n", *RelocatedFdt));\r | |
298 | }\r | |
299 | }\r | |
300 | \r | |
301 | *RelocatedFdtAlloc = *RelocatedFdt;\r | |
302 | if (FdtAlignment != 0) {\r | |
303 | *RelocatedFdt = ALIGN (*RelocatedFdt, FdtAlignment);\r | |
304 | }\r | |
305 | \r | |
306 | // Load the Original FDT tree into the new region\r | |
307 | Error = fdt_open_into ((VOID*)(UINTN) OriginalFdt,\r | |
308 | (VOID*)(UINTN)(*RelocatedFdt), *RelocatedFdtSize);\r | |
309 | if (Error) {\r | |
310 | DEBUG ((EFI_D_ERROR, "fdt_open_into(): %a\n", fdt_strerror (Error)));\r | |
311 | gBS->FreePages (*RelocatedFdtAlloc, EFI_SIZE_TO_PAGES (*RelocatedFdtSize));\r | |
312 | return EFI_INVALID_PARAMETER;\r | |
313 | }\r | |
314 | \r | |
315 | DEBUG_CODE_BEGIN();\r | |
316 | //DebugDumpFdt (fdt);\r | |
317 | DEBUG_CODE_END();\r | |
318 | \r | |
319 | return EFI_SUCCESS;\r | |
320 | }\r | |
321 | \r | |
0a6653bc | 322 | \r |
323 | EFI_STATUS\r | |
324 | PrepareFdt (\r | |
325 | IN CONST CHAR8* CommandLineArguments,\r | |
326 | IN EFI_PHYSICAL_ADDRESS InitrdImage,\r | |
327 | IN UINTN InitrdImageSize,\r | |
328 | IN OUT EFI_PHYSICAL_ADDRESS *FdtBlobBase,\r | |
c63626b7 | 329 | IN OUT UINTN *FdtBlobSize\r |
0a6653bc | 330 | )\r |
331 | {\r | |
332 | EFI_STATUS Status;\r | |
333 | EFI_PHYSICAL_ADDRESS NewFdtBlobBase;\r | |
387653a4 | 334 | EFI_PHYSICAL_ADDRESS NewFdtBlobAllocation;\r |
0a6653bc | 335 | UINTN NewFdtBlobSize;\r |
336 | VOID* fdt;\r | |
337 | INTN err;\r | |
338 | INTN node;\r | |
339 | INTN cpu_node;\r | |
25402f5d | 340 | INT32 lenp;\r |
0a6653bc | 341 | CONST VOID* BootArg;\r |
bc87b507 | 342 | CONST VOID* Method;\r |
0a6653bc | 343 | EFI_PHYSICAL_ADDRESS InitrdImageStart;\r |
344 | EFI_PHYSICAL_ADDRESS InitrdImageEnd;\r | |
345 | FdtRegion Region;\r | |
346 | UINTN Index;\r | |
347 | CHAR8 Name[10];\r | |
348 | LIST_ENTRY ResourceList;\r | |
349 | BDS_SYSTEM_MEMORY_RESOURCE *Resource;\r | |
350 | ARM_PROCESSOR_TABLE *ArmProcessorTable;\r | |
351 | ARM_CORE_INFO *ArmCoreInfoTable;\r | |
352 | UINT32 MpId;\r | |
353 | UINT32 ClusterId;\r | |
354 | UINT32 CoreId;\r | |
355 | UINT64 CpuReleaseAddr;\r | |
a01e042d | 356 | UINTN MemoryMapSize;\r |
357 | EFI_MEMORY_DESCRIPTOR *MemoryMap;\r | |
a58266e5 | 358 | EFI_MEMORY_DESCRIPTOR *MemoryMapPtr;\r |
a01e042d | 359 | UINTN MapKey;\r |
360 | UINTN DescriptorSize;\r | |
361 | UINT32 DescriptorVersion;\r | |
362 | UINTN Pages;\r | |
bc87b507 | 363 | BOOLEAN PsciSmcSupported;\r |
7e91decd | 364 | UINTN OriginalFdtSize;\r |
3809e6e0 | 365 | BOOLEAN CpusNodeExist;\r |
e359565e | 366 | UINTN CoreMpId;\r |
367 | UINTN Smc;\r | |
bc87b507 | 368 | \r |
cc9f2157 | 369 | NewFdtBlobAllocation = 0;\r |
0a6653bc | 370 | \r |
7e91decd | 371 | //\r |
372 | // Sanity checks on the original FDT blob.\r | |
373 | //\r | |
0a6653bc | 374 | err = fdt_check_header ((VOID*)(UINTN)(*FdtBlobBase));\r |
375 | if (err != 0) {\r | |
376 | Print (L"ERROR: Device Tree header not valid (err:%d)\n", err);\r | |
377 | return EFI_INVALID_PARAMETER;\r | |
378 | }\r | |
379 | \r | |
7e91decd | 380 | // The original FDT blob might have been loaded partially.\r |
381 | // Check that it is not the case.\r | |
382 | OriginalFdtSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));\r | |
383 | if (OriginalFdtSize > *FdtBlobSize) {\r | |
384 | Print (L"ERROR: Incomplete FDT. Only %d/%d bytes have been loaded.\n",\r | |
385 | *FdtBlobSize, OriginalFdtSize);\r | |
386 | return EFI_INVALID_PARAMETER;\r | |
387 | }\r | |
388 | \r | |
0a6653bc | 389 | //\r |
cc9f2157 | 390 | // Relocate the FDT to its final location.\r |
0a6653bc | 391 | //\r |
cc9f2157 | 392 | Status = RelocateFdt (*FdtBlobBase, OriginalFdtSize,\r |
393 | &NewFdtBlobBase, &NewFdtBlobSize, &NewFdtBlobAllocation);\r | |
394 | if (EFI_ERROR (Status)) {\r | |
395 | goto FAIL_RELOCATE_FDT;\r | |
387653a4 | 396 | }\r |
397 | \r | |
cc9f2157 | 398 | //\r |
399 | // Ensure the Power State Coordination Interface (PSCI) SMCs are there if supported\r | |
400 | //\r | |
401 | PsciSmcSupported = FALSE;\r | |
402 | if (FeaturePcdGet (PcdArmPsciSupport) == TRUE) {\r | |
403 | PsciSmcSupported = IsPsciSmcSupported();\r | |
404 | if (PsciSmcSupported == FALSE) {\r | |
405 | DEBUG ((EFI_D_ERROR, "Warning: The Power State Coordination Interface (PSCI) is not supported by your platform Trusted Firmware.\n"));\r | |
0a6653bc | 406 | }\r |
407 | }\r | |
408 | \r | |
0a6653bc | 409 | fdt = (VOID*)(UINTN)NewFdtBlobBase;\r |
0a6653bc | 410 | \r |
cc9f2157 | 411 | node = fdt_subnode_offset (fdt, 0, "chosen");\r |
0a6653bc | 412 | if (node < 0) {\r |
413 | // The 'chosen' node does not exist, create it\r | |
414 | node = fdt_add_subnode(fdt, 0, "chosen");\r | |
415 | if (node < 0) {\r | |
416 | DEBUG((EFI_D_ERROR,"Error on finding 'chosen' node\n"));\r | |
417 | Status = EFI_INVALID_PARAMETER;\r | |
cc9f2157 | 418 | goto FAIL_COMPLETE_FDT;\r |
0a6653bc | 419 | }\r |
420 | }\r | |
421 | \r | |
422 | DEBUG_CODE_BEGIN();\r | |
423 | BootArg = fdt_getprop(fdt, node, "bootargs", &lenp);\r | |
424 | if (BootArg != NULL) {\r | |
425 | DEBUG((EFI_D_ERROR,"BootArg: %a\n",BootArg));\r | |
426 | }\r | |
427 | DEBUG_CODE_END();\r | |
428 | \r | |
a01e042d | 429 | //\r |
0a6653bc | 430 | // Set Linux CmdLine\r |
a01e042d | 431 | //\r |
0a6653bc | 432 | if ((CommandLineArguments != NULL) && (AsciiStrLen (CommandLineArguments) > 0)) {\r |
433 | err = fdt_setprop(fdt, node, "bootargs", CommandLineArguments, AsciiStrSize(CommandLineArguments));\r | |
434 | if (err) {\r | |
435 | DEBUG((EFI_D_ERROR,"Fail to set new 'bootarg' (err:%d)\n",err));\r | |
436 | }\r | |
437 | }\r | |
438 | \r | |
a01e042d | 439 | //\r |
0a6653bc | 440 | // Set Linux Initrd\r |
a01e042d | 441 | //\r |
0a6653bc | 442 | if (InitrdImageSize != 0) {\r |
443 | InitrdImageStart = cpu_to_fdt64 (InitrdImage);\r | |
444 | err = fdt_setprop(fdt, node, "linux,initrd-start", &InitrdImageStart, sizeof(EFI_PHYSICAL_ADDRESS));\r | |
445 | if (err) {\r | |
446 | DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err));\r | |
447 | }\r | |
448 | InitrdImageEnd = cpu_to_fdt64 (InitrdImage + InitrdImageSize);\r | |
449 | err = fdt_setprop(fdt, node, "linux,initrd-end", &InitrdImageEnd, sizeof(EFI_PHYSICAL_ADDRESS));\r | |
450 | if (err) {\r | |
451 | DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err));\r | |
452 | }\r | |
453 | }\r | |
454 | \r | |
a01e042d | 455 | //\r |
0a6653bc | 456 | // Set Physical memory setup if does not exist\r |
a01e042d | 457 | //\r |
0a6653bc | 458 | node = fdt_subnode_offset(fdt, 0, "memory");\r |
459 | if (node < 0) {\r | |
460 | // The 'memory' node does not exist, create it\r | |
461 | node = fdt_add_subnode(fdt, 0, "memory");\r | |
462 | if (node >= 0) {\r | |
463 | fdt_setprop_string(fdt, node, "name", "memory");\r | |
464 | fdt_setprop_string(fdt, node, "device_type", "memory");\r | |
7e91decd | 465 | \r |
0a6653bc | 466 | GetSystemMemoryResources (&ResourceList);\r |
467 | Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)ResourceList.ForwardLink;\r | |
7e91decd | 468 | \r |
8255c569 | 469 | Region.Base = cpu_to_fdtn ((UINTN)Resource->PhysicalStart);\r |
470 | Region.Size = cpu_to_fdtn ((UINTN)Resource->ResourceLength);\r | |
0a6653bc | 471 | \r |
472 | err = fdt_setprop(fdt, node, "reg", &Region, sizeof(Region));\r | |
473 | if (err) {\r | |
474 | DEBUG((EFI_D_ERROR,"Fail to set new 'memory region' (err:%d)\n",err));\r | |
475 | }\r | |
476 | }\r | |
477 | }\r | |
478 | \r | |
a01e042d | 479 | //\r |
480 | // Add the memory regions reserved by the UEFI Firmware\r | |
481 | //\r | |
482 | \r | |
483 | // Retrieve the UEFI Memory Map\r | |
484 | MemoryMap = NULL;\r | |
485 | MemoryMapSize = 0;\r | |
486 | Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r | |
487 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
a58266e5 OM |
488 | // The UEFI specification advises to allocate more memory for the MemoryMap buffer between successive\r |
489 | // calls to GetMemoryMap(), since allocation of the new buffer may potentially increase memory map size.\r | |
a01e042d | 490 | Pages = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;\r |
491 | MemoryMap = AllocatePages (Pages);\r | |
a58266e5 OM |
492 | if (MemoryMap == NULL) {\r |
493 | Status = EFI_OUT_OF_RESOURCES;\r | |
494 | goto FAIL_COMPLETE_FDT;\r | |
495 | }\r | |
a01e042d | 496 | Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r |
497 | }\r | |
498 | \r | |
499 | // Go through the list and add the reserved region to the Device Tree\r | |
500 | if (!EFI_ERROR(Status)) {\r | |
a58266e5 OM |
501 | MemoryMapPtr = MemoryMap;\r |
502 | for (Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++) {\r | |
503 | if (IsLinuxReservedRegion ((EFI_MEMORY_TYPE)MemoryMapPtr->Type)) {\r | |
b83a92b3 | 504 | DEBUG((DEBUG_VERBOSE, "Reserved region of type %d [0x%lX, 0x%lX]\n",\r |
a58266e5 OM |
505 | MemoryMapPtr->Type,\r |
506 | (UINTN)MemoryMapPtr->PhysicalStart,\r | |
507 | (UINTN)(MemoryMapPtr->PhysicalStart + MemoryMapPtr->NumberOfPages * EFI_PAGE_SIZE)));\r | |
508 | err = fdt_add_mem_rsv(fdt, MemoryMapPtr->PhysicalStart, MemoryMapPtr->NumberOfPages * EFI_PAGE_SIZE);\r | |
a01e042d | 509 | if (err != 0) {\r |
510 | Print(L"Warning: Fail to add 'memreserve' (err:%d)\n", err);\r | |
511 | }\r | |
512 | }\r | |
a58266e5 | 513 | MemoryMapPtr = (EFI_MEMORY_DESCRIPTOR*)((UINTN)MemoryMapPtr + DescriptorSize);\r |
a01e042d | 514 | }\r |
515 | }\r | |
516 | \r | |
517 | //\r | |
e359565e | 518 | // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms.\r |
519 | //\r | |
520 | // For 'cpus' and 'cpu' device tree nodes bindings, refer to this file\r | |
521 | // in the kernel documentation:\r | |
522 | // Documentation/devicetree/bindings/arm/cpus.txt\r | |
a01e042d | 523 | //\r |
0a6653bc | 524 | for (Index=0; Index < gST->NumberOfTableEntries; Index++) {\r |
525 | // Check for correct GUID type\r | |
526 | if (CompareGuid (&gArmMpCoreInfoGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r | |
527 | MpId = ArmReadMpidr ();\r | |
528 | ClusterId = GET_CLUSTER_ID(MpId);\r | |
529 | CoreId = GET_CORE_ID(MpId);\r | |
530 | \r | |
531 | node = fdt_subnode_offset(fdt, 0, "cpus");\r | |
532 | if (node < 0) {\r | |
533 | // Create the /cpus node\r | |
534 | node = fdt_add_subnode(fdt, 0, "cpus");\r | |
535 | fdt_setprop_string(fdt, node, "name", "cpus");\r | |
e359565e | 536 | fdt_setprop_cell (fdt, node, "#address-cells", sizeof (UINTN) / 4);\r |
0a6653bc | 537 | fdt_setprop_cell(fdt, node, "#size-cells", 0);\r |
3809e6e0 | 538 | CpusNodeExist = FALSE;\r |
539 | } else {\r | |
540 | CpusNodeExist = TRUE;\r | |
0a6653bc | 541 | }\r |
542 | \r | |
543 | // Get pointer to ARM processor table\r | |
544 | ArmProcessorTable = (ARM_PROCESSOR_TABLE *)gST->ConfigurationTable[Index].VendorTable;\r | |
545 | ArmCoreInfoTable = ArmProcessorTable->ArmCpus;\r | |
546 | \r | |
547 | for (Index = 0; Index < ArmProcessorTable->NumberOfEntries; Index++) {\r | |
86d75840 OM |
548 | CoreMpId = (UINTN) GET_MPID (ArmCoreInfoTable[Index].ClusterId,\r |
549 | ArmCoreInfoTable[Index].CoreId);\r | |
550 | AsciiSPrint (Name, 10, "cpu@%x", CoreMpId);\r | |
3809e6e0 | 551 | \r |
e359565e | 552 | // If the 'cpus' node did not exist then create all the 'cpu' nodes.\r |
553 | // In case 'cpus' node is provided in the original FDT then we do not add\r | |
554 | // any 'cpu' node.\r | |
3809e6e0 | 555 | if (!CpusNodeExist) {\r |
e359565e | 556 | cpu_node = fdt_add_subnode (fdt, node, Name);\r |
557 | if (cpu_node < 0) {\r | |
558 | DEBUG ((EFI_D_ERROR, "Error on creating '%s' node\n", Name));\r | |
559 | Status = EFI_INVALID_PARAMETER;\r | |
560 | goto FAIL_COMPLETE_FDT;\r | |
561 | }\r | |
562 | \r | |
563 | fdt_setprop_string (fdt, cpu_node, "device_type", "cpu");\r | |
86d75840 | 564 | \r |
e359565e | 565 | CoreMpId = cpu_to_fdtn (CoreMpId);\r |
566 | fdt_setprop (fdt, cpu_node, "reg", &CoreMpId, sizeof (CoreMpId));\r | |
567 | if (PsciSmcSupported) {\r | |
568 | fdt_setprop_string (fdt, cpu_node, "enable-method", "psci");\r | |
569 | }\r | |
3809e6e0 | 570 | } else {\r |
571 | cpu_node = fdt_subnode_offset(fdt, node, Name);\r | |
0a6653bc | 572 | }\r |
573 | \r | |
bc87b507 | 574 | // If Power State Coordination Interface (PSCI) is not supported then it is expected the secondary\r |
39f58c9b | 575 | // cores are spinning waiting for the Operating System to release them\r |
3809e6e0 | 576 | if ((PsciSmcSupported == FALSE) && (cpu_node >= 0)) {\r |
39f58c9b | 577 | // We as the bootloader are responsible for either creating or updating\r |
578 | // these entries. Do not trust the entries in the DT. We only know about\r | |
579 | // 'spin-table' type. Do not try to update other types if defined.\r | |
bc87b507 | 580 | Method = fdt_getprop(fdt, cpu_node, "enable-method", &lenp);\r |
39f58c9b | 581 | if ( (Method == NULL) || (!AsciiStrCmp((CHAR8 *)Method, "spin-table")) ) {\r |
bc87b507 | 582 | fdt_setprop_string(fdt, cpu_node, "enable-method", "spin-table");\r |
583 | CpuReleaseAddr = cpu_to_fdt64(ArmCoreInfoTable[Index].MailboxSetAddress);\r | |
584 | fdt_setprop(fdt, cpu_node, "cpu-release-addr", &CpuReleaseAddr, sizeof(CpuReleaseAddr));\r | |
585 | \r | |
586 | // If it is not the primary core than the cpu should be disabled\r | |
587 | if (((ArmCoreInfoTable[Index].ClusterId != ClusterId) || (ArmCoreInfoTable[Index].CoreId != CoreId))) {\r | |
588 | fdt_setprop_string(fdt, cpu_node, "status", "disabled");\r | |
589 | }\r | |
39f58c9b | 590 | } else {\r |
591 | Print(L"Warning: Unsupported enable-method type for CPU[%d] : %a\n", Index, (CHAR8 *)Method);\r | |
bc87b507 | 592 | }\r |
0a6653bc | 593 | }\r |
594 | }\r | |
595 | break;\r | |
596 | }\r | |
597 | }\r | |
598 | \r | |
bc87b507 | 599 | // If the Power State Coordination Interface is supported then we signal it in the Device Tree\r |
600 | if (PsciSmcSupported == TRUE) {\r | |
601 | // Before to create it we check if the node is not already defined in the Device Tree\r | |
602 | node = fdt_subnode_offset(fdt, 0, "psci");\r | |
603 | if (node < 0) {\r | |
604 | // The 'psci' node does not exist, create it\r | |
605 | node = fdt_add_subnode(fdt, 0, "psci");\r | |
606 | if (node < 0) {\r | |
607 | DEBUG((EFI_D_ERROR,"Error on creating 'psci' node\n"));\r | |
608 | Status = EFI_INVALID_PARAMETER;\r | |
cc9f2157 | 609 | goto FAIL_COMPLETE_FDT;\r |
bc87b507 | 610 | } else {\r |
e359565e | 611 | fdt_setprop_string (fdt, node, "compatible", "arm,psci");\r |
612 | fdt_setprop_string (fdt, node, "method", "smc");\r | |
613 | \r | |
614 | Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_SUSPEND);\r | |
615 | fdt_setprop (fdt, node, "cpu_suspend", &Smc, sizeof (Smc));\r | |
616 | \r | |
617 | Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_OFF);\r | |
618 | fdt_setprop (fdt, node, "cpu_off", &Smc, sizeof (Smc));\r | |
619 | \r | |
620 | Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_ON);\r | |
621 | fdt_setprop (fdt, node, "cpu_on", &Smc, sizeof (Smc));\r | |
622 | \r | |
623 | Smc = cpu_to_fdtn (ARM_SMC_ARM_MIGRATE);\r | |
624 | fdt_setprop (fdt, node, "migrate", &Smc, sizeof (Smc));\r | |
bc87b507 | 625 | }\r |
626 | }\r | |
627 | }\r | |
628 | \r | |
0a6653bc | 629 | DEBUG_CODE_BEGIN();\r |
630 | //DebugDumpFdt (fdt);\r | |
631 | DEBUG_CODE_END();\r | |
632 | \r | |
a90e3279 | 633 | // If we succeeded to generate the new Device Tree then free the old Device Tree\r |
634 | gBS->FreePages (*FdtBlobBase, EFI_SIZE_TO_PAGES (*FdtBlobSize));\r | |
635 | \r | |
0a6653bc | 636 | *FdtBlobBase = NewFdtBlobBase;\r |
637 | *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(NewFdtBlobBase));\r | |
638 | return EFI_SUCCESS;\r | |
639 | \r | |
cc9f2157 | 640 | FAIL_COMPLETE_FDT:\r |
387653a4 | 641 | gBS->FreePages (NewFdtBlobAllocation, EFI_SIZE_TO_PAGES (NewFdtBlobSize));\r |
a90e3279 | 642 | \r |
cc9f2157 | 643 | FAIL_RELOCATE_FDT:\r |
a90e3279 | 644 | *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));\r |
cc9f2157 | 645 | // Return success even if we failed to update the FDT blob.\r |
646 | // The original one is still valid.\r | |
0a6653bc | 647 | return EFI_SUCCESS;\r |
648 | }\r |