device_pm.c 31 KB

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