xref: /linux/drivers/firmware/efi/libstub/loongarch.c (revision adc1e5c6203cf13fe05a1ead08edcb3d3a3baae8)
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 #include "loongarch-stub.h"
12 
13 typedef void __noreturn (*kernel_entry_t)(bool efi, unsigned long cmdline,
14 					  unsigned long systab);
15 
check_platform_features(void)16 efi_status_t check_platform_features(void)
17 {
18 	return EFI_SUCCESS;
19 }
20 
efi_cache_sync_image(unsigned long image_base,unsigned long alloc_size)21 void efi_cache_sync_image(unsigned long image_base, unsigned long alloc_size)
22 {
23 	asm volatile ("ibar 0" ::: "memory");
24 }
25 
26 struct exit_boot_struct {
27 	efi_memory_desc_t	*runtime_map;
28 	int			runtime_entry_count;
29 };
30 
exit_boot_func(struct efi_boot_memmap * map,void * priv)31 static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
32 {
33 	struct exit_boot_struct *p = priv;
34 
35 	/*
36 	 * Update the memory map with virtual addresses. The function will also
37 	 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
38 	 * entries so that we can pass it straight to SetVirtualAddressMap()
39 	 */
40 	efi_get_virtmap(map->map, map->map_size, map->desc_size,
41 			p->runtime_map, &p->runtime_entry_count);
42 
43 	return EFI_SUCCESS;
44 }
45 
kernel_entry_address(unsigned long kernel_addr,efi_loaded_image_t * image)46 unsigned long __weak kernel_entry_address(unsigned long kernel_addr,
47 		efi_loaded_image_t *image)
48 {
49 	return *(unsigned long *)(kernel_addr + 8) - PHYSADDR(VMLINUX_LOAD_ADDRESS) + kernel_addr;
50 }
51 
efi_boot_kernel(void * handle,efi_loaded_image_t * image,unsigned long kernel_addr,char * cmdline_ptr)52 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
53 			     unsigned long kernel_addr, char *cmdline_ptr)
54 {
55 	kernel_entry_t real_kernel_entry;
56 	struct exit_boot_struct priv;
57 	unsigned long desc_size;
58 	efi_status_t status;
59 	u32 desc_ver;
60 
61 	status = efi_alloc_virtmap(&priv.runtime_map, &desc_size, &desc_ver);
62 	if (status != EFI_SUCCESS) {
63 		efi_err("Unable to retrieve UEFI memory map.\n");
64 		return status;
65 	}
66 
67 	efi_info("Exiting boot services\n");
68 
69 	efi_novamap = false;
70 	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
71 	if (status != EFI_SUCCESS)
72 		return status;
73 
74 	/* Install the new virtual address map */
75 	efi_rt_call(set_virtual_address_map,
76 		    priv.runtime_entry_count * desc_size, desc_size,
77 		    desc_ver, priv.runtime_map);
78 
79 	/* Config Direct Mapping */
80 	csr_write(CSR_DMW0_INIT, LOONGARCH_CSR_DMWIN0);
81 	csr_write(CSR_DMW1_INIT, LOONGARCH_CSR_DMWIN1);
82 	csr_write(CSR_DMW2_INIT, LOONGARCH_CSR_DMWIN2);
83 	csr_write(CSR_DMW3_INIT, LOONGARCH_CSR_DMWIN3);
84 
85 	real_kernel_entry = (void *)kernel_entry_address(kernel_addr, image);
86 
87 	real_kernel_entry(true, (unsigned long)cmdline_ptr,
88 			  (unsigned long)efi_system_table);
89 }
90