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