Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Architecture-specific ACPI-based support for suspend-to-idle.
0004  *
0005  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
0006  * Author: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
0007  * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
0008  *
0009  * On platforms supporting the Low Power S0 Idle interface there is an ACPI
0010  * device object with the PNP0D80 compatible device ID (System Power Management
0011  * Controller) and a specific _DSM method under it.  That method, if present,
0012  * can be used to indicate to the platform that the OS is transitioning into a
0013  * low-power state in which certain types of activity are not desirable or that
0014  * it is leaving such a state, which allows the platform to adjust its operation
0015  * mode accordingly.
0016  */
0017 
0018 #include <linux/acpi.h>
0019 #include <linux/device.h>
0020 #include <linux/suspend.h>
0021 
0022 #include "../sleep.h"
0023 
0024 #ifdef CONFIG_SUSPEND
0025 
0026 static bool sleep_no_lps0 __read_mostly;
0027 module_param(sleep_no_lps0, bool, 0644);
0028 MODULE_PARM_DESC(sleep_no_lps0, "Do not use the special LPS0 device interface");
0029 
0030 static const struct acpi_device_id lps0_device_ids[] = {
0031     {"PNP0D80", },
0032     {"", },
0033 };
0034 
0035 /* Microsoft platform agnostic UUID */
0036 #define ACPI_LPS0_DSM_UUID_MICROSOFT      "11e00d56-ce64-47ce-837b-1f898f9aa461"
0037 
0038 #define ACPI_LPS0_DSM_UUID  "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
0039 
0040 #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS    1
0041 #define ACPI_LPS0_SCREEN_OFF    3
0042 #define ACPI_LPS0_SCREEN_ON 4
0043 #define ACPI_LPS0_ENTRY     5
0044 #define ACPI_LPS0_EXIT      6
0045 #define ACPI_LPS0_MS_ENTRY      7
0046 #define ACPI_LPS0_MS_EXIT       8
0047 
0048 /* AMD */
0049 #define ACPI_LPS0_DSM_UUID_AMD      "e3f32452-febc-43ce-9039-932122d37721"
0050 #define ACPI_LPS0_ENTRY_AMD         2
0051 #define ACPI_LPS0_EXIT_AMD          3
0052 #define ACPI_LPS0_SCREEN_OFF_AMD    4
0053 #define ACPI_LPS0_SCREEN_ON_AMD     5
0054 
0055 static acpi_handle lps0_device_handle;
0056 static guid_t lps0_dsm_guid;
0057 static int lps0_dsm_func_mask;
0058 
0059 static guid_t lps0_dsm_guid_microsoft;
0060 static int lps0_dsm_func_mask_microsoft;
0061 
0062 /* Device constraint entry structure */
0063 struct lpi_device_info {
0064     char *name;
0065     int enabled;
0066     union acpi_object *package;
0067 };
0068 
0069 /* Constraint package structure */
0070 struct lpi_device_constraint {
0071     int uid;
0072     int min_dstate;
0073     int function_states;
0074 };
0075 
0076 struct lpi_constraints {
0077     acpi_handle handle;
0078     int min_dstate;
0079 };
0080 
0081 /* AMD Constraint package structure */
0082 struct lpi_device_constraint_amd {
0083     char *name;
0084     int enabled;
0085     int function_states;
0086     int min_dstate;
0087 };
0088 
0089 static LIST_HEAD(lps0_s2idle_devops_head);
0090 
0091 static struct lpi_constraints *lpi_constraints_table;
0092 static int lpi_constraints_table_size;
0093 static int rev_id;
0094 
0095 static void lpi_device_get_constraints_amd(void)
0096 {
0097     union acpi_object *out_obj;
0098     int i, j, k;
0099 
0100     out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
0101                       rev_id, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
0102                       NULL, ACPI_TYPE_PACKAGE);
0103 
0104     acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
0105               out_obj ? "successful" : "failed");
0106 
0107     if (!out_obj)
0108         return;
0109 
0110     for (i = 0; i < out_obj->package.count; i++) {
0111         union acpi_object *package = &out_obj->package.elements[i];
0112 
0113         if (package->type == ACPI_TYPE_PACKAGE) {
0114             lpi_constraints_table = kcalloc(package->package.count,
0115                             sizeof(*lpi_constraints_table),
0116                             GFP_KERNEL);
0117 
0118             if (!lpi_constraints_table)
0119                 goto free_acpi_buffer;
0120 
0121             acpi_handle_debug(lps0_device_handle,
0122                       "LPI: constraints list begin:\n");
0123 
0124             for (j = 0; j < package->package.count; ++j) {
0125                 union acpi_object *info_obj = &package->package.elements[j];
0126                 struct lpi_device_constraint_amd dev_info = {};
0127                 struct lpi_constraints *list;
0128                 acpi_status status;
0129 
0130                 for (k = 0; k < info_obj->package.count; ++k) {
0131                     union acpi_object *obj = &info_obj->package.elements[k];
0132 
0133                     list = &lpi_constraints_table[lpi_constraints_table_size];
0134                     list->min_dstate = -1;
0135 
0136                     switch (k) {
0137                     case 0:
0138                         dev_info.enabled = obj->integer.value;
0139                         break;
0140                     case 1:
0141                         dev_info.name = obj->string.pointer;
0142                         break;
0143                     case 2:
0144                         dev_info.function_states = obj->integer.value;
0145                         break;
0146                     case 3:
0147                         dev_info.min_dstate = obj->integer.value;
0148                         break;
0149                     }
0150 
0151                     if (!dev_info.enabled || !dev_info.name ||
0152                         !dev_info.min_dstate)
0153                         continue;
0154 
0155                     status = acpi_get_handle(NULL, dev_info.name,
0156                                  &list->handle);
0157                     if (ACPI_FAILURE(status))
0158                         continue;
0159 
0160                     acpi_handle_debug(lps0_device_handle,
0161                               "Name:%s\n", dev_info.name);
0162 
0163                     list->min_dstate = dev_info.min_dstate;
0164 
0165                     if (list->min_dstate < 0) {
0166                         acpi_handle_debug(lps0_device_handle,
0167                                   "Incomplete constraint defined\n");
0168                         continue;
0169                     }
0170                 }
0171                 lpi_constraints_table_size++;
0172             }
0173         }
0174     }
0175 
0176     acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
0177 
0178 free_acpi_buffer:
0179     ACPI_FREE(out_obj);
0180 }
0181 
0182 static void lpi_device_get_constraints(void)
0183 {
0184     union acpi_object *out_obj;
0185     int i;
0186 
0187     out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
0188                       1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
0189                       NULL, ACPI_TYPE_PACKAGE);
0190 
0191     acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
0192               out_obj ? "successful" : "failed");
0193 
0194     if (!out_obj)
0195         return;
0196 
0197     lpi_constraints_table = kcalloc(out_obj->package.count,
0198                     sizeof(*lpi_constraints_table),
0199                     GFP_KERNEL);
0200     if (!lpi_constraints_table)
0201         goto free_acpi_buffer;
0202 
0203     acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
0204 
0205     for (i = 0; i < out_obj->package.count; i++) {
0206         struct lpi_constraints *constraint;
0207         acpi_status status;
0208         union acpi_object *package = &out_obj->package.elements[i];
0209         struct lpi_device_info info = { };
0210         int package_count = 0, j;
0211 
0212         if (!package)
0213             continue;
0214 
0215         for (j = 0; j < package->package.count; ++j) {
0216             union acpi_object *element =
0217                     &(package->package.elements[j]);
0218 
0219             switch (element->type) {
0220             case ACPI_TYPE_INTEGER:
0221                 info.enabled = element->integer.value;
0222                 break;
0223             case ACPI_TYPE_STRING:
0224                 info.name = element->string.pointer;
0225                 break;
0226             case ACPI_TYPE_PACKAGE:
0227                 package_count = element->package.count;
0228                 info.package = element->package.elements;
0229                 break;
0230             }
0231         }
0232 
0233         if (!info.enabled || !info.package || !info.name)
0234             continue;
0235 
0236         constraint = &lpi_constraints_table[lpi_constraints_table_size];
0237 
0238         status = acpi_get_handle(NULL, info.name, &constraint->handle);
0239         if (ACPI_FAILURE(status))
0240             continue;
0241 
0242         acpi_handle_debug(lps0_device_handle,
0243                   "index:%d Name:%s\n", i, info.name);
0244 
0245         constraint->min_dstate = -1;
0246 
0247         for (j = 0; j < package_count; ++j) {
0248             union acpi_object *info_obj = &info.package[j];
0249             union acpi_object *cnstr_pkg;
0250             union acpi_object *obj;
0251             struct lpi_device_constraint dev_info;
0252 
0253             switch (info_obj->type) {
0254             case ACPI_TYPE_INTEGER:
0255                 /* version */
0256                 break;
0257             case ACPI_TYPE_PACKAGE:
0258                 if (info_obj->package.count < 2)
0259                     break;
0260 
0261                 cnstr_pkg = info_obj->package.elements;
0262                 obj = &cnstr_pkg[0];
0263                 dev_info.uid = obj->integer.value;
0264                 obj = &cnstr_pkg[1];
0265                 dev_info.min_dstate = obj->integer.value;
0266 
0267                 acpi_handle_debug(lps0_device_handle,
0268                     "uid:%d min_dstate:%s\n",
0269                     dev_info.uid,
0270                     acpi_power_state_string(dev_info.min_dstate));
0271 
0272                 constraint->min_dstate = dev_info.min_dstate;
0273                 break;
0274             }
0275         }
0276 
0277         if (constraint->min_dstate < 0) {
0278             acpi_handle_debug(lps0_device_handle,
0279                       "Incomplete constraint defined\n");
0280             continue;
0281         }
0282 
0283         lpi_constraints_table_size++;
0284     }
0285 
0286     acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
0287 
0288 free_acpi_buffer:
0289     ACPI_FREE(out_obj);
0290 }
0291 
0292 static void lpi_check_constraints(void)
0293 {
0294     int i;
0295 
0296     for (i = 0; i < lpi_constraints_table_size; ++i) {
0297         acpi_handle handle = lpi_constraints_table[i].handle;
0298         struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
0299 
0300         if (!adev)
0301             continue;
0302 
0303         acpi_handle_debug(handle,
0304             "LPI: required min power state:%s current power state:%s\n",
0305             acpi_power_state_string(lpi_constraints_table[i].min_dstate),
0306             acpi_power_state_string(adev->power.state));
0307 
0308         if (!adev->flags.power_manageable) {
0309             acpi_handle_info(handle, "LPI: Device not power manageable\n");
0310             lpi_constraints_table[i].handle = NULL;
0311             continue;
0312         }
0313 
0314         if (adev->power.state < lpi_constraints_table[i].min_dstate)
0315             acpi_handle_info(handle,
0316                 "LPI: Constraint not met; min power state:%s current power state:%s\n",
0317                 acpi_power_state_string(lpi_constraints_table[i].min_dstate),
0318                 acpi_power_state_string(adev->power.state));
0319     }
0320 }
0321 
0322 static void acpi_sleep_run_lps0_dsm(unsigned int func, unsigned int func_mask, guid_t dsm_guid)
0323 {
0324     union acpi_object *out_obj;
0325 
0326     if (!(func_mask & (1 << func)))
0327         return;
0328 
0329     out_obj = acpi_evaluate_dsm(lps0_device_handle, &dsm_guid,
0330                     rev_id, func, NULL);
0331     ACPI_FREE(out_obj);
0332 
0333     acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
0334               func, out_obj ? "successful" : "failed");
0335 }
0336 
0337 static bool acpi_s2idle_vendor_amd(void)
0338 {
0339     return boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
0340 }
0341 
0342 static int validate_dsm(acpi_handle handle, const char *uuid, int rev, guid_t *dsm_guid)
0343 {
0344     union acpi_object *obj;
0345     int ret = -EINVAL;
0346 
0347     guid_parse(uuid, dsm_guid);
0348     obj = acpi_evaluate_dsm(handle, dsm_guid, rev, 0, NULL);
0349 
0350     /* Check if the _DSM is present and as expected. */
0351     if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length == 0 ||
0352         obj->buffer.length > sizeof(u32)) {
0353         acpi_handle_debug(handle,
0354                 "_DSM UUID %s rev %d function 0 evaluation failed\n", uuid, rev);
0355         goto out;
0356     }
0357 
0358     ret = *(int *)obj->buffer.pointer;
0359     acpi_handle_debug(handle, "_DSM UUID %s rev %d function mask: 0x%x\n", uuid, rev, ret);
0360 
0361 out:
0362     ACPI_FREE(obj);
0363     return ret;
0364 }
0365 
0366 static int lps0_device_attach(struct acpi_device *adev,
0367                   const struct acpi_device_id *not_used)
0368 {
0369     if (lps0_device_handle)
0370         return 0;
0371 
0372     if (acpi_s2idle_vendor_amd()) {
0373         /* AMD0004, AMD0005, AMDI0005:
0374          * - Should use rev_id 0x0
0375          * - function mask > 0x3: Should use AMD method, but has off by one bug
0376          * - function mask = 0x3: Should use Microsoft method
0377          * AMDI0006:
0378          * - should use rev_id 0x0
0379          * - function mask = 0x3: Should use Microsoft method
0380          * AMDI0007:
0381          * - Should use rev_id 0x2
0382          * - Should only use AMD method
0383          */
0384         const char *hid = acpi_device_hid(adev);
0385         rev_id = strcmp(hid, "AMDI0007") ? 0 : 2;
0386         lps0_dsm_func_mask = validate_dsm(adev->handle,
0387                     ACPI_LPS0_DSM_UUID_AMD, rev_id, &lps0_dsm_guid);
0388         lps0_dsm_func_mask_microsoft = validate_dsm(adev->handle,
0389                     ACPI_LPS0_DSM_UUID_MICROSOFT, 0,
0390                     &lps0_dsm_guid_microsoft);
0391         if (lps0_dsm_func_mask > 0x3 && (!strcmp(hid, "AMD0004") ||
0392                          !strcmp(hid, "AMD0005") ||
0393                          !strcmp(hid, "AMDI0005"))) {
0394             lps0_dsm_func_mask = (lps0_dsm_func_mask << 1) | 0x1;
0395             acpi_handle_debug(adev->handle, "_DSM UUID %s: Adjusted function mask: 0x%x\n",
0396                       ACPI_LPS0_DSM_UUID_AMD, lps0_dsm_func_mask);
0397         } else if (lps0_dsm_func_mask_microsoft > 0 &&
0398                 (!strcmp(hid, "AMDI0007") ||
0399                  !strcmp(hid, "AMDI0008"))) {
0400             lps0_dsm_func_mask_microsoft = -EINVAL;
0401             acpi_handle_debug(adev->handle, "_DSM Using AMD method\n");
0402         }
0403     } else {
0404         rev_id = 1;
0405         lps0_dsm_func_mask = validate_dsm(adev->handle,
0406                     ACPI_LPS0_DSM_UUID, rev_id, &lps0_dsm_guid);
0407         lps0_dsm_func_mask_microsoft = -EINVAL;
0408     }
0409 
0410     if (lps0_dsm_func_mask < 0 && lps0_dsm_func_mask_microsoft < 0)
0411         return 0; //function evaluation failed
0412 
0413     lps0_device_handle = adev->handle;
0414 
0415     if (acpi_s2idle_vendor_amd())
0416         lpi_device_get_constraints_amd();
0417     else
0418         lpi_device_get_constraints();
0419 
0420     /*
0421      * Use suspend-to-idle by default if ACPI_FADT_LOW_POWER_S0 is set in
0422      * the FADT and the default suspend mode was not set from the command
0423      * line.
0424      */
0425     if ((acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) &&
0426         mem_sleep_default > PM_SUSPEND_MEM && !acpi_sleep_default_s3) {
0427         mem_sleep_current = PM_SUSPEND_TO_IDLE;
0428         pr_info("Low-power S0 idle used by default for system suspend\n");
0429     }
0430 
0431     /*
0432      * Some LPS0 systems, like ASUS Zenbook UX430UNR/i7-8550U, require the
0433      * EC GPE to be enabled while suspended for certain wakeup devices to
0434      * work, so mark it as wakeup-capable.
0435      */
0436     acpi_ec_mark_gpe_for_wake();
0437 
0438     return 0;
0439 }
0440 
0441 static struct acpi_scan_handler lps0_handler = {
0442     .ids = lps0_device_ids,
0443     .attach = lps0_device_attach,
0444 };
0445 
0446 int acpi_s2idle_prepare_late(void)
0447 {
0448     struct acpi_s2idle_dev_ops *handler;
0449 
0450     if (!lps0_device_handle || sleep_no_lps0)
0451         return 0;
0452 
0453     if (pm_debug_messages_on)
0454         lpi_check_constraints();
0455 
0456     /* Screen off */
0457     if (lps0_dsm_func_mask > 0)
0458         acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
0459                     ACPI_LPS0_SCREEN_OFF_AMD :
0460                     ACPI_LPS0_SCREEN_OFF,
0461                     lps0_dsm_func_mask, lps0_dsm_guid);
0462 
0463     if (lps0_dsm_func_mask_microsoft > 0)
0464         acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF,
0465                 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
0466 
0467     /* LPS0 entry */
0468     if (lps0_dsm_func_mask > 0)
0469         acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
0470                     ACPI_LPS0_ENTRY_AMD :
0471                     ACPI_LPS0_ENTRY,
0472                     lps0_dsm_func_mask, lps0_dsm_guid);
0473     if (lps0_dsm_func_mask_microsoft > 0) {
0474         acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY,
0475                 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
0476         /* modern standby entry */
0477         acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_ENTRY,
0478                 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
0479     }
0480 
0481     list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node) {
0482         if (handler->prepare)
0483             handler->prepare();
0484     }
0485 
0486     return 0;
0487 }
0488 
0489 void acpi_s2idle_restore_early(void)
0490 {
0491     struct acpi_s2idle_dev_ops *handler;
0492 
0493     if (!lps0_device_handle || sleep_no_lps0)
0494         return;
0495 
0496     list_for_each_entry(handler, &lps0_s2idle_devops_head, list_node)
0497         if (handler->restore)
0498             handler->restore();
0499 
0500     /* Modern standby exit */
0501     if (lps0_dsm_func_mask_microsoft > 0)
0502         acpi_sleep_run_lps0_dsm(ACPI_LPS0_MS_EXIT,
0503                 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
0504 
0505     /* LPS0 exit */
0506     if (lps0_dsm_func_mask > 0)
0507         acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
0508                     ACPI_LPS0_EXIT_AMD :
0509                     ACPI_LPS0_EXIT,
0510                     lps0_dsm_func_mask, lps0_dsm_guid);
0511     if (lps0_dsm_func_mask_microsoft > 0)
0512         acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT,
0513                 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
0514 
0515     /* Screen on */
0516     if (lps0_dsm_func_mask_microsoft > 0)
0517         acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON,
0518                 lps0_dsm_func_mask_microsoft, lps0_dsm_guid_microsoft);
0519     if (lps0_dsm_func_mask > 0)
0520         acpi_sleep_run_lps0_dsm(acpi_s2idle_vendor_amd() ?
0521                     ACPI_LPS0_SCREEN_ON_AMD :
0522                     ACPI_LPS0_SCREEN_ON,
0523                     lps0_dsm_func_mask, lps0_dsm_guid);
0524 }
0525 
0526 static const struct platform_s2idle_ops acpi_s2idle_ops_lps0 = {
0527     .begin = acpi_s2idle_begin,
0528     .prepare = acpi_s2idle_prepare,
0529     .prepare_late = acpi_s2idle_prepare_late,
0530     .wake = acpi_s2idle_wake,
0531     .restore_early = acpi_s2idle_restore_early,
0532     .restore = acpi_s2idle_restore,
0533     .end = acpi_s2idle_end,
0534 };
0535 
0536 void acpi_s2idle_setup(void)
0537 {
0538     acpi_scan_add_handler(&lps0_handler);
0539     s2idle_set_ops(&acpi_s2idle_ops_lps0);
0540 }
0541 
0542 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
0543 {
0544     if (!lps0_device_handle || sleep_no_lps0)
0545         return -ENODEV;
0546 
0547     lock_system_sleep();
0548     list_add(&arg->list_node, &lps0_s2idle_devops_head);
0549     unlock_system_sleep();
0550 
0551     return 0;
0552 }
0553 EXPORT_SYMBOL_GPL(acpi_register_lps0_dev);
0554 
0555 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
0556 {
0557     if (!lps0_device_handle || sleep_no_lps0)
0558         return;
0559 
0560     lock_system_sleep();
0561     list_del(&arg->list_node);
0562     unlock_system_sleep();
0563 }
0564 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
0565 
0566 #endif /* CONFIG_SUSPEND */