Skip to contentSkip to conversation

Add cache line alignment support

PATCH Superseded Anton Staaf 3 participants 13 messages Oct 3, 2011 – Oct 4, 2011

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Add cache line alignment support

The cache line alignment issue has gone around a couple of times now. This patch set implements all of the details that we have discussed about the implementation of ALLOC_CACHE_ALIGN_BUFFER other than the switch to the Linux style L1_CACHE_BYTES, L1_CACHE_SHIFT, and ARCH_DMA_MINALIGN defines mentioned by Mike Frysinger. It also includes patches that use the macro to fix MMC and ext2 buffers, as well as define the CONFIG_SYS_CACHELINE_SIZE value for Tegra and add a default value for CONFIG_SYS_CACHELINE_SIZE with a warning if it is used.

About the Linux defines that Mike suggests. It may make sense to move these patches over to use that mechanism, especially since there are only a few configs that define CONFIG_SYS_CACHELINE_SIZE anyway. Also, since this is an architecture specific parameter and not a board specific one it makes sense that these configs would be architecture specific. I am also not supper happy with the fact that most boards generate a warning with this patchset, because they don't set CONFIG_SYS_CACHELINE_SIZE. But I'm not sure that silently providing a default is a good idea.

I think we can discuss the relative merrits of that move independently of the implementation of this fix though, and I wanted to get this discussion going again.

Thanks,
Anton

---

Anton Staaf (5):
cache: add ALLOC_CACHE_ALIGN_BUFFER macro
cache: add default setting for CONFIG_SYS_CACHELINE_SIZE
tegra: define CONFIG_SYS_CACHELINE_SIZE for tegra
mmc: dcache: allocate cache aligned buffer for scr and switch_status
ext2: Cache line aligned partial sector bounce buffer

 doc/README.arm-caches           |    2 +
 drivers/mmc/mmc.c               |   10 +++---
 fs/ext2/dev.c                   |    2 +-
 include/common.h                |   67 +++++++++++++++++++++++++++++++++++++++
 include/configs/tegra2-common.h |    2 +
 5 files changed, 77 insertions(+), 6 deletions(-)
Signature · 1 line
1.7.3.1
#1 lore raw

PATCH 1/5 cache: add ALLOC_CACHE_ALIGN_BUFFER macro

This macro is used to allocate cache line size aligned stack buffers for use with DMA hardware.

Cc Lukasz Majewski <[email protected]>
Cc Kyungmin Park <[email protected]>
Cc Mike Frysinger <[email protected]>
Cc Aneesh V <[email protected]>
Cc Albert ARIBAUD <[email protected]>

Change-Id: I2c2b3b9bf6a3a5fd1f2e45d65373026bdd76b0a8

---
 doc/README.arm-caches |    2 +
 include/common.h      |   59 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 0 deletions(-)
doc/README.arm-caches +2 -0
index cd2b458..f6a52e3 100644
@@ -40,6 +40,8 @@ Buffer Requirements:
 - If the buffer is not cache-line aligned invalidation will be restricted
   to the aligned part. That is, one cache-line at the respective boundary
   may be left out while doing invalidation.
+- A suitable buffer can be alloced on the stack using the
+  ALLOC_CACHE_ALIGN_BUFFER macro.
 
 Cleanup Before Linux:
 - cleanup_before_linux() should flush the D-cache, invalidate I-cache, and
include/common.h +59 -0

65 lines not on this page · raw message

Signature · 1 line
1.7.3.1
#3 lore raw

PATCH 2/5 cache: add default setting for CONFIG_SYS_CACHELINE_SIZE

In reply to Anton Staaf

CONFIG_SYS_CACHELINE_SIZE is supposed to be defined for all configs. But most configs do not specify it. This patch will set CONFIG_SYS_CACHELINE_SIZE to a default value equal to the GCC builtin macro __BIGGEST_ALIGNMENT__. This is not strictly correct, so it also generates a warning if the default is used.

Cc Lukasz Majewski <[email protected]>
Cc Kyungmin Park <[email protected]>
Cc Mike Frysinger <[email protected]>
Cc Aneesh V <[email protected]>
Cc Albert ARIBAUD <[email protected]>

Change-Id: I346afb6c417ccc9bbb353940e1819e209405986e

---
 include/common.h |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
include/common.h +8 -0
index cdd76c5..9981e36 100644
@@ -790,6 +790,14 @@ int cpu_release(int nr, int argc, char * const argv[]);
 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
 
 /*
+ *
+ */
+#if !defined(CONFIG_SYS_CACHELINE_SIZE)
+#warning CONFIG_SYS_CACHELINE_SIZE not defined, using __BIGGEST_ALIGNMENT__
+#define CONFIG_SYS_CACHELINE_SIZE	__BIGGEST_ALIGNMENT__
+#endif
+
+/*
  * The ALLOC_CACHE_ALIGN_BUFFER macro is used to allocate a cache line size
  * aligned buffer on the stack.  Such a buffer is useful for DMA operations
  * where flushing and invalidating the cache before and after a read and/or
Signature · 1 line
1.7.3.1
#6 lore raw

PATCH 3/5 tegra: define CONFIG_SYS_CACHELINE_SIZE for tegra

In reply to Anton Staaf
Cc Tom Warren <[email protected]>
Cc Simon Glass <[email protected]>
Cc Mike Frysinger <[email protected]>
Cc Albert ARIBAUD <[email protected]>

Change-Id: I5c4bcfc0bfe59158ff249fe3be6640eec6d3cc76

---
 include/configs/tegra2-common.h |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
include/configs/tegra2-common.h +2 -0
index 73e0f05..a9c665c 100644
@@ -33,6 +33,8 @@
 #define CONFIG_MACH_TEGRA_GENERIC	/* which is a Tegra generic machine */
 #define CONFIG_SYS_L2CACHE_OFF		/* No L2 cache */
 
+#define CONFIG_SYS_CACHELINE_SIZE	32
+
 #define CONFIG_ENABLE_CORTEXA9		/* enable CPU (A9 complex) */
 
 #include <asm/arch/tegra2.h>		/* get chip and board defs */
Signature · 1 line
1.7.3.1
#8 lore raw

PATCH 4/5 mmc: dcache: allocate cache aligned buffer for scr and switch_status

In reply to Anton Staaf

Currently the sd_change_freq function allocates two buffers on the stack that it passes down to the MMC device driver. These buffers could be unaligned to the L1 dcache line size. This causes problems when using DMA and with caches enabled.

This patch correctly cache alignes the buffers used for reading the scr register and switch status values from an MMC device.

Change-Id Ifa8414f572ef907681bd2d5ff3950285a215357d
Cc Lukasz Majewski <[email protected]>
Cc Mike Frysinger <[email protected]>
Cc Albert ARIBAUD <[email protected]>
---
 drivers/mmc/mmc.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
drivers/mmc/mmc.c +5 -5
index 391bc2b..ba6fbfe 100644
@@ -702,8 +702,8 @@ int sd_change_freq(struct mmc *mmc)
 {
 	int err;
 	struct mmc_cmd cmd;
-	uint scr[2];
-	uint switch_status[16];
+	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
+	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
 	struct mmc_data data;
 	int timeout;
 
@@ -731,7 +731,7 @@ int sd_change_freq(struct mmc *mmc)
 	timeout = 3;
 
 retry_scr:
-	data.dest = (char *)&scr;
+	data.dest = (char *)scr;
 	data.blocksize = 8;
 	data.blocks = 1;
 	data.flags = MMC_DATA_READ;
@@ -773,7 +773,7 @@ retry_scr:
 	timeout = 4;
 	while (timeout--) {
 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
-				(u8 *)&switch_status);
+				(u8 *)switch_status);
 
 		if (err)
 			return err;
@@ -787,7 +787,7 @@ retry_scr:
 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
 		return 0;
 
-	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
+	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
 
 	if (err)
 		return err;
Signature · 1 line
1.7.3.1
#10 lore raw

PATCH 5/5 ext2: Cache line aligned partial sector bounce buffer

In reply to Anton Staaf

Currently, if a device read request is done that does not begin or end on a sector boundary a stack allocated bounce buffer is used to perform the read, and then just the part of the sector that is needed is copied into the users buffer. This stack allocation can mean that the bounce buffer will not be aligned to the dcache line size. This is a problem when caches are enabled because unaligned cache invalidates are not safe.

This patch uses ALLOC_CACHE_ALIGN_BUFFER to create a stack allocated cache line size aligned bounce buffer.

Cc Lukasz Majewski <[email protected]>
Cc Mike Frysinger <[email protected]>
Cc Dave Liu <[email protected]>
Cc Andy Fleming <[email protected]>
Cc Albert ARIBAUD <[email protected]>

Change-Id: I32e1594d90ef039137bb219b0f7ced55768744ff

---
 fs/ext2/dev.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/ext2/dev.c +1 -1
index 78851d0..874e211 100644
@@ -52,7 +52,7 @@ int ext2fs_set_blk_dev(block_dev_desc_t *rbdd, int part)
 
 int ext2fs_devread(int sector, int byte_offset, int byte_len, char *buf)
 {
-	char sec_buf[SECTOR_SIZE];
+	ALLOC_CACHE_ALIGN_BUFFER(char, sec_buf, SECTOR_SIZE);
 	unsigned sectors;
 
 	/*
Signature · 1 line
1.7.3.1
#12 lore raw

cover Add cache line alignment support

In reply to Anton Staaf
Quoted 9 lines · On Monday, October 03, 2011 19:54:55 Anton Staaf wrote:

The cache line alignment issue has gone around a couple of times now. This patch set implements all of the details that we have discussed about the implementation of ALLOC_CACHE_ALIGN_BUFFER other than the switch to the Linux style L1_CACHE_BYTES, L1_CACHE_SHIFT, and ARCH_DMA_MINALIGN defines mentioned by Mike Frysinger. It also includes patches that use the macro to fix MMC and ext2 buffers, as well as define the CONFIG_SYS_CACHELINE_SIZE value for Tegra and add a default value for CONFIG_SYS_CACHELINE_SIZE with a warning if it is used.

Quoted 8 lines

About the Linux defines that Mike suggests. It may make sense to move these patches over to use that mechanism, especially since there are only a few configs that define CONFIG_SYS_CACHELINE_SIZE anyway. Also, since this is an architecture specific parameter and not a board specific one it makes sense that these configs would be architecture specific. I am also not supper happy with the fact that most boards generate a warning with this patchset, because they don't set CONFIG_SYS_CACHELINE_SIZE. But I'm not sure that silently providing a default is a good idea.

this looks good to me. my only complaint really has nothing to do with this patchset, but with CONFIG_SYS_CACHELINE_SIZE and the L1_CACHE_xxx defines. but we can address that separately since the current law of the land in u-boot appears to be CONFIG_SYS_CACHELINE_SIZE.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20111004/31fc34ed/attachment.pgp

#4 lore raw

PATCH 2/5 cache: add default setting for CONFIG_SYS_CACHELINE_SIZE

In reply to Anton Staaf

include/common.h int cpu_release(int nr, int argc, char * const argv[]); · line 794

Quoted 6 lines · On Monday, October 03, 2011 19:54:57 Anton Staaf wrote:
include/common.h +2 -0
 
 /*
+ *
+ */

no comment ? :)
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20111004/92eccfce/attachment.pgp

#5 lore raw

PATCH 2/5 cache: add default setting for CONFIG_SYS_CACHELINE_SIZE

In reply to Mike Frysinger
Quoted 9 lines · Mike Frysinger
Quoted 6 lines · On Monday, October 03, 2011 19:54:57 Anton Staaf wrote:
include/common.h +0 -0

?/*
+ *
+ */

no comment ? :)

Doh, I knew I was forgetting something. I'll fix this today with a v2.

Thanks,
Anton

Quoted 2 lines

-mike