device_pm.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/acpi/device_pm.c - ACPI device power management routines.
  4. *
  5. * Copyright (C) 2012, Intel Corp.
  6. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. */
  12. #define pr_fmt(fmt) "PM: " fmt
  13. #include <linux/acpi.h>
  14. #include <linux/export.h>
  15. #include <linux/mutex.h>
  16. #include <linux/pm_qos.h>
  17. #include <linux/pm_domain.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/suspend.h>
  20. #include "fan.h"
  21. #include "internal.h"
  22. /**
  23. * acpi_power_state_string - String representation of ACPI device power state.
  24. * @state: ACPI device power state to return the string representation of.
  25. */
  26. const char *acpi_power_state_string(int state)
  27. {
  28. switch (state) {
  29. case ACPI_STATE_D0:
  30. return "D0";
  31. case ACPI_STATE_D1:
  32. return "D1";
  33. case ACPI_STATE_D2:
  34. return "D2";
  35. case ACPI_STATE_D3_HOT:
  36. return "D3hot";
  37. case ACPI_STATE_D3_COLD:
  38. return "D3cold";
  39. default:
  40. return "(unknown)";
  41. }
  42. }
  43. static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
  44. {
  45. unsigned long long psc;
  46. acpi_status status;
  47. status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
  48. if (ACPI_FAILURE(status))
  49. return -ENODEV;
  50. *state = psc;
  51. return 0;
  52. }
  53. /**
  54. * acpi_device_get_power - Get power state of an ACPI device.
  55. * @device: Device to get the power state of.
  56. * @state: Place to store the power state of the device.
  57. *
  58. * This function does not update the device's power.state field, but it may
  59. * update its parent's power.state field (when the parent's power state is
  60. * unknown and the device's power state turns out to be D0).
  61. *
  62. * Also, it does not update power resource reference counters to ensure that
  63. * the power state returned by it will be persistent and it may return a power
  64. * state shallower than previously set by acpi_device_set_power() for @device
  65. * (if that power state depends on any power resources).
  66. */
  67. int acpi_device_get_power(struct acpi_device *device, int *state)
  68. {
  69. int result = ACPI_STATE_UNKNOWN;
  70. struct acpi_device *parent;
  71. int error;
  72. if (!device || !state)
  73. return -EINVAL;
  74. parent = acpi_dev_parent(device);
  75. if (!device->flags.power_manageable) {
  76. /* TBD: Non-recursive algorithm for walking up hierarchy. */
  77. *state = parent ? parent->power.state : ACPI_STATE_D0;
  78. goto out;
  79. }
  80. /*
  81. * Get the device's power state from power resources settings and _PSC,
  82. * if available.
  83. */
  84. if (device->power.flags.power_resources) {
  85. error = acpi_power_get_inferred_state(device, &result);
  86. if (error)
  87. return error;
  88. }
  89. if (device->power.flags.explicit_get) {
  90. int psc;
  91. error = acpi_dev_pm_explicit_get(device, &psc);
  92. if (error)
  93. return error;
  94. /*
  95. * The power resources settings may indicate a power state
  96. * shallower than the actual power state of the device, because
  97. * the same power resources may be referenced by other devices.
  98. *
  99. * For systems predating ACPI 4.0 we assume that D3hot is the
  100. * deepest state that can be supported.
  101. */
  102. if (psc > result && psc < ACPI_STATE_D3_COLD)
  103. result = psc;
  104. else if (result == ACPI_STATE_UNKNOWN)
  105. result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
  106. }
  107. /*
  108. * If we were unsure about the device parent's power state up to this
  109. * point, the fact that the device is in D0 implies that the parent has
  110. * to be in D0 too, except if ignore_parent is set.
  111. */
  112. if (!device->power.flags.ignore_parent && parent &&
  113. parent->power.state == ACPI_STATE_UNKNOWN &&
  114. result == ACPI_STATE_D0)
  115. parent->power.state = ACPI_STATE_D0;
  116. *state = result;
  117. out:
  118. acpi_handle_debug(device->handle, "Power state: %s\n",
  119. acpi_power_state_string(*state));
  120. return 0;
  121. }
  122. static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
  123. {
  124. if (adev->power.states[state].flags.explicit_set) {
  125. char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
  126. acpi_status status;
  127. status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
  128. if (ACPI_FAILURE(status))
  129. return -ENODEV;
  130. }
  131. return 0;
  132. }
  133. /**
  134. * acpi_device_set_power - Set power state of an ACPI device.
  135. * @device: Device to set the power state of.
  136. * @state: New power state to set.
  137. *
  138. * Callers must ensure that the device is power manageable before using this
  139. * function.
  140. */
  141. int acpi_device_set_power(struct acpi_device *device, int state)
  142. {
  143. int target_state = state;
  144. int result = 0;
  145. if (!device || !device->flags.power_manageable
  146. || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  147. return -EINVAL;
  148. acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
  149. acpi_power_state_string(device->power.state),
  150. acpi_power_state_string(state));
  151. /* Make sure this is a valid target state */
  152. /* There is a special case for D0 addressed below. */
  153. if (state > ACPI_STATE_D0 && state == device->power.state)
  154. goto no_change;
  155. if (state == ACPI_STATE_D3_COLD) {
  156. /*
  157. * For transitions to D3cold we need to execute _PS3 and then
  158. * possibly drop references to the power resources in use.
  159. */
  160. state = ACPI_STATE_D3_HOT;
  161. /* If D3cold is not supported, use D3hot as the target state. */
  162. if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
  163. target_state = state;
  164. } else if (!device->power.states[state].flags.valid) {
  165. acpi_handle_debug(device->handle, "Power state %s not supported\n",
  166. acpi_power_state_string(state));
  167. return -ENODEV;
  168. }
  169. if (!device->power.flags.ignore_parent) {
  170. struct acpi_device *parent;
  171. parent = acpi_dev_parent(device);
  172. if (parent && state < parent->power.state) {
  173. acpi_handle_debug(device->handle,
  174. "Cannot transition to %s for parent in %s\n",
  175. acpi_power_state_string(state),
  176. acpi_power_state_string(parent->power.state));
  177. return -ENODEV;
  178. }
  179. }
  180. /*
  181. * Transition Power
  182. * ----------------
  183. * In accordance with ACPI 6, _PSx is executed before manipulating power
  184. * resources, unless the target state is D0, in which case _PS0 is
  185. * supposed to be executed after turning the power resources on.
  186. */
  187. if (state > ACPI_STATE_D0) {
  188. /*
  189. * According to ACPI 6, devices cannot go from lower-power
  190. * (deeper) states to higher-power (shallower) states.
  191. */
  192. if (state < device->power.state) {
  193. acpi_handle_debug(device->handle,
  194. "Cannot transition from %s to %s\n",
  195. acpi_power_state_string(device->power.state),
  196. acpi_power_state_string(state));
  197. return -ENODEV;
  198. }
  199. /*
  200. * If the device goes from D3hot to D3cold, _PS3 has been
  201. * evaluated for it already, so skip it in that case.
  202. */
  203. if (device->power.state < ACPI_STATE_D3_HOT) {
  204. result = acpi_dev_pm_explicit_set(device, state);
  205. if (result)
  206. goto end;
  207. }
  208. if (device->power.flags.power_resources)
  209. result = acpi_power_transition(device, target_state);
  210. } else {
  211. int cur_state = device->power.state;
  212. if (device->power.flags.power_resources) {
  213. result = acpi_power_transition(device, ACPI_STATE_D0);
  214. if (result)
  215. goto end;
  216. }
  217. if (cur_state == ACPI_STATE_D0) {
  218. int psc;
  219. /* Nothing to do here if _PSC is not present. */
  220. if (!device->power.flags.explicit_get)
  221. goto no_change;
  222. /*
  223. * The power state of the device was set to D0 last
  224. * time, but that might have happened before a
  225. * system-wide transition involving the platform
  226. * firmware, so it may be necessary to evaluate _PS0
  227. * for the device here. However, use extra care here
  228. * and evaluate _PSC to check the device's current power
  229. * state, and only invoke _PS0 if the evaluation of _PSC
  230. * is successful and it returns a power state different
  231. * from D0.
  232. */
  233. result = acpi_dev_pm_explicit_get(device, &psc);
  234. if (result || psc == ACPI_STATE_D0)
  235. goto no_change;
  236. }
  237. result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  238. }
  239. end:
  240. if (result) {
  241. acpi_handle_debug(device->handle,
  242. "Failed to change power state to %s\n",
  243. acpi_power_state_string(target_state));
  244. } else {
  245. device->power.state = target_state;
  246. acpi_handle_debug(device->handle, "Power state changed to %s\n",
  247. acpi_power_state_string(target_state));
  248. }
  249. return result;
  250. no_change:
  251. acpi_handle_debug(device->handle, "Already in %s\n",
  252. acpi_power_state_string(state));
  253. return 0;
  254. }
  255. EXPORT_SYMBOL(acpi_device_set_power);
  256. int acpi_bus_set_power(acpi_handle handle, int state)
  257. {
  258. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  259. if (device)
  260. return acpi_device_set_power(device, state);
  261. return -ENODEV;
  262. }
  263. EXPORT_SYMBOL(acpi_bus_set_power);
  264. int acpi_bus_init_power(struct acpi_device *device)
  265. {
  266. int state;
  267. int result;
  268. if (!device)
  269. return -EINVAL;
  270. device->power.state = ACPI_STATE_UNKNOWN;
  271. if (!acpi_device_is_present(device)) {
  272. device->flags.initialized = false;
  273. return -ENXIO;
  274. }
  275. result = acpi_device_get_power(device, &state);
  276. if (result)
  277. return result;
  278. if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
  279. /* Reference count the power resources. */
  280. result = acpi_power_on_resources(device, state);
  281. if (result)
  282. return result;
  283. if (state == ACPI_STATE_D0) {
  284. /*
  285. * If _PSC is not present and the state inferred from
  286. * power resources appears to be D0, it still may be
  287. * necessary to execute _PS0 at this point, because
  288. * another device using the same power resources may
  289. * have been put into D0 previously and that's why we
  290. * see D0 here.
  291. */
  292. result = acpi_dev_pm_explicit_set(device, state);
  293. if (result)
  294. return result;
  295. }
  296. } else if (state == ACPI_STATE_UNKNOWN) {
  297. /*
  298. * No power resources and missing _PSC? Cross fingers and make
  299. * it D0 in hope that this is what the BIOS put the device into.
  300. * [We tried to force D0 here by executing _PS0, but that broke
  301. * Toshiba P870-303 in a nasty way.]
  302. */
  303. state = ACPI_STATE_D0;
  304. }
  305. device->power.state = state;
  306. return 0;
  307. }
  308. /**
  309. * acpi_device_fix_up_power - Force device with missing _PSC into D0.
  310. * @device: Device object whose power state is to be fixed up.
  311. *
  312. * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
  313. * are assumed to be put into D0 by the BIOS. However, in some cases that may
  314. * not be the case and this function should be used then.
  315. */
  316. int acpi_device_fix_up_power(struct acpi_device *device)
  317. {
  318. int ret = 0;
  319. if (!device->power.flags.power_resources
  320. && !device->power.flags.explicit_get
  321. && device->power.state == ACPI_STATE_D0)
  322. ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  323. return ret;
  324. }
  325. EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
  326. static int fix_up_power_if_applicable(struct acpi_device *adev, void *not_used)
  327. {
  328. if (adev->status.present && adev->status.enabled)
  329. acpi_device_fix_up_power(adev);
  330. return 0;
  331. }
  332. /**
  333. * acpi_device_fix_up_power_extended - Force device and its children into D0.
  334. * @adev: Parent device object whose power state is to be fixed up.
  335. *
  336. * Call acpi_device_fix_up_power() for @adev and its children so long as they
  337. * are reported as present and enabled.
  338. */
  339. void acpi_device_fix_up_power_extended(struct acpi_device *adev)
  340. {
  341. acpi_device_fix_up_power(adev);
  342. acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL);
  343. }
  344. EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_extended);
  345. /**
  346. * acpi_device_fix_up_power_children - Force a device's children into D0.
  347. * @adev: Parent device object whose children's power state is to be fixed up.
  348. *
  349. * Call acpi_device_fix_up_power() for @adev's children so long as they
  350. * are reported as present and enabled.
  351. */
  352. void acpi_device_fix_up_power_children(struct acpi_device *adev)
  353. {
  354. acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL);
  355. }
  356. EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_children);
  357. int acpi_device_update_power(struct acpi_device *device, int *state_p)
  358. {
  359. int state;
  360. int result;
  361. if (device->power.state == ACPI_STATE_UNKNOWN) {
  362. result = acpi_bus_init_power(device);
  363. if (!result && state_p)
  364. *state_p = device->power.state;
  365. return result;
  366. }
  367. result = acpi_device_get_power(device, &state);
  368. if (result)
  369. return result;
  370. if (state == ACPI_STATE_UNKNOWN) {
  371. state = ACPI_STATE_D0;
  372. result = acpi_device_set_power(device, state);
  373. if (result)
  374. return result;
  375. } else {
  376. if (device->power.flags.power_resources) {
  377. /*
  378. * We don't need to really switch the state, bu we need
  379. * to update the power resources' reference counters.
  380. */
  381. result = acpi_power_transition(device, state);
  382. if (result)
  383. return result;
  384. }
  385. device->power.state = state;
  386. }
  387. if (state_p)
  388. *state_p = state;
  389. return 0;
  390. }
  391. EXPORT_SYMBOL_GPL(acpi_device_update_power);
  392. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  393. {
  394. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  395. if (device)
  396. return acpi_device_update_power(device, state_p);
  397. return -ENODEV;
  398. }
  399. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  400. bool acpi_bus_power_manageable(acpi_handle handle)
  401. {
  402. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  403. return device && device->flags.power_manageable;
  404. }
  405. EXPORT_SYMBOL(acpi_bus_power_manageable);
  406. static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used)
  407. {
  408. if (!(adev->flags.power_manageable && adev->pnp.type.bus_address))
  409. return 0;
  410. acpi_handle_debug(adev->handle, "Power state: %s\n",
  411. acpi_power_state_string(adev->power.state));
  412. if (adev->power.state == ACPI_STATE_D3_COLD)
  413. return acpi_device_set_power(adev, ACPI_STATE_D0);
  414. return 0;
  415. }
  416. /**
  417. * acpi_dev_power_up_children_with_adr - Power up childres with valid _ADR
  418. * @adev: Parent ACPI device object.
  419. *
  420. * Change the power states of the direct children of @adev that are in D3cold
  421. * and hold valid _ADR objects to D0 in order to allow bus (e.g. PCI)
  422. * enumeration code to access them.
  423. */
  424. void acpi_dev_power_up_children_with_adr(struct acpi_device *adev)
  425. {
  426. acpi_dev_for_each_child(adev, acpi_power_up_if_adr_present, NULL);
  427. }
  428. /**
  429. * acpi_dev_power_state_for_wake - Deepest power state for wakeup signaling
  430. * @adev: ACPI companion of the target device.
  431. *
  432. * Evaluate _S0W for @adev and return the value produced by it or return
  433. * ACPI_STATE_UNKNOWN on errors (including _S0W not present).
  434. */
  435. u8 acpi_dev_power_state_for_wake(struct acpi_device *adev)
  436. {
  437. unsigned long long state;
  438. acpi_status status;
  439. status = acpi_evaluate_integer(adev->handle, "_S0W", NULL, &state);
  440. if (ACPI_FAILURE(status))
  441. return ACPI_STATE_UNKNOWN;
  442. return state;
  443. }
  444. #ifdef CONFIG_PM
  445. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  446. static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
  447. void acpi_pm_wakeup_event(struct device *dev)
  448. {
  449. pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
  450. }
  451. EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
  452. static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
  453. {
  454. struct acpi_device *adev;
  455. if (val != ACPI_NOTIFY_DEVICE_WAKE)
  456. return;
  457. acpi_handle_debug(handle, "Wake notify\n");
  458. adev = acpi_get_acpi_dev(handle);
  459. if (!adev)
  460. return;
  461. mutex_lock(&acpi_pm_notifier_lock);
  462. if (adev->wakeup.flags.notifier_present) {
  463. pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
  464. if (adev->wakeup.context.func) {
  465. acpi_handle_debug(handle, "Running %pS for %s\n",
  466. adev->wakeup.context.func,
  467. dev_name(adev->wakeup.context.dev));
  468. adev->wakeup.context.func(&adev->wakeup.context);
  469. }
  470. }
  471. mutex_unlock(&acpi_pm_notifier_lock);
  472. acpi_put_acpi_dev(adev);
  473. }
  474. /**
  475. * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
  476. * @adev: ACPI device to add the notify handler for.
  477. * @dev: Device to generate a wakeup event for while handling the notification.
  478. * @func: Work function to execute when handling the notification.
  479. *
  480. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  481. * PM wakeup events. For example, wakeup events may be generated for bridges
  482. * if one of the devices below the bridge is signaling wakeup, even if the
  483. * bridge itself doesn't have a wakeup GPE associated with it.
  484. */
  485. acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
  486. void (*func)(struct acpi_device_wakeup_context *context))
  487. {
  488. acpi_status status = AE_ALREADY_EXISTS;
  489. if (!dev && !func)
  490. return AE_BAD_PARAMETER;
  491. mutex_lock(&acpi_pm_notifier_install_lock);
  492. if (adev->wakeup.flags.notifier_present)
  493. goto out;
  494. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  495. acpi_pm_notify_handler, NULL);
  496. if (ACPI_FAILURE(status))
  497. goto out;
  498. mutex_lock(&acpi_pm_notifier_lock);
  499. adev->wakeup.ws = wakeup_source_register(dev, dev_name(&adev->dev));
  500. adev->wakeup.context.dev = dev;
  501. adev->wakeup.context.func = func;
  502. adev->wakeup.flags.notifier_present = true;
  503. mutex_unlock(&acpi_pm_notifier_lock);
  504. out:
  505. mutex_unlock(&acpi_pm_notifier_install_lock);
  506. return status;
  507. }
  508. /**
  509. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  510. * @adev: ACPI device to remove the notifier from.
  511. */
  512. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
  513. {
  514. acpi_status status = AE_BAD_PARAMETER;
  515. mutex_lock(&acpi_pm_notifier_install_lock);
  516. if (!adev->wakeup.flags.notifier_present)
  517. goto out;
  518. status = acpi_remove_notify_handler(adev->handle,
  519. ACPI_SYSTEM_NOTIFY,
  520. acpi_pm_notify_handler);
  521. if (ACPI_FAILURE(status))
  522. goto out;
  523. mutex_lock(&acpi_pm_notifier_lock);
  524. adev->wakeup.context.func = NULL;
  525. adev->wakeup.context.dev = NULL;
  526. wakeup_source_unregister(adev->wakeup.ws);
  527. adev->wakeup.flags.notifier_present = false;
  528. mutex_unlock(&acpi_pm_notifier_lock);
  529. out:
  530. mutex_unlock(&acpi_pm_notifier_install_lock);
  531. return status;
  532. }
  533. bool acpi_bus_can_wakeup(acpi_handle handle)
  534. {
  535. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  536. return device && device->wakeup.flags.valid;
  537. }
  538. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  539. bool acpi_pm_device_can_wakeup(struct device *dev)
  540. {
  541. struct acpi_device *adev = ACPI_COMPANION(dev);
  542. return adev ? acpi_device_can_wakeup(adev) : false;
  543. }
  544. /**
  545. * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
  546. * @dev: Device whose preferred target power state to return.
  547. * @adev: ACPI device node corresponding to @dev.
  548. * @target_state: System state to match the resultant device state.
  549. * @d_min_p: Location to store the highest power state available to the device.
  550. * @d_max_p: Location to store the lowest power state available to the device.
  551. *
  552. * Find the lowest power (highest number) and highest power (lowest number) ACPI
  553. * device power states that the device can be in while the system is in the
  554. * state represented by @target_state. Store the integer numbers representing
  555. * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
  556. * respectively.
  557. *
  558. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  559. * actually corresponds to @dev before using this function.
  560. *
  561. * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
  562. * returns a value that doesn't make sense. The memory locations pointed to by
  563. * @d_max_p and @d_min_p are only modified on success.
  564. */
  565. static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
  566. u32 target_state, int *d_min_p, int *d_max_p)
  567. {
  568. char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
  569. acpi_handle handle = adev->handle;
  570. unsigned long long ret;
  571. int d_min, d_max;
  572. bool wakeup = false;
  573. bool has_sxd = false;
  574. acpi_status status;
  575. /*
  576. * If the system state is S0, the lowest power state the device can be
  577. * in is D3cold, unless the device has _S0W and is supposed to signal
  578. * wakeup, in which case the return value of _S0W has to be used as the
  579. * lowest power state available to the device.
  580. */
  581. d_min = ACPI_STATE_D0;
  582. d_max = ACPI_STATE_D3_COLD;
  583. /*
  584. * If present, _SxD methods return the minimum D-state (highest power
  585. * state) we can use for the corresponding S-states. Otherwise, the
  586. * minimum D-state is D0 (ACPI 3.x).
  587. */
  588. if (target_state > ACPI_STATE_S0) {
  589. /*
  590. * We rely on acpi_evaluate_integer() not clobbering the integer
  591. * provided if AE_NOT_FOUND is returned.
  592. */
  593. ret = d_min;
  594. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  595. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  596. || ret > ACPI_STATE_D3_COLD)
  597. return -ENODATA;
  598. /*
  599. * We need to handle legacy systems where D3hot and D3cold are
  600. * the same and 3 is returned in both cases, so fall back to
  601. * D3cold if D3hot is not a valid state.
  602. */
  603. if (!adev->power.states[ret].flags.valid) {
  604. if (ret == ACPI_STATE_D3_HOT)
  605. ret = ACPI_STATE_D3_COLD;
  606. else
  607. return -ENODATA;
  608. }
  609. if (status == AE_OK)
  610. has_sxd = true;
  611. d_min = ret;
  612. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  613. && adev->wakeup.sleep_state >= target_state;
  614. } else if (device_may_wakeup(dev) && dev->power.wakeirq) {
  615. /*
  616. * The ACPI subsystem doesn't manage the wake bit for IRQs
  617. * defined with ExclusiveAndWake and SharedAndWake. Instead we
  618. * expect them to be managed via the PM subsystem. Drivers
  619. * should call dev_pm_set_wake_irq to register an IRQ as a wake
  620. * source.
  621. *
  622. * If a device has a wake IRQ attached we need to check the
  623. * _S0W method to get the correct wake D-state. Otherwise we
  624. * end up putting the device into D3Cold which will more than
  625. * likely disable wake functionality.
  626. */
  627. wakeup = true;
  628. } else {
  629. /* ACPI GPE is specified in _PRW. */
  630. wakeup = adev->wakeup.flags.valid;
  631. }
  632. /*
  633. * If _PRW says we can wake up the system from the target sleep state,
  634. * the D-state returned by _SxD is sufficient for that (we assume a
  635. * wakeup-aware driver if wake is set). Still, if _SxW exists
  636. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  637. * can wake the system. _S0W may be valid, too.
  638. */
  639. if (wakeup) {
  640. method[3] = 'W';
  641. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  642. if (status == AE_NOT_FOUND) {
  643. /* No _SxW. In this case, the ACPI spec says that we
  644. * must not go into any power state deeper than the
  645. * value returned from _SxD.
  646. */
  647. if (has_sxd && target_state > ACPI_STATE_S0)
  648. d_max = d_min;
  649. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  650. /* Fall back to D3cold if ret is not a valid state. */
  651. if (!adev->power.states[ret].flags.valid)
  652. ret = ACPI_STATE_D3_COLD;
  653. d_max = ret > d_min ? ret : d_min;
  654. } else {
  655. return -ENODATA;
  656. }
  657. }
  658. if (d_min_p)
  659. *d_min_p = d_min;
  660. if (d_max_p)
  661. *d_max_p = d_max;
  662. return 0;
  663. }
  664. /**
  665. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  666. * @dev: Device whose preferred target power state to return.
  667. * @d_min_p: Location to store the upper limit of the allowed states range.
  668. * @d_max_in: Deepest low-power state to take into consideration.
  669. * Return value: Preferred power state of the device on success, -ENODEV
  670. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  671. * incorrect, or -ENODATA on ACPI method failure.
  672. *
  673. * The caller must ensure that @dev is valid before using this function.
  674. */
  675. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  676. {
  677. struct acpi_device *adev;
  678. int ret, d_min, d_max;
  679. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  680. return -EINVAL;
  681. if (d_max_in > ACPI_STATE_D2) {
  682. enum pm_qos_flags_status stat;
  683. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  684. if (stat == PM_QOS_FLAGS_ALL)
  685. d_max_in = ACPI_STATE_D2;
  686. }
  687. adev = ACPI_COMPANION(dev);
  688. if (!adev) {
  689. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  690. return -ENODEV;
  691. }
  692. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  693. &d_min, &d_max);
  694. if (ret)
  695. return ret;
  696. if (d_max_in < d_min)
  697. return -EINVAL;
  698. if (d_max > d_max_in) {
  699. for (d_max = d_max_in; d_max > d_min; d_max--) {
  700. if (adev->power.states[d_max].flags.valid)
  701. break;
  702. }
  703. }
  704. if (d_min_p)
  705. *d_min_p = d_min;
  706. return d_max;
  707. }
  708. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  709. /**
  710. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  711. * @context: Device wakeup context.
  712. */
  713. static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
  714. {
  715. struct device *dev = context->dev;
  716. if (dev) {
  717. pm_wakeup_event(dev, 0);
  718. pm_request_resume(dev);
  719. }
  720. }
  721. static DEFINE_MUTEX(acpi_wakeup_lock);
  722. static int __acpi_device_wakeup_enable(struct acpi_device *adev,
  723. u32 target_state)
  724. {
  725. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  726. acpi_status status;
  727. int error = 0;
  728. mutex_lock(&acpi_wakeup_lock);
  729. /*
  730. * If the device wakeup power is already enabled, disable it and enable
  731. * it again in case it depends on the configuration of subordinate
  732. * devices and the conditions have changed since it was enabled last
  733. * time.
  734. */
  735. if (wakeup->enable_count > 0)
  736. acpi_disable_wakeup_device_power(adev);
  737. error = acpi_enable_wakeup_device_power(adev, target_state);
  738. if (error) {
  739. if (wakeup->enable_count > 0) {
  740. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  741. wakeup->enable_count = 0;
  742. }
  743. goto out;
  744. }
  745. if (wakeup->enable_count > 0)
  746. goto inc;
  747. status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  748. if (ACPI_FAILURE(status)) {
  749. acpi_disable_wakeup_device_power(adev);
  750. error = -EIO;
  751. goto out;
  752. }
  753. acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
  754. (unsigned int)wakeup->gpe_number);
  755. inc:
  756. if (wakeup->enable_count < INT_MAX)
  757. wakeup->enable_count++;
  758. else
  759. acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
  760. out:
  761. mutex_unlock(&acpi_wakeup_lock);
  762. return error;
  763. }
  764. /**
  765. * acpi_device_wakeup_enable - Enable wakeup functionality for device.
  766. * @adev: ACPI device to enable wakeup functionality for.
  767. * @target_state: State the system is transitioning into.
  768. *
  769. * Enable the GPE associated with @adev so that it can generate wakeup signals
  770. * for the device in response to external (remote) events and enable wakeup
  771. * power for it.
  772. *
  773. * Callers must ensure that @adev is a valid ACPI device node before executing
  774. * this function.
  775. */
  776. static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
  777. {
  778. return __acpi_device_wakeup_enable(adev, target_state);
  779. }
  780. /**
  781. * acpi_device_wakeup_disable - Disable wakeup functionality for device.
  782. * @adev: ACPI device to disable wakeup functionality for.
  783. *
  784. * Disable the GPE associated with @adev and disable wakeup power for it.
  785. *
  786. * Callers must ensure that @adev is a valid ACPI device node before executing
  787. * this function.
  788. */
  789. static void acpi_device_wakeup_disable(struct acpi_device *adev)
  790. {
  791. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  792. mutex_lock(&acpi_wakeup_lock);
  793. if (!wakeup->enable_count)
  794. goto out;
  795. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  796. acpi_disable_wakeup_device_power(adev);
  797. wakeup->enable_count--;
  798. out:
  799. mutex_unlock(&acpi_wakeup_lock);
  800. }
  801. /**
  802. * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
  803. * @dev: Device to enable/disable to generate wakeup events.
  804. * @enable: Whether to enable or disable the wakeup functionality.
  805. */
  806. int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
  807. {
  808. struct acpi_device *adev;
  809. int error;
  810. adev = ACPI_COMPANION(dev);
  811. if (!adev) {
  812. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  813. return -ENODEV;
  814. }
  815. if (!acpi_device_can_wakeup(adev))
  816. return -EINVAL;
  817. if (!enable) {
  818. acpi_device_wakeup_disable(adev);
  819. dev_dbg(dev, "Wakeup disabled by ACPI\n");
  820. return 0;
  821. }
  822. error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
  823. if (!error)
  824. dev_dbg(dev, "Wakeup enabled by ACPI\n");
  825. return error;
  826. }
  827. EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
  828. /**
  829. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  830. * @dev: Device to put into a low-power state.
  831. * @adev: ACPI device node corresponding to @dev.
  832. * @system_state: System state to choose the device state for.
  833. */
  834. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  835. u32 system_state)
  836. {
  837. int ret, state;
  838. if (!acpi_device_power_manageable(adev))
  839. return 0;
  840. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  841. return ret ? ret : acpi_device_set_power(adev, state);
  842. }
  843. /**
  844. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  845. * @adev: ACPI device node to put into the full-power state.
  846. */
  847. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  848. {
  849. return acpi_device_power_manageable(adev) ?
  850. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  851. }
  852. /**
  853. * acpi_dev_suspend - Put device into a low-power state using ACPI.
  854. * @dev: Device to put into a low-power state.
  855. * @wakeup: Whether or not to enable wakeup for the device.
  856. *
  857. * Put the given device into a low-power state using the standard ACPI
  858. * mechanism. Set up remote wakeup if desired, choose the state to put the
  859. * device into (this checks if remote wakeup is expected to work too), and set
  860. * the power state of the device.
  861. */
  862. int acpi_dev_suspend(struct device *dev, bool wakeup)
  863. {
  864. struct acpi_device *adev = ACPI_COMPANION(dev);
  865. u32 target_state = acpi_target_system_state();
  866. int error;
  867. if (!adev)
  868. return 0;
  869. if (wakeup && acpi_device_can_wakeup(adev)) {
  870. error = acpi_device_wakeup_enable(adev, target_state);
  871. if (error)
  872. return -EAGAIN;
  873. } else {
  874. wakeup = false;
  875. }
  876. error = acpi_dev_pm_low_power(dev, adev, target_state);
  877. if (error && wakeup)
  878. acpi_device_wakeup_disable(adev);
  879. return error;
  880. }
  881. EXPORT_SYMBOL_GPL(acpi_dev_suspend);
  882. /**
  883. * acpi_dev_resume - Put device into the full-power state using ACPI.
  884. * @dev: Device to put into the full-power state.
  885. *
  886. * Put the given device into the full-power state using the standard ACPI
  887. * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
  888. */
  889. int acpi_dev_resume(struct device *dev)
  890. {
  891. struct acpi_device *adev = ACPI_COMPANION(dev);
  892. int error;
  893. if (!adev)
  894. return 0;
  895. error = acpi_dev_pm_full_power(adev);
  896. acpi_device_wakeup_disable(adev);
  897. return error;
  898. }
  899. EXPORT_SYMBOL_GPL(acpi_dev_resume);
  900. /**
  901. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  902. * @dev: Device to suspend.
  903. *
  904. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  905. * it into a runtime low-power state.
  906. */
  907. int acpi_subsys_runtime_suspend(struct device *dev)
  908. {
  909. int ret = pm_generic_runtime_suspend(dev);
  910. return ret ? ret : acpi_dev_suspend(dev, true);
  911. }
  912. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  913. /**
  914. * acpi_subsys_runtime_resume - Resume device using ACPI.
  915. * @dev: Device to Resume.
  916. *
  917. * Use ACPI to put the given device into the full-power state and carry out the
  918. * generic runtime resume procedure for it.
  919. */
  920. int acpi_subsys_runtime_resume(struct device *dev)
  921. {
  922. int ret = acpi_dev_resume(dev);
  923. return ret ? ret : pm_generic_runtime_resume(dev);
  924. }
  925. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  926. #ifdef CONFIG_PM_SLEEP
  927. static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
  928. {
  929. u32 sys_target = acpi_target_system_state();
  930. int ret, state;
  931. if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
  932. device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
  933. return true;
  934. if (sys_target == ACPI_STATE_S0)
  935. return false;
  936. if (adev->power.flags.dsw_present)
  937. return true;
  938. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  939. if (ret)
  940. return true;
  941. return state != adev->power.state;
  942. }
  943. /**
  944. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  945. * @dev: Device to prepare.
  946. */
  947. int acpi_subsys_prepare(struct device *dev)
  948. {
  949. struct acpi_device *adev = ACPI_COMPANION(dev);
  950. dev_pm_set_strict_midlayer(dev, true);
  951. if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
  952. int ret = dev->driver->pm->prepare(dev);
  953. if (ret < 0)
  954. return ret;
  955. if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
  956. return 0;
  957. }
  958. return !acpi_dev_needs_resume(dev, adev);
  959. }
  960. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  961. /**
  962. * acpi_subsys_complete - Finalize device's resume during system resume.
  963. * @dev: Device to handle.
  964. */
  965. void acpi_subsys_complete(struct device *dev)
  966. {
  967. pm_generic_complete(dev);
  968. /*
  969. * If the device had been runtime-suspended before the system went into
  970. * the sleep state it is going out of and it has never been resumed till
  971. * now, resume it in case the firmware powered it up.
  972. */
  973. if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
  974. pm_request_resume(dev);
  975. dev_pm_set_strict_midlayer(dev, false);
  976. }
  977. EXPORT_SYMBOL_GPL(acpi_subsys_complete);
  978. /**
  979. * acpi_subsys_suspend - Run the device driver's suspend callback.
  980. * @dev: Device to handle.
  981. *
  982. * Follow PCI and resume devices from runtime suspend before running their
  983. * system suspend callbacks, unless the driver can cope with runtime-suspended
  984. * devices during system suspend and there are no ACPI-specific reasons for
  985. * resuming them.
  986. */
  987. int acpi_subsys_suspend(struct device *dev)
  988. {
  989. if (!dev_pm_smart_suspend(dev) ||
  990. acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
  991. pm_runtime_resume(dev);
  992. return pm_generic_suspend(dev);
  993. }
  994. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  995. /**
  996. * acpi_subsys_suspend_late - Suspend device using ACPI.
  997. * @dev: Device to suspend.
  998. *
  999. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  1000. * it into a low-power state during system transition into a sleep state.
  1001. */
  1002. int acpi_subsys_suspend_late(struct device *dev)
  1003. {
  1004. int ret;
  1005. if (dev_pm_skip_suspend(dev))
  1006. return 0;
  1007. ret = pm_generic_suspend_late(dev);
  1008. return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
  1009. }
  1010. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  1011. /**
  1012. * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
  1013. * @dev: Device to suspend.
  1014. */
  1015. int acpi_subsys_suspend_noirq(struct device *dev)
  1016. {
  1017. int ret;
  1018. if (dev_pm_skip_suspend(dev))
  1019. return 0;
  1020. ret = pm_generic_suspend_noirq(dev);
  1021. if (ret)
  1022. return ret;
  1023. /*
  1024. * If the target system sleep state is suspend-to-idle, it is sufficient
  1025. * to check whether or not the device's wakeup settings are good for
  1026. * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
  1027. * acpi_subsys_complete() to take care of fixing up the device's state
  1028. * anyway, if need be.
  1029. */
  1030. if (device_can_wakeup(dev) && !device_may_wakeup(dev))
  1031. dev->power.may_skip_resume = false;
  1032. return 0;
  1033. }
  1034. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
  1035. /**
  1036. * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
  1037. * @dev: Device to handle.
  1038. */
  1039. static int acpi_subsys_resume_noirq(struct device *dev)
  1040. {
  1041. if (dev_pm_skip_resume(dev))
  1042. return 0;
  1043. return pm_generic_resume_noirq(dev);
  1044. }
  1045. /**
  1046. * acpi_subsys_resume_early - Resume device using ACPI.
  1047. * @dev: Device to Resume.
  1048. *
  1049. * Use ACPI to put the given device into the full-power state and carry out the
  1050. * generic early resume procedure for it during system transition into the
  1051. * working state, but only do that if device either defines early resume
  1052. * handler, or does not define power operations at all. Otherwise powering up
  1053. * of the device is postponed to the normal resume phase.
  1054. */
  1055. static int acpi_subsys_resume_early(struct device *dev)
  1056. {
  1057. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1058. int ret;
  1059. if (dev_pm_skip_resume(dev))
  1060. return 0;
  1061. if (pm && !pm->resume_early) {
  1062. dev_dbg(dev, "Postponing ACPI PM to normal resume stage\n");
  1063. return 0;
  1064. }
  1065. ret = acpi_dev_resume(dev);
  1066. return ret ? ret : pm_generic_resume_early(dev);
  1067. }
  1068. /**
  1069. * acpi_subsys_resume - Resume device using ACPI.
  1070. * @dev: Device to Resume.
  1071. *
  1072. * Use ACPI to put the given device into the full-power state if it has not been
  1073. * powered up during early resume phase, and carry out the generic resume
  1074. * procedure for it during system transition into the working state.
  1075. */
  1076. static int acpi_subsys_resume(struct device *dev)
  1077. {
  1078. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1079. int ret = 0;
  1080. if (!dev_pm_skip_resume(dev) && pm && !pm->resume_early) {
  1081. dev_dbg(dev, "Applying postponed ACPI PM\n");
  1082. ret = acpi_dev_resume(dev);
  1083. }
  1084. return ret ? ret : pm_generic_resume(dev);
  1085. }
  1086. /**
  1087. * acpi_subsys_freeze - Run the device driver's freeze callback.
  1088. * @dev: Device to handle.
  1089. */
  1090. int acpi_subsys_freeze(struct device *dev)
  1091. {
  1092. /*
  1093. * Resume all runtime-suspended devices before creating a snapshot
  1094. * image of system memory, because the restore kernel generally cannot
  1095. * be expected to always handle them consistently and they need to be
  1096. * put into the runtime-active metastate during system resume anyway,
  1097. * so it is better to ensure that the state saved in the image will be
  1098. * always consistent with that.
  1099. */
  1100. pm_runtime_resume(dev);
  1101. return pm_generic_freeze(dev);
  1102. }
  1103. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  1104. /**
  1105. * acpi_subsys_restore_early - Restore device using ACPI.
  1106. * @dev: Device to restore.
  1107. */
  1108. int acpi_subsys_restore_early(struct device *dev)
  1109. {
  1110. int ret = acpi_dev_resume(dev);
  1111. return ret ? ret : pm_generic_restore_early(dev);
  1112. }
  1113. EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
  1114. /**
  1115. * acpi_subsys_poweroff - Run the device driver's poweroff callback.
  1116. * @dev: Device to handle.
  1117. *
  1118. * Follow PCI and resume devices from runtime suspend before running their
  1119. * system poweroff callbacks, unless the driver can cope with runtime-suspended
  1120. * devices during system suspend and there are no ACPI-specific reasons for
  1121. * resuming them.
  1122. */
  1123. int acpi_subsys_poweroff(struct device *dev)
  1124. {
  1125. if (!dev_pm_smart_suspend(dev) ||
  1126. acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
  1127. pm_runtime_resume(dev);
  1128. return pm_generic_poweroff(dev);
  1129. }
  1130. EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
  1131. /**
  1132. * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
  1133. * @dev: Device to handle.
  1134. *
  1135. * Carry out the generic late poweroff procedure for @dev and use ACPI to put
  1136. * it into a low-power state during system transition into a sleep state.
  1137. */
  1138. static int acpi_subsys_poweroff_late(struct device *dev)
  1139. {
  1140. int ret;
  1141. if (dev_pm_skip_suspend(dev))
  1142. return 0;
  1143. ret = pm_generic_poweroff_late(dev);
  1144. if (ret)
  1145. return ret;
  1146. return acpi_dev_suspend(dev, device_may_wakeup(dev));
  1147. }
  1148. /**
  1149. * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
  1150. * @dev: Device to suspend.
  1151. */
  1152. static int acpi_subsys_poweroff_noirq(struct device *dev)
  1153. {
  1154. if (dev_pm_skip_suspend(dev))
  1155. return 0;
  1156. return pm_generic_poweroff_noirq(dev);
  1157. }
  1158. #endif /* CONFIG_PM_SLEEP */
  1159. static void acpi_dev_pm_detach(struct device *dev, bool power_off);
  1160. static struct dev_pm_domain acpi_general_pm_domain = {
  1161. .ops = {
  1162. .runtime_suspend = acpi_subsys_runtime_suspend,
  1163. .runtime_resume = acpi_subsys_runtime_resume,
  1164. #ifdef CONFIG_PM_SLEEP
  1165. .prepare = acpi_subsys_prepare,
  1166. .complete = acpi_subsys_complete,
  1167. .suspend = acpi_subsys_suspend,
  1168. .resume = acpi_subsys_resume,
  1169. .suspend_late = acpi_subsys_suspend_late,
  1170. .suspend_noirq = acpi_subsys_suspend_noirq,
  1171. .resume_noirq = acpi_subsys_resume_noirq,
  1172. .resume_early = acpi_subsys_resume_early,
  1173. .freeze = acpi_subsys_freeze,
  1174. .poweroff = acpi_subsys_poweroff,
  1175. .poweroff_late = acpi_subsys_poweroff_late,
  1176. .poweroff_noirq = acpi_subsys_poweroff_noirq,
  1177. .restore_early = acpi_subsys_restore_early,
  1178. #endif
  1179. },
  1180. .detach = acpi_dev_pm_detach,
  1181. };
  1182. /**
  1183. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  1184. * @dev: Device to take care of.
  1185. * @power_off: Whether or not to try to remove power from the device.
  1186. *
  1187. * Remove the device from the general ACPI PM domain and remove its wakeup
  1188. * notifier. If @power_off is set, additionally remove power from the device if
  1189. * possible.
  1190. *
  1191. * Callers must ensure proper synchronization of this function with power
  1192. * management callbacks.
  1193. */
  1194. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  1195. {
  1196. struct acpi_device *adev = ACPI_COMPANION(dev);
  1197. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  1198. dev_pm_domain_set(dev, NULL);
  1199. acpi_remove_pm_notifier(adev);
  1200. if (power_off) {
  1201. /*
  1202. * If the device's PM QoS resume latency limit or flags
  1203. * have been exposed to user space, they have to be
  1204. * hidden at this point, so that they don't affect the
  1205. * choice of the low-power state to put the device into.
  1206. */
  1207. dev_pm_qos_hide_latency_limit(dev);
  1208. dev_pm_qos_hide_flags(dev);
  1209. acpi_device_wakeup_disable(adev);
  1210. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  1211. }
  1212. }
  1213. }
  1214. /**
  1215. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  1216. * @dev: Device to prepare.
  1217. * @power_on: Whether or not to power on the device.
  1218. *
  1219. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  1220. * attached to it, install a wakeup notification handler for the device and
  1221. * add it to the general ACPI PM domain. If @power_on is set, the device will
  1222. * be put into the ACPI D0 state before the function returns.
  1223. *
  1224. * This assumes that the @dev's bus type uses generic power management callbacks
  1225. * (or doesn't use any power management callbacks at all).
  1226. *
  1227. * Callers must ensure proper synchronization of this function with power
  1228. * management callbacks.
  1229. */
  1230. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  1231. {
  1232. /*
  1233. * Skip devices whose ACPI companions match the device IDs below,
  1234. * because they require special power management handling incompatible
  1235. * with the generic ACPI PM domain.
  1236. */
  1237. static const struct acpi_device_id special_pm_ids[] = {
  1238. ACPI_FAN_DEVICE_IDS,
  1239. {}
  1240. };
  1241. struct acpi_device *adev = ACPI_COMPANION(dev);
  1242. if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
  1243. return 0;
  1244. /*
  1245. * Only attach the power domain to the first device if the
  1246. * companion is shared by multiple. This is to prevent doing power
  1247. * management twice.
  1248. */
  1249. if (!acpi_device_is_first_physical_node(adev, dev))
  1250. return 0;
  1251. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  1252. dev_pm_domain_set(dev, &acpi_general_pm_domain);
  1253. if (power_on) {
  1254. acpi_dev_pm_full_power(adev);
  1255. acpi_device_wakeup_disable(adev);
  1256. }
  1257. return 1;
  1258. }
  1259. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  1260. /**
  1261. * acpi_storage_d3 - Check if D3 should be used in the suspend path
  1262. * @dev: Device to check
  1263. *
  1264. * Return %true if the platform firmware wants @dev to be programmed
  1265. * into D3hot or D3cold (if supported) in the suspend path, or %false
  1266. * when there is no specific preference. On some platforms, if this
  1267. * hint is ignored, @dev may remain unresponsive after suspending the
  1268. * platform as a whole.
  1269. *
  1270. * Although the property has storage in the name it actually is
  1271. * applied to the PCIe slot and plugging in a non-storage device the
  1272. * same platform restrictions will likely apply.
  1273. */
  1274. bool acpi_storage_d3(struct device *dev)
  1275. {
  1276. struct acpi_device *adev = ACPI_COMPANION(dev);
  1277. u8 val;
  1278. if (force_storage_d3())
  1279. return true;
  1280. if (!adev)
  1281. return false;
  1282. if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable",
  1283. &val))
  1284. return false;
  1285. return val == 1;
  1286. }
  1287. EXPORT_SYMBOL_GPL(acpi_storage_d3);
  1288. /**
  1289. * acpi_dev_state_d0 - Tell if the device is in D0 power state
  1290. * @dev: Physical device the ACPI power state of which to check
  1291. *
  1292. * On a system without ACPI, return true. On a system with ACPI, return true if
  1293. * the current ACPI power state of the device is D0, or false otherwise.
  1294. *
  1295. * Note that the power state of a device is not well-defined after it has been
  1296. * passed to acpi_device_set_power() and before that function returns, so it is
  1297. * not valid to ask for the ACPI power state of the device in that time frame.
  1298. *
  1299. * This function is intended to be used in a driver's probe or remove
  1300. * function. See Documentation/firmware-guide/acpi/non-d0-probe.rst for
  1301. * more information.
  1302. */
  1303. bool acpi_dev_state_d0(struct device *dev)
  1304. {
  1305. struct acpi_device *adev = ACPI_COMPANION(dev);
  1306. if (!adev)
  1307. return true;
  1308. return adev->power.state == ACPI_STATE_D0;
  1309. }
  1310. EXPORT_SYMBOL_GPL(acpi_dev_state_d0);
  1311. #endif /* CONFIG_PM */