1 // SPDX-License-Identifier: GPL-2.0
3 * SCMI Powercap support.
5 * Copyright (C) 2022 ARM Ltd.
8 #include <linux/device.h>
9 #include <linux/math.h>
10 #include <linux/limits.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/powercap.h>
14 #include <linux/scmi_protocol.h>
16 #define to_scmi_powercap_zone(z) \
17 container_of(z, struct scmi_powercap_zone, zone)
19 static const struct scmi_powercap_proto_ops *powercap_ops;
21 struct scmi_powercap_zone {
24 struct scmi_protocol_handle *ph;
25 const struct scmi_powercap_info *info;
26 struct scmi_powercap_zone *spzones;
27 struct powercap_zone zone;
28 struct list_head node;
31 struct scmi_powercap_root {
32 unsigned int num_zones;
33 struct scmi_powercap_zone *spzones;
34 struct list_head *registered_zones;
37 static struct powercap_control_type *scmi_top_pcntrl;
39 static int scmi_powercap_zone_release(struct powercap_zone *pz)
44 static int scmi_powercap_get_max_power_range_uw(struct powercap_zone *pz,
45 u64 *max_power_range_uw)
47 *max_power_range_uw = U32_MAX;
51 static int scmi_powercap_get_power_uw(struct powercap_zone *pz,
54 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
58 if (!spz->info->powercap_monitoring)
61 ret = powercap_ops->measurements_get(spz->ph, spz->info->id, &avg_power,
66 *power_uw = avg_power;
67 if (spz->info->powercap_scale_mw)
73 static const struct powercap_zone_ops zone_ops = {
74 .get_max_power_range_uw = scmi_powercap_get_max_power_range_uw,
75 .get_power_uw = scmi_powercap_get_power_uw,
76 .release = scmi_powercap_zone_release,
79 static void scmi_powercap_normalize_cap(const struct scmi_powercap_zone *spz,
80 u64 power_limit_uw, u32 *norm)
82 bool scale_mw = spz->info->powercap_scale_mw;
85 val = scale_mw ? DIV_ROUND_UP_ULL(power_limit_uw, 1000) : power_limit_uw;
87 * This cast is lossless since here @req_power is certain to be within
88 * the range [min_power_cap, max_power_cap] whose bounds are assured to
89 * be two unsigned 32bits quantities.
91 *norm = clamp_t(u32, val, spz->info->min_power_cap,
92 spz->info->max_power_cap);
93 *norm = rounddown(*norm, spz->info->power_cap_step);
95 val = (scale_mw) ? *norm * 1000 : *norm;
96 if (power_limit_uw != val)
98 "Normalized %s:CAP - requested:%llu - normalized:%llu\n",
99 spz->info->name, power_limit_uw, val);
102 static int scmi_powercap_set_power_limit_uw(struct powercap_zone *pz, int cid,
105 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
108 if (!spz->info->powercap_cap_config)
111 scmi_powercap_normalize_cap(spz, power_uw, &norm_power);
113 return powercap_ops->cap_set(spz->ph, spz->info->id, norm_power, false);
116 static int scmi_powercap_get_power_limit_uw(struct powercap_zone *pz, int cid,
119 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
123 ret = powercap_ops->cap_get(spz->ph, spz->info->id, &power);
127 *power_limit_uw = power;
128 if (spz->info->powercap_scale_mw)
129 *power_limit_uw *= 1000;
134 static void scmi_powercap_normalize_time(const struct scmi_powercap_zone *spz,
135 u64 time_us, u32 *norm)
138 * This cast is lossless since here @time_us is certain to be within the
139 * range [min_pai, max_pai] whose bounds are assured to be two unsigned
142 *norm = clamp_t(u32, time_us, spz->info->min_pai, spz->info->max_pai);
143 *norm = rounddown(*norm, spz->info->pai_step);
145 if (time_us != *norm)
147 "Normalized %s:PAI - requested:%llu - normalized:%u\n",
148 spz->info->name, time_us, *norm);
151 static int scmi_powercap_set_time_window_us(struct powercap_zone *pz, int cid,
154 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
157 if (!spz->info->powercap_pai_config)
160 scmi_powercap_normalize_time(spz, time_window_us, &norm_pai);
162 return powercap_ops->pai_set(spz->ph, spz->info->id, norm_pai);
165 static int scmi_powercap_get_time_window_us(struct powercap_zone *pz, int cid,
168 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
172 ret = powercap_ops->pai_get(spz->ph, spz->info->id, &pai);
176 *time_window_us = pai;
181 static int scmi_powercap_get_max_power_uw(struct powercap_zone *pz, int cid,
184 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
186 *max_power_uw = spz->info->max_power_cap;
187 if (spz->info->powercap_scale_mw)
188 *max_power_uw *= 1000;
193 static int scmi_powercap_get_min_power_uw(struct powercap_zone *pz, int cid,
196 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
198 *min_power_uw = spz->info->min_power_cap;
199 if (spz->info->powercap_scale_mw)
200 *min_power_uw *= 1000;
205 static int scmi_powercap_get_max_time_window_us(struct powercap_zone *pz,
206 int cid, u64 *time_window_us)
208 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
210 *time_window_us = spz->info->max_pai;
215 static int scmi_powercap_get_min_time_window_us(struct powercap_zone *pz,
216 int cid, u64 *time_window_us)
218 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
220 *time_window_us = (u64)spz->info->min_pai;
225 static const char *scmi_powercap_get_name(struct powercap_zone *pz, int cid)
227 return "SCMI power-cap";
230 static const struct powercap_zone_constraint_ops constraint_ops = {
231 .set_power_limit_uw = scmi_powercap_set_power_limit_uw,
232 .get_power_limit_uw = scmi_powercap_get_power_limit_uw,
233 .set_time_window_us = scmi_powercap_set_time_window_us,
234 .get_time_window_us = scmi_powercap_get_time_window_us,
235 .get_max_power_uw = scmi_powercap_get_max_power_uw,
236 .get_min_power_uw = scmi_powercap_get_min_power_uw,
237 .get_max_time_window_us = scmi_powercap_get_max_time_window_us,
238 .get_min_time_window_us = scmi_powercap_get_min_time_window_us,
239 .get_name = scmi_powercap_get_name,
242 static void scmi_powercap_unregister_all_zones(struct scmi_powercap_root *pr)
246 /* Un-register children zones first starting from the leaves */
247 for (i = pr->num_zones - 1; i >= 0; i--) {
248 if (!list_empty(&pr->registered_zones[i])) {
249 struct scmi_powercap_zone *spz;
251 list_for_each_entry(spz, &pr->registered_zones[i], node)
252 powercap_unregister_zone(scmi_top_pcntrl,
259 scmi_powercap_is_zone_registered(struct scmi_powercap_zone *spz)
261 return !list_empty(&spz->node);
264 static inline unsigned int
265 scmi_powercap_get_zone_height(struct scmi_powercap_zone *spz)
267 if (spz->info->parent_id == SCMI_POWERCAP_ROOT_ZONE_ID)
270 return spz->spzones[spz->info->parent_id].height + 1;
273 static inline struct scmi_powercap_zone *
274 scmi_powercap_get_parent_zone(struct scmi_powercap_zone *spz)
276 if (spz->info->parent_id == SCMI_POWERCAP_ROOT_ZONE_ID)
279 return &spz->spzones[spz->info->parent_id];
283 * scmi_powercap_register_zone - Register an SCMI powercap zone recursively
285 * @pr: A reference to the root powercap zones descriptors
286 * @spz: A reference to the SCMI powercap zone to register
288 * When registering SCMI powercap zones with the powercap framework we should
289 * take care to always register zones starting from the root ones and to
290 * deregister starting from the leaves.
292 * Unfortunately we cannot assume that the array of available SCMI powercap
293 * zones provided by the SCMI platform firmware is built to comply with such
296 * This function, given an SCMI powercap zone to register, takes care to walk
297 * the SCMI powercap zones tree up to the root looking recursively for
298 * unregistered parent zones before registering the provided zone; at the same
299 * time each registered zone height in such a tree is accounted for and each
300 * zone, once registered, is stored in the @registered_zones array that is
301 * indexed by zone height: this way will be trivial, at unregister time, to walk
302 * the @registered_zones array backward and unregister all the zones starting
303 * from the leaves, removing children zones before parents.
305 * While doing this, we prune away any zone marked as invalid (like the ones
306 * sporting an SCMI abstract power scale) as long as they are positioned as
307 * leaves in the SCMI powercap zones hierarchy: any non-leaf invalid zone causes
308 * the entire process to fail since we cannot assume the correctness of an SCMI
309 * powercap zones hierarchy if some of the internal nodes are missing.
311 * Note that the array of SCMI powercap zones as returned by the SCMI platform
312 * is known to be sane, i.e. zones relationships have been validated at the
315 * Return: 0 on Success
317 static int scmi_powercap_register_zone(struct scmi_powercap_root *pr,
318 struct scmi_powercap_zone *spz)
321 struct scmi_powercap_zone *parent;
326 parent = scmi_powercap_get_parent_zone(spz);
327 if (parent && !scmi_powercap_is_zone_registered(parent)) {
329 * Bail out if a parent domain was marked as unsupported:
330 * only domains participating as leaves can be skipped.
335 ret = scmi_powercap_register_zone(pr, parent);
340 if (!scmi_powercap_is_zone_registered(spz)) {
341 struct powercap_zone *z;
343 z = powercap_register_zone(&spz->zone,
346 parent ? &parent->zone : NULL,
347 &zone_ops, 1, &constraint_ops);
349 spz->height = scmi_powercap_get_zone_height(spz);
351 &pr->registered_zones[spz->height]);
353 "Registered node %s - parent %s - height:%d\n",
355 parent ? parent->info->name : "ROOT",
361 "Error registering node:%s - parent:%s - h:%d - ret:%d\n",
363 parent ? parent->info->name : "ROOT",
371 static int scmi_powercap_probe(struct scmi_device *sdev)
374 struct scmi_powercap_root *pr;
375 struct scmi_powercap_zone *spz;
376 struct scmi_protocol_handle *ph;
377 struct device *dev = &sdev->dev;
382 powercap_ops = sdev->handle->devm_protocol_get(sdev,
383 SCMI_PROTOCOL_POWERCAP,
385 if (IS_ERR(powercap_ops))
386 return PTR_ERR(powercap_ops);
388 pr = devm_kzalloc(dev, sizeof(*pr), GFP_KERNEL);
392 ret = powercap_ops->num_domains_get(ph);
394 dev_err(dev, "number of powercap domains not found\n");
399 pr->spzones = devm_kcalloc(dev, pr->num_zones,
400 sizeof(*pr->spzones), GFP_KERNEL);
404 /* Allocate for worst possible scenario of maximum tree height. */
405 pr->registered_zones = devm_kcalloc(dev, pr->num_zones,
406 sizeof(*pr->registered_zones),
408 if (!pr->registered_zones)
411 for (i = 0, spz = pr->spzones; i < pr->num_zones; i++, spz++) {
413 * Powercap domains are validate by the protocol layer, i.e.
414 * when only non-NULL domains are returned here, whose
415 * parent_id is assured to point to another valid domain.
417 spz->info = powercap_ops->info_get(ph, i);
421 spz->spzones = pr->spzones;
422 INIT_LIST_HEAD(&spz->node);
423 INIT_LIST_HEAD(&pr->registered_zones[i]);
426 * Forcibly skip powercap domains using an abstract scale.
427 * Note that only leaves domains can be skipped, so this could
428 * lead later to a global failure.
430 if (!spz->info->powercap_scale_uw &&
431 !spz->info->powercap_scale_mw) {
433 "Abstract power scale not supported. Skip %s.\n",
441 * Scan array of retrieved SCMI powercap domains and register them
442 * recursively starting from the root domains.
444 for (i = 0, spz = pr->spzones; i < pr->num_zones; i++, spz++) {
445 ret = scmi_powercap_register_zone(pr, spz);
448 "Failed to register powercap zone %s - ret:%d\n",
449 spz->info->name, ret);
450 scmi_powercap_unregister_all_zones(pr);
455 dev_set_drvdata(dev, pr);
457 dev_info(dev, "Registered %d SCMI Powercap domains !\n", pr->num_zones);
462 static void scmi_powercap_remove(struct scmi_device *sdev)
464 struct device *dev = &sdev->dev;
465 struct scmi_powercap_root *pr = dev_get_drvdata(dev);
467 scmi_powercap_unregister_all_zones(pr);
470 static const struct scmi_device_id scmi_id_table[] = {
471 { SCMI_PROTOCOL_POWERCAP, "powercap" },
474 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
476 static struct scmi_driver scmi_powercap_driver = {
477 .name = "scmi-powercap",
478 .probe = scmi_powercap_probe,
479 .remove = scmi_powercap_remove,
480 .id_table = scmi_id_table,
483 static int __init scmi_powercap_init(void)
487 scmi_top_pcntrl = powercap_register_control_type(NULL, "arm-scmi", NULL);
488 if (IS_ERR(scmi_top_pcntrl))
489 return PTR_ERR(scmi_top_pcntrl);
491 ret = scmi_register(&scmi_powercap_driver);
493 powercap_unregister_control_type(scmi_top_pcntrl);
497 module_init(scmi_powercap_init);
499 static void __exit scmi_powercap_exit(void)
501 scmi_unregister(&scmi_powercap_driver);
503 powercap_unregister_control_type(scmi_top_pcntrl);
505 module_exit(scmi_powercap_exit);
508 MODULE_DESCRIPTION("ARM SCMI Powercap driver");
509 MODULE_LICENSE("GPL");