device_pm.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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 <[email protected]>
  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. int acpi_device_update_power(struct acpi_device *device, int *state_p)
  346. {
  347. int state;
  348. int result;
  349. if (device->power.state == ACPI_STATE_UNKNOWN) {
  350. result = acpi_bus_init_power(device);
  351. if (!result && state_p)
  352. *state_p = device->power.state;
  353. return result;
  354. }
  355. result = acpi_device_get_power(device, &state);
  356. if (result)
  357. return result;
  358. if (state == ACPI_STATE_UNKNOWN) {
  359. state = ACPI_STATE_D0;
  360. result = acpi_device_set_power(device, state);
  361. if (result)
  362. return result;
  363. } else {
  364. if (device->power.flags.power_resources) {
  365. /*
  366. * We don't need to really switch the state, bu we need
  367. * to update the power resources' reference counters.
  368. */
  369. result = acpi_power_transition(device, state);
  370. if (result)
  371. return result;
  372. }
  373. device->power.state = state;
  374. }
  375. if (state_p)
  376. *state_p = state;
  377. return 0;
  378. }
  379. EXPORT_SYMBOL_GPL(acpi_device_update_power);
  380. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  381. {
  382. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  383. if (device)
  384. return acpi_device_update_power(device, state_p);
  385. return -ENODEV;
  386. }
  387. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  388. bool acpi_bus_power_manageable(acpi_handle handle)
  389. {
  390. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  391. return device && device->flags.power_manageable;
  392. }
  393. EXPORT_SYMBOL(acpi_bus_power_manageable);
  394. static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used)
  395. {
  396. if (!(adev->flags.power_manageable && adev->pnp.type.bus_address))
  397. return 0;
  398. acpi_handle_debug(adev->handle, "Power state: %s\n",
  399. acpi_power_state_string(adev->power.state));
  400. if (adev->power.state == ACPI_STATE_D3_COLD)
  401. return acpi_device_set_power(adev, ACPI_STATE_D0);
  402. return 0;
  403. }
  404. /**
  405. * acpi_dev_power_up_children_with_adr - Power up childres with valid _ADR
  406. * @adev: Parent ACPI device object.
  407. *
  408. * Change the power states of the direct children of @adev that are in D3cold
  409. * and hold valid _ADR objects to D0 in order to allow bus (e.g. PCI)
  410. * enumeration code to access them.
  411. */
  412. void acpi_dev_power_up_children_with_adr(struct acpi_device *adev)
  413. {
  414. acpi_dev_for_each_child(adev, acpi_power_up_if_adr_present, NULL);
  415. }
  416. /**
  417. * acpi_dev_power_state_for_wake - Deepest power state for wakeup signaling
  418. * @adev: ACPI companion of the target device.
  419. *
  420. * Evaluate _S0W for @adev and return the value produced by it or return
  421. * ACPI_STATE_UNKNOWN on errors (including _S0W not present).
  422. */
  423. u8 acpi_dev_power_state_for_wake(struct acpi_device *adev)
  424. {
  425. unsigned long long state;
  426. acpi_status status;
  427. status = acpi_evaluate_integer(adev->handle, "_S0W", NULL, &state);
  428. if (ACPI_FAILURE(status))
  429. return ACPI_STATE_UNKNOWN;
  430. return state;
  431. }
  432. #ifdef CONFIG_PM
  433. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  434. static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
  435. void acpi_pm_wakeup_event(struct device *dev)
  436. {
  437. pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
  438. }
  439. EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
  440. static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
  441. {
  442. struct acpi_device *adev;
  443. if (val != ACPI_NOTIFY_DEVICE_WAKE)
  444. return;
  445. acpi_handle_debug(handle, "Wake notify\n");
  446. adev = acpi_get_acpi_dev(handle);
  447. if (!adev)
  448. return;
  449. mutex_lock(&acpi_pm_notifier_lock);
  450. if (adev->wakeup.flags.notifier_present) {
  451. pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
  452. if (adev->wakeup.context.func) {
  453. acpi_handle_debug(handle, "Running %pS for %s\n",
  454. adev->wakeup.context.func,
  455. dev_name(adev->wakeup.context.dev));
  456. adev->wakeup.context.func(&adev->wakeup.context);
  457. }
  458. }
  459. mutex_unlock(&acpi_pm_notifier_lock);
  460. acpi_put_acpi_dev(adev);
  461. }
  462. /**
  463. * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
  464. * @adev: ACPI device to add the notify handler for.
  465. * @dev: Device to generate a wakeup event for while handling the notification.
  466. * @func: Work function to execute when handling the notification.
  467. *
  468. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  469. * PM wakeup events. For example, wakeup events may be generated for bridges
  470. * if one of the devices below the bridge is signaling wakeup, even if the
  471. * bridge itself doesn't have a wakeup GPE associated with it.
  472. */
  473. acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
  474. void (*func)(struct acpi_device_wakeup_context *context))
  475. {
  476. acpi_status status = AE_ALREADY_EXISTS;
  477. if (!dev && !func)
  478. return AE_BAD_PARAMETER;
  479. mutex_lock(&acpi_pm_notifier_install_lock);
  480. if (adev->wakeup.flags.notifier_present)
  481. goto out;
  482. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  483. acpi_pm_notify_handler, NULL);
  484. if (ACPI_FAILURE(status))
  485. goto out;
  486. mutex_lock(&acpi_pm_notifier_lock);
  487. adev->wakeup.ws = wakeup_source_register(&adev->dev,
  488. dev_name(&adev->dev));
  489. adev->wakeup.context.dev = dev;
  490. adev->wakeup.context.func = func;
  491. adev->wakeup.flags.notifier_present = true;
  492. mutex_unlock(&acpi_pm_notifier_lock);
  493. out:
  494. mutex_unlock(&acpi_pm_notifier_install_lock);
  495. return status;
  496. }
  497. /**
  498. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  499. * @adev: ACPI device to remove the notifier from.
  500. */
  501. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
  502. {
  503. acpi_status status = AE_BAD_PARAMETER;
  504. mutex_lock(&acpi_pm_notifier_install_lock);
  505. if (!adev->wakeup.flags.notifier_present)
  506. goto out;
  507. status = acpi_remove_notify_handler(adev->handle,
  508. ACPI_SYSTEM_NOTIFY,
  509. acpi_pm_notify_handler);
  510. if (ACPI_FAILURE(status))
  511. goto out;
  512. mutex_lock(&acpi_pm_notifier_lock);
  513. adev->wakeup.context.func = NULL;
  514. adev->wakeup.context.dev = NULL;
  515. wakeup_source_unregister(adev->wakeup.ws);
  516. adev->wakeup.flags.notifier_present = false;
  517. mutex_unlock(&acpi_pm_notifier_lock);
  518. out:
  519. mutex_unlock(&acpi_pm_notifier_install_lock);
  520. return status;
  521. }
  522. bool acpi_bus_can_wakeup(acpi_handle handle)
  523. {
  524. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  525. return device && device->wakeup.flags.valid;
  526. }
  527. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  528. bool acpi_pm_device_can_wakeup(struct device *dev)
  529. {
  530. struct acpi_device *adev = ACPI_COMPANION(dev);
  531. return adev ? acpi_device_can_wakeup(adev) : false;
  532. }
  533. /**
  534. * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
  535. * @dev: Device whose preferred target power state to return.
  536. * @adev: ACPI device node corresponding to @dev.
  537. * @target_state: System state to match the resultant device state.
  538. * @d_min_p: Location to store the highest power state available to the device.
  539. * @d_max_p: Location to store the lowest power state available to the device.
  540. *
  541. * Find the lowest power (highest number) and highest power (lowest number) ACPI
  542. * device power states that the device can be in while the system is in the
  543. * state represented by @target_state. Store the integer numbers representing
  544. * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
  545. * respectively.
  546. *
  547. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  548. * actually corresponds to @dev before using this function.
  549. *
  550. * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
  551. * returns a value that doesn't make sense. The memory locations pointed to by
  552. * @d_max_p and @d_min_p are only modified on success.
  553. */
  554. static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
  555. u32 target_state, int *d_min_p, int *d_max_p)
  556. {
  557. char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
  558. acpi_handle handle = adev->handle;
  559. unsigned long long ret;
  560. int d_min, d_max;
  561. bool wakeup = false;
  562. bool has_sxd = false;
  563. acpi_status status;
  564. /*
  565. * If the system state is S0, the lowest power state the device can be
  566. * in is D3cold, unless the device has _S0W and is supposed to signal
  567. * wakeup, in which case the return value of _S0W has to be used as the
  568. * lowest power state available to the device.
  569. */
  570. d_min = ACPI_STATE_D0;
  571. d_max = ACPI_STATE_D3_COLD;
  572. /*
  573. * If present, _SxD methods return the minimum D-state (highest power
  574. * state) we can use for the corresponding S-states. Otherwise, the
  575. * minimum D-state is D0 (ACPI 3.x).
  576. */
  577. if (target_state > ACPI_STATE_S0) {
  578. /*
  579. * We rely on acpi_evaluate_integer() not clobbering the integer
  580. * provided if AE_NOT_FOUND is returned.
  581. */
  582. ret = d_min;
  583. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  584. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  585. || ret > ACPI_STATE_D3_COLD)
  586. return -ENODATA;
  587. /*
  588. * We need to handle legacy systems where D3hot and D3cold are
  589. * the same and 3 is returned in both cases, so fall back to
  590. * D3cold if D3hot is not a valid state.
  591. */
  592. if (!adev->power.states[ret].flags.valid) {
  593. if (ret == ACPI_STATE_D3_HOT)
  594. ret = ACPI_STATE_D3_COLD;
  595. else
  596. return -ENODATA;
  597. }
  598. if (status == AE_OK)
  599. has_sxd = true;
  600. d_min = ret;
  601. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  602. && adev->wakeup.sleep_state >= target_state;
  603. } else if (device_may_wakeup(dev) && dev->power.wakeirq) {
  604. /*
  605. * The ACPI subsystem doesn't manage the wake bit for IRQs
  606. * defined with ExclusiveAndWake and SharedAndWake. Instead we
  607. * expect them to be managed via the PM subsystem. Drivers
  608. * should call dev_pm_set_wake_irq to register an IRQ as a wake
  609. * source.
  610. *
  611. * If a device has a wake IRQ attached we need to check the
  612. * _S0W method to get the correct wake D-state. Otherwise we
  613. * end up putting the device into D3Cold which will more than
  614. * likely disable wake functionality.
  615. */
  616. wakeup = true;
  617. } else {
  618. /* ACPI GPE is specified in _PRW. */
  619. wakeup = adev->wakeup.flags.valid;
  620. }
  621. /*
  622. * If _PRW says we can wake up the system from the target sleep state,
  623. * the D-state returned by _SxD is sufficient for that (we assume a
  624. * wakeup-aware driver if wake is set). Still, if _SxW exists
  625. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  626. * can wake the system. _S0W may be valid, too.
  627. */
  628. if (wakeup) {
  629. method[3] = 'W';
  630. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  631. if (status == AE_NOT_FOUND) {
  632. /* No _SxW. In this case, the ACPI spec says that we
  633. * must not go into any power state deeper than the
  634. * value returned from _SxD.
  635. */
  636. if (has_sxd && target_state > ACPI_STATE_S0)
  637. d_max = d_min;
  638. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  639. /* Fall back to D3cold if ret is not a valid state. */
  640. if (!adev->power.states[ret].flags.valid)
  641. ret = ACPI_STATE_D3_COLD;
  642. d_max = ret > d_min ? ret : d_min;
  643. } else {
  644. return -ENODATA;
  645. }
  646. }
  647. if (d_min_p)
  648. *d_min_p = d_min;
  649. if (d_max_p)
  650. *d_max_p = d_max;
  651. return 0;
  652. }
  653. /**
  654. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  655. * @dev: Device whose preferred target power state to return.
  656. * @d_min_p: Location to store the upper limit of the allowed states range.
  657. * @d_max_in: Deepest low-power state to take into consideration.
  658. * Return value: Preferred power state of the device on success, -ENODEV
  659. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  660. * incorrect, or -ENODATA on ACPI method failure.
  661. *
  662. * The caller must ensure that @dev is valid before using this function.
  663. */
  664. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  665. {
  666. struct acpi_device *adev;
  667. int ret, d_min, d_max;
  668. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  669. return -EINVAL;
  670. if (d_max_in > ACPI_STATE_D2) {
  671. enum pm_qos_flags_status stat;
  672. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  673. if (stat == PM_QOS_FLAGS_ALL)
  674. d_max_in = ACPI_STATE_D2;
  675. }
  676. adev = ACPI_COMPANION(dev);
  677. if (!adev) {
  678. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  679. return -ENODEV;
  680. }
  681. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  682. &d_min, &d_max);
  683. if (ret)
  684. return ret;
  685. if (d_max_in < d_min)
  686. return -EINVAL;
  687. if (d_max > d_max_in) {
  688. for (d_max = d_max_in; d_max > d_min; d_max--) {
  689. if (adev->power.states[d_max].flags.valid)
  690. break;
  691. }
  692. }
  693. if (d_min_p)
  694. *d_min_p = d_min;
  695. return d_max;
  696. }
  697. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  698. /**
  699. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  700. * @context: Device wakeup context.
  701. */
  702. static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
  703. {
  704. struct device *dev = context->dev;
  705. if (dev) {
  706. pm_wakeup_event(dev, 0);
  707. pm_request_resume(dev);
  708. }
  709. }
  710. static DEFINE_MUTEX(acpi_wakeup_lock);
  711. static int __acpi_device_wakeup_enable(struct acpi_device *adev,
  712. u32 target_state)
  713. {
  714. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  715. acpi_status status;
  716. int error = 0;
  717. mutex_lock(&acpi_wakeup_lock);
  718. /*
  719. * If the device wakeup power is already enabled, disable it and enable
  720. * it again in case it depends on the configuration of subordinate
  721. * devices and the conditions have changed since it was enabled last
  722. * time.
  723. */
  724. if (wakeup->enable_count > 0)
  725. acpi_disable_wakeup_device_power(adev);
  726. error = acpi_enable_wakeup_device_power(adev, target_state);
  727. if (error) {
  728. if (wakeup->enable_count > 0) {
  729. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  730. wakeup->enable_count = 0;
  731. }
  732. goto out;
  733. }
  734. if (wakeup->enable_count > 0)
  735. goto inc;
  736. status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  737. if (ACPI_FAILURE(status)) {
  738. acpi_disable_wakeup_device_power(adev);
  739. error = -EIO;
  740. goto out;
  741. }
  742. acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
  743. (unsigned int)wakeup->gpe_number);
  744. inc:
  745. if (wakeup->enable_count < INT_MAX)
  746. wakeup->enable_count++;
  747. else
  748. acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
  749. out:
  750. mutex_unlock(&acpi_wakeup_lock);
  751. return error;
  752. }
  753. /**
  754. * acpi_device_wakeup_enable - Enable wakeup functionality for device.
  755. * @adev: ACPI device to enable wakeup functionality for.
  756. * @target_state: State the system is transitioning into.
  757. *
  758. * Enable the GPE associated with @adev so that it can generate wakeup signals
  759. * for the device in response to external (remote) events and enable wakeup
  760. * power for it.
  761. *
  762. * Callers must ensure that @adev is a valid ACPI device node before executing
  763. * this function.
  764. */
  765. static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
  766. {
  767. return __acpi_device_wakeup_enable(adev, target_state);
  768. }
  769. /**
  770. * acpi_device_wakeup_disable - Disable wakeup functionality for device.
  771. * @adev: ACPI device to disable wakeup functionality for.
  772. *
  773. * Disable the GPE associated with @adev and disable wakeup power for it.
  774. *
  775. * Callers must ensure that @adev is a valid ACPI device node before executing
  776. * this function.
  777. */
  778. static void acpi_device_wakeup_disable(struct acpi_device *adev)
  779. {
  780. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  781. mutex_lock(&acpi_wakeup_lock);
  782. if (!wakeup->enable_count)
  783. goto out;
  784. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  785. acpi_disable_wakeup_device_power(adev);
  786. wakeup->enable_count--;
  787. out:
  788. mutex_unlock(&acpi_wakeup_lock);
  789. }
  790. /**
  791. * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
  792. * @dev: Device to enable/disable to generate wakeup events.
  793. * @enable: Whether to enable or disable the wakeup functionality.
  794. */
  795. int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
  796. {
  797. struct acpi_device *adev;
  798. int error;
  799. adev = ACPI_COMPANION(dev);
  800. if (!adev) {
  801. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  802. return -ENODEV;
  803. }
  804. if (!acpi_device_can_wakeup(adev))
  805. return -EINVAL;
  806. if (!enable) {
  807. acpi_device_wakeup_disable(adev);
  808. dev_dbg(dev, "Wakeup disabled by ACPI\n");
  809. return 0;
  810. }
  811. error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
  812. if (!error)
  813. dev_dbg(dev, "Wakeup enabled by ACPI\n");
  814. return error;
  815. }
  816. EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
  817. /**
  818. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  819. * @dev: Device to put into a low-power state.
  820. * @adev: ACPI device node corresponding to @dev.
  821. * @system_state: System state to choose the device state for.
  822. */
  823. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  824. u32 system_state)
  825. {
  826. int ret, state;
  827. if (!acpi_device_power_manageable(adev))
  828. return 0;
  829. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  830. return ret ? ret : acpi_device_set_power(adev, state);
  831. }
  832. /**
  833. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  834. * @adev: ACPI device node to put into the full-power state.
  835. */
  836. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  837. {
  838. return acpi_device_power_manageable(adev) ?
  839. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  840. }
  841. /**
  842. * acpi_dev_suspend - Put device into a low-power state using ACPI.
  843. * @dev: Device to put into a low-power state.
  844. * @wakeup: Whether or not to enable wakeup for the device.
  845. *
  846. * Put the given device into a low-power state using the standard ACPI
  847. * mechanism. Set up remote wakeup if desired, choose the state to put the
  848. * device into (this checks if remote wakeup is expected to work too), and set
  849. * the power state of the device.
  850. */
  851. int acpi_dev_suspend(struct device *dev, bool wakeup)
  852. {
  853. struct acpi_device *adev = ACPI_COMPANION(dev);
  854. u32 target_state = acpi_target_system_state();
  855. int error;
  856. if (!adev)
  857. return 0;
  858. if (wakeup && acpi_device_can_wakeup(adev)) {
  859. error = acpi_device_wakeup_enable(adev, target_state);
  860. if (error)
  861. return -EAGAIN;
  862. } else {
  863. wakeup = false;
  864. }
  865. error = acpi_dev_pm_low_power(dev, adev, target_state);
  866. if (error && wakeup)
  867. acpi_device_wakeup_disable(adev);
  868. return error;
  869. }
  870. EXPORT_SYMBOL_GPL(acpi_dev_suspend);
  871. /**
  872. * acpi_dev_resume - Put device into the full-power state using ACPI.
  873. * @dev: Device to put into the full-power state.
  874. *
  875. * Put the given device into the full-power state using the standard ACPI
  876. * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
  877. */
  878. int acpi_dev_resume(struct device *dev)
  879. {
  880. struct acpi_device *adev = ACPI_COMPANION(dev);
  881. int error;
  882. if (!adev)
  883. return 0;
  884. error = acpi_dev_pm_full_power(adev);
  885. acpi_device_wakeup_disable(adev);
  886. return error;
  887. }
  888. EXPORT_SYMBOL_GPL(acpi_dev_resume);
  889. /**
  890. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  891. * @dev: Device to suspend.
  892. *
  893. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  894. * it into a runtime low-power state.
  895. */
  896. int acpi_subsys_runtime_suspend(struct device *dev)
  897. {
  898. int ret = pm_generic_runtime_suspend(dev);
  899. return ret ? ret : acpi_dev_suspend(dev, true);
  900. }
  901. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  902. /**
  903. * acpi_subsys_runtime_resume - Resume device using ACPI.
  904. * @dev: Device to Resume.
  905. *
  906. * Use ACPI to put the given device into the full-power state and carry out the
  907. * generic runtime resume procedure for it.
  908. */
  909. int acpi_subsys_runtime_resume(struct device *dev)
  910. {
  911. int ret = acpi_dev_resume(dev);
  912. return ret ? ret : pm_generic_runtime_resume(dev);
  913. }
  914. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  915. #ifdef CONFIG_PM_SLEEP
  916. static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
  917. {
  918. u32 sys_target = acpi_target_system_state();
  919. int ret, state;
  920. if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
  921. device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
  922. return true;
  923. if (sys_target == ACPI_STATE_S0)
  924. return false;
  925. if (adev->power.flags.dsw_present)
  926. return true;
  927. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  928. if (ret)
  929. return true;
  930. return state != adev->power.state;
  931. }
  932. /**
  933. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  934. * @dev: Device to prepare.
  935. */
  936. int acpi_subsys_prepare(struct device *dev)
  937. {
  938. struct acpi_device *adev = ACPI_COMPANION(dev);
  939. if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
  940. int ret = dev->driver->pm->prepare(dev);
  941. if (ret < 0)
  942. return ret;
  943. if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
  944. return 0;
  945. }
  946. return !acpi_dev_needs_resume(dev, adev);
  947. }
  948. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  949. /**
  950. * acpi_subsys_complete - Finalize device's resume during system resume.
  951. * @dev: Device to handle.
  952. */
  953. void acpi_subsys_complete(struct device *dev)
  954. {
  955. pm_generic_complete(dev);
  956. /*
  957. * If the device had been runtime-suspended before the system went into
  958. * the sleep state it is going out of and it has never been resumed till
  959. * now, resume it in case the firmware powered it up.
  960. */
  961. if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
  962. pm_request_resume(dev);
  963. }
  964. EXPORT_SYMBOL_GPL(acpi_subsys_complete);
  965. /**
  966. * acpi_subsys_suspend - Run the device driver's suspend callback.
  967. * @dev: Device to handle.
  968. *
  969. * Follow PCI and resume devices from runtime suspend before running their
  970. * system suspend callbacks, unless the driver can cope with runtime-suspended
  971. * devices during system suspend and there are no ACPI-specific reasons for
  972. * resuming them.
  973. */
  974. int acpi_subsys_suspend(struct device *dev)
  975. {
  976. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  977. acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
  978. pm_runtime_resume(dev);
  979. return pm_generic_suspend(dev);
  980. }
  981. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  982. /**
  983. * acpi_subsys_suspend_late - Suspend device using ACPI.
  984. * @dev: Device to suspend.
  985. *
  986. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  987. * it into a low-power state during system transition into a sleep state.
  988. */
  989. int acpi_subsys_suspend_late(struct device *dev)
  990. {
  991. int ret;
  992. if (dev_pm_skip_suspend(dev))
  993. return 0;
  994. ret = pm_generic_suspend_late(dev);
  995. return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
  996. }
  997. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  998. /**
  999. * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
  1000. * @dev: Device to suspend.
  1001. */
  1002. int acpi_subsys_suspend_noirq(struct device *dev)
  1003. {
  1004. int ret;
  1005. if (dev_pm_skip_suspend(dev))
  1006. return 0;
  1007. ret = pm_generic_suspend_noirq(dev);
  1008. if (ret)
  1009. return ret;
  1010. /*
  1011. * If the target system sleep state is suspend-to-idle, it is sufficient
  1012. * to check whether or not the device's wakeup settings are good for
  1013. * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
  1014. * acpi_subsys_complete() to take care of fixing up the device's state
  1015. * anyway, if need be.
  1016. */
  1017. if (device_can_wakeup(dev) && !device_may_wakeup(dev))
  1018. dev->power.may_skip_resume = false;
  1019. return 0;
  1020. }
  1021. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
  1022. /**
  1023. * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
  1024. * @dev: Device to handle.
  1025. */
  1026. static int acpi_subsys_resume_noirq(struct device *dev)
  1027. {
  1028. if (dev_pm_skip_resume(dev))
  1029. return 0;
  1030. return pm_generic_resume_noirq(dev);
  1031. }
  1032. /**
  1033. * acpi_subsys_resume_early - Resume device using ACPI.
  1034. * @dev: Device to Resume.
  1035. *
  1036. * Use ACPI to put the given device into the full-power state and carry out the
  1037. * generic early resume procedure for it during system transition into the
  1038. * working state, but only do that if device either defines early resume
  1039. * handler, or does not define power operations at all. Otherwise powering up
  1040. * of the device is postponed to the normal resume phase.
  1041. */
  1042. static int acpi_subsys_resume_early(struct device *dev)
  1043. {
  1044. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1045. int ret;
  1046. if (dev_pm_skip_resume(dev))
  1047. return 0;
  1048. if (pm && !pm->resume_early) {
  1049. dev_dbg(dev, "postponing D0 transition to normal resume stage\n");
  1050. return 0;
  1051. }
  1052. ret = acpi_dev_resume(dev);
  1053. return ret ? ret : pm_generic_resume_early(dev);
  1054. }
  1055. /**
  1056. * acpi_subsys_resume - Resume device using ACPI.
  1057. * @dev: Device to Resume.
  1058. *
  1059. * Use ACPI to put the given device into the full-power state if it has not been
  1060. * powered up during early resume phase, and carry out the generic resume
  1061. * procedure for it during system transition into the working state.
  1062. */
  1063. static int acpi_subsys_resume(struct device *dev)
  1064. {
  1065. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  1066. int ret = 0;
  1067. if (!dev_pm_skip_resume(dev) && pm && !pm->resume_early) {
  1068. dev_dbg(dev, "executing postponed D0 transition\n");
  1069. ret = acpi_dev_resume(dev);
  1070. }
  1071. return ret ? ret : pm_generic_resume(dev);
  1072. }
  1073. /**
  1074. * acpi_subsys_freeze - Run the device driver's freeze callback.
  1075. * @dev: Device to handle.
  1076. */
  1077. int acpi_subsys_freeze(struct device *dev)
  1078. {
  1079. /*
  1080. * Resume all runtime-suspended devices before creating a snapshot
  1081. * image of system memory, because the restore kernel generally cannot
  1082. * be expected to always handle them consistently and they need to be
  1083. * put into the runtime-active metastate during system resume anyway,
  1084. * so it is better to ensure that the state saved in the image will be
  1085. * always consistent with that.
  1086. */
  1087. pm_runtime_resume(dev);
  1088. return pm_generic_freeze(dev);
  1089. }
  1090. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  1091. /**
  1092. * acpi_subsys_restore_early - Restore device using ACPI.
  1093. * @dev: Device to restore.
  1094. */
  1095. int acpi_subsys_restore_early(struct device *dev)
  1096. {
  1097. int ret = acpi_dev_resume(dev);
  1098. return ret ? ret : pm_generic_restore_early(dev);
  1099. }
  1100. EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
  1101. /**
  1102. * acpi_subsys_poweroff - Run the device driver's poweroff callback.
  1103. * @dev: Device to handle.
  1104. *
  1105. * Follow PCI and resume devices from runtime suspend before running their
  1106. * system poweroff callbacks, unless the driver can cope with runtime-suspended
  1107. * devices during system suspend and there are no ACPI-specific reasons for
  1108. * resuming them.
  1109. */
  1110. int acpi_subsys_poweroff(struct device *dev)
  1111. {
  1112. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  1113. acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
  1114. pm_runtime_resume(dev);
  1115. return pm_generic_poweroff(dev);
  1116. }
  1117. EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
  1118. /**
  1119. * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
  1120. * @dev: Device to handle.
  1121. *
  1122. * Carry out the generic late poweroff procedure for @dev and use ACPI to put
  1123. * it into a low-power state during system transition into a sleep state.
  1124. */
  1125. static int acpi_subsys_poweroff_late(struct device *dev)
  1126. {
  1127. int ret;
  1128. if (dev_pm_skip_suspend(dev))
  1129. return 0;
  1130. ret = pm_generic_poweroff_late(dev);
  1131. if (ret)
  1132. return ret;
  1133. return acpi_dev_suspend(dev, device_may_wakeup(dev));
  1134. }
  1135. /**
  1136. * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
  1137. * @dev: Device to suspend.
  1138. */
  1139. static int acpi_subsys_poweroff_noirq(struct device *dev)
  1140. {
  1141. if (dev_pm_skip_suspend(dev))
  1142. return 0;
  1143. return pm_generic_poweroff_noirq(dev);
  1144. }
  1145. #endif /* CONFIG_PM_SLEEP */
  1146. static struct dev_pm_domain acpi_general_pm_domain = {
  1147. .ops = {
  1148. .runtime_suspend = acpi_subsys_runtime_suspend,
  1149. .runtime_resume = acpi_subsys_runtime_resume,
  1150. #ifdef CONFIG_PM_SLEEP
  1151. .prepare = acpi_subsys_prepare,
  1152. .complete = acpi_subsys_complete,
  1153. .suspend = acpi_subsys_suspend,
  1154. .resume = acpi_subsys_resume,
  1155. .suspend_late = acpi_subsys_suspend_late,
  1156. .suspend_noirq = acpi_subsys_suspend_noirq,
  1157. .resume_noirq = acpi_subsys_resume_noirq,
  1158. .resume_early = acpi_subsys_resume_early,
  1159. .freeze = acpi_subsys_freeze,
  1160. .poweroff = acpi_subsys_poweroff,
  1161. .poweroff_late = acpi_subsys_poweroff_late,
  1162. .poweroff_noirq = acpi_subsys_poweroff_noirq,
  1163. .restore_early = acpi_subsys_restore_early,
  1164. #endif
  1165. },
  1166. };
  1167. /**
  1168. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  1169. * @dev: Device to take care of.
  1170. * @power_off: Whether or not to try to remove power from the device.
  1171. *
  1172. * Remove the device from the general ACPI PM domain and remove its wakeup
  1173. * notifier. If @power_off is set, additionally remove power from the device if
  1174. * possible.
  1175. *
  1176. * Callers must ensure proper synchronization of this function with power
  1177. * management callbacks.
  1178. */
  1179. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  1180. {
  1181. struct acpi_device *adev = ACPI_COMPANION(dev);
  1182. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  1183. dev_pm_domain_set(dev, NULL);
  1184. acpi_remove_pm_notifier(adev);
  1185. if (power_off) {
  1186. /*
  1187. * If the device's PM QoS resume latency limit or flags
  1188. * have been exposed to user space, they have to be
  1189. * hidden at this point, so that they don't affect the
  1190. * choice of the low-power state to put the device into.
  1191. */
  1192. dev_pm_qos_hide_latency_limit(dev);
  1193. dev_pm_qos_hide_flags(dev);
  1194. acpi_device_wakeup_disable(adev);
  1195. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  1196. }
  1197. }
  1198. }
  1199. /**
  1200. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  1201. * @dev: Device to prepare.
  1202. * @power_on: Whether or not to power on the device.
  1203. *
  1204. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  1205. * attached to it, install a wakeup notification handler for the device and
  1206. * add it to the general ACPI PM domain. If @power_on is set, the device will
  1207. * be put into the ACPI D0 state before the function returns.
  1208. *
  1209. * This assumes that the @dev's bus type uses generic power management callbacks
  1210. * (or doesn't use any power management callbacks at all).
  1211. *
  1212. * Callers must ensure proper synchronization of this function with power
  1213. * management callbacks.
  1214. */
  1215. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  1216. {
  1217. /*
  1218. * Skip devices whose ACPI companions match the device IDs below,
  1219. * because they require special power management handling incompatible
  1220. * with the generic ACPI PM domain.
  1221. */
  1222. static const struct acpi_device_id special_pm_ids[] = {
  1223. ACPI_FAN_DEVICE_IDS,
  1224. {}
  1225. };
  1226. struct acpi_device *adev = ACPI_COMPANION(dev);
  1227. if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
  1228. return 0;
  1229. /*
  1230. * Only attach the power domain to the first device if the
  1231. * companion is shared by multiple. This is to prevent doing power
  1232. * management twice.
  1233. */
  1234. if (!acpi_device_is_first_physical_node(adev, dev))
  1235. return 0;
  1236. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  1237. dev_pm_domain_set(dev, &acpi_general_pm_domain);
  1238. if (power_on) {
  1239. acpi_dev_pm_full_power(adev);
  1240. acpi_device_wakeup_disable(adev);
  1241. }
  1242. dev->pm_domain->detach = acpi_dev_pm_detach;
  1243. return 1;
  1244. }
  1245. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  1246. /**
  1247. * acpi_storage_d3 - Check if D3 should be used in the suspend path
  1248. * @dev: Device to check
  1249. *
  1250. * Return %true if the platform firmware wants @dev to be programmed
  1251. * into D3hot or D3cold (if supported) in the suspend path, or %false
  1252. * when there is no specific preference. On some platforms, if this
  1253. * hint is ignored, @dev may remain unresponsive after suspending the
  1254. * platform as a whole.
  1255. *
  1256. * Although the property has storage in the name it actually is
  1257. * applied to the PCIe slot and plugging in a non-storage device the
  1258. * same platform restrictions will likely apply.
  1259. */
  1260. bool acpi_storage_d3(struct device *dev)
  1261. {
  1262. struct acpi_device *adev = ACPI_COMPANION(dev);
  1263. u8 val;
  1264. if (force_storage_d3())
  1265. return true;
  1266. if (!adev)
  1267. return false;
  1268. if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable",
  1269. &val))
  1270. return false;
  1271. return val == 1;
  1272. }
  1273. EXPORT_SYMBOL_GPL(acpi_storage_d3);
  1274. /**
  1275. * acpi_dev_state_d0 - Tell if the device is in D0 power state
  1276. * @dev: Physical device the ACPI power state of which to check
  1277. *
  1278. * On a system without ACPI, return true. On a system with ACPI, return true if
  1279. * the current ACPI power state of the device is D0, or false otherwise.
  1280. *
  1281. * Note that the power state of a device is not well-defined after it has been
  1282. * passed to acpi_device_set_power() and before that function returns, so it is
  1283. * not valid to ask for the ACPI power state of the device in that time frame.
  1284. *
  1285. * This function is intended to be used in a driver's probe or remove
  1286. * function. See Documentation/firmware-guide/acpi/non-d0-probe.rst for
  1287. * more information.
  1288. */
  1289. bool acpi_dev_state_d0(struct device *dev)
  1290. {
  1291. struct acpi_device *adev = ACPI_COMPANION(dev);
  1292. if (!adev)
  1293. return true;
  1294. return adev->power.state == ACPI_STATE_D0;
  1295. }
  1296. EXPORT_SYMBOL_GPL(acpi_dev_state_d0);
  1297. #endif /* CONFIG_PM */