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 = kzalloc_objs(struct acpi_device *, 249 pss->package.count, GFP_KERNEL); 250 if (!resource->domain_devices) { 251 res = -ENOMEM; 252 goto end; 253 } 254 255 resource->holders_dir = kobject_create_and_add("measures", 256 &resource->acpi_dev->dev.kobj); 257 if (!resource->holders_dir) { 258 res = -ENOMEM; 259 goto exit_free; 260 } 261 262 resource->num_domain_devices = pss->package.count; 263 264 for (i = 0; i < pss->package.count; i++) { 265 struct acpi_device *obj; 266 union acpi_object *element = &pss->package.elements[i]; 267 268 /* Refuse non-references */ 269 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) 270 continue; 271 272 /* Create a symlink to domain objects */ 273 obj = acpi_get_acpi_dev(element->reference.handle); 274 resource->domain_devices[i] = obj; 275 if (!obj) 276 continue; 277 278 res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj, 279 kobject_name(&obj->dev.kobj)); 280 if (res) { 281 acpi_dev_put(obj); 282 resource->domain_devices[i] = NULL; 283 } 284 } 285 286 res = 0; 287 goto end; 288 289 exit_free: 290 kfree(resource->domain_devices); 291 end: 292 kfree(buffer.pointer); 293 return res; 294 } 295 296 static int set_trip(struct acpi_power_meter_resource *resource, u16 trip_idx, 297 unsigned long trip) 298 { 299 unsigned long trip_bk; 300 int ret; 301 302 trip = DIV_ROUND_CLOSEST(trip, 1000); 303 trip_bk = resource->trip[trip_idx]; 304 305 resource->trip[trip_idx] = trip; 306 ret = set_acpi_trip(resource); 307 if (ret) { 308 dev_err(&resource->acpi_dev->dev, "set %s failed.\n", 309 (trip_idx == POWER_METER_TRIP_AVERAGE_MIN_IDX) ? 310 "power1_average_min" : "power1_average_max"); 311 resource->trip[trip_idx] = trip_bk; 312 } 313 314 return ret; 315 } 316 317 static int set_cap(struct acpi_power_meter_resource *resource, 318 unsigned long cap) 319 { 320 union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 321 struct acpi_object_list args = { 1, &arg0 }; 322 unsigned long long data; 323 acpi_status status; 324 325 cap = DIV_ROUND_CLOSEST(cap, 1000); 326 if (cap > resource->caps.max_cap || cap < resource->caps.min_cap) 327 return -EINVAL; 328 329 arg0.integer.value = cap; 330 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_SHL", 331 &args, &data); 332 if (ACPI_FAILURE(status)) { 333 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_SHL", 334 status); 335 return -EINVAL; 336 } 337 resource->cap = cap; 338 339 /* _SHL returns 0 on success, nonzero otherwise */ 340 if (data) 341 return -EINVAL; 342 343 return 0; 344 } 345 346 static int set_avg_interval(struct acpi_power_meter_resource *resource, 347 unsigned long val) 348 { 349 union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 350 struct acpi_object_list args = { 1, &arg0 }; 351 unsigned long long data; 352 acpi_status status; 353 354 if (val > resource->caps.max_avg_interval || 355 val < resource->caps.min_avg_interval) 356 return -EINVAL; 357 358 arg0.integer.value = val; 359 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PAI", 360 &args, &data); 361 if (ACPI_FAILURE(status)) { 362 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PAI", 363 status); 364 return -EINVAL; 365 } 366 resource->avg_interval = val; 367 368 /* _PAI returns 0 on success, nonzero otherwise */ 369 if (data) 370 return -EINVAL; 371 372 return 0; 373 } 374 375 static int get_power_alarm_state(struct acpi_power_meter_resource *resource, 376 long *val) 377 { 378 int ret; 379 380 ret = update_meter(resource); 381 if (ret) 382 return ret; 383 384 /* need to update cap if not to support the notification. */ 385 if (!(resource->caps.flags & POWER_METER_CAN_NOTIFY)) { 386 ret = update_cap(resource); 387 if (ret) 388 return ret; 389 resource->power_alarm = resource->power > resource->cap; 390 *val = resource->power_alarm; 391 } else { 392 *val = resource->power_alarm || resource->power > resource->cap; 393 resource->power_alarm = resource->power > resource->cap; 394 } 395 396 return 0; 397 } 398 399 static umode_t power_meter_is_visible(const void *data, 400 enum hwmon_sensor_types type, 401 u32 attr, int channel) 402 { 403 const struct acpi_power_meter_resource *res = data; 404 405 if (type != hwmon_power) 406 return 0; 407 408 switch (attr) { 409 case hwmon_power_average: 410 case hwmon_power_average_interval_min: 411 case hwmon_power_average_interval_max: 412 if (res->caps.flags & POWER_METER_CAN_MEASURE) 413 return 0444; 414 break; 415 case hwmon_power_average_interval: 416 if (res->caps.flags & POWER_METER_CAN_MEASURE) 417 return 0644; 418 break; 419 case hwmon_power_cap_min: 420 case hwmon_power_cap_max: 421 case hwmon_power_alarm: 422 if (res->caps.flags & POWER_METER_CAN_CAP && can_cap_in_hardware()) 423 return 0444; 424 break; 425 case hwmon_power_cap: 426 if (res->caps.flags & POWER_METER_CAN_CAP && can_cap_in_hardware()) { 427 if (res->caps.configurable_cap) 428 return 0644; 429 else 430 return 0444; 431 } 432 break; 433 default: 434 break; 435 } 436 437 return 0; 438 } 439 440 static int power_meter_read(struct device *dev, enum hwmon_sensor_types type, 441 u32 attr, int channel, long *val) 442 { 443 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 444 int ret = 0; 445 446 if (type != hwmon_power) 447 return -EINVAL; 448 449 guard(mutex)(&res->lock); 450 451 switch (attr) { 452 case hwmon_power_average: 453 ret = update_meter(res); 454 if (ret) 455 return ret; 456 if (res->power == UNKNOWN_POWER) 457 return -ENODATA; 458 *val = res->power * 1000; 459 break; 460 case hwmon_power_average_interval_min: 461 *val = res->caps.min_avg_interval; 462 break; 463 case hwmon_power_average_interval_max: 464 *val = res->caps.max_avg_interval; 465 break; 466 case hwmon_power_average_interval: 467 ret = update_avg_interval(res); 468 if (ret) 469 return ret; 470 *val = (res)->avg_interval; 471 break; 472 case hwmon_power_cap_min: 473 *val = res->caps.min_cap * 1000; 474 break; 475 case hwmon_power_cap_max: 476 *val = res->caps.max_cap * 1000; 477 break; 478 case hwmon_power_alarm: 479 ret = get_power_alarm_state(res, val); 480 if (ret) 481 return ret; 482 break; 483 case hwmon_power_cap: 484 ret = update_cap(res); 485 if (ret) 486 return ret; 487 *val = res->cap * 1000; 488 break; 489 default: 490 break; 491 } 492 493 return 0; 494 } 495 496 static int power_meter_write(struct device *dev, enum hwmon_sensor_types type, 497 u32 attr, int channel, long val) 498 { 499 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 500 int ret; 501 502 if (type != hwmon_power) 503 return -EINVAL; 504 505 guard(mutex)(&res->lock); 506 switch (attr) { 507 case hwmon_power_cap: 508 ret = set_cap(res, val); 509 break; 510 case hwmon_power_average_interval: 511 ret = set_avg_interval(res, val); 512 break; 513 default: 514 ret = -EOPNOTSUPP; 515 } 516 517 return ret; 518 } 519 520 static const struct hwmon_channel_info * const power_meter_info[] = { 521 HWMON_CHANNEL_INFO(power, HWMON_P_AVERAGE | 522 HWMON_P_AVERAGE_INTERVAL | HWMON_P_AVERAGE_INTERVAL_MIN | 523 HWMON_P_AVERAGE_INTERVAL_MAX | HWMON_P_CAP | HWMON_P_CAP_MIN | 524 HWMON_P_CAP_MAX | HWMON_P_ALARM), 525 NULL 526 }; 527 528 static const struct hwmon_ops power_meter_ops = { 529 .is_visible = power_meter_is_visible, 530 .read = power_meter_read, 531 .write = power_meter_write, 532 }; 533 534 static const struct hwmon_chip_info power_meter_chip_info = { 535 .ops = &power_meter_ops, 536 .info = power_meter_info, 537 }; 538 539 static ssize_t power1_average_max_store(struct device *dev, 540 struct device_attribute *attr, 541 const char *buf, size_t count) 542 { 543 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 544 unsigned long trip; 545 int ret; 546 547 ret = kstrtoul(buf, 10, &trip); 548 if (ret) 549 return ret; 550 551 mutex_lock(&res->lock); 552 ret = set_trip(res, POWER_METER_TRIP_AVERAGE_MAX_IDX, trip); 553 mutex_unlock(&res->lock); 554 555 return ret == 0 ? count : ret; 556 } 557 558 static ssize_t power1_average_min_store(struct device *dev, 559 struct device_attribute *attr, 560 const char *buf, size_t count) 561 { 562 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 563 unsigned long trip; 564 int ret; 565 566 ret = kstrtoul(buf, 10, &trip); 567 if (ret) 568 return ret; 569 570 mutex_lock(&res->lock); 571 ret = set_trip(res, POWER_METER_TRIP_AVERAGE_MIN_IDX, trip); 572 mutex_unlock(&res->lock); 573 574 return ret == 0 ? count : ret; 575 } 576 577 static ssize_t power1_average_min_show(struct device *dev, 578 struct device_attribute *attr, char *buf) 579 { 580 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 581 582 if (res->trip[POWER_METER_TRIP_AVERAGE_MIN_IDX] < 0) 583 return sysfs_emit(buf, "unknown\n"); 584 585 return sysfs_emit(buf, "%lld\n", 586 res->trip[POWER_METER_TRIP_AVERAGE_MIN_IDX] * 1000); 587 } 588 589 static ssize_t power1_average_max_show(struct device *dev, 590 struct device_attribute *attr, char *buf) 591 { 592 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 593 594 if (res->trip[POWER_METER_TRIP_AVERAGE_MAX_IDX] < 0) 595 return sysfs_emit(buf, "unknown\n"); 596 597 return sysfs_emit(buf, "%lld\n", 598 res->trip[POWER_METER_TRIP_AVERAGE_MAX_IDX] * 1000); 599 } 600 601 static ssize_t power1_cap_hyst_show(struct device *dev, 602 struct device_attribute *attr, char *buf) 603 { 604 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 605 606 if (res->caps.hysteresis == UNKNOWN_HYSTERESIS) 607 return sysfs_emit(buf, "unknown\n"); 608 609 return sysfs_emit(buf, "%llu\n", res->caps.hysteresis * 1000); 610 } 611 612 static ssize_t power1_accuracy_show(struct device *dev, 613 struct device_attribute *attr, 614 char *buf) 615 { 616 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 617 unsigned int acc = res->caps.accuracy; 618 619 return sysfs_emit(buf, "%u.%u%%\n", acc / 1000, acc % 1000); 620 } 621 622 static ssize_t power1_is_battery_show(struct device *dev, 623 struct device_attribute *attr, 624 char *buf) 625 { 626 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 627 628 return sysfs_emit(buf, "%u\n", 629 res->caps.flags & POWER_METER_IS_BATTERY ? 1 : 0); 630 } 631 632 static ssize_t power1_model_number_show(struct device *dev, 633 struct device_attribute *attr, 634 char *buf) 635 { 636 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 637 638 return sysfs_emit(buf, "%s\n", res->model_number); 639 } 640 641 static ssize_t power1_oem_info_show(struct device *dev, 642 struct device_attribute *attr, 643 char *buf) 644 { 645 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 646 647 return sysfs_emit(buf, "%s\n", res->oem_info); 648 } 649 650 static ssize_t power1_serial_number_show(struct device *dev, 651 struct device_attribute *attr, 652 char *buf) 653 { 654 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 655 656 return sysfs_emit(buf, "%s\n", res->serial_number); 657 } 658 659 /* depend on POWER_METER_CAN_TRIP */ 660 static DEVICE_ATTR_RW(power1_average_max); 661 static DEVICE_ATTR_RW(power1_average_min); 662 663 /* depend on POWER_METER_CAN_CAP */ 664 static DEVICE_ATTR_RO(power1_cap_hyst); 665 666 /* depend on POWER_METER_CAN_MEASURE */ 667 static DEVICE_ATTR_RO(power1_accuracy); 668 static DEVICE_ATTR_RO(power1_is_battery); 669 670 static DEVICE_ATTR_RO(power1_model_number); 671 static DEVICE_ATTR_RO(power1_oem_info); 672 static DEVICE_ATTR_RO(power1_serial_number); 673 674 static umode_t power_extra_is_visible(struct kobject *kobj, 675 struct attribute *attr, int idx) 676 { 677 struct device *dev = kobj_to_dev(kobj); 678 struct acpi_power_meter_resource *res = dev_get_drvdata(dev); 679 680 if (attr == &dev_attr_power1_is_battery.attr || 681 attr == &dev_attr_power1_accuracy.attr) { 682 if ((res->caps.flags & POWER_METER_CAN_MEASURE) == 0) 683 return 0; 684 } 685 686 if (attr == &dev_attr_power1_cap_hyst.attr) { 687 if ((res->caps.flags & POWER_METER_CAN_CAP) == 0) { 688 return 0; 689 } else if (!can_cap_in_hardware()) { 690 dev_warn(&res->acpi_dev->dev, 691 "Ignoring unsafe software power cap!\n"); 692 return 0; 693 } 694 } 695 696 if (attr == &dev_attr_power1_average_max.attr || 697 attr == &dev_attr_power1_average_min.attr) { 698 if ((res->caps.flags & POWER_METER_CAN_TRIP) == 0) 699 return 0; 700 } 701 702 return attr->mode; 703 } 704 705 static struct attribute *power_extra_attrs[] = { 706 &dev_attr_power1_average_max.attr, 707 &dev_attr_power1_average_min.attr, 708 &dev_attr_power1_cap_hyst.attr, 709 &dev_attr_power1_accuracy.attr, 710 &dev_attr_power1_is_battery.attr, 711 &dev_attr_power1_model_number.attr, 712 &dev_attr_power1_oem_info.attr, 713 &dev_attr_power1_serial_number.attr, 714 NULL 715 }; 716 717 static const struct attribute_group power_extra_group = { 718 .attrs = power_extra_attrs, 719 .is_visible = power_extra_is_visible, 720 }; 721 722 __ATTRIBUTE_GROUPS(power_extra); 723 724 static void free_capabilities(struct acpi_power_meter_resource *resource) 725 { 726 acpi_string *str; 727 int i; 728 729 str = &resource->model_number; 730 for (i = 0; i < 3; i++, str++) { 731 kfree(*str); 732 *str = NULL; 733 } 734 } 735 736 static 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_evaluation_failure_warn(resource->acpi_dev->handle, "_PMC", 751 status); 752 return -ENODEV; 753 } 754 755 pss = buffer.pointer; 756 if (!pss || 757 pss->type != ACPI_TYPE_PACKAGE || 758 pss->package.count != 14) { 759 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME 760 "Invalid _PMC data\n"); 761 res = -EFAULT; 762 goto end; 763 } 764 765 /* Grab all the integer data at once */ 766 state.length = sizeof(struct acpi_power_meter_capabilities); 767 state.pointer = &resource->caps; 768 769 status = acpi_extract_package(pss, &format, &state); 770 if (ACPI_FAILURE(status)) { 771 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME 772 "_PMC package parsing failed: %s\n", 773 acpi_format_exception(status)); 774 res = -EFAULT; 775 goto end; 776 } 777 778 if (resource->caps.units) { 779 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME 780 "Unknown units %llu.\n", 781 resource->caps.units); 782 res = -EINVAL; 783 goto end; 784 } 785 786 /* Grab the string data */ 787 str = &resource->model_number; 788 789 for (i = 11; i < 14; i++) { 790 union acpi_object *element = &pss->package.elements[i]; 791 792 if (element->type != ACPI_TYPE_STRING) { 793 res = -EINVAL; 794 goto error; 795 } 796 797 *str = kmemdup_nul(element->string.pointer, element->string.length, 798 GFP_KERNEL); 799 if (!*str) { 800 res = -ENOMEM; 801 goto error; 802 } 803 804 str++; 805 } 806 807 dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n"); 808 goto end; 809 error: 810 free_capabilities(resource); 811 end: 812 kfree(buffer.pointer); 813 return res; 814 } 815 816 /* Handle ACPI event notifications */ 817 static 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 guard(mutex)(&acpi_notify_lock); 828 829 switch (event) { 830 case METER_NOTIFY_CONFIG: 831 if (!IS_ERR(resource->hwmon_dev)) 832 hwmon_device_unregister(resource->hwmon_dev); 833 834 mutex_lock(&resource->lock); 835 836 free_capabilities(resource); 837 remove_domain_devices(resource); 838 res = read_capabilities(resource); 839 if (res) 840 dev_err_once(&device->dev, "read capabilities failed.\n"); 841 res = read_domain_devices(resource); 842 if (res && res != -ENODEV) 843 dev_err_once(&device->dev, "read domain devices failed.\n"); 844 845 mutex_unlock(&resource->lock); 846 847 resource->hwmon_dev = 848 hwmon_device_register_with_info(&device->dev, 849 ACPI_POWER_METER_NAME, 850 resource, 851 &power_meter_chip_info, 852 power_extra_groups); 853 if (IS_ERR(resource->hwmon_dev)) 854 dev_err_once(&device->dev, "register hwmon device failed.\n"); 855 856 break; 857 case METER_NOTIFY_TRIP: 858 sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME); 859 break; 860 case METER_NOTIFY_CAP: 861 mutex_lock(&resource->lock); 862 res = update_cap(resource); 863 if (res) 864 dev_err_once(&device->dev, "update cap failed when capping value is changed.\n"); 865 mutex_unlock(&resource->lock); 866 sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME); 867 break; 868 case METER_NOTIFY_INTERVAL: 869 sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME); 870 break; 871 case METER_NOTIFY_CAPPING: 872 mutex_lock(&resource->lock); 873 resource->power_alarm = true; 874 mutex_unlock(&resource->lock); 875 sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME); 876 dev_info(&device->dev, "Capping in progress.\n"); 877 break; 878 default: 879 WARN(1, "Unexpected event %d\n", event); 880 break; 881 } 882 883 acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS, 884 dev_name(&device->dev), event, 0); 885 } 886 887 static int acpi_power_meter_add(struct acpi_device *device) 888 { 889 int res; 890 struct acpi_power_meter_resource *resource; 891 892 if (!device) 893 return -EINVAL; 894 895 resource = kzalloc_obj(*resource); 896 if (!resource) 897 return -ENOMEM; 898 899 resource->sensors_valid = 0; 900 resource->acpi_dev = device; 901 mutex_init(&resource->lock); 902 strscpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME); 903 strscpy(acpi_device_class(device), ACPI_POWER_METER_CLASS); 904 device->driver_data = resource; 905 906 #if IS_REACHABLE(CONFIG_ACPI_IPMI) 907 /* 908 * On Dell systems several methods of acpi_power_meter access 909 * variables in IPMI region, so wait until IPMI space handler is 910 * installed by acpi_ipmi and also wait until SMI is selected to make 911 * the space handler fully functional. 912 */ 913 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.")) { 914 struct acpi_device *ipi_device = acpi_dev_get_first_match_dev("IPI0001", NULL, -1); 915 916 if (ipi_device && acpi_wait_for_acpi_ipmi()) 917 dev_warn(&device->dev, "Waiting for ACPI IPMI timeout"); 918 acpi_dev_put(ipi_device); 919 } 920 #endif 921 922 res = read_capabilities(resource); 923 if (res) 924 goto exit_free; 925 926 resource->trip[0] = -1; 927 resource->trip[1] = -1; 928 929 /* _PMD method is optional. */ 930 res = read_domain_devices(resource); 931 if (res && res != -ENODEV) 932 goto exit_free_capability; 933 934 resource->hwmon_dev = 935 hwmon_device_register_with_info(&device->dev, 936 ACPI_POWER_METER_NAME, resource, 937 &power_meter_chip_info, 938 power_extra_groups); 939 if (IS_ERR(resource->hwmon_dev)) { 940 res = PTR_ERR(resource->hwmon_dev); 941 goto exit_remove; 942 } 943 944 res = 0; 945 goto exit; 946 947 exit_remove: 948 remove_domain_devices(resource); 949 exit_free_capability: 950 free_capabilities(resource); 951 exit_free: 952 kfree(resource); 953 exit: 954 return res; 955 } 956 957 static void acpi_power_meter_remove(struct acpi_device *device) 958 { 959 struct acpi_power_meter_resource *resource; 960 961 if (!device || !acpi_driver_data(device)) 962 return; 963 964 resource = acpi_driver_data(device); 965 if (!IS_ERR(resource->hwmon_dev)) 966 hwmon_device_unregister(resource->hwmon_dev); 967 968 remove_domain_devices(resource); 969 free_capabilities(resource); 970 971 kfree(resource); 972 } 973 974 static int acpi_power_meter_resume(struct device *dev) 975 { 976 struct acpi_power_meter_resource *resource; 977 978 if (!dev) 979 return -EINVAL; 980 981 resource = acpi_driver_data(to_acpi_device(dev)); 982 if (!resource) 983 return -EINVAL; 984 985 free_capabilities(resource); 986 read_capabilities(resource); 987 988 return 0; 989 } 990 991 static DEFINE_SIMPLE_DEV_PM_OPS(acpi_power_meter_pm, NULL, 992 acpi_power_meter_resume); 993 994 static struct acpi_driver acpi_power_meter_driver = { 995 .name = "power_meter", 996 .class = ACPI_POWER_METER_CLASS, 997 .ids = power_meter_ids, 998 .ops = { 999 .add = acpi_power_meter_add, 1000 .remove = acpi_power_meter_remove, 1001 .notify = acpi_power_meter_notify, 1002 }, 1003 .drv.pm = pm_sleep_ptr(&acpi_power_meter_pm), 1004 }; 1005 1006 /* Module init/exit routines */ 1007 static int __init enable_cap_knobs(const struct dmi_system_id *d) 1008 { 1009 cap_in_hardware = 1; 1010 return 0; 1011 } 1012 1013 static const struct dmi_system_id pm_dmi_table[] __initconst = { 1014 { 1015 enable_cap_knobs, "IBM Active Energy Manager", 1016 { 1017 DMI_MATCH(DMI_SYS_VENDOR, "IBM") 1018 }, 1019 }, 1020 {} 1021 }; 1022 1023 static int __init acpi_power_meter_init(void) 1024 { 1025 int result; 1026 1027 if (acpi_disabled) 1028 return -ENODEV; 1029 1030 dmi_check_system(pm_dmi_table); 1031 1032 result = acpi_bus_register_driver(&acpi_power_meter_driver); 1033 if (result < 0) 1034 return result; 1035 1036 return 0; 1037 } 1038 1039 static void __exit acpi_power_meter_exit(void) 1040 { 1041 acpi_bus_unregister_driver(&acpi_power_meter_driver); 1042 } 1043 1044 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>"); 1045 MODULE_DESCRIPTION("ACPI 4.0 power meter driver"); 1046 MODULE_LICENSE("GPL"); 1047 1048 module_param(force_cap_on, bool, 0644); 1049 MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so."); 1050 1051 module_init(acpi_power_meter_init); 1052 module_exit(acpi_power_meter_exit); 1053