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
:
210 case EfiReservedMemoryType
:
224 BOOLEAN PsciSmcSupported
;
227 PsciSmcSupported
= FALSE
;
229 // Check the SMC response to the Presence SMC
230 Rx
= ARM_SMC_ID_PRESENCE
;
236 if (Rx
== ARM_TRUSTZONE_UID_4LETTERID
) {
237 Rx
= ARM_SMC_ID_UID
+ 1;
239 if (Rx
== ARM_TRUSTZONE_ARM_UID
) {
240 PsciSmcSupported
= TRUE
;
245 return PsciSmcSupported
;
250 ** Relocate the FDT blob to a more appropriate location for the Linux kernel.
251 ** This function will allocate memory for the relocated FDT blob.
253 ** @retval EFI_SUCCESS on success.
254 ** @retval EFI_OUT_OF_RESOURCES or EFI_INVALID_PARAMETER on failure.
259 EFI_PHYSICAL_ADDRESS OriginalFdt
,
260 UINTN OriginalFdtSize
,
261 EFI_PHYSICAL_ADDRESS
*RelocatedFdt
,
262 UINTN
*RelocatedFdtSize
,
263 EFI_PHYSICAL_ADDRESS
*RelocatedFdtAlloc
270 *RelocatedFdtSize
= OriginalFdtSize
+ FDT_ADDITIONAL_ENTRIES_SIZE
;
272 // If FDT load address needs to be aligned, allocate more space.
273 FdtAlignment
= PcdGet32 (PcdArmLinuxFdtAlignment
);
274 if (FdtAlignment
!= 0) {
275 *RelocatedFdtSize
+= FdtAlignment
;
278 // Try below a watermark address.
279 Status
= EFI_NOT_FOUND
;
280 if (PcdGet32 (PcdArmLinuxFdtMaxOffset
) != 0) {
281 *RelocatedFdt
= LINUX_FDT_MAX_OFFSET
;
282 Status
= gBS
->AllocatePages (AllocateMaxAddress
, EfiBootServicesData
,
283 EFI_SIZE_TO_PAGES (*RelocatedFdtSize
), RelocatedFdt
);
284 if (EFI_ERROR (Status
)) {
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
));
289 // Try anywhere there is available space.
290 if (EFI_ERROR (Status
)) {
291 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesData
,
292 EFI_SIZE_TO_PAGES (*RelocatedFdtSize
), RelocatedFdt
);
293 if (EFI_ERROR (Status
)) {
294 ASSERT_EFI_ERROR (Status
);
295 return EFI_OUT_OF_RESOURCES
;
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
));
301 *RelocatedFdtAlloc
= *RelocatedFdt
;
302 if (FdtAlignment
!= 0) {
303 *RelocatedFdt
= ALIGN (*RelocatedFdt
, FdtAlignment
);
306 // Load the Original FDT tree into the new region
307 Error
= fdt_open_into ((VOID
*)(UINTN
) OriginalFdt
,
308 (VOID
*)(UINTN
)(*RelocatedFdt
), *RelocatedFdtSize
);
310 DEBUG ((EFI_D_ERROR
, "fdt_open_into(): %a\n", fdt_strerror (Error
)));
311 gBS
->FreePages (*RelocatedFdtAlloc
, EFI_SIZE_TO_PAGES (*RelocatedFdtSize
));
312 return EFI_INVALID_PARAMETER
;
316 //DebugDumpFdt (fdt);
325 IN CONST CHAR8
* CommandLineArguments
,
326 IN EFI_PHYSICAL_ADDRESS InitrdImage
,
327 IN UINTN InitrdImageSize
,
328 IN OUT EFI_PHYSICAL_ADDRESS
*FdtBlobBase
,
329 IN OUT UINTN
*FdtBlobSize
333 EFI_PHYSICAL_ADDRESS NewFdtBlobBase
;
334 EFI_PHYSICAL_ADDRESS NewFdtBlobAllocation
;
335 UINTN NewFdtBlobSize
;
343 EFI_PHYSICAL_ADDRESS InitrdImageStart
;
344 EFI_PHYSICAL_ADDRESS InitrdImageEnd
;
348 LIST_ENTRY ResourceList
;
349 BDS_SYSTEM_MEMORY_RESOURCE
*Resource
;
350 ARM_PROCESSOR_TABLE
*ArmProcessorTable
;
351 ARM_CORE_INFO
*ArmCoreInfoTable
;
355 UINT64 CpuReleaseAddr
;
357 EFI_MEMORY_DESCRIPTOR
*MemoryMap
;
358 EFI_MEMORY_DESCRIPTOR
*MemoryMapPtr
;
360 UINTN DescriptorSize
;
361 UINT32 DescriptorVersion
;
363 BOOLEAN PsciSmcSupported
;
364 UINTN OriginalFdtSize
;
365 BOOLEAN CpusNodeExist
;
369 NewFdtBlobAllocation
= 0;
372 // Sanity checks on the original FDT blob.
374 err
= fdt_check_header ((VOID
*)(UINTN
)(*FdtBlobBase
));
376 Print (L
"ERROR: Device Tree header not valid (err:%d)\n", err
);
377 return EFI_INVALID_PARAMETER
;
380 // The original FDT blob might have been loaded partially.
381 // Check that it is not the case.
382 OriginalFdtSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(*FdtBlobBase
));
383 if (OriginalFdtSize
> *FdtBlobSize
) {
384 Print (L
"ERROR: Incomplete FDT. Only %d/%d bytes have been loaded.\n",
385 *FdtBlobSize
, OriginalFdtSize
);
386 return EFI_INVALID_PARAMETER
;
390 // Relocate the FDT to its final location.
392 Status
= RelocateFdt (*FdtBlobBase
, OriginalFdtSize
,
393 &NewFdtBlobBase
, &NewFdtBlobSize
, &NewFdtBlobAllocation
);
394 if (EFI_ERROR (Status
)) {
395 goto FAIL_RELOCATE_FDT
;
399 // Ensure the Power State Coordination Interface (PSCI) SMCs are there if supported
401 PsciSmcSupported
= FALSE
;
402 if (FeaturePcdGet (PcdArmPsciSupport
) == TRUE
) {
403 PsciSmcSupported
= IsPsciSmcSupported();
404 if (PsciSmcSupported
== FALSE
) {
405 DEBUG ((EFI_D_ERROR
, "Warning: The Power State Coordination Interface (PSCI) is not supported by your platform Trusted Firmware.\n"));
409 fdt
= (VOID
*)(UINTN
)NewFdtBlobBase
;
411 node
= fdt_subnode_offset (fdt
, 0, "chosen");
413 // The 'chosen' node does not exist, create it
414 node
= fdt_add_subnode(fdt
, 0, "chosen");
416 DEBUG((EFI_D_ERROR
,"Error on finding 'chosen' node\n"));
417 Status
= EFI_INVALID_PARAMETER
;
418 goto FAIL_COMPLETE_FDT
;
423 BootArg
= fdt_getprop(fdt
, node
, "bootargs", &lenp
);
424 if (BootArg
!= NULL
) {
425 DEBUG((EFI_D_ERROR
,"BootArg: %a\n",BootArg
));
432 if ((CommandLineArguments
!= NULL
) && (AsciiStrLen (CommandLineArguments
) > 0)) {
433 err
= fdt_setprop(fdt
, node
, "bootargs", CommandLineArguments
, AsciiStrSize(CommandLineArguments
));
435 DEBUG((EFI_D_ERROR
,"Fail to set new 'bootarg' (err:%d)\n",err
));
442 if (InitrdImageSize
!= 0) {
443 InitrdImageStart
= cpu_to_fdt64 (InitrdImage
);
444 err
= fdt_setprop(fdt
, node
, "linux,initrd-start", &InitrdImageStart
, sizeof(EFI_PHYSICAL_ADDRESS
));
446 DEBUG((EFI_D_ERROR
,"Fail to set new 'linux,initrd-start' (err:%d)\n",err
));
448 InitrdImageEnd
= cpu_to_fdt64 (InitrdImage
+ InitrdImageSize
);
449 err
= fdt_setprop(fdt
, node
, "linux,initrd-end", &InitrdImageEnd
, sizeof(EFI_PHYSICAL_ADDRESS
));
451 DEBUG((EFI_D_ERROR
,"Fail to set new 'linux,initrd-start' (err:%d)\n",err
));
456 // Set Physical memory setup if does not exist
458 node
= fdt_subnode_offset(fdt
, 0, "memory");
460 // The 'memory' node does not exist, create it
461 node
= fdt_add_subnode(fdt
, 0, "memory");
463 fdt_setprop_string(fdt
, node
, "name", "memory");
464 fdt_setprop_string(fdt
, node
, "device_type", "memory");
466 GetSystemMemoryResources (&ResourceList
);
467 Resource
= (BDS_SYSTEM_MEMORY_RESOURCE
*)ResourceList
.ForwardLink
;
469 Region
.Base
= cpu_to_fdtn ((UINTN
)Resource
->PhysicalStart
);
470 Region
.Size
= cpu_to_fdtn ((UINTN
)Resource
->ResourceLength
);
472 err
= fdt_setprop(fdt
, node
, "reg", &Region
, sizeof(Region
));
474 DEBUG((EFI_D_ERROR
,"Fail to set new 'memory region' (err:%d)\n",err
));
480 // Add the memory regions reserved by the UEFI Firmware
483 // Retrieve the UEFI Memory Map
486 Status
= gBS
->GetMemoryMap (&MemoryMapSize
, MemoryMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
487 if (Status
== EFI_BUFFER_TOO_SMALL
) {
488 // The UEFI specification advises to allocate more memory for the MemoryMap buffer between successive
489 // calls to GetMemoryMap(), since allocation of the new buffer may potentially increase memory map size.
490 Pages
= EFI_SIZE_TO_PAGES (MemoryMapSize
) + 1;
491 MemoryMap
= AllocatePages (Pages
);
492 if (MemoryMap
== NULL
) {
493 Status
= EFI_OUT_OF_RESOURCES
;
494 goto FAIL_COMPLETE_FDT
;
496 Status
= gBS
->GetMemoryMap (&MemoryMapSize
, MemoryMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
499 // Go through the list and add the reserved region to the Device Tree
500 if (!EFI_ERROR(Status
)) {
501 MemoryMapPtr
= MemoryMap
;
502 for (Index
= 0; Index
< (MemoryMapSize
/ DescriptorSize
); Index
++) {
503 if (IsLinuxReservedRegion ((EFI_MEMORY_TYPE
)MemoryMapPtr
->Type
)) {
504 DEBUG((DEBUG_VERBOSE
, "Reserved region of type %d [0x%lX, 0x%lX]\n",
506 (UINTN
)MemoryMapPtr
->PhysicalStart
,
507 (UINTN
)(MemoryMapPtr
->PhysicalStart
+ MemoryMapPtr
->NumberOfPages
* EFI_PAGE_SIZE
)));
508 err
= fdt_add_mem_rsv(fdt
, MemoryMapPtr
->PhysicalStart
, MemoryMapPtr
->NumberOfPages
* EFI_PAGE_SIZE
);
510 Print(L
"Warning: Fail to add 'memreserve' (err:%d)\n", err
);
513 MemoryMapPtr
= (EFI_MEMORY_DESCRIPTOR
*)((UINTN
)MemoryMapPtr
+ DescriptorSize
);
518 // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms.
520 // For 'cpus' and 'cpu' device tree nodes bindings, refer to this file
521 // in the kernel documentation:
522 // Documentation/devicetree/bindings/arm/cpus.txt
524 for (Index
=0; Index
< gST
->NumberOfTableEntries
; Index
++) {
525 // Check for correct GUID type
526 if (CompareGuid (&gArmMpCoreInfoGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
527 MpId
= ArmReadMpidr ();
528 ClusterId
= GET_CLUSTER_ID(MpId
);
529 CoreId
= GET_CORE_ID(MpId
);
531 node
= fdt_subnode_offset(fdt
, 0, "cpus");
533 // Create the /cpus node
534 node
= fdt_add_subnode(fdt
, 0, "cpus");
535 fdt_setprop_string(fdt
, node
, "name", "cpus");
536 fdt_setprop_cell (fdt
, node
, "#address-cells", sizeof (UINTN
) / 4);
537 fdt_setprop_cell(fdt
, node
, "#size-cells", 0);
538 CpusNodeExist
= FALSE
;
540 CpusNodeExist
= TRUE
;
543 // Get pointer to ARM processor table
544 ArmProcessorTable
= (ARM_PROCESSOR_TABLE
*)gST
->ConfigurationTable
[Index
].VendorTable
;
545 ArmCoreInfoTable
= ArmProcessorTable
->ArmCpus
;
547 for (Index
= 0; Index
< ArmProcessorTable
->NumberOfEntries
; Index
++) {
548 CoreMpId
= (UINTN
) GET_MPID (ArmCoreInfoTable
[Index
].ClusterId
,
549 ArmCoreInfoTable
[Index
].CoreId
);
550 AsciiSPrint (Name
, 10, "cpu@%x", CoreMpId
);
552 // If the 'cpus' node did not exist then create all the 'cpu' nodes.
553 // In case 'cpus' node is provided in the original FDT then we do not add
555 if (!CpusNodeExist
) {
556 cpu_node
= fdt_add_subnode (fdt
, node
, Name
);
558 DEBUG ((EFI_D_ERROR
, "Error on creating '%s' node\n", Name
));
559 Status
= EFI_INVALID_PARAMETER
;
560 goto FAIL_COMPLETE_FDT
;
563 fdt_setprop_string (fdt
, cpu_node
, "device_type", "cpu");
565 CoreMpId
= cpu_to_fdtn (CoreMpId
);
566 fdt_setprop (fdt
, cpu_node
, "reg", &CoreMpId
, sizeof (CoreMpId
));
567 if (PsciSmcSupported
) {
568 fdt_setprop_string (fdt
, cpu_node
, "enable-method", "psci");
571 cpu_node
= fdt_subnode_offset(fdt
, node
, Name
);
574 // If Power State Coordination Interface (PSCI) is not supported then it is expected the secondary
575 // cores are spinning waiting for the Operating System to release them
576 if ((PsciSmcSupported
== FALSE
) && (cpu_node
>= 0)) {
577 // We as the bootloader are responsible for either creating or updating
578 // these entries. Do not trust the entries in the DT. We only know about
579 // 'spin-table' type. Do not try to update other types if defined.
580 Method
= fdt_getprop(fdt
, cpu_node
, "enable-method", &lenp
);
581 if ( (Method
== NULL
) || (!AsciiStrCmp((CHAR8
*)Method
, "spin-table")) ) {
582 fdt_setprop_string(fdt
, cpu_node
, "enable-method", "spin-table");
583 CpuReleaseAddr
= cpu_to_fdt64(ArmCoreInfoTable
[Index
].MailboxSetAddress
);
584 fdt_setprop(fdt
, cpu_node
, "cpu-release-addr", &CpuReleaseAddr
, sizeof(CpuReleaseAddr
));
586 // If it is not the primary core than the cpu should be disabled
587 if (((ArmCoreInfoTable
[Index
].ClusterId
!= ClusterId
) || (ArmCoreInfoTable
[Index
].CoreId
!= CoreId
))) {
588 fdt_setprop_string(fdt
, cpu_node
, "status", "disabled");
591 Print(L
"Warning: Unsupported enable-method type for CPU[%d] : %a\n", Index
, (CHAR8
*)Method
);
599 // If the Power State Coordination Interface is supported then we signal it in the Device Tree
600 if (PsciSmcSupported
== TRUE
) {
601 // Before to create it we check if the node is not already defined in the Device Tree
602 node
= fdt_subnode_offset(fdt
, 0, "psci");
604 // The 'psci' node does not exist, create it
605 node
= fdt_add_subnode(fdt
, 0, "psci");
607 DEBUG((EFI_D_ERROR
,"Error on creating 'psci' node\n"));
608 Status
= EFI_INVALID_PARAMETER
;
609 goto FAIL_COMPLETE_FDT
;
611 fdt_setprop_string (fdt
, node
, "compatible", "arm,psci");
612 fdt_setprop_string (fdt
, node
, "method", "smc");
614 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_SUSPEND
);
615 fdt_setprop (fdt
, node
, "cpu_suspend", &Smc
, sizeof (Smc
));
617 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_OFF
);
618 fdt_setprop (fdt
, node
, "cpu_off", &Smc
, sizeof (Smc
));
620 Smc
= cpu_to_fdtn (ARM_SMC_ARM_CPU_ON
);
621 fdt_setprop (fdt
, node
, "cpu_on", &Smc
, sizeof (Smc
));
623 Smc
= cpu_to_fdtn (ARM_SMC_ARM_MIGRATE
);
624 fdt_setprop (fdt
, node
, "migrate", &Smc
, sizeof (Smc
));
630 //DebugDumpFdt (fdt);
633 // If we succeeded to generate the new Device Tree then free the old Device Tree
634 gBS
->FreePages (*FdtBlobBase
, EFI_SIZE_TO_PAGES (*FdtBlobSize
));
636 *FdtBlobBase
= NewFdtBlobBase
;
637 *FdtBlobSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(NewFdtBlobBase
));
641 gBS
->FreePages (NewFdtBlobAllocation
, EFI_SIZE_TO_PAGES (NewFdtBlobSize
));
644 *FdtBlobSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(*FdtBlobBase
));
645 // Return success even if we failed to update the FDT blob.
646 // The original one is still valid.