1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * drivers/acpi/power.c - ACPI Power Resources management. 4 * 5 * Copyright (C) 2001 - 2015 Intel Corp. 6 * Author: Andy Grover <andrew.grover@intel.com> 7 * Author: Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 8 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 9 */ 10 11 /* 12 * ACPI power-managed devices may be controlled in two ways: 13 * 1. via "Device Specific (D-State) Control" 14 * 2. via "Power Resource Control". 15 * The code below deals with ACPI Power Resources control. 16 * 17 * An ACPI "power resource object" represents a software controllable power 18 * plane, clock plane, or other resource depended on by a device. 19 * 20 * A device may rely on multiple power resources, and a power resource 21 * may be shared by multiple devices. 22 */ 23 24 #define pr_fmt(fmt) "ACPI: PM: " fmt 25 26 #include <linux/dmi.h> 27 #include <linux/kernel.h> 28 #include <linux/module.h> 29 #include <linux/init.h> 30 #include <linux/types.h> 31 #include <linux/slab.h> 32 #include <linux/string_choices.h> 33 #include <linux/pm_runtime.h> 34 #include <linux/sysfs.h> 35 #include <linux/acpi.h> 36 #include "sleep.h" 37 #include "internal.h" 38 39 #define ACPI_POWER_CLASS "power_resource" 40 #define ACPI_POWER_DEVICE_NAME "Power Resource" 41 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00 42 #define ACPI_POWER_RESOURCE_STATE_ON 0x01 43 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF 44 45 struct acpi_power_dependent_device { 46 struct device *dev; 47 struct list_head node; 48 }; 49 50 struct acpi_power_resource { 51 struct acpi_device device; 52 struct list_head list_node; 53 u32 system_level; 54 u32 order; 55 unsigned int ref_count; 56 u8 state; 57 struct mutex resource_lock; 58 struct list_head dependents; 59 }; 60 61 struct acpi_power_resource_entry { 62 struct list_head node; 63 struct acpi_power_resource *resource; 64 }; 65 66 static LIST_HEAD(acpi_power_resource_list); 67 static DEFINE_MUTEX(power_resource_list_lock); 68 69 /* -------------------------------------------------------------------------- 70 Power Resource Management 71 -------------------------------------------------------------------------- */ 72 73 static inline const char *resource_dev_name(struct acpi_power_resource *pr) 74 { 75 return dev_name(&pr->device.dev); 76 } 77 78 static inline 79 struct acpi_power_resource *to_power_resource(struct acpi_device *device) 80 { 81 return container_of(device, struct acpi_power_resource, device); 82 } 83 84 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) 85 { 86 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 87 88 if (!device) 89 return NULL; 90 91 return to_power_resource(device); 92 } 93 94 static int acpi_power_resources_list_add(acpi_handle handle, 95 struct list_head *list) 96 { 97 struct acpi_power_resource *resource = acpi_power_get_context(handle); 98 struct acpi_power_resource_entry *entry; 99 100 if (!resource || !list) 101 return -EINVAL; 102 103 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 104 if (!entry) 105 return -ENOMEM; 106 107 entry->resource = resource; 108 if (!list_empty(list)) { 109 struct acpi_power_resource_entry *e; 110 111 list_for_each_entry(e, list, node) 112 if (e->resource->order > resource->order) { 113 list_add_tail(&entry->node, &e->node); 114 return 0; 115 } 116 } 117 list_add_tail(&entry->node, list); 118 return 0; 119 } 120 121 void acpi_power_resources_list_free(struct list_head *list) 122 { 123 struct acpi_power_resource_entry *entry, *e; 124 125 list_for_each_entry_safe(entry, e, list, node) { 126 list_del(&entry->node); 127 kfree(entry); 128 } 129 } 130 131 static bool acpi_power_resource_is_dup(union acpi_object *package, 132 unsigned int start, unsigned int i) 133 { 134 acpi_handle rhandle, dup; 135 unsigned int j; 136 137 /* The caller is expected to check the package element types */ 138 rhandle = package->package.elements[i].reference.handle; 139 for (j = start; j < i; j++) { 140 dup = package->package.elements[j].reference.handle; 141 if (dup == rhandle) 142 return true; 143 } 144 145 return false; 146 } 147 148 int acpi_extract_power_resources(union acpi_object *package, unsigned int start, 149 struct list_head *list) 150 { 151 unsigned int i; 152 int err = 0; 153 154 for (i = start; i < package->package.count; i++) { 155 union acpi_object *element = &package->package.elements[i]; 156 struct acpi_device *rdev; 157 acpi_handle rhandle; 158 159 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { 160 err = -ENODATA; 161 break; 162 } 163 rhandle = element->reference.handle; 164 if (!rhandle) { 165 err = -ENODEV; 166 break; 167 } 168 169 /* Some ACPI tables contain duplicate power resource references */ 170 if (acpi_power_resource_is_dup(package, start, i)) 171 continue; 172 173 rdev = acpi_add_power_resource(rhandle); 174 if (!rdev) { 175 err = -ENODEV; 176 break; 177 } 178 err = acpi_power_resources_list_add(rhandle, list); 179 if (err) 180 break; 181 } 182 if (err) 183 acpi_power_resources_list_free(list); 184 185 return err; 186 } 187 188 static int __get_state(acpi_handle handle, u8 *state) 189 { 190 acpi_status status = AE_OK; 191 unsigned long long sta = 0; 192 u8 cur_state; 193 194 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); 195 if (ACPI_FAILURE(status)) 196 return -ENODEV; 197 198 cur_state = sta & ACPI_POWER_RESOURCE_STATE_ON; 199 200 acpi_handle_debug(handle, "Power resource is %s\n", 201 str_on_off(cur_state)); 202 203 *state = cur_state; 204 return 0; 205 } 206 207 static int acpi_power_get_state(struct acpi_power_resource *resource, u8 *state) 208 { 209 if (resource->state == ACPI_POWER_RESOURCE_STATE_UNKNOWN) { 210 int ret; 211 212 ret = __get_state(resource->device.handle, &resource->state); 213 if (ret) 214 return ret; 215 } 216 217 *state = resource->state; 218 return 0; 219 } 220 221 static int acpi_power_get_list_state(struct list_head *list, u8 *state) 222 { 223 struct acpi_power_resource_entry *entry; 224 u8 cur_state = ACPI_POWER_RESOURCE_STATE_OFF; 225 226 if (!list || !state) 227 return -EINVAL; 228 229 /* The state of the list is 'on' IFF all resources are 'on'. */ 230 list_for_each_entry(entry, list, node) { 231 struct acpi_power_resource *resource = entry->resource; 232 int result; 233 234 mutex_lock(&resource->resource_lock); 235 result = acpi_power_get_state(resource, &cur_state); 236 mutex_unlock(&resource->resource_lock); 237 if (result) 238 return result; 239 240 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON) 241 break; 242 } 243 244 pr_debug("Power resource list is %s\n", str_on_off(cur_state)); 245 246 *state = cur_state; 247 return 0; 248 } 249 250 static int 251 acpi_power_resource_add_dependent(struct acpi_power_resource *resource, 252 struct device *dev) 253 { 254 struct acpi_power_dependent_device *dep; 255 int ret = 0; 256 257 mutex_lock(&resource->resource_lock); 258 list_for_each_entry(dep, &resource->dependents, node) { 259 /* Only add it once */ 260 if (dep->dev == dev) 261 goto unlock; 262 } 263 264 dep = kzalloc(sizeof(*dep), GFP_KERNEL); 265 if (!dep) { 266 ret = -ENOMEM; 267 goto unlock; 268 } 269 270 dep->dev = dev; 271 list_add_tail(&dep->node, &resource->dependents); 272 dev_dbg(dev, "added power dependency to [%s]\n", 273 resource_dev_name(resource)); 274 275 unlock: 276 mutex_unlock(&resource->resource_lock); 277 return ret; 278 } 279 280 static void 281 acpi_power_resource_remove_dependent(struct acpi_power_resource *resource, 282 struct device *dev) 283 { 284 struct acpi_power_dependent_device *dep; 285 286 mutex_lock(&resource->resource_lock); 287 list_for_each_entry(dep, &resource->dependents, node) { 288 if (dep->dev == dev) { 289 list_del(&dep->node); 290 kfree(dep); 291 dev_dbg(dev, "removed power dependency to [%s]\n", 292 resource_dev_name(resource)); 293 break; 294 } 295 } 296 mutex_unlock(&resource->resource_lock); 297 } 298 299 /** 300 * acpi_device_power_add_dependent - Add dependent device of this ACPI device 301 * @adev: ACPI device pointer 302 * @dev: Dependent device 303 * 304 * If @adev has non-empty _PR0 the @dev is added as dependent device to all 305 * power resources returned by it. This means that whenever these power 306 * resources are turned _ON the dependent devices get runtime resumed. This 307 * is needed for devices such as PCI to allow its driver to re-initialize 308 * it after it went to D0uninitialized. 309 * 310 * If @adev does not have _PR0 this does nothing. 311 * 312 * Returns %0 in case of success and negative errno otherwise. 313 */ 314 int acpi_device_power_add_dependent(struct acpi_device *adev, 315 struct device *dev) 316 { 317 struct acpi_power_resource_entry *entry; 318 struct list_head *resources; 319 int ret; 320 321 if (!adev->flags.power_manageable) 322 return 0; 323 324 resources = &adev->power.states[ACPI_STATE_D0].resources; 325 list_for_each_entry(entry, resources, node) { 326 ret = acpi_power_resource_add_dependent(entry->resource, dev); 327 if (ret) 328 goto err; 329 } 330 331 return 0; 332 333 err: 334 list_for_each_entry(entry, resources, node) 335 acpi_power_resource_remove_dependent(entry->resource, dev); 336 337 return ret; 338 } 339 340 /** 341 * acpi_device_power_remove_dependent - Remove dependent device 342 * @adev: ACPI device pointer 343 * @dev: Dependent device 344 * 345 * Does the opposite of acpi_device_power_add_dependent() and removes the 346 * dependent device if it is found. Can be called to @adev that does not 347 * have _PR0 as well. 348 */ 349 void acpi_device_power_remove_dependent(struct acpi_device *adev, 350 struct device *dev) 351 { 352 struct acpi_power_resource_entry *entry; 353 struct list_head *resources; 354 355 if (!adev->flags.power_manageable) 356 return; 357 358 resources = &adev->power.states[ACPI_STATE_D0].resources; 359 list_for_each_entry_reverse(entry, resources, node) 360 acpi_power_resource_remove_dependent(entry->resource, dev); 361 } 362 363 static int __acpi_power_on(struct acpi_power_resource *resource) 364 { 365 acpi_handle handle = resource->device.handle; 366 struct acpi_power_dependent_device *dep; 367 acpi_status status = AE_OK; 368 369 status = acpi_evaluate_object(handle, "_ON", NULL, NULL); 370 if (ACPI_FAILURE(status)) { 371 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN; 372 return -ENODEV; 373 } 374 375 resource->state = ACPI_POWER_RESOURCE_STATE_ON; 376 377 acpi_handle_debug(handle, "Power resource turned on\n"); 378 379 /* 380 * If there are other dependents on this power resource we need to 381 * resume them now so that their drivers can re-initialize the 382 * hardware properly after it went back to D0. 383 */ 384 if (list_empty(&resource->dependents) || 385 list_is_singular(&resource->dependents)) 386 return 0; 387 388 list_for_each_entry(dep, &resource->dependents, node) { 389 dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n", 390 resource_dev_name(resource)); 391 pm_request_resume(dep->dev); 392 } 393 394 return 0; 395 } 396 397 static int acpi_power_on_unlocked(struct acpi_power_resource *resource) 398 { 399 int result = 0; 400 401 if (resource->ref_count++) { 402 acpi_handle_debug(resource->device.handle, 403 "Power resource already on\n"); 404 } else { 405 result = __acpi_power_on(resource); 406 if (result) 407 resource->ref_count--; 408 } 409 return result; 410 } 411 412 static int acpi_power_on(struct acpi_power_resource *resource) 413 { 414 int result; 415 416 mutex_lock(&resource->resource_lock); 417 result = acpi_power_on_unlocked(resource); 418 mutex_unlock(&resource->resource_lock); 419 return result; 420 } 421 422 static int __acpi_power_off(struct acpi_power_resource *resource) 423 { 424 acpi_handle handle = resource->device.handle; 425 acpi_status status; 426 427 status = acpi_evaluate_object(handle, "_OFF", NULL, NULL); 428 if (ACPI_FAILURE(status)) { 429 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN; 430 return -ENODEV; 431 } 432 433 resource->state = ACPI_POWER_RESOURCE_STATE_OFF; 434 435 acpi_handle_debug(handle, "Power resource turned off\n"); 436 437 return 0; 438 } 439 440 static int acpi_power_off_unlocked(struct acpi_power_resource *resource) 441 { 442 int result = 0; 443 444 if (!resource->ref_count) { 445 acpi_handle_debug(resource->device.handle, 446 "Power resource already off\n"); 447 return 0; 448 } 449 450 if (--resource->ref_count) { 451 acpi_handle_debug(resource->device.handle, 452 "Power resource still in use\n"); 453 } else { 454 result = __acpi_power_off(resource); 455 if (result) 456 resource->ref_count++; 457 } 458 return result; 459 } 460 461 static int acpi_power_off(struct acpi_power_resource *resource) 462 { 463 int result; 464 465 mutex_lock(&resource->resource_lock); 466 result = acpi_power_off_unlocked(resource); 467 mutex_unlock(&resource->resource_lock); 468 return result; 469 } 470 471 static int acpi_power_off_list(struct list_head *list) 472 { 473 struct acpi_power_resource_entry *entry; 474 int result = 0; 475 476 list_for_each_entry_reverse(entry, list, node) { 477 result = acpi_power_off(entry->resource); 478 if (result) 479 goto err; 480 } 481 return 0; 482 483 err: 484 list_for_each_entry_continue(entry, list, node) 485 acpi_power_on(entry->resource); 486 487 return result; 488 } 489 490 static int acpi_power_on_list(struct list_head *list) 491 { 492 struct acpi_power_resource_entry *entry; 493 int result = 0; 494 495 list_for_each_entry(entry, list, node) { 496 result = acpi_power_on(entry->resource); 497 if (result) 498 goto err; 499 } 500 return 0; 501 502 err: 503 list_for_each_entry_continue_reverse(entry, list, node) 504 acpi_power_off(entry->resource); 505 506 return result; 507 } 508 509 static struct attribute *attrs[] = { 510 NULL, 511 }; 512 513 static const struct attribute_group attr_groups[] = { 514 [ACPI_STATE_D0] = { 515 .name = "power_resources_D0", 516 .attrs = attrs, 517 }, 518 [ACPI_STATE_D1] = { 519 .name = "power_resources_D1", 520 .attrs = attrs, 521 }, 522 [ACPI_STATE_D2] = { 523 .name = "power_resources_D2", 524 .attrs = attrs, 525 }, 526 [ACPI_STATE_D3_HOT] = { 527 .name = "power_resources_D3hot", 528 .attrs = attrs, 529 }, 530 }; 531 532 static const struct attribute_group wakeup_attr_group = { 533 .name = "power_resources_wakeup", 534 .attrs = attrs, 535 }; 536 537 static void acpi_power_hide_list(struct acpi_device *adev, 538 struct list_head *resources, 539 const struct attribute_group *attr_group) 540 { 541 struct acpi_power_resource_entry *entry; 542 543 if (list_empty(resources)) 544 return; 545 546 list_for_each_entry_reverse(entry, resources, node) { 547 struct acpi_device *res_dev = &entry->resource->device; 548 549 sysfs_remove_link_from_group(&adev->dev.kobj, 550 attr_group->name, 551 dev_name(&res_dev->dev)); 552 } 553 sysfs_remove_group(&adev->dev.kobj, attr_group); 554 } 555 556 static void acpi_power_expose_list(struct acpi_device *adev, 557 struct list_head *resources, 558 const struct attribute_group *attr_group) 559 { 560 struct acpi_power_resource_entry *entry; 561 int ret; 562 563 if (list_empty(resources)) 564 return; 565 566 ret = sysfs_create_group(&adev->dev.kobj, attr_group); 567 if (ret) 568 return; 569 570 list_for_each_entry(entry, resources, node) { 571 struct acpi_device *res_dev = &entry->resource->device; 572 573 ret = sysfs_add_link_to_group(&adev->dev.kobj, 574 attr_group->name, 575 &res_dev->dev.kobj, 576 dev_name(&res_dev->dev)); 577 if (ret) { 578 acpi_power_hide_list(adev, resources, attr_group); 579 break; 580 } 581 } 582 } 583 584 static void acpi_power_expose_hide(struct acpi_device *adev, 585 struct list_head *resources, 586 const struct attribute_group *attr_group, 587 bool expose) 588 { 589 if (expose) 590 acpi_power_expose_list(adev, resources, attr_group); 591 else 592 acpi_power_hide_list(adev, resources, attr_group); 593 } 594 595 void acpi_power_add_remove_device(struct acpi_device *adev, bool add) 596 { 597 int state; 598 599 if (adev->wakeup.flags.valid) 600 acpi_power_expose_hide(adev, &adev->wakeup.resources, 601 &wakeup_attr_group, add); 602 603 if (!adev->power.flags.power_resources) 604 return; 605 606 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) 607 acpi_power_expose_hide(adev, 608 &adev->power.states[state].resources, 609 &attr_groups[state], add); 610 } 611 612 int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p) 613 { 614 struct acpi_power_resource_entry *entry; 615 int system_level = 5; 616 617 list_for_each_entry(entry, list, node) { 618 struct acpi_power_resource *resource = entry->resource; 619 u8 state; 620 621 mutex_lock(&resource->resource_lock); 622 623 /* 624 * Make sure that the power resource state and its reference 625 * counter value are consistent with each other. 626 */ 627 if (!resource->ref_count && 628 !acpi_power_get_state(resource, &state) && 629 state == ACPI_POWER_RESOURCE_STATE_ON) 630 __acpi_power_off(resource); 631 632 if (system_level > resource->system_level) 633 system_level = resource->system_level; 634 635 mutex_unlock(&resource->resource_lock); 636 } 637 *system_level_p = system_level; 638 return 0; 639 } 640 641 /* -------------------------------------------------------------------------- 642 Device Power Management 643 -------------------------------------------------------------------------- */ 644 645 /** 646 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in 647 * ACPI 3.0) _PSW (Power State Wake) 648 * @dev: Device to handle. 649 * @enable: 0 - disable, 1 - enable the wake capabilities of the device. 650 * @sleep_state: Target sleep state of the system. 651 * @dev_state: Target power state of the device. 652 * 653 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power 654 * State Wake) for the device, if present. On failure reset the device's 655 * wakeup.flags.valid flag. 656 * 657 * RETURN VALUE: 658 * 0 if either _DSW or _PSW has been successfully executed 659 * 0 if neither _DSW nor _PSW has been found 660 * -ENODEV if the execution of either _DSW or _PSW has failed 661 */ 662 int acpi_device_sleep_wake(struct acpi_device *dev, 663 int enable, int sleep_state, int dev_state) 664 { 665 union acpi_object in_arg[3]; 666 struct acpi_object_list arg_list = { 3, in_arg }; 667 acpi_status status = AE_OK; 668 669 /* 670 * Try to execute _DSW first. 671 * 672 * Three arguments are needed for the _DSW object: 673 * Argument 0: enable/disable the wake capabilities 674 * Argument 1: target system state 675 * Argument 2: target device state 676 * When _DSW object is called to disable the wake capabilities, maybe 677 * the first argument is filled. The values of the other two arguments 678 * are meaningless. 679 */ 680 in_arg[0].type = ACPI_TYPE_INTEGER; 681 in_arg[0].integer.value = enable; 682 in_arg[1].type = ACPI_TYPE_INTEGER; 683 in_arg[1].integer.value = sleep_state; 684 in_arg[2].type = ACPI_TYPE_INTEGER; 685 in_arg[2].integer.value = dev_state; 686 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL); 687 if (ACPI_SUCCESS(status)) { 688 return 0; 689 } else if (status != AE_NOT_FOUND) { 690 acpi_handle_info(dev->handle, "_DSW execution failed\n"); 691 dev->wakeup.flags.valid = 0; 692 return -ENODEV; 693 } 694 695 /* Execute _PSW */ 696 status = acpi_execute_simple_method(dev->handle, "_PSW", enable); 697 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { 698 acpi_handle_info(dev->handle, "_PSW execution failed\n"); 699 dev->wakeup.flags.valid = 0; 700 return -ENODEV; 701 } 702 703 return 0; 704 } 705 706 /* 707 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229): 708 * 1. Power on the power resources required for the wakeup device 709 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power 710 * State Wake) for the device, if present 711 */ 712 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state) 713 { 714 int err = 0; 715 716 if (!dev || !dev->wakeup.flags.valid) 717 return -EINVAL; 718 719 mutex_lock(&acpi_device_lock); 720 721 dev_dbg(&dev->dev, "Enabling wakeup power (count %d)\n", 722 dev->wakeup.prepare_count); 723 724 if (dev->wakeup.prepare_count++) 725 goto out; 726 727 err = acpi_power_on_list(&dev->wakeup.resources); 728 if (err) { 729 dev_err(&dev->dev, "Cannot turn on wakeup power resources\n"); 730 dev->wakeup.flags.valid = 0; 731 goto out; 732 } 733 734 /* 735 * Passing 3 as the third argument below means the device may be 736 * put into arbitrary power state afterward. 737 */ 738 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); 739 if (err) { 740 acpi_power_off_list(&dev->wakeup.resources); 741 dev->wakeup.prepare_count = 0; 742 goto out; 743 } 744 745 dev_dbg(&dev->dev, "Wakeup power enabled\n"); 746 747 out: 748 mutex_unlock(&acpi_device_lock); 749 return err; 750 } 751 752 /* 753 * Shutdown a wakeup device, counterpart of above method 754 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power 755 * State Wake) for the device, if present 756 * 2. Shutdown down the power resources 757 */ 758 int acpi_disable_wakeup_device_power(struct acpi_device *dev) 759 { 760 struct acpi_power_resource_entry *entry; 761 int err = 0; 762 763 if (!dev || !dev->wakeup.flags.valid) 764 return -EINVAL; 765 766 mutex_lock(&acpi_device_lock); 767 768 dev_dbg(&dev->dev, "Disabling wakeup power (count %d)\n", 769 dev->wakeup.prepare_count); 770 771 /* Do nothing if wakeup power has not been enabled for this device. */ 772 if (dev->wakeup.prepare_count <= 0) 773 goto out; 774 775 if (--dev->wakeup.prepare_count > 0) 776 goto out; 777 778 err = acpi_device_sleep_wake(dev, 0, 0, 0); 779 if (err) 780 goto out; 781 782 /* 783 * All of the power resources in the list need to be turned off even if 784 * there are errors. 785 */ 786 list_for_each_entry(entry, &dev->wakeup.resources, node) { 787 int ret; 788 789 ret = acpi_power_off(entry->resource); 790 if (ret && !err) 791 err = ret; 792 } 793 if (err) { 794 dev_err(&dev->dev, "Cannot turn off wakeup power resources\n"); 795 dev->wakeup.flags.valid = 0; 796 goto out; 797 } 798 799 dev_dbg(&dev->dev, "Wakeup power disabled\n"); 800 801 out: 802 mutex_unlock(&acpi_device_lock); 803 return err; 804 } 805 806 int acpi_power_get_inferred_state(struct acpi_device *device, int *state) 807 { 808 u8 list_state = ACPI_POWER_RESOURCE_STATE_OFF; 809 int result = 0; 810 int i = 0; 811 812 if (!device || !state) 813 return -EINVAL; 814 815 /* 816 * We know a device's inferred power state when all the resources 817 * required for a given D-state are 'on'. 818 */ 819 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { 820 struct list_head *list = &device->power.states[i].resources; 821 822 if (list_empty(list)) 823 continue; 824 825 result = acpi_power_get_list_state(list, &list_state); 826 if (result) 827 return result; 828 829 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) { 830 *state = i; 831 return 0; 832 } 833 } 834 835 *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ? 836 ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT; 837 return 0; 838 } 839 840 int acpi_power_on_resources(struct acpi_device *device, int state) 841 { 842 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT) 843 return -EINVAL; 844 845 return acpi_power_on_list(&device->power.states[state].resources); 846 } 847 848 int acpi_power_transition(struct acpi_device *device, int state) 849 { 850 int result = 0; 851 852 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) 853 return -EINVAL; 854 855 if (device->power.state == state || !device->flags.power_manageable) 856 return 0; 857 858 if ((device->power.state < ACPI_STATE_D0) 859 || (device->power.state > ACPI_STATE_D3_COLD)) 860 return -ENODEV; 861 862 /* 863 * First we reference all power resources required in the target list 864 * (e.g. so the device doesn't lose power while transitioning). Then, 865 * we dereference all power resources used in the current list. 866 */ 867 if (state < ACPI_STATE_D3_COLD) 868 result = acpi_power_on_list( 869 &device->power.states[state].resources); 870 871 if (!result && device->power.state < ACPI_STATE_D3_COLD) 872 acpi_power_off_list( 873 &device->power.states[device->power.state].resources); 874 875 /* We shouldn't change the state unless the above operations succeed. */ 876 device->power.state = result ? ACPI_STATE_UNKNOWN : state; 877 878 return result; 879 } 880 881 static void acpi_release_power_resource(struct device *dev) 882 { 883 struct acpi_device *device = to_acpi_device(dev); 884 struct acpi_power_resource *resource; 885 886 resource = container_of(device, struct acpi_power_resource, device); 887 888 mutex_lock(&power_resource_list_lock); 889 list_del(&resource->list_node); 890 mutex_unlock(&power_resource_list_lock); 891 892 acpi_free_pnp_ids(&device->pnp); 893 kfree(resource); 894 } 895 896 static ssize_t resource_in_use_show(struct device *dev, 897 struct device_attribute *attr, 898 char *buf) 899 { 900 struct acpi_power_resource *resource; 901 902 resource = to_power_resource(to_acpi_device(dev)); 903 return sprintf(buf, "%u\n", !!resource->ref_count); 904 } 905 static DEVICE_ATTR_RO(resource_in_use); 906 907 static void acpi_power_sysfs_remove(struct acpi_device *device) 908 { 909 device_remove_file(&device->dev, &dev_attr_resource_in_use); 910 } 911 912 static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource) 913 { 914 mutex_lock(&power_resource_list_lock); 915 916 if (!list_empty(&acpi_power_resource_list)) { 917 struct acpi_power_resource *r; 918 919 list_for_each_entry(r, &acpi_power_resource_list, list_node) 920 if (r->order > resource->order) { 921 list_add_tail(&resource->list_node, &r->list_node); 922 goto out; 923 } 924 } 925 list_add_tail(&resource->list_node, &acpi_power_resource_list); 926 927 out: 928 mutex_unlock(&power_resource_list_lock); 929 } 930 931 struct acpi_device *acpi_add_power_resource(acpi_handle handle) 932 { 933 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 934 struct acpi_power_resource *resource; 935 union acpi_object acpi_object; 936 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object }; 937 acpi_status status; 938 u8 state_dummy; 939 int result; 940 941 if (device) 942 return device; 943 944 resource = kzalloc(sizeof(*resource), GFP_KERNEL); 945 if (!resource) 946 return NULL; 947 948 device = &resource->device; 949 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER, 950 acpi_release_power_resource); 951 mutex_init(&resource->resource_lock); 952 INIT_LIST_HEAD(&resource->list_node); 953 INIT_LIST_HEAD(&resource->dependents); 954 strscpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); 955 strscpy(acpi_device_class(device), ACPI_POWER_CLASS); 956 device->power.state = ACPI_STATE_UNKNOWN; 957 device->flags.match_driver = true; 958 959 /* Evaluate the object to get the system level and resource order. */ 960 status = acpi_evaluate_object(handle, NULL, NULL, &buffer); 961 if (ACPI_FAILURE(status)) 962 goto err; 963 964 resource->system_level = acpi_object.power_resource.system_level; 965 resource->order = acpi_object.power_resource.resource_order; 966 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN; 967 968 /* Get the initial state or just flip it on if that fails. */ 969 if (acpi_power_get_state(resource, &state_dummy)) 970 __acpi_power_on(resource); 971 972 acpi_handle_info(handle, "New power resource\n"); 973 974 result = acpi_tie_acpi_dev(device); 975 if (result) 976 goto err; 977 978 result = acpi_device_add(device); 979 if (result) 980 goto err; 981 982 if (!device_create_file(&device->dev, &dev_attr_resource_in_use)) 983 device->remove = acpi_power_sysfs_remove; 984 985 acpi_power_add_resource_to_list(resource); 986 acpi_device_add_finalize(device); 987 return device; 988 989 err: 990 acpi_release_power_resource(&device->dev); 991 return NULL; 992 } 993 994 #ifdef CONFIG_ACPI_SLEEP 995 void acpi_resume_power_resources(void) 996 { 997 struct acpi_power_resource *resource; 998 999 mutex_lock(&power_resource_list_lock); 1000 1001 list_for_each_entry(resource, &acpi_power_resource_list, list_node) { 1002 int result; 1003 u8 state; 1004 1005 mutex_lock(&resource->resource_lock); 1006 1007 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN; 1008 result = acpi_power_get_state(resource, &state); 1009 if (result) { 1010 mutex_unlock(&resource->resource_lock); 1011 continue; 1012 } 1013 1014 if (state == ACPI_POWER_RESOURCE_STATE_OFF 1015 && resource->ref_count) { 1016 acpi_handle_debug(resource->device.handle, "Turning ON\n"); 1017 __acpi_power_on(resource); 1018 } 1019 1020 mutex_unlock(&resource->resource_lock); 1021 } 1022 1023 mutex_unlock(&power_resource_list_lock); 1024 } 1025 #endif 1026 1027 static const struct dmi_system_id dmi_leave_unused_power_resources_on[] = { 1028 { 1029 /* 1030 * The Toshiba Click Mini has a CPR3 power-resource which must 1031 * be on for the touchscreen to work, but which is not in any 1032 * _PR? lists. The other 2 affected power-resources are no-ops. 1033 */ 1034 .matches = { 1035 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"), 1036 DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"), 1037 }, 1038 }, 1039 {} 1040 }; 1041 1042 /** 1043 * acpi_turn_off_unused_power_resources - Turn off power resources not in use. 1044 */ 1045 void acpi_turn_off_unused_power_resources(void) 1046 { 1047 struct acpi_power_resource *resource; 1048 1049 if (dmi_check_system(dmi_leave_unused_power_resources_on)) 1050 return; 1051 1052 mutex_lock(&power_resource_list_lock); 1053 1054 list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) { 1055 mutex_lock(&resource->resource_lock); 1056 1057 if (!resource->ref_count && 1058 resource->state == ACPI_POWER_RESOURCE_STATE_ON) { 1059 acpi_handle_debug(resource->device.handle, "Turning OFF\n"); 1060 __acpi_power_off(resource); 1061 } 1062 1063 mutex_unlock(&resource->resource_lock); 1064 } 1065 1066 mutex_unlock(&power_resource_list_lock); 1067 } 1068