Skip to contentSkip to conversation

mmc: Remove alignment hole for cmdidx in struct mmc_cmd

RFC Superseded Jonas Karlman 3 participants 5 messages Sep 29, 2023 – Oct 5, 2023 Arm (32-bit)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 mmc: Remove alignment hole for cmdidx in struct mmc_cmd

The alignment hole caused by cmdidx in struct mmc_cmd cause strange issues together with the peephole2 optimization on Amlogic SoCs. Following was observed while working on SPL support for Amlogic SoCs.

sd_get_capabilities() normally issue a CMD55 followed by a CMD51. However, on at least Amlogic S905 (Cortex-A53) and S905X3 (Cortex-A55), CMD55 was instead followed by CMD8 (and a few reties) in SPL.

Code from the call site:

  cmd.cmdidx = SD_CMD_APP_SEND_SCR; // 51
  ...
  data.blocksize = 8;
  ...
  err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);

Running the code with MMC_TRACE enabled shows:

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:8
                ARG                      0x00000000
                RET                      -110

Removing the alignment hole by changing cmdidx from ushort to uint or building with -fno-peephole2 flag seem to resolve this issue.

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:51
                ARG                      0x00000000
                MMC_RSP_R1,5,6,7         0x00000920

Same issue was observed building U-Boot with gcc 8-13. Please advise on how to best work around this possible gcc optimization bug.

---
 arch/arm/cpu/armv8/config.mk | 2 ++
 include/mmc.h                | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
arch/arm/cpu/armv8/config.mk +2 -0
index 4d74b2a533e0..7177dcd7c73b 100644
@@ -7,6 +7,8 @@ PLATFORM_RELFLAGS += $(call cc-option,-mbranch-protection=none)
 PF_NO_UNALIGNED := $(call cc-option, -mstrict-align)
 PLATFORM_CPPFLAGS += $(PF_NO_UNALIGNED)
 
+PLATFORM_CPPFLAGS += $(call cc-option,-fno-peephole2)
+
 EFI_LDS := elf_aarch64_efi.lds
 EFI_CRT0 := crt0_aarch64_efi.o
 EFI_RELOC := reloc_aarch64_efi.o
include/mmc.h +1 -1
index 9aef31ea5deb..7b3868bb5664 100644
@@ -413,7 +413,7 @@ struct mmc_cid {
 };
 
 struct mmc_cmd {
-	ushort cmdidx;
+	uint cmdidx;
 	uint resp_type;
 	uint cmdarg;
 	uint response[4];
Signature · 1 line
2.42.0
#1 lore raw
Quoted 35 lines · Jonas Karlman

The alignment hole caused by cmdidx in struct mmc_cmd cause strange issues together with the peephole2 optimization on Amlogic SoCs. Following was observed while working on SPL support for Amlogic SoCs.

sd_get_capabilities() normally issue a CMD55 followed by a CMD51. However, on at least Amlogic S905 (Cortex-A53) and S905X3 (Cortex-A55), CMD55 was instead followed by CMD8 (and a few reties) in SPL.

Code from the call site:

  cmd.cmdidx = SD_CMD_APP_SEND_SCR; // 51
  ...
  data.blocksize = 8;
  ...
  err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);

Running the code with MMC_TRACE enabled shows:

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:8
                ARG                      0x00000000
                RET                      -110

Removing the alignment hole by changing cmdidx from ushort to uint or building with -fno-peephole2 flag seem to resolve this issue.

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:51
                ARG                      0x00000000
                MMC_RSP_R1,5,6,7         0x00000920
Quoted 4 lines

Same issue was observed building U-Boot with gcc 8-13. Please advise on how to best work around this possible gcc optimization bug.

I'll try reproducing this. I'm using this:
https://git.vitali64.duckdns.org/misc/u-boot-kii-pro.git (branch:
wip/spl, known to boot on S905/GXBB)

For reference, I'm using this version of aarch64 gcc (from the Arch Linux repos):

	$ aarch64-linux-gnu-gcc --version
	aarch64-linux-gnu-gcc (GCC) 13.1.0
	Copyright (C) 2023 Free Software Foundation, Inc.
	This is free software; see the source for copying conditions.  There is NO
	warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

And this version of (host) gcc:

	$ gcc --version
	gcc (GCC) 13.1.1 20230429
	Copyright (C) 2023 Free Software Foundation, Inc.
	This is free software; see the source for copying conditions.  There is NO
	warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Cheers.

#2 lore raw
In reply to Jonas Karlman
Quoted 50 lines · Jonas Karlman

The alignment hole caused by cmdidx in struct mmc_cmd cause strange issues together with the peephole2 optimization on Amlogic SoCs. Following was observed while working on SPL support for Amlogic SoCs.

sd_get_capabilities() normally issue a CMD55 followed by a CMD51. However, on at least Amlogic S905 (Cortex-A53) and S905X3 (Cortex-A55), CMD55 was instead followed by CMD8 (and a few reties) in SPL.

Code from the call site:

  cmd.cmdidx = SD_CMD_APP_SEND_SCR; // 51
  ...
  data.blocksize = 8;
  ...
  err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);

Running the code with MMC_TRACE enabled shows:

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:8
                ARG                      0x00000000
                RET                      -110

Removing the alignment hole by changing cmdidx from ushort to uint or building with -fno-peephole2 flag seem to resolve this issue.

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:51
                ARG                      0x00000000
                MMC_RSP_R1,5,6,7         0x00000920

Same issue was observed building U-Boot with gcc 8-13. Please advise on how to best work around this possible gcc optimization bug.

---
 arch/arm/cpu/armv8/config.mk | 2 ++
 include/mmc.h                | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
arch/arm/cpu/armv8/config.mk +0 -0
index 4d74b2a533e0..7177dcd7c73b 100644

arch/arm/cpu/armv8/config.mk PLATFORM_RELFLAGS += $(call cc-option,-mbranch-protection=none) · line 10

Quoted 4 lines
arch/arm/cpu/armv8/config.mk +1 -0
@@ -7,6 +7,8 @@ PLATFORM_RELFLAGS += $(call cc-option,-mbranch-protection=none)
 PF_NO_UNALIGNED := $(call cc-option, -mstrict-align)
 PLATFORM_CPPFLAGS += $(PF_NO_UNALIGNED)
 
+PLATFORM_CPPFLAGS += $(call cc-option,-fno-peephole2)

Eek really?

Quoted 9 lines
arch/arm/cpu/armv8/config.mk +1 -0
+
 EFI_LDS := elf_aarch64_efi.lds
 EFI_CRT0 := crt0_aarch64_efi.o
 EFI_RELOC := reloc_aarch64_efi.o
include/mmc.h +0 -0
index 9aef31ea5deb..7b3868bb5664 100644

include/mmc.h struct mmc_cid { · line 416

Quoted 5 lines
include/mmc.h +1 -1
@@ -413,7 +413,7 @@ struct mmc_cid {
 };
 
 struct mmc_cmd {
-       ushort cmdidx;
+       uint cmdidx;

This bit looks OK.

Quoted 6 lines
include/mmc.h +0 -1
        uint resp_type;
        uint cmdarg;
        uint response[4];
--

2.42.0

Regards,
Simon

#3 lore raw
Quoted 54 lines · Simon Glass
Quoted 53 lines · Jonas Karlman

The alignment hole caused by cmdidx in struct mmc_cmd cause strange issues together with the peephole2 optimization on Amlogic SoCs. Following was observed while working on SPL support for Amlogic SoCs.

sd_get_capabilities() normally issue a CMD55 followed by a CMD51. However, on at least Amlogic S905 (Cortex-A53) and S905X3 (Cortex-A55), CMD55 was instead followed by CMD8 (and a few reties) in SPL.

Code from the call site:

  cmd.cmdidx = SD_CMD_APP_SEND_SCR; // 51
  ...
  data.blocksize = 8;
  ...
  err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);

Running the code with MMC_TRACE enabled shows:

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:8
                ARG                      0x00000000
                RET                      -110

Removing the alignment hole by changing cmdidx from ushort to uint or building with -fno-peephole2 flag seem to resolve this issue.

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:51
                ARG                      0x00000000
                MMC_RSP_R1,5,6,7         0x00000920

Same issue was observed building U-Boot with gcc 8-13. Please advise on how to best work around this possible gcc optimization bug.

---
 arch/arm/cpu/armv8/config.mk | 2 ++
 include/mmc.h                | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
arch/arm/cpu/armv8/config.mk +0 -0
index 4d74b2a533e0..7177dcd7c73b 100644
@@ -7,6 +7,8 @@ PLATFORM_RELFLAGS += $(call cc-option,-mbranch-protection=none)
 PF_NO_UNALIGNED := $(call cc-option, -mstrict-align)
 PLATFORM_CPPFLAGS += $(PF_NO_UNALIGNED)
 
Quoted 3 lines

arch/arm/cpu/armv8/config.mk PLATFORM_RELFLAGS += $(call cc-option,-mbranch-protection=none) · line 10

Quoted 1 line
arch/arm/cpu/armv8/config.mk +1 -0
+PLATFORM_CPPFLAGS += $(call cc-option,-fno-peephole2)

Eek really?

As mentioned the issue could be resolved with changing cmdidx from ushort to uint or disabling the peephole2 optimization.

For the purpose of trying to get more feedback on the issue I included both changes that would resolve my issue.

Also if there is one struct affected, there could be other similar structs affected by a similar issue.

I do not really know if this could be a cpu errata, compiler bug or something else, including some assembly from a non-working and working case (peephole2 optimization enabled/disabled).

Code generated with ushort and peephole2 enabled (not working):

  60 06 80 52     mov        w0,#0x33		// 51
  f5 3f 06 91     add        x21,sp,#0x18f
  e0 23 01 79     strh       w0,[sp, #cmd]
  e0 03 00 b2     mov        x0,#0x100000001
  b5 e6 7a 92     and        x21,x21,#-0x40
  f5 03 06 a9     stp        x21,x0,[sp, #data]
  00 01 80 52     mov        w0,#0x8
  e2 83 01 91     add        x2,sp,#0x60
  e1 43 02 91     add        x1,sp,#0x90
  63 00 80 52     mov        w3,#0x3
  e0 73 00 b9     str        w0,[sp, #blocksize]
  e0 03 13 aa     mov        x0,x19
  f6 ff 12 29     stp        w22,wzr,[sp, #resp_type]
  50 fd ff 97     bl         mmc_send_cmd_retry

Code generated with ushort and peephole2 disabled (working):

  60 06 80 52     mov        w0,#0x33		// 51
  e0 23 01 79     strh       w0,[sp, #cmd]
  e0 03 00 b2     mov        x0,#0x100000001
  f5 3f 06 91     add        x21,sp,#0x18f
  e0 37 00 f9     str        x0,[sp, #local_168]
  00 01 80 52     mov        w0,#0x8
  b5 e6 7a 92     and        x21,x21,#-0x40
  e2 83 01 91     add        x2,sp,#0x60
  e1 43 02 91     add        x1,sp,#0x90
  63 00 80 52     mov        w3,#0x3
  f5 33 00 f9     str        x21,[sp, #data]
  e0 73 00 b9     str        w0,[sp, #blocksize]
  e0 03 13 aa     mov        x0,x19
  f6 97 00 b9     str        w22,[sp, #resp_type]
  ff 9b 00 b9     str        wzr,[sp, #cmdarg]
  4a fd ff 97     bl         mmc_send_cmd_retry

Hoping someone can see something strange I cannot.

Regards,
Jonas

Quoted 27 lines
Quoted 10 lines
arch/arm/cpu/armv8/config.mk +1 -0
+
 EFI_LDS := elf_aarch64_efi.lds
 EFI_CRT0 := crt0_aarch64_efi.o
 EFI_RELOC := reloc_aarch64_efi.o
include/mmc.h +0 -0
index 9aef31ea5deb..7b3868bb5664 100644

include/mmc.h struct mmc_cid { · line 416

Quoted 5 lines
include/mmc.h +1 -1
@@ -413,7 +413,7 @@ struct mmc_cid {
 };
 
 struct mmc_cmd {
-       ushort cmdidx;
+       uint cmdidx;

This bit looks OK.

Quoted 6 lines
include/mmc.h +0 -1
        uint resp_type;
        uint cmdarg;
        uint response[4];
--

2.42.0

Regards,
Simon

#4 lore raw

Hi Jonas,

Quoted 109 lines · Jonas Karlman
Quoted 54 lines · Simon Glass
Quoted 53 lines · Jonas Karlman

The alignment hole caused by cmdidx in struct mmc_cmd cause strange issues together with the peephole2 optimization on Amlogic SoCs. Following was observed while working on SPL support for Amlogic SoCs.

sd_get_capabilities() normally issue a CMD55 followed by a CMD51. However, on at least Amlogic S905 (Cortex-A53) and S905X3 (Cortex-A55), CMD55 was instead followed by CMD8 (and a few reties) in SPL.

Code from the call site:

  cmd.cmdidx = SD_CMD_APP_SEND_SCR; // 51
  ...
  data.blocksize = 8;
  ...
  err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);

Running the code with MMC_TRACE enabled shows:

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:8
                ARG                      0x00000000
                RET                      -110

Removing the alignment hole by changing cmdidx from ushort to uint or building with -fno-peephole2 flag seem to resolve this issue.

CMD_SEND:55

                ARG                      0x50480000
                MMC_RSP_R1,5,6,7         0x00000920
CMD_SEND:51
                ARG                      0x00000000
                MMC_RSP_R1,5,6,7         0x00000920

Same issue was observed building U-Boot with gcc 8-13. Please advise on how to best work around this possible gcc optimization bug.

---
 arch/arm/cpu/armv8/config.mk | 2 ++
 include/mmc.h                | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
arch/arm/cpu/armv8/config.mk +0 -0
index 4d74b2a533e0..7177dcd7c73b 100644
@@ -7,6 +7,8 @@ PLATFORM_RELFLAGS += $(call cc-option,-mbranch-protection=none)
 PF_NO_UNALIGNED := $(call cc-option, -mstrict-align)
 PLATFORM_CPPFLAGS += $(PF_NO_UNALIGNED)
 
Quoted 3 lines

arch/arm/cpu/armv8/config.mk PLATFORM_RELFLAGS += $(call cc-option,-mbranch-protection=none) · line 10

Quoted 1 line
arch/arm/cpu/armv8/config.mk +1 -0
+PLATFORM_CPPFLAGS += $(call cc-option,-fno-peephole2)

Eek really?

As mentioned the issue could be resolved with changing cmdidx from ushort to uint or disabling the peephole2 optimization.

For the purpose of trying to get more feedback on the issue I included both changes that would resolve my issue.

Also if there is one struct affected, there could be other similar structs affected by a similar issue.

I do not really know if this could be a cpu errata, compiler bug or something else, including some assembly from a non-working and working case (peephole2 optimization enabled/disabled).

Code generated with ushort and peephole2 enabled (not working):

  60 06 80 52     mov        w0,#0x33           // 51
  f5 3f 06 91     add        x21,sp,#0x18f
  e0 23 01 79     strh       w0,[sp, #cmd]
  e0 03 00 b2     mov        x0,#0x100000001
  b5 e6 7a 92     and        x21,x21,#-0x40
  f5 03 06 a9     stp        x21,x0,[sp, #data]
  00 01 80 52     mov        w0,#0x8
  e2 83 01 91     add        x2,sp,#0x60
  e1 43 02 91     add        x1,sp,#0x90
  63 00 80 52     mov        w3,#0x3
  e0 73 00 b9     str        w0,[sp, #blocksize]
  e0 03 13 aa     mov        x0,x19
  f6 ff 12 29     stp        w22,wzr,[sp, #resp_type]
  50 fd ff 97     bl         mmc_send_cmd_retry

Code generated with ushort and peephole2 disabled (working):

  60 06 80 52     mov        w0,#0x33           // 51
  e0 23 01 79     strh       w0,[sp, #cmd]
  e0 03 00 b2     mov        x0,#0x100000001
  f5 3f 06 91     add        x21,sp,#0x18f
  e0 37 00 f9     str        x0,[sp, #local_168]
  00 01 80 52     mov        w0,#0x8
  b5 e6 7a 92     and        x21,x21,#-0x40
  e2 83 01 91     add        x2,sp,#0x60
  e1 43 02 91     add        x1,sp,#0x90
  63 00 80 52     mov        w3,#0x3
  f5 33 00 f9     str        x21,[sp, #data]
  e0 73 00 b9     str        w0,[sp, #blocksize]
  e0 03 13 aa     mov        x0,x19
  f6 97 00 b9     str        w22,[sp, #resp_type]
  ff 9b 00 b9     str        wzr,[sp, #cmdarg]
  4a fd ff 97     bl         mmc_send_cmd_retry
Quoted 1 line

Hoping someone can see something strange I cannot.

I used to be able to see what compilers were doing, but sometime over the past 10 years they got a lot smarter and I didn't.

I don't see anything wrong with the C code. The setting of cmd.resp_type to the same value could perhaps be fooling the compiler?

Regards,
Simon