14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
281a0bc39SRoy Franz /*
381a0bc39SRoy Franz  * Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
481a0bc39SRoy Franz  */
581a0bc39SRoy Franz #include <linux/efi.h>
681a0bc39SRoy Franz #include <asm/efi.h>
781a0bc39SRoy Franz 
8eeff7d63SArd Biesheuvel #include "efistub.h"
9eeff7d63SArd Biesheuvel 
102a55280aSArd Biesheuvel static efi_guid_t cpu_state_guid = LINUX_EFI_ARM_CPU_STATE_TABLE_GUID;
112a55280aSArd Biesheuvel 
122a55280aSArd Biesheuvel struct efi_arm_entry_state *efi_entry_state;
132a55280aSArd Biesheuvel 
get_cpu_state(u32 * cpsr,u32 * sctlr)142a55280aSArd Biesheuvel static void get_cpu_state(u32 *cpsr, u32 *sctlr)
152a55280aSArd Biesheuvel {
162a55280aSArd Biesheuvel 	asm("mrs %0, cpsr" : "=r"(*cpsr));
172a55280aSArd Biesheuvel 	if ((*cpsr & MODE_MASK) == HYP_MODE)
182a55280aSArd Biesheuvel 		asm("mrc p15, 4, %0, c1, c0, 0" : "=r"(*sctlr));
192a55280aSArd Biesheuvel 	else
202a55280aSArd Biesheuvel 		asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr));
212a55280aSArd Biesheuvel }
222a55280aSArd Biesheuvel 
check_platform_features(void)23cd33a5c1SArd Biesheuvel efi_status_t check_platform_features(void)
242ec0f0a3SArd Biesheuvel {
252a55280aSArd Biesheuvel 	efi_status_t status;
262a55280aSArd Biesheuvel 	u32 cpsr, sctlr;
272ec0f0a3SArd Biesheuvel 	int block;
282ec0f0a3SArd Biesheuvel 
292a55280aSArd Biesheuvel 	get_cpu_state(&cpsr, &sctlr);
302a55280aSArd Biesheuvel 
312a55280aSArd Biesheuvel 	efi_info("Entering in %s mode with MMU %sabled\n",
322a55280aSArd Biesheuvel 		 ((cpsr & MODE_MASK) == HYP_MODE) ? "HYP" : "SVC",
332a55280aSArd Biesheuvel 		 (sctlr & 1) ? "en" : "dis");
342a55280aSArd Biesheuvel 
352a55280aSArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
362a55280aSArd Biesheuvel 			     sizeof(*efi_entry_state),
372a55280aSArd Biesheuvel 			     (void **)&efi_entry_state);
382a55280aSArd Biesheuvel 	if (status != EFI_SUCCESS) {
392a55280aSArd Biesheuvel 		efi_err("allocate_pool() failed\n");
402a55280aSArd Biesheuvel 		return status;
412a55280aSArd Biesheuvel 	}
422a55280aSArd Biesheuvel 
432a55280aSArd Biesheuvel 	efi_entry_state->cpsr_before_ebs = cpsr;
442a55280aSArd Biesheuvel 	efi_entry_state->sctlr_before_ebs = sctlr;
452a55280aSArd Biesheuvel 
462a55280aSArd Biesheuvel 	status = efi_bs_call(install_configuration_table, &cpu_state_guid,
472a55280aSArd Biesheuvel 			     efi_entry_state);
482a55280aSArd Biesheuvel 	if (status != EFI_SUCCESS) {
492a55280aSArd Biesheuvel 		efi_err("install_configuration_table() failed\n");
502a55280aSArd Biesheuvel 		goto free_state;
512a55280aSArd Biesheuvel 	}
522a55280aSArd Biesheuvel 
532ec0f0a3SArd Biesheuvel 	/* non-LPAE kernels can run anywhere */
542ec0f0a3SArd Biesheuvel 	if (!IS_ENABLED(CONFIG_ARM_LPAE))
552ec0f0a3SArd Biesheuvel 		return EFI_SUCCESS;
562ec0f0a3SArd Biesheuvel 
572ec0f0a3SArd Biesheuvel 	/* LPAE kernels need compatible hardware */
582ec0f0a3SArd Biesheuvel 	block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
592ec0f0a3SArd Biesheuvel 	if (block < 5) {
60793473c2SArvind Sankar 		efi_err("This LPAE kernel is not supported by your CPU\n");
612a55280aSArd Biesheuvel 		status = EFI_UNSUPPORTED;
622a55280aSArd Biesheuvel 		goto drop_table;
632ec0f0a3SArd Biesheuvel 	}
642ec0f0a3SArd Biesheuvel 	return EFI_SUCCESS;
652a55280aSArd Biesheuvel 
662a55280aSArd Biesheuvel drop_table:
672a55280aSArd Biesheuvel 	efi_bs_call(install_configuration_table, &cpu_state_guid, NULL);
682a55280aSArd Biesheuvel free_state:
692a55280aSArd Biesheuvel 	efi_bs_call(free_pool, efi_entry_state);
702a55280aSArd Biesheuvel 	return status;
712a55280aSArd Biesheuvel }
722a55280aSArd Biesheuvel 
efi_handle_post_ebs_state(void)732a55280aSArd Biesheuvel void efi_handle_post_ebs_state(void)
742a55280aSArd Biesheuvel {
752a55280aSArd Biesheuvel 	get_cpu_state(&efi_entry_state->cpsr_after_ebs,
762a55280aSArd Biesheuvel 		      &efi_entry_state->sctlr_after_ebs);
772ec0f0a3SArd Biesheuvel }
782ec0f0a3SArd Biesheuvel 
handle_kernel_image(unsigned long * image_addr,unsigned long * image_size,unsigned long * reserve_addr,unsigned long * reserve_size,efi_loaded_image_t * image,efi_handle_t image_handle)79cd33a5c1SArd Biesheuvel efi_status_t handle_kernel_image(unsigned long *image_addr,
80318532bfSArd Biesheuvel 				 unsigned long *image_size,
81318532bfSArd Biesheuvel 				 unsigned long *reserve_addr,
82318532bfSArd Biesheuvel 				 unsigned long *reserve_size,
83*416a9f84SArd Biesheuvel 				 efi_loaded_image_t *image,
84*416a9f84SArd Biesheuvel 				 efi_handle_t image_handle)
85318532bfSArd Biesheuvel {
86762cd288SArd Biesheuvel 	const int slack = TEXT_OFFSET - 5 * PAGE_SIZE;
87762cd288SArd Biesheuvel 	int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN;
88762cd288SArd Biesheuvel 	unsigned long alloc_base, kernel_base;
89318532bfSArd Biesheuvel 	efi_status_t status;
90318532bfSArd Biesheuvel 
9141cd96faSArd Biesheuvel 	/*
92762cd288SArd Biesheuvel 	 * Allocate space for the decompressed kernel as low as possible.
93762cd288SArd Biesheuvel 	 * The region should be 16 MiB aligned, but the first 'slack' bytes
94762cd288SArd Biesheuvel 	 * are not used by Linux, so we allow those to be occupied by the
95762cd288SArd Biesheuvel 	 * firmware.
9641cd96faSArd Biesheuvel 	 */
97762cd288SArd Biesheuvel 	status = efi_low_alloc_above(alloc_size, EFI_PAGE_SIZE, &alloc_base, 0x0);
98318532bfSArd Biesheuvel 	if (status != EFI_SUCCESS) {
99793473c2SArvind Sankar 		efi_err("Unable to allocate memory for uncompressed kernel.\n");
10081a0bc39SRoy Franz 		return status;
10181a0bc39SRoy Franz 	}
10281a0bc39SRoy Franz 
103762cd288SArd Biesheuvel 	if ((alloc_base % EFI_PHYS_ALIGN) > slack) {
104762cd288SArd Biesheuvel 		/*
105762cd288SArd Biesheuvel 		 * More than 'slack' bytes are already occupied at the base of
106762cd288SArd Biesheuvel 		 * the allocation, so we need to advance to the next 16 MiB block.
107762cd288SArd Biesheuvel 		 */
108762cd288SArd Biesheuvel 		kernel_base = round_up(alloc_base, EFI_PHYS_ALIGN);
109762cd288SArd Biesheuvel 		efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n",
110762cd288SArd Biesheuvel 			 alloc_base, kernel_base);
111762cd288SArd Biesheuvel 	} else {
112762cd288SArd Biesheuvel 		kernel_base = round_down(alloc_base, EFI_PHYS_ALIGN);
113762cd288SArd Biesheuvel 	}
114762cd288SArd Biesheuvel 
115762cd288SArd Biesheuvel 	*reserve_addr = kernel_base + slack;
116762cd288SArd Biesheuvel 	*reserve_size = MAX_UNCOMP_KERNEL_SIZE;
117762cd288SArd Biesheuvel 
118762cd288SArd Biesheuvel 	/* now free the parts that we will not use */
119762cd288SArd Biesheuvel 	if (*reserve_addr > alloc_base) {
120762cd288SArd Biesheuvel 		efi_bs_call(free_pages, alloc_base,
121762cd288SArd Biesheuvel 			    (*reserve_addr - alloc_base) / EFI_PAGE_SIZE);
122762cd288SArd Biesheuvel 		alloc_size -= *reserve_addr - alloc_base;
123762cd288SArd Biesheuvel 	}
124762cd288SArd Biesheuvel 	efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE,
125762cd288SArd Biesheuvel 		    (alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE);
126762cd288SArd Biesheuvel 
127762cd288SArd Biesheuvel 	*image_addr = kernel_base + TEXT_OFFSET;
12881a0bc39SRoy Franz 	*image_size = 0;
129762cd288SArd Biesheuvel 
130762cd288SArd Biesheuvel 	efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n",
131762cd288SArd Biesheuvel 		  *image_addr, *reserve_addr);
132762cd288SArd Biesheuvel 
13381a0bc39SRoy Franz 	return EFI_SUCCESS;
13481a0bc39SRoy Franz }
135