zt00
Posts: 7
Joined: Mon Sep 02, 2019 5:03 pm

Obtain CPU Temperature in Kernel

Mon Sep 02, 2019 5:05 pm

Dear All,

I am using a raspberry pi B+ that uses a Broadcom BCM2837 SoC with an ARMV8 processor. I want to get the cpu temperature in a Linux kernel file. Like in x86, I can use rdmsr_on_cpu function to load the temperature from MSR_IA32_THERM_STATUS register. But I don't know how to do it on Arm, can someone please guide me?

Any help would be greatly appreciated.

Sincerely,

ZT

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

Re: Obtain CPU Temperature in Kernel

Mon Sep 02, 2019 6:22 pm

Well, it's not ARM specific, but Pi specifc.

You can use the command vcgencmd measure_temp or you can read the contents of /sys/class/thermal/thermal_zone0/temp

andrum99
Posts: 829
Joined: Fri Jul 20, 2012 2:41 pm

Re: Obtain CPU Temperature in Kernel

Mon Sep 02, 2019 7:45 pm

zt00 wrote:
Mon Sep 02, 2019 5:05 pm
Dear All,

I am using a raspberry pi B+ that uses a Broadcom BCM2837 SoC with an ARMV8 processor. I want to get the cpu temperature in a Linux kernel file. Like in x86, I can use rdmsr_on_cpu function to load the temperature from MSR_IA32_THERM_STATUS register.
That's a very hardware-specific way of getting the CPU temperature. It is usually better to use a more abstracted view of the system. The /sys filesystem is where you can find the CPU temperature on all machines where it is supported and enabled.

zt00
Posts: 7
Joined: Mon Sep 02, 2019 5:03 pm

Re: Obtain CPU Temperature in Kernel

Tue Sep 03, 2019 1:14 pm

Thank you for the replies. But I want to get the temperature in a kernel driver. I think it will increase the running time if it makes a system call to read the temperature from a file. I am wondering if I could access the on-chip sensor directly or load the temperature from the register where the system saves the temperature

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7329
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Obtain CPU Temperature in Kernel

Tue Sep 03, 2019 2:36 pm

Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

zt00
Posts: 7
Joined: Mon Sep 02, 2019 5:03 pm

Re: Obtain CPU Temperature in Kernel

Wed Sep 04, 2019 2:49 pm

6by9 wrote:
Tue Sep 03, 2019 2:36 pm
https://github.com/raspberrypi/linux/bl ... _thermal.c is the thermal driver for BCM2835.
A naive question, why do we need to get the temperature by a drive? why can't we get access to the on-chip thermal sensor or the register directly?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7329
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Obtain CPU Temperature in Kernel

Wed Sep 04, 2019 3:06 pm

zt00 wrote:
Wed Sep 04, 2019 2:49 pm
6by9 wrote:
Tue Sep 03, 2019 2:36 pm
https://github.com/raspberrypi/linux/bl ... _thermal.c is the thermal driver for BCM2835.
A naive question, why do we need to get the temperature by a drive? why can't we get access to the on-chip thermal sensor or the register directly?
It abstracts out dependencies.

On a theoretical Pi5 suppose the thermal sensor changes either partially or totally.
- If you go through the driver route then that is already handled for you by the updates to the one driver.
- Similarly if the hardware moves base address, then that is handled for you by the device tree changes.
- If you've hacked things together in your driver and made assumptions about the hardware, then your driver is now broken.

Feel free to take the register accesses from bcm2835_thermal.c and duplicate them in your driver. You'll also need to tie it together with the device tree configuration for the driver - https://github.com/raspberrypi/linux/bl ... n.dtsi#L27
Just don't complain if things ever get updated and your driver breaks.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

zt00
Posts: 7
Joined: Mon Sep 02, 2019 5:03 pm

Re: Obtain CPU Temperature in Kernel

Wed Sep 04, 2019 6:17 pm

6by9 wrote:
Wed Sep 04, 2019 3:06 pm
zt00 wrote:
Wed Sep 04, 2019 2:49 pm
6by9 wrote:
Tue Sep 03, 2019 2:36 pm
https://github.com/raspberrypi/linux/bl ... _thermal.c is the thermal driver for BCM2835.
A naive question, why do we need to get the temperature by a drive? why can't we get access to the on-chip thermal sensor or the register directly?
It abstracts out dependencies.

On a theoretical Pi5 suppose the thermal sensor changes either partially or totally.
- If you go through the driver route then that is already handled for you by the updates to the one driver.
- Similarly if the hardware moves base address, then that is handled for you by the device tree changes.
- If you've hacked things together in your driver and made assumptions about the hardware, then your driver is now broken.

Feel free to take the register accesses from bcm2835_thermal.c and duplicate them in your driver. You'll also need to tie it together with the device tree configuration for the driver - https://github.com/raspberrypi/linux/bl ... n.dtsi#L27
Just don't complain if things ever get updated and your driver breaks.
Thanks for sharing this link. I have gone through the files in Linux kernel but couldn't find this. I have another question. I have done the same thing on a desktop, loading the temperature from the register. But I simply did it by using rdmsr_on_cpu(prev->cpu, MSR_IA32_THERM_STATUS, &eax, &edx) nothing else. Why is it so difficult to do it on arm?
Thank you for your patience. I really appreciate your help.

andrum99
Posts: 829
Joined: Fri Jul 20, 2012 2:41 pm

Re: Obtain CPU Temperature in Kernel

Wed Sep 04, 2019 7:58 pm

zt00 wrote:
Wed Sep 04, 2019 6:17 pm
Why is it so difficult to do it on arm?
From what I can gather, because ARM is targeted at the embedded market, anything that is not needed is left out of the processor core. So when the ARM cores are added to a chip, they don't have temperature sensors in them - you need to add your own hardware block. Broadcom added their own when they designed the 2835, and it is accessed in that way as an device separate to the ARM cores. This differs to how x86 tends to be done, in that the processors are built for desktop or laptop machines, so the CPU cores can have other things in them that use up power.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7329
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Obtain CPU Temperature in Kernel

Wed Sep 04, 2019 8:08 pm

zt00 wrote:
Wed Sep 04, 2019 6:17 pm
Thanks for sharing this link. I have gone through the files in Linux kernel but couldn't find this. I have another question. I have done the same thing on a desktop, loading the temperature from the register. But I simply did it by using rdmsr_on_cpu(prev->cpu, MSR_IA32_THERM_STATUS, &eax, &edx) nothing else. Why is it so difficult to do it on arm?
Mainly because Intel is a CPU, vs ARM generally being an SoC. x86 also has a huge amount of legacy rubbish associated, and things left in there for backwards compatibility.

On ARM a thermal sensor is not part of the CPU, it's a peripheral. You could have multiple thermal sensors spread across the SoC, so those need to be configurable, hence device tree.
There's also no obligation for all thermal peripherals to return temperatures with the same ranges or units, hence using a driver to unify the behaviour.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

zt00
Posts: 7
Joined: Mon Sep 02, 2019 5:03 pm

Re: Obtain CPU Temperature in Kernel

Sun Sep 08, 2019 6:12 pm

andrum99 wrote:
Wed Sep 04, 2019 7:58 pm
zt00 wrote:
Wed Sep 04, 2019 6:17 pm
Why is it so difficult to do it on arm?
From what I can gather, because ARM is targeted at the embedded market, anything that is not needed is left out of the processor core. So when the ARM cores are added to a chip, they don't have temperature sensors in them - you need to add your own hardware block. Broadcom added their own when they designed the 2835, and it is accessed in that way as an device separate to the ARM cores. This differs to how x86 tends to be done, in that the processors are built for desktop or laptop machines, so the CPU cores can have other things in them that use up power.
Thank you for your detailed and helpful explanation.

zt00
Posts: 7
Joined: Mon Sep 02, 2019 5:03 pm

Re: Obtain CPU Temperature in Kernel

Sun Sep 08, 2019 6:16 pm

6by9 wrote:
Wed Sep 04, 2019 8:08 pm
zt00 wrote:
Wed Sep 04, 2019 6:17 pm
Thanks for sharing this link. I have gone through the files in Linux kernel but couldn't find this. I have another question. I have done the same thing on a desktop, loading the temperature from the register. But I simply did it by using rdmsr_on_cpu(prev->cpu, MSR_IA32_THERM_STATUS, &eax, &edx) nothing else. Why is it so difficult to do it on arm?
Mainly because Intel is a CPU, vs ARM generally being an SoC. x86 also has a huge amount of legacy rubbish associated, and things left in there for backwards compatibility.

On ARM a thermal sensor is not part of the CPU, it's a peripheral. You could have multiple thermal sensors spread across the SoC, so those need to be configurable, hence device tree.
There's also no obligation for all thermal peripherals to return temperatures with the same ranges or units, hence using a driver to unify the behaviour.
Thank you for your detailed and helpful explanation.

Gene1934
Posts: 53
Joined: Tue May 02, 2017 12:47 pm

Re: Obtain CPU Temperature in Kernel

Tue Sep 10, 2019 1:34 am

according to the command at the start of this thread, /sys/class/thermal/thermal_zone0/temp, I get a large integer of 34862, which doesn't seem to change. How is that translated into a meaningful figure? This pi is actively fan cooled.

Thanks.

Gene1934

User avatar
jcyr
Posts: 357
Joined: Sun Apr 23, 2017 1:31 pm
Location: Atlanta

Re: Obtain CPU Temperature in Kernel

Tue Sep 10, 2019 2:19 am

Gene1934 wrote:
Tue Sep 10, 2019 1:34 am
according to the command at the start of this thread, /sys/class/thermal/thermal_zone0/temp, I get a large integer of 34862, which doesn't seem to change. How is that translated into a meaningful figure? This pi is actively fan cooled.

Thanks.

Gene1934
Divide it by 1000 to get the temperature in degrees Celsius.

Divide it by 555.555, then add 32 to get the temperature in degrees Fahrenheit.
It's um...uh...well it's kinda like...and it's got a bit of...

RonR
Posts: 520
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Obtain CPU Temperature in Kernel

Tue Sep 10, 2019 2:26 am

Gene1934 wrote:
Tue Sep 10, 2019 1:34 am
according to the command at the start of this thread, /sys/class/thermal/thermal_zone0/temp, I get a large integer of 34862, which doesn't seem to change. How is that translated into a meaningful figure? This pi is actively fan cooled.

That is 34.862 degrees centigrade. Using nano, copy/paste the following into a script file named 'temp', make it executable (chmod +x temp), and run it (./temp) to display the CPU temperature:

Code: Select all

#!/bin/bash

TEMP=$(cat /sys/class/thermal/thermal_zone0/temp)

TEMP1=$((TEMP / 1000))
TEMP2=$((TEMP % 1000))

printf "Temp: %d.%.3d C\n" ${TEMP1} ${TEMP2}

jj_0
Posts: 69
Joined: Wed Jul 11, 2012 7:07 am

Re: Obtain CPU Temperature in Kernel

Tue Sep 10, 2019 5:10 am

zt00 wrote:
Wed Sep 04, 2019 2:49 pm
6by9 wrote:
Tue Sep 03, 2019 2:36 pm
https://github.com/raspberrypi/linux/bl ... _thermal.c is the thermal driver for BCM2835.
A naive question, why do we need to get the temperature by a drive? why can't we get access to the on-chip thermal sensor or the register directly?
Unix/Linux philosophy is to present 'everything' as a file. The sys/class/thermal/thermal_zone0/temp file isn't physically present on a drive, accessing it either causes a call to the kernel temperature driver or uses an already collected value stored in memory (I'm not sure which).

Return to “Advanced users”