1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * A hwmon driver for ACPI 4.0 power meters 4 * Copyright (C) 2009 IBM 5 * 6 * Author: Darrick J. Wong <darrick.wong@oracle.com> 7 */ 8 9#include <linux/module.h> 10#include <linux/hwmon.h> 11#include <linux/hwmon-sysfs.h> 12#include <linux/jiffies.h> 13#include <linux/mutex.h> 14#include <linux/dmi.h> 15#include <linux/slab.h> 16#include <linux/kdev_t.h> 17#include <linux/sched.h> 18#include <linux/time.h> 19#include <linux/err.h> 20#include <linux/acpi.h> 21 22#define ACPI_POWER_METER_NAME "power_meter" 23ACPI_MODULE_NAME(ACPI_POWER_METER_NAME); 24#define ACPI_POWER_METER_DEVICE_NAME "Power Meter" 25#define ACPI_POWER_METER_CLASS "pwr_meter_resource" 26 27#define NUM_SENSORS 17 28 29#define POWER_METER_CAN_MEASURE (1 << 0) 30#define POWER_METER_CAN_TRIP (1 << 1) 31#define POWER_METER_CAN_CAP (1 << 2) 32#define POWER_METER_CAN_NOTIFY (1 << 3) 33#define POWER_METER_IS_BATTERY (1 << 8) 34#define UNKNOWN_HYSTERESIS 0xFFFFFFFF 35#define UNKNOWN_POWER 0xFFFFFFFF 36 37#define METER_NOTIFY_CONFIG 0x80 38#define METER_NOTIFY_TRIP 0x81 39#define METER_NOTIFY_CAP 0x82 40#define METER_NOTIFY_CAPPING 0x83 41#define METER_NOTIFY_INTERVAL 0x84 42 43#define POWER_AVERAGE_NAME "power1_average" 44#define POWER_CAP_NAME "power1_cap" 45#define POWER_AVG_INTERVAL_NAME "power1_average_interval" 46#define POWER_ALARM_NAME "power1_alarm" 47 48static int cap_in_hardware; 49static bool force_cap_on; 50 51static int can_cap_in_hardware(void) 52{ 53 return force_cap_on || cap_in_hardware; 54} 55 56static const struct acpi_device_id power_meter_ids[] = { 57 {"ACPI000D", 0}, 58 {"", 0}, 59}; 60MODULE_DEVICE_TABLE(acpi, power_meter_ids); 61 62struct acpi_power_meter_capabilities { 63 u64 flags; 64 u64 units; 65 u64 type; 66 u64 accuracy; 67 u64 sampling_time; 68 u64 min_avg_interval; 69 u64 max_avg_interval; 70 u64 hysteresis; 71 u64 configurable_cap; 72 u64 min_cap; 73 u64 max_cap; 74}; 75 76struct acpi_power_meter_resource { 77 struct acpi_device *acpi_dev; 78 acpi_bus_id name; 79 struct mutex lock; 80 struct device *hwmon_dev; 81 struct acpi_power_meter_capabilities caps; 82 acpi_string model_number; 83 acpi_string serial_number; 84 acpi_string oem_info; 85 u64 power; 86 u64 cap; 87 u64 avg_interval; 88 int sensors_valid; 89 unsigned long sensors_last_updated; 90 struct sensor_device_attribute sensors[NUM_SENSORS]; 91 int num_sensors; 92 s64 trip[2]; 93 int num_domain_devices; 94 struct acpi_device **domain_devices; 95 struct kobject *holders_dir; 96}; 97 98struct sensor_template { 99 char *label; 100 ssize_t (*show)(struct device *dev, 101 struct device_attribute *devattr, 102 char *buf); 103 ssize_t (*set)(struct device *dev, 104 struct device_attribute *devattr, 105 const char *buf, size_t count); 106 int index; 107}; 108 109/* Averaging interval */ 110static int update_avg_interval(struct acpi_power_meter_resource *resource) 111{ 112 unsigned long long data; 113 acpi_status status; 114 115 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GAI", 116 NULL, &data); 117 if (ACPI_FAILURE(status)) { 118 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _GAI")); 119 return -ENODEV; 120 } 121 122 resource->avg_interval = data; 123 return 0; 124} 125 126static ssize_t show_avg_interval(struct device *dev, 127 struct device_attribute *devattr, 128 char *buf) 129{ 130 struct acpi_device *acpi_dev = to_acpi_device(dev); 131 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 132 133 mutex_lock(&resource->lock); 134 update_avg_interval(resource); 135 mutex_unlock(&resource->lock); 136 137 return sprintf(buf, "%llu\n", resource->avg_interval); 138} 139 140static ssize_t set_avg_interval(struct device *dev, 141 struct device_attribute *devattr, 142 const char *buf, size_t count) 143{ 144 struct acpi_device *acpi_dev = to_acpi_device(dev); 145 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 146 union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 147 struct acpi_object_list args = { 1, &arg0 }; 148 int res; 149 unsigned long temp; 150 unsigned long long data; 151 acpi_status status; 152 153 res = kstrtoul(buf, 10, &temp); 154 if (res) 155 return res; 156 157 if (temp > resource->caps.max_avg_interval || 158 temp < resource->caps.min_avg_interval) 159 return -EINVAL; 160 arg0.integer.value = temp; 161 162 mutex_lock(&resource->lock); 163 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PAI", 164 &args, &data); 165 if (!ACPI_FAILURE(status)) 166 resource->avg_interval = temp; 167 mutex_unlock(&resource->lock); 168 169 if (ACPI_FAILURE(status)) { 170 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PAI")); 171 return -EINVAL; 172 } 173 174 /* _PAI returns 0 on success, nonzero otherwise */ 175 if (data) 176 return -EINVAL; 177 178 return count; 179} 180 181/* Cap functions */ 182static int update_cap(struct acpi_power_meter_resource *resource) 183{ 184 unsigned long long data; 185 acpi_status status; 186 187 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GHL", 188 NULL, &data); 189 if (ACPI_FAILURE(status)) { 190 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _GHL")); 191 return -ENODEV; 192 } 193 194 resource->cap = data; 195 return 0; 196} 197 198static ssize_t show_cap(struct device *dev, 199 struct device_attribute *devattr, 200 char *buf) 201{ 202 struct acpi_device *acpi_dev = to_acpi_device(dev); 203 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 204 205 mutex_lock(&resource->lock); 206 update_cap(resource); 207 mutex_unlock(&resource->lock); 208 209 return sprintf(buf, "%llu\n", resource->cap * 1000); 210} 211 212static ssize_t set_cap(struct device *dev, struct device_attribute *devattr, 213 const char *buf, size_t count) 214{ 215 struct acpi_device *acpi_dev = to_acpi_device(dev); 216 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 217 union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 218 struct acpi_object_list args = { 1, &arg0 }; 219 int res; 220 unsigned long temp; 221 unsigned long long data; 222 acpi_status status; 223 224 res = kstrtoul(buf, 10, &temp); 225 if (res) 226 return res; 227 228 temp = DIV_ROUND_CLOSEST(temp, 1000); 229 if (temp > resource->caps.max_cap || temp < resource->caps.min_cap) 230 return -EINVAL; 231 arg0.integer.value = temp; 232 233 mutex_lock(&resource->lock); 234 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_SHL", 235 &args, &data); 236 if (!ACPI_FAILURE(status)) 237 resource->cap = temp; 238 mutex_unlock(&resource->lock); 239 240 if (ACPI_FAILURE(status)) { 241 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SHL")); 242 return -EINVAL; 243 } 244 245 /* _SHL returns 0 on success, nonzero otherwise */ 246 if (data) 247 return -EINVAL; 248 249 return count; 250} 251 252/* Power meter trip points */ 253static int set_acpi_trip(struct acpi_power_meter_resource *resource) 254{ 255 union acpi_object arg_objs[] = { 256 {ACPI_TYPE_INTEGER}, 257 {ACPI_TYPE_INTEGER} 258 }; 259 struct acpi_object_list args = { 2, arg_objs }; 260 unsigned long long data; 261 acpi_status status; 262 263 /* Both trip levels must be set */ 264 if (resource->trip[0] < 0 || resource->trip[1] < 0) 265 return 0; 266 267 /* This driver stores min, max; ACPI wants max, min. */ 268 arg_objs[0].integer.value = resource->trip[1]; 269 arg_objs[1].integer.value = resource->trip[0]; 270 271 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PTP", 272 &args, &data); 273 if (ACPI_FAILURE(status)) { 274 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTP")); 275 return -EINVAL; 276 } 277 278 /* _PTP returns 0 on success, nonzero otherwise */ 279 if (data) 280 return -EINVAL; 281 282 return 0; 283} 284 285static ssize_t set_trip(struct device *dev, struct device_attribute *devattr, 286 const char *buf, size_t count) 287{ 288 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 289 struct acpi_device *acpi_dev = to_acpi_device(dev); 290 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 291 int res; 292 unsigned long temp; 293 294 res = kstrtoul(buf, 10, &temp); 295 if (res) 296 return res; 297 298 temp = DIV_ROUND_CLOSEST(temp, 1000); 299 300 mutex_lock(&resource->lock); 301 resource->trip[attr->index - 7] = temp; 302 res = set_acpi_trip(resource); 303 mutex_unlock(&resource->lock); 304 305 if (res) 306 return res; 307 308 return count; 309} 310 311/* Power meter */ 312static int update_meter(struct acpi_power_meter_resource *resource) 313{ 314 unsigned long long data; 315 acpi_status status; 316 unsigned long local_jiffies = jiffies; 317 318 if (time_before(local_jiffies, resource->sensors_last_updated + 319 msecs_to_jiffies(resource->caps.sampling_time)) && 320 resource->sensors_valid) 321 return 0; 322 323 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM", 324 NULL, &data); 325 if (ACPI_FAILURE(status)) { 326 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMM")); 327 return -ENODEV; 328 } 329 330 resource->power = data; 331 resource->sensors_valid = 1; 332 resource->sensors_last_updated = jiffies; 333 return 0; 334} 335 336static ssize_t show_power(struct device *dev, 337 struct device_attribute *devattr, 338 char *buf) 339{ 340 struct acpi_device *acpi_dev = to_acpi_device(dev); 341 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 342 343 mutex_lock(&resource->lock); 344 update_meter(resource); 345 mutex_unlock(&resource->lock); 346 347 if (resource->power == UNKNOWN_POWER) 348 return -ENODATA; 349 350 return sprintf(buf, "%llu\n", resource->power * 1000); 351} 352 353/* Miscellaneous */ 354static ssize_t show_str(struct device *dev, 355 struct device_attribute *devattr, 356 char *buf) 357{ 358 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 359 struct acpi_device *acpi_dev = to_acpi_device(dev); 360 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 361 acpi_string val; 362 int ret; 363 364 mutex_lock(&resource->lock); 365 switch (attr->index) { 366 case 0: 367 val = resource->model_number; 368 break; 369 case 1: 370 val = resource->serial_number; 371 break; 372 case 2: 373 val = resource->oem_info; 374 break; 375 default: 376 WARN(1, "Implementation error: unexpected attribute index %d\n", 377 attr->index); 378 val = ""; 379 break; 380 } 381 ret = sprintf(buf, "%s\n", val); 382 mutex_unlock(&resource->lock); 383 return ret; 384} 385 386static ssize_t show_val(struct device *dev, 387 struct device_attribute *devattr, 388 char *buf) 389{ 390 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 391 struct acpi_device *acpi_dev = to_acpi_device(dev); 392 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 393 u64 val = 0; 394 395 switch (attr->index) { 396 case 0: 397 val = resource->caps.min_avg_interval; 398 break; 399 case 1: 400 val = resource->caps.max_avg_interval; 401 break; 402 case 2: 403 val = resource->caps.min_cap * 1000; 404 break; 405 case 3: 406 val = resource->caps.max_cap * 1000; 407 break; 408 case 4: 409 if (resource->caps.hysteresis == UNKNOWN_HYSTERESIS) 410 return sprintf(buf, "unknown\n"); 411 412 val = resource->caps.hysteresis * 1000; 413 break; 414 case 5: 415 if (resource->caps.flags & POWER_METER_IS_BATTERY) 416 val = 1; 417 else 418 val = 0; 419 break; 420 case 6: 421 if (resource->power > resource->cap) 422 val = 1; 423 else 424 val = 0; 425 break; 426 case 7: 427 case 8: 428 if (resource->trip[attr->index - 7] < 0) 429 return sprintf(buf, "unknown\n"); 430 431 val = resource->trip[attr->index - 7] * 1000; 432 break; 433 default: 434 WARN(1, "Implementation error: unexpected attribute index %d\n", 435 attr->index); 436 break; 437 } 438 439 return sprintf(buf, "%llu\n", val); 440} 441 442static ssize_t show_accuracy(struct device *dev, 443 struct device_attribute *devattr, 444 char *buf) 445{ 446 struct acpi_device *acpi_dev = to_acpi_device(dev); 447 struct acpi_power_meter_resource *resource = acpi_dev->driver_data; 448 unsigned int acc = resource->caps.accuracy; 449 450 return sprintf(buf, "%u.%u%%\n", acc / 1000, acc % 1000); 451} 452 453static ssize_t show_name(struct device *dev, 454 struct device_attribute *devattr, 455 char *buf) 456{ 457 return sprintf(buf, "%s\n", ACPI_POWER_METER_NAME); 458} 459 460#define RO_SENSOR_TEMPLATE(_label, _show, _index) \ 461 { \ 462 .label = _label, \ 463 .show = _show, \ 464 .index = _index, \ 465 } 466 467#define RW_SENSOR_TEMPLATE(_label, _show, _set, _index) \ 468 { \ 469 .label = _label, \ 470 .show = _show, \ 471 .set = _set, \ 472 .index = _index, \ 473 } 474 475/* Sensor descriptions. If you add a sensor, update NUM_SENSORS above! */ 476static struct sensor_template meter_attrs[] = { 477 RO_SENSOR_TEMPLATE(POWER_AVERAGE_NAME, show_power, 0), 478 RO_SENSOR_TEMPLATE("power1_accuracy", show_accuracy, 0), 479 RO_SENSOR_TEMPLATE("power1_average_interval_min", show_val, 0), 480 RO_SENSOR_TEMPLATE("power1_average_interval_max", show_val, 1), 481 RO_SENSOR_TEMPLATE("power1_is_battery", show_val, 5), 482 RW_SENSOR_TEMPLATE(POWER_AVG_INTERVAL_NAME, show_avg_interval, 483 set_avg_interval, 0), 484 {}, 485}; 486 487static struct sensor_template misc_cap_attrs[] = { 488 RO_SENSOR_TEMPLATE("power1_cap_min", show_val, 2), 489 RO_SENSOR_TEMPLATE("power1_cap_max", show_val, 3), 490 RO_SENSOR_TEMPLATE("power1_cap_hyst", show_val, 4), 491 RO_SENSOR_TEMPLATE(POWER_ALARM_NAME, show_val, 6), 492 {}, 493}; 494 495static struct sensor_template ro_cap_attrs[] = { 496 RO_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, 0), 497 {}, 498}; 499 500static struct sensor_template rw_cap_attrs[] = { 501 RW_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, set_cap, 0), 502 {}, 503}; 504 505static struct sensor_template trip_attrs[] = { 506 RW_SENSOR_TEMPLATE("power1_average_min", show_val, set_trip, 7), 507 RW_SENSOR_TEMPLATE("power1_average_max", show_val, set_trip, 8), 508 {}, 509}; 510 511static struct sensor_template misc_attrs[] = { 512 RO_SENSOR_TEMPLATE("name", show_name, 0), 513 RO_SENSOR_TEMPLATE("power1_model_number", show_str, 0), 514 RO_SENSOR_TEMPLATE("power1_oem_info", show_str, 2), 515 RO_SENSOR_TEMPLATE("power1_serial_number", show_str, 1), 516 {}, 517}; 518 519#undef RO_SENSOR_TEMPLATE 520#undef RW_SENSOR_TEMPLATE 521 522/* Read power domain data */ 523static void remove_domain_devices(struct acpi_power_meter_resource *resource) 524{ 525 int i; 526 527 if (!resource->num_domain_devices) 528 return; 529 530 for (i = 0; i < resource->num_domain_devices; i++) { 531 struct acpi_device *obj = resource->domain_devices[i]; 532 if (!obj) 533 continue; 534 535 sysfs_remove_link(resource->holders_dir, 536 kobject_name(&obj->dev.kobj)); 537 put_device(&obj->dev); 538 } 539 540 kfree(resource->domain_devices); 541 kobject_put(resource->holders_dir); 542 resource->num_domain_devices = 0; 543} 544 545static int read_domain_devices(struct acpi_power_meter_resource *resource) 546{ 547 int res = 0; 548 int i; 549 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 550 union acpi_object *pss; 551 acpi_status status; 552 553 status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMD", NULL, 554 &buffer); 555 if (ACPI_FAILURE(status)) { 556 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMD")); 557 return -ENODEV; 558 } 559 560 pss = buffer.pointer; 561 if (!pss || 562 pss->type != ACPI_TYPE_PACKAGE) { 563 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME 564 "Invalid _PMD data\n"); 565 res = -EFAULT; 566 goto end; 567 } 568 569 if (!pss->package.count) 570 goto end; 571 572 resource->domain_devices = kcalloc(pss->package.count, 573 sizeof(struct acpi_device *), 574 GFP_KERNEL); 575 if (!resource->domain_devices) { 576 res = -ENOMEM; 577 goto end; 578 } 579 580 resource->holders_dir = kobject_create_and_add("measures", 581 &resource->acpi_dev->dev.kobj); 582 if (!resource->holders_dir) { 583 res = -ENOMEM; 584 goto exit_free; 585 } 586 587 resource->num_domain_devices = pss->package.count; 588 589 for (i = 0; i < pss->package.count; i++) { 590 struct acpi_device *obj; 591 union acpi_object *element = &(pss->package.elements[i]); 592 593 /* Refuse non-references */ 594 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) 595 continue; 596 597 /* Create a symlink to domain objects */ 598 resource->domain_devices[i] = NULL; 599 if (acpi_bus_get_device(element->reference.handle, 600 &resource->domain_devices[i])) 601 continue; 602 603 obj = resource->domain_devices[i]; 604 get_device(&obj->dev); 605 606 res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj, 607 kobject_name(&obj->dev.kobj)); 608 if (res) { 609 put_device(&obj->dev); 610 resource->domain_devices[i] = NULL; 611 } 612 } 613 614 res = 0; 615 goto end; 616 617exit_free: 618 kfree(resource->domain_devices); 619end: 620 kfree(buffer.pointer); 621 return res; 622} 623 624/* Registration and deregistration */ 625static int register_attrs(struct acpi_power_meter_resource *resource, 626 struct sensor_template *attrs) 627{ 628 struct device *dev = &resource->acpi_dev->dev; 629 struct sensor_device_attribute *sensors = 630 &resource->sensors[resource->num_sensors]; 631 int res = 0; 632 633 while (attrs->label) { 634 sensors->dev_attr.attr.name = attrs->label; 635 sensors->dev_attr.attr.mode = 0444; 636 sensors->dev_attr.show = attrs->show; 637 sensors->index = attrs->index; 638 639 if (attrs->set) { 640 sensors->dev_attr.attr.mode |= 0200; 641 sensors->dev_attr.store = attrs->set; 642 } 643 644 sysfs_attr_init(&sensors->dev_attr.attr); 645 res = device_create_file(dev, &sensors->dev_attr); 646 if (res) { 647 sensors->dev_attr.attr.name = NULL; 648 goto error; 649 } 650 sensors++; 651 resource->num_sensors++; 652 attrs++; 653 } 654 655error: 656 return res; 657} 658 659static void remove_attrs(struct acpi_power_meter_resource *resource) 660{ 661 int i; 662 663 for (i = 0; i < resource->num_sensors; i++) { 664 if (!resource->sensors[i].dev_attr.attr.name) 665 continue; 666 device_remove_file(&resource->acpi_dev->dev, 667 &resource->sensors[i].dev_attr); 668 } 669 670 remove_domain_devices(resource); 671 672 resource->num_sensors = 0; 673} 674 675static int setup_attrs(struct acpi_power_meter_resource *resource) 676{ 677 int res = 0; 678 679 res = read_domain_devices(resource); 680 if (res) 681 return res; 682 683 if (resource->caps.flags & POWER_METER_CAN_MEASURE) { 684 res = register_attrs(resource, meter_attrs); 685 if (res) 686 goto error; 687 } 688 689 if (resource->caps.flags & POWER_METER_CAN_CAP) { 690 if (!can_cap_in_hardware()) { 691 dev_warn(&resource->acpi_dev->dev, 692 "Ignoring unsafe software power cap!\n"); 693 goto skip_unsafe_cap; 694 } 695 696 if (resource->caps.configurable_cap) 697 res = register_attrs(resource, rw_cap_attrs); 698 else 699 res = register_attrs(resource, ro_cap_attrs); 700 701 if (res) 702 goto error; 703 704 res = register_attrs(resource, misc_cap_attrs); 705 if (res) 706 goto error; 707 } 708 709skip_unsafe_cap: 710 if (resource->caps.flags & POWER_METER_CAN_TRIP) { 711 res = register_attrs(resource, trip_attrs); 712 if (res) 713 goto error; 714 } 715 716 res = register_attrs(resource, misc_attrs); 717 if (res) 718 goto error; 719 720 return res; 721error: 722 remove_attrs(resource); 723 return res; 724} 725 726static void free_capabilities(struct acpi_power_meter_resource *resource) 727{ 728 acpi_string *str; 729 int i; 730 731 str = &resource->model_number; 732 for (i = 0; i < 3; i++, str++) 733 kfree(*str); 734} 735 736static int read_capabilities(struct acpi_power_meter_resource *resource) 737{ 738 int res = 0; 739 int i; 740 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 741 struct acpi_buffer state = { 0, NULL }; 742 struct acpi_buffer format = { sizeof("NNNNNNNNNNN"), "NNNNNNNNNNN" }; 743 union acpi_object *pss; 744 acpi_string *str; 745 acpi_status status; 746 747 status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMC", NULL, 748 &buffer); 749 if (ACPI_FAILURE(status)) { 750 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMC")); 751 return -ENODEV; 752 } 753 754 pss = buffer.pointer; 755 if (!pss || 756 pss->type != ACPI_TYPE_PACKAGE || 757 pss->package.count != 14) { 758 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME 759 "Invalid _PMC data\n"); 760 res = -EFAULT; 761 goto end; 762 } 763 764 /* Grab all the integer data at once */ 765 state.length = sizeof(struct acpi_power_meter_capabilities); 766 state.pointer = &resource->caps; 767 768 status = acpi_extract_package(pss, &format, &state); 769 if (ACPI_FAILURE(status)) { 770 ACPI_EXCEPTION((AE_INFO, status, "Invalid data")); 771 res = -EFAULT; 772 goto end; 773 } 774 775 if (resource->caps.units) { 776 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME 777 "Unknown units %llu.\n", 778 resource->caps.units); 779 res = -EINVAL; 780 goto end; 781 } 782 783 /* Grab the string data */ 784 str = &resource->model_number; 785 786 for (i = 11; i < 14; i++) { 787 union acpi_object *element = &(pss->package.elements[i]); 788 789 if (element->type != ACPI_TYPE_STRING) { 790 res = -EINVAL; 791 goto error; 792 } 793 794 *str = kcalloc(element->string.length + 1, sizeof(u8), 795 GFP_KERNEL); 796 if (!*str) { 797 res = -ENOMEM; 798 goto error; 799 } 800 801 strncpy(*str, element->string.pointer, element->string.length); 802 str++; 803 } 804 805 dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n"); 806 goto end; 807error: 808 str = &resource->model_number; 809 for (i = 0; i < 3; i++, str++) 810 kfree(*str); 811end: 812 kfree(buffer.pointer); 813 return res; 814} 815 816/* Handle ACPI event notifications */ 817static void acpi_power_meter_notify(struct acpi_device *device, u32 event) 818{ 819 struct acpi_power_meter_resource *resource; 820 int res; 821 822 if (!device || !acpi_driver_data(device)) 823 return; 824 825 resource = acpi_driver_data(device); 826 827 switch (event) { 828 case METER_NOTIFY_CONFIG: 829 mutex_lock(&resource->lock); 830 free_capabilities(resource); 831 res = read_capabilities(resource); 832 mutex_unlock(&resource->lock); 833 if (res) 834 break; 835 836 remove_attrs(resource); 837 setup_attrs(resource); 838 break; 839 case METER_NOTIFY_TRIP: 840 sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME); 841 break; 842 case METER_NOTIFY_CAP: 843 sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME); 844 break; 845 case METER_NOTIFY_INTERVAL: 846 sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME); 847 break; 848 case METER_NOTIFY_CAPPING: 849 sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME); 850 dev_info(&device->dev, "Capping in progress.\n"); 851 break; 852 default: 853 WARN(1, "Unexpected event %d\n", event); 854 break; 855 } 856 857 acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS, 858 dev_name(&device->dev), event, 0); 859} 860 861static int acpi_power_meter_add(struct acpi_device *device) 862{ 863 int res; 864 struct acpi_power_meter_resource *resource; 865 866 if (!device) 867 return -EINVAL; 868 869 resource = kzalloc(sizeof(struct acpi_power_meter_resource), 870 GFP_KERNEL); 871 if (!resource) 872 return -ENOMEM; 873 874 resource->sensors_valid = 0; 875 resource->acpi_dev = device; 876 mutex_init(&resource->lock); 877 strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME); 878 strcpy(acpi_device_class(device), ACPI_POWER_METER_CLASS); 879 device->driver_data = resource; 880 881 free_capabilities(resource); 882 res = read_capabilities(resource); 883 if (res) 884 goto exit_free; 885 886 resource->trip[0] = resource->trip[1] = -1; 887 888 res = setup_attrs(resource); 889 if (res) 890 goto exit_free_capability; 891 892 resource->hwmon_dev = hwmon_device_register(&device->dev); 893 if (IS_ERR(resource->hwmon_dev)) { 894 res = PTR_ERR(resource->hwmon_dev); 895 goto exit_remove; 896 } 897 898 res = 0; 899 goto exit; 900 901exit_remove: 902 remove_attrs(resource); 903exit_free_capability: 904 free_capabilities(resource); 905exit_free: 906 kfree(resource); 907exit: 908 return res; 909} 910 911static int acpi_power_meter_remove(struct acpi_device *device) 912{ 913 struct acpi_power_meter_resource *resource; 914 915 if (!device || !acpi_driver_data(device)) 916 return -EINVAL; 917 918 resource = acpi_driver_data(device); 919 hwmon_device_unregister(resource->hwmon_dev); 920 921 remove_attrs(resource); 922 free_capabilities(resource); 923 924 kfree(resource); 925 return 0; 926} 927 928#ifdef CONFIG_PM_SLEEP 929 930static int acpi_power_meter_resume(struct device *dev) 931{ 932 struct acpi_power_meter_resource *resource; 933 934 if (!dev) 935 return -EINVAL; 936 937 resource = acpi_driver_data(to_acpi_device(dev)); 938 if (!resource) 939 return -EINVAL; 940 941 free_capabilities(resource); 942 read_capabilities(resource); 943 944 return 0; 945} 946 947#endif /* CONFIG_PM_SLEEP */ 948 949static SIMPLE_DEV_PM_OPS(acpi_power_meter_pm, NULL, acpi_power_meter_resume); 950 951static struct acpi_driver acpi_power_meter_driver = { 952 .name = "power_meter", 953 .class = ACPI_POWER_METER_CLASS, 954 .ids = power_meter_ids, 955 .ops = { 956 .add = acpi_power_meter_add, 957 .remove = acpi_power_meter_remove, 958 .notify = acpi_power_meter_notify, 959 }, 960 .drv.pm = &acpi_power_meter_pm, 961}; 962 963/* Module init/exit routines */ 964static int __init enable_cap_knobs(const struct dmi_system_id *d) 965{ 966 cap_in_hardware = 1; 967 return 0; 968} 969 970static const struct dmi_system_id pm_dmi_table[] __initconst = { 971 { 972 enable_cap_knobs, "IBM Active Energy Manager", 973 { 974 DMI_MATCH(DMI_SYS_VENDOR, "IBM") 975 }, 976 }, 977 {} 978}; 979 980static int __init acpi_power_meter_init(void) 981{ 982 int result; 983 984 if (acpi_disabled) 985 return -ENODEV; 986 987 dmi_check_system(pm_dmi_table); 988 989 result = acpi_bus_register_driver(&acpi_power_meter_driver); 990 if (result < 0) 991 return result; 992 993 return 0; 994} 995 996static void __exit acpi_power_meter_exit(void) 997{ 998 acpi_bus_unregister_driver(&acpi_power_meter_driver); 999} 1000 1001MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>"); 1002MODULE_DESCRIPTION("ACPI 4.0 power meter driver"); 1003MODULE_LICENSE("GPL"); 1004 1005module_param(force_cap_on, bool, 0644); 1006MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so."); 1007 1008module_init(acpi_power_meter_init); 1009module_exit(acpi_power_meter_exit); 1010