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