device_pm.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. int acpi_device_update_power(struct acpi_device *device, int *state_p)
  284. {
  285. int state;
  286. int result;
  287. if (device->power.state == ACPI_STATE_UNKNOWN) {
  288. result = acpi_bus_init_power(device);
  289. if (!result && state_p)
  290. *state_p = device->power.state;
  291. return result;
  292. }
  293. result = acpi_device_get_power(device, &state);
  294. if (result)
  295. return result;
  296. if (state == ACPI_STATE_UNKNOWN) {
  297. state = ACPI_STATE_D0;
  298. result = acpi_device_set_power(device, state);
  299. if (result)
  300. return result;
  301. } else {
  302. if (device->power.flags.power_resources) {
  303. /*
  304. * We don't need to really switch the state, bu we need
  305. * to update the power resources' reference counters.
  306. */
  307. result = acpi_power_transition(device, state);
  308. if (result)
  309. return result;
  310. }
  311. device->power.state = state;
  312. }
  313. if (state_p)
  314. *state_p = state;
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(acpi_device_update_power);
  318. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  319. {
  320. struct acpi_device *device;
  321. int result;
  322. result = acpi_bus_get_device(handle, &device);
  323. return result ? result : acpi_device_update_power(device, state_p);
  324. }
  325. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  326. bool acpi_bus_power_manageable(acpi_handle handle)
  327. {
  328. struct acpi_device *device;
  329. int result;
  330. result = acpi_bus_get_device(handle, &device);
  331. return result ? false : device->flags.power_manageable;
  332. }
  333. EXPORT_SYMBOL(acpi_bus_power_manageable);
  334. #ifdef CONFIG_PM
  335. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  336. static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
  337. {
  338. struct acpi_device *adev;
  339. if (val != ACPI_NOTIFY_DEVICE_WAKE)
  340. return;
  341. adev = acpi_bus_get_acpi_device(handle);
  342. if (!adev)
  343. return;
  344. mutex_lock(&acpi_pm_notifier_lock);
  345. if (adev->wakeup.flags.notifier_present) {
  346. __pm_wakeup_event(adev->wakeup.ws, 0);
  347. if (adev->wakeup.context.work.func)
  348. queue_pm_work(&adev->wakeup.context.work);
  349. }
  350. mutex_unlock(&acpi_pm_notifier_lock);
  351. acpi_bus_put_acpi_device(adev);
  352. }
  353. /**
  354. * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
  355. * @adev: ACPI device to add the notify handler for.
  356. * @dev: Device to generate a wakeup event for while handling the notification.
  357. * @work_func: Work function to execute when handling the notification.
  358. *
  359. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  360. * PM wakeup events. For example, wakeup events may be generated for bridges
  361. * if one of the devices below the bridge is signaling wakeup, even if the
  362. * bridge itself doesn't have a wakeup GPE associated with it.
  363. */
  364. acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
  365. void (*work_func)(struct work_struct *work))
  366. {
  367. acpi_status status = AE_ALREADY_EXISTS;
  368. if (!dev && !work_func)
  369. return AE_BAD_PARAMETER;
  370. mutex_lock(&acpi_pm_notifier_lock);
  371. if (adev->wakeup.flags.notifier_present)
  372. goto out;
  373. adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
  374. adev->wakeup.context.dev = dev;
  375. if (work_func)
  376. INIT_WORK(&adev->wakeup.context.work, work_func);
  377. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  378. acpi_pm_notify_handler, NULL);
  379. if (ACPI_FAILURE(status))
  380. goto out;
  381. adev->wakeup.flags.notifier_present = true;
  382. out:
  383. mutex_unlock(&acpi_pm_notifier_lock);
  384. return status;
  385. }
  386. /**
  387. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  388. * @adev: ACPI device to remove the notifier from.
  389. */
  390. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
  391. {
  392. acpi_status status = AE_BAD_PARAMETER;
  393. mutex_lock(&acpi_pm_notifier_lock);
  394. if (!adev->wakeup.flags.notifier_present)
  395. goto out;
  396. status = acpi_remove_notify_handler(adev->handle,
  397. ACPI_SYSTEM_NOTIFY,
  398. acpi_pm_notify_handler);
  399. if (ACPI_FAILURE(status))
  400. goto out;
  401. if (adev->wakeup.context.work.func) {
  402. cancel_work_sync(&adev->wakeup.context.work);
  403. adev->wakeup.context.work.func = NULL;
  404. }
  405. adev->wakeup.context.dev = NULL;
  406. wakeup_source_unregister(adev->wakeup.ws);
  407. adev->wakeup.flags.notifier_present = false;
  408. out:
  409. mutex_unlock(&acpi_pm_notifier_lock);
  410. return status;
  411. }
  412. bool acpi_bus_can_wakeup(acpi_handle handle)
  413. {
  414. struct acpi_device *device;
  415. int result;
  416. result = acpi_bus_get_device(handle, &device);
  417. return result ? false : device->wakeup.flags.valid;
  418. }
  419. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  420. /**
  421. * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
  422. * @dev: Device whose preferred target power state to return.
  423. * @adev: ACPI device node corresponding to @dev.
  424. * @target_state: System state to match the resultant device state.
  425. * @d_min_p: Location to store the highest power state available to the device.
  426. * @d_max_p: Location to store the lowest power state available to the device.
  427. *
  428. * Find the lowest power (highest number) and highest power (lowest number) ACPI
  429. * device power states that the device can be in while the system is in the
  430. * state represented by @target_state. Store the integer numbers representing
  431. * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
  432. * respectively.
  433. *
  434. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  435. * actually corresponds to @dev before using this function.
  436. *
  437. * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
  438. * returns a value that doesn't make sense. The memory locations pointed to by
  439. * @d_max_p and @d_min_p are only modified on success.
  440. */
  441. static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
  442. u32 target_state, int *d_min_p, int *d_max_p)
  443. {
  444. char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
  445. acpi_handle handle = adev->handle;
  446. unsigned long long ret;
  447. int d_min, d_max;
  448. bool wakeup = false;
  449. acpi_status status;
  450. /*
  451. * If the system state is S0, the lowest power state the device can be
  452. * in is D3cold, unless the device has _S0W and is supposed to signal
  453. * wakeup, in which case the return value of _S0W has to be used as the
  454. * lowest power state available to the device.
  455. */
  456. d_min = ACPI_STATE_D0;
  457. d_max = ACPI_STATE_D3_COLD;
  458. /*
  459. * If present, _SxD methods return the minimum D-state (highest power
  460. * state) we can use for the corresponding S-states. Otherwise, the
  461. * minimum D-state is D0 (ACPI 3.x).
  462. */
  463. if (target_state > ACPI_STATE_S0) {
  464. /*
  465. * We rely on acpi_evaluate_integer() not clobbering the integer
  466. * provided if AE_NOT_FOUND is returned.
  467. */
  468. ret = d_min;
  469. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  470. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  471. || ret > ACPI_STATE_D3_COLD)
  472. return -ENODATA;
  473. /*
  474. * We need to handle legacy systems where D3hot and D3cold are
  475. * the same and 3 is returned in both cases, so fall back to
  476. * D3cold if D3hot is not a valid state.
  477. */
  478. if (!adev->power.states[ret].flags.valid) {
  479. if (ret == ACPI_STATE_D3_HOT)
  480. ret = ACPI_STATE_D3_COLD;
  481. else
  482. return -ENODATA;
  483. }
  484. d_min = ret;
  485. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  486. && adev->wakeup.sleep_state >= target_state;
  487. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  488. PM_QOS_FLAGS_NONE) {
  489. wakeup = adev->wakeup.flags.valid;
  490. }
  491. /*
  492. * If _PRW says we can wake up the system from the target sleep state,
  493. * the D-state returned by _SxD is sufficient for that (we assume a
  494. * wakeup-aware driver if wake is set). Still, if _SxW exists
  495. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  496. * can wake the system. _S0W may be valid, too.
  497. */
  498. if (wakeup) {
  499. method[3] = 'W';
  500. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  501. if (status == AE_NOT_FOUND) {
  502. if (target_state > ACPI_STATE_S0)
  503. d_max = d_min;
  504. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  505. /* Fall back to D3cold if ret is not a valid state. */
  506. if (!adev->power.states[ret].flags.valid)
  507. ret = ACPI_STATE_D3_COLD;
  508. d_max = ret > d_min ? ret : d_min;
  509. } else {
  510. return -ENODATA;
  511. }
  512. }
  513. if (d_min_p)
  514. *d_min_p = d_min;
  515. if (d_max_p)
  516. *d_max_p = d_max;
  517. return 0;
  518. }
  519. /**
  520. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  521. * @dev: Device whose preferred target power state to return.
  522. * @d_min_p: Location to store the upper limit of the allowed states range.
  523. * @d_max_in: Deepest low-power state to take into consideration.
  524. * Return value: Preferred power state of the device on success, -ENODEV
  525. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  526. * incorrect, or -ENODATA on ACPI method failure.
  527. *
  528. * The caller must ensure that @dev is valid before using this function.
  529. */
  530. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  531. {
  532. struct acpi_device *adev;
  533. int ret, d_min, d_max;
  534. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  535. return -EINVAL;
  536. if (d_max_in > ACPI_STATE_D2) {
  537. enum pm_qos_flags_status stat;
  538. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  539. if (stat == PM_QOS_FLAGS_ALL)
  540. d_max_in = ACPI_STATE_D2;
  541. }
  542. adev = ACPI_COMPANION(dev);
  543. if (!adev) {
  544. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  545. return -ENODEV;
  546. }
  547. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  548. &d_min, &d_max);
  549. if (ret)
  550. return ret;
  551. if (d_max_in < d_min)
  552. return -EINVAL;
  553. if (d_max > d_max_in) {
  554. for (d_max = d_max_in; d_max > d_min; d_max--) {
  555. if (adev->power.states[d_max].flags.valid)
  556. break;
  557. }
  558. }
  559. if (d_min_p)
  560. *d_min_p = d_min;
  561. return d_max;
  562. }
  563. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  564. /**
  565. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  566. * @work: Work item to handle.
  567. */
  568. static void acpi_pm_notify_work_func(struct work_struct *work)
  569. {
  570. struct device *dev;
  571. dev = container_of(work, struct acpi_device_wakeup_context, work)->dev;
  572. if (dev) {
  573. pm_wakeup_event(dev, 0);
  574. pm_runtime_resume(dev);
  575. }
  576. }
  577. /**
  578. * acpi_device_wakeup - Enable/disable wakeup functionality for device.
  579. * @adev: ACPI device to enable/disable wakeup functionality for.
  580. * @target_state: State the system is transitioning into.
  581. * @enable: Whether to enable or disable the wakeup functionality.
  582. *
  583. * Enable/disable the GPE associated with @adev so that it can generate
  584. * wakeup signals for the device in response to external (remote) events and
  585. * enable/disable device wakeup power.
  586. *
  587. * Callers must ensure that @adev is a valid ACPI device node before executing
  588. * this function.
  589. */
  590. static int acpi_device_wakeup(struct acpi_device *adev, u32 target_state,
  591. bool enable)
  592. {
  593. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  594. if (enable) {
  595. acpi_status res;
  596. int error;
  597. error = acpi_enable_wakeup_device_power(adev, target_state);
  598. if (error)
  599. return error;
  600. if (adev->wakeup.flags.enabled)
  601. return 0;
  602. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  603. if (ACPI_SUCCESS(res)) {
  604. adev->wakeup.flags.enabled = 1;
  605. } else {
  606. acpi_disable_wakeup_device_power(adev);
  607. return -EIO;
  608. }
  609. } else {
  610. if (adev->wakeup.flags.enabled) {
  611. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  612. adev->wakeup.flags.enabled = 0;
  613. }
  614. acpi_disable_wakeup_device_power(adev);
  615. }
  616. return 0;
  617. }
  618. /**
  619. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  620. * @dev: Device to enable/disable the platform to wake up.
  621. * @enable: Whether to enable or disable the wakeup functionality.
  622. */
  623. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  624. {
  625. struct acpi_device *adev;
  626. if (!device_run_wake(phys_dev))
  627. return -EINVAL;
  628. adev = ACPI_COMPANION(phys_dev);
  629. if (!adev) {
  630. dev_dbg(phys_dev, "ACPI companion missing in %s!\n", __func__);
  631. return -ENODEV;
  632. }
  633. return acpi_device_wakeup(adev, ACPI_STATE_S0, enable);
  634. }
  635. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  636. #ifdef CONFIG_PM_SLEEP
  637. /**
  638. * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
  639. * @dev: Device to enable/desible to wake up the system from sleep states.
  640. * @enable: Whether to enable or disable @dev to wake up the system.
  641. */
  642. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  643. {
  644. struct acpi_device *adev;
  645. int error;
  646. if (!device_can_wakeup(dev))
  647. return -EINVAL;
  648. adev = ACPI_COMPANION(dev);
  649. if (!adev) {
  650. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  651. return -ENODEV;
  652. }
  653. error = acpi_device_wakeup(adev, acpi_target_system_state(), enable);
  654. if (!error)
  655. dev_info(dev, "System wakeup %s by ACPI\n",
  656. enable ? "enabled" : "disabled");
  657. return error;
  658. }
  659. #endif /* CONFIG_PM_SLEEP */
  660. /**
  661. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  662. * @dev: Device to put into a low-power state.
  663. * @adev: ACPI device node corresponding to @dev.
  664. * @system_state: System state to choose the device state for.
  665. */
  666. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  667. u32 system_state)
  668. {
  669. int ret, state;
  670. if (!acpi_device_power_manageable(adev))
  671. return 0;
  672. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  673. return ret ? ret : acpi_device_set_power(adev, state);
  674. }
  675. /**
  676. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  677. * @adev: ACPI device node to put into the full-power state.
  678. */
  679. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  680. {
  681. return acpi_device_power_manageable(adev) ?
  682. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  683. }
  684. /**
  685. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  686. * @dev: Device to put into a low-power state.
  687. *
  688. * Put the given device into a runtime low-power state using the standard ACPI
  689. * mechanism. Set up remote wakeup if desired, choose the state to put the
  690. * device into (this checks if remote wakeup is expected to work too), and set
  691. * the power state of the device.
  692. */
  693. int acpi_dev_runtime_suspend(struct device *dev)
  694. {
  695. struct acpi_device *adev = ACPI_COMPANION(dev);
  696. bool remote_wakeup;
  697. int error;
  698. if (!adev)
  699. return 0;
  700. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  701. PM_QOS_FLAGS_NONE;
  702. error = acpi_device_wakeup(adev, ACPI_STATE_S0, remote_wakeup);
  703. if (remote_wakeup && error)
  704. return -EAGAIN;
  705. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  706. if (error)
  707. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  708. return error;
  709. }
  710. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  711. /**
  712. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  713. * @dev: Device to put into the full-power state.
  714. *
  715. * Put the given device into the full-power state using the standard ACPI
  716. * mechanism at run time. Set the power state of the device to ACPI D0 and
  717. * disable remote wakeup.
  718. */
  719. int acpi_dev_runtime_resume(struct device *dev)
  720. {
  721. struct acpi_device *adev = ACPI_COMPANION(dev);
  722. int error;
  723. if (!adev)
  724. return 0;
  725. error = acpi_dev_pm_full_power(adev);
  726. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  727. return error;
  728. }
  729. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  730. /**
  731. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  732. * @dev: Device to suspend.
  733. *
  734. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  735. * it into a runtime low-power state.
  736. */
  737. int acpi_subsys_runtime_suspend(struct device *dev)
  738. {
  739. int ret = pm_generic_runtime_suspend(dev);
  740. return ret ? ret : acpi_dev_runtime_suspend(dev);
  741. }
  742. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  743. /**
  744. * acpi_subsys_runtime_resume - Resume device using ACPI.
  745. * @dev: Device to Resume.
  746. *
  747. * Use ACPI to put the given device into the full-power state and carry out the
  748. * generic runtime resume procedure for it.
  749. */
  750. int acpi_subsys_runtime_resume(struct device *dev)
  751. {
  752. int ret = acpi_dev_runtime_resume(dev);
  753. return ret ? ret : pm_generic_runtime_resume(dev);
  754. }
  755. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  756. #ifdef CONFIG_PM_SLEEP
  757. /**
  758. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  759. * @dev: Device to put into a low-power state.
  760. *
  761. * Put the given device into a low-power state during system transition to a
  762. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  763. * desired, choose the state to put the device into (this checks if system
  764. * wakeup is expected to work too), and set the power state of the device.
  765. */
  766. int acpi_dev_suspend_late(struct device *dev)
  767. {
  768. struct acpi_device *adev = ACPI_COMPANION(dev);
  769. u32 target_state;
  770. bool wakeup;
  771. int error;
  772. if (!adev)
  773. return 0;
  774. target_state = acpi_target_system_state();
  775. wakeup = device_may_wakeup(dev) && acpi_device_can_wakeup(adev);
  776. error = acpi_device_wakeup(adev, target_state, wakeup);
  777. if (wakeup && error)
  778. return error;
  779. error = acpi_dev_pm_low_power(dev, adev, target_state);
  780. if (error)
  781. acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false);
  782. return error;
  783. }
  784. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  785. /**
  786. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  787. * @dev: Device to put into the full-power state.
  788. *
  789. * Put the given device into the full-power state using the standard ACPI
  790. * mechanism during system transition to the working state. Set the power
  791. * state of the device to ACPI D0 and disable remote wakeup.
  792. */
  793. int acpi_dev_resume_early(struct device *dev)
  794. {
  795. struct acpi_device *adev = ACPI_COMPANION(dev);
  796. int error;
  797. if (!adev)
  798. return 0;
  799. error = acpi_dev_pm_full_power(adev);
  800. acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false);
  801. return error;
  802. }
  803. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  804. /**
  805. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  806. * @dev: Device to prepare.
  807. */
  808. int acpi_subsys_prepare(struct device *dev)
  809. {
  810. struct acpi_device *adev = ACPI_COMPANION(dev);
  811. u32 sys_target;
  812. int ret, state;
  813. ret = pm_generic_prepare(dev);
  814. if (ret < 0)
  815. return ret;
  816. if (!adev || !pm_runtime_suspended(dev)
  817. || device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
  818. return 0;
  819. sys_target = acpi_target_system_state();
  820. if (sys_target == ACPI_STATE_S0)
  821. return 1;
  822. if (adev->power.flags.dsw_present)
  823. return 0;
  824. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  825. return !ret && state == adev->power.state;
  826. }
  827. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  828. /**
  829. * acpi_subsys_suspend - Run the device driver's suspend callback.
  830. * @dev: Device to handle.
  831. *
  832. * Follow PCI and resume devices suspended at run time before running their
  833. * system suspend callbacks.
  834. */
  835. int acpi_subsys_suspend(struct device *dev)
  836. {
  837. pm_runtime_resume(dev);
  838. return pm_generic_suspend(dev);
  839. }
  840. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  841. /**
  842. * acpi_subsys_suspend_late - Suspend device using ACPI.
  843. * @dev: Device to suspend.
  844. *
  845. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  846. * it into a low-power state during system transition into a sleep state.
  847. */
  848. int acpi_subsys_suspend_late(struct device *dev)
  849. {
  850. int ret = pm_generic_suspend_late(dev);
  851. return ret ? ret : acpi_dev_suspend_late(dev);
  852. }
  853. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  854. /**
  855. * acpi_subsys_resume_early - Resume device using ACPI.
  856. * @dev: Device to Resume.
  857. *
  858. * Use ACPI to put the given device into the full-power state and carry out the
  859. * generic early resume procedure for it during system transition into the
  860. * working state.
  861. */
  862. int acpi_subsys_resume_early(struct device *dev)
  863. {
  864. int ret = acpi_dev_resume_early(dev);
  865. return ret ? ret : pm_generic_resume_early(dev);
  866. }
  867. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  868. /**
  869. * acpi_subsys_freeze - Run the device driver's freeze callback.
  870. * @dev: Device to handle.
  871. */
  872. int acpi_subsys_freeze(struct device *dev)
  873. {
  874. /*
  875. * This used to be done in acpi_subsys_prepare() for all devices and
  876. * some drivers may depend on it, so do it here. Ideally, however,
  877. * runtime-suspended devices should not be touched during freeze/thaw
  878. * transitions.
  879. */
  880. pm_runtime_resume(dev);
  881. return pm_generic_freeze(dev);
  882. }
  883. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  884. #endif /* CONFIG_PM_SLEEP */
  885. static struct dev_pm_domain acpi_general_pm_domain = {
  886. .ops = {
  887. .runtime_suspend = acpi_subsys_runtime_suspend,
  888. .runtime_resume = acpi_subsys_runtime_resume,
  889. #ifdef CONFIG_PM_SLEEP
  890. .prepare = acpi_subsys_prepare,
  891. .complete = pm_complete_with_resume_check,
  892. .suspend = acpi_subsys_suspend,
  893. .suspend_late = acpi_subsys_suspend_late,
  894. .resume_early = acpi_subsys_resume_early,
  895. .freeze = acpi_subsys_freeze,
  896. .poweroff = acpi_subsys_suspend,
  897. .poweroff_late = acpi_subsys_suspend_late,
  898. .restore_early = acpi_subsys_resume_early,
  899. #endif
  900. },
  901. };
  902. /**
  903. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  904. * @dev: Device to take care of.
  905. * @power_off: Whether or not to try to remove power from the device.
  906. *
  907. * Remove the device from the general ACPI PM domain and remove its wakeup
  908. * notifier. If @power_off is set, additionally remove power from the device if
  909. * possible.
  910. *
  911. * Callers must ensure proper synchronization of this function with power
  912. * management callbacks.
  913. */
  914. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  915. {
  916. struct acpi_device *adev = ACPI_COMPANION(dev);
  917. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  918. dev_pm_domain_set(dev, NULL);
  919. acpi_remove_pm_notifier(adev);
  920. if (power_off) {
  921. /*
  922. * If the device's PM QoS resume latency limit or flags
  923. * have been exposed to user space, they have to be
  924. * hidden at this point, so that they don't affect the
  925. * choice of the low-power state to put the device into.
  926. */
  927. dev_pm_qos_hide_latency_limit(dev);
  928. dev_pm_qos_hide_flags(dev);
  929. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  930. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  931. }
  932. }
  933. }
  934. /**
  935. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  936. * @dev: Device to prepare.
  937. * @power_on: Whether or not to power on the device.
  938. *
  939. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  940. * attached to it, install a wakeup notification handler for the device and
  941. * add it to the general ACPI PM domain. If @power_on is set, the device will
  942. * be put into the ACPI D0 state before the function returns.
  943. *
  944. * This assumes that the @dev's bus type uses generic power management callbacks
  945. * (or doesn't use any power management callbacks at all).
  946. *
  947. * Callers must ensure proper synchronization of this function with power
  948. * management callbacks.
  949. */
  950. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  951. {
  952. struct acpi_device *adev = ACPI_COMPANION(dev);
  953. if (!adev)
  954. return -ENODEV;
  955. if (dev->pm_domain)
  956. return -EEXIST;
  957. /*
  958. * Only attach the power domain to the first device if the
  959. * companion is shared by multiple. This is to prevent doing power
  960. * management twice.
  961. */
  962. if (!acpi_device_is_first_physical_node(adev, dev))
  963. return -EBUSY;
  964. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  965. dev_pm_domain_set(dev, &acpi_general_pm_domain);
  966. if (power_on) {
  967. acpi_dev_pm_full_power(adev);
  968. acpi_device_wakeup(adev, ACPI_STATE_S0, false);
  969. }
  970. dev->pm_domain->detach = acpi_dev_pm_detach;
  971. return 0;
  972. }
  973. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  974. #endif /* CONFIG_PM */