From fdaa73f9a1db40a1951d0ca6a4b1029d605e57c5 Mon Sep 17 00:00:00 2001 From: Dylan Corrales Date: Fri, 6 Nov 2026 00:06:43 -0500 Subject: [PATCH] ram: keep a total that is already a power of two rounded_num() counts the halvings it takes to reach zero and returns 1 << that count, so an exact power of two comes back doubled: 512 -> 1024. A kernel MemTotal is never one, which is why it never showed, but since #268 the Ambarella total is the top of the DSP carve-out, and on the S3L camera from #266 that is exactly 512M (0x07800000 + 0x18800000). Both stock and OpenIPC kernels reported `total: 1024M` there. Return n unchanged when it is already a power of two. backup.c rounds its total with the same function and gets the same fix. ambarella_test now covers the 512M case, a 114940 kB MemTotal (112 -> 128), 513 -> 1024 and 1 -> 1; without the fix the 512M and 1 cases fail. --- src/hal/ambarella_test.c | 8 ++++++++ src/hal/common.c | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/hal/ambarella_test.c b/src/hal/ambarella_test.c index d8c65187..63ca1c73 100644 --- a/src/hal/ambarella_test.c +++ b/src/hal/ambarella_test.c @@ -8,6 +8,7 @@ #include #include "hal/ambarella.h" +#include "hal/common.h" int chip_generation; char chip_name[128]; @@ -93,6 +94,13 @@ int main() { unlink(path); CHECK(!ambarella_iavmem_from_dt(path, &start, &size)); + /* ram.c rounds the total up to a power of two; on this board the total + * already is one and must stay 512M, not become 1024M. */ + CHECK(rounded_num((0x07800000u + 0x18800000u) / 1024 / 1024) == 512); + CHECK(rounded_num(112) == 128); /* a MemTotal of 114940 kB */ + CHECK(rounded_num(513) == 1024); + CHECK(rounded_num(1) == 1); + if (failures) return EXIT_FAILURE; puts("ambarella_test: all cases passed"); diff --git a/src/hal/common.c b/src/hal/common.c index b449d948..20fa94e9 100644 --- a/src/hal/common.c +++ b/src/hal/common.c @@ -268,6 +268,9 @@ unsigned long kernel_mem() { } uint32_t rounded_num(uint32_t n) { + if (n && !(n & (n - 1))) + return n; + int i; for (i = 0; n; i++) { n /= 2;