Skip to content

Commit

Permalink
common/memsize.c: Fix get_ram_size() when cache is enabled
Browse files Browse the repository at this point in the history
Ensure that every write is flushed to memory and afterward reads are
from memory.
Since the algorithm rely on the fact that accessing to not existent
memory lead to write at addr / 2 without this modification accesses
to aliased (not physically present) addresses are cached and
wrong size is returned.

This was discovered while working on a TI AM625 based board
where cache is normally enabled, see commit c02712a ("arm: mach-k3: Enable dcache in SPL").

Signed-off-by: Emanuele Ghidoli <emanuele.ghidoli@toradex.com>
  • Loading branch information
eghidoli committed May 29, 2023
1 parent 6dcee70 commit e753179
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions common/memsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
#include <common.h>
#include <init.h>
#include <asm/global_data.h>
#include <cpu_func.h>
#include <stdint.h>

DECLARE_GLOBAL_DATA_PTR;

#ifdef CONFIG_SYS_CACHELINE_SIZE
# define MEMSIZE_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
#else
/* Just use the greatest cache flush alignment requirement I'm aware of */
# define MEMSIZE_CACHELINE_SIZE 128
#endif

#ifdef __PPC__
/*
* At least on G2 PowerPC cores, sequential accesses to non-existent
Expand All @@ -20,6 +29,17 @@ DECLARE_GLOBAL_DATA_PTR;
# define sync() /* nothing */
#endif

static void dcache_flush_invalidate(volatile long *p)
{
if (dcache_status()) {
uintptr_t start, stop;
start = ALIGN_DOWN((uintptr_t)p, MEMSIZE_CACHELINE_SIZE);
stop = start + MEMSIZE_CACHELINE_SIZE;
flush_dcache_range(start, stop);
invalidate_dcache_range(start, stop);
}
}

/*
* Check memory range for valid RAM. A simple memory test determines
* the actually available RAM size between addresses `base' and
Expand All @@ -41,6 +61,7 @@ long get_ram_size(long *base, long maxsize)
save[i++] = *addr;
sync();
*addr = ~cnt;
dcache_flush_invalidate(addr);
}

addr = base;
Expand All @@ -50,6 +71,8 @@ long get_ram_size(long *base, long maxsize)
*addr = 0;

sync();
dcache_flush_invalidate(addr);

if ((val = *addr) != 0) {
/* Restore the original data before leaving the function. */
sync();
Expand Down

0 comments on commit e753179

Please sign in to comment.