From 6e82c2415f20e7ac80f1f6a887d0b5a8dbc60ebd Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Sat, 3 May 2025 08:20:57 +0200 Subject: [PATCH] refactor(core): refactor power manager syscalls [no changelog] --- core/SConscript.firmware | 2 +- core/SConscript.kernel | 2 +- .../power_manager/fuel_gauge/battery_model.c | 3 +++ .../sys/power_manager/fuel_gauge/fuel_gauge.c | 4 +++- .../sys/power_manager/stm32u5/power_control.c | 3 +++ .../sys/power_manager/stm32u5/power_manager.c | 3 +++ .../power_manager/stm32u5/power_monitoring.c | 1 + .../sys/power_manager/stm32u5/power_states.c | 3 +++ core/embed/sys/startup/stm32/bootutils.c | 2 +- .../sys/syscall/stm32/syscall_dispatch.c | 20 ++++++++-------- .../embed/sys/syscall/stm32/syscall_numbers.h | 6 ++--- core/embed/sys/syscall/stm32/syscall_stubs.c | 23 +++++++++++-------- .../sys/syscall/stm32/syscall_verifiers.c | 10 ++++---- .../sys/syscall/stm32/syscall_verifiers.h | 6 ++--- 14 files changed, 53 insertions(+), 35 deletions(-) diff --git a/core/SConscript.firmware b/core/SConscript.firmware index d09bf069e7..05739da877 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -46,7 +46,7 @@ FEATURE_FLAGS = { "AES_GCM": BENCHMARK or THP, } -FEATURES_WANTED = ["input", "sd_card", "rgb_led", "dma2d", "consumption_mask", "usb" ,"optiga", "haptic", "ble", "tropic", "powerctl", "display"] +FEATURES_WANTED = ["input", "sd_card", "rgb_led", "dma2d", "consumption_mask", "usb" ,"optiga", "haptic", "ble", "tropic", "power_manager", "display"] if DISABLE_OPTIGA: if PYOPT != '0': raise RuntimeError("DISABLE_OPTIGA requires PYOPT=0") diff --git a/core/SConscript.kernel b/core/SConscript.kernel index 0ba20f2852..2660d15f1b 100644 --- a/core/SConscript.kernel +++ b/core/SConscript.kernel @@ -30,7 +30,7 @@ FEATURE_FLAGS = { "AES_GCM": True, } -FEATURES_WANTED = ["input", "sd_card", "rgb_led", "dma2d", "consumption_mask", "usb" ,"optiga", "haptic", "ble", "tropic", "powerctl", "display", "applet"] +FEATURES_WANTED = ["input", "sd_card", "rgb_led", "dma2d", "consumption_mask", "usb" ,"optiga", "haptic", "ble", "tropic", "power_manager", "display", "applet"] if DISABLE_OPTIGA: # TODO use PYOPT instead of PRODUCTION, same as in firmware, blocked on #4253 if PRODUCTION: diff --git a/core/embed/sys/power_manager/fuel_gauge/battery_model.c b/core/embed/sys/power_manager/fuel_gauge/battery_model.c index bf278d5604..286680d924 100644 --- a/core/embed/sys/power_manager/fuel_gauge/battery_model.c +++ b/core/embed/sys/power_manager/fuel_gauge/battery_model.c @@ -16,6 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#ifdef KERNEL_MODE #include "battery_model.h" #include @@ -282,3 +283,5 @@ float battery_soc(float ocv, float temperature, bool discharging_mode) { : BATTERY_OCV_CHARGE_PARAMS[0]; return calc_soc_from_ocv(params, ocv); } + +#endif diff --git a/core/embed/sys/power_manager/fuel_gauge/fuel_gauge.c b/core/embed/sys/power_manager/fuel_gauge/fuel_gauge.c index 2bb63c8d49..39360f7be6 100644 --- a/core/embed/sys/power_manager/fuel_gauge/fuel_gauge.c +++ b/core/embed/sys/power_manager/fuel_gauge/fuel_gauge.c @@ -16,7 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - +#ifdef KERNEL_MODE #include "fuel_gauge.h" #include #include "battery_model.h" @@ -142,3 +142,5 @@ float fuel_gauge_update(fuel_gauge_state_t* state, uint32_t dt_ms, return state->soc_latched; } + +#endif diff --git a/core/embed/sys/power_manager/stm32u5/power_control.c b/core/embed/sys/power_manager/stm32u5/power_control.c index 8446808856..ef9860e555 100644 --- a/core/embed/sys/power_manager/stm32u5/power_control.c +++ b/core/embed/sys/power_manager/stm32u5/power_control.c @@ -16,6 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#ifdef KERNEL_MODE #include #include @@ -234,3 +235,5 @@ static void pm_background_tasks_suspend(void) { static bool pm_background_tasks_suspended(void) { return true; } static void pm_background_tasks_resume(void) {} + +#endif diff --git a/core/embed/sys/power_manager/stm32u5/power_manager.c b/core/embed/sys/power_manager/stm32u5/power_manager.c index 97a1bd810c..3a31f81027 100644 --- a/core/embed/sys/power_manager/stm32u5/power_manager.c +++ b/core/embed/sys/power_manager/stm32u5/power_manager.c @@ -16,6 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#ifdef KERNEL_MODE #include #include @@ -415,3 +416,5 @@ static void pm_shutdown_timer_handler(void* context) { drv->shutdown_timer_elapsed = true; pm_process_state_machine(); } + +#endif diff --git a/core/embed/sys/power_manager/stm32u5/power_monitoring.c b/core/embed/sys/power_manager/stm32u5/power_monitoring.c index cba9d55528..b0d2bae836 100644 --- a/core/embed/sys/power_manager/stm32u5/power_monitoring.c +++ b/core/embed/sys/power_manager/stm32u5/power_monitoring.c @@ -17,6 +17,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#ifdef KERNEL_MODE #include #include diff --git a/core/embed/sys/power_manager/stm32u5/power_states.c b/core/embed/sys/power_manager/stm32u5/power_states.c index 085009972f..504bc8d418 100644 --- a/core/embed/sys/power_manager/stm32u5/power_states.c +++ b/core/embed/sys/power_manager/stm32u5/power_states.c @@ -16,6 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#ifdef KERNEL_MODE #include #include @@ -275,3 +276,5 @@ void pm_exit_shutting_down(pm_driver_t* drv) { systimer_unset(drv->shutdown_timer); drv->shutdown_timer_elapsed = false; } + +#endif diff --git a/core/embed/sys/startup/stm32/bootutils.c b/core/embed/sys/startup/stm32/bootutils.c index 95a94373a8..90525d6569 100644 --- a/core/embed/sys/startup/stm32/bootutils.c +++ b/core/embed/sys/startup/stm32/bootutils.c @@ -37,7 +37,7 @@ #ifdef KERNEL_MODE -// Battery powered devices (USE_POWERCTL) should not stall +// Battery powered devices (USE_POWER_MANAGER) should not stall // after showing RSOD, as it would drain the battery. #ifdef USE_POWER_MANAGER #ifdef RSOD_INFINITE_LOOP diff --git a/core/embed/sys/syscall/stm32/syscall_dispatch.c b/core/embed/sys/syscall/stm32/syscall_dispatch.c index 60a372c3ad..d2ab1c9eeb 100644 --- a/core/embed/sys/syscall/stm32/syscall_dispatch.c +++ b/core/embed/sys/syscall/stm32/syscall_dispatch.c @@ -61,8 +61,8 @@ #include #endif -#ifdef USE_POWERCTL -#include +#ifdef USE_POWER_MANAGER +#include #endif #ifdef USE_RGB_LED @@ -732,18 +732,18 @@ __attribute((no_stack_protector)) void syscall_handler(uint32_t *args, } break; #endif -#ifdef USE_POWERCTL - case SYSCALL_POWERCTL_SUSPEND: { - powerctl_suspend(); +#ifdef USE_POWER_MANAGER + case SYSCALL_POWER_MANAGER_SUSPEND: { + args[0] = pm_suspend(); } break; - case SYSCALL_POWERCTL_HIBERNATE: { - args[0] = powerctl_hibernate(); + case SYSCALL_POWER_MANAGER_HIBERNATE: { + args[0] = pm_hibernate(); } - case SYSCALL_POWERCTL_GET_STATUS: { - powerctl_status_t *status = (powerctl_status_t *)args[0]; - args[0] = powerctl_get_status__verified(status); + case SYSCALL_POWER_MANAGER_GET_STATE: { + pm_state_t *status = (pm_state_t *)args[0]; + args[0] = pm_get_state__verified(status); } #endif diff --git a/core/embed/sys/syscall/stm32/syscall_numbers.h b/core/embed/sys/syscall/stm32/syscall_numbers.h index 1b65a6ae39..c999707cc9 100644 --- a/core/embed/sys/syscall/stm32/syscall_numbers.h +++ b/core/embed/sys/syscall/stm32/syscall_numbers.h @@ -149,9 +149,9 @@ typedef enum { SYSCALL_BLE_CAN_READ, SYSCALL_BLE_READ, - SYSCALL_POWERCTL_SUSPEND, - SYSCALL_POWERCTL_HIBERNATE, - SYSCALL_POWERCTL_GET_STATUS, + SYSCALL_POWER_MANAGER_SUSPEND, + SYSCALL_POWER_MANAGER_HIBERNATE, + SYSCALL_POWER_MANAGER_GET_STATE, SYSCALL_JPEGDEC_OPEN, SYSCALL_JPEGDEC_CLOSE, diff --git a/core/embed/sys/syscall/stm32/syscall_stubs.c b/core/embed/sys/syscall/stm32/syscall_stubs.c index d98c30f956..9cb20c820a 100644 --- a/core/embed/sys/syscall/stm32/syscall_stubs.c +++ b/core/embed/sys/syscall/stm32/syscall_stubs.c @@ -680,24 +680,27 @@ uint32_t ble_read(uint8_t *data, uint16_t len) { #endif // ============================================================================= -// powerctl.h +// power_manager.h // ============================================================================= -#ifdef USE_POWERCTL +#ifdef USE_POWER_MANAGER -#include +#include -void powerctl_suspend(void) { syscall_invoke0(SYSCALL_POWERCTL_SUSPEND); } - -bool powerctl_hibernate(void) { - return (bool)syscall_invoke0(SYSCALL_POWERCTL_HIBERNATE); +pm_status_t pm_suspend(void) { + return (pm_status_t)syscall_invoke0(SYSCALL_POWER_MANAGER_SUSPEND); } -bool powerctl_get_status(powerctl_status_t *status) { - return (bool)syscall_invoke1((uint32_t)status, SYSCALL_POWERCTL_GET_STATUS); +pm_status_t pm_hibernate(void) { + return (pm_status_t)syscall_invoke0(SYSCALL_POWER_MANAGER_HIBERNATE); } -#endif // USE_POWERCTL +pm_status_t pm_get_status(pm_state_t *status) { + return (pm_status_t)syscall_invoke1((uint32_t)status, + SYSCALL_POWER_MANAGER_GET_STATE); +} + +#endif // USE_POWER_MANAGER // ============================================================================= // jpegdec.h diff --git a/core/embed/sys/syscall/stm32/syscall_verifiers.c b/core/embed/sys/syscall/stm32/syscall_verifiers.c index 0b04ea539b..6c7fd39928 100644 --- a/core/embed/sys/syscall/stm32/syscall_verifiers.c +++ b/core/embed/sys/syscall/stm32/syscall_verifiers.c @@ -817,22 +817,22 @@ access_violation: // --------------------------------------------------------------------- -#ifdef USE_POWERCTL +#ifdef USE_POWER_MANAGER -bool powerctl_get_status__verified(powerctl_status_t *status) { +pm_status_t pm_get_state__verified(pm_state_t *status) { if (!probe_write_access(status, sizeof(*status))) { goto access_violation; } - powerctl_status_t status_copy = {0}; - bool retval = powerctl_get_status(&status_copy); + pm_state_t status_copy = {0}; + pm_status_t retval = pm_get_state(&status_copy); *status = status_copy; return retval; access_violation: apptask_access_violation(); - return false; + return PM_ERROR; } #endif diff --git a/core/embed/sys/syscall/stm32/syscall_verifiers.h b/core/embed/sys/syscall/stm32/syscall_verifiers.h index 880e350aa6..77b99e473d 100644 --- a/core/embed/sys/syscall/stm32/syscall_verifiers.h +++ b/core/embed/sys/syscall/stm32/syscall_verifiers.h @@ -209,11 +209,11 @@ secbool ble_read__verified(uint8_t *data, size_t len); // --------------------------------------------------------------------- -#ifdef USE_POWERCTL +#ifdef USE_POWER_MANAGER -#include +#include -bool powerctl_get_status__verified(powerctl_status_t *status); +pm_status_t pm_get_state__verified(pm_state_t *status); #endif