3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
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
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.
15 #include <Library/ArmSmcLib.h>
16 #include <Library/PcdLib.h>
19 #include <IndustryStandard/ArmSmc.h>
21 #include "BdsInternal.h"
22 #include "BdsLinuxLoader.h"
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)))
30 cpu_to_fdtn (UINTN x
) {
31 if (sizeof (UINTN
) == sizeof (UINT32
)) {
32 return cpu_to_fdt32 (x
);
34 return cpu_to_fdt64 (x
);
51 CONST CHAR8
*s
= data
;
59 // Must terminate with zero
60 if (s
[len
- 1] != '\0') {
65 while (*s
/* && isprint(*s)*/) {
69 // Not zero, or not done yet
70 if (*s
!= '\0' || (s
+ 1 - ss
) < len
) {
85 CONST CHAR8
*p
= data
;
87 // No data, don't print
91 if (IsPrintableString (data
, len
)) {
92 Print(L
" = \"%a\"", (const char *)data
);
93 } else if ((len
% 4) == 0) {
95 for (i
= 0; i
< len
; i
+= 4) {
96 Print(L
"0x%08x%a", fdt32_to_cpu(GET_CELL(p
)),i
< (len
- 4) ? " " : "");
101 for (i
= 0; i
< len
; i
++)
102 Print(L
"%02x%a", *p
++, i
< len
- 1 ? " " : "");
112 struct fdt_header
*bph
;
115 CONST CHAR8
* p_struct
;
116 CONST CHAR8
* p_strings
;
127 // Can 'memreserve' be printed by below code?
128 INTN num
= fdt_num_mem_rsv(FdtBlob
);
130 UINT64 addr
= 0,size
= 0;
132 for (i
= 0; i
< num
; i
++) {
133 err
= fdt_get_mem_rsv(FdtBlob
, i
, &addr
, &size
);
135 DEBUG((EFI_D_ERROR
, "Error (%d) : Cannot get memreserve section (%d)\n", err
, i
));
138 Print(L
"/memreserve/ \t0x%lx \t0x%lx;\n",addr
,size
);
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
);
154 while ((tag
= fdt32_to_cpu(GET_CELL(p
))) != FDT_END
) {
155 if (tag
== FDT_BEGIN_NODE
) {
157 p
= PALIGN(p
+ AsciiStrLen (s
) + 1, 4);
162 Print(L
"%*s%a {\n", depth
* shift
, L
" ", s
);
168 if (tag
== FDT_END_NODE
) {
171 Print(L
"%*s};\n", depth
* shift
, L
" ");
175 if (tag
== FDT_NOP
) {
176 Print(L
"%*s// [NOP]\n", depth
* shift
, L
" ");
180 if (tag
!= FDT_PROP
) {
181 Print(L
"%*s ** Unknown tag 0x%08x\n", depth
* shift
, L
" ", tag
);
184 sz
= fdt32_to_cpu(GET_CELL(p
));
185 s
= p_strings
+ fdt32_to_cpu(GET_CELL(p
));
186 if (version
< 16 && sz
>= 8)
190 p
= PALIGN(p
+ sz
, 4);
192 Print(L
"%*s%a", depth
* shift
, L
" ", s
);
200 IsLinuxReservedRegion (
201 IN EFI_MEMORY_TYPE MemoryType
205 case EfiRuntimeServicesCode
:
206 case EfiRuntimeServicesData
:
207 case EfiUnusableMemory
:
208 case EfiACPIReclaimMemory
:
209 case EfiACPIMemoryNVS
:
223 BOOLEAN PsciSmcSupported
;
226 PsciSmcSupported
= FALSE
;
228 // Check the SMC response to the Presence SMC
229 Rx
= ARM_SMC_ID_PRESENCE
;
235 if (Rx
== ARM_TRUSTZONE_UID_4LETTERID
) {
236 Rx
= ARM_SMC_ID_UID
+ 1;
238 if (Rx
== ARM_TRUSTZONE_ARM_UID
) {
239 PsciSmcSupported
= TRUE
;
244 return PsciSmcSupported
;
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.
252 ** @retval EFI_SUCCESS on success.
253 ** @retval EFI_OUT_OF_RESOURCES or EFI_INVALID_PARAMETER on failure.
258 EFI_PHYSICAL_ADDRESS OriginalFdt
,
259 UINTN OriginalFdtSize
,
260 EFI_PHYSICAL_ADDRESS
*RelocatedFdt
,
261 UINTN
*RelocatedFdtSize
,
262 EFI_PHYSICAL_ADDRESS
*RelocatedFdtAlloc
269 *RelocatedFdtSize
= OriginalFdtSize
+ FDT_ADDITIONAL_ENTRIES_SIZE
;
271 // If FDT load address needs to be aligned, allocate more space.
272 FdtAlignment
= PcdGet32 (PcdArmLinuxFdtAlignment
);
273 if (FdtAlignment
!= 0) {
274 *RelocatedFdtSize
+= FdtAlignment
;
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
));
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
;
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
));
300 *RelocatedFdtAlloc
= *RelocatedFdt
;
301 if (FdtAlignment
!= 0) {
302 *RelocatedFdt
= ALIGN (*RelocatedFdt
, FdtAlignment
);
305 // Load the Original FDT tree into the new region
306 Error
= fdt_open_into ((VOID
*)(UINTN
) OriginalFdt
,
307 (VOID
*)(UINTN
)(*RelocatedFdt
), *RelocatedFdtSize
);
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
;
315 //DebugDumpFdt (fdt);
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
332 EFI_PHYSICAL_ADDRESS NewFdtBlobBase
;
333 EFI_PHYSICAL_ADDRESS NewFdtBlobAllocation
;
334 UINTN NewFdtBlobSize
;
342 EFI_PHYSICAL_ADDRESS InitrdImageStart
;
343 EFI_PHYSICAL_ADDRESS InitrdImageEnd
;
347 LIST_ENTRY ResourceList
;
348 BDS_SYSTEM_MEMORY_RESOURCE
*Resource
;
349 ARM_PROCESSOR_TABLE
*ArmProcessorTable
;
350 ARM_CORE_INFO
*ArmCoreInfoTable
;
354 UINT64 CpuReleaseAddr
;
356 EFI_MEMORY_DESCRIPTOR
*MemoryMap
;
358 UINTN DescriptorSize
;
359 UINT32 DescriptorVersion
;
361 BOOLEAN PsciSmcSupported
;
362 UINTN OriginalFdtSize
;
363 BOOLEAN CpusNodeExist
;
367 NewFdtBlobAllocation
= 0;
370 // Sanity checks on the original FDT blob.
372 err
= fdt_check_header ((VOID
*)(UINTN
)(*FdtBlobBase
));
374 Print (L
"ERROR: Device Tree header not valid (err:%d)\n", err
);
375 return EFI_INVALID_PARAMETER
;
378 // The original FDT blob might have been loaded partially.
379 // Check that it is not the case.
380 OriginalFdtSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(*FdtBlobBase
));
381 if (OriginalFdtSize
> *FdtBlobSize
) {
382 Print (L
"ERROR: Incomplete FDT. Only %d/%d bytes have been loaded.\n",
383 *FdtBlobSize
, OriginalFdtSize
);
384 return EFI_INVALID_PARAMETER
;
388 // Relocate the FDT to its final location.
390 Status
= RelocateFdt (*FdtBlobBase
, OriginalFdtSize
,
391 &NewFdtBlobBase
, &NewFdtBlobSize
, &NewFdtBlobAllocation
);
392 if (EFI_ERROR (Status
)) {
393 goto FAIL_RELOCATE_FDT
;
397 // Ensure the Power State Coordination Interface (PSCI) SMCs are there if supported
399 PsciSmcSupported
= FALSE
;
400 if (FeaturePcdGet (PcdArmPsciSupport
) == TRUE
) {
401 PsciSmcSupported
= IsPsciSmcSupported();
402 if (PsciSmcSupported
== FALSE
) {
403 DEBUG ((EFI_D_ERROR
, "Warning: The Power State Coordination Interface (PSCI) is not supported by your platform Trusted Firmware.\n"));
407 fdt
= (VOID
*)(UINTN
)NewFdtBlobBase
;
409 node
= fdt_subnode_offset (fdt
, 0, "chosen");
411 // The 'chosen' node does not exist, create it
412 node
= fdt_add_subnode(fdt
, 0, "chosen");
414 DEBUG((EFI_D_ERROR
,"Error on finding 'chosen' node\n"));
415 Status
= EFI_INVALID_PARAMETER
;
416 goto FAIL_COMPLETE_FDT
;
421 BootArg
= fdt_getprop(fdt
, node
, "bootargs", &lenp
);
422 if (BootArg
!= NULL
) {
423 DEBUG((EFI_D_ERROR
,"BootArg: %a\n",BootArg
));
430 if ((CommandLineArguments
!= NULL
) && (AsciiStrLen (CommandLineArguments
) > 0)) {
431 err
= fdt_setprop(fdt
, node
, "bootargs", CommandLineArguments
, AsciiStrSize(CommandLineArguments
));
433 DEBUG((EFI_D_ERROR
,"Fail to set new 'bootarg' (err:%d)\n",err
));
440 if (InitrdImageSize
!= 0) {
441 InitrdImageStart
= cpu_to_fdt64 (InitrdImage
);
442 err
= fdt_setprop(fdt
, node
, "linux,initrd-start", &InitrdImageStart
, sizeof(EFI_PHYSICAL_ADDRESS
));
444 DEBUG((EFI_D_ERROR
,"Fail to set new 'linux,initrd-start' (err:%d)\n",err
));
446 InitrdImageEnd
= cpu_to_fdt64 (InitrdImage
+ InitrdImageSize
);
447 err
= fdt_setprop(fdt
, node
, "linux,initrd-end", &InitrdImageEnd
, sizeof(EFI_PHYSICAL_ADDRESS
));
449 DEBUG((EFI_D_ERROR
,"Fail to set new 'linux,initrd-start' (err:%d)\n",err
));
454 // Set Physical memory setup if does not exist
456 node
= fdt_subnode_offset(fdt
, 0, "memory");
458 // The 'memory' node does not exist, create it
459 node
= fdt_add_subnode(fdt
, 0, "memory");
461 fdt_setprop_string(fdt
, node
, "name", "memory");
462 fdt_setprop_string(fdt
, node
, "device_type", "memory");
464 GetSystemMemoryResources (&ResourceList
);
465 Resource
= (BDS_SYSTEM_MEMORY_RESOURCE
*)ResourceList
.ForwardLink
;
467 Region
.Base
= cpu_to_fdtn ((UINTN
)Resource
->PhysicalStart
);
468 Region
.Size
= cpu_to_fdtn ((UINTN
)Resource
->ResourceLength
);
470 err
= fdt_setprop(fdt
, node
, "reg", &Region
, sizeof(Region
));
472 DEBUG((EFI_D_ERROR
,"Fail to set new 'memory region' (err:%d)\n",err
));
478 // Add the memory regions reserved by the UEFI Firmware
481 // Retrieve the UEFI Memory Map
484 Status
= gBS
->GetMemoryMap (&MemoryMapSize
, MemoryMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
485 if (Status
== EFI_BUFFER_TOO_SMALL
) {
486 Pages
= EFI_SIZE_TO_PAGES (MemoryMapSize
) + 1;
487 MemoryMap
= AllocatePages (Pages
);
488 Status
= gBS
->GetMemoryMap (&MemoryMapSize
, MemoryMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
491 // Go through the list and add the reserved region to the Device Tree
492 if (!EFI_ERROR(Status
)) {
493 for (Index
= 0; Index
< (MemoryMapSize
/ sizeof(EFI_MEMORY_DESCRIPTOR
)); Index
++) {
494 if (IsLinuxReservedRegion ((EFI_MEMORY_TYPE
)MemoryMap
[Index
].Type
)) {
495 DEBUG((DEBUG_VERBOSE
, "Reserved region of type %d [0x%X, 0x%X]\n",
496 MemoryMap
[Index
].Type
,
497 (UINTN
)MemoryMap
[Index
].PhysicalStart
,
498 (UINTN
)(MemoryMap
[Index
].PhysicalStart
+ MemoryMap
[Index
].NumberOfPages
* EFI_PAGE_SIZE
)));
499 err
= fdt_add_mem_rsv(fdt
, MemoryMap
[Index
].PhysicalStart
, MemoryMap
[Index
].NumberOfPages
* EFI_PAGE_SIZE
);
501 Print(L
"Warning: Fail to add 'memreserve' (err:%d)\n", err
);
508 // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms.
510 // For 'cpus' and 'cpu' device tree nodes bindings, refer to this file
511 // in the kernel documentation:
512 // Documentation/devicetree/bindings/arm/cpus.txt
514 for (Index
=0; Index
< gST
->NumberOfTableEntries
; Index
++) {
515 // Check for correct GUID type
516 if (CompareGuid (&gArmMpCoreInfoGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
517 MpId
= ArmReadMpidr ();
518 ClusterId
= GET_CLUSTER_ID(MpId
);
519 CoreId
= GET_CORE_ID(MpId
);
521 node
= fdt_subnode_offset(fdt
, 0, "cpus");
523 // Create the /cpus node
524 node
= fdt_add_subnode(fdt
, 0, "cpus");
525 fdt_setprop_string(fdt
, node
, "name", "cpus");
526 fdt_setprop_cell (fdt
, node
, "#address-cells", sizeof (UINTN
) / 4);
527 fdt_setprop_cell(fdt
, node
, "#size-cells", 0);
528 CpusNodeExist
= FALSE
;
530 CpusNodeExist
= TRUE
;
533 // Get pointer to ARM processor table
534 ArmProcessorTable
= (ARM_PROCESSOR_TABLE
*)gST
->ConfigurationTable
[Index
].VendorTable
;
535 ArmCoreInfoTable
= ArmProcessorTable
->ArmCpus
;
537 for (Index
= 0; Index
< ArmProcessorTable
->NumberOfEntries
; Index
++) {
538 CoreMpId
= (UINTN
) GET_MPID (ArmCoreInfoTable
[Index
].ClusterId
,
539 ArmCoreInfoTable
[Index
].CoreId
);
540 AsciiSPrint (Name
, 10, "cpu@%x", CoreMpId
);
542 // If the 'cpus' node did not exist then create all the 'cpu' nodes.
543 // In case 'cpus' node is provided in the original FDT then we do not add
545 if (!CpusNodeExist
) {
546 cpu_node
= fdt_add_subnode (fdt
, node
, Name
);
548 DEBUG ((EFI_D_ERROR
, "Error on creating '%s' node\n", Name
));
549 Status
= EFI_INVALID_PARAMETER
;
550 goto FAIL_COMPLETE_FDT
;
553 fdt_setprop_string (fdt
, cpu_node
, "device_type", "cpu");
555 CoreMpId
= cpu_to_fdtn (CoreMpId
);
556 fdt_setprop (fdt
, cpu_node
, "reg", &CoreMpId
, sizeof (CoreMpId
));
557 if (PsciSmcSupported
) {
558 fdt_setprop_string (fdt
, cpu_node
, "enable-method", "psci");
561 cpu_node
= fdt_subnode_offset(fdt
, node
, Name
);
564 // If Power State Coordination Interface (PSCI) is not supported then it is expected the secondary
565 // cores are spinning waiting for the Operating System to release them
566 if ((PsciSmcSupported
== FALSE
) && (cpu_node
>= 0)) {
567 // We as the bootloader are responsible for either creating or updating
568 // these entries. Do not trust the entries in the DT. We only know about
569 // 'spin-table' type. Do not try to update other types if defined.
570 Method
= fdt_getprop(fdt
, cpu_node
, "enable-method", &lenp
);
571 if ( (Method
== NULL
) || (!AsciiStrCmp((CHAR8
*)Method
, "spin-table")) ) {
572 fdt_setprop_string(fdt
, cpu_node
, "enable-method", "spin-table");
573 CpuReleaseAddr
= cpu_to_fdt64(ArmCoreInfoTable
[Index
].MailboxSetAddress
);
574 fdt_setprop(fdt
, cpu_node
, "cpu-release-addr", &CpuReleaseAddr
, sizeof(CpuReleaseAddr
));
576 // If it is not the primary core than the cpu should be disabled
577 if (((ArmCoreInfoTable
[Index
].ClusterId
!= ClusterId
) || (ArmCoreInfoTable
[Index
].CoreId
!= CoreId
))) {
578 fdt_setprop_string(fdt
, cpu_node
, "status", "disabled");
581 Print(L
"Warning: Unsupported enable-method type for CPU[%d] : %a\n", Index
, (CHAR8
*)Method
);
589 // If the Power State Coordination Interface is supported then we signal it in the Device Tree
590 if (PsciSmcSupported
== TRUE
) {
591 // Before to create it we check if the node is not already defined in the Device Tree
592 node
= fdt_subnode_offset(fdt
, 0, "psci");
594 // The 'psci' node does not exist, create it
595 node
= fdt_add_subnode(fdt
, 0, "psci");
597 DEBUG((EFI_D_ERROR
,"Error on creating 'psci' node\n"));
598 Status
= EFI_INVALID_PARAMETER
;
599 goto FAIL_COMPLETE_FDT
;
601 fdt_setprop_string (fdt
, node
, "compatible", "arm,psci");
602 fdt_setprop_string (fdt
, node
, "method", "smc");
604 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_SUSPEND
);
605 fdt_setprop (fdt
, node
, "cpu_suspend", &Smc
, sizeof (Smc
));
607 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_OFF
);
608 fdt_setprop (fdt
, node
, "cpu_off", &Smc
, sizeof (Smc
));
610 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_ON
);
611 fdt_setprop (fdt
, node
, "cpu_on", &Smc
, sizeof (Smc
));
613 Smc
= cpu_to_fdtn (ARM_SMC_ARM_MIGRATE
);
614 fdt_setprop (fdt
, node
, "migrate", &Smc
, sizeof (Smc
));
620 //DebugDumpFdt (fdt);
623 // If we succeeded to generate the new Device Tree then free the old Device Tree
624 gBS
->FreePages (*FdtBlobBase
, EFI_SIZE_TO_PAGES (*FdtBlobSize
));
626 *FdtBlobBase
= NewFdtBlobBase
;
627 *FdtBlobSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(NewFdtBlobBase
));
631 gBS
->FreePages (NewFdtBlobAllocation
, EFI_SIZE_TO_PAGES (NewFdtBlobSize
));
634 *FdtBlobSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(*FdtBlobBase
));
635 // Return success even if we failed to update the FDT blob.
636 // The original one is still valid.