power: reset: Introduce the Power State Change Reasons Recording (PSCRR) framework
Subsystems (8)
v16 cover power: reset: Introduce the Power State Change Reasons Recording (PSCRR) framework
Hello all,
A system power-state transition - a shutdown or a reboot - rarely has a single, unambiguous cause, and no one component sees the whole picture: the PMIC, the watchdog, the SoC reset registers, the bootloader and software each only know their own part, so there is no single source of truth.
PSCRR gives these sources one central place to report side by side. Every provider keeps its own view - several providers, and several reasons within a provider, can be reported at once and are deliberately not collapsed into a single "winning" cause (a bit of a minority report). On top of that it adds a software-backed recorder, so software-detected reasons - under-voltage, over-temperature, a watchdog pretimeout, a panic, a controlled reboot - are captured too and survive into the next boot.
The NVMEM-cell binding a recorder needs to pick its storage is still deadlocked, and several projects already need the framework, so this v13 posts the framework, its reboot/reason infrastructure and the sysfs documentation only; the provider drivers (NVMEM, PMIC, ...) and the tests follow separately once the binding is resolved. One built-in provider, reporting the bootloader's device-tree /chosen/reset-source, is kept so the framework is useful on its own without a new binding.
For followup drivers, see:
https://github.com/olerem/linux-2.6/tree/v7.2/topic/pscr-v12
Changes in v16:
- Add Reviewed-by tags from Bradley Morgan (patches 2-4) and Daniel
Lezcano (patches 1-3).
- Drop the unused PSCR_MAX_REASON macro - Daniel Lezcano.
- Rename from "Invalid" to "Invalid reason" - Daniel Lezcano.
- Link to v15: https://lore.kernel.org/r/[email protected]
Changes in v15:
- Add static_assert() to keep psc_reason_descs and PSCR_REASON_COUNT in sync.
- Fix pscrr error codes: use -EOPNOTSUPP for unsupported recording and -EINVAL
for unsupported reasons.
- pscrr: add comments explaining safe lockless provider access in sysfs
callbacks (Matti Vaittinen)
- Add Reviewed-by tags from Matti Vaittine.
- Link to v14: https://lore.kernel.org/r/[email protected]
Changes in v14:
- Ignore v13, it was mistakenly posted as a reply to Oleksij's v12 patch series.
Changes in v13:
- Rework pscrr_provider_register() based on review feedback from Francesco Valla:
move the pscrr_root check before allocation and narrow pscrr_lock scope.
- Carry the series forward with Faruque Ansari as author and Oleksij Rempel as co-developer, and vice versa where required.
- Link to v12: https://lore.kernel.org/r/[email protected]
Changes in v12:
- Drop all provider drivers and their tests; post the framework alone. The NVMEM-cell binding a recorder needs to pick its storage is still deadlocked, and several projects already need the framework - so unblock the core now and let the providers (NVMEM, PMIC, ...) follow separately.
- Rework into a multi-provider design (per-provider /sys/kernel/pscrr/ directories); add reason tokens and a built-in /chosen/reset-source provider.
Changes in v11:
- add missing break reported by kernel test robot <[email protected]>
Changes in v10:
- add some add Reviewed-by tags
- regulator_handle_critical: set pscr = PSCR_UNKNOWN for default case
- make g_pscrr static
Changes in v9:
- Remove redundant pr_crit() messages before hw_protection_trigger()
- Replace psc_reason_to_str() switch with static const string array
- Mark psc_last_reason as static
Changes in v8:
- Use DEFINE_GUARD() and guard(g_pscrr) for scoped locking of the global pscrr_core struct
- Replace manual mutex_lock/unlock with automatic cleanup-based guard() usage
- Centralize backend and locking state in struct pscrr_core
- Prepare for future multi-backend support with clean encapsulation
- Improve sysfs documentation
Changes in v7:
- document expected values in sysfs documentation
- make write support optional
Changes in v6:
- add sysfs documentation
- push core part of the reset reason into kernel/reboot.c
Changes in v5:
- fix compile with NVMEM=n and potential issues with NVMEM=m
Changes in v4:
- fix compile with CONFIG_PSCRR=n
Changes in v3:
- rework to remove devicetree dependencies
- extend NVMEM to search devices and cells by names
Changes in v2:
- rename the framework from PSCR to PSCRR (last R is for Recorder)
- extend the power-on reason header and reuse it for the detected reason
- rebase on top of v6.8-rc1
---
Faruque Ansari (6):
power: Extend power_on_reason.h for upcoming PSCRR framework
reboot: hw_protection_trigger: use standardized numeric shutdown/reboot reasons instead of strings
reboot: add parsable tokens for power state change reasons
reboot: extend psc_reason with power-on and reset causes
power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage
Documentation: Add sysfs documentation for PSCRR
Documentation/ABI/testing/sysfs-kernel-pscrr | 108 +++++ MAINTAINERS | 11 + drivers/platform/chrome/cros_ec_lpc.c | 2 +- drivers/power/reset/Kconfig | 2 + drivers/power/reset/Makefile | 1 + drivers/power/reset/pscrr/Kconfig | 33 ++ drivers/power/reset/pscrr/Makefile | 2 + drivers/power/reset/pscrr/pscrr.c | 640 +++++++++++++++++++++++++++ drivers/regulator/core.c | 16 +- drivers/regulator/irq_helpers.c | 9 +- drivers/thermal/thermal_core.c | 3 +- include/linux/power/power_on_reason.h | 5 + include/linux/pscrr.h | 107 +++++ include/linux/reboot.h | 108 ++++- kernel/reboot.c | 169 ++++++- 15 files changed, 1194 insertions(+), 22 deletions(-)
---
base-commit: 5c4d4169604b335c38bbc79bc1fc03042981fc6f
change-id: 20260924-pscrr-framework-df3c1661565b
Best regards,
Signature · 1 line
Faruque Ansari <[email protected]>
PATCH v16 1/6 power: Extend power_on_reason.h for upcoming PSCRR framework
Prepare for the introduction of the Power State Change Reason Recorder (PSCRR) framework by expanding the power_on_reason.h header. This extension includes new power-on reasons:
- POWER_ON_REASON_OVER_CURRENT for over-current conditions.
- POWER_ON_REASON_REGULATOR_FAILURE for regulator failures.
- POWER_ON_REASON_OVER_TEMPERATURE for over temperature situations.
- POWER_ON_REASON_EC_PANIC for EC panics.
- POWER_ON_REASON_EXTERNAL for an external reset source.
changes v12:
- add POWER_ON_REASON_EXTERNAL
changes v10:
- add Reviewed-by: Sebastian Reichel ...
changes v6:
- add POWER_ON_REASON_EC_PANIC
- s/POWER_ON_REASON_OVERTEMPERATURE/POWER_ON_REASON_OVER_TEMPERATURE
--- include/linux/power/power_on_reason.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/power/power_on_reason.h +5 -0
@@ -15,5 +15,10 @@#define POWER_ON_REASON_XTAL_FAIL "crystal oscillator failure"#define POWER_ON_REASON_BROWN_OUT "brown-out reset"#define POWER_ON_REASON_UNKNOWN "unknown reason" +#define POWER_ON_REASON_OVER_CURRENT "over current" +#define POWER_ON_REASON_REGULATOR_FAILURE "regulator failure" +#define POWER_ON_REASON_OVER_TEMPERATURE "over temperature" +#define POWER_ON_REASON_EC_PANIC "EC panic" +#define POWER_ON_REASON_EXTERNAL "external reset"#endif /* POWER_ON_REASON_H */
Signature · 1 line
2.34.1
PATCH v16 2/6 reboot: hw_protection_trigger: use standardized numeric shutdown/reboot reasons instead of strings
In reply to Faruque AnsariPrepares the kernel for the Power State Change Reason (PSCR) recorder, which will store shutdown and reboot reasons in persistent storage.
Instead of using string-based reason descriptions, which are often too large to fit within limited storage spaces (e.g., RTC clocks with only 8 bits of battery-backed storage), we introduce enum psc_reason. This enumerates predefined reasons for power state changes, making it efficient to store and retrieve shutdown causes.
Key changes:
- Introduced enum psc_reason, defining structured reasons for power state changes.
- Replaced string-based shutdown reasons with psc_reason identifiers.
- Implemented get_psc_reason() and set_psc_reason() for tracking the last shutdown cause.
- Added psc_reason_to_str() to map enum values to human-readable strings.
- Updated hw_protection_trigger() to use psc_reason instead of string parameters.
- Updated all consumers of hw_protection_trigger() to pass an appropriate psc_reason value instead of a string.
- All structured logs now go through a single pr_emerg() in __hw_protection_trigger(), providing consistent output:
HARDWARE PROTECTION <action>: <reason-code> (<reason-string>)
changes v16:
- Add Reviewed-by tag from Bradley Morgan.
- Add Reviewed-by tag from Daniel Lezcano.
- Remove unused PSCR_MAX_REASON macro - Daniel Lezcano.
- Rename "Invalid" string to "Invalid reason" - Daniel Lezcano changes v12:
- drop Tested-by: Faruque Ansari and Francesco Valla; the end-to-end recording flow they tested has been reworked (multi-provider) and the recorder driver is no longer part of the series - re-testing is welcome
changes v10
- regulator_handle_critical: set pscr = PSCR_UNKNOWN for default case
- add Acked-by: Daniel Lezcano ..
changes v9:
- Remove redundant pr_crit() messages before hw_protection_trigger()
- Replace psc_reason_to_str() switch with static const string array
- Mark psc_last_reason as static
changes v8:
- add Acked/Reviewed-by.
changes v6:
- added in this version
(cherry picked from commit 29591d88486c392e2264de73c4e90106f181b33c)
--- drivers/platform/chrome/cros_ec_lpc.c | 2 +- drivers/regulator/core.c | 16 ++--- drivers/regulator/irq_helpers.c | 9 +-- drivers/thermal/thermal_core.c | 3 +- include/linux/reboot.h | 78 ++++++++++++++++++++++- kernel/reboot.c | 112 ++++++++++++++++++++++++++++++++-- 6 files changed, 198 insertions(+), 22 deletions(-)
drivers/platform/chrome/cros_ec_lpc.c +1 -1
@@ -455,7 +455,7 @@ static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)blocking_notifier_call_chain (&ec_dev->panic_notifier,0 , ec_dev);kobject_uevent_env (&ec_dev->dev->kobj,KOBJ_CHANGE , (char **)env);/* Begin orderly shutdown. EC will force reset after a short period. */ -__hw_protection_trigger ("CrOS EC Panic" ,-1 ,HWPROT_ACT_SHUTDOWN ); +__hw_protection_trigger (PSCR_EC_PANIC ,-1 ,HWPROT_ACT_SHUTDOWN );/* Do not query for other events after a panic is reported */ return ; }
drivers/regulator/core.c +8 -8
@@ -5551,26 +5551,26 @@ EXPORT_SYMBOL_GPL(regulator_bulk_free);static void regulator_handle_critical (struct regulator_dev *rdev,unsigned long event) { -const char *reason =NULL ; +enum psc_reason pscr;if (!rdev->constraints->system_critical)return ;switch (event) {case REGULATOR_EVENT_UNDER_VOLTAGE : - reason ="System critical regulator: voltage drop detected" ; + pscr =PSCR_UNDER_VOLTAGE ;break ;case REGULATOR_EVENT_OVER_CURRENT : - reason ="System critical regulator: over-current detected" ; + pscr =PSCR_OVER_CURRENT ;break ;case REGULATOR_EVENT_FAIL : - reason ="System critical regulator: unknown error" ; + pscr =PSCR_REGULATOR_FAILURE ; +break ; +default : + pscr =PSCR_UNKNOWN ; } -if (!reason) -return ; - -hw_protection_trigger (reason, +hw_protection_trigger (pscr, rdev->constraints->uv_less_critical_window_ms); }
drivers/regulator/irq_helpers.c +5 -4
@@ -64,15 +64,16 @@ static void regulator_notifier_isr_work(struct work_struct *work) reread:if (d->fatal_cnt && h->retry_cnt > d->fatal_cnt) {if (!d->die) -return hw_protection_trigger ("Regulator HW failure? - no IC recovery" , +return hw_protection_trigger (PSCR_REGULATOR_FAILURE ,REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS ); + ret = d->die (rid);/* * If the 'last resort' IC recovery failed we will have * nothing else left to do... */ if (ret) -return hw_protection_trigger ("Regulator HW failure. IC recovery failed" , +return hw_protection_trigger (PSCR_REGULATOR_FAILURE ,REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS );/*
@@ -263,13 +264,13 @@ static irqreturn_t regulator_notifier_isr(int irq, void *data)if (d->fatal_cnt && h->retry_cnt > d->fatal_cnt) {/* If we have no recovery, just try shut down straight away */ if (!d->die) { -hw_protection_trigger ("Regulator failure. Retry count exceeded" , +hw_protection_trigger (PSCR_REGULATOR_FAILURE ,REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS ); }else { ret = d->die (rid);/* If die() failed shut down as a last attempt to save the HW */ if (ret) -hw_protection_trigger ("Regulator failure. Recovery failed" , +hw_protection_trigger (PSCR_REGULATOR_FAILURE ,REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS ); } }
drivers/thermal/thermal_core.c +1 -2
@@ -319,11 +319,10 @@ static void thermal_zone_device_halt(struct thermal_zone_device *tz,* Its a must for forced_emergency_poweroff_work to be scheduled. */ int poweroff_delay_ms =CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS ; -const char *msg ="Temperature too high" ;dev_emerg (&tz->device,"%s: critical temperature reached\n" , tz->type); -__hw_protection_trigger (msg, poweroff_delay_ms, action); +__hw_protection_trigger (PSCR_OVER_TEMPERATURE , poweroff_delay_ms, action); }void thermal_zone_device_critical (struct thermal_zone_device *tz)
include/linux/reboot.h +75 -3
105 lines not on this page · raw message
kernel/reboot.c +108 -4
145 lines not on this page · raw message
Signature · 1 line
2.34.1
kernel test robot
build botRe: [PATCH v16 2/6] reboot: hw_protection_trigger: use standardized numeric shutdown/reboot reasons instead of strings
kernel test robot noticed the following build errors:
Hi Faruque,
kernel test robot noticed the following build errors:
[auto build test ERROR on 5c4d4169604b335c38bbc79bc1fc03042981fc6f]
url: https://github.com/intel-lab-lkp/linux/commits/Faruque-Ansari/power-Extend-power_on_reason-h-for-upcoming-PSCRR-framework/20261007-123636 base: 5c4d4169604b335c38bbc79bc1fc03042981fc6f patch link: https://lore.kernel.org/r/20261007-pscrr-framework-v16-2-3642c277bebb%40oss.qualcomm.com patch subject: [PATCH v16 2/6] reboot: hw_protection_trigger: use standardized numeric shutdown/reboot reasons instead of strings config: um-allmodconfig (https://download.01.org/0day-ci/archive/20261013/[email protected]/config) compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18) rustc: rustc 1.96.0 (ac68faa20 2026-05-25) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261013/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> drivers/power/supply/macsmc-power.c:587:25: error: incompatible pointer to integer conversion passing 'char[37]' to parameter of type 'enum psc_reason' [-Wint-conversion]
587 | hw_protection_trigger("Battery voltage below design minimum", 1500);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ include/linux/reboot.h:276:58: note: passing argument to parameter 'reason' here
276 | static inline void hw_protection_trigger(enum psc_reason reason,
| ^
1 error generated.
vim +587 drivers/power/supply/macsmc-power.c
0ebf821cf6c75de Hector Martin 2026-02-17 554
0ebf821cf6c75de Hector Martin 2026-02-17 555 static void macsmc_power_critical_work(struct work_struct *wrk)
0ebf821cf6c75de Hector Martin 2026-02-17 556 {
0ebf821cf6c75de Hector Martin 2026-02-17 557 struct macsmc_power *power = container_of(wrk, struct macsmc_power, critical_work);
0ebf821cf6c75de Hector Martin 2026-02-17 558 u16 bitv, b0av;
0ebf821cf6c75de Hector Martin 2026-02-17 559 u32 bcf0;
0ebf821cf6c75de Hector Martin 2026-02-17 560
0ebf821cf6c75de Hector Martin 2026-02-17 561 if (!power->batt)
0ebf821cf6c75de Hector Martin 2026-02-17 562 return;
0ebf821cf6c75de Hector Martin 2026-02-17 563
0ebf821cf6c75de Hector Martin 2026-02-17 564 /*
0ebf821cf6c75de Hector Martin 2026-02-17 565 * Avoid duplicate atempts at emergency shutdown
0ebf821cf6c75de Hector Martin 2026-02-17 566 */
0ebf821cf6c75de Hector Martin 2026-02-17 567 if (power->emergency_shutdown_triggered || system_state > SYSTEM_RUNNING)
0ebf821cf6c75de Hector Martin 2026-02-17 568 return;
0ebf821cf6c75de Hector Martin 2026-02-17 569
0ebf821cf6c75de Hector Martin 2026-02-17 570 /*
0ebf821cf6c75de Hector Martin 2026-02-17 571 * EMERGENCY: Check voltage vs design minimum.
0ebf821cf6c75de Hector Martin 2026-02-17 572 * If we are below BITV, the battery is physically exhausted.
0ebf821cf6c75de Hector Martin 2026-02-17 573 * We must shut down NOW to protect the filesystem.
0ebf821cf6c75de Hector Martin 2026-02-17 574 */
0ebf821cf6c75de Hector Martin 2026-02-17 575 if (apple_smc_read_u16(power->smc, SMC_KEY(BITV), &bitv) >= 0 &&
0ebf821cf6c75de Hector Martin 2026-02-17 576 apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &b0av) >= 0 &&
0ebf821cf6c75de Hector Martin 2026-02-17 577 b0av < bitv) {
0ebf821cf6c75de Hector Martin 2026-02-17 578 power->emergency_shutdown_triggered = true;
0ebf821cf6c75de Hector Martin 2026-02-17 579 dev_emerg(power->dev,
0ebf821cf6c75de Hector Martin 2026-02-17 580 "Battery voltage (%d mV) below design minimum (%d mV)! Emergency shutdown.\n",
0ebf821cf6c75de Hector Martin 2026-02-17 581 b0av, bitv);
0ebf821cf6c75de Hector Martin 2026-02-17 582
0ebf821cf6c75de Hector Martin 2026-02-17 583 /*
0ebf821cf6c75de Hector Martin 2026-02-17 584 * Shutdown is now imminent. Kick userspace again and give it some
0ebf821cf6c75de Hector Martin 2026-02-17 585 * brief time to (hopefully) flush what's needed, before forcing.
0ebf821cf6c75de Hector Martin 2026-02-17 586 */
0ebf821cf6c75de Hector Martin 2026-02-17 @587 hw_protection_trigger("Battery voltage below design minimum", 1500);
0ebf821cf6c75de Hector Martin 2026-02-17 588 }
0ebf821cf6c75de Hector Martin 2026-02-17 589
0ebf821cf6c75de Hector Martin 2026-02-17 590 /*
0ebf821cf6c75de Hector Martin 2026-02-17 591 * Avoid duplicate attempts at orderly shutdown.
0ebf821cf6c75de Hector Martin 2026-02-17 592 * Voltage check is above this as we may want to
0ebf821cf6c75de Hector Martin 2026-02-17 593 * "upgrade" an orderly shutdown to a critical power
0ebf821cf6c75de Hector Martin 2026-02-17 594 * off if voltage drops.
0ebf821cf6c75de Hector Martin 2026-02-17 595 */
0ebf821cf6c75de Hector Martin 2026-02-17 596 if (power->orderly_shutdown_triggered || system_state > SYSTEM_RUNNING)
0ebf821cf6c75de Hector Martin 2026-02-17 597 return;
0ebf821cf6c75de Hector Martin 2026-02-17 598
0ebf821cf6c75de Hector Martin 2026-02-17 599 /*
0ebf821cf6c75de Hector Martin 2026-02-17 600 * Check if SMC flagged the battery as empty.
0ebf821cf6c75de Hector Martin 2026-02-17 601 * We trigger a graceful shutdown to let the OS save data.
0ebf821cf6c75de Hector Martin 2026-02-17 602 */
f7b253a6e217f71 Sasha Finkelstein 2026-07-12 603 if (macsmc_battery_read_bcf0(power, &bcf0) == 0 && bcf0 != 0) {
0ebf821cf6c75de Hector Martin 2026-02-17 604 power->orderly_shutdown_triggered = true;
0ebf821cf6c75de Hector Martin 2026-02-17 605 dev_crit(power->dev, "Battery critical (empty flag set). Triggering orderly shutdown.\n");
0ebf821cf6c75de Hector Martin 2026-02-17 606 orderly_poweroff(true);
0ebf821cf6c75de Hector Martin 2026-02-17 607 }
0ebf821cf6c75de Hector Martin 2026-02-17 608 }
0ebf821cf6c75de Hector Martin 2026-02-17 609 --
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v16 2/6] reboot: hw_protection_trigger: use standardized numeric shutdown/reboot reasons instead of strings
In reply to Faruque Ansarikernel test robot noticed the following build errors:
Hi Faruque,
kernel test robot noticed the following build errors:
[auto build test ERROR on 5c4d4169604b335c38bbc79bc1fc03042981fc6f]
url: https://github.com/intel-lab-lkp/linux/commits/Faruque-Ansari/power-Extend-power_on_reason-h-for-upcoming-PSCRR-framework/20261007-123636 base: 5c4d4169604b335c38bbc79bc1fc03042981fc6f patch link: https://lore.kernel.org/r/20261007-pscrr-framework-v16-2-3642c277bebb%40oss.qualcomm.com patch subject: [PATCH v16 2/6] reboot: hw_protection_trigger: use standardized numeric shutdown/reboot reasons instead of strings config: powerpc-allmodconfig (https://download.01.org/0day-ci/archive/20261013/[email protected]/config) compiler: powerpc64-linux-gcc (GCC) 16.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261013/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
drivers/power/supply/macsmc-power.c: In function 'macsmc_power_critical_work':
>> drivers/power/supply/macsmc-power.c:587:39: error: incompatible type for argument 1 of 'hw_protection_trigger'
587 | hw_protection_trigger("Battery voltage below design minimum", 1500);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| char * In file included from drivers/power/supply/macsmc-power.c:22:
include/linux/reboot.h:276:58: note: expected 'enum psc_reason' but argument is of type 'char *'
276 | static inline void hw_protection_trigger(enum psc_reason reason,
| ~~~~~~~~~~~~~~~~^~~~~~
vim +/hw_protection_trigger +587 drivers/power/supply/macsmc-power.c
0ebf821cf6c75d Hector Martin 2026-02-17 554
0ebf821cf6c75d Hector Martin 2026-02-17 555 static void macsmc_power_critical_work(struct work_struct *wrk)
0ebf821cf6c75d Hector Martin 2026-02-17 556 {
0ebf821cf6c75d Hector Martin 2026-02-17 557 struct macsmc_power *power = container_of(wrk, struct macsmc_power, critical_work);
0ebf821cf6c75d Hector Martin 2026-02-17 558 u16 bitv, b0av;
0ebf821cf6c75d Hector Martin 2026-02-17 559 u32 bcf0;
0ebf821cf6c75d Hector Martin 2026-02-17 560
0ebf821cf6c75d Hector Martin 2026-02-17 561 if (!power->batt)
0ebf821cf6c75d Hector Martin 2026-02-17 562 return;
0ebf821cf6c75d Hector Martin 2026-02-17 563
0ebf821cf6c75d Hector Martin 2026-02-17 564 /*
0ebf821cf6c75d Hector Martin 2026-02-17 565 * Avoid duplicate atempts at emergency shutdown
0ebf821cf6c75d Hector Martin 2026-02-17 566 */
0ebf821cf6c75d Hector Martin 2026-02-17 567 if (power->emergency_shutdown_triggered || system_state > SYSTEM_RUNNING)
0ebf821cf6c75d Hector Martin 2026-02-17 568 return;
0ebf821cf6c75d Hector Martin 2026-02-17 569
0ebf821cf6c75d Hector Martin 2026-02-17 570 /*
0ebf821cf6c75d Hector Martin 2026-02-17 571 * EMERGENCY: Check voltage vs design minimum.
0ebf821cf6c75d Hector Martin 2026-02-17 572 * If we are below BITV, the battery is physically exhausted.
0ebf821cf6c75d Hector Martin 2026-02-17 573 * We must shut down NOW to protect the filesystem.
0ebf821cf6c75d Hector Martin 2026-02-17 574 */
0ebf821cf6c75d Hector Martin 2026-02-17 575 if (apple_smc_read_u16(power->smc, SMC_KEY(BITV), &bitv) >= 0 &&
0ebf821cf6c75d Hector Martin 2026-02-17 576 apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &b0av) >= 0 &&
0ebf821cf6c75d Hector Martin 2026-02-17 577 b0av < bitv) {
0ebf821cf6c75d Hector Martin 2026-02-17 578 power->emergency_shutdown_triggered = true;
0ebf821cf6c75d Hector Martin 2026-02-17 579 dev_emerg(power->dev,
0ebf821cf6c75d Hector Martin 2026-02-17 580 "Battery voltage (%d mV) below design minimum (%d mV)! Emergency shutdown.\n",
0ebf821cf6c75d Hector Martin 2026-02-17 581 b0av, bitv);
0ebf821cf6c75d Hector Martin 2026-02-17 582
0ebf821cf6c75d Hector Martin 2026-02-17 583 /*
0ebf821cf6c75d Hector Martin 2026-02-17 584 * Shutdown is now imminent. Kick userspace again and give it some
0ebf821cf6c75d Hector Martin 2026-02-17 585 * brief time to (hopefully) flush what's needed, before forcing.
0ebf821cf6c75d Hector Martin 2026-02-17 586 */
0ebf821cf6c75d Hector Martin 2026-02-17 @587 hw_protection_trigger("Battery voltage below design minimum", 1500);
0ebf821cf6c75d Hector Martin 2026-02-17 588 }
0ebf821cf6c75d Hector Martin 2026-02-17 589
0ebf821cf6c75d Hector Martin 2026-02-17 590 /*
0ebf821cf6c75d Hector Martin 2026-02-17 591 * Avoid duplicate attempts at orderly shutdown.
0ebf821cf6c75d Hector Martin 2026-02-17 592 * Voltage check is above this as we may want to
0ebf821cf6c75d Hector Martin 2026-02-17 593 * "upgrade" an orderly shutdown to a critical power
0ebf821cf6c75d Hector Martin 2026-02-17 594 * off if voltage drops.
0ebf821cf6c75d Hector Martin 2026-02-17 595 */
0ebf821cf6c75d Hector Martin 2026-02-17 596 if (power->orderly_shutdown_triggered || system_state > SYSTEM_RUNNING)
0ebf821cf6c75d Hector Martin 2026-02-17 597 return;
0ebf821cf6c75d Hector Martin 2026-02-17 598
0ebf821cf6c75d Hector Martin 2026-02-17 599 /*
0ebf821cf6c75d Hector Martin 2026-02-17 600 * Check if SMC flagged the battery as empty.
0ebf821cf6c75d Hector Martin 2026-02-17 601 * We trigger a graceful shutdown to let the OS save data.
0ebf821cf6c75d Hector Martin 2026-02-17 602 */
f7b253a6e217f7 Sasha Finkelstein 2026-07-12 603 if (macsmc_battery_read_bcf0(power, &bcf0) == 0 && bcf0 != 0) {
0ebf821cf6c75d Hector Martin 2026-02-17 604 power->orderly_shutdown_triggered = true;
0ebf821cf6c75d Hector Martin 2026-02-17 605 dev_crit(power->dev, "Battery critical (empty flag set). Triggering orderly shutdown.\n");
0ebf821cf6c75d Hector Martin 2026-02-17 606 orderly_poweroff(true);
0ebf821cf6c75d Hector Martin 2026-02-17 607 }
0ebf821cf6c75d Hector Martin 2026-02-17 608 }
0ebf821cf6c75d Hector Martin 2026-02-17 609 --
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
PATCH v16 3/6 reboot: add parsable tokens for power state change reasons
In reply to Faruque Ansaripsc_reason_to_str() returns human-readable labels that contain spaces (e.g. "over temperature"). Those are fine for logs but unusable as values in a space-separated sysfs list or as a write target.
Extend the single reason descriptor table with a stable, space-free token next to the existing label, and add psc_reason_to_token() and psc_reason_from_token() so consumers can emit and parse reasons without inventing their own string table.
changes v16:
- Add Reviewed-by tag from Bradley Morgan.
- Add Reviewed-by tag from Daniel Lezcano.
changes v15:
- Add Reviewed-by tags from Matti Vaittinen.
changes v12:
- new patch
--- include/linux/reboot.h | 2 ++ kernel/reboot.c | 77 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 63 insertions(+), 16 deletions(-)
include/linux/reboot.h +2 -0
@@ -244,6 +244,8 @@ enum psc_reason { };const char *psc_reason_to_str (enum psc_reason reason); +const char *psc_reason_to_token (enum psc_reason reason); +int psc_reason_from_token (const char *token,enum psc_reason *reason);/** * enum hw_protection_action - Hardware protection action
kernel/reboot.c +61 -16
95 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v16 4/6 reboot: extend psc_reason with power-on and reset causes
In reply to Faruque Ansaripsc_reason covered only the protection reasons. Extend it to mirror the full POWER_ON_REASON_* vocabulary - power-on, watchdog, software, external, RTC, reset button, CPU clock and crystal failures - so a provider can report why the system last powered on, not only why it was shut down. New values are appended to keep the existing numeric ABI.
changes v16:
- Add Reviewed-by tag from Bradley Morgan.
changes v15:
- add a static_assert() tying ARRAY_SIZE(psc_reason_descs) to PSCR_REASON_COUNT, so a future enum addition without a matching table entry fails the build instead of silently reading a hole (Matti Vaittinen)
- Add Reviewed-by tags from Matti Vaittinen.
changes v12:
- new patch
--- include/linux/reboot.h | 34 +++++++++++++++++++++++++++++++--- kernel/reboot.c | 12 ++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-)
include/linux/reboot.h +31 -3
@@ -227,9 +227,28 @@ extern void orderly_reboot(void);* notify value ACPI_NOTIFY_CROS_EC_PANIC on ChromeOS EC hardware); the * kernel only relays that report, see cros_ec_lpc_acpi_notify(). * +* @PSCR_POWER_ON: Regular cold power-on (e.g. via a PMIC power-on request). +* +* @PSCR_WATCHDOG: Reset caused by a watchdog timeout. +* +* @PSCR_SOFTWARE: Software-initiated reset or reboot. +* +* @PSCR_EXTERNAL: Reset asserted through an external pin or reset input. +* +* @PSCR_RTC: Wake-up or power-on triggered by the RTC. +* +* @PSCR_RESET_BUTTON: Reset triggered by a user reset button. +* +* @PSCR_CPU_CLK_FAIL: Reset caused by a CPU clock failure. +* +* @PSCR_XTAL_FAIL: Reset caused by a crystal oscillator failure. +* * @PSCR_REASON_COUNT: Number of defined power state change reasons. This * value is useful for range checking and potential future extensions * while maintaining compatibility. +* +* The reasons mirror the POWER_ON_REASON_* strings in +* <linux/power/power_on_reason.h>; keep the two in sync when extending. */ enum psc_reason {PSCR_UNKNOWN ,
@@ -238,9 +257,18 @@ enum psc_reason {PSCR_REGULATOR_FAILURE ,PSCR_OVER_TEMPERATURE ,PSCR_EC_PANIC , - -/* Number of reasons */ - PSCR_REASON_COUNT, +/* Append new reasons here; the numeric order is ABI. */ +PSCR_POWER_ON , +PSCR_WATCHDOG , +PSCR_SOFTWARE , +PSCR_EXTERNAL , +PSCR_RTC , +PSCR_RESET_BUTTON , +PSCR_CPU_CLK_FAIL , +PSCR_XTAL_FAIL , + +/* Number of reasons. Must remain the last member. */ +PSCR_REASON_COUNT };const char *psc_reason_to_str (enum psc_reason reason);
kernel/reboot.c +12 -0
@@ -7,7 +7,9 @@#define pr_fmt (fmt) "reboot: " fmt +#include <linux/array_size.h> #include <linux/atomic.h> +#include <linux/build_bug.h> #include <linux/ctype.h> #include <linux/export.h> #include <linux/kexec.h>
@@ -1102,7 +1104,17 @@ static const struct psc_reason_desc psc_reason_descs[] = { [PSCR_REGULATOR_FAILURE] = { "regulator-failure", POWER_ON_REASON_REGULATOR_FAILURE }, [PSCR_OVER_TEMPERATURE] = { "over-temperature", POWER_ON_REASON_OVER_TEMPERATURE }, [PSCR_EC_PANIC] = { "ec-panic", POWER_ON_REASON_EC_PANIC }, + [PSCR_POWER_ON] = { "power-on", POWER_ON_REASON_REGULAR }, + [PSCR_WATCHDOG] = { "watchdog", POWER_ON_REASON_WATCHDOG }, + [PSCR_SOFTWARE] = { "software", POWER_ON_REASON_SOFTWARE }, + [PSCR_EXTERNAL] = { "external", POWER_ON_REASON_EXTERNAL }, + [PSCR_RTC] = { "rtc", POWER_ON_REASON_RTC }, + [PSCR_RESET_BUTTON] = { "reset-button", POWER_ON_REASON_RST_BTN }, + [PSCR_CPU_CLK_FAIL] = { "cpu-clock-failure", POWER_ON_REASON_CPU_CLK_FAIL }, + [PSCR_XTAL_FAIL] = { "crystal-failure", POWER_ON_REASON_XTAL_FAIL }, }; +static_assert (ARRAY_SIZE (psc_reason_descs) ==PSCR_REASON_COUNT , +"psc_reason_descs[] must have an entry for every psc_reason" );/** * psc_reason_to_str - Human-readable label for a power state change reason.
Signature · 1 line
2.34.1
PATCH v16 5/6 power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage
In reply to Faruque AnsariIntroduce the Power State Change Reasons Recording (PSCRR) framework: a generic mechanism to record why the last power state change (shutdown or reboot) happened - under-voltage, thermal, watchdog, software-triggered, etc. - so a postmortem reason survives a reboot or an abrupt power loss.
PSCRR is built around providers. A provider is either a hardware reason source (a PMIC, SoC reset controller or watchdog exposing a reset cause) or a recorder that persists the reason across a power cycle (an NVMEM or RTC scratch cell). Each provider gets a directory under /sys/kernel/pscrr/providerN/ exposing its name, backing device, the set of observed reasons (as tokens), its capabilities, the reasons it supports and - for recorders - a record policy. The reason set is deliberately not collapsed to a single winning cause, since resets are often multi-causal.
Reasons are the numeric enum psc_reason values from reboot.h, shared with the POWER_ON_REASON_* vocabulary, so they store compactly in small battery-backed cells. The current reason (get/set_psc_reason(), set by the thermal/regulator/hw_protection paths) is written to every recorder from the reboot notifier.
changes v15:
- Use -EOPNOTSUPP for missing write_reason() support instead of -EPERM.
- Use -EINVAL for unsupported reason values instead of -EOPNOTSUPP.
- Add a comment explaining provider lifetime guarantees for sysfs callbacks.
- Add Reviewed-by tags from Matti Vaittinen.
changes v13:
- Narrow pscrr_lock usage in pscrr_provider_register() to only protect pscrr_dirs list updates.
- Check pscrr_root before allocation and limit locking around ida_alloc() and kobject/sysfs setup.
changes v12:
- rework into a multi-provider framework: one sysfs directory per provider instead of a single backend
- add per-provider capabilities, supported_reasons and record policy
- register providers through a const ops table via (devm_)pscrr_provider_register()
- report reasons as parsable tokens
- move the sources under drivers/power/reset/pscrr/
- add a MAINTAINERS entry; PSCRR depends on POWER_RESET
- drop Reviewed-by: Matti Vaittinen; the framework was substantially reworked since it was given
changes v10:
- make g_pscrr static
changes v8:
- introduce struct pscrr_core to encapsulate backend and locking
- replace global mutex and backend pointer with centralized pscrr_core
- use DEFINE_GUARD() + guard(g_pscrr) for scoped mutex locking
- simplify code using local backend pointer after locking
- prepare code structure for future multi-backend support changes v7:
- make write_reason optional
- update documentation
changes v6:
- move enum pscr_reason to kernel reboot core
- move reason storage to reboot core
- add locking
--- MAINTAINERS | 11 + drivers/power/reset/Kconfig | 2 + drivers/power/reset/Makefile | 1 + drivers/power/reset/pscrr/Kconfig | 33 ++ drivers/power/reset/pscrr/Makefile | 2 + drivers/power/reset/pscrr/pscrr.c | 640 +++++++++++++++++++++++++++++++++++++ include/linux/pscrr.h | 107 +++++++ 7 files changed, 796 insertions(+)
MAINTAINERS +11 -0
@@ -21967,6 +21967,17 @@ F: Documentation/devicetree/bindings/connector/pcie-m2-e-connector.yaml F: Documentation/devicetree/bindings/connector/pcie-m2-m-connector.yaml F: drivers/power/sequencing/pwrseq-pcie-m2.c +POWER STATE CHANGE REASON RECORDING (PSCRR) +M: Oleksij Rempel <[email protected]> +M: Faruque Ansari <[email protected]> +R: Pengutronix Kernel Team <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/ABI/testing/sysfs-kernel-pscrr +F: drivers/power/reset/pscrr/ +F: include/linux/pscrr.h +F: tools/testing/selftests/pscrr/ + POWER STATE COORDINATION INTERFACE (PSCI) M: Mark Rutland <[email protected]> M: Lorenzo Pieralisi <[email protected]>
drivers/power/reset/Kconfig +2 -0
@@ -377,3 +377,5 @@ config POWER_RESET_QEMU_VIRT_CTRL provides this controller, such as the m68k virt machine. endif + +source "drivers/power/reset/pscrr/Kconfig"
drivers/power/reset/Makefile +1 -0
@@ -37,6 +37,7 @@ obj-$(CONFIG_POWER_RESET_KEYSTONE) += keystone-reset.o obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o obj-$(CONFIG_POWER_RESET_RMOBILE) += rmobile-reset.o +obj-$(CONFIG_PSCRR) += pscrr/ obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o obj-$(CONFIG_POWER_RESET_SC27XX) += sc27xx-poweroff.o
drivers/power/reset/pscrr/Kconfig new file +33 -0
@@ -0,0 +1,33 @@ +# SPDX-License-Identifier: GPL-2.0-only +menuconfig PSCRR + bool "Power State Change Reasons Recording (PSCRR) Framework" + depends on POWER_RESET + help + Enables the Power State Change Reasons Recording (PSCRR) framework. + + PSCRR records why the system last shut down or rebooted into + non-volatile storage, so the reason survives the reset and can be + read by the bootloader or early user space on the next boot. Reasons + come from software (thermal or regulator hardware-protection events, + a watchdog pretimeout, a kernel panic, a controlled reboot) or from + hardware reset-cause registers (PMIC, SoC reset controller, watchdog). + + It helps whenever the real cause would otherwise be lost or ambiguous + on the next boot, for example: + + - the reset is abrupt and block storage (eMMC/NAND) may not survive + it; on systems that can detect the event in time - such as an + imminent power loss with a short backup-capacitor window - the + reason can still be persisted first; + + - the hardware does not preserve the cause and the boot looks like a + plain power-on reset, e.g. a watchdog pretimeout that escalates + into a panic reboot would otherwise be indistinguishable from a + POR. + + The framework is extensible and exposes software-defined and + hardware-reported reasons through one interface. Sudden power cuts, + CPU freezes or other uncontrolled resets may still go unrecorded + unless hardware provides the reset cause. + + If unsure, say N.
drivers/power/reset/pscrr/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_PSCRR) += pscrr.o
drivers/power/reset/pscrr/pscrr.c new file +640 -0
640 lines not on this page · raw message
include/linux/pscrr.h new file +107 -0
107 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v16 5/6] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage
Quoted 21 lines · Faruque Ansari
Introduce the Power State Change Reasons Recording (PSCRR) framework: a generic mechanism to record why the last power state change (shutdown or reboot) happened - under-voltage, thermal, watchdog, software-triggered, etc. - so a postmortem reason survives a reboot or an abrupt power loss.
PSCRR is built around providers. A provider is either a hardware reason source (a PMIC, SoC reset controller or watchdog exposing a reset cause) or a recorder that persists the reason across a power cycle (an NVMEM or RTC scratch cell). Each provider gets a directory under
/sys/kernel/pscrr/providerN/exposing its name, backing device, the set of observed reasons (as tokens), its capabilities, the reasons it supports and - for recorders - a record policy. The reason set is deliberately not collapsed to a single winning cause, since resets are often multi-causal.Reasons are the numeric enum
psc_reasonvalues fromreboot.h, shared with the POWER_ON_REASON_* vocabulary, so they store compactly in small battery-backed cells. The current reason (get/set_psc_reason(), set by the thermal/regulator/hw_protection paths) is written to every recorder from the reboot notifier.
Quoted 7 lines
Co-developed-by Oleksij Rempel <[email protected]>Signed-off-by Oleksij Rempel <[email protected]>Reported-by Francesco Valla <[email protected]>Reviewed-by Matti Vaittinen <[email protected]>Signed-off-by Faruque Ansari <[email protected]>
Re: [PATCH v16 5/6] power: reset: Introduce PSCR Recording Framework for Non-Volatile Storage
In reply to Faruque AnsariQuoted 21 lines · Faruque Ansari
Introduce the Power State Change Reasons Recording (PSCRR) framework: a generic mechanism to record why the last power state change (shutdown or reboot) happened - under-voltage, thermal, watchdog, software-triggered, etc. - so a postmortem reason survives a reboot or an abrupt power loss.
PSCRR is built around providers. A provider is either a hardware reason source (a PMIC, SoC reset controller or watchdog exposing a reset cause) or a recorder that persists the reason across a power cycle (an NVMEM or RTC scratch cell). Each provider gets a directory under
/sys/kernel/pscrr/providerN/exposing its name, backing device, the set of observed reasons (as tokens), its capabilities, the reasons it supports and - for recorders - a record policy. The reason set is deliberately not collapsed to a single winning cause, since resets are often multi-causal.Reasons are the numeric enum
psc_reasonvalues fromreboot.h, shared with the POWER_ON_REASON_* vocabulary, so they store compactly in small battery-backed cells. The current reason (get/set_psc_reason(), set by the thermal/regulator/hw_protection paths) is written to every recorder from the reboot notifier.
Quoted 5 lines
Co-developed-by Oleksij Rempel <[email protected]>Signed-off-by Oleksij Rempel <[email protected]>Reported-by Francesco Valla <[email protected]>Reviewed-by Matti Vaittinen <[email protected]>
Hmm. LGTM from my standpoint:
Quoted 892 lines
Signed-off-by Faruque Ansari <[email protected]>changes v15:
- Use-EOPNOTSUPPfor missingwrite_reason()support instead of-EPERM.
- Use-EINVALfor unsupported reason values instead of-EOPNOTSUPP.
- Add a comment explaining provider lifetime guarantees for sysfs callbacks.
- Add Reviewed-by tags from Matti Vaittinen.changes v13:
- Narrowpscrr_lockusage inpscrr_provider_register()to only protectpscrr_dirslist updates.
- Checkpscrr_rootbefore allocation and limit locking aroundida_alloc()and kobject/sysfs setup.
changes v12:
- rework into a multi-provider framework: one sysfs directory per provider instead of a single backend
- add per-provider capabilities,supported_reasonsand record policy
- register providers through a const ops table via (devm_)pscrr_provider_register()
- report reasons as parsable tokens
- move the sources underdrivers/power/reset/pscrr/
- add aMAINTAINERSentry; PSCRR depends onPOWER_RESET
- drop Reviewed-by: Matti Vaittinen; the framework was substantially reworked since it was given
changes v10:
- makeg_pscrrstatic
changes v8:
- introduce structpscrr_coreto encapsulate backend and locking
- replace global mutex and backend pointer with centralizedpscrr_core
- useDEFINE_GUARD()+guard(g_pscrr)for scoped mutex locking
- simplify code using local backend pointer after locking
- prepare code structure for future multi-backend support changes v7:
- makewrite_reasonoptional
- update documentation
changes v6:
- move enumpscr_reasonto kernel reboot core
- move reason storage to reboot core
- add locking
---MAINTAINERS | 11 + drivers/power/reset/Kconfig | 2 + drivers/power/reset/Makefile | 1 + drivers/power/reset/pscrr/Kconfig | 33 ++ drivers/power/reset/pscrr/Makefile | 2 + drivers/power/reset/pscrr/pscrr.c | 640 +++++++++++++++++++++++++++++++++++++ include/linux/pscrr.h | 107 +++++++ 7 files changed, 796 insertions(+)MAINTAINERS +0
-0@@ -21967,6 +21967,17 @@ F: Documentation/devicetree/bindings/connector/pcie-m2-e-connector.yamlF:
Documentation/devicetree/bindings/connector/pcie-m2-m-connector.yaml
F:drivers/power/sequencing/pwrseq-pcie-m2.cMAINTAINERS +11
-0+POWER STATE CHANGE REASON RECORDING (PSCRR) +M: Oleksij Rempel <[email protected]> +M: Faruque Ansari <[email protected]> +R: Pengutronix Kernel Team <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/ABI/testing/sysfs-kernel-pscrr +F: drivers/power/reset/pscrr/ +F: include/linux/pscrr.h +F: tools/testing/selftests/pscrr/ +POWER STATE COORDINATION INTERFACE (PSCI)
M: Mark Rutland <[email protected]>
M: Lorenzo Pieralisi <[email protected]>drivers/power/reset/Kconfig +0
-0@@ -377,3 +377,5 @@ config POWER_RESET_QEMU_VIRT_CTRL provides this controller, such as the m68k virt machine.endif
drivers/power/reset/Kconfig +2
-0+ +source "drivers/power/reset/pscrr/Kconfig"drivers/power/reset/Makefile +0
-0@@ -37,6 +37,7 @@ obj-$(CONFIG_POWER_RESET_KEYSTONE) += keystone-reset.oobj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o obj-$(CONFIG_POWER_RESET_RMOBILE) += rmobile-reset.o +obj-$(CONFIG_PSCRR) += pscrr/ obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o obj-$(CONFIG_POWER_RESET_SC27XX) += sc27xx-poweroff.o
drivers/power/reset/pscrr/Kconfig new file +33
-0@@ -0,0 +1,33 @@ +# SPDX-License-Identifier: GPL-2.0-only +menuconfig PSCRR + bool "Power State Change Reasons Recording (PSCRR) Framework" + depends on POWER_RESET + help + Enables the Power State Change Reasons Recording (PSCRR) framework. + + PSCRR records why the system last shut down or rebooted into + non-volatile storage, so the reason survives the reset and can be + read by the bootloader or early user space on the next boot. Reasons + come from software (thermal or regulator hardware-protection events, + a watchdog pretimeout, a kernel panic, a controlled reboot) or from + hardware reset-cause registers (PMIC, SoC reset controller, watchdog). + + It helps whenever the real cause would otherwise be lost or ambiguous + on the next boot, for example: + + - the reset is abrupt and block storage (eMMC/NAND) may not survive + it; on systems that can detect the event in time - such as an + imminent power loss with a short backup-capacitor window - the + reason can still be persisted first; + + - the hardware does not preserve the cause and the boot looks like a + plain power-on reset, e.g. a watchdog pretimeout that escalates + into a panic reboot would otherwise be indistinguishable from a + POR. + + The framework is extensible and exposes software-defined and + hardware-reported reasons through one interface. Sudden power cuts, + CPU freezes or other uncontrolled resets may still go unrecorded + unless hardware provides the reset cause. + + If unsure, say N.drivers/power/reset/pscrr/Makefile new file +2
-0@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_PSCRR) += pscrr.odrivers/power/reset/pscrr/pscrr.c new file +640
-0@@ -0,0 +1,640 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* pscrr.c - Core Power State Change Reason Recording +* +* PSCRR records why the last power state change (shutdown/reboot) happened. +* Reasons come from providers: hardware reset-cause registers (PMIC, SoC reset +* controller, watchdog), persistent recorders (NVMEM/RTC scratch), the +* bootloader's device-tree /chosen/reset-source (a built-in provider here), or +* test stubs. Each provider gets a directory under /sys/kernel/pscrr/ and reports +* the full set of reasons it observed - the picture is deliberately not +* collapsed to a single "winning" cause, since resets are often multi-causal. +* +* Sysfs (per provider, under /sys/kernel/pscrr/providerN/): +* name ro human label of the provider +* device symlink to the backing device (if any) +* reason the provider's reason set, as tokens; writable (record +* one reason) when the provider supports it +* caps ro non-default capabilities ("writable"); empty for a +* read-only, single-slot provider +* supported_reasons ro reasons this provider can report or record +* record_policy recorders only: keep the "first" or "last" reason +* recorded in a power cycle +* +* The kernel keeps the first (root cause) and last power-state-change reason +* (get_psc_first_reason()/get_psc_reason(), set by the thermal, regulator and +* hw_protection paths). At reboot each recorder is given the first or the last +* reason according to its record policy. +* +* Copyright (C) 2025 Pengutronix, Oleksij Rempel <[email protected]> +* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. +*/ + +#define pr_fmt (fmt) "PSCRR: " fmt + +#include <linux/bitmap.h> +#include <linux/device.h> +#include <linux/idr.h> +#include <linux/kernel.h> +#include <linux/kobject.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/of.h> +#include <linux/pscrr.h> +#include <linux/reboot.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/sysfs.h> + +static struct kobject *pscrr_root; +static DEFINE_MUTEX (pscrr_lock); +static LIST_HEAD (pscrr_dirs); +static DEFINE_IDA (pscrr_ida); + +/* +* Record policy: when several reasons are recorded in one power cycle, keep the +* first (root cause) or overwrite with the last. Global, tunable via sysfs. +*/ +enum pscrr_record_policy { +PSCRR_RECORD_FIRST , +PSCRR_RECORD_LAST , +}; + +/* +* Per-provider sysfs directory. Core-owned and self-freeing on kobject_put(), +* so its lifetime is decoupled from the caller-owned struct pscrr_provider. +* +* The sysfs show/store callbacks below dereference ->provider without +* pscrr_lock: kobject_del() in pscrr_provider_unregister() blocks until +* every in-flight callback on this kobject returns before the directory +* (and thus ->provider) can be torn down, so a callback that is running +* always sees a live provider. +*/ +struct pscrr_provider_dir { +struct kobject kobj; +struct pscrr_provider *provider; +struct list_head node; +int id; +enum pscrr_record_policy policy;/* single-slot recorder: keep first/last */ +bool recorded;/* a reason was recorded this power cycle */ +}; + +static inline struct pscrr_provider_dir *to_pscrr_dir (struct kobject *kobj) +{ + return container_of(kobj, struct pscrr_provider_dir, kobj); +} + +/*----------------------------------------------------------------------*/ +/* Per-provider attributes */ +/*----------------------------------------------------------------------*/ + +static ssize_t name_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"%s\n" ,to_pscrr_dir (kobj)->provider->name); +} + +static struct kobj_attribute pscrr_name_attr =__ATTR_RO (name); + +static int pscrr_parse_reason (const char *buf,enum psc_reason *out) +{ +unsigned int val; + +/* Accept either a decimal index or a reason token. */ +if (!kstrtouint (buf,0 , &val)) { +if (val >=PSCR_REASON_COUNT ) +return -ERANGE ; + *out = val; +return 0 ; + } + +return psc_reason_from_token (buf, out); +} + +static ssize_t reason_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +struct pscrr_provider *p =to_pscrr_dir (kobj)->provider; +DECLARE_BITMAP (reasons,PSCR_REASON_COUNT ); +ssize_t len =0 ; +int bit, ret; + +bitmap_zero (reasons,PSCR_REASON_COUNT ); + + ret = p->ops->read_reasons (p, reasons); +if (ret) +return ret; + +for_each_set_bit (bit, reasons,PSCR_REASON_COUNT ) + len +=sysfs_emit_at (buf, len,"%s%s" , len ?" " :"" , +psc_reason_to_token (bit)); + len +=sysfs_emit_at (buf, len,"\n" ); + +return len; +} + +/* Record @reason into @dir honouring the global record policy. */ +static int pscrr_do_record (struct pscrr_provider_dir *dir,enum psc_reason reason) +{ +struct pscrr_provider *p = dir->provider; +int ret; + +if (!p->ops->write_reason) +return -EOPNOTSUPP ; + +/* +* PSCR_UNKNOWN clears the slot and releases the latch, regardless of +* policy, so a reason recorded afterwards is taken again. +*/ +if (reason ==PSCR_UNKNOWN ) { + ret = p->ops->write_reason (p, reason); +if (ret) +return ret; + + dir->recorded = false; +return 0 ; + } + +/* Reject reasons the provider does not advertise (NULL == all). */ +if (p->supported_reasons && !test_bit (reason, p->supported_reasons)) +return -EINVAL ; + +/* "first" policy: keep the first reason recorded this power cycle. */ +if (dir->policy ==PSCRR_RECORD_FIRST && dir->recorded) +return 0 ; + + ret = p->ops->write_reason (p, reason); +if (ret) +return ret; + + dir->recorded = true; + +return 0 ; +} + +static ssize_t reason_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t count) +{ +struct pscrr_provider_dir *dir =to_pscrr_dir (kobj); +enum psc_reason reason; +int ret; + +if (!dir->provider->ops->write_reason) +return -EOPNOTSUPP ; + + ret =pscrr_parse_reason (buf, &reason); +if (ret) +return ret; + +/* Serialise the record state against concurrent stores and the notifier. */ +scoped_guard (mutex, &pscrr_lock) + ret = pscrr_do_record(dir, reason); +if (ret) +return ret; + +return count; +} + +static struct kobj_attribute pscrr_reason_attr = +__ATTR (reason,0644 , reason_show, reason_store); + +static ssize_t caps_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +struct pscrr_provider *p =to_pscrr_dir (kobj)->provider; +ssize_t len =0 ; + +/* Readable and single-slot are the defaults and not listed. */ +if (p->ops->write_reason) + len +=sysfs_emit_at (buf, len,"writable" ); + +return len +sysfs_emit_at (buf, len,"\n" ); +} + +static struct kobj_attribute pscrr_caps_attr =__ATTR_RO (caps); + +static ssize_t supported_reasons_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +struct pscrr_provider *p =to_pscrr_dir (kobj)->provider; +const unsigned long *sup = p->supported_reasons; +ssize_t len =0 ; +int i; + +for (i =0 ; i <PSCR_REASON_COUNT ; i++) { +if (sup && !test_bit (i, sup))/* NULL means all */ +continue ; + len +=sysfs_emit_at (buf, len,"%s%s" , len ?" " :"" , +psc_reason_to_token (i)); + } + +return len +sysfs_emit_at (buf, len,"\n" ); +} + +static struct kobj_attribute pscrr_supported_attr = +__ATTR (supported_reasons,0444 , supported_reasons_show,NULL ); + +static ssize_t record_policy_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +struct pscrr_provider_dir *dir =to_pscrr_dir (kobj); + +return sysfs_emit (buf,"%s\n" , +READ_ONCE (dir->policy) ==PSCRR_RECORD_FIRST ? +"first" :"last" ); +} + +static ssize_t record_policy_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +struct pscrr_provider_dir *dir =to_pscrr_dir (kobj); + +guard (mutex)(&pscrr_lock); + +if (sysfs_streq (buf,"first" )) +WRITE_ONCE (dir->policy,PSCRR_RECORD_FIRST ); +else if (sysfs_streq (buf,"last" )) +WRITE_ONCE (dir->policy,PSCRR_RECORD_LAST ); +else +return -EINVAL ; + +return count; +} + +static struct kobj_attribute pscrr_record_policy_attr = +__ATTR (record_policy,0644 , record_policy_show, record_policy_store); + +static struct attribute *pscrr_dir_attrs[] = { + &pscrr_name_attr.attr, + &pscrr_reason_attr.attr, + &pscrr_caps_attr.attr, + &pscrr_supported_attr.attr, + &pscrr_record_policy_attr.attr, +NULL , +}; + +static umode_t pscrr_dir_is_visible (struct kobject *kobj,struct attribute *attr, +int n) +{ +struct pscrr_provider *p =to_pscrr_dir (kobj)->provider; + +/* A provider without write_reason() exposes reason read-only. */ +if (attr == &pscrr_reason_attr.attr && !p->ops->write_reason) +return 0444 ; + +/* record_policy only applies to a (single-slot) recorder. */ +if (attr == &pscrr_record_policy_attr.attr && !p->ops->write_reason) +return 0 ; + +return attr->mode; +} + +static const struct attribute_group pscrr_dir_group = { + .attrs = pscrr_dir_attrs, + .is_visible = pscrr_dir_is_visible, +}; + +static const struct attribute_group *pscrr_dir_groups[] = { + &pscrr_dir_group, +NULL , +}; + +static void pscrr_dir_release (struct kobject *kobj) +{ +kfree (to_pscrr_dir (kobj)); +} + +static const struct kobj_type pscrr_dir_ktype = { + .sysfs_ops = &kobj_sysfs_ops, + .release = pscrr_dir_release, + .default_groups = pscrr_dir_groups, +}; + +/*----------------------------------------------------------------------*/ +/* Provider registration */ +/*----------------------------------------------------------------------*/ + +/** +* pscrr_provider_register - register a power state change reason provider +* @p: caller-owned provider description +* +* Creates /sys/kernel/pscrr/providerN/ with "name" and "reason" attributes +* and, when @p->dev is set, a "device" symlink. @p->reason is writable when +* @p provides write_reason(). The provider must outlive the matching +* pscrr_provider_unregister() call. +* +* Return: 0 on success or a negative errno. +*/ +int pscrr_provider_register (struct pscrr_provider *p) +{ +struct pscrr_provider_dir *dir; +int ret; + +if (!p || !p->name || !p->ops || !p->ops->read_reasons) +return -EINVAL ; + +/* +* pscrr_root is set once at core init and cleared at core exit; +* neither transition is serialised against this function by +* pscrr_lock, so check it up front rather than under the lock below. +*/ +if (!pscrr_root) +return -ENODEV ; + + dir =kzalloc_obj (*dir); +if (!dir) +return -ENOMEM ; + + dir->provider = p; + dir->policy =PSCRR_RECORD_FIRST ; + +/* ida_alloc() and the kobject/sysfs calls below are individually thread-safe. */ + dir->id =ida_alloc (&pscrr_ida,GFP_KERNEL ); +if (dir->id <0 ) { + ret = dir->id; +kfree (dir); +return ret; + } + + ret =kobject_init_and_add (&dir->kobj, &pscrr_dir_ktype, pscrr_root, +"provider%d" , dir->id); +if (ret) { +/* +* kobject_init_and_add() failed: per its contract only +* kobject_put() may follow, no kobject_del(). +*/ +ida_free (&pscrr_ida, dir->id); +kobject_put (&dir->kobj); +return ret; + } + +if (p->dev) { + ret =sysfs_create_link (&dir->kobj, &p->dev->kobj,"device" ); +if (ret) + goto err_del; + } + +/* pscrr_lock only serialises pscrr_dirs against concurrent (un)registration. */ + scoped_guard(mutex, &pscrr_lock) +list_add_tail (&dir->node, &pscrr_dirs); + +return 0 ; + +err_del: +kobject_del (&dir->kobj); +ida_free (&pscrr_ida, dir->id); +kobject_put (&dir->kobj); +return ret; +} +EXPORT_SYMBOL_GPL (pscrr_provider_register); + +/** +* pscrr_provider_unregister - remove a previously registered provider +* @p: the provider passed to pscrr_provider_register() +*/ +void pscrr_provider_unregister (struct pscrr_provider *p) +{ +struct pscrr_provider_dir *dir =NULL , *iter; + + scoped_guard(mutex, &pscrr_lock) { +list_for_each_entry (iter, &pscrr_dirs, node) { +if (iter->provider == p) { + dir = iter; +list_del (&dir->node); +break ; + } + } + } + +if (!dir) +return ; + +/* +* Tear the sysfs directory down outside pscrr_lock: kobject_del() +* drains in-flight reason/record_policy stores, which take pscrr_lock, +* so holding it here would deadlock. Release the id only once the +* directory is gone, so a concurrent register cannot reuse it and +* collide on the providerN name. +*/ +if (p->dev) +sysfs_remove_link (&dir->kobj,"device" ); +kobject_del (&dir->kobj); +ida_free (&pscrr_ida, dir->id); +kobject_put (&dir->kobj); +} +EXPORT_SYMBOL_GPL (pscrr_provider_unregister); + +static void pscrr_provider_devm_release (void *p) +{ +pscrr_provider_unregister (p); +} + +/** +* devm_pscrr_provider_register - device-managed reason provider registration +* @dev: device the provider belongs to; also backs the "device" symlink +* @name: human-readable provider label +* @ops: provider callback table; read_reasons() is required, write_reason() is +* optional and makes the provider a recorder +* @supported_reasons: bitmap of the reasons the provider supports, or NULL +* for all; set before the provider is exposed in sysfs +* @priv: provider private data, handed back to the @ops callbacks +* +* Allocates and registers a struct pscrr_provider and schedules its +* unregistration when @dev is unbound, so the caller keeps no reference to it. +* +* Return: the registered provider on success or an ERR_PTR() on failure. When +* CONFIG_PSCRR is disabled the call resolves to a stub returning NULL, so +* callers need no IS_ENABLED() guard. +*/ +struct pscrr_provider * +devm_pscrr_provider_register (struct device *dev,const char *name, +const struct pscrr_provider_ops *ops, +const unsigned long *supported_reasons,void *priv) +{ +struct pscrr_provider *p; +int ret; + + p =devm_kzalloc (dev,sizeof (*p),GFP_KERNEL ); +if (!p) +return ERR_PTR (-ENOMEM ); + + p->name = name; + p->dev = dev; + p->ops = ops; + p->supported_reasons = supported_reasons; + p->priv = priv; + + ret =pscrr_provider_register (p); +if (ret) +return ERR_PTR (ret); + + ret =devm_add_action_or_reset (dev, pscrr_provider_devm_release, p); +if (ret) +return ERR_PTR (ret); + +return p; +} +EXPORT_SYMBOL_GPL (devm_pscrr_provider_register); + +/*----------------------------------------------------------------------*/ +/* Record path: reboot notifier writes the current reason to recorders */ +/*----------------------------------------------------------------------*/ + +/* +* Record the current power-state-change reason into every provider, giving +* each the first (root cause) or the last reason according to its record +* policy. The kernel keeps both, so "first" is meaningful even when later +* events overwrite the last reason. The caller holds pscrr_lock, or runs where +* the provider list is stable (panic). +*/ +static void pscrr_record_current (void ) +{ +enum psc_reason first =get_psc_first_reason (); +enum psc_reason last =get_psc_reason (); +struct pscrr_provider_dir *dir; + +list_for_each_entry (dir, &pscrr_dirs, node) +pscrr_do_record (dir, dir->policy ==PSCRR_RECORD_FIRST ? + first : last); +} + +static int pscrr_reboot_notifier (struct notifier_block *nb, +unsigned long action,void *unused) +{ +guard (mutex)(&pscrr_lock); + +/* +* A reboot, halt or power-off that reaches here with no more specific +* reason is software-initiated by definition. Record it as such rather +* than leaving it unattributed; a real cause set earlier (thermal, +* under-voltage, ...) is already latched and left untouched. +*/ +if (get_psc_reason () ==PSCR_UNKNOWN ) +set_psc_reason (PSCR_SOFTWARE ); + +pscrr_record_current (); + +return NOTIFY_DONE ; +} + +static struct notifier_block pscrr_reboot_nb = { + .notifier_call = pscrr_reboot_notifier, +}; + +/*----------------------------------------------------------------------*/ +/* Built-in provider: device-tree /chosen/reset-source */ +/*----------------------------------------------------------------------*/ + +/* +* Bootloaders such as barebox record the SoC reset cause in the standard +* device-tree /chosen/reset-source property. When it is present, surface it as +* a read-only, device-less provider so the bootloader's view of the last reset +* shows up next to any hardware or software providers - the framework just +* reads the property already there, with no dedicated node or new binding. +*/ +static const struct { +const char *name; +enum psc_reason reason; +} pscrr_reset_source_map[] = { + {"POR" ,PSCR_POWER_ON }, + {"RST" ,PSCR_SOFTWARE }, + {"WDG" ,PSCR_WATCHDOG }, + {"THERM" ,PSCR_OVER_TEMPERATURE }, + {"EXT" ,PSCR_EXTERNAL }, + {"BROWNOUT" ,PSCR_UNDER_VOLTAGE }, +}; + +static const unsigned long +pscrr_reset_source_supported[BITS_TO_LONGS (PSCR_REASON_COUNT )] = { +BIT (PSCR_UNDER_VOLTAGE ) |BIT (PSCR_OVER_TEMPERATURE ) | +BIT (PSCR_POWER_ON ) |BIT (PSCR_WATCHDOG ) |BIT (PSCR_SOFTWARE ) | +BIT (PSCR_EXTERNAL ), +}; + +/* Parsed once at init; read back by the provider's read_reasons(). */ +static enum psc_reason pscrr_reset_source_reason =PSCR_UNKNOWN ; + +static int pscrr_reset_source_read (struct pscrr_provider *p, +unsigned long *reasons) +{ +set_bit (pscrr_reset_source_reason, reasons); + +return 0 ; +} + +static const struct pscrr_provider_ops pscrr_reset_source_ops = { + .read_reasons = pscrr_reset_source_read, +}; + +static struct pscrr_provider pscrr_reset_source_provider = { + .name ="reset-source" , + .ops = &pscrr_reset_source_ops, + .supported_reasons = pscrr_reset_source_supported, +}; + +static void __init pscrr_register_reset_source (void ) +{ +const char *name; +int i, ret; + +if (!IS_ENABLED (CONFIG_OF ) || !of_chosen) +return ; + +if (of_property_read_string (of_chosen,"reset-source" , &name)) +return ; + +for (i =0 ; i <ARRAY_SIZE (pscrr_reset_source_map); i++) +if (!strcmp (name, pscrr_reset_source_map[i].name)) { + pscrr_reset_source_reason = pscrr_reset_source_map[i].reason; +break ; + } + + ret =pscrr_provider_register (&pscrr_reset_source_provider); +if (ret) +pr_warn ("failed to register the reset-source provider: %d\n" , ret); +} + +/*----------------------------------------------------------------------*/ +/* Module init/exit */ +/*----------------------------------------------------------------------*/ + +static int __init pscrr_core_init (void ) +{ +int ret; + + pscrr_root =kobject_create_and_add ("pscrr" , kernel_kobj); +if (!pscrr_root) +return -ENOMEM ; + + ret =register_reboot_notifier (&pscrr_reboot_nb); +if (ret) { +kobject_put (pscrr_root); + pscrr_root =NULL ; +return ret; + } + +pscrr_register_reset_source (); + +return 0 ; +} + +static void __exit pscrr_core_exit(void) +{ +pscrr_provider_unregister (&pscrr_reset_source_provider); +unregister_reboot_notifier (&pscrr_reboot_nb); +kobject_put (pscrr_root); + pscrr_root =NULL ; +ida_destroy (&pscrr_ida); +} + +/* Bring the core up before device drivers probe and register providers. */ +subsys_initcall (pscrr_core_init); +module_exit (pscrr_core_exit); + +MODULE_AUTHOR ("Oleksij Rempel <[email protected]>" ); +MODULE_AUTHOR ("Faruque Ansari <[email protected]>" ); +MODULE_DESCRIPTION ("Power State Change Reason Recording (PSCRR) core" ); +MODULE_LICENSE ("GPL" );include/linux/pscrr.h new file +97
-0@@ -0,0 +1,107 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* pscrr.h - Public header for Power State Change Reason Recording (PSCRR). +* +* Copyright (C) 2025 Pengutronix, Oleksij Rempel <[email protected]> +*/ + +#ifndef __PSCRR_H__ +#define __PSCRR_H__ + +#include <linux/reboot.h> + +struct device ; +struct pscrr_provider ; + +/** +* struct pscrr_provider_ops - Provider callbacks. +* @read_reasons: Fill @reasons with the complete set this source observed. +* @reasons is a bitmap of PSCR_REASON_COUNT bits; set members +* with set_bit(PSCR_x, reasons). Return 0 on success or a +* negative errno. Required. Driver state is reached via p->priv. +* @write_reason: Persist @reason. Called from the reboot notifier for +* recorder-capable providers so the cause survives the power +* cycle. Leave NULL for read-only hardware sources. +* +* A const ops table per provider type; extend it with new callbacks without +* touching the registration signature or existing callers. +*/ +struct pscrr_provider_ops { +int (*read_reasons)(struct pscrr_provider *p,unsigned long *reasons); +int (*write_reason)(struct pscrr_provider *p,enum psc_reason reason); +}; + +/** +* struct pscrr_provider - A source (and optionally recorder) of power state +* change reasons. +* +* A provider represents one place the system can learn *why* the last power +* state change happened: a hardware reset-cause register (PMIC, SoC SRC, +* watchdog), a persistent recorder (NVMEM/RTC scratch), or a test stub. Each +* registered provider gets its own directory under /sys/kernel/pscrr/, so the +* full, un-prioritised picture is visible: several providers - and several +* reasons within one provider - can be reported simultaneously. +* +* @name: Human-readable label, exported as the "name" attribute. The +* directory itself is core-indexed (providerN), so this need not +* be unique. Required. +* @dev: Backing device. When set it is exported as the "device" symlink +* in the provider directory, tying the reason to real hardware. +* May be NULL (e.g. for a test provider). +* @ops: Provider callbacks. Required. +* @supported_reasons: Bitmap of the reasons this provider can store or report, +* limited e.g. by the storage size. 0 means all reasons. +* @priv: Provider private data, passed back through the callbacks. +* +* Providers are readable and single-slot by default; only capabilities beyond +* that (currently: writable) are advertised. +*/ +struct pscrr_provider { +const char *name; +struct device *dev; +const struct pscrr_provider_ops *ops; +const unsigned long *supported_reasons; +void *priv; +}; + +#if IS_ENABLED (CONFIG_PSCRR ) +int pscrr_provider_register (struct pscrr_provider *p); +void pscrr_provider_unregister (struct pscrr_provider *p); + +/** +* devm_pscrr_provider_register - allocate, fill and register a provider +* @dev: device the provider belongs to (also the "device" symlink target) +* @name: provider label +* @ops: provider callbacks +* @supported_reasons: bitmap of supported reasons, or NULL for all +* @priv: driver state passed back through the callbacks +* +* The provider is unregistered automatically on device teardown. +* +* Return: the provider on success, ERR_PTR() on failure, or NULL when PSCRR +* is not built (so the caller need not guard the call). +*/ +struct pscrr_provider * +devm_pscrr_provider_register (struct device *dev,const char *name, +const struct pscrr_provider_ops *ops, +const unsigned long *supported_reasons,void *priv); +#else +static inline int pscrr_provider_register (struct pscrr_provider *p) +{ +return -EOPNOTSUPP ; +} + +static inline void pscrr_provider_unregister (struct pscrr_provider *p) +{ +} +
include/linux/pscrr.h line 107
Quoted 10 lines
include/linux/pscrr.h new file +10
-0+static inline struct pscrr_provider * +devm_pscrr_provider_register (struct device *dev,const char *name, +const struct pscrr_provider_ops *ops, +const unsigned long *supported_reasons,void *priv) +{ +return NULL ; +} +#endif + +#endif /* __PSCRR_H__ */
--- Thanks!
"I'm not a very positive person" - Linus torvalds
PATCH v16 6/6 Documentation: Add sysfs documentation for PSCRR
In reply to Faruque AnsariDocument the Power State Change Reasons Recording (PSCRR) sysfs interface under /sys/kernel/pscrr/: the per-provider directories and their name, device, reason, caps, supported_reasons and record_policy attributes, including the stable reason token values.
Changes v15:
- Add Reviewed-by tags from Matti Vaittinen.
changes v12:
- rewrite for the per-provider interface (providerN/ directories with caps, supported_reasons and record_policy)
- rename the file to sysfs-kernel-pscrr to match the sysfs path
- refresh KernelVersion/Date
- drop Reviewed-by: Matti Vaittinen; the documentation was rewritten changes v8:
- simplify and clarify example sysfs value comments
- add note that not all values are meaningful on every system changes v7:
- document expected values
--- Documentation/ABI/testing/sysfs-kernel-pscrr | 108 +++++++++++++++++++++++++++ 1 file changed, 108 insertions(+)
Documentation/ABI/testing/sysfs-kernel-pscrr new file +108 -0
@@ -0,0 +1,108 @@ +What: /sys/kernel/pscrr/ +Date: July 2026 +KernelVersion: 7.2 +Contact: Oleksij Rempel <[email protected]> +Description: + Root directory of the Power State Change Reason Recording + (PSCRR) framework. It contains one subdirectory per registered + reason provider, named providerN (N is an arbitrary, stable + index assigned at registration). + + A provider is either a hardware reason source (a PMIC, SoC + reset controller or watchdog exposing a reset cause) or a + recorder that persists the current reason across a power cycle + (e.g. an NVMEM or RTC scratch cell). The set of reasons is + deliberately not collapsed to a single "winning" cause, since + resets are often multi-causal. + +What: /sys/kernel/pscrr/providerN/name +Date: July 2026 +KernelVersion: 7.2 +Contact: Oleksij Rempel <[email protected]> +Description: + (RO) Human-readable label identifying the provider, e.g. + "pca9450" or "nvmem". + +What: /sys/kernel/pscrr/providerN/device +Date: July 2026 +KernelVersion: 7.2 +Contact: Oleksij Rempel <[email protected]> +Description: + Symbolic link to the backing struct device of the provider. + Present only for providers that are bound to a device. + +What: /sys/kernel/pscrr/providerN/reason +Date: July 2026 +KernelVersion: 7.2 +Contact: Oleksij Rempel <[email protected]> +Description: + The set of power state change reasons observed by this + provider, as a space-separated list of reason tokens (an + empty line means no reason is recorded). + + The attribute is writable only for providers that can record + a reason; for a pure hardware source it is read-only. A write + records one reason and accepts either a reason token or its + decimal index. The tokens and their stable numeric values are: + + == ================= ============================================ + 0 unknown Unknown or unspecified reason + 1 under-voltage Supply voltage dropped below a safe level + 2 over-current Excessive current draw / possible short + 3 regulator-failure Voltage regulator failure + 4 over-temperature Unsafe temperature detected + 5 ec-panic Embedded controller (EC) panic + 6 power-on Regular cold power-on + 7 watchdog Watchdog timeout + 8 software Software-initiated reset or reboot + 9 external External reset input asserted + 10 rtc RTC-triggered wake-up or power-on + 11 reset-button User reset button + 12 cpu-clock-failure CPU clock failure + 13 crystal-failure Crystal oscillator failure + == ================= ============================================ + + The numeric order is stable ABI: new reasons are only ever + appended. A provider may support only a subset of these; see + "supported_reasons". + +What: /sys/kernel/pscrr/providerN/caps +Date: July 2026 +KernelVersion: 7.2 +Contact: Oleksij Rempel <[email protected]> +Description: + (RO) Space-separated list of the provider's non-default + capabilities. Being readable and storing a single reason are + the defaults and are not listed. Currently defined: + + ======== ============================================== + writable the provider can record a reason (see "reason" + and "record_policy") + ======== ============================================== + + An empty line therefore denotes a read-only, single-slot + provider. + +What: /sys/kernel/pscrr/providerN/supported_reasons +Date: July 2026 +KernelVersion: 7.2 +Contact: Oleksij Rempel <[email protected]> +Description: + (RO) Space-separated list of the reason tokens (see "reason") + this provider is able to report or record. A provider that + supports every reason lists them all. + +What: /sys/kernel/pscrr/providerN/record_policy +Date: July 2026 +KernelVersion: 7.2 +Contact: Oleksij Rempel <[email protected]> +Description: + (RW) Policy used when more than one reason is recorded during a + single power cycle. Present only for providers that can record + (see "caps"). Valid values are: + + ===== ================================================= + first keep the first reason recorded this cycle (the + root cause); this is the default + last overwrite with the most recently recorded reason + ===== =================================================
Signature · 1 line
2.34.1
Re: [PATCH v16 6/6] Documentation: Add sysfs documentation for PSCRR
Quoted 10 lines · Faruque Ansari
Document the Power State Change Reasons Recording (PSCRR) sysfs interface under
/sys/kernel/pscrr/: the per-provider directories and their name, device, reason, caps,supported_reasonsandrecord_policyattributes, including the stable reason token values.Co-developed-by Oleksij Rempel <[email protected]>Signed-off-by Oleksij Rempel <[email protected]>Reviewed-by Matti Vaittinen <[email protected]>Signed-off-by Faruque Ansari <[email protected]>
Quoted 14 lines
Changes v15:
- Add Reviewed-by tags from Matti Vaittinen.
changes v12:
- rewrite for the per-provider interface (providerN/ directories with caps,supported_reasonsandrecord_policy)
- rename the file to sysfs-kernel-pscrr to match the sysfs path
- refresh KernelVersion/Date
- drop Reviewed-by: Matti Vaittinen; the documentation was rewritten changes v8:
- simplify and clarify example sysfs value comments
- add note that not all values are meaningful on every system changes v7:
- document expected values
---