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