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