From 4b9ee772eaa82188b0eb8e05bdd1707c2a992004 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 18 Mar 2021 19:25:12 +0100 Subject: ACPI: scan: Turn off unused power resources during initialization It is reported that on certain platforms there are power resources that are not associated with any devices physically present in the platform. Those power resources are expected to be turned off by the OS in accordance with the ACPI specification (section 7.3 of ACPI 6.4) which currently is not done by Linux and that may lead to obscure issues. For instance, leaving those power resources in the "on" state may prevent the platform from reaching the lowest power state in suspend-to-idle which leads to excessive power draw. For this reason, turn all of the unused ACPI power resources off at the end of the initial namespace scan for devices in analogy with resume from suspend-to-RAM. Link: https://uefi.org/specs/ACPI/6.4/07_Power_and_Performance_Mgmt/device-power-management-objects.html Reported-by: David Box Tested-by: Wendy Wang Signed-off-by: Rafael J. Wysocki --- drivers/acpi/scan.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'drivers/acpi/scan.c') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index a184529d8fa4..1584c9e463bd 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -2360,6 +2360,8 @@ int __init acpi_scan_init(void) } } + acpi_turn_off_unused_power_resources(); + acpi_scan_initialized = true; out: -- cgit v1.2.3 From e6c1067dc1becf6c00408f59e0efa28b8d206371 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Wed, 7 Apr 2021 16:30:01 +0200 Subject: ACPI: scan: Fold acpi_bus_type_and_status() into its caller There is only one caller of acpi_bus_type_and_status() which is acpi_bus_check_add(), so fold the former into the latter and use the observation that the initial status of the device is ACPI_STA_DEFAULT in all cases except for ACPI_BUS_TYPE_PROCESSOR to simplify the code. No intentional functional impact. Signed-off-by: Rafael J. Wysocki Reviewed-by: Hans de Goede --- drivers/acpi/scan.c | 80 +++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 48 deletions(-) (limited to 'drivers/acpi/scan.c') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 6efe7edd7b1e..7f39bea6328a 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1763,50 +1763,6 @@ static bool acpi_device_should_be_hidden(acpi_handle handle) return true; } -static int acpi_bus_type_and_status(acpi_handle handle, int *type, - unsigned long long *sta) -{ - acpi_status status; - acpi_object_type acpi_type; - - status = acpi_get_type(handle, &acpi_type); - if (ACPI_FAILURE(status)) - return -ENODEV; - - switch (acpi_type) { - case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ - case ACPI_TYPE_DEVICE: - if (acpi_device_should_be_hidden(handle)) - return -ENODEV; - - *type = ACPI_BUS_TYPE_DEVICE; - /* - * acpi_add_single_object updates this once we've an acpi_device - * so that acpi_bus_get_status' quirk handling can be used. - */ - *sta = ACPI_STA_DEFAULT; - break; - case ACPI_TYPE_PROCESSOR: - *type = ACPI_BUS_TYPE_PROCESSOR; - status = acpi_bus_get_status_handle(handle, sta); - if (ACPI_FAILURE(status)) - return -ENODEV; - break; - case ACPI_TYPE_THERMAL: - *type = ACPI_BUS_TYPE_THERMAL; - *sta = ACPI_STA_DEFAULT; - break; - case ACPI_TYPE_POWER: - *type = ACPI_BUS_TYPE_POWER; - *sta = ACPI_STA_DEFAULT; - break; - default: - return -ENODEV; - } - - return 0; -} - bool acpi_device_is_present(const struct acpi_device *adev) { return adev->status.present || adev->status.functional; @@ -1953,18 +1909,46 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep, struct acpi_device **adev_p) { struct acpi_device *device = NULL; - unsigned long long sta; + unsigned long long sta = ACPI_STA_DEFAULT; + acpi_object_type acpi_type; int type; - int result; acpi_bus_get_device(handle, &device); if (device) goto out; - result = acpi_bus_type_and_status(handle, &type, &sta); - if (result) + if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type))) return AE_OK; + switch (acpi_type) { + case ACPI_TYPE_DEVICE: + if (acpi_device_should_be_hidden(handle)) + return AE_OK; + + fallthrough; + case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ + type = ACPI_BUS_TYPE_DEVICE; + break; + + case ACPI_TYPE_PROCESSOR: + if (ACPI_FAILURE(acpi_bus_get_status_handle(handle, &sta))) + return AE_OK; + + type = ACPI_BUS_TYPE_PROCESSOR; + break; + + case ACPI_TYPE_THERMAL: + type = ACPI_BUS_TYPE_THERMAL; + break; + + case ACPI_TYPE_POWER: + type = ACPI_BUS_TYPE_POWER; + break; + + default: + return AE_OK; + } + if (type == ACPI_BUS_TYPE_POWER) { acpi_add_power_resource(handle); return AE_OK; -- cgit v1.2.3 From 02056a4f9209b7e9f65fab193069cbff36ec92b5 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Wed, 7 Apr 2021 16:30:56 +0200 Subject: ACPI: scan: Rearrange checks in acpi_bus_check_add() Rearrange the checks in acpi_bus_check_add() to avoid checking the "type" twice and take "check_dep" into account only for ACPI_TYPE_DEVICE objects. No intentional functional impact. Signed-off-by: Rafael J. Wysocki Reviewed-by: Hans de Goede --- drivers/acpi/scan.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'drivers/acpi/scan.c') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 7f39bea6328a..5b5fa77cec0e 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1831,7 +1831,7 @@ static void acpi_scan_init_hotplug(struct acpi_device *adev) } } -static u32 acpi_scan_check_dep(acpi_handle handle) +static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep) { struct acpi_handle_list dep_devices; acpi_status status; @@ -1844,7 +1844,8 @@ static u32 acpi_scan_check_dep(acpi_handle handle) * 2. ACPI nodes describing USB ports. * Still, checking for _HID catches more then just these cases ... */ - if (!acpi_has_method(handle, "_DEP") || !acpi_has_method(handle, "_HID")) + if (!check_dep || !acpi_has_method(handle, "_DEP") || + !acpi_has_method(handle, "_HID")) return 0; status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices); @@ -1925,6 +1926,12 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep, if (acpi_device_should_be_hidden(handle)) return AE_OK; + /* Bail out if there are dependencies. */ + if (acpi_scan_check_dep(handle, check_dep) > 0) { + acpi_bus_scan_second_pass = true; + return AE_CTRL_DEPTH; + } + fallthrough; case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ type = ACPI_BUS_TYPE_DEVICE; @@ -1942,27 +1949,12 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep, break; case ACPI_TYPE_POWER: - type = ACPI_BUS_TYPE_POWER; - break; - - default: - return AE_OK; - } - - if (type == ACPI_BUS_TYPE_POWER) { acpi_add_power_resource(handle); + fallthrough; + default: return AE_OK; } - if (type == ACPI_BUS_TYPE_DEVICE && check_dep) { - u32 count = acpi_scan_check_dep(handle); - /* Bail out if the number of recorded dependencies is not 0. */ - if (count > 0) { - acpi_bus_scan_second_pass = true; - return AE_CTRL_DEPTH; - } - } - acpi_add_single_object(&device, handle, type, sta); if (!device) return AE_CTRL_DEPTH; -- cgit v1.2.3 From f926e94338158a5f84cbe4b0c7a9438c303762f8 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Wed, 7 Apr 2021 16:31:54 +0200 Subject: ACPI: scan: Drop sta argument from acpi_add_single_object() Move the initial status check for ACPI_BUS_TYPE_PROCESSOR objects into acpi_add_single_object() so it is not necessary to pass the "sta" argument to it, get rid of that argument from there and update the callers of that function accordingly. No intentional functional impact. Signed-off-by: Rafael J. Wysocki Reviewed-by: Hans de Goede --- drivers/acpi/scan.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'drivers/acpi/scan.c') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 5b5fa77cec0e..787ba8aac9b5 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1681,15 +1681,18 @@ void acpi_device_add_finalize(struct acpi_device *device) } static int acpi_add_single_object(struct acpi_device **child, - acpi_handle handle, int type, - unsigned long long sta) + acpi_handle handle, int type) { struct acpi_device_info *info = NULL; + unsigned long long sta = ACPI_STA_DEFAULT; struct acpi_device *device; int result; - if (handle != ACPI_ROOT_OBJECT && type == ACPI_BUS_TYPE_DEVICE) + if (type == ACPI_BUS_TYPE_DEVICE && handle != ACPI_ROOT_OBJECT) acpi_get_object_info(handle, &info); + else if (type == ACPI_BUS_TYPE_PROCESSOR && + ACPI_FAILURE(acpi_bus_get_status_handle(handle, &sta))) + return -ENODEV; device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); if (!device) { @@ -1910,7 +1913,6 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep, struct acpi_device **adev_p) { struct acpi_device *device = NULL; - unsigned long long sta = ACPI_STA_DEFAULT; acpi_object_type acpi_type; int type; @@ -1938,9 +1940,6 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep, break; case ACPI_TYPE_PROCESSOR: - if (ACPI_FAILURE(acpi_bus_get_status_handle(handle, &sta))) - return AE_OK; - type = ACPI_BUS_TYPE_PROCESSOR; break; @@ -1955,7 +1954,7 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep, return AE_OK; } - acpi_add_single_object(&device, handle, type, sta); + acpi_add_single_object(&device, handle, type); if (!device) return AE_CTRL_DEPTH; @@ -2229,8 +2228,7 @@ int acpi_bus_register_early_device(int type) struct acpi_device *device = NULL; int result; - result = acpi_add_single_object(&device, NULL, - type, ACPI_STA_DEFAULT); + result = acpi_add_single_object(&device, NULL, type); if (result) return result; @@ -2250,8 +2248,7 @@ static int acpi_bus_scan_fixed(void) struct acpi_device *device = NULL; result = acpi_add_single_object(&device, NULL, - ACPI_BUS_TYPE_POWER_BUTTON, - ACPI_STA_DEFAULT); + ACPI_BUS_TYPE_POWER_BUTTON); if (result) return result; @@ -2267,8 +2264,7 @@ static int acpi_bus_scan_fixed(void) struct acpi_device *device = NULL; result = acpi_add_single_object(&device, NULL, - ACPI_BUS_TYPE_SLEEP_BUTTON, - ACPI_STA_DEFAULT); + ACPI_BUS_TYPE_SLEEP_BUTTON); if (result) return result; -- cgit v1.2.3 From f5d9ab1d803456f5215f853e9286659933b59afe Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Wed, 7 Apr 2021 16:32:43 +0200 Subject: ACPI: scan: Drop sta argument from acpi_init_device_object() Use the observation that the initial status check for ACPI_BUS_TYPE_PROCESSOR objects can be carried out in the same way as for ACPI_BUS_TYPE_DEVICE objects and it is not necessary to fail acpi_add_single_object() if acpi_bus_get_status_handle() returns an error for a processor (its status can be set to 0 instead) to simplify acpi_add_single_object(). Accordingly, drop the "sta" argument from acpi_init_device_object() as it can always set the initial status to ACPI_STA_DEFAULT and let its caller correct that later on. Signed-off-by: Rafael J. Wysocki Reviewed-by: Hans de Goede --- drivers/acpi/scan.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'drivers/acpi/scan.c') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 787ba8aac9b5..902cecb218d3 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1649,15 +1649,14 @@ static bool acpi_device_enumeration_by_parent(struct acpi_device *device) } void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, - int type, unsigned long long sta, - struct acpi_device_info *info) + int type, struct acpi_device_info *info) { INIT_LIST_HEAD(&device->pnp.ids); device->device_type = type; device->handle = handle; device->parent = acpi_bus_get_parent(handle); fwnode_init(&device->fwnode, &acpi_device_fwnode_ops); - acpi_set_device_status(device, sta); + acpi_set_device_status(device, ACPI_STA_DEFAULT); acpi_device_get_busid(device); acpi_set_pnp_ids(handle, &device->pnp, type, info); acpi_init_properties(device); @@ -1680,19 +1679,21 @@ void acpi_device_add_finalize(struct acpi_device *device) kobject_uevent(&device->dev.kobj, KOBJ_ADD); } +static void acpi_scan_init_status(struct acpi_device *adev) +{ + if (acpi_bus_get_status(adev)) + acpi_set_device_status(adev, 0); +} + static int acpi_add_single_object(struct acpi_device **child, acpi_handle handle, int type) { struct acpi_device_info *info = NULL; - unsigned long long sta = ACPI_STA_DEFAULT; struct acpi_device *device; int result; if (type == ACPI_BUS_TYPE_DEVICE && handle != ACPI_ROOT_OBJECT) acpi_get_object_info(handle, &info); - else if (type == ACPI_BUS_TYPE_PROCESSOR && - ACPI_FAILURE(acpi_bus_get_status_handle(handle, &sta))) - return -ENODEV; device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); if (!device) { @@ -1700,16 +1701,15 @@ static int acpi_add_single_object(struct acpi_device **child, return -ENOMEM; } - acpi_init_device_object(device, handle, type, sta, info); + acpi_init_device_object(device, handle, type, info); kfree(info); /* - * For ACPI_BUS_TYPE_DEVICE getting the status is delayed till here so - * that we can call acpi_bus_get_status() and use its quirk handling. - * Note this must be done before the get power-/wakeup_dev-flags calls. + * Getting the status is delayed till here so that we can call + * acpi_bus_get_status() and use its quirk handling. Note that + * this must be done before the get power-/wakeup_dev-flags calls. */ - if (type == ACPI_BUS_TYPE_DEVICE) - if (acpi_bus_get_status(device) < 0) - acpi_set_device_status(device, 0); + if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) + acpi_scan_init_status(device); acpi_bus_get_power_flags(device); acpi_bus_get_wakeup_device_flags(device); -- cgit v1.2.3 From c830dbcfccbf70be94f15dbb4781be5ffb210d98 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Wed, 7 Apr 2021 16:33:38 +0200 Subject: ACPI: scan: Call acpi_get_object_info() from acpi_set_pnp_ids() Notice that it is not necessary to call acpi_get_object_info() from acpi_add_single_object() in order to pass the pointer returned by it to acpi_init_device_object() and from there to acpi_set_pnp_ids(). It is more straightforward to call acpi_get_object_info() from acpi_set_pnp_ids() and avoid unnecessary pointer passing, so change the code accordingly. No intentional functional impact. Signed-off-by: Rafael J. Wysocki Reviewed-by: Hans de Goede --- drivers/acpi/scan.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'drivers/acpi/scan.c') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 902cecb218d3..053777845475 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1307,8 +1307,9 @@ static bool acpi_object_is_system_bus(acpi_handle handle) } static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp, - int device_type, struct acpi_device_info *info) + int device_type) { + struct acpi_device_info *info = NULL; struct acpi_pnp_device_id_list *cid_list; int i; @@ -1319,6 +1320,7 @@ static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp, break; } + acpi_get_object_info(handle, &info); if (!info) { pr_err(PREFIX "%s: Error reading device info\n", __func__); @@ -1344,6 +1346,8 @@ static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp, if (info->valid & ACPI_VALID_CLS) acpi_add_id(pnp, info->class_code.string); + kfree(info); + /* * Some devices don't reliably have _HIDs & _CIDs, so add * synthetic HIDs to make sure drivers can find them. @@ -1649,7 +1653,7 @@ static bool acpi_device_enumeration_by_parent(struct acpi_device *device) } void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, - int type, struct acpi_device_info *info) + int type) { INIT_LIST_HEAD(&device->pnp.ids); device->device_type = type; @@ -1658,7 +1662,7 @@ void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, fwnode_init(&device->fwnode, &acpi_device_fwnode_ops); acpi_set_device_status(device, ACPI_STA_DEFAULT); acpi_device_get_busid(device); - acpi_set_pnp_ids(handle, &device->pnp, type, info); + acpi_set_pnp_ids(handle, &device->pnp, type); acpi_init_properties(device); acpi_bus_get_flags(device); device->flags.match_driver = false; @@ -1688,21 +1692,14 @@ static void acpi_scan_init_status(struct acpi_device *adev) static int acpi_add_single_object(struct acpi_device **child, acpi_handle handle, int type) { - struct acpi_device_info *info = NULL; struct acpi_device *device; int result; - if (type == ACPI_BUS_TYPE_DEVICE && handle != ACPI_ROOT_OBJECT) - acpi_get_object_info(handle, &info); - device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); - if (!device) { - kfree(info); + if (!device) return -ENOMEM; - } - acpi_init_device_object(device, handle, type, info); - kfree(info); + acpi_init_device_object(device, handle, type); /* * Getting the status is delayed till here so that we can call * acpi_bus_get_status() and use its quirk handling. Note that -- cgit v1.2.3 From 020505581119d191ee8da478783e2465d7f5fa8e Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Sat, 10 Apr 2021 17:02:53 +0300 Subject: ACPI: scan: Utilize match_string() API We have already an API to match a string in the array of strings. Utilize it instead of open coded analogues. Signed-off-by: Andy Shevchenko Signed-off-by: Rafael J. Wysocki --- drivers/acpi/scan.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'drivers/acpi/scan.c') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 053777845475..1dea11786ea9 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -757,27 +757,25 @@ static bool acpi_info_matches_ids(struct acpi_device_info *info, const char * const ids[]) { struct acpi_pnp_device_id_list *cid_list = NULL; - int i; + int i, index; if (!(info->valid & ACPI_VALID_HID)) return false; + index = match_string(ids, -1, info->hardware_id.string); + if (index >= 0) + return true; + if (info->valid & ACPI_VALID_CID) cid_list = &info->compatible_id_list; - for (i = 0; ids[i]; i++) { - int j; + if (!cid_list) + return false; - if (!strcmp(info->hardware_id.string, ids[i])) + for (i = 0; i < cid_list->count; i++) { + index = match_string(ids, -1, cid_list->ids[i].string); + if (index >= 0) return true; - - if (!cid_list) - continue; - - for (j = 0; j < cid_list->count; j++) { - if (!strcmp(cid_list->ids[j].string, ids[i])) - return true; - } } return false; -- cgit v1.2.3 From 4cbaba4e3e4a8a00ed90193ae519c52ba01ea756 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Tue, 13 Apr 2021 01:23:58 +0300 Subject: ACPI: bus: Introduce acpi_dev_get() and reuse it in ACPI code Introduce acpi_dev_get() to have a symmetrical API with acpi_dev_put() and reuse both in ACPI code in drivers/acpi/. While at it, use acpi_bus_put_acpi_device() in one place instead of the above. Signed-off-by: Andy Shevchenko Signed-off-by: Rafael J. Wysocki --- drivers/acpi/scan.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'drivers/acpi/scan.c') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 1dea11786ea9..5e0e8ec221c5 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -530,7 +530,7 @@ static void acpi_device_del_work_fn(struct work_struct *work_not_used) * used by the device. */ acpi_power_transition(adev, ACPI_STATE_D3_COLD); - put_device(&adev->dev); + acpi_dev_put(adev); } } @@ -604,8 +604,7 @@ EXPORT_SYMBOL(acpi_bus_get_device); static void get_acpi_device(void *dev) { - if (dev) - get_device(&((struct acpi_device *)dev)->dev); + acpi_dev_get(dev); } struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle) @@ -615,7 +614,7 @@ struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle) void acpi_bus_put_acpi_device(struct acpi_device *adev) { - put_device(&adev->dev); + acpi_dev_put(adev); } static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id) @@ -2355,7 +2354,7 @@ int __init acpi_scan_init(void) acpi_detach_data(acpi_root->handle, acpi_scan_drop_device); acpi_device_del(acpi_root); - put_device(&acpi_root->dev); + acpi_bus_put_acpi_device(acpi_root); goto out; } } -- cgit v1.2.3