]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/BdsLib/BdsLinuxFdt.c
ArmPkg/BdsLib: Update FDT CPU node format
[mirror_edk2.git] / ArmPkg / Library / BdsLib / BdsLinuxFdt.c
CommitLineData
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
8255c569 28STATIC inline\r
29UINTN\r
30cpu_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 38typedef struct {\r
39 UINTN Base;\r
40 UINTN Size;\r
41} FdtRegion;\r
42\r
43\r
0a6653bc 44STATIC\r
45UINTN\r
46IsPrintableString (\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
77STATIC\r
78VOID\r
79PrintData (\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
107VOID\r
108DebugDumpFdt (\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 198STATIC\r
199BOOLEAN\r
200IsLinuxReservedRegion (\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
210 return TRUE;\r
211 default:\r
212 return FALSE;\r
213 }\r
214}\r
215\r
216\r
cc9f2157 217STATIC\r
218BOOLEAN\r
219IsPsciSmcSupported (\r
220 VOID\r
221 )\r
222{\r
223 BOOLEAN PsciSmcSupported;\r
224 UINTN Rx;\r
225\r
226 PsciSmcSupported = FALSE;\r
227\r
228 // Check the SMC response to the Presence SMC\r
229 Rx = ARM_SMC_ID_PRESENCE;\r
230 ArmCallSmc (&Rx);\r
231 if (Rx == 1) {\r
232 // Check the SMC UID\r
233 Rx = ARM_SMC_ID_UID;\r
234 ArmCallSmc (&Rx);\r
235 if (Rx == ARM_TRUSTZONE_UID_4LETTERID) {\r
236 Rx = ARM_SMC_ID_UID + 1;\r
237 ArmCallSmc (&Rx);\r
238 if (Rx == ARM_TRUSTZONE_ARM_UID) {\r
239 PsciSmcSupported = TRUE;\r
240 }\r
241 }\r
242 }\r
243\r
244 return PsciSmcSupported;\r
245}\r
246\r
247\r
248/**\r
249** Relocate the FDT blob to a more appropriate location for the Linux kernel.\r
250** This function will allocate memory for the relocated FDT blob.\r
251**\r
252** @retval EFI_SUCCESS on success.\r
253** @retval EFI_OUT_OF_RESOURCES or EFI_INVALID_PARAMETER on failure.\r
254*/\r
255STATIC\r
256EFI_STATUS\r
257RelocateFdt (\r
258 EFI_PHYSICAL_ADDRESS OriginalFdt,\r
259 UINTN OriginalFdtSize,\r
260 EFI_PHYSICAL_ADDRESS *RelocatedFdt,\r
261 UINTN *RelocatedFdtSize,\r
262 EFI_PHYSICAL_ADDRESS *RelocatedFdtAlloc\r
263 )\r
264{\r
265 EFI_STATUS Status;\r
266 INTN Error;\r
267 UINT32 FdtAlignment;\r
268\r
269 *RelocatedFdtSize = OriginalFdtSize + FDT_ADDITIONAL_ENTRIES_SIZE;\r
270\r
271 // If FDT load address needs to be aligned, allocate more space.\r
272 FdtAlignment = PcdGet32 (PcdArmLinuxFdtAlignment);\r
273 if (FdtAlignment != 0) {\r
274 *RelocatedFdtSize += FdtAlignment;\r
275 }\r
276\r
277 // Try below a watermark address.\r
278 Status = EFI_NOT_FOUND;\r
279 if (PcdGet32 (PcdArmLinuxFdtMaxOffset) != 0) {\r
280 *RelocatedFdt = LINUX_FDT_MAX_OFFSET;\r
281 Status = gBS->AllocatePages (AllocateMaxAddress, EfiBootServicesData,\r
282 EFI_SIZE_TO_PAGES (*RelocatedFdtSize), RelocatedFdt);\r
283 if (EFI_ERROR (Status)) {\r
284 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
285 }\r
286 }\r
287\r
288 // Try anywhere there is available space.\r
289 if (EFI_ERROR (Status)) {\r
290 Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,\r
291 EFI_SIZE_TO_PAGES (*RelocatedFdtSize), RelocatedFdt);\r
292 if (EFI_ERROR (Status)) {\r
293 ASSERT_EFI_ERROR (Status);\r
294 return EFI_OUT_OF_RESOURCES;\r
295 } else {\r
296 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
297 }\r
298 }\r
299\r
300 *RelocatedFdtAlloc = *RelocatedFdt;\r
301 if (FdtAlignment != 0) {\r
302 *RelocatedFdt = ALIGN (*RelocatedFdt, FdtAlignment);\r
303 }\r
304\r
305 // Load the Original FDT tree into the new region\r
306 Error = fdt_open_into ((VOID*)(UINTN) OriginalFdt,\r
307 (VOID*)(UINTN)(*RelocatedFdt), *RelocatedFdtSize);\r
308 if (Error) {\r
309 DEBUG ((EFI_D_ERROR, "fdt_open_into(): %a\n", fdt_strerror (Error)));\r
310 gBS->FreePages (*RelocatedFdtAlloc, EFI_SIZE_TO_PAGES (*RelocatedFdtSize));\r
311 return EFI_INVALID_PARAMETER;\r
312 }\r
313\r
314 DEBUG_CODE_BEGIN();\r
315 //DebugDumpFdt (fdt);\r
316 DEBUG_CODE_END();\r
317\r
318 return EFI_SUCCESS;\r
319}\r
320\r
0a6653bc 321\r
322EFI_STATUS\r
323PrepareFdt (\r
324 IN CONST CHAR8* CommandLineArguments,\r
325 IN EFI_PHYSICAL_ADDRESS InitrdImage,\r
326 IN UINTN InitrdImageSize,\r
327 IN OUT EFI_PHYSICAL_ADDRESS *FdtBlobBase,\r
c63626b7 328 IN OUT UINTN *FdtBlobSize\r
0a6653bc 329 )\r
330{\r
331 EFI_STATUS Status;\r
332 EFI_PHYSICAL_ADDRESS NewFdtBlobBase;\r
387653a4 333 EFI_PHYSICAL_ADDRESS NewFdtBlobAllocation;\r
0a6653bc 334 UINTN NewFdtBlobSize;\r
335 VOID* fdt;\r
336 INTN err;\r
337 INTN node;\r
338 INTN cpu_node;\r
339 INTN lenp;\r
340 CONST VOID* BootArg;\r
bc87b507 341 CONST VOID* Method;\r
0a6653bc 342 EFI_PHYSICAL_ADDRESS InitrdImageStart;\r
343 EFI_PHYSICAL_ADDRESS InitrdImageEnd;\r
344 FdtRegion Region;\r
345 UINTN Index;\r
346 CHAR8 Name[10];\r
347 LIST_ENTRY ResourceList;\r
348 BDS_SYSTEM_MEMORY_RESOURCE *Resource;\r
349 ARM_PROCESSOR_TABLE *ArmProcessorTable;\r
350 ARM_CORE_INFO *ArmCoreInfoTable;\r
351 UINT32 MpId;\r
352 UINT32 ClusterId;\r
353 UINT32 CoreId;\r
354 UINT64 CpuReleaseAddr;\r
a01e042d 355 UINTN MemoryMapSize;\r
356 EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
357 UINTN MapKey;\r
358 UINTN DescriptorSize;\r
359 UINT32 DescriptorVersion;\r
360 UINTN Pages;\r
bc87b507 361 BOOLEAN PsciSmcSupported;\r
7e91decd 362 UINTN OriginalFdtSize;\r
3809e6e0 363 BOOLEAN CpusNodeExist;\r
e359565e 364 UINTN CoreMpId;\r
365 UINTN Smc;\r
bc87b507 366\r
cc9f2157 367 NewFdtBlobAllocation = 0;\r
0a6653bc 368\r
7e91decd 369 //\r
370 // Sanity checks on the original FDT blob.\r
371 //\r
0a6653bc 372 err = fdt_check_header ((VOID*)(UINTN)(*FdtBlobBase));\r
373 if (err != 0) {\r
374 Print (L"ERROR: Device Tree header not valid (err:%d)\n", err);\r
375 return EFI_INVALID_PARAMETER;\r
376 }\r
377\r
7e91decd 378 // The original FDT blob might have been loaded partially.\r
379 // Check that it is not the case.\r
380 OriginalFdtSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));\r
381 if (OriginalFdtSize > *FdtBlobSize) {\r
382 Print (L"ERROR: Incomplete FDT. Only %d/%d bytes have been loaded.\n",\r
383 *FdtBlobSize, OriginalFdtSize);\r
384 return EFI_INVALID_PARAMETER;\r
385 }\r
386\r
0a6653bc 387 //\r
cc9f2157 388 // Relocate the FDT to its final location.\r
0a6653bc 389 //\r
cc9f2157 390 Status = RelocateFdt (*FdtBlobBase, OriginalFdtSize,\r
391 &NewFdtBlobBase, &NewFdtBlobSize, &NewFdtBlobAllocation);\r
392 if (EFI_ERROR (Status)) {\r
393 goto FAIL_RELOCATE_FDT;\r
387653a4 394 }\r
395\r
cc9f2157 396 //\r
397 // Ensure the Power State Coordination Interface (PSCI) SMCs are there if supported\r
398 //\r
399 PsciSmcSupported = FALSE;\r
400 if (FeaturePcdGet (PcdArmPsciSupport) == TRUE) {\r
401 PsciSmcSupported = IsPsciSmcSupported();\r
402 if (PsciSmcSupported == FALSE) {\r
403 DEBUG ((EFI_D_ERROR, "Warning: The Power State Coordination Interface (PSCI) is not supported by your platform Trusted Firmware.\n"));\r
0a6653bc 404 }\r
405 }\r
406\r
0a6653bc 407 fdt = (VOID*)(UINTN)NewFdtBlobBase;\r
0a6653bc 408\r
cc9f2157 409 node = fdt_subnode_offset (fdt, 0, "chosen");\r
0a6653bc 410 if (node < 0) {\r
411 // The 'chosen' node does not exist, create it\r
412 node = fdt_add_subnode(fdt, 0, "chosen");\r
413 if (node < 0) {\r
414 DEBUG((EFI_D_ERROR,"Error on finding 'chosen' node\n"));\r
415 Status = EFI_INVALID_PARAMETER;\r
cc9f2157 416 goto FAIL_COMPLETE_FDT;\r
0a6653bc 417 }\r
418 }\r
419\r
420 DEBUG_CODE_BEGIN();\r
421 BootArg = fdt_getprop(fdt, node, "bootargs", &lenp);\r
422 if (BootArg != NULL) {\r
423 DEBUG((EFI_D_ERROR,"BootArg: %a\n",BootArg));\r
424 }\r
425 DEBUG_CODE_END();\r
426\r
a01e042d 427 //\r
0a6653bc 428 // Set Linux CmdLine\r
a01e042d 429 //\r
0a6653bc 430 if ((CommandLineArguments != NULL) && (AsciiStrLen (CommandLineArguments) > 0)) {\r
431 err = fdt_setprop(fdt, node, "bootargs", CommandLineArguments, AsciiStrSize(CommandLineArguments));\r
432 if (err) {\r
433 DEBUG((EFI_D_ERROR,"Fail to set new 'bootarg' (err:%d)\n",err));\r
434 }\r
435 }\r
436\r
a01e042d 437 //\r
0a6653bc 438 // Set Linux Initrd\r
a01e042d 439 //\r
0a6653bc 440 if (InitrdImageSize != 0) {\r
441 InitrdImageStart = cpu_to_fdt64 (InitrdImage);\r
442 err = fdt_setprop(fdt, node, "linux,initrd-start", &InitrdImageStart, sizeof(EFI_PHYSICAL_ADDRESS));\r
443 if (err) {\r
444 DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err));\r
445 }\r
446 InitrdImageEnd = cpu_to_fdt64 (InitrdImage + InitrdImageSize);\r
447 err = fdt_setprop(fdt, node, "linux,initrd-end", &InitrdImageEnd, sizeof(EFI_PHYSICAL_ADDRESS));\r
448 if (err) {\r
449 DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err));\r
450 }\r
451 }\r
452\r
a01e042d 453 //\r
0a6653bc 454 // Set Physical memory setup if does not exist\r
a01e042d 455 //\r
0a6653bc 456 node = fdt_subnode_offset(fdt, 0, "memory");\r
457 if (node < 0) {\r
458 // The 'memory' node does not exist, create it\r
459 node = fdt_add_subnode(fdt, 0, "memory");\r
460 if (node >= 0) {\r
461 fdt_setprop_string(fdt, node, "name", "memory");\r
462 fdt_setprop_string(fdt, node, "device_type", "memory");\r
7e91decd 463\r
0a6653bc 464 GetSystemMemoryResources (&ResourceList);\r
465 Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)ResourceList.ForwardLink;\r
7e91decd 466\r
8255c569 467 Region.Base = cpu_to_fdtn ((UINTN)Resource->PhysicalStart);\r
468 Region.Size = cpu_to_fdtn ((UINTN)Resource->ResourceLength);\r
0a6653bc 469\r
470 err = fdt_setprop(fdt, node, "reg", &Region, sizeof(Region));\r
471 if (err) {\r
472 DEBUG((EFI_D_ERROR,"Fail to set new 'memory region' (err:%d)\n",err));\r
473 }\r
474 }\r
475 }\r
476\r
a01e042d 477 //\r
478 // Add the memory regions reserved by the UEFI Firmware\r
479 //\r
480\r
481 // Retrieve the UEFI Memory Map\r
482 MemoryMap = NULL;\r
483 MemoryMapSize = 0;\r
484 Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r
485 if (Status == EFI_BUFFER_TOO_SMALL) {\r
486 Pages = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;\r
487 MemoryMap = AllocatePages (Pages);\r
488 Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r
489 }\r
490\r
491 // Go through the list and add the reserved region to the Device Tree\r
492 if (!EFI_ERROR(Status)) {\r
493 for (Index = 0; Index < (MemoryMapSize / sizeof(EFI_MEMORY_DESCRIPTOR)); Index++) {\r
494 if (IsLinuxReservedRegion ((EFI_MEMORY_TYPE)MemoryMap[Index].Type)) {\r
495 DEBUG((DEBUG_VERBOSE, "Reserved region of type %d [0x%X, 0x%X]\n",\r
496 MemoryMap[Index].Type,\r
497 (UINTN)MemoryMap[Index].PhysicalStart,\r
498 (UINTN)(MemoryMap[Index].PhysicalStart + MemoryMap[Index].NumberOfPages * EFI_PAGE_SIZE)));\r
499 err = fdt_add_mem_rsv(fdt, MemoryMap[Index].PhysicalStart, MemoryMap[Index].NumberOfPages * EFI_PAGE_SIZE);\r
500 if (err != 0) {\r
501 Print(L"Warning: Fail to add 'memreserve' (err:%d)\n", err);\r
502 }\r
503 }\r
504 }\r
505 }\r
506\r
507 //\r
e359565e 508 // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms.\r
509 //\r
510 // For 'cpus' and 'cpu' device tree nodes bindings, refer to this file\r
511 // in the kernel documentation:\r
512 // Documentation/devicetree/bindings/arm/cpus.txt\r
a01e042d 513 //\r
0a6653bc 514 for (Index=0; Index < gST->NumberOfTableEntries; Index++) {\r
515 // Check for correct GUID type\r
516 if (CompareGuid (&gArmMpCoreInfoGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
517 MpId = ArmReadMpidr ();\r
518 ClusterId = GET_CLUSTER_ID(MpId);\r
519 CoreId = GET_CORE_ID(MpId);\r
520\r
521 node = fdt_subnode_offset(fdt, 0, "cpus");\r
522 if (node < 0) {\r
523 // Create the /cpus node\r
524 node = fdt_add_subnode(fdt, 0, "cpus");\r
525 fdt_setprop_string(fdt, node, "name", "cpus");\r
e359565e 526 fdt_setprop_cell (fdt, node, "#address-cells", sizeof (UINTN) / 4);\r
0a6653bc 527 fdt_setprop_cell(fdt, node, "#size-cells", 0);\r
3809e6e0 528 CpusNodeExist = FALSE;\r
529 } else {\r
530 CpusNodeExist = TRUE;\r
0a6653bc 531 }\r
532\r
533 // Get pointer to ARM processor table\r
534 ArmProcessorTable = (ARM_PROCESSOR_TABLE *)gST->ConfigurationTable[Index].VendorTable;\r
535 ArmCoreInfoTable = ArmProcessorTable->ArmCpus;\r
536\r
537 for (Index = 0; Index < ArmProcessorTable->NumberOfEntries; Index++) {\r
86d75840
OM
538 CoreMpId = (UINTN) GET_MPID (ArmCoreInfoTable[Index].ClusterId,\r
539 ArmCoreInfoTable[Index].CoreId);\r
540 AsciiSPrint (Name, 10, "cpu@%x", CoreMpId);\r
3809e6e0 541\r
e359565e 542 // If the 'cpus' node did not exist then create all the 'cpu' nodes.\r
543 // In case 'cpus' node is provided in the original FDT then we do not add\r
544 // any 'cpu' node.\r
3809e6e0 545 if (!CpusNodeExist) {\r
e359565e 546 cpu_node = fdt_add_subnode (fdt, node, Name);\r
547 if (cpu_node < 0) {\r
548 DEBUG ((EFI_D_ERROR, "Error on creating '%s' node\n", Name));\r
549 Status = EFI_INVALID_PARAMETER;\r
550 goto FAIL_COMPLETE_FDT;\r
551 }\r
552\r
553 fdt_setprop_string (fdt, cpu_node, "device_type", "cpu");\r
86d75840 554\r
e359565e 555 CoreMpId = cpu_to_fdtn (CoreMpId);\r
556 fdt_setprop (fdt, cpu_node, "reg", &CoreMpId, sizeof (CoreMpId));\r
557 if (PsciSmcSupported) {\r
558 fdt_setprop_string (fdt, cpu_node, "enable-method", "psci");\r
559 }\r
3809e6e0 560 } else {\r
561 cpu_node = fdt_subnode_offset(fdt, node, Name);\r
0a6653bc 562 }\r
563\r
bc87b507 564 // If Power State Coordination Interface (PSCI) is not supported then it is expected the secondary\r
39f58c9b 565 // cores are spinning waiting for the Operating System to release them\r
3809e6e0 566 if ((PsciSmcSupported == FALSE) && (cpu_node >= 0)) {\r
39f58c9b 567 // We as the bootloader are responsible for either creating or updating\r
568 // these entries. Do not trust the entries in the DT. We only know about\r
569 // 'spin-table' type. Do not try to update other types if defined.\r
bc87b507 570 Method = fdt_getprop(fdt, cpu_node, "enable-method", &lenp);\r
39f58c9b 571 if ( (Method == NULL) || (!AsciiStrCmp((CHAR8 *)Method, "spin-table")) ) {\r
bc87b507 572 fdt_setprop_string(fdt, cpu_node, "enable-method", "spin-table");\r
573 CpuReleaseAddr = cpu_to_fdt64(ArmCoreInfoTable[Index].MailboxSetAddress);\r
574 fdt_setprop(fdt, cpu_node, "cpu-release-addr", &CpuReleaseAddr, sizeof(CpuReleaseAddr));\r
575\r
576 // If it is not the primary core than the cpu should be disabled\r
577 if (((ArmCoreInfoTable[Index].ClusterId != ClusterId) || (ArmCoreInfoTable[Index].CoreId != CoreId))) {\r
578 fdt_setprop_string(fdt, cpu_node, "status", "disabled");\r
579 }\r
39f58c9b 580 } else {\r
581 Print(L"Warning: Unsupported enable-method type for CPU[%d] : %a\n", Index, (CHAR8 *)Method);\r
bc87b507 582 }\r
0a6653bc 583 }\r
584 }\r
585 break;\r
586 }\r
587 }\r
588\r
bc87b507 589 // If the Power State Coordination Interface is supported then we signal it in the Device Tree\r
590 if (PsciSmcSupported == TRUE) {\r
591 // Before to create it we check if the node is not already defined in the Device Tree\r
592 node = fdt_subnode_offset(fdt, 0, "psci");\r
593 if (node < 0) {\r
594 // The 'psci' node does not exist, create it\r
595 node = fdt_add_subnode(fdt, 0, "psci");\r
596 if (node < 0) {\r
597 DEBUG((EFI_D_ERROR,"Error on creating 'psci' node\n"));\r
598 Status = EFI_INVALID_PARAMETER;\r
cc9f2157 599 goto FAIL_COMPLETE_FDT;\r
bc87b507 600 } else {\r
e359565e 601 fdt_setprop_string (fdt, node, "compatible", "arm,psci");\r
602 fdt_setprop_string (fdt, node, "method", "smc");\r
603\r
604 Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_SUSPEND);\r
605 fdt_setprop (fdt, node, "cpu_suspend", &Smc, sizeof (Smc));\r
606\r
607 Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_OFF);\r
608 fdt_setprop (fdt, node, "cpu_off", &Smc, sizeof (Smc));\r
609\r
610 Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_ON);\r
611 fdt_setprop (fdt, node, "cpu_on", &Smc, sizeof (Smc));\r
612\r
613 Smc = cpu_to_fdtn (ARM_SMC_ARM_MIGRATE);\r
614 fdt_setprop (fdt, node, "migrate", &Smc, sizeof (Smc));\r
bc87b507 615 }\r
616 }\r
617 }\r
618\r
0a6653bc 619 DEBUG_CODE_BEGIN();\r
620 //DebugDumpFdt (fdt);\r
621 DEBUG_CODE_END();\r
622\r
a90e3279 623 // If we succeeded to generate the new Device Tree then free the old Device Tree\r
624 gBS->FreePages (*FdtBlobBase, EFI_SIZE_TO_PAGES (*FdtBlobSize));\r
625\r
0a6653bc 626 *FdtBlobBase = NewFdtBlobBase;\r
627 *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(NewFdtBlobBase));\r
628 return EFI_SUCCESS;\r
629\r
cc9f2157 630FAIL_COMPLETE_FDT:\r
387653a4 631 gBS->FreePages (NewFdtBlobAllocation, EFI_SIZE_TO_PAGES (NewFdtBlobSize));\r
a90e3279 632\r
cc9f2157 633FAIL_RELOCATE_FDT:\r
a90e3279 634 *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));\r
cc9f2157 635 // Return success even if we failed to update the FDT blob.\r
636 // The original one is still valid.\r
0a6653bc 637 return EFI_SUCCESS;\r
638}\r