Skip to contentSkip to conversation

armv8: Fix get_sctlr() return type

PATCH In review Sam Protsenko 3 participants 6 messages Nov 7, 2024 – Nov 18, 2024 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 armv8: Fix get_sctlr() return type

SCTLR_EL2 is a 64-bit register [1]. Return its value as long (64 bit) instead of int (32 bit) in get_sctlr() to make sure it's not trimmed.

[1] https://developer.arm.com/documentation/ddi0595/2021-06/AArch64-Registers/SCTLR-EL2--System-Control-Register--EL2-?lang=en

Fixes 0ae7653128c8 ("arm64: core support")

---
Changes in v2:
- None (this patch was introduced in v2)

 arch/arm/cpu/armv8/cache_v8.c | 2 +-
 arch/arm/include/asm/system.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm/cpu/armv8/cache_v8.c +1 -1
index e6be6359c5d9..5d6953ffedd1 100644
@@ -825,7 +825,7 @@ void dcache_enable(void)
 
 void dcache_disable(void)
 {
-	uint32_t sctlr;
+	unsigned long sctlr;
 
 	sctlr = get_sctlr();
 
arch/arm/include/asm/system.h +1 -1
index 52f6c9b934d7..dbf9ab43e280 100644
@@ -171,7 +171,7 @@ static inline unsigned int current_el(void)
 	return 3 & (el >> 2);
 }
 
-static inline unsigned int get_sctlr(void)
+static inline unsigned long get_sctlr(void)
 {
 	unsigned int el;
 	unsigned long val;
Signature · 1 line
2.39.5
#1 lore raw

PATCH v2 2/2 armv8: cpu: Implement allow_unaligned()

Usually ARMv8 platforms allow unaligned access for Normal memory. But some chips might not allow it by default, having SCTLR.A bit set to 1 before U-Boot execution. One such example is Exynos850 SoC. As allow_unaligned() is not implemented for ARMv8 at the moment, its __weak implementation is used, which does nothing. That might lead to unaligned access abort, for example when running EFI selftest. Fix that by implementing allow_unaligned() for ARMv8.

The issue was found when running EFI selftest on E850-96 board (Exynos850 based):

    => bootefi selftest $fdtcontroladdr

    ...
    Executing 'HII database protocols'
    "Synchronous Abort" handler, esr 0x96000021, far 0xbaac0991
    ...
    resetting ...

Unaligned abort happens in u16_strnlen(), which is called from efi_hii_sibt_string_ucs2_block_next():

    u16_strlen(blk->string_text)

where 'blk' type is struct efi_hii_sibt_string_ucs2_block. Because this struct is packed, doing "->string_text" makes 'blk' address incremented by 1 byte, which makes it unaligned. Although allow_unaligned() was called in efi_init_early() before EFI selftest execution, it wasn't implemented for ARMv8 CPUs, so data abort happened.

---
Changes in v2:
- None

 arch/arm/cpu/armv8/cpu.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm/cpu/armv8/cpu.c +5 -0
index d568efa427ab..82ecf02f4b03 100644
@@ -94,3 +94,8 @@ void armv8_setup_psci(void)
 	secure_ram_addr(psci_arch_init)();
 }
 #endif
+
+void allow_unaligned(void)
+{
+	set_sctlr(get_sctlr() & ~CR_A);
+}
Signature · 1 line
2.39.5
#2 lore raw
In reply to Sam Protsenko
Quoted 22 lines · Sam Protsenko

SCTLR_EL2 is a 64-bit register [1]. Return its value as long (64 bit) instead of int (32 bit) in get_sctlr() to make sure it's not trimmed.

[1] https://developer.arm.com/documentation/ddi0595/2021-06/AArch64-Registers/SCTLR-EL2--System-Control-Register--EL2-?lang=en

Fixes 0ae7653128c8 ("arm64: core support")

---
Changes in v2:
- None (this patch was introduced in v2)

 arch/arm/cpu/armv8/cache_v8.c | 2 +-
 arch/arm/include/asm/system.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm/cpu/armv8/cache_v8.c +0 -0
index e6be6359c5d9..5d6953ffedd1 100644

arch/arm/cpu/armv8/cache_v8.c void dcache_enable(void) · line 828

Quoted 5 lines
arch/arm/cpu/armv8/cache_v8.c +1 -1
@@ -825,7 +825,7 @@ void dcache_enable(void)
 
 void dcache_disable(void)
 {
-       uint32_t sctlr;
+       unsigned long sctlr;

Although that's correct since it's a 64bit platform, isn't it better to define it as u64 to be sure we'll have at least 64 bits?

Thanks
/Ilias

Quoted 19 lines
arch/arm/cpu/armv8/cache_v8.c +0 -0
        sctlr = get_sctlr();
 
arch/arm/include/asm/system.h +1 -2
index 52f6c9b934d7..dbf9ab43e280 100644
@@ -171,7 +171,7 @@ static inline unsigned int current_el(void)
        return 3 & (el >> 2);
 }
 
-static inline unsigned int get_sctlr(void)
+static inline unsigned long get_sctlr(void)
 {
        unsigned int el;
        unsigned long val;
--

2.39.5

#3 lore raw
Quoted 25 lines · Ilias Apalodimas
Quoted 23 lines · Sam Protsenko

SCTLR_EL2 is a 64-bit register [1]. Return its value as long (64 bit) instead of int (32 bit) in get_sctlr() to make sure it's not trimmed.

[1] https://developer.arm.com/documentation/ddi0595/2021-06/AArch64-Registers/SCTLR-EL2--System-Control-Register--EL2-?lang=en

Fixes 0ae7653128c8 ("arm64: core support")

---
Changes in v2:
- None (this patch was introduced in v2)

 arch/arm/cpu/armv8/cache_v8.c | 2 +-
 arch/arm/include/asm/system.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm/cpu/armv8/cache_v8.c +0 -0
index e6be6359c5d9..5d6953ffedd1 100644
@@ -825,7 +825,7 @@ void dcache_enable(void)
 
Quoted 8 lines

arch/arm/cpu/armv8/cache_v8.c void dcache_enable(void) · line 828

Quoted 4 lines
arch/arm/cpu/armv8/cache_v8.c +1 -1
 void dcache_disable(void)
 {
-       uint32_t sctlr;
+       unsigned long sctlr;

Although that's correct since it's a 64bit platform, isn't it better to define it as u64 to be sure we'll have at least 64 bits?

Thanks for reviewing this! I considered making it uint64_t while reworking this bit. This 'sctlr' variable is assigned to get_sctlr() return value, and then passed to set_sctlr() as a parameter, both of which are 'unsigned long'. So I made 'sctlr' unsigned long too, just to match the signatures of mentioned functions. As for get_sctlr(), I just followed the style already existed inside and around that function. As you said, both u64 and unsigned long are 64-bit long types on ARMv8, and that's definitely ARMv8 specific file, so I guess that should be fine? In any case, if you have a strong opinion about this, please let me know and I'll rework it; both u64 and unsigned long look ok to me in this context.

Quoted 21 lines

Thanks
/Ilias

Quoted 19 lines
arch/arm/cpu/armv8/cache_v8.c +0 -0
        sctlr = get_sctlr();
 
arch/arm/include/asm/system.h +1 -2
index 52f6c9b934d7..dbf9ab43e280 100644
@@ -171,7 +171,7 @@ static inline unsigned int current_el(void)
        return 3 & (el >> 2);
 }
 
-static inline unsigned int get_sctlr(void)
+static inline unsigned long get_sctlr(void)
 {
        unsigned int el;
        unsigned long val;
--

2.39.5

Reviewed-by
#4 lore raw
Quoted 37 lines · Sam Protsenko
Quoted 25 lines · Ilias Apalodimas
Quoted 23 lines · Sam Protsenko

SCTLR_EL2 is a 64-bit register [1]. Return its value as long (64 bit) instead of int (32 bit) in get_sctlr() to make sure it's not trimmed.

[1] https://developer.arm.com/documentation/ddi0595/2021-06/AArch64-Registers/SCTLR-EL2--System-Control-Register--EL2-?lang=en

Fixes 0ae7653128c8 ("arm64: core support")

---
Changes in v2:
- None (this patch was introduced in v2)

 arch/arm/cpu/armv8/cache_v8.c | 2 +-
 arch/arm/include/asm/system.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm/cpu/armv8/cache_v8.c +0 -0
index e6be6359c5d9..5d6953ffedd1 100644
@@ -825,7 +825,7 @@ void dcache_enable(void)
 
Quoted 8 lines

arch/arm/cpu/armv8/cache_v8.c void dcache_enable(void) · line 828

Quoted 4 lines
arch/arm/cpu/armv8/cache_v8.c +1 -1
 void dcache_disable(void)
 {
-       uint32_t sctlr;
+       unsigned long sctlr;

Although that's correct since it's a 64bit platform, isn't it better to define it as u64 to be sure we'll have at least 64 bits?

Quoted 11 lines

Thanks for reviewing this! I considered making it uint64_t while reworking this bit. This 'sctlr' variable is assigned to get_sctlr() return value, and then passed to set_sctlr() as a parameter, both of which are 'unsigned long'. So I made 'sctlr' unsigned long too, just to match the signatures of mentioned functions. As for get_sctlr(), I just followed the style already existed inside and around that function. As you said, both u64 and unsigned long are 64-bit long types on ARMv8, and that's definitely ARMv8 specific file, so I guess that should be fine? In any case, if you have a strong opinion about this, please let me know and I'll rework it; both u64 and unsigned long look ok to me in this context.

Yea perhaps I am nitpicking too much

Quoted 22 lines
Quoted 22 lines

Thanks
/Ilias

Quoted 19 lines
arch/arm/cpu/armv8/cache_v8.c +0 -0
        sctlr = get_sctlr();
 
arch/arm/include/asm/system.h +1 -2
index 52f6c9b934d7..dbf9ab43e280 100644
@@ -171,7 +171,7 @@ static inline unsigned int current_el(void)
        return 3 & (el >> 2);
 }
 
-static inline unsigned int get_sctlr(void)
+static inline unsigned long get_sctlr(void)
 {
        unsigned int el;
        unsigned long val;
--

2.39.5