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)))
35 CONST CHAR8
*s
= data
;
43 // Must terminate with zero
44 if (s
[len
- 1] != '\0') {
49 while (*s
/* && isprint(*s)*/) {
53 // Not zero, or not done yet
54 if (*s
!= '\0' || (s
+ 1 - ss
) < len
) {
69 CONST CHAR8
*p
= data
;
71 // No data, don't print
75 if (IsPrintableString (data
, len
)) {
76 Print(L
" = \"%a\"", (const char *)data
);
77 } else if ((len
% 4) == 0) {
79 for (i
= 0; i
< len
; i
+= 4) {
80 Print(L
"0x%08x%a", fdt32_to_cpu(GET_CELL(p
)),i
< (len
- 4) ? " " : "");
85 for (i
= 0; i
< len
; i
++)
86 Print(L
"%02x%a", *p
++, i
< len
- 1 ? " " : "");
96 struct fdt_header
*bph
;
99 CONST CHAR8
* p_struct
;
100 CONST CHAR8
* p_strings
;
111 // Can 'memreserve' be printed by below code?
112 INTN num
= fdt_num_mem_rsv(FdtBlob
);
114 UINT64 addr
= 0,size
= 0;
116 for (i
= 0; i
< num
; i
++) {
117 err
= fdt_get_mem_rsv(FdtBlob
, i
, &addr
, &size
);
119 DEBUG((EFI_D_ERROR
, "Error (%d) : Cannot get memreserve section (%d)\n", err
, i
));
122 Print(L
"/memreserve/ \t0x%lx \t0x%lx;\n",addr
,size
);
131 off_dt
= fdt32_to_cpu(bph
->off_dt_struct
);
132 off_str
= fdt32_to_cpu(bph
->off_dt_strings
);
133 p_struct
= (CONST CHAR8
*)FdtBlob
+ off_dt
;
134 p_strings
= (CONST CHAR8
*)FdtBlob
+ off_str
;
135 version
= fdt32_to_cpu(bph
->version
);
138 while ((tag
= fdt32_to_cpu(GET_CELL(p
))) != FDT_END
) {
139 if (tag
== FDT_BEGIN_NODE
) {
141 p
= PALIGN(p
+ AsciiStrLen (s
) + 1, 4);
146 Print(L
"%*s%a {\n", depth
* shift
, L
" ", s
);
152 if (tag
== FDT_END_NODE
) {
155 Print(L
"%*s};\n", depth
* shift
, L
" ");
159 if (tag
== FDT_NOP
) {
160 Print(L
"%*s// [NOP]\n", depth
* shift
, L
" ");
164 if (tag
!= FDT_PROP
) {
165 Print(L
"%*s ** Unknown tag 0x%08x\n", depth
* shift
, L
" ", tag
);
168 sz
= fdt32_to_cpu(GET_CELL(p
));
169 s
= p_strings
+ fdt32_to_cpu(GET_CELL(p
));
170 if (version
< 16 && sz
>= 8)
174 p
= PALIGN(p
+ sz
, 4);
176 Print(L
"%*s%a", depth
* shift
, L
" ", s
);
184 IsLinuxReservedRegion (
185 IN EFI_MEMORY_TYPE MemoryType
189 case EfiRuntimeServicesCode
:
190 case EfiRuntimeServicesData
:
191 case EfiUnusableMemory
:
192 case EfiACPIReclaimMemory
:
193 case EfiACPIMemoryNVS
:
208 IN CONST CHAR8
* CommandLineArguments
,
209 IN EFI_PHYSICAL_ADDRESS InitrdImage
,
210 IN UINTN InitrdImageSize
,
211 IN OUT EFI_PHYSICAL_ADDRESS
*FdtBlobBase
,
212 IN OUT UINTN
*FdtBlobSize
216 EFI_PHYSICAL_ADDRESS NewFdtBlobBase
;
217 UINTN NewFdtBlobSize
;
225 EFI_PHYSICAL_ADDRESS InitrdImageStart
;
226 EFI_PHYSICAL_ADDRESS InitrdImageEnd
;
230 LIST_ENTRY ResourceList
;
231 BDS_SYSTEM_MEMORY_RESOURCE
*Resource
;
232 ARM_PROCESSOR_TABLE
*ArmProcessorTable
;
233 ARM_CORE_INFO
*ArmCoreInfoTable
;
237 UINT64 CpuReleaseAddr
;
239 EFI_MEMORY_DESCRIPTOR
*MemoryMap
;
241 UINTN DescriptorSize
;
242 UINT32 DescriptorVersion
;
244 BOOLEAN PsciSmcSupported
;
246 UINTN OriginalFdtSize
;
247 BOOLEAN CpusNodeExist
;
250 // Ensure the Power State Coordination Interface (PSCI) SMCs are there if supported
252 PsciSmcSupported
= FALSE
;
253 if (FeaturePcdGet (PcdArmPsciSupport
) == TRUE
) {
254 // Check the SMC response to the Presence SMC
255 Rx
= ARM_SMC_ID_PRESENCE
;
261 if (Rx
== ARM_TRUSTZONE_UID_4LETTERID
) {
262 Rx
= ARM_SMC_ID_UID
+ 1;
264 if (Rx
== ARM_TRUSTZONE_ARM_UID
) {
265 PsciSmcSupported
= TRUE
;
268 if (PsciSmcSupported
== FALSE
) {
269 DEBUG((EFI_D_ERROR
,"Warning: The Power State Coordination Interface (PSCI) is not supported"
270 "by your platform Trusted Firmware.\n"));
276 // Sanity checks on the original FDT blob.
278 err
= fdt_check_header ((VOID
*)(UINTN
)(*FdtBlobBase
));
280 Print (L
"ERROR: Device Tree header not valid (err:%d)\n", err
);
281 return EFI_INVALID_PARAMETER
;
284 // The original FDT blob might have been loaded partially.
285 // Check that it is not the case.
286 OriginalFdtSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(*FdtBlobBase
));
287 if (OriginalFdtSize
> *FdtBlobSize
) {
288 Print (L
"ERROR: Incomplete FDT. Only %d/%d bytes have been loaded.\n",
289 *FdtBlobSize
, OriginalFdtSize
);
290 return EFI_INVALID_PARAMETER
;
294 // Allocate memory for the new FDT
296 NewFdtBlobSize
= OriginalFdtSize
+ FDT_ADDITIONAL_ENTRIES_SIZE
;
298 // Try below a watermark address
299 Status
= EFI_NOT_FOUND
;
300 if (PcdGet32(PcdArmLinuxFdtMaxOffset
) != 0) {
301 NewFdtBlobBase
= LINUX_FDT_MAX_OFFSET
;
302 Status
= gBS
->AllocatePages (AllocateMaxAddress
, EfiBootServicesData
, EFI_SIZE_TO_PAGES(NewFdtBlobSize
), &NewFdtBlobBase
);
303 if (EFI_ERROR(Status
)) {
304 DEBUG ((EFI_D_WARN
, "Warning: Failed to load FDT below address 0x%lX (%r). Will try again at a random address anywhere.\n", NewFdtBlobBase
, Status
));
308 // Try anywhere there is available space
309 if (EFI_ERROR(Status
)) {
310 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesData
, EFI_SIZE_TO_PAGES(NewFdtBlobSize
), &NewFdtBlobBase
);
311 if (EFI_ERROR(Status
)) {
312 ASSERT_EFI_ERROR(Status
);
313 goto FAIL_ALLOCATE_NEW_FDT
;
315 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", NewFdtBlobBase
));
319 // Load the Original FDT tree into the new region
320 fdt
= (VOID
*)(UINTN
)NewFdtBlobBase
;
321 err
= fdt_open_into((VOID
*)(UINTN
)(*FdtBlobBase
), fdt
, NewFdtBlobSize
);
323 DEBUG((EFI_D_ERROR
, "fdt_open_into(): %a\n", fdt_strerror(err
)));
324 Status
= EFI_INVALID_PARAMETER
;
329 //DebugDumpFdt (fdt);
332 node
= fdt_subnode_offset(fdt
, 0, "chosen");
334 // The 'chosen' node does not exist, create it
335 node
= fdt_add_subnode(fdt
, 0, "chosen");
337 DEBUG((EFI_D_ERROR
,"Error on finding 'chosen' node\n"));
338 Status
= EFI_INVALID_PARAMETER
;
344 BootArg
= fdt_getprop(fdt
, node
, "bootargs", &lenp
);
345 if (BootArg
!= NULL
) {
346 DEBUG((EFI_D_ERROR
,"BootArg: %a\n",BootArg
));
353 if ((CommandLineArguments
!= NULL
) && (AsciiStrLen (CommandLineArguments
) > 0)) {
354 err
= fdt_setprop(fdt
, node
, "bootargs", CommandLineArguments
, AsciiStrSize(CommandLineArguments
));
356 DEBUG((EFI_D_ERROR
,"Fail to set new 'bootarg' (err:%d)\n",err
));
363 if (InitrdImageSize
!= 0) {
364 InitrdImageStart
= cpu_to_fdt64 (InitrdImage
);
365 err
= fdt_setprop(fdt
, node
, "linux,initrd-start", &InitrdImageStart
, sizeof(EFI_PHYSICAL_ADDRESS
));
367 DEBUG((EFI_D_ERROR
,"Fail to set new 'linux,initrd-start' (err:%d)\n",err
));
369 InitrdImageEnd
= cpu_to_fdt64 (InitrdImage
+ InitrdImageSize
);
370 err
= fdt_setprop(fdt
, node
, "linux,initrd-end", &InitrdImageEnd
, sizeof(EFI_PHYSICAL_ADDRESS
));
372 DEBUG((EFI_D_ERROR
,"Fail to set new 'linux,initrd-start' (err:%d)\n",err
));
377 // Set Physical memory setup if does not exist
379 node
= fdt_subnode_offset(fdt
, 0, "memory");
381 // The 'memory' node does not exist, create it
382 node
= fdt_add_subnode(fdt
, 0, "memory");
384 fdt_setprop_string(fdt
, node
, "name", "memory");
385 fdt_setprop_string(fdt
, node
, "device_type", "memory");
387 GetSystemMemoryResources (&ResourceList
);
388 Resource
= (BDS_SYSTEM_MEMORY_RESOURCE
*)ResourceList
.ForwardLink
;
390 if (sizeof(UINTN
) == sizeof(UINT32
)) {
391 Region
.Base
= cpu_to_fdt32((UINTN
)Resource
->PhysicalStart
);
392 Region
.Size
= cpu_to_fdt32((UINTN
)Resource
->ResourceLength
);
394 Region
.Base
= cpu_to_fdt64((UINTN
)Resource
->PhysicalStart
);
395 Region
.Size
= cpu_to_fdt64((UINTN
)Resource
->ResourceLength
);
398 err
= fdt_setprop(fdt
, node
, "reg", &Region
, sizeof(Region
));
400 DEBUG((EFI_D_ERROR
,"Fail to set new 'memory region' (err:%d)\n",err
));
406 // Add the memory regions reserved by the UEFI Firmware
409 // Retrieve the UEFI Memory Map
412 Status
= gBS
->GetMemoryMap (&MemoryMapSize
, MemoryMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
413 if (Status
== EFI_BUFFER_TOO_SMALL
) {
414 Pages
= EFI_SIZE_TO_PAGES (MemoryMapSize
) + 1;
415 MemoryMap
= AllocatePages (Pages
);
416 Status
= gBS
->GetMemoryMap (&MemoryMapSize
, MemoryMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
419 // Go through the list and add the reserved region to the Device Tree
420 if (!EFI_ERROR(Status
)) {
421 for (Index
= 0; Index
< (MemoryMapSize
/ sizeof(EFI_MEMORY_DESCRIPTOR
)); Index
++) {
422 if (IsLinuxReservedRegion ((EFI_MEMORY_TYPE
)MemoryMap
[Index
].Type
)) {
423 DEBUG((DEBUG_VERBOSE
, "Reserved region of type %d [0x%X, 0x%X]\n",
424 MemoryMap
[Index
].Type
,
425 (UINTN
)MemoryMap
[Index
].PhysicalStart
,
426 (UINTN
)(MemoryMap
[Index
].PhysicalStart
+ MemoryMap
[Index
].NumberOfPages
* EFI_PAGE_SIZE
)));
427 err
= fdt_add_mem_rsv(fdt
, MemoryMap
[Index
].PhysicalStart
, MemoryMap
[Index
].NumberOfPages
* EFI_PAGE_SIZE
);
429 Print(L
"Warning: Fail to add 'memreserve' (err:%d)\n", err
);
436 // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms
438 for (Index
=0; Index
< gST
->NumberOfTableEntries
; Index
++) {
439 // Check for correct GUID type
440 if (CompareGuid (&gArmMpCoreInfoGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
441 MpId
= ArmReadMpidr ();
442 ClusterId
= GET_CLUSTER_ID(MpId
);
443 CoreId
= GET_CORE_ID(MpId
);
445 node
= fdt_subnode_offset(fdt
, 0, "cpus");
447 // Create the /cpus node
448 node
= fdt_add_subnode(fdt
, 0, "cpus");
449 fdt_setprop_string(fdt
, node
, "name", "cpus");
450 fdt_setprop_cell(fdt
, node
, "#address-cells", 1);
451 fdt_setprop_cell(fdt
, node
, "#size-cells", 0);
452 CpusNodeExist
= FALSE
;
454 CpusNodeExist
= TRUE
;
457 // Get pointer to ARM processor table
458 ArmProcessorTable
= (ARM_PROCESSOR_TABLE
*)gST
->ConfigurationTable
[Index
].VendorTable
;
459 ArmCoreInfoTable
= ArmProcessorTable
->ArmCpus
;
461 for (Index
= 0; Index
< ArmProcessorTable
->NumberOfEntries
; Index
++) {
462 AsciiSPrint (Name
, 10, "cpu@%d", Index
);
464 // If the 'cpus' node did not exist then creates the 'cpu' nodes. In case 'cpus' node
465 // is provided in the original FDT then we do not add any 'cpu' node.
466 if (!CpusNodeExist
) {
467 cpu_node
= fdt_add_subnode(fdt
, node
, Name
);
468 fdt_setprop_string(fdt
, cpu_node
, "device-type", "cpu");
469 fdt_setprop(fdt
, cpu_node
, "reg", &Index
, sizeof(Index
));
471 cpu_node
= fdt_subnode_offset(fdt
, node
, Name
);
474 // If Power State Coordination Interface (PSCI) is not supported then it is expected the secondary
475 // cores are spinning waiting for the Operating System to release them
476 if ((PsciSmcSupported
== FALSE
) && (cpu_node
>= 0)) {
477 // We as the bootloader are responsible for either creating or updating
478 // these entries. Do not trust the entries in the DT. We only know about
479 // 'spin-table' type. Do not try to update other types if defined.
480 Method
= fdt_getprop(fdt
, cpu_node
, "enable-method", &lenp
);
481 if ( (Method
== NULL
) || (!AsciiStrCmp((CHAR8
*)Method
, "spin-table")) ) {
482 fdt_setprop_string(fdt
, cpu_node
, "enable-method", "spin-table");
483 CpuReleaseAddr
= cpu_to_fdt64(ArmCoreInfoTable
[Index
].MailboxSetAddress
);
484 fdt_setprop(fdt
, cpu_node
, "cpu-release-addr", &CpuReleaseAddr
, sizeof(CpuReleaseAddr
));
486 // If it is not the primary core than the cpu should be disabled
487 if (((ArmCoreInfoTable
[Index
].ClusterId
!= ClusterId
) || (ArmCoreInfoTable
[Index
].CoreId
!= CoreId
))) {
488 fdt_setprop_string(fdt
, cpu_node
, "status", "disabled");
491 Print(L
"Warning: Unsupported enable-method type for CPU[%d] : %a\n", Index
, (CHAR8
*)Method
);
499 // If the Power State Coordination Interface is supported then we signal it in the Device Tree
500 if (PsciSmcSupported
== TRUE
) {
501 // Before to create it we check if the node is not already defined in the Device Tree
502 node
= fdt_subnode_offset(fdt
, 0, "psci");
504 // The 'psci' node does not exist, create it
505 node
= fdt_add_subnode(fdt
, 0, "psci");
507 DEBUG((EFI_D_ERROR
,"Error on creating 'psci' node\n"));
508 Status
= EFI_INVALID_PARAMETER
;
511 fdt_setprop_string(fdt
, node
, "compatible", "arm,psci");
512 fdt_setprop_string(fdt
, node
, "method", "smc");
513 fdt_setprop_cell(fdt
, node
, "cpu_suspend", ARM_SMC_ARM_CPU_SUSPEND
);
514 fdt_setprop_cell(fdt
, node
, "cpu_off", ARM_SMC_ARM_CPU_OFF
);
515 fdt_setprop_cell(fdt
, node
, "cpu_on", ARM_SMC_ARM_CPU_ON
);
516 fdt_setprop_cell(fdt
, node
, "cpu_migrate", ARM_SMC_ARM_MIGRATE
);
522 //DebugDumpFdt (fdt);
525 // If we succeeded to generate the new Device Tree then free the old Device Tree
526 gBS
->FreePages (*FdtBlobBase
, EFI_SIZE_TO_PAGES (*FdtBlobSize
));
528 *FdtBlobBase
= NewFdtBlobBase
;
529 *FdtBlobSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(NewFdtBlobBase
));
533 gBS
->FreePages (NewFdtBlobBase
, EFI_SIZE_TO_PAGES (NewFdtBlobSize
));
535 FAIL_ALLOCATE_NEW_FDT
:
536 *FdtBlobSize
= (UINTN
)fdt_totalsize ((VOID
*)(UINTN
)(*FdtBlobBase
));
537 // Return success even if we failed to update the FDT blob. The original one is still valid.