device_pm.c 32 KB

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