Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PowerCap: Add class driver

The power capping framework providing a consistent interface between the
kernel and user space that allows power capping drivers to expose their
settings to user space in a uniform way.
The overall design of the framework is described in the documentation
added by the previous patch in this series.

Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Signed-off-by: Jacob Pan <jacob.jun.pan@linux.intel.com>
Reviewed-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

authored by

Srinivas Pandruvada and committed by
Rafael J. Wysocki
75d2364e e23feb16

+1028
+19
drivers/powercap/Kconfig
··· 1 + # 2 + # Generic power capping sysfs interface configuration 3 + # 4 + 5 + menuconfig POWERCAP 6 + bool "Generic powercap sysfs driver" 7 + help 8 + The power capping sysfs interface allows kernel subsystems to expose power 9 + capping settings to user space in a consistent way. Usually, it consists 10 + of multiple control types that determine which settings may be exposed and 11 + power zones representing parts of the system that can be subject to power 12 + capping. 13 + 14 + If you want this code to be compiled in, say Y here. 15 + 16 + if POWERCAP 17 + # Client driver configurations go here. 18 + 19 + endif
+1
drivers/powercap/Makefile
··· 1 + obj-$(CONFIG_POWERCAP) += powercap_sys.o
+683
drivers/powercap/powercap_sys.c
··· 1 + /* 2 + * Power capping class 3 + * Copyright (c) 2013, Intel Corporation. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms and conditions of the GNU General Public License, 7 + * version 2, as published by the Free Software Foundation. 8 + * 9 + * This program is distributed in the hope it will be useful, but WITHOUT 10 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 + * more details. 13 + * 14 + * You should have received a copy of the GNU General Public License along with 15 + * this program; if not, write to the Free Software Foundation, Inc. 16 + * 17 + */ 18 + 19 + #include <linux/module.h> 20 + #include <linux/device.h> 21 + #include <linux/err.h> 22 + #include <linux/slab.h> 23 + #include <linux/powercap.h> 24 + 25 + #define to_powercap_zone(n) container_of(n, struct powercap_zone, dev) 26 + #define to_powercap_control_type(n) \ 27 + container_of(n, struct powercap_control_type, dev) 28 + 29 + /* Power zone show function */ 30 + #define define_power_zone_show(_attr) \ 31 + static ssize_t _attr##_show(struct device *dev, \ 32 + struct device_attribute *dev_attr,\ 33 + char *buf) \ 34 + { \ 35 + u64 value; \ 36 + ssize_t len = -EINVAL; \ 37 + struct powercap_zone *power_zone = to_powercap_zone(dev); \ 38 + \ 39 + if (power_zone->ops->get_##_attr) { \ 40 + if (!power_zone->ops->get_##_attr(power_zone, &value)) \ 41 + len = sprintf(buf, "%lld\n", value); \ 42 + } \ 43 + \ 44 + return len; \ 45 + } 46 + 47 + /* The only meaningful input is 0 (reset), others are silently ignored */ 48 + #define define_power_zone_store(_attr) \ 49 + static ssize_t _attr##_store(struct device *dev,\ 50 + struct device_attribute *dev_attr, \ 51 + const char *buf, size_t count) \ 52 + { \ 53 + int err; \ 54 + struct powercap_zone *power_zone = to_powercap_zone(dev); \ 55 + u64 value; \ 56 + \ 57 + err = kstrtoull(buf, 10, &value); \ 58 + if (err) \ 59 + return -EINVAL; \ 60 + if (value) \ 61 + return count; \ 62 + if (power_zone->ops->reset_##_attr) { \ 63 + if (!power_zone->ops->reset_##_attr(power_zone)) \ 64 + return count; \ 65 + } \ 66 + \ 67 + return -EINVAL; \ 68 + } 69 + 70 + /* Power zone constraint show function */ 71 + #define define_power_zone_constraint_show(_attr) \ 72 + static ssize_t show_constraint_##_attr(struct device *dev, \ 73 + struct device_attribute *dev_attr,\ 74 + char *buf) \ 75 + { \ 76 + u64 value; \ 77 + ssize_t len = -ENODATA; \ 78 + struct powercap_zone *power_zone = to_powercap_zone(dev); \ 79 + int id; \ 80 + struct powercap_zone_constraint *pconst;\ 81 + \ 82 + if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \ 83 + return -EINVAL; \ 84 + if (id >= power_zone->const_id_cnt) \ 85 + return -EINVAL; \ 86 + pconst = &power_zone->constraints[id]; \ 87 + if (pconst && pconst->ops && pconst->ops->get_##_attr) { \ 88 + if (!pconst->ops->get_##_attr(power_zone, id, &value)) \ 89 + len = sprintf(buf, "%lld\n", value); \ 90 + } \ 91 + \ 92 + return len; \ 93 + } 94 + 95 + /* Power zone constraint store function */ 96 + #define define_power_zone_constraint_store(_attr) \ 97 + static ssize_t store_constraint_##_attr(struct device *dev,\ 98 + struct device_attribute *dev_attr, \ 99 + const char *buf, size_t count) \ 100 + { \ 101 + int err; \ 102 + u64 value; \ 103 + struct powercap_zone *power_zone = to_powercap_zone(dev); \ 104 + int id; \ 105 + struct powercap_zone_constraint *pconst;\ 106 + \ 107 + if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) \ 108 + return -EINVAL; \ 109 + if (id >= power_zone->const_id_cnt) \ 110 + return -EINVAL; \ 111 + pconst = &power_zone->constraints[id]; \ 112 + err = kstrtoull(buf, 10, &value); \ 113 + if (err) \ 114 + return -EINVAL; \ 115 + if (pconst && pconst->ops && pconst->ops->set_##_attr) { \ 116 + if (!pconst->ops->set_##_attr(power_zone, id, value)) \ 117 + return count; \ 118 + } \ 119 + \ 120 + return -ENODATA; \ 121 + } 122 + 123 + /* Power zone information callbacks */ 124 + define_power_zone_show(power_uw); 125 + define_power_zone_show(max_power_range_uw); 126 + define_power_zone_show(energy_uj); 127 + define_power_zone_store(energy_uj); 128 + define_power_zone_show(max_energy_range_uj); 129 + 130 + /* Power zone attributes */ 131 + static DEVICE_ATTR_RO(max_power_range_uw); 132 + static DEVICE_ATTR_RO(power_uw); 133 + static DEVICE_ATTR_RO(max_energy_range_uj); 134 + static DEVICE_ATTR_RW(energy_uj); 135 + 136 + /* Power zone constraint attributes callbacks */ 137 + define_power_zone_constraint_show(power_limit_uw); 138 + define_power_zone_constraint_store(power_limit_uw); 139 + define_power_zone_constraint_show(time_window_us); 140 + define_power_zone_constraint_store(time_window_us); 141 + define_power_zone_constraint_show(max_power_uw); 142 + define_power_zone_constraint_show(min_power_uw); 143 + define_power_zone_constraint_show(max_time_window_us); 144 + define_power_zone_constraint_show(min_time_window_us); 145 + 146 + /* For one time seeding of constraint device attributes */ 147 + struct powercap_constraint_attr { 148 + struct device_attribute power_limit_attr; 149 + struct device_attribute time_window_attr; 150 + struct device_attribute max_power_attr; 151 + struct device_attribute min_power_attr; 152 + struct device_attribute max_time_window_attr; 153 + struct device_attribute min_time_window_attr; 154 + struct device_attribute name_attr; 155 + }; 156 + 157 + static struct powercap_constraint_attr 158 + constraint_attrs[MAX_CONSTRAINTS_PER_ZONE]; 159 + 160 + /* A list of powercap control_types */ 161 + static LIST_HEAD(powercap_cntrl_list); 162 + /* Mutex to protect list of powercap control_types */ 163 + static DEFINE_MUTEX(powercap_cntrl_list_lock); 164 + 165 + #define POWERCAP_CONSTRAINT_NAME_LEN 30 /* Some limit to avoid overflow */ 166 + static ssize_t show_constraint_name(struct device *dev, 167 + struct device_attribute *dev_attr, 168 + char *buf) 169 + { 170 + const char *name; 171 + struct powercap_zone *power_zone = to_powercap_zone(dev); 172 + int id; 173 + ssize_t len = -ENODATA; 174 + struct powercap_zone_constraint *pconst; 175 + 176 + if (!sscanf(dev_attr->attr.name, "constraint_%d_", &id)) 177 + return -EINVAL; 178 + if (id >= power_zone->const_id_cnt) 179 + return -EINVAL; 180 + pconst = &power_zone->constraints[id]; 181 + 182 + if (pconst && pconst->ops && pconst->ops->get_name) { 183 + name = pconst->ops->get_name(power_zone, id); 184 + if (name) { 185 + snprintf(buf, POWERCAP_CONSTRAINT_NAME_LEN, 186 + "%s\n", name); 187 + buf[POWERCAP_CONSTRAINT_NAME_LEN] = '\0'; 188 + len = strlen(buf); 189 + } 190 + } 191 + 192 + return len; 193 + } 194 + 195 + static int create_constraint_attribute(int id, const char *name, 196 + int mode, 197 + struct device_attribute *dev_attr, 198 + ssize_t (*show)(struct device *, 199 + struct device_attribute *, char *), 200 + ssize_t (*store)(struct device *, 201 + struct device_attribute *, 202 + const char *, size_t) 203 + ) 204 + { 205 + 206 + dev_attr->attr.name = kasprintf(GFP_KERNEL, "constraint_%d_%s", 207 + id, name); 208 + if (!dev_attr->attr.name) 209 + return -ENOMEM; 210 + dev_attr->attr.mode = mode; 211 + dev_attr->show = show; 212 + dev_attr->store = store; 213 + 214 + return 0; 215 + } 216 + 217 + static void free_constraint_attributes(void) 218 + { 219 + int i; 220 + 221 + for (i = 0; i < MAX_CONSTRAINTS_PER_ZONE; ++i) { 222 + kfree(constraint_attrs[i].power_limit_attr.attr.name); 223 + kfree(constraint_attrs[i].time_window_attr.attr.name); 224 + kfree(constraint_attrs[i].name_attr.attr.name); 225 + kfree(constraint_attrs[i].max_power_attr.attr.name); 226 + kfree(constraint_attrs[i].min_power_attr.attr.name); 227 + kfree(constraint_attrs[i].max_time_window_attr.attr.name); 228 + kfree(constraint_attrs[i].min_time_window_attr.attr.name); 229 + } 230 + } 231 + 232 + static int seed_constraint_attributes(void) 233 + { 234 + int i; 235 + int ret; 236 + 237 + for (i = 0; i < MAX_CONSTRAINTS_PER_ZONE; ++i) { 238 + ret = create_constraint_attribute(i, "power_limit_uw", 239 + S_IWUSR | S_IRUGO, 240 + &constraint_attrs[i].power_limit_attr, 241 + show_constraint_power_limit_uw, 242 + store_constraint_power_limit_uw); 243 + if (ret) 244 + goto err_alloc; 245 + ret = create_constraint_attribute(i, "time_window_us", 246 + S_IWUSR | S_IRUGO, 247 + &constraint_attrs[i].time_window_attr, 248 + show_constraint_time_window_us, 249 + store_constraint_time_window_us); 250 + if (ret) 251 + goto err_alloc; 252 + ret = create_constraint_attribute(i, "name", S_IRUGO, 253 + &constraint_attrs[i].name_attr, 254 + show_constraint_name, 255 + NULL); 256 + if (ret) 257 + goto err_alloc; 258 + ret = create_constraint_attribute(i, "max_power_uw", S_IRUGO, 259 + &constraint_attrs[i].max_power_attr, 260 + show_constraint_max_power_uw, 261 + NULL); 262 + if (ret) 263 + goto err_alloc; 264 + ret = create_constraint_attribute(i, "min_power_uw", S_IRUGO, 265 + &constraint_attrs[i].min_power_attr, 266 + show_constraint_min_power_uw, 267 + NULL); 268 + if (ret) 269 + goto err_alloc; 270 + ret = create_constraint_attribute(i, "max_time_window_us", 271 + S_IRUGO, 272 + &constraint_attrs[i].max_time_window_attr, 273 + show_constraint_max_time_window_us, 274 + NULL); 275 + if (ret) 276 + goto err_alloc; 277 + ret = create_constraint_attribute(i, "min_time_window_us", 278 + S_IRUGO, 279 + &constraint_attrs[i].min_time_window_attr, 280 + show_constraint_min_time_window_us, 281 + NULL); 282 + if (ret) 283 + goto err_alloc; 284 + 285 + } 286 + 287 + return 0; 288 + 289 + err_alloc: 290 + free_constraint_attributes(); 291 + 292 + return ret; 293 + } 294 + 295 + static int create_constraints(struct powercap_zone *power_zone, 296 + int nr_constraints, 297 + struct powercap_zone_constraint_ops *const_ops) 298 + { 299 + int i; 300 + int ret = 0; 301 + int count; 302 + struct powercap_zone_constraint *pconst; 303 + 304 + if (!power_zone || !const_ops || !const_ops->get_power_limit_uw || 305 + !const_ops->set_power_limit_uw || 306 + !const_ops->get_time_window_us || 307 + !const_ops->set_time_window_us) 308 + return -EINVAL; 309 + 310 + count = power_zone->zone_attr_count; 311 + for (i = 0; i < nr_constraints; ++i) { 312 + pconst = &power_zone->constraints[i]; 313 + pconst->ops = const_ops; 314 + pconst->id = power_zone->const_id_cnt; 315 + power_zone->const_id_cnt++; 316 + power_zone->zone_dev_attrs[count++] = 317 + &constraint_attrs[i].power_limit_attr.attr; 318 + power_zone->zone_dev_attrs[count++] = 319 + &constraint_attrs[i].time_window_attr.attr; 320 + if (pconst->ops->get_name) 321 + power_zone->zone_dev_attrs[count++] = 322 + &constraint_attrs[i].name_attr.attr; 323 + if (pconst->ops->get_max_power_uw) 324 + power_zone->zone_dev_attrs[count++] = 325 + &constraint_attrs[i].max_power_attr.attr; 326 + if (pconst->ops->get_min_power_uw) 327 + power_zone->zone_dev_attrs[count++] = 328 + &constraint_attrs[i].min_power_attr.attr; 329 + if (pconst->ops->get_max_time_window_us) 330 + power_zone->zone_dev_attrs[count++] = 331 + &constraint_attrs[i].max_time_window_attr.attr; 332 + if (pconst->ops->get_min_time_window_us) 333 + power_zone->zone_dev_attrs[count++] = 334 + &constraint_attrs[i].min_time_window_attr.attr; 335 + } 336 + power_zone->zone_attr_count = count; 337 + 338 + return ret; 339 + } 340 + 341 + static bool control_type_valid(void *control_type) 342 + { 343 + struct powercap_control_type *pos = NULL; 344 + bool found = false; 345 + 346 + mutex_lock(&powercap_cntrl_list_lock); 347 + 348 + list_for_each_entry(pos, &powercap_cntrl_list, node) { 349 + if (pos == control_type) { 350 + found = true; 351 + break; 352 + } 353 + } 354 + mutex_unlock(&powercap_cntrl_list_lock); 355 + 356 + return found; 357 + } 358 + 359 + static ssize_t name_show(struct device *dev, 360 + struct device_attribute *attr, 361 + char *buf) 362 + { 363 + struct powercap_zone *power_zone = to_powercap_zone(dev); 364 + 365 + return sprintf(buf, "%s\n", power_zone->name); 366 + } 367 + 368 + static DEVICE_ATTR_RO(name); 369 + 370 + /* Create zone and attributes in sysfs */ 371 + static void create_power_zone_common_attributes( 372 + struct powercap_zone *power_zone) 373 + { 374 + int count = 0; 375 + 376 + power_zone->zone_dev_attrs[count++] = &dev_attr_name.attr; 377 + if (power_zone->ops->get_max_energy_range_uj) 378 + power_zone->zone_dev_attrs[count++] = 379 + &dev_attr_max_energy_range_uj.attr; 380 + if (power_zone->ops->get_energy_uj) 381 + power_zone->zone_dev_attrs[count++] = 382 + &dev_attr_energy_uj.attr; 383 + if (power_zone->ops->get_power_uw) 384 + power_zone->zone_dev_attrs[count++] = 385 + &dev_attr_power_uw.attr; 386 + if (power_zone->ops->get_max_power_range_uw) 387 + power_zone->zone_dev_attrs[count++] = 388 + &dev_attr_max_power_range_uw.attr; 389 + power_zone->zone_dev_attrs[count] = NULL; 390 + power_zone->zone_attr_count = count; 391 + } 392 + 393 + static void powercap_release(struct device *dev) 394 + { 395 + bool allocated; 396 + 397 + if (dev->parent) { 398 + struct powercap_zone *power_zone = to_powercap_zone(dev); 399 + 400 + /* Store flag as the release() may free memory */ 401 + allocated = power_zone->allocated; 402 + /* Remove id from parent idr struct */ 403 + idr_remove(power_zone->parent_idr, power_zone->id); 404 + /* Destroy idrs allocated for this zone */ 405 + idr_destroy(&power_zone->idr); 406 + kfree(power_zone->name); 407 + kfree(power_zone->zone_dev_attrs); 408 + kfree(power_zone->constraints); 409 + if (power_zone->ops->release) 410 + power_zone->ops->release(power_zone); 411 + if (allocated) 412 + kfree(power_zone); 413 + } else { 414 + struct powercap_control_type *control_type = 415 + to_powercap_control_type(dev); 416 + 417 + /* Store flag as the release() may free memory */ 418 + allocated = control_type->allocated; 419 + idr_destroy(&control_type->idr); 420 + mutex_destroy(&control_type->lock); 421 + if (control_type->ops && control_type->ops->release) 422 + control_type->ops->release(control_type); 423 + if (allocated) 424 + kfree(control_type); 425 + } 426 + } 427 + 428 + static ssize_t enabled_show(struct device *dev, 429 + struct device_attribute *attr, 430 + char *buf) 431 + { 432 + bool mode = true; 433 + 434 + /* Default is enabled */ 435 + if (dev->parent) { 436 + struct powercap_zone *power_zone = to_powercap_zone(dev); 437 + if (power_zone->ops->get_enable) 438 + if (power_zone->ops->get_enable(power_zone, &mode)) 439 + mode = false; 440 + } else { 441 + struct powercap_control_type *control_type = 442 + to_powercap_control_type(dev); 443 + if (control_type->ops && control_type->ops->get_enable) 444 + if (control_type->ops->get_enable(control_type, &mode)) 445 + mode = false; 446 + } 447 + 448 + return sprintf(buf, "%d\n", mode); 449 + } 450 + 451 + static ssize_t enabled_store(struct device *dev, 452 + struct device_attribute *attr, 453 + const char *buf, size_t len) 454 + { 455 + bool mode; 456 + 457 + if (strtobool(buf, &mode)) 458 + return -EINVAL; 459 + if (dev->parent) { 460 + struct powercap_zone *power_zone = to_powercap_zone(dev); 461 + if (power_zone->ops->set_enable) 462 + if (!power_zone->ops->set_enable(power_zone, mode)) 463 + return len; 464 + } else { 465 + struct powercap_control_type *control_type = 466 + to_powercap_control_type(dev); 467 + if (control_type->ops && control_type->ops->set_enable) 468 + if (!control_type->ops->set_enable(control_type, mode)) 469 + return len; 470 + } 471 + 472 + return -ENOSYS; 473 + } 474 + 475 + static struct device_attribute powercap_def_attrs[] = { 476 + __ATTR(enabled, S_IWUSR | S_IRUGO, enabled_show, 477 + enabled_store), 478 + __ATTR_NULL 479 + }; 480 + 481 + static struct class powercap_class = { 482 + .name = "powercap", 483 + .dev_release = powercap_release, 484 + .dev_attrs = powercap_def_attrs, 485 + }; 486 + 487 + struct powercap_zone *powercap_register_zone( 488 + struct powercap_zone *power_zone, 489 + struct powercap_control_type *control_type, 490 + const char *name, 491 + struct powercap_zone *parent, 492 + const struct powercap_zone_ops *ops, 493 + int nr_constraints, 494 + struct powercap_zone_constraint_ops *const_ops) 495 + { 496 + int result; 497 + int nr_attrs; 498 + 499 + if (!name || !control_type || !ops || 500 + nr_constraints > MAX_CONSTRAINTS_PER_ZONE || 501 + (!ops->get_energy_uj && !ops->get_power_uw) || 502 + !control_type_valid(control_type)) 503 + return ERR_PTR(-EINVAL); 504 + 505 + if (power_zone) { 506 + if (!ops->release) 507 + return ERR_PTR(-EINVAL); 508 + memset(power_zone, 0, sizeof(*power_zone)); 509 + } else { 510 + power_zone = kzalloc(sizeof(*power_zone), GFP_KERNEL); 511 + if (!power_zone) 512 + return ERR_PTR(-ENOMEM); 513 + power_zone->allocated = true; 514 + } 515 + power_zone->ops = ops; 516 + power_zone->control_type_inst = control_type; 517 + if (!parent) { 518 + power_zone->dev.parent = &control_type->dev; 519 + power_zone->parent_idr = &control_type->idr; 520 + } else { 521 + power_zone->dev.parent = &parent->dev; 522 + power_zone->parent_idr = &parent->idr; 523 + } 524 + power_zone->dev.class = &powercap_class; 525 + 526 + mutex_lock(&control_type->lock); 527 + /* Using idr to get the unique id */ 528 + result = idr_alloc(power_zone->parent_idr, NULL, 0, 0, GFP_KERNEL); 529 + if (result < 0) 530 + goto err_idr_alloc; 531 + 532 + power_zone->id = result; 533 + idr_init(&power_zone->idr); 534 + power_zone->name = kstrdup(name, GFP_KERNEL); 535 + if (!power_zone->name) 536 + goto err_name_alloc; 537 + dev_set_name(&power_zone->dev, "%s:%x", 538 + dev_name(power_zone->dev.parent), 539 + power_zone->id); 540 + power_zone->constraints = kzalloc(sizeof(*power_zone->constraints) * 541 + nr_constraints, GFP_KERNEL); 542 + if (!power_zone->constraints) 543 + goto err_const_alloc; 544 + 545 + nr_attrs = nr_constraints * POWERCAP_CONSTRAINTS_ATTRS + 546 + POWERCAP_ZONE_MAX_ATTRS + 1; 547 + power_zone->zone_dev_attrs = kzalloc(sizeof(void *) * 548 + nr_attrs, GFP_KERNEL); 549 + if (!power_zone->zone_dev_attrs) 550 + goto err_attr_alloc; 551 + create_power_zone_common_attributes(power_zone); 552 + result = create_constraints(power_zone, nr_constraints, const_ops); 553 + if (result) 554 + goto err_dev_ret; 555 + 556 + power_zone->zone_dev_attrs[power_zone->zone_attr_count] = NULL; 557 + power_zone->dev_zone_attr_group.attrs = power_zone->zone_dev_attrs; 558 + power_zone->dev_attr_groups[0] = &power_zone->dev_zone_attr_group; 559 + power_zone->dev_attr_groups[1] = NULL; 560 + power_zone->dev.groups = power_zone->dev_attr_groups; 561 + result = device_register(&power_zone->dev); 562 + if (result) 563 + goto err_dev_ret; 564 + 565 + control_type->nr_zones++; 566 + mutex_unlock(&control_type->lock); 567 + 568 + return power_zone; 569 + 570 + err_dev_ret: 571 + kfree(power_zone->zone_dev_attrs); 572 + err_attr_alloc: 573 + kfree(power_zone->constraints); 574 + err_const_alloc: 575 + kfree(power_zone->name); 576 + err_name_alloc: 577 + idr_remove(power_zone->parent_idr, power_zone->id); 578 + err_idr_alloc: 579 + if (power_zone->allocated) 580 + kfree(power_zone); 581 + mutex_unlock(&control_type->lock); 582 + 583 + return ERR_PTR(result); 584 + } 585 + EXPORT_SYMBOL_GPL(powercap_register_zone); 586 + 587 + int powercap_unregister_zone(struct powercap_control_type *control_type, 588 + struct powercap_zone *power_zone) 589 + { 590 + if (!power_zone || !control_type) 591 + return -EINVAL; 592 + 593 + mutex_lock(&control_type->lock); 594 + control_type->nr_zones--; 595 + mutex_unlock(&control_type->lock); 596 + 597 + device_unregister(&power_zone->dev); 598 + 599 + return 0; 600 + } 601 + EXPORT_SYMBOL_GPL(powercap_unregister_zone); 602 + 603 + struct powercap_control_type *powercap_register_control_type( 604 + struct powercap_control_type *control_type, 605 + const char *name, 606 + const struct powercap_control_type_ops *ops) 607 + { 608 + int result; 609 + 610 + if (!name) 611 + return ERR_PTR(-EINVAL); 612 + if (control_type) { 613 + if (!ops || !ops->release) 614 + return ERR_PTR(-EINVAL); 615 + memset(control_type, 0, sizeof(*control_type)); 616 + } else { 617 + control_type = kzalloc(sizeof(*control_type), GFP_KERNEL); 618 + if (!control_type) 619 + return ERR_PTR(-ENOMEM); 620 + control_type->allocated = true; 621 + } 622 + mutex_init(&control_type->lock); 623 + control_type->ops = ops; 624 + INIT_LIST_HEAD(&control_type->node); 625 + control_type->dev.class = &powercap_class; 626 + dev_set_name(&control_type->dev, name); 627 + result = device_register(&control_type->dev); 628 + if (result) { 629 + if (control_type->allocated) 630 + kfree(control_type); 631 + return ERR_PTR(result); 632 + } 633 + idr_init(&control_type->idr); 634 + 635 + mutex_lock(&powercap_cntrl_list_lock); 636 + list_add_tail(&control_type->node, &powercap_cntrl_list); 637 + mutex_unlock(&powercap_cntrl_list_lock); 638 + 639 + return control_type; 640 + } 641 + EXPORT_SYMBOL_GPL(powercap_register_control_type); 642 + 643 + int powercap_unregister_control_type(struct powercap_control_type *control_type) 644 + { 645 + struct powercap_control_type *pos = NULL; 646 + 647 + if (control_type->nr_zones) { 648 + dev_err(&control_type->dev, "Zones of this type still not freed\n"); 649 + return -EINVAL; 650 + } 651 + mutex_lock(&powercap_cntrl_list_lock); 652 + list_for_each_entry(pos, &powercap_cntrl_list, node) { 653 + if (pos == control_type) { 654 + list_del(&control_type->node); 655 + mutex_unlock(&powercap_cntrl_list_lock); 656 + device_unregister(&control_type->dev); 657 + return 0; 658 + } 659 + } 660 + mutex_unlock(&powercap_cntrl_list_lock); 661 + 662 + return -ENODEV; 663 + } 664 + EXPORT_SYMBOL_GPL(powercap_unregister_control_type); 665 + 666 + static int __init powercap_init(void) 667 + { 668 + int result = 0; 669 + 670 + result = seed_constraint_attributes(); 671 + if (result) 672 + return result; 673 + 674 + result = class_register(&powercap_class); 675 + 676 + return result; 677 + } 678 + 679 + device_initcall(powercap_init); 680 + 681 + MODULE_DESCRIPTION("PowerCap sysfs Driver"); 682 + MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 683 + MODULE_LICENSE("GPL v2");
+325
include/linux/powercap.h
··· 1 + /* 2 + * powercap.h: Data types and headers for sysfs power capping interface 3 + * Copyright (c) 2013, Intel Corporation. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms and conditions of the GNU General Public License, 7 + * version 2, as published by the Free Software Foundation. 8 + * 9 + * This program is distributed in the hope it will be useful, but WITHOUT 10 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 + * more details. 13 + * 14 + * You should have received a copy of the GNU General Public License along with 15 + * this program; if not, write to the Free Software Foundation, Inc. 16 + * 17 + */ 18 + 19 + #ifndef __POWERCAP_H__ 20 + #define __POWERCAP_H__ 21 + 22 + #include <linux/device.h> 23 + #include <linux/idr.h> 24 + 25 + /* 26 + * A power cap class device can contain multiple powercap control_types. 27 + * Each control_type can have multiple power zones, which can be independently 28 + * controlled. Each power zone can have one or more constraints. 29 + */ 30 + 31 + struct powercap_control_type; 32 + struct powercap_zone; 33 + struct powercap_zone_constraint; 34 + 35 + /** 36 + * struct powercap_control_type_ops - Define control type callbacks 37 + * @set_enable: Enable/Disable whole control type. 38 + * Default is enabled. But this callback allows all zones 39 + * to be in disable state and remove any applied power 40 + * limits. If disabled power zone can only be monitored 41 + * not controlled. 42 + * @get_enable: get Enable/Disable status. 43 + * @release: Callback to inform that last reference to this 44 + * control type is closed. So it is safe to free data 45 + * structure associated with this control type. 46 + * This callback is mandatory if the client own memory 47 + * for the control type. 48 + * 49 + * This structure defines control type callbacks to be implemented by client 50 + * drivers 51 + */ 52 + struct powercap_control_type_ops { 53 + int (*set_enable) (struct powercap_control_type *, bool mode); 54 + int (*get_enable) (struct powercap_control_type *, bool *mode); 55 + int (*release) (struct powercap_control_type *); 56 + }; 57 + 58 + /** 59 + * struct powercap_control_type- Defines a powercap control_type 60 + * @name: name of control_type 61 + * @dev: device for this control_type 62 + * @idr: idr to have unique id for its child 63 + * @root_node: Root holding power zones for this control_type 64 + * @ops: Pointer to callback struct 65 + * @node_lock: mutex for control type 66 + * @allocated: This is possible that client owns the memory 67 + * used by this structure. In this case 68 + * this flag is set to false by framework to 69 + * prevent deallocation during release process. 70 + * Otherwise this flag is set to true. 71 + * @ctrl_inst: link to the control_type list 72 + * 73 + * Defines powercap control_type. This acts as a container for power 74 + * zones, which use same method to control power. E.g. RAPL, RAPL-PCI etc. 75 + * All fields are private and should not be used by client drivers. 76 + */ 77 + struct powercap_control_type { 78 + struct device dev; 79 + struct idr idr; 80 + int nr_zones; 81 + const struct powercap_control_type_ops *ops; 82 + struct mutex lock; 83 + bool allocated; 84 + struct list_head node; 85 + }; 86 + 87 + /** 88 + * struct powercap_zone_ops - Define power zone callbacks 89 + * @get_max_energy_range_uj: Get maximum range of energy counter in 90 + * micro-joules. 91 + * @get_energy_uj: Get current energy counter in micro-joules. 92 + * @reset_energy_uj: Reset micro-joules energy counter. 93 + * @get_max_power_range_uw: Get maximum range of power counter in 94 + * micro-watts. 95 + * @get_power_uw: Get current power counter in micro-watts. 96 + * @set_enable: Enable/Disable power zone controls. 97 + * Default is enabled. 98 + * @get_enable: get Enable/Disable status. 99 + * @release: Callback to inform that last reference to this 100 + * control type is closed. So it is safe to free 101 + * data structure associated with this 102 + * control type. Mandatory, if client driver owns 103 + * the power_zone memory. 104 + * 105 + * This structure defines zone callbacks to be implemented by client drivers. 106 + * Client drives can define both energy and power related callbacks. But at 107 + * the least one type (either power or energy) is mandatory. Client drivers 108 + * should handle mutual exclusion, if required in callbacks. 109 + */ 110 + struct powercap_zone_ops { 111 + int (*get_max_energy_range_uj) (struct powercap_zone *, u64 *); 112 + int (*get_energy_uj) (struct powercap_zone *, u64 *); 113 + int (*reset_energy_uj) (struct powercap_zone *); 114 + int (*get_max_power_range_uw) (struct powercap_zone *, u64 *); 115 + int (*get_power_uw) (struct powercap_zone *, u64 *); 116 + int (*set_enable) (struct powercap_zone *, bool mode); 117 + int (*get_enable) (struct powercap_zone *, bool *mode); 118 + int (*release) (struct powercap_zone *); 119 + }; 120 + 121 + #define POWERCAP_ZONE_MAX_ATTRS 6 122 + #define POWERCAP_CONSTRAINTS_ATTRS 8 123 + #define MAX_CONSTRAINTS_PER_ZONE 10 124 + /** 125 + * struct powercap_zone- Defines instance of a power cap zone 126 + * @id: Unique id 127 + * @name: Power zone name. 128 + * @control_type_inst: Control type instance for this zone. 129 + * @ops: Pointer to the zone operation structure. 130 + * @dev: Instance of a device. 131 + * @const_id_cnt: Number of constraint defined. 132 + * @idr: Instance to an idr entry for children zones. 133 + * @parent_idr: To remove reference from the parent idr. 134 + * @private_data: Private data pointer if any for this zone. 135 + * @zone_dev_attrs: Attributes associated with this device. 136 + * @zone_attr_count: Attribute count. 137 + * @dev_zone_attr_group: Attribute group for attributes. 138 + * @dev_attr_groups: Attribute group store to register with device. 139 + * @allocated: This is possible that client owns the memory 140 + * used by this structure. In this case 141 + * this flag is set to false by framework to 142 + * prevent deallocation during release process. 143 + * Otherwise this flag is set to true. 144 + * @constraint_ptr: List of constraints for this zone. 145 + * 146 + * This defines a power zone instance. The fields of this structure are 147 + * private, and should not be used by client drivers. 148 + */ 149 + struct powercap_zone { 150 + int id; 151 + char *name; 152 + void *control_type_inst; 153 + const struct powercap_zone_ops *ops; 154 + struct device dev; 155 + int const_id_cnt; 156 + struct idr idr; 157 + struct idr *parent_idr; 158 + void *private_data; 159 + struct attribute **zone_dev_attrs; 160 + int zone_attr_count; 161 + struct attribute_group dev_zone_attr_group; 162 + const struct attribute_group *dev_attr_groups[2]; /* 1 group + NULL */ 163 + bool allocated; 164 + struct powercap_zone_constraint *constraints; 165 + }; 166 + 167 + /** 168 + * struct powercap_zone_constraint_ops - Define constraint callbacks 169 + * @set_power_limit_uw: Set power limit in micro-watts. 170 + * @get_power_limit_uw: Get power limit in micro-watts. 171 + * @set_time_window_us: Set time window in micro-seconds. 172 + * @get_time_window_us: Get time window in micro-seconds. 173 + * @get_max_power_uw: Get max power allowed in micro-watts. 174 + * @get_min_power_uw: Get min power allowed in micro-watts. 175 + * @get_max_time_window_us: Get max time window allowed in micro-seconds. 176 + * @get_min_time_window_us: Get min time window allowed in micro-seconds. 177 + * @get_name: Get the name of constraint 178 + * 179 + * This structure is used to define the constraint callbacks for the client 180 + * drivers. The following callbacks are mandatory and can't be NULL: 181 + * set_power_limit_uw 182 + * get_power_limit_uw 183 + * set_time_window_us 184 + * get_time_window_us 185 + * get_name 186 + * Client drivers should handle mutual exclusion, if required in callbacks. 187 + */ 188 + struct powercap_zone_constraint_ops { 189 + int (*set_power_limit_uw) (struct powercap_zone *, int, u64); 190 + int (*get_power_limit_uw) (struct powercap_zone *, int, u64 *); 191 + int (*set_time_window_us) (struct powercap_zone *, int, u64); 192 + int (*get_time_window_us) (struct powercap_zone *, int, u64 *); 193 + int (*get_max_power_uw) (struct powercap_zone *, int, u64 *); 194 + int (*get_min_power_uw) (struct powercap_zone *, int, u64 *); 195 + int (*get_max_time_window_us) (struct powercap_zone *, int, u64 *); 196 + int (*get_min_time_window_us) (struct powercap_zone *, int, u64 *); 197 + const char *(*get_name) (struct powercap_zone *, int); 198 + }; 199 + 200 + /** 201 + * struct powercap_zone_constraint- Defines instance of a constraint 202 + * @id: Instance Id of this constraint. 203 + * @power_zone: Pointer to the power zone for this constraint. 204 + * @ops: Pointer to the constraint callbacks. 205 + * 206 + * This defines a constraint instance. 207 + */ 208 + struct powercap_zone_constraint { 209 + int id; 210 + struct powercap_zone *power_zone; 211 + struct powercap_zone_constraint_ops *ops; 212 + }; 213 + 214 + 215 + /* For clients to get their device pointer, may be used for dev_dbgs */ 216 + #define POWERCAP_GET_DEV(power_zone) (&power_zone->dev) 217 + 218 + /** 219 + * powercap_set_zone_data() - Set private data for a zone 220 + * @power_zone: A pointer to the valid zone instance. 221 + * @pdata: A pointer to the user private data. 222 + * 223 + * Allows client drivers to associate some private data to zone instance. 224 + */ 225 + static inline void powercap_set_zone_data(struct powercap_zone *power_zone, 226 + void *pdata) 227 + { 228 + if (power_zone) 229 + power_zone->private_data = pdata; 230 + } 231 + 232 + /** 233 + * powercap_get_zone_data() - Get private data for a zone 234 + * @power_zone: A pointer to the valid zone instance. 235 + * 236 + * Allows client drivers to get private data associate with a zone, 237 + * using call to powercap_set_zone_data. 238 + */ 239 + static inline void *powercap_get_zone_data(struct powercap_zone *power_zone) 240 + { 241 + if (power_zone) 242 + return power_zone->private_data; 243 + return NULL; 244 + } 245 + 246 + /** 247 + * powercap_register_control_type() - Register a control_type with framework 248 + * @control_type: Pointer to client allocated memory for the control type 249 + * structure storage. If this is NULL, powercap framework 250 + * will allocate memory and own it. 251 + * Advantage of this parameter is that client can embed 252 + * this data in its data structures and allocate in a 253 + * single call, preventing multiple allocations. 254 + * @control_type_name: The Name of this control_type, which will be shown 255 + * in the sysfs Interface. 256 + * @ops: Callbacks for control type. This parameter is optional. 257 + * 258 + * Used to create a control_type with the power capping class. Here control_type 259 + * can represent a type of technology, which can control a range of power zones. 260 + * For example a control_type can be RAPL (Running Average Power Limit) 261 + * Intel® 64 and IA-32 Processor Architectures. The name can be any string 262 + * which must be unique, otherwise this function returns NULL. 263 + * A pointer to the control_type instance is returned on success. 264 + */ 265 + struct powercap_control_type *powercap_register_control_type( 266 + struct powercap_control_type *control_type, 267 + const char *name, 268 + const struct powercap_control_type_ops *ops); 269 + 270 + /** 271 + * powercap_unregister_control_type() - Unregister a control_type from framework 272 + * @instance: A pointer to the valid control_type instance. 273 + * 274 + * Used to unregister a control_type with the power capping class. 275 + * All power zones registered under this control type have to be unregistered 276 + * before calling this function, or it will fail with an error code. 277 + */ 278 + int powercap_unregister_control_type(struct powercap_control_type *instance); 279 + 280 + /* Zone register/unregister API */ 281 + 282 + /** 283 + * powercap_register_zone() - Register a power zone 284 + * @power_zone: Pointer to client allocated memory for the power zone structure 285 + * storage. If this is NULL, powercap framework will allocate 286 + * memory and own it. Advantage of this parameter is that client 287 + * can embed this data in its data structures and allocate in a 288 + * single call, preventing multiple allocations. 289 + * @control_type: A control_type instance under which this zone operates. 290 + * @name: A name for this zone. 291 + * @parent: A pointer to the parent power zone instance if any or NULL 292 + * @ops: Pointer to zone operation callback structure. 293 + * @no_constraints: Number of constraints for this zone 294 + * @const_ops: Pointer to constraint callback structure 295 + * 296 + * Register a power zone under a given control type. A power zone must register 297 + * a pointer to a structure representing zone callbacks. 298 + * A power zone can be located under a parent power zone, in which case @parent 299 + * should point to it. Otherwise, if @parent is NULL, the new power zone will 300 + * be located directly under the given control type 301 + * For each power zone there may be a number of constraints that appear in the 302 + * sysfs under that zone as attributes with unique numeric IDs. 303 + * Returns pointer to the power_zone on success. 304 + */ 305 + struct powercap_zone *powercap_register_zone( 306 + struct powercap_zone *power_zone, 307 + struct powercap_control_type *control_type, 308 + const char *name, 309 + struct powercap_zone *parent, 310 + const struct powercap_zone_ops *ops, 311 + int nr_constraints, 312 + struct powercap_zone_constraint_ops *const_ops); 313 + 314 + /** 315 + * powercap_unregister_zone() - Unregister a zone device 316 + * @control_type: A pointer to the valid instance of a control_type. 317 + * @power_zone: A pointer to the valid zone instance for a control_type 318 + * 319 + * Used to unregister a zone device for a control_type. Caller should 320 + * make sure that children for this zone are unregistered first. 321 + */ 322 + int powercap_unregister_zone(struct powercap_control_type *control_type, 323 + struct powercap_zone *power_zone); 324 + 325 + #endif