s2idle.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Architecture-specific ACPI-based support for suspend-to-idle.
  4. *
  5. * Author: Rafael J. Wysocki <[email protected]>
  6. * Author: Srinivas Pandruvada <[email protected]>
  7. * Author: Shyam Sundar S K <[email protected]>
  8. *
  9. * On platforms supporting the Low Power S0 Idle interface there is an ACPI
  10. * device object with the PNP0D80 compatible device ID (System Power Management
  11. * Controller) and a specific _DSM method under it. That method, if present,
  12. * can be used to indicate to the platform that the OS is transitioning into a
  13. * low-power state in which certain types of activity are not desirable or that
  14. * it is leaving such a state, which allows the platform to adjust its operation
  15. * mode accordingly.
  16. */
  17. #include <linux/acpi.h>
  18. #include <linux/device.h>
  19. #include <linux/dmi.h>
  20. #include <linux/suspend.h>
  21. #include "../sleep.h"
  22. #ifdef CONFIG_SUSPEND
  23. static bool sleep_no_lps0 __read_mostly;
  24. module_param(sleep_no_lps0, bool, 0644);
  25. MODULE_PARM_DESC(sleep_no_lps0, "Do not use the special LPS0 device interface");
  26. static const struct acpi_device_id lps0_device_ids[] = {
  27. {"PNP0D80", },
  28. {"", },
  29. };
  30. /* Microsoft platform agnostic UUID */
  31. #define ACPI_LPS0_DSM_UUID_MICROSOFT "11e00d56-ce64-47ce-837b-1f898f9aa461"
  32. #define ACPI_LPS0_DSM_UUID "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
  33. #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS 1
  34. #define ACPI_LPS0_SCREEN_OFF 3
  35. #define ACPI_LPS0_SCREEN_ON 4
  36. #define ACPI_LPS0_ENTRY 5
  37. #define ACPI_LPS0_EXIT 6
  38. #define ACPI_LPS0_MS_ENTRY 7
  39. #define ACPI_LPS0_MS_EXIT 8
  40. /* AMD */
  41. #define ACPI_LPS0_DSM_UUID_AMD "e3f32452-febc-43ce-9039-932122d37721"
  42. #define ACPI_LPS0_ENTRY_AMD 2
  43. #define ACPI_LPS0_EXIT_AMD 3
  44. #define ACPI_LPS0_SCREEN_OFF_AMD 4
  45. #define ACPI_LPS0_SCREEN_ON_AMD 5
  46. static acpi_handle lps0_device_handle;
  47. static guid_t lps0_dsm_guid;
  48. static int lps0_dsm_func_mask;
  49. static guid_t lps0_dsm_guid_microsoft;
  50. static int lps0_dsm_func_mask_microsoft;
  51. /* Device constraint entry structure */
  52. struct lpi_device_info {
  53. char *name;
  54. int enabled;
  55. union acpi_object *package;
  56. };
  57. /* Constraint package structure */
  58. struct lpi_device_constraint {
  59. int uid;
  60. int min_dstate;
  61. int function_states;
  62. };
  63. struct lpi_constraints {
  64. acpi_handle handle;
  65. int min_dstate;
  66. };
  67. /* AMD Constraint package structure */
  68. struct lpi_device_constraint_amd {
  69. char *name;
  70. int enabled;
  71. int function_states;
  72. int min_dstate;
  73. };
  74. static LIST_HEAD(lps0_s2idle_devops_head);
  75. static struct lpi_constraints *lpi_constraints_table;
  76. static int lpi_constraints_table_size;
  77. static int rev_id;
  78. static void lpi_device_get_constraints_amd(void)
  79. {
  80. union acpi_object *out_obj;
  81. int i, j, k;
  82. out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
  83. rev_id, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
  84. NULL, ACPI_TYPE_PACKAGE);
  85. acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
  86. out_obj ? "successful" : "failed");
  87. if (!out_obj)
  88. return;
  89. for (i = 0; i < out_obj->package.count; i++) {
  90. union acpi_object *package = &out_obj->package.elements[i];
  91. if (package->type == ACPI_TYPE_PACKAGE) {
  92. if (lpi_constraints_table) {
  93. acpi_handle_err(lps0_device_handle,
  94. "Duplicate constraints list\n");
  95. goto free_acpi_buffer;
  96. }
  97. lpi_constraints_table = kcalloc(package->package.count,
  98. sizeof(*lpi_constraints_table),
  99. GFP_KERNEL);
  100. if (!lpi_constraints_table)
  101. goto free_acpi_buffer;
  102. acpi_handle_debug(lps0_device_handle,
  103. "LPI: constraints list begin:\n");
  104. for (j = 0; j < package->package.count; j++) {
  105. union acpi_object *info_obj = &package->package.elements[j];
  106. struct lpi_device_constraint_amd dev_info = {};
  107. struct lpi_constraints *list;
  108. acpi_status status;
  109. list = &lpi_constraints_table[lpi_constraints_table_size];
  110. for (k = 0; k < info_obj->package.count; k++) {
  111. union acpi_object *obj = &info_obj->package.elements[k];
  112. switch (k) {
  113. case 0:
  114. dev_info.enabled = obj->integer.value;
  115. break;
  116. case 1:
  117. dev_info.name = obj->string.pointer;
  118. break;
  119. case 2:
  120. dev_info.function_states = obj->integer.value;
  121. break;
  122. case 3:
  123. dev_info.min_dstate = obj->integer.value;
  124. break;
  125. }
  126. }
  127. if (!dev_info.enabled || !dev_info.name ||
  128. !dev_info.min_dstate)
  129. continue;
  130. status = acpi_get_handle(NULL, dev_info.name, &list->handle);
  131. if (ACPI_FAILURE(status))
  132. continue;
  133. acpi_handle_debug(lps0_device_handle,
  134. "Name:%s\n", dev_info.name);
  135. list->min_dstate = dev_info.min_dstate;
  136. lpi_constraints_table_size++;
  137. }
  138. }
  139. }
  140. acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
  141. free_acpi_buffer:
  142. ACPI_FREE(out_obj);
  143. }
  144. static void lpi_device_get_constraints(void)
  145. {
  146. union acpi_object *out_obj;
  147. int i;
  148. out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
  149. 1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
  150. NULL, ACPI_TYPE_PACKAGE);
  151. acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
  152. out_obj ? "successful" : "failed");
  153. if (!out_obj)
  154. return;
  155. lpi_constraints_table = kcalloc(out_obj->package.count,
  156. sizeof(*lpi_constraints_table),
  157. GFP_KERNEL);
  158. if (!lpi_constraints_table)
  159. goto free_acpi_buffer;
  160. acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
  161. for (i = 0; i < out_obj->package.count; i++) {
  162. struct lpi_constraints *constraint;
  163. acpi_status status;
  164. union acpi_object *package = &out_obj->package.elements[i];
  165. struct lpi_device_info info = { };
  166. int package_count = 0, j;
  167. if (!package)
  168. continue;
  169. for (j = 0; j < package->package.count; j++) {
  170. union acpi_object *element =
  171. &(package->package.elements[j]);
  172. switch (element->type) {
  173. case ACPI_TYPE_INTEGER:
  174. info.enabled = element->integer.value;
  175. break;
  176. case ACPI_TYPE_STRING:
  177. info.name = element->string.pointer;
  178. break;
  179. case ACPI_TYPE_PACKAGE:
  180. package_count = element->package.count;
  181. info.package = element->package.elements;
  182. break;
  183. }
  184. }
  185. if (!info.enabled || !info.package || !info.name)
  186. continue;
  187. constraint = &lpi_constraints_table[lpi_constraints_table_size];
  188. status = acpi_get_handle(NULL, info.name, &constraint->handle);
  189. if (ACPI_FAILURE(status))
  190. continue;
  191. acpi_handle_debug(lps0_device_handle,
  192. "index:%d Name:%s\n", i, info.name);
  193. constraint->min_dstate = -1;
  194. for (j = 0; j < package_count; j++) {
  195. union acpi_object *info_obj = &info.package[j];
  196. union acpi_object *cnstr_pkg;
  197. union acpi_object *obj;
  198. struct lpi_device_constraint dev_info;
  199. switch (info_obj->type) {
  200. case ACPI_TYPE_INTEGER:
  201. /* version */
  202. break;
  203. case ACPI_TYPE_PACKAGE:
  204. if (info_obj->package.count < 2)
  205. break;
  206. cnstr_pkg = info_obj->package.elements;
  207. obj = &cnstr_pkg[0];
  208. dev_info.uid = obj->integer.value;
  209. obj = &cnstr_pkg[1];
  210. dev_info.min_dstate = obj->integer.value;
  211. acpi_handle_debug(lps0_device_handle,
  212. "uid:%d min_dstate:%s\n",
  213. dev_info.uid,
  214. acpi_power_state_string(dev_info.min_dstate));
  215. constraint->min_dstate = dev_info.min_dstate;
  216. break;
  217. }
  218. }
  219. if (constraint->min_dstate < 0) {
  220. acpi_handle_debug(lps0_device_handle,
  221. "Incomplete constraint defined\n");
  222. continue;
  223. }
  224. lpi_constraints_table_size++;
  225. }
  226. acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
  227. free_acpi_buffer:
  228. ACPI_FREE(out_obj);
  229. }
  230. static void lpi_check_constraints(void)
  231. {
  232. int i;
  233. for (i = 0; i < lpi_constraints_table_size; ++i) {
  234. acpi_handle handle = lpi_constraints_table[i].handle;
  235. struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
  236. if (!adev)
  237. continue;
  238. acpi_handle_debug(handle,
  239. "LPI: required min power state:%s current power state:%s\n",
  240. acpi_power_state_string(lpi_constraints_table[i].min_dstate),
  241. acpi_power_state_string(adev->power.state));
  242. if (!adev->flags.power_manageable) {
  243. acpi_handle_info(handle, "LPI: Device not power manageable\n");
  244. lpi_constraints_table[i].handle = NULL;
  245. continue;
  246. }
  247. if (adev->power.state < lpi_constraints_table[i].min_dstate)
  248. acpi_handle_info(handle,
  249. "LPI: Constraint not met; min power state:%s current power state:%s\n",
  250. acpi_power_state_string(lpi_constraints_table[i].min_dstate),
  251. acpi_power_state_string(adev->power.state));
  252. }
  253. }
  254. static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid)
  255. {
  256. union acpi_object *out_obj;
  257. if (!(func_mask & (1 << func)))
  258. return;
  259. out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid,
  260. rev_id, func, NULL);
  261. ACPI_FREE(out_obj);
  262. acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
  263. func, out_obj ? "successful" : "failed");
  264. }
  265. static bool acpi_s2idle_vendor_amd(void)
  266. {
  267. return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
  268. }
  269. static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid)
  270. {
  271. union acpi_object *obj;
  272. int ret = -EINVAL;
  273. guid_parse(uuid, dsm_guid);
  274. obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL);
  275. /* Check if the _DSM is present and as expected. */
  276. if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length == 0 ||
  277. obj->buffer.length > sizeof(u32)) {
  278. acpi_handle_debug(handle,
  279. "_DSM UUID %s rev %d function 0 evaluation failed\n", uuid, rev);
  280. goto out;
  281. }
  282. ret = *(int *)obj->buffer.pointer;
  283. acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret);
  284. out:
  285. ACPI_FREE(obj);
  286. return ret;
  287. }
  288. struct amd_lps0_hid_device_data {
  289. const bool check_off_by_one;
  290. };
  291. static const struct amd_lps0_hid_device_data amd_picasso = {
  292. .check_off_by_one = true,
  293. };
  294. static const struct amd_lps0_hid_device_data amd_cezanne = {
  295. .check_off_by_one = false,
  296. };
  297. static const struct acpi_device_id amd_hid_ids[] = {
  298. {"AMD0004", (kernel_ulong_t)&amd_picasso, },
  299. {"AMD0005", (kernel_ulong_t)&amd_picasso, },
  300. {"AMDI0005", (kernel_ulong_t)&amd_picasso, },
  301. {"AMDI0006", (kernel_ulong_t)&amd_cezanne, },
  302. {}
  303. };
  304. static int lps0_device_attach(struct acpi_device *adev,
  305. const struct acpi_device_id *not_used)
  306. {
  307. if (lps0_device_handle)
  308. return 0;
  309. lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle,
  310. ACPI_LPS0_DSM_UUID_MICROSOFT, 0,
  311. &lps0_dsm_guid_microsoft);
  312. if (acpi_s2idle_vendor_amd()) {
  313. static const struct acpi_device_id *dev_id;
  314. const struct amd_lps0_hid_device_data *data;
  315. for (dev_id = &amd_hid_ids[0]; dev_id->id[0]; dev_id++)
  316. if (acpi_dev_hid_uid_match(adev, dev_id->id, NULL))
  317. break;
  318. if (dev_id->id[0])
  319. data = (const struct amd_lps0_hid_device_data *) dev_id->driver_data;
  320. else
  321. data = &amd_cezanne;
  322. lps0_dsm_func_mask = validate_dsm(adev->handle,
  323. ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid);
  324. if (lps0_dsm_func_mask > 0x3 && data->check_off_by_one) {
  325. lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1;
  326. acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n",
  327. ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask);
  328. } else if (lps0_dsm_func_mask_microsoft > 0 && rev_id) {
  329. lps0_dsm_func_mask_microsoft = -EINVAL;
  330. acpi_handle_debug(adev->handle, "_DSM Using AMD method\n");
  331. }
  332. } else {
  333. rev_id = 1;
  334. lps0_dsm_func_mask = validate_dsm(adev->handle,
  335. ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid);
  336. lps0_dsm_func_mask_microsoft = -EINVAL;
  337. }
  338. if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
  339. return 0; //function evaluation failed
  340. lps0_device_handle = adev->handle;
  341. if (acpi_s2idle_vendor_amd())
  342. lpi_device_get_constraints_amd();
  343. else
  344. lpi_device_get_constraints();
  345. /*
  346. * Use suspend-to-idle by default if ACPI_FADT_LOW_POWER_S0 is set in
  347. * the FADT and the default suspend mode was not set from the command
  348. * line.
  349. */
  350. if ((acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) &&
  351. mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3) {
  352. mem_sleep_current = PM_SUSPEND_TO_IDLE;
  353. pr_info("Low-power S0 idle used by default for system suspend\n");
  354. }
  355. /*
  356. * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
  357. * EC GPE to be enabled while suspended for certain wakeup devices to
  358. * work, so mark it as wakeup-capable.
  359. */
  360. acpi_ec_mark_gpe_for_wake();
  361. return 0;
  362. }
  363. static struct acpi_scan_handler lps0_handler = {
  364. .ids = lps0_device_ids,
  365. .attach = lps0_device_attach,
  366. };
  367. int acpi_s2idle_prepare_late(void)
  368. {
  369. struct acpi_s2idle_dev_ops *handler;
  370. if (!lps0_device_handle || sleep_no_lps0)
  371. return 0;
  372. if (pm_debug_messages_on)
  373. lpi_check_constraints();
  374. /* Screen off */
  375. if (lps0_dsm_func_mask > 0)
  376. acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
  377. ACPI_LPS0_SCREEN_OFF_AMD :
  378. ACPI_LPS0_SCREEN_OFF,
  379. lps0_dsm_func_mask, lps0_dsm_guid);
  380. if (lps0_dsm_func_mask_microsoft > 0)
  381. acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF,
  382. lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
  383. /* LPS0 entry */
  384. if (lps0_dsm_func_mask > 0)
  385. acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
  386. ACPI_LPS0_ENTRY_AMD :
  387. ACPI_LPS0_ENTRY,
  388. lps0_dsm_func_mask, lps0_dsm_guid);
  389. if (lps0_dsm_func_mask_microsoft > 0) {
  390. acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY,
  391. lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
  392. /* modern standby entry */
  393. acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY,
  394. lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
  395. }
  396. list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
  397. if (handler->prepare)
  398. handler->prepare();
  399. }
  400. return 0;
  401. }
  402. void acpi_s2idle_check(void)
  403. {
  404. struct acpi_s2idle_dev_ops *handler;
  405. if (!lps0_device_handle || sleep_no_lps0)
  406. return;
  407. list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
  408. if (handler->check)
  409. handler->check();
  410. }
  411. }
  412. void acpi_s2idle_restore_early(void)
  413. {
  414. struct acpi_s2idle_dev_ops *handler;
  415. if (!lps0_device_handle || sleep_no_lps0)
  416. return;
  417. list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
  418. if (handler->restore)
  419. handler->restore();
  420. /* Modern standby exit */
  421. if (lps0_dsm_func_mask_microsoft > 0)
  422. acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT,
  423. lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
  424. /* LPS0 exit */
  425. if (lps0_dsm_func_mask > 0)
  426. acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
  427. ACPI_LPS0_EXIT_AMD :
  428. ACPI_LPS0_EXIT,
  429. lps0_dsm_func_mask, lps0_dsm_guid);
  430. if (lps0_dsm_func_mask_microsoft > 0)
  431. acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT,
  432. lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
  433. /* Screen on */
  434. if (lps0_dsm_func_mask_microsoft > 0)
  435. acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON,
  436. lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
  437. if (lps0_dsm_func_mask > 0)
  438. acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
  439. ACPI_LPS0_SCREEN_ON_AMD :
  440. ACPI_LPS0_SCREEN_ON,
  441. lps0_dsm_func_mask, lps0_dsm_guid);
  442. }
  443. static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
  444. .begin = acpi_s2idle_begin,
  445. .prepare = acpi_s2idle_prepare,
  446. .prepare_late = acpi_s2idle_prepare_late,
  447. .check = acpi_s2idle_check,
  448. .wake = acpi_s2idle_wake,
  449. .restore_early = acpi_s2idle_restore_early,
  450. .restore = acpi_s2idle_restore,
  451. .end = acpi_s2idle_end,
  452. };
  453. void __init acpi_s2idle_setup(void)
  454. {
  455. acpi_scan_add_handler(&lps0_handler);
  456. s2idle_set_ops(&acpi_s2idle_ops_lps0);
  457. }
  458. int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
  459. {
  460. unsigned int sleep_flags;
  461. if (!lps0_device_handle || sleep_no_lps0)
  462. return -ENODEV;
  463. sleep_flags = lock_system_sleep();
  464. list_add(&arg->list_node, &lps0_s2idle_devops_head);
  465. unlock_system_sleep(sleep_flags);
  466. return 0;
  467. }
  468. EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
  469. void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
  470. {
  471. unsigned int sleep_flags;
  472. if (!lps0_device_handle || sleep_no_lps0)
  473. return;
  474. sleep_flags = lock_system_sleep();
  475. list_del(&arg->list_node);
  476. unlock_system_sleep(sleep_flags);
  477. }
  478. EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
  479. #endif /* CONFIG_SUSPEND */