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