1*d729b554SArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2*d729b554SArd Biesheuvel /*
3*d729b554SArd Biesheuvel  * Author: Yun Liu <liuyun@loongson.cn>
4*d729b554SArd Biesheuvel  *         Huacai Chen <chenhuacai@loongson.cn>
5*d729b554SArd Biesheuvel  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6*d729b554SArd Biesheuvel  */
7*d729b554SArd Biesheuvel 
8*d729b554SArd Biesheuvel #include <asm/efi.h>
9*d729b554SArd Biesheuvel #include <asm/addrspace.h>
10*d729b554SArd Biesheuvel #include "efistub.h"
11*d729b554SArd Biesheuvel 
12*d729b554SArd Biesheuvel typedef void __noreturn (*kernel_entry_t)(bool efi, unsigned long cmdline,
13*d729b554SArd Biesheuvel 					  unsigned long systab);
14*d729b554SArd Biesheuvel 
15*d729b554SArd Biesheuvel efi_status_t check_platform_features(void)
16*d729b554SArd Biesheuvel {
17*d729b554SArd Biesheuvel 	return EFI_SUCCESS;
18*d729b554SArd Biesheuvel }
19*d729b554SArd Biesheuvel 
20*d729b554SArd Biesheuvel struct exit_boot_struct {
21*d729b554SArd Biesheuvel 	efi_memory_desc_t	*runtime_map;
22*d729b554SArd Biesheuvel 	int			runtime_entry_count;
23*d729b554SArd Biesheuvel };
24*d729b554SArd Biesheuvel 
25*d729b554SArd Biesheuvel static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
26*d729b554SArd Biesheuvel {
27*d729b554SArd Biesheuvel 	struct exit_boot_struct *p = priv;
28*d729b554SArd Biesheuvel 
29*d729b554SArd Biesheuvel 	/*
30*d729b554SArd Biesheuvel 	 * Update the memory map with virtual addresses. The function will also
31*d729b554SArd Biesheuvel 	 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
32*d729b554SArd Biesheuvel 	 * entries so that we can pass it straight to SetVirtualAddressMap()
33*d729b554SArd Biesheuvel 	 */
34*d729b554SArd Biesheuvel 	efi_get_virtmap(map->map, map->map_size, map->desc_size,
35*d729b554SArd Biesheuvel 			p->runtime_map, &p->runtime_entry_count);
36*d729b554SArd Biesheuvel 
37*d729b554SArd Biesheuvel 	return EFI_SUCCESS;
38*d729b554SArd Biesheuvel }
39*d729b554SArd Biesheuvel 
40*d729b554SArd Biesheuvel unsigned long __weak kernel_entry_address(void)
41*d729b554SArd Biesheuvel {
42*d729b554SArd Biesheuvel 	return *(unsigned long *)(PHYSADDR(VMLINUX_LOAD_ADDRESS) + 8);
43*d729b554SArd Biesheuvel }
44*d729b554SArd Biesheuvel 
45*d729b554SArd Biesheuvel efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
46*d729b554SArd Biesheuvel 			     unsigned long kernel_addr, char *cmdline_ptr)
47*d729b554SArd Biesheuvel {
48*d729b554SArd Biesheuvel 	kernel_entry_t real_kernel_entry;
49*d729b554SArd Biesheuvel 	struct exit_boot_struct priv;
50*d729b554SArd Biesheuvel 	unsigned long desc_size;
51*d729b554SArd Biesheuvel 	efi_status_t status;
52*d729b554SArd Biesheuvel 	u32 desc_ver;
53*d729b554SArd Biesheuvel 
54*d729b554SArd Biesheuvel 	status = efi_alloc_virtmap(&priv.runtime_map, &desc_size, &desc_ver);
55*d729b554SArd Biesheuvel 	if (status != EFI_SUCCESS) {
56*d729b554SArd Biesheuvel 		efi_err("Unable to retrieve UEFI memory map.\n");
57*d729b554SArd Biesheuvel 		return status;
58*d729b554SArd Biesheuvel 	}
59*d729b554SArd Biesheuvel 
60*d729b554SArd Biesheuvel 	efi_info("Exiting boot services\n");
61*d729b554SArd Biesheuvel 
62*d729b554SArd Biesheuvel 	efi_novamap = false;
63*d729b554SArd Biesheuvel 	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
64*d729b554SArd Biesheuvel 	if (status != EFI_SUCCESS)
65*d729b554SArd Biesheuvel 		return status;
66*d729b554SArd Biesheuvel 
67*d729b554SArd Biesheuvel 	/* Install the new virtual address map */
68*d729b554SArd Biesheuvel 	efi_rt_call(set_virtual_address_map,
69*d729b554SArd Biesheuvel 		    priv.runtime_entry_count * desc_size, desc_size,
70*d729b554SArd Biesheuvel 		    desc_ver, priv.runtime_map);
71*d729b554SArd Biesheuvel 
72*d729b554SArd Biesheuvel 	/* Config Direct Mapping */
73*d729b554SArd Biesheuvel 	csr_write64(CSR_DMW0_INIT, LOONGARCH_CSR_DMWIN0);
74*d729b554SArd Biesheuvel 	csr_write64(CSR_DMW1_INIT, LOONGARCH_CSR_DMWIN1);
75*d729b554SArd Biesheuvel 
76*d729b554SArd Biesheuvel 	real_kernel_entry = (void *)kernel_entry_address();
77*d729b554SArd Biesheuvel 
78*d729b554SArd Biesheuvel 	real_kernel_entry(true, (unsigned long)cmdline_ptr,
79*d729b554SArd Biesheuvel 			  (unsigned long)efi_system_table);
80*d729b554SArd Biesheuvel }
81