// SPDX-License-Identifier: GPL-2.0-only /* * Copyright 2020 Linaro Limited * * Author: Daniel Lezcano <[email protected]> * * The powercap based Dynamic Thermal Power Management framework * provides to the userspace a consistent API to set the power limit * on some devices. * * DTPM defines the functions to create a tree of constraints. Each * parent node is a virtual description of the aggregation of the * children. It propagates the constraints set at its level to its * children and collect the children power information. The leaves of * the tree are the real devices which have the ability to get their * current power consumption and set their power limit. */ #define pr_fmt(fmt) … #include <linux/dtpm.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/powercap.h> #include <linux/slab.h> #include <linux/mutex.h> #include <linux/of.h> #include "dtpm_subsys.h" #define DTPM_POWER_LIMIT_FLAG … static const char *constraint_name[] = …; static DEFINE_MUTEX(dtpm_lock); static struct powercap_control_type *pct; static struct dtpm *root; static int get_time_window_us(struct powercap_zone *pcz, int cid, u64 *window) { … } static int set_time_window_us(struct powercap_zone *pcz, int cid, u64 window) { … } static int get_max_power_range_uw(struct powercap_zone *pcz, u64 *max_power_uw) { … } static int __get_power_uw(struct dtpm *dtpm, u64 *power_uw) { … } static int get_power_uw(struct powercap_zone *pcz, u64 *power_uw) { … } static void __dtpm_rebalance_weight(struct dtpm *dtpm) { … } static void __dtpm_sub_power(struct dtpm *dtpm) { … } static void __dtpm_add_power(struct dtpm *dtpm) { … } /** * dtpm_update_power - Update the power on the dtpm * @dtpm: a pointer to a dtpm structure to update * * Function to update the power values of the dtpm node specified in * parameter. These new values will be propagated to the tree. * * Return: zero on success, -EINVAL if the values are inconsistent */ int dtpm_update_power(struct dtpm *dtpm) { … } /** * dtpm_release_zone - Cleanup when the node is released * @pcz: a pointer to a powercap_zone structure * * Do some housecleaning and update the weight on the tree. The * release will be denied if the node has children. This function must * be called by the specific release callback of the different * backends. * * Return: 0 on success, -EBUSY if there are children */ int dtpm_release_zone(struct powercap_zone *pcz) { … } static int get_power_limit_uw(struct powercap_zone *pcz, int cid, u64 *power_limit) { … } /* * Set the power limit on the nodes, the power limit is distributed * given the weight of the children. * * The dtpm node lock must be held when calling this function. */ static int __set_power_limit_uw(struct dtpm *dtpm, int cid, u64 power_limit) { … } static int set_power_limit_uw(struct powercap_zone *pcz, int cid, u64 power_limit) { … } static const char *get_constraint_name(struct powercap_zone *pcz, int cid) { … } static int get_max_power_uw(struct powercap_zone *pcz, int id, u64 *max_power) { … } static struct powercap_zone_constraint_ops constraint_ops = …; static struct powercap_zone_ops zone_ops = …; /** * dtpm_init - Allocate and initialize a dtpm struct * @dtpm: The dtpm struct pointer to be initialized * @ops: The dtpm device specific ops, NULL for a virtual node */ void dtpm_init(struct dtpm *dtpm, struct dtpm_ops *ops) { … } /** * dtpm_unregister - Unregister a dtpm node from the hierarchy tree * @dtpm: a pointer to a dtpm structure corresponding to the node to be removed * * Call the underlying powercap unregister function. That will call * the release callback of the powercap zone. */ void dtpm_unregister(struct dtpm *dtpm) { … } /** * dtpm_register - Register a dtpm node in the hierarchy tree * @name: a string specifying the name of the node * @dtpm: a pointer to a dtpm structure corresponding to the new node * @parent: a pointer to a dtpm structure corresponding to the parent node * * Create a dtpm node in the tree. If no parent is specified, the node * is the root node of the hierarchy. If the root node already exists, * then the registration will fail. The powercap controller must be * initialized before calling this function. * * The dtpm structure must be initialized with the power numbers * before calling this function. * * Return: zero on success, a negative value in case of error: * -EAGAIN: the function is called before the framework is initialized. * -EBUSY: the root node is already inserted * -EINVAL: * there is no root node yet and @parent is specified * * no all ops are defined * * parent have ops which are reserved for leaves * Other negative values are reported back from the powercap framework */ int dtpm_register(const char *name, struct dtpm *dtpm, struct dtpm *parent) { … } static struct dtpm *dtpm_setup_virtual(const struct dtpm_node *hierarchy, struct dtpm *parent) { … } static struct dtpm *dtpm_setup_dt(const struct dtpm_node *hierarchy, struct dtpm *parent) { … } dtpm_node_callback_t; static dtpm_node_callback_t dtpm_node_callback[] = …; static int dtpm_for_each_child(const struct dtpm_node *hierarchy, const struct dtpm_node *it, struct dtpm *parent) { … } /** * dtpm_create_hierarchy - Create the dtpm hierarchy * @dtpm_match_table: Pointer to the array of device ID structures * * The function is called by the platform specific code with the * description of the different node in the hierarchy. It creates the * tree in the sysfs filesystem under the powercap dtpm entry. * * The expected tree has the format: * * struct dtpm_node hierarchy[] = { * [0] { .name = "topmost", type = DTPM_NODE_VIRTUAL }, * [1] { .name = "package", .type = DTPM_NODE_VIRTUAL, .parent = &hierarchy[0] }, * [2] { .name = "/cpus/cpu0", .type = DTPM_NODE_DT, .parent = &hierarchy[1] }, * [3] { .name = "/cpus/cpu1", .type = DTPM_NODE_DT, .parent = &hierarchy[1] }, * [4] { .name = "/cpus/cpu2", .type = DTPM_NODE_DT, .parent = &hierarchy[1] }, * [5] { .name = "/cpus/cpu3", .type = DTPM_NODE_DT, .parent = &hierarchy[1] }, * [6] { } * }; * * The last element is always an empty one and marks the end of the * array. * * Return: zero on success, a negative value in case of error. Errors * are reported back from the underlying functions. */ int dtpm_create_hierarchy(struct of_device_id *dtpm_match_table) { … } EXPORT_SYMBOL_GPL(…); static void __dtpm_destroy_hierarchy(struct dtpm *dtpm) { … } void dtpm_destroy_hierarchy(void) { … } EXPORT_SYMBOL_GPL(…);