xref: /linux-6.15/drivers/acpi/device_pm.c (revision 62fcb99b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/acpi/device_pm.c - ACPI device power management routines.
4  *
5  * Copyright (C) 2012, Intel Corp.
6  * Author: Rafael J. Wysocki <[email protected]>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  */
12 
13 #define pr_fmt(fmt) "PM: " fmt
14 
15 #include <linux/acpi.h>
16 #include <linux/export.h>
17 #include <linux/mutex.h>
18 #include <linux/pm_qos.h>
19 #include <linux/pm_domain.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/suspend.h>
22 
23 #include "fan.h"
24 #include "internal.h"
25 
26 /**
27  * acpi_power_state_string - String representation of ACPI device power state.
28  * @state: ACPI device power state to return the string representation of.
29  */
30 const char *acpi_power_state_string(int state)
31 {
32 	switch (state) {
33 	case ACPI_STATE_D0:
34 		return "D0";
35 	case ACPI_STATE_D1:
36 		return "D1";
37 	case ACPI_STATE_D2:
38 		return "D2";
39 	case ACPI_STATE_D3_HOT:
40 		return "D3hot";
41 	case ACPI_STATE_D3_COLD:
42 		return "D3cold";
43 	default:
44 		return "(unknown)";
45 	}
46 }
47 
48 static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
49 {
50 	unsigned long long psc;
51 	acpi_status status;
52 
53 	status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
54 	if (ACPI_FAILURE(status))
55 		return -ENODEV;
56 
57 	*state = psc;
58 	return 0;
59 }
60 
61 /**
62  * acpi_device_get_power - Get power state of an ACPI device.
63  * @device: Device to get the power state of.
64  * @state: Place to store the power state of the device.
65  *
66  * This function does not update the device's power.state field, but it may
67  * update its parent's power.state field (when the parent's power state is
68  * unknown and the device's power state turns out to be D0).
69  *
70  * Also, it does not update power resource reference counters to ensure that
71  * the power state returned by it will be persistent and it may return a power
72  * state shallower than previously set by acpi_device_set_power() for @device
73  * (if that power state depends on any power resources).
74  */
75 int acpi_device_get_power(struct acpi_device *device, int *state)
76 {
77 	struct acpi_device *parent = acpi_dev_parent(device);
78 	int result = ACPI_STATE_UNKNOWN;
79 	int error;
80 
81 	if (!device || !state)
82 		return -EINVAL;
83 
84 	if (!device->flags.power_manageable) {
85 		/* TBD: Non-recursive algorithm for walking up hierarchy. */
86 		*state = parent ? parent->power.state : ACPI_STATE_D0;
87 		goto out;
88 	}
89 
90 	/*
91 	 * Get the device's power state from power resources settings and _PSC,
92 	 * if available.
93 	 */
94 	if (device->power.flags.power_resources) {
95 		error = acpi_power_get_inferred_state(device, &result);
96 		if (error)
97 			return error;
98 	}
99 	if (device->power.flags.explicit_get) {
100 		int psc;
101 
102 		error = acpi_dev_pm_explicit_get(device, &psc);
103 		if (error)
104 			return error;
105 
106 		/*
107 		 * The power resources settings may indicate a power state
108 		 * shallower than the actual power state of the device, because
109 		 * the same power resources may be referenced by other devices.
110 		 *
111 		 * For systems predating ACPI 4.0 we assume that D3hot is the
112 		 * deepest state that can be supported.
113 		 */
114 		if (psc > result && psc < ACPI_STATE_D3_COLD)
115 			result = psc;
116 		else if (result == ACPI_STATE_UNKNOWN)
117 			result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
118 	}
119 
120 	/*
121 	 * If we were unsure about the device parent's power state up to this
122 	 * point, the fact that the device is in D0 implies that the parent has
123 	 * to be in D0 too, except if ignore_parent is set.
124 	 */
125 	if (!device->power.flags.ignore_parent && parent &&
126 	    parent->power.state == ACPI_STATE_UNKNOWN &&
127 	    result == ACPI_STATE_D0)
128 		parent->power.state = ACPI_STATE_D0;
129 
130 	*state = result;
131 
132  out:
133 	acpi_handle_debug(device->handle, "Power state: %s\n",
134 			  acpi_power_state_string(*state));
135 
136 	return 0;
137 }
138 
139 static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
140 {
141 	if (adev->power.states[state].flags.explicit_set) {
142 		char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
143 		acpi_status status;
144 
145 		status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
146 		if (ACPI_FAILURE(status))
147 			return -ENODEV;
148 	}
149 	return 0;
150 }
151 
152 /**
153  * acpi_device_set_power - Set power state of an ACPI device.
154  * @device: Device to set the power state of.
155  * @state: New power state to set.
156  *
157  * Callers must ensure that the device is power manageable before using this
158  * function.
159  */
160 int acpi_device_set_power(struct acpi_device *device, int state)
161 {
162 	struct acpi_device *parent = acpi_dev_parent(device);
163 	int target_state = state;
164 	int result = 0;
165 
166 	if (!device || !device->flags.power_manageable
167 	    || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
168 		return -EINVAL;
169 
170 	acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
171 			  acpi_power_state_string(device->power.state),
172 			  acpi_power_state_string(state));
173 
174 	/* Make sure this is a valid target state */
175 
176 	/* There is a special case for D0 addressed below. */
177 	if (state > ACPI_STATE_D0 && state == device->power.state)
178 		goto no_change;
179 
180 	if (state == ACPI_STATE_D3_COLD) {
181 		/*
182 		 * For transitions to D3cold we need to execute _PS3 and then
183 		 * possibly drop references to the power resources in use.
184 		 */
185 		state = ACPI_STATE_D3_HOT;
186 		/* If D3cold is not supported, use D3hot as the target state. */
187 		if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
188 			target_state = state;
189 	} else if (!device->power.states[state].flags.valid) {
190 		acpi_handle_debug(device->handle, "Power state %s not supported\n",
191 				  acpi_power_state_string(state));
192 		return -ENODEV;
193 	}
194 
195 	if (!device->power.flags.ignore_parent && parent &&
196 	    state < parent->power.state) {
197 		acpi_handle_debug(device->handle,
198 				  "Cannot transition to %s for parent in %s\n",
199 				  acpi_power_state_string(state),
200 				  acpi_power_state_string(parent->power.state));
201 		return -ENODEV;
202 	}
203 
204 	/*
205 	 * Transition Power
206 	 * ----------------
207 	 * In accordance with ACPI 6, _PSx is executed before manipulating power
208 	 * resources, unless the target state is D0, in which case _PS0 is
209 	 * supposed to be executed after turning the power resources on.
210 	 */
211 	if (state > ACPI_STATE_D0) {
212 		/*
213 		 * According to ACPI 6, devices cannot go from lower-power
214 		 * (deeper) states to higher-power (shallower) states.
215 		 */
216 		if (state < device->power.state) {
217 			acpi_handle_debug(device->handle,
218 					  "Cannot transition from %s to %s\n",
219 					  acpi_power_state_string(device->power.state),
220 					  acpi_power_state_string(state));
221 			return -ENODEV;
222 		}
223 
224 		/*
225 		 * If the device goes from D3hot to D3cold, _PS3 has been
226 		 * evaluated for it already, so skip it in that case.
227 		 */
228 		if (device->power.state < ACPI_STATE_D3_HOT) {
229 			result = acpi_dev_pm_explicit_set(device, state);
230 			if (result)
231 				goto end;
232 		}
233 
234 		if (device->power.flags.power_resources)
235 			result = acpi_power_transition(device, target_state);
236 	} else {
237 		int cur_state = device->power.state;
238 
239 		if (device->power.flags.power_resources) {
240 			result = acpi_power_transition(device, ACPI_STATE_D0);
241 			if (result)
242 				goto end;
243 		}
244 
245 		if (cur_state == ACPI_STATE_D0) {
246 			int psc;
247 
248 			/* Nothing to do here if _PSC is not present. */
249 			if (!device->power.flags.explicit_get)
250 				goto no_change;
251 
252 			/*
253 			 * The power state of the device was set to D0 last
254 			 * time, but that might have happened before a
255 			 * system-wide transition involving the platform
256 			 * firmware, so it may be necessary to evaluate _PS0
257 			 * for the device here.  However, use extra care here
258 			 * and evaluate _PSC to check the device's current power
259 			 * state, and only invoke _PS0 if the evaluation of _PSC
260 			 * is successful and it returns a power state different
261 			 * from D0.
262 			 */
263 			result = acpi_dev_pm_explicit_get(device, &psc);
264 			if (result || psc == ACPI_STATE_D0)
265 				goto no_change;
266 		}
267 
268 		result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
269 	}
270 
271 end:
272 	if (result) {
273 		acpi_handle_debug(device->handle,
274 				  "Failed to change power state to %s\n",
275 				  acpi_power_state_string(target_state));
276 	} else {
277 		device->power.state = target_state;
278 		acpi_handle_debug(device->handle, "Power state changed to %s\n",
279 				  acpi_power_state_string(target_state));
280 	}
281 
282 	return result;
283 
284 no_change:
285 	acpi_handle_debug(device->handle, "Already in %s\n",
286 			  acpi_power_state_string(state));
287 	return 0;
288 }
289 EXPORT_SYMBOL(acpi_device_set_power);
290 
291 int acpi_bus_set_power(acpi_handle handle, int state)
292 {
293 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
294 
295 	if (device)
296 		return acpi_device_set_power(device, state);
297 
298 	return -ENODEV;
299 }
300 EXPORT_SYMBOL(acpi_bus_set_power);
301 
302 int acpi_bus_init_power(struct acpi_device *device)
303 {
304 	int state;
305 	int result;
306 
307 	if (!device)
308 		return -EINVAL;
309 
310 	device->power.state = ACPI_STATE_UNKNOWN;
311 	if (!acpi_device_is_present(device)) {
312 		device->flags.initialized = false;
313 		return -ENXIO;
314 	}
315 
316 	result = acpi_device_get_power(device, &state);
317 	if (result)
318 		return result;
319 
320 	if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
321 		/* Reference count the power resources. */
322 		result = acpi_power_on_resources(device, state);
323 		if (result)
324 			return result;
325 
326 		if (state == ACPI_STATE_D0) {
327 			/*
328 			 * If _PSC is not present and the state inferred from
329 			 * power resources appears to be D0, it still may be
330 			 * necessary to execute _PS0 at this point, because
331 			 * another device using the same power resources may
332 			 * have been put into D0 previously and that's why we
333 			 * see D0 here.
334 			 */
335 			result = acpi_dev_pm_explicit_set(device, state);
336 			if (result)
337 				return result;
338 		}
339 	} else if (state == ACPI_STATE_UNKNOWN) {
340 		/*
341 		 * No power resources and missing _PSC?  Cross fingers and make
342 		 * it D0 in hope that this is what the BIOS put the device into.
343 		 * [We tried to force D0 here by executing _PS0, but that broke
344 		 * Toshiba P870-303 in a nasty way.]
345 		 */
346 		state = ACPI_STATE_D0;
347 	}
348 	device->power.state = state;
349 	return 0;
350 }
351 
352 /**
353  * acpi_device_fix_up_power - Force device with missing _PSC into D0.
354  * @device: Device object whose power state is to be fixed up.
355  *
356  * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
357  * are assumed to be put into D0 by the BIOS.  However, in some cases that may
358  * not be the case and this function should be used then.
359  */
360 int acpi_device_fix_up_power(struct acpi_device *device)
361 {
362 	int ret = 0;
363 
364 	if (!device->power.flags.power_resources
365 	    && !device->power.flags.explicit_get
366 	    && device->power.state == ACPI_STATE_D0)
367 		ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
368 
369 	return ret;
370 }
371 EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
372 
373 static int fix_up_power_if_applicable(struct acpi_device *adev, void *not_used)
374 {
375 	if (adev->status.present && adev->status.enabled)
376 		acpi_device_fix_up_power(adev);
377 
378 	return 0;
379 }
380 
381 /**
382  * acpi_device_fix_up_power_extended - Force device and its children into D0.
383  * @adev: Parent device object whose power state is to be fixed up.
384  *
385  * Call acpi_device_fix_up_power() for @adev and its children so long as they
386  * are reported as present and enabled.
387  */
388 void acpi_device_fix_up_power_extended(struct acpi_device *adev)
389 {
390 	acpi_device_fix_up_power(adev);
391 	acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL);
392 }
393 EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_extended);
394 
395 int acpi_device_update_power(struct acpi_device *device, int *state_p)
396 {
397 	int state;
398 	int result;
399 
400 	if (device->power.state == ACPI_STATE_UNKNOWN) {
401 		result = acpi_bus_init_power(device);
402 		if (!result && state_p)
403 			*state_p = device->power.state;
404 
405 		return result;
406 	}
407 
408 	result = acpi_device_get_power(device, &state);
409 	if (result)
410 		return result;
411 
412 	if (state == ACPI_STATE_UNKNOWN) {
413 		state = ACPI_STATE_D0;
414 		result = acpi_device_set_power(device, state);
415 		if (result)
416 			return result;
417 	} else {
418 		if (device->power.flags.power_resources) {
419 			/*
420 			 * We don't need to really switch the state, bu we need
421 			 * to update the power resources' reference counters.
422 			 */
423 			result = acpi_power_transition(device, state);
424 			if (result)
425 				return result;
426 		}
427 		device->power.state = state;
428 	}
429 	if (state_p)
430 		*state_p = state;
431 
432 	return 0;
433 }
434 EXPORT_SYMBOL_GPL(acpi_device_update_power);
435 
436 int acpi_bus_update_power(acpi_handle handle, int *state_p)
437 {
438 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
439 
440 	if (device)
441 		return acpi_device_update_power(device, state_p);
442 
443 	return -ENODEV;
444 }
445 EXPORT_SYMBOL_GPL(acpi_bus_update_power);
446 
447 bool acpi_bus_power_manageable(acpi_handle handle)
448 {
449 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
450 
451 	return device && device->flags.power_manageable;
452 }
453 EXPORT_SYMBOL(acpi_bus_power_manageable);
454 
455 static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used)
456 {
457 	if (!(adev->flags.power_manageable && adev->pnp.type.bus_address))
458 		return 0;
459 
460 	acpi_handle_debug(adev->handle, "Power state: %s\n",
461 			  acpi_power_state_string(adev->power.state));
462 
463 	if (adev->power.state == ACPI_STATE_D3_COLD)
464 		return acpi_device_set_power(adev, ACPI_STATE_D0);
465 
466 	return 0;
467 }
468 
469 /**
470  * acpi_dev_power_up_children_with_adr - Power up childres with valid _ADR
471  * @adev: Parent ACPI device object.
472  *
473  * Change the power states of the direct children of @adev that are in D3cold
474  * and hold valid _ADR objects to D0 in order to allow bus (e.g. PCI)
475  * enumeration code to access them.
476  */
477 void acpi_dev_power_up_children_with_adr(struct acpi_device *adev)
478 {
479 	acpi_dev_for_each_child(adev, acpi_power_up_if_adr_present, NULL);
480 }
481 
482 #ifdef CONFIG_PM
483 static DEFINE_MUTEX(acpi_pm_notifier_lock);
484 static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
485 
486 void acpi_pm_wakeup_event(struct device *dev)
487 {
488 	pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
489 }
490 EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
491 
492 static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
493 {
494 	struct acpi_device *adev;
495 
496 	if (val != ACPI_NOTIFY_DEVICE_WAKE)
497 		return;
498 
499 	acpi_handle_debug(handle, "Wake notify\n");
500 
501 	adev = acpi_get_acpi_dev(handle);
502 	if (!adev)
503 		return;
504 
505 	mutex_lock(&acpi_pm_notifier_lock);
506 
507 	if (adev->wakeup.flags.notifier_present) {
508 		pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
509 		if (adev->wakeup.context.func) {
510 			acpi_handle_debug(handle, "Running %pS for %s\n",
511 					  adev->wakeup.context.func,
512 					  dev_name(adev->wakeup.context.dev));
513 			adev->wakeup.context.func(&adev->wakeup.context);
514 		}
515 	}
516 
517 	mutex_unlock(&acpi_pm_notifier_lock);
518 
519 	acpi_put_acpi_dev(adev);
520 }
521 
522 /**
523  * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
524  * @adev: ACPI device to add the notify handler for.
525  * @dev: Device to generate a wakeup event for while handling the notification.
526  * @func: Work function to execute when handling the notification.
527  *
528  * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
529  * PM wakeup events.  For example, wakeup events may be generated for bridges
530  * if one of the devices below the bridge is signaling wakeup, even if the
531  * bridge itself doesn't have a wakeup GPE associated with it.
532  */
533 acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
534 			void (*func)(struct acpi_device_wakeup_context *context))
535 {
536 	acpi_status status = AE_ALREADY_EXISTS;
537 
538 	if (!dev && !func)
539 		return AE_BAD_PARAMETER;
540 
541 	mutex_lock(&acpi_pm_notifier_install_lock);
542 
543 	if (adev->wakeup.flags.notifier_present)
544 		goto out;
545 
546 	status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
547 					     acpi_pm_notify_handler, NULL);
548 	if (ACPI_FAILURE(status))
549 		goto out;
550 
551 	mutex_lock(&acpi_pm_notifier_lock);
552 	adev->wakeup.ws = wakeup_source_register(&adev->dev,
553 						 dev_name(&adev->dev));
554 	adev->wakeup.context.dev = dev;
555 	adev->wakeup.context.func = func;
556 	adev->wakeup.flags.notifier_present = true;
557 	mutex_unlock(&acpi_pm_notifier_lock);
558 
559  out:
560 	mutex_unlock(&acpi_pm_notifier_install_lock);
561 	return status;
562 }
563 
564 /**
565  * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
566  * @adev: ACPI device to remove the notifier from.
567  */
568 acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
569 {
570 	acpi_status status = AE_BAD_PARAMETER;
571 
572 	mutex_lock(&acpi_pm_notifier_install_lock);
573 
574 	if (!adev->wakeup.flags.notifier_present)
575 		goto out;
576 
577 	status = acpi_remove_notify_handler(adev->handle,
578 					    ACPI_SYSTEM_NOTIFY,
579 					    acpi_pm_notify_handler);
580 	if (ACPI_FAILURE(status))
581 		goto out;
582 
583 	mutex_lock(&acpi_pm_notifier_lock);
584 	adev->wakeup.context.func = NULL;
585 	adev->wakeup.context.dev = NULL;
586 	wakeup_source_unregister(adev->wakeup.ws);
587 	adev->wakeup.flags.notifier_present = false;
588 	mutex_unlock(&acpi_pm_notifier_lock);
589 
590  out:
591 	mutex_unlock(&acpi_pm_notifier_install_lock);
592 	return status;
593 }
594 
595 bool acpi_bus_can_wakeup(acpi_handle handle)
596 {
597 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
598 
599 	return device && device->wakeup.flags.valid;
600 }
601 EXPORT_SYMBOL(acpi_bus_can_wakeup);
602 
603 bool acpi_pm_device_can_wakeup(struct device *dev)
604 {
605 	struct acpi_device *adev = ACPI_COMPANION(dev);
606 
607 	return adev ? acpi_device_can_wakeup(adev) : false;
608 }
609 
610 /**
611  * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
612  * @dev: Device whose preferred target power state to return.
613  * @adev: ACPI device node corresponding to @dev.
614  * @target_state: System state to match the resultant device state.
615  * @d_min_p: Location to store the highest power state available to the device.
616  * @d_max_p: Location to store the lowest power state available to the device.
617  *
618  * Find the lowest power (highest number) and highest power (lowest number) ACPI
619  * device power states that the device can be in while the system is in the
620  * state represented by @target_state.  Store the integer numbers representing
621  * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
622  * respectively.
623  *
624  * Callers must ensure that @dev and @adev are valid pointers and that @adev
625  * actually corresponds to @dev before using this function.
626  *
627  * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
628  * returns a value that doesn't make sense.  The memory locations pointed to by
629  * @d_max_p and @d_min_p are only modified on success.
630  */
631 static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
632 				 u32 target_state, int *d_min_p, int *d_max_p)
633 {
634 	char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
635 	acpi_handle handle = adev->handle;
636 	unsigned long long ret;
637 	int d_min, d_max;
638 	bool wakeup = false;
639 	bool has_sxd = false;
640 	acpi_status status;
641 
642 	/*
643 	 * If the system state is S0, the lowest power state the device can be
644 	 * in is D3cold, unless the device has _S0W and is supposed to signal
645 	 * wakeup, in which case the return value of _S0W has to be used as the
646 	 * lowest power state available to the device.
647 	 */
648 	d_min = ACPI_STATE_D0;
649 	d_max = ACPI_STATE_D3_COLD;
650 
651 	/*
652 	 * If present, _SxD methods return the minimum D-state (highest power
653 	 * state) we can use for the corresponding S-states.  Otherwise, the
654 	 * minimum D-state is D0 (ACPI 3.x).
655 	 */
656 	if (target_state > ACPI_STATE_S0) {
657 		/*
658 		 * We rely on acpi_evaluate_integer() not clobbering the integer
659 		 * provided if AE_NOT_FOUND is returned.
660 		 */
661 		ret = d_min;
662 		status = acpi_evaluate_integer(handle, method, NULL, &ret);
663 		if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
664 		    || ret > ACPI_STATE_D3_COLD)
665 			return -ENODATA;
666 
667 		/*
668 		 * We need to handle legacy systems where D3hot and D3cold are
669 		 * the same and 3 is returned in both cases, so fall back to
670 		 * D3cold if D3hot is not a valid state.
671 		 */
672 		if (!adev->power.states[ret].flags.valid) {
673 			if (ret == ACPI_STATE_D3_HOT)
674 				ret = ACPI_STATE_D3_COLD;
675 			else
676 				return -ENODATA;
677 		}
678 
679 		if (status == AE_OK)
680 			has_sxd = true;
681 
682 		d_min = ret;
683 		wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
684 			&& adev->wakeup.sleep_state >= target_state;
685 	} else {
686 		wakeup = adev->wakeup.flags.valid;
687 	}
688 
689 	/*
690 	 * If _PRW says we can wake up the system from the target sleep state,
691 	 * the D-state returned by _SxD is sufficient for that (we assume a
692 	 * wakeup-aware driver if wake is set).  Still, if _SxW exists
693 	 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
694 	 * can wake the system.  _S0W may be valid, too.
695 	 */
696 	if (wakeup) {
697 		method[3] = 'W';
698 		status = acpi_evaluate_integer(handle, method, NULL, &ret);
699 		if (status == AE_NOT_FOUND) {
700 			/* No _SxW. In this case, the ACPI spec says that we
701 			 * must not go into any power state deeper than the
702 			 * value returned from _SxD.
703 			 */
704 			if (has_sxd && target_state > ACPI_STATE_S0)
705 				d_max = d_min;
706 		} else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
707 			/* Fall back to D3cold if ret is not a valid state. */
708 			if (!adev->power.states[ret].flags.valid)
709 				ret = ACPI_STATE_D3_COLD;
710 
711 			d_max = ret > d_min ? ret : d_min;
712 		} else {
713 			return -ENODATA;
714 		}
715 	}
716 
717 	if (d_min_p)
718 		*d_min_p = d_min;
719 
720 	if (d_max_p)
721 		*d_max_p = d_max;
722 
723 	return 0;
724 }
725 
726 /**
727  * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
728  * @dev: Device whose preferred target power state to return.
729  * @d_min_p: Location to store the upper limit of the allowed states range.
730  * @d_max_in: Deepest low-power state to take into consideration.
731  * Return value: Preferred power state of the device on success, -ENODEV
732  * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
733  * incorrect, or -ENODATA on ACPI method failure.
734  *
735  * The caller must ensure that @dev is valid before using this function.
736  */
737 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
738 {
739 	struct acpi_device *adev;
740 	int ret, d_min, d_max;
741 
742 	if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
743 		return -EINVAL;
744 
745 	if (d_max_in > ACPI_STATE_D2) {
746 		enum pm_qos_flags_status stat;
747 
748 		stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
749 		if (stat == PM_QOS_FLAGS_ALL)
750 			d_max_in = ACPI_STATE_D2;
751 	}
752 
753 	adev = ACPI_COMPANION(dev);
754 	if (!adev) {
755 		dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
756 		return -ENODEV;
757 	}
758 
759 	ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
760 				    &d_min, &d_max);
761 	if (ret)
762 		return ret;
763 
764 	if (d_max_in < d_min)
765 		return -EINVAL;
766 
767 	if (d_max > d_max_in) {
768 		for (d_max = d_max_in; d_max > d_min; d_max--) {
769 			if (adev->power.states[d_max].flags.valid)
770 				break;
771 		}
772 	}
773 
774 	if (d_min_p)
775 		*d_min_p = d_min;
776 
777 	return d_max;
778 }
779 EXPORT_SYMBOL(acpi_pm_device_sleep_state);
780 
781 /**
782  * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
783  * @context: Device wakeup context.
784  */
785 static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
786 {
787 	struct device *dev = context->dev;
788 
789 	if (dev) {
790 		pm_wakeup_event(dev, 0);
791 		pm_request_resume(dev);
792 	}
793 }
794 
795 static DEFINE_MUTEX(acpi_wakeup_lock);
796 
797 static int __acpi_device_wakeup_enable(struct acpi_device *adev,
798 				       u32 target_state)
799 {
800 	struct acpi_device_wakeup *wakeup = &adev->wakeup;
801 	acpi_status status;
802 	int error = 0;
803 
804 	mutex_lock(&acpi_wakeup_lock);
805 
806 	/*
807 	 * If the device wakeup power is already enabled, disable it and enable
808 	 * it again in case it depends on the configuration of subordinate
809 	 * devices and the conditions have changed since it was enabled last
810 	 * time.
811 	 */
812 	if (wakeup->enable_count > 0)
813 		acpi_disable_wakeup_device_power(adev);
814 
815 	error = acpi_enable_wakeup_device_power(adev, target_state);
816 	if (error) {
817 		if (wakeup->enable_count > 0) {
818 			acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
819 			wakeup->enable_count = 0;
820 		}
821 		goto out;
822 	}
823 
824 	if (wakeup->enable_count > 0)
825 		goto inc;
826 
827 	status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
828 	if (ACPI_FAILURE(status)) {
829 		acpi_disable_wakeup_device_power(adev);
830 		error = -EIO;
831 		goto out;
832 	}
833 
834 	acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
835 			  (unsigned int)wakeup->gpe_number);
836 
837 inc:
838 	if (wakeup->enable_count < INT_MAX)
839 		wakeup->enable_count++;
840 	else
841 		acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
842 
843 out:
844 	mutex_unlock(&acpi_wakeup_lock);
845 	return error;
846 }
847 
848 /**
849  * acpi_device_wakeup_enable - Enable wakeup functionality for device.
850  * @adev: ACPI device to enable wakeup functionality for.
851  * @target_state: State the system is transitioning into.
852  *
853  * Enable the GPE associated with @adev so that it can generate wakeup signals
854  * for the device in response to external (remote) events and enable wakeup
855  * power for it.
856  *
857  * Callers must ensure that @adev is a valid ACPI device node before executing
858  * this function.
859  */
860 static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
861 {
862 	return __acpi_device_wakeup_enable(adev, target_state);
863 }
864 
865 /**
866  * acpi_device_wakeup_disable - Disable wakeup functionality for device.
867  * @adev: ACPI device to disable wakeup functionality for.
868  *
869  * Disable the GPE associated with @adev and disable wakeup power for it.
870  *
871  * Callers must ensure that @adev is a valid ACPI device node before executing
872  * this function.
873  */
874 static void acpi_device_wakeup_disable(struct acpi_device *adev)
875 {
876 	struct acpi_device_wakeup *wakeup = &adev->wakeup;
877 
878 	mutex_lock(&acpi_wakeup_lock);
879 
880 	if (!wakeup->enable_count)
881 		goto out;
882 
883 	acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
884 	acpi_disable_wakeup_device_power(adev);
885 
886 	wakeup->enable_count--;
887 
888 out:
889 	mutex_unlock(&acpi_wakeup_lock);
890 }
891 
892 /**
893  * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
894  * @dev: Device to enable/disable to generate wakeup events.
895  * @enable: Whether to enable or disable the wakeup functionality.
896  */
897 int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
898 {
899 	struct acpi_device *adev;
900 	int error;
901 
902 	adev = ACPI_COMPANION(dev);
903 	if (!adev) {
904 		dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
905 		return -ENODEV;
906 	}
907 
908 	if (!acpi_device_can_wakeup(adev))
909 		return -EINVAL;
910 
911 	if (!enable) {
912 		acpi_device_wakeup_disable(adev);
913 		dev_dbg(dev, "Wakeup disabled by ACPI\n");
914 		return 0;
915 	}
916 
917 	error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
918 	if (!error)
919 		dev_dbg(dev, "Wakeup enabled by ACPI\n");
920 
921 	return error;
922 }
923 EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
924 
925 /**
926  * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
927  * @dev: Device to put into a low-power state.
928  * @adev: ACPI device node corresponding to @dev.
929  * @system_state: System state to choose the device state for.
930  */
931 static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
932 				 u32 system_state)
933 {
934 	int ret, state;
935 
936 	if (!acpi_device_power_manageable(adev))
937 		return 0;
938 
939 	ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
940 	return ret ? ret : acpi_device_set_power(adev, state);
941 }
942 
943 /**
944  * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
945  * @adev: ACPI device node to put into the full-power state.
946  */
947 static int acpi_dev_pm_full_power(struct acpi_device *adev)
948 {
949 	return acpi_device_power_manageable(adev) ?
950 		acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
951 }
952 
953 /**
954  * acpi_dev_suspend - Put device into a low-power state using ACPI.
955  * @dev: Device to put into a low-power state.
956  * @wakeup: Whether or not to enable wakeup for the device.
957  *
958  * Put the given device into a low-power state using the standard ACPI
959  * mechanism.  Set up remote wakeup if desired, choose the state to put the
960  * device into (this checks if remote wakeup is expected to work too), and set
961  * the power state of the device.
962  */
963 int acpi_dev_suspend(struct device *dev, bool wakeup)
964 {
965 	struct acpi_device *adev = ACPI_COMPANION(dev);
966 	u32 target_state = acpi_target_system_state();
967 	int error;
968 
969 	if (!adev)
970 		return 0;
971 
972 	if (wakeup && acpi_device_can_wakeup(adev)) {
973 		error = acpi_device_wakeup_enable(adev, target_state);
974 		if (error)
975 			return -EAGAIN;
976 	} else {
977 		wakeup = false;
978 	}
979 
980 	error = acpi_dev_pm_low_power(dev, adev, target_state);
981 	if (error && wakeup)
982 		acpi_device_wakeup_disable(adev);
983 
984 	return error;
985 }
986 EXPORT_SYMBOL_GPL(acpi_dev_suspend);
987 
988 /**
989  * acpi_dev_resume - Put device into the full-power state using ACPI.
990  * @dev: Device to put into the full-power state.
991  *
992  * Put the given device into the full-power state using the standard ACPI
993  * mechanism.  Set the power state of the device to ACPI D0 and disable wakeup.
994  */
995 int acpi_dev_resume(struct device *dev)
996 {
997 	struct acpi_device *adev = ACPI_COMPANION(dev);
998 	int error;
999 
1000 	if (!adev)
1001 		return 0;
1002 
1003 	error = acpi_dev_pm_full_power(adev);
1004 	acpi_device_wakeup_disable(adev);
1005 	return error;
1006 }
1007 EXPORT_SYMBOL_GPL(acpi_dev_resume);
1008 
1009 /**
1010  * acpi_subsys_runtime_suspend - Suspend device using ACPI.
1011  * @dev: Device to suspend.
1012  *
1013  * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
1014  * it into a runtime low-power state.
1015  */
1016 int acpi_subsys_runtime_suspend(struct device *dev)
1017 {
1018 	int ret = pm_generic_runtime_suspend(dev);
1019 
1020 	return ret ? ret : acpi_dev_suspend(dev, true);
1021 }
1022 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
1023 
1024 /**
1025  * acpi_subsys_runtime_resume - Resume device using ACPI.
1026  * @dev: Device to Resume.
1027  *
1028  * Use ACPI to put the given device into the full-power state and carry out the
1029  * generic runtime resume procedure for it.
1030  */
1031 int acpi_subsys_runtime_resume(struct device *dev)
1032 {
1033 	int ret = acpi_dev_resume(dev);
1034 
1035 	return ret ? ret : pm_generic_runtime_resume(dev);
1036 }
1037 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
1038 
1039 #ifdef CONFIG_PM_SLEEP
1040 static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
1041 {
1042 	u32 sys_target = acpi_target_system_state();
1043 	int ret, state;
1044 
1045 	if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
1046 	    device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
1047 		return true;
1048 
1049 	if (sys_target == ACPI_STATE_S0)
1050 		return false;
1051 
1052 	if (adev->power.flags.dsw_present)
1053 		return true;
1054 
1055 	ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
1056 	if (ret)
1057 		return true;
1058 
1059 	return state != adev->power.state;
1060 }
1061 
1062 /**
1063  * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
1064  * @dev: Device to prepare.
1065  */
1066 int acpi_subsys_prepare(struct device *dev)
1067 {
1068 	struct acpi_device *adev = ACPI_COMPANION(dev);
1069 
1070 	if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
1071 		int ret = dev->driver->pm->prepare(dev);
1072 
1073 		if (ret < 0)
1074 			return ret;
1075 
1076 		if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
1077 			return 0;
1078 	}
1079 
1080 	return !acpi_dev_needs_resume(dev, adev);
1081 }
1082 EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
1083 
1084 /**
1085  * acpi_subsys_complete - Finalize device's resume during system resume.
1086  * @dev: Device to handle.
1087  */
1088 void acpi_subsys_complete(struct device *dev)
1089 {
1090 	pm_generic_complete(dev);
1091 	/*
1092 	 * If the device had been runtime-suspended before the system went into
1093 	 * the sleep state it is going out of and it has never been resumed till
1094 	 * now, resume it in case the firmware powered it up.
1095 	 */
1096 	if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
1097 		pm_request_resume(dev);
1098 }
1099 EXPORT_SYMBOL_GPL(acpi_subsys_complete);
1100 
1101 /**
1102  * acpi_subsys_suspend - Run the device driver's suspend callback.
1103  * @dev: Device to handle.
1104  *
1105  * Follow PCI and resume devices from runtime suspend before running their
1106  * system suspend callbacks, unless the driver can cope with runtime-suspended
1107  * devices during system suspend and there are no ACPI-specific reasons for
1108  * resuming them.
1109  */
1110 int acpi_subsys_suspend(struct device *dev)
1111 {
1112 	if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1113 	    acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1114 		pm_runtime_resume(dev);
1115 
1116 	return pm_generic_suspend(dev);
1117 }
1118 EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
1119 
1120 /**
1121  * acpi_subsys_suspend_late - Suspend device using ACPI.
1122  * @dev: Device to suspend.
1123  *
1124  * Carry out the generic late suspend procedure for @dev and use ACPI to put
1125  * it into a low-power state during system transition into a sleep state.
1126  */
1127 int acpi_subsys_suspend_late(struct device *dev)
1128 {
1129 	int ret;
1130 
1131 	if (dev_pm_skip_suspend(dev))
1132 		return 0;
1133 
1134 	ret = pm_generic_suspend_late(dev);
1135 	return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
1136 }
1137 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1138 
1139 /**
1140  * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
1141  * @dev: Device to suspend.
1142  */
1143 int acpi_subsys_suspend_noirq(struct device *dev)
1144 {
1145 	int ret;
1146 
1147 	if (dev_pm_skip_suspend(dev))
1148 		return 0;
1149 
1150 	ret = pm_generic_suspend_noirq(dev);
1151 	if (ret)
1152 		return ret;
1153 
1154 	/*
1155 	 * If the target system sleep state is suspend-to-idle, it is sufficient
1156 	 * to check whether or not the device's wakeup settings are good for
1157 	 * runtime PM.  Otherwise, the pm_resume_via_firmware() check will cause
1158 	 * acpi_subsys_complete() to take care of fixing up the device's state
1159 	 * anyway, if need be.
1160 	 */
1161 	if (device_can_wakeup(dev) && !device_may_wakeup(dev))
1162 		dev->power.may_skip_resume = false;
1163 
1164 	return 0;
1165 }
1166 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
1167 
1168 /**
1169  * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
1170  * @dev: Device to handle.
1171  */
1172 static int acpi_subsys_resume_noirq(struct device *dev)
1173 {
1174 	if (dev_pm_skip_resume(dev))
1175 		return 0;
1176 
1177 	return pm_generic_resume_noirq(dev);
1178 }
1179 
1180 /**
1181  * acpi_subsys_resume_early - Resume device using ACPI.
1182  * @dev: Device to Resume.
1183  *
1184  * Use ACPI to put the given device into the full-power state and carry out the
1185  * generic early resume procedure for it during system transition into the
1186  * working state, but only do that if device either defines early resume
1187  * handler, or does not define power operations at all. Otherwise powering up
1188  * of the device is postponed to the normal resume phase.
1189  */
1190 static int acpi_subsys_resume_early(struct device *dev)
1191 {
1192 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1193 	int ret;
1194 
1195 	if (dev_pm_skip_resume(dev))
1196 		return 0;
1197 
1198 	if (pm && !pm->resume_early) {
1199 		dev_dbg(dev, "postponing D0 transition to normal resume stage\n");
1200 		return 0;
1201 	}
1202 
1203 	ret = acpi_dev_resume(dev);
1204 	return ret ? ret : pm_generic_resume_early(dev);
1205 }
1206 
1207 /**
1208  * acpi_subsys_resume - Resume device using ACPI.
1209  * @dev: Device to Resume.
1210  *
1211  * Use ACPI to put the given device into the full-power state if it has not been
1212  * powered up during early resume phase, and carry out the generic resume
1213  * procedure for it during system transition into the working state.
1214  */
1215 static int acpi_subsys_resume(struct device *dev)
1216 {
1217 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1218 	int ret = 0;
1219 
1220 	if (!dev_pm_skip_resume(dev) && pm && !pm->resume_early) {
1221 		dev_dbg(dev, "executing postponed D0 transition\n");
1222 		ret = acpi_dev_resume(dev);
1223 	}
1224 
1225 	return ret ? ret : pm_generic_resume(dev);
1226 }
1227 
1228 /**
1229  * acpi_subsys_freeze - Run the device driver's freeze callback.
1230  * @dev: Device to handle.
1231  */
1232 int acpi_subsys_freeze(struct device *dev)
1233 {
1234 	/*
1235 	 * Resume all runtime-suspended devices before creating a snapshot
1236 	 * image of system memory, because the restore kernel generally cannot
1237 	 * be expected to always handle them consistently and they need to be
1238 	 * put into the runtime-active metastate during system resume anyway,
1239 	 * so it is better to ensure that the state saved in the image will be
1240 	 * always consistent with that.
1241 	 */
1242 	pm_runtime_resume(dev);
1243 
1244 	return pm_generic_freeze(dev);
1245 }
1246 EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
1247 
1248 /**
1249  * acpi_subsys_restore_early - Restore device using ACPI.
1250  * @dev: Device to restore.
1251  */
1252 int acpi_subsys_restore_early(struct device *dev)
1253 {
1254 	int ret = acpi_dev_resume(dev);
1255 
1256 	return ret ? ret : pm_generic_restore_early(dev);
1257 }
1258 EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
1259 
1260 /**
1261  * acpi_subsys_poweroff - Run the device driver's poweroff callback.
1262  * @dev: Device to handle.
1263  *
1264  * Follow PCI and resume devices from runtime suspend before running their
1265  * system poweroff callbacks, unless the driver can cope with runtime-suspended
1266  * devices during system suspend and there are no ACPI-specific reasons for
1267  * resuming them.
1268  */
1269 int acpi_subsys_poweroff(struct device *dev)
1270 {
1271 	if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1272 	    acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1273 		pm_runtime_resume(dev);
1274 
1275 	return pm_generic_poweroff(dev);
1276 }
1277 EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
1278 
1279 /**
1280  * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
1281  * @dev: Device to handle.
1282  *
1283  * Carry out the generic late poweroff procedure for @dev and use ACPI to put
1284  * it into a low-power state during system transition into a sleep state.
1285  */
1286 static int acpi_subsys_poweroff_late(struct device *dev)
1287 {
1288 	int ret;
1289 
1290 	if (dev_pm_skip_suspend(dev))
1291 		return 0;
1292 
1293 	ret = pm_generic_poweroff_late(dev);
1294 	if (ret)
1295 		return ret;
1296 
1297 	return acpi_dev_suspend(dev, device_may_wakeup(dev));
1298 }
1299 
1300 /**
1301  * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
1302  * @dev: Device to suspend.
1303  */
1304 static int acpi_subsys_poweroff_noirq(struct device *dev)
1305 {
1306 	if (dev_pm_skip_suspend(dev))
1307 		return 0;
1308 
1309 	return pm_generic_poweroff_noirq(dev);
1310 }
1311 #endif /* CONFIG_PM_SLEEP */
1312 
1313 static struct dev_pm_domain acpi_general_pm_domain = {
1314 	.ops = {
1315 		.runtime_suspend = acpi_subsys_runtime_suspend,
1316 		.runtime_resume = acpi_subsys_runtime_resume,
1317 #ifdef CONFIG_PM_SLEEP
1318 		.prepare = acpi_subsys_prepare,
1319 		.complete = acpi_subsys_complete,
1320 		.suspend = acpi_subsys_suspend,
1321 		.resume = acpi_subsys_resume,
1322 		.suspend_late = acpi_subsys_suspend_late,
1323 		.suspend_noirq = acpi_subsys_suspend_noirq,
1324 		.resume_noirq = acpi_subsys_resume_noirq,
1325 		.resume_early = acpi_subsys_resume_early,
1326 		.freeze = acpi_subsys_freeze,
1327 		.poweroff = acpi_subsys_poweroff,
1328 		.poweroff_late = acpi_subsys_poweroff_late,
1329 		.poweroff_noirq = acpi_subsys_poweroff_noirq,
1330 		.restore_early = acpi_subsys_restore_early,
1331 #endif
1332 	},
1333 };
1334 
1335 /**
1336  * acpi_dev_pm_detach - Remove ACPI power management from the device.
1337  * @dev: Device to take care of.
1338  * @power_off: Whether or not to try to remove power from the device.
1339  *
1340  * Remove the device from the general ACPI PM domain and remove its wakeup
1341  * notifier.  If @power_off is set, additionally remove power from the device if
1342  * possible.
1343  *
1344  * Callers must ensure proper synchronization of this function with power
1345  * management callbacks.
1346  */
1347 static void acpi_dev_pm_detach(struct device *dev, bool power_off)
1348 {
1349 	struct acpi_device *adev = ACPI_COMPANION(dev);
1350 
1351 	if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1352 		dev_pm_domain_set(dev, NULL);
1353 		acpi_remove_pm_notifier(adev);
1354 		if (power_off) {
1355 			/*
1356 			 * If the device's PM QoS resume latency limit or flags
1357 			 * have been exposed to user space, they have to be
1358 			 * hidden at this point, so that they don't affect the
1359 			 * choice of the low-power state to put the device into.
1360 			 */
1361 			dev_pm_qos_hide_latency_limit(dev);
1362 			dev_pm_qos_hide_flags(dev);
1363 			acpi_device_wakeup_disable(adev);
1364 			acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1365 		}
1366 	}
1367 }
1368 
1369 /**
1370  * acpi_dev_pm_attach - Prepare device for ACPI power management.
1371  * @dev: Device to prepare.
1372  * @power_on: Whether or not to power on the device.
1373  *
1374  * If @dev has a valid ACPI handle that has a valid struct acpi_device object
1375  * attached to it, install a wakeup notification handler for the device and
1376  * add it to the general ACPI PM domain.  If @power_on is set, the device will
1377  * be put into the ACPI D0 state before the function returns.
1378  *
1379  * This assumes that the @dev's bus type uses generic power management callbacks
1380  * (or doesn't use any power management callbacks at all).
1381  *
1382  * Callers must ensure proper synchronization of this function with power
1383  * management callbacks.
1384  */
1385 int acpi_dev_pm_attach(struct device *dev, bool power_on)
1386 {
1387 	/*
1388 	 * Skip devices whose ACPI companions match the device IDs below,
1389 	 * because they require special power management handling incompatible
1390 	 * with the generic ACPI PM domain.
1391 	 */
1392 	static const struct acpi_device_id special_pm_ids[] = {
1393 		ACPI_FAN_DEVICE_IDS,
1394 		{}
1395 	};
1396 	struct acpi_device *adev = ACPI_COMPANION(dev);
1397 
1398 	if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
1399 		return 0;
1400 
1401 	/*
1402 	 * Only attach the power domain to the first device if the
1403 	 * companion is shared by multiple. This is to prevent doing power
1404 	 * management twice.
1405 	 */
1406 	if (!acpi_device_is_first_physical_node(adev, dev))
1407 		return 0;
1408 
1409 	acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
1410 	dev_pm_domain_set(dev, &acpi_general_pm_domain);
1411 	if (power_on) {
1412 		acpi_dev_pm_full_power(adev);
1413 		acpi_device_wakeup_disable(adev);
1414 	}
1415 
1416 	dev->pm_domain->detach = acpi_dev_pm_detach;
1417 	return 1;
1418 }
1419 EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
1420 
1421 /**
1422  * acpi_storage_d3 - Check if D3 should be used in the suspend path
1423  * @dev: Device to check
1424  *
1425  * Return %true if the platform firmware wants @dev to be programmed
1426  * into D3hot or D3cold (if supported) in the suspend path, or %false
1427  * when there is no specific preference. On some platforms, if this
1428  * hint is ignored, @dev may remain unresponsive after suspending the
1429  * platform as a whole.
1430  *
1431  * Although the property has storage in the name it actually is
1432  * applied to the PCIe slot and plugging in a non-storage device the
1433  * same platform restrictions will likely apply.
1434  */
1435 bool acpi_storage_d3(struct device *dev)
1436 {
1437 	struct acpi_device *adev = ACPI_COMPANION(dev);
1438 	u8 val;
1439 
1440 	if (force_storage_d3())
1441 		return true;
1442 
1443 	if (!adev)
1444 		return false;
1445 	if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable",
1446 			&val))
1447 		return false;
1448 	return val == 1;
1449 }
1450 EXPORT_SYMBOL_GPL(acpi_storage_d3);
1451 
1452 /**
1453  * acpi_dev_state_d0 - Tell if the device is in D0 power state
1454  * @dev: Physical device the ACPI power state of which to check
1455  *
1456  * On a system without ACPI, return true. On a system with ACPI, return true if
1457  * the current ACPI power state of the device is D0, or false otherwise.
1458  *
1459  * Note that the power state of a device is not well-defined after it has been
1460  * passed to acpi_device_set_power() and before that function returns, so it is
1461  * not valid to ask for the ACPI power state of the device in that time frame.
1462  *
1463  * This function is intended to be used in a driver's probe or remove
1464  * function. See Documentation/firmware-guide/acpi/low-power-probe.rst for
1465  * more information.
1466  */
1467 bool acpi_dev_state_d0(struct device *dev)
1468 {
1469 	struct acpi_device *adev = ACPI_COMPANION(dev);
1470 
1471 	if (!adev)
1472 		return true;
1473 
1474 	return adev->power.state == ACPI_STATE_D0;
1475 }
1476 EXPORT_SYMBOL_GPL(acpi_dev_state_d0);
1477 
1478 #endif /* CONFIG_PM */
1479