1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2020 Linaro Limited
5 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
7 * The powercap based Dynamic Thermal Power Management framework
8 * provides to the userspace a consistent API to set the power limit
11 * DTPM defines the functions to create a tree of constraints. Each
12 * parent node is a virtual description of the aggregation of the
13 * children. It propagates the constraints set at its level to its
14 * children and collect the children power information. The leaves of
15 * the tree are the real devices which have the ability to get their
16 * current power consumption and set their power limit.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/dtpm.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/powercap.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
28 #include "dtpm_subsys.h"
30 #define DTPM_POWER_LIMIT_FLAG 0
32 static const char *constraint_name[] = {
36 static DEFINE_MUTEX(dtpm_lock);
37 static struct powercap_control_type *pct;
38 static struct dtpm *root;
40 static int get_time_window_us(struct powercap_zone *pcz, int cid, u64 *window)
45 static int set_time_window_us(struct powercap_zone *pcz, int cid, u64 window)
50 static int get_max_power_range_uw(struct powercap_zone *pcz, u64 *max_power_uw)
52 struct dtpm *dtpm = to_dtpm(pcz);
54 *max_power_uw = dtpm->power_max - dtpm->power_min;
59 static int __get_power_uw(struct dtpm *dtpm, u64 *power_uw)
66 *power_uw = dtpm->ops->get_power_uw(dtpm);
72 list_for_each_entry(child, &dtpm->children, sibling) {
73 ret = __get_power_uw(child, &power);
82 static int get_power_uw(struct powercap_zone *pcz, u64 *power_uw)
84 return __get_power_uw(to_dtpm(pcz), power_uw);
87 static void __dtpm_rebalance_weight(struct dtpm *dtpm)
91 list_for_each_entry(child, &dtpm->children, sibling) {
93 pr_debug("Setting weight '%d' for '%s'\n",
94 child->weight, child->zone.name);
96 child->weight = DIV64_U64_ROUND_CLOSEST(
97 child->power_max * 1024, dtpm->power_max);
99 __dtpm_rebalance_weight(child);
103 static void __dtpm_sub_power(struct dtpm *dtpm)
105 struct dtpm *parent = dtpm->parent;
108 parent->power_min -= dtpm->power_min;
109 parent->power_max -= dtpm->power_max;
110 parent->power_limit -= dtpm->power_limit;
111 parent = parent->parent;
115 static void __dtpm_add_power(struct dtpm *dtpm)
117 struct dtpm *parent = dtpm->parent;
120 parent->power_min += dtpm->power_min;
121 parent->power_max += dtpm->power_max;
122 parent->power_limit += dtpm->power_limit;
123 parent = parent->parent;
128 * dtpm_update_power - Update the power on the dtpm
129 * @dtpm: a pointer to a dtpm structure to update
131 * Function to update the power values of the dtpm node specified in
132 * parameter. These new values will be propagated to the tree.
134 * Return: zero on success, -EINVAL if the values are inconsistent
136 int dtpm_update_power(struct dtpm *dtpm)
140 __dtpm_sub_power(dtpm);
142 ret = dtpm->ops->update_power_uw(dtpm);
144 pr_err("Failed to update power for '%s': %d\n",
145 dtpm->zone.name, ret);
147 if (!test_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags))
148 dtpm->power_limit = dtpm->power_max;
150 __dtpm_add_power(dtpm);
153 __dtpm_rebalance_weight(root);
159 * dtpm_release_zone - Cleanup when the node is released
160 * @pcz: a pointer to a powercap_zone structure
162 * Do some housecleaning and update the weight on the tree. The
163 * release will be denied if the node has children. This function must
164 * be called by the specific release callback of the different
167 * Return: 0 on success, -EBUSY if there are children
169 int dtpm_release_zone(struct powercap_zone *pcz)
171 struct dtpm *dtpm = to_dtpm(pcz);
172 struct dtpm *parent = dtpm->parent;
174 if (!list_empty(&dtpm->children))
178 list_del(&dtpm->sibling);
180 __dtpm_sub_power(dtpm);
183 dtpm->ops->release(dtpm);
190 static int get_power_limit_uw(struct powercap_zone *pcz,
191 int cid, u64 *power_limit)
193 *power_limit = to_dtpm(pcz)->power_limit;
199 * Set the power limit on the nodes, the power limit is distributed
200 * given the weight of the children.
202 * The dtpm node lock must be held when calling this function.
204 static int __set_power_limit_uw(struct dtpm *dtpm, int cid, u64 power_limit)
211 * A max power limitation means we remove the power limit,
212 * otherwise we set a constraint and flag the dtpm node.
214 if (power_limit == dtpm->power_max) {
215 clear_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags);
217 set_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags);
220 pr_debug("Setting power limit for '%s': %llu uW\n",
221 dtpm->zone.name, power_limit);
224 * Only leaves of the dtpm tree has ops to get/set the power
227 dtpm->power_limit = dtpm->ops->set_power_uw(dtpm, power_limit);
229 dtpm->power_limit = 0;
231 list_for_each_entry(child, &dtpm->children, sibling) {
234 * Integer division rounding will inevitably
235 * lead to a different min or max value when
236 * set several times. In order to restore the
237 * initial value, we force the child's min or
238 * max power every time if the constraint is
241 if (power_limit == dtpm->power_max) {
242 power = child->power_max;
243 } else if (power_limit == dtpm->power_min) {
244 power = child->power_min;
246 power = DIV_ROUND_CLOSEST_ULL(
247 power_limit * child->weight, 1024);
250 pr_debug("Setting power limit for '%s': %llu uW\n",
251 child->zone.name, power);
253 ret = __set_power_limit_uw(child, cid, power);
255 ret = get_power_limit_uw(&child->zone, cid, &power);
260 dtpm->power_limit += power;
267 static int set_power_limit_uw(struct powercap_zone *pcz,
268 int cid, u64 power_limit)
270 struct dtpm *dtpm = to_dtpm(pcz);
274 * Don't allow values outside of the power range previously
275 * set when initializing the power numbers.
277 power_limit = clamp_val(power_limit, dtpm->power_min, dtpm->power_max);
279 ret = __set_power_limit_uw(dtpm, cid, power_limit);
281 pr_debug("%s: power limit: %llu uW, power max: %llu uW\n",
282 dtpm->zone.name, dtpm->power_limit, dtpm->power_max);
287 static const char *get_constraint_name(struct powercap_zone *pcz, int cid)
289 return constraint_name[cid];
292 static int get_max_power_uw(struct powercap_zone *pcz, int id, u64 *max_power)
294 *max_power = to_dtpm(pcz)->power_max;
299 static struct powercap_zone_constraint_ops constraint_ops = {
300 .set_power_limit_uw = set_power_limit_uw,
301 .get_power_limit_uw = get_power_limit_uw,
302 .set_time_window_us = set_time_window_us,
303 .get_time_window_us = get_time_window_us,
304 .get_max_power_uw = get_max_power_uw,
305 .get_name = get_constraint_name,
308 static struct powercap_zone_ops zone_ops = {
309 .get_max_power_range_uw = get_max_power_range_uw,
310 .get_power_uw = get_power_uw,
311 .release = dtpm_release_zone,
315 * dtpm_init - Allocate and initialize a dtpm struct
316 * @dtpm: The dtpm struct pointer to be initialized
317 * @ops: The dtpm device specific ops, NULL for a virtual node
319 void dtpm_init(struct dtpm *dtpm, struct dtpm_ops *ops)
322 INIT_LIST_HEAD(&dtpm->children);
323 INIT_LIST_HEAD(&dtpm->sibling);
330 * dtpm_unregister - Unregister a dtpm node from the hierarchy tree
331 * @dtpm: a pointer to a dtpm structure corresponding to the node to be removed
333 * Call the underlying powercap unregister function. That will call
334 * the release callback of the powercap zone.
336 void dtpm_unregister(struct dtpm *dtpm)
338 powercap_unregister_zone(pct, &dtpm->zone);
340 pr_debug("Unregistered dtpm node '%s'\n", dtpm->zone.name);
344 * dtpm_register - Register a dtpm node in the hierarchy tree
345 * @name: a string specifying the name of the node
346 * @dtpm: a pointer to a dtpm structure corresponding to the new node
347 * @parent: a pointer to a dtpm structure corresponding to the parent node
349 * Create a dtpm node in the tree. If no parent is specified, the node
350 * is the root node of the hierarchy. If the root node already exists,
351 * then the registration will fail. The powercap controller must be
352 * initialized before calling this function.
354 * The dtpm structure must be initialized with the power numbers
355 * before calling this function.
357 * Return: zero on success, a negative value in case of error:
358 * -EAGAIN: the function is called before the framework is initialized.
359 * -EBUSY: the root node is already inserted
360 * -EINVAL: * there is no root node yet and @parent is specified
361 * * no all ops are defined
362 * * parent have ops which are reserved for leaves
363 * Other negative values are reported back from the powercap framework
365 int dtpm_register(const char *name, struct dtpm *dtpm, struct dtpm *parent)
367 struct powercap_zone *pcz;
378 if (parent && parent->ops)
384 if (dtpm->ops && !(dtpm->ops->set_power_uw &&
385 dtpm->ops->get_power_uw &&
386 dtpm->ops->update_power_uw &&
390 pcz = powercap_register_zone(&dtpm->zone, pct, name,
391 parent ? &parent->zone : NULL,
392 &zone_ops, MAX_DTPM_CONSTRAINTS,
398 list_add_tail(&dtpm->sibling, &parent->children);
399 dtpm->parent = parent;
404 if (dtpm->ops && !dtpm->ops->update_power_uw(dtpm)) {
405 __dtpm_add_power(dtpm);
406 dtpm->power_limit = dtpm->power_max;
409 pr_debug("Registered dtpm node '%s' / %llu-%llu uW, \n",
410 dtpm->zone.name, dtpm->power_min, dtpm->power_max);
415 static struct dtpm *dtpm_setup_virtual(const struct dtpm_node *hierarchy,
421 dtpm = kzalloc(sizeof(*dtpm), GFP_KERNEL);
423 return ERR_PTR(-ENOMEM);
424 dtpm_init(dtpm, NULL);
426 ret = dtpm_register(hierarchy->name, dtpm, parent);
428 pr_err("Failed to register dtpm node '%s': %d\n",
429 hierarchy->name, ret);
437 static struct dtpm *dtpm_setup_dt(const struct dtpm_node *hierarchy,
440 struct device_node *np;
443 np = of_find_node_by_path(hierarchy->name);
445 pr_err("Failed to find '%s'\n", hierarchy->name);
446 return ERR_PTR(-ENXIO);
449 for (i = 0; i < ARRAY_SIZE(dtpm_subsys); i++) {
451 if (!dtpm_subsys[i]->setup)
454 ret = dtpm_subsys[i]->setup(parent, np);
456 pr_err("Failed to setup '%s': %d\n", dtpm_subsys[i]->name, ret);
465 * By returning a NULL pointer, we let know the caller there
466 * is no child for us as we are a leaf of the tree
471 typedef struct dtpm * (*dtpm_node_callback_t)(const struct dtpm_node *, struct dtpm *);
473 static dtpm_node_callback_t dtpm_node_callback[] = {
474 [DTPM_NODE_VIRTUAL] = dtpm_setup_virtual,
475 [DTPM_NODE_DT] = dtpm_setup_dt,
478 static int dtpm_for_each_child(const struct dtpm_node *hierarchy,
479 const struct dtpm_node *it, struct dtpm *parent)
484 for (i = 0; hierarchy[i].name; i++) {
486 if (hierarchy[i].parent != it)
489 dtpm = dtpm_node_callback[hierarchy[i].type](&hierarchy[i], parent);
492 * A NULL pointer means there is no children, hence we
493 * continue without going deeper in the recursivity.
499 * There are multiple reasons why the callback could
500 * fail. The generic glue is abstracting the backend
501 * and therefore it is not possible to report back or
502 * take a decision based on the error. In any case,
503 * if this call fails, it is not critical in the
504 * hierarchy creation, we can assume the underlying
505 * service is not found, so we continue without this
506 * branch in the tree but with a warning to log the
507 * information the node was not created.
510 pr_warn("Failed to create '%s' in the hierarchy\n",
515 ret = dtpm_for_each_child(hierarchy, &hierarchy[i], dtpm);
524 * dtpm_create_hierarchy - Create the dtpm hierarchy
525 * @hierarchy: An array of struct dtpm_node describing the hierarchy
527 * The function is called by the platform specific code with the
528 * description of the different node in the hierarchy. It creates the
529 * tree in the sysfs filesystem under the powercap dtpm entry.
531 * The expected tree has the format:
533 * struct dtpm_node hierarchy[] = {
534 * [0] { .name = "topmost", type = DTPM_NODE_VIRTUAL },
535 * [1] { .name = "package", .type = DTPM_NODE_VIRTUAL, .parent = &hierarchy[0] },
536 * [2] { .name = "/cpus/cpu0", .type = DTPM_NODE_DT, .parent = &hierarchy[1] },
537 * [3] { .name = "/cpus/cpu1", .type = DTPM_NODE_DT, .parent = &hierarchy[1] },
538 * [4] { .name = "/cpus/cpu2", .type = DTPM_NODE_DT, .parent = &hierarchy[1] },
539 * [5] { .name = "/cpus/cpu3", .type = DTPM_NODE_DT, .parent = &hierarchy[1] },
543 * The last element is always an empty one and marks the end of the
546 * Return: zero on success, a negative value in case of error. Errors
547 * are reported back from the underlying functions.
549 int dtpm_create_hierarchy(struct of_device_id *dtpm_match_table)
551 const struct of_device_id *match;
552 const struct dtpm_node *hierarchy;
553 struct device_node *np;
556 mutex_lock(&dtpm_lock);
563 pct = powercap_register_control_type(NULL, "dtpm", NULL);
565 pr_err("Failed to register control type\n");
571 np = of_find_node_by_path("/");
575 match = of_match_node(dtpm_match_table, np);
582 hierarchy = match->data;
588 ret = dtpm_for_each_child(hierarchy, NULL, NULL);
592 for (i = 0; i < ARRAY_SIZE(dtpm_subsys); i++) {
594 if (!dtpm_subsys[i]->init)
597 ret = dtpm_subsys[i]->init();
599 pr_info("Failed to initialize '%s': %d",
600 dtpm_subsys[i]->name, ret);
603 mutex_unlock(&dtpm_lock);
608 powercap_unregister_control_type(pct);
612 mutex_unlock(&dtpm_lock);
616 EXPORT_SYMBOL_GPL(dtpm_create_hierarchy);
618 static void __dtpm_destroy_hierarchy(struct dtpm *dtpm)
620 struct dtpm *child, *aux;
622 list_for_each_entry_safe(child, aux, &dtpm->children, sibling)
623 __dtpm_destroy_hierarchy(child);
626 * At this point, we know all children were removed from the
627 * recursive call before
629 dtpm_unregister(dtpm);
632 void dtpm_destroy_hierarchy(void)
636 mutex_lock(&dtpm_lock);
641 __dtpm_destroy_hierarchy(root);
644 for (i = 0; i < ARRAY_SIZE(dtpm_subsys); i++) {
646 if (!dtpm_subsys[i]->exit)
649 dtpm_subsys[i]->exit();
652 powercap_unregister_control_type(pct);
659 mutex_unlock(&dtpm_lock);
661 EXPORT_SYMBOL_GPL(dtpm_destroy_hierarchy);