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 int scmi_powercap_zone_enable_set(struct powercap_zone *pz, bool mode)
75 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
77 return powercap_ops->cap_enable_set(spz->ph, spz->info->id, mode);
80 static int scmi_powercap_zone_enable_get(struct powercap_zone *pz, bool *mode)
82 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
84 return powercap_ops->cap_enable_get(spz->ph, spz->info->id, mode);
87 static const struct powercap_zone_ops zone_ops = {
88 .get_max_power_range_uw = scmi_powercap_get_max_power_range_uw,
89 .get_power_uw = scmi_powercap_get_power_uw,
90 .release = scmi_powercap_zone_release,
91 .set_enable = scmi_powercap_zone_enable_set,
92 .get_enable = scmi_powercap_zone_enable_get,
95 static void scmi_powercap_normalize_cap(const struct scmi_powercap_zone *spz,
96 u64 power_limit_uw, u32 *norm)
98 bool scale_mw = spz->info->powercap_scale_mw;
101 val = scale_mw ? DIV_ROUND_UP_ULL(power_limit_uw, 1000) : power_limit_uw;
103 * This cast is lossless since here @req_power is certain to be within
104 * the range [min_power_cap, max_power_cap] whose bounds are assured to
105 * be two unsigned 32bits quantities.
107 *norm = clamp_t(u32, val, spz->info->min_power_cap,
108 spz->info->max_power_cap);
109 *norm = rounddown(*norm, spz->info->power_cap_step);
111 val = (scale_mw) ? *norm * 1000 : *norm;
112 if (power_limit_uw != val)
114 "Normalized %s:CAP - requested:%llu - normalized:%llu\n",
115 spz->info->name, power_limit_uw, val);
118 static int scmi_powercap_set_power_limit_uw(struct powercap_zone *pz, int cid,
121 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
124 if (!spz->info->powercap_cap_config)
127 scmi_powercap_normalize_cap(spz, power_uw, &norm_power);
129 return powercap_ops->cap_set(spz->ph, spz->info->id, norm_power, false);
132 static int scmi_powercap_get_power_limit_uw(struct powercap_zone *pz, int cid,
135 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
139 ret = powercap_ops->cap_get(spz->ph, spz->info->id, &power);
143 *power_limit_uw = power;
144 if (spz->info->powercap_scale_mw)
145 *power_limit_uw *= 1000;
150 static void scmi_powercap_normalize_time(const struct scmi_powercap_zone *spz,
151 u64 time_us, u32 *norm)
154 * This cast is lossless since here @time_us is certain to be within the
155 * range [min_pai, max_pai] whose bounds are assured to be two unsigned
158 *norm = clamp_t(u32, time_us, spz->info->min_pai, spz->info->max_pai);
159 *norm = rounddown(*norm, spz->info->pai_step);
161 if (time_us != *norm)
163 "Normalized %s:PAI - requested:%llu - normalized:%u\n",
164 spz->info->name, time_us, *norm);
167 static int scmi_powercap_set_time_window_us(struct powercap_zone *pz, int cid,
170 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
173 if (!spz->info->powercap_pai_config)
176 scmi_powercap_normalize_time(spz, time_window_us, &norm_pai);
178 return powercap_ops->pai_set(spz->ph, spz->info->id, norm_pai);
181 static int scmi_powercap_get_time_window_us(struct powercap_zone *pz, int cid,
184 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
188 ret = powercap_ops->pai_get(spz->ph, spz->info->id, &pai);
192 *time_window_us = pai;
197 static int scmi_powercap_get_max_power_uw(struct powercap_zone *pz, int cid,
200 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
202 *max_power_uw = spz->info->max_power_cap;
203 if (spz->info->powercap_scale_mw)
204 *max_power_uw *= 1000;
209 static int scmi_powercap_get_min_power_uw(struct powercap_zone *pz, int cid,
212 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
214 *min_power_uw = spz->info->min_power_cap;
215 if (spz->info->powercap_scale_mw)
216 *min_power_uw *= 1000;
221 static int scmi_powercap_get_max_time_window_us(struct powercap_zone *pz,
222 int cid, u64 *time_window_us)
224 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
226 *time_window_us = spz->info->max_pai;
231 static int scmi_powercap_get_min_time_window_us(struct powercap_zone *pz,
232 int cid, u64 *time_window_us)
234 struct scmi_powercap_zone *spz = to_scmi_powercap_zone(pz);
236 *time_window_us = (u64)spz->info->min_pai;
241 static const char *scmi_powercap_get_name(struct powercap_zone *pz, int cid)
243 return "SCMI power-cap";
246 static const struct powercap_zone_constraint_ops constraint_ops = {
247 .set_power_limit_uw = scmi_powercap_set_power_limit_uw,
248 .get_power_limit_uw = scmi_powercap_get_power_limit_uw,
249 .set_time_window_us = scmi_powercap_set_time_window_us,
250 .get_time_window_us = scmi_powercap_get_time_window_us,
251 .get_max_power_uw = scmi_powercap_get_max_power_uw,
252 .get_min_power_uw = scmi_powercap_get_min_power_uw,
253 .get_max_time_window_us = scmi_powercap_get_max_time_window_us,
254 .get_min_time_window_us = scmi_powercap_get_min_time_window_us,
255 .get_name = scmi_powercap_get_name,
258 static void scmi_powercap_unregister_all_zones(struct scmi_powercap_root *pr)
262 /* Un-register children zones first starting from the leaves */
263 for (i = pr->num_zones - 1; i >= 0; i--) {
264 if (!list_empty(&pr->registered_zones[i])) {
265 struct scmi_powercap_zone *spz;
267 list_for_each_entry(spz, &pr->registered_zones[i], node)
268 powercap_unregister_zone(scmi_top_pcntrl,
275 scmi_powercap_is_zone_registered(struct scmi_powercap_zone *spz)
277 return !list_empty(&spz->node);
280 static inline unsigned int
281 scmi_powercap_get_zone_height(struct scmi_powercap_zone *spz)
283 if (spz->info->parent_id == SCMI_POWERCAP_ROOT_ZONE_ID)
286 return spz->spzones[spz->info->parent_id].height + 1;
289 static inline struct scmi_powercap_zone *
290 scmi_powercap_get_parent_zone(struct scmi_powercap_zone *spz)
292 if (spz->info->parent_id == SCMI_POWERCAP_ROOT_ZONE_ID)
295 return &spz->spzones[spz->info->parent_id];
299 * scmi_powercap_register_zone - Register an SCMI powercap zone recursively
301 * @pr: A reference to the root powercap zones descriptors
302 * @spz: A reference to the SCMI powercap zone to register
304 * When registering SCMI powercap zones with the powercap framework we should
305 * take care to always register zones starting from the root ones and to
306 * deregister starting from the leaves.
308 * Unfortunately we cannot assume that the array of available SCMI powercap
309 * zones provided by the SCMI platform firmware is built to comply with such
312 * This function, given an SCMI powercap zone to register, takes care to walk
313 * the SCMI powercap zones tree up to the root looking recursively for
314 * unregistered parent zones before registering the provided zone; at the same
315 * time each registered zone height in such a tree is accounted for and each
316 * zone, once registered, is stored in the @registered_zones array that is
317 * indexed by zone height: this way will be trivial, at unregister time, to walk
318 * the @registered_zones array backward and unregister all the zones starting
319 * from the leaves, removing children zones before parents.
321 * While doing this, we prune away any zone marked as invalid (like the ones
322 * sporting an SCMI abstract power scale) as long as they are positioned as
323 * leaves in the SCMI powercap zones hierarchy: any non-leaf invalid zone causes
324 * the entire process to fail since we cannot assume the correctness of an SCMI
325 * powercap zones hierarchy if some of the internal nodes are missing.
327 * Note that the array of SCMI powercap zones as returned by the SCMI platform
328 * is known to be sane, i.e. zones relationships have been validated at the
331 * Return: 0 on Success
333 static int scmi_powercap_register_zone(struct scmi_powercap_root *pr,
334 struct scmi_powercap_zone *spz)
337 struct scmi_powercap_zone *parent;
342 parent = scmi_powercap_get_parent_zone(spz);
343 if (parent && !scmi_powercap_is_zone_registered(parent)) {
345 * Bail out if a parent domain was marked as unsupported:
346 * only domains participating as leaves can be skipped.
351 ret = scmi_powercap_register_zone(pr, parent);
356 if (!scmi_powercap_is_zone_registered(spz)) {
357 struct powercap_zone *z;
359 z = powercap_register_zone(&spz->zone,
362 parent ? &parent->zone : NULL,
363 &zone_ops, 1, &constraint_ops);
365 spz->height = scmi_powercap_get_zone_height(spz);
367 &pr->registered_zones[spz->height]);
369 "Registered node %s - parent %s - height:%d\n",
371 parent ? parent->info->name : "ROOT",
377 "Error registering node:%s - parent:%s - h:%d - ret:%d\n",
379 parent ? parent->info->name : "ROOT",
387 static int scmi_powercap_probe(struct scmi_device *sdev)
390 struct scmi_powercap_root *pr;
391 struct scmi_powercap_zone *spz;
392 struct scmi_protocol_handle *ph;
393 struct device *dev = &sdev->dev;
398 powercap_ops = sdev->handle->devm_protocol_get(sdev,
399 SCMI_PROTOCOL_POWERCAP,
401 if (IS_ERR(powercap_ops))
402 return PTR_ERR(powercap_ops);
404 pr = devm_kzalloc(dev, sizeof(*pr), GFP_KERNEL);
408 ret = powercap_ops->num_domains_get(ph);
410 dev_err(dev, "number of powercap domains not found\n");
415 pr->spzones = devm_kcalloc(dev, pr->num_zones,
416 sizeof(*pr->spzones), GFP_KERNEL);
420 /* Allocate for worst possible scenario of maximum tree height. */
421 pr->registered_zones = devm_kcalloc(dev, pr->num_zones,
422 sizeof(*pr->registered_zones),
424 if (!pr->registered_zones)
427 for (i = 0, spz = pr->spzones; i < pr->num_zones; i++, spz++) {
429 * Powercap domains are validate by the protocol layer, i.e.
430 * when only non-NULL domains are returned here, whose
431 * parent_id is assured to point to another valid domain.
433 spz->info = powercap_ops->info_get(ph, i);
437 spz->spzones = pr->spzones;
438 INIT_LIST_HEAD(&spz->node);
439 INIT_LIST_HEAD(&pr->registered_zones[i]);
442 * Forcibly skip powercap domains using an abstract scale.
443 * Note that only leaves domains can be skipped, so this could
444 * lead later to a global failure.
446 if (!spz->info->powercap_scale_uw &&
447 !spz->info->powercap_scale_mw) {
449 "Abstract power scale not supported. Skip %s.\n",
457 * Scan array of retrieved SCMI powercap domains and register them
458 * recursively starting from the root domains.
460 for (i = 0, spz = pr->spzones; i < pr->num_zones; i++, spz++) {
461 ret = scmi_powercap_register_zone(pr, spz);
464 "Failed to register powercap zone %s - ret:%d\n",
465 spz->info->name, ret);
466 scmi_powercap_unregister_all_zones(pr);
471 dev_set_drvdata(dev, pr);
473 dev_info(dev, "Registered %d SCMI Powercap domains !\n", pr->num_zones);
478 static void scmi_powercap_remove(struct scmi_device *sdev)
480 struct device *dev = &sdev->dev;
481 struct scmi_powercap_root *pr = dev_get_drvdata(dev);
483 scmi_powercap_unregister_all_zones(pr);
486 static const struct scmi_device_id scmi_id_table[] = {
487 { SCMI_PROTOCOL_POWERCAP, "powercap" },
490 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
492 static struct scmi_driver scmi_powercap_driver = {
493 .name = "scmi-powercap",
494 .probe = scmi_powercap_probe,
495 .remove = scmi_powercap_remove,
496 .id_table = scmi_id_table,
499 static int __init scmi_powercap_init(void)
503 scmi_top_pcntrl = powercap_register_control_type(NULL, "arm-scmi", NULL);
504 if (IS_ERR(scmi_top_pcntrl))
505 return PTR_ERR(scmi_top_pcntrl);
507 ret = scmi_register(&scmi_powercap_driver);
509 powercap_unregister_control_type(scmi_top_pcntrl);
513 module_init(scmi_powercap_init);
515 static void __exit scmi_powercap_exit(void)
517 scmi_unregister(&scmi_powercap_driver);
519 powercap_unregister_control_type(scmi_top_pcntrl);
521 module_exit(scmi_powercap_exit);
523 MODULE_AUTHOR("Cristian Marussi <cristian.marussi@arm.com>");
524 MODULE_DESCRIPTION("ARM SCMI Powercap driver");
525 MODULE_LICENSE("GPL");