2022-05-31 13:04:11 +03:00
/* SPDX-License-Identifier: GPL-2.0 */
/ *
* Copyright ( C ) 2 0 2 0 - 2 0 2 2 L o o n g s o n T e c h n o l o g y C o r p o r a t i o n L i m i t e d
* /
# include < l i n u x / i n i t . h >
# include < l i n u x / t h r e a d s . h >
# include < a s m / a d d r s p a c e . h >
# include < a s m / a s m . h >
# include < a s m / a s m m a c r o . h >
# include < a s m / r e g d e f . h >
# include < a s m / l o o n g a r c h . h >
# include < a s m / s t a c k f r a m e . h >
efi/loongarch: Add efistub booting support
This patch adds efistub booting support, which is the standard UEFI boot
protocol for LoongArch to use.
We use generic efistub, which means we can pass boot information (i.e.,
system table, memory map, kernel command line, initrd) via a light FDT
and drop a lot of non-standard code.
We use a flat mapping to map the efi runtime in the kernel's address
space. In efi, VA = PA; in kernel, VA = PA + PAGE_OFFSET. As a result,
flat mapping is not identity mapping, SetVirtualAddressMap() is still
needed for the efi runtime.
Tested-by: Xi Ruoyao <xry111@xry111.site>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
[ardb: change fpic to fpie as suggested by Xi Ruoyao]
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-08-19 13:20:37 +03:00
# ifdef C O N F I G _ E F I _ S T U B
# include " e f i - h e a d e r . S "
_ _ HEAD
_head :
.word MZ_MAGIC /* "MZ", MS-DOS header */
.org 0x3c /* 0x04 ~ 0x3b reserved */
.long pe_header - _ head / * O f f s e t t o t h e P E h e a d e r * /
pe_header :
_ _ EFI_ P E _ H E A D E R
SYM_ D A T A ( k e r n e l _ a s i z e , . l o n g _ e n d - _ t e x t ) ;
SYM_ D A T A ( k e r n e l _ f s i z e , . l o n g _ e d a t a - _ t e x t ) ;
SYM_ D A T A ( k e r n e l _ o f f s e t , . l o n g k e r n e l _ o f f s e t - _ t e x t ) ;
# endif
2022-05-31 13:04:11 +03:00
_ _ REF
2022-09-26 17:25:29 +03:00
.align 12
2022-05-31 13:04:11 +03:00
SYM_ C O D E _ S T A R T ( k e r n e l _ e n t r y ) # k e r n e l e n t r y p o i n t
/* Config direct window and set PG */
li. d t 0 , C S R _ D M W 0 _ I N I T # U C , P L V 0 , 0 x80 0 0 x x x x x x x x x x x x
csrwr t 0 , L O O N G A R C H _ C S R _ D M W I N 0
li. d t 0 , C S R _ D M W 1 _ I N I T # C A , P L V 0 , 0 x90 0 0 x x x x x x x x x x x x
csrwr t 0 , L O O N G A R C H _ C S R _ D M W I N 1
2022-08-11 15:52:12 +03:00
/ * We m i g h t n o t g e t l a u n c h e d a t t h e a d d r e s s t h e k e r n e l i s l i n k e d t o ,
so w e j u m p t h e r e . * /
la. a b s t 0 , 0 f
jr t 0
0 :
2022-05-31 13:04:11 +03:00
/* Enable PG */
li. w t 0 , 0 x b0 # P L V = 0 , I E =0 , P G =1
csrwr t 0 , L O O N G A R C H _ C S R _ C R M D
li. w t 0 , 0 x04 # P L V = 0 , P I E =1 , P W E =0
csrwr t 0 , L O O N G A R C H _ C S R _ P R M D
li. w t 0 , 0 x00 # F P E = 0 , S X E =0 , A S X E =0 , B T E =0
csrwr t 0 , L O O N G A R C H _ C S R _ E U E N
la t 0 , _ _ b s s _ s t a r t # c l e a r . b s s
st. d z e r o , t 0 , 0
la t 1 , _ _ b s s _ s t o p - L O N G S I Z E
1 :
addi. d t 0 , t 0 , L O N G S I Z E
st. d z e r o , t 0 , 0
bne t 0 , t 1 , 1 b
la t 0 , f w _ a r g 0
st. d a0 , t 0 , 0 # f i r m w a r e a r g u m e n t s
la t 0 , f w _ a r g 1
st. d a1 , t 0 , 0
efi/loongarch: libstub: remove dependency on flattened DT
LoongArch does not use FDT or DT natively [yet], and the only reason it
currently uses it is so that it can reuse the existing EFI stub code.
Overloading the DT with data passed between the EFI stub and the core
kernel has been a source of problems: there is the overlap between
information provided by EFI which DT can also provide (initrd base/size,
command line, memory descriptions), requiring us to reason about which
is which and what to prioritize. It has also resulted in ABI leaks,
i.e., internal ABI being promoted to external ABI inadvertently because
the bootloader can set the EFI stub's DT properties as well (e.g.,
"kaslr-seed"). This has become especially problematic with boot
environments that want to pretend that EFI boot is being done (to access
ACPI and SMBIOS tables, for instance) but have no ability to execute the
EFI stub, and so the environment that the EFI stub creates is emulated
[poorly, in some cases].
Another downside of treating DT like this is that the DT binary that the
kernel receives is different from the one created by the firmware, which
is undesirable in the context of secure and measured boot.
Given that LoongArch support in Linux is brand new, we can avoid these
pitfalls, and treat the DT strictly as a hardware description, and use a
separate handover method between the EFI stub and the kernel. Now that
initrd loading and passing the EFI memory map have been refactored into
pure EFI routines that use EFI configuration tables, the only thing we
need to pass directly is the kernel command line (even if we could pass
this via a config table as well, it is used extremely early, so passing
it directly is preferred in this case.)
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Huacai Chen <chenhuacai@loongson.cn>
2022-09-16 20:48:53 +03:00
la t 0 , f w _ a r g 2
st. d a2 , t 0 , 0
2022-05-31 13:04:11 +03:00
/* KSave3 used for percpu base, initialized as 0 */
csrwr z e r o , P E R C P U _ B A S E _ K S
/* GPR21 used for percpu base (runtime), initialized as 0 */
2022-07-26 18:57:18 +03:00
move u 0 , z e r o
2022-05-31 13:04:11 +03:00
la t p , i n i t _ t h r e a d _ u n i o n
/* Set the SP after an empty pt_regs. */
PTR_ L I s p , ( _ T H R E A D _ S I Z E - 3 2 - P T _ S I Z E )
PTR_ A D D s p , s p , t p
set_ s a v e d _ s p s p , t 0 , t 1
PTR_ A D D I s p , s p , - 4 * S Z R E G # i n i t s t a c k p o i n t e r
bl s t a r t _ k e r n e l
SYM_ C O D E _ E N D ( k e r n e l _ e n t r y )
2022-05-31 13:04:12 +03:00
# ifdef C O N F I G _ S M P
/ *
* SMP s l a v e c p u s e n t r y p o i n t . B o a r d s p e c i f i c c o d e f o r b o o t s t r a p c a l l s t h i s
* function a f t e r s e t t i n g u p t h e s t a c k a n d t p r e g i s t e r s .
* /
SYM_ C O D E _ S T A R T ( s m p b o o t _ e n t r y )
li. d t 0 , C S R _ D M W 0 _ I N I T # U C , P L V 0
csrwr t 0 , L O O N G A R C H _ C S R _ D M W I N 0
li. d t 0 , C S R _ D M W 1 _ I N I T # C A , P L V 0
csrwr t 0 , L O O N G A R C H _ C S R _ D M W I N 1
2022-08-11 15:52:12 +03:00
la. a b s t 0 , 0 f
jr t 0
0 :
/* Enable PG */
2022-05-31 13:04:12 +03:00
li. w t 0 , 0 x b0 # P L V = 0 , I E =0 , P G =1
csrwr t 0 , L O O N G A R C H _ C S R _ C R M D
li. w t 0 , 0 x04 # P L V = 0 , P I E =1 , P W E =0
csrwr t 0 , L O O N G A R C H _ C S R _ P R M D
li. w t 0 , 0 x00 # F P E = 0 , S X E =0 , A S X E =0 , B T E =0
csrwr t 0 , L O O N G A R C H _ C S R _ E U E N
la. a b s t 0 , c p u b o o t _ d a t a
ld. d s p , t 0 , C P U _ B O O T _ S T A C K
ld. d t p , t 0 , C P U _ B O O T _ T I N F O
bl s t a r t _ s e c o n d a r y
SYM_ C O D E _ E N D ( s m p b o o t _ e n t r y )
# endif / * C O N F I G _ S M P * /
2022-05-31 13:04:11 +03:00
SYM_ E N T R Y ( k e r n e l _ e n t r y _ e n d , S Y M _ L _ G L O B A L , S Y M _ A _ N O N E )