Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firmware/raspberrypi: Add a get_throttled sysfs file #2397

Merged
merged 1 commit into from
Feb 27, 2018

Conversation

notro
Copy link
Contributor

@notro notro commented Feb 24, 2018

This is a result of the discussion in #2367.

Kernel log:

[  530.474085] Under-voltage detected! (0x00050005)
[  534.634106] Voltage normalised (0x00000000)

A simple script that polls on the sysfs file and shows the changes:

import select
import datetime

epoll = select.epoll()

file = open("/sys/devices/platform/soc/soc:firmware/get_throttled")
epoll.register(file.fileno(), select.EPOLLPRI | select.EPOLLERR)
status = file.read()

while(True):
    epoll.poll()
    file.seek(0)
    status = file.read()
    get_throttled = int(status, 16)
    print("%s 0x%08x" % (datetime.datetime.now().strftime('%H:%M:%S'), get_throttled))

epoll.unregister(file.fileno())
file.close()

A simple proof of concept for desktop notification:

import dbus
import select
import datetime
import signal

msg = """Under-voltage is blah blah not good.
See: http://some-explanation.org
"""

def quit(signo, _frame):
    exit(0)

for sig in ('TERM', 'HUP', 'INT'):
    signal.signal(getattr(signal, 'SIG'+sig), quit);

bus = dbus.SessionBus()
notifier = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
iface = dbus.Interface(notifier, 'org.freedesktop.Notifications')
nid = None

epoll = select.epoll()
file = open("/sys/devices/platform/soc/soc:firmware/get_throttled")
epoll.register(file.fileno(), select.EPOLLPRI | select.EPOLLERR)
status = file.read()

while(True):
    epoll.poll()
    file.seek(0)
    status = file.read()
    get_throttled = int(status, 16)

    if get_throttled & (1 << 16):
        try:
            nid = iface.Notify('get_throttled', 0, 'dialog-warning', 'Under-voltage!', msg, [], {}, 0)
        except dbus.exceptions.DBusException as e:
            sys.stderr.write("Error sending notification: %s\n" % e); sys.stderr.flush()
    elif nid:
        try: # The user might have closed it
            iface.CloseNotification(nid)
        except dbus.exceptions.DBusException:
            pass
        nid = None

epoll.unregister(file.fileno())
file.close()

dbus notification daemon (supports cancelling notifications):

sudo apt install dunst

Under-voltage due to inadequate power supplies is a recurring problem for
new Raspberry Pi users. There are visual indications that an
under-voltage situation is occuring like blinking power led and a
lightning icon on the desktop (not shown when using the vc4 driver), but
for new users it's not obvious that this signifies a critical situation.

This patch provides a twofold improvement to the situation:

Firstly it logs under-voltage events to the kernel log. This provides
information also for headless installations.

Secondly it provides a sysfs file to read the value. This improves on
'vcgencmd' by providing change notification. Userspace can poll on the
file and be notified of changes to the value.
A script can poll the file and use dbus notification to put a windows on
the desktop with information about the severity with a recommendation to
change the power supply. A link to more information can also be provided.
Only changes to the sticky bits are reported (cleared between readings).

Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
pr_info("Voltage normalised (0x%08x)\n", *value);
}

sysfs_notify(&fw->cl.dev->kobj, NULL, "get_throttled");
Copy link
Contributor

Choose a reason for hiding this comment

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

I know the polling rate is very low so the answer is largely moot, but is there a reason for waking any waiters regardless of whether the state has changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We only reach this if there are changes, see a couple of lines up:

if (new_sticky == old_sticky)
	return 0;

@pelwell
Copy link
Contributor

pelwell commented Feb 27, 2018

No objections.

@popcornmix popcornmix merged commit daa138d into raspberrypi:rpi-4.14.y Feb 27, 2018
popcornmix added a commit to raspberrypi/firmware that referenced this pull request Mar 5, 2018
kernel: Fix GCC 6.4.0 and 7.3.0 warnings
See: raspberrypi/linux#2413

kernel: audioinjector-octo: Add continuous clock feature
See: raspberrypi/linux#2409

kernel: overlays: Add 'upstream' overlay
See: raspberrypi/linux#2393

kernel: overlays: Add overlay for PiBell soundcard
See: https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=99784&p=1279490#p1278971

kernel: Removing (broken) RaspiDac3 support

kernel: i2c: bcm2835: Set up the rising/falling edge delays
See: raspberrypi/linux#2407

kernel: overlays: Add updated mmc1 alias to sdio overlays

kernel: config: Enable CONFIG_GPIO_MOCKUP module
See: raspberrypi/linux#2410

kernel: overlays: Rework sdio overlays to allow polling
See: raspberrypi/linux#2401

kernel: firmware/raspberrypi: Add a get_throttled sysfs file
See: raspberrypi/linux#2397

firmware: dtoverlay: Also allow fragment-<n> in overlays

firmware: i2c_gpio: Optimise and run clients faster
popcornmix added a commit to Hexxeh/rpi-firmware that referenced this pull request Mar 5, 2018
kernel: Fix GCC 6.4.0 and 7.3.0 warnings
See: raspberrypi/linux#2413

kernel: audioinjector-octo: Add continuous clock feature
See: raspberrypi/linux#2409

kernel: overlays: Add 'upstream' overlay
See: raspberrypi/linux#2393

kernel: overlays: Add overlay for PiBell soundcard
See: https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=99784&p=1279490#p1278971

kernel: Removing (broken) RaspiDac3 support

kernel: i2c: bcm2835: Set up the rising/falling edge delays
See: raspberrypi/linux#2407

kernel: overlays: Add updated mmc1 alias to sdio overlays

kernel: config: Enable CONFIG_GPIO_MOCKUP module
See: raspberrypi/linux#2410

kernel: overlays: Rework sdio overlays to allow polling
See: raspberrypi/linux#2401

kernel: firmware/raspberrypi: Add a get_throttled sysfs file
See: raspberrypi/linux#2397

firmware: dtoverlay: Also allow fragment-<n> in overlays

firmware: i2c_gpio: Optimise and run clients faster
@cyr-ius
Copy link

cyr-ius commented Mar 17, 2018

I compiled the last kernel 4.14.27 and since then I have the following message in loop in the kern.log
raspberrypi-firmware soc:firmware: Request 0x00030046 returned status 0x80000001
and i have no problem of under voltage
Pi Zero W
vcgencmd measure_clock arm
frequency(45)=1000106000
vcgencmd measure_clock core
frequency(1)=400000000
vcgencmd measure_volts
volt=1.2000V

vcgencmd measure_temp; vcgencmd get_throttled; vcgencmd measure_temp
temp=37.9'C
throttled=0x0
temp=37.9'C

I have the problem on my Pi Zero and Pi 2

How to stop this message ?

@pelwell
Copy link
Contributor

pelwell commented Mar 17, 2018

Answered elsewhere.

@jcea
Copy link

jcea commented Aug 11, 2020

I am using kernel "4.19.122-1-osmc" in a Raspberry Pi 3B+ Rev 1.3. The file "/sys/devices/platform/soc/soc:firmware/get_throttled" exists, but it always returns zero:

root@osmcpi:~# vcgencmd get_throttled
throttled=0x80008
root@osmcpi:~# cat "/sys/devices/platform/soc/soc:firmware/get_throttled" 
0

More details in https://discourse.osmc.tv/t/sys-devices-platform-soc-soc-firmware-get-throttled-not-working/86639

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.

5 participants