power.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/acpi/power.c - ACPI Power Resources management.
  4. *
  5. * Copyright (C) 2001 - 2015 Intel Corp.
  6. * Author: Andy Grover <[email protected]>
  7. * Author: Paul Diefenbaugh <[email protected]>
  8. * Author: Rafael J. Wysocki <[email protected]>
  9. */
  10. /*
  11. * ACPI power-managed devices may be controlled in two ways:
  12. * 1. via "Device Specific (D-State) Control"
  13. * 2. via "Power Resource Control".
  14. * The code below deals with ACPI Power Resources control.
  15. *
  16. * An ACPI "power resource object" represents a software controllable power
  17. * plane, clock plane, or other resource depended on by a device.
  18. *
  19. * A device may rely on multiple power resources, and a power resource
  20. * may be shared by multiple devices.
  21. */
  22. #define pr_fmt(fmt) "ACPI: PM: " fmt
  23. #include <linux/dmi.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/acpi.h>
  32. #include "sleep.h"
  33. #include "internal.h"
  34. #define ACPI_POWER_CLASS "power_resource"
  35. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  36. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  37. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  38. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  39. struct acpi_power_dependent_device {
  40. struct device *dev;
  41. struct list_head node;
  42. };
  43. struct acpi_power_resource {
  44. struct acpi_device device;
  45. struct list_head list_node;
  46. u32 system_level;
  47. u32 order;
  48. unsigned int ref_count;
  49. u8 state;
  50. struct mutex resource_lock;
  51. struct list_head dependents;
  52. };
  53. struct acpi_power_resource_entry {
  54. struct list_head node;
  55. struct acpi_power_resource *resource;
  56. };
  57. static LIST_HEAD(acpi_power_resource_list);
  58. static DEFINE_MUTEX(power_resource_list_lock);
  59. /* --------------------------------------------------------------------------
  60. Power Resource Management
  61. -------------------------------------------------------------------------- */
  62. static inline const char *resource_dev_name(struct acpi_power_resource *pr)
  63. {
  64. return dev_name(&pr->device.dev);
  65. }
  66. static inline
  67. struct acpi_power_resource *to_power_resource(struct acpi_device *device)
  68. {
  69. return container_of(device, struct acpi_power_resource, device);
  70. }
  71. static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
  72. {
  73. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  74. if (!device)
  75. return NULL;
  76. return to_power_resource(device);
  77. }
  78. static int acpi_power_resources_list_add(acpi_handle handle,
  79. struct list_head *list)
  80. {
  81. struct acpi_power_resource *resource = acpi_power_get_context(handle);
  82. struct acpi_power_resource_entry *entry;
  83. if (!resource || !list)
  84. return -EINVAL;
  85. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  86. if (!entry)
  87. return -ENOMEM;
  88. entry->resource = resource;
  89. if (!list_empty(list)) {
  90. struct acpi_power_resource_entry *e;
  91. list_for_each_entry(e, list, node)
  92. if (e->resource->order > resource->order) {
  93. list_add_tail(&entry->node, &e->node);
  94. return 0;
  95. }
  96. }
  97. list_add_tail(&entry->node, list);
  98. return 0;
  99. }
  100. void acpi_power_resources_list_free(struct list_head *list)
  101. {
  102. struct acpi_power_resource_entry *entry, *e;
  103. list_for_each_entry_safe(entry, e, list, node) {
  104. list_del(&entry->node);
  105. kfree(entry);
  106. }
  107. }
  108. static bool acpi_power_resource_is_dup(union acpi_object *package,
  109. unsigned int start, unsigned int i)
  110. {
  111. acpi_handle rhandle, dup;
  112. unsigned int j;
  113. /* The caller is expected to check the package element types */
  114. rhandle = package->package.elements[i].reference.handle;
  115. for (j = start; j < i; j++) {
  116. dup = package->package.elements[j].reference.handle;
  117. if (dup == rhandle)
  118. return true;
  119. }
  120. return false;
  121. }
  122. int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
  123. struct list_head *list)
  124. {
  125. unsigned int i;
  126. int err = 0;
  127. for (i = start; i < package->package.count; i++) {
  128. union acpi_object *element = &package->package.elements[i];
  129. struct acpi_device *rdev;
  130. acpi_handle rhandle;
  131. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  132. err = -ENODATA;
  133. break;
  134. }
  135. rhandle = element->reference.handle;
  136. if (!rhandle) {
  137. err = -ENODEV;
  138. break;
  139. }
  140. /* Some ACPI tables contain duplicate power resource references */
  141. if (acpi_power_resource_is_dup(package, start, i))
  142. continue;
  143. rdev = acpi_add_power_resource(rhandle);
  144. if (!rdev) {
  145. err = -ENODEV;
  146. break;
  147. }
  148. err = acpi_power_resources_list_add(rhandle, list);
  149. if (err)
  150. break;
  151. }
  152. if (err)
  153. acpi_power_resources_list_free(list);
  154. return err;
  155. }
  156. static int __get_state(acpi_handle handle, u8 *state)
  157. {
  158. acpi_status status = AE_OK;
  159. unsigned long long sta = 0;
  160. u8 cur_state;
  161. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  162. if (ACPI_FAILURE(status))
  163. return -ENODEV;
  164. cur_state = sta & ACPI_POWER_RESOURCE_STATE_ON;
  165. acpi_handle_debug(handle, "Power resource is %s\n",
  166. cur_state ? "on" : "off");
  167. *state = cur_state;
  168. return 0;
  169. }
  170. static int acpi_power_get_state(struct acpi_power_resource *resource, u8 *state)
  171. {
  172. if (resource->state == ACPI_POWER_RESOURCE_STATE_UNKNOWN) {
  173. int ret;
  174. ret = __get_state(resource->device.handle, &resource->state);
  175. if (ret)
  176. return ret;
  177. }
  178. *state = resource->state;
  179. return 0;
  180. }
  181. static int acpi_power_get_list_state(struct list_head *list, u8 *state)
  182. {
  183. struct acpi_power_resource_entry *entry;
  184. u8 cur_state = ACPI_POWER_RESOURCE_STATE_OFF;
  185. if (!list || !state)
  186. return -EINVAL;
  187. /* The state of the list is 'on' IFF all resources are 'on'. */
  188. list_for_each_entry(entry, list, node) {
  189. struct acpi_power_resource *resource = entry->resource;
  190. int result;
  191. mutex_lock(&resource->resource_lock);
  192. result = acpi_power_get_state(resource, &cur_state);
  193. mutex_unlock(&resource->resource_lock);
  194. if (result)
  195. return result;
  196. if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
  197. break;
  198. }
  199. pr_debug("Power resource list is %s\n", cur_state ? "on" : "off");
  200. *state = cur_state;
  201. return 0;
  202. }
  203. static int
  204. acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
  205. struct device *dev)
  206. {
  207. struct acpi_power_dependent_device *dep;
  208. int ret = 0;
  209. mutex_lock(&resource->resource_lock);
  210. list_for_each_entry(dep, &resource->dependents, node) {
  211. /* Only add it once */
  212. if (dep->dev == dev)
  213. goto unlock;
  214. }
  215. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  216. if (!dep) {
  217. ret = -ENOMEM;
  218. goto unlock;
  219. }
  220. dep->dev = dev;
  221. list_add_tail(&dep->node, &resource->dependents);
  222. dev_dbg(dev, "added power dependency to [%s]\n",
  223. resource_dev_name(resource));
  224. unlock:
  225. mutex_unlock(&resource->resource_lock);
  226. return ret;
  227. }
  228. static void
  229. acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
  230. struct device *dev)
  231. {
  232. struct acpi_power_dependent_device *dep;
  233. mutex_lock(&resource->resource_lock);
  234. list_for_each_entry(dep, &resource->dependents, node) {
  235. if (dep->dev == dev) {
  236. list_del(&dep->node);
  237. kfree(dep);
  238. dev_dbg(dev, "removed power dependency to [%s]\n",
  239. resource_dev_name(resource));
  240. break;
  241. }
  242. }
  243. mutex_unlock(&resource->resource_lock);
  244. }
  245. /**
  246. * acpi_device_power_add_dependent - Add dependent device of this ACPI device
  247. * @adev: ACPI device pointer
  248. * @dev: Dependent device
  249. *
  250. * If @adev has non-empty _PR0 the @dev is added as dependent device to all
  251. * power resources returned by it. This means that whenever these power
  252. * resources are turned _ON the dependent devices get runtime resumed. This
  253. * is needed for devices such as PCI to allow its driver to re-initialize
  254. * it after it went to D0uninitialized.
  255. *
  256. * If @adev does not have _PR0 this does nothing.
  257. *
  258. * Returns %0 in case of success and negative errno otherwise.
  259. */
  260. int acpi_device_power_add_dependent(struct acpi_device *adev,
  261. struct device *dev)
  262. {
  263. struct acpi_power_resource_entry *entry;
  264. struct list_head *resources;
  265. int ret;
  266. if (!adev->flags.power_manageable)
  267. return 0;
  268. resources = &adev->power.states[ACPI_STATE_D0].resources;
  269. list_for_each_entry(entry, resources, node) {
  270. ret = acpi_power_resource_add_dependent(entry->resource, dev);
  271. if (ret)
  272. goto err;
  273. }
  274. return 0;
  275. err:
  276. list_for_each_entry(entry, resources, node)
  277. acpi_power_resource_remove_dependent(entry->resource, dev);
  278. return ret;
  279. }
  280. /**
  281. * acpi_device_power_remove_dependent - Remove dependent device
  282. * @adev: ACPI device pointer
  283. * @dev: Dependent device
  284. *
  285. * Does the opposite of acpi_device_power_add_dependent() and removes the
  286. * dependent device if it is found. Can be called to @adev that does not
  287. * have _PR0 as well.
  288. */
  289. void acpi_device_power_remove_dependent(struct acpi_device *adev,
  290. struct device *dev)
  291. {
  292. struct acpi_power_resource_entry *entry;
  293. struct list_head *resources;
  294. if (!adev->flags.power_manageable)
  295. return;
  296. resources = &adev->power.states[ACPI_STATE_D0].resources;
  297. list_for_each_entry_reverse(entry, resources, node)
  298. acpi_power_resource_remove_dependent(entry->resource, dev);
  299. }
  300. static int __acpi_power_on(struct acpi_power_resource *resource)
  301. {
  302. acpi_handle handle = resource->device.handle;
  303. struct acpi_power_dependent_device *dep;
  304. acpi_status status = AE_OK;
  305. status = acpi_evaluate_object(handle, "_ON", NULL, NULL);
  306. if (ACPI_FAILURE(status)) {
  307. resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
  308. return -ENODEV;
  309. }
  310. resource->state = ACPI_POWER_RESOURCE_STATE_ON;
  311. acpi_handle_debug(handle, "Power resource turned on\n");
  312. /*
  313. * If there are other dependents on this power resource we need to
  314. * resume them now so that their drivers can re-initialize the
  315. * hardware properly after it went back to D0.
  316. */
  317. if (list_empty(&resource->dependents) ||
  318. list_is_singular(&resource->dependents))
  319. return 0;
  320. list_for_each_entry(dep, &resource->dependents, node) {
  321. dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
  322. resource_dev_name(resource));
  323. pm_request_resume(dep->dev);
  324. }
  325. return 0;
  326. }
  327. static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
  328. {
  329. int result = 0;
  330. if (resource->ref_count++) {
  331. acpi_handle_debug(resource->device.handle,
  332. "Power resource already on\n");
  333. } else {
  334. result = __acpi_power_on(resource);
  335. if (result)
  336. resource->ref_count--;
  337. }
  338. return result;
  339. }
  340. static int acpi_power_on(struct acpi_power_resource *resource)
  341. {
  342. int result;
  343. mutex_lock(&resource->resource_lock);
  344. result = acpi_power_on_unlocked(resource);
  345. mutex_unlock(&resource->resource_lock);
  346. return result;
  347. }
  348. static int __acpi_power_off(struct acpi_power_resource *resource)
  349. {
  350. acpi_handle handle = resource->device.handle;
  351. acpi_status status;
  352. status = acpi_evaluate_object(handle, "_OFF", NULL, NULL);
  353. if (ACPI_FAILURE(status)) {
  354. resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
  355. return -ENODEV;
  356. }
  357. resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
  358. acpi_handle_debug(handle, "Power resource turned off\n");
  359. return 0;
  360. }
  361. static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
  362. {
  363. int result = 0;
  364. if (!resource->ref_count) {
  365. acpi_handle_debug(resource->device.handle,
  366. "Power resource already off\n");
  367. return 0;
  368. }
  369. if (--resource->ref_count) {
  370. acpi_handle_debug(resource->device.handle,
  371. "Power resource still in use\n");
  372. } else {
  373. result = __acpi_power_off(resource);
  374. if (result)
  375. resource->ref_count++;
  376. }
  377. return result;
  378. }
  379. static int acpi_power_off(struct acpi_power_resource *resource)
  380. {
  381. int result;
  382. mutex_lock(&resource->resource_lock);
  383. result = acpi_power_off_unlocked(resource);
  384. mutex_unlock(&resource->resource_lock);
  385. return result;
  386. }
  387. static int acpi_power_off_list(struct list_head *list)
  388. {
  389. struct acpi_power_resource_entry *entry;
  390. int result = 0;
  391. list_for_each_entry_reverse(entry, list, node) {
  392. result = acpi_power_off(entry->resource);
  393. if (result)
  394. goto err;
  395. }
  396. return 0;
  397. err:
  398. list_for_each_entry_continue(entry, list, node)
  399. acpi_power_on(entry->resource);
  400. return result;
  401. }
  402. static int acpi_power_on_list(struct list_head *list)
  403. {
  404. struct acpi_power_resource_entry *entry;
  405. int result = 0;
  406. list_for_each_entry(entry, list, node) {
  407. result = acpi_power_on(entry->resource);
  408. if (result)
  409. goto err;
  410. }
  411. return 0;
  412. err:
  413. list_for_each_entry_continue_reverse(entry, list, node)
  414. acpi_power_off(entry->resource);
  415. return result;
  416. }
  417. static struct attribute *attrs[] = {
  418. NULL,
  419. };
  420. static const struct attribute_group attr_groups[] = {
  421. [ACPI_STATE_D0] = {
  422. .name = "power_resources_D0",
  423. .attrs = attrs,
  424. },
  425. [ACPI_STATE_D1] = {
  426. .name = "power_resources_D1",
  427. .attrs = attrs,
  428. },
  429. [ACPI_STATE_D2] = {
  430. .name = "power_resources_D2",
  431. .attrs = attrs,
  432. },
  433. [ACPI_STATE_D3_HOT] = {
  434. .name = "power_resources_D3hot",
  435. .attrs = attrs,
  436. },
  437. };
  438. static const struct attribute_group wakeup_attr_group = {
  439. .name = "power_resources_wakeup",
  440. .attrs = attrs,
  441. };
  442. static void acpi_power_hide_list(struct acpi_device *adev,
  443. struct list_head *resources,
  444. const struct attribute_group *attr_group)
  445. {
  446. struct acpi_power_resource_entry *entry;
  447. if (list_empty(resources))
  448. return;
  449. list_for_each_entry_reverse(entry, resources, node) {
  450. struct acpi_device *res_dev = &entry->resource->device;
  451. sysfs_remove_link_from_group(&adev->dev.kobj,
  452. attr_group->name,
  453. dev_name(&res_dev->dev));
  454. }
  455. sysfs_remove_group(&adev->dev.kobj, attr_group);
  456. }
  457. static void acpi_power_expose_list(struct acpi_device *adev,
  458. struct list_head *resources,
  459. const struct attribute_group *attr_group)
  460. {
  461. struct acpi_power_resource_entry *entry;
  462. int ret;
  463. if (list_empty(resources))
  464. return;
  465. ret = sysfs_create_group(&adev->dev.kobj, attr_group);
  466. if (ret)
  467. return;
  468. list_for_each_entry(entry, resources, node) {
  469. struct acpi_device *res_dev = &entry->resource->device;
  470. ret = sysfs_add_link_to_group(&adev->dev.kobj,
  471. attr_group->name,
  472. &res_dev->dev.kobj,
  473. dev_name(&res_dev->dev));
  474. if (ret) {
  475. acpi_power_hide_list(adev, resources, attr_group);
  476. break;
  477. }
  478. }
  479. }
  480. static void acpi_power_expose_hide(struct acpi_device *adev,
  481. struct list_head *resources,
  482. const struct attribute_group *attr_group,
  483. bool expose)
  484. {
  485. if (expose)
  486. acpi_power_expose_list(adev, resources, attr_group);
  487. else
  488. acpi_power_hide_list(adev, resources, attr_group);
  489. }
  490. void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
  491. {
  492. int state;
  493. if (adev->wakeup.flags.valid)
  494. acpi_power_expose_hide(adev, &adev->wakeup.resources,
  495. &wakeup_attr_group, add);
  496. if (!adev->power.flags.power_resources)
  497. return;
  498. for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
  499. acpi_power_expose_hide(adev,
  500. &adev->power.states[state].resources,
  501. &attr_groups[state], add);
  502. }
  503. int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
  504. {
  505. struct acpi_power_resource_entry *entry;
  506. int system_level = 5;
  507. list_for_each_entry(entry, list, node) {
  508. struct acpi_power_resource *resource = entry->resource;
  509. u8 state;
  510. mutex_lock(&resource->resource_lock);
  511. /*
  512. * Make sure that the power resource state and its reference
  513. * counter value are consistent with each other.
  514. */
  515. if (!resource->ref_count &&
  516. !acpi_power_get_state(resource, &state) &&
  517. state == ACPI_POWER_RESOURCE_STATE_ON)
  518. __acpi_power_off(resource);
  519. if (system_level > resource->system_level)
  520. system_level = resource->system_level;
  521. mutex_unlock(&resource->resource_lock);
  522. }
  523. *system_level_p = system_level;
  524. return 0;
  525. }
  526. /* --------------------------------------------------------------------------
  527. Device Power Management
  528. -------------------------------------------------------------------------- */
  529. /**
  530. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  531. * ACPI 3.0) _PSW (Power State Wake)
  532. * @dev: Device to handle.
  533. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  534. * @sleep_state: Target sleep state of the system.
  535. * @dev_state: Target power state of the device.
  536. *
  537. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  538. * State Wake) for the device, if present. On failure reset the device's
  539. * wakeup.flags.valid flag.
  540. *
  541. * RETURN VALUE:
  542. * 0 if either _DSW or _PSW has been successfully executed
  543. * 0 if neither _DSW nor _PSW has been found
  544. * -ENODEV if the execution of either _DSW or _PSW has failed
  545. */
  546. int acpi_device_sleep_wake(struct acpi_device *dev,
  547. int enable, int sleep_state, int dev_state)
  548. {
  549. union acpi_object in_arg[3];
  550. struct acpi_object_list arg_list = { 3, in_arg };
  551. acpi_status status = AE_OK;
  552. /*
  553. * Try to execute _DSW first.
  554. *
  555. * Three arguments are needed for the _DSW object:
  556. * Argument 0: enable/disable the wake capabilities
  557. * Argument 1: target system state
  558. * Argument 2: target device state
  559. * When _DSW object is called to disable the wake capabilities, maybe
  560. * the first argument is filled. The values of the other two arguments
  561. * are meaningless.
  562. */
  563. in_arg[0].type = ACPI_TYPE_INTEGER;
  564. in_arg[0].integer.value = enable;
  565. in_arg[1].type = ACPI_TYPE_INTEGER;
  566. in_arg[1].integer.value = sleep_state;
  567. in_arg[2].type = ACPI_TYPE_INTEGER;
  568. in_arg[2].integer.value = dev_state;
  569. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  570. if (ACPI_SUCCESS(status)) {
  571. return 0;
  572. } else if (status != AE_NOT_FOUND) {
  573. acpi_handle_info(dev->handle, "_DSW execution failed\n");
  574. dev->wakeup.flags.valid = 0;
  575. return -ENODEV;
  576. }
  577. /* Execute _PSW */
  578. status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
  579. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  580. acpi_handle_info(dev->handle, "_PSW execution failed\n");
  581. dev->wakeup.flags.valid = 0;
  582. return -ENODEV;
  583. }
  584. return 0;
  585. }
  586. /*
  587. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  588. * 1. Power on the power resources required for the wakeup device
  589. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  590. * State Wake) for the device, if present
  591. */
  592. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  593. {
  594. int err = 0;
  595. if (!dev || !dev->wakeup.flags.valid)
  596. return -EINVAL;
  597. mutex_lock(&acpi_device_lock);
  598. dev_dbg(&dev->dev, "Enabling wakeup power (count %d)\n",
  599. dev->wakeup.prepare_count);
  600. if (dev->wakeup.prepare_count++)
  601. goto out;
  602. err = acpi_power_on_list(&dev->wakeup.resources);
  603. if (err) {
  604. dev_err(&dev->dev, "Cannot turn on wakeup power resources\n");
  605. dev->wakeup.flags.valid = 0;
  606. goto out;
  607. }
  608. /*
  609. * Passing 3 as the third argument below means the device may be
  610. * put into arbitrary power state afterward.
  611. */
  612. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  613. if (err) {
  614. acpi_power_off_list(&dev->wakeup.resources);
  615. dev->wakeup.prepare_count = 0;
  616. goto out;
  617. }
  618. dev_dbg(&dev->dev, "Wakeup power enabled\n");
  619. out:
  620. mutex_unlock(&acpi_device_lock);
  621. return err;
  622. }
  623. /*
  624. * Shutdown a wakeup device, counterpart of above method
  625. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  626. * State Wake) for the device, if present
  627. * 2. Shutdown down the power resources
  628. */
  629. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  630. {
  631. struct acpi_power_resource_entry *entry;
  632. int err = 0;
  633. if (!dev || !dev->wakeup.flags.valid)
  634. return -EINVAL;
  635. mutex_lock(&acpi_device_lock);
  636. dev_dbg(&dev->dev, "Disabling wakeup power (count %d)\n",
  637. dev->wakeup.prepare_count);
  638. /* Do nothing if wakeup power has not been enabled for this device. */
  639. if (dev->wakeup.prepare_count <= 0)
  640. goto out;
  641. if (--dev->wakeup.prepare_count > 0)
  642. goto out;
  643. err = acpi_device_sleep_wake(dev, 0, 0, 0);
  644. if (err)
  645. goto out;
  646. /*
  647. * All of the power resources in the list need to be turned off even if
  648. * there are errors.
  649. */
  650. list_for_each_entry(entry, &dev->wakeup.resources, node) {
  651. int ret;
  652. ret = acpi_power_off(entry->resource);
  653. if (ret && !err)
  654. err = ret;
  655. }
  656. if (err) {
  657. dev_err(&dev->dev, "Cannot turn off wakeup power resources\n");
  658. dev->wakeup.flags.valid = 0;
  659. goto out;
  660. }
  661. dev_dbg(&dev->dev, "Wakeup power disabled\n");
  662. out:
  663. mutex_unlock(&acpi_device_lock);
  664. return err;
  665. }
  666. int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
  667. {
  668. u8 list_state = ACPI_POWER_RESOURCE_STATE_OFF;
  669. int result = 0;
  670. int i = 0;
  671. if (!device || !state)
  672. return -EINVAL;
  673. /*
  674. * We know a device's inferred power state when all the resources
  675. * required for a given D-state are 'on'.
  676. */
  677. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  678. struct list_head *list = &device->power.states[i].resources;
  679. if (list_empty(list))
  680. continue;
  681. result = acpi_power_get_list_state(list, &list_state);
  682. if (result)
  683. return result;
  684. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  685. *state = i;
  686. return 0;
  687. }
  688. }
  689. *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
  690. ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
  691. return 0;
  692. }
  693. int acpi_power_on_resources(struct acpi_device *device, int state)
  694. {
  695. if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
  696. return -EINVAL;
  697. return acpi_power_on_list(&device->power.states[state].resources);
  698. }
  699. int acpi_power_transition(struct acpi_device *device, int state)
  700. {
  701. int result = 0;
  702. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  703. return -EINVAL;
  704. if (device->power.state == state || !device->flags.power_manageable)
  705. return 0;
  706. if ((device->power.state < ACPI_STATE_D0)
  707. || (device->power.state > ACPI_STATE_D3_COLD))
  708. return -ENODEV;
  709. /*
  710. * First we reference all power resources required in the target list
  711. * (e.g. so the device doesn't lose power while transitioning). Then,
  712. * we dereference all power resources used in the current list.
  713. */
  714. if (state < ACPI_STATE_D3_COLD)
  715. result = acpi_power_on_list(
  716. &device->power.states[state].resources);
  717. if (!result && device->power.state < ACPI_STATE_D3_COLD)
  718. acpi_power_off_list(
  719. &device->power.states[device->power.state].resources);
  720. /* We shouldn't change the state unless the above operations succeed. */
  721. device->power.state = result ? ACPI_STATE_UNKNOWN : state;
  722. return result;
  723. }
  724. static void acpi_release_power_resource(struct device *dev)
  725. {
  726. struct acpi_device *device = to_acpi_device(dev);
  727. struct acpi_power_resource *resource;
  728. resource = container_of(device, struct acpi_power_resource, device);
  729. mutex_lock(&power_resource_list_lock);
  730. list_del(&resource->list_node);
  731. mutex_unlock(&power_resource_list_lock);
  732. acpi_free_pnp_ids(&device->pnp);
  733. kfree(resource);
  734. }
  735. static ssize_t resource_in_use_show(struct device *dev,
  736. struct device_attribute *attr,
  737. char *buf)
  738. {
  739. struct acpi_power_resource *resource;
  740. resource = to_power_resource(to_acpi_device(dev));
  741. return sprintf(buf, "%u\n", !!resource->ref_count);
  742. }
  743. static DEVICE_ATTR_RO(resource_in_use);
  744. static void acpi_power_sysfs_remove(struct acpi_device *device)
  745. {
  746. device_remove_file(&device->dev, &dev_attr_resource_in_use);
  747. }
  748. static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
  749. {
  750. mutex_lock(&power_resource_list_lock);
  751. if (!list_empty(&acpi_power_resource_list)) {
  752. struct acpi_power_resource *r;
  753. list_for_each_entry(r, &acpi_power_resource_list, list_node)
  754. if (r->order > resource->order) {
  755. list_add_tail(&resource->list_node, &r->list_node);
  756. goto out;
  757. }
  758. }
  759. list_add_tail(&resource->list_node, &acpi_power_resource_list);
  760. out:
  761. mutex_unlock(&power_resource_list_lock);
  762. }
  763. struct acpi_device *acpi_add_power_resource(acpi_handle handle)
  764. {
  765. struct acpi_device *device = acpi_fetch_acpi_dev(handle);
  766. struct acpi_power_resource *resource;
  767. union acpi_object acpi_object;
  768. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  769. acpi_status status;
  770. u8 state_dummy;
  771. int result;
  772. if (device)
  773. return device;
  774. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  775. if (!resource)
  776. return NULL;
  777. device = &resource->device;
  778. acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
  779. acpi_release_power_resource);
  780. mutex_init(&resource->resource_lock);
  781. INIT_LIST_HEAD(&resource->list_node);
  782. INIT_LIST_HEAD(&resource->dependents);
  783. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  784. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  785. device->power.state = ACPI_STATE_UNKNOWN;
  786. device->flags.match_driver = true;
  787. /* Evaluate the object to get the system level and resource order. */
  788. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  789. if (ACPI_FAILURE(status))
  790. goto err;
  791. resource->system_level = acpi_object.power_resource.system_level;
  792. resource->order = acpi_object.power_resource.resource_order;
  793. resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
  794. /* Get the initial state or just flip it on if that fails. */
  795. if (acpi_power_get_state(resource, &state_dummy))
  796. __acpi_power_on(resource);
  797. pr_info("%s [%s]\n", acpi_device_name(device), acpi_device_bid(device));
  798. result = acpi_tie_acpi_dev(device);
  799. if (result)
  800. goto err;
  801. result = acpi_device_add(device);
  802. if (result)
  803. goto err;
  804. if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
  805. device->remove = acpi_power_sysfs_remove;
  806. acpi_power_add_resource_to_list(resource);
  807. acpi_device_add_finalize(device);
  808. return device;
  809. err:
  810. acpi_release_power_resource(&device->dev);
  811. return NULL;
  812. }
  813. #ifdef CONFIG_ACPI_SLEEP
  814. void acpi_resume_power_resources(void)
  815. {
  816. struct acpi_power_resource *resource;
  817. mutex_lock(&power_resource_list_lock);
  818. list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
  819. int result;
  820. u8 state;
  821. mutex_lock(&resource->resource_lock);
  822. resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
  823. result = acpi_power_get_state(resource, &state);
  824. if (result) {
  825. mutex_unlock(&resource->resource_lock);
  826. continue;
  827. }
  828. if (state == ACPI_POWER_RESOURCE_STATE_OFF
  829. && resource->ref_count) {
  830. acpi_handle_debug(resource->device.handle, "Turning ON\n");
  831. __acpi_power_on(resource);
  832. }
  833. mutex_unlock(&resource->resource_lock);
  834. }
  835. mutex_unlock(&power_resource_list_lock);
  836. }
  837. #endif
  838. static const struct dmi_system_id dmi_leave_unused_power_resources_on[] = {
  839. {
  840. /*
  841. * The Toshiba Click Mini has a CPR3 power-resource which must
  842. * be on for the touchscreen to work, but which is not in any
  843. * _PR? lists. The other 2 affected power-resources are no-ops.
  844. */
  845. .matches = {
  846. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  847. DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
  848. },
  849. },
  850. {}
  851. };
  852. /**
  853. * acpi_turn_off_unused_power_resources - Turn off power resources not in use.
  854. */
  855. void acpi_turn_off_unused_power_resources(void)
  856. {
  857. struct acpi_power_resource *resource;
  858. if (dmi_check_system(dmi_leave_unused_power_resources_on))
  859. return;
  860. mutex_lock(&power_resource_list_lock);
  861. list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
  862. mutex_lock(&resource->resource_lock);
  863. if (!resource->ref_count &&
  864. resource->state == ACPI_POWER_RESOURCE_STATE_ON) {
  865. acpi_handle_debug(resource->device.handle, "Turning OFF\n");
  866. __acpi_power_off(resource);
  867. }
  868. mutex_unlock(&resource->resource_lock);
  869. }
  870. mutex_unlock(&power_resource_list_lock);
  871. }