Fix battery drain sources across suspend, CPUfreq, radios, and daemons
This commit is contained in:
1 parent
4b36aa0726
commit
7f3b3c2c0c
24 files changed
+487
-64
No files matched your search
@@ -41,6 +41,18 @@ typedef struct {
|
||||
uint64_t last_input_ms;
|
||||
geminux_idle_stage_t idle_stage;
|
||||
geminux_power_policy_t policy;
|
||||
bool charge_thresh_set;
|
||||
bool policy_applied;
|
||||
geminux_profile_t applied_profile;
|
||||
bool applied_ac;
|
||||
bool applied_a72_ready;
|
||||
bool applied_cpufreq_present;
|
||||
pid_t consys_restore_pid;
|
||||
bool consys_suspended;
|
||||
bool bt_was_loaded;
|
||||
bool bt_was_blocked;
|
||||
bool gnss_was_loaded;
|
||||
bool radio_was_loaded;
|
||||
} powerd_ctx_t;
|
||||
|
||||
static uint64_t monotonic_ms(void) {
|
||||
@@ -78,7 +90,11 @@ static void ensure_run_dir(void) {
|
||||
}
|
||||
|
||||
static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
|
||||
write_sysfs_str("/sys/class/power_supply/battery/charge_control_end_threshold", "100");
|
||||
if (!ctx->charge_thresh_set) {
|
||||
if (write_sysfs_str("/sys/class/power_supply/battery/charge_control_end_threshold", "100") == 0) {
|
||||
ctx->charge_thresh_set = true;
|
||||
}
|
||||
}
|
||||
ctx->policy.on_ac_power = false;
|
||||
DIR *d = opendir("/sys/class/power_supply");
|
||||
if (d) {
|
||||
@@ -117,6 +133,8 @@ static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
|
||||
|
||||
static void lock_x11_session(void);
|
||||
static void trigger_suspend(powerd_ctx_t *ctx);
|
||||
static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force);
|
||||
static void apply_cpu_policy(powerd_ctx_t *ctx, bool force);
|
||||
|
||||
static void save_active_brightness(powerd_ctx_t *ctx, const char *bri_path) {
|
||||
if (ctx->idle_stage != IDLE_STAGE_ACTIVE) return;
|
||||
@@ -222,10 +240,19 @@ static void note_user_activity(powerd_ctx_t *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_cpu_policy(powerd_ctx_t *ctx) {
|
||||
static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) {
|
||||
bool a72_ready = (access("/sys/module/mt6797_a72", F_OK) == 0);
|
||||
bool cpufreq_present = (access("/sys/devices/system/cpu/cpufreq/policy0", F_OK) == 0);
|
||||
if (!force && ctx->policy_applied &&
|
||||
ctx->applied_profile == ctx->policy.profile &&
|
||||
ctx->applied_ac == ctx->policy.on_ac_power &&
|
||||
ctx->applied_a72_ready == a72_ready &&
|
||||
ctx->applied_cpufreq_present == cpufreq_present) {
|
||||
return;
|
||||
}
|
||||
|
||||
geminux_profile_params_t par = geminux_policy_params(ctx->policy.profile, ctx->policy.on_ac_power);
|
||||
|
||||
bool a72_ready = (access("/sys/module/mt6797_a72", F_OK) == 0);
|
||||
if (!par.a72_online || a72_ready) {
|
||||
for (int cpu = 8; cpu <= 9; cpu++) {
|
||||
char p[128];
|
||||
@@ -244,7 +271,8 @@ static void apply_cpu_policy(powerd_ctx_t *ctx) {
|
||||
write_sysfs_str(p, par.governor);
|
||||
|
||||
int first_cpu = atoi(de->d_name + 6);
|
||||
unsigned long cap = (first_cpu >= 8) ? par.a72_max_khz : par.a53_max_khz;
|
||||
unsigned long cap = (first_cpu >= 8) ? par.a72_max_khz :
|
||||
(first_cpu >= 4) ? par.a53_max_khz : par.a53_ll_max_khz;
|
||||
if (cap > 0) {
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_max_freq", de->d_name);
|
||||
snprintf(freq_buf, sizeof(freq_buf), "%lu", cap);
|
||||
@@ -254,8 +282,20 @@ static void apply_cpu_policy(powerd_ctx_t *ctx) {
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
ensure_run_dir();
|
||||
write_sysfs_str(PROFILE_FILE, geminux_policy_profile_name(ctx->policy.profile));
|
||||
if (force || !ctx->policy_applied || ctx->applied_profile != ctx->policy.profile) {
|
||||
ensure_run_dir();
|
||||
write_sysfs_str(PROFILE_FILE, geminux_policy_profile_name(ctx->policy.profile));
|
||||
}
|
||||
|
||||
ctx->policy_applied = true;
|
||||
ctx->applied_profile = ctx->policy.profile;
|
||||
ctx->applied_ac = ctx->policy.on_ac_power;
|
||||
ctx->applied_a72_ready = a72_ready;
|
||||
ctx->applied_cpufreq_present = cpufreq_present;
|
||||
}
|
||||
|
||||
static void apply_cpu_policy(powerd_ctx_t *ctx, bool force) {
|
||||
apply_cpu_sysfs(ctx, force);
|
||||
evaluate_idle(ctx);
|
||||
}
|
||||
|
||||
@@ -334,23 +374,194 @@ static void lock_x11_session(void) {
|
||||
close(pfd[0]);
|
||||
}
|
||||
|
||||
static int run_cmd_sync(const char *const argv[]) {
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) return -1;
|
||||
if (pid == 0) {
|
||||
int devnull = open("/dev/null", O_RDWR);
|
||||
if (devnull >= 0) {
|
||||
if (devnull != STDIN_FILENO) dup2(devnull, STDIN_FILENO);
|
||||
if (devnull != STDOUT_FILENO) dup2(devnull, STDOUT_FILENO);
|
||||
if (devnull != STDERR_FILENO) dup2(devnull, STDERR_FILENO);
|
||||
if (devnull > 2) close(devnull);
|
||||
}
|
||||
execv(argv[0], (char *const *)argv);
|
||||
_exit(127);
|
||||
}
|
||||
int st = 0;
|
||||
while (waitpid(pid, &st, 0) < 0) {
|
||||
if (errno != EINTR) return -1;
|
||||
}
|
||||
return (WIFEXITED(st) && WEXITSTATUS(st) == 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
static void reap_consys_restore(powerd_ctx_t *ctx, bool wait_block) {
|
||||
if (ctx->consys_restore_pid <= 0) return;
|
||||
int opts = wait_block ? 0 : WNOHANG;
|
||||
pid_t r;
|
||||
do {
|
||||
r = waitpid(ctx->consys_restore_pid, NULL, opts);
|
||||
} while (r < 0 && errno == EINTR);
|
||||
if (r == ctx->consys_restore_pid || (r < 0 && errno == ECHILD)) {
|
||||
ctx->consys_restore_pid = -1;
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_bt_rfkill_soft_blocked(void) {
|
||||
DIR *d = opendir("/sys/class/rfkill");
|
||||
if (!d) return true;
|
||||
bool found = false;
|
||||
bool blocked = true;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], typ[32] = {0}, soft[16] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/type", de->d_name);
|
||||
if (read_sysfs_line(p, typ, sizeof(typ)) == 0 && strcmp(typ, "bluetooth") == 0) {
|
||||
found = true;
|
||||
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/soft", de->d_name);
|
||||
if (read_sysfs_line(p, soft, sizeof(soft)) == 0 && atoi(soft) == 0) {
|
||||
blocked = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
return found ? blocked : true;
|
||||
}
|
||||
|
||||
static void set_bt_rfkill_soft_blocked(bool blocked) {
|
||||
DIR *d = opendir("/sys/class/rfkill");
|
||||
if (d) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], typ[32] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/type", de->d_name);
|
||||
if (read_sysfs_line(p, typ, sizeof(typ)) == 0 && strcmp(typ, "bluetooth") == 0) {
|
||||
snprintf(p, sizeof(p), "/sys/class/rfkill/%s/soft", de->d_name);
|
||||
write_sysfs_str(p, blocked ? "1" : "0");
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
if (blocked) {
|
||||
const char *argv[] = {"/usr/sbin/rfkill", "block", "bluetooth", NULL};
|
||||
run_cmd_sync(argv);
|
||||
}
|
||||
}
|
||||
|
||||
/* mt6797_consys_prepare() refuses suspend with -EBUSY while CONN is powered;
|
||||
* unloading reverse dependents lets mt6797_consys_remove() power off CONN/VCN rails. */
|
||||
static void prepare_consys_for_suspend(powerd_ctx_t *ctx) {
|
||||
reap_consys_restore(ctx, true);
|
||||
if (access("/sys/module/mt6797_consys", F_OK) != 0) {
|
||||
ctx->consys_suspended = false;
|
||||
return;
|
||||
}
|
||||
ctx->consys_suspended = true;
|
||||
ctx->bt_was_loaded = (access("/sys/module/btmt6797", F_OK) == 0);
|
||||
ctx->bt_was_blocked = ctx->bt_was_loaded ? is_bt_rfkill_soft_blocked() : true;
|
||||
ctx->gnss_was_loaded = (access("/sys/module/gnss_mt6797", F_OK) == 0);
|
||||
ctx->radio_was_loaded = (access("/sys/module/radio_mt6797", F_OK) == 0);
|
||||
|
||||
static const char *const mods[] = {
|
||||
"btmt6797",
|
||||
"radio_mt6797",
|
||||
"gnss_mt6797",
|
||||
"mac",
|
||||
"mt6797_consys",
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(mods) / sizeof(mods[0]); i++) {
|
||||
char mpath[128];
|
||||
snprintf(mpath, sizeof(mpath), "/sys/module/%s", mods[i]);
|
||||
if (access(mpath, F_OK) == 0) {
|
||||
const char *argv[] = {"/sbin/rmmod", mods[i], NULL};
|
||||
run_cmd_sync(argv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void restore_consys_after_resume(powerd_ctx_t *ctx) {
|
||||
if (!ctx->consys_suspended) return;
|
||||
ctx->consys_suspended = false;
|
||||
reap_consys_restore(ctx, true);
|
||||
|
||||
bool bt_loaded = ctx->bt_was_loaded;
|
||||
bool bt_blocked = ctx->bt_was_blocked;
|
||||
bool gnss_loaded = ctx->gnss_was_loaded;
|
||||
bool radio_loaded = ctx->radio_was_loaded;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) return;
|
||||
if (pid == 0) {
|
||||
int devnull = open("/dev/null", O_RDWR);
|
||||
if (devnull >= 0) {
|
||||
if (devnull != STDIN_FILENO) dup2(devnull, STDIN_FILENO);
|
||||
if (devnull != STDOUT_FILENO) dup2(devnull, STDOUT_FILENO);
|
||||
if (devnull != STDERR_FILENO) dup2(devnull, STDERR_FILENO);
|
||||
if (devnull > 2) close(devnull);
|
||||
}
|
||||
const char *wlan_core[] = {"/sbin/modprobe", "mt6797-wlan-core", NULL};
|
||||
const char *consys[] = {"/sbin/modprobe", "mt6797-consys", NULL};
|
||||
const char *mac[] = {"/sbin/modprobe", "mac", NULL};
|
||||
run_cmd_sync(wlan_core);
|
||||
run_cmd_sync(consys);
|
||||
run_cmd_sync(mac);
|
||||
if (bt_loaded) {
|
||||
const char *bt[] = {"/sbin/modprobe", "btmt6797", NULL};
|
||||
run_cmd_sync(bt);
|
||||
if (bt_blocked) {
|
||||
set_bt_rfkill_soft_blocked(true);
|
||||
}
|
||||
}
|
||||
if (gnss_loaded) {
|
||||
const char *gnss[] = {"/sbin/modprobe", "gnss-mt6797", NULL};
|
||||
run_cmd_sync(gnss);
|
||||
}
|
||||
if (radio_loaded) {
|
||||
const char *radio[] = {"/sbin/modprobe", "radio-mt6797", NULL};
|
||||
run_cmd_sync(radio);
|
||||
}
|
||||
_exit(0);
|
||||
}
|
||||
ctx->consys_restore_pid = pid;
|
||||
}
|
||||
|
||||
static void refresh_lid_state(powerd_ctx_t *ctx) {
|
||||
for (int i = 0; i < ctx->num_ev; i++) {
|
||||
unsigned long evbits = 0;
|
||||
if (ioctl(ctx->ev_fds[i], EVIOCGBIT(0, sizeof(evbits)), &evbits) >= 0 &&
|
||||
(evbits & (1UL << EV_SW))) {
|
||||
unsigned long swbits = 0, swstate = 0;
|
||||
if (ioctl(ctx->ev_fds[i], EVIOCGBIT(EV_SW, sizeof(swbits)), &swbits) >= 0 &&
|
||||
(swbits & (1UL << SW_LID)) &&
|
||||
ioctl(ctx->ev_fds[i], EVIOCGSW(sizeof(swstate)), &swstate) >= 0) {
|
||||
ctx->policy.lid_closed = !!(swstate & (1UL << SW_LID));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
lock_x11_session();
|
||||
toggle_backlight(ctx, false);
|
||||
prepare_consys_for_suspend(ctx);
|
||||
write_sysfs_str("/sys/power/mem_sleep", "deep");
|
||||
if (write_sysfs_str("/sys/power/state", "mem") == 0 ||
|
||||
write_sysfs_str("/sys/power/state", "freeze") == 0) {
|
||||
/* Woken back up from suspend */
|
||||
bool suspended = (write_sysfs_str("/sys/power/state", "mem") == 0 ||
|
||||
write_sysfs_str("/sys/power/state", "freeze") == 0);
|
||||
restore_consys_after_resume(ctx);
|
||||
refresh_lid_state(ctx);
|
||||
refresh_ac_and_hdmi(ctx);
|
||||
if (suspended && !ctx->policy.lid_closed) {
|
||||
ctx->idle_stage = IDLE_STAGE_ACTIVE;
|
||||
ctx->last_input_ms = monotonic_ms();
|
||||
toggle_backlight(ctx, true);
|
||||
refresh_ac_and_hdmi(ctx);
|
||||
apply_cpu_policy(ctx);
|
||||
} else if (!ctx->policy.lid_closed) {
|
||||
ctx->idle_stage = IDLE_STAGE_ACTIVE;
|
||||
ctx->last_input_ms = monotonic_ms();
|
||||
toggle_backlight(ctx, true);
|
||||
evaluate_idle(ctx);
|
||||
apply_cpu_policy(ctx, true);
|
||||
} else {
|
||||
toggle_backlight(ctx, false);
|
||||
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
apply_cpu_sysfs(ctx, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,7 +607,7 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
|
||||
|
||||
if (strncmp(buf, "profile ", 8) == 0) {
|
||||
if (geminux_policy_set_profile_name(&ctx->policy, buf + 8)) {
|
||||
apply_cpu_policy(ctx);
|
||||
apply_cpu_policy(ctx, false);
|
||||
dprintf(cfd, "OK profile=%s\n", geminux_policy_profile_name(ctx->policy.profile));
|
||||
} else {
|
||||
dprintf(cfd, "ERR unknown profile\n");
|
||||
@@ -406,11 +617,11 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
|
||||
trigger_suspend(ctx);
|
||||
} else if (strncmp(buf, "inhibit add ", 12) == 0) {
|
||||
geminux_policy_add_inhibit(&ctx->policy, buf + 12);
|
||||
apply_cpu_policy(ctx);
|
||||
apply_cpu_policy(ctx, false);
|
||||
dprintf(cfd, "OK inhibits=%d\n", ctx->policy.inhibit_count);
|
||||
} else if (strncmp(buf, "inhibit del ", 12) == 0) {
|
||||
geminux_policy_del_inhibit(&ctx->policy, buf + 12);
|
||||
apply_cpu_policy(ctx);
|
||||
apply_cpu_policy(ctx, false);
|
||||
dprintf(cfd, "OK inhibits=%d\n", ctx->policy.inhibit_count);
|
||||
} else if (strcmp(buf, "lid-suspend on") == 0) {
|
||||
ctx->policy.lid_suspend_enabled = true;
|
||||
@@ -469,6 +680,7 @@ int main(void) {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
powerd_ctx_t ctx;
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
ctx.consys_restore_pid = -1;
|
||||
ctx.saved_brightness = 128;
|
||||
geminux_policy_init(&ctx.policy);
|
||||
|
||||
@@ -497,12 +709,13 @@ int main(void) {
|
||||
ctx.idle_stage = IDLE_STAGE_ACTIVE;
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
scan_input_devices(&ctx);
|
||||
apply_cpu_policy(&ctx);
|
||||
apply_cpu_policy(&ctx, true);
|
||||
|
||||
struct epoll_event events[16];
|
||||
bool running = true;
|
||||
while (running) {
|
||||
int n = epoll_wait(ctx.epfd, events, 16, -1);
|
||||
reap_consys_restore(&ctx, false);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
@@ -514,7 +727,7 @@ int main(void) {
|
||||
while (read(ctx.sig_fd, &si, sizeof(si)) > 0) {
|
||||
if (si.ssi_signo == SIGHUP) {
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
apply_cpu_policy(&ctx);
|
||||
apply_cpu_policy(&ctx, true);
|
||||
} else {
|
||||
running = false;
|
||||
}
|
||||
@@ -534,19 +747,28 @@ int main(void) {
|
||||
char ubuf[2048];
|
||||
ssize_t ur;
|
||||
bool need_ev_rescan = false;
|
||||
bool need_ps_or_drm = false;
|
||||
while ((ur = recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0)) > 0) {
|
||||
if (memmem(ubuf, (size_t)ur, "SUBSYSTEM=input", 15)) {
|
||||
if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "input")) {
|
||||
need_ev_rescan = true;
|
||||
}
|
||||
if (geminux_uevent_has_subsystem(ubuf, (size_t)ur, "power_supply") ||
|
||||
geminux_uevent_has_subsystem(ubuf, (size_t)ur, "drm")) {
|
||||
need_ps_or_drm = true;
|
||||
}
|
||||
}
|
||||
if (need_ev_rescan) scan_input_devices(&ctx);
|
||||
bool prev_ac = ctx.policy.on_ac_power;
|
||||
bool prev_hdmi = ctx.policy.hdmi_connected;
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
if (prev_ac != ctx.policy.on_ac_power || prev_hdmi != ctx.policy.hdmi_connected) {
|
||||
note_user_activity(&ctx);
|
||||
if (need_ps_or_drm) {
|
||||
bool prev_ac = ctx.policy.on_ac_power;
|
||||
bool prev_hdmi = ctx.policy.hdmi_connected;
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
if (prev_ac != ctx.policy.on_ac_power || prev_hdmi != ctx.policy.hdmi_connected) {
|
||||
if (!ctx.policy.lid_closed) {
|
||||
note_user_activity(&ctx);
|
||||
}
|
||||
apply_cpu_policy(&ctx, false);
|
||||
}
|
||||
}
|
||||
apply_cpu_policy(&ctx);
|
||||
} else {
|
||||
struct input_event iev;
|
||||
bool saw_activity = false;
|
||||
@@ -567,7 +789,7 @@ int main(void) {
|
||||
ctx.last_input_ms = monotonic_ms();
|
||||
toggle_backlight(&ctx, true);
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
apply_cpu_policy(&ctx);
|
||||
apply_cpu_policy(&ctx, true);
|
||||
}
|
||||
} else if (iev.type == EV_KEY && iev.code == KEY_POWER && iev.value == 1) {
|
||||
if (ctx.idle_stage == IDLE_STAGE_LOCKED_BLANK) {
|
||||
|
||||
Reference in new issue
Block a user