Skip to content

ram: keep a total that is already a power of two - #269

Merged
openipc-ai merged 1 commit into
OpenIPC:masterfrom
DeathCamel58:ram-power-of-two
Nov 6, 2026
Merged

openipc-ai merged 1 commit into
OpenIPC:masterfrom
DeathCamel58:ram-power-of-two

Conversation

@DeathCamel58

Copy link
Copy Markdown
Contributor

Follow-up to #268, from the testing in #266.

rounded_num() counts the halvings it takes to reach zero and returns 1 << count, so an exact power of two comes back doubled: rounded_num(512) is 1024. A kernel MemTotal is never a power of two, so this never showed. Since #268, though, the Ambarella total is the top of the DSP carve-out, and on the S3L camera in #266 that is exactly 512 MiB (0x07800000 + 0x18800000). ambarella_totalmem() returns the right 524288 KiB; the doubling happens in ram.c's rounding.

The fix returns n unchanged when it is already a power of two. backup.c rounds its total with the same function and gets the same fix.

Testing

  • ambarella_test gains checks for the 512M case, a 114940 kB MemTotal (112 → 128), 513 → 1024 and 1 → 1. Without the fix the 512M and 1 cases fail; with it all pass.
  • On the camera from SoC not recognised: Ambarella S3L (Amcrest IP4M-1026E / Dahua IPC-HFW1430SN) #266 (Amcrest IP4M-1026E, Ambarella S3L, 512 MiB): arm32 built with the CI toolchain (toolchain.hisilicon-hi3516cv100, musl) and packed with UPX 4.2.3 as in release.yml:
latest (7c51a7e) this PR
Stock Dahua 3.10.73 (IAVMEM from the boot log) total: 1024M, media: 392M total: 512M, media: 392M
OpenIPC 3.10.104 (/proc/device-tree/iavmem/reg) total: 1024M, media: 392M total: 512M, media: 392M

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
OpenIPC#268 the Ambarella total is the top of the DSP carve-out, and on the S3L
camera from OpenIPC#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.
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Preserve RAM totals that are already powers of two

🐞 Bug fix 🧪 Tests 🕐 Less than 10 minutes

Grey Divider

AI Description

• Keep exact power-of-two RAM totals unchanged instead of doubling them.
• Correct the displayed total for Ambarella S3L cameras with 512 MiB of RAM.
• Add regression checks for exact powers and totals that still need rounding.
Diagram

graph TD
  H["HAL total memory"] --> M["MiB conversion"] --> D{"Power of two?"}
  D -->|yes| K["Keep total"] --> O["RAM and backup"]
  D -->|no| R["Round upward"] --> O
Loading
High-Level Assessment

Keep the fix in the shared rounding helper: it corrects both RAM reporting and backup generation without changing Ambarella memory detection or duplicating logic at the call sites.

Files changed (2) +11 / -0

Bug fix (1) +3 / -0
common.cPreserve power-of-two inputs in rounded_num +3/-0

Preserve power-of-two inputs in rounded_num

• Returns a nonzero input unchanged when it is already a power of two. Other inputs retain the existing rounding behavior, correcting both RAM and backup callers.

src/hal/common.c

Tests (1) +8 / -0
ambarella_test.cCover exact and non-exact RAM rounding +8/-0

Cover exact and non-exact RAM rounding

• Adds checks for the S3L camera’s 512 MiB total and for 1 MiB, both of which must remain unchanged. Also verifies that 112 and 513 MiB still round up.

src/hal/ambarella_test.c

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can switch off images and animations for a plain-text comment

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@openipc-ai openipc-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct, thanks for finding it. I wrote in #268 that this camera should report 512M without checking the rounding in ram.c, and I'm glad your test caught it.

Verified at this commit:

  • rounded_num() has two callers, ram.c and backup.c.
  • Across 0 to 4999, the only values that change are exact powers of two; everything else rounds as before. 0 still gives 1.
  • ambarella_test, anyka_test, longse_test, reginfo_test, sensors_test and hal_hisi_test all pass.
  • With the fix removed, the 512M and 1 checks fail, as you describe.

Behaviour change to be aware of: this isn't Ambarella-only. Any HAL whose total lands exactly on a power of two was doubled too, and now reports the true figure. For backup.c, that is the value it writes when setTotalMem is set. Both are the correct result.

@openipc-ai
openipc-ai merged commit c92b388 into OpenIPC:master Nov 6, 2026
5 checks passed
@DeathCamel58
DeathCamel58 deleted the ram-power-of-two branch November 6, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants