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