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