1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * scan.c - support for transforming the ACPI namespace into individual objects 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 68acf4108SHanjun Guo #define pr_fmt(fmt) "ACPI: " fmt 78acf4108SHanjun Guo 81da177e4SLinus Torvalds #include <linux/module.h> 91da177e4SLinus Torvalds #include <linux/init.h> 105a0e3ad6STejun Heo #include <linux/slab.h> 119b6d97b6SRandy Dunlap #include <linux/kernel.h> 121da177e4SLinus Torvalds #include <linux/acpi.h> 13643b8e4dSLorenzo Pieralisi #include <linux/acpi_iort.h> 143cf48554SJean-Philippe Brucker #include <linux/acpi_viot.h> 1511a8c5e3SJean-Philippe Brucker #include <linux/iommu.h> 1674523c90SAlok N Kataria #include <linux/signal.h> 1774523c90SAlok N Kataria #include <linux/kthread.h> 18222e82acSDarrick J. Wong #include <linux/dmi.h> 190a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h> 20ca9ef3abSLukas Wunner #include <linux/platform_data/x86/apple.h> 21ca5999fdSMike Rapoport #include <linux/pgtable.h> 22882c982dSHeikki Krogerus #include <linux/crc32.h> 23d783156eSRafael J. Wysocki 24e60cc7a6SBjorn Helgaas #include "internal.h" 25e60cc7a6SBjorn Helgaas 261da177e4SLinus Torvalds extern struct acpi_device *acpi_root; 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #define ACPI_BUS_CLASS "system_bus" 2929b71a1cSThomas Renninger #define ACPI_BUS_HID "LNXSYBUS" 301da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME "System Bus" 311da177e4SLinus Torvalds 32859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent) 33859ac9a4SBjorn Helgaas 34d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page) 35d783156eSRafael J. Wysocki 36620e112cSThomas Renninger static const char *dummy_hid = "device"; 372b2ae7c7SThomas Renninger 3840e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list); 3940e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock); 402d12b6b3SLukas Wunner LIST_HEAD(acpi_bus_id_list); 41c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock); 42ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list); 439090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock); 441da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list); 45e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock); 461da177e4SLinus Torvalds 47a4e081b0SShannon Zhao /* 48a4e081b0SShannon Zhao * The UART device described by the SPCR table is the only object which needs 49a4e081b0SShannon Zhao * special-casing. Everything else is covered by ACPI namespace paths in STAO 50a4e081b0SShannon Zhao * table. 51a4e081b0SShannon Zhao */ 52a4e081b0SShannon Zhao static u64 spcr_uart_addr; 53a4e081b0SShannon Zhao 543757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void) 553757b948SRafael J. Wysocki { 563757b948SRafael J. Wysocki mutex_lock(&acpi_scan_lock); 573757b948SRafael J. Wysocki } 583757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire); 593757b948SRafael J. Wysocki 603757b948SRafael J. Wysocki void acpi_scan_lock_release(void) 613757b948SRafael J. Wysocki { 623757b948SRafael J. Wysocki mutex_unlock(&acpi_scan_lock); 633757b948SRafael J. Wysocki } 643757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release); 653757b948SRafael J. Wysocki 66e525506fSRafael J. Wysocki void acpi_lock_hp_context(void) 67e525506fSRafael J. Wysocki { 68e525506fSRafael J. Wysocki mutex_lock(&acpi_hp_context_lock); 69e525506fSRafael J. Wysocki } 70e525506fSRafael J. Wysocki 71e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void) 72e525506fSRafael J. Wysocki { 73e525506fSRafael J. Wysocki mutex_unlock(&acpi_hp_context_lock); 74e525506fSRafael J. Wysocki } 75e525506fSRafael J. Wysocki 765d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev, 775d513205SRafael J. Wysocki struct acpi_hotplug_context *hp, 785d513205SRafael J. Wysocki int (*notify)(struct acpi_device *, u32), 795d513205SRafael J. Wysocki void (*uevent)(struct acpi_device *, u32)) 805d513205SRafael J. Wysocki { 815d513205SRafael J. Wysocki acpi_lock_hp_context(); 82ba574dc8SRafael J. Wysocki hp->notify = notify; 83ba574dc8SRafael J. Wysocki hp->uevent = uevent; 84ba574dc8SRafael J. Wysocki acpi_set_hp_context(adev, hp); 855d513205SRafael J. Wysocki acpi_unlock_hp_context(); 865d513205SRafael J. Wysocki } 875d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context); 885d513205SRafael J. Wysocki 89ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler) 90ca589f94SRafael J. Wysocki { 91d34afa9dSRafael J. Wysocki if (!handler) 92ca589f94SRafael J. Wysocki return -EINVAL; 93ca589f94SRafael J. Wysocki 94ca589f94SRafael J. Wysocki list_add_tail(&handler->list_node, &acpi_scan_handlers_list); 95ca589f94SRafael J. Wysocki return 0; 96ca589f94SRafael J. Wysocki } 97ca589f94SRafael J. Wysocki 983f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler, 993f8055c3SRafael J. Wysocki const char *hotplug_profile_name) 1003f8055c3SRafael J. Wysocki { 1013f8055c3SRafael J. Wysocki int error; 1023f8055c3SRafael J. Wysocki 1033f8055c3SRafael J. Wysocki error = acpi_scan_add_handler(handler); 1043f8055c3SRafael J. Wysocki if (error) 1053f8055c3SRafael J. Wysocki return error; 1063f8055c3SRafael J. Wysocki 1073f8055c3SRafael J. Wysocki acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name); 1083f8055c3SRafael J. Wysocki return 0; 1093f8055c3SRafael J. Wysocki } 1103f8055c3SRafael J. Wysocki 111caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent) 112d22ddcbcSRafael J. Wysocki { 113d22ddcbcSRafael J. Wysocki struct acpi_device_physical_node *pn; 114d22ddcbcSRafael J. Wysocki bool offline = true; 11527664c58SLee, Chun-Yi char *envp[] = { "EVENT=offline", NULL }; 116d22ddcbcSRafael J. Wysocki 1174c533c80SRafael J. Wysocki /* 1184c533c80SRafael J. Wysocki * acpi_container_offline() calls this for all of the container's 1194c533c80SRafael J. Wysocki * children under the container's physical_node_lock lock. 1204c533c80SRafael J. Wysocki */ 1214c533c80SRafael J. Wysocki mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING); 122d22ddcbcSRafael J. Wysocki 123d22ddcbcSRafael J. Wysocki list_for_each_entry(pn, &adev->physical_node_list, node) 124d22ddcbcSRafael J. Wysocki if (device_supports_offline(pn->dev) && !pn->dev->offline) { 125caa73ea1SRafael J. Wysocki if (uevent) 12627664c58SLee, Chun-Yi kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp); 127caa73ea1SRafael J. Wysocki 128d22ddcbcSRafael J. Wysocki offline = false; 129d22ddcbcSRafael J. Wysocki break; 130d22ddcbcSRafael J. Wysocki } 131d22ddcbcSRafael J. Wysocki 132d22ddcbcSRafael J. Wysocki mutex_unlock(&adev->physical_node_lock); 133d22ddcbcSRafael J. Wysocki return offline; 134d22ddcbcSRafael J. Wysocki } 135d22ddcbcSRafael J. Wysocki 1367f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data, 1377f28ddecSRafael J. Wysocki void **ret_p) 138683058e3SRafael J. Wysocki { 139e3c963c4SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 140683058e3SRafael J. Wysocki struct acpi_device_physical_node *pn; 141303bfdb1SRafael J. Wysocki bool second_pass = (bool)data; 142683058e3SRafael J. Wysocki acpi_status status = AE_OK; 143683058e3SRafael J. Wysocki 144e3c963c4SRafael J. Wysocki if (!device) 145683058e3SRafael J. Wysocki return AE_OK; 146683058e3SRafael J. Wysocki 1477f28ddecSRafael J. Wysocki if (device->handler && !device->handler->hotplug.enabled) { 1487f28ddecSRafael J. Wysocki *ret_p = &device->dev; 1497f28ddecSRafael J. Wysocki return AE_SUPPORT; 1507f28ddecSRafael J. Wysocki } 1517f28ddecSRafael J. Wysocki 152683058e3SRafael J. Wysocki mutex_lock(&device->physical_node_lock); 153683058e3SRafael J. Wysocki 154683058e3SRafael J. Wysocki list_for_each_entry(pn, &device->physical_node_list, node) { 155683058e3SRafael J. Wysocki int ret; 156683058e3SRafael J. Wysocki 157303bfdb1SRafael J. Wysocki if (second_pass) { 158303bfdb1SRafael J. Wysocki /* Skip devices offlined by the first pass. */ 159303bfdb1SRafael J. Wysocki if (pn->put_online) 160303bfdb1SRafael J. Wysocki continue; 161303bfdb1SRafael J. Wysocki } else { 162303bfdb1SRafael J. Wysocki pn->put_online = false; 163303bfdb1SRafael J. Wysocki } 164683058e3SRafael J. Wysocki ret = device_offline(pn->dev); 165303bfdb1SRafael J. Wysocki if (ret >= 0) { 166303bfdb1SRafael J. Wysocki pn->put_online = !ret; 167303bfdb1SRafael J. Wysocki } else { 168303bfdb1SRafael J. Wysocki *ret_p = pn->dev; 169303bfdb1SRafael J. Wysocki if (second_pass) { 170683058e3SRafael J. Wysocki status = AE_ERROR; 171683058e3SRafael J. Wysocki break; 172683058e3SRafael J. Wysocki } 173303bfdb1SRafael J. Wysocki } 174683058e3SRafael J. Wysocki } 175683058e3SRafael J. Wysocki 176683058e3SRafael J. Wysocki mutex_unlock(&device->physical_node_lock); 177683058e3SRafael J. Wysocki 178683058e3SRafael J. Wysocki return status; 179683058e3SRafael J. Wysocki } 180683058e3SRafael J. Wysocki 1817f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data, 1827f28ddecSRafael J. Wysocki void **ret_p) 183683058e3SRafael J. Wysocki { 184e3c963c4SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 185683058e3SRafael J. Wysocki struct acpi_device_physical_node *pn; 186683058e3SRafael J. Wysocki 187e3c963c4SRafael J. Wysocki if (!device) 188683058e3SRafael J. Wysocki return AE_OK; 189683058e3SRafael J. Wysocki 190683058e3SRafael J. Wysocki mutex_lock(&device->physical_node_lock); 191683058e3SRafael J. Wysocki 192683058e3SRafael J. Wysocki list_for_each_entry(pn, &device->physical_node_list, node) 193683058e3SRafael J. Wysocki if (pn->put_online) { 194683058e3SRafael J. Wysocki device_online(pn->dev); 195683058e3SRafael J. Wysocki pn->put_online = false; 196683058e3SRafael J. Wysocki } 197683058e3SRafael J. Wysocki 198683058e3SRafael J. Wysocki mutex_unlock(&device->physical_node_lock); 199683058e3SRafael J. Wysocki 200683058e3SRafael J. Wysocki return AE_OK; 201683058e3SRafael J. Wysocki } 202683058e3SRafael J. Wysocki 203d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device) 2041da177e4SLinus Torvalds { 2055993c467SYinghai Lu acpi_handle handle = device->handle; 206d22ddcbcSRafael J. Wysocki struct device *errdev = NULL; 207a33ec399SRafael J. Wysocki acpi_status status; 208f058cdf4SRafael J. Wysocki 209303bfdb1SRafael J. Wysocki /* 210303bfdb1SRafael J. Wysocki * Carry out two passes here and ignore errors in the first pass, 211303bfdb1SRafael J. Wysocki * because if the devices in question are memory blocks and 212303bfdb1SRafael J. Wysocki * CONFIG_MEMCG is set, one of the blocks may hold data structures 213303bfdb1SRafael J. Wysocki * that the other blocks depend on, but it is not known in advance which 214303bfdb1SRafael J. Wysocki * block holds them. 215303bfdb1SRafael J. Wysocki * 216303bfdb1SRafael J. Wysocki * If the first pass is successful, the second one isn't needed, though. 217303bfdb1SRafael J. Wysocki */ 2187f28ddecSRafael J. Wysocki status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 2197f28ddecSRafael J. Wysocki NULL, acpi_bus_offline, (void *)false, 2207f28ddecSRafael J. Wysocki (void **)&errdev); 2217f28ddecSRafael J. Wysocki if (status == AE_SUPPORT) { 2227f28ddecSRafael J. Wysocki dev_warn(errdev, "Offline disabled.\n"); 223683058e3SRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 2247f28ddecSRafael J. Wysocki acpi_bus_online, NULL, NULL, NULL); 2257f28ddecSRafael J. Wysocki return -EPERM; 2267f28ddecSRafael J. Wysocki } 2277f28ddecSRafael J. Wysocki acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev); 228303bfdb1SRafael J. Wysocki if (errdev) { 229303bfdb1SRafael J. Wysocki errdev = NULL; 230303bfdb1SRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 2317f28ddecSRafael J. Wysocki NULL, acpi_bus_offline, (void *)true, 2327f28ddecSRafael J. Wysocki (void **)&errdev); 233ffc10d82SMichal Hocko if (!errdev) 2347f28ddecSRafael J. Wysocki acpi_bus_offline(handle, 0, (void *)true, 235303bfdb1SRafael J. Wysocki (void **)&errdev); 236303bfdb1SRafael J. Wysocki 237ffc10d82SMichal Hocko if (errdev) { 238303bfdb1SRafael J. Wysocki dev_warn(errdev, "Offline failed.\n"); 2397f28ddecSRafael J. Wysocki acpi_bus_online(handle, 0, NULL, NULL); 240303bfdb1SRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, 2417f28ddecSRafael J. Wysocki ACPI_UINT32_MAX, acpi_bus_online, 2427f28ddecSRafael J. Wysocki NULL, NULL, NULL); 243683058e3SRafael J. Wysocki return -EBUSY; 244683058e3SRafael J. Wysocki } 245303bfdb1SRafael J. Wysocki } 246d22ddcbcSRafael J. Wysocki return 0; 247d22ddcbcSRafael J. Wysocki } 248d22ddcbcSRafael J. Wysocki 249d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device) 250d22ddcbcSRafael J. Wysocki { 251d22ddcbcSRafael J. Wysocki acpi_handle handle = device->handle; 252d22ddcbcSRafael J. Wysocki unsigned long long sta; 253d22ddcbcSRafael J. Wysocki acpi_status status; 254d22ddcbcSRafael J. Wysocki 255ffc10d82SMichal Hocko if (device->handler && device->handler->hotplug.demand_offline) { 256caa73ea1SRafael J. Wysocki if (!acpi_scan_is_offline(device, true)) 257d22ddcbcSRafael J. Wysocki return -EBUSY; 258d22ddcbcSRafael J. Wysocki } else { 259d22ddcbcSRafael J. Wysocki int error = acpi_scan_try_to_offline(device); 260d22ddcbcSRafael J. Wysocki if (error) 261d22ddcbcSRafael J. Wysocki return error; 262d22ddcbcSRafael J. Wysocki } 263683058e3SRafael J. Wysocki 264e52d9d8cSRafael J. Wysocki acpi_handle_debug(handle, "Ejecting\n"); 26526d46867SZhang Rui 266bfee26dbSRafael J. Wysocki acpi_bus_trim(device); 267683058e3SRafael J. Wysocki 2687d2421f8SJiang Liu acpi_evaluate_lck(handle, 0); 2691da177e4SLinus Torvalds /* 2701da177e4SLinus Torvalds * TBD: _EJD support. 2711da177e4SLinus Torvalds */ 2727d2421f8SJiang Liu status = acpi_evaluate_ej0(handle); 2737d2421f8SJiang Liu if (status == AE_NOT_FOUND) 274a33ec399SRafael J. Wysocki return -ENODEV; 2757d2421f8SJiang Liu else if (ACPI_FAILURE(status)) 276a33ec399SRafael J. Wysocki return -EIO; 277f058cdf4SRafael J. Wysocki 278882fd12eSToshi Kani /* 279882fd12eSToshi Kani * Verify if eject was indeed successful. If not, log an error 280882fd12eSToshi Kani * message. No need to call _OST since _EJ0 call was made OK. 281882fd12eSToshi Kani */ 282882fd12eSToshi Kani status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); 283882fd12eSToshi Kani if (ACPI_FAILURE(status)) { 284882fd12eSToshi Kani acpi_handle_warn(handle, 285882fd12eSToshi Kani "Status check after eject failed (0x%x)\n", status); 286882fd12eSToshi Kani } else if (sta & ACPI_STA_DEVICE_ENABLED) { 287882fd12eSToshi Kani acpi_handle_warn(handle, 288882fd12eSToshi Kani "Eject incomplete - status 0x%llx\n", sta); 289882fd12eSToshi Kani } 290882fd12eSToshi Kani 291a33ec399SRafael J. Wysocki return 0; 292a33ec399SRafael J. Wysocki } 293f058cdf4SRafael J. Wysocki 294443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev) 295443fc820SRafael J. Wysocki { 296443fc820SRafael J. Wysocki if (!acpi_device_enumerated(adev)) { 297443fc820SRafael J. Wysocki dev_warn(&adev->dev, "Still not present\n"); 298443fc820SRafael J. Wysocki return -EALREADY; 299443fc820SRafael J. Wysocki } 300443fc820SRafael J. Wysocki acpi_bus_trim(adev); 301443fc820SRafael J. Wysocki return 0; 302443fc820SRafael J. Wysocki } 303443fc820SRafael J. Wysocki 304c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev) 305a33ec399SRafael J. Wysocki { 306f943db40SRafael J. Wysocki int error; 307a33ec399SRafael J. Wysocki 308443fc820SRafael J. Wysocki acpi_bus_get_status(adev); 309443fc820SRafael J. Wysocki if (adev->status.present || adev->status.functional) { 310443fc820SRafael J. Wysocki /* 311443fc820SRafael J. Wysocki * This function is only called for device objects for which 312443fc820SRafael J. Wysocki * matching scan handlers exist. The only situation in which 313443fc820SRafael J. Wysocki * the scan handler is not attached to this device object yet 314443fc820SRafael J. Wysocki * is when the device has just appeared (either it wasn't 315443fc820SRafael J. Wysocki * present at all before or it was removed and then added 316443fc820SRafael J. Wysocki * again). 317443fc820SRafael J. Wysocki */ 318443fc820SRafael J. Wysocki if (adev->handler) { 319443fc820SRafael J. Wysocki dev_warn(&adev->dev, "Already enumerated\n"); 320443fc820SRafael J. Wysocki return -EALREADY; 321443fc820SRafael J. Wysocki } 322c27b2c33SRafael J. Wysocki error = acpi_bus_scan(adev->handle); 323a33ec399SRafael J. Wysocki if (error) { 324c27b2c33SRafael J. Wysocki dev_warn(&adev->dev, "Namespace scan failure\n"); 325c27b2c33SRafael J. Wysocki return error; 326a33ec399SRafael J. Wysocki } 32746394fd0SRafael J. Wysocki if (!adev->handler) { 328c27b2c33SRafael J. Wysocki dev_warn(&adev->dev, "Enumeration failure\n"); 329443fc820SRafael J. Wysocki error = -ENODEV; 330443fc820SRafael J. Wysocki } 331443fc820SRafael J. Wysocki } else { 332443fc820SRafael J. Wysocki error = acpi_scan_device_not_present(adev); 333443fc820SRafael J. Wysocki } 334443fc820SRafael J. Wysocki return error; 335443fc820SRafael J. Wysocki } 336443fc820SRafael J. Wysocki 337443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev) 338443fc820SRafael J. Wysocki { 339443fc820SRafael J. Wysocki struct acpi_scan_handler *handler = adev->handler; 340443fc820SRafael J. Wysocki struct acpi_device *child; 341443fc820SRafael J. Wysocki int error; 342443fc820SRafael J. Wysocki 343443fc820SRafael J. Wysocki acpi_bus_get_status(adev); 344443fc820SRafael J. Wysocki if (!(adev->status.present || adev->status.functional)) { 345443fc820SRafael J. Wysocki acpi_scan_device_not_present(adev); 346443fc820SRafael J. Wysocki return 0; 347443fc820SRafael J. Wysocki } 348443fc820SRafael J. Wysocki if (handler && handler->hotplug.scan_dependent) 349443fc820SRafael J. Wysocki return handler->hotplug.scan_dependent(adev); 350443fc820SRafael J. Wysocki 351443fc820SRafael J. Wysocki error = acpi_bus_scan(adev->handle); 352443fc820SRafael J. Wysocki if (error) { 353443fc820SRafael J. Wysocki dev_warn(&adev->dev, "Namespace scan failure\n"); 354443fc820SRafael J. Wysocki return error; 355443fc820SRafael J. Wysocki } 356443fc820SRafael J. Wysocki list_for_each_entry(child, &adev->children, node) { 357443fc820SRafael J. Wysocki error = acpi_scan_bus_check(child); 358443fc820SRafael J. Wysocki if (error) 359443fc820SRafael J. Wysocki return error; 360a33ec399SRafael J. Wysocki } 361c27b2c33SRafael J. Wysocki return 0; 362c27b2c33SRafael J. Wysocki } 363c27b2c33SRafael J. Wysocki 3643c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type) 3653c2cc7ffSRafael J. Wysocki { 3663c2cc7ffSRafael J. Wysocki switch (type) { 3673c2cc7ffSRafael J. Wysocki case ACPI_NOTIFY_BUS_CHECK: 3683c2cc7ffSRafael J. Wysocki return acpi_scan_bus_check(adev); 3693c2cc7ffSRafael J. Wysocki case ACPI_NOTIFY_DEVICE_CHECK: 3703c2cc7ffSRafael J. Wysocki return acpi_scan_device_check(adev); 3713c2cc7ffSRafael J. Wysocki case ACPI_NOTIFY_EJECT_REQUEST: 3723c2cc7ffSRafael J. Wysocki case ACPI_OST_EC_OSPM_EJECT: 373dd2151beSRafael J. Wysocki if (adev->handler && !adev->handler->hotplug.enabled) { 374dd2151beSRafael J. Wysocki dev_info(&adev->dev, "Eject disabled\n"); 375dd2151beSRafael J. Wysocki return -EPERM; 376dd2151beSRafael J. Wysocki } 377700b8422SRafael J. Wysocki acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST, 378dd2151beSRafael J. Wysocki ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); 3793c2cc7ffSRafael J. Wysocki return acpi_scan_hot_remove(adev); 3803c2cc7ffSRafael J. Wysocki } 3813c2cc7ffSRafael J. Wysocki return -EINVAL; 3823c2cc7ffSRafael J. Wysocki } 3833c2cc7ffSRafael J. Wysocki 3841e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src) 385c27b2c33SRafael J. Wysocki { 386c27b2c33SRafael J. Wysocki u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; 3873c2cc7ffSRafael J. Wysocki int error = -ENODEV; 388c27b2c33SRafael J. Wysocki 389c27b2c33SRafael J. Wysocki lock_device_hotplug(); 390c27b2c33SRafael J. Wysocki mutex_lock(&acpi_scan_lock); 391c27b2c33SRafael J. Wysocki 392c27b2c33SRafael J. Wysocki /* 393c27b2c33SRafael J. Wysocki * The device object's ACPI handle cannot become invalid as long as we 3943c2cc7ffSRafael J. Wysocki * are holding acpi_scan_lock, but it might have become invalid before 395c27b2c33SRafael J. Wysocki * that lock was acquired. 396c27b2c33SRafael J. Wysocki */ 397c27b2c33SRafael J. Wysocki if (adev->handle == INVALID_ACPI_HANDLE) 3983c2cc7ffSRafael J. Wysocki goto err_out; 399c27b2c33SRafael J. Wysocki 4001e2380cdSRafael J. Wysocki if (adev->flags.is_dock_station) { 4011e2380cdSRafael J. Wysocki error = dock_notify(adev, src); 4021e2380cdSRafael J. Wysocki } else if (adev->flags.hotplug_notify) { 4033c2cc7ffSRafael J. Wysocki error = acpi_generic_hotplug_event(adev, src); 4043c2cc7ffSRafael J. Wysocki } else { 405be27b3dcSRafael J. Wysocki int (*notify)(struct acpi_device *, u32); 4063c2cc7ffSRafael J. Wysocki 4073c2cc7ffSRafael J. Wysocki acpi_lock_hp_context(); 408be27b3dcSRafael J. Wysocki notify = adev->hp ? adev->hp->notify : NULL; 4093c2cc7ffSRafael J. Wysocki acpi_unlock_hp_context(); 4103c2cc7ffSRafael J. Wysocki /* 4113c2cc7ffSRafael J. Wysocki * There may be additional notify handlers for device objects 4123c2cc7ffSRafael J. Wysocki * without the .event() callback, so ignore them here. 4133c2cc7ffSRafael J. Wysocki */ 414be27b3dcSRafael J. Wysocki if (notify) 415be27b3dcSRafael J. Wysocki error = notify(adev, src); 4163c2cc7ffSRafael J. Wysocki else 4173c2cc7ffSRafael J. Wysocki goto out; 418c27b2c33SRafael J. Wysocki } 419d429e5c1SLee, Chun-Yi switch (error) { 420d429e5c1SLee, Chun-Yi case 0: 421a33ec399SRafael J. Wysocki ost_code = ACPI_OST_SC_SUCCESS; 422d429e5c1SLee, Chun-Yi break; 423d429e5c1SLee, Chun-Yi case -EPERM: 424d429e5c1SLee, Chun-Yi ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED; 425d429e5c1SLee, Chun-Yi break; 426d429e5c1SLee, Chun-Yi case -EBUSY: 427d429e5c1SLee, Chun-Yi ost_code = ACPI_OST_SC_DEVICE_BUSY; 428d429e5c1SLee, Chun-Yi break; 429d429e5c1SLee, Chun-Yi default: 430d429e5c1SLee, Chun-Yi ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; 431d429e5c1SLee, Chun-Yi break; 432d429e5c1SLee, Chun-Yi } 433a33ec399SRafael J. Wysocki 4343c2cc7ffSRafael J. Wysocki err_out: 435700b8422SRafael J. Wysocki acpi_evaluate_ost(adev->handle, src, ost_code, NULL); 4363c2cc7ffSRafael J. Wysocki 4373c2cc7ffSRafael J. Wysocki out: 43878ea4639SRafael J. Wysocki acpi_bus_put_acpi_device(adev); 439a33ec399SRafael J. Wysocki mutex_unlock(&acpi_scan_lock); 440e0ae8feeSRafael J. Wysocki unlock_device_hotplug(); 441a33ec399SRafael J. Wysocki } 442a33ec399SRafael J. Wysocki 4430b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device) 4440b224527SRafael J. Wysocki { 4450b224527SRafael J. Wysocki int i; 4460b224527SRafael J. Wysocki 447993cbe59SRafael J. Wysocki if (device->wakeup.flags.valid) 448993cbe59SRafael J. Wysocki acpi_power_resources_list_free(&device->wakeup.resources); 449993cbe59SRafael J. Wysocki 4501b1f3e16SRafael J. Wysocki if (!device->power.flags.power_resources) 4510b224527SRafael J. Wysocki return; 4520b224527SRafael J. Wysocki 4530b224527SRafael J. Wysocki for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { 4540b224527SRafael J. Wysocki struct acpi_device_power_state *ps = &device->power.states[i]; 4550b224527SRafael J. Wysocki acpi_power_resources_list_free(&ps->resources); 4560b224527SRafael J. Wysocki } 4577f47fa6cSBjorn Helgaas } 4587f47fa6cSBjorn Helgaas 4591890a97aSPatrick Mochel static void acpi_device_release(struct device *dev) 4601890a97aSPatrick Mochel { 4611890a97aSPatrick Mochel struct acpi_device *acpi_dev = to_acpi_device(dev); 4621890a97aSPatrick Mochel 463ffdcd955SMika Westerberg acpi_free_properties(acpi_dev); 464c0af4175SToshi Kani acpi_free_pnp_ids(&acpi_dev->pnp); 4650b224527SRafael J. Wysocki acpi_free_power_resources_lists(acpi_dev); 4661890a97aSPatrick Mochel kfree(acpi_dev); 4671890a97aSPatrick Mochel } 4681890a97aSPatrick Mochel 469d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device) 470caf5c03fSRafael J. Wysocki { 471ca9dc8d4SLukas Wunner struct acpi_device_bus_id *acpi_device_bus_id; 472ca9dc8d4SLukas Wunner 473d783156eSRafael J. Wysocki mutex_lock(&acpi_device_lock); 474d783156eSRafael J. Wysocki if (device->parent) 475d783156eSRafael J. Wysocki list_del(&device->node); 476d783156eSRafael J. Wysocki 477ca9dc8d4SLukas Wunner list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) 478ca9dc8d4SLukas Wunner if (!strcmp(acpi_device_bus_id->bus_id, 479ca9dc8d4SLukas Wunner acpi_device_hid(device))) { 480eb50aaf9SAndy Shevchenko ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no); 481eb50aaf9SAndy Shevchenko if (ida_is_empty(&acpi_device_bus_id->instance_ida)) { 482ca9dc8d4SLukas Wunner list_del(&acpi_device_bus_id->node); 483a58015d6SDexuan Cui kfree_const(acpi_device_bus_id->bus_id); 484ca9dc8d4SLukas Wunner kfree(acpi_device_bus_id); 485ca9dc8d4SLukas Wunner } 486ca9dc8d4SLukas Wunner break; 487ca9dc8d4SLukas Wunner } 488ca9dc8d4SLukas Wunner 489d783156eSRafael J. Wysocki list_del(&device->wakeup_list); 490d783156eSRafael J. Wysocki mutex_unlock(&acpi_device_lock); 491d783156eSRafael J. Wysocki 492d783156eSRafael J. Wysocki acpi_power_add_remove_device(device, false); 493d783156eSRafael J. Wysocki acpi_device_remove_files(device); 494d783156eSRafael J. Wysocki if (device->remove) 495d783156eSRafael J. Wysocki device->remove(device); 496d783156eSRafael J. Wysocki 497d783156eSRafael J. Wysocki device_del(&device->dev); 498d783156eSRafael J. Wysocki } 499d783156eSRafael J. Wysocki 50068bdb677SOctavian Purdila static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain); 50168bdb677SOctavian Purdila 502d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list); 503d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock); 504d783156eSRafael J. Wysocki 505d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used) 506d783156eSRafael J. Wysocki { 507d783156eSRafael J. Wysocki for (;;) { 508d783156eSRafael J. Wysocki struct acpi_device *adev; 509d783156eSRafael J. Wysocki 510d783156eSRafael J. Wysocki mutex_lock(&acpi_device_del_lock); 511d783156eSRafael J. Wysocki 512d783156eSRafael J. Wysocki if (list_empty(&acpi_device_del_list)) { 513d783156eSRafael J. Wysocki mutex_unlock(&acpi_device_del_lock); 514d783156eSRafael J. Wysocki break; 515d783156eSRafael J. Wysocki } 516d783156eSRafael J. Wysocki adev = list_first_entry(&acpi_device_del_list, 517d783156eSRafael J. Wysocki struct acpi_device, del_list); 518d783156eSRafael J. Wysocki list_del(&adev->del_list); 519d783156eSRafael J. Wysocki 520d783156eSRafael J. Wysocki mutex_unlock(&acpi_device_del_lock); 521d783156eSRafael J. Wysocki 52268bdb677SOctavian Purdila blocking_notifier_call_chain(&acpi_reconfig_chain, 52368bdb677SOctavian Purdila ACPI_RECONFIG_DEVICE_REMOVE, adev); 52468bdb677SOctavian Purdila 525d783156eSRafael J. Wysocki acpi_device_del(adev); 526d783156eSRafael J. Wysocki /* 527d783156eSRafael J. Wysocki * Drop references to all power resources that might have been 528d783156eSRafael J. Wysocki * used by the device. 529d783156eSRafael J. Wysocki */ 530d783156eSRafael J. Wysocki acpi_power_transition(adev, ACPI_STATE_D3_COLD); 5314cbaba4eSAndy Shevchenko acpi_dev_put(adev); 532d783156eSRafael J. Wysocki } 533d783156eSRafael J. Wysocki } 534d783156eSRafael J. Wysocki 535d783156eSRafael J. Wysocki /** 536d783156eSRafael J. Wysocki * acpi_scan_drop_device - Drop an ACPI device object. 537d783156eSRafael J. Wysocki * @handle: Handle of an ACPI namespace node, not used. 538d783156eSRafael J. Wysocki * @context: Address of the ACPI device object to drop. 539d783156eSRafael J. Wysocki * 540d783156eSRafael J. Wysocki * This is invoked by acpi_ns_delete_node() during the removal of the ACPI 541d783156eSRafael J. Wysocki * namespace node the device object pointed to by @context is attached to. 542d783156eSRafael J. Wysocki * 543d783156eSRafael J. Wysocki * The unregistration is carried out asynchronously to avoid running 544d783156eSRafael J. Wysocki * acpi_device_del() under the ACPICA's namespace mutex and the list is used to 545d783156eSRafael J. Wysocki * ensure the correct ordering (the device objects must be unregistered in the 546d783156eSRafael J. Wysocki * same order in which the corresponding namespace nodes are deleted). 547d783156eSRafael J. Wysocki */ 548d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context) 549d783156eSRafael J. Wysocki { 550d783156eSRafael J. Wysocki static DECLARE_WORK(work, acpi_device_del_work_fn); 551d783156eSRafael J. Wysocki struct acpi_device *adev = context; 552d783156eSRafael J. Wysocki 553d783156eSRafael J. Wysocki mutex_lock(&acpi_device_del_lock); 554d783156eSRafael J. Wysocki 555d783156eSRafael J. Wysocki /* 556d783156eSRafael J. Wysocki * Use the ACPI hotplug workqueue which is ordered, so this work item 557d783156eSRafael J. Wysocki * won't run after any hotplug work items submitted subsequently. That 558d783156eSRafael J. Wysocki * prevents attempts to register device objects identical to those being 559d783156eSRafael J. Wysocki * deleted from happening concurrently (such attempts result from 560d783156eSRafael J. Wysocki * hotplug events handled via the ACPI hotplug workqueue). It also will 561935ab850STom Saeger * run after all of the work items submitted previously, which helps 562d783156eSRafael J. Wysocki * those work items to ensure that they are not accessing stale device 563d783156eSRafael J. Wysocki * objects. 564d783156eSRafael J. Wysocki */ 565d783156eSRafael J. Wysocki if (list_empty(&acpi_device_del_list)) 566d783156eSRafael J. Wysocki acpi_queue_hotplug_work(&work); 567d783156eSRafael J. Wysocki 568d783156eSRafael J. Wysocki list_add_tail(&adev->del_list, &acpi_device_del_list); 569d783156eSRafael J. Wysocki /* Make acpi_ns_validate_handle() return NULL for this handle. */ 570d783156eSRafael J. Wysocki adev->handle = INVALID_ACPI_HANDLE; 571d783156eSRafael J. Wysocki 572d783156eSRafael J. Wysocki mutex_unlock(&acpi_device_del_lock); 573caf5c03fSRafael J. Wysocki } 574caf5c03fSRafael J. Wysocki 57583e2c8fcSRafael J. Wysocki static struct acpi_device *handle_to_device(acpi_handle handle, 57678ea4639SRafael J. Wysocki void (*callback)(void *)) 577caf5c03fSRafael J. Wysocki { 57883e2c8fcSRafael J. Wysocki struct acpi_device *adev = NULL; 579caf5c03fSRafael J. Wysocki acpi_status status; 580caf5c03fSRafael J. Wysocki 58178ea4639SRafael J. Wysocki status = acpi_get_data_full(handle, acpi_scan_drop_device, 58283e2c8fcSRafael J. Wysocki (void **)&adev, callback); 58383e2c8fcSRafael J. Wysocki if (ACPI_FAILURE(status) || !adev) { 58483e2c8fcSRafael J. Wysocki acpi_handle_debug(handle, "No context!\n"); 58583e2c8fcSRafael J. Wysocki return NULL; 586caf5c03fSRafael J. Wysocki } 58783e2c8fcSRafael J. Wysocki return adev; 588caf5c03fSRafael J. Wysocki } 58978ea4639SRafael J. Wysocki 59078ea4639SRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device) 59178ea4639SRafael J. Wysocki { 59283e2c8fcSRafael J. Wysocki if (!device) 59383e2c8fcSRafael J. Wysocki return -EINVAL; 59483e2c8fcSRafael J. Wysocki 59583e2c8fcSRafael J. Wysocki *device = handle_to_device(handle, NULL); 59683e2c8fcSRafael J. Wysocki if (!*device) 59783e2c8fcSRafael J. Wysocki return -ENODEV; 59883e2c8fcSRafael J. Wysocki 59983e2c8fcSRafael J. Wysocki return 0; 60078ea4639SRafael J. Wysocki } 6016585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device); 602caf5c03fSRafael J. Wysocki 603e3c963c4SRafael J. Wysocki /** 604e3c963c4SRafael J. Wysocki * acpi_fetch_acpi_dev - Retrieve ACPI device object. 605e3c963c4SRafael J. Wysocki * @handle: ACPI handle associated with the requested ACPI device object. 606e3c963c4SRafael J. Wysocki * 607e3c963c4SRafael J. Wysocki * Return a pointer to the ACPI device object associated with @handle, if 608e3c963c4SRafael J. Wysocki * present, or NULL otherwise. 609e3c963c4SRafael J. Wysocki */ 610e3c963c4SRafael J. Wysocki struct acpi_device *acpi_fetch_acpi_dev(acpi_handle handle) 611e3c963c4SRafael J. Wysocki { 612e3c963c4SRafael J. Wysocki return handle_to_device(handle, NULL); 613e3c963c4SRafael J. Wysocki } 614e3c963c4SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_fetch_acpi_dev); 615e3c963c4SRafael J. Wysocki 61678ea4639SRafael J. Wysocki static void get_acpi_device(void *dev) 61778ea4639SRafael J. Wysocki { 6184cbaba4eSAndy Shevchenko acpi_dev_get(dev); 61978ea4639SRafael J. Wysocki } 62078ea4639SRafael J. Wysocki 62178ea4639SRafael J. Wysocki struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle) 62278ea4639SRafael J. Wysocki { 62383e2c8fcSRafael J. Wysocki return handle_to_device(handle, get_acpi_device); 62478ea4639SRafael J. Wysocki } 6252c59a32dSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_get_acpi_device); 62678ea4639SRafael J. Wysocki 627c1013ff7SRafael J. Wysocki static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id) 628c1013ff7SRafael J. Wysocki { 629c1013ff7SRafael J. Wysocki struct acpi_device_bus_id *acpi_device_bus_id; 630c1013ff7SRafael J. Wysocki 631c1013ff7SRafael J. Wysocki /* Find suitable bus_id and instance number in acpi_bus_id_list. */ 632c1013ff7SRafael J. Wysocki list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { 633c1013ff7SRafael J. Wysocki if (!strcmp(acpi_device_bus_id->bus_id, dev_id)) 634c1013ff7SRafael J. Wysocki return acpi_device_bus_id; 635c1013ff7SRafael J. Wysocki } 636c1013ff7SRafael J. Wysocki return NULL; 637c1013ff7SRafael J. Wysocki } 638c1013ff7SRafael J. Wysocki 639eb50aaf9SAndy Shevchenko static int acpi_device_set_name(struct acpi_device *device, 640eb50aaf9SAndy Shevchenko struct acpi_device_bus_id *acpi_device_bus_id) 641eb50aaf9SAndy Shevchenko { 642eb50aaf9SAndy Shevchenko struct ida *instance_ida = &acpi_device_bus_id->instance_ida; 643eb50aaf9SAndy Shevchenko int result; 644eb50aaf9SAndy Shevchenko 645eb50aaf9SAndy Shevchenko result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL); 646eb50aaf9SAndy Shevchenko if (result < 0) 647eb50aaf9SAndy Shevchenko return result; 648eb50aaf9SAndy Shevchenko 649eb50aaf9SAndy Shevchenko device->pnp.instance_no = result; 650eb50aaf9SAndy Shevchenko dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result); 651eb50aaf9SAndy Shevchenko return 0; 652eb50aaf9SAndy Shevchenko } 653eb50aaf9SAndy Shevchenko 654c6a493a1SRafael J. Wysocki static int acpi_tie_acpi_dev(struct acpi_device *adev) 655c6a493a1SRafael J. Wysocki { 656c6a493a1SRafael J. Wysocki acpi_handle handle = adev->handle; 657c6a493a1SRafael J. Wysocki acpi_status status; 658c6a493a1SRafael J. Wysocki 659c6a493a1SRafael J. Wysocki if (!handle) 660c6a493a1SRafael J. Wysocki return 0; 661c6a493a1SRafael J. Wysocki 662c6a493a1SRafael J. Wysocki status = acpi_attach_data(handle, acpi_scan_drop_device, adev); 663c6a493a1SRafael J. Wysocki if (ACPI_FAILURE(status)) { 664c6a493a1SRafael J. Wysocki acpi_handle_err(handle, "Unable to attach device data\n"); 665c6a493a1SRafael J. Wysocki return -ENODEV; 666c6a493a1SRafael J. Wysocki } 667c6a493a1SRafael J. Wysocki 668c6a493a1SRafael J. Wysocki return 0; 669c6a493a1SRafael J. Wysocki } 670c6a493a1SRafael J. Wysocki 671882c982dSHeikki Krogerus static void acpi_store_pld_crc(struct acpi_device *adev) 672882c982dSHeikki Krogerus { 673882c982dSHeikki Krogerus struct acpi_pld_info *pld; 674882c982dSHeikki Krogerus acpi_status status; 675882c982dSHeikki Krogerus 676882c982dSHeikki Krogerus status = acpi_get_physical_device_location(adev->handle, &pld); 677882c982dSHeikki Krogerus if (ACPI_FAILURE(status)) 678882c982dSHeikki Krogerus return; 679882c982dSHeikki Krogerus 680882c982dSHeikki Krogerus adev->pld_crc = crc32(~0, pld, sizeof(*pld)); 681882c982dSHeikki Krogerus ACPI_FREE(pld); 682882c982dSHeikki Krogerus } 683882c982dSHeikki Krogerus 6845f4ce260SRafael J. Wysocki static int __acpi_device_add(struct acpi_device *device, 68582c7d5efSRafael J. Wysocki void (*release)(struct device *)) 6861da177e4SLinus Torvalds { 687c1013ff7SRafael J. Wysocki struct acpi_device_bus_id *acpi_device_bus_id; 688e49bd2ddSZhang Rui int result; 68966b7ed40SBjorn Helgaas 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * Linkage 6921da177e4SLinus Torvalds * ------- 6931da177e4SLinus Torvalds * Link this device to its parent and siblings. 6941da177e4SLinus Torvalds */ 6951da177e4SLinus Torvalds INIT_LIST_HEAD(&device->children); 6961da177e4SLinus Torvalds INIT_LIST_HEAD(&device->node); 6971da177e4SLinus Torvalds INIT_LIST_HEAD(&device->wakeup_list); 6981033f904SLan Tianyu INIT_LIST_HEAD(&device->physical_node_list); 699d783156eSRafael J. Wysocki INIT_LIST_HEAD(&device->del_list); 7001033f904SLan Tianyu mutex_init(&device->physical_node_lock); 7011da177e4SLinus Torvalds 7029090589dSShaohua Li mutex_lock(&acpi_device_lock); 703c1013ff7SRafael J. Wysocki 704c1013ff7SRafael J. Wysocki acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device)); 705c1013ff7SRafael J. Wysocki if (acpi_device_bus_id) { 706eb50aaf9SAndy Shevchenko result = acpi_device_set_name(device, acpi_device_bus_id); 707eb50aaf9SAndy Shevchenko if (result) 708eb50aaf9SAndy Shevchenko goto err_unlock; 709c1013ff7SRafael J. Wysocki } else { 710c1013ff7SRafael J. Wysocki acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id), 711c1013ff7SRafael J. Wysocki GFP_KERNEL); 712c1013ff7SRafael J. Wysocki if (!acpi_device_bus_id) { 713c1013ff7SRafael J. Wysocki result = -ENOMEM; 714c1013ff7SRafael J. Wysocki goto err_unlock; 715e49bd2ddSZhang Rui } 716a58015d6SDexuan Cui acpi_device_bus_id->bus_id = 717a58015d6SDexuan Cui kstrdup_const(acpi_device_hid(device), GFP_KERNEL); 718a58015d6SDexuan Cui if (!acpi_device_bus_id->bus_id) { 719c1013ff7SRafael J. Wysocki kfree(acpi_device_bus_id); 720a58015d6SDexuan Cui result = -ENOMEM; 721c1013ff7SRafael J. Wysocki goto err_unlock; 722a58015d6SDexuan Cui } 723a58015d6SDexuan Cui 724eb50aaf9SAndy Shevchenko ida_init(&acpi_device_bus_id->instance_ida); 725eb50aaf9SAndy Shevchenko 726eb50aaf9SAndy Shevchenko result = acpi_device_set_name(device, acpi_device_bus_id); 727eb50aaf9SAndy Shevchenko if (result) { 7280c8bd174SChristophe JAILLET kfree_const(acpi_device_bus_id->bus_id); 729eb50aaf9SAndy Shevchenko kfree(acpi_device_bus_id); 730eb50aaf9SAndy Shevchenko goto err_unlock; 731eb50aaf9SAndy Shevchenko } 732eb50aaf9SAndy Shevchenko 733e49bd2ddSZhang Rui list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); 734e49bd2ddSZhang Rui } 735e49bd2ddSZhang Rui 73633b57150SLen Brown if (device->parent) 7371da177e4SLinus Torvalds list_add_tail(&device->node, &device->parent->children); 73833b57150SLen Brown 7391da177e4SLinus Torvalds if (device->wakeup.flags.valid) 7401da177e4SLinus Torvalds list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); 7415e73c518SRafael J. Wysocki 742882c982dSHeikki Krogerus acpi_store_pld_crc(device); 743882c982dSHeikki Krogerus 7449090589dSShaohua Li mutex_unlock(&acpi_device_lock); 7451da177e4SLinus Torvalds 7461890a97aSPatrick Mochel if (device->parent) 74766b7ed40SBjorn Helgaas device->dev.parent = &device->parent->dev; 7485e73c518SRafael J. Wysocki 7491890a97aSPatrick Mochel device->dev.bus = &acpi_bus_type; 75082c7d5efSRafael J. Wysocki device->dev.release = release; 751cf860be6SRafael J. Wysocki result = device_add(&device->dev); 752e49bd2ddSZhang Rui if (result) { 7538b12b922SAlex Chiang dev_err(&device->dev, "Error registering device\n"); 754d43e167dSRafael J. Wysocki goto err; 755e49bd2ddSZhang Rui } 756f883d9dbSPatrick Mochel 757e49bd2ddSZhang Rui result = acpi_device_setup_files(device); 758e49bd2ddSZhang Rui if (result) 7598acf4108SHanjun Guo pr_err("Error creating sysfs interface for device %s\n", 7600794469dSKay Sievers dev_name(&device->dev)); 761e49bd2ddSZhang Rui 762e49bd2ddSZhang Rui return 0; 763d43e167dSRafael J. Wysocki 764d43e167dSRafael J. Wysocki err: 7659090589dSShaohua Li mutex_lock(&acpi_device_lock); 7665e73c518SRafael J. Wysocki 76733b57150SLen Brown if (device->parent) 768e49bd2ddSZhang Rui list_del(&device->node); 7695e73c518SRafael J. Wysocki 770e49bd2ddSZhang Rui list_del(&device->wakeup_list); 771a58015d6SDexuan Cui 772c1013ff7SRafael J. Wysocki err_unlock: 7739090589dSShaohua Li mutex_unlock(&acpi_device_lock); 774d43e167dSRafael J. Wysocki 775d783156eSRafael J. Wysocki acpi_detach_data(device->handle, acpi_scan_drop_device); 7765e73c518SRafael J. Wysocki 777e49bd2ddSZhang Rui return result; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7805f4ce260SRafael J. Wysocki int acpi_device_add(struct acpi_device *adev, void (*release)(struct device *)) 7815f4ce260SRafael J. Wysocki { 7825f4ce260SRafael J. Wysocki int ret; 7835f4ce260SRafael J. Wysocki 7845f4ce260SRafael J. Wysocki ret = acpi_tie_acpi_dev(adev); 7855f4ce260SRafael J. Wysocki if (ret) 7865f4ce260SRafael J. Wysocki return ret; 7875f4ce260SRafael J. Wysocki 7885f4ce260SRafael J. Wysocki return __acpi_device_add(adev, release); 7895f4ce260SRafael J. Wysocki } 7905f4ce260SRafael J. Wysocki 7919e89dde2SZhang Rui /* -------------------------------------------------------------------------- 7921da177e4SLinus Torvalds Device Enumeration 7931da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 7942ef33ee7SRafael J. Wysocki static bool acpi_info_matches_ids(struct acpi_device_info *info, 7952ef33ee7SRafael J. Wysocki const char * const ids[]) 796637b9f1aSHans de Goede { 7972ef33ee7SRafael J. Wysocki struct acpi_pnp_device_id_list *cid_list = NULL; 79802050558SAndy Shevchenko int i, index; 799637b9f1aSHans de Goede 800637b9f1aSHans de Goede if (!(info->valid & ACPI_VALID_HID)) 801637b9f1aSHans de Goede return false; 802637b9f1aSHans de Goede 80302050558SAndy Shevchenko index = match_string(ids, -1, info->hardware_id.string); 80402050558SAndy Shevchenko if (index >= 0) 80502050558SAndy Shevchenko return true; 80602050558SAndy Shevchenko 8072ef33ee7SRafael J. Wysocki if (info->valid & ACPI_VALID_CID) 8082ef33ee7SRafael J. Wysocki cid_list = &info->compatible_id_list; 8092ef33ee7SRafael J. Wysocki 8102ef33ee7SRafael J. Wysocki if (!cid_list) 81102050558SAndy Shevchenko return false; 8122ef33ee7SRafael J. Wysocki 81302050558SAndy Shevchenko for (i = 0; i < cid_list->count; i++) { 81402050558SAndy Shevchenko index = match_string(ids, -1, cid_list->ids[i].string); 81502050558SAndy Shevchenko if (index >= 0) 8162ef33ee7SRafael J. Wysocki return true; 8172ef33ee7SRafael J. Wysocki } 818637b9f1aSHans de Goede 819637b9f1aSHans de Goede return false; 820637b9f1aSHans de Goede } 821637b9f1aSHans de Goede 822637b9f1aSHans de Goede /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */ 8232ef33ee7SRafael J. Wysocki static const char * const acpi_ignore_dep_ids[] = { 8242ef33ee7SRafael J. Wysocki "PNP0D80", /* Windows-compatible System Power Management Controller */ 8259272e97aSHans de Goede "INT33BD", /* Intel Baytrail Mailbox Device */ 826637b9f1aSHans de Goede NULL 827637b9f1aSHans de Goede }; 828637b9f1aSHans de Goede 8299d9bcae4SHans de Goede /* List of HIDs for which we honor deps of matching ACPI devs, when checking _DEP lists. */ 8309d9bcae4SHans de Goede static const char * const acpi_honor_dep_ids[] = { 8319d9bcae4SHans de Goede "INT3472", /* Camera sensor PMIC / clk and regulator info */ 8329d9bcae4SHans de Goede NULL 8339d9bcae4SHans de Goede }; 8349d9bcae4SHans de Goede 8355c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle) 8365c478f49SBjorn Helgaas { 837e3c963c4SRafael J. Wysocki struct acpi_device *device; 8385c478f49SBjorn Helgaas acpi_status status; 8395c478f49SBjorn Helgaas 8405c478f49SBjorn Helgaas /* 8415c478f49SBjorn Helgaas * Fixed hardware devices do not appear in the namespace and do not 8425c478f49SBjorn Helgaas * have handles, but we fabricate acpi_devices for them, so we have 8435c478f49SBjorn Helgaas * to deal with them specially. 8445c478f49SBjorn Helgaas */ 845456de893SRafael J. Wysocki if (!handle) 8465c478f49SBjorn Helgaas return acpi_root; 8475c478f49SBjorn Helgaas 8485c478f49SBjorn Helgaas do { 8495c478f49SBjorn Helgaas status = acpi_get_parent(handle, &handle); 8505c478f49SBjorn Helgaas if (ACPI_FAILURE(status)) 851456de893SRafael J. Wysocki return status == AE_NULL_ENTRY ? NULL : acpi_root; 852e3c963c4SRafael J. Wysocki 853e3c963c4SRafael J. Wysocki device = acpi_fetch_acpi_dev(handle); 854e3c963c4SRafael J. Wysocki } while (!device); 8555c478f49SBjorn Helgaas return device; 8565c478f49SBjorn Helgaas } 8575c478f49SBjorn Helgaas 858c8f7a62cSLen Brown acpi_status 859c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) 860c8f7a62cSLen Brown { 861c8f7a62cSLen Brown acpi_status status; 862c8f7a62cSLen Brown acpi_handle tmp; 863c8f7a62cSLen Brown struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 864c8f7a62cSLen Brown union acpi_object *obj; 865c8f7a62cSLen Brown 866c8f7a62cSLen Brown status = acpi_get_handle(handle, "_EJD", &tmp); 867c8f7a62cSLen Brown if (ACPI_FAILURE(status)) 868c8f7a62cSLen Brown return status; 869c8f7a62cSLen Brown 870c8f7a62cSLen Brown status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); 871c8f7a62cSLen Brown if (ACPI_SUCCESS(status)) { 872c8f7a62cSLen Brown obj = buffer.pointer; 8733b5fee59SHolger Macht status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, 8743b5fee59SHolger Macht ejd); 875c8f7a62cSLen Brown kfree(buffer.pointer); 876c8f7a62cSLen Brown } 877c8f7a62cSLen Brown return status; 878c8f7a62cSLen Brown } 879c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); 880c8f7a62cSLen Brown 88140381a3cSBjorn Helgaas static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev) 8821da177e4SLinus Torvalds { 88340381a3cSBjorn Helgaas acpi_handle handle = dev->handle; 88440381a3cSBjorn Helgaas struct acpi_device_wakeup *wakeup = &dev->wakeup; 885b581a7f9SRafael J. Wysocki struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 886b581a7f9SRafael J. Wysocki union acpi_object *package = NULL; 8871da177e4SLinus Torvalds union acpi_object *element = NULL; 888b581a7f9SRafael J. Wysocki acpi_status status; 889e88c9c60SRafael J. Wysocki int err = -ENODATA; 8901da177e4SLinus Torvalds 891993cbe59SRafael J. Wysocki INIT_LIST_HEAD(&wakeup->resources); 8921da177e4SLinus Torvalds 893b581a7f9SRafael J. Wysocki /* _PRW */ 894b581a7f9SRafael J. Wysocki status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); 895b581a7f9SRafael J. Wysocki if (ACPI_FAILURE(status)) { 896e52d9d8cSRafael J. Wysocki acpi_handle_info(handle, "_PRW evaluation failed: %s\n", 897e52d9d8cSRafael J. Wysocki acpi_format_exception(status)); 898e88c9c60SRafael J. Wysocki return err; 899b581a7f9SRafael J. Wysocki } 900b581a7f9SRafael J. Wysocki 901b581a7f9SRafael J. Wysocki package = (union acpi_object *)buffer.pointer; 902b581a7f9SRafael J. Wysocki 903e88c9c60SRafael J. Wysocki if (!package || package->package.count < 2) 904b581a7f9SRafael J. Wysocki goto out; 905b581a7f9SRafael J. Wysocki 9061da177e4SLinus Torvalds element = &(package->package.elements[0]); 907e88c9c60SRafael J. Wysocki if (!element) 908b581a7f9SRafael J. Wysocki goto out; 909e88c9c60SRafael J. Wysocki 9101da177e4SLinus Torvalds if (element->type == ACPI_TYPE_PACKAGE) { 9111da177e4SLinus Torvalds if ((element->package.count < 2) || 9121da177e4SLinus Torvalds (element->package.elements[0].type != 9131da177e4SLinus Torvalds ACPI_TYPE_LOCAL_REFERENCE) 914e88c9c60SRafael J. Wysocki || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) 915b581a7f9SRafael J. Wysocki goto out; 916e88c9c60SRafael J. Wysocki 917b581a7f9SRafael J. Wysocki wakeup->gpe_device = 9181da177e4SLinus Torvalds element->package.elements[0].reference.handle; 919b581a7f9SRafael J. Wysocki wakeup->gpe_number = 9201da177e4SLinus Torvalds (u32) element->package.elements[1].integer.value; 9211da177e4SLinus Torvalds } else if (element->type == ACPI_TYPE_INTEGER) { 922b581a7f9SRafael J. Wysocki wakeup->gpe_device = NULL; 923b581a7f9SRafael J. Wysocki wakeup->gpe_number = element->integer.value; 924b581a7f9SRafael J. Wysocki } else { 925b581a7f9SRafael J. Wysocki goto out; 926b581a7f9SRafael J. Wysocki } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds element = &(package->package.elements[1]); 929e88c9c60SRafael J. Wysocki if (element->type != ACPI_TYPE_INTEGER) 930b581a7f9SRafael J. Wysocki goto out; 931e88c9c60SRafael J. Wysocki 932b581a7f9SRafael J. Wysocki wakeup->sleep_state = element->integer.value; 9331da177e4SLinus Torvalds 934e88c9c60SRafael J. Wysocki err = acpi_extract_power_resources(package, 2, &wakeup->resources); 935e88c9c60SRafael J. Wysocki if (err) 936b581a7f9SRafael J. Wysocki goto out; 9371da177e4SLinus Torvalds 9380596a52bSRafael J. Wysocki if (!list_empty(&wakeup->resources)) { 9390596a52bSRafael J. Wysocki int sleep_state; 9409874647bSRafael J. Wysocki 941b5d667ebSRafael J. Wysocki err = acpi_power_wakeup_list_init(&wakeup->resources, 942b5d667ebSRafael J. Wysocki &sleep_state); 943b5d667ebSRafael J. Wysocki if (err) { 944b5d667ebSRafael J. Wysocki acpi_handle_warn(handle, "Retrieving current states " 945b5d667ebSRafael J. Wysocki "of wakeup power resources failed\n"); 946b5d667ebSRafael J. Wysocki acpi_power_resources_list_free(&wakeup->resources); 947b5d667ebSRafael J. Wysocki goto out; 948b5d667ebSRafael J. Wysocki } 9490596a52bSRafael J. Wysocki if (sleep_state < wakeup->sleep_state) { 9500596a52bSRafael J. Wysocki acpi_handle_warn(handle, "Overriding _PRW sleep state " 9510596a52bSRafael J. Wysocki "(S%d) by S%d from power resources\n", 9520596a52bSRafael J. Wysocki (int)wakeup->sleep_state, sleep_state); 9530596a52bSRafael J. Wysocki wakeup->sleep_state = sleep_state; 9540596a52bSRafael J. Wysocki } 9550596a52bSRafael J. Wysocki } 956b581a7f9SRafael J. Wysocki 957b581a7f9SRafael J. Wysocki out: 958b581a7f9SRafael J. Wysocki kfree(buffer.pointer); 959e88c9c60SRafael J. Wysocki return err; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 962a1a66393SRafael J. Wysocki static bool acpi_wakeup_gpe_init(struct acpi_device *device) 9631da177e4SLinus Torvalds { 9640519ade7SMathias Krause static const struct acpi_device_id button_device_ids[] = { 9655ceb5f05SBjorn Helgaas {"PNP0C0C", 0}, /* Power button */ 9665ceb5f05SBjorn Helgaas {"PNP0C0D", 0}, /* Lid */ 9675ceb5f05SBjorn Helgaas {"PNP0C0E", 0}, /* Sleep button */ 96829b71a1cSThomas Renninger {"", 0}, 96929b71a1cSThomas Renninger }; 970bd9b2f9aSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &device->wakeup; 971f517709dSRafael J. Wysocki acpi_status status; 972f517709dSRafael J. Wysocki 973bd9b2f9aSRafael J. Wysocki wakeup->flags.notifier_present = 0; 974f517709dSRafael J. Wysocki 975f517709dSRafael J. Wysocki /* Power button, Lid switch always enable wakeup */ 976f517709dSRafael J. Wysocki if (!acpi_match_device_ids(device, button_device_ids)) { 977b7e38304SZhang Rui if (!acpi_match_device_ids(device, &button_device_ids[1])) { 978b7e38304SZhang Rui /* Do not use Lid/sleep button for S5 wakeup */ 979bd9b2f9aSRafael J. Wysocki if (wakeup->sleep_state == ACPI_STATE_S5) 980bd9b2f9aSRafael J. Wysocki wakeup->sleep_state = ACPI_STATE_S4; 981b7e38304SZhang Rui } 982bd9b2f9aSRafael J. Wysocki acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number); 983f2b56bc8SRafael J. Wysocki device_set_wakeup_capable(&device->dev, true); 984a1a66393SRafael J. Wysocki return true; 985f517709dSRafael J. Wysocki } 986f517709dSRafael J. Wysocki 987a1a66393SRafael J. Wysocki status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device, 988bd9b2f9aSRafael J. Wysocki wakeup->gpe_number); 989a1a66393SRafael J. Wysocki return ACPI_SUCCESS(status); 990f517709dSRafael J. Wysocki } 991f517709dSRafael J. Wysocki 992d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) 993f517709dSRafael J. Wysocki { 994e88c9c60SRafael J. Wysocki int err; 99529b71a1cSThomas Renninger 996d57d09a4SRafael J. Wysocki /* Presence of _PRW indicates wake capable */ 997952c63e9SJiang Liu if (!acpi_has_method(device->handle, "_PRW")) 998d57d09a4SRafael J. Wysocki return; 999d57d09a4SRafael J. Wysocki 100040381a3cSBjorn Helgaas err = acpi_bus_extract_wakeup_device_power_package(device); 1001e88c9c60SRafael J. Wysocki if (err) { 1002e52d9d8cSRafael J. Wysocki dev_err(&device->dev, "Unable to extract wakeup power resources"); 1003d57d09a4SRafael J. Wysocki return; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 1006a1a66393SRafael J. Wysocki device->wakeup.flags.valid = acpi_wakeup_gpe_init(device); 10079b83ccd2SRafael J. Wysocki device->wakeup.prepare_count = 0; 1008a1a66393SRafael J. Wysocki /* 1009a1a66393SRafael J. Wysocki * Call _PSW/_DSW object to disable its ability to wake the sleeping 1010729b2bdbSZhao Yakui * system for the ACPI device with the _PRW object. 1011603fadf3SBjorn Helgaas * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW. 1012729b2bdbSZhao Yakui * So it is necessary to call _DSW object first. Only when it is not 1013729b2bdbSZhao Yakui * present will the _PSW object used. 1014729b2bdbSZhao Yakui */ 1015e88c9c60SRafael J. Wysocki err = acpi_device_sleep_wake(device, 0, 0, 0); 1016e88c9c60SRafael J. Wysocki if (err) 101705de0686STian Tao pr_debug("error in _DSW or _PSW evaluation\n"); 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 1020f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state) 10211da177e4SLinus Torvalds { 1022f33ce568SRafael J. Wysocki struct acpi_device_power_state *ps = &device->power.states[state]; 1023ef85bdbeSRafael J. Wysocki char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' }; 1024ef85bdbeSRafael J. Wysocki struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 1025f33ce568SRafael J. Wysocki acpi_status status; 10269e89dde2SZhang Rui 1027f33ce568SRafael J. Wysocki INIT_LIST_HEAD(&ps->resources); 1028f33ce568SRafael J. Wysocki 1029ef85bdbeSRafael J. Wysocki /* Evaluate "_PRx" to get referenced power resources */ 1030ef85bdbeSRafael J. Wysocki status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer); 1031ef85bdbeSRafael J. Wysocki if (ACPI_SUCCESS(status)) { 1032ef85bdbeSRafael J. Wysocki union acpi_object *package = buffer.pointer; 1033f33ce568SRafael J. Wysocki 1034ef85bdbeSRafael J. Wysocki if (buffer.length && package 1035ef85bdbeSRafael J. Wysocki && package->type == ACPI_TYPE_PACKAGE 1036956ad9d9SRafael J. Wysocki && package->package.count) 1037956ad9d9SRafael J. Wysocki acpi_extract_power_resources(package, 0, &ps->resources); 1038956ad9d9SRafael J. Wysocki 1039ef85bdbeSRafael J. Wysocki ACPI_FREE(buffer.pointer); 1040f33ce568SRafael J. Wysocki } 1041f33ce568SRafael J. Wysocki 1042f33ce568SRafael J. Wysocki /* Evaluate "_PSx" to see if we can do explicit sets */ 1043ef85bdbeSRafael J. Wysocki pathname[2] = 'S'; 1044952c63e9SJiang Liu if (acpi_has_method(device->handle, pathname)) 1045f33ce568SRafael J. Wysocki ps->flags.explicit_set = 1; 1046f33ce568SRafael J. Wysocki 104720dacb71SRafael J. Wysocki /* State is valid if there are means to put the device into it. */ 104820dacb71SRafael J. Wysocki if (!list_empty(&ps->resources) || ps->flags.explicit_set) 1049f33ce568SRafael J. Wysocki ps->flags.valid = 1; 1050f33ce568SRafael J. Wysocki 1051f33ce568SRafael J. Wysocki ps->power = -1; /* Unknown - driver assigned */ 1052f33ce568SRafael J. Wysocki ps->latency = -1; /* Unknown - driver assigned */ 1053f33ce568SRafael J. Wysocki } 1054f33ce568SRafael J. Wysocki 1055d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device) 10561da177e4SLinus Torvalds { 1057b340c7d6SSakari Ailus unsigned long long dsc = ACPI_STATE_D0; 10588bc5053bSRafael J. Wysocki u32 i; 10591da177e4SLinus Torvalds 1060d43e167dSRafael J. Wysocki /* Presence of _PS0|_PR0 indicates 'power manageable' */ 1061952c63e9SJiang Liu if (!acpi_has_method(device->handle, "_PS0") && 1062952c63e9SJiang Liu !acpi_has_method(device->handle, "_PR0")) 1063d43e167dSRafael J. Wysocki return; 1064d43e167dSRafael J. Wysocki 1065d43e167dSRafael J. Wysocki device->flags.power_manageable = 1; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds /* 10689e89dde2SZhang Rui * Power Management Flags 10691da177e4SLinus Torvalds */ 1070952c63e9SJiang Liu if (acpi_has_method(device->handle, "_PSC")) 10719e89dde2SZhang Rui device->power.flags.explicit_get = 1; 1072f25c0ae2SRafael J. Wysocki 1073952c63e9SJiang Liu if (acpi_has_method(device->handle, "_IRC")) 10749e89dde2SZhang Rui device->power.flags.inrush_current = 1; 10751da177e4SLinus Torvalds 1076f25c0ae2SRafael J. Wysocki if (acpi_has_method(device->handle, "_DSW")) 1077f25c0ae2SRafael J. Wysocki device->power.flags.dsw_present = 1; 1078f25c0ae2SRafael J. Wysocki 1079b340c7d6SSakari Ailus acpi_evaluate_integer(device->handle, "_DSC", NULL, &dsc); 1080b340c7d6SSakari Ailus device->power.state_for_enumeration = dsc; 1081b340c7d6SSakari Ailus 10821da177e4SLinus Torvalds /* 10839e89dde2SZhang Rui * Enumerate supported power management states 10841da177e4SLinus Torvalds */ 1085f33ce568SRafael J. Wysocki for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) 1086f33ce568SRafael J. Wysocki acpi_bus_init_power_state(device, i); 10871da177e4SLinus Torvalds 10880b224527SRafael J. Wysocki INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources); 10899e89dde2SZhang Rui 1090956ad9d9SRafael J. Wysocki /* Set the defaults for D0 and D3hot (always supported). */ 10919e89dde2SZhang Rui device->power.states[ACPI_STATE_D0].flags.valid = 1; 10929e89dde2SZhang Rui device->power.states[ACPI_STATE_D0].power = 100; 109320dacb71SRafael J. Wysocki device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1; 10941399dfcdSAaron Lu 1095956ad9d9SRafael J. Wysocki /* 1096956ad9d9SRafael J. Wysocki * Use power resources only if the D0 list of them is populated, because 1097956ad9d9SRafael J. Wysocki * some platforms may provide _PR3 only to indicate D3cold support and 1098956ad9d9SRafael J. Wysocki * in those cases the power resources list returned by it may be bogus. 1099956ad9d9SRafael J. Wysocki */ 1100956ad9d9SRafael J. Wysocki if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) { 1101956ad9d9SRafael J. Wysocki device->power.flags.power_resources = 1; 1102956ad9d9SRafael J. Wysocki /* 1103956ad9d9SRafael J. Wysocki * D3cold is supported if the D3hot list of power resources is 1104956ad9d9SRafael J. Wysocki * not empty. 1105956ad9d9SRafael J. Wysocki */ 1106956ad9d9SRafael J. Wysocki if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources)) 1107956ad9d9SRafael J. Wysocki device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1; 1108956ad9d9SRafael J. Wysocki } 1109956ad9d9SRafael J. Wysocki 11101b1f3e16SRafael J. Wysocki if (acpi_bus_init_power(device)) 1111b3785492SRafael J. Wysocki device->flags.power_manageable = 0; 1112b3785492SRafael J. Wysocki } 1113c8f7a62cSLen Brown 1114d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device) 11151da177e4SLinus Torvalds { 11161da177e4SLinus Torvalds /* Presence of _STA indicates 'dynamic_status' */ 1117952c63e9SJiang Liu if (acpi_has_method(device->handle, "_STA")) 11181da177e4SLinus Torvalds device->flags.dynamic_status = 1; 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds /* Presence of _RMV indicates 'removable' */ 1121952c63e9SJiang Liu if (acpi_has_method(device->handle, "_RMV")) 11221da177e4SLinus Torvalds device->flags.removable = 1; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds /* Presence of _EJD|_EJ0 indicates 'ejectable' */ 1125952c63e9SJiang Liu if (acpi_has_method(device->handle, "_EJD") || 1126952c63e9SJiang Liu acpi_has_method(device->handle, "_EJ0")) 11271da177e4SLinus Torvalds device->flags.ejectable = 1; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 1130c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device) 11311da177e4SLinus Torvalds { 11321da177e4SLinus Torvalds char bus_id[5] = { '?', 0 }; 11331da177e4SLinus Torvalds struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; 11341da177e4SLinus Torvalds int i = 0; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds /* 11371da177e4SLinus Torvalds * Bus ID 11381da177e4SLinus Torvalds * ------ 11391da177e4SLinus Torvalds * The device's Bus ID is simply the object name. 11401da177e4SLinus Torvalds * TBD: Shouldn't this value be unique (within the ACPI namespace)? 11411da177e4SLinus Torvalds */ 1142859ac9a4SBjorn Helgaas if (ACPI_IS_ROOT_DEVICE(device)) { 11431da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "ACPI"); 1144859ac9a4SBjorn Helgaas return; 1145859ac9a4SBjorn Helgaas } 1146859ac9a4SBjorn Helgaas 1147859ac9a4SBjorn Helgaas switch (device->device_type) { 11481da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 11491da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "PWRF"); 11501da177e4SLinus Torvalds break; 11511da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 11521da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "SLPF"); 11531da177e4SLinus Torvalds break; 1154a64a62ceSLv Zheng case ACPI_BUS_TYPE_ECDT_EC: 1155a64a62ceSLv Zheng strcpy(device->pnp.bus_id, "ECDT"); 1156a64a62ceSLv Zheng break; 11571da177e4SLinus Torvalds default: 115866b7ed40SBjorn Helgaas acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer); 11591da177e4SLinus Torvalds /* Clean up trailing underscores (if any) */ 11601da177e4SLinus Torvalds for (i = 3; i > 1; i--) { 11611da177e4SLinus Torvalds if (bus_id[i] == '_') 11621da177e4SLinus Torvalds bus_id[i] = '\0'; 11631da177e4SLinus Torvalds else 11641da177e4SLinus Torvalds break; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds strcpy(device->pnp.bus_id, bus_id); 11671da177e4SLinus Torvalds break; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 117154735266SZhang Rui /* 1172ebf4df8dSJiang Liu * acpi_ata_match - see if an acpi object is an ATA device 1173ebf4df8dSJiang Liu * 1174ebf4df8dSJiang Liu * If an acpi object has one of the ACPI ATA methods defined, 1175ebf4df8dSJiang Liu * then we can safely call it an ATA device. 1176ebf4df8dSJiang Liu */ 1177ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle) 1178ebf4df8dSJiang Liu { 1179ebf4df8dSJiang Liu return acpi_has_method(handle, "_GTF") || 1180ebf4df8dSJiang Liu acpi_has_method(handle, "_GTM") || 1181ebf4df8dSJiang Liu acpi_has_method(handle, "_STM") || 1182ebf4df8dSJiang Liu acpi_has_method(handle, "_SDD"); 1183ebf4df8dSJiang Liu } 1184ebf4df8dSJiang Liu 1185ebf4df8dSJiang Liu /* 1186d4e1a692SToshi Kani * acpi_bay_match - see if an acpi object is an ejectable driver bay 118754735266SZhang Rui * 118854735266SZhang Rui * If an acpi object is ejectable and has one of the ACPI ATA methods defined, 118954735266SZhang Rui * then we can safely call it an ejectable drive bay 119054735266SZhang Rui */ 1191ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle) 1192d4e1a692SToshi Kani { 119354735266SZhang Rui acpi_handle phandle; 119454735266SZhang Rui 1195952c63e9SJiang Liu if (!acpi_has_method(handle, "_EJ0")) 1196ebf4df8dSJiang Liu return false; 1197ebf4df8dSJiang Liu if (acpi_ata_match(handle)) 1198ebf4df8dSJiang Liu return true; 1199ebf4df8dSJiang Liu if (ACPI_FAILURE(acpi_get_parent(handle, &phandle))) 1200ebf4df8dSJiang Liu return false; 120154735266SZhang Rui 1202ebf4df8dSJiang Liu return acpi_ata_match(phandle); 120354735266SZhang Rui } 120454735266SZhang Rui 1205b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev) 12061e2380cdSRafael J. Wysocki { 1207b43109faSRafael J. Wysocki struct acpi_hardware_id *hwid; 12081e2380cdSRafael J. Wysocki 1209b43109faSRafael J. Wysocki list_for_each_entry(hwid, &adev->pnp.ids, list) 1210b43109faSRafael J. Wysocki if (!strcmp("PNP0C0A", hwid->id)) 1211b43109faSRafael J. Wysocki return true; 1212b43109faSRafael J. Wysocki 12131e2380cdSRafael J. Wysocki return false; 12141e2380cdSRafael J. Wysocki } 12151e2380cdSRafael J. Wysocki 1216b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev) 12171e2380cdSRafael J. Wysocki { 1218b43109faSRafael J. Wysocki acpi_handle handle = adev->handle; 1219b43109faSRafael J. Wysocki 1220b43109faSRafael J. Wysocki if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev)) 12211e2380cdSRafael J. Wysocki return true; 12221e2380cdSRafael J. Wysocki 12231e2380cdSRafael J. Wysocki return acpi_bay_match(handle); 12241e2380cdSRafael J. Wysocki } 12251e2380cdSRafael J. Wysocki 12263620f2f2SFrank Seidel /* 1227d4e1a692SToshi Kani * acpi_dock_match - see if an acpi object has a _DCK method 12283620f2f2SFrank Seidel */ 1229ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle) 12303620f2f2SFrank Seidel { 1231ebf4df8dSJiang Liu return acpi_has_method(handle, "_DCK"); 12323620f2f2SFrank Seidel } 12333620f2f2SFrank Seidel 1234adc8bb8eSHans de Goede static acpi_status 1235adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context, 1236adc8bb8eSHans de Goede void **return_value) 1237adc8bb8eSHans de Goede { 1238adc8bb8eSHans de Goede long *cap = context; 1239adc8bb8eSHans de Goede 1240adc8bb8eSHans de Goede if (acpi_has_method(handle, "_BCM") && 1241adc8bb8eSHans de Goede acpi_has_method(handle, "_BCL")) { 1242e52d9d8cSRafael J. Wysocki acpi_handle_debug(handle, "Found generic backlight support\n"); 1243adc8bb8eSHans de Goede *cap |= ACPI_VIDEO_BACKLIGHT; 1244adc8bb8eSHans de Goede /* We have backlight support, no need to scan further */ 1245adc8bb8eSHans de Goede return AE_CTRL_TERMINATE; 1246adc8bb8eSHans de Goede } 1247adc8bb8eSHans de Goede return 0; 1248adc8bb8eSHans de Goede } 1249adc8bb8eSHans de Goede 1250adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be 1251adc8bb8eSHans de Goede * handled by video.ko. 1252adc8bb8eSHans de Goede * The device will get a Linux specific CID added in scan.c to 1253adc8bb8eSHans de Goede * identify the device as an ACPI graphics device 1254adc8bb8eSHans de Goede * Be aware that the graphics device may not be physically present 1255adc8bb8eSHans de Goede * Use acpi_video_get_capabilities() to detect general ACPI video 1256adc8bb8eSHans de Goede * capabilities of present cards 1257adc8bb8eSHans de Goede */ 1258adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle) 1259adc8bb8eSHans de Goede { 1260adc8bb8eSHans de Goede long video_caps = 0; 1261adc8bb8eSHans de Goede 1262adc8bb8eSHans de Goede /* Is this device able to support video switching ? */ 1263adc8bb8eSHans de Goede if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS")) 1264adc8bb8eSHans de Goede video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING; 1265adc8bb8eSHans de Goede 1266adc8bb8eSHans de Goede /* Is this device able to retrieve a video ROM ? */ 1267adc8bb8eSHans de Goede if (acpi_has_method(handle, "_ROM")) 1268adc8bb8eSHans de Goede video_caps |= ACPI_VIDEO_ROM_AVAILABLE; 1269adc8bb8eSHans de Goede 1270adc8bb8eSHans de Goede /* Is this device able to configure which video head to be POSTed ? */ 1271adc8bb8eSHans de Goede if (acpi_has_method(handle, "_VPO") && 1272adc8bb8eSHans de Goede acpi_has_method(handle, "_GPD") && 1273adc8bb8eSHans de Goede acpi_has_method(handle, "_SPD")) 1274adc8bb8eSHans de Goede video_caps |= ACPI_VIDEO_DEVICE_POSTING; 1275adc8bb8eSHans de Goede 1276adc8bb8eSHans de Goede /* Only check for backlight functionality if one of the above hit. */ 1277adc8bb8eSHans de Goede if (video_caps) 1278adc8bb8eSHans de Goede acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1279adc8bb8eSHans de Goede ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL, 1280adc8bb8eSHans de Goede &video_caps, NULL); 1281adc8bb8eSHans de Goede 1282adc8bb8eSHans de Goede return video_caps; 1283adc8bb8eSHans de Goede } 1284adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device); 1285adc8bb8eSHans de Goede 1286620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device) 128715b8dd53SBob Moore { 12887f47fa6cSBjorn Helgaas struct acpi_hardware_id *hid; 128915b8dd53SBob Moore 12902b2ae7c7SThomas Renninger if (list_empty(&device->pnp.ids)) 12912b2ae7c7SThomas Renninger return dummy_hid; 12922b2ae7c7SThomas Renninger 12937f47fa6cSBjorn Helgaas hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list); 12947f47fa6cSBjorn Helgaas return hid->id; 12957f47fa6cSBjorn Helgaas } 12967f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid); 129715b8dd53SBob Moore 1298d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id) 12997f47fa6cSBjorn Helgaas { 13007f47fa6cSBjorn Helgaas struct acpi_hardware_id *id; 130115b8dd53SBob Moore 13027f47fa6cSBjorn Helgaas id = kmalloc(sizeof(*id), GFP_KERNEL); 13037f47fa6cSBjorn Helgaas if (!id) 13047f47fa6cSBjorn Helgaas return; 130515b8dd53SBob Moore 13066a0d12efSRasmus Villemoes id->id = kstrdup_const(dev_id, GFP_KERNEL); 13077f47fa6cSBjorn Helgaas if (!id->id) { 13087f47fa6cSBjorn Helgaas kfree(id); 13097f47fa6cSBjorn Helgaas return; 131015b8dd53SBob Moore } 131115b8dd53SBob Moore 1312d4e1a692SToshi Kani list_add_tail(&id->list, &pnp->ids); 1313d4e1a692SToshi Kani pnp->type.hardware_id = 1; 131415b8dd53SBob Moore } 131515b8dd53SBob Moore 1316222e82acSDarrick J. Wong /* 1317222e82acSDarrick J. Wong * Old IBM workstations have a DSDT bug wherein the SMBus object 1318222e82acSDarrick J. Wong * lacks the SMBUS01 HID and the methods do not have the necessary "_" 1319222e82acSDarrick J. Wong * prefix. Work around this. 1320222e82acSDarrick J. Wong */ 1321ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle) 1322222e82acSDarrick J. Wong { 1323ebf4df8dSJiang Liu char node_name[ACPI_PATH_SEGMENT_LENGTH]; 1324ebf4df8dSJiang Liu struct acpi_buffer path = { sizeof(node_name), node_name }; 1325222e82acSDarrick J. Wong 1326222e82acSDarrick J. Wong if (!dmi_name_in_vendors("IBM")) 1327ebf4df8dSJiang Liu return false; 1328222e82acSDarrick J. Wong 1329222e82acSDarrick J. Wong /* Look for SMBS object */ 1330ebf4df8dSJiang Liu if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) || 1331ebf4df8dSJiang Liu strcmp("SMBS", path.pointer)) 1332ebf4df8dSJiang Liu return false; 1333222e82acSDarrick J. Wong 1334222e82acSDarrick J. Wong /* Does it have the necessary (but misnamed) methods? */ 1335952c63e9SJiang Liu if (acpi_has_method(handle, "SBI") && 1336952c63e9SJiang Liu acpi_has_method(handle, "SBR") && 1337952c63e9SJiang Liu acpi_has_method(handle, "SBW")) 1338ebf4df8dSJiang Liu return true; 1339ebf4df8dSJiang Liu 1340ebf4df8dSJiang Liu return false; 1341222e82acSDarrick J. Wong } 1342222e82acSDarrick J. Wong 1343ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle) 1344ae3caa80SJiang Liu { 1345ae3caa80SJiang Liu acpi_handle tmp; 1346ae3caa80SJiang Liu 1347ae3caa80SJiang Liu if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) && 1348ae3caa80SJiang Liu tmp == handle) 1349ae3caa80SJiang Liu return true; 1350ae3caa80SJiang Liu if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) && 1351ae3caa80SJiang Liu tmp == handle) 1352ae3caa80SJiang Liu return true; 1353ae3caa80SJiang Liu 1354ae3caa80SJiang Liu return false; 1355ae3caa80SJiang Liu } 1356ae3caa80SJiang Liu 1357c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp, 1358c830dbcfSRafael J. Wysocki int device_type) 13591da177e4SLinus Torvalds { 1360c830dbcfSRafael J. Wysocki struct acpi_device_info *info = NULL; 136178e25fefSLv Zheng struct acpi_pnp_device_id_list *cid_list; 13627f47fa6cSBjorn Helgaas int i; 13631da177e4SLinus Torvalds 1364c0af4175SToshi Kani switch (device_type) { 13651da177e4SLinus Torvalds case ACPI_BUS_TYPE_DEVICE: 1366c0af4175SToshi Kani if (handle == ACPI_ROOT_OBJECT) { 1367c0af4175SToshi Kani acpi_add_id(pnp, ACPI_SYSTEM_HID); 1368859ac9a4SBjorn Helgaas break; 1369859ac9a4SBjorn Helgaas } 1370859ac9a4SBjorn Helgaas 1371c830dbcfSRafael J. Wysocki acpi_get_object_info(handle, &info); 13726091b263SHans de Goede if (!info) { 13738acf4108SHanjun Guo pr_err("%s: Error reading device info\n", __func__); 13741da177e4SLinus Torvalds return; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 1377549e6845SRafael J. Wysocki if (info->valid & ACPI_VALID_HID) { 1378c0af4175SToshi Kani acpi_add_id(pnp, info->hardware_id.string); 1379549e6845SRafael J. Wysocki pnp->type.platform_id = 1; 138057f3674fSBjorn Helgaas if (info->valid & ACPI_VALID_CID) { 138115b8dd53SBob Moore cid_list = &info->compatible_id_list; 138257f3674fSBjorn Helgaas for (i = 0; i < cid_list->count; i++) 1383c0af4175SToshi Kani acpi_add_id(pnp, cid_list->ids[i].string); 138457f3674fSBjorn Helgaas } 1385e38f9ff6SRafael J. Wysocki } 13861da177e4SLinus Torvalds if (info->valid & ACPI_VALID_ADR) { 1387c0af4175SToshi Kani pnp->bus_address = info->address; 1388c0af4175SToshi Kani pnp->type.bus_address = 1; 13891da177e4SLinus Torvalds } 1390ccf78040SLv Zheng if (info->valid & ACPI_VALID_UID) 1391c0af4175SToshi Kani pnp->unique_id = kstrdup(info->unique_id.string, 1392ccf78040SLv Zheng GFP_KERNEL); 139326095a01SSuthikulpanit, Suravee if (info->valid & ACPI_VALID_CLS) 139426095a01SSuthikulpanit, Suravee acpi_add_id(pnp, info->class_code.string); 1395ae843332SZhang Rui 1396c830dbcfSRafael J. Wysocki kfree(info); 1397c830dbcfSRafael J. Wysocki 139857f3674fSBjorn Helgaas /* 139957f3674fSBjorn Helgaas * Some devices don't reliably have _HIDs & _CIDs, so add 140057f3674fSBjorn Helgaas * synthetic HIDs to make sure drivers can find them. 14013620f2f2SFrank Seidel */ 1402c0af4175SToshi Kani if (acpi_is_video_device(handle)) 1403c0af4175SToshi Kani acpi_add_id(pnp, ACPI_VIDEO_HID); 1404ebf4df8dSJiang Liu else if (acpi_bay_match(handle)) 1405c0af4175SToshi Kani acpi_add_id(pnp, ACPI_BAY_HID); 1406ebf4df8dSJiang Liu else if (acpi_dock_match(handle)) 1407c0af4175SToshi Kani acpi_add_id(pnp, ACPI_DOCK_HID); 1408ebf4df8dSJiang Liu else if (acpi_ibm_smbus_match(handle)) 1409c0af4175SToshi Kani acpi_add_id(pnp, ACPI_SMBUS_IBM_HID); 1410ae3caa80SJiang Liu else if (list_empty(&pnp->ids) && 1411ae3caa80SJiang Liu acpi_object_is_system_bus(handle)) { 1412ae3caa80SJiang Liu /* \_SB, \_TZ, LNXSYBUS */ 1413ae3caa80SJiang Liu acpi_add_id(pnp, ACPI_BUS_HID); 1414c0af4175SToshi Kani strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME); 1415c0af4175SToshi Kani strcpy(pnp->device_class, ACPI_BUS_CLASS); 1416b7b30de5SBjorn Helgaas } 141754735266SZhang Rui 14181da177e4SLinus Torvalds break; 14191da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER: 1420c0af4175SToshi Kani acpi_add_id(pnp, ACPI_POWER_HID); 14211da177e4SLinus Torvalds break; 14221da177e4SLinus Torvalds case ACPI_BUS_TYPE_PROCESSOR: 1423c0af4175SToshi Kani acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID); 14241da177e4SLinus Torvalds break; 14251da177e4SLinus Torvalds case ACPI_BUS_TYPE_THERMAL: 1426c0af4175SToshi Kani acpi_add_id(pnp, ACPI_THERMAL_HID); 14271da177e4SLinus Torvalds break; 14281da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 1429c0af4175SToshi Kani acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF); 14301da177e4SLinus Torvalds break; 14311da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 1432c0af4175SToshi Kani acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF); 14331da177e4SLinus Torvalds break; 1434a64a62ceSLv Zheng case ACPI_BUS_TYPE_ECDT_EC: 1435a64a62ceSLv Zheng acpi_add_id(pnp, ACPI_ECDT_HID); 1436a64a62ceSLv Zheng break; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds 1440c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp) 1441c0af4175SToshi Kani { 1442c0af4175SToshi Kani struct acpi_hardware_id *id, *tmp; 1443c0af4175SToshi Kani 1444c0af4175SToshi Kani list_for_each_entry_safe(id, tmp, &pnp->ids, list) { 14456a0d12efSRasmus Villemoes kfree_const(id->id); 1446c0af4175SToshi Kani kfree(id); 1447c0af4175SToshi Kani } 1448c0af4175SToshi Kani kfree(pnp->unique_id); 1449c0af4175SToshi Kani } 1450c0af4175SToshi Kani 1451b84f196dSSuthikulpanit, Suravee /** 1452b84f196dSSuthikulpanit, Suravee * acpi_dma_supported - Check DMA support for the specified device. 1453b84f196dSSuthikulpanit, Suravee * @adev: The pointer to acpi device 1454b84f196dSSuthikulpanit, Suravee * 1455b84f196dSSuthikulpanit, Suravee * Return false if DMA is not supported. Otherwise, return true 1456b84f196dSSuthikulpanit, Suravee */ 14573d7c821cSAndy Shevchenko bool acpi_dma_supported(const struct acpi_device *adev) 1458b84f196dSSuthikulpanit, Suravee { 1459b84f196dSSuthikulpanit, Suravee if (!adev) 1460b84f196dSSuthikulpanit, Suravee return false; 1461b84f196dSSuthikulpanit, Suravee 1462b84f196dSSuthikulpanit, Suravee if (adev->flags.cca_seen) 1463b84f196dSSuthikulpanit, Suravee return true; 1464b84f196dSSuthikulpanit, Suravee 1465b84f196dSSuthikulpanit, Suravee /* 1466b84f196dSSuthikulpanit, Suravee * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent 1467b84f196dSSuthikulpanit, Suravee * DMA on "Intel platforms". Presumably that includes all x86 and 1468b84f196dSSuthikulpanit, Suravee * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y. 1469b84f196dSSuthikulpanit, Suravee */ 1470b84f196dSSuthikulpanit, Suravee if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED)) 1471b84f196dSSuthikulpanit, Suravee return true; 1472b84f196dSSuthikulpanit, Suravee 1473b84f196dSSuthikulpanit, Suravee return false; 1474b84f196dSSuthikulpanit, Suravee } 1475b84f196dSSuthikulpanit, Suravee 1476b84f196dSSuthikulpanit, Suravee /** 1477b84f196dSSuthikulpanit, Suravee * acpi_get_dma_attr - Check the supported DMA attr for the specified device. 1478b84f196dSSuthikulpanit, Suravee * @adev: The pointer to acpi device 1479b84f196dSSuthikulpanit, Suravee * 1480b84f196dSSuthikulpanit, Suravee * Return enum dev_dma_attr. 1481b84f196dSSuthikulpanit, Suravee */ 1482b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev) 1483b84f196dSSuthikulpanit, Suravee { 1484b84f196dSSuthikulpanit, Suravee if (!acpi_dma_supported(adev)) 1485b84f196dSSuthikulpanit, Suravee return DEV_DMA_NOT_SUPPORTED; 1486b84f196dSSuthikulpanit, Suravee 1487b84f196dSSuthikulpanit, Suravee if (adev->flags.coherent_dma) 1488b84f196dSSuthikulpanit, Suravee return DEV_DMA_COHERENT; 1489b84f196dSSuthikulpanit, Suravee else 1490b84f196dSSuthikulpanit, Suravee return DEV_DMA_NON_COHERENT; 1491b84f196dSSuthikulpanit, Suravee } 1492b84f196dSSuthikulpanit, Suravee 1493d760a1baSLorenzo Pieralisi /** 1494c04ac679SLorenzo Pieralisi * acpi_dma_get_range() - Get device DMA parameters. 1495c04ac679SLorenzo Pieralisi * 1496c04ac679SLorenzo Pieralisi * @dev: device to configure 1497c04ac679SLorenzo Pieralisi * @dma_addr: pointer device DMA address result 1498c04ac679SLorenzo Pieralisi * @offset: pointer to the DMA offset result 1499c04ac679SLorenzo Pieralisi * @size: pointer to DMA range size result 1500c04ac679SLorenzo Pieralisi * 1501c04ac679SLorenzo Pieralisi * Evaluate DMA regions and return respectively DMA region start, offset 1502c04ac679SLorenzo Pieralisi * and size in dma_addr, offset and size on parsing success; it does not 1503c04ac679SLorenzo Pieralisi * update the passed in values on failure. 1504c04ac679SLorenzo Pieralisi * 1505c04ac679SLorenzo Pieralisi * Return 0 on success, < 0 on failure. 1506c04ac679SLorenzo Pieralisi */ 1507c04ac679SLorenzo Pieralisi int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset, 1508c04ac679SLorenzo Pieralisi u64 *size) 1509c04ac679SLorenzo Pieralisi { 1510c04ac679SLorenzo Pieralisi struct acpi_device *adev; 1511c04ac679SLorenzo Pieralisi LIST_HEAD(list); 1512c04ac679SLorenzo Pieralisi struct resource_entry *rentry; 1513c04ac679SLorenzo Pieralisi int ret; 1514c04ac679SLorenzo Pieralisi struct device *dma_dev = dev; 1515c04ac679SLorenzo Pieralisi u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0; 1516c04ac679SLorenzo Pieralisi 1517c04ac679SLorenzo Pieralisi /* 1518c04ac679SLorenzo Pieralisi * Walk the device tree chasing an ACPI companion with a _DMA 1519c04ac679SLorenzo Pieralisi * object while we go. Stop if we find a device with an ACPI 1520c04ac679SLorenzo Pieralisi * companion containing a _DMA method. 1521c04ac679SLorenzo Pieralisi */ 1522c04ac679SLorenzo Pieralisi do { 1523c04ac679SLorenzo Pieralisi adev = ACPI_COMPANION(dma_dev); 1524c04ac679SLorenzo Pieralisi if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA)) 1525c04ac679SLorenzo Pieralisi break; 1526c04ac679SLorenzo Pieralisi 1527c04ac679SLorenzo Pieralisi dma_dev = dma_dev->parent; 1528c04ac679SLorenzo Pieralisi } while (dma_dev); 1529c04ac679SLorenzo Pieralisi 1530c04ac679SLorenzo Pieralisi if (!dma_dev) 1531c04ac679SLorenzo Pieralisi return -ENODEV; 1532c04ac679SLorenzo Pieralisi 1533c04ac679SLorenzo Pieralisi if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) { 1534c04ac679SLorenzo Pieralisi acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n"); 1535c04ac679SLorenzo Pieralisi return -EINVAL; 1536c04ac679SLorenzo Pieralisi } 1537c04ac679SLorenzo Pieralisi 1538c04ac679SLorenzo Pieralisi ret = acpi_dev_get_dma_resources(adev, &list); 1539c04ac679SLorenzo Pieralisi if (ret > 0) { 1540c04ac679SLorenzo Pieralisi list_for_each_entry(rentry, &list, node) { 1541c04ac679SLorenzo Pieralisi if (dma_offset && rentry->offset != dma_offset) { 1542c04ac679SLorenzo Pieralisi ret = -EINVAL; 1543c04ac679SLorenzo Pieralisi dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n"); 1544c04ac679SLorenzo Pieralisi goto out; 1545c04ac679SLorenzo Pieralisi } 1546c04ac679SLorenzo Pieralisi dma_offset = rentry->offset; 1547c04ac679SLorenzo Pieralisi 1548c04ac679SLorenzo Pieralisi /* Take lower and upper limits */ 1549c04ac679SLorenzo Pieralisi if (rentry->res->start < dma_start) 1550c04ac679SLorenzo Pieralisi dma_start = rentry->res->start; 1551c04ac679SLorenzo Pieralisi if (rentry->res->end > dma_end) 1552c04ac679SLorenzo Pieralisi dma_end = rentry->res->end; 1553c04ac679SLorenzo Pieralisi } 1554c04ac679SLorenzo Pieralisi 1555c04ac679SLorenzo Pieralisi if (dma_start >= dma_end) { 1556c04ac679SLorenzo Pieralisi ret = -EINVAL; 1557c04ac679SLorenzo Pieralisi dev_dbg(dma_dev, "Invalid DMA regions configuration\n"); 1558c04ac679SLorenzo Pieralisi goto out; 1559c04ac679SLorenzo Pieralisi } 1560c04ac679SLorenzo Pieralisi 1561c04ac679SLorenzo Pieralisi *dma_addr = dma_start - dma_offset; 1562c04ac679SLorenzo Pieralisi len = dma_end - dma_start; 1563c04ac679SLorenzo Pieralisi *size = max(len, len + 1); 1564c04ac679SLorenzo Pieralisi *offset = dma_offset; 1565c04ac679SLorenzo Pieralisi } 1566c04ac679SLorenzo Pieralisi out: 1567c04ac679SLorenzo Pieralisi acpi_dev_free_resource_list(&list); 1568c04ac679SLorenzo Pieralisi 1569c04ac679SLorenzo Pieralisi return ret >= 0 ? 0 : ret; 1570c04ac679SLorenzo Pieralisi } 1571c04ac679SLorenzo Pieralisi 157211a8c5e3SJean-Philippe Brucker #ifdef CONFIG_IOMMU_API 157311a8c5e3SJean-Philippe Brucker int acpi_iommu_fwspec_init(struct device *dev, u32 id, 157411a8c5e3SJean-Philippe Brucker struct fwnode_handle *fwnode, 157511a8c5e3SJean-Philippe Brucker const struct iommu_ops *ops) 157611a8c5e3SJean-Philippe Brucker { 157711a8c5e3SJean-Philippe Brucker int ret = iommu_fwspec_init(dev, fwnode, ops); 157811a8c5e3SJean-Philippe Brucker 157911a8c5e3SJean-Philippe Brucker if (!ret) 158011a8c5e3SJean-Philippe Brucker ret = iommu_fwspec_add_ids(dev, &id, 1); 158111a8c5e3SJean-Philippe Brucker 158211a8c5e3SJean-Philippe Brucker return ret; 158311a8c5e3SJean-Philippe Brucker } 158411a8c5e3SJean-Philippe Brucker 158511a8c5e3SJean-Philippe Brucker static inline const struct iommu_ops *acpi_iommu_fwspec_ops(struct device *dev) 158611a8c5e3SJean-Philippe Brucker { 158711a8c5e3SJean-Philippe Brucker struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 158811a8c5e3SJean-Philippe Brucker 158911a8c5e3SJean-Philippe Brucker return fwspec ? fwspec->ops : NULL; 159011a8c5e3SJean-Philippe Brucker } 159111a8c5e3SJean-Philippe Brucker 159211a8c5e3SJean-Philippe Brucker static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev, 159311a8c5e3SJean-Philippe Brucker const u32 *id_in) 159411a8c5e3SJean-Philippe Brucker { 159511a8c5e3SJean-Philippe Brucker int err; 159611a8c5e3SJean-Philippe Brucker const struct iommu_ops *ops; 159711a8c5e3SJean-Philippe Brucker 159811a8c5e3SJean-Philippe Brucker /* 159911a8c5e3SJean-Philippe Brucker * If we already translated the fwspec there is nothing left to do, 160011a8c5e3SJean-Philippe Brucker * return the iommu_ops. 160111a8c5e3SJean-Philippe Brucker */ 160211a8c5e3SJean-Philippe Brucker ops = acpi_iommu_fwspec_ops(dev); 160311a8c5e3SJean-Philippe Brucker if (ops) 160411a8c5e3SJean-Philippe Brucker return ops; 160511a8c5e3SJean-Philippe Brucker 160611a8c5e3SJean-Philippe Brucker err = iort_iommu_configure_id(dev, id_in); 16073cf48554SJean-Philippe Brucker if (err && err != -EPROBE_DEFER) 16083cf48554SJean-Philippe Brucker err = viot_iommu_configure(dev); 160911a8c5e3SJean-Philippe Brucker 161011a8c5e3SJean-Philippe Brucker /* 161111a8c5e3SJean-Philippe Brucker * If we have reason to believe the IOMMU driver missed the initial 161211a8c5e3SJean-Philippe Brucker * iommu_probe_device() call for dev, replay it to get things in order. 161311a8c5e3SJean-Philippe Brucker */ 161411a8c5e3SJean-Philippe Brucker if (!err && dev->bus && !device_iommu_mapped(dev)) 161511a8c5e3SJean-Philippe Brucker err = iommu_probe_device(dev); 161611a8c5e3SJean-Philippe Brucker 161711a8c5e3SJean-Philippe Brucker /* Ignore all other errors apart from EPROBE_DEFER */ 161811a8c5e3SJean-Philippe Brucker if (err == -EPROBE_DEFER) { 161911a8c5e3SJean-Philippe Brucker return ERR_PTR(err); 162011a8c5e3SJean-Philippe Brucker } else if (err) { 162111a8c5e3SJean-Philippe Brucker dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); 162211a8c5e3SJean-Philippe Brucker return NULL; 162311a8c5e3SJean-Philippe Brucker } 162411a8c5e3SJean-Philippe Brucker return acpi_iommu_fwspec_ops(dev); 162511a8c5e3SJean-Philippe Brucker } 162611a8c5e3SJean-Philippe Brucker 162711a8c5e3SJean-Philippe Brucker #else /* !CONFIG_IOMMU_API */ 162811a8c5e3SJean-Philippe Brucker 162911a8c5e3SJean-Philippe Brucker int acpi_iommu_fwspec_init(struct device *dev, u32 id, 163011a8c5e3SJean-Philippe Brucker struct fwnode_handle *fwnode, 163111a8c5e3SJean-Philippe Brucker const struct iommu_ops *ops) 163211a8c5e3SJean-Philippe Brucker { 163311a8c5e3SJean-Philippe Brucker return -ENODEV; 163411a8c5e3SJean-Philippe Brucker } 163511a8c5e3SJean-Philippe Brucker 163611a8c5e3SJean-Philippe Brucker static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev, 163711a8c5e3SJean-Philippe Brucker const u32 *id_in) 163811a8c5e3SJean-Philippe Brucker { 163911a8c5e3SJean-Philippe Brucker return NULL; 164011a8c5e3SJean-Philippe Brucker } 164111a8c5e3SJean-Philippe Brucker 164211a8c5e3SJean-Philippe Brucker #endif /* !CONFIG_IOMMU_API */ 164311a8c5e3SJean-Philippe Brucker 1644c04ac679SLorenzo Pieralisi /** 1645c1e97359SJohn Garry * acpi_dma_configure_id - Set-up DMA configuration for the device. 1646d760a1baSLorenzo Pieralisi * @dev: The pointer to the device 1647d760a1baSLorenzo Pieralisi * @attr: device dma attributes 1648b8e069a2SLorenzo Pieralisi * @input_id: input device id const value pointer 1649d760a1baSLorenzo Pieralisi */ 1650b8e069a2SLorenzo Pieralisi int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr, 1651b8e069a2SLorenzo Pieralisi const u32 *input_id) 1652d760a1baSLorenzo Pieralisi { 1653643b8e4dSLorenzo Pieralisi const struct iommu_ops *iommu; 16547ad42639SLorenzo Pieralisi u64 dma_addr = 0, size = 0; 1655643b8e4dSLorenzo Pieralisi 1656e5361ca2SRobin Murphy if (attr == DEV_DMA_NOT_SUPPORTED) { 1657e5361ca2SRobin Murphy set_dma_ops(dev, &dma_dummy_ops); 1658e5361ca2SRobin Murphy return 0; 1659e5361ca2SRobin Murphy } 1660e5361ca2SRobin Murphy 1661db59e1b6SJean-Philippe Brucker acpi_arch_dma_setup(dev, &dma_addr, &size); 1662d760a1baSLorenzo Pieralisi 166311a8c5e3SJean-Philippe Brucker iommu = acpi_iommu_configure_id(dev, input_id); 166445586c70SMasahiro Yamada if (PTR_ERR(iommu) == -EPROBE_DEFER) 1665058f8c3fSSricharan R return -EPROBE_DEFER; 1666643b8e4dSLorenzo Pieralisi 16677ad42639SLorenzo Pieralisi arch_setup_dma_ops(dev, dma_addr, size, 16687ad42639SLorenzo Pieralisi iommu, attr == DEV_DMA_COHERENT); 16695a1bb638SSricharan R 16705a1bb638SSricharan R return 0; 1671d760a1baSLorenzo Pieralisi } 1672b8e069a2SLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_configure_id); 1673d760a1baSLorenzo Pieralisi 1674d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev) 1675d0562674SSuthikulpanit, Suravee { 1676d0562674SSuthikulpanit, Suravee unsigned long long cca = 0; 1677d0562674SSuthikulpanit, Suravee acpi_status status; 1678d0562674SSuthikulpanit, Suravee struct acpi_device *parent = adev->parent; 1679d0562674SSuthikulpanit, Suravee 1680d0562674SSuthikulpanit, Suravee if (parent && parent->flags.cca_seen) { 1681d0562674SSuthikulpanit, Suravee /* 1682d0562674SSuthikulpanit, Suravee * From ACPI spec, OSPM will ignore _CCA if an ancestor 1683d0562674SSuthikulpanit, Suravee * already saw one. 1684d0562674SSuthikulpanit, Suravee */ 1685d0562674SSuthikulpanit, Suravee adev->flags.cca_seen = 1; 1686d0562674SSuthikulpanit, Suravee cca = parent->flags.coherent_dma; 1687d0562674SSuthikulpanit, Suravee } else { 1688d0562674SSuthikulpanit, Suravee status = acpi_evaluate_integer(adev->handle, "_CCA", 1689d0562674SSuthikulpanit, Suravee NULL, &cca); 1690d0562674SSuthikulpanit, Suravee if (ACPI_SUCCESS(status)) 1691d0562674SSuthikulpanit, Suravee adev->flags.cca_seen = 1; 1692d0562674SSuthikulpanit, Suravee else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED)) 1693d0562674SSuthikulpanit, Suravee /* 1694d0562674SSuthikulpanit, Suravee * If architecture does not specify that _CCA is 1695d0562674SSuthikulpanit, Suravee * required for DMA-able devices (e.g. x86), 1696d0562674SSuthikulpanit, Suravee * we default to _CCA=1. 1697d0562674SSuthikulpanit, Suravee */ 1698d0562674SSuthikulpanit, Suravee cca = 1; 1699d0562674SSuthikulpanit, Suravee else 1700d0562674SSuthikulpanit, Suravee acpi_handle_debug(adev->handle, 1701d0562674SSuthikulpanit, Suravee "ACPI device is missing _CCA.\n"); 1702d0562674SSuthikulpanit, Suravee } 1703d0562674SSuthikulpanit, Suravee 1704d0562674SSuthikulpanit, Suravee adev->flags.coherent_dma = cca; 1705d0562674SSuthikulpanit, Suravee } 1706d0562674SSuthikulpanit, Suravee 1707e361d1f8SFrédéric Danis static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data) 1708e4330d8bSJarkko Nikula { 1709e361d1f8SFrédéric Danis bool *is_serial_bus_slave_p = data; 1710e4330d8bSJarkko Nikula 1711e4330d8bSJarkko Nikula if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 1712e4330d8bSJarkko Nikula return 1; 1713e4330d8bSJarkko Nikula 1714e361d1f8SFrédéric Danis *is_serial_bus_slave_p = true; 1715e4330d8bSJarkko Nikula 1716e4330d8bSJarkko Nikula /* no need to do more checking */ 1717e4330d8bSJarkko Nikula return -1; 1718e4330d8bSJarkko Nikula } 1719e4330d8bSJarkko Nikula 1720dfda4492SJohn Garry static bool acpi_is_indirect_io_slave(struct acpi_device *device) 1721dfda4492SJohn Garry { 1722dfda4492SJohn Garry struct acpi_device *parent = device->parent; 1723a6f07295SJohn Garry static const struct acpi_device_id indirect_io_hosts[] = { 1724dfda4492SJohn Garry {"HISI0191", 0}, 1725dfda4492SJohn Garry {} 1726dfda4492SJohn Garry }; 1727dfda4492SJohn Garry 1728dfda4492SJohn Garry return parent && !acpi_match_device_ids(parent, indirect_io_hosts); 1729dfda4492SJohn Garry } 1730dfda4492SJohn Garry 1731d87fb091SJohn Garry static bool acpi_device_enumeration_by_parent(struct acpi_device *device) 1732e4330d8bSJarkko Nikula { 1733e4330d8bSJarkko Nikula struct list_head resource_list; 1734e361d1f8SFrédéric Danis bool is_serial_bus_slave = false; 1735f85196bdSHans de Goede static const struct acpi_device_id ignore_serial_bus_ids[] = { 1736aba94139SHans de Goede /* 17375e63b2eaSLucas Tanure * These devices have multiple SerialBus resources and a client 17385e63b2eaSLucas Tanure * device must be instantiated for each of them, each with 17395e63b2eaSLucas Tanure * its own device id. 17405e63b2eaSLucas Tanure * Normally we only instantiate one client device for the first 17415e63b2eaSLucas Tanure * resource, using the ACPI HID as id. These special cases are handled 17425e63b2eaSLucas Tanure * by the drivers/platform/x86/serial-multi-instantiate.c driver, which 17435e63b2eaSLucas Tanure * knows which client device id to use for each resource. 1744aba94139SHans de Goede */ 1745aba94139SHans de Goede {"BSG1160", }, 174696f984d3SHans de Goede {"BSG2150", }, 1747*d9c01c53SLucas Tanure {"CSC3551", }, 1748589edb56SHans de Goede {"INT33FE", }, 1749a3dd034aSAndy Shevchenko {"INT3515", }, 1750*d9c01c53SLucas Tanure /* Non-conforming _HID for Cirrus Logic already released */ 1751*d9c01c53SLucas Tanure {"CLSA0100", }, 1752f85196bdSHans de Goede /* 1753f85196bdSHans de Goede * HIDs of device with an UartSerialBusV2 resource for which userspace 1754f85196bdSHans de Goede * expects a regular tty cdev to be created (instead of the in kernel 1755f85196bdSHans de Goede * serdev) and which have a kernel driver which expects a platform_dev 1756f85196bdSHans de Goede * such as the rfkill-gpio driver. 1757f85196bdSHans de Goede */ 1758f85196bdSHans de Goede {"BCM4752", }, 1759f85196bdSHans de Goede {"LNV4752", }, 1760aba94139SHans de Goede {} 1761aba94139SHans de Goede }; 1762e4330d8bSJarkko Nikula 1763dfda4492SJohn Garry if (acpi_is_indirect_io_slave(device)) 1764dfda4492SJohn Garry return true; 1765dfda4492SJohn Garry 1766ca9ef3abSLukas Wunner /* Macs use device properties in lieu of _CRS resources */ 1767ca9ef3abSLukas Wunner if (x86_apple_machine && 1768ca9ef3abSLukas Wunner (fwnode_property_present(&device->fwnode, "spiSclkPeriod") || 1769e361d1f8SFrédéric Danis fwnode_property_present(&device->fwnode, "i2cAddress") || 1770e361d1f8SFrédéric Danis fwnode_property_present(&device->fwnode, "baud"))) 1771ca9ef3abSLukas Wunner return true; 1772ca9ef3abSLukas Wunner 1773f85196bdSHans de Goede if (!acpi_match_device_ids(device, ignore_serial_bus_ids)) 1774aba94139SHans de Goede return false; 1775aba94139SHans de Goede 1776e4330d8bSJarkko Nikula INIT_LIST_HEAD(&resource_list); 1777e361d1f8SFrédéric Danis acpi_dev_get_resources(device, &resource_list, 1778e361d1f8SFrédéric Danis acpi_check_serial_bus_slave, 1779e361d1f8SFrédéric Danis &is_serial_bus_slave); 1780e4330d8bSJarkko Nikula acpi_dev_free_resource_list(&resource_list); 1781e4330d8bSJarkko Nikula 1782e361d1f8SFrédéric Danis return is_serial_bus_slave; 1783e4330d8bSJarkko Nikula } 1784e4330d8bSJarkko Nikula 178582c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, 1786c830dbcfSRafael J. Wysocki int type) 17871da177e4SLinus Torvalds { 1788d43e167dSRafael J. Wysocki INIT_LIST_HEAD(&device->pnp.ids); 1789d43e167dSRafael J. Wysocki device->device_type = type; 1790d43e167dSRafael J. Wysocki device->handle = handle; 1791d43e167dSRafael J. Wysocki device->parent = acpi_bus_get_parent(handle); 179201bb86b3SSaravana Kannan fwnode_init(&device->fwnode, &acpi_device_fwnode_ops); 1793f5d9ab1dSRafael J. Wysocki acpi_set_device_status(device, ACPI_STA_DEFAULT); 1794d43e167dSRafael J. Wysocki acpi_device_get_busid(device); 1795c830dbcfSRafael J. Wysocki acpi_set_pnp_ids(handle, &device->pnp, type); 1796ffdcd955SMika Westerberg acpi_init_properties(device); 1797d43e167dSRafael J. Wysocki acpi_bus_get_flags(device); 17982c0d4fe0SRafael J. Wysocki device->flags.match_driver = false; 1799202317a5SRafael J. Wysocki device->flags.initialized = true; 1800d87fb091SJohn Garry device->flags.enumeration_by_parent = 1801d87fb091SJohn Garry acpi_device_enumeration_by_parent(device); 180210c7e20bSOctavian Purdila acpi_device_clear_enumerated(device); 1803cf860be6SRafael J. Wysocki device_initialize(&device->dev); 1804cf860be6SRafael J. Wysocki dev_set_uevent_suppress(&device->dev, true); 1805d0562674SSuthikulpanit, Suravee acpi_init_coherency(device); 18066d279758SRafael J. Wysocki } 18076d279758SRafael J. Wysocki 18086d279758SRafael J. Wysocki static void acpi_scan_dep_init(struct acpi_device *adev) 18096d279758SRafael J. Wysocki { 18106d279758SRafael J. Wysocki struct acpi_dep_data *dep; 18116d279758SRafael J. Wysocki 18126d279758SRafael J. Wysocki list_for_each_entry(dep, &acpi_dep_list, node) { 18139d9bcae4SHans de Goede if (dep->consumer == adev->handle) { 18149d9bcae4SHans de Goede if (dep->honor_dep) 18159d9bcae4SHans de Goede adev->flags.honor_deps = 1; 18169d9bcae4SHans de Goede 18176d279758SRafael J. Wysocki adev->dep_unmet++; 18186d279758SRafael J. Wysocki } 1819cf860be6SRafael J. Wysocki } 18209d9bcae4SHans de Goede } 1821bc3b0772SBjorn Helgaas 1822cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device) 1823cf860be6SRafael J. Wysocki { 1824cf860be6SRafael J. Wysocki dev_set_uevent_suppress(&device->dev, false); 1825cf860be6SRafael J. Wysocki kobject_uevent(&device->dev.kobj, KOBJ_ADD); 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 1828f5d9ab1dSRafael J. Wysocki static void acpi_scan_init_status(struct acpi_device *adev) 18291da177e4SLinus Torvalds { 1830f5d9ab1dSRafael J. Wysocki if (acpi_bus_get_status(adev)) 1831f5d9ab1dSRafael J. Wysocki acpi_set_device_status(adev, 0); 1832f5d9ab1dSRafael J. Wysocki } 1833f5d9ab1dSRafael J. Wysocki 18341da177e4SLinus Torvalds static int acpi_add_single_object(struct acpi_device **child, 18356d279758SRafael J. Wysocki acpi_handle handle, int type, bool dep_init) 18361da177e4SLinus Torvalds { 1837e52d9d8cSRafael J. Wysocki struct acpi_device *device; 18385f4ce260SRafael J. Wysocki bool release_dep_lock = false; 1839e52d9d8cSRafael J. Wysocki int result; 18406091b263SHans de Goede 184136bcbec7SBurman Yan device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); 1842c830dbcfSRafael J. Wysocki if (!device) 1843d550d98dSPatrick Mochel return -ENOMEM; 18441da177e4SLinus Torvalds 1845c830dbcfSRafael J. Wysocki acpi_init_device_object(device, handle, type); 184663347db0SHans de Goede /* 1847f5d9ab1dSRafael J. Wysocki * Getting the status is delayed till here so that we can call 1848f5d9ab1dSRafael J. Wysocki * acpi_bus_get_status() and use its quirk handling. Note that 1849f5d9ab1dSRafael J. Wysocki * this must be done before the get power-/wakeup_dev-flags calls. 185063347db0SHans de Goede */ 18516d279758SRafael J. Wysocki if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) { 18525f4ce260SRafael J. Wysocki if (dep_init) { 18535f4ce260SRafael J. Wysocki mutex_lock(&acpi_dep_list_lock); 18545f4ce260SRafael J. Wysocki /* 18555f4ce260SRafael J. Wysocki * Hold the lock until the acpi_tie_acpi_dev() call 18565f4ce260SRafael J. Wysocki * below to prevent concurrent acpi_scan_clear_dep() 18575f4ce260SRafael J. Wysocki * from deleting a dependency list entry without 18585f4ce260SRafael J. Wysocki * updating dep_unmet for the device. 18595f4ce260SRafael J. Wysocki */ 18605f4ce260SRafael J. Wysocki release_dep_lock = true; 18616d279758SRafael J. Wysocki acpi_scan_dep_init(device); 18625f4ce260SRafael J. Wysocki } 1863f5d9ab1dSRafael J. Wysocki acpi_scan_init_status(device); 18646d279758SRafael J. Wysocki } 186563347db0SHans de Goede 1866d43e167dSRafael J. Wysocki acpi_bus_get_power_flags(device); 1867d57d09a4SRafael J. Wysocki acpi_bus_get_wakeup_device_flags(device); 18681da177e4SLinus Torvalds 18695f4ce260SRafael J. Wysocki result = acpi_tie_acpi_dev(device); 18705f4ce260SRafael J. Wysocki 18715f4ce260SRafael J. Wysocki if (release_dep_lock) 18725f4ce260SRafael J. Wysocki mutex_unlock(&acpi_dep_list_lock); 18735f4ce260SRafael J. Wysocki 18745f4ce260SRafael J. Wysocki if (!result) 18755f4ce260SRafael J. Wysocki result = __acpi_device_add(device, acpi_device_release); 18765f4ce260SRafael J. Wysocki 1877d43e167dSRafael J. Wysocki if (result) { 1878718fb0deSHugh Dickins acpi_device_release(&device->dev); 1879d550d98dSPatrick Mochel return result; 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds 1882bc9b6407SRafael J. Wysocki acpi_power_add_remove_device(device, true); 1883cf860be6SRafael J. Wysocki acpi_device_add_finalize(device); 1884e52d9d8cSRafael J. Wysocki 1885e52d9d8cSRafael J. Wysocki acpi_handle_debug(handle, "Added as %s, parent %s\n", 1886e52d9d8cSRafael J. Wysocki dev_name(&device->dev), device->parent ? 1887e52d9d8cSRafael J. Wysocki dev_name(&device->parent->dev) : "(null)"); 1888e52d9d8cSRafael J. Wysocki 18891da177e4SLinus Torvalds *child = device; 1890d43e167dSRafael J. Wysocki return 0; 1891bf325f95SRafael J. Wysocki } 1892bf325f95SRafael J. Wysocki 1893a4e081b0SShannon Zhao static acpi_status acpi_get_resource_memory(struct acpi_resource *ares, 1894a4e081b0SShannon Zhao void *context) 1895a4e081b0SShannon Zhao { 1896a4e081b0SShannon Zhao struct resource *res = context; 1897a4e081b0SShannon Zhao 1898a4e081b0SShannon Zhao if (acpi_dev_resource_memory(ares, res)) 1899a4e081b0SShannon Zhao return AE_CTRL_TERMINATE; 1900a4e081b0SShannon Zhao 1901a4e081b0SShannon Zhao return AE_OK; 1902a4e081b0SShannon Zhao } 1903a4e081b0SShannon Zhao 1904a4e081b0SShannon Zhao static bool acpi_device_should_be_hidden(acpi_handle handle) 1905a4e081b0SShannon Zhao { 1906a4e081b0SShannon Zhao acpi_status status; 1907a4e081b0SShannon Zhao struct resource res; 1908a4e081b0SShannon Zhao 1909a4e081b0SShannon Zhao /* Check if it should ignore the UART device */ 1910a4e081b0SShannon Zhao if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS))) 1911a4e081b0SShannon Zhao return false; 1912a4e081b0SShannon Zhao 1913a4e081b0SShannon Zhao /* 1914a4e081b0SShannon Zhao * The UART device described in SPCR table is assumed to have only one 1915a4e081b0SShannon Zhao * memory resource present. So we only look for the first one here. 1916a4e081b0SShannon Zhao */ 1917a4e081b0SShannon Zhao status = acpi_walk_resources(handle, METHOD_NAME__CRS, 1918a4e081b0SShannon Zhao acpi_get_resource_memory, &res); 1919a4e081b0SShannon Zhao if (ACPI_FAILURE(status) || res.start != spcr_uart_addr) 1920a4e081b0SShannon Zhao return false; 1921a4e081b0SShannon Zhao 1922a4e081b0SShannon Zhao acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n", 1923a4e081b0SShannon Zhao &res.start); 1924a4e081b0SShannon Zhao 1925a4e081b0SShannon Zhao return true; 1926a4e081b0SShannon Zhao } 1927a4e081b0SShannon Zhao 1928cde1f95fSSakari Ailus bool acpi_device_is_present(const struct acpi_device *adev) 1929202317a5SRafael J. Wysocki { 1930cde1f95fSSakari Ailus return adev->status.present || adev->status.functional; 1931202317a5SRafael J. Wysocki } 1932202317a5SRafael J. Wysocki 19334b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler, 1934844142c3SRasmus Villemoes const char *idstr, 1935c5698074SRafael J. Wysocki const struct acpi_device_id **matchid) 1936c5698074SRafael J. Wysocki { 1937c5698074SRafael J. Wysocki const struct acpi_device_id *devid; 1938c5698074SRafael J. Wysocki 1939aca0a4ebSRafael J. Wysocki if (handler->match) 1940aca0a4ebSRafael J. Wysocki return handler->match(idstr, matchid); 1941aca0a4ebSRafael J. Wysocki 1942c5698074SRafael J. Wysocki for (devid = handler->ids; devid->id[0]; devid++) 1943c5698074SRafael J. Wysocki if (!strcmp((char *)devid->id, idstr)) { 1944c5698074SRafael J. Wysocki if (matchid) 1945c5698074SRafael J. Wysocki *matchid = devid; 1946c5698074SRafael J. Wysocki 19474b59cc1fSRafael J. Wysocki return true; 19484b59cc1fSRafael J. Wysocki } 19494b59cc1fSRafael J. Wysocki 19504b59cc1fSRafael J. Wysocki return false; 19514b59cc1fSRafael J. Wysocki } 19524b59cc1fSRafael J. Wysocki 1953844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr, 19546b772e8fSToshi Kani const struct acpi_device_id **matchid) 19556b772e8fSToshi Kani { 19566b772e8fSToshi Kani struct acpi_scan_handler *handler; 19576b772e8fSToshi Kani 19586b772e8fSToshi Kani list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) 19596b772e8fSToshi Kani if (acpi_scan_handler_matching(handler, idstr, matchid)) 19606b772e8fSToshi Kani return handler; 19616b772e8fSToshi Kani 19626b772e8fSToshi Kani return NULL; 19636b772e8fSToshi Kani } 19646b772e8fSToshi Kani 19653f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val) 19663f8055c3SRafael J. Wysocki { 19673f8055c3SRafael J. Wysocki if (!!hotplug->enabled == !!val) 19683f8055c3SRafael J. Wysocki return; 19693f8055c3SRafael J. Wysocki 19703f8055c3SRafael J. Wysocki mutex_lock(&acpi_scan_lock); 19713f8055c3SRafael J. Wysocki 19723f8055c3SRafael J. Wysocki hotplug->enabled = val; 19733f8055c3SRafael J. Wysocki 19743f8055c3SRafael J. Wysocki mutex_unlock(&acpi_scan_lock); 19753f8055c3SRafael J. Wysocki } 19763f8055c3SRafael J. Wysocki 19773c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev) 19784b59cc1fSRafael J. Wysocki { 19796b772e8fSToshi Kani struct acpi_hardware_id *hwid; 19803c2cc7ffSRafael J. Wysocki 1981b43109faSRafael J. Wysocki if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) { 19821e2380cdSRafael J. Wysocki acpi_dock_add(adev); 19831e2380cdSRafael J. Wysocki return; 19841e2380cdSRafael J. Wysocki } 19853c2cc7ffSRafael J. Wysocki list_for_each_entry(hwid, &adev->pnp.ids, list) { 19864b59cc1fSRafael J. Wysocki struct acpi_scan_handler *handler; 19874b59cc1fSRafael J. Wysocki 19886b772e8fSToshi Kani handler = acpi_scan_match_handler(hwid->id, NULL); 19891a699476SRafael J. Wysocki if (handler) { 19903c2cc7ffSRafael J. Wysocki adev->flags.hotplug_notify = true; 19916b772e8fSToshi Kani break; 19926b772e8fSToshi Kani } 19936b772e8fSToshi Kani } 19941a699476SRafael J. Wysocki } 19956b772e8fSToshi Kani 199602056a4fSRafael J. Wysocki static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep) 199740e7fcb1SLan Tianyu { 199840e7fcb1SLan Tianyu struct acpi_handle_list dep_devices; 199940e7fcb1SLan Tianyu acpi_status status; 20006fc25088SRafael J. Wysocki u32 count; 200140e7fcb1SLan Tianyu int i; 200240e7fcb1SLan Tianyu 200371da201fSRafael J. Wysocki /* 200471da201fSRafael J. Wysocki * Check for _HID here to avoid deferring the enumeration of: 200571da201fSRafael J. Wysocki * 1. PCI devices. 200671da201fSRafael J. Wysocki * 2. ACPI nodes describing USB ports. 200771da201fSRafael J. Wysocki * Still, checking for _HID catches more then just these cases ... 200871da201fSRafael J. Wysocki */ 200902056a4fSRafael J. Wysocki if (!check_dep || !acpi_has_method(handle, "_DEP") || 201002056a4fSRafael J. Wysocki !acpi_has_method(handle, "_HID")) 20116fc25088SRafael J. Wysocki return 0; 201263347db0SHans de Goede 20136fc25088SRafael J. Wysocki status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices); 201440e7fcb1SLan Tianyu if (ACPI_FAILURE(status)) { 20156fc25088SRafael J. Wysocki acpi_handle_debug(handle, "Failed to evaluate _DEP.\n"); 20166fc25088SRafael J. Wysocki return 0; 201740e7fcb1SLan Tianyu } 201840e7fcb1SLan Tianyu 20196fc25088SRafael J. Wysocki for (count = 0, i = 0; i < dep_devices.count; i++) { 202040e7fcb1SLan Tianyu struct acpi_device_info *info; 20216fc25088SRafael J. Wysocki struct acpi_dep_data *dep; 20229d9bcae4SHans de Goede bool skip, honor_dep; 202340e7fcb1SLan Tianyu 202440e7fcb1SLan Tianyu status = acpi_get_object_info(dep_devices.handles[i], &info); 202540e7fcb1SLan Tianyu if (ACPI_FAILURE(status)) { 20266fc25088SRafael J. Wysocki acpi_handle_debug(handle, "Error reading _DEP device info\n"); 202740e7fcb1SLan Tianyu continue; 202840e7fcb1SLan Tianyu } 202940e7fcb1SLan Tianyu 20302ef33ee7SRafael J. Wysocki skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids); 20319d9bcae4SHans de Goede honor_dep = acpi_info_matches_ids(info, acpi_honor_dep_ids); 203240e7fcb1SLan Tianyu kfree(info); 203340e7fcb1SLan Tianyu 203440e7fcb1SLan Tianyu if (skip) 203540e7fcb1SLan Tianyu continue; 203640e7fcb1SLan Tianyu 20376fc25088SRafael J. Wysocki dep = kzalloc(sizeof(*dep), GFP_KERNEL); 203840e7fcb1SLan Tianyu if (!dep) 20396fc25088SRafael J. Wysocki continue; 20406fc25088SRafael J. Wysocki 20416fc25088SRafael J. Wysocki count++; 204240e7fcb1SLan Tianyu 204391438aebSRafael J. Wysocki dep->supplier = dep_devices.handles[i]; 20446fc25088SRafael J. Wysocki dep->consumer = handle; 20459d9bcae4SHans de Goede dep->honor_dep = honor_dep; 204640e7fcb1SLan Tianyu 204740e7fcb1SLan Tianyu mutex_lock(&acpi_dep_list_lock); 204840e7fcb1SLan Tianyu list_add_tail(&dep->node , &acpi_dep_list); 204940e7fcb1SLan Tianyu mutex_unlock(&acpi_dep_list_lock); 205040e7fcb1SLan Tianyu } 20516fc25088SRafael J. Wysocki 20526fc25088SRafael J. Wysocki return count; 205340e7fcb1SLan Tianyu } 205440e7fcb1SLan Tianyu 20550de7fb7cSRafael J. Wysocki static bool acpi_bus_scan_second_pass; 20560de7fb7cSRafael J. Wysocki 205771da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep, 205871da201fSRafael J. Wysocki struct acpi_device **adev_p) 20591da177e4SLinus Torvalds { 2060e3c963c4SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 2061e6c1067dSRafael J. Wysocki acpi_object_type acpi_type; 20626fc25088SRafael J. Wysocki int type; 20631da177e4SLinus Torvalds 20644002bf38SRafael J. Wysocki if (device) 20654002bf38SRafael J. Wysocki goto out; 20664002bf38SRafael J. Wysocki 2067e6c1067dSRafael J. Wysocki if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type))) 206851a85fafSBjorn Helgaas return AE_OK; 20691da177e4SLinus Torvalds 2070e6c1067dSRafael J. Wysocki switch (acpi_type) { 2071e6c1067dSRafael J. Wysocki case ACPI_TYPE_DEVICE: 2072e6c1067dSRafael J. Wysocki if (acpi_device_should_be_hidden(handle)) 207382c7d5efSRafael J. Wysocki return AE_OK; 207482c7d5efSRafael J. Wysocki 207502056a4fSRafael J. Wysocki /* Bail out if there are dependencies. */ 207602056a4fSRafael J. Wysocki if (acpi_scan_check_dep(handle, check_dep) > 0) { 20770de7fb7cSRafael J. Wysocki acpi_bus_scan_second_pass = true; 207871da201fSRafael J. Wysocki return AE_CTRL_DEPTH; 207971da201fSRafael J. Wysocki } 208002056a4fSRafael J. Wysocki 2081e6c1067dSRafael J. Wysocki fallthrough; 2082e6c1067dSRafael J. Wysocki case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ 2083e6c1067dSRafael J. Wysocki type = ACPI_BUS_TYPE_DEVICE; 2084e6c1067dSRafael J. Wysocki break; 2085e6c1067dSRafael J. Wysocki 2086e6c1067dSRafael J. Wysocki case ACPI_TYPE_PROCESSOR: 2087e6c1067dSRafael J. Wysocki type = ACPI_BUS_TYPE_PROCESSOR; 2088e6c1067dSRafael J. Wysocki break; 2089e6c1067dSRafael J. Wysocki 2090e6c1067dSRafael J. Wysocki case ACPI_TYPE_THERMAL: 2091e6c1067dSRafael J. Wysocki type = ACPI_BUS_TYPE_THERMAL; 2092e6c1067dSRafael J. Wysocki break; 2093e6c1067dSRafael J. Wysocki 2094e6c1067dSRafael J. Wysocki case ACPI_TYPE_POWER: 209502056a4fSRafael J. Wysocki acpi_add_power_resource(handle); 209602056a4fSRafael J. Wysocki fallthrough; 2097e6c1067dSRafael J. Wysocki default: 2098e6c1067dSRafael J. Wysocki return AE_OK; 20990de7fb7cSRafael J. Wysocki } 21006fc25088SRafael J. Wysocki 21013e759425SHans de Goede /* 21023e759425SHans de Goede * If check_dep is true at this point, the device has no dependencies, 21033e759425SHans de Goede * or the creation of the device object would have been postponed above. 21043e759425SHans de Goede */ 21056d279758SRafael J. Wysocki acpi_add_single_object(&device, handle, type, !check_dep); 21066d279758SRafael J. Wysocki if (!device) 21076d279758SRafael J. Wysocki return AE_CTRL_DEPTH; 21086d279758SRafael J. Wysocki 21096d279758SRafael J. Wysocki acpi_scan_init_hotplug(device); 21103c2cc7ffSRafael J. Wysocki 21114002bf38SRafael J. Wysocki out: 211271da201fSRafael J. Wysocki if (!*adev_p) 211371da201fSRafael J. Wysocki *adev_p = device; 2114805d410fSRafael J. Wysocki 211551a85fafSBjorn Helgaas return AE_OK; 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds 211871da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used, 211971da201fSRafael J. Wysocki void *not_used, void **ret_p) 212071da201fSRafael J. Wysocki { 212171da201fSRafael J. Wysocki return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p); 212271da201fSRafael J. Wysocki } 212371da201fSRafael J. Wysocki 212471da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used, 212571da201fSRafael J. Wysocki void *not_used, void **ret_p) 212671da201fSRafael J. Wysocki { 212771da201fSRafael J. Wysocki return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p); 212871da201fSRafael J. Wysocki } 212971da201fSRafael J. Wysocki 213048459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device) 213148459340SZhang Rui { 213248459340SZhang Rui /* 2133d87fb091SJohn Garry * Do not enumerate devices with enumeration_by_parent flag set as 2134d87fb091SJohn Garry * they will be enumerated by their respective parents. 213548459340SZhang Rui */ 2136d87fb091SJohn Garry if (!device->flags.enumeration_by_parent) { 21371571875bSHeikki Krogerus acpi_create_platform_device(device, NULL); 213810c7e20bSOctavian Purdila acpi_device_set_enumerated(device); 213968bdb677SOctavian Purdila } else { 214068bdb677SOctavian Purdila blocking_notifier_call_chain(&acpi_reconfig_chain, 214168bdb677SOctavian Purdila ACPI_RECONFIG_DEVICE_ADD, device); 214210c7e20bSOctavian Purdila } 214348459340SZhang Rui } 214448459340SZhang Rui 21457d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = { 2146ee892094SRafael J. Wysocki {ACPI_DT_NAMESPACE_HID, }, 21477d284352SRafael J. Wysocki {"", }, 21487d284352SRafael J. Wysocki }; 21497d284352SRafael J. Wysocki 21507d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev, 21517d284352SRafael J. Wysocki const struct acpi_device_id *not_used) 21527d284352SRafael J. Wysocki { 21537d284352SRafael J. Wysocki /* 2154ee892094SRafael J. Wysocki * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test 2155ee892094SRafael J. Wysocki * below can be unconditional. 21567d284352SRafael J. Wysocki */ 21577d284352SRafael J. Wysocki if (adev->data.of_compatible) 21587d284352SRafael J. Wysocki acpi_default_enumeration(adev); 21597d284352SRafael J. Wysocki 21607d284352SRafael J. Wysocki return 1; 21617d284352SRafael J. Wysocki } 21627d284352SRafael J. Wysocki 21637d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = { 21647d284352SRafael J. Wysocki .ids = generic_device_ids, 21657d284352SRafael J. Wysocki .attach = acpi_generic_device_attach, 21667d284352SRafael J. Wysocki }; 21677d284352SRafael J. Wysocki 216887b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device) 216987b85b3cSRafael J. Wysocki { 217087b85b3cSRafael J. Wysocki struct acpi_hardware_id *hwid; 217187b85b3cSRafael J. Wysocki int ret = 0; 217287b85b3cSRafael J. Wysocki 217387b85b3cSRafael J. Wysocki list_for_each_entry(hwid, &device->pnp.ids, list) { 2174c5698074SRafael J. Wysocki const struct acpi_device_id *devid; 2175c5698074SRafael J. Wysocki struct acpi_scan_handler *handler; 217687b85b3cSRafael J. Wysocki 2177c5698074SRafael J. Wysocki handler = acpi_scan_match_handler(hwid->id, &devid); 2178c5698074SRafael J. Wysocki if (handler) { 2179d34afa9dSRafael J. Wysocki if (!handler->attach) { 2180d34afa9dSRafael J. Wysocki device->pnp.type.platform_id = 0; 2181d34afa9dSRafael J. Wysocki continue; 2182d34afa9dSRafael J. Wysocki } 2183c5698074SRafael J. Wysocki device->handler = handler; 21849cb32acfSRafael J. Wysocki ret = handler->attach(device, devid); 21859cb32acfSRafael J. Wysocki if (ret > 0) 2186c5698074SRafael J. Wysocki break; 21879cb32acfSRafael J. Wysocki 21889cb32acfSRafael J. Wysocki device->handler = NULL; 21899cb32acfSRafael J. Wysocki if (ret < 0) 2190c5698074SRafael J. Wysocki break; 2191c5698074SRafael J. Wysocki } 2192c5698074SRafael J. Wysocki } 219348459340SZhang Rui 2194ca589f94SRafael J. Wysocki return ret; 2195ca589f94SRafael J. Wysocki } 2196ca589f94SRafael J. Wysocki 219771da201fSRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device, bool first_pass) 219851a85fafSBjorn Helgaas { 21992c22e652SRafael J. Wysocki struct acpi_device *child; 220071da201fSRafael J. Wysocki bool skip = !first_pass && device->flags.visited; 22011e2380cdSRafael J. Wysocki acpi_handle ejd; 2202ca589f94SRafael J. Wysocki int ret; 22031da177e4SLinus Torvalds 220471da201fSRafael J. Wysocki if (skip) 220571da201fSRafael J. Wysocki goto ok; 220671da201fSRafael J. Wysocki 22071e2380cdSRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd))) 22081e2380cdSRafael J. Wysocki register_dock_dependent_device(device, ejd); 22091e2380cdSRafael J. Wysocki 22102c22e652SRafael J. Wysocki acpi_bus_get_status(device); 22119d9bcae4SHans de Goede /* Skip devices that are not ready for enumeration (e.g. not present) */ 22129d9bcae4SHans de Goede if (!acpi_dev_ready_for_enumeration(device)) { 2213cde1f95fSSakari Ailus device->flags.initialized = false; 221410c7e20bSOctavian Purdila acpi_device_clear_enumerated(device); 22151b1f3e16SRafael J. Wysocki device->flags.power_manageable = 0; 22162c22e652SRafael J. Wysocki return; 22172c22e652SRafael J. Wysocki } 22183a391a39SRafael J. Wysocki if (device->handler) 22192c22e652SRafael J. Wysocki goto ok; 22203a391a39SRafael J. Wysocki 2221202317a5SRafael J. Wysocki if (!device->flags.initialized) { 22221b1f3e16SRafael J. Wysocki device->flags.power_manageable = 22231b1f3e16SRafael J. Wysocki device->power.states[ACPI_STATE_D0].flags.valid; 22241b1f3e16SRafael J. Wysocki if (acpi_bus_init_power(device)) 22251b1f3e16SRafael J. Wysocki device->flags.power_manageable = 0; 22261b1f3e16SRafael J. Wysocki 2227202317a5SRafael J. Wysocki device->flags.initialized = true; 2228c381fc3aSRafael J. Wysocki } else if (device->flags.visited) { 2229c381fc3aSRafael J. Wysocki goto ok; 2230202317a5SRafael J. Wysocki } 223110c7e20bSOctavian Purdila 2232ca589f94SRafael J. Wysocki ret = acpi_scan_attach_handler(device); 22336931007cSRafael J. Wysocki if (ret < 0) 22342c22e652SRafael J. Wysocki return; 22356931007cSRafael J. Wysocki 22366931007cSRafael J. Wysocki device->flags.match_driver = true; 2237d87fb091SJohn Garry if (ret > 0 && !device->flags.enumeration_by_parent) { 2238f406270bSRafael J. Wysocki acpi_device_set_enumerated(device); 2239f406270bSRafael J. Wysocki goto ok; 2240f406270bSRafael J. Wysocki } 2241f406270bSRafael J. Wysocki 2242ca589f94SRafael J. Wysocki ret = device_attach(&device->dev); 2243202317a5SRafael J. Wysocki if (ret < 0) 22442c22e652SRafael J. Wysocki return; 22457d284352SRafael J. Wysocki 2246d87fb091SJohn Garry if (device->pnp.type.platform_id || device->flags.enumeration_by_parent) 2247e4330d8bSJarkko Nikula acpi_default_enumeration(device); 2248d87fb091SJohn Garry else 2249d87fb091SJohn Garry acpi_device_set_enumerated(device); 2250202317a5SRafael J. Wysocki 2251202317a5SRafael J. Wysocki ok: 22522c22e652SRafael J. Wysocki list_for_each_entry(child, &device->children, node) 225371da201fSRafael J. Wysocki acpi_bus_attach(child, first_pass); 22548ab17fc9SRafael J. Wysocki 225571da201fSRafael J. Wysocki if (!skip && device->handler && device->handler->hotplug.notify_online) 22568ab17fc9SRafael J. Wysocki device->handler->hotplug.notify_online(device); 2257805d410fSRafael J. Wysocki } 22583fb02738SRajesh Shah 2259b83e2b30SDaniel Scally static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data) 226040e7fcb1SLan Tianyu { 226140e7fcb1SLan Tianyu struct acpi_device *adev; 226240e7fcb1SLan Tianyu 2263b83e2b30SDaniel Scally adev = acpi_bus_get_acpi_device(dep->consumer); 2264ad4d451eSRafael J. Wysocki if (adev) { 2265b83e2b30SDaniel Scally *(struct acpi_device **)data = adev; 2266b83e2b30SDaniel Scally return 1; 2267b83e2b30SDaniel Scally } 2268ad4d451eSRafael J. Wysocki /* Continue parsing if the device object is not present. */ 2269ad4d451eSRafael J. Wysocki return 0; 2270ad4d451eSRafael J. Wysocki } 2271b83e2b30SDaniel Scally 2272dc612486SRafael J. Wysocki struct acpi_scan_clear_dep_work { 2273dc612486SRafael J. Wysocki struct work_struct work; 2274dc612486SRafael J. Wysocki struct acpi_device *adev; 2275dc612486SRafael J. Wysocki }; 2276dc612486SRafael J. Wysocki 2277dc612486SRafael J. Wysocki static void acpi_scan_clear_dep_fn(struct work_struct *work) 2278dc612486SRafael J. Wysocki { 2279dc612486SRafael J. Wysocki struct acpi_scan_clear_dep_work *cdw; 2280dc612486SRafael J. Wysocki 2281dc612486SRafael J. Wysocki cdw = container_of(work, struct acpi_scan_clear_dep_work, work); 2282dc612486SRafael J. Wysocki 2283dc612486SRafael J. Wysocki acpi_scan_lock_acquire(); 2284dc612486SRafael J. Wysocki acpi_bus_attach(cdw->adev, true); 2285dc612486SRafael J. Wysocki acpi_scan_lock_release(); 2286dc612486SRafael J. Wysocki 2287dc612486SRafael J. Wysocki acpi_dev_put(cdw->adev); 2288dc612486SRafael J. Wysocki kfree(cdw); 2289dc612486SRafael J. Wysocki } 2290dc612486SRafael J. Wysocki 2291dc612486SRafael J. Wysocki static bool acpi_scan_clear_dep_queue(struct acpi_device *adev) 2292dc612486SRafael J. Wysocki { 2293dc612486SRafael J. Wysocki struct acpi_scan_clear_dep_work *cdw; 2294dc612486SRafael J. Wysocki 2295dc612486SRafael J. Wysocki if (adev->dep_unmet) 2296dc612486SRafael J. Wysocki return false; 2297dc612486SRafael J. Wysocki 2298dc612486SRafael J. Wysocki cdw = kmalloc(sizeof(*cdw), GFP_KERNEL); 2299dc612486SRafael J. Wysocki if (!cdw) 2300dc612486SRafael J. Wysocki return false; 2301dc612486SRafael J. Wysocki 2302dc612486SRafael J. Wysocki cdw->adev = adev; 2303dc612486SRafael J. Wysocki INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn); 2304dc612486SRafael J. Wysocki /* 2305dc612486SRafael J. Wysocki * Since the work function may block on the lock until the entire 2306dc612486SRafael J. Wysocki * initial enumeration of devices is complete, put it into the unbound 2307dc612486SRafael J. Wysocki * workqueue. 2308dc612486SRafael J. Wysocki */ 2309dc612486SRafael J. Wysocki queue_work(system_unbound_wq, &cdw->work); 2310dc612486SRafael J. Wysocki 2311dc612486SRafael J. Wysocki return true; 2312dc612486SRafael J. Wysocki } 2313dc612486SRafael J. Wysocki 2314a9e10e58SDaniel Scally static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data) 231540e7fcb1SLan Tianyu { 2316dc612486SRafael J. Wysocki struct acpi_device *adev = acpi_bus_get_acpi_device(dep->consumer); 231740e7fcb1SLan Tianyu 23180f347aa0SHans de Goede if (adev) { 231940e7fcb1SLan Tianyu adev->dep_unmet--; 2320dc612486SRafael J. Wysocki if (!acpi_scan_clear_dep_queue(adev)) 2321dc612486SRafael J. Wysocki acpi_dev_put(adev); 23220f347aa0SHans de Goede } 232371da201fSRafael J. Wysocki 232440e7fcb1SLan Tianyu list_del(&dep->node); 232540e7fcb1SLan Tianyu kfree(dep); 2326a9e10e58SDaniel Scally 2327a9e10e58SDaniel Scally return 0; 2328a9e10e58SDaniel Scally } 2329a9e10e58SDaniel Scally 2330a9e10e58SDaniel Scally /** 2331a9e10e58SDaniel Scally * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list 2332a9e10e58SDaniel Scally * @handle: The ACPI handle of the supplier device 2333a9e10e58SDaniel Scally * @callback: Pointer to the callback function to apply 2334a9e10e58SDaniel Scally * @data: Pointer to some data to pass to the callback 2335a9e10e58SDaniel Scally * 2336a9e10e58SDaniel Scally * The return value of the callback determines this function's behaviour. If 0 2337a9e10e58SDaniel Scally * is returned we continue to iterate over acpi_dep_list. If a positive value 2338a9e10e58SDaniel Scally * is returned then the loop is broken but this function returns 0. If a 2339a9e10e58SDaniel Scally * negative value is returned by the callback then the loop is broken and that 2340a9e10e58SDaniel Scally * value is returned as the final error. 2341a9e10e58SDaniel Scally */ 2342aff0dbd0SRafael J. Wysocki static int acpi_walk_dep_device_list(acpi_handle handle, 2343a9e10e58SDaniel Scally int (*callback)(struct acpi_dep_data *, void *), 2344a9e10e58SDaniel Scally void *data) 2345a9e10e58SDaniel Scally { 2346a9e10e58SDaniel Scally struct acpi_dep_data *dep, *tmp; 234723db673dSColin Ian King int ret = 0; 2348a9e10e58SDaniel Scally 2349a9e10e58SDaniel Scally mutex_lock(&acpi_dep_list_lock); 2350a9e10e58SDaniel Scally list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) { 2351a9e10e58SDaniel Scally if (dep->supplier == handle) { 2352a9e10e58SDaniel Scally ret = callback(dep, data); 2353a9e10e58SDaniel Scally if (ret) 2354a9e10e58SDaniel Scally break; 235540e7fcb1SLan Tianyu } 235640e7fcb1SLan Tianyu } 235740e7fcb1SLan Tianyu mutex_unlock(&acpi_dep_list_lock); 2358a9e10e58SDaniel Scally 2359a9e10e58SDaniel Scally return ret > 0 ? 0 : ret; 236040e7fcb1SLan Tianyu } 236140e7fcb1SLan Tianyu 2362b8bd759aSRafael J. Wysocki /** 2363a9e10e58SDaniel Scally * acpi_dev_clear_dependencies - Inform consumers that the device is now active 2364a9e10e58SDaniel Scally * @supplier: Pointer to the supplier &struct acpi_device 2365a9e10e58SDaniel Scally * 2366a9e10e58SDaniel Scally * Clear dependencies on the given device. 2367a9e10e58SDaniel Scally */ 2368a9e10e58SDaniel Scally void acpi_dev_clear_dependencies(struct acpi_device *supplier) 2369a9e10e58SDaniel Scally { 2370a9e10e58SDaniel Scally acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL); 2371a9e10e58SDaniel Scally } 2372a9e10e58SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies); 2373a9e10e58SDaniel Scally 2374a9e10e58SDaniel Scally /** 23759d9bcae4SHans de Goede * acpi_dev_ready_for_enumeration - Check if the ACPI device is ready for enumeration 23769d9bcae4SHans de Goede * @device: Pointer to the &struct acpi_device to check 23779d9bcae4SHans de Goede * 23789d9bcae4SHans de Goede * Check if the device is present and has no unmet dependencies. 23799d9bcae4SHans de Goede * 23809d9bcae4SHans de Goede * Return true if the device is ready for enumeratino. Otherwise, return false. 23819d9bcae4SHans de Goede */ 23829d9bcae4SHans de Goede bool acpi_dev_ready_for_enumeration(const struct acpi_device *device) 23839d9bcae4SHans de Goede { 23849d9bcae4SHans de Goede if (device->flags.honor_deps && device->dep_unmet) 23859d9bcae4SHans de Goede return false; 23869d9bcae4SHans de Goede 23879d9bcae4SHans de Goede return acpi_device_is_present(device); 23889d9bcae4SHans de Goede } 23899d9bcae4SHans de Goede EXPORT_SYMBOL_GPL(acpi_dev_ready_for_enumeration); 23909d9bcae4SHans de Goede 23919d9bcae4SHans de Goede /** 2392b83e2b30SDaniel Scally * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier 2393b83e2b30SDaniel Scally * @supplier: Pointer to the dependee device 2394b83e2b30SDaniel Scally * 2395b83e2b30SDaniel Scally * Returns the first &struct acpi_device which declares itself dependent on 2396b83e2b30SDaniel Scally * @supplier via the _DEP buffer, parsed from the acpi_dep_list. 2397b83e2b30SDaniel Scally * 2398b83e2b30SDaniel Scally * The caller is responsible for putting the reference to adev when it is no 2399b83e2b30SDaniel Scally * longer needed. 2400b83e2b30SDaniel Scally */ 2401b83e2b30SDaniel Scally struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier) 2402b83e2b30SDaniel Scally { 2403b83e2b30SDaniel Scally struct acpi_device *adev = NULL; 2404b83e2b30SDaniel Scally 2405b83e2b30SDaniel Scally acpi_walk_dep_device_list(supplier->handle, 2406b83e2b30SDaniel Scally acpi_dev_get_first_consumer_dev_cb, &adev); 2407b83e2b30SDaniel Scally 2408b83e2b30SDaniel Scally return adev; 2409b83e2b30SDaniel Scally } 2410b83e2b30SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev); 2411c10d7a13SDaniel Drake 24123fb02738SRajesh Shah /** 24131da177e4SLinus Torvalds * acpi_bus_scan - Add ACPI device node objects in a given namespace scope. 24142c0d4fe0SRafael J. Wysocki * @handle: Root of the namespace scope to scan. 24152c0d4fe0SRafael J. Wysocki * 24162c0d4fe0SRafael J. Wysocki * Scan a given ACPI tree (probably recently hot-plugged) and create and add 24175c478f49SBjorn Helgaas * found devices. 2418c4168bffSLi Shaohua * 24192c0d4fe0SRafael J. Wysocki * If no devices were found, -ENODEV is returned, but it does not mean that 24202c0d4fe0SRafael J. Wysocki * there has been a real error. There just have been no suitable ACPI objects 24212c0d4fe0SRafael J. Wysocki * in the table trunk from which the kernel could create a device and add an 24222c0d4fe0SRafael J. Wysocki * appropriate driver. 242388346167SRafael J. Wysocki * 24242c0d4fe0SRafael J. Wysocki * Must be called under acpi_scan_lock. 24253fb02738SRajesh Shah */ 24261da177e4SLinus Torvalds int acpi_bus_scan(acpi_handle handle) 24272c0d4fe0SRafael J. Wysocki { 242871da201fSRafael J. Wysocki struct acpi_device *device = NULL; 24291da177e4SLinus Torvalds 24300de7fb7cSRafael J. Wysocki acpi_bus_scan_second_pass = false; 24310de7fb7cSRafael J. Wysocki 243271da201fSRafael J. Wysocki /* Pass 1: Avoid enumerating devices with missing dependencies. */ 243371da201fSRafael J. Wysocki 243471da201fSRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device))) 24351da177e4SLinus Torvalds acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 243671da201fSRafael J. Wysocki acpi_bus_check_add_1, NULL, NULL, 243771da201fSRafael J. Wysocki (void **)&device); 24381da177e4SLinus Torvalds 243971da201fSRafael J. Wysocki if (!device) 24401da177e4SLinus Torvalds return -ENODEV; 244171da201fSRafael J. Wysocki 244271da201fSRafael J. Wysocki acpi_bus_attach(device, true); 244371da201fSRafael J. Wysocki 24440de7fb7cSRafael J. Wysocki if (!acpi_bus_scan_second_pass) 24450de7fb7cSRafael J. Wysocki return 0; 24460de7fb7cSRafael J. Wysocki 244771da201fSRafael J. Wysocki /* Pass 2: Enumerate all of the remaining devices. */ 244871da201fSRafael J. Wysocki 244971da201fSRafael J. Wysocki device = NULL; 245071da201fSRafael J. Wysocki 245171da201fSRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device))) 245271da201fSRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 245371da201fSRafael J. Wysocki acpi_bus_check_add_2, NULL, NULL, 245471da201fSRafael J. Wysocki (void **)&device); 245571da201fSRafael J. Wysocki 245671da201fSRafael J. Wysocki acpi_bus_attach(device, false); 245771da201fSRafael J. Wysocki 245871da201fSRafael J. Wysocki return 0; 24591da177e4SLinus Torvalds } 24601da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_scan); 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds /** 24631da177e4SLinus Torvalds * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects. 24641da177e4SLinus Torvalds * @adev: Root of the ACPI namespace scope to walk. 24651da177e4SLinus Torvalds * 24661da177e4SLinus Torvalds * Must be called under acpi_scan_lock. 24671da177e4SLinus Torvalds */ 24681da177e4SLinus Torvalds void acpi_bus_trim(struct acpi_device *adev) 24691da177e4SLinus Torvalds { 24701da177e4SLinus Torvalds struct acpi_scan_handler *handler = adev->handler; 24711da177e4SLinus Torvalds struct acpi_device *child; 24721da177e4SLinus Torvalds 24731da177e4SLinus Torvalds list_for_each_entry_reverse(child, &adev->children, node) 24741da177e4SLinus Torvalds acpi_bus_trim(child); 24751da177e4SLinus Torvalds 24761da177e4SLinus Torvalds adev->flags.match_driver = false; 24771da177e4SLinus Torvalds if (handler) { 24781da177e4SLinus Torvalds if (handler->detach) 24791da177e4SLinus Torvalds handler->detach(adev); 24801da177e4SLinus Torvalds 24811da177e4SLinus Torvalds adev->handler = NULL; 24821da177e4SLinus Torvalds } else { 24831da177e4SLinus Torvalds device_release_driver(&adev->dev); 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds /* 24861da177e4SLinus Torvalds * Most likely, the device is going away, so put it into D3cold before 24871da177e4SLinus Torvalds * that. 24881da177e4SLinus Torvalds */ 24891da177e4SLinus Torvalds acpi_device_set_power(adev, ACPI_STATE_D3_COLD); 24901da177e4SLinus Torvalds adev->flags.initialized = false; 249110c7e20bSOctavian Purdila acpi_device_clear_enumerated(adev); 24921da177e4SLinus Torvalds } 24931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(acpi_bus_trim); 24941da177e4SLinus Torvalds 2495a64a62ceSLv Zheng int acpi_bus_register_early_device(int type) 2496a64a62ceSLv Zheng { 2497a64a62ceSLv Zheng struct acpi_device *device = NULL; 2498a64a62ceSLv Zheng int result; 2499a64a62ceSLv Zheng 25006d279758SRafael J. Wysocki result = acpi_add_single_object(&device, NULL, type, false); 2501a64a62ceSLv Zheng if (result) 2502a64a62ceSLv Zheng return result; 2503a64a62ceSLv Zheng 2504a64a62ceSLv Zheng device->flags.match_driver = true; 2505a64a62ceSLv Zheng return device_attach(&device->dev); 2506a64a62ceSLv Zheng } 2507a64a62ceSLv Zheng EXPORT_SYMBOL_GPL(acpi_bus_register_early_device); 2508a64a62ceSLv Zheng 2509681e7187SRafael J. Wysocki static void acpi_bus_scan_fixed(void) 25101da177e4SLinus Torvalds { 25111da177e4SLinus Torvalds if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) { 2512681e7187SRafael J. Wysocki struct acpi_device *adev = NULL; 25131da177e4SLinus Torvalds 2514681e7187SRafael J. Wysocki acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_POWER_BUTTON, 2515681e7187SRafael J. Wysocki false); 2516681e7187SRafael J. Wysocki if (adev) { 2517681e7187SRafael J. Wysocki adev->flags.match_driver = true; 2518681e7187SRafael J. Wysocki if (device_attach(&adev->dev) >= 0) 2519681e7187SRafael J. Wysocki device_init_wakeup(&adev->dev, true); 2520681e7187SRafael J. Wysocki else 2521681e7187SRafael J. Wysocki dev_dbg(&adev->dev, "No driver\n"); 2522681e7187SRafael J. Wysocki } 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 25251da177e4SLinus Torvalds if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) { 2526681e7187SRafael J. Wysocki struct acpi_device *adev = NULL; 25271da177e4SLinus Torvalds 2528681e7187SRafael J. Wysocki acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_SLEEP_BUTTON, 2529681e7187SRafael J. Wysocki false); 2530681e7187SRafael J. Wysocki if (adev) { 2531681e7187SRafael J. Wysocki adev->flags.match_driver = true; 2532681e7187SRafael J. Wysocki if (device_attach(&adev->dev) < 0) 2533681e7187SRafael J. Wysocki dev_dbg(&adev->dev, "No driver\n"); 25341da177e4SLinus Torvalds } 2535681e7187SRafael J. Wysocki } 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 2538a4e081b0SShannon Zhao static void __init acpi_get_spcr_uart_addr(void) 2539a4e081b0SShannon Zhao { 2540a4e081b0SShannon Zhao acpi_status status; 2541a4e081b0SShannon Zhao struct acpi_table_spcr *spcr_ptr; 2542a4e081b0SShannon Zhao 2543a4e081b0SShannon Zhao status = acpi_get_table(ACPI_SIG_SPCR, 0, 2544a4e081b0SShannon Zhao (struct acpi_table_header **)&spcr_ptr); 2545ead7ba13SHanjun Guo if (ACPI_FAILURE(status)) { 25468acf4108SHanjun Guo pr_warn("STAO table present, but SPCR is missing\n"); 2547ead7ba13SHanjun Guo return; 2548ead7ba13SHanjun Guo } 2549ead7ba13SHanjun Guo 2550a4e081b0SShannon Zhao spcr_uart_addr = spcr_ptr->serial_port.address; 2551ead7ba13SHanjun Guo acpi_put_table((struct acpi_table_header *)spcr_ptr); 2552a4e081b0SShannon Zhao } 2553a4e081b0SShannon Zhao 255468bdb677SOctavian Purdila static bool acpi_scan_initialized; 255568bdb677SOctavian Purdila 2556b6c55b16SRafael J. Wysocki void __init acpi_scan_init(void) 25571da177e4SLinus Torvalds { 2558a4e081b0SShannon Zhao acpi_status status; 2559a4e081b0SShannon Zhao struct acpi_table_stao *stao_ptr; 25601da177e4SLinus Torvalds 256192ef2a25SRafael J. Wysocki acpi_pci_root_init(); 25624daeaf68SRafael J. Wysocki acpi_pci_link_init(); 2563ac212b69SRafael J. Wysocki acpi_processor_init(); 2564cb0701acSAndy Shevchenko acpi_platform_init(); 2565f58b082aSRafael J. Wysocki acpi_lpss_init(); 256692082a88SKen Xue acpi_apd_init(); 25672fa97febSLan Tianyu acpi_cmos_rtc_init(); 2568737f1a9fSRafael J. Wysocki acpi_container_init(); 25690a347644SRafael J. Wysocki acpi_memory_hotplug_init(); 2570cc6a0e31SMika Westerberg acpi_watchdog_init(); 2571eec15edbSZhang Rui acpi_pnp_init(); 25723230bbfcSZhang Rui acpi_int340x_thermal_init(); 25736ce2e188SGraeme Gregory acpi_amba_init(); 2574eeb2d80dSSrinivas Pandruvada acpi_init_lpit(); 257597d9a9e9SRafael J. Wysocki 25767d284352SRafael J. Wysocki acpi_scan_add_handler(&generic_device_handler); 25777d284352SRafael J. Wysocki 2578a4e081b0SShannon Zhao /* 2579a4e081b0SShannon Zhao * If there is STAO table, check whether it needs to ignore the UART 2580a4e081b0SShannon Zhao * device in SPCR table. 2581a4e081b0SShannon Zhao */ 2582a4e081b0SShannon Zhao status = acpi_get_table(ACPI_SIG_STAO, 0, 2583a4e081b0SShannon Zhao (struct acpi_table_header **)&stao_ptr); 2584a4e081b0SShannon Zhao if (ACPI_SUCCESS(status)) { 2585a4e081b0SShannon Zhao if (stao_ptr->header.length > sizeof(struct acpi_table_stao)) 25868acf4108SHanjun Guo pr_info("STAO Name List not yet supported.\n"); 2587a4e081b0SShannon Zhao 2588a4e081b0SShannon Zhao if (stao_ptr->ignore_uart) 2589a4e081b0SShannon Zhao acpi_get_spcr_uart_addr(); 2590ead7ba13SHanjun Guo 2591ead7ba13SHanjun Guo acpi_put_table((struct acpi_table_header *)stao_ptr); 2592a4e081b0SShannon Zhao } 2593a4e081b0SShannon Zhao 2594eb7f43c4SRafael J. Wysocki acpi_gpe_apply_masked_gpes(); 2595eb7f43c4SRafael J. Wysocki acpi_update_all_gpes(); 2596eb7f43c4SRafael J. Wysocki 25977291edcaSDavid Hildenbrand /* 25987291edcaSDavid Hildenbrand * Although we call __add_memory() that is documented to require the 25997291edcaSDavid Hildenbrand * device_hotplug_lock, it is not necessary here because this is an 26007291edcaSDavid Hildenbrand * early code when userspace or any other code path cannot trigger 26017291edcaSDavid Hildenbrand * hotplug/hotunplug operations. 26027291edcaSDavid Hildenbrand */ 26033757b948SRafael J. Wysocki mutex_lock(&acpi_scan_lock); 26041da177e4SLinus Torvalds /* 26051da177e4SLinus Torvalds * Enumerate devices in the ACPI namespace. 26061da177e4SLinus Torvalds */ 2607b6c55b16SRafael J. Wysocki if (acpi_bus_scan(ACPI_ROOT_OBJECT)) 2608c96f195dSRafael J. Wysocki goto unlock; 2609c04209a7SAlexey Starikovskiy 2610e3c963c4SRafael J. Wysocki acpi_root = acpi_fetch_acpi_dev(ACPI_ROOT_OBJECT); 2611e3c963c4SRafael J. Wysocki if (!acpi_root) 2612c96f195dSRafael J. Wysocki goto unlock; 2613b8bd759aSRafael J. Wysocki 26142650ef42SAaron Lu /* Fixed feature devices do not exist on HW-reduced platform */ 2615681e7187SRafael J. Wysocki if (!acpi_gbl_reduced_hardware) 2616681e7187SRafael J. Wysocki acpi_bus_scan_fixed(); 2617b8bd759aSRafael J. Wysocki 26186381195aSRafael J. Wysocki acpi_turn_off_unused_power_resources(); 261929038ae2SRafael J. Wysocki 262068bdb677SOctavian Purdila acpi_scan_initialized = true; 262168bdb677SOctavian Purdila 2622c96f195dSRafael J. Wysocki unlock: 26233757b948SRafael J. Wysocki mutex_unlock(&acpi_scan_lock); 26241da177e4SLinus Torvalds } 2625e647b532SMarc Zyngier 2626e647b532SMarc Zyngier static struct acpi_probe_entry *ape; 2627e647b532SMarc Zyngier static int acpi_probe_count; 26285331d9caSLorenzo Pieralisi static DEFINE_MUTEX(acpi_probe_mutex); 2629e647b532SMarc Zyngier 263060574d1eSKeith Busch static int __init acpi_match_madt(union acpi_subtable_headers *header, 2631e647b532SMarc Zyngier const unsigned long end) 2632e647b532SMarc Zyngier { 263360574d1eSKeith Busch if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape)) 2634e647b532SMarc Zyngier if (!ape->probe_subtbl(header, end)) 2635e647b532SMarc Zyngier acpi_probe_count++; 2636e647b532SMarc Zyngier 2637e647b532SMarc Zyngier return 0; 2638e647b532SMarc Zyngier } 2639e647b532SMarc Zyngier 2640e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr) 2641e647b532SMarc Zyngier { 2642e647b532SMarc Zyngier int count = 0; 2643e647b532SMarc Zyngier 2644e647b532SMarc Zyngier if (acpi_disabled) 2645e647b532SMarc Zyngier return 0; 2646e647b532SMarc Zyngier 26475331d9caSLorenzo Pieralisi mutex_lock(&acpi_probe_mutex); 2648e647b532SMarc Zyngier for (ape = ap_head; nr; ape++, nr--) { 26495599fb69SBob Moore if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) { 2650e647b532SMarc Zyngier acpi_probe_count = 0; 2651e647b532SMarc Zyngier acpi_table_parse_madt(ape->type, acpi_match_madt, 0); 2652e647b532SMarc Zyngier count += acpi_probe_count; 2653e647b532SMarc Zyngier } else { 2654e647b532SMarc Zyngier int res; 2655e647b532SMarc Zyngier res = acpi_table_parse(ape->id, ape->probe_table); 2656e647b532SMarc Zyngier if (!res) 2657e647b532SMarc Zyngier count++; 2658e647b532SMarc Zyngier } 2659e647b532SMarc Zyngier } 26605331d9caSLorenzo Pieralisi mutex_unlock(&acpi_probe_mutex); 2661e647b532SMarc Zyngier 2662e647b532SMarc Zyngier return count; 2663e647b532SMarc Zyngier } 266468bdb677SOctavian Purdila 266568bdb677SOctavian Purdila static void acpi_table_events_fn(struct work_struct *work) 266668bdb677SOctavian Purdila { 266768bdb677SOctavian Purdila acpi_scan_lock_acquire(); 266868bdb677SOctavian Purdila acpi_bus_scan(ACPI_ROOT_OBJECT); 266968bdb677SOctavian Purdila acpi_scan_lock_release(); 26708d287e82SRafael J. Wysocki 26718d287e82SRafael J. Wysocki kfree(work); 267268bdb677SOctavian Purdila } 267368bdb677SOctavian Purdila 26748d287e82SRafael J. Wysocki void acpi_scan_table_notify(void) 267568bdb677SOctavian Purdila { 26768d287e82SRafael J. Wysocki struct work_struct *work; 267768bdb677SOctavian Purdila 267868bdb677SOctavian Purdila if (!acpi_scan_initialized) 267968bdb677SOctavian Purdila return; 268068bdb677SOctavian Purdila 26818d287e82SRafael J. Wysocki work = kmalloc(sizeof(*work), GFP_KERNEL); 26828d287e82SRafael J. Wysocki if (!work) 268368bdb677SOctavian Purdila return; 268468bdb677SOctavian Purdila 26858d287e82SRafael J. Wysocki INIT_WORK(work, acpi_table_events_fn); 26868d287e82SRafael J. Wysocki schedule_work(work); 268768bdb677SOctavian Purdila } 268868bdb677SOctavian Purdila 268968bdb677SOctavian Purdila int acpi_reconfig_notifier_register(struct notifier_block *nb) 269068bdb677SOctavian Purdila { 269168bdb677SOctavian Purdila return blocking_notifier_chain_register(&acpi_reconfig_chain, nb); 269268bdb677SOctavian Purdila } 269368bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_register); 269468bdb677SOctavian Purdila 269568bdb677SOctavian Purdila int acpi_reconfig_notifier_unregister(struct notifier_block *nb) 269668bdb677SOctavian Purdila { 269768bdb677SOctavian Purdila return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb); 269868bdb677SOctavian Purdila } 269968bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_unregister); 2700