1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2013, Intel Corporation.
7 #include <linux/module.h>
8 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/powercap.h>
13 #define to_powercap_zone(n) container_of(n, struct powercap_zone, dev)
14 #define to_powercap_control_type(n) \
15 container_of(n, struct powercap_control_type, dev)
17 /* Power zone show function */
18 #define define_power_zone_show(_attr) \
19 static ssize_t _attr##_show(struct device *dev, \
20 struct device_attribute *dev_attr,\
24 ssize_t len = -EINVAL; \
25 struct powercap_zone *power_zone = to_powercap_zone(dev); \
27 if (power_zone->ops->get_##_attr) { \
28 if (!power_zone->ops->get_##_attr(power_zone, &value)) \
29 len = sprintf(buf, "%lld\n", value); \
35 /* The only meaningful input is 0 (reset), others are silently ignored */
36 #define define_power_zone_store(_attr) \
37 static ssize_t _attr##_store(struct device *dev,\
38 struct device_attribute *dev_attr, \
39 const char *buf, size_t count) \
42 struct powercap_zone *power_zone = to_powercap_zone(dev); \
45 err = kstrtoull(buf, 10, &value); \
50 if (power_zone->ops->reset_##_attr) { \
51 if (!power_zone->ops->reset_##_attr(power_zone)) \
58 /* Power zone constraint show function */
59 #define define_power_zone_constraint_show(_attr) \
60 static ssize_t show_constraint_##_attr(struct device *dev, \
61 struct device_attribute *dev_attr,\
65 ssize_t len = -ENODATA; \
66 struct powercap_zone *power_zone = to_powercap_zone(dev); \
68 struct powercap_zone_constraint *pconst;\
70 if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
72 if (id >= power_zone->const_id_cnt) \
74 pconst = &power_zone->constraints[id]; \
75 if (pconst && pconst->ops && pconst->ops->get_##_attr) { \
76 if (!pconst->ops->get_##_attr(power_zone, id, &value)) \
77 len = sprintf(buf, "%lld\n", value); \
83 /* Power zone constraint store function */
84 #define define_power_zone_constraint_store(_attr) \
85 static ssize_t store_constraint_##_attr(struct device *dev,\
86 struct device_attribute *dev_attr, \
87 const char *buf, size_t count) \
91 struct powercap_zone *power_zone = to_powercap_zone(dev); \
93 struct powercap_zone_constraint *pconst;\
95 if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \
97 if (id >= power_zone->const_id_cnt) \
99 pconst = &power_zone->constraints[id]; \
100 err = kstrtoull(buf, 10, &value); \
103 if (pconst && pconst->ops && pconst->ops->set_##_attr) { \
104 if (!pconst->ops->set_##_attr(power_zone, id, value)) \
111 /* Power zone information callbacks */
112 define_power_zone_show(power_uw);
113 define_power_zone_show(max_power_range_uw);
114 define_power_zone_show(energy_uj);
115 define_power_zone_store(energy_uj);
116 define_power_zone_show(max_energy_range_uj);
118 /* Power zone attributes */
119 static DEVICE_ATTR_RO(max_power_range_uw);
120 static DEVICE_ATTR_RO(power_uw);
121 static DEVICE_ATTR_RO(max_energy_range_uj);
122 static DEVICE_ATTR_RW(energy_uj);
124 /* Power zone constraint attributes callbacks */
125 define_power_zone_constraint_show(power_limit_uw);
126 define_power_zone_constraint_store(power_limit_uw);
127 define_power_zone_constraint_show(time_window_us);
128 define_power_zone_constraint_store(time_window_us);
129 define_power_zone_constraint_show(max_power_uw);
130 define_power_zone_constraint_show(min_power_uw);
131 define_power_zone_constraint_show(max_time_window_us);
132 define_power_zone_constraint_show(min_time_window_us);
134 /* For one time seeding of constraint device attributes */
135 struct powercap_constraint_attr {
136 struct device_attribute power_limit_attr;
137 struct device_attribute time_window_attr;
138 struct device_attribute max_power_attr;
139 struct device_attribute min_power_attr;
140 struct device_attribute max_time_window_attr;
141 struct device_attribute min_time_window_attr;
142 struct device_attribute name_attr;
145 static struct powercap_constraint_attr
146 constraint_attrs[MAX_CONSTRAINTS_PER_ZONE];
148 /* A list of powercap control_types */
149 static LIST_HEAD(powercap_cntrl_list);
150 /* Mutex to protect list of powercap control_types */
151 static DEFINE_MUTEX(powercap_cntrl_list_lock);
153 #define POWERCAP_CONSTRAINT_NAME_LEN 30 /* Some limit to avoid overflow */
154 static ssize_t show_constraint_name(struct device *dev,
155 struct device_attribute *dev_attr,
159 struct powercap_zone *power_zone = to_powercap_zone(dev);
161 ssize_t len = -ENODATA;
162 struct powercap_zone_constraint *pconst;
164 if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id))
166 if (id >= power_zone->const_id_cnt)
168 pconst = &power_zone->constraints[id];
170 if (pconst && pconst->ops && pconst->ops->get_name) {
171 name = pconst->ops->get_name(power_zone, id);
173 sprintf(buf, "%.*s\n", POWERCAP_CONSTRAINT_NAME_LEN - 1,
182 static int create_constraint_attribute(int id, const char *name,
184 struct device_attribute *dev_attr,
185 ssize_t (*show)(struct device *,
186 struct device_attribute *, char *),
187 ssize_t (*store)(struct device *,
188 struct device_attribute *,
189 const char *, size_t)
193 dev_attr->attr.name = kasprintf(GFP_KERNEL, "constraint_%d_%s",
195 if (!dev_attr->attr.name)
197 dev_attr->attr.mode = mode;
198 dev_attr->show = show;
199 dev_attr->store = store;
204 static void free_constraint_attributes(void)
208 for (i = 0; i < MAX_CONSTRAINTS_PER_ZONE; ++i) {
209 kfree(constraint_attrs[i].power_limit_attr.attr.name);
210 kfree(constraint_attrs[i].time_window_attr.attr.name);
211 kfree(constraint_attrs[i].name_attr.attr.name);
212 kfree(constraint_attrs[i].max_power_attr.attr.name);
213 kfree(constraint_attrs[i].min_power_attr.attr.name);
214 kfree(constraint_attrs[i].max_time_window_attr.attr.name);
215 kfree(constraint_attrs[i].min_time_window_attr.attr.name);
219 static int seed_constraint_attributes(void)
224 for (i = 0; i < MAX_CONSTRAINTS_PER_ZONE; ++i) {
225 ret = create_constraint_attribute(i, "power_limit_uw",
227 &constraint_attrs[i].power_limit_attr,
228 show_constraint_power_limit_uw,
229 store_constraint_power_limit_uw);
232 ret = create_constraint_attribute(i, "time_window_us",
234 &constraint_attrs[i].time_window_attr,
235 show_constraint_time_window_us,
236 store_constraint_time_window_us);
239 ret = create_constraint_attribute(i, "name", S_IRUGO,
240 &constraint_attrs[i].name_attr,
241 show_constraint_name,
245 ret = create_constraint_attribute(i, "max_power_uw", S_IRUGO,
246 &constraint_attrs[i].max_power_attr,
247 show_constraint_max_power_uw,
251 ret = create_constraint_attribute(i, "min_power_uw", S_IRUGO,
252 &constraint_attrs[i].min_power_attr,
253 show_constraint_min_power_uw,
257 ret = create_constraint_attribute(i, "max_time_window_us",
259 &constraint_attrs[i].max_time_window_attr,
260 show_constraint_max_time_window_us,
264 ret = create_constraint_attribute(i, "min_time_window_us",
266 &constraint_attrs[i].min_time_window_attr,
267 show_constraint_min_time_window_us,
277 free_constraint_attributes();
282 static int create_constraints(struct powercap_zone *power_zone,
284 const struct powercap_zone_constraint_ops *const_ops)
289 struct powercap_zone_constraint *pconst;
291 if (!power_zone || !const_ops || !const_ops->get_power_limit_uw ||
292 !const_ops->set_power_limit_uw ||
293 !const_ops->get_time_window_us ||
294 !const_ops->set_time_window_us)
297 count = power_zone->zone_attr_count;
298 for (i = 0; i < nr_constraints; ++i) {
299 pconst = &power_zone->constraints[i];
300 pconst->ops = const_ops;
301 pconst->id = power_zone->const_id_cnt;
302 power_zone->const_id_cnt++;
303 power_zone->zone_dev_attrs[count++] =
304 &constraint_attrs[i].power_limit_attr.attr;
305 power_zone->zone_dev_attrs[count++] =
306 &constraint_attrs[i].time_window_attr.attr;
307 if (pconst->ops->get_name)
308 power_zone->zone_dev_attrs[count++] =
309 &constraint_attrs[i].name_attr.attr;
310 if (pconst->ops->get_max_power_uw)
311 power_zone->zone_dev_attrs[count++] =
312 &constraint_attrs[i].max_power_attr.attr;
313 if (pconst->ops->get_min_power_uw)
314 power_zone->zone_dev_attrs[count++] =
315 &constraint_attrs[i].min_power_attr.attr;
316 if (pconst->ops->get_max_time_window_us)
317 power_zone->zone_dev_attrs[count++] =
318 &constraint_attrs[i].max_time_window_attr.attr;
319 if (pconst->ops->get_min_time_window_us)
320 power_zone->zone_dev_attrs[count++] =
321 &constraint_attrs[i].min_time_window_attr.attr;
323 power_zone->zone_attr_count = count;
328 static bool control_type_valid(void *control_type)
330 struct powercap_control_type *pos = NULL;
333 mutex_lock(&powercap_cntrl_list_lock);
335 list_for_each_entry(pos, &powercap_cntrl_list, node) {
336 if (pos == control_type) {
341 mutex_unlock(&powercap_cntrl_list_lock);
346 static ssize_t name_show(struct device *dev,
347 struct device_attribute *attr,
350 struct powercap_zone *power_zone = to_powercap_zone(dev);
352 return sprintf(buf, "%s\n", power_zone->name);
355 static DEVICE_ATTR_RO(name);
357 /* Create zone and attributes in sysfs */
358 static void create_power_zone_common_attributes(
359 struct powercap_zone *power_zone)
363 power_zone->zone_dev_attrs[count++] = &dev_attr_name.attr;
364 if (power_zone->ops->get_max_energy_range_uj)
365 power_zone->zone_dev_attrs[count++] =
366 &dev_attr_max_energy_range_uj.attr;
367 if (power_zone->ops->get_energy_uj) {
368 if (power_zone->ops->reset_energy_uj)
369 dev_attr_energy_uj.attr.mode = S_IWUSR | S_IRUSR;
371 dev_attr_energy_uj.attr.mode = S_IRUSR;
372 power_zone->zone_dev_attrs[count++] =
373 &dev_attr_energy_uj.attr;
375 if (power_zone->ops->get_power_uw)
376 power_zone->zone_dev_attrs[count++] =
377 &dev_attr_power_uw.attr;
378 if (power_zone->ops->get_max_power_range_uw)
379 power_zone->zone_dev_attrs[count++] =
380 &dev_attr_max_power_range_uw.attr;
381 power_zone->zone_dev_attrs[count] = NULL;
382 power_zone->zone_attr_count = count;
385 static void powercap_release(struct device *dev)
390 struct powercap_zone *power_zone = to_powercap_zone(dev);
392 /* Store flag as the release() may free memory */
393 allocated = power_zone->allocated;
394 /* Remove id from parent idr struct */
395 idr_remove(power_zone->parent_idr, power_zone->id);
396 /* Destroy idrs allocated for this zone */
397 idr_destroy(&power_zone->idr);
398 kfree(power_zone->name);
399 kfree(power_zone->zone_dev_attrs);
400 kfree(power_zone->constraints);
401 if (power_zone->ops->release)
402 power_zone->ops->release(power_zone);
406 struct powercap_control_type *control_type =
407 to_powercap_control_type(dev);
409 /* Store flag as the release() may free memory */
410 allocated = control_type->allocated;
411 idr_destroy(&control_type->idr);
412 mutex_destroy(&control_type->lock);
413 if (control_type->ops && control_type->ops->release)
414 control_type->ops->release(control_type);
420 static ssize_t enabled_show(struct device *dev,
421 struct device_attribute *attr,
426 /* Default is enabled */
428 struct powercap_zone *power_zone = to_powercap_zone(dev);
429 if (power_zone->ops->get_enable)
430 if (power_zone->ops->get_enable(power_zone, &mode))
433 struct powercap_control_type *control_type =
434 to_powercap_control_type(dev);
435 if (control_type->ops && control_type->ops->get_enable)
436 if (control_type->ops->get_enable(control_type, &mode))
440 return sprintf(buf, "%d\n", mode);
443 static ssize_t enabled_store(struct device *dev,
444 struct device_attribute *attr,
445 const char *buf, size_t len)
449 if (strtobool(buf, &mode))
452 struct powercap_zone *power_zone = to_powercap_zone(dev);
453 if (power_zone->ops->set_enable)
454 if (!power_zone->ops->set_enable(power_zone, mode))
457 struct powercap_control_type *control_type =
458 to_powercap_control_type(dev);
459 if (control_type->ops && control_type->ops->set_enable)
460 if (!control_type->ops->set_enable(control_type, mode))
467 static DEVICE_ATTR_RW(enabled);
469 static struct attribute *powercap_attrs[] = {
470 &dev_attr_enabled.attr,
473 ATTRIBUTE_GROUPS(powercap);
475 static struct class powercap_class = {
477 .dev_release = powercap_release,
478 .dev_groups = powercap_groups,
481 struct powercap_zone *powercap_register_zone(
482 struct powercap_zone *power_zone,
483 struct powercap_control_type *control_type,
485 struct powercap_zone *parent,
486 const struct powercap_zone_ops *ops,
488 const struct powercap_zone_constraint_ops *const_ops)
493 if (!name || !control_type || !ops ||
494 nr_constraints > MAX_CONSTRAINTS_PER_ZONE ||
495 (!ops->get_energy_uj && !ops->get_power_uw) ||
496 !control_type_valid(control_type))
497 return ERR_PTR(-EINVAL);
501 return ERR_PTR(-EINVAL);
502 memset(power_zone, 0, sizeof(*power_zone));
504 power_zone = kzalloc(sizeof(*power_zone), GFP_KERNEL);
506 return ERR_PTR(-ENOMEM);
507 power_zone->allocated = true;
509 power_zone->ops = ops;
510 power_zone->control_type_inst = control_type;
512 power_zone->dev.parent = &control_type->dev;
513 power_zone->parent_idr = &control_type->idr;
515 power_zone->dev.parent = &parent->dev;
516 power_zone->parent_idr = &parent->idr;
518 power_zone->dev.class = &powercap_class;
520 mutex_lock(&control_type->lock);
521 /* Using idr to get the unique id */
522 result = idr_alloc(power_zone->parent_idr, NULL, 0, 0, GFP_KERNEL);
526 power_zone->id = result;
527 idr_init(&power_zone->idr);
529 power_zone->name = kstrdup(name, GFP_KERNEL);
530 if (!power_zone->name)
532 dev_set_name(&power_zone->dev, "%s:%x",
533 dev_name(power_zone->dev.parent),
535 power_zone->constraints = kcalloc(nr_constraints,
536 sizeof(*power_zone->constraints),
538 if (!power_zone->constraints)
539 goto err_const_alloc;
541 nr_attrs = nr_constraints * POWERCAP_CONSTRAINTS_ATTRS +
542 POWERCAP_ZONE_MAX_ATTRS + 1;
543 power_zone->zone_dev_attrs = kcalloc(nr_attrs, sizeof(void *),
545 if (!power_zone->zone_dev_attrs)
547 create_power_zone_common_attributes(power_zone);
548 result = create_constraints(power_zone, nr_constraints, const_ops);
552 power_zone->zone_dev_attrs[power_zone->zone_attr_count] = NULL;
553 power_zone->dev_zone_attr_group.attrs = power_zone->zone_dev_attrs;
554 power_zone->dev_attr_groups[0] = &power_zone->dev_zone_attr_group;
555 power_zone->dev_attr_groups[1] = NULL;
556 power_zone->dev.groups = power_zone->dev_attr_groups;
557 result = device_register(&power_zone->dev);
561 control_type->nr_zones++;
562 mutex_unlock(&control_type->lock);
567 kfree(power_zone->zone_dev_attrs);
569 kfree(power_zone->constraints);
571 kfree(power_zone->name);
573 idr_remove(power_zone->parent_idr, power_zone->id);
575 if (power_zone->allocated)
577 mutex_unlock(&control_type->lock);
579 return ERR_PTR(result);
581 EXPORT_SYMBOL_GPL(powercap_register_zone);
583 int powercap_unregister_zone(struct powercap_control_type *control_type,
584 struct powercap_zone *power_zone)
586 if (!power_zone || !control_type)
589 mutex_lock(&control_type->lock);
590 control_type->nr_zones--;
591 mutex_unlock(&control_type->lock);
593 device_unregister(&power_zone->dev);
597 EXPORT_SYMBOL_GPL(powercap_unregister_zone);
599 struct powercap_control_type *powercap_register_control_type(
600 struct powercap_control_type *control_type,
602 const struct powercap_control_type_ops *ops)
607 return ERR_PTR(-EINVAL);
609 if (!ops || !ops->release)
610 return ERR_PTR(-EINVAL);
611 memset(control_type, 0, sizeof(*control_type));
613 control_type = kzalloc(sizeof(*control_type), GFP_KERNEL);
615 return ERR_PTR(-ENOMEM);
616 control_type->allocated = true;
618 mutex_init(&control_type->lock);
619 control_type->ops = ops;
620 INIT_LIST_HEAD(&control_type->node);
621 control_type->dev.class = &powercap_class;
622 dev_set_name(&control_type->dev, "%s", name);
623 result = device_register(&control_type->dev);
625 if (control_type->allocated)
627 return ERR_PTR(result);
629 idr_init(&control_type->idr);
631 mutex_lock(&powercap_cntrl_list_lock);
632 list_add_tail(&control_type->node, &powercap_cntrl_list);
633 mutex_unlock(&powercap_cntrl_list_lock);
637 EXPORT_SYMBOL_GPL(powercap_register_control_type);
639 int powercap_unregister_control_type(struct powercap_control_type *control_type)
641 struct powercap_control_type *pos = NULL;
643 if (control_type->nr_zones) {
644 dev_err(&control_type->dev, "Zones of this type still not freed\n");
647 mutex_lock(&powercap_cntrl_list_lock);
648 list_for_each_entry(pos, &powercap_cntrl_list, node) {
649 if (pos == control_type) {
650 list_del(&control_type->node);
651 mutex_unlock(&powercap_cntrl_list_lock);
652 device_unregister(&control_type->dev);
656 mutex_unlock(&powercap_cntrl_list_lock);
660 EXPORT_SYMBOL_GPL(powercap_unregister_control_type);
662 static int __init powercap_init(void)
666 result = seed_constraint_attributes();
670 return class_register(&powercap_class);
673 fs_initcall(powercap_init);
675 MODULE_DESCRIPTION("PowerCap sysfs Driver");
676 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
677 MODULE_LICENSE("GPL v2");