xref: /OK3568_Linux_fs/kernel/drivers/firmware/efi/libstub/arm32-stub.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #include <linux/efi.h>
6*4882a593Smuzhiyun #include <asm/efi.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include "efistub.h"
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun static efi_guid_t cpu_state_guid = LINUX_EFI_ARM_CPU_STATE_TABLE_GUID;
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun struct efi_arm_entry_state *efi_entry_state;
13*4882a593Smuzhiyun 
get_cpu_state(u32 * cpsr,u32 * sctlr)14*4882a593Smuzhiyun static void get_cpu_state(u32 *cpsr, u32 *sctlr)
15*4882a593Smuzhiyun {
16*4882a593Smuzhiyun 	asm("mrs %0, cpsr" : "=r"(*cpsr));
17*4882a593Smuzhiyun 	if ((*cpsr & MODE_MASK) == HYP_MODE)
18*4882a593Smuzhiyun 		asm("mrc p15, 4, %0, c1, c0, 0" : "=r"(*sctlr));
19*4882a593Smuzhiyun 	else
20*4882a593Smuzhiyun 		asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr));
21*4882a593Smuzhiyun }
22*4882a593Smuzhiyun 
check_platform_features(void)23*4882a593Smuzhiyun efi_status_t check_platform_features(void)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	efi_status_t status;
26*4882a593Smuzhiyun 	u32 cpsr, sctlr;
27*4882a593Smuzhiyun 	int block;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	get_cpu_state(&cpsr, &sctlr);
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	efi_info("Entering in %s mode with MMU %sabled\n",
32*4882a593Smuzhiyun 		 ((cpsr & MODE_MASK) == HYP_MODE) ? "HYP" : "SVC",
33*4882a593Smuzhiyun 		 (sctlr & 1) ? "en" : "dis");
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
36*4882a593Smuzhiyun 			     sizeof(*efi_entry_state),
37*4882a593Smuzhiyun 			     (void **)&efi_entry_state);
38*4882a593Smuzhiyun 	if (status != EFI_SUCCESS) {
39*4882a593Smuzhiyun 		efi_err("allocate_pool() failed\n");
40*4882a593Smuzhiyun 		return status;
41*4882a593Smuzhiyun 	}
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	efi_entry_state->cpsr_before_ebs = cpsr;
44*4882a593Smuzhiyun 	efi_entry_state->sctlr_before_ebs = sctlr;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	status = efi_bs_call(install_configuration_table, &cpu_state_guid,
47*4882a593Smuzhiyun 			     efi_entry_state);
48*4882a593Smuzhiyun 	if (status != EFI_SUCCESS) {
49*4882a593Smuzhiyun 		efi_err("install_configuration_table() failed\n");
50*4882a593Smuzhiyun 		goto free_state;
51*4882a593Smuzhiyun 	}
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* non-LPAE kernels can run anywhere */
54*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_ARM_LPAE))
55*4882a593Smuzhiyun 		return EFI_SUCCESS;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* LPAE kernels need compatible hardware */
58*4882a593Smuzhiyun 	block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
59*4882a593Smuzhiyun 	if (block < 5) {
60*4882a593Smuzhiyun 		efi_err("This LPAE kernel is not supported by your CPU\n");
61*4882a593Smuzhiyun 		status = EFI_UNSUPPORTED;
62*4882a593Smuzhiyun 		goto drop_table;
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 	return EFI_SUCCESS;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun drop_table:
67*4882a593Smuzhiyun 	efi_bs_call(install_configuration_table, &cpu_state_guid, NULL);
68*4882a593Smuzhiyun free_state:
69*4882a593Smuzhiyun 	efi_bs_call(free_pool, efi_entry_state);
70*4882a593Smuzhiyun 	return status;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
efi_handle_post_ebs_state(void)73*4882a593Smuzhiyun void efi_handle_post_ebs_state(void)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	get_cpu_state(&efi_entry_state->cpsr_after_ebs,
76*4882a593Smuzhiyun 		      &efi_entry_state->sctlr_after_ebs);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
80*4882a593Smuzhiyun 
alloc_screen_info(void)81*4882a593Smuzhiyun struct screen_info *alloc_screen_info(void)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	struct screen_info *si;
84*4882a593Smuzhiyun 	efi_status_t status;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/*
87*4882a593Smuzhiyun 	 * Unlike on arm64, where we can directly fill out the screen_info
88*4882a593Smuzhiyun 	 * structure from the stub, we need to allocate a buffer to hold
89*4882a593Smuzhiyun 	 * its contents while we hand over to the kernel proper from the
90*4882a593Smuzhiyun 	 * decompressor.
91*4882a593Smuzhiyun 	 */
92*4882a593Smuzhiyun 	status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
93*4882a593Smuzhiyun 			     sizeof(*si), (void **)&si);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
96*4882a593Smuzhiyun 		return NULL;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	status = efi_bs_call(install_configuration_table,
99*4882a593Smuzhiyun 			     &screen_info_guid, si);
100*4882a593Smuzhiyun 	if (status == EFI_SUCCESS)
101*4882a593Smuzhiyun 		return si;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	efi_bs_call(free_pool, si);
104*4882a593Smuzhiyun 	return NULL;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
free_screen_info(struct screen_info * si)107*4882a593Smuzhiyun void free_screen_info(struct screen_info *si)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	if (!si)
110*4882a593Smuzhiyun 		return;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	efi_bs_call(install_configuration_table, &screen_info_guid, NULL);
113*4882a593Smuzhiyun 	efi_bs_call(free_pool, si);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
handle_kernel_image(unsigned long * image_addr,unsigned long * image_size,unsigned long * reserve_addr,unsigned long * reserve_size,efi_loaded_image_t * image)116*4882a593Smuzhiyun efi_status_t handle_kernel_image(unsigned long *image_addr,
117*4882a593Smuzhiyun 				 unsigned long *image_size,
118*4882a593Smuzhiyun 				 unsigned long *reserve_addr,
119*4882a593Smuzhiyun 				 unsigned long *reserve_size,
120*4882a593Smuzhiyun 				 efi_loaded_image_t *image)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	const int slack = TEXT_OFFSET - 5 * PAGE_SIZE;
123*4882a593Smuzhiyun 	int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN;
124*4882a593Smuzhiyun 	unsigned long alloc_base, kernel_base;
125*4882a593Smuzhiyun 	efi_status_t status;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	/*
128*4882a593Smuzhiyun 	 * Allocate space for the decompressed kernel as low as possible.
129*4882a593Smuzhiyun 	 * The region should be 16 MiB aligned, but the first 'slack' bytes
130*4882a593Smuzhiyun 	 * are not used by Linux, so we allow those to be occupied by the
131*4882a593Smuzhiyun 	 * firmware.
132*4882a593Smuzhiyun 	 */
133*4882a593Smuzhiyun 	status = efi_low_alloc_above(alloc_size, EFI_PAGE_SIZE, &alloc_base, 0x0);
134*4882a593Smuzhiyun 	if (status != EFI_SUCCESS) {
135*4882a593Smuzhiyun 		efi_err("Unable to allocate memory for uncompressed kernel.\n");
136*4882a593Smuzhiyun 		return status;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if ((alloc_base % EFI_PHYS_ALIGN) > slack) {
140*4882a593Smuzhiyun 		/*
141*4882a593Smuzhiyun 		 * More than 'slack' bytes are already occupied at the base of
142*4882a593Smuzhiyun 		 * the allocation, so we need to advance to the next 16 MiB block.
143*4882a593Smuzhiyun 		 */
144*4882a593Smuzhiyun 		kernel_base = round_up(alloc_base, EFI_PHYS_ALIGN);
145*4882a593Smuzhiyun 		efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n",
146*4882a593Smuzhiyun 			 alloc_base, kernel_base);
147*4882a593Smuzhiyun 	} else {
148*4882a593Smuzhiyun 		kernel_base = round_down(alloc_base, EFI_PHYS_ALIGN);
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	*reserve_addr = kernel_base + slack;
152*4882a593Smuzhiyun 	*reserve_size = MAX_UNCOMP_KERNEL_SIZE;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/* now free the parts that we will not use */
155*4882a593Smuzhiyun 	if (*reserve_addr > alloc_base) {
156*4882a593Smuzhiyun 		efi_bs_call(free_pages, alloc_base,
157*4882a593Smuzhiyun 			    (*reserve_addr - alloc_base) / EFI_PAGE_SIZE);
158*4882a593Smuzhiyun 		alloc_size -= *reserve_addr - alloc_base;
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun 	efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE,
161*4882a593Smuzhiyun 		    (alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	*image_addr = kernel_base + TEXT_OFFSET;
164*4882a593Smuzhiyun 	*image_size = 0;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n",
167*4882a593Smuzhiyun 		  *image_addr, *reserve_addr);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return EFI_SUCCESS;
170*4882a593Smuzhiyun }
171