1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
10 static efi_guid_t cpu_state_guid
= LINUX_EFI_ARM_CPU_STATE_TABLE_GUID
;
12 struct efi_arm_entry_state
*efi_entry_state
;
14 static void get_cpu_state(u32
*cpsr
, u32
*sctlr
)
16 asm("mrs %0, cpsr" : "=r"(*cpsr
));
17 if ((*cpsr
& MODE_MASK
) == HYP_MODE
)
18 asm("mrc p15, 4, %0, c1, c0, 0" : "=r"(*sctlr
));
20 asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr
));
23 efi_status_t
check_platform_features(void)
29 get_cpu_state(&cpsr
, &sctlr
);
31 efi_info("Entering in %s mode with MMU %sabled\n",
32 ((cpsr
& MODE_MASK
) == HYP_MODE
) ? "HYP" : "SVC",
33 (sctlr
& 1) ? "en" : "dis");
35 status
= efi_bs_call(allocate_pool
, EFI_LOADER_DATA
,
36 sizeof(*efi_entry_state
),
37 (void **)&efi_entry_state
);
38 if (status
!= EFI_SUCCESS
) {
39 efi_err("allocate_pool() failed\n");
43 efi_entry_state
->cpsr_before_ebs
= cpsr
;
44 efi_entry_state
->sctlr_before_ebs
= sctlr
;
46 status
= efi_bs_call(install_configuration_table
, &cpu_state_guid
,
48 if (status
!= EFI_SUCCESS
) {
49 efi_err("install_configuration_table() failed\n");
53 /* non-LPAE kernels can run anywhere */
54 if (!IS_ENABLED(CONFIG_ARM_LPAE
))
57 /* LPAE kernels need compatible hardware */
58 block
= cpuid_feature_extract(CPUID_EXT_MMFR0
, 0);
60 efi_err("This LPAE kernel is not supported by your CPU\n");
61 status
= EFI_UNSUPPORTED
;
67 efi_bs_call(install_configuration_table
, &cpu_state_guid
, NULL
);
69 efi_bs_call(free_pool
, efi_entry_state
);
73 void efi_handle_post_ebs_state(void)
75 get_cpu_state(&efi_entry_state
->cpsr_after_ebs
,
76 &efi_entry_state
->sctlr_after_ebs
);
79 efi_status_t
handle_kernel_image(unsigned long *image_addr
,
80 unsigned long *image_size
,
81 unsigned long *reserve_addr
,
82 unsigned long *reserve_size
,
83 efi_loaded_image_t
*image
,
84 efi_handle_t image_handle
)
86 const int slack
= TEXT_OFFSET
- 5 * PAGE_SIZE
;
87 int alloc_size
= MAX_UNCOMP_KERNEL_SIZE
+ EFI_PHYS_ALIGN
;
88 unsigned long alloc_base
, kernel_base
;
92 * Allocate space for the decompressed kernel as low as possible.
93 * The region should be 16 MiB aligned, but the first 'slack' bytes
94 * are not used by Linux, so we allow those to be occupied by the
97 status
= efi_low_alloc_above(alloc_size
, EFI_PAGE_SIZE
, &alloc_base
, 0x0);
98 if (status
!= EFI_SUCCESS
) {
99 efi_err("Unable to allocate memory for uncompressed kernel.\n");
103 if ((alloc_base
% EFI_PHYS_ALIGN
) > slack
) {
105 * More than 'slack' bytes are already occupied at the base of
106 * the allocation, so we need to advance to the next 16 MiB block.
108 kernel_base
= round_up(alloc_base
, EFI_PHYS_ALIGN
);
109 efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n",
110 alloc_base
, kernel_base
);
112 kernel_base
= round_down(alloc_base
, EFI_PHYS_ALIGN
);
115 *reserve_addr
= kernel_base
+ slack
;
116 *reserve_size
= MAX_UNCOMP_KERNEL_SIZE
;
118 /* now free the parts that we will not use */
119 if (*reserve_addr
> alloc_base
) {
120 efi_bs_call(free_pages
, alloc_base
,
121 (*reserve_addr
- alloc_base
) / EFI_PAGE_SIZE
);
122 alloc_size
-= *reserve_addr
- alloc_base
;
124 efi_bs_call(free_pages
, *reserve_addr
+ MAX_UNCOMP_KERNEL_SIZE
,
125 (alloc_size
- MAX_UNCOMP_KERNEL_SIZE
) / EFI_PAGE_SIZE
);
127 *image_addr
= kernel_base
+ TEXT_OFFSET
;
130 efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n",
131 *image_addr
, *reserve_addr
);