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