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
;
357 EFI_MEMORY_DESCRIPTOR
*MemoryMapPtr
;
359 UINTN DescriptorSize
;
360 UINT32 DescriptorVersion
;
362 BOOLEAN PsciSmcSupported
;
363 UINTN OriginalFdtSize
;
364 BOOLEAN CpusNodeExist
;
368 NewFdtBlobAllocation
= 0;
371 // Sanity checks on the original FDT blob.
373 err
= fdt_check_header ((VOID
*)(UINTN
)(*FdtBlobBase
));
375 Print (L
"ERROR: Device Tree header not valid (err:%d)\n", err
);
376 return EFI_INVALID_PARAMETER
;
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
;
389 // Relocate the FDT to its final location.
391 Status
= RelocateFdt (*FdtBlobBase
, OriginalFdtSize
,
392 &NewFdtBlobBase
, &NewFdtBlobSize
, &NewFdtBlobAllocation
);
393 if (EFI_ERROR (Status
)) {
394 goto FAIL_RELOCATE_FDT
;
398 // Ensure the Power State Coordination Interface (PSCI) SMCs are there if supported
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"));
408 fdt
= (VOID
*)(UINTN
)NewFdtBlobBase
;
410 node
= fdt_subnode_offset (fdt
, 0, "chosen");
412 // The 'chosen' node does not exist, create it
413 node
= fdt_add_subnode(fdt
, 0, "chosen");
415 DEBUG((EFI_D_ERROR
,"Error on finding 'chosen' node\n"));
416 Status
= EFI_INVALID_PARAMETER
;
417 goto FAIL_COMPLETE_FDT
;
422 BootArg
= fdt_getprop(fdt
, node
, "bootargs", &lenp
);
423 if (BootArg
!= NULL
) {
424 DEBUG((EFI_D_ERROR
,"BootArg: %a\n",BootArg
));
431 if ((CommandLineArguments
!= NULL
) && (AsciiStrLen (CommandLineArguments
) > 0)) {
432 err
= fdt_setprop(fdt
, node
, "bootargs", CommandLineArguments
, AsciiStrSize(CommandLineArguments
));
434 DEBUG((EFI_D_ERROR
,"Fail to set new 'bootarg' (err:%d)\n",err
));
441 if (InitrdImageSize
!= 0) {
442 InitrdImageStart
= cpu_to_fdt64 (InitrdImage
);
443 err
= fdt_setprop(fdt
, node
, "linux,initrd-start", &InitrdImageStart
, sizeof(EFI_PHYSICAL_ADDRESS
));
445 DEBUG((EFI_D_ERROR
,"Fail to set new 'linux,initrd-start' (err:%d)\n",err
));
447 InitrdImageEnd
= cpu_to_fdt64 (InitrdImage
+ InitrdImageSize
);
448 err
= fdt_setprop(fdt
, node
, "linux,initrd-end", &InitrdImageEnd
, sizeof(EFI_PHYSICAL_ADDRESS
));
450 DEBUG((EFI_D_ERROR
,"Fail to set new 'linux,initrd-start' (err:%d)\n",err
));
455 // Set Physical memory setup if does not exist
457 node
= fdt_subnode_offset(fdt
, 0, "memory");
459 // The 'memory' node does not exist, create it
460 node
= fdt_add_subnode(fdt
, 0, "memory");
462 fdt_setprop_string(fdt
, node
, "name", "memory");
463 fdt_setprop_string(fdt
, node
, "device_type", "memory");
465 GetSystemMemoryResources (&ResourceList
);
466 Resource
= (BDS_SYSTEM_MEMORY_RESOURCE
*)ResourceList
.ForwardLink
;
468 Region
.Base
= cpu_to_fdtn ((UINTN
)Resource
->PhysicalStart
);
469 Region
.Size
= cpu_to_fdtn ((UINTN
)Resource
->ResourceLength
);
471 err
= fdt_setprop(fdt
, node
, "reg", &Region
, sizeof(Region
));
473 DEBUG((EFI_D_ERROR
,"Fail to set new 'memory region' (err:%d)\n",err
));
479 // Add the memory regions reserved by the UEFI Firmware
482 // Retrieve the UEFI Memory Map
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
;
495 Status
= gBS
->GetMemoryMap (&MemoryMapSize
, MemoryMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
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",
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
);
509 Print(L
"Warning: Fail to add 'memreserve' (err:%d)\n", err
);
512 MemoryMapPtr
= (EFI_MEMORY_DESCRIPTOR
*)((UINTN
)MemoryMapPtr
+ DescriptorSize
);
517 // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms.
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
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
);
530 node
= fdt_subnode_offset(fdt
, 0, "cpus");
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
;
539 CpusNodeExist
= TRUE
;
542 // Get pointer to ARM processor table
543 ArmProcessorTable
= (ARM_PROCESSOR_TABLE
*)gST
->ConfigurationTable
[Index
].VendorTable
;
544 ArmCoreInfoTable
= ArmProcessorTable
->ArmCpus
;
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
);
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
554 if (!CpusNodeExist
) {
555 cpu_node
= fdt_add_subnode (fdt
, node
, Name
);
557 DEBUG ((EFI_D_ERROR
, "Error on creating '%s' node\n", Name
));
558 Status
= EFI_INVALID_PARAMETER
;
559 goto FAIL_COMPLETE_FDT
;
562 fdt_setprop_string (fdt
, cpu_node
, "device_type", "cpu");
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");
570 cpu_node
= fdt_subnode_offset(fdt
, node
, Name
);
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
));
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");
590 Print(L
"Warning: Unsupported enable-method type for CPU[%d] : %a\n", Index
, (CHAR8
*)Method
);
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");
603 // The 'psci' node does not exist, create it
604 node
= fdt_add_subnode(fdt
, 0, "psci");
606 DEBUG((EFI_D_ERROR
,"Error on creating 'psci' node\n"));
607 Status
= EFI_INVALID_PARAMETER
;
608 goto FAIL_COMPLETE_FDT
;
610 fdt_setprop_string (fdt
, node
, "compatible", "arm,psci");
611 fdt_setprop_string (fdt
, node
, "method", "smc");
613 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_SUSPEND
);
614 fdt_setprop (fdt
, node
, "cpu_suspend", &Smc
, sizeof (Smc
));
616 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_OFF
);
617 fdt_setprop (fdt
, node
, "cpu_off", &Smc
, sizeof (Smc
));
619 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_ON
);
620 fdt_setprop (fdt
, node
, "cpu_on", &Smc
, sizeof (Smc
));
622 Smc
= cpu_to_fdtn (ARM_SMC_ARM_MIGRATE
);
623 fdt_setprop (fdt
, node
, "migrate", &Smc
, sizeof (Smc
));
629 //DebugDumpFdt (fdt);
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
));
635 *FdtBlobBase
= NewFdtBlobBase
;
636 *FdtBlobSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(NewFdtBlobBase
));
640 gBS
->FreePages (NewFdtBlobAllocation
, EFI_SIZE_TO_PAGES (NewFdtBlobSize
));
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.