device_pm.c 29 KB

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