Page 1 of 1

Detecting RPi4 programatically

Posted: Mon Sep 09, 2019 11:29 am
by bzt
Hi,

Is it possible to detect if an AArch64 image is running on an RPi3 or on an RPi4?

I have a feeling that I saw something similar here, using an ARM system register and comparing it's value against some magic constants. But no matter how hard I try, I simply can't find that post. It would help if I could remember something specific, but unfortunately I don't. Now I'm curious if it was just wishful dreaming on my part.

What came into my mind is that RPi4 can use a lot more memory than the RPi3. Maybe ID_AA64MMFR0 reports a wider memory bus on A72? Or is there any other system register that differs for sure?

Thanks
bzt

Re: Detecting RPi4 programatically

Posted: Mon Sep 09, 2019 1:24 pm
by topguy
Or just "grep xxx /proc/cpuinfo", where xxx is replaced by relevant value for Pi3 or Pi¤ maybe..

Re: Detecting RPi4 programatically

Posted: Mon Sep 09, 2019 1:52 pm
by DougieLawson
topguy wrote:
Mon Sep 09, 2019 1:24 pm
Or just "grep xxx /proc/cpuinfo", where xxx is replaced by relevant value for Pi3 or Pi¤ maybe..
That doesn't really work in bare metal as you don't get the /proc virtual filesystem. I guess a sneak peak at how the Linux kernel determines the hardware model is probably what's needed for the OP.

Re: Detecting RPi4 programatically

Posted: Mon Sep 09, 2019 2:25 pm
by LdB
He has to be talking about Linux because in true baremetal there is no way in hell you don't know what mode your in ... the Start address, IRQ table, opcodes and stack setups and EABI are different. You have to compile for all that at startup and if you start in 32 bit mode you are forever stuck in 32 bit mode. The one question you would never think about in true baremetal is gee I wonder if I started in AARCH64 and I know bzt knows all that :-) Put simply you really can't run 32bit code in 64bit mode it will crash and burn unless you did special engineering.

So the only possibility I could see is he is trying to pick it from a linux app. You can't directly do it from the app because a 32 bit app can be hosted in AARCH64 and I don't imagine linux is going to let you near the control registers. So I am guessing the suggestion above would be a good start point.

Re: Detecting RPi4 programatically

Posted: Mon Sep 09, 2019 2:34 pm
by rpdom
DougieLawson wrote:
Mon Sep 09, 2019 1:52 pm
topguy wrote:
Mon Sep 09, 2019 1:24 pm
Or just "grep xxx /proc/cpuinfo", where xxx is replaced by relevant value for Pi3 or Pi¤ maybe..
That doesn't really work in bare metal as you don't get the /proc virtual filesystem. I guess a sneak peak at how the Linux kernel determines the hardware model is probably what's needed for the OP.
In Bare Metal there is (or was, I haven't kept up to date recently) the Mailbox interface which can give details of the hardware.

Re: Detecting RPi4 programatically

Posted: Mon Sep 09, 2019 2:37 pm
by LdB
It will tell you model/version details etc but you can't get what mode the CPU is currently running in from it or launched with.

In baremetal just make a global flag default is clear ... set it if you enter in AARCh64 .. problem solved.

Re: Detecting RPi4 programatically

Posted: Mon Sep 09, 2019 2:42 pm
by rst
bzt wrote:
Mon Sep 09, 2019 11:29 am
Is it possible to detect if an AArch64 image is running on an RPi3 or on an RPi4?
You can use the MIDR_EL1 system control register. It reports the PartNum in [15:4], which is 0xD03 for the Cortex-A53 (RPi 3) and 0xD08 for the Cortex-A72 (RPi 4).

Re: Detecting RPi4 programatically

Posted: Tue Sep 10, 2019 2:36 pm
by bzt
Hi,

Thank you for your answers!

@LdB: it's not about 32 bit vs. 64 bit, but running AArch64 mode on A53 vs. A72. And I need that in bare metal, so not under Linux.

@rpdom: unfortunately mailbox is a no go, because A53 and A72 uses different MMIO base address. We need to know that before we can use mailboxes. Actually that's what I'm trying to do, to determine the MMIO base address in run-time.

@DougieLawson: peeking into how the Linux kernel fills up /proc/cpuinfo with data is actually a very good idea! I feel stupid that I haven't thought of that! Thanks!
rst wrote:
Mon Sep 09, 2019 2:42 pm
You can use the MIDR_EL1 system control register. It reports the PartNum in [15:4], which is 0xD03 for the Cortex-A53 (RPi 3) and 0xD08 for the Cortex-A72 (RPi 4).
That's exactly what I was looking for, thank you very very much!

Cheers,
bzt

EDIT: auch, I feel extremely stupid right now. Knowing the name of the system register, I was able to find that post, and... it was written by me. :-O However rst provided the PartNum value for the RPi4, which I didn't know, so thanks!

Re: Detecting RPi4 programatically

Posted: Tue Sep 10, 2019 5:01 pm
by rst
@bzt You are welcome.