device_pm.c 33 KB

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