bzt
Posts: 393
Joined: Sat Oct 14, 2017 9:57 pm

Detecting RPi4 programatically

Mon Sep 09, 2019 11:29 am

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

User avatar
topguy
Posts: 5969
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Detecting RPi4 programatically

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..

User avatar
DougieLawson
Posts: 36583
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Detecting RPi4 programatically

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

LdB
Posts: 1328
Joined: Wed Dec 07, 2016 2:29 pm

Re: Detecting RPi4 programatically

Mon Sep 09, 2019 2:25 pm

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.
Last edited by LdB on Mon Sep 09, 2019 2:36 pm, edited 1 time in total.

User avatar
rpdom
Posts: 15618
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Detecting RPi4 programatically

Mon Sep 09, 2019 2:34 pm

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.

LdB
Posts: 1328
Joined: Wed Dec 07, 2016 2:29 pm

Re: Detecting RPi4 programatically

Mon Sep 09, 2019 2:37 pm

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.

rst
Posts: 416
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: Detecting RPi4 programatically

Mon Sep 09, 2019 2:42 pm

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).

bzt
Posts: 393
Joined: Sat Oct 14, 2017 9:57 pm

Re: Detecting RPi4 programatically

Tue Sep 10, 2019 2:36 pm

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!

rst
Posts: 416
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: Detecting RPi4 programatically

Tue Sep 10, 2019 5:01 pm

@bzt You are welcome.

Return to “Bare metal, Assembly language”