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