GNU Linux-libre 5.13.14-gnu1
[releases.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 #define ACPI_LPS0_DSM_UUID      "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
36
37 #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS        1
38 #define ACPI_LPS0_SCREEN_OFF    3
39 #define ACPI_LPS0_SCREEN_ON     4
40 #define ACPI_LPS0_ENTRY         5
41 #define ACPI_LPS0_EXIT          6
42
43 /* AMD */
44 #define ACPI_LPS0_DSM_UUID_AMD      "e3f32452-febc-43ce-9039-932122d37721"
45 #define ACPI_LPS0_ENTRY_AMD         2
46 #define ACPI_LPS0_EXIT_AMD          3
47 #define ACPI_LPS0_SCREEN_OFF_AMD    4
48 #define ACPI_LPS0_SCREEN_ON_AMD     5
49
50 static acpi_handle lps0_device_handle;
51 static guid_t lps0_dsm_guid;
52 static char lps0_dsm_func_mask;
53
54 /* Device constraint entry structure */
55 struct lpi_device_info {
56         char *name;
57         int enabled;
58         union acpi_object *package;
59 };
60
61 /* Constraint package structure */
62 struct lpi_device_constraint {
63         int uid;
64         int min_dstate;
65         int function_states;
66 };
67
68 struct lpi_constraints {
69         acpi_handle handle;
70         int min_dstate;
71 };
72
73 /* AMD */
74 /* Device constraint entry structure */
75 struct lpi_device_info_amd {
76         int revision;
77         int count;
78         union acpi_object *package;
79 };
80
81 /* 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 struct lpi_constraints *lpi_constraints_table;
90 static int lpi_constraints_table_size;
91 static int rev_id;
92
93 static void lpi_device_get_constraints_amd(void)
94 {
95         union acpi_object *out_obj;
96         int i, j, k;
97
98         out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
99                                           1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
100                                           NULL, ACPI_TYPE_PACKAGE);
101
102         if (!out_obj)
103                 return;
104
105         acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
106                           out_obj ? "successful" : "failed");
107
108         for (i = 0; i < out_obj->package.count; i++) {
109                 union acpi_object *package = &out_obj->package.elements[i];
110
111                 if (package->type == ACPI_TYPE_PACKAGE) {
112                         lpi_constraints_table = kcalloc(package->package.count,
113                                                         sizeof(*lpi_constraints_table),
114                                                         GFP_KERNEL);
115
116                         if (!lpi_constraints_table)
117                                 goto free_acpi_buffer;
118
119                         acpi_handle_debug(lps0_device_handle,
120                                           "LPI: constraints list begin:\n");
121
122                         for (j = 0; j < package->package.count; ++j) {
123                                 union acpi_object *info_obj = &package->package.elements[j];
124                                 struct lpi_device_constraint_amd dev_info = {};
125                                 struct lpi_constraints *list;
126                                 acpi_status status;
127
128                                 for (k = 0; k < info_obj->package.count; ++k) {
129                                         union acpi_object *obj = &info_obj->package.elements[k];
130
131                                         list = &lpi_constraints_table[lpi_constraints_table_size];
132                                         list->min_dstate = -1;
133
134                                         switch (k) {
135                                         case 0:
136                                                 dev_info.enabled = obj->integer.value;
137                                                 break;
138                                         case 1:
139                                                 dev_info.name = obj->string.pointer;
140                                                 break;
141                                         case 2:
142                                                 dev_info.function_states = obj->integer.value;
143                                                 break;
144                                         case 3:
145                                                 dev_info.min_dstate = obj->integer.value;
146                                                 break;
147                                         }
148
149                                         if (!dev_info.enabled || !dev_info.name ||
150                                             !dev_info.min_dstate)
151                                                 continue;
152
153                                         status = acpi_get_handle(NULL, dev_info.name,
154                                                                  &list->handle);
155                                         if (ACPI_FAILURE(status))
156                                                 continue;
157
158                                         acpi_handle_debug(lps0_device_handle,
159                                                           "Name:%s\n", dev_info.name);
160
161                                         list->min_dstate = dev_info.min_dstate;
162
163                                         if (list->min_dstate < 0) {
164                                                 acpi_handle_debug(lps0_device_handle,
165                                                                   "Incomplete constraint defined\n");
166                                                 continue;
167                                         }
168                                 }
169                                 lpi_constraints_table_size++;
170                         }
171                 }
172         }
173
174         acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
175
176 free_acpi_buffer:
177         ACPI_FREE(out_obj);
178 }
179
180 static void lpi_device_get_constraints(void)
181 {
182         union acpi_object *out_obj;
183         int i;
184
185         out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
186                                           1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
187                                           NULL, ACPI_TYPE_PACKAGE);
188
189         acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
190                           out_obj ? "successful" : "failed");
191
192         if (!out_obj)
193                 return;
194
195         lpi_constraints_table = kcalloc(out_obj->package.count,
196                                         sizeof(*lpi_constraints_table),
197                                         GFP_KERNEL);
198         if (!lpi_constraints_table)
199                 goto free_acpi_buffer;
200
201         acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
202
203         for (i = 0; i < out_obj->package.count; i++) {
204                 struct lpi_constraints *constraint;
205                 acpi_status status;
206                 union acpi_object *package = &out_obj->package.elements[i];
207                 struct lpi_device_info info = { };
208                 int package_count = 0, j;
209
210                 if (!package)
211                         continue;
212
213                 for (j = 0; j < package->package.count; ++j) {
214                         union acpi_object *element =
215                                         &(package->package.elements[j]);
216
217                         switch (element->type) {
218                         case ACPI_TYPE_INTEGER:
219                                 info.enabled = element->integer.value;
220                                 break;
221                         case ACPI_TYPE_STRING:
222                                 info.name = element->string.pointer;
223                                 break;
224                         case ACPI_TYPE_PACKAGE:
225                                 package_count = element->package.count;
226                                 info.package = element->package.elements;
227                                 break;
228                         }
229                 }
230
231                 if (!info.enabled || !info.package || !info.name)
232                         continue;
233
234                 constraint = &lpi_constraints_table[lpi_constraints_table_size];
235
236                 status = acpi_get_handle(NULL, info.name, &constraint->handle);
237                 if (ACPI_FAILURE(status))
238                         continue;
239
240                 acpi_handle_debug(lps0_device_handle,
241                                   "index:%d Name:%s\n", i, info.name);
242
243                 constraint->min_dstate = -1;
244
245                 for (j = 0; j < package_count; ++j) {
246                         union acpi_object *info_obj = &info.package[j];
247                         union acpi_object *cnstr_pkg;
248                         union acpi_object *obj;
249                         struct lpi_device_constraint dev_info;
250
251                         switch (info_obj->type) {
252                         case ACPI_TYPE_INTEGER:
253                                 /* version */
254                                 break;
255                         case ACPI_TYPE_PACKAGE:
256                                 if (info_obj->package.count < 2)
257                                         break;
258
259                                 cnstr_pkg = info_obj->package.elements;
260                                 obj = &cnstr_pkg[0];
261                                 dev_info.uid = obj->integer.value;
262                                 obj = &cnstr_pkg[1];
263                                 dev_info.min_dstate = obj->integer.value;
264
265                                 acpi_handle_debug(lps0_device_handle,
266                                         "uid:%d min_dstate:%s\n",
267                                         dev_info.uid,
268                                         acpi_power_state_string(dev_info.min_dstate));
269
270                                 constraint->min_dstate = dev_info.min_dstate;
271                                 break;
272                         }
273                 }
274
275                 if (constraint->min_dstate < 0) {
276                         acpi_handle_debug(lps0_device_handle,
277                                           "Incomplete constraint defined\n");
278                         continue;
279                 }
280
281                 lpi_constraints_table_size++;
282         }
283
284         acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
285
286 free_acpi_buffer:
287         ACPI_FREE(out_obj);
288 }
289
290 static void lpi_check_constraints(void)
291 {
292         int i;
293
294         for (i = 0; i < lpi_constraints_table_size; ++i) {
295                 acpi_handle handle = lpi_constraints_table[i].handle;
296                 struct acpi_device *adev;
297
298                 if (!handle || acpi_bus_get_device(handle, &adev))
299                         continue;
300
301                 acpi_handle_debug(handle,
302                         "LPI: required min power state:%s current power state:%s\n",
303                         acpi_power_state_string(lpi_constraints_table[i].min_dstate),
304                         acpi_power_state_string(adev->power.state));
305
306                 if (!adev->flags.power_manageable) {
307                         acpi_handle_info(handle, "LPI: Device not power manageable\n");
308                         lpi_constraints_table[i].handle = NULL;
309                         continue;
310                 }
311
312                 if (adev->power.state < lpi_constraints_table[i].min_dstate)
313                         acpi_handle_info(handle,
314                                 "LPI: Constraint not met; min power state:%s current power state:%s\n",
315                                 acpi_power_state_string(lpi_constraints_table[i].min_dstate),
316                                 acpi_power_state_string(adev->power.state));
317         }
318 }
319
320 static void acpi_sleep_run_lps0_dsm(unsigned int func)
321 {
322         union acpi_object *out_obj;
323
324         if (!(lps0_dsm_func_mask & (1 << func)))
325                 return;
326
327         out_obj = acpi_evaluate_dsm(lps0_device_handle, &lps0_dsm_guid, rev_id, func, NULL);
328         ACPI_FREE(out_obj);
329
330         acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
331                           func, out_obj ? "successful" : "failed");
332 }
333
334 static bool acpi_s2idle_vendor_amd(void)
335 {
336         return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
337 }
338
339 static int lps0_device_attach(struct acpi_device *adev,
340                               const struct acpi_device_id *not_used)
341 {
342         union acpi_object *out_obj;
343
344         if (lps0_device_handle)
345                 return 0;
346
347         if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
348                 return 0;
349
350         if (acpi_s2idle_vendor_amd()) {
351                 guid_parse(ACPI_LPS0_DSM_UUID_AMD, &lps0_dsm_guid);
352                 out_obj = acpi_evaluate_dsm(adev->handle, &lps0_dsm_guid, 0, 0, NULL);
353                 rev_id = 0;
354         } else {
355                 guid_parse(ACPI_LPS0_DSM_UUID, &lps0_dsm_guid);
356                 out_obj = acpi_evaluate_dsm(adev->handle, &lps0_dsm_guid, 1, 0, NULL);
357                 rev_id = 1;
358         }
359
360         /* Check if the _DSM is present and as expected. */
361         if (!out_obj || out_obj->type != ACPI_TYPE_BUFFER) {
362                 acpi_handle_debug(adev->handle,
363                                   "_DSM function 0 evaluation failed\n");
364                 return 0;
365         }
366
367         lps0_dsm_func_mask = *(char *)out_obj->buffer.pointer;
368
369         ACPI_FREE(out_obj);
370
371         acpi_handle_debug(adev->handle, "_DSM function mask: 0x%x\n",
372                           lps0_dsm_func_mask);
373
374         lps0_device_handle = adev->handle;
375
376         if (acpi_s2idle_vendor_amd())
377                 lpi_device_get_constraints_amd();
378         else
379                 lpi_device_get_constraints();
380
381         /*
382          * Use suspend-to-idle by default if the default suspend mode was not
383          * set from the command line.
384          */
385         if (mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3)
386                 mem_sleep_current = PM_SUSPEND_TO_IDLE;
387
388         /*
389          * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
390          * EC GPE to be enabled while suspended for certain wakeup devices to
391          * work, so mark it as wakeup-capable.
392          */
393         acpi_ec_mark_gpe_for_wake();
394
395         return 0;
396 }
397
398 static struct acpi_scan_handler lps0_handler = {
399         .ids = lps0_device_ids,
400         .attach = lps0_device_attach,
401 };
402
403 int acpi_s2idle_prepare_late(void)
404 {
405         if (!lps0_device_handle || sleep_no_lps0)
406                 return 0;
407
408         if (pm_debug_messages_on)
409                 lpi_check_constraints();
410
411         if (acpi_s2idle_vendor_amd()) {
412                 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF_AMD);
413                 acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY_AMD);
414         } else {
415                 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF);
416                 acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY);
417         }
418
419         return 0;
420 }
421
422 void acpi_s2idle_restore_early(void)
423 {
424         if (!lps0_device_handle || sleep_no_lps0)
425                 return;
426
427         if (acpi_s2idle_vendor_amd()) {
428                 acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT_AMD);
429                 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON_AMD);
430         } else {
431                 acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT);
432                 acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON);
433         }
434 }
435
436 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
437         .begin = acpi_s2idle_begin,
438         .prepare = acpi_s2idle_prepare,
439         .prepare_late = acpi_s2idle_prepare_late,
440         .wake = acpi_s2idle_wake,
441         .restore_early = acpi_s2idle_restore_early,
442         .restore = acpi_s2idle_restore,
443         .end = acpi_s2idle_end,
444 };
445
446 void acpi_s2idle_setup(void)
447 {
448         acpi_scan_add_handler(&lps0_handler);
449         s2idle_set_ops(&acpi_s2idle_ops_lps0);
450 }
451
452 #endif /* CONFIG_SUSPEND */