[PATCH 12/17] PM: EM: Add argument to get_cost() for runtime modification

From: Lukasz Luba
Date: Tue Mar 14 2023 - 06:36:46 EST


The Energy Model (EM) supports runtime modifications. Let also the
artificial EM use this new feature and allow to update the 'cost' values
at runtime. When the artificial EM is used there is a need to provide
two callbacks: get_cost() and update_power(), not only the last one.

Update also CPPC driver code, since the new argument is needed there
to compile properly and register EM.

Signed-off-by: Lukasz Luba <lukasz.luba@xxxxxxx>
---
drivers/cpufreq/cppc_cpufreq.c | 2 +-
include/linux/energy_model.h | 7 ++++++-
kernel/power/energy_model.c | 9 +++++----
3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
index 432dfb4e8027..bfbfb7b134ac 100644
--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
@@ -573,7 +573,7 @@ static int cppc_get_cpu_power(struct device *cpu_dev,
}

static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz,
- unsigned long *cost)
+ unsigned long *cost, void *priv)
{
unsigned long perf_step, perf_prev;
struct cppc_perf_caps *perf_caps;
diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h
index 8e3fa2b6bf28..b8506df9af2d 100644
--- a/include/linux/energy_model.h
+++ b/include/linux/energy_model.h
@@ -162,6 +162,8 @@ struct em_data_callback {
* @freq : Frequency at the performance state in kHz
* @cost : The cost value for the performance state
* (modified)
+ * @priv : Pointer to private data useful for tracking context
+ * during run-time modifications of EM.
*
* In case of CPUs, the cost is the one of a single CPU in the domain.
* It is expected to fit in the [0, EM_MAX_POWER] range due to internal
@@ -170,7 +172,7 @@ struct em_data_callback {
* Return 0 on success, or appropriate error value in case of failure.
*/
int (*get_cost)(struct device *dev, unsigned long freq,
- unsigned long *cost);
+ unsigned long *cost, void *priv);

/**
* update_power() - Provide new power at the given performance state of
@@ -199,6 +201,9 @@ struct em_data_callback {
#define EM_DATA_CB(_active_power_cb) \
EM_ADV_DATA_CB(_active_power_cb, NULL)
#define EM_UPDATE_CB(_update_power_cb) { .update_power = &_update_power_cb }
+#define EM_ADV_UPDATE_CB(_update_power_cb, _cost_cb) \
+ { .update_power = &_update_power_cb, \
+ .get_cost = _cost_cb }

struct em_perf_domain *em_cpu_get(int cpu);
struct em_perf_domain *em_pd_get(struct device *dev);
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index e0e8fba3d02b..4fcc3b9d63e0 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -168,7 +168,7 @@ static void em_perf_runtime_table_set(struct device *dev,

static int em_compute_costs(struct device *dev, struct em_perf_state *table,
struct em_data_callback *cb, int nr_states,
- unsigned long flags)
+ unsigned long flags, void *priv)
{
unsigned long prev_cost = ULONG_MAX;
u64 fmax;
@@ -180,7 +180,8 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
unsigned long power_res, cost;

if (flags & EM_PERF_DOMAIN_ARTIFICIAL && cb->get_cost) {
- ret = cb->get_cost(dev, table[i].frequency, &cost);
+ ret = cb->get_cost(dev, table[i].frequency, &cost,
+ priv);
if (ret || !cost || cost > EM_MAX_POWER) {
dev_err(dev, "EM: invalid cost %lu %d\n",
cost, ret);
@@ -280,7 +281,7 @@ int em_dev_update_perf_domain(struct device *dev, struct em_data_callback *cb,
}

ret = em_compute_costs(dev, runtime_table->state, cb,
- pd->nr_perf_states, pd->flags);
+ pd->nr_perf_states, pd->flags, priv);
if (ret)
goto free_runtime_state_table;

@@ -350,7 +351,7 @@ static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
table[i].frequency = prev_freq = freq;
}

- ret = em_compute_costs(dev, table, cb, nr_states, flags);
+ ret = em_compute_costs(dev, table, cb, nr_states, flags, NULL);
if (ret)
goto free_ps_table;

--
2.17.1