efi/loongarch: Change MMU translation mode
PATCH 1/1 efi/loongarch: Change MMU translation mode
From: Wang Yao <[email protected]>
Refer ot UEFI spec v2.10 section 2.3.8 LoongArch Platforms:
The processor is in the following execution mode during boot service:
...
The memory is in physical addressing mode. LoongArch architecture defines two memory access mode, namely direct address translation mode and mapped address translation mode.
So need to change MMU translation mode before config direct mapping.
--- drivers/firmware/efi/libstub/loongarch.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/loongarch.c +7 -1
@@ -49,7 +49,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,struct exit_boot_struct priv;unsigned long desc_size;efi_status_t status; -u32 desc_ver; +u32 desc_ver, val; status =efi_alloc_virtmap (&priv.runtime_map, &desc_size, &desc_ver);if (status !=EFI_SUCCESS ) {
@@ -69,6 +69,12 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, priv.runtime_entry_count * desc_size, desc_size, desc_ver, priv.runtime_map); +/* Change address translation mode */ + val =csr_read32 (LOONGARCH_CSR_CRMD ); + val &= ~CSR_CRMD_DA ; + val |=CSR_CRMD_PG ; +csr_write32 (val,LOONGARCH_CSR_CRMD ); +/* Config Direct Mapping */ csr_write64 (CSR_DMW0_INIT ,LOONGARCH_CSR_DMWIN0 );csr_write64 (CSR_DMW1_INIT ,LOONGARCH_CSR_DMWIN1 );
Signature · 1 line
2.27.0
Hi, Yao,
Quoted 5 lines · On Wed, Nov 22, 2023 at 4:50 PM <[email protected]> wrote:
From: Wang Yao <[email protected]>
Refer ot UEFI spec v2.10 section 2.3.8 LoongArch Platforms:
Quoted 7 lines
The processor is in the following execution mode during boot service:
...
The memory is in physical addressing mode. LoongArch architecture defines two memory access mode, namely direct address translation mode and mapped address translation mode.So need to change MMU translation mode before config direct mapping.
From UEFI spec v2.10 section 2.3.8, you can also see "MMU enabled", so PG is already enabled in UEFI.
Huacai
Quoted 35 lines
Signed-off-by Wang Yao <[email protected]>--- drivers/firmware/efi/libstub/loongarch.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/firmware/efi/libstub/loongarch.c +7
-2@@ -49,7 +49,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,struct exit_boot_struct priv;unsigned long desc_size;efi_status_t status; -u32 desc_ver; +u32 desc_ver, val; status =efi_alloc_virtmap (&priv.runtime_map, &desc_size, &desc_ver);if (status !=EFI_SUCCESS ) {@@ -69,6 +69,12 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, priv.runtime_entry_count * desc_size, desc_size, desc_ver, priv.runtime_map); +/* Change address translation mode */ + val =csr_read32 (LOONGARCH_CSR_CRMD ); + val &= ~CSR_CRMD_DA ; + val |=CSR_CRMD_PG ; +csr_write32 (val,LOONGARCH_CSR_CRMD ); +/* Config Direct Mapping */ csr_write64 (CSR_DMW0_INIT ,LOONGARCH_CSR_DMWIN0 );csr_write64 (CSR_DMW1_INIT ,LOONGARCH_CSR_DMWIN1 ); --2.27.0
Quoted 15 lines · Huacai Chen <[email protected]> 于2023年11月22日周三 22:22写道:
Hi, Yao,
Quoted 11 lines · On Wed, Nov 22, 2023 at 4:50 PM <[email protected]> wrote:
From: Wang Yao <[email protected]>
Refer ot UEFI spec v2.10 section 2.3.8 LoongArch Platforms:
The processor is in the following execution mode during boot service:
...
The memory is in physical addressing mode. LoongArch architecture defines two memory access mode, namely direct address translation mode and mapped address translation mode.
Quoted 6 lines
Quoted 1 line
So need to change MMU translation mode before config direct mapping.
From UEFI spec v2.10 section 2.3.8, you can also see "MMU enabled", so PG is already enabled in UEFI.
Huacai
Hi,Huacai,
From LoongArch-Vol1-v1.10-EN.pdf , DA or PG both refer to a mode enabled by MMU, Will the UEFI firmware not use DA mode?
The UEFI spec did not specify the use of DA or PG mode, but the kernel must use PG.
Best regards,
Ainux Wang.
Quoted 35 lines
Quoted 35 lines
Signed-off-by Wang Yao <[email protected]>--- drivers/firmware/efi/libstub/loongarch.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/firmware/efi/libstub/loongarch.c +7
-2@@ -49,7 +49,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,struct exit_boot_struct priv;unsigned long desc_size;efi_status_t status; -u32 desc_ver; +u32 desc_ver, val; status =efi_alloc_virtmap (&priv.runtime_map, &desc_size, &desc_ver);if (status !=EFI_SUCCESS ) {@@ -69,6 +69,12 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, priv.runtime_entry_count * desc_size, desc_size, desc_ver, priv.runtime_map); +/* Change address translation mode */ + val =csr_read32 (LOONGARCH_CSR_CRMD ); + val &= ~CSR_CRMD_DA ; + val |=CSR_CRMD_PG ; +csr_write32 (val,LOONGARCH_CSR_CRMD ); +/* Config Direct Mapping */ csr_write64 (CSR_DMW0_INIT ,LOONGARCH_CSR_DMWIN0 );csr_write64 (CSR_DMW1_INIT ,LOONGARCH_CSR_DMWIN1 ); --2.27.0
Quoted 21 lines · Ainux Wang
Quoted 19 lines · Huacai Chen <[email protected]> 于2023年11月22日周三 22:22写道:
Hi, Yao,
Quoted 5 lines · On Wed, Nov 22, 2023 at 4:50 PM <[email protected]> wrote:
From: Wang Yao <[email protected]>
Refer ot UEFI spec v2.10 section 2.3.8 LoongArch Platforms:
Quoted 7 lines
The processor is in the following execution mode during boot service:
...
The memory is in physical addressing mode. LoongArch architecture defines two memory access mode, namely direct address translation mode and mapped address translation mode.So need to change MMU translation mode before config direct mapping.
From UEFI spec v2.10 section 2.3.8, you can also see "MMU enabled", so PG is already enabled in UEFI.
Quoted 8 lines
Quoted 2 lines
Huacai
Hi,Huacai,
From LoongArch-Vol1-v1.10-EN.pdf , DA or PG both refer to a mode enabled by MMU, Will the UEFI firmware not use DA mode?
The UEFI spec did not specify the use of DA or PG mode, but the kernel must use PG.
In theory you are right, but "MMU" usually means "TLB", including this case. EFI runtime needs TLB, so UEFI cannot be in DA mode.
Huacai
Quoted 38 lines
Best regards,
Ainux Wang.Quoted 35 lines
Quoted 35 lines
Signed-off-by Wang Yao <[email protected]>--- drivers/firmware/efi/libstub/loongarch.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/firmware/efi/libstub/loongarch.c +7
-2@@ -49,7 +49,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,struct exit_boot_struct priv;unsigned long desc_size;efi_status_t status; -u32 desc_ver; +u32 desc_ver, val; status =efi_alloc_virtmap (&priv.runtime_map, &desc_size, &desc_ver);if (status !=EFI_SUCCESS ) {@@ -69,6 +69,12 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, priv.runtime_entry_count * desc_size, desc_size, desc_ver, priv.runtime_map); +/* Change address translation mode */ + val =csr_read32 (LOONGARCH_CSR_CRMD ); + val &= ~CSR_CRMD_DA ; + val |=CSR_CRMD_PG ; +csr_write32 (val,LOONGARCH_CSR_CRMD ); +/* Config Direct Mapping */ csr_write64 (CSR_DMW0_INIT ,LOONGARCH_CSR_DMWIN0 );csr_write64 (CSR_DMW1_INIT ,LOONGARCH_CSR_DMWIN1 ); --2.27.0
Quoted 27 lines · Huacai Chen <[email protected]> 于2023年11月23日周四 12:05写道:
Quoted 25 lines · Ainux Wang
Quoted 15 lines · Huacai Chen <[email protected]> 于2023年11月22日周三 22:22写道:
Hi, Yao,
Quoted 11 lines · On Wed, Nov 22, 2023 at 4:50 PM <[email protected]> wrote:
From: Wang Yao <[email protected]>
Refer ot UEFI spec v2.10 section 2.3.8 LoongArch Platforms:
The processor is in the following execution mode during boot service:
...
The memory is in physical addressing mode. LoongArch architecture defines two memory access mode, namely direct address translation mode and mapped address translation mode.Quoted 6 lines
Quoted 1 line
So need to change MMU translation mode before config direct mapping.
From UEFI spec v2.10 section 2.3.8, you can also see "MMU enabled", so PG is already enabled in UEFI.
Huacai
Hi,Huacai,
Quoted 9 lines
Quoted 4 lines
From LoongArch-Vol1-v1.10-EN.pdf , DA or PG both refer to a mode enabled by MMU, Will the UEFI firmware not use DA mode?
The UEFI spec did not specify the use of DA or PG mode, but the kernel must use PG.In theory you are right, but "MMU" usually means "TLB", including this case. EFI runtime needs TLB, so UEFI cannot be in DA mode.
Huacai
Is it possible that set_virtual_address_map() only configures the DMW under PG mode
without switching DA to PG mode?
I noticed in efi_get_virtmap() that virt_addr = phys_addr + 0x8000_0000_0000_000,
and I also observed in the kernel's head.S there's a PG mode switch process ,which could enable runtime services to use PG mode.
However, this occurs after the STUB,
so does the STUB phase really require or ensure that the UEFI firmware is definitely in
PG mode?
Quoted 38 lines
Quoted 38 lines
Best regards,
Ainux Wang.Quoted 35 lines
Quoted 35 lines
Signed-off-by Wang Yao <[email protected]>--- drivers/firmware/efi/libstub/loongarch.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/firmware/efi/libstub/loongarch.c +7
-2@@ -49,7 +49,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,struct exit_boot_struct priv;unsigned long desc_size;efi_status_t status; -u32 desc_ver; +u32 desc_ver, val; status =efi_alloc_virtmap (&priv.runtime_map, &desc_size, &desc_ver);if (status !=EFI_SUCCESS ) {@@ -69,6 +69,12 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, priv.runtime_entry_count * desc_size, desc_size, desc_ver, priv.runtime_map); +/* Change address translation mode */ + val =csr_read32 (LOONGARCH_CSR_CRMD ); + val &= ~CSR_CRMD_DA ; + val |=CSR_CRMD_PG ; +csr_write32 (val,LOONGARCH_CSR_CRMD ); +/* Config Direct Mapping */ csr_write64 (CSR_DMW0_INIT ,LOONGARCH_CSR_DMWIN0 );csr_write64 (CSR_DMW1_INIT ,LOONGARCH_CSR_DMWIN1 ); --2.27.0
Quoted 47 lines · Ainux Wang
Quoted 27 lines · Huacai Chen <[email protected]> 于2023年11月23日周四 12:05写道:
Quoted 25 lines · Ainux Wang
Quoted 15 lines · Huacai Chen <[email protected]> 于2023年11月22日周三 22:22写道:
Hi, Yao,
Quoted 11 lines · On Wed, Nov 22, 2023 at 4:50 PM <[email protected]> wrote:
From: Wang Yao <[email protected]>
Refer ot UEFI spec v2.10 section 2.3.8 LoongArch Platforms:
The processor is in the following execution mode during boot service:
...
The memory is in physical addressing mode. LoongArch architecture defines two memory access mode, namely direct address translation mode and mapped address translation mode.Quoted 6 lines
Quoted 1 line
So need to change MMU translation mode before config direct mapping.
From UEFI spec v2.10 section 2.3.8, you can also see "MMU enabled", so PG is already enabled in UEFI.
Huacai
Hi,Huacai,
Quoted 9 lines
Quoted 4 lines
From LoongArch-Vol1-v1.10-EN.pdf , DA or PG both refer to a mode enabled by MMU, Will the UEFI firmware not use DA mode?
The UEFI spec did not specify the use of DA or PG mode, but the kernel must use PG.In theory you are right, but "MMU" usually means "TLB", including this case. EFI runtime needs TLB, so UEFI cannot be in DA mode.
Huacai
Is it possible that
set_virtual_address_map()only configures the DMW under PG mode
without switching DA to PG mode?I noticed in
efi_get_virtmap()thatvirt_addr=phys_addr+ 0x8000_0000_0000_000,
and I also observed in the kernel'shead.Sthere's a PG mode switch process ,which could enable runtime services to use PG mode.
Quoted 4 lines
However, this occurs after the STUB,
so does the STUB phase really require or ensure that the UEFI firmware is definitely in
PG mode?
Anything you think redundant may be not redundant, remember that UEFI is not the only BIOS and Linux is not the only OS. So Linux kernel cannot assume any BIOS is the same as UEFI does, UEFI cannot assume any OS is the same as Linux does, too.
Huacai
Quoted 39 lines
Quoted 39 lines
Quoted 39 lines
Best regards,
Ainux Wang.Quoted 35 lines
Quoted 35 lines
Signed-off-by Wang Yao <[email protected]>--- drivers/firmware/efi/libstub/loongarch.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/firmware/efi/libstub/loongarch.c +7
-2@@ -49,7 +49,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,struct exit_boot_struct priv;unsigned long desc_size;efi_status_t status; -u32 desc_ver; +u32 desc_ver, val; status =efi_alloc_virtmap (&priv.runtime_map, &desc_size, &desc_ver);if (status !=EFI_SUCCESS ) {@@ -69,6 +69,12 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, priv.runtime_entry_count * desc_size, desc_size, desc_ver, priv.runtime_map); +/* Change address translation mode */ + val =csr_read32 (LOONGARCH_CSR_CRMD ); + val &= ~CSR_CRMD_DA ; + val |=CSR_CRMD_PG ; +csr_write32 (val,LOONGARCH_CSR_CRMD ); +/* Config Direct Mapping */ csr_write64 (CSR_DMW0_INIT ,LOONGARCH_CSR_DMWIN0 );csr_write64 (CSR_DMW1_INIT ,LOONGARCH_CSR_DMWIN1 ); --2.27.0