device_pm.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. /**
  255. * acpi_device_fix_up_power - Force device with missing _PSC into D0.
  256. * @device: Device object whose power state is to be fixed up.
  257. *
  258. * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
  259. * are assumed to be put into D0 by the BIOS. However, in some cases that may
  260. * not be the case and this function should be used then.
  261. */
  262. int acpi_device_fix_up_power(struct acpi_device *device)
  263. {
  264. int ret = 0;
  265. if (!device->power.flags.power_resources
  266. && !device->power.flags.explicit_get
  267. && device->power.state == ACPI_STATE_D0)
  268. ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  269. return ret;
  270. }
  271. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  272. {
  273. struct acpi_device *device;
  274. int state;
  275. int result;
  276. result = acpi_bus_get_device(handle, &device);
  277. if (result)
  278. return result;
  279. result = acpi_device_get_power(device, &state);
  280. if (result)
  281. return result;
  282. if (state == ACPI_STATE_UNKNOWN)
  283. state = ACPI_STATE_D0;
  284. result = acpi_device_set_power(device, state);
  285. if (!result && state_p)
  286. *state_p = state;
  287. return result;
  288. }
  289. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  290. bool acpi_bus_power_manageable(acpi_handle handle)
  291. {
  292. struct acpi_device *device;
  293. int result;
  294. result = acpi_bus_get_device(handle, &device);
  295. return result ? false : device->flags.power_manageable;
  296. }
  297. EXPORT_SYMBOL(acpi_bus_power_manageable);
  298. #ifdef CONFIG_PM
  299. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  300. /**
  301. * acpi_add_pm_notifier - Register PM notifier for given ACPI device.
  302. * @adev: ACPI device to add the notifier for.
  303. * @context: Context information to pass to the notifier routine.
  304. *
  305. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  306. * PM wakeup events. For example, wakeup events may be generated for bridges
  307. * if one of the devices below the bridge is signaling wakeup, even if the
  308. * bridge itself doesn't have a wakeup GPE associated with it.
  309. */
  310. acpi_status acpi_add_pm_notifier(struct acpi_device *adev,
  311. acpi_notify_handler handler, void *context)
  312. {
  313. acpi_status status = AE_ALREADY_EXISTS;
  314. mutex_lock(&acpi_pm_notifier_lock);
  315. if (adev->wakeup.flags.notifier_present)
  316. goto out;
  317. status = acpi_install_notify_handler(adev->handle,
  318. ACPI_SYSTEM_NOTIFY,
  319. handler, context);
  320. if (ACPI_FAILURE(status))
  321. goto out;
  322. adev->wakeup.flags.notifier_present = true;
  323. out:
  324. mutex_unlock(&acpi_pm_notifier_lock);
  325. return status;
  326. }
  327. /**
  328. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  329. * @adev: ACPI device to remove the notifier from.
  330. */
  331. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,
  332. acpi_notify_handler handler)
  333. {
  334. acpi_status status = AE_BAD_PARAMETER;
  335. mutex_lock(&acpi_pm_notifier_lock);
  336. if (!adev->wakeup.flags.notifier_present)
  337. goto out;
  338. status = acpi_remove_notify_handler(adev->handle,
  339. ACPI_SYSTEM_NOTIFY,
  340. handler);
  341. if (ACPI_FAILURE(status))
  342. goto out;
  343. adev->wakeup.flags.notifier_present = false;
  344. out:
  345. mutex_unlock(&acpi_pm_notifier_lock);
  346. return status;
  347. }
  348. bool acpi_bus_can_wakeup(acpi_handle handle)
  349. {
  350. struct acpi_device *device;
  351. int result;
  352. result = acpi_bus_get_device(handle, &device);
  353. return result ? false : device->wakeup.flags.valid;
  354. }
  355. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  356. /**
  357. * acpi_device_power_state - Get preferred power state of ACPI device.
  358. * @dev: Device whose preferred target power state to return.
  359. * @adev: ACPI device node corresponding to @dev.
  360. * @target_state: System state to match the resultant device state.
  361. * @d_max_in: Deepest low-power state to take into consideration.
  362. * @d_min_p: Location to store the upper limit of the allowed states range.
  363. * Return value: Preferred power state of the device on success, -ENODEV
  364. * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  365. *
  366. * Find the lowest power (highest number) ACPI device power state that the
  367. * device can be in while the system is in the state represented by
  368. * @target_state. If @d_min_p is set, the highest power (lowest number) device
  369. * power state that @dev can be in for the given system sleep state is stored
  370. * at the location pointed to by it.
  371. *
  372. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  373. * actually corresponds to @dev before using this function.
  374. */
  375. int acpi_device_power_state(struct device *dev, struct acpi_device *adev,
  376. u32 target_state, int d_max_in, int *d_min_p)
  377. {
  378. char acpi_method[] = "_SxD";
  379. unsigned long long d_min, d_max;
  380. bool wakeup = false;
  381. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
  382. return -EINVAL;
  383. if (d_max_in > ACPI_STATE_D3_HOT) {
  384. enum pm_qos_flags_status stat;
  385. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  386. if (stat == PM_QOS_FLAGS_ALL)
  387. d_max_in = ACPI_STATE_D3_HOT;
  388. }
  389. acpi_method[2] = '0' + target_state;
  390. /*
  391. * If the sleep state is S0, the lowest limit from ACPI is D3,
  392. * but if the device has _S0W, we will use the value from _S0W
  393. * as the lowest limit from ACPI. Finally, we will constrain
  394. * the lowest limit with the specified one.
  395. */
  396. d_min = ACPI_STATE_D0;
  397. d_max = ACPI_STATE_D3;
  398. /*
  399. * If present, _SxD methods return the minimum D-state (highest power
  400. * state) we can use for the corresponding S-states. Otherwise, the
  401. * minimum D-state is D0 (ACPI 3.x).
  402. *
  403. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  404. * provided -- that's our fault recovery, we ignore retval.
  405. */
  406. if (target_state > ACPI_STATE_S0) {
  407. acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min);
  408. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  409. && adev->wakeup.sleep_state >= target_state;
  410. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  411. PM_QOS_FLAGS_NONE) {
  412. wakeup = adev->wakeup.flags.valid;
  413. }
  414. /*
  415. * If _PRW says we can wake up the system from the target sleep state,
  416. * the D-state returned by _SxD is sufficient for that (we assume a
  417. * wakeup-aware driver if wake is set). Still, if _SxW exists
  418. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  419. * can wake the system. _S0W may be valid, too.
  420. */
  421. if (wakeup) {
  422. acpi_status status;
  423. acpi_method[3] = 'W';
  424. status = acpi_evaluate_integer(adev->handle, acpi_method, NULL,
  425. &d_max);
  426. if (ACPI_FAILURE(status)) {
  427. if (target_state != ACPI_STATE_S0 ||
  428. status != AE_NOT_FOUND)
  429. d_max = d_min;
  430. } else if (d_max < d_min) {
  431. /* Warn the user of the broken DSDT */
  432. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  433. acpi_method);
  434. /* Sanitize it */
  435. d_min = d_max;
  436. }
  437. }
  438. if (d_max_in < d_min)
  439. return -EINVAL;
  440. if (d_min_p)
  441. *d_min_p = d_min;
  442. /* constrain d_max with specified lowest limit (max number) */
  443. if (d_max > d_max_in) {
  444. for (d_max = d_max_in; d_max > d_min; d_max--) {
  445. if (adev->power.states[d_max].flags.valid)
  446. break;
  447. }
  448. }
  449. return d_max;
  450. }
  451. EXPORT_SYMBOL_GPL(acpi_device_power_state);
  452. /**
  453. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  454. * @dev: Device whose preferred target power state to return.
  455. * @d_min_p: Location to store the upper limit of the allowed states range.
  456. * @d_max_in: Deepest low-power state to take into consideration.
  457. * Return value: Preferred power state of the device on success, -ENODEV
  458. * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  459. *
  460. * The caller must ensure that @dev is valid before using this function.
  461. */
  462. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  463. {
  464. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  465. struct acpi_device *adev;
  466. if (!handle || acpi_bus_get_device(handle, &adev)) {
  467. dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
  468. return -ENODEV;
  469. }
  470. return acpi_device_power_state(dev, adev, acpi_target_system_state(),
  471. d_max_in, d_min_p);
  472. }
  473. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  474. #ifdef CONFIG_PM_RUNTIME
  475. /**
  476. * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
  477. * @handle: ACPI handle of the device the notification is for.
  478. * @event: Type of the signaled event.
  479. * @context: Device corresponding to @handle.
  480. */
  481. static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)
  482. {
  483. struct device *dev = context;
  484. if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {
  485. pm_wakeup_event(dev, 0);
  486. pm_runtime_resume(dev);
  487. }
  488. }
  489. /**
  490. * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
  491. * @adev: ACPI device to enable/disable the remote wakeup for.
  492. * @enable: Whether to enable or disable the wakeup functionality.
  493. *
  494. * Enable/disable the GPE associated with @adev so that it can generate
  495. * wakeup signals for the device in response to external (remote) events and
  496. * enable/disable device wakeup power.
  497. *
  498. * Callers must ensure that @adev is a valid ACPI device node before executing
  499. * this function.
  500. */
  501. int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
  502. {
  503. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  504. if (enable) {
  505. acpi_status res;
  506. int error;
  507. error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
  508. if (error)
  509. return error;
  510. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  511. if (ACPI_FAILURE(res)) {
  512. acpi_disable_wakeup_device_power(adev);
  513. return -EIO;
  514. }
  515. } else {
  516. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  517. acpi_disable_wakeup_device_power(adev);
  518. }
  519. return 0;
  520. }
  521. /**
  522. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  523. * @dev: Device to enable/disable the platform to wake up.
  524. * @enable: Whether to enable or disable the wakeup functionality.
  525. */
  526. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  527. {
  528. struct acpi_device *adev;
  529. acpi_handle handle;
  530. if (!device_run_wake(phys_dev))
  531. return -EINVAL;
  532. handle = DEVICE_ACPI_HANDLE(phys_dev);
  533. if (!handle || acpi_bus_get_device(handle, &adev)) {
  534. dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
  535. __func__);
  536. return -ENODEV;
  537. }
  538. return __acpi_device_run_wake(adev, enable);
  539. }
  540. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  541. #else
  542. static inline void acpi_wakeup_device(acpi_handle handle, u32 event,
  543. void *context) {}
  544. #endif /* CONFIG_PM_RUNTIME */
  545. #ifdef CONFIG_PM_SLEEP
  546. /**
  547. * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
  548. * @dev: Device to enable/desible to wake up the system.
  549. * @target_state: System state the device is supposed to wake up from.
  550. * @enable: Whether to enable or disable @dev to wake up the system.
  551. */
  552. int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
  553. bool enable)
  554. {
  555. return enable ?
  556. acpi_enable_wakeup_device_power(adev, target_state) :
  557. acpi_disable_wakeup_device_power(adev);
  558. }
  559. /**
  560. * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
  561. * @dev: Device to enable/desible to wake up the system from sleep states.
  562. * @enable: Whether to enable or disable @dev to wake up the system.
  563. */
  564. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  565. {
  566. acpi_handle handle;
  567. struct acpi_device *adev;
  568. int error;
  569. if (!device_can_wakeup(dev))
  570. return -EINVAL;
  571. handle = DEVICE_ACPI_HANDLE(dev);
  572. if (!handle || acpi_bus_get_device(handle, &adev)) {
  573. dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
  574. return -ENODEV;
  575. }
  576. error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
  577. enable);
  578. if (!error)
  579. dev_info(dev, "System wakeup %s by ACPI\n",
  580. enable ? "enabled" : "disabled");
  581. return error;
  582. }
  583. #endif /* CONFIG_PM_SLEEP */
  584. /**
  585. * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
  586. * @dev: Device to get the ACPI node for.
  587. */
  588. struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
  589. {
  590. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  591. struct acpi_device *adev;
  592. return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL;
  593. }
  594. /**
  595. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  596. * @dev: Device to put into a low-power state.
  597. * @adev: ACPI device node corresponding to @dev.
  598. * @system_state: System state to choose the device state for.
  599. */
  600. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  601. u32 system_state)
  602. {
  603. int power_state;
  604. if (!acpi_device_power_manageable(adev))
  605. return 0;
  606. power_state = acpi_device_power_state(dev, adev, system_state,
  607. ACPI_STATE_D3, NULL);
  608. if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3)
  609. return -EIO;
  610. return acpi_device_set_power(adev, power_state);
  611. }
  612. /**
  613. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  614. * @adev: ACPI device node to put into the full-power state.
  615. */
  616. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  617. {
  618. return acpi_device_power_manageable(adev) ?
  619. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  620. }
  621. #ifdef CONFIG_PM_RUNTIME
  622. /**
  623. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  624. * @dev: Device to put into a low-power state.
  625. *
  626. * Put the given device into a runtime low-power state using the standard ACPI
  627. * mechanism. Set up remote wakeup if desired, choose the state to put the
  628. * device into (this checks if remote wakeup is expected to work too), and set
  629. * the power state of the device.
  630. */
  631. int acpi_dev_runtime_suspend(struct device *dev)
  632. {
  633. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  634. bool remote_wakeup;
  635. int error;
  636. if (!adev)
  637. return 0;
  638. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  639. PM_QOS_FLAGS_NONE;
  640. error = __acpi_device_run_wake(adev, remote_wakeup);
  641. if (remote_wakeup && error)
  642. return -EAGAIN;
  643. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  644. if (error)
  645. __acpi_device_run_wake(adev, false);
  646. return error;
  647. }
  648. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  649. /**
  650. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  651. * @dev: Device to put into the full-power state.
  652. *
  653. * Put the given device into the full-power state using the standard ACPI
  654. * mechanism at run time. Set the power state of the device to ACPI D0 and
  655. * disable remote wakeup.
  656. */
  657. int acpi_dev_runtime_resume(struct device *dev)
  658. {
  659. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  660. int error;
  661. if (!adev)
  662. return 0;
  663. error = acpi_dev_pm_full_power(adev);
  664. __acpi_device_run_wake(adev, false);
  665. return error;
  666. }
  667. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  668. /**
  669. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  670. * @dev: Device to suspend.
  671. *
  672. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  673. * it into a runtime low-power state.
  674. */
  675. int acpi_subsys_runtime_suspend(struct device *dev)
  676. {
  677. int ret = pm_generic_runtime_suspend(dev);
  678. return ret ? ret : acpi_dev_runtime_suspend(dev);
  679. }
  680. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  681. /**
  682. * acpi_subsys_runtime_resume - Resume device using ACPI.
  683. * @dev: Device to Resume.
  684. *
  685. * Use ACPI to put the given device into the full-power state and carry out the
  686. * generic runtime resume procedure for it.
  687. */
  688. int acpi_subsys_runtime_resume(struct device *dev)
  689. {
  690. int ret = acpi_dev_runtime_resume(dev);
  691. return ret ? ret : pm_generic_runtime_resume(dev);
  692. }
  693. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  694. #endif /* CONFIG_PM_RUNTIME */
  695. #ifdef CONFIG_PM_SLEEP
  696. /**
  697. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  698. * @dev: Device to put into a low-power state.
  699. *
  700. * Put the given device into a low-power state during system transition to a
  701. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  702. * desired, choose the state to put the device into (this checks if system
  703. * wakeup is expected to work too), and set the power state of the device.
  704. */
  705. int acpi_dev_suspend_late(struct device *dev)
  706. {
  707. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  708. u32 target_state;
  709. bool wakeup;
  710. int error;
  711. if (!adev)
  712. return 0;
  713. target_state = acpi_target_system_state();
  714. wakeup = device_may_wakeup(dev);
  715. error = __acpi_device_sleep_wake(adev, target_state, wakeup);
  716. if (wakeup && error)
  717. return error;
  718. error = acpi_dev_pm_low_power(dev, adev, target_state);
  719. if (error)
  720. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  721. return error;
  722. }
  723. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  724. /**
  725. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  726. * @dev: Device to put into the full-power state.
  727. *
  728. * Put the given device into the full-power state using the standard ACPI
  729. * mechanism during system transition to the working state. Set the power
  730. * state of the device to ACPI D0 and disable remote wakeup.
  731. */
  732. int acpi_dev_resume_early(struct device *dev)
  733. {
  734. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  735. int error;
  736. if (!adev)
  737. return 0;
  738. error = acpi_dev_pm_full_power(adev);
  739. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  740. return error;
  741. }
  742. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  743. /**
  744. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  745. * @dev: Device to prepare.
  746. */
  747. int acpi_subsys_prepare(struct device *dev)
  748. {
  749. /*
  750. * Follow PCI and resume devices suspended at run time before running
  751. * their system suspend callbacks.
  752. */
  753. pm_runtime_resume(dev);
  754. return pm_generic_prepare(dev);
  755. }
  756. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  757. /**
  758. * acpi_subsys_suspend_late - Suspend device using ACPI.
  759. * @dev: Device to suspend.
  760. *
  761. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  762. * it into a low-power state during system transition into a sleep state.
  763. */
  764. int acpi_subsys_suspend_late(struct device *dev)
  765. {
  766. int ret = pm_generic_suspend_late(dev);
  767. return ret ? ret : acpi_dev_suspend_late(dev);
  768. }
  769. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  770. /**
  771. * acpi_subsys_resume_early - Resume device using ACPI.
  772. * @dev: Device to Resume.
  773. *
  774. * Use ACPI to put the given device into the full-power state and carry out the
  775. * generic early resume procedure for it during system transition into the
  776. * working state.
  777. */
  778. int acpi_subsys_resume_early(struct device *dev)
  779. {
  780. int ret = acpi_dev_resume_early(dev);
  781. return ret ? ret : pm_generic_resume_early(dev);
  782. }
  783. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  784. #endif /* CONFIG_PM_SLEEP */
  785. static struct dev_pm_domain acpi_general_pm_domain = {
  786. .ops = {
  787. #ifdef CONFIG_PM_RUNTIME
  788. .runtime_suspend = acpi_subsys_runtime_suspend,
  789. .runtime_resume = acpi_subsys_runtime_resume,
  790. .runtime_idle = pm_generic_runtime_idle,
  791. #endif
  792. #ifdef CONFIG_PM_SLEEP
  793. .prepare = acpi_subsys_prepare,
  794. .suspend_late = acpi_subsys_suspend_late,
  795. .resume_early = acpi_subsys_resume_early,
  796. .poweroff_late = acpi_subsys_suspend_late,
  797. .restore_early = acpi_subsys_resume_early,
  798. #endif
  799. },
  800. };
  801. /**
  802. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  803. * @dev: Device to prepare.
  804. * @power_on: Whether or not to power on the device.
  805. *
  806. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  807. * attached to it, install a wakeup notification handler for the device and
  808. * add it to the general ACPI PM domain. If @power_on is set, the device will
  809. * be put into the ACPI D0 state before the function returns.
  810. *
  811. * This assumes that the @dev's bus type uses generic power management callbacks
  812. * (or doesn't use any power management callbacks at all).
  813. *
  814. * Callers must ensure proper synchronization of this function with power
  815. * management callbacks.
  816. */
  817. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  818. {
  819. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  820. if (!adev)
  821. return -ENODEV;
  822. if (dev->pm_domain)
  823. return -EEXIST;
  824. acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
  825. dev->pm_domain = &acpi_general_pm_domain;
  826. if (power_on) {
  827. acpi_dev_pm_full_power(adev);
  828. __acpi_device_run_wake(adev, false);
  829. }
  830. return 0;
  831. }
  832. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  833. /**
  834. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  835. * @dev: Device to take care of.
  836. * @power_off: Whether or not to try to remove power from the device.
  837. *
  838. * Remove the device from the general ACPI PM domain and remove its wakeup
  839. * notifier. If @power_off is set, additionally remove power from the device if
  840. * possible.
  841. *
  842. * Callers must ensure proper synchronization of this function with power
  843. * management callbacks.
  844. */
  845. void acpi_dev_pm_detach(struct device *dev, bool power_off)
  846. {
  847. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  848. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  849. dev->pm_domain = NULL;
  850. acpi_remove_pm_notifier(adev, acpi_wakeup_device);
  851. if (power_off) {
  852. /*
  853. * If the device's PM QoS resume latency limit or flags
  854. * have been exposed to user space, they have to be
  855. * hidden at this point, so that they don't affect the
  856. * choice of the low-power state to put the device into.
  857. */
  858. dev_pm_qos_hide_latency_limit(dev);
  859. dev_pm_qos_hide_flags(dev);
  860. __acpi_device_run_wake(adev, false);
  861. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  862. }
  863. }
  864. }
  865. EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);
  866. /**
  867. * acpi_dev_pm_add_dependent - Add physical device depending for PM.
  868. * @handle: Handle of ACPI device node.
  869. * @depdev: Device depending on that node for PM.
  870. */
  871. void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev)
  872. {
  873. struct acpi_device_physical_node *dep;
  874. struct acpi_device *adev;
  875. if (!depdev || acpi_bus_get_device(handle, &adev))
  876. return;
  877. mutex_lock(&adev->physical_node_lock);
  878. list_for_each_entry(dep, &adev->power_dependent, node)
  879. if (dep->dev == depdev)
  880. goto out;
  881. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  882. if (dep) {
  883. dep->dev = depdev;
  884. list_add_tail(&dep->node, &adev->power_dependent);
  885. }
  886. out:
  887. mutex_unlock(&adev->physical_node_lock);
  888. }
  889. EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent);
  890. /**
  891. * acpi_dev_pm_remove_dependent - Remove physical device depending for PM.
  892. * @handle: Handle of ACPI device node.
  893. * @depdev: Device depending on that node for PM.
  894. */
  895. void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev)
  896. {
  897. struct acpi_device_physical_node *dep;
  898. struct acpi_device *adev;
  899. if (!depdev || acpi_bus_get_device(handle, &adev))
  900. return;
  901. mutex_lock(&adev->physical_node_lock);
  902. list_for_each_entry(dep, &adev->power_dependent, node)
  903. if (dep->dev == depdev) {
  904. list_del(&dep->node);
  905. kfree(dep);
  906. break;
  907. }
  908. mutex_unlock(&adev->physical_node_lock);
  909. }
  910. EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent);
  911. #endif /* CONFIG_PM */