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 61da177e4SLinus Torvalds #include <linux/module.h> 71da177e4SLinus Torvalds #include <linux/init.h> 85a0e3ad6STejun Heo #include <linux/slab.h> 99b6d97b6SRandy Dunlap #include <linux/kernel.h> 101da177e4SLinus Torvalds #include <linux/acpi.h> 11643b8e4dSLorenzo Pieralisi #include <linux/acpi_iort.h> 1274523c90SAlok N Kataria #include <linux/signal.h> 1374523c90SAlok N Kataria #include <linux/kthread.h> 14222e82acSDarrick J. Wong #include <linux/dmi.h> 15d1efe3c3SLance Ortiz #include <linux/nls.h> 160a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h> 17ca9ef3abSLukas Wunner #include <linux/platform_data/x86/apple.h> 18ca5999fdSMike Rapoport #include <linux/pgtable.h> 19d783156eSRafael J. Wysocki 20e60cc7a6SBjorn Helgaas #include "internal.h" 21e60cc7a6SBjorn Helgaas 221da177e4SLinus Torvalds #define _COMPONENT ACPI_BUS_COMPONENT 23f52fd66dSLen Brown ACPI_MODULE_NAME("scan"); 241da177e4SLinus Torvalds extern struct acpi_device *acpi_root; 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #define ACPI_BUS_CLASS "system_bus" 2729b71a1cSThomas Renninger #define ACPI_BUS_HID "LNXSYBUS" 281da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME "System Bus" 291da177e4SLinus Torvalds 30859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent) 31859ac9a4SBjorn Helgaas 32d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page) 33d783156eSRafael J. Wysocki 34620e112cSThomas Renninger static const char *dummy_hid = "device"; 352b2ae7c7SThomas Renninger 3640e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list); 3740e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock); 382d12b6b3SLukas Wunner LIST_HEAD(acpi_bus_id_list); 39c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock); 40ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list); 419090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock); 421da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list); 43e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock); 441da177e4SLinus Torvalds 45a4e081b0SShannon Zhao /* 46a4e081b0SShannon Zhao * The UART device described by the SPCR table is the only object which needs 47a4e081b0SShannon Zhao * special-casing. Everything else is covered by ACPI namespace paths in STAO 48a4e081b0SShannon Zhao * table. 49a4e081b0SShannon Zhao */ 50a4e081b0SShannon Zhao static u64 spcr_uart_addr; 51a4e081b0SShannon Zhao 5240e7fcb1SLan Tianyu struct acpi_dep_data { 5340e7fcb1SLan Tianyu struct list_head node; 5491438aebSRafael J. Wysocki acpi_handle supplier; 5591438aebSRafael J. Wysocki acpi_handle consumer; 5640e7fcb1SLan Tianyu }; 5740e7fcb1SLan Tianyu 583757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void) 593757b948SRafael J. Wysocki { 603757b948SRafael J. Wysocki mutex_lock(&acpi_scan_lock); 613757b948SRafael J. Wysocki } 623757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire); 633757b948SRafael J. Wysocki 643757b948SRafael J. Wysocki void acpi_scan_lock_release(void) 653757b948SRafael J. Wysocki { 663757b948SRafael J. Wysocki mutex_unlock(&acpi_scan_lock); 673757b948SRafael J. Wysocki } 683757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release); 693757b948SRafael J. Wysocki 70e525506fSRafael J. Wysocki void acpi_lock_hp_context(void) 71e525506fSRafael J. Wysocki { 72e525506fSRafael J. Wysocki mutex_lock(&acpi_hp_context_lock); 73e525506fSRafael J. Wysocki } 74e525506fSRafael J. Wysocki 75e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void) 76e525506fSRafael J. Wysocki { 77e525506fSRafael J. Wysocki mutex_unlock(&acpi_hp_context_lock); 78e525506fSRafael J. Wysocki } 79e525506fSRafael J. Wysocki 805d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev, 815d513205SRafael J. Wysocki struct acpi_hotplug_context *hp, 825d513205SRafael J. Wysocki int (*notify)(struct acpi_device *, u32), 835d513205SRafael J. Wysocki void (*uevent)(struct acpi_device *, u32)) 845d513205SRafael J. Wysocki { 855d513205SRafael J. Wysocki acpi_lock_hp_context(); 86ba574dc8SRafael J. Wysocki hp->notify = notify; 87ba574dc8SRafael J. Wysocki hp->uevent = uevent; 88ba574dc8SRafael J. Wysocki acpi_set_hp_context(adev, hp); 895d513205SRafael J. Wysocki acpi_unlock_hp_context(); 905d513205SRafael J. Wysocki } 915d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context); 925d513205SRafael J. Wysocki 93ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler) 94ca589f94SRafael J. Wysocki { 95d34afa9dSRafael J. Wysocki if (!handler) 96ca589f94SRafael J. Wysocki return -EINVAL; 97ca589f94SRafael J. Wysocki 98ca589f94SRafael J. Wysocki list_add_tail(&handler->list_node, &acpi_scan_handlers_list); 99ca589f94SRafael J. Wysocki return 0; 100ca589f94SRafael J. Wysocki } 101ca589f94SRafael J. Wysocki 1023f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler, 1033f8055c3SRafael J. Wysocki const char *hotplug_profile_name) 1043f8055c3SRafael J. Wysocki { 1053f8055c3SRafael J. Wysocki int error; 1063f8055c3SRafael J. Wysocki 1073f8055c3SRafael J. Wysocki error = acpi_scan_add_handler(handler); 1083f8055c3SRafael J. Wysocki if (error) 1093f8055c3SRafael J. Wysocki return error; 1103f8055c3SRafael J. Wysocki 1113f8055c3SRafael J. Wysocki acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name); 1123f8055c3SRafael J. Wysocki return 0; 1133f8055c3SRafael J. Wysocki } 1143f8055c3SRafael J. Wysocki 115caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent) 116d22ddcbcSRafael J. Wysocki { 117d22ddcbcSRafael J. Wysocki struct acpi_device_physical_node *pn; 118d22ddcbcSRafael J. Wysocki bool offline = true; 11927664c58SLee, Chun-Yi char *envp[] = { "EVENT=offline", NULL }; 120d22ddcbcSRafael J. Wysocki 1214c533c80SRafael J. Wysocki /* 1224c533c80SRafael J. Wysocki * acpi_container_offline() calls this for all of the container's 1234c533c80SRafael J. Wysocki * children under the container's physical_node_lock lock. 1244c533c80SRafael J. Wysocki */ 1254c533c80SRafael J. Wysocki mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING); 126d22ddcbcSRafael J. Wysocki 127d22ddcbcSRafael J. Wysocki list_for_each_entry(pn, &adev->physical_node_list, node) 128d22ddcbcSRafael J. Wysocki if (device_supports_offline(pn->dev) && !pn->dev->offline) { 129caa73ea1SRafael J. Wysocki if (uevent) 13027664c58SLee, Chun-Yi kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp); 131caa73ea1SRafael J. Wysocki 132d22ddcbcSRafael J. Wysocki offline = false; 133d22ddcbcSRafael J. Wysocki break; 134d22ddcbcSRafael J. Wysocki } 135d22ddcbcSRafael J. Wysocki 136d22ddcbcSRafael J. Wysocki mutex_unlock(&adev->physical_node_lock); 137d22ddcbcSRafael J. Wysocki return offline; 138d22ddcbcSRafael J. Wysocki } 139d22ddcbcSRafael J. Wysocki 1407f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data, 1417f28ddecSRafael J. Wysocki void **ret_p) 142683058e3SRafael J. Wysocki { 143683058e3SRafael J. Wysocki struct acpi_device *device = NULL; 144683058e3SRafael J. Wysocki struct acpi_device_physical_node *pn; 145303bfdb1SRafael J. Wysocki bool second_pass = (bool)data; 146683058e3SRafael J. Wysocki acpi_status status = AE_OK; 147683058e3SRafael J. Wysocki 148683058e3SRafael J. Wysocki if (acpi_bus_get_device(handle, &device)) 149683058e3SRafael J. Wysocki return AE_OK; 150683058e3SRafael J. Wysocki 1517f28ddecSRafael J. Wysocki if (device->handler && !device->handler->hotplug.enabled) { 1527f28ddecSRafael J. Wysocki *ret_p = &device->dev; 1537f28ddecSRafael J. Wysocki return AE_SUPPORT; 1547f28ddecSRafael J. Wysocki } 1557f28ddecSRafael J. Wysocki 156683058e3SRafael J. Wysocki mutex_lock(&device->physical_node_lock); 157683058e3SRafael J. Wysocki 158683058e3SRafael J. Wysocki list_for_each_entry(pn, &device->physical_node_list, node) { 159683058e3SRafael J. Wysocki int ret; 160683058e3SRafael J. Wysocki 161303bfdb1SRafael J. Wysocki if (second_pass) { 162303bfdb1SRafael J. Wysocki /* Skip devices offlined by the first pass. */ 163303bfdb1SRafael J. Wysocki if (pn->put_online) 164303bfdb1SRafael J. Wysocki continue; 165303bfdb1SRafael J. Wysocki } else { 166303bfdb1SRafael J. Wysocki pn->put_online = false; 167303bfdb1SRafael J. Wysocki } 168683058e3SRafael J. Wysocki ret = device_offline(pn->dev); 169303bfdb1SRafael J. Wysocki if (ret >= 0) { 170303bfdb1SRafael J. Wysocki pn->put_online = !ret; 171303bfdb1SRafael J. Wysocki } else { 172303bfdb1SRafael J. Wysocki *ret_p = pn->dev; 173303bfdb1SRafael J. Wysocki if (second_pass) { 174683058e3SRafael J. Wysocki status = AE_ERROR; 175683058e3SRafael J. Wysocki break; 176683058e3SRafael J. Wysocki } 177303bfdb1SRafael J. Wysocki } 178683058e3SRafael J. Wysocki } 179683058e3SRafael J. Wysocki 180683058e3SRafael J. Wysocki mutex_unlock(&device->physical_node_lock); 181683058e3SRafael J. Wysocki 182683058e3SRafael J. Wysocki return status; 183683058e3SRafael J. Wysocki } 184683058e3SRafael J. Wysocki 1857f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data, 1867f28ddecSRafael J. Wysocki void **ret_p) 187683058e3SRafael J. Wysocki { 188683058e3SRafael J. Wysocki struct acpi_device *device = NULL; 189683058e3SRafael J. Wysocki struct acpi_device_physical_node *pn; 190683058e3SRafael J. Wysocki 191683058e3SRafael J. Wysocki if (acpi_bus_get_device(handle, &device)) 192683058e3SRafael J. Wysocki return AE_OK; 193683058e3SRafael J. Wysocki 194683058e3SRafael J. Wysocki mutex_lock(&device->physical_node_lock); 195683058e3SRafael J. Wysocki 196683058e3SRafael J. Wysocki list_for_each_entry(pn, &device->physical_node_list, node) 197683058e3SRafael J. Wysocki if (pn->put_online) { 198683058e3SRafael J. Wysocki device_online(pn->dev); 199683058e3SRafael J. Wysocki pn->put_online = false; 200683058e3SRafael J. Wysocki } 201683058e3SRafael J. Wysocki 202683058e3SRafael J. Wysocki mutex_unlock(&device->physical_node_lock); 203683058e3SRafael J. Wysocki 204683058e3SRafael J. Wysocki return AE_OK; 205683058e3SRafael J. Wysocki } 206683058e3SRafael J. Wysocki 207d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device) 2081da177e4SLinus Torvalds { 2095993c467SYinghai Lu acpi_handle handle = device->handle; 210d22ddcbcSRafael J. Wysocki struct device *errdev = NULL; 211a33ec399SRafael J. Wysocki acpi_status status; 212f058cdf4SRafael J. Wysocki 213303bfdb1SRafael J. Wysocki /* 214303bfdb1SRafael J. Wysocki * Carry out two passes here and ignore errors in the first pass, 215303bfdb1SRafael J. Wysocki * because if the devices in question are memory blocks and 216303bfdb1SRafael J. Wysocki * CONFIG_MEMCG is set, one of the blocks may hold data structures 217303bfdb1SRafael J. Wysocki * that the other blocks depend on, but it is not known in advance which 218303bfdb1SRafael J. Wysocki * block holds them. 219303bfdb1SRafael J. Wysocki * 220303bfdb1SRafael J. Wysocki * If the first pass is successful, the second one isn't needed, though. 221303bfdb1SRafael J. Wysocki */ 2227f28ddecSRafael J. Wysocki status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 2237f28ddecSRafael J. Wysocki NULL, acpi_bus_offline, (void *)false, 2247f28ddecSRafael J. Wysocki (void **)&errdev); 2257f28ddecSRafael J. Wysocki if (status == AE_SUPPORT) { 2267f28ddecSRafael J. Wysocki dev_warn(errdev, "Offline disabled.\n"); 227683058e3SRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 2287f28ddecSRafael J. Wysocki acpi_bus_online, NULL, NULL, NULL); 2297f28ddecSRafael J. Wysocki return -EPERM; 2307f28ddecSRafael J. Wysocki } 2317f28ddecSRafael J. Wysocki acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev); 232303bfdb1SRafael J. Wysocki if (errdev) { 233303bfdb1SRafael J. Wysocki errdev = NULL; 234303bfdb1SRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 2357f28ddecSRafael J. Wysocki NULL, acpi_bus_offline, (void *)true, 2367f28ddecSRafael J. Wysocki (void **)&errdev); 237ffc10d82SMichal Hocko if (!errdev) 2387f28ddecSRafael J. Wysocki acpi_bus_offline(handle, 0, (void *)true, 239303bfdb1SRafael J. Wysocki (void **)&errdev); 240303bfdb1SRafael J. Wysocki 241ffc10d82SMichal Hocko if (errdev) { 242303bfdb1SRafael J. Wysocki dev_warn(errdev, "Offline failed.\n"); 2437f28ddecSRafael J. Wysocki acpi_bus_online(handle, 0, NULL, NULL); 244303bfdb1SRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, 2457f28ddecSRafael J. Wysocki ACPI_UINT32_MAX, acpi_bus_online, 2467f28ddecSRafael J. Wysocki NULL, NULL, NULL); 247683058e3SRafael J. Wysocki return -EBUSY; 248683058e3SRafael J. Wysocki } 249303bfdb1SRafael J. Wysocki } 250d22ddcbcSRafael J. Wysocki return 0; 251d22ddcbcSRafael J. Wysocki } 252d22ddcbcSRafael J. Wysocki 253d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device) 254d22ddcbcSRafael J. Wysocki { 255d22ddcbcSRafael J. Wysocki acpi_handle handle = device->handle; 256d22ddcbcSRafael J. Wysocki unsigned long long sta; 257d22ddcbcSRafael J. Wysocki acpi_status status; 258d22ddcbcSRafael J. Wysocki 259ffc10d82SMichal Hocko if (device->handler && device->handler->hotplug.demand_offline) { 260caa73ea1SRafael J. Wysocki if (!acpi_scan_is_offline(device, true)) 261d22ddcbcSRafael J. Wysocki return -EBUSY; 262d22ddcbcSRafael J. Wysocki } else { 263d22ddcbcSRafael J. Wysocki int error = acpi_scan_try_to_offline(device); 264d22ddcbcSRafael J. Wysocki if (error) 265d22ddcbcSRafael J. Wysocki return error; 266d22ddcbcSRafael J. Wysocki } 267683058e3SRafael J. Wysocki 26826d46867SZhang Rui ACPI_DEBUG_PRINT((ACPI_DB_INFO, 2690794469dSKay Sievers "Hot-removing device %s...\n", dev_name(&device->dev))); 27026d46867SZhang Rui 271bfee26dbSRafael J. Wysocki acpi_bus_trim(device); 272683058e3SRafael J. Wysocki 2737d2421f8SJiang Liu acpi_evaluate_lck(handle, 0); 2741da177e4SLinus Torvalds /* 2751da177e4SLinus Torvalds * TBD: _EJD support. 2761da177e4SLinus Torvalds */ 2777d2421f8SJiang Liu status = acpi_evaluate_ej0(handle); 2787d2421f8SJiang Liu if (status == AE_NOT_FOUND) 279a33ec399SRafael J. Wysocki return -ENODEV; 2807d2421f8SJiang Liu else if (ACPI_FAILURE(status)) 281a33ec399SRafael J. Wysocki return -EIO; 282f058cdf4SRafael J. Wysocki 283882fd12eSToshi Kani /* 284882fd12eSToshi Kani * Verify if eject was indeed successful. If not, log an error 285882fd12eSToshi Kani * message. No need to call _OST since _EJ0 call was made OK. 286882fd12eSToshi Kani */ 287882fd12eSToshi Kani status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); 288882fd12eSToshi Kani if (ACPI_FAILURE(status)) { 289882fd12eSToshi Kani acpi_handle_warn(handle, 290882fd12eSToshi Kani "Status check after eject failed (0x%x)\n", status); 291882fd12eSToshi Kani } else if (sta & ACPI_STA_DEVICE_ENABLED) { 292882fd12eSToshi Kani acpi_handle_warn(handle, 293882fd12eSToshi Kani "Eject incomplete - status 0x%llx\n", sta); 294882fd12eSToshi Kani } 295882fd12eSToshi Kani 296a33ec399SRafael J. Wysocki return 0; 297a33ec399SRafael J. Wysocki } 298f058cdf4SRafael J. Wysocki 299443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev) 300443fc820SRafael J. Wysocki { 301443fc820SRafael J. Wysocki if (!acpi_device_enumerated(adev)) { 302443fc820SRafael J. Wysocki dev_warn(&adev->dev, "Still not present\n"); 303443fc820SRafael J. Wysocki return -EALREADY; 304443fc820SRafael J. Wysocki } 305443fc820SRafael J. Wysocki acpi_bus_trim(adev); 306443fc820SRafael J. Wysocki return 0; 307443fc820SRafael J. Wysocki } 308443fc820SRafael J. Wysocki 309c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev) 310a33ec399SRafael J. Wysocki { 311f943db40SRafael J. Wysocki int error; 312a33ec399SRafael J. Wysocki 313443fc820SRafael J. Wysocki acpi_bus_get_status(adev); 314443fc820SRafael J. Wysocki if (adev->status.present || adev->status.functional) { 315443fc820SRafael J. Wysocki /* 316443fc820SRafael J. Wysocki * This function is only called for device objects for which 317443fc820SRafael J. Wysocki * matching scan handlers exist. The only situation in which 318443fc820SRafael J. Wysocki * the scan handler is not attached to this device object yet 319443fc820SRafael J. Wysocki * is when the device has just appeared (either it wasn't 320443fc820SRafael J. Wysocki * present at all before or it was removed and then added 321443fc820SRafael J. Wysocki * again). 322443fc820SRafael J. Wysocki */ 323443fc820SRafael J. Wysocki if (adev->handler) { 324443fc820SRafael J. Wysocki dev_warn(&adev->dev, "Already enumerated\n"); 325443fc820SRafael J. Wysocki return -EALREADY; 326443fc820SRafael J. Wysocki } 327c27b2c33SRafael J. Wysocki error = acpi_bus_scan(adev->handle); 328a33ec399SRafael J. Wysocki if (error) { 329c27b2c33SRafael J. Wysocki dev_warn(&adev->dev, "Namespace scan failure\n"); 330c27b2c33SRafael J. Wysocki return error; 331a33ec399SRafael J. Wysocki } 33246394fd0SRafael J. Wysocki if (!adev->handler) { 333c27b2c33SRafael J. Wysocki dev_warn(&adev->dev, "Enumeration failure\n"); 334443fc820SRafael J. Wysocki error = -ENODEV; 335443fc820SRafael J. Wysocki } 336443fc820SRafael J. Wysocki } else { 337443fc820SRafael J. Wysocki error = acpi_scan_device_not_present(adev); 338443fc820SRafael J. Wysocki } 339443fc820SRafael J. Wysocki return error; 340443fc820SRafael J. Wysocki } 341443fc820SRafael J. Wysocki 342443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev) 343443fc820SRafael J. Wysocki { 344443fc820SRafael J. Wysocki struct acpi_scan_handler *handler = adev->handler; 345443fc820SRafael J. Wysocki struct acpi_device *child; 346443fc820SRafael J. Wysocki int error; 347443fc820SRafael J. Wysocki 348443fc820SRafael J. Wysocki acpi_bus_get_status(adev); 349443fc820SRafael J. Wysocki if (!(adev->status.present || adev->status.functional)) { 350443fc820SRafael J. Wysocki acpi_scan_device_not_present(adev); 351443fc820SRafael J. Wysocki return 0; 352443fc820SRafael J. Wysocki } 353443fc820SRafael J. Wysocki if (handler && handler->hotplug.scan_dependent) 354443fc820SRafael J. Wysocki return handler->hotplug.scan_dependent(adev); 355443fc820SRafael J. Wysocki 356443fc820SRafael J. Wysocki error = acpi_bus_scan(adev->handle); 357443fc820SRafael J. Wysocki if (error) { 358443fc820SRafael J. Wysocki dev_warn(&adev->dev, "Namespace scan failure\n"); 359443fc820SRafael J. Wysocki return error; 360443fc820SRafael J. Wysocki } 361443fc820SRafael J. Wysocki list_for_each_entry(child, &adev->children, node) { 362443fc820SRafael J. Wysocki error = acpi_scan_bus_check(child); 363443fc820SRafael J. Wysocki if (error) 364443fc820SRafael J. Wysocki return error; 365a33ec399SRafael J. Wysocki } 366c27b2c33SRafael J. Wysocki return 0; 367c27b2c33SRafael J. Wysocki } 368c27b2c33SRafael J. Wysocki 3693c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type) 3703c2cc7ffSRafael J. Wysocki { 3713c2cc7ffSRafael J. Wysocki switch (type) { 3723c2cc7ffSRafael J. Wysocki case ACPI_NOTIFY_BUS_CHECK: 3733c2cc7ffSRafael J. Wysocki return acpi_scan_bus_check(adev); 3743c2cc7ffSRafael J. Wysocki case ACPI_NOTIFY_DEVICE_CHECK: 3753c2cc7ffSRafael J. Wysocki return acpi_scan_device_check(adev); 3763c2cc7ffSRafael J. Wysocki case ACPI_NOTIFY_EJECT_REQUEST: 3773c2cc7ffSRafael J. Wysocki case ACPI_OST_EC_OSPM_EJECT: 378dd2151beSRafael J. Wysocki if (adev->handler && !adev->handler->hotplug.enabled) { 379dd2151beSRafael J. Wysocki dev_info(&adev->dev, "Eject disabled\n"); 380dd2151beSRafael J. Wysocki return -EPERM; 381dd2151beSRafael J. Wysocki } 382700b8422SRafael J. Wysocki acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST, 383dd2151beSRafael J. Wysocki ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); 3843c2cc7ffSRafael J. Wysocki return acpi_scan_hot_remove(adev); 3853c2cc7ffSRafael J. Wysocki } 3863c2cc7ffSRafael J. Wysocki return -EINVAL; 3873c2cc7ffSRafael J. Wysocki } 3883c2cc7ffSRafael J. Wysocki 3891e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src) 390c27b2c33SRafael J. Wysocki { 391c27b2c33SRafael J. Wysocki u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; 3923c2cc7ffSRafael J. Wysocki int error = -ENODEV; 393c27b2c33SRafael J. Wysocki 394c27b2c33SRafael J. Wysocki lock_device_hotplug(); 395c27b2c33SRafael J. Wysocki mutex_lock(&acpi_scan_lock); 396c27b2c33SRafael J. Wysocki 397c27b2c33SRafael J. Wysocki /* 398c27b2c33SRafael J. Wysocki * The device object's ACPI handle cannot become invalid as long as we 3993c2cc7ffSRafael J. Wysocki * are holding acpi_scan_lock, but it might have become invalid before 400c27b2c33SRafael J. Wysocki * that lock was acquired. 401c27b2c33SRafael J. Wysocki */ 402c27b2c33SRafael J. Wysocki if (adev->handle == INVALID_ACPI_HANDLE) 4033c2cc7ffSRafael J. Wysocki goto err_out; 404c27b2c33SRafael J. Wysocki 4051e2380cdSRafael J. Wysocki if (adev->flags.is_dock_station) { 4061e2380cdSRafael J. Wysocki error = dock_notify(adev, src); 4071e2380cdSRafael J. Wysocki } else if (adev->flags.hotplug_notify) { 4083c2cc7ffSRafael J. Wysocki error = acpi_generic_hotplug_event(adev, src); 4093c2cc7ffSRafael J. Wysocki } else { 410be27b3dcSRafael J. Wysocki int (*notify)(struct acpi_device *, u32); 4113c2cc7ffSRafael J. Wysocki 4123c2cc7ffSRafael J. Wysocki acpi_lock_hp_context(); 413be27b3dcSRafael J. Wysocki notify = adev->hp ? adev->hp->notify : NULL; 4143c2cc7ffSRafael J. Wysocki acpi_unlock_hp_context(); 4153c2cc7ffSRafael J. Wysocki /* 4163c2cc7ffSRafael J. Wysocki * There may be additional notify handlers for device objects 4173c2cc7ffSRafael J. Wysocki * without the .event() callback, so ignore them here. 4183c2cc7ffSRafael J. Wysocki */ 419be27b3dcSRafael J. Wysocki if (notify) 420be27b3dcSRafael J. Wysocki error = notify(adev, src); 4213c2cc7ffSRafael J. Wysocki else 4223c2cc7ffSRafael J. Wysocki goto out; 423c27b2c33SRafael J. Wysocki } 424d429e5c1SLee, Chun-Yi switch (error) { 425d429e5c1SLee, Chun-Yi case 0: 426a33ec399SRafael J. Wysocki ost_code = ACPI_OST_SC_SUCCESS; 427d429e5c1SLee, Chun-Yi break; 428d429e5c1SLee, Chun-Yi case -EPERM: 429d429e5c1SLee, Chun-Yi ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED; 430d429e5c1SLee, Chun-Yi break; 431d429e5c1SLee, Chun-Yi case -EBUSY: 432d429e5c1SLee, Chun-Yi ost_code = ACPI_OST_SC_DEVICE_BUSY; 433d429e5c1SLee, Chun-Yi break; 434d429e5c1SLee, Chun-Yi default: 435d429e5c1SLee, Chun-Yi ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; 436d429e5c1SLee, Chun-Yi break; 437d429e5c1SLee, Chun-Yi } 438a33ec399SRafael J. Wysocki 4393c2cc7ffSRafael J. Wysocki err_out: 440700b8422SRafael J. Wysocki acpi_evaluate_ost(adev->handle, src, ost_code, NULL); 4413c2cc7ffSRafael J. Wysocki 4423c2cc7ffSRafael J. Wysocki out: 44378ea4639SRafael J. Wysocki acpi_bus_put_acpi_device(adev); 444a33ec399SRafael J. Wysocki mutex_unlock(&acpi_scan_lock); 445e0ae8feeSRafael J. Wysocki unlock_device_hotplug(); 446a33ec399SRafael J. Wysocki } 447a33ec399SRafael J. Wysocki 4480b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device) 4490b224527SRafael J. Wysocki { 4500b224527SRafael J. Wysocki int i; 4510b224527SRafael J. Wysocki 452993cbe59SRafael J. Wysocki if (device->wakeup.flags.valid) 453993cbe59SRafael J. Wysocki acpi_power_resources_list_free(&device->wakeup.resources); 454993cbe59SRafael J. Wysocki 4551b1f3e16SRafael J. Wysocki if (!device->power.flags.power_resources) 4560b224527SRafael J. Wysocki return; 4570b224527SRafael J. Wysocki 4580b224527SRafael J. Wysocki for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { 4590b224527SRafael J. Wysocki struct acpi_device_power_state *ps = &device->power.states[i]; 4600b224527SRafael J. Wysocki acpi_power_resources_list_free(&ps->resources); 4610b224527SRafael J. Wysocki } 4627f47fa6cSBjorn Helgaas } 4637f47fa6cSBjorn Helgaas 4641890a97aSPatrick Mochel static void acpi_device_release(struct device *dev) 4651890a97aSPatrick Mochel { 4661890a97aSPatrick Mochel struct acpi_device *acpi_dev = to_acpi_device(dev); 4671890a97aSPatrick Mochel 468ffdcd955SMika Westerberg acpi_free_properties(acpi_dev); 469c0af4175SToshi Kani acpi_free_pnp_ids(&acpi_dev->pnp); 4700b224527SRafael J. Wysocki acpi_free_power_resources_lists(acpi_dev); 4711890a97aSPatrick Mochel kfree(acpi_dev); 4721890a97aSPatrick Mochel } 4731890a97aSPatrick Mochel 474d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device) 475caf5c03fSRafael J. Wysocki { 476ca9dc8d4SLukas Wunner struct acpi_device_bus_id *acpi_device_bus_id; 477ca9dc8d4SLukas Wunner 478d783156eSRafael J. Wysocki mutex_lock(&acpi_device_lock); 479d783156eSRafael J. Wysocki if (device->parent) 480d783156eSRafael J. Wysocki list_del(&device->node); 481d783156eSRafael J. Wysocki 482ca9dc8d4SLukas Wunner list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) 483ca9dc8d4SLukas Wunner if (!strcmp(acpi_device_bus_id->bus_id, 484ca9dc8d4SLukas Wunner acpi_device_hid(device))) { 485ca9dc8d4SLukas Wunner if (acpi_device_bus_id->instance_no > 0) 486ca9dc8d4SLukas Wunner acpi_device_bus_id->instance_no--; 487ca9dc8d4SLukas Wunner else { 488ca9dc8d4SLukas Wunner list_del(&acpi_device_bus_id->node); 489a58015d6SDexuan Cui kfree_const(acpi_device_bus_id->bus_id); 490ca9dc8d4SLukas Wunner kfree(acpi_device_bus_id); 491ca9dc8d4SLukas Wunner } 492ca9dc8d4SLukas Wunner break; 493ca9dc8d4SLukas Wunner } 494ca9dc8d4SLukas Wunner 495d783156eSRafael J. Wysocki list_del(&device->wakeup_list); 496d783156eSRafael J. Wysocki mutex_unlock(&acpi_device_lock); 497d783156eSRafael J. Wysocki 498d783156eSRafael J. Wysocki acpi_power_add_remove_device(device, false); 499d783156eSRafael J. Wysocki acpi_device_remove_files(device); 500d783156eSRafael J. Wysocki if (device->remove) 501d783156eSRafael J. Wysocki device->remove(device); 502d783156eSRafael J. Wysocki 503d783156eSRafael J. Wysocki device_del(&device->dev); 504d783156eSRafael J. Wysocki } 505d783156eSRafael J. Wysocki 50668bdb677SOctavian Purdila static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain); 50768bdb677SOctavian Purdila 508d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list); 509d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock); 510d783156eSRafael J. Wysocki 511d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used) 512d783156eSRafael J. Wysocki { 513d783156eSRafael J. Wysocki for (;;) { 514d783156eSRafael J. Wysocki struct acpi_device *adev; 515d783156eSRafael J. Wysocki 516d783156eSRafael J. Wysocki mutex_lock(&acpi_device_del_lock); 517d783156eSRafael J. Wysocki 518d783156eSRafael J. Wysocki if (list_empty(&acpi_device_del_list)) { 519d783156eSRafael J. Wysocki mutex_unlock(&acpi_device_del_lock); 520d783156eSRafael J. Wysocki break; 521d783156eSRafael J. Wysocki } 522d783156eSRafael J. Wysocki adev = list_first_entry(&acpi_device_del_list, 523d783156eSRafael J. Wysocki struct acpi_device, del_list); 524d783156eSRafael J. Wysocki list_del(&adev->del_list); 525d783156eSRafael J. Wysocki 526d783156eSRafael J. Wysocki mutex_unlock(&acpi_device_del_lock); 527d783156eSRafael J. Wysocki 52868bdb677SOctavian Purdila blocking_notifier_call_chain(&acpi_reconfig_chain, 52968bdb677SOctavian Purdila ACPI_RECONFIG_DEVICE_REMOVE, adev); 53068bdb677SOctavian Purdila 531d783156eSRafael J. Wysocki acpi_device_del(adev); 532d783156eSRafael J. Wysocki /* 533d783156eSRafael J. Wysocki * Drop references to all power resources that might have been 534d783156eSRafael J. Wysocki * used by the device. 535d783156eSRafael J. Wysocki */ 536d783156eSRafael J. Wysocki acpi_power_transition(adev, ACPI_STATE_D3_COLD); 537d783156eSRafael J. Wysocki put_device(&adev->dev); 538d783156eSRafael J. Wysocki } 539d783156eSRafael J. Wysocki } 540d783156eSRafael J. Wysocki 541d783156eSRafael J. Wysocki /** 542d783156eSRafael J. Wysocki * acpi_scan_drop_device - Drop an ACPI device object. 543d783156eSRafael J. Wysocki * @handle: Handle of an ACPI namespace node, not used. 544d783156eSRafael J. Wysocki * @context: Address of the ACPI device object to drop. 545d783156eSRafael J. Wysocki * 546d783156eSRafael J. Wysocki * This is invoked by acpi_ns_delete_node() during the removal of the ACPI 547d783156eSRafael J. Wysocki * namespace node the device object pointed to by @context is attached to. 548d783156eSRafael J. Wysocki * 549d783156eSRafael J. Wysocki * The unregistration is carried out asynchronously to avoid running 550d783156eSRafael J. Wysocki * acpi_device_del() under the ACPICA's namespace mutex and the list is used to 551d783156eSRafael J. Wysocki * ensure the correct ordering (the device objects must be unregistered in the 552d783156eSRafael J. Wysocki * same order in which the corresponding namespace nodes are deleted). 553d783156eSRafael J. Wysocki */ 554d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context) 555d783156eSRafael J. Wysocki { 556d783156eSRafael J. Wysocki static DECLARE_WORK(work, acpi_device_del_work_fn); 557d783156eSRafael J. Wysocki struct acpi_device *adev = context; 558d783156eSRafael J. Wysocki 559d783156eSRafael J. Wysocki mutex_lock(&acpi_device_del_lock); 560d783156eSRafael J. Wysocki 561d783156eSRafael J. Wysocki /* 562d783156eSRafael J. Wysocki * Use the ACPI hotplug workqueue which is ordered, so this work item 563d783156eSRafael J. Wysocki * won't run after any hotplug work items submitted subsequently. That 564d783156eSRafael J. Wysocki * prevents attempts to register device objects identical to those being 565d783156eSRafael J. Wysocki * deleted from happening concurrently (such attempts result from 566d783156eSRafael J. Wysocki * hotplug events handled via the ACPI hotplug workqueue). It also will 567d783156eSRafael J. Wysocki * run after all of the work items submitted previosuly, which helps 568d783156eSRafael J. Wysocki * those work items to ensure that they are not accessing stale device 569d783156eSRafael J. Wysocki * objects. 570d783156eSRafael J. Wysocki */ 571d783156eSRafael J. Wysocki if (list_empty(&acpi_device_del_list)) 572d783156eSRafael J. Wysocki acpi_queue_hotplug_work(&work); 573d783156eSRafael J. Wysocki 574d783156eSRafael J. Wysocki list_add_tail(&adev->del_list, &acpi_device_del_list); 575d783156eSRafael J. Wysocki /* Make acpi_ns_validate_handle() return NULL for this handle. */ 576d783156eSRafael J. Wysocki adev->handle = INVALID_ACPI_HANDLE; 577d783156eSRafael J. Wysocki 578d783156eSRafael J. Wysocki mutex_unlock(&acpi_device_del_lock); 579caf5c03fSRafael J. Wysocki } 580caf5c03fSRafael J. Wysocki 58178ea4639SRafael J. Wysocki static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device, 58278ea4639SRafael J. Wysocki void (*callback)(void *)) 583caf5c03fSRafael J. Wysocki { 584caf5c03fSRafael J. Wysocki acpi_status status; 585caf5c03fSRafael J. Wysocki 586caf5c03fSRafael J. Wysocki if (!device) 587caf5c03fSRafael J. Wysocki return -EINVAL; 588caf5c03fSRafael J. Wysocki 58978a18fecSHans de Goede *device = NULL; 59078a18fecSHans de Goede 59178ea4639SRafael J. Wysocki status = acpi_get_data_full(handle, acpi_scan_drop_device, 59278ea4639SRafael J. Wysocki (void **)device, callback); 593caf5c03fSRafael J. Wysocki if (ACPI_FAILURE(status) || !*device) { 594caf5c03fSRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n", 595caf5c03fSRafael J. Wysocki handle)); 596caf5c03fSRafael J. Wysocki return -ENODEV; 597caf5c03fSRafael J. Wysocki } 598caf5c03fSRafael J. Wysocki return 0; 599caf5c03fSRafael J. Wysocki } 60078ea4639SRafael J. Wysocki 60178ea4639SRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device) 60278ea4639SRafael J. Wysocki { 60378ea4639SRafael J. Wysocki return acpi_get_device_data(handle, device, NULL); 60478ea4639SRafael J. Wysocki } 6056585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device); 606caf5c03fSRafael J. Wysocki 60778ea4639SRafael J. Wysocki static void get_acpi_device(void *dev) 60878ea4639SRafael J. Wysocki { 60978ea4639SRafael J. Wysocki if (dev) 61078ea4639SRafael J. Wysocki get_device(&((struct acpi_device *)dev)->dev); 61178ea4639SRafael J. Wysocki } 61278ea4639SRafael J. Wysocki 61378ea4639SRafael J. Wysocki struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle) 61478ea4639SRafael J. Wysocki { 61578ea4639SRafael J. Wysocki struct acpi_device *adev = NULL; 61678ea4639SRafael J. Wysocki 61778ea4639SRafael J. Wysocki acpi_get_device_data(handle, &adev, get_acpi_device); 61878ea4639SRafael J. Wysocki return adev; 61978ea4639SRafael J. Wysocki } 62078ea4639SRafael J. Wysocki 62178ea4639SRafael J. Wysocki void acpi_bus_put_acpi_device(struct acpi_device *adev) 62278ea4639SRafael J. Wysocki { 62378ea4639SRafael J. Wysocki put_device(&adev->dev); 62478ea4639SRafael J. Wysocki } 62578ea4639SRafael J. Wysocki 626*c1013ff7SRafael J. Wysocki static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id) 627*c1013ff7SRafael J. Wysocki { 628*c1013ff7SRafael J. Wysocki struct acpi_device_bus_id *acpi_device_bus_id; 629*c1013ff7SRafael J. Wysocki 630*c1013ff7SRafael J. Wysocki /* Find suitable bus_id and instance number in acpi_bus_id_list. */ 631*c1013ff7SRafael J. Wysocki list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { 632*c1013ff7SRafael J. Wysocki if (!strcmp(acpi_device_bus_id->bus_id, dev_id)) 633*c1013ff7SRafael J. Wysocki return acpi_device_bus_id; 634*c1013ff7SRafael J. Wysocki } 635*c1013ff7SRafael J. Wysocki return NULL; 636*c1013ff7SRafael J. Wysocki } 637*c1013ff7SRafael J. Wysocki 638cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device, 63982c7d5efSRafael J. Wysocki void (*release)(struct device *)) 6401da177e4SLinus Torvalds { 641*c1013ff7SRafael J. Wysocki struct acpi_device_bus_id *acpi_device_bus_id; 642e49bd2ddSZhang Rui int result; 64366b7ed40SBjorn Helgaas 644d43e167dSRafael J. Wysocki if (device->handle) { 645d43e167dSRafael J. Wysocki acpi_status status; 646d43e167dSRafael J. Wysocki 647d783156eSRafael J. Wysocki status = acpi_attach_data(device->handle, acpi_scan_drop_device, 648d43e167dSRafael J. Wysocki device); 649d43e167dSRafael J. Wysocki if (ACPI_FAILURE(status)) { 650d43e167dSRafael J. Wysocki acpi_handle_err(device->handle, 651d43e167dSRafael J. Wysocki "Unable to attach device data\n"); 652d43e167dSRafael J. Wysocki return -ENODEV; 653d43e167dSRafael J. Wysocki } 654d43e167dSRafael J. Wysocki } 655d43e167dSRafael J. Wysocki 6561da177e4SLinus Torvalds /* 6571da177e4SLinus Torvalds * Linkage 6581da177e4SLinus Torvalds * ------- 6591da177e4SLinus Torvalds * Link this device to its parent and siblings. 6601da177e4SLinus Torvalds */ 6611da177e4SLinus Torvalds INIT_LIST_HEAD(&device->children); 6621da177e4SLinus Torvalds INIT_LIST_HEAD(&device->node); 6631da177e4SLinus Torvalds INIT_LIST_HEAD(&device->wakeup_list); 6641033f904SLan Tianyu INIT_LIST_HEAD(&device->physical_node_list); 665d783156eSRafael J. Wysocki INIT_LIST_HEAD(&device->del_list); 6661033f904SLan Tianyu mutex_init(&device->physical_node_lock); 6671da177e4SLinus Torvalds 6689090589dSShaohua Li mutex_lock(&acpi_device_lock); 669*c1013ff7SRafael J. Wysocki 670*c1013ff7SRafael J. Wysocki acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device)); 671*c1013ff7SRafael J. Wysocki if (acpi_device_bus_id) { 672e49bd2ddSZhang Rui acpi_device_bus_id->instance_no++; 673*c1013ff7SRafael J. Wysocki } else { 674*c1013ff7SRafael J. Wysocki acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id), 675*c1013ff7SRafael J. Wysocki GFP_KERNEL); 676*c1013ff7SRafael J. Wysocki if (!acpi_device_bus_id) { 677*c1013ff7SRafael J. Wysocki result = -ENOMEM; 678*c1013ff7SRafael J. Wysocki goto err_unlock; 679e49bd2ddSZhang Rui } 680a58015d6SDexuan Cui acpi_device_bus_id->bus_id = 681a58015d6SDexuan Cui kstrdup_const(acpi_device_hid(device), GFP_KERNEL); 682a58015d6SDexuan Cui if (!acpi_device_bus_id->bus_id) { 683*c1013ff7SRafael J. Wysocki kfree(acpi_device_bus_id); 684a58015d6SDexuan Cui result = -ENOMEM; 685*c1013ff7SRafael J. Wysocki goto err_unlock; 686a58015d6SDexuan Cui } 687a58015d6SDexuan Cui 688e49bd2ddSZhang Rui list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); 689e49bd2ddSZhang Rui } 6900794469dSKay Sievers dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); 691e49bd2ddSZhang Rui 69233b57150SLen Brown if (device->parent) 6931da177e4SLinus Torvalds list_add_tail(&device->node, &device->parent->children); 69433b57150SLen Brown 6951da177e4SLinus Torvalds if (device->wakeup.flags.valid) 6961da177e4SLinus Torvalds list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); 6979090589dSShaohua Li mutex_unlock(&acpi_device_lock); 6981da177e4SLinus Torvalds 6991890a97aSPatrick Mochel if (device->parent) 70066b7ed40SBjorn Helgaas device->dev.parent = &device->parent->dev; 7011890a97aSPatrick Mochel device->dev.bus = &acpi_bus_type; 70282c7d5efSRafael J. Wysocki device->dev.release = release; 703cf860be6SRafael J. Wysocki result = device_add(&device->dev); 704e49bd2ddSZhang Rui if (result) { 7058b12b922SAlex Chiang dev_err(&device->dev, "Error registering device\n"); 706d43e167dSRafael J. Wysocki goto err; 707e49bd2ddSZhang Rui } 708f883d9dbSPatrick Mochel 709e49bd2ddSZhang Rui result = acpi_device_setup_files(device); 710e49bd2ddSZhang Rui if (result) 7110794469dSKay Sievers printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n", 7120794469dSKay Sievers dev_name(&device->dev)); 713e49bd2ddSZhang Rui 714e49bd2ddSZhang Rui return 0; 715d43e167dSRafael J. Wysocki 716d43e167dSRafael J. Wysocki err: 7179090589dSShaohua Li mutex_lock(&acpi_device_lock); 71833b57150SLen Brown if (device->parent) 719e49bd2ddSZhang Rui list_del(&device->node); 720e49bd2ddSZhang Rui list_del(&device->wakeup_list); 721a58015d6SDexuan Cui 722*c1013ff7SRafael J. Wysocki err_unlock: 7239090589dSShaohua Li mutex_unlock(&acpi_device_lock); 724d43e167dSRafael J. Wysocki 725d783156eSRafael J. Wysocki acpi_detach_data(device->handle, acpi_scan_drop_device); 726e49bd2ddSZhang Rui return result; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7299e89dde2SZhang Rui /* -------------------------------------------------------------------------- 7301da177e4SLinus Torvalds Device Enumeration 7311da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 7322ef33ee7SRafael J. Wysocki static bool acpi_info_matches_ids(struct acpi_device_info *info, 7332ef33ee7SRafael J. Wysocki const char * const ids[]) 734637b9f1aSHans de Goede { 7352ef33ee7SRafael J. Wysocki struct acpi_pnp_device_id_list *cid_list = NULL; 736637b9f1aSHans de Goede int i; 737637b9f1aSHans de Goede 738637b9f1aSHans de Goede if (!(info->valid & ACPI_VALID_HID)) 739637b9f1aSHans de Goede return false; 740637b9f1aSHans de Goede 7412ef33ee7SRafael J. Wysocki if (info->valid & ACPI_VALID_CID) 7422ef33ee7SRafael J. Wysocki cid_list = &info->compatible_id_list; 7432ef33ee7SRafael J. Wysocki 7442ef33ee7SRafael J. Wysocki for (i = 0; ids[i]; i++) { 7452ef33ee7SRafael J. Wysocki int j; 7462ef33ee7SRafael J. Wysocki 7472ef33ee7SRafael J. Wysocki if (!strcmp(info->hardware_id.string, ids[i])) 748637b9f1aSHans de Goede return true; 7492ef33ee7SRafael J. Wysocki 7502ef33ee7SRafael J. Wysocki if (!cid_list) 7512ef33ee7SRafael J. Wysocki continue; 7522ef33ee7SRafael J. Wysocki 7532ef33ee7SRafael J. Wysocki for (j = 0; j < cid_list->count; j++) { 7542ef33ee7SRafael J. Wysocki if (!strcmp(cid_list->ids[j].string, ids[i])) 7552ef33ee7SRafael J. Wysocki return true; 7562ef33ee7SRafael J. Wysocki } 757637b9f1aSHans de Goede } 758637b9f1aSHans de Goede 759637b9f1aSHans de Goede return false; 760637b9f1aSHans de Goede } 761637b9f1aSHans de Goede 762637b9f1aSHans de Goede /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */ 7632ef33ee7SRafael J. Wysocki static const char * const acpi_ignore_dep_ids[] = { 7642ef33ee7SRafael J. Wysocki "PNP0D80", /* Windows-compatible System Power Management Controller */ 7659272e97aSHans de Goede "INT33BD", /* Intel Baytrail Mailbox Device */ 766637b9f1aSHans de Goede NULL 767637b9f1aSHans de Goede }; 768637b9f1aSHans de Goede 7695c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle) 7705c478f49SBjorn Helgaas { 771456de893SRafael J. Wysocki struct acpi_device *device = NULL; 7725c478f49SBjorn Helgaas acpi_status status; 7735c478f49SBjorn Helgaas 7745c478f49SBjorn Helgaas /* 7755c478f49SBjorn Helgaas * Fixed hardware devices do not appear in the namespace and do not 7765c478f49SBjorn Helgaas * have handles, but we fabricate acpi_devices for them, so we have 7775c478f49SBjorn Helgaas * to deal with them specially. 7785c478f49SBjorn Helgaas */ 779456de893SRafael J. Wysocki if (!handle) 7805c478f49SBjorn Helgaas return acpi_root; 7815c478f49SBjorn Helgaas 7825c478f49SBjorn Helgaas do { 7835c478f49SBjorn Helgaas status = acpi_get_parent(handle, &handle); 7845c478f49SBjorn Helgaas if (ACPI_FAILURE(status)) 785456de893SRafael J. Wysocki return status == AE_NULL_ENTRY ? NULL : acpi_root; 786456de893SRafael J. Wysocki } while (acpi_bus_get_device(handle, &device)); 7875c478f49SBjorn Helgaas return device; 7885c478f49SBjorn Helgaas } 7895c478f49SBjorn Helgaas 790c8f7a62cSLen Brown acpi_status 791c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) 792c8f7a62cSLen Brown { 793c8f7a62cSLen Brown acpi_status status; 794c8f7a62cSLen Brown acpi_handle tmp; 795c8f7a62cSLen Brown struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 796c8f7a62cSLen Brown union acpi_object *obj; 797c8f7a62cSLen Brown 798c8f7a62cSLen Brown status = acpi_get_handle(handle, "_EJD", &tmp); 799c8f7a62cSLen Brown if (ACPI_FAILURE(status)) 800c8f7a62cSLen Brown return status; 801c8f7a62cSLen Brown 802c8f7a62cSLen Brown status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); 803c8f7a62cSLen Brown if (ACPI_SUCCESS(status)) { 804c8f7a62cSLen Brown obj = buffer.pointer; 8053b5fee59SHolger Macht status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, 8063b5fee59SHolger Macht ejd); 807c8f7a62cSLen Brown kfree(buffer.pointer); 808c8f7a62cSLen Brown } 809c8f7a62cSLen Brown return status; 810c8f7a62cSLen Brown } 811c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); 812c8f7a62cSLen Brown 81340381a3cSBjorn Helgaas static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev) 8141da177e4SLinus Torvalds { 81540381a3cSBjorn Helgaas acpi_handle handle = dev->handle; 81640381a3cSBjorn Helgaas struct acpi_device_wakeup *wakeup = &dev->wakeup; 817b581a7f9SRafael J. Wysocki struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 818b581a7f9SRafael J. Wysocki union acpi_object *package = NULL; 8191da177e4SLinus Torvalds union acpi_object *element = NULL; 820b581a7f9SRafael J. Wysocki acpi_status status; 821e88c9c60SRafael J. Wysocki int err = -ENODATA; 8221da177e4SLinus Torvalds 823993cbe59SRafael J. Wysocki INIT_LIST_HEAD(&wakeup->resources); 8241da177e4SLinus Torvalds 825b581a7f9SRafael J. Wysocki /* _PRW */ 826b581a7f9SRafael J. Wysocki status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); 827b581a7f9SRafael J. Wysocki if (ACPI_FAILURE(status)) { 828b581a7f9SRafael J. Wysocki ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); 829e88c9c60SRafael J. Wysocki return err; 830b581a7f9SRafael J. Wysocki } 831b581a7f9SRafael J. Wysocki 832b581a7f9SRafael J. Wysocki package = (union acpi_object *)buffer.pointer; 833b581a7f9SRafael J. Wysocki 834e88c9c60SRafael J. Wysocki if (!package || package->package.count < 2) 835b581a7f9SRafael J. Wysocki goto out; 836b581a7f9SRafael J. Wysocki 8371da177e4SLinus Torvalds element = &(package->package.elements[0]); 838e88c9c60SRafael J. Wysocki if (!element) 839b581a7f9SRafael J. Wysocki goto out; 840e88c9c60SRafael J. Wysocki 8411da177e4SLinus Torvalds if (element->type == ACPI_TYPE_PACKAGE) { 8421da177e4SLinus Torvalds if ((element->package.count < 2) || 8431da177e4SLinus Torvalds (element->package.elements[0].type != 8441da177e4SLinus Torvalds ACPI_TYPE_LOCAL_REFERENCE) 845e88c9c60SRafael J. Wysocki || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) 846b581a7f9SRafael J. Wysocki goto out; 847e88c9c60SRafael J. Wysocki 848b581a7f9SRafael J. Wysocki wakeup->gpe_device = 8491da177e4SLinus Torvalds element->package.elements[0].reference.handle; 850b581a7f9SRafael J. Wysocki wakeup->gpe_number = 8511da177e4SLinus Torvalds (u32) element->package.elements[1].integer.value; 8521da177e4SLinus Torvalds } else if (element->type == ACPI_TYPE_INTEGER) { 853b581a7f9SRafael J. Wysocki wakeup->gpe_device = NULL; 854b581a7f9SRafael J. Wysocki wakeup->gpe_number = element->integer.value; 855b581a7f9SRafael J. Wysocki } else { 856b581a7f9SRafael J. Wysocki goto out; 857b581a7f9SRafael J. Wysocki } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds element = &(package->package.elements[1]); 860e88c9c60SRafael J. Wysocki if (element->type != ACPI_TYPE_INTEGER) 861b581a7f9SRafael J. Wysocki goto out; 862e88c9c60SRafael J. Wysocki 863b581a7f9SRafael J. Wysocki wakeup->sleep_state = element->integer.value; 8641da177e4SLinus Torvalds 865e88c9c60SRafael J. Wysocki err = acpi_extract_power_resources(package, 2, &wakeup->resources); 866e88c9c60SRafael J. Wysocki if (err) 867b581a7f9SRafael J. Wysocki goto out; 8681da177e4SLinus Torvalds 8690596a52bSRafael J. Wysocki if (!list_empty(&wakeup->resources)) { 8700596a52bSRafael J. Wysocki int sleep_state; 8719874647bSRafael J. Wysocki 872b5d667ebSRafael J. Wysocki err = acpi_power_wakeup_list_init(&wakeup->resources, 873b5d667ebSRafael J. Wysocki &sleep_state); 874b5d667ebSRafael J. Wysocki if (err) { 875b5d667ebSRafael J. Wysocki acpi_handle_warn(handle, "Retrieving current states " 876b5d667ebSRafael J. Wysocki "of wakeup power resources failed\n"); 877b5d667ebSRafael J. Wysocki acpi_power_resources_list_free(&wakeup->resources); 878b5d667ebSRafael J. Wysocki goto out; 879b5d667ebSRafael J. Wysocki } 8800596a52bSRafael J. Wysocki if (sleep_state < wakeup->sleep_state) { 8810596a52bSRafael J. Wysocki acpi_handle_warn(handle, "Overriding _PRW sleep state " 8820596a52bSRafael J. Wysocki "(S%d) by S%d from power resources\n", 8830596a52bSRafael J. Wysocki (int)wakeup->sleep_state, sleep_state); 8840596a52bSRafael J. Wysocki wakeup->sleep_state = sleep_state; 8850596a52bSRafael J. Wysocki } 8860596a52bSRafael J. Wysocki } 887b581a7f9SRafael J. Wysocki 888b581a7f9SRafael J. Wysocki out: 889b581a7f9SRafael J. Wysocki kfree(buffer.pointer); 890e88c9c60SRafael J. Wysocki return err; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 893a1a66393SRafael J. Wysocki static bool acpi_wakeup_gpe_init(struct acpi_device *device) 8941da177e4SLinus Torvalds { 8950519ade7SMathias Krause static const struct acpi_device_id button_device_ids[] = { 8965ceb5f05SBjorn Helgaas {"PNP0C0C", 0}, /* Power button */ 8975ceb5f05SBjorn Helgaas {"PNP0C0D", 0}, /* Lid */ 8985ceb5f05SBjorn Helgaas {"PNP0C0E", 0}, /* Sleep button */ 89929b71a1cSThomas Renninger {"", 0}, 90029b71a1cSThomas Renninger }; 901bd9b2f9aSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &device->wakeup; 902f517709dSRafael J. Wysocki acpi_status status; 903f517709dSRafael J. Wysocki 904bd9b2f9aSRafael J. Wysocki wakeup->flags.notifier_present = 0; 905f517709dSRafael J. Wysocki 906f517709dSRafael J. Wysocki /* Power button, Lid switch always enable wakeup */ 907f517709dSRafael J. Wysocki if (!acpi_match_device_ids(device, button_device_ids)) { 908b7e38304SZhang Rui if (!acpi_match_device_ids(device, &button_device_ids[1])) { 909b7e38304SZhang Rui /* Do not use Lid/sleep button for S5 wakeup */ 910bd9b2f9aSRafael J. Wysocki if (wakeup->sleep_state == ACPI_STATE_S5) 911bd9b2f9aSRafael J. Wysocki wakeup->sleep_state = ACPI_STATE_S4; 912b7e38304SZhang Rui } 913bd9b2f9aSRafael J. Wysocki acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number); 914f2b56bc8SRafael J. Wysocki device_set_wakeup_capable(&device->dev, true); 915a1a66393SRafael J. Wysocki return true; 916f517709dSRafael J. Wysocki } 917f517709dSRafael J. Wysocki 918a1a66393SRafael J. Wysocki status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device, 919bd9b2f9aSRafael J. Wysocki wakeup->gpe_number); 920a1a66393SRafael J. Wysocki return ACPI_SUCCESS(status); 921f517709dSRafael J. Wysocki } 922f517709dSRafael J. Wysocki 923d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) 924f517709dSRafael J. Wysocki { 925e88c9c60SRafael J. Wysocki int err; 92629b71a1cSThomas Renninger 927d57d09a4SRafael J. Wysocki /* Presence of _PRW indicates wake capable */ 928952c63e9SJiang Liu if (!acpi_has_method(device->handle, "_PRW")) 929d57d09a4SRafael J. Wysocki return; 930d57d09a4SRafael J. Wysocki 93140381a3cSBjorn Helgaas err = acpi_bus_extract_wakeup_device_power_package(device); 932e88c9c60SRafael J. Wysocki if (err) { 933e88c9c60SRafael J. Wysocki dev_err(&device->dev, "_PRW evaluation error: %d\n", err); 934d57d09a4SRafael J. Wysocki return; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 937a1a66393SRafael J. Wysocki device->wakeup.flags.valid = acpi_wakeup_gpe_init(device); 9389b83ccd2SRafael J. Wysocki device->wakeup.prepare_count = 0; 939a1a66393SRafael J. Wysocki /* 940a1a66393SRafael J. Wysocki * Call _PSW/_DSW object to disable its ability to wake the sleeping 941729b2bdbSZhao Yakui * system for the ACPI device with the _PRW object. 942603fadf3SBjorn Helgaas * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW. 943729b2bdbSZhao Yakui * So it is necessary to call _DSW object first. Only when it is not 944729b2bdbSZhao Yakui * present will the _PSW object used. 945729b2bdbSZhao Yakui */ 946e88c9c60SRafael J. Wysocki err = acpi_device_sleep_wake(device, 0, 0, 0); 947e88c9c60SRafael J. Wysocki if (err) 94805de0686STian Tao pr_debug("error in _DSW or _PSW evaluation\n"); 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 951f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state) 9521da177e4SLinus Torvalds { 953f33ce568SRafael J. Wysocki struct acpi_device_power_state *ps = &device->power.states[state]; 954ef85bdbeSRafael J. Wysocki char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' }; 955ef85bdbeSRafael J. Wysocki struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 956f33ce568SRafael J. Wysocki acpi_status status; 9579e89dde2SZhang Rui 958f33ce568SRafael J. Wysocki INIT_LIST_HEAD(&ps->resources); 959f33ce568SRafael J. Wysocki 960ef85bdbeSRafael J. Wysocki /* Evaluate "_PRx" to get referenced power resources */ 961ef85bdbeSRafael J. Wysocki status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer); 962ef85bdbeSRafael J. Wysocki if (ACPI_SUCCESS(status)) { 963ef85bdbeSRafael J. Wysocki union acpi_object *package = buffer.pointer; 964f33ce568SRafael J. Wysocki 965ef85bdbeSRafael J. Wysocki if (buffer.length && package 966ef85bdbeSRafael J. Wysocki && package->type == ACPI_TYPE_PACKAGE 967956ad9d9SRafael J. Wysocki && package->package.count) 968956ad9d9SRafael J. Wysocki acpi_extract_power_resources(package, 0, &ps->resources); 969956ad9d9SRafael J. Wysocki 970ef85bdbeSRafael J. Wysocki ACPI_FREE(buffer.pointer); 971f33ce568SRafael J. Wysocki } 972f33ce568SRafael J. Wysocki 973f33ce568SRafael J. Wysocki /* Evaluate "_PSx" to see if we can do explicit sets */ 974ef85bdbeSRafael J. Wysocki pathname[2] = 'S'; 975952c63e9SJiang Liu if (acpi_has_method(device->handle, pathname)) 976f33ce568SRafael J. Wysocki ps->flags.explicit_set = 1; 977f33ce568SRafael J. Wysocki 97820dacb71SRafael J. Wysocki /* State is valid if there are means to put the device into it. */ 97920dacb71SRafael J. Wysocki if (!list_empty(&ps->resources) || ps->flags.explicit_set) 980f33ce568SRafael J. Wysocki ps->flags.valid = 1; 981f33ce568SRafael J. Wysocki 982f33ce568SRafael J. Wysocki ps->power = -1; /* Unknown - driver assigned */ 983f33ce568SRafael J. Wysocki ps->latency = -1; /* Unknown - driver assigned */ 984f33ce568SRafael J. Wysocki } 985f33ce568SRafael J. Wysocki 986d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device) 9871da177e4SLinus Torvalds { 9888bc5053bSRafael J. Wysocki u32 i; 9891da177e4SLinus Torvalds 990d43e167dSRafael J. Wysocki /* Presence of _PS0|_PR0 indicates 'power manageable' */ 991952c63e9SJiang Liu if (!acpi_has_method(device->handle, "_PS0") && 992952c63e9SJiang Liu !acpi_has_method(device->handle, "_PR0")) 993d43e167dSRafael J. Wysocki return; 994d43e167dSRafael J. Wysocki 995d43e167dSRafael J. Wysocki device->flags.power_manageable = 1; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds /* 9989e89dde2SZhang Rui * Power Management Flags 9991da177e4SLinus Torvalds */ 1000952c63e9SJiang Liu if (acpi_has_method(device->handle, "_PSC")) 10019e89dde2SZhang Rui device->power.flags.explicit_get = 1; 1002f25c0ae2SRafael J. Wysocki 1003952c63e9SJiang Liu if (acpi_has_method(device->handle, "_IRC")) 10049e89dde2SZhang Rui device->power.flags.inrush_current = 1; 10051da177e4SLinus Torvalds 1006f25c0ae2SRafael J. Wysocki if (acpi_has_method(device->handle, "_DSW")) 1007f25c0ae2SRafael J. Wysocki device->power.flags.dsw_present = 1; 1008f25c0ae2SRafael J. Wysocki 10091da177e4SLinus Torvalds /* 10109e89dde2SZhang Rui * Enumerate supported power management states 10111da177e4SLinus Torvalds */ 1012f33ce568SRafael J. Wysocki for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) 1013f33ce568SRafael J. Wysocki acpi_bus_init_power_state(device, i); 10141da177e4SLinus Torvalds 10150b224527SRafael J. Wysocki INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources); 10169e89dde2SZhang Rui 1017956ad9d9SRafael J. Wysocki /* Set the defaults for D0 and D3hot (always supported). */ 10189e89dde2SZhang Rui device->power.states[ACPI_STATE_D0].flags.valid = 1; 10199e89dde2SZhang Rui device->power.states[ACPI_STATE_D0].power = 100; 102020dacb71SRafael J. Wysocki device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1; 10211399dfcdSAaron Lu 1022956ad9d9SRafael J. Wysocki /* 1023956ad9d9SRafael J. Wysocki * Use power resources only if the D0 list of them is populated, because 1024956ad9d9SRafael J. Wysocki * some platforms may provide _PR3 only to indicate D3cold support and 1025956ad9d9SRafael J. Wysocki * in those cases the power resources list returned by it may be bogus. 1026956ad9d9SRafael J. Wysocki */ 1027956ad9d9SRafael J. Wysocki if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) { 1028956ad9d9SRafael J. Wysocki device->power.flags.power_resources = 1; 1029956ad9d9SRafael J. Wysocki /* 1030956ad9d9SRafael J. Wysocki * D3cold is supported if the D3hot list of power resources is 1031956ad9d9SRafael J. Wysocki * not empty. 1032956ad9d9SRafael J. Wysocki */ 1033956ad9d9SRafael J. Wysocki if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources)) 1034956ad9d9SRafael J. Wysocki device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1; 1035956ad9d9SRafael J. Wysocki } 1036956ad9d9SRafael J. Wysocki 10371b1f3e16SRafael J. Wysocki if (acpi_bus_init_power(device)) 1038b3785492SRafael J. Wysocki device->flags.power_manageable = 0; 1039b3785492SRafael J. Wysocki } 1040c8f7a62cSLen Brown 1041d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device) 10421da177e4SLinus Torvalds { 10431da177e4SLinus Torvalds /* Presence of _STA indicates 'dynamic_status' */ 1044952c63e9SJiang Liu if (acpi_has_method(device->handle, "_STA")) 10451da177e4SLinus Torvalds device->flags.dynamic_status = 1; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* Presence of _RMV indicates 'removable' */ 1048952c63e9SJiang Liu if (acpi_has_method(device->handle, "_RMV")) 10491da177e4SLinus Torvalds device->flags.removable = 1; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds /* Presence of _EJD|_EJ0 indicates 'ejectable' */ 1052952c63e9SJiang Liu if (acpi_has_method(device->handle, "_EJD") || 1053952c63e9SJiang Liu acpi_has_method(device->handle, "_EJ0")) 10541da177e4SLinus Torvalds device->flags.ejectable = 1; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 1057c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device) 10581da177e4SLinus Torvalds { 10591da177e4SLinus Torvalds char bus_id[5] = { '?', 0 }; 10601da177e4SLinus Torvalds struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; 10611da177e4SLinus Torvalds int i = 0; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds /* 10641da177e4SLinus Torvalds * Bus ID 10651da177e4SLinus Torvalds * ------ 10661da177e4SLinus Torvalds * The device's Bus ID is simply the object name. 10671da177e4SLinus Torvalds * TBD: Shouldn't this value be unique (within the ACPI namespace)? 10681da177e4SLinus Torvalds */ 1069859ac9a4SBjorn Helgaas if (ACPI_IS_ROOT_DEVICE(device)) { 10701da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "ACPI"); 1071859ac9a4SBjorn Helgaas return; 1072859ac9a4SBjorn Helgaas } 1073859ac9a4SBjorn Helgaas 1074859ac9a4SBjorn Helgaas switch (device->device_type) { 10751da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 10761da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "PWRF"); 10771da177e4SLinus Torvalds break; 10781da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 10791da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "SLPF"); 10801da177e4SLinus Torvalds break; 1081a64a62ceSLv Zheng case ACPI_BUS_TYPE_ECDT_EC: 1082a64a62ceSLv Zheng strcpy(device->pnp.bus_id, "ECDT"); 1083a64a62ceSLv Zheng break; 10841da177e4SLinus Torvalds default: 108566b7ed40SBjorn Helgaas acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer); 10861da177e4SLinus Torvalds /* Clean up trailing underscores (if any) */ 10871da177e4SLinus Torvalds for (i = 3; i > 1; i--) { 10881da177e4SLinus Torvalds if (bus_id[i] == '_') 10891da177e4SLinus Torvalds bus_id[i] = '\0'; 10901da177e4SLinus Torvalds else 10911da177e4SLinus Torvalds break; 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds strcpy(device->pnp.bus_id, bus_id); 10941da177e4SLinus Torvalds break; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 109854735266SZhang Rui /* 1099ebf4df8dSJiang Liu * acpi_ata_match - see if an acpi object is an ATA device 1100ebf4df8dSJiang Liu * 1101ebf4df8dSJiang Liu * If an acpi object has one of the ACPI ATA methods defined, 1102ebf4df8dSJiang Liu * then we can safely call it an ATA device. 1103ebf4df8dSJiang Liu */ 1104ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle) 1105ebf4df8dSJiang Liu { 1106ebf4df8dSJiang Liu return acpi_has_method(handle, "_GTF") || 1107ebf4df8dSJiang Liu acpi_has_method(handle, "_GTM") || 1108ebf4df8dSJiang Liu acpi_has_method(handle, "_STM") || 1109ebf4df8dSJiang Liu acpi_has_method(handle, "_SDD"); 1110ebf4df8dSJiang Liu } 1111ebf4df8dSJiang Liu 1112ebf4df8dSJiang Liu /* 1113d4e1a692SToshi Kani * acpi_bay_match - see if an acpi object is an ejectable driver bay 111454735266SZhang Rui * 111554735266SZhang Rui * If an acpi object is ejectable and has one of the ACPI ATA methods defined, 111654735266SZhang Rui * then we can safely call it an ejectable drive bay 111754735266SZhang Rui */ 1118ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle) 1119d4e1a692SToshi Kani { 112054735266SZhang Rui acpi_handle phandle; 112154735266SZhang Rui 1122952c63e9SJiang Liu if (!acpi_has_method(handle, "_EJ0")) 1123ebf4df8dSJiang Liu return false; 1124ebf4df8dSJiang Liu if (acpi_ata_match(handle)) 1125ebf4df8dSJiang Liu return true; 1126ebf4df8dSJiang Liu if (ACPI_FAILURE(acpi_get_parent(handle, &phandle))) 1127ebf4df8dSJiang Liu return false; 112854735266SZhang Rui 1129ebf4df8dSJiang Liu return acpi_ata_match(phandle); 113054735266SZhang Rui } 113154735266SZhang Rui 1132b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev) 11331e2380cdSRafael J. Wysocki { 1134b43109faSRafael J. Wysocki struct acpi_hardware_id *hwid; 11351e2380cdSRafael J. Wysocki 1136b43109faSRafael J. Wysocki list_for_each_entry(hwid, &adev->pnp.ids, list) 1137b43109faSRafael J. Wysocki if (!strcmp("PNP0C0A", hwid->id)) 1138b43109faSRafael J. Wysocki return true; 1139b43109faSRafael J. Wysocki 11401e2380cdSRafael J. Wysocki return false; 11411e2380cdSRafael J. Wysocki } 11421e2380cdSRafael J. Wysocki 1143b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev) 11441e2380cdSRafael J. Wysocki { 1145b43109faSRafael J. Wysocki acpi_handle handle = adev->handle; 1146b43109faSRafael J. Wysocki 1147b43109faSRafael J. Wysocki if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev)) 11481e2380cdSRafael J. Wysocki return true; 11491e2380cdSRafael J. Wysocki 11501e2380cdSRafael J. Wysocki return acpi_bay_match(handle); 11511e2380cdSRafael J. Wysocki } 11521e2380cdSRafael J. Wysocki 11533620f2f2SFrank Seidel /* 1154d4e1a692SToshi Kani * acpi_dock_match - see if an acpi object has a _DCK method 11553620f2f2SFrank Seidel */ 1156ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle) 11573620f2f2SFrank Seidel { 1158ebf4df8dSJiang Liu return acpi_has_method(handle, "_DCK"); 11593620f2f2SFrank Seidel } 11603620f2f2SFrank Seidel 1161adc8bb8eSHans de Goede static acpi_status 1162adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context, 1163adc8bb8eSHans de Goede void **return_value) 1164adc8bb8eSHans de Goede { 1165adc8bb8eSHans de Goede long *cap = context; 1166adc8bb8eSHans de Goede 1167adc8bb8eSHans de Goede if (acpi_has_method(handle, "_BCM") && 1168adc8bb8eSHans de Goede acpi_has_method(handle, "_BCL")) { 1169adc8bb8eSHans de Goede ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight " 1170adc8bb8eSHans de Goede "support\n")); 1171adc8bb8eSHans de Goede *cap |= ACPI_VIDEO_BACKLIGHT; 1172adc8bb8eSHans de Goede /* We have backlight support, no need to scan further */ 1173adc8bb8eSHans de Goede return AE_CTRL_TERMINATE; 1174adc8bb8eSHans de Goede } 1175adc8bb8eSHans de Goede return 0; 1176adc8bb8eSHans de Goede } 1177adc8bb8eSHans de Goede 1178adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be 1179adc8bb8eSHans de Goede * handled by video.ko. 1180adc8bb8eSHans de Goede * The device will get a Linux specific CID added in scan.c to 1181adc8bb8eSHans de Goede * identify the device as an ACPI graphics device 1182adc8bb8eSHans de Goede * Be aware that the graphics device may not be physically present 1183adc8bb8eSHans de Goede * Use acpi_video_get_capabilities() to detect general ACPI video 1184adc8bb8eSHans de Goede * capabilities of present cards 1185adc8bb8eSHans de Goede */ 1186adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle) 1187adc8bb8eSHans de Goede { 1188adc8bb8eSHans de Goede long video_caps = 0; 1189adc8bb8eSHans de Goede 1190adc8bb8eSHans de Goede /* Is this device able to support video switching ? */ 1191adc8bb8eSHans de Goede if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS")) 1192adc8bb8eSHans de Goede video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING; 1193adc8bb8eSHans de Goede 1194adc8bb8eSHans de Goede /* Is this device able to retrieve a video ROM ? */ 1195adc8bb8eSHans de Goede if (acpi_has_method(handle, "_ROM")) 1196adc8bb8eSHans de Goede video_caps |= ACPI_VIDEO_ROM_AVAILABLE; 1197adc8bb8eSHans de Goede 1198adc8bb8eSHans de Goede /* Is this device able to configure which video head to be POSTed ? */ 1199adc8bb8eSHans de Goede if (acpi_has_method(handle, "_VPO") && 1200adc8bb8eSHans de Goede acpi_has_method(handle, "_GPD") && 1201adc8bb8eSHans de Goede acpi_has_method(handle, "_SPD")) 1202adc8bb8eSHans de Goede video_caps |= ACPI_VIDEO_DEVICE_POSTING; 1203adc8bb8eSHans de Goede 1204adc8bb8eSHans de Goede /* Only check for backlight functionality if one of the above hit. */ 1205adc8bb8eSHans de Goede if (video_caps) 1206adc8bb8eSHans de Goede acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1207adc8bb8eSHans de Goede ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL, 1208adc8bb8eSHans de Goede &video_caps, NULL); 1209adc8bb8eSHans de Goede 1210adc8bb8eSHans de Goede return video_caps; 1211adc8bb8eSHans de Goede } 1212adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device); 1213adc8bb8eSHans de Goede 1214620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device) 121515b8dd53SBob Moore { 12167f47fa6cSBjorn Helgaas struct acpi_hardware_id *hid; 121715b8dd53SBob Moore 12182b2ae7c7SThomas Renninger if (list_empty(&device->pnp.ids)) 12192b2ae7c7SThomas Renninger return dummy_hid; 12202b2ae7c7SThomas Renninger 12217f47fa6cSBjorn Helgaas hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list); 12227f47fa6cSBjorn Helgaas return hid->id; 12237f47fa6cSBjorn Helgaas } 12247f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid); 122515b8dd53SBob Moore 1226d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id) 12277f47fa6cSBjorn Helgaas { 12287f47fa6cSBjorn Helgaas struct acpi_hardware_id *id; 122915b8dd53SBob Moore 12307f47fa6cSBjorn Helgaas id = kmalloc(sizeof(*id), GFP_KERNEL); 12317f47fa6cSBjorn Helgaas if (!id) 12327f47fa6cSBjorn Helgaas return; 123315b8dd53SBob Moore 12346a0d12efSRasmus Villemoes id->id = kstrdup_const(dev_id, GFP_KERNEL); 12357f47fa6cSBjorn Helgaas if (!id->id) { 12367f47fa6cSBjorn Helgaas kfree(id); 12377f47fa6cSBjorn Helgaas return; 123815b8dd53SBob Moore } 123915b8dd53SBob Moore 1240d4e1a692SToshi Kani list_add_tail(&id->list, &pnp->ids); 1241d4e1a692SToshi Kani pnp->type.hardware_id = 1; 124215b8dd53SBob Moore } 124315b8dd53SBob Moore 1244222e82acSDarrick J. Wong /* 1245222e82acSDarrick J. Wong * Old IBM workstations have a DSDT bug wherein the SMBus object 1246222e82acSDarrick J. Wong * lacks the SMBUS01 HID and the methods do not have the necessary "_" 1247222e82acSDarrick J. Wong * prefix. Work around this. 1248222e82acSDarrick J. Wong */ 1249ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle) 1250222e82acSDarrick J. Wong { 1251ebf4df8dSJiang Liu char node_name[ACPI_PATH_SEGMENT_LENGTH]; 1252ebf4df8dSJiang Liu struct acpi_buffer path = { sizeof(node_name), node_name }; 1253222e82acSDarrick J. Wong 1254222e82acSDarrick J. Wong if (!dmi_name_in_vendors("IBM")) 1255ebf4df8dSJiang Liu return false; 1256222e82acSDarrick J. Wong 1257222e82acSDarrick J. Wong /* Look for SMBS object */ 1258ebf4df8dSJiang Liu if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) || 1259ebf4df8dSJiang Liu strcmp("SMBS", path.pointer)) 1260ebf4df8dSJiang Liu return false; 1261222e82acSDarrick J. Wong 1262222e82acSDarrick J. Wong /* Does it have the necessary (but misnamed) methods? */ 1263952c63e9SJiang Liu if (acpi_has_method(handle, "SBI") && 1264952c63e9SJiang Liu acpi_has_method(handle, "SBR") && 1265952c63e9SJiang Liu acpi_has_method(handle, "SBW")) 1266ebf4df8dSJiang Liu return true; 1267ebf4df8dSJiang Liu 1268ebf4df8dSJiang Liu return false; 1269222e82acSDarrick J. Wong } 1270222e82acSDarrick J. Wong 1271ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle) 1272ae3caa80SJiang Liu { 1273ae3caa80SJiang Liu acpi_handle tmp; 1274ae3caa80SJiang Liu 1275ae3caa80SJiang Liu if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) && 1276ae3caa80SJiang Liu tmp == handle) 1277ae3caa80SJiang Liu return true; 1278ae3caa80SJiang Liu if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) && 1279ae3caa80SJiang Liu tmp == handle) 1280ae3caa80SJiang Liu return true; 1281ae3caa80SJiang Liu 1282ae3caa80SJiang Liu return false; 1283ae3caa80SJiang Liu } 1284ae3caa80SJiang Liu 1285c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp, 12866091b263SHans de Goede int device_type, struct acpi_device_info *info) 12871da177e4SLinus Torvalds { 128878e25fefSLv Zheng struct acpi_pnp_device_id_list *cid_list; 12897f47fa6cSBjorn Helgaas int i; 12901da177e4SLinus Torvalds 1291c0af4175SToshi Kani switch (device_type) { 12921da177e4SLinus Torvalds case ACPI_BUS_TYPE_DEVICE: 1293c0af4175SToshi Kani if (handle == ACPI_ROOT_OBJECT) { 1294c0af4175SToshi Kani acpi_add_id(pnp, ACPI_SYSTEM_HID); 1295859ac9a4SBjorn Helgaas break; 1296859ac9a4SBjorn Helgaas } 1297859ac9a4SBjorn Helgaas 12986091b263SHans de Goede if (!info) { 1299c0af4175SToshi Kani pr_err(PREFIX "%s: Error reading device info\n", 1300c0af4175SToshi Kani __func__); 13011da177e4SLinus Torvalds return; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 1304549e6845SRafael J. Wysocki if (info->valid & ACPI_VALID_HID) { 1305c0af4175SToshi Kani acpi_add_id(pnp, info->hardware_id.string); 1306549e6845SRafael J. Wysocki pnp->type.platform_id = 1; 1307549e6845SRafael J. Wysocki } 130857f3674fSBjorn Helgaas if (info->valid & ACPI_VALID_CID) { 130915b8dd53SBob Moore cid_list = &info->compatible_id_list; 131057f3674fSBjorn Helgaas for (i = 0; i < cid_list->count; i++) 1311c0af4175SToshi Kani acpi_add_id(pnp, cid_list->ids[i].string); 131257f3674fSBjorn Helgaas } 13131da177e4SLinus Torvalds if (info->valid & ACPI_VALID_ADR) { 1314c0af4175SToshi Kani pnp->bus_address = info->address; 1315c0af4175SToshi Kani pnp->type.bus_address = 1; 13161da177e4SLinus Torvalds } 1317ccf78040SLv Zheng if (info->valid & ACPI_VALID_UID) 1318c0af4175SToshi Kani pnp->unique_id = kstrdup(info->unique_id.string, 1319ccf78040SLv Zheng GFP_KERNEL); 132026095a01SSuthikulpanit, Suravee if (info->valid & ACPI_VALID_CLS) 132126095a01SSuthikulpanit, Suravee acpi_add_id(pnp, info->class_code.string); 1322ae843332SZhang Rui 132357f3674fSBjorn Helgaas /* 132457f3674fSBjorn Helgaas * Some devices don't reliably have _HIDs & _CIDs, so add 132557f3674fSBjorn Helgaas * synthetic HIDs to make sure drivers can find them. 13263620f2f2SFrank Seidel */ 1327c0af4175SToshi Kani if (acpi_is_video_device(handle)) 1328c0af4175SToshi Kani acpi_add_id(pnp, ACPI_VIDEO_HID); 1329ebf4df8dSJiang Liu else if (acpi_bay_match(handle)) 1330c0af4175SToshi Kani acpi_add_id(pnp, ACPI_BAY_HID); 1331ebf4df8dSJiang Liu else if (acpi_dock_match(handle)) 1332c0af4175SToshi Kani acpi_add_id(pnp, ACPI_DOCK_HID); 1333ebf4df8dSJiang Liu else if (acpi_ibm_smbus_match(handle)) 1334c0af4175SToshi Kani acpi_add_id(pnp, ACPI_SMBUS_IBM_HID); 1335ae3caa80SJiang Liu else if (list_empty(&pnp->ids) && 1336ae3caa80SJiang Liu acpi_object_is_system_bus(handle)) { 1337ae3caa80SJiang Liu /* \_SB, \_TZ, LNXSYBUS */ 1338ae3caa80SJiang Liu acpi_add_id(pnp, ACPI_BUS_HID); 1339c0af4175SToshi Kani strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME); 1340c0af4175SToshi Kani strcpy(pnp->device_class, ACPI_BUS_CLASS); 1341b7b30de5SBjorn Helgaas } 134254735266SZhang Rui 13431da177e4SLinus Torvalds break; 13441da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER: 1345c0af4175SToshi Kani acpi_add_id(pnp, ACPI_POWER_HID); 13461da177e4SLinus Torvalds break; 13471da177e4SLinus Torvalds case ACPI_BUS_TYPE_PROCESSOR: 1348c0af4175SToshi Kani acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID); 13491da177e4SLinus Torvalds break; 13501da177e4SLinus Torvalds case ACPI_BUS_TYPE_THERMAL: 1351c0af4175SToshi Kani acpi_add_id(pnp, ACPI_THERMAL_HID); 13521da177e4SLinus Torvalds break; 13531da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 1354c0af4175SToshi Kani acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF); 13551da177e4SLinus Torvalds break; 13561da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 1357c0af4175SToshi Kani acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF); 13581da177e4SLinus Torvalds break; 1359a64a62ceSLv Zheng case ACPI_BUS_TYPE_ECDT_EC: 1360a64a62ceSLv Zheng acpi_add_id(pnp, ACPI_ECDT_HID); 1361a64a62ceSLv Zheng break; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 1365c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp) 1366c0af4175SToshi Kani { 1367c0af4175SToshi Kani struct acpi_hardware_id *id, *tmp; 1368c0af4175SToshi Kani 1369c0af4175SToshi Kani list_for_each_entry_safe(id, tmp, &pnp->ids, list) { 13706a0d12efSRasmus Villemoes kfree_const(id->id); 1371c0af4175SToshi Kani kfree(id); 1372c0af4175SToshi Kani } 1373c0af4175SToshi Kani kfree(pnp->unique_id); 1374c0af4175SToshi Kani } 1375c0af4175SToshi Kani 1376b84f196dSSuthikulpanit, Suravee /** 1377b84f196dSSuthikulpanit, Suravee * acpi_dma_supported - Check DMA support for the specified device. 1378b84f196dSSuthikulpanit, Suravee * @adev: The pointer to acpi device 1379b84f196dSSuthikulpanit, Suravee * 1380b84f196dSSuthikulpanit, Suravee * Return false if DMA is not supported. Otherwise, return true 1381b84f196dSSuthikulpanit, Suravee */ 1382b84f196dSSuthikulpanit, Suravee bool acpi_dma_supported(struct acpi_device *adev) 1383b84f196dSSuthikulpanit, Suravee { 1384b84f196dSSuthikulpanit, Suravee if (!adev) 1385b84f196dSSuthikulpanit, Suravee return false; 1386b84f196dSSuthikulpanit, Suravee 1387b84f196dSSuthikulpanit, Suravee if (adev->flags.cca_seen) 1388b84f196dSSuthikulpanit, Suravee return true; 1389b84f196dSSuthikulpanit, Suravee 1390b84f196dSSuthikulpanit, Suravee /* 1391b84f196dSSuthikulpanit, Suravee * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent 1392b84f196dSSuthikulpanit, Suravee * DMA on "Intel platforms". Presumably that includes all x86 and 1393b84f196dSSuthikulpanit, Suravee * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y. 1394b84f196dSSuthikulpanit, Suravee */ 1395b84f196dSSuthikulpanit, Suravee if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED)) 1396b84f196dSSuthikulpanit, Suravee return true; 1397b84f196dSSuthikulpanit, Suravee 1398b84f196dSSuthikulpanit, Suravee return false; 1399b84f196dSSuthikulpanit, Suravee } 1400b84f196dSSuthikulpanit, Suravee 1401b84f196dSSuthikulpanit, Suravee /** 1402b84f196dSSuthikulpanit, Suravee * acpi_get_dma_attr - Check the supported DMA attr for the specified device. 1403b84f196dSSuthikulpanit, Suravee * @adev: The pointer to acpi device 1404b84f196dSSuthikulpanit, Suravee * 1405b84f196dSSuthikulpanit, Suravee * Return enum dev_dma_attr. 1406b84f196dSSuthikulpanit, Suravee */ 1407b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev) 1408b84f196dSSuthikulpanit, Suravee { 1409b84f196dSSuthikulpanit, Suravee if (!acpi_dma_supported(adev)) 1410b84f196dSSuthikulpanit, Suravee return DEV_DMA_NOT_SUPPORTED; 1411b84f196dSSuthikulpanit, Suravee 1412b84f196dSSuthikulpanit, Suravee if (adev->flags.coherent_dma) 1413b84f196dSSuthikulpanit, Suravee return DEV_DMA_COHERENT; 1414b84f196dSSuthikulpanit, Suravee else 1415b84f196dSSuthikulpanit, Suravee return DEV_DMA_NON_COHERENT; 1416b84f196dSSuthikulpanit, Suravee } 1417b84f196dSSuthikulpanit, Suravee 1418d760a1baSLorenzo Pieralisi /** 1419c04ac679SLorenzo Pieralisi * acpi_dma_get_range() - Get device DMA parameters. 1420c04ac679SLorenzo Pieralisi * 1421c04ac679SLorenzo Pieralisi * @dev: device to configure 1422c04ac679SLorenzo Pieralisi * @dma_addr: pointer device DMA address result 1423c04ac679SLorenzo Pieralisi * @offset: pointer to the DMA offset result 1424c04ac679SLorenzo Pieralisi * @size: pointer to DMA range size result 1425c04ac679SLorenzo Pieralisi * 1426c04ac679SLorenzo Pieralisi * Evaluate DMA regions and return respectively DMA region start, offset 1427c04ac679SLorenzo Pieralisi * and size in dma_addr, offset and size on parsing success; it does not 1428c04ac679SLorenzo Pieralisi * update the passed in values on failure. 1429c04ac679SLorenzo Pieralisi * 1430c04ac679SLorenzo Pieralisi * Return 0 on success, < 0 on failure. 1431c04ac679SLorenzo Pieralisi */ 1432c04ac679SLorenzo Pieralisi int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset, 1433c04ac679SLorenzo Pieralisi u64 *size) 1434c04ac679SLorenzo Pieralisi { 1435c04ac679SLorenzo Pieralisi struct acpi_device *adev; 1436c04ac679SLorenzo Pieralisi LIST_HEAD(list); 1437c04ac679SLorenzo Pieralisi struct resource_entry *rentry; 1438c04ac679SLorenzo Pieralisi int ret; 1439c04ac679SLorenzo Pieralisi struct device *dma_dev = dev; 1440c04ac679SLorenzo Pieralisi u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0; 1441c04ac679SLorenzo Pieralisi 1442c04ac679SLorenzo Pieralisi /* 1443c04ac679SLorenzo Pieralisi * Walk the device tree chasing an ACPI companion with a _DMA 1444c04ac679SLorenzo Pieralisi * object while we go. Stop if we find a device with an ACPI 1445c04ac679SLorenzo Pieralisi * companion containing a _DMA method. 1446c04ac679SLorenzo Pieralisi */ 1447c04ac679SLorenzo Pieralisi do { 1448c04ac679SLorenzo Pieralisi adev = ACPI_COMPANION(dma_dev); 1449c04ac679SLorenzo Pieralisi if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA)) 1450c04ac679SLorenzo Pieralisi break; 1451c04ac679SLorenzo Pieralisi 1452c04ac679SLorenzo Pieralisi dma_dev = dma_dev->parent; 1453c04ac679SLorenzo Pieralisi } while (dma_dev); 1454c04ac679SLorenzo Pieralisi 1455c04ac679SLorenzo Pieralisi if (!dma_dev) 1456c04ac679SLorenzo Pieralisi return -ENODEV; 1457c04ac679SLorenzo Pieralisi 1458c04ac679SLorenzo Pieralisi if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) { 1459c04ac679SLorenzo Pieralisi acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n"); 1460c04ac679SLorenzo Pieralisi return -EINVAL; 1461c04ac679SLorenzo Pieralisi } 1462c04ac679SLorenzo Pieralisi 1463c04ac679SLorenzo Pieralisi ret = acpi_dev_get_dma_resources(adev, &list); 1464c04ac679SLorenzo Pieralisi if (ret > 0) { 1465c04ac679SLorenzo Pieralisi list_for_each_entry(rentry, &list, node) { 1466c04ac679SLorenzo Pieralisi if (dma_offset && rentry->offset != dma_offset) { 1467c04ac679SLorenzo Pieralisi ret = -EINVAL; 1468c04ac679SLorenzo Pieralisi dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n"); 1469c04ac679SLorenzo Pieralisi goto out; 1470c04ac679SLorenzo Pieralisi } 1471c04ac679SLorenzo Pieralisi dma_offset = rentry->offset; 1472c04ac679SLorenzo Pieralisi 1473c04ac679SLorenzo Pieralisi /* Take lower and upper limits */ 1474c04ac679SLorenzo Pieralisi if (rentry->res->start < dma_start) 1475c04ac679SLorenzo Pieralisi dma_start = rentry->res->start; 1476c04ac679SLorenzo Pieralisi if (rentry->res->end > dma_end) 1477c04ac679SLorenzo Pieralisi dma_end = rentry->res->end; 1478c04ac679SLorenzo Pieralisi } 1479c04ac679SLorenzo Pieralisi 1480c04ac679SLorenzo Pieralisi if (dma_start >= dma_end) { 1481c04ac679SLorenzo Pieralisi ret = -EINVAL; 1482c04ac679SLorenzo Pieralisi dev_dbg(dma_dev, "Invalid DMA regions configuration\n"); 1483c04ac679SLorenzo Pieralisi goto out; 1484c04ac679SLorenzo Pieralisi } 1485c04ac679SLorenzo Pieralisi 1486c04ac679SLorenzo Pieralisi *dma_addr = dma_start - dma_offset; 1487c04ac679SLorenzo Pieralisi len = dma_end - dma_start; 1488c04ac679SLorenzo Pieralisi *size = max(len, len + 1); 1489c04ac679SLorenzo Pieralisi *offset = dma_offset; 1490c04ac679SLorenzo Pieralisi } 1491c04ac679SLorenzo Pieralisi out: 1492c04ac679SLorenzo Pieralisi acpi_dev_free_resource_list(&list); 1493c04ac679SLorenzo Pieralisi 1494c04ac679SLorenzo Pieralisi return ret >= 0 ? 0 : ret; 1495c04ac679SLorenzo Pieralisi } 1496c04ac679SLorenzo Pieralisi 1497c04ac679SLorenzo Pieralisi /** 1498c1e97359SJohn Garry * acpi_dma_configure_id - Set-up DMA configuration for the device. 1499d760a1baSLorenzo Pieralisi * @dev: The pointer to the device 1500d760a1baSLorenzo Pieralisi * @attr: device dma attributes 1501b8e069a2SLorenzo Pieralisi * @input_id: input device id const value pointer 1502d760a1baSLorenzo Pieralisi */ 1503b8e069a2SLorenzo Pieralisi int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr, 1504b8e069a2SLorenzo Pieralisi const u32 *input_id) 1505d760a1baSLorenzo Pieralisi { 1506643b8e4dSLorenzo Pieralisi const struct iommu_ops *iommu; 15077ad42639SLorenzo Pieralisi u64 dma_addr = 0, size = 0; 1508643b8e4dSLorenzo Pieralisi 1509e5361ca2SRobin Murphy if (attr == DEV_DMA_NOT_SUPPORTED) { 1510e5361ca2SRobin Murphy set_dma_ops(dev, &dma_dummy_ops); 1511e5361ca2SRobin Murphy return 0; 1512e5361ca2SRobin Murphy } 1513e5361ca2SRobin Murphy 15147ad42639SLorenzo Pieralisi iort_dma_setup(dev, &dma_addr, &size); 1515d760a1baSLorenzo Pieralisi 1516b8e069a2SLorenzo Pieralisi iommu = iort_iommu_configure_id(dev, input_id); 151745586c70SMasahiro Yamada if (PTR_ERR(iommu) == -EPROBE_DEFER) 1518058f8c3fSSricharan R return -EPROBE_DEFER; 1519643b8e4dSLorenzo Pieralisi 15207ad42639SLorenzo Pieralisi arch_setup_dma_ops(dev, dma_addr, size, 15217ad42639SLorenzo Pieralisi iommu, attr == DEV_DMA_COHERENT); 15225a1bb638SSricharan R 15235a1bb638SSricharan R return 0; 1524d760a1baSLorenzo Pieralisi } 1525b8e069a2SLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_configure_id); 1526d760a1baSLorenzo Pieralisi 1527d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev) 1528d0562674SSuthikulpanit, Suravee { 1529d0562674SSuthikulpanit, Suravee unsigned long long cca = 0; 1530d0562674SSuthikulpanit, Suravee acpi_status status; 1531d0562674SSuthikulpanit, Suravee struct acpi_device *parent = adev->parent; 1532d0562674SSuthikulpanit, Suravee 1533d0562674SSuthikulpanit, Suravee if (parent && parent->flags.cca_seen) { 1534d0562674SSuthikulpanit, Suravee /* 1535d0562674SSuthikulpanit, Suravee * From ACPI spec, OSPM will ignore _CCA if an ancestor 1536d0562674SSuthikulpanit, Suravee * already saw one. 1537d0562674SSuthikulpanit, Suravee */ 1538d0562674SSuthikulpanit, Suravee adev->flags.cca_seen = 1; 1539d0562674SSuthikulpanit, Suravee cca = parent->flags.coherent_dma; 1540d0562674SSuthikulpanit, Suravee } else { 1541d0562674SSuthikulpanit, Suravee status = acpi_evaluate_integer(adev->handle, "_CCA", 1542d0562674SSuthikulpanit, Suravee NULL, &cca); 1543d0562674SSuthikulpanit, Suravee if (ACPI_SUCCESS(status)) 1544d0562674SSuthikulpanit, Suravee adev->flags.cca_seen = 1; 1545d0562674SSuthikulpanit, Suravee else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED)) 1546d0562674SSuthikulpanit, Suravee /* 1547d0562674SSuthikulpanit, Suravee * If architecture does not specify that _CCA is 1548d0562674SSuthikulpanit, Suravee * required for DMA-able devices (e.g. x86), 1549d0562674SSuthikulpanit, Suravee * we default to _CCA=1. 1550d0562674SSuthikulpanit, Suravee */ 1551d0562674SSuthikulpanit, Suravee cca = 1; 1552d0562674SSuthikulpanit, Suravee else 1553d0562674SSuthikulpanit, Suravee acpi_handle_debug(adev->handle, 1554d0562674SSuthikulpanit, Suravee "ACPI device is missing _CCA.\n"); 1555d0562674SSuthikulpanit, Suravee } 1556d0562674SSuthikulpanit, Suravee 1557d0562674SSuthikulpanit, Suravee adev->flags.coherent_dma = cca; 1558d0562674SSuthikulpanit, Suravee } 1559d0562674SSuthikulpanit, Suravee 1560e361d1f8SFrédéric Danis static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data) 1561e4330d8bSJarkko Nikula { 1562e361d1f8SFrédéric Danis bool *is_serial_bus_slave_p = data; 1563e4330d8bSJarkko Nikula 1564e4330d8bSJarkko Nikula if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 1565e4330d8bSJarkko Nikula return 1; 1566e4330d8bSJarkko Nikula 1567e361d1f8SFrédéric Danis *is_serial_bus_slave_p = true; 1568e4330d8bSJarkko Nikula 1569e4330d8bSJarkko Nikula /* no need to do more checking */ 1570e4330d8bSJarkko Nikula return -1; 1571e4330d8bSJarkko Nikula } 1572e4330d8bSJarkko Nikula 1573dfda4492SJohn Garry static bool acpi_is_indirect_io_slave(struct acpi_device *device) 1574dfda4492SJohn Garry { 1575dfda4492SJohn Garry struct acpi_device *parent = device->parent; 1576a6f07295SJohn Garry static const struct acpi_device_id indirect_io_hosts[] = { 1577dfda4492SJohn Garry {"HISI0191", 0}, 1578dfda4492SJohn Garry {} 1579dfda4492SJohn Garry }; 1580dfda4492SJohn Garry 1581dfda4492SJohn Garry return parent && !acpi_match_device_ids(parent, indirect_io_hosts); 1582dfda4492SJohn Garry } 1583dfda4492SJohn Garry 1584d87fb091SJohn Garry static bool acpi_device_enumeration_by_parent(struct acpi_device *device) 1585e4330d8bSJarkko Nikula { 1586e4330d8bSJarkko Nikula struct list_head resource_list; 1587e361d1f8SFrédéric Danis bool is_serial_bus_slave = false; 1588aba94139SHans de Goede /* 1589aba94139SHans de Goede * These devices have multiple I2cSerialBus resources and an i2c-client 1590aba94139SHans de Goede * must be instantiated for each, each with its own i2c_device_id. 1591aba94139SHans de Goede * Normally we only instantiate an i2c-client for the first resource, 1592aba94139SHans de Goede * using the ACPI HID as id. These special cases are handled by the 1593aba94139SHans de Goede * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows 1594aba94139SHans de Goede * which i2c_device_id to use for each resource. 1595aba94139SHans de Goede */ 1596aba94139SHans de Goede static const struct acpi_device_id i2c_multi_instantiate_ids[] = { 1597aba94139SHans de Goede {"BSG1160", }, 159896f984d3SHans de Goede {"BSG2150", }, 1599589edb56SHans de Goede {"INT33FE", }, 1600a3dd034aSAndy Shevchenko {"INT3515", }, 1601aba94139SHans de Goede {} 1602aba94139SHans de Goede }; 1603e4330d8bSJarkko Nikula 1604dfda4492SJohn Garry if (acpi_is_indirect_io_slave(device)) 1605dfda4492SJohn Garry return true; 1606dfda4492SJohn Garry 1607ca9ef3abSLukas Wunner /* Macs use device properties in lieu of _CRS resources */ 1608ca9ef3abSLukas Wunner if (x86_apple_machine && 1609ca9ef3abSLukas Wunner (fwnode_property_present(&device->fwnode, "spiSclkPeriod") || 1610e361d1f8SFrédéric Danis fwnode_property_present(&device->fwnode, "i2cAddress") || 1611e361d1f8SFrédéric Danis fwnode_property_present(&device->fwnode, "baud"))) 1612ca9ef3abSLukas Wunner return true; 1613ca9ef3abSLukas Wunner 1614aba94139SHans de Goede /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */ 1615aba94139SHans de Goede if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids)) 1616aba94139SHans de Goede return false; 1617aba94139SHans de Goede 1618e4330d8bSJarkko Nikula INIT_LIST_HEAD(&resource_list); 1619e361d1f8SFrédéric Danis acpi_dev_get_resources(device, &resource_list, 1620e361d1f8SFrédéric Danis acpi_check_serial_bus_slave, 1621e361d1f8SFrédéric Danis &is_serial_bus_slave); 1622e4330d8bSJarkko Nikula acpi_dev_free_resource_list(&resource_list); 1623e4330d8bSJarkko Nikula 1624e361d1f8SFrédéric Danis return is_serial_bus_slave; 1625e4330d8bSJarkko Nikula } 1626e4330d8bSJarkko Nikula 162782c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, 16286091b263SHans de Goede int type, unsigned long long sta, 16296091b263SHans de Goede struct acpi_device_info *info) 16301da177e4SLinus Torvalds { 1631d43e167dSRafael J. Wysocki INIT_LIST_HEAD(&device->pnp.ids); 1632d43e167dSRafael J. Wysocki device->device_type = type; 1633d43e167dSRafael J. Wysocki device->handle = handle; 1634d43e167dSRafael J. Wysocki device->parent = acpi_bus_get_parent(handle); 163501bb86b3SSaravana Kannan fwnode_init(&device->fwnode, &acpi_device_fwnode_ops); 163625db115bSRafael J. Wysocki acpi_set_device_status(device, sta); 1637d43e167dSRafael J. Wysocki acpi_device_get_busid(device); 16386091b263SHans de Goede acpi_set_pnp_ids(handle, &device->pnp, type, info); 1639ffdcd955SMika Westerberg acpi_init_properties(device); 1640d43e167dSRafael J. Wysocki acpi_bus_get_flags(device); 16412c0d4fe0SRafael J. Wysocki device->flags.match_driver = false; 1642202317a5SRafael J. Wysocki device->flags.initialized = true; 1643d87fb091SJohn Garry device->flags.enumeration_by_parent = 1644d87fb091SJohn Garry acpi_device_enumeration_by_parent(device); 164510c7e20bSOctavian Purdila acpi_device_clear_enumerated(device); 1646cf860be6SRafael J. Wysocki device_initialize(&device->dev); 1647cf860be6SRafael J. Wysocki dev_set_uevent_suppress(&device->dev, true); 1648d0562674SSuthikulpanit, Suravee acpi_init_coherency(device); 1649cf860be6SRafael J. Wysocki } 1650bc3b0772SBjorn Helgaas 1651cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device) 1652cf860be6SRafael J. Wysocki { 1653cf860be6SRafael J. Wysocki dev_set_uevent_suppress(&device->dev, false); 1654cf860be6SRafael J. Wysocki kobject_uevent(&device->dev.kobj, KOBJ_ADD); 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 16575c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child, 16585c478f49SBjorn Helgaas acpi_handle handle, int type, 16592c0d4fe0SRafael J. Wysocki unsigned long long sta) 16601da177e4SLinus Torvalds { 166177c24888SBjorn Helgaas int result; 166277c24888SBjorn Helgaas struct acpi_device *device; 166329aaefa6SBjorn Helgaas struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 16646091b263SHans de Goede struct acpi_device_info *info = NULL; 16656091b263SHans de Goede 16666091b263SHans de Goede if (handle != ACPI_ROOT_OBJECT && type == ACPI_BUS_TYPE_DEVICE) 16676091b263SHans de Goede acpi_get_object_info(handle, &info); 16681da177e4SLinus Torvalds 166936bcbec7SBurman Yan device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); 16701da177e4SLinus Torvalds if (!device) { 16716468463aSLen Brown printk(KERN_ERR PREFIX "Memory allocation error\n"); 16726091b263SHans de Goede kfree(info); 1673d550d98dSPatrick Mochel return -ENOMEM; 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds 16766091b263SHans de Goede acpi_init_device_object(device, handle, type, sta, info); 16776091b263SHans de Goede kfree(info); 167863347db0SHans de Goede /* 167963347db0SHans de Goede * For ACPI_BUS_TYPE_DEVICE getting the status is delayed till here so 168063347db0SHans de Goede * that we can call acpi_bus_get_status() and use its quirk handling. 168163347db0SHans de Goede * Note this must be done before the get power-/wakeup_dev-flags calls. 168263347db0SHans de Goede */ 168363347db0SHans de Goede if (type == ACPI_BUS_TYPE_DEVICE) 16845971b0c1SHans de Goede if (acpi_bus_get_status(device) < 0) 16855971b0c1SHans de Goede acpi_set_device_status(device, 0); 168663347db0SHans de Goede 1687d43e167dSRafael J. Wysocki acpi_bus_get_power_flags(device); 1688d57d09a4SRafael J. Wysocki acpi_bus_get_wakeup_device_flags(device); 16891da177e4SLinus Torvalds 1690cf860be6SRafael J. Wysocki result = acpi_device_add(device, acpi_device_release); 1691d43e167dSRafael J. Wysocki if (result) { 1692718fb0deSHugh Dickins acpi_device_release(&device->dev); 1693d550d98dSPatrick Mochel return result; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 1696bc9b6407SRafael J. Wysocki acpi_power_add_remove_device(device, true); 1697cf860be6SRafael J. Wysocki acpi_device_add_finalize(device); 16981da177e4SLinus Torvalds acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 1699d43e167dSRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n", 1700d43e167dSRafael J. Wysocki dev_name(&device->dev), (char *) buffer.pointer, 1701d43e167dSRafael J. Wysocki device->parent ? dev_name(&device->parent->dev) : "(null)")); 17021da177e4SLinus Torvalds kfree(buffer.pointer); 17031da177e4SLinus Torvalds *child = device; 1704d43e167dSRafael J. Wysocki return 0; 1705bf325f95SRafael J. Wysocki } 1706bf325f95SRafael J. Wysocki 1707a4e081b0SShannon Zhao static acpi_status acpi_get_resource_memory(struct acpi_resource *ares, 1708a4e081b0SShannon Zhao void *context) 1709a4e081b0SShannon Zhao { 1710a4e081b0SShannon Zhao struct resource *res = context; 1711a4e081b0SShannon Zhao 1712a4e081b0SShannon Zhao if (acpi_dev_resource_memory(ares, res)) 1713a4e081b0SShannon Zhao return AE_CTRL_TERMINATE; 1714a4e081b0SShannon Zhao 1715a4e081b0SShannon Zhao return AE_OK; 1716a4e081b0SShannon Zhao } 1717a4e081b0SShannon Zhao 1718a4e081b0SShannon Zhao static bool acpi_device_should_be_hidden(acpi_handle handle) 1719a4e081b0SShannon Zhao { 1720a4e081b0SShannon Zhao acpi_status status; 1721a4e081b0SShannon Zhao struct resource res; 1722a4e081b0SShannon Zhao 1723a4e081b0SShannon Zhao /* Check if it should ignore the UART device */ 1724a4e081b0SShannon Zhao if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS))) 1725a4e081b0SShannon Zhao return false; 1726a4e081b0SShannon Zhao 1727a4e081b0SShannon Zhao /* 1728a4e081b0SShannon Zhao * The UART device described in SPCR table is assumed to have only one 1729a4e081b0SShannon Zhao * memory resource present. So we only look for the first one here. 1730a4e081b0SShannon Zhao */ 1731a4e081b0SShannon Zhao status = acpi_walk_resources(handle, METHOD_NAME__CRS, 1732a4e081b0SShannon Zhao acpi_get_resource_memory, &res); 1733a4e081b0SShannon Zhao if (ACPI_FAILURE(status) || res.start != spcr_uart_addr) 1734a4e081b0SShannon Zhao return false; 1735a4e081b0SShannon Zhao 1736a4e081b0SShannon Zhao acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n", 1737a4e081b0SShannon Zhao &res.start); 1738a4e081b0SShannon Zhao 1739a4e081b0SShannon Zhao return true; 1740a4e081b0SShannon Zhao } 1741a4e081b0SShannon Zhao 1742778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type, 1743778cbc1dSBjorn Helgaas unsigned long long *sta) 1744778cbc1dSBjorn Helgaas { 1745778cbc1dSBjorn Helgaas acpi_status status; 1746778cbc1dSBjorn Helgaas acpi_object_type acpi_type; 1747778cbc1dSBjorn Helgaas 1748778cbc1dSBjorn Helgaas status = acpi_get_type(handle, &acpi_type); 1749778cbc1dSBjorn Helgaas if (ACPI_FAILURE(status)) 1750778cbc1dSBjorn Helgaas return -ENODEV; 1751778cbc1dSBjorn Helgaas 1752778cbc1dSBjorn Helgaas switch (acpi_type) { 1753778cbc1dSBjorn Helgaas case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ 1754778cbc1dSBjorn Helgaas case ACPI_TYPE_DEVICE: 1755a4e081b0SShannon Zhao if (acpi_device_should_be_hidden(handle)) 1756a4e081b0SShannon Zhao return -ENODEV; 1757a4e081b0SShannon Zhao 1758778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_DEVICE; 175963347db0SHans de Goede /* 176063347db0SHans de Goede * acpi_add_single_object updates this once we've an acpi_device 176163347db0SHans de Goede * so that acpi_bus_get_status' quirk handling can be used. 176263347db0SHans de Goede */ 17635971b0c1SHans de Goede *sta = ACPI_STA_DEFAULT; 1764778cbc1dSBjorn Helgaas break; 1765778cbc1dSBjorn Helgaas case ACPI_TYPE_PROCESSOR: 1766778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_PROCESSOR; 1767778cbc1dSBjorn Helgaas status = acpi_bus_get_status_handle(handle, sta); 1768778cbc1dSBjorn Helgaas if (ACPI_FAILURE(status)) 1769778cbc1dSBjorn Helgaas return -ENODEV; 1770778cbc1dSBjorn Helgaas break; 1771778cbc1dSBjorn Helgaas case ACPI_TYPE_THERMAL: 1772778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_THERMAL; 1773778cbc1dSBjorn Helgaas *sta = ACPI_STA_DEFAULT; 1774778cbc1dSBjorn Helgaas break; 1775778cbc1dSBjorn Helgaas case ACPI_TYPE_POWER: 1776778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_POWER; 1777778cbc1dSBjorn Helgaas *sta = ACPI_STA_DEFAULT; 1778778cbc1dSBjorn Helgaas break; 1779778cbc1dSBjorn Helgaas default: 1780778cbc1dSBjorn Helgaas return -ENODEV; 1781778cbc1dSBjorn Helgaas } 1782778cbc1dSBjorn Helgaas 1783778cbc1dSBjorn Helgaas return 0; 1784778cbc1dSBjorn Helgaas } 1785778cbc1dSBjorn Helgaas 1786cde1f95fSSakari Ailus bool acpi_device_is_present(const struct acpi_device *adev) 1787202317a5SRafael J. Wysocki { 1788cde1f95fSSakari Ailus return adev->status.present || adev->status.functional; 1789202317a5SRafael J. Wysocki } 1790202317a5SRafael J. Wysocki 17914b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler, 1792844142c3SRasmus Villemoes const char *idstr, 1793c5698074SRafael J. Wysocki const struct acpi_device_id **matchid) 1794c5698074SRafael J. Wysocki { 1795c5698074SRafael J. Wysocki const struct acpi_device_id *devid; 1796c5698074SRafael J. Wysocki 1797aca0a4ebSRafael J. Wysocki if (handler->match) 1798aca0a4ebSRafael J. Wysocki return handler->match(idstr, matchid); 1799aca0a4ebSRafael J. Wysocki 1800c5698074SRafael J. Wysocki for (devid = handler->ids; devid->id[0]; devid++) 1801c5698074SRafael J. Wysocki if (!strcmp((char *)devid->id, idstr)) { 1802c5698074SRafael J. Wysocki if (matchid) 1803c5698074SRafael J. Wysocki *matchid = devid; 1804c5698074SRafael J. Wysocki 18054b59cc1fSRafael J. Wysocki return true; 18064b59cc1fSRafael J. Wysocki } 18074b59cc1fSRafael J. Wysocki 18084b59cc1fSRafael J. Wysocki return false; 18094b59cc1fSRafael J. Wysocki } 18104b59cc1fSRafael J. Wysocki 1811844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr, 18126b772e8fSToshi Kani const struct acpi_device_id **matchid) 18136b772e8fSToshi Kani { 18146b772e8fSToshi Kani struct acpi_scan_handler *handler; 18156b772e8fSToshi Kani 18166b772e8fSToshi Kani list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) 18176b772e8fSToshi Kani if (acpi_scan_handler_matching(handler, idstr, matchid)) 18186b772e8fSToshi Kani return handler; 18196b772e8fSToshi Kani 18206b772e8fSToshi Kani return NULL; 18216b772e8fSToshi Kani } 18226b772e8fSToshi Kani 18233f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val) 18243f8055c3SRafael J. Wysocki { 18253f8055c3SRafael J. Wysocki if (!!hotplug->enabled == !!val) 18263f8055c3SRafael J. Wysocki return; 18273f8055c3SRafael J. Wysocki 18283f8055c3SRafael J. Wysocki mutex_lock(&acpi_scan_lock); 18293f8055c3SRafael J. Wysocki 18303f8055c3SRafael J. Wysocki hotplug->enabled = val; 18313f8055c3SRafael J. Wysocki 18323f8055c3SRafael J. Wysocki mutex_unlock(&acpi_scan_lock); 18333f8055c3SRafael J. Wysocki } 18343f8055c3SRafael J. Wysocki 18353c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev) 18364b59cc1fSRafael J. Wysocki { 18376b772e8fSToshi Kani struct acpi_hardware_id *hwid; 18383c2cc7ffSRafael J. Wysocki 1839b43109faSRafael J. Wysocki if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) { 18401e2380cdSRafael J. Wysocki acpi_dock_add(adev); 18411e2380cdSRafael J. Wysocki return; 18421e2380cdSRafael J. Wysocki } 18433c2cc7ffSRafael J. Wysocki list_for_each_entry(hwid, &adev->pnp.ids, list) { 18444b59cc1fSRafael J. Wysocki struct acpi_scan_handler *handler; 18454b59cc1fSRafael J. Wysocki 18466b772e8fSToshi Kani handler = acpi_scan_match_handler(hwid->id, NULL); 18471a699476SRafael J. Wysocki if (handler) { 18483c2cc7ffSRafael J. Wysocki adev->flags.hotplug_notify = true; 18496b772e8fSToshi Kani break; 18506b772e8fSToshi Kani } 18516b772e8fSToshi Kani } 18521a699476SRafael J. Wysocki } 18536b772e8fSToshi Kani 18546fc25088SRafael J. Wysocki static u32 acpi_scan_check_dep(acpi_handle handle) 185540e7fcb1SLan Tianyu { 185640e7fcb1SLan Tianyu struct acpi_handle_list dep_devices; 185740e7fcb1SLan Tianyu acpi_status status; 18586fc25088SRafael J. Wysocki u32 count; 185940e7fcb1SLan Tianyu int i; 186040e7fcb1SLan Tianyu 186171da201fSRafael J. Wysocki /* 186271da201fSRafael J. Wysocki * Check for _HID here to avoid deferring the enumeration of: 186371da201fSRafael J. Wysocki * 1. PCI devices. 186471da201fSRafael J. Wysocki * 2. ACPI nodes describing USB ports. 186571da201fSRafael J. Wysocki * Still, checking for _HID catches more then just these cases ... 186671da201fSRafael J. Wysocki */ 186771da201fSRafael J. Wysocki if (!acpi_has_method(handle, "_DEP") || !acpi_has_method(handle, "_HID")) 18686fc25088SRafael J. Wysocki return 0; 186963347db0SHans de Goede 18706fc25088SRafael J. Wysocki status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices); 187140e7fcb1SLan Tianyu if (ACPI_FAILURE(status)) { 18726fc25088SRafael J. Wysocki acpi_handle_debug(handle, "Failed to evaluate _DEP.\n"); 18736fc25088SRafael J. Wysocki return 0; 187440e7fcb1SLan Tianyu } 187540e7fcb1SLan Tianyu 18766fc25088SRafael J. Wysocki for (count = 0, i = 0; i < dep_devices.count; i++) { 187740e7fcb1SLan Tianyu struct acpi_device_info *info; 18786fc25088SRafael J. Wysocki struct acpi_dep_data *dep; 18796fc25088SRafael J. Wysocki bool skip; 188040e7fcb1SLan Tianyu 188140e7fcb1SLan Tianyu status = acpi_get_object_info(dep_devices.handles[i], &info); 188240e7fcb1SLan Tianyu if (ACPI_FAILURE(status)) { 18836fc25088SRafael J. Wysocki acpi_handle_debug(handle, "Error reading _DEP device info\n"); 188440e7fcb1SLan Tianyu continue; 188540e7fcb1SLan Tianyu } 188640e7fcb1SLan Tianyu 18872ef33ee7SRafael J. Wysocki skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids); 188840e7fcb1SLan Tianyu kfree(info); 188940e7fcb1SLan Tianyu 189040e7fcb1SLan Tianyu if (skip) 189140e7fcb1SLan Tianyu continue; 189240e7fcb1SLan Tianyu 18936fc25088SRafael J. Wysocki dep = kzalloc(sizeof(*dep), GFP_KERNEL); 189440e7fcb1SLan Tianyu if (!dep) 18956fc25088SRafael J. Wysocki continue; 18966fc25088SRafael J. Wysocki 18976fc25088SRafael J. Wysocki count++; 189840e7fcb1SLan Tianyu 189991438aebSRafael J. Wysocki dep->supplier = dep_devices.handles[i]; 19006fc25088SRafael J. Wysocki dep->consumer = handle; 190140e7fcb1SLan Tianyu 190240e7fcb1SLan Tianyu mutex_lock(&acpi_dep_list_lock); 190340e7fcb1SLan Tianyu list_add_tail(&dep->node , &acpi_dep_list); 190440e7fcb1SLan Tianyu mutex_unlock(&acpi_dep_list_lock); 190540e7fcb1SLan Tianyu } 19066fc25088SRafael J. Wysocki 19076fc25088SRafael J. Wysocki return count; 190840e7fcb1SLan Tianyu } 190940e7fcb1SLan Tianyu 191071da201fSRafael J. Wysocki static void acpi_scan_dep_init(struct acpi_device *adev) 191171da201fSRafael J. Wysocki { 191271da201fSRafael J. Wysocki struct acpi_dep_data *dep; 191371da201fSRafael J. Wysocki 191471da201fSRafael J. Wysocki mutex_lock(&acpi_dep_list_lock); 191571da201fSRafael J. Wysocki 191671da201fSRafael J. Wysocki list_for_each_entry(dep, &acpi_dep_list, node) { 191771da201fSRafael J. Wysocki if (dep->consumer == adev->handle) 191871da201fSRafael J. Wysocki adev->dep_unmet++; 191971da201fSRafael J. Wysocki } 192071da201fSRafael J. Wysocki 192171da201fSRafael J. Wysocki mutex_unlock(&acpi_dep_list_lock); 192271da201fSRafael J. Wysocki } 192371da201fSRafael J. Wysocki 19240de7fb7cSRafael J. Wysocki static bool acpi_bus_scan_second_pass; 19250de7fb7cSRafael J. Wysocki 192671da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep, 192771da201fSRafael J. Wysocki struct acpi_device **adev_p) 19281da177e4SLinus Torvalds { 1929805d410fSRafael J. Wysocki struct acpi_device *device = NULL; 1930778cbc1dSBjorn Helgaas unsigned long long sta; 19316fc25088SRafael J. Wysocki int type; 1932778cbc1dSBjorn Helgaas int result; 19331da177e4SLinus Torvalds 19344002bf38SRafael J. Wysocki acpi_bus_get_device(handle, &device); 19354002bf38SRafael J. Wysocki if (device) 19364002bf38SRafael J. Wysocki goto out; 19374002bf38SRafael J. Wysocki 1938778cbc1dSBjorn Helgaas result = acpi_bus_type_and_status(handle, &type, &sta); 1939778cbc1dSBjorn Helgaas if (result) 194051a85fafSBjorn Helgaas return AE_OK; 19411da177e4SLinus Torvalds 194282c7d5efSRafael J. Wysocki if (type == ACPI_BUS_TYPE_POWER) { 194382c7d5efSRafael J. Wysocki acpi_add_power_resource(handle); 194482c7d5efSRafael J. Wysocki return AE_OK; 194582c7d5efSRafael J. Wysocki } 194682c7d5efSRafael J. Wysocki 194771da201fSRafael J. Wysocki if (type == ACPI_BUS_TYPE_DEVICE && check_dep) { 194871da201fSRafael J. Wysocki u32 count = acpi_scan_check_dep(handle); 194971da201fSRafael J. Wysocki /* Bail out if the number of recorded dependencies is not 0. */ 19500de7fb7cSRafael J. Wysocki if (count > 0) { 19510de7fb7cSRafael J. Wysocki acpi_bus_scan_second_pass = true; 195271da201fSRafael J. Wysocki return AE_CTRL_DEPTH; 195371da201fSRafael J. Wysocki } 19540de7fb7cSRafael J. Wysocki } 19556fc25088SRafael J. Wysocki 19562c0d4fe0SRafael J. Wysocki acpi_add_single_object(&device, handle, type, sta); 1957e3b87f8aSBjorn Helgaas if (!device) 195851a85fafSBjorn Helgaas return AE_CTRL_DEPTH; 19591da177e4SLinus Torvalds 19603c2cc7ffSRafael J. Wysocki acpi_scan_init_hotplug(device); 196171da201fSRafael J. Wysocki if (!check_dep) 196271da201fSRafael J. Wysocki acpi_scan_dep_init(device); 19633c2cc7ffSRafael J. Wysocki 19644002bf38SRafael J. Wysocki out: 196571da201fSRafael J. Wysocki if (!*adev_p) 196671da201fSRafael J. Wysocki *adev_p = device; 1967805d410fSRafael J. Wysocki 196851a85fafSBjorn Helgaas return AE_OK; 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds 197171da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used, 197271da201fSRafael J. Wysocki void *not_used, void **ret_p) 197371da201fSRafael J. Wysocki { 197471da201fSRafael J. Wysocki return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p); 197571da201fSRafael J. Wysocki } 197671da201fSRafael J. Wysocki 197771da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used, 197871da201fSRafael J. Wysocki void *not_used, void **ret_p) 197971da201fSRafael J. Wysocki { 198071da201fSRafael J. Wysocki return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p); 198171da201fSRafael J. Wysocki } 198271da201fSRafael J. Wysocki 198348459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device) 198448459340SZhang Rui { 198548459340SZhang Rui /* 1986d87fb091SJohn Garry * Do not enumerate devices with enumeration_by_parent flag set as 1987d87fb091SJohn Garry * they will be enumerated by their respective parents. 198848459340SZhang Rui */ 1989d87fb091SJohn Garry if (!device->flags.enumeration_by_parent) { 19901571875bSHeikki Krogerus acpi_create_platform_device(device, NULL); 199110c7e20bSOctavian Purdila acpi_device_set_enumerated(device); 199268bdb677SOctavian Purdila } else { 199368bdb677SOctavian Purdila blocking_notifier_call_chain(&acpi_reconfig_chain, 199468bdb677SOctavian Purdila ACPI_RECONFIG_DEVICE_ADD, device); 199510c7e20bSOctavian Purdila } 199648459340SZhang Rui } 199748459340SZhang Rui 19987d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = { 1999ee892094SRafael J. Wysocki {ACPI_DT_NAMESPACE_HID, }, 20007d284352SRafael J. Wysocki {"", }, 20017d284352SRafael J. Wysocki }; 20027d284352SRafael J. Wysocki 20037d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev, 20047d284352SRafael J. Wysocki const struct acpi_device_id *not_used) 20057d284352SRafael J. Wysocki { 20067d284352SRafael J. Wysocki /* 2007ee892094SRafael J. Wysocki * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test 2008ee892094SRafael J. Wysocki * below can be unconditional. 20097d284352SRafael J. Wysocki */ 20107d284352SRafael J. Wysocki if (adev->data.of_compatible) 20117d284352SRafael J. Wysocki acpi_default_enumeration(adev); 20127d284352SRafael J. Wysocki 20137d284352SRafael J. Wysocki return 1; 20147d284352SRafael J. Wysocki } 20157d284352SRafael J. Wysocki 20167d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = { 20177d284352SRafael J. Wysocki .ids = generic_device_ids, 20187d284352SRafael J. Wysocki .attach = acpi_generic_device_attach, 20197d284352SRafael J. Wysocki }; 20207d284352SRafael J. Wysocki 202187b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device) 202287b85b3cSRafael J. Wysocki { 202387b85b3cSRafael J. Wysocki struct acpi_hardware_id *hwid; 202487b85b3cSRafael J. Wysocki int ret = 0; 202587b85b3cSRafael J. Wysocki 202687b85b3cSRafael J. Wysocki list_for_each_entry(hwid, &device->pnp.ids, list) { 2027c5698074SRafael J. Wysocki const struct acpi_device_id *devid; 2028c5698074SRafael J. Wysocki struct acpi_scan_handler *handler; 202987b85b3cSRafael J. Wysocki 2030c5698074SRafael J. Wysocki handler = acpi_scan_match_handler(hwid->id, &devid); 2031c5698074SRafael J. Wysocki if (handler) { 2032d34afa9dSRafael J. Wysocki if (!handler->attach) { 2033d34afa9dSRafael J. Wysocki device->pnp.type.platform_id = 0; 2034d34afa9dSRafael J. Wysocki continue; 2035d34afa9dSRafael J. Wysocki } 2036c5698074SRafael J. Wysocki device->handler = handler; 20379cb32acfSRafael J. Wysocki ret = handler->attach(device, devid); 20389cb32acfSRafael J. Wysocki if (ret > 0) 2039c5698074SRafael J. Wysocki break; 20409cb32acfSRafael J. Wysocki 20419cb32acfSRafael J. Wysocki device->handler = NULL; 20429cb32acfSRafael J. Wysocki if (ret < 0) 2043c5698074SRafael J. Wysocki break; 2044c5698074SRafael J. Wysocki } 2045c5698074SRafael J. Wysocki } 204648459340SZhang Rui 2047ca589f94SRafael J. Wysocki return ret; 2048ca589f94SRafael J. Wysocki } 2049ca589f94SRafael J. Wysocki 205071da201fSRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device, bool first_pass) 205151a85fafSBjorn Helgaas { 20522c22e652SRafael J. Wysocki struct acpi_device *child; 205371da201fSRafael J. Wysocki bool skip = !first_pass && device->flags.visited; 20541e2380cdSRafael J. Wysocki acpi_handle ejd; 2055ca589f94SRafael J. Wysocki int ret; 20561da177e4SLinus Torvalds 205771da201fSRafael J. Wysocki if (skip) 205871da201fSRafael J. Wysocki goto ok; 205971da201fSRafael J. Wysocki 20601e2380cdSRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd))) 20611e2380cdSRafael J. Wysocki register_dock_dependent_device(device, ejd); 20621e2380cdSRafael J. Wysocki 20632c22e652SRafael J. Wysocki acpi_bus_get_status(device); 2064202317a5SRafael J. Wysocki /* Skip devices that are not present. */ 20652c22e652SRafael J. Wysocki if (!acpi_device_is_present(device)) { 2066cde1f95fSSakari Ailus device->flags.initialized = false; 206710c7e20bSOctavian Purdila acpi_device_clear_enumerated(device); 20681b1f3e16SRafael J. Wysocki device->flags.power_manageable = 0; 20692c22e652SRafael J. Wysocki return; 20702c22e652SRafael J. Wysocki } 20713a391a39SRafael J. Wysocki if (device->handler) 20722c22e652SRafael J. Wysocki goto ok; 20733a391a39SRafael J. Wysocki 2074202317a5SRafael J. Wysocki if (!device->flags.initialized) { 20751b1f3e16SRafael J. Wysocki device->flags.power_manageable = 20761b1f3e16SRafael J. Wysocki device->power.states[ACPI_STATE_D0].flags.valid; 20771b1f3e16SRafael J. Wysocki if (acpi_bus_init_power(device)) 20781b1f3e16SRafael J. Wysocki device->flags.power_manageable = 0; 20791b1f3e16SRafael J. Wysocki 2080202317a5SRafael J. Wysocki device->flags.initialized = true; 2081c381fc3aSRafael J. Wysocki } else if (device->flags.visited) { 2082c381fc3aSRafael J. Wysocki goto ok; 2083202317a5SRafael J. Wysocki } 208410c7e20bSOctavian Purdila 2085ca589f94SRafael J. Wysocki ret = acpi_scan_attach_handler(device); 20866931007cSRafael J. Wysocki if (ret < 0) 20872c22e652SRafael J. Wysocki return; 20886931007cSRafael J. Wysocki 20896931007cSRafael J. Wysocki device->flags.match_driver = true; 2090d87fb091SJohn Garry if (ret > 0 && !device->flags.enumeration_by_parent) { 2091f406270bSRafael J. Wysocki acpi_device_set_enumerated(device); 2092f406270bSRafael J. Wysocki goto ok; 2093f406270bSRafael J. Wysocki } 2094f406270bSRafael J. Wysocki 2095ca589f94SRafael J. Wysocki ret = device_attach(&device->dev); 2096202317a5SRafael J. Wysocki if (ret < 0) 20972c22e652SRafael J. Wysocki return; 20987d284352SRafael J. Wysocki 2099d87fb091SJohn Garry if (device->pnp.type.platform_id || device->flags.enumeration_by_parent) 2100e4330d8bSJarkko Nikula acpi_default_enumeration(device); 2101d87fb091SJohn Garry else 2102d87fb091SJohn Garry acpi_device_set_enumerated(device); 2103202317a5SRafael J. Wysocki 2104202317a5SRafael J. Wysocki ok: 21052c22e652SRafael J. Wysocki list_for_each_entry(child, &device->children, node) 210671da201fSRafael J. Wysocki acpi_bus_attach(child, first_pass); 21078ab17fc9SRafael J. Wysocki 210871da201fSRafael J. Wysocki if (!skip && device->handler && device->handler->hotplug.notify_online) 21098ab17fc9SRafael J. Wysocki device->handler->hotplug.notify_online(device); 2110805d410fSRafael J. Wysocki } 21113fb02738SRajesh Shah 211240e7fcb1SLan Tianyu void acpi_walk_dep_device_list(acpi_handle handle) 211340e7fcb1SLan Tianyu { 211440e7fcb1SLan Tianyu struct acpi_dep_data *dep, *tmp; 211540e7fcb1SLan Tianyu struct acpi_device *adev; 211640e7fcb1SLan Tianyu 211740e7fcb1SLan Tianyu mutex_lock(&acpi_dep_list_lock); 211840e7fcb1SLan Tianyu list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) { 211991438aebSRafael J. Wysocki if (dep->supplier == handle) { 212091438aebSRafael J. Wysocki acpi_bus_get_device(dep->consumer, &adev); 212140e7fcb1SLan Tianyu if (!adev) 212240e7fcb1SLan Tianyu continue; 212340e7fcb1SLan Tianyu 212440e7fcb1SLan Tianyu adev->dep_unmet--; 212540e7fcb1SLan Tianyu if (!adev->dep_unmet) 212671da201fSRafael J. Wysocki acpi_bus_attach(adev, true); 212771da201fSRafael J. Wysocki 212840e7fcb1SLan Tianyu list_del(&dep->node); 212940e7fcb1SLan Tianyu kfree(dep); 213040e7fcb1SLan Tianyu } 213140e7fcb1SLan Tianyu } 213240e7fcb1SLan Tianyu mutex_unlock(&acpi_dep_list_lock); 213340e7fcb1SLan Tianyu } 213440e7fcb1SLan Tianyu EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list); 213540e7fcb1SLan Tianyu 2136b8bd759aSRafael J. Wysocki /** 2137b8bd759aSRafael J. Wysocki * acpi_bus_scan - Add ACPI device node objects in a given namespace scope. 2138b8bd759aSRafael J. Wysocki * @handle: Root of the namespace scope to scan. 2139b8bd759aSRafael J. Wysocki * 2140b8bd759aSRafael J. Wysocki * Scan a given ACPI tree (probably recently hot-plugged) and create and add 2141b8bd759aSRafael J. Wysocki * found devices. 2142b8bd759aSRafael J. Wysocki * 2143b8bd759aSRafael J. Wysocki * If no devices were found, -ENODEV is returned, but it does not mean that 2144b8bd759aSRafael J. Wysocki * there has been a real error. There just have been no suitable ACPI objects 2145b8bd759aSRafael J. Wysocki * in the table trunk from which the kernel could create a device and add an 2146b8bd759aSRafael J. Wysocki * appropriate driver. 21473757b948SRafael J. Wysocki * 21483757b948SRafael J. Wysocki * Must be called under acpi_scan_lock. 2149b8bd759aSRafael J. Wysocki */ 2150b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle) 21513fb02738SRajesh Shah { 215271da201fSRafael J. Wysocki struct acpi_device *device = NULL; 2153c511cc19SRafael J. Wysocki 21540de7fb7cSRafael J. Wysocki acpi_bus_scan_second_pass = false; 21550de7fb7cSRafael J. Wysocki 215671da201fSRafael J. Wysocki /* Pass 1: Avoid enumerating devices with missing dependencies. */ 215771da201fSRafael J. Wysocki 215871da201fSRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device))) 21591da177e4SLinus Torvalds acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 216071da201fSRafael J. Wysocki acpi_bus_check_add_1, NULL, NULL, 216171da201fSRafael J. Wysocki (void **)&device); 21623fb02738SRajesh Shah 216371da201fSRafael J. Wysocki if (!device) 21642c22e652SRafael J. Wysocki return -ENODEV; 216571da201fSRafael J. Wysocki 216671da201fSRafael J. Wysocki acpi_bus_attach(device, true); 216771da201fSRafael J. Wysocki 21680de7fb7cSRafael J. Wysocki if (!acpi_bus_scan_second_pass) 21690de7fb7cSRafael J. Wysocki return 0; 21700de7fb7cSRafael J. Wysocki 217171da201fSRafael J. Wysocki /* Pass 2: Enumerate all of the remaining devices. */ 217271da201fSRafael J. Wysocki 217371da201fSRafael J. Wysocki device = NULL; 217471da201fSRafael J. Wysocki 217571da201fSRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device))) 217671da201fSRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 217771da201fSRafael J. Wysocki acpi_bus_check_add_2, NULL, NULL, 217871da201fSRafael J. Wysocki (void **)&device); 217971da201fSRafael J. Wysocki 218071da201fSRafael J. Wysocki acpi_bus_attach(device, false); 218171da201fSRafael J. Wysocki 218271da201fSRafael J. Wysocki return 0; 21833fb02738SRajesh Shah } 2184b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan); 21851da177e4SLinus Torvalds 21863757b948SRafael J. Wysocki /** 21872c22e652SRafael J. Wysocki * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects. 21882c22e652SRafael J. Wysocki * @adev: Root of the ACPI namespace scope to walk. 21893757b948SRafael J. Wysocki * 21903757b948SRafael J. Wysocki * Must be called under acpi_scan_lock. 21913757b948SRafael J. Wysocki */ 21922c22e652SRafael J. Wysocki void acpi_bus_trim(struct acpi_device *adev) 21931da177e4SLinus Torvalds { 21942c22e652SRafael J. Wysocki struct acpi_scan_handler *handler = adev->handler; 21952c22e652SRafael J. Wysocki struct acpi_device *child; 21962c22e652SRafael J. Wysocki 21972c22e652SRafael J. Wysocki list_for_each_entry_reverse(child, &adev->children, node) 21982c22e652SRafael J. Wysocki acpi_bus_trim(child); 21992c22e652SRafael J. Wysocki 2200a951c773SRafael J. Wysocki adev->flags.match_driver = false; 22012c22e652SRafael J. Wysocki if (handler) { 22022c22e652SRafael J. Wysocki if (handler->detach) 22032c22e652SRafael J. Wysocki handler->detach(adev); 22042c22e652SRafael J. Wysocki 22052c22e652SRafael J. Wysocki adev->handler = NULL; 22062c22e652SRafael J. Wysocki } else { 22072c22e652SRafael J. Wysocki device_release_driver(&adev->dev); 22082c22e652SRafael J. Wysocki } 22091da177e4SLinus Torvalds /* 22102c22e652SRafael J. Wysocki * Most likely, the device is going away, so put it into D3cold before 22112c22e652SRafael J. Wysocki * that. 22121da177e4SLinus Torvalds */ 22132c22e652SRafael J. Wysocki acpi_device_set_power(adev, ACPI_STATE_D3_COLD); 22142c22e652SRafael J. Wysocki adev->flags.initialized = false; 221510c7e20bSOctavian Purdila acpi_device_clear_enumerated(adev); 22161da177e4SLinus Torvalds } 2217ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim); 2218ceaba663SKristen Accardi 2219a64a62ceSLv Zheng int acpi_bus_register_early_device(int type) 2220a64a62ceSLv Zheng { 2221a64a62ceSLv Zheng struct acpi_device *device = NULL; 2222a64a62ceSLv Zheng int result; 2223a64a62ceSLv Zheng 2224a64a62ceSLv Zheng result = acpi_add_single_object(&device, NULL, 2225a64a62ceSLv Zheng type, ACPI_STA_DEFAULT); 2226a64a62ceSLv Zheng if (result) 2227a64a62ceSLv Zheng return result; 2228a64a62ceSLv Zheng 2229a64a62ceSLv Zheng device->flags.match_driver = true; 2230a64a62ceSLv Zheng return device_attach(&device->dev); 2231a64a62ceSLv Zheng } 2232a64a62ceSLv Zheng EXPORT_SYMBOL_GPL(acpi_bus_register_early_device); 2233a64a62ceSLv Zheng 2234e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void) 22351da177e4SLinus Torvalds { 22361da177e4SLinus Torvalds int result = 0; 2237c4168bffSLi Shaohua 22381da177e4SLinus Torvalds /* 22391da177e4SLinus Torvalds * Enumerate all fixed-feature devices. 22401da177e4SLinus Torvalds */ 22412c0d4fe0SRafael J. Wysocki if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) { 22422c0d4fe0SRafael J. Wysocki struct acpi_device *device = NULL; 22432c0d4fe0SRafael J. Wysocki 22445c478f49SBjorn Helgaas result = acpi_add_single_object(&device, NULL, 2245c4168bffSLi Shaohua ACPI_BUS_TYPE_POWER_BUTTON, 22462c0d4fe0SRafael J. Wysocki ACPI_STA_DEFAULT); 22472c0d4fe0SRafael J. Wysocki if (result) 22482c0d4fe0SRafael J. Wysocki return result; 22492c0d4fe0SRafael J. Wysocki 225088346167SRafael J. Wysocki device->flags.match_driver = true; 22512c0d4fe0SRafael J. Wysocki result = device_attach(&device->dev); 22522c0d4fe0SRafael J. Wysocki if (result < 0) 22532c0d4fe0SRafael J. Wysocki return result; 22542c0d4fe0SRafael J. Wysocki 2255c10d7a13SDaniel Drake device_init_wakeup(&device->dev, true); 22563fb02738SRajesh Shah } 22571da177e4SLinus Torvalds 22582c0d4fe0SRafael J. Wysocki if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) { 22592c0d4fe0SRafael J. Wysocki struct acpi_device *device = NULL; 22602c0d4fe0SRafael J. Wysocki 22615c478f49SBjorn Helgaas result = acpi_add_single_object(&device, NULL, 2262c4168bffSLi Shaohua ACPI_BUS_TYPE_SLEEP_BUTTON, 22632c0d4fe0SRafael J. Wysocki ACPI_STA_DEFAULT); 22642c0d4fe0SRafael J. Wysocki if (result) 22652c0d4fe0SRafael J. Wysocki return result; 22662c0d4fe0SRafael J. Wysocki 226788346167SRafael J. Wysocki device->flags.match_driver = true; 22682c0d4fe0SRafael J. Wysocki result = device_attach(&device->dev); 22693fb02738SRajesh Shah } 22701da177e4SLinus Torvalds 22712c0d4fe0SRafael J. Wysocki return result < 0 ? result : 0; 22721da177e4SLinus Torvalds } 22731da177e4SLinus Torvalds 2274a4e081b0SShannon Zhao static void __init acpi_get_spcr_uart_addr(void) 2275a4e081b0SShannon Zhao { 2276a4e081b0SShannon Zhao acpi_status status; 2277a4e081b0SShannon Zhao struct acpi_table_spcr *spcr_ptr; 2278a4e081b0SShannon Zhao 2279a4e081b0SShannon Zhao status = acpi_get_table(ACPI_SIG_SPCR, 0, 2280a4e081b0SShannon Zhao (struct acpi_table_header **)&spcr_ptr); 2281ead7ba13SHanjun Guo if (ACPI_FAILURE(status)) { 2282ead7ba13SHanjun Guo pr_warn(PREFIX "STAO table present, but SPCR is missing\n"); 2283ead7ba13SHanjun Guo return; 2284ead7ba13SHanjun Guo } 2285ead7ba13SHanjun Guo 2286a4e081b0SShannon Zhao spcr_uart_addr = spcr_ptr->serial_port.address; 2287ead7ba13SHanjun Guo acpi_put_table((struct acpi_table_header *)spcr_ptr); 2288a4e081b0SShannon Zhao } 2289a4e081b0SShannon Zhao 229068bdb677SOctavian Purdila static bool acpi_scan_initialized; 229168bdb677SOctavian Purdila 2292e747f274SBjorn Helgaas int __init acpi_scan_init(void) 22931da177e4SLinus Torvalds { 22941da177e4SLinus Torvalds int result; 2295a4e081b0SShannon Zhao acpi_status status; 2296a4e081b0SShannon Zhao struct acpi_table_stao *stao_ptr; 22971da177e4SLinus Torvalds 229892ef2a25SRafael J. Wysocki acpi_pci_root_init(); 22994daeaf68SRafael J. Wysocki acpi_pci_link_init(); 2300ac212b69SRafael J. Wysocki acpi_processor_init(); 2301cb0701acSAndy Shevchenko acpi_platform_init(); 2302f58b082aSRafael J. Wysocki acpi_lpss_init(); 230392082a88SKen Xue acpi_apd_init(); 23042fa97febSLan Tianyu acpi_cmos_rtc_init(); 2305737f1a9fSRafael J. Wysocki acpi_container_init(); 23060a347644SRafael J. Wysocki acpi_memory_hotplug_init(); 2307cc6a0e31SMika Westerberg acpi_watchdog_init(); 2308eec15edbSZhang Rui acpi_pnp_init(); 23093230bbfcSZhang Rui acpi_int340x_thermal_init(); 23106ce2e188SGraeme Gregory acpi_amba_init(); 2311eeb2d80dSSrinivas Pandruvada acpi_init_lpit(); 231297d9a9e9SRafael J. Wysocki 23137d284352SRafael J. Wysocki acpi_scan_add_handler(&generic_device_handler); 23147d284352SRafael J. Wysocki 2315a4e081b0SShannon Zhao /* 2316a4e081b0SShannon Zhao * If there is STAO table, check whether it needs to ignore the UART 2317a4e081b0SShannon Zhao * device in SPCR table. 2318a4e081b0SShannon Zhao */ 2319a4e081b0SShannon Zhao status = acpi_get_table(ACPI_SIG_STAO, 0, 2320a4e081b0SShannon Zhao (struct acpi_table_header **)&stao_ptr); 2321a4e081b0SShannon Zhao if (ACPI_SUCCESS(status)) { 2322a4e081b0SShannon Zhao if (stao_ptr->header.length > sizeof(struct acpi_table_stao)) 2323ead7ba13SHanjun Guo pr_info(PREFIX "STAO Name List not yet supported.\n"); 2324a4e081b0SShannon Zhao 2325a4e081b0SShannon Zhao if (stao_ptr->ignore_uart) 2326a4e081b0SShannon Zhao acpi_get_spcr_uart_addr(); 2327ead7ba13SHanjun Guo 2328ead7ba13SHanjun Guo acpi_put_table((struct acpi_table_header *)stao_ptr); 2329a4e081b0SShannon Zhao } 2330a4e081b0SShannon Zhao 2331eb7f43c4SRafael J. Wysocki acpi_gpe_apply_masked_gpes(); 2332eb7f43c4SRafael J. Wysocki acpi_update_all_gpes(); 2333eb7f43c4SRafael J. Wysocki 23347291edcaSDavid Hildenbrand /* 23357291edcaSDavid Hildenbrand * Although we call __add_memory() that is documented to require the 23367291edcaSDavid Hildenbrand * device_hotplug_lock, it is not necessary here because this is an 23377291edcaSDavid Hildenbrand * early code when userspace or any other code path cannot trigger 23387291edcaSDavid Hildenbrand * hotplug/hotunplug operations. 23397291edcaSDavid Hildenbrand */ 23403757b948SRafael J. Wysocki mutex_lock(&acpi_scan_lock); 23411da177e4SLinus Torvalds /* 23421da177e4SLinus Torvalds * Enumerate devices in the ACPI namespace. 23431da177e4SLinus Torvalds */ 23440cd6ac52SRafael J. Wysocki result = acpi_bus_scan(ACPI_ROOT_OBJECT); 23451da177e4SLinus Torvalds if (result) 23463757b948SRafael J. Wysocki goto out; 23471da177e4SLinus Torvalds 23480cd6ac52SRafael J. Wysocki result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root); 23491da177e4SLinus Torvalds if (result) 23503757b948SRafael J. Wysocki goto out; 2351b8bd759aSRafael J. Wysocki 23522650ef42SAaron Lu /* Fixed feature devices do not exist on HW-reduced platform */ 23532650ef42SAaron Lu if (!acpi_gbl_reduced_hardware) { 2354b8bd759aSRafael J. Wysocki result = acpi_bus_scan_fixed(); 2355b8bd759aSRafael J. Wysocki if (result) { 23562650ef42SAaron Lu acpi_detach_data(acpi_root->handle, 23572650ef42SAaron Lu acpi_scan_drop_device); 2358202317a5SRafael J. Wysocki acpi_device_del(acpi_root); 2359202317a5SRafael J. Wysocki put_device(&acpi_root->dev); 23603757b948SRafael J. Wysocki goto out; 2361b8bd759aSRafael J. Wysocki } 23622650ef42SAaron Lu } 2363b8bd759aSRafael J. Wysocki 236468bdb677SOctavian Purdila acpi_scan_initialized = true; 236568bdb677SOctavian Purdila 23663757b948SRafael J. Wysocki out: 23673757b948SRafael J. Wysocki mutex_unlock(&acpi_scan_lock); 23683757b948SRafael J. Wysocki return result; 23691da177e4SLinus Torvalds } 2370e647b532SMarc Zyngier 2371e647b532SMarc Zyngier static struct acpi_probe_entry *ape; 2372e647b532SMarc Zyngier static int acpi_probe_count; 23735331d9caSLorenzo Pieralisi static DEFINE_MUTEX(acpi_probe_mutex); 2374e647b532SMarc Zyngier 237560574d1eSKeith Busch static int __init acpi_match_madt(union acpi_subtable_headers *header, 2376e647b532SMarc Zyngier const unsigned long end) 2377e647b532SMarc Zyngier { 237860574d1eSKeith Busch if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape)) 2379e647b532SMarc Zyngier if (!ape->probe_subtbl(header, end)) 2380e647b532SMarc Zyngier acpi_probe_count++; 2381e647b532SMarc Zyngier 2382e647b532SMarc Zyngier return 0; 2383e647b532SMarc Zyngier } 2384e647b532SMarc Zyngier 2385e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr) 2386e647b532SMarc Zyngier { 2387e647b532SMarc Zyngier int count = 0; 2388e647b532SMarc Zyngier 2389e647b532SMarc Zyngier if (acpi_disabled) 2390e647b532SMarc Zyngier return 0; 2391e647b532SMarc Zyngier 23925331d9caSLorenzo Pieralisi mutex_lock(&acpi_probe_mutex); 2393e647b532SMarc Zyngier for (ape = ap_head; nr; ape++, nr--) { 23945599fb69SBob Moore if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) { 2395e647b532SMarc Zyngier acpi_probe_count = 0; 2396e647b532SMarc Zyngier acpi_table_parse_madt(ape->type, acpi_match_madt, 0); 2397e647b532SMarc Zyngier count += acpi_probe_count; 2398e647b532SMarc Zyngier } else { 2399e647b532SMarc Zyngier int res; 2400e647b532SMarc Zyngier res = acpi_table_parse(ape->id, ape->probe_table); 2401e647b532SMarc Zyngier if (!res) 2402e647b532SMarc Zyngier count++; 2403e647b532SMarc Zyngier } 2404e647b532SMarc Zyngier } 24055331d9caSLorenzo Pieralisi mutex_unlock(&acpi_probe_mutex); 2406e647b532SMarc Zyngier 2407e647b532SMarc Zyngier return count; 2408e647b532SMarc Zyngier } 240968bdb677SOctavian Purdila 241068bdb677SOctavian Purdila struct acpi_table_events_work { 241168bdb677SOctavian Purdila struct work_struct work; 241268bdb677SOctavian Purdila void *table; 241368bdb677SOctavian Purdila u32 event; 241468bdb677SOctavian Purdila }; 241568bdb677SOctavian Purdila 241668bdb677SOctavian Purdila static void acpi_table_events_fn(struct work_struct *work) 241768bdb677SOctavian Purdila { 241868bdb677SOctavian Purdila struct acpi_table_events_work *tew; 241968bdb677SOctavian Purdila 242068bdb677SOctavian Purdila tew = container_of(work, struct acpi_table_events_work, work); 242168bdb677SOctavian Purdila 242268bdb677SOctavian Purdila if (tew->event == ACPI_TABLE_EVENT_LOAD) { 242368bdb677SOctavian Purdila acpi_scan_lock_acquire(); 242468bdb677SOctavian Purdila acpi_bus_scan(ACPI_ROOT_OBJECT); 242568bdb677SOctavian Purdila acpi_scan_lock_release(); 242668bdb677SOctavian Purdila } 242768bdb677SOctavian Purdila 242868bdb677SOctavian Purdila kfree(tew); 242968bdb677SOctavian Purdila } 243068bdb677SOctavian Purdila 243168bdb677SOctavian Purdila void acpi_scan_table_handler(u32 event, void *table, void *context) 243268bdb677SOctavian Purdila { 243368bdb677SOctavian Purdila struct acpi_table_events_work *tew; 243468bdb677SOctavian Purdila 243568bdb677SOctavian Purdila if (!acpi_scan_initialized) 243668bdb677SOctavian Purdila return; 243768bdb677SOctavian Purdila 243868bdb677SOctavian Purdila if (event != ACPI_TABLE_EVENT_LOAD) 243968bdb677SOctavian Purdila return; 244068bdb677SOctavian Purdila 244168bdb677SOctavian Purdila tew = kmalloc(sizeof(*tew), GFP_KERNEL); 244268bdb677SOctavian Purdila if (!tew) 244368bdb677SOctavian Purdila return; 244468bdb677SOctavian Purdila 244568bdb677SOctavian Purdila INIT_WORK(&tew->work, acpi_table_events_fn); 244668bdb677SOctavian Purdila tew->table = table; 244768bdb677SOctavian Purdila tew->event = event; 244868bdb677SOctavian Purdila 244968bdb677SOctavian Purdila schedule_work(&tew->work); 245068bdb677SOctavian Purdila } 245168bdb677SOctavian Purdila 245268bdb677SOctavian Purdila int acpi_reconfig_notifier_register(struct notifier_block *nb) 245368bdb677SOctavian Purdila { 245468bdb677SOctavian Purdila return blocking_notifier_chain_register(&acpi_reconfig_chain, nb); 245568bdb677SOctavian Purdila } 245668bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_register); 245768bdb677SOctavian Purdila 245868bdb677SOctavian Purdila int acpi_reconfig_notifier_unregister(struct notifier_block *nb) 245968bdb677SOctavian Purdila { 246068bdb677SOctavian Purdila return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb); 246168bdb677SOctavian Purdila } 246268bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_unregister); 2463