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>
15 #include <linux/slab.h>
17 #define to_scmi_powercap_zone(z) \
18 container_of(z, struct scmi_powercap_zone, zone)
20 static const struct scmi_powercap_proto_ops *powercap_ops;
22 struct scmi_powercap_zone {
27 struct scmi_protocol_handle *ph;
28 const struct scmi_powercap_info *info;
29 struct scmi_powercap_zone *spzones;
30 struct powercap_zone zone;
31 struct list_head node;
34 struct scmi_powercap_root {
35 unsigned int num_zones;
36 struct scmi_powercap_zone *spzones;
37 struct list_head *registered_zones;
38 struct list_head scmi_zones;
41 static struct powercap_control_type *scmi_top_pcntrl;
43 static int scmi_powercap_zone_release(struct powercap_zone *pz)
48 static int scmi_powercap_get_max_power_range_uw(struct powercap_zone *pz,
49 u64 *max_power_range_uw)
51 *max_power_range_uw = U32_MAX;
55 static int scmi_powercap_get_power_uw(struct powercap_zone *pz,
58 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
62 if (!spz->info->powercap_monitoring)
65 ret = powercap_ops->measurements_get(spz->ph, spz->info->id, &avg_power,
70 *power_uw = avg_power;
71 if (spz->info->powercap_scale_mw)
77 static int scmi_powercap_zone_enable_set(struct powercap_zone *pz, bool mode)
79 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
81 return powercap_ops->cap_enable_set(spz->ph, spz->info->id, mode);
84 static int scmi_powercap_zone_enable_get(struct powercap_zone *pz, bool *mode)
86 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
88 return powercap_ops->cap_enable_get(spz->ph, spz->info->id, mode);
91 static const struct powercap_zone_ops zone_ops = {
92 .get_max_power_range_uw = scmi_powercap_get_max_power_range_uw,
93 .get_power_uw = scmi_powercap_get_power_uw,
94 .release = scmi_powercap_zone_release,
95 .set_enable = scmi_powercap_zone_enable_set,
96 .get_enable = scmi_powercap_zone_enable_get,
99 static void scmi_powercap_normalize_cap(const struct scmi_powercap_zone *spz,
100 u64 power_limit_uw, u32 *norm)
102 bool scale_mw = spz->info->powercap_scale_mw;
105 val = scale_mw ? DIV_ROUND_UP_ULL(power_limit_uw, 1000) : power_limit_uw;
107 * This cast is lossless since here @req_power is certain to be within
108 * the range [min_power_cap, max_power_cap] whose bounds are assured to
109 * be two unsigned 32bits quantities.
111 *norm = clamp_t(u32, val, spz->info->min_power_cap,
112 spz->info->max_power_cap);
113 *norm = rounddown(*norm, spz->info->power_cap_step);
115 val = (scale_mw) ? *norm * 1000 : *norm;
116 if (power_limit_uw != val)
118 "Normalized %s:CAP - requested:%llu - normalized:%llu\n",
119 spz->info->name, power_limit_uw, val);
122 static int scmi_powercap_set_power_limit_uw(struct powercap_zone *pz, int cid,
125 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
128 if (!spz->info->powercap_cap_config)
131 scmi_powercap_normalize_cap(spz, power_uw, &norm_power);
133 return powercap_ops->cap_set(spz->ph, spz->info->id, norm_power, false);
136 static int scmi_powercap_get_power_limit_uw(struct powercap_zone *pz, int cid,
139 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
143 ret = powercap_ops->cap_get(spz->ph, spz->info->id, &power);
147 *power_limit_uw = power;
148 if (spz->info->powercap_scale_mw)
149 *power_limit_uw *= 1000;
154 static void scmi_powercap_normalize_time(const struct scmi_powercap_zone *spz,
155 u64 time_us, u32 *norm)
158 * This cast is lossless since here @time_us is certain to be within the
159 * range [min_pai, max_pai] whose bounds are assured to be two unsigned
162 *norm = clamp_t(u32, time_us, spz->info->min_pai, spz->info->max_pai);
163 *norm = rounddown(*norm, spz->info->pai_step);
165 if (time_us != *norm)
167 "Normalized %s:PAI - requested:%llu - normalized:%u\n",
168 spz->info->name, time_us, *norm);
171 static int scmi_powercap_set_time_window_us(struct powercap_zone *pz, int cid,
174 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
177 if (!spz->info->powercap_pai_config)
180 scmi_powercap_normalize_time(spz, time_window_us, &norm_pai);
182 return powercap_ops->pai_set(spz->ph, spz->info->id, norm_pai);
185 static int scmi_powercap_get_time_window_us(struct powercap_zone *pz, int cid,
188 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
192 ret = powercap_ops->pai_get(spz->ph, spz->info->id, &pai);
196 *time_window_us = pai;
201 static int scmi_powercap_get_max_power_uw(struct powercap_zone *pz, int cid,
204 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
206 *max_power_uw = spz->info->max_power_cap;
207 if (spz->info->powercap_scale_mw)
208 *max_power_uw *= 1000;
213 static int scmi_powercap_get_min_power_uw(struct powercap_zone *pz, int cid,
216 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
218 *min_power_uw = spz->info->min_power_cap;
219 if (spz->info->powercap_scale_mw)
220 *min_power_uw *= 1000;
225 static int scmi_powercap_get_max_time_window_us(struct powercap_zone *pz,
226 int cid, u64 *time_window_us)
228 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
230 *time_window_us = spz->info->max_pai;
235 static int scmi_powercap_get_min_time_window_us(struct powercap_zone *pz,
236 int cid, u64 *time_window_us)
238 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
240 *time_window_us = (u64)spz->info->min_pai;
245 static const char *scmi_powercap_get_name(struct powercap_zone *pz, int cid)
247 return "SCMI power-cap";
250 static const struct powercap_zone_constraint_ops constraint_ops = {
251 .set_power_limit_uw = scmi_powercap_set_power_limit_uw,
252 .get_power_limit_uw = scmi_powercap_get_power_limit_uw,
253 .set_time_window_us = scmi_powercap_set_time_window_us,
254 .get_time_window_us = scmi_powercap_get_time_window_us,
255 .get_max_power_uw = scmi_powercap_get_max_power_uw,
256 .get_min_power_uw = scmi_powercap_get_min_power_uw,
257 .get_max_time_window_us = scmi_powercap_get_max_time_window_us,
258 .get_min_time_window_us = scmi_powercap_get_min_time_window_us,
259 .get_name = scmi_powercap_get_name,
262 static void scmi_powercap_unregister_all_zones(struct scmi_powercap_root *pr)
266 /* Un-register children zones first starting from the leaves */
267 for (i = pr->num_zones - 1; i >= 0; i--) {
268 if (!list_empty(&pr->registered_zones[i])) {
269 struct scmi_powercap_zone *spz;
271 list_for_each_entry(spz, &pr->registered_zones[i], node)
272 powercap_unregister_zone(scmi_top_pcntrl,
278 static inline unsigned int
279 scmi_powercap_get_zone_height(struct scmi_powercap_zone *spz)
281 if (spz->info->parent_id == SCMI_POWERCAP_ROOT_ZONE_ID)
284 return spz->spzones[spz->info->parent_id].height + 1;
287 static inline struct scmi_powercap_zone *
288 scmi_powercap_get_parent_zone(struct scmi_powercap_zone *spz)
290 if (spz->info->parent_id == SCMI_POWERCAP_ROOT_ZONE_ID)
293 return &spz->spzones[spz->info->parent_id];
296 static int scmi_powercap_register_zone(struct scmi_powercap_root *pr,
297 struct scmi_powercap_zone *spz,
298 struct scmi_powercap_zone *parent)
301 struct powercap_zone *z;
304 list_del(&spz->node);
308 z = powercap_register_zone(&spz->zone, scmi_top_pcntrl, spz->info->name,
309 parent ? &parent->zone : NULL,
310 &zone_ops, 1, &constraint_ops);
312 spz->height = scmi_powercap_get_zone_height(spz);
313 spz->registered = true;
314 list_move(&spz->node, &pr->registered_zones[spz->height]);
315 dev_dbg(spz->dev, "Registered node %s - parent %s - height:%d\n",
316 spz->info->name, parent ? parent->info->name : "ROOT",
319 list_del(&spz->node);
322 "Error registering node:%s - parent:%s - h:%d - ret:%d\n",
324 parent ? parent->info->name : "ROOT",
332 * scmi_zones_register- Register SCMI powercap zones starting from parent zones
334 * @dev: A reference to the SCMI device
335 * @pr: A reference to the root powercap zones descriptors
337 * When registering SCMI powercap zones with the powercap framework we should
338 * take care to always register zones starting from the root ones and to
339 * deregister starting from the leaves.
341 * Unfortunately we cannot assume that the array of available SCMI powercap
342 * zones provided by the SCMI platform firmware is built to comply with such
345 * This function, given the set of SCMI powercap zones to register, takes care
346 * to walk the SCMI powercap zones trees up to the root registering any
347 * unregistered parent zone before registering the child zones; at the same
348 * time each registered-zone height in such a tree is accounted for and each
349 * zone, once registered, is stored in the @registered_zones array that is
350 * indexed by zone height: this way will be trivial, at unregister time, to walk
351 * the @registered_zones array backward and unregister all the zones starting
352 * from the leaves, removing children zones before parents.
354 * While doing this, we prune away any zone marked as invalid (like the ones
355 * sporting an SCMI abstract power scale) as long as they are positioned as
356 * leaves in the SCMI powercap zones hierarchy: any non-leaf invalid zone causes
357 * the entire process to fail since we cannot assume the correctness of an SCMI
358 * powercap zones hierarchy if some of the internal nodes are missing.
360 * Note that the array of SCMI powercap zones as returned by the SCMI platform
361 * is known to be sane, i.e. zones relationships have been validated at the
364 * Return: 0 on Success
366 static int scmi_zones_register(struct device *dev,
367 struct scmi_powercap_root *pr)
370 unsigned int sp = 0, reg_zones = 0;
371 struct scmi_powercap_zone *spz, **zones_stack;
373 zones_stack = kcalloc(pr->num_zones, sizeof(spz), GFP_KERNEL);
377 spz = list_first_entry_or_null(&pr->scmi_zones,
378 struct scmi_powercap_zone, node);
380 struct scmi_powercap_zone *parent;
382 parent = scmi_powercap_get_parent_zone(spz);
383 if (parent && !parent->registered) {
384 zones_stack[sp++] = spz;
387 ret = scmi_powercap_register_zone(pr, spz, parent);
391 /* Failed to register a non-leaf zone.
395 "Failed to register non-leaf zone - ret:%d\n",
397 scmi_powercap_unregister_all_zones(pr);
401 /* Pick next zone to process */
403 spz = zones_stack[--sp];
405 spz = list_first_entry_or_null(&pr->scmi_zones,
406 struct scmi_powercap_zone,
413 dev_info(dev, "Registered %d SCMI Powercap domains !\n", reg_zones);
418 static int scmi_powercap_probe(struct scmi_device *sdev)
421 struct scmi_powercap_root *pr;
422 struct scmi_powercap_zone *spz;
423 struct scmi_protocol_handle *ph;
424 struct device *dev = &sdev->dev;
429 powercap_ops = sdev->handle->devm_protocol_get(sdev,
430 SCMI_PROTOCOL_POWERCAP,
432 if (IS_ERR(powercap_ops))
433 return PTR_ERR(powercap_ops);
435 pr = devm_kzalloc(dev, sizeof(*pr), GFP_KERNEL);
439 ret = powercap_ops->num_domains_get(ph);
441 dev_err(dev, "number of powercap domains not found\n");
446 pr->spzones = devm_kcalloc(dev, pr->num_zones,
447 sizeof(*pr->spzones), GFP_KERNEL);
451 /* Allocate for worst possible scenario of maximum tree height. */
452 pr->registered_zones = devm_kcalloc(dev, pr->num_zones,
453 sizeof(*pr->registered_zones),
455 if (!pr->registered_zones)
458 INIT_LIST_HEAD(&pr->scmi_zones);
460 for (i = 0, spz = pr->spzones; i < pr->num_zones; i++, spz++) {
462 * Powercap domains are validate by the protocol layer, i.e.
463 * when only non-NULL domains are returned here, whose
464 * parent_id is assured to point to another valid domain.
466 spz->info = powercap_ops->info_get(ph, i);
470 spz->spzones = pr->spzones;
471 INIT_LIST_HEAD(&spz->node);
472 INIT_LIST_HEAD(&pr->registered_zones[i]);
474 list_add_tail(&spz->node, &pr->scmi_zones);
476 * Forcibly skip powercap domains using an abstract scale.
477 * Note that only leaves domains can be skipped, so this could
478 * lead later to a global failure.
480 if (!spz->info->powercap_scale_uw &&
481 !spz->info->powercap_scale_mw) {
483 "Abstract power scale not supported. Skip %s.\n",
491 * Scan array of retrieved SCMI powercap domains and register them
492 * recursively starting from the root domains.
494 ret = scmi_zones_register(dev, pr);
498 dev_set_drvdata(dev, pr);
503 static void scmi_powercap_remove(struct scmi_device *sdev)
505 struct device *dev = &sdev->dev;
506 struct scmi_powercap_root *pr = dev_get_drvdata(dev);
508 scmi_powercap_unregister_all_zones(pr);
511 static const struct scmi_device_id scmi_id_table[] = {
512 { SCMI_PROTOCOL_POWERCAP, "powercap" },
515 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
517 static struct scmi_driver scmi_powercap_driver = {
518 .name = "scmi-powercap",
519 .probe = scmi_powercap_probe,
520 .remove = scmi_powercap_remove,
521 .id_table = scmi_id_table,
524 static int __init scmi_powercap_init(void)
528 scmi_top_pcntrl = powercap_register_control_type(NULL, "arm-scmi", NULL);
529 if (IS_ERR(scmi_top_pcntrl))
530 return PTR_ERR(scmi_top_pcntrl);
532 ret = scmi_register(&scmi_powercap_driver);
534 powercap_unregister_control_type(scmi_top_pcntrl);
538 module_init(scmi_powercap_init);
540 static void __exit scmi_powercap_exit(void)
542 scmi_unregister(&scmi_powercap_driver);
544 powercap_unregister_control_type(scmi_top_pcntrl);
546 module_exit(scmi_powercap_exit);
548 MODULE_AUTHOR("Cristian Marussi <cristian.marussi@arm.com>");
549 MODULE_DESCRIPTION("ARM SCMI Powercap driver");
550 MODULE_LICENSE("GPL");