Unified free RAM detection method
Hi, I am currently writing a bare metal OS and I am wondering if there is a non-hardcoded method of detecting all of the free ram (I.e. not used by the VC or by MMIO) on every raspberry PI? I know that the mailbox can be used to detect a single region of memory (on the qemu emulated raspb pi 1a+ I get a base of 0x0, and a length of 0x1c000000), but I know that for other raspberry PIs, the total ram is split in two by the GPU memory.
I know that on the raspberry pi 1, detecting memory after the kernel is as simple as putting a tag in the linker script at the end of the kernel image, and assuming that all memory before that is used by DTB and CLI args, but also know that on other versions the kernel is loaded in 0x200000 or 0x800000, so there must be some free RAM below that region.
Is there any programatic way to solve this or do you have to hardcode all of the memory regions?
I know that on the raspberry pi 1, detecting memory after the kernel is as simple as putting a tag in the linker script at the end of the kernel image, and assuming that all memory before that is used by DTB and CLI args, but also know that on other versions the kernel is loaded in 0x200000 or 0x800000, so there must be some free RAM below that region.
Is there any programatic way to solve this or do you have to hardcode all of the memory regions?
- cleverca22
- Posts: 9593
- Joined: Sat Aug 18, 2012 2:33 pm
Re: Unified free RAM detection method
the proper method is to use device-tree
it gives you a list of addr+size pairs, telling you exactly which ram is available for use by the arm cores
it gives you a list of addr+size pairs, telling you exactly which ram is available for use by the arm cores
Re: Unified free RAM detection method
Hmm, when I tried this using the rapberry pi 1 DTB, and looked at the memory map using the device tree dump in DTS format, this is what was in the memory section:cleverca22 wrote: ↑Sun Mar 30, 2025 10:53 amthe proper method is to use device-tree
it gives you a list of addr+size pairs, telling you exactly which ram is available for use by the arm cores
Code: Select all
memory@0 {
device_type = "memory";
reg = <0x00 0x00>;
};
- cleverca22
- Posts: 9593
- Joined: Sat Aug 18, 2012 2:33 pm
Re: Unified free RAM detection method
thats because the firmware changes the DTB at bootup, and provides the final DTB to your kernelpicoder90 wrote: ↑Sun Mar 30, 2025 9:56 pmHmm, when I tried this using the rapberry pi 1 DTB, and looked at the memory map using the device tree dump in DTS format, this is what was in the memory section:cleverca22 wrote: ↑Sun Mar 30, 2025 10:53 amthe proper method is to use device-tree
it gives you a list of addr+size pairs, telling you exactly which ram is available for use by the arm cores
The dtb was called bcm2708-rpi-b-plus.dtb.Code: Select all
memory@0 { device_type = "memory"; reg = <0x00 0x00>; };
this is because of variables like gpu_mem, that can change the layout, so the final answer can only be known at boot time
Re: Unified free RAM detection method
Ahh got it, so to use it bare metal side, you would have to write a parser for it. Cheers.
Return to "Bare metal, Assembly language"
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion