aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ramips/patches-5.10/325-mt7621-fix-memory-detect.patch
diff options
context:
space:
mode:
authorChuanhong Guo <gch981213@gmail.com>2022-02-10 23:09:46 +0800
committerChuanhong Guo <gch981213@gmail.com>2022-02-14 11:59:12 +0800
commit2f024b79331141e2a62c9bf3601c803b26bde77b (patch)
treed34659ce8957c1bb384b4133be744045a6cc5640 /target/linux/ramips/patches-5.10/325-mt7621-fix-memory-detect.patch
parent5205010a546fed2227c208f6bd025b47398f4cf3 (diff)
downloadupstream-2f024b79331141e2a62c9bf3601c803b26bde77b.tar.gz
upstream-2f024b79331141e2a62c9bf3601c803b26bde77b.tar.bz2
upstream-2f024b79331141e2a62c9bf3601c803b26bde77b.zip
ramips: mt7621: do memory detection on KSEG1
It's reported that current memory detection code occasionally detects larger memory under some bootloaders. Current memory detection code tests whether address space wraps around on KSEG0, which is unreliable because it's cached. Rewrite memory size detection to perform the same test on KSEG1 instead. While at it, this patch also does the following two things: 1. use a fixed pattern instead of a random function pointer as the magic value. 2. add an additional memory write and a second comparison as part of the test to prevent possible smaller memory detection result due to leftover values in memory. Fixes: 6d91ddf517 ("ramips: mt7621: add support for memory detection") Reported-by: Rui Salvaterra <rsalvaterra@gmail.com> Tested-by: Rui Salvaterra <rsalvaterra@gmail.com> Signed-off-by: Chuanhong Guo <gch981213@gmail.com>
Diffstat (limited to 'target/linux/ramips/patches-5.10/325-mt7621-fix-memory-detect.patch')
-rw-r--r--target/linux/ramips/patches-5.10/325-mt7621-fix-memory-detect.patch58
1 files changed, 58 insertions, 0 deletions
diff --git a/target/linux/ramips/patches-5.10/325-mt7621-fix-memory-detect.patch b/target/linux/ramips/patches-5.10/325-mt7621-fix-memory-detect.patch
new file mode 100644
index 0000000000..ad072ec54e
--- /dev/null
+++ b/target/linux/ramips/patches-5.10/325-mt7621-fix-memory-detect.patch
@@ -0,0 +1,58 @@
+--- a/arch/mips/ralink/mt7621.c
++++ b/arch/mips/ralink/mt7621.c
+@@ -56,7 +56,9 @@
+ #define MT7621_GPIO_MODE_SDHCI_SHIFT 18
+ #define MT7621_GPIO_MODE_SDHCI_GPIO 1
+
+-static void *detect_magic __initdata = detect_memory_region;
++#define MT7621_MEM_TEST_PATTERN 0xaa5555aa
++
++static u32 detect_magic __initdata;
+
+ static struct rt2880_pmx_func uart1_grp[] = { FUNC("uart1", 0, 1, 2) };
+ static struct rt2880_pmx_func i2c_grp[] = { FUNC("i2c", 0, 3, 2) };
+@@ -142,24 +144,32 @@ static struct clk *__init mt7621_add_sys
+ return clk;
+ }
+
++static bool __init mt7621_addr_wraparound_test(phys_addr_t size)
++{
++ void *dm = (void *)KSEG1ADDR(&detect_magic);
++ if (CPHYSADDR(dm + size) >= MT7621_LOWMEM_MAX_SIZE)
++ return true;
++ __raw_writel(MT7621_MEM_TEST_PATTERN, dm);
++ if (__raw_readl(dm) != __raw_readl(dm + size))
++ return false;
++ __raw_writel(!MT7621_MEM_TEST_PATTERN, dm);
++ return __raw_readl(dm) == __raw_readl(dm + size);
++}
++
+ void __init mt7621_memory_detect(void)
+ {
+- void *dm = &detect_magic;
+ phys_addr_t size;
+
+- for (size = 32 * SZ_1M; size < 256 * SZ_1M; size <<= 1) {
+- if (!__builtin_memcmp(dm, dm + size, sizeof(detect_magic)))
+- break;
++ for (size = 32 * SZ_1M; size <= 256 * SZ_1M; size <<= 1) {
++ if (mt7621_addr_wraparound_test(size)) {
++ memblock_add(MT7621_LOWMEM_BASE, size);
++ return;
++ }
+ }
+
+- if ((size == 256 * SZ_1M) &&
+- (CPHYSADDR(dm + size) < MT7621_LOWMEM_MAX_SIZE) &&
+- __builtin_memcmp(dm, dm + size, sizeof(detect_magic))) {
+- memblock_add(MT7621_LOWMEM_BASE, MT7621_LOWMEM_MAX_SIZE);
+- memblock_add(MT7621_HIGHMEM_BASE, MT7621_HIGHMEM_SIZE);
+- } else {
+- memblock_add(MT7621_LOWMEM_BASE, size);
+- }
++ /* addr doesn't wrap around at dm + 256M, assume 512M memory. */
++ memblock_add(MT7621_LOWMEM_BASE, MT7621_LOWMEM_MAX_SIZE);
++ memblock_add(MT7621_HIGHMEM_BASE, MT7621_HIGHMEM_SIZE);
+ }
+
+ void __init ralink_clk_init(void)