device_pm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. * drivers/acpi/device_pm.c - ACPI device power management routines.
  3. *
  4. * Copyright (C) 2012, Intel Corp.
  5. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #include <linux/acpi.h>
  21. #include <linux/export.h>
  22. #include <linux/mutex.h>
  23. #include <linux/pm_qos.h>
  24. #include <linux/pm_domain.h>
  25. #include <linux/pm_runtime.h>
  26. #include "internal.h"
  27. #define _COMPONENT ACPI_POWER_COMPONENT
  28. ACPI_MODULE_NAME("device_pm");
  29. /**
  30. * acpi_power_state_string - String representation of ACPI device power state.
  31. * @state: ACPI device power state to return the string representation of.
  32. */
  33. const char *acpi_power_state_string(int state)
  34. {
  35. switch (state) {
  36. case ACPI_STATE_D0:
  37. return "D0";
  38. case ACPI_STATE_D1:
  39. return "D1";
  40. case ACPI_STATE_D2:
  41. return "D2";
  42. case ACPI_STATE_D3_HOT:
  43. return "D3hot";
  44. case ACPI_STATE_D3_COLD:
  45. return "D3cold";
  46. default:
  47. return "(unknown)";
  48. }
  49. }
  50. /**
  51. * acpi_device_get_power - Get power state of an ACPI device.
  52. * @device: Device to get the power state of.
  53. * @state: Place to store the power state of the device.
  54. *
  55. * This function does not update the device's power.state field, but it may
  56. * update its parent's power.state field (when the parent's power state is
  57. * unknown and the device's power state turns out to be D0).
  58. */
  59. int acpi_device_get_power(struct acpi_device *device, int *state)
  60. {
  61. int result = ACPI_STATE_UNKNOWN;
  62. if (!device || !state)
  63. return -EINVAL;
  64. if (!device->flags.power_manageable) {
  65. /* TBD: Non-recursive algorithm for walking up hierarchy. */
  66. *state = device->parent ?
  67. device->parent->power.state : ACPI_STATE_D0;
  68. goto out;
  69. }
  70. /*
  71. * Get the device's power state from power resources settings and _PSC,
  72. * if available.
  73. */
  74. if (device->power.flags.power_resources) {
  75. int error = acpi_power_get_inferred_state(device, &result);
  76. if (error)
  77. return error;
  78. }
  79. if (device->power.flags.explicit_get) {
  80. acpi_handle handle = device->handle;
  81. unsigned long long psc;
  82. acpi_status status;
  83. status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc);
  84. if (ACPI_FAILURE(status))
  85. return -ENODEV;
  86. /*
  87. * The power resources settings may indicate a power state
  88. * shallower than the actual power state of the device, because
  89. * the same power resources may be referenced by other devices.
  90. *
  91. * For systems predating ACPI 4.0 we assume that D3hot is the
  92. * deepest state that can be supported.
  93. */
  94. if (psc > result && psc < ACPI_STATE_D3_COLD)
  95. result = psc;
  96. else if (result == ACPI_STATE_UNKNOWN)
  97. result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
  98. }
  99. /*
  100. * If we were unsure about the device parent's power state up to this
  101. * point, the fact that the device is in D0 implies that the parent has
  102. * to be in D0 too, except if ignore_parent is set.
  103. */
  104. if (!device->power.flags.ignore_parent && device->parent
  105. && device->parent->power.state == ACPI_STATE_UNKNOWN
  106. && result == ACPI_STATE_D0)
  107. device->parent->power.state = ACPI_STATE_D0;
  108. *state = result;
  109. out:
  110. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
  111. device->pnp.bus_id, acpi_power_state_string(*state)));
  112. return 0;
  113. }
  114. static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
  115. {
  116. if (adev->power.states[state].flags.explicit_set) {
  117. char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
  118. acpi_status status;
  119. status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
  120. if (ACPI_FAILURE(status))
  121. return -ENODEV;
  122. }
  123. return 0;
  124. }
  125. /**
  126. * acpi_device_set_power - Set power state of an ACPI device.
  127. * @device: Device to set the power state of.
  128. * @state: New power state to set.
  129. *
  130. * Callers must ensure that the device is power manageable before using this
  131. * function.
  132. */
  133. int acpi_device_set_power(struct acpi_device *device, int state)
  134. {
  135. int target_state = state;
  136. int result = 0;
  137. if (!device || !device->flags.power_manageable
  138. || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  139. return -EINVAL;
  140. /* Make sure this is a valid target state */
  141. if (state == device->power.state) {
  142. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
  143. device->pnp.bus_id,
  144. acpi_power_state_string(state)));
  145. return 0;
  146. }
  147. if (state == ACPI_STATE_D3_COLD) {
  148. /*
  149. * For transitions to D3cold we need to execute _PS3 and then
  150. * possibly drop references to the power resources in use.
  151. */
  152. state = ACPI_STATE_D3_HOT;
  153. /* If _PR3 is not available, use D3hot as the target state. */
  154. if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
  155. target_state = state;
  156. } else if (!device->power.states[state].flags.valid) {
  157. dev_warn(&device->dev, "Power state %s not supported\n",
  158. acpi_power_state_string(state));
  159. return -ENODEV;
  160. }
  161. if (!device->power.flags.ignore_parent &&
  162. device->parent && (state < device->parent->power.state)) {
  163. dev_warn(&device->dev,
  164. "Cannot transition to power state %s for parent in %s\n",
  165. acpi_power_state_string(state),
  166. acpi_power_state_string(device->parent->power.state));
  167. return -ENODEV;
  168. }
  169. /*
  170. * Transition Power
  171. * ----------------
  172. * In accordance with ACPI 6, _PSx is executed before manipulating power
  173. * resources, unless the target state is D0, in which case _PS0 is
  174. * supposed to be executed after turning the power resources on.
  175. */
  176. if (state > ACPI_STATE_D0) {
  177. /*
  178. * According to ACPI 6, devices cannot go from lower-power
  179. * (deeper) states to higher-power (shallower) states.
  180. */
  181. if (state < device->power.state) {
  182. dev_warn(&device->dev, "Cannot transition from %s to %s\n",
  183. acpi_power_state_string(device->power.state),
  184. acpi_power_state_string(state));
  185. return -ENODEV;
  186. }
  187. result = acpi_dev_pm_explicit_set(device, state);
  188. if (result)
  189. goto end;
  190. if (device->power.flags.power_resources)
  191. result = acpi_power_transition(device, target_state);
  192. } else {
  193. if (device->power.flags.power_resources) {
  194. result = acpi_power_transition(device, ACPI_STATE_D0);
  195. if (result)
  196. goto end;
  197. }
  198. result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  199. }
  200. end:
  201. if (result) {
  202. dev_warn(&device->dev, "Failed to change power state to %s\n",
  203. acpi_power_state_string(state));
  204. } else {
  205. device->power.state = target_state;
  206. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  207. "Device [%s] transitioned to %s\n",
  208. device->pnp.bus_id,
  209. acpi_power_state_string(state)));
  210. }
  211. return result;
  212. }
  213. EXPORT_SYMBOL(acpi_device_set_power);
  214. int acpi_bus_set_power(acpi_handle handle, int state)
  215. {
  216. struct acpi_device *device;
  217. int result;
  218. result = acpi_bus_get_device(handle, &device);
  219. if (result)
  220. return result;
  221. return acpi_device_set_power(device, state);
  222. }
  223. EXPORT_SYMBOL(acpi_bus_set_power);
  224. int acpi_bus_init_power(struct acpi_device *device)
  225. {
  226. int state;
  227. int result;
  228. if (!device)
  229. return -EINVAL;
  230. device->power.state = ACPI_STATE_UNKNOWN;
  231. if (!acpi_device_is_present(device))
  232. return -ENXIO;
  233. result = acpi_device_get_power(device, &state);
  234. if (result)
  235. return result;
  236. if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
  237. /* Reference count the power resources. */
  238. result = acpi_power_on_resources(device, state);
  239. if (result)
  240. return result;
  241. if (state == ACPI_STATE_D0) {
  242. /*
  243. * If _PSC is not present and the state inferred from
  244. * power resources appears to be D0, it still may be
  245. * necessary to execute _PS0 at this point, because
  246. * another device using the same power resources may
  247. * have been put into D0 previously and that's why we
  248. * see D0 here.
  249. */
  250. result = acpi_dev_pm_explicit_set(device, state);
  251. if (result)
  252. return result;
  253. }
  254. } else if (state == ACPI_STATE_UNKNOWN) {
  255. /*
  256. * No power resources and missing _PSC? Cross fingers and make
  257. * it D0 in hope that this is what the BIOS put the device into.
  258. * [We tried to force D0 here by executing _PS0, but that broke
  259. * Toshiba P870-303 in a nasty way.]
  260. */
  261. state = ACPI_STATE_D0;
  262. }
  263. device->power.state = state;
  264. return 0;
  265. }
  266. /**
  267. * acpi_device_fix_up_power - Force device with missing _PSC into D0.
  268. * @device: Device object whose power state is to be fixed up.
  269. *
  270. * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
  271. * are assumed to be put into D0 by the BIOS. However, in some cases that may
  272. * not be the case and this function should be used then.
  273. */
  274. int acpi_device_fix_up_power(struct acpi_device *device)
  275. {
  276. int ret = 0;
  277. if (!device->power.flags.power_resources
  278. && !device->power.flags.explicit_get
  279. && device->power.state == ACPI_STATE_D0)
  280. ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  281. return ret;
  282. }
  283. EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
  284. int acpi_device_update_power(struct acpi_device *device, int *state_p)
  285. {
  286. int state;
  287. int result;
  288. if (device->power.state == ACPI_STATE_UNKNOWN) {
  289. result = acpi_bus_init_power(device);
  290. if (!result && state_p)
  291. *state_p = device->power.state;
  292. return result;
  293. }
  294. result = acpi_device_get_power(device, &state);
  295. if (result)
  296. return result;
  297. if (state == ACPI_STATE_UNKNOWN) {
  298. state = ACPI_STATE_D0;
  299. result = acpi_device_set_power(device, state);
  300. if (result)
  301. return result;
  302. } else {
  303. if (device->power.flags.power_resources) {
  304. /*
  305. * We don't need to really switch the state, bu we need
  306. * to update the power resources' reference counters.
  307. */
  308. result = acpi_power_transition(device, state);
  309. if (result)
  310. return result;
  311. }
  312. device->power.state = state;
  313. }
  314. if (state_p)
  315. *state_p = state;
  316. return 0;
  317. }
  318. EXPORT_SYMBOL_GPL(acpi_device_update_power);
  319. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  320. {
  321. struct acpi_device *device;
  322. int result;
  323. result = acpi_bus_get_device(handle, &device);
  324. return result ? result : acpi_device_update_power(device, state_p);
  325. }
  326. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  327. bool acpi_bus_power_manageable(acpi_handle handle)
  328. {
  329. struct acpi_device *device;
  330. int result;
  331. result = acpi_bus_get_device(handle, &device);
  332. return result ? false : device->flags.power_manageable;
  333. }
  334. EXPORT_SYMBOL(acpi_bus_power_manageable);
  335. #ifdef CONFIG_PM
  336. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  337. static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
  338. {
  339. struct acpi_device *adev;
  340. if (val != ACPI_NOTIFY_DEVICE_WAKE)
  341. return;
  342. adev = acpi_bus_get_acpi_device(handle);
  343. if (!adev)
  344. return;
  345. mutex_lock(&acpi_pm_notifier_lock);
  346. if (adev->wakeup.flags.notifier_present) {
  347. __pm_wakeup_event(adev->wakeup.ws, 0);
  348. if (adev->wakeup.context.work.func)
  349. queue_pm_work(&adev->wakeup.context.work);
  350. }
  351. mutex_unlock(&acpi_pm_notifier_lock);
  352. acpi_bus_put_acpi_device(adev);
  353. }
  354. /**
  355. * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
  356. * @adev: ACPI device to add the notify handler for.
  357. * @dev: Device to generate a wakeup event for while handling the notification.
  358. * @work_func: Work function to execute when handling the notification.
  359. *
  360. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  361. * PM wakeup events. For example, wakeup events may be generated for bridges
  362. * if one of the devices below the bridge is signaling wakeup, even if the
  363. * bridge itself doesn't have a wakeup GPE associated with it.
  364. */
  365. acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
  366. void (*work_func)(struct work_struct *work))
  367. {
  368. acpi_status status = AE_ALREADY_EXISTS;
  369. if (!dev && !work_func)
  370. return AE_BAD_PARAMETER;
  371. mutex_lock(&acpi_pm_notifier_lock);
  372. if (adev->wakeup.flags.notifier_present)
  373. goto out;
  374. adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
  375. adev->wakeup.context.dev = dev;
  376. if (work_func)
  377. INIT_WORK(&adev->wakeup.context.work, work_func);
  378. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  379. acpi_pm_notify_handler, NULL);
  380. if (ACPI_FAILURE(status))
  381. goto out;
  382. adev->wakeup.flags.notifier_present = true;
  383. out:
  384. mutex_unlock(&acpi_pm_notifier_lock);
  385. return status;
  386. }
  387. /**
  388. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  389. * @adev: ACPI device to remove the notifier from.
  390. */
  391. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
  392. {
  393. acpi_status status = AE_BAD_PARAMETER;
  394. mutex_lock(&acpi_pm_notifier_lock);
  395. if (!adev->wakeup.flags.notifier_present)
  396. goto out;
  397. status = acpi_remove_notify_handler(adev->handle,
  398. ACPI_SYSTEM_NOTIFY,
  399. acpi_pm_notify_handler);
  400. if (ACPI_FAILURE(status))
  401. goto out;
  402. if (adev->wakeup.context.work.func) {
  403. cancel_work_sync(&adev->wakeup.context.work);
  404. adev->wakeup.context.work.func = NULL;
  405. }
  406. adev->wakeup.context.dev = NULL;
  407. wakeup_source_unregister(adev->wakeup.ws);
  408. adev->wakeup.flags.notifier_present = false;
  409. out:
  410. mutex_unlock(&acpi_pm_notifier_lock);
  411. return status;
  412. }
  413. bool acpi_bus_can_wakeup(acpi_handle handle)
  414. {
  415. struct acpi_device *device;
  416. int result;
  417. result = acpi_bus_get_device(handle, &device);
  418. return result ? false : device->wakeup.flags.valid;
  419. }
  420. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  421. /**
  422. * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
  423. * @dev: Device whose preferred target power state to return.
  424. * @adev: ACPI device node corresponding to @dev.
  425. * @target_state: System state to match the resultant device state.
  426. * @d_min_p: Location to store the highest power state available to the device.
  427. * @d_max_p: Location to store the lowest power state available to the device.
  428. *
  429. * Find the lowest power (highest number) and highest power (lowest number) ACPI
  430. * device power states that the device can be in while the system is in the
  431. * state represented by @target_state. Store the integer numbers representing
  432. * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
  433. * respectively.
  434. *
  435. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  436. * actually corresponds to @dev before using this function.
  437. *
  438. * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
  439. * returns a value that doesn't make sense. The memory locations pointed to by
  440. * @d_max_p and @d_min_p are only modified on success.
  441. */
  442. static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
  443. u32 target_state, int *d_min_p, int *d_max_p)
  444. {
  445. char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
  446. acpi_handle handle = adev->handle;
  447. unsigned long long ret;
  448. int d_min, d_max;
  449. bool wakeup = false;
  450. acpi_status status;
  451. /*
  452. * If the system state is S0, the lowest power state the device can be
  453. * in is D3cold, unless the device has _S0W and is supposed to signal
  454. * wakeup, in which case the return value of _S0W has to be used as the
  455. * lowest power state available to the device.
  456. */
  457. d_min = ACPI_STATE_D0;
  458. d_max = ACPI_STATE_D3_COLD;
  459. /*
  460. * If present, _SxD methods return the minimum D-state (highest power
  461. * state) we can use for the corresponding S-states. Otherwise, the
  462. * minimum D-state is D0 (ACPI 3.x).
  463. */
  464. if (target_state > ACPI_STATE_S0) {
  465. /*
  466. * We rely on acpi_evaluate_integer() not clobbering the integer
  467. * provided if AE_NOT_FOUND is returned.
  468. */
  469. ret = d_min;
  470. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  471. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  472. || ret > ACPI_STATE_D3_COLD)
  473. return -ENODATA;
  474. /*
  475. * We need to handle legacy systems where D3hot and D3cold are
  476. * the same and 3 is returned in both cases, so fall back to
  477. * D3cold if D3hot is not a valid state.
  478. */
  479. if (!adev->power.states[ret].flags.valid) {
  480. if (ret == ACPI_STATE_D3_HOT)
  481. ret = ACPI_STATE_D3_COLD;
  482. else
  483. return -ENODATA;
  484. }
  485. d_min = ret;
  486. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  487. && adev->wakeup.sleep_state >= target_state;
  488. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  489. PM_QOS_FLAGS_NONE) {
  490. wakeup = adev->wakeup.flags.valid;
  491. }
  492. /*
  493. * If _PRW says we can wake up the system from the target sleep state,
  494. * the D-state returned by _SxD is sufficient for that (we assume a
  495. * wakeup-aware driver if wake is set). Still, if _SxW exists
  496. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  497. * can wake the system. _S0W may be valid, too.
  498. */
  499. if (wakeup) {
  500. method[3] = 'W';
  501. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  502. if (status == AE_NOT_FOUND) {
  503. if (target_state > ACPI_STATE_S0)
  504. d_max = d_min;
  505. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  506. /* Fall back to D3cold if ret is not a valid state. */
  507. if (!adev->power.states[ret].flags.valid)
  508. ret = ACPI_STATE_D3_COLD;
  509. d_max = ret > d_min ? ret : d_min;
  510. } else {
  511. return -ENODATA;
  512. }
  513. }
  514. if (d_min_p)
  515. *d_min_p = d_min;
  516. if (d_max_p)
  517. *d_max_p = d_max;
  518. return 0;
  519. }
  520. /**
  521. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  522. * @dev: Device whose preferred target power state to return.
  523. * @d_min_p: Location to store the upper limit of the allowed states range.
  524. * @d_max_in: Deepest low-power state to take into consideration.
  525. * Return value: Preferred power state of the device on success, -ENODEV
  526. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  527. * incorrect, or -ENODATA on ACPI method failure.
  528. *
  529. * The caller must ensure that @dev is valid before using this function.
  530. */
  531. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  532. {
  533. struct acpi_device *adev;
  534. int ret, d_min, d_max;
  535. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  536. return -EINVAL;
  537. if (d_max_in > ACPI_STATE_D2) {
  538. enum pm_qos_flags_status stat;
  539. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  540. if (stat == PM_QOS_FLAGS_ALL)
  541. d_max_in = ACPI_STATE_D2;
  542. }
  543. adev = ACPI_COMPANION(dev);
  544. if (!adev) {
  545. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  546. return -ENODEV;
  547. }
  548. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  549. &d_min, &d_max);
  550. if (ret)
  551. return ret;
  552. if (d_max_in < d_min)
  553. return -EINVAL;
  554. if (d_max > d_max_in) {
  555. for (d_max = d_max_in; d_max > d_min; d_max--) {
  556. if (adev->power.states[d_max].flags.valid)
  557. break;
  558. }
  559. }
  560. if (d_min_p)
  561. *d_min_p = d_min;
  562. return d_max;
  563. }
  564. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  565. /**
  566. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  567. * @work: Work item to handle.
  568. */
  569. static void acpi_pm_notify_work_func(struct work_struct *work)
  570. {
  571. struct device *dev;
  572. dev = container_of(work, struct acpi_device_wakeup_context, work)->dev;
  573. if (dev) {
  574. pm_wakeup_event(dev, 0);
  575. pm_runtime_resume(dev);
  576. }
  577. }
  578. /**
  579. * acpi_device_wakeup - Enable/disable wakeup functionality for device.
  580. * @adev: ACPI device to enable/disable wakeup functionality for.
  581. * @target_state: State the system is transitioning into.
  582. * @enable: Whether to enable or disable the wakeup functionality.
  583. *
  584. * Enable/disable the GPE associated with @adev so that it can generate
  585. * wakeup signals for the device in response to external (remote) events and
  586. * enable/disable device wakeup power.
  587. *
  588. * Callers must ensure that @adev is a valid ACPI device node before executing
  589. * this function.
  590. */
  591. static int acpi_device_wakeup(struct acpi_device *adev, u32 target_state,
  592. bool enable)
  593. {
  594. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  595. if (enable) {
  596. acpi_status res;
  597. int error;
  598. error = acpi_enable_wakeup_device_power(adev, target_state);
  599. if (error)
  600. return error;
  601. if (adev->wakeup.flags.enabled)
  602. return 0;
  603. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  604. if (ACPI_SUCCESS(res)) {
  605. adev->wakeup.flags.enabled = 1;
  606. } else {
  607. acpi_disable_wakeup_device_power(adev);
  608. return -EIO;
  609. }
  610. } else {
  611. if (adev->wakeup.flags.enabled) {
  612. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  613. adev->wakeup.flags.enabled = 0;
  614. }
  615. acpi_disable_wakeup_device_power(adev);
  616. }
  617. return 0;
  618. }
  619. /**
  620. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  621. * @dev: Device to enable/disable the platform to wake up.
  622. * @enable: Whether to enable or disable the wakeup functionality.
  623. */
  624. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  625. {
  626. struct acpi_device *adev;
  627. if (!device_run_wake(phys_dev))
  628. return -EINVAL;
  629. adev = ACPI_COMPANION(phys_dev);
  630. if (!adev) {
  631. dev_dbg(phys_dev, "ACPI companion missing in %s!\n", __func__);
  632. return -ENODEV;
  633. }
  634. return acpi_device_wakeup(adev, ACPI_STATE_S0, enable);
  635. }
  636. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  637. #ifdef CONFIG_PM_SLEEP
  638. /**
  639. * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
  640. * @dev: Device to enable/desible to wake up the system from sleep states.
  641. * @enable: Whether to enable or disable @dev to wake up the system.
  642. */
  643. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  644. {
  645. struct acpi_device *adev;
  646. int error;
  647. if (!device_can_wakeup(dev))
  648. return -EINVAL;
  649. adev = ACPI_COMPANION(dev);
  650. if (!adev) {
  651. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  652. return -ENODEV;
  653. }
  654. error = acpi_device_wakeup(adev, acpi_target_system_state(), enable);
  655. if (!error)
  656. dev_info(dev, "System wakeup %s by ACPI\n",
  657. enable ? "enabled" : "disabled");
  658. return error;
  659. }
  660. #endif /* CONFIG_PM_SLEEP */
  661. /**
  662. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  663. * @dev: Device to put into a low-power state.
  664. * @adev: ACPI device node corresponding to @dev.
  665. * @system_state: System state to choose the device state for.
  666. */
  667. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  668. u32 system_state)
  669. {
  670. int ret, state;
  671. if (!acpi_device_power_manageable(adev))
  672. return 0;
  673. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  674. return ret ? ret : acpi_device_set_power(adev, state);
  675. }
  676. /**
  677. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  678. * @adev: ACPI device node to put into the full-power state.
  679. */
  680. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  681. {
  682. return acpi_device_power_manageable(adev) ?
  683. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  684. }
  685. /**
  686. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  687. * @dev: Device to put into a low-power state.
  688. *
  689. * Put the given device into a runtime low-power state using the standard ACPI
  690. * mechanism. Set up remote wakeup if desired, choose the state to put the
  691. * device into (this checks if remote wakeup is expected to work too), and set
  692. * the power state of the device.
  693. */
  694. int acpi_dev_runtime_suspend(struct device *dev)
  695. {
  696. struct acpi_device *adev = ACPI_COMPANION(dev);
  697. bool remote_wakeup;
  698. int error;
  699. if (!adev)
  700. return 0;
  701. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  702. PM_QOS_FLAGS_NONE;
  703. error = acpi_device_wakeup(adev, ACPI_STATE_S0, remote_wakeup);
  704. if (remote_wakeup && error)
  705. return -EAGAIN;
  706. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  707. if (error)
  708. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  709. return error;
  710. }
  711. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  712. /**
  713. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  714. * @dev: Device to put into the full-power state.
  715. *
  716. * Put the given device into the full-power state using the standard ACPI
  717. * mechanism at run time. Set the power state of the device to ACPI D0 and
  718. * disable remote wakeup.
  719. */
  720. int acpi_dev_runtime_resume(struct device *dev)
  721. {
  722. struct acpi_device *adev = ACPI_COMPANION(dev);
  723. int error;
  724. if (!adev)
  725. return 0;
  726. error = acpi_dev_pm_full_power(adev);
  727. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  728. return error;
  729. }
  730. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  731. /**
  732. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  733. * @dev: Device to suspend.
  734. *
  735. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  736. * it into a runtime low-power state.
  737. */
  738. int acpi_subsys_runtime_suspend(struct device *dev)
  739. {
  740. int ret = pm_generic_runtime_suspend(dev);
  741. return ret ? ret : acpi_dev_runtime_suspend(dev);
  742. }
  743. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  744. /**
  745. * acpi_subsys_runtime_resume - Resume device using ACPI.
  746. * @dev: Device to Resume.
  747. *
  748. * Use ACPI to put the given device into the full-power state and carry out the
  749. * generic runtime resume procedure for it.
  750. */
  751. int acpi_subsys_runtime_resume(struct device *dev)
  752. {
  753. int ret = acpi_dev_runtime_resume(dev);
  754. return ret ? ret : pm_generic_runtime_resume(dev);
  755. }
  756. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  757. #ifdef CONFIG_PM_SLEEP
  758. /**
  759. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  760. * @dev: Device to put into a low-power state.
  761. *
  762. * Put the given device into a low-power state during system transition to a
  763. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  764. * desired, choose the state to put the device into (this checks if system
  765. * wakeup is expected to work too), and set the power state of the device.
  766. */
  767. int acpi_dev_suspend_late(struct device *dev)
  768. {
  769. struct acpi_device *adev = ACPI_COMPANION(dev);
  770. u32 target_state;
  771. bool wakeup;
  772. int error;
  773. if (!adev)
  774. return 0;
  775. target_state = acpi_target_system_state();
  776. wakeup = device_may_wakeup(dev) && acpi_device_can_wakeup(adev);
  777. error = acpi_device_wakeup(adev, target_state, wakeup);
  778. if (wakeup && error)
  779. return error;
  780. error = acpi_dev_pm_low_power(dev, adev, target_state);
  781. if (error)
  782. acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false);
  783. return error;
  784. }
  785. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  786. /**
  787. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  788. * @dev: Device to put into the full-power state.
  789. *
  790. * Put the given device into the full-power state using the standard ACPI
  791. * mechanism during system transition to the working state. Set the power
  792. * state of the device to ACPI D0 and disable remote wakeup.
  793. */
  794. int acpi_dev_resume_early(struct device *dev)
  795. {
  796. struct acpi_device *adev = ACPI_COMPANION(dev);
  797. int error;
  798. if (!adev)
  799. return 0;
  800. error = acpi_dev_pm_full_power(adev);
  801. acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false);
  802. return error;
  803. }
  804. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  805. /**
  806. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  807. * @dev: Device to prepare.
  808. */
  809. int acpi_subsys_prepare(struct device *dev)
  810. {
  811. struct acpi_device *adev = ACPI_COMPANION(dev);
  812. u32 sys_target;
  813. int ret, state;
  814. ret = pm_generic_prepare(dev);
  815. if (ret < 0)
  816. return ret;
  817. if (!adev || !pm_runtime_suspended(dev)
  818. || device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
  819. return 0;
  820. sys_target = acpi_target_system_state();
  821. if (sys_target == ACPI_STATE_S0)
  822. return 1;
  823. if (adev->power.flags.dsw_present)
  824. return 0;
  825. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  826. return !ret && state == adev->power.state;
  827. }
  828. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  829. /**
  830. * acpi_subsys_suspend - Run the device driver's suspend callback.
  831. * @dev: Device to handle.
  832. *
  833. * Follow PCI and resume devices suspended at run time before running their
  834. * system suspend callbacks.
  835. */
  836. int acpi_subsys_suspend(struct device *dev)
  837. {
  838. pm_runtime_resume(dev);
  839. return pm_generic_suspend(dev);
  840. }
  841. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  842. /**
  843. * acpi_subsys_suspend_late - Suspend device using ACPI.
  844. * @dev: Device to suspend.
  845. *
  846. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  847. * it into a low-power state during system transition into a sleep state.
  848. */
  849. int acpi_subsys_suspend_late(struct device *dev)
  850. {
  851. int ret = pm_generic_suspend_late(dev);
  852. return ret ? ret : acpi_dev_suspend_late(dev);
  853. }
  854. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  855. /**
  856. * acpi_subsys_resume_early - Resume device using ACPI.
  857. * @dev: Device to Resume.
  858. *
  859. * Use ACPI to put the given device into the full-power state and carry out the
  860. * generic early resume procedure for it during system transition into the
  861. * working state.
  862. */
  863. int acpi_subsys_resume_early(struct device *dev)
  864. {
  865. int ret = acpi_dev_resume_early(dev);
  866. return ret ? ret : pm_generic_resume_early(dev);
  867. }
  868. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  869. /**
  870. * acpi_subsys_freeze - Run the device driver's freeze callback.
  871. * @dev: Device to handle.
  872. */
  873. int acpi_subsys_freeze(struct device *dev)
  874. {
  875. /*
  876. * This used to be done in acpi_subsys_prepare() for all devices and
  877. * some drivers may depend on it, so do it here. Ideally, however,
  878. * runtime-suspended devices should not be touched during freeze/thaw
  879. * transitions.
  880. */
  881. pm_runtime_resume(dev);
  882. return pm_generic_freeze(dev);
  883. }
  884. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  885. #endif /* CONFIG_PM_SLEEP */
  886. static struct dev_pm_domain acpi_general_pm_domain = {
  887. .ops = {
  888. .runtime_suspend = acpi_subsys_runtime_suspend,
  889. .runtime_resume = acpi_subsys_runtime_resume,
  890. #ifdef CONFIG_PM_SLEEP
  891. .prepare = acpi_subsys_prepare,
  892. .complete = pm_complete_with_resume_check,
  893. .suspend = acpi_subsys_suspend,
  894. .suspend_late = acpi_subsys_suspend_late,
  895. .resume_early = acpi_subsys_resume_early,
  896. .freeze = acpi_subsys_freeze,
  897. .poweroff = acpi_subsys_suspend,
  898. .poweroff_late = acpi_subsys_suspend_late,
  899. .restore_early = acpi_subsys_resume_early,
  900. #endif
  901. },
  902. };
  903. /**
  904. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  905. * @dev: Device to take care of.
  906. * @power_off: Whether or not to try to remove power from the device.
  907. *
  908. * Remove the device from the general ACPI PM domain and remove its wakeup
  909. * notifier. If @power_off is set, additionally remove power from the device if
  910. * possible.
  911. *
  912. * Callers must ensure proper synchronization of this function with power
  913. * management callbacks.
  914. */
  915. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  916. {
  917. struct acpi_device *adev = ACPI_COMPANION(dev);
  918. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  919. dev_pm_domain_set(dev, NULL);
  920. acpi_remove_pm_notifier(adev);
  921. if (power_off) {
  922. /*
  923. * If the device's PM QoS resume latency limit or flags
  924. * have been exposed to user space, they have to be
  925. * hidden at this point, so that they don't affect the
  926. * choice of the low-power state to put the device into.
  927. */
  928. dev_pm_qos_hide_latency_limit(dev);
  929. dev_pm_qos_hide_flags(dev);
  930. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  931. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  932. }
  933. }
  934. }
  935. /**
  936. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  937. * @dev: Device to prepare.
  938. * @power_on: Whether or not to power on the device.
  939. *
  940. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  941. * attached to it, install a wakeup notification handler for the device and
  942. * add it to the general ACPI PM domain. If @power_on is set, the device will
  943. * be put into the ACPI D0 state before the function returns.
  944. *
  945. * This assumes that the @dev's bus type uses generic power management callbacks
  946. * (or doesn't use any power management callbacks at all).
  947. *
  948. * Callers must ensure proper synchronization of this function with power
  949. * management callbacks.
  950. */
  951. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  952. {
  953. struct acpi_device *adev = ACPI_COMPANION(dev);
  954. if (!adev)
  955. return -ENODEV;
  956. if (dev->pm_domain)
  957. return -EEXIST;
  958. /*
  959. * Only attach the power domain to the first device if the
  960. * companion is shared by multiple. This is to prevent doing power
  961. * management twice.
  962. */
  963. if (!acpi_device_is_first_physical_node(adev, dev))
  964. return -EBUSY;
  965. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  966. dev_pm_domain_set(dev, &acpi_general_pm_domain);
  967. if (power_on) {
  968. acpi_dev_pm_full_power(adev);
  969. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  970. }
  971. dev->pm_domain->detach = acpi_dev_pm_detach;
  972. return 0;
  973. }
  974. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  975. #endif /* CONFIG_PM */