1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2021 Linaro Limited
5 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
7 * The devfreq device combined with the energy model and the load can
8 * give an estimation of the power consumption as well as limiting the
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/cpumask.h>
15 #include <linux/devfreq.h>
16 #include <linux/dtpm.h>
17 #include <linux/energy_model.h>
19 #include <linux/pm_qos.h>
20 #include <linux/slab.h>
21 #include <linux/units.h>
25 struct dev_pm_qos_request qos_req
;
26 struct devfreq
*devfreq
;
29 static struct dtpm_devfreq
*to_dtpm_devfreq(struct dtpm
*dtpm
)
31 return container_of(dtpm
, struct dtpm_devfreq
, dtpm
);
34 static int update_pd_power_uw(struct dtpm
*dtpm
)
36 struct dtpm_devfreq
*dtpm_devfreq
= to_dtpm_devfreq(dtpm
);
37 struct devfreq
*devfreq
= dtpm_devfreq
->devfreq
;
38 struct device
*dev
= devfreq
->dev
.parent
;
39 struct em_perf_domain
*pd
= em_pd_get(dev
);
40 struct em_perf_state
*table
;
43 table
= em_perf_state_from_pd(pd
);
45 dtpm
->power_min
= table
[0].power
;
47 dtpm
->power_max
= table
[pd
->nr_perf_states
- 1].power
;
53 static u64
set_pd_power_limit(struct dtpm
*dtpm
, u64 power_limit
)
55 struct dtpm_devfreq
*dtpm_devfreq
= to_dtpm_devfreq(dtpm
);
56 struct devfreq
*devfreq
= dtpm_devfreq
->devfreq
;
57 struct device
*dev
= devfreq
->dev
.parent
;
58 struct em_perf_domain
*pd
= em_pd_get(dev
);
59 struct em_perf_state
*table
;
64 table
= em_perf_state_from_pd(pd
);
65 for (i
= 0; i
< pd
->nr_perf_states
; i
++) {
66 if (table
[i
].power
> power_limit
)
70 freq
= table
[i
- 1].frequency
;
71 power_limit
= table
[i
- 1].power
;
74 dev_pm_qos_update_request(&dtpm_devfreq
->qos_req
, freq
);
79 static void _normalize_load(struct devfreq_dev_status
*status
)
81 if (status
->total_time
> 0xfffff) {
82 status
->total_time
>>= 10;
83 status
->busy_time
>>= 10;
86 status
->busy_time
<<= 10;
87 status
->busy_time
/= status
->total_time
? : 1;
89 status
->busy_time
= status
->busy_time
? : 1;
90 status
->total_time
= 1024;
93 static u64
get_pd_power_uw(struct dtpm
*dtpm
)
95 struct dtpm_devfreq
*dtpm_devfreq
= to_dtpm_devfreq(dtpm
);
96 struct devfreq
*devfreq
= dtpm_devfreq
->devfreq
;
97 struct device
*dev
= devfreq
->dev
.parent
;
98 struct em_perf_domain
*pd
= em_pd_get(dev
);
99 struct devfreq_dev_status status
;
100 struct em_perf_state
*table
;
105 mutex_lock(&devfreq
->lock
);
106 status
= devfreq
->last_status
;
107 mutex_unlock(&devfreq
->lock
);
109 freq
= DIV_ROUND_UP(status
.current_frequency
, HZ_PER_KHZ
);
110 _normalize_load(&status
);
113 table
= em_perf_state_from_pd(pd
);
114 for (i
= 0; i
< pd
->nr_perf_states
; i
++) {
116 if (table
[i
].frequency
< freq
)
119 power
= table
[i
].power
;
120 power
*= status
.busy_time
;
130 static void pd_release(struct dtpm
*dtpm
)
132 struct dtpm_devfreq
*dtpm_devfreq
= to_dtpm_devfreq(dtpm
);
134 if (dev_pm_qos_request_active(&dtpm_devfreq
->qos_req
))
135 dev_pm_qos_remove_request(&dtpm_devfreq
->qos_req
);
140 static struct dtpm_ops dtpm_ops
= {
141 .set_power_uw
= set_pd_power_limit
,
142 .get_power_uw
= get_pd_power_uw
,
143 .update_power_uw
= update_pd_power_uw
,
144 .release
= pd_release
,
147 static int __dtpm_devfreq_setup(struct devfreq
*devfreq
, struct dtpm
*parent
)
149 struct device
*dev
= devfreq
->dev
.parent
;
150 struct dtpm_devfreq
*dtpm_devfreq
;
151 struct em_perf_domain
*pd
;
156 ret
= dev_pm_opp_of_register_em(dev
, NULL
);
158 pr_err("No energy model available for '%s'\n", dev_name(dev
));
163 dtpm_devfreq
= kzalloc(sizeof(*dtpm_devfreq
), GFP_KERNEL
);
167 dtpm_init(&dtpm_devfreq
->dtpm
, &dtpm_ops
);
169 dtpm_devfreq
->devfreq
= devfreq
;
171 ret
= dtpm_register(dev_name(dev
), &dtpm_devfreq
->dtpm
, parent
);
173 pr_err("Failed to register '%s': %d\n", dev_name(dev
), ret
);
178 ret
= dev_pm_qos_add_request(dev
, &dtpm_devfreq
->qos_req
,
179 DEV_PM_QOS_MAX_FREQUENCY
,
180 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE
);
182 pr_err("Failed to add QoS request: %d\n", ret
);
183 goto out_dtpm_unregister
;
186 dtpm_update_power(&dtpm_devfreq
->dtpm
);
191 dtpm_unregister(&dtpm_devfreq
->dtpm
);
196 static int dtpm_devfreq_setup(struct dtpm
*dtpm
, struct device_node
*np
)
198 struct devfreq
*devfreq
;
200 devfreq
= devfreq_get_devfreq_by_node(np
);
204 return __dtpm_devfreq_setup(devfreq
, dtpm
);
207 struct dtpm_subsys_ops dtpm_devfreq_ops
= {
208 .name
= KBUILD_MODNAME
,
209 .setup
= dtpm_devfreq_setup
,