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