xref: /openbmc/linux/drivers/acpi/scan.c (revision c10383e8ddf4810b9a5c1595404c2724d925a0a6)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * scan.c - support for transforming the ACPI namespace into individual objects
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
68acf4108SHanjun Guo #define pr_fmt(fmt) "ACPI: " fmt
78acf4108SHanjun Guo 
81da177e4SLinus Torvalds #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/init.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
119b6d97b6SRandy Dunlap #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/acpi.h>
13643b8e4dSLorenzo Pieralisi #include <linux/acpi_iort.h>
143cf48554SJean-Philippe Brucker #include <linux/acpi_viot.h>
1511a8c5e3SJean-Philippe Brucker #include <linux/iommu.h>
1674523c90SAlok N Kataria #include <linux/signal.h>
1774523c90SAlok N Kataria #include <linux/kthread.h>
18222e82acSDarrick J. Wong #include <linux/dmi.h>
190a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
20ca9ef3abSLukas Wunner #include <linux/platform_data/x86/apple.h>
21ca5999fdSMike Rapoport #include <linux/pgtable.h>
22d783156eSRafael J. Wysocki 
23e60cc7a6SBjorn Helgaas #include "internal.h"
24e60cc7a6SBjorn Helgaas 
251da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2829b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
291da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
301da177e4SLinus Torvalds 
31859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
32859ac9a4SBjorn Helgaas 
33d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)
34d783156eSRafael J. Wysocki 
35620e112cSThomas Renninger static const char *dummy_hid = "device";
362b2ae7c7SThomas Renninger 
3740e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list);
3840e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock);
392d12b6b3SLukas Wunner LIST_HEAD(acpi_bus_id_list);
40c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
41ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
429090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
431da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
44e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock);
451da177e4SLinus Torvalds 
46a4e081b0SShannon Zhao /*
47a4e081b0SShannon Zhao  * The UART device described by the SPCR table is the only object which needs
48a4e081b0SShannon Zhao  * special-casing. Everything else is covered by ACPI namespace paths in STAO
49a4e081b0SShannon Zhao  * table.
50a4e081b0SShannon Zhao  */
51a4e081b0SShannon Zhao static u64 spcr_uart_addr;
52a4e081b0SShannon Zhao 
533757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
543757b948SRafael J. Wysocki {
553757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
563757b948SRafael J. Wysocki }
573757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
583757b948SRafael J. Wysocki 
593757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
603757b948SRafael J. Wysocki {
613757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
623757b948SRafael J. Wysocki }
633757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
643757b948SRafael J. Wysocki 
65e525506fSRafael J. Wysocki void acpi_lock_hp_context(void)
66e525506fSRafael J. Wysocki {
67e525506fSRafael J. Wysocki 	mutex_lock(&acpi_hp_context_lock);
68e525506fSRafael J. Wysocki }
69e525506fSRafael J. Wysocki 
70e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void)
71e525506fSRafael J. Wysocki {
72e525506fSRafael J. Wysocki 	mutex_unlock(&acpi_hp_context_lock);
73e525506fSRafael J. Wysocki }
74e525506fSRafael J. Wysocki 
755d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev,
765d513205SRafael J. Wysocki 				struct acpi_hotplug_context *hp,
775d513205SRafael J. Wysocki 				int (*notify)(struct acpi_device *, u32),
785d513205SRafael J. Wysocki 				void (*uevent)(struct acpi_device *, u32))
795d513205SRafael J. Wysocki {
805d513205SRafael J. Wysocki 	acpi_lock_hp_context();
81ba574dc8SRafael J. Wysocki 	hp->notify = notify;
82ba574dc8SRafael J. Wysocki 	hp->uevent = uevent;
83ba574dc8SRafael J. Wysocki 	acpi_set_hp_context(adev, hp);
845d513205SRafael J. Wysocki 	acpi_unlock_hp_context();
855d513205SRafael J. Wysocki }
865d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
875d513205SRafael J. Wysocki 
88ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
89ca589f94SRafael J. Wysocki {
90d34afa9dSRafael J. Wysocki 	if (!handler)
91ca589f94SRafael J. Wysocki 		return -EINVAL;
92ca589f94SRafael J. Wysocki 
93ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
94ca589f94SRafael J. Wysocki 	return 0;
95ca589f94SRafael J. Wysocki }
96ca589f94SRafael J. Wysocki 
973f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
983f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
993f8055c3SRafael J. Wysocki {
1003f8055c3SRafael J. Wysocki 	int error;
1013f8055c3SRafael J. Wysocki 
1023f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
1033f8055c3SRafael J. Wysocki 	if (error)
1043f8055c3SRafael J. Wysocki 		return error;
1053f8055c3SRafael J. Wysocki 
1063f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
1073f8055c3SRafael J. Wysocki 	return 0;
1083f8055c3SRafael J. Wysocki }
1093f8055c3SRafael J. Wysocki 
110caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
111d22ddcbcSRafael J. Wysocki {
112d22ddcbcSRafael J. Wysocki 	struct acpi_device_physical_node *pn;
113d22ddcbcSRafael J. Wysocki 	bool offline = true;
11427664c58SLee, Chun-Yi 	char *envp[] = { "EVENT=offline", NULL };
115d22ddcbcSRafael J. Wysocki 
1164c533c80SRafael J. Wysocki 	/*
1174c533c80SRafael J. Wysocki 	 * acpi_container_offline() calls this for all of the container's
1184c533c80SRafael J. Wysocki 	 * children under the container's physical_node_lock lock.
1194c533c80SRafael J. Wysocki 	 */
1204c533c80SRafael J. Wysocki 	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
121d22ddcbcSRafael J. Wysocki 
122d22ddcbcSRafael J. Wysocki 	list_for_each_entry(pn, &adev->physical_node_list, node)
123d22ddcbcSRafael J. Wysocki 		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
124caa73ea1SRafael J. Wysocki 			if (uevent)
12527664c58SLee, Chun-Yi 				kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
126caa73ea1SRafael J. Wysocki 
127d22ddcbcSRafael J. Wysocki 			offline = false;
128d22ddcbcSRafael J. Wysocki 			break;
129d22ddcbcSRafael J. Wysocki 		}
130d22ddcbcSRafael J. Wysocki 
131d22ddcbcSRafael J. Wysocki 	mutex_unlock(&adev->physical_node_lock);
132d22ddcbcSRafael J. Wysocki 	return offline;
133d22ddcbcSRafael J. Wysocki }
134d22ddcbcSRafael J. Wysocki 
1357f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
1367f28ddecSRafael J. Wysocki 				    void **ret_p)
137683058e3SRafael J. Wysocki {
138683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
139683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
140303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
141683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
142683058e3SRafael J. Wysocki 
143683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
144683058e3SRafael J. Wysocki 		return AE_OK;
145683058e3SRafael J. Wysocki 
1467f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
1477f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
1487f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
1497f28ddecSRafael J. Wysocki 	}
1507f28ddecSRafael J. Wysocki 
151683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
152683058e3SRafael J. Wysocki 
153683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
154683058e3SRafael J. Wysocki 		int ret;
155683058e3SRafael J. Wysocki 
156303bfdb1SRafael J. Wysocki 		if (second_pass) {
157303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
158303bfdb1SRafael J. Wysocki 			if (pn->put_online)
159303bfdb1SRafael J. Wysocki 				continue;
160303bfdb1SRafael J. Wysocki 		} else {
161303bfdb1SRafael J. Wysocki 			pn->put_online = false;
162303bfdb1SRafael J. Wysocki 		}
163683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
164303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
165303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
166303bfdb1SRafael J. Wysocki 		} else {
167303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
168303bfdb1SRafael J. Wysocki 			if (second_pass) {
169683058e3SRafael J. Wysocki 				status = AE_ERROR;
170683058e3SRafael J. Wysocki 				break;
171683058e3SRafael J. Wysocki 			}
172303bfdb1SRafael J. Wysocki 		}
173683058e3SRafael J. Wysocki 	}
174683058e3SRafael J. Wysocki 
175683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
176683058e3SRafael J. Wysocki 
177683058e3SRafael J. Wysocki 	return status;
178683058e3SRafael J. Wysocki }
179683058e3SRafael J. Wysocki 
1807f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
1817f28ddecSRafael J. Wysocki 				   void **ret_p)
182683058e3SRafael J. Wysocki {
183683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
184683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
185683058e3SRafael J. Wysocki 
186683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
187683058e3SRafael J. Wysocki 		return AE_OK;
188683058e3SRafael J. Wysocki 
189683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
190683058e3SRafael J. Wysocki 
191683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
192683058e3SRafael J. Wysocki 		if (pn->put_online) {
193683058e3SRafael J. Wysocki 			device_online(pn->dev);
194683058e3SRafael J. Wysocki 			pn->put_online = false;
195683058e3SRafael J. Wysocki 		}
196683058e3SRafael J. Wysocki 
197683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
198683058e3SRafael J. Wysocki 
199683058e3SRafael J. Wysocki 	return AE_OK;
200683058e3SRafael J. Wysocki }
201683058e3SRafael J. Wysocki 
202d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device)
2031da177e4SLinus Torvalds {
2045993c467SYinghai Lu 	acpi_handle handle = device->handle;
205d22ddcbcSRafael J. Wysocki 	struct device *errdev = NULL;
206a33ec399SRafael J. Wysocki 	acpi_status status;
207f058cdf4SRafael J. Wysocki 
208303bfdb1SRafael J. Wysocki 	/*
209303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
210303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
211303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
212303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
213303bfdb1SRafael J. Wysocki 	 * block holds them.
214303bfdb1SRafael J. Wysocki 	 *
215303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
216303bfdb1SRafael J. Wysocki 	 */
2177f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2187f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
2197f28ddecSRafael J. Wysocki 				     (void **)&errdev);
2207f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
2217f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
222683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2237f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
2247f28ddecSRafael J. Wysocki 		return -EPERM;
2257f28ddecSRafael J. Wysocki 	}
2267f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
227303bfdb1SRafael J. Wysocki 	if (errdev) {
228303bfdb1SRafael J. Wysocki 		errdev = NULL;
229303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2307f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
2317f28ddecSRafael J. Wysocki 				    (void **)&errdev);
232ffc10d82SMichal Hocko 		if (!errdev)
2337f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
234303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
235303bfdb1SRafael J. Wysocki 
236ffc10d82SMichal Hocko 		if (errdev) {
237303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
2387f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
239303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
2407f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
2417f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
242683058e3SRafael J. Wysocki 			return -EBUSY;
243683058e3SRafael J. Wysocki 		}
244303bfdb1SRafael J. Wysocki 	}
245d22ddcbcSRafael J. Wysocki 	return 0;
246d22ddcbcSRafael J. Wysocki }
247d22ddcbcSRafael J. Wysocki 
248d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
249d22ddcbcSRafael J. Wysocki {
250d22ddcbcSRafael J. Wysocki 	acpi_handle handle = device->handle;
251d22ddcbcSRafael J. Wysocki 	unsigned long long sta;
252d22ddcbcSRafael J. Wysocki 	acpi_status status;
253d22ddcbcSRafael J. Wysocki 
254ffc10d82SMichal Hocko 	if (device->handler && device->handler->hotplug.demand_offline) {
255caa73ea1SRafael J. Wysocki 		if (!acpi_scan_is_offline(device, true))
256d22ddcbcSRafael J. Wysocki 			return -EBUSY;
257d22ddcbcSRafael J. Wysocki 	} else {
258d22ddcbcSRafael J. Wysocki 		int error = acpi_scan_try_to_offline(device);
259d22ddcbcSRafael J. Wysocki 		if (error)
260d22ddcbcSRafael J. Wysocki 			return error;
261d22ddcbcSRafael J. Wysocki 	}
262683058e3SRafael J. Wysocki 
263e52d9d8cSRafael J. Wysocki 	acpi_handle_debug(handle, "Ejecting\n");
26426d46867SZhang Rui 
265bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
266683058e3SRafael J. Wysocki 
2677d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2681da177e4SLinus Torvalds 	/*
2691da177e4SLinus Torvalds 	 * TBD: _EJD support.
2701da177e4SLinus Torvalds 	 */
2717d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2727d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
273a33ec399SRafael J. Wysocki 		return -ENODEV;
2747d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
275a33ec399SRafael J. Wysocki 		return -EIO;
276f058cdf4SRafael J. Wysocki 
277882fd12eSToshi Kani 	/*
278882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
279882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
280882fd12eSToshi Kani 	 */
281882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
282882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
283882fd12eSToshi Kani 		acpi_handle_warn(handle,
284882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
285882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
286882fd12eSToshi Kani 		acpi_handle_warn(handle,
287882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
288882fd12eSToshi Kani 	}
289882fd12eSToshi Kani 
290a33ec399SRafael J. Wysocki 	return 0;
291a33ec399SRafael J. Wysocki }
292f058cdf4SRafael J. Wysocki 
293443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev)
294443fc820SRafael J. Wysocki {
295443fc820SRafael J. Wysocki 	if (!acpi_device_enumerated(adev)) {
296443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Still not present\n");
297443fc820SRafael J. Wysocki 		return -EALREADY;
298443fc820SRafael J. Wysocki 	}
299443fc820SRafael J. Wysocki 	acpi_bus_trim(adev);
300443fc820SRafael J. Wysocki 	return 0;
301443fc820SRafael J. Wysocki }
302443fc820SRafael J. Wysocki 
303c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev)
304a33ec399SRafael J. Wysocki {
305f943db40SRafael J. Wysocki 	int error;
306a33ec399SRafael J. Wysocki 
307443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
308443fc820SRafael J. Wysocki 	if (adev->status.present || adev->status.functional) {
309443fc820SRafael J. Wysocki 		/*
310443fc820SRafael J. Wysocki 		 * This function is only called for device objects for which
311443fc820SRafael J. Wysocki 		 * matching scan handlers exist.  The only situation in which
312443fc820SRafael J. Wysocki 		 * the scan handler is not attached to this device object yet
313443fc820SRafael J. Wysocki 		 * is when the device has just appeared (either it wasn't
314443fc820SRafael J. Wysocki 		 * present at all before or it was removed and then added
315443fc820SRafael J. Wysocki 		 * again).
316443fc820SRafael J. Wysocki 		 */
317443fc820SRafael J. Wysocki 		if (adev->handler) {
318443fc820SRafael J. Wysocki 			dev_warn(&adev->dev, "Already enumerated\n");
319443fc820SRafael J. Wysocki 			return -EALREADY;
320443fc820SRafael J. Wysocki 		}
321c27b2c33SRafael J. Wysocki 		error = acpi_bus_scan(adev->handle);
322a33ec399SRafael J. Wysocki 		if (error) {
323c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Namespace scan failure\n");
324c27b2c33SRafael J. Wysocki 			return error;
325a33ec399SRafael J. Wysocki 		}
32646394fd0SRafael J. Wysocki 		if (!adev->handler) {
327c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Enumeration failure\n");
328443fc820SRafael J. Wysocki 			error = -ENODEV;
329443fc820SRafael J. Wysocki 		}
330443fc820SRafael J. Wysocki 	} else {
331443fc820SRafael J. Wysocki 		error = acpi_scan_device_not_present(adev);
332443fc820SRafael J. Wysocki 	}
333443fc820SRafael J. Wysocki 	return error;
334443fc820SRafael J. Wysocki }
335443fc820SRafael J. Wysocki 
336443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev)
337443fc820SRafael J. Wysocki {
338443fc820SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
339443fc820SRafael J. Wysocki 	struct acpi_device *child;
340443fc820SRafael J. Wysocki 	int error;
341443fc820SRafael J. Wysocki 
342443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
343443fc820SRafael J. Wysocki 	if (!(adev->status.present || adev->status.functional)) {
344443fc820SRafael J. Wysocki 		acpi_scan_device_not_present(adev);
345443fc820SRafael J. Wysocki 		return 0;
346443fc820SRafael J. Wysocki 	}
347443fc820SRafael J. Wysocki 	if (handler && handler->hotplug.scan_dependent)
348443fc820SRafael J. Wysocki 		return handler->hotplug.scan_dependent(adev);
349443fc820SRafael J. Wysocki 
350443fc820SRafael J. Wysocki 	error = acpi_bus_scan(adev->handle);
351443fc820SRafael J. Wysocki 	if (error) {
352443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Namespace scan failure\n");
353443fc820SRafael J. Wysocki 		return error;
354443fc820SRafael J. Wysocki 	}
355443fc820SRafael J. Wysocki 	list_for_each_entry(child, &adev->children, node) {
356443fc820SRafael J. Wysocki 		error = acpi_scan_bus_check(child);
357443fc820SRafael J. Wysocki 		if (error)
358443fc820SRafael J. Wysocki 			return error;
359a33ec399SRafael J. Wysocki 	}
360c27b2c33SRafael J. Wysocki 	return 0;
361c27b2c33SRafael J. Wysocki }
362c27b2c33SRafael J. Wysocki 
3633c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
3643c2cc7ffSRafael J. Wysocki {
3653c2cc7ffSRafael J. Wysocki 	switch (type) {
3663c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
3673c2cc7ffSRafael J. Wysocki 		return acpi_scan_bus_check(adev);
3683c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
3693c2cc7ffSRafael J. Wysocki 		return acpi_scan_device_check(adev);
3703c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
3713c2cc7ffSRafael J. Wysocki 	case ACPI_OST_EC_OSPM_EJECT:
372dd2151beSRafael J. Wysocki 		if (adev->handler && !adev->handler->hotplug.enabled) {
373dd2151beSRafael J. Wysocki 			dev_info(&adev->dev, "Eject disabled\n");
374dd2151beSRafael J. Wysocki 			return -EPERM;
375dd2151beSRafael J. Wysocki 		}
376700b8422SRafael J. Wysocki 		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
377dd2151beSRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3783c2cc7ffSRafael J. Wysocki 		return acpi_scan_hot_remove(adev);
3793c2cc7ffSRafael J. Wysocki 	}
3803c2cc7ffSRafael J. Wysocki 	return -EINVAL;
3813c2cc7ffSRafael J. Wysocki }
3823c2cc7ffSRafael J. Wysocki 
3831e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src)
384c27b2c33SRafael J. Wysocki {
385c27b2c33SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
3863c2cc7ffSRafael J. Wysocki 	int error = -ENODEV;
387c27b2c33SRafael J. Wysocki 
388c27b2c33SRafael J. Wysocki 	lock_device_hotplug();
389c27b2c33SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
390c27b2c33SRafael J. Wysocki 
391c27b2c33SRafael J. Wysocki 	/*
392c27b2c33SRafael J. Wysocki 	 * The device object's ACPI handle cannot become invalid as long as we
3933c2cc7ffSRafael J. Wysocki 	 * are holding acpi_scan_lock, but it might have become invalid before
394c27b2c33SRafael J. Wysocki 	 * that lock was acquired.
395c27b2c33SRafael J. Wysocki 	 */
396c27b2c33SRafael J. Wysocki 	if (adev->handle == INVALID_ACPI_HANDLE)
3973c2cc7ffSRafael J. Wysocki 		goto err_out;
398c27b2c33SRafael J. Wysocki 
3991e2380cdSRafael J. Wysocki 	if (adev->flags.is_dock_station) {
4001e2380cdSRafael J. Wysocki 		error = dock_notify(adev, src);
4011e2380cdSRafael J. Wysocki 	} else if (adev->flags.hotplug_notify) {
4023c2cc7ffSRafael J. Wysocki 		error = acpi_generic_hotplug_event(adev, src);
4033c2cc7ffSRafael J. Wysocki 	} else {
404be27b3dcSRafael J. Wysocki 		int (*notify)(struct acpi_device *, u32);
4053c2cc7ffSRafael J. Wysocki 
4063c2cc7ffSRafael J. Wysocki 		acpi_lock_hp_context();
407be27b3dcSRafael J. Wysocki 		notify = adev->hp ? adev->hp->notify : NULL;
4083c2cc7ffSRafael J. Wysocki 		acpi_unlock_hp_context();
4093c2cc7ffSRafael J. Wysocki 		/*
4103c2cc7ffSRafael J. Wysocki 		 * There may be additional notify handlers for device objects
4113c2cc7ffSRafael J. Wysocki 		 * without the .event() callback, so ignore them here.
4123c2cc7ffSRafael J. Wysocki 		 */
413be27b3dcSRafael J. Wysocki 		if (notify)
414be27b3dcSRafael J. Wysocki 			error = notify(adev, src);
4153c2cc7ffSRafael J. Wysocki 		else
4163c2cc7ffSRafael J. Wysocki 			goto out;
417c27b2c33SRafael J. Wysocki 	}
418d429e5c1SLee, Chun-Yi 	switch (error) {
419d429e5c1SLee, Chun-Yi 	case 0:
420a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_SUCCESS;
421d429e5c1SLee, Chun-Yi 		break;
422d429e5c1SLee, Chun-Yi 	case -EPERM:
423d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
424d429e5c1SLee, Chun-Yi 		break;
425d429e5c1SLee, Chun-Yi 	case -EBUSY:
426d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_DEVICE_BUSY;
427d429e5c1SLee, Chun-Yi 		break;
428d429e5c1SLee, Chun-Yi 	default:
429d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
430d429e5c1SLee, Chun-Yi 		break;
431d429e5c1SLee, Chun-Yi 	}
432a33ec399SRafael J. Wysocki 
4333c2cc7ffSRafael J. Wysocki  err_out:
434700b8422SRafael J. Wysocki 	acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
4353c2cc7ffSRafael J. Wysocki 
4363c2cc7ffSRafael J. Wysocki  out:
43778ea4639SRafael J. Wysocki 	acpi_bus_put_acpi_device(adev);
438a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
439e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
440a33ec399SRafael J. Wysocki }
441a33ec399SRafael J. Wysocki 
4420b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
4430b224527SRafael J. Wysocki {
4440b224527SRafael J. Wysocki 	int i;
4450b224527SRafael J. Wysocki 
446993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
447993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
448993cbe59SRafael J. Wysocki 
4491b1f3e16SRafael J. Wysocki 	if (!device->power.flags.power_resources)
4500b224527SRafael J. Wysocki 		return;
4510b224527SRafael J. Wysocki 
4520b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
4530b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
4540b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
4550b224527SRafael J. Wysocki 	}
4567f47fa6cSBjorn Helgaas }
4577f47fa6cSBjorn Helgaas 
4581890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
4591890a97aSPatrick Mochel {
4601890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4611890a97aSPatrick Mochel 
462ffdcd955SMika Westerberg 	acpi_free_properties(acpi_dev);
463c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
4640b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
4651890a97aSPatrick Mochel 	kfree(acpi_dev);
4661890a97aSPatrick Mochel }
4671890a97aSPatrick Mochel 
468d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device)
469caf5c03fSRafael J. Wysocki {
470ca9dc8d4SLukas Wunner 	struct acpi_device_bus_id *acpi_device_bus_id;
471ca9dc8d4SLukas Wunner 
472d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_lock);
473d783156eSRafael J. Wysocki 	if (device->parent)
474d783156eSRafael J. Wysocki 		list_del(&device->node);
475d783156eSRafael J. Wysocki 
476ca9dc8d4SLukas Wunner 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
477ca9dc8d4SLukas Wunner 		if (!strcmp(acpi_device_bus_id->bus_id,
478ca9dc8d4SLukas Wunner 			    acpi_device_hid(device))) {
479eb50aaf9SAndy Shevchenko 			ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
480eb50aaf9SAndy Shevchenko 			if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
481ca9dc8d4SLukas Wunner 				list_del(&acpi_device_bus_id->node);
482a58015d6SDexuan Cui 				kfree_const(acpi_device_bus_id->bus_id);
483ca9dc8d4SLukas Wunner 				kfree(acpi_device_bus_id);
484ca9dc8d4SLukas Wunner 			}
485ca9dc8d4SLukas Wunner 			break;
486ca9dc8d4SLukas Wunner 		}
487ca9dc8d4SLukas Wunner 
488d783156eSRafael J. Wysocki 	list_del(&device->wakeup_list);
489d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_lock);
490d783156eSRafael J. Wysocki 
491d783156eSRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
492d783156eSRafael J. Wysocki 	acpi_device_remove_files(device);
493d783156eSRafael J. Wysocki 	if (device->remove)
494d783156eSRafael J. Wysocki 		device->remove(device);
495d783156eSRafael J. Wysocki 
496d783156eSRafael J. Wysocki 	device_del(&device->dev);
497d783156eSRafael J. Wysocki }
498d783156eSRafael J. Wysocki 
49968bdb677SOctavian Purdila static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
50068bdb677SOctavian Purdila 
501d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list);
502d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock);
503d783156eSRafael J. Wysocki 
504d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used)
505d783156eSRafael J. Wysocki {
506d783156eSRafael J. Wysocki 	for (;;) {
507d783156eSRafael J. Wysocki 		struct acpi_device *adev;
508d783156eSRafael J. Wysocki 
509d783156eSRafael J. Wysocki 		mutex_lock(&acpi_device_del_lock);
510d783156eSRafael J. Wysocki 
511d783156eSRafael J. Wysocki 		if (list_empty(&acpi_device_del_list)) {
512d783156eSRafael J. Wysocki 			mutex_unlock(&acpi_device_del_lock);
513d783156eSRafael J. Wysocki 			break;
514d783156eSRafael J. Wysocki 		}
515d783156eSRafael J. Wysocki 		adev = list_first_entry(&acpi_device_del_list,
516d783156eSRafael J. Wysocki 					struct acpi_device, del_list);
517d783156eSRafael J. Wysocki 		list_del(&adev->del_list);
518d783156eSRafael J. Wysocki 
519d783156eSRafael J. Wysocki 		mutex_unlock(&acpi_device_del_lock);
520d783156eSRafael J. Wysocki 
52168bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
52268bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_REMOVE, adev);
52368bdb677SOctavian Purdila 
524d783156eSRafael J. Wysocki 		acpi_device_del(adev);
525d783156eSRafael J. Wysocki 		/*
526d783156eSRafael J. Wysocki 		 * Drop references to all power resources that might have been
527d783156eSRafael J. Wysocki 		 * used by the device.
528d783156eSRafael J. Wysocki 		 */
529d783156eSRafael J. Wysocki 		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
5304cbaba4eSAndy Shevchenko 		acpi_dev_put(adev);
531d783156eSRafael J. Wysocki 	}
532d783156eSRafael J. Wysocki }
533d783156eSRafael J. Wysocki 
534d783156eSRafael J. Wysocki /**
535d783156eSRafael J. Wysocki  * acpi_scan_drop_device - Drop an ACPI device object.
536d783156eSRafael J. Wysocki  * @handle: Handle of an ACPI namespace node, not used.
537d783156eSRafael J. Wysocki  * @context: Address of the ACPI device object to drop.
538d783156eSRafael J. Wysocki  *
539d783156eSRafael J. Wysocki  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
540d783156eSRafael J. Wysocki  * namespace node the device object pointed to by @context is attached to.
541d783156eSRafael J. Wysocki  *
542d783156eSRafael J. Wysocki  * The unregistration is carried out asynchronously to avoid running
543d783156eSRafael J. Wysocki  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
544d783156eSRafael J. Wysocki  * ensure the correct ordering (the device objects must be unregistered in the
545d783156eSRafael J. Wysocki  * same order in which the corresponding namespace nodes are deleted).
546d783156eSRafael J. Wysocki  */
547d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context)
548d783156eSRafael J. Wysocki {
549d783156eSRafael J. Wysocki 	static DECLARE_WORK(work, acpi_device_del_work_fn);
550d783156eSRafael J. Wysocki 	struct acpi_device *adev = context;
551d783156eSRafael J. Wysocki 
552d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_del_lock);
553d783156eSRafael J. Wysocki 
554d783156eSRafael J. Wysocki 	/*
555d783156eSRafael J. Wysocki 	 * Use the ACPI hotplug workqueue which is ordered, so this work item
556d783156eSRafael J. Wysocki 	 * won't run after any hotplug work items submitted subsequently.  That
557d783156eSRafael J. Wysocki 	 * prevents attempts to register device objects identical to those being
558d783156eSRafael J. Wysocki 	 * deleted from happening concurrently (such attempts result from
559d783156eSRafael J. Wysocki 	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
560935ab850STom Saeger 	 * run after all of the work items submitted previously, which helps
561d783156eSRafael J. Wysocki 	 * those work items to ensure that they are not accessing stale device
562d783156eSRafael J. Wysocki 	 * objects.
563d783156eSRafael J. Wysocki 	 */
564d783156eSRafael J. Wysocki 	if (list_empty(&acpi_device_del_list))
565d783156eSRafael J. Wysocki 		acpi_queue_hotplug_work(&work);
566d783156eSRafael J. Wysocki 
567d783156eSRafael J. Wysocki 	list_add_tail(&adev->del_list, &acpi_device_del_list);
568d783156eSRafael J. Wysocki 	/* Make acpi_ns_validate_handle() return NULL for this handle. */
569d783156eSRafael J. Wysocki 	adev->handle = INVALID_ACPI_HANDLE;
570d783156eSRafael J. Wysocki 
571d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_del_lock);
572caf5c03fSRafael J. Wysocki }
573caf5c03fSRafael J. Wysocki 
57483e2c8fcSRafael J. Wysocki static struct acpi_device *handle_to_device(acpi_handle handle,
57578ea4639SRafael J. Wysocki 					    void (*callback)(void *))
576caf5c03fSRafael J. Wysocki {
57783e2c8fcSRafael J. Wysocki 	struct acpi_device *adev = NULL;
578caf5c03fSRafael J. Wysocki 	acpi_status status;
579caf5c03fSRafael J. Wysocki 
58078ea4639SRafael J. Wysocki 	status = acpi_get_data_full(handle, acpi_scan_drop_device,
58183e2c8fcSRafael J. Wysocki 				    (void **)&adev, callback);
58283e2c8fcSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !adev) {
58383e2c8fcSRafael J. Wysocki 		acpi_handle_debug(handle, "No context!\n");
58483e2c8fcSRafael J. Wysocki 		return NULL;
585caf5c03fSRafael J. Wysocki 	}
58683e2c8fcSRafael J. Wysocki 	return adev;
587caf5c03fSRafael J. Wysocki }
58878ea4639SRafael J. Wysocki 
58978ea4639SRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
59078ea4639SRafael J. Wysocki {
59183e2c8fcSRafael J. Wysocki 	if (!device)
59283e2c8fcSRafael J. Wysocki 		return -EINVAL;
59383e2c8fcSRafael J. Wysocki 
59483e2c8fcSRafael J. Wysocki 	*device = handle_to_device(handle, NULL);
59583e2c8fcSRafael J. Wysocki 	if (!*device)
59683e2c8fcSRafael J. Wysocki 		return -ENODEV;
59783e2c8fcSRafael J. Wysocki 
59883e2c8fcSRafael J. Wysocki 	return 0;
59978ea4639SRafael J. Wysocki }
6006585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device);
601caf5c03fSRafael J. Wysocki 
60278ea4639SRafael J. Wysocki static void get_acpi_device(void *dev)
60378ea4639SRafael J. Wysocki {
6044cbaba4eSAndy Shevchenko 	acpi_dev_get(dev);
60578ea4639SRafael J. Wysocki }
60678ea4639SRafael J. Wysocki 
60778ea4639SRafael J. Wysocki struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
60878ea4639SRafael J. Wysocki {
60983e2c8fcSRafael J. Wysocki 	return handle_to_device(handle, get_acpi_device);
61078ea4639SRafael J. Wysocki }
61178ea4639SRafael J. Wysocki 
612c1013ff7SRafael J. Wysocki static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
613c1013ff7SRafael J. Wysocki {
614c1013ff7SRafael J. Wysocki 	struct acpi_device_bus_id *acpi_device_bus_id;
615c1013ff7SRafael J. Wysocki 
616c1013ff7SRafael J. Wysocki 	/* Find suitable bus_id and instance number in acpi_bus_id_list. */
617c1013ff7SRafael J. Wysocki 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
618c1013ff7SRafael J. Wysocki 		if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
619c1013ff7SRafael J. Wysocki 			return acpi_device_bus_id;
620c1013ff7SRafael J. Wysocki 	}
621c1013ff7SRafael J. Wysocki 	return NULL;
622c1013ff7SRafael J. Wysocki }
623c1013ff7SRafael J. Wysocki 
624eb50aaf9SAndy Shevchenko static int acpi_device_set_name(struct acpi_device *device,
625eb50aaf9SAndy Shevchenko 				struct acpi_device_bus_id *acpi_device_bus_id)
626eb50aaf9SAndy Shevchenko {
627eb50aaf9SAndy Shevchenko 	struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
628eb50aaf9SAndy Shevchenko 	int result;
629eb50aaf9SAndy Shevchenko 
630eb50aaf9SAndy Shevchenko 	result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
631eb50aaf9SAndy Shevchenko 	if (result < 0)
632eb50aaf9SAndy Shevchenko 		return result;
633eb50aaf9SAndy Shevchenko 
634eb50aaf9SAndy Shevchenko 	device->pnp.instance_no = result;
635eb50aaf9SAndy Shevchenko 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
636eb50aaf9SAndy Shevchenko 	return 0;
637eb50aaf9SAndy Shevchenko }
638eb50aaf9SAndy Shevchenko 
639c6a493a1SRafael J. Wysocki static int acpi_tie_acpi_dev(struct acpi_device *adev)
640c6a493a1SRafael J. Wysocki {
641c6a493a1SRafael J. Wysocki 	acpi_handle handle = adev->handle;
642c6a493a1SRafael J. Wysocki 	acpi_status status;
643c6a493a1SRafael J. Wysocki 
644c6a493a1SRafael J. Wysocki 	if (!handle)
645c6a493a1SRafael J. Wysocki 		return 0;
646c6a493a1SRafael J. Wysocki 
647c6a493a1SRafael J. Wysocki 	status = acpi_attach_data(handle, acpi_scan_drop_device, adev);
648c6a493a1SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
649c6a493a1SRafael J. Wysocki 		acpi_handle_err(handle, "Unable to attach device data\n");
650c6a493a1SRafael J. Wysocki 		return -ENODEV;
651c6a493a1SRafael J. Wysocki 	}
652c6a493a1SRafael J. Wysocki 
653c6a493a1SRafael J. Wysocki 	return 0;
654c6a493a1SRafael J. Wysocki }
655c6a493a1SRafael J. Wysocki 
6565f4ce260SRafael J. Wysocki static int __acpi_device_add(struct acpi_device *device,
65782c7d5efSRafael J. Wysocki 			     void (*release)(struct device *))
6581da177e4SLinus Torvalds {
659c1013ff7SRafael J. Wysocki 	struct acpi_device_bus_id *acpi_device_bus_id;
660e49bd2ddSZhang Rui 	int result;
66166b7ed40SBjorn Helgaas 
6621da177e4SLinus Torvalds 	/*
6631da177e4SLinus Torvalds 	 * Linkage
6641da177e4SLinus Torvalds 	 * -------
6651da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
6661da177e4SLinus Torvalds 	 */
6671da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
6681da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
6691da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
6701033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
671d783156eSRafael J. Wysocki 	INIT_LIST_HEAD(&device->del_list);
6721033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
6731da177e4SLinus Torvalds 
6749090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
675c1013ff7SRafael J. Wysocki 
676c1013ff7SRafael J. Wysocki 	acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
677c1013ff7SRafael J. Wysocki 	if (acpi_device_bus_id) {
678eb50aaf9SAndy Shevchenko 		result = acpi_device_set_name(device, acpi_device_bus_id);
679eb50aaf9SAndy Shevchenko 		if (result)
680eb50aaf9SAndy Shevchenko 			goto err_unlock;
681c1013ff7SRafael J. Wysocki 	} else {
682c1013ff7SRafael J. Wysocki 		acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
683c1013ff7SRafael J. Wysocki 					     GFP_KERNEL);
684c1013ff7SRafael J. Wysocki 		if (!acpi_device_bus_id) {
685c1013ff7SRafael J. Wysocki 			result = -ENOMEM;
686c1013ff7SRafael J. Wysocki 			goto err_unlock;
687e49bd2ddSZhang Rui 		}
688a58015d6SDexuan Cui 		acpi_device_bus_id->bus_id =
689a58015d6SDexuan Cui 			kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
690a58015d6SDexuan Cui 		if (!acpi_device_bus_id->bus_id) {
691c1013ff7SRafael J. Wysocki 			kfree(acpi_device_bus_id);
692a58015d6SDexuan Cui 			result = -ENOMEM;
693c1013ff7SRafael J. Wysocki 			goto err_unlock;
694a58015d6SDexuan Cui 		}
695a58015d6SDexuan Cui 
696eb50aaf9SAndy Shevchenko 		ida_init(&acpi_device_bus_id->instance_ida);
697eb50aaf9SAndy Shevchenko 
698eb50aaf9SAndy Shevchenko 		result = acpi_device_set_name(device, acpi_device_bus_id);
699eb50aaf9SAndy Shevchenko 		if (result) {
7000c8bd174SChristophe JAILLET 			kfree_const(acpi_device_bus_id->bus_id);
701eb50aaf9SAndy Shevchenko 			kfree(acpi_device_bus_id);
702eb50aaf9SAndy Shevchenko 			goto err_unlock;
703eb50aaf9SAndy Shevchenko 		}
704eb50aaf9SAndy Shevchenko 
705e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
706e49bd2ddSZhang Rui 	}
707e49bd2ddSZhang Rui 
70833b57150SLen Brown 	if (device->parent)
7091da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
71033b57150SLen Brown 
7111da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
7121da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
7135e73c518SRafael J. Wysocki 
7149090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
7151da177e4SLinus Torvalds 
7161890a97aSPatrick Mochel 	if (device->parent)
71766b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
7185e73c518SRafael J. Wysocki 
7191890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
72082c7d5efSRafael J. Wysocki 	device->dev.release = release;
721cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
722e49bd2ddSZhang Rui 	if (result) {
7238b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
724d43e167dSRafael J. Wysocki 		goto err;
725e49bd2ddSZhang Rui 	}
726f883d9dbSPatrick Mochel 
727e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
728e49bd2ddSZhang Rui 	if (result)
7298acf4108SHanjun Guo 		pr_err("Error creating sysfs interface for device %s\n",
7300794469dSKay Sievers 		       dev_name(&device->dev));
731e49bd2ddSZhang Rui 
732e49bd2ddSZhang Rui 	return 0;
733d43e167dSRafael J. Wysocki 
734d43e167dSRafael J. Wysocki err:
7359090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
7365e73c518SRafael J. Wysocki 
73733b57150SLen Brown 	if (device->parent)
738e49bd2ddSZhang Rui 		list_del(&device->node);
7395e73c518SRafael J. Wysocki 
740e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
741a58015d6SDexuan Cui 
742c1013ff7SRafael J. Wysocki err_unlock:
7439090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
744d43e167dSRafael J. Wysocki 
745d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
7465e73c518SRafael J. Wysocki 
747e49bd2ddSZhang Rui 	return result;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7505f4ce260SRafael J. Wysocki int acpi_device_add(struct acpi_device *adev, void (*release)(struct device *))
7515f4ce260SRafael J. Wysocki {
7525f4ce260SRafael J. Wysocki 	int ret;
7535f4ce260SRafael J. Wysocki 
7545f4ce260SRafael J. Wysocki 	ret = acpi_tie_acpi_dev(adev);
7555f4ce260SRafael J. Wysocki 	if (ret)
7565f4ce260SRafael J. Wysocki 		return ret;
7575f4ce260SRafael J. Wysocki 
7585f4ce260SRafael J. Wysocki 	return __acpi_device_add(adev, release);
7595f4ce260SRafael J. Wysocki }
7605f4ce260SRafael J. Wysocki 
7619e89dde2SZhang Rui /* --------------------------------------------------------------------------
7621da177e4SLinus Torvalds                                  Device Enumeration
7631da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
7642ef33ee7SRafael J. Wysocki static bool acpi_info_matches_ids(struct acpi_device_info *info,
7652ef33ee7SRafael J. Wysocki 				  const char * const ids[])
766637b9f1aSHans de Goede {
7672ef33ee7SRafael J. Wysocki 	struct acpi_pnp_device_id_list *cid_list = NULL;
76802050558SAndy Shevchenko 	int i, index;
769637b9f1aSHans de Goede 
770637b9f1aSHans de Goede 	if (!(info->valid & ACPI_VALID_HID))
771637b9f1aSHans de Goede 		return false;
772637b9f1aSHans de Goede 
77302050558SAndy Shevchenko 	index = match_string(ids, -1, info->hardware_id.string);
77402050558SAndy Shevchenko 	if (index >= 0)
77502050558SAndy Shevchenko 		return true;
77602050558SAndy Shevchenko 
7772ef33ee7SRafael J. Wysocki 	if (info->valid & ACPI_VALID_CID)
7782ef33ee7SRafael J. Wysocki 		cid_list = &info->compatible_id_list;
7792ef33ee7SRafael J. Wysocki 
7802ef33ee7SRafael J. Wysocki 	if (!cid_list)
78102050558SAndy Shevchenko 		return false;
7822ef33ee7SRafael J. Wysocki 
78302050558SAndy Shevchenko 	for (i = 0; i < cid_list->count; i++) {
78402050558SAndy Shevchenko 		index = match_string(ids, -1, cid_list->ids[i].string);
78502050558SAndy Shevchenko 		if (index >= 0)
7862ef33ee7SRafael J. Wysocki 			return true;
7872ef33ee7SRafael J. Wysocki 	}
788637b9f1aSHans de Goede 
789637b9f1aSHans de Goede 	return false;
790637b9f1aSHans de Goede }
791637b9f1aSHans de Goede 
792637b9f1aSHans de Goede /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
7932ef33ee7SRafael J. Wysocki static const char * const acpi_ignore_dep_ids[] = {
7942ef33ee7SRafael J. Wysocki 	"PNP0D80", /* Windows-compatible System Power Management Controller */
7959272e97aSHans de Goede 	"INT33BD", /* Intel Baytrail Mailbox Device */
796637b9f1aSHans de Goede 	NULL
797637b9f1aSHans de Goede };
798637b9f1aSHans de Goede 
7995c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
8005c478f49SBjorn Helgaas {
801456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
8025c478f49SBjorn Helgaas 	acpi_status status;
8035c478f49SBjorn Helgaas 
8045c478f49SBjorn Helgaas 	/*
8055c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
8065c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
8075c478f49SBjorn Helgaas 	 * to deal with them specially.
8085c478f49SBjorn Helgaas 	 */
809456de893SRafael J. Wysocki 	if (!handle)
8105c478f49SBjorn Helgaas 		return acpi_root;
8115c478f49SBjorn Helgaas 
8125c478f49SBjorn Helgaas 	do {
8135c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
8145c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
815456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
816456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
8175c478f49SBjorn Helgaas 	return device;
8185c478f49SBjorn Helgaas }
8195c478f49SBjorn Helgaas 
820c8f7a62cSLen Brown acpi_status
821c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
822c8f7a62cSLen Brown {
823c8f7a62cSLen Brown 	acpi_status status;
824c8f7a62cSLen Brown 	acpi_handle tmp;
825c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
826c8f7a62cSLen Brown 	union acpi_object *obj;
827c8f7a62cSLen Brown 
828c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
829c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
830c8f7a62cSLen Brown 		return status;
831c8f7a62cSLen Brown 
832c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
833c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
834c8f7a62cSLen Brown 		obj = buffer.pointer;
8353b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
8363b5fee59SHolger Macht 					 ejd);
837c8f7a62cSLen Brown 		kfree(buffer.pointer);
838c8f7a62cSLen Brown 	}
839c8f7a62cSLen Brown 	return status;
840c8f7a62cSLen Brown }
841c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
842c8f7a62cSLen Brown 
84340381a3cSBjorn Helgaas static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
8441da177e4SLinus Torvalds {
84540381a3cSBjorn Helgaas 	acpi_handle handle = dev->handle;
84640381a3cSBjorn Helgaas 	struct acpi_device_wakeup *wakeup = &dev->wakeup;
847b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
848b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
8491da177e4SLinus Torvalds 	union acpi_object *element = NULL;
850b581a7f9SRafael J. Wysocki 	acpi_status status;
851e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
8521da177e4SLinus Torvalds 
853993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
8541da177e4SLinus Torvalds 
855b581a7f9SRafael J. Wysocki 	/* _PRW */
856b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
857b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
858e52d9d8cSRafael J. Wysocki 		acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
859e52d9d8cSRafael J. Wysocki 				 acpi_format_exception(status));
860e88c9c60SRafael J. Wysocki 		return err;
861b581a7f9SRafael J. Wysocki 	}
862b581a7f9SRafael J. Wysocki 
863b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
864b581a7f9SRafael J. Wysocki 
865e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
866b581a7f9SRafael J. Wysocki 		goto out;
867b581a7f9SRafael J. Wysocki 
8681da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
869e88c9c60SRafael J. Wysocki 	if (!element)
870b581a7f9SRafael J. Wysocki 		goto out;
871e88c9c60SRafael J. Wysocki 
8721da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
8731da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
8741da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
8751da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
876e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
877b581a7f9SRafael J. Wysocki 			goto out;
878e88c9c60SRafael J. Wysocki 
879b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
8801da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
881b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
8821da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
8831da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
884b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
885b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
886b581a7f9SRafael J. Wysocki 	} else {
887b581a7f9SRafael J. Wysocki 		goto out;
888b581a7f9SRafael J. Wysocki 	}
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
891e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
892b581a7f9SRafael J. Wysocki 		goto out;
893e88c9c60SRafael J. Wysocki 
894b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
8951da177e4SLinus Torvalds 
896e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
897e88c9c60SRafael J. Wysocki 	if (err)
898b581a7f9SRafael J. Wysocki 		goto out;
8991da177e4SLinus Torvalds 
9000596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
9010596a52bSRafael J. Wysocki 		int sleep_state;
9029874647bSRafael J. Wysocki 
903b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
904b5d667ebSRafael J. Wysocki 						  &sleep_state);
905b5d667ebSRafael J. Wysocki 		if (err) {
906b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
907b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
908b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
909b5d667ebSRafael J. Wysocki 			goto out;
910b5d667ebSRafael J. Wysocki 		}
9110596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
9120596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
9130596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
9140596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
9150596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
9160596a52bSRafael J. Wysocki 		}
9170596a52bSRafael J. Wysocki 	}
918b581a7f9SRafael J. Wysocki 
919b581a7f9SRafael J. Wysocki  out:
920b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
921e88c9c60SRafael J. Wysocki 	return err;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
924a1a66393SRafael J. Wysocki static bool acpi_wakeup_gpe_init(struct acpi_device *device)
9251da177e4SLinus Torvalds {
9260519ade7SMathias Krause 	static const struct acpi_device_id button_device_ids[] = {
9275ceb5f05SBjorn Helgaas 		{"PNP0C0C", 0},		/* Power button */
9285ceb5f05SBjorn Helgaas 		{"PNP0C0D", 0},		/* Lid */
9295ceb5f05SBjorn Helgaas 		{"PNP0C0E", 0},		/* Sleep button */
93029b71a1cSThomas Renninger 		{"", 0},
93129b71a1cSThomas Renninger 	};
932bd9b2f9aSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &device->wakeup;
933f517709dSRafael J. Wysocki 	acpi_status status;
934f517709dSRafael J. Wysocki 
935bd9b2f9aSRafael J. Wysocki 	wakeup->flags.notifier_present = 0;
936f517709dSRafael J. Wysocki 
937f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
938f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
939b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
940b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
941bd9b2f9aSRafael J. Wysocki 			if (wakeup->sleep_state == ACPI_STATE_S5)
942bd9b2f9aSRafael J. Wysocki 				wakeup->sleep_state = ACPI_STATE_S4;
943b7e38304SZhang Rui 		}
944bd9b2f9aSRafael J. Wysocki 		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
945f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
946a1a66393SRafael J. Wysocki 		return true;
947f517709dSRafael J. Wysocki 	}
948f517709dSRafael J. Wysocki 
949a1a66393SRafael J. Wysocki 	status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
950bd9b2f9aSRafael J. Wysocki 					 wakeup->gpe_number);
951a1a66393SRafael J. Wysocki 	return ACPI_SUCCESS(status);
952f517709dSRafael J. Wysocki }
953f517709dSRafael J. Wysocki 
954d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
955f517709dSRafael J. Wysocki {
956e88c9c60SRafael J. Wysocki 	int err;
95729b71a1cSThomas Renninger 
958d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
959952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
960d57d09a4SRafael J. Wysocki 		return;
961d57d09a4SRafael J. Wysocki 
96240381a3cSBjorn Helgaas 	err = acpi_bus_extract_wakeup_device_power_package(device);
963e88c9c60SRafael J. Wysocki 	if (err) {
964e52d9d8cSRafael J. Wysocki 		dev_err(&device->dev, "Unable to extract wakeup power resources");
965d57d09a4SRafael J. Wysocki 		return;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
968a1a66393SRafael J. Wysocki 	device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
9699b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
970a1a66393SRafael J. Wysocki 	/*
971a1a66393SRafael J. Wysocki 	 * Call _PSW/_DSW object to disable its ability to wake the sleeping
972729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
973603fadf3SBjorn Helgaas 	 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
974729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
975729b2bdbSZhao Yakui 	 * present will the _PSW object used.
976729b2bdbSZhao Yakui 	 */
977e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
978e88c9c60SRafael J. Wysocki 	if (err)
97905de0686STian Tao 		pr_debug("error in _DSW or _PSW evaluation\n");
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
982f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
9831da177e4SLinus Torvalds {
984f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
985ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
986ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
987f33ce568SRafael J. Wysocki 	acpi_status status;
9889e89dde2SZhang Rui 
989f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
990f33ce568SRafael J. Wysocki 
991ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
992ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
993ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
994ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
995f33ce568SRafael J. Wysocki 
996ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
997ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
998956ad9d9SRafael J. Wysocki 		    && package->package.count)
999956ad9d9SRafael J. Wysocki 			acpi_extract_power_resources(package, 0, &ps->resources);
1000956ad9d9SRafael J. Wysocki 
1001ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
1002f33ce568SRafael J. Wysocki 	}
1003f33ce568SRafael J. Wysocki 
1004f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1005ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
1006952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
1007f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1008f33ce568SRafael J. Wysocki 
100920dacb71SRafael J. Wysocki 	/* State is valid if there are means to put the device into it. */
101020dacb71SRafael J. Wysocki 	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
1011f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1012f33ce568SRafael J. Wysocki 
1013f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1014f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1015f33ce568SRafael J. Wysocki }
1016f33ce568SRafael J. Wysocki 
1017d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
10181da177e4SLinus Torvalds {
10198bc5053bSRafael J. Wysocki 	u32 i;
10201da177e4SLinus Torvalds 
1021d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1022952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
1023952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
1024d43e167dSRafael J. Wysocki 		return;
1025d43e167dSRafael J. Wysocki 
1026d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	/*
10299e89dde2SZhang Rui 	 * Power Management Flags
10301da177e4SLinus Torvalds 	 */
1031952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
10329e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
1033f25c0ae2SRafael J. Wysocki 
1034952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
10359e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
10361da177e4SLinus Torvalds 
1037f25c0ae2SRafael J. Wysocki 	if (acpi_has_method(device->handle, "_DSW"))
1038f25c0ae2SRafael J. Wysocki 		device->power.flags.dsw_present = 1;
1039f25c0ae2SRafael J. Wysocki 
10401da177e4SLinus Torvalds 	/*
10419e89dde2SZhang Rui 	 * Enumerate supported power management states
10421da177e4SLinus Torvalds 	 */
1043f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1044f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
10451da177e4SLinus Torvalds 
10460b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
10479e89dde2SZhang Rui 
1048956ad9d9SRafael J. Wysocki 	/* Set the defaults for D0 and D3hot (always supported). */
10499e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
10509e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
105120dacb71SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
10521399dfcdSAaron Lu 
1053956ad9d9SRafael J. Wysocki 	/*
1054956ad9d9SRafael J. Wysocki 	 * Use power resources only if the D0 list of them is populated, because
1055956ad9d9SRafael J. Wysocki 	 * some platforms may provide _PR3 only to indicate D3cold support and
1056956ad9d9SRafael J. Wysocki 	 * in those cases the power resources list returned by it may be bogus.
1057956ad9d9SRafael J. Wysocki 	 */
1058956ad9d9SRafael J. Wysocki 	if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1059956ad9d9SRafael J. Wysocki 		device->power.flags.power_resources = 1;
1060956ad9d9SRafael J. Wysocki 		/*
1061956ad9d9SRafael J. Wysocki 		 * D3cold is supported if the D3hot list of power resources is
1062956ad9d9SRafael J. Wysocki 		 * not empty.
1063956ad9d9SRafael J. Wysocki 		 */
1064956ad9d9SRafael J. Wysocki 		if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1065956ad9d9SRafael J. Wysocki 			device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1066956ad9d9SRafael J. Wysocki 	}
1067956ad9d9SRafael J. Wysocki 
10681b1f3e16SRafael J. Wysocki 	if (acpi_bus_init_power(device))
1069b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1070b3785492SRafael J. Wysocki }
1071c8f7a62cSLen Brown 
1072d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
1075952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
10761da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
1079952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
10801da177e4SLinus Torvalds 		device->flags.removable = 1;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1083952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
1084952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
10851da177e4SLinus Torvalds 		device->flags.ejectable = 1;
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
1088c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
10891da177e4SLinus Torvalds {
10901da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
10911da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
10921da177e4SLinus Torvalds 	int i = 0;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	/*
10951da177e4SLinus Torvalds 	 * Bus ID
10961da177e4SLinus Torvalds 	 * ------
10971da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
10981da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
10991da177e4SLinus Torvalds 	 */
1100859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
11011da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1102859ac9a4SBjorn Helgaas 		return;
1103859ac9a4SBjorn Helgaas 	}
1104859ac9a4SBjorn Helgaas 
1105859ac9a4SBjorn Helgaas 	switch (device->device_type) {
11061da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
11071da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
11081da177e4SLinus Torvalds 		break;
11091da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
11101da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
11111da177e4SLinus Torvalds 		break;
1112a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1113a64a62ceSLv Zheng 		strcpy(device->pnp.bus_id, "ECDT");
1114a64a62ceSLv Zheng 		break;
11151da177e4SLinus Torvalds 	default:
111666b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
11171da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
11181da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
11191da177e4SLinus Torvalds 			if (bus_id[i] == '_')
11201da177e4SLinus Torvalds 				bus_id[i] = '\0';
11211da177e4SLinus Torvalds 			else
11221da177e4SLinus Torvalds 				break;
11231da177e4SLinus Torvalds 		}
11241da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
11251da177e4SLinus Torvalds 		break;
11261da177e4SLinus Torvalds 	}
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
112954735266SZhang Rui /*
1130ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1131ebf4df8dSJiang Liu  *
1132ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1133ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1134ebf4df8dSJiang Liu  */
1135ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1136ebf4df8dSJiang Liu {
1137ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1138ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1139ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1140ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1141ebf4df8dSJiang Liu }
1142ebf4df8dSJiang Liu 
1143ebf4df8dSJiang Liu /*
1144d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
114554735266SZhang Rui  *
114654735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
114754735266SZhang Rui  * then we can safely call it an ejectable drive bay
114854735266SZhang Rui  */
1149ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1150d4e1a692SToshi Kani {
115154735266SZhang Rui 	acpi_handle phandle;
115254735266SZhang Rui 
1153952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1154ebf4df8dSJiang Liu 		return false;
1155ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1156ebf4df8dSJiang Liu 		return true;
1157ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1158ebf4df8dSJiang Liu 		return false;
115954735266SZhang Rui 
1160ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
116154735266SZhang Rui }
116254735266SZhang Rui 
1163b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev)
11641e2380cdSRafael J. Wysocki {
1165b43109faSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
11661e2380cdSRafael J. Wysocki 
1167b43109faSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list)
1168b43109faSRafael J. Wysocki 		if (!strcmp("PNP0C0A", hwid->id))
1169b43109faSRafael J. Wysocki 			return true;
1170b43109faSRafael J. Wysocki 
11711e2380cdSRafael J. Wysocki 	return false;
11721e2380cdSRafael J. Wysocki }
11731e2380cdSRafael J. Wysocki 
1174b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev)
11751e2380cdSRafael J. Wysocki {
1176b43109faSRafael J. Wysocki 	acpi_handle handle = adev->handle;
1177b43109faSRafael J. Wysocki 
1178b43109faSRafael J. Wysocki 	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
11791e2380cdSRafael J. Wysocki 		return true;
11801e2380cdSRafael J. Wysocki 
11811e2380cdSRafael J. Wysocki 	return acpi_bay_match(handle);
11821e2380cdSRafael J. Wysocki }
11831e2380cdSRafael J. Wysocki 
11843620f2f2SFrank Seidel /*
1185d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
11863620f2f2SFrank Seidel  */
1187ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
11883620f2f2SFrank Seidel {
1189ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
11903620f2f2SFrank Seidel }
11913620f2f2SFrank Seidel 
1192adc8bb8eSHans de Goede static acpi_status
1193adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1194adc8bb8eSHans de Goede 			  void **return_value)
1195adc8bb8eSHans de Goede {
1196adc8bb8eSHans de Goede 	long *cap = context;
1197adc8bb8eSHans de Goede 
1198adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_BCM") &&
1199adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_BCL")) {
1200e52d9d8cSRafael J. Wysocki 		acpi_handle_debug(handle, "Found generic backlight support\n");
1201adc8bb8eSHans de Goede 		*cap |= ACPI_VIDEO_BACKLIGHT;
1202adc8bb8eSHans de Goede 		/* We have backlight support, no need to scan further */
1203adc8bb8eSHans de Goede 		return AE_CTRL_TERMINATE;
1204adc8bb8eSHans de Goede 	}
1205adc8bb8eSHans de Goede 	return 0;
1206adc8bb8eSHans de Goede }
1207adc8bb8eSHans de Goede 
1208adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be
1209adc8bb8eSHans de Goede  * handled by video.ko.
1210adc8bb8eSHans de Goede  * The device will get a Linux specific CID added in scan.c to
1211adc8bb8eSHans de Goede  * identify the device as an ACPI graphics device
1212adc8bb8eSHans de Goede  * Be aware that the graphics device may not be physically present
1213adc8bb8eSHans de Goede  * Use acpi_video_get_capabilities() to detect general ACPI video
1214adc8bb8eSHans de Goede  * capabilities of present cards
1215adc8bb8eSHans de Goede  */
1216adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle)
1217adc8bb8eSHans de Goede {
1218adc8bb8eSHans de Goede 	long video_caps = 0;
1219adc8bb8eSHans de Goede 
1220adc8bb8eSHans de Goede 	/* Is this device able to support video switching ? */
1221adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1222adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1223adc8bb8eSHans de Goede 
1224adc8bb8eSHans de Goede 	/* Is this device able to retrieve a video ROM ? */
1225adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_ROM"))
1226adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1227adc8bb8eSHans de Goede 
1228adc8bb8eSHans de Goede 	/* Is this device able to configure which video head to be POSTed ? */
1229adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_VPO") &&
1230adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_GPD") &&
1231adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_SPD"))
1232adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1233adc8bb8eSHans de Goede 
1234adc8bb8eSHans de Goede 	/* Only check for backlight functionality if one of the above hit. */
1235adc8bb8eSHans de Goede 	if (video_caps)
1236adc8bb8eSHans de Goede 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1237adc8bb8eSHans de Goede 				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1238adc8bb8eSHans de Goede 				    &video_caps, NULL);
1239adc8bb8eSHans de Goede 
1240adc8bb8eSHans de Goede 	return video_caps;
1241adc8bb8eSHans de Goede }
1242adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device);
1243adc8bb8eSHans de Goede 
1244620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
124515b8dd53SBob Moore {
12467f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
124715b8dd53SBob Moore 
12482b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
12492b2ae7c7SThomas Renninger 		return dummy_hid;
12502b2ae7c7SThomas Renninger 
12517f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
12527f47fa6cSBjorn Helgaas 	return hid->id;
12537f47fa6cSBjorn Helgaas }
12547f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
125515b8dd53SBob Moore 
1256d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
12577f47fa6cSBjorn Helgaas {
12587f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
125915b8dd53SBob Moore 
12607f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
12617f47fa6cSBjorn Helgaas 	if (!id)
12627f47fa6cSBjorn Helgaas 		return;
126315b8dd53SBob Moore 
12646a0d12efSRasmus Villemoes 	id->id = kstrdup_const(dev_id, GFP_KERNEL);
12657f47fa6cSBjorn Helgaas 	if (!id->id) {
12667f47fa6cSBjorn Helgaas 		kfree(id);
12677f47fa6cSBjorn Helgaas 		return;
126815b8dd53SBob Moore 	}
126915b8dd53SBob Moore 
1270d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1271d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
127215b8dd53SBob Moore }
127315b8dd53SBob Moore 
1274222e82acSDarrick J. Wong /*
1275222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1276222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1277222e82acSDarrick J. Wong  * prefix.  Work around this.
1278222e82acSDarrick J. Wong  */
1279ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1280222e82acSDarrick J. Wong {
1281ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1282ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1283222e82acSDarrick J. Wong 
1284222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1285ebf4df8dSJiang Liu 		return false;
1286222e82acSDarrick J. Wong 
1287222e82acSDarrick J. Wong 	/* Look for SMBS object */
1288ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1289ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1290ebf4df8dSJiang Liu 		return false;
1291222e82acSDarrick J. Wong 
1292222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1293952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1294952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1295952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1296ebf4df8dSJiang Liu 		return true;
1297ebf4df8dSJiang Liu 
1298ebf4df8dSJiang Liu 	return false;
1299222e82acSDarrick J. Wong }
1300222e82acSDarrick J. Wong 
1301ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle)
1302ae3caa80SJiang Liu {
1303ae3caa80SJiang Liu 	acpi_handle tmp;
1304ae3caa80SJiang Liu 
1305ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1306ae3caa80SJiang Liu 	    tmp == handle)
1307ae3caa80SJiang Liu 		return true;
1308ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1309ae3caa80SJiang Liu 	    tmp == handle)
1310ae3caa80SJiang Liu 		return true;
1311ae3caa80SJiang Liu 
1312ae3caa80SJiang Liu 	return false;
1313ae3caa80SJiang Liu }
1314ae3caa80SJiang Liu 
1315c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1316c830dbcfSRafael J. Wysocki 			     int device_type)
13171da177e4SLinus Torvalds {
1318c830dbcfSRafael J. Wysocki 	struct acpi_device_info *info = NULL;
131978e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
13207f47fa6cSBjorn Helgaas 	int i;
13211da177e4SLinus Torvalds 
1322c0af4175SToshi Kani 	switch (device_type) {
13231da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1324c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1325c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1326859ac9a4SBjorn Helgaas 			break;
1327859ac9a4SBjorn Helgaas 		}
1328859ac9a4SBjorn Helgaas 
1329c830dbcfSRafael J. Wysocki 		acpi_get_object_info(handle, &info);
13306091b263SHans de Goede 		if (!info) {
13318acf4108SHanjun Guo 			pr_err("%s: Error reading device info\n", __func__);
13321da177e4SLinus Torvalds 			return;
13331da177e4SLinus Torvalds 		}
13341da177e4SLinus Torvalds 
1335549e6845SRafael J. Wysocki 		if (info->valid & ACPI_VALID_HID) {
1336c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
1337549e6845SRafael J. Wysocki 			pnp->type.platform_id = 1;
1338549e6845SRafael J. Wysocki 		}
133957f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
134015b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
134157f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1342c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
134357f3674fSBjorn Helgaas 		}
13441da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1345c0af4175SToshi Kani 			pnp->bus_address = info->address;
1346c0af4175SToshi Kani 			pnp->type.bus_address = 1;
13471da177e4SLinus Torvalds 		}
1348ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1349c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1350ccf78040SLv Zheng 							GFP_KERNEL);
135126095a01SSuthikulpanit, Suravee 		if (info->valid & ACPI_VALID_CLS)
135226095a01SSuthikulpanit, Suravee 			acpi_add_id(pnp, info->class_code.string);
1353ae843332SZhang Rui 
1354c830dbcfSRafael J. Wysocki 		kfree(info);
1355c830dbcfSRafael J. Wysocki 
135657f3674fSBjorn Helgaas 		/*
135757f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
135857f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
13593620f2f2SFrank Seidel 		 */
1360c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1361c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1362ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1363c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1364ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1365c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1366ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1367c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1368ae3caa80SJiang Liu 		else if (list_empty(&pnp->ids) &&
1369ae3caa80SJiang Liu 			 acpi_object_is_system_bus(handle)) {
1370ae3caa80SJiang Liu 			/* \_SB, \_TZ, LNXSYBUS */
1371ae3caa80SJiang Liu 			acpi_add_id(pnp, ACPI_BUS_HID);
1372c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1373c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1374b7b30de5SBjorn Helgaas 		}
137554735266SZhang Rui 
13761da177e4SLinus Torvalds 		break;
13771da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1378c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
13791da177e4SLinus Torvalds 		break;
13801da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1381c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
13821da177e4SLinus Torvalds 		break;
13831da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1384c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
13851da177e4SLinus Torvalds 		break;
13861da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1387c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
13881da177e4SLinus Torvalds 		break;
13891da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1390c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
13911da177e4SLinus Torvalds 		break;
1392a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1393a64a62ceSLv Zheng 		acpi_add_id(pnp, ACPI_ECDT_HID);
1394a64a62ceSLv Zheng 		break;
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
1398c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1399c0af4175SToshi Kani {
1400c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1401c0af4175SToshi Kani 
1402c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
14036a0d12efSRasmus Villemoes 		kfree_const(id->id);
1404c0af4175SToshi Kani 		kfree(id);
1405c0af4175SToshi Kani 	}
1406c0af4175SToshi Kani 	kfree(pnp->unique_id);
1407c0af4175SToshi Kani }
1408c0af4175SToshi Kani 
1409b84f196dSSuthikulpanit, Suravee /**
1410b84f196dSSuthikulpanit, Suravee  * acpi_dma_supported - Check DMA support for the specified device.
1411b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1412b84f196dSSuthikulpanit, Suravee  *
1413b84f196dSSuthikulpanit, Suravee  * Return false if DMA is not supported. Otherwise, return true
1414b84f196dSSuthikulpanit, Suravee  */
14153d7c821cSAndy Shevchenko bool acpi_dma_supported(const struct acpi_device *adev)
1416b84f196dSSuthikulpanit, Suravee {
1417b84f196dSSuthikulpanit, Suravee 	if (!adev)
1418b84f196dSSuthikulpanit, Suravee 		return false;
1419b84f196dSSuthikulpanit, Suravee 
1420b84f196dSSuthikulpanit, Suravee 	if (adev->flags.cca_seen)
1421b84f196dSSuthikulpanit, Suravee 		return true;
1422b84f196dSSuthikulpanit, Suravee 
1423b84f196dSSuthikulpanit, Suravee 	/*
1424b84f196dSSuthikulpanit, Suravee 	* Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1425b84f196dSSuthikulpanit, Suravee 	* DMA on "Intel platforms".  Presumably that includes all x86 and
1426b84f196dSSuthikulpanit, Suravee 	* ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1427b84f196dSSuthikulpanit, Suravee 	*/
1428b84f196dSSuthikulpanit, Suravee 	if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1429b84f196dSSuthikulpanit, Suravee 		return true;
1430b84f196dSSuthikulpanit, Suravee 
1431b84f196dSSuthikulpanit, Suravee 	return false;
1432b84f196dSSuthikulpanit, Suravee }
1433b84f196dSSuthikulpanit, Suravee 
1434b84f196dSSuthikulpanit, Suravee /**
1435b84f196dSSuthikulpanit, Suravee  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1436b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1437b84f196dSSuthikulpanit, Suravee  *
1438b84f196dSSuthikulpanit, Suravee  * Return enum dev_dma_attr.
1439b84f196dSSuthikulpanit, Suravee  */
1440b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1441b84f196dSSuthikulpanit, Suravee {
1442b84f196dSSuthikulpanit, Suravee 	if (!acpi_dma_supported(adev))
1443b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NOT_SUPPORTED;
1444b84f196dSSuthikulpanit, Suravee 
1445b84f196dSSuthikulpanit, Suravee 	if (adev->flags.coherent_dma)
1446b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_COHERENT;
1447b84f196dSSuthikulpanit, Suravee 	else
1448b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NON_COHERENT;
1449b84f196dSSuthikulpanit, Suravee }
1450b84f196dSSuthikulpanit, Suravee 
1451d760a1baSLorenzo Pieralisi /**
1452c04ac679SLorenzo Pieralisi  * acpi_dma_get_range() - Get device DMA parameters.
1453c04ac679SLorenzo Pieralisi  *
1454c04ac679SLorenzo Pieralisi  * @dev: device to configure
1455c04ac679SLorenzo Pieralisi  * @dma_addr: pointer device DMA address result
1456c04ac679SLorenzo Pieralisi  * @offset: pointer to the DMA offset result
1457c04ac679SLorenzo Pieralisi  * @size: pointer to DMA range size result
1458c04ac679SLorenzo Pieralisi  *
1459c04ac679SLorenzo Pieralisi  * Evaluate DMA regions and return respectively DMA region start, offset
1460c04ac679SLorenzo Pieralisi  * and size in dma_addr, offset and size on parsing success; it does not
1461c04ac679SLorenzo Pieralisi  * update the passed in values on failure.
1462c04ac679SLorenzo Pieralisi  *
1463c04ac679SLorenzo Pieralisi  * Return 0 on success, < 0 on failure.
1464c04ac679SLorenzo Pieralisi  */
1465c04ac679SLorenzo Pieralisi int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1466c04ac679SLorenzo Pieralisi 		       u64 *size)
1467c04ac679SLorenzo Pieralisi {
1468c04ac679SLorenzo Pieralisi 	struct acpi_device *adev;
1469c04ac679SLorenzo Pieralisi 	LIST_HEAD(list);
1470c04ac679SLorenzo Pieralisi 	struct resource_entry *rentry;
1471c04ac679SLorenzo Pieralisi 	int ret;
1472c04ac679SLorenzo Pieralisi 	struct device *dma_dev = dev;
1473c04ac679SLorenzo Pieralisi 	u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1474c04ac679SLorenzo Pieralisi 
1475c04ac679SLorenzo Pieralisi 	/*
1476c04ac679SLorenzo Pieralisi 	 * Walk the device tree chasing an ACPI companion with a _DMA
1477c04ac679SLorenzo Pieralisi 	 * object while we go. Stop if we find a device with an ACPI
1478c04ac679SLorenzo Pieralisi 	 * companion containing a _DMA method.
1479c04ac679SLorenzo Pieralisi 	 */
1480c04ac679SLorenzo Pieralisi 	do {
1481c04ac679SLorenzo Pieralisi 		adev = ACPI_COMPANION(dma_dev);
1482c04ac679SLorenzo Pieralisi 		if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1483c04ac679SLorenzo Pieralisi 			break;
1484c04ac679SLorenzo Pieralisi 
1485c04ac679SLorenzo Pieralisi 		dma_dev = dma_dev->parent;
1486c04ac679SLorenzo Pieralisi 	} while (dma_dev);
1487c04ac679SLorenzo Pieralisi 
1488c04ac679SLorenzo Pieralisi 	if (!dma_dev)
1489c04ac679SLorenzo Pieralisi 		return -ENODEV;
1490c04ac679SLorenzo Pieralisi 
1491c04ac679SLorenzo Pieralisi 	if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1492c04ac679SLorenzo Pieralisi 		acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1493c04ac679SLorenzo Pieralisi 		return -EINVAL;
1494c04ac679SLorenzo Pieralisi 	}
1495c04ac679SLorenzo Pieralisi 
1496c04ac679SLorenzo Pieralisi 	ret = acpi_dev_get_dma_resources(adev, &list);
1497c04ac679SLorenzo Pieralisi 	if (ret > 0) {
1498c04ac679SLorenzo Pieralisi 		list_for_each_entry(rentry, &list, node) {
1499c04ac679SLorenzo Pieralisi 			if (dma_offset && rentry->offset != dma_offset) {
1500c04ac679SLorenzo Pieralisi 				ret = -EINVAL;
1501c04ac679SLorenzo Pieralisi 				dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1502c04ac679SLorenzo Pieralisi 				goto out;
1503c04ac679SLorenzo Pieralisi 			}
1504c04ac679SLorenzo Pieralisi 			dma_offset = rentry->offset;
1505c04ac679SLorenzo Pieralisi 
1506c04ac679SLorenzo Pieralisi 			/* Take lower and upper limits */
1507c04ac679SLorenzo Pieralisi 			if (rentry->res->start < dma_start)
1508c04ac679SLorenzo Pieralisi 				dma_start = rentry->res->start;
1509c04ac679SLorenzo Pieralisi 			if (rentry->res->end > dma_end)
1510c04ac679SLorenzo Pieralisi 				dma_end = rentry->res->end;
1511c04ac679SLorenzo Pieralisi 		}
1512c04ac679SLorenzo Pieralisi 
1513c04ac679SLorenzo Pieralisi 		if (dma_start >= dma_end) {
1514c04ac679SLorenzo Pieralisi 			ret = -EINVAL;
1515c04ac679SLorenzo Pieralisi 			dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1516c04ac679SLorenzo Pieralisi 			goto out;
1517c04ac679SLorenzo Pieralisi 		}
1518c04ac679SLorenzo Pieralisi 
1519c04ac679SLorenzo Pieralisi 		*dma_addr = dma_start - dma_offset;
1520c04ac679SLorenzo Pieralisi 		len = dma_end - dma_start;
1521c04ac679SLorenzo Pieralisi 		*size = max(len, len + 1);
1522c04ac679SLorenzo Pieralisi 		*offset = dma_offset;
1523c04ac679SLorenzo Pieralisi 	}
1524c04ac679SLorenzo Pieralisi  out:
1525c04ac679SLorenzo Pieralisi 	acpi_dev_free_resource_list(&list);
1526c04ac679SLorenzo Pieralisi 
1527c04ac679SLorenzo Pieralisi 	return ret >= 0 ? 0 : ret;
1528c04ac679SLorenzo Pieralisi }
1529c04ac679SLorenzo Pieralisi 
153011a8c5e3SJean-Philippe Brucker #ifdef CONFIG_IOMMU_API
153111a8c5e3SJean-Philippe Brucker int acpi_iommu_fwspec_init(struct device *dev, u32 id,
153211a8c5e3SJean-Philippe Brucker 			   struct fwnode_handle *fwnode,
153311a8c5e3SJean-Philippe Brucker 			   const struct iommu_ops *ops)
153411a8c5e3SJean-Philippe Brucker {
153511a8c5e3SJean-Philippe Brucker 	int ret = iommu_fwspec_init(dev, fwnode, ops);
153611a8c5e3SJean-Philippe Brucker 
153711a8c5e3SJean-Philippe Brucker 	if (!ret)
153811a8c5e3SJean-Philippe Brucker 		ret = iommu_fwspec_add_ids(dev, &id, 1);
153911a8c5e3SJean-Philippe Brucker 
154011a8c5e3SJean-Philippe Brucker 	return ret;
154111a8c5e3SJean-Philippe Brucker }
154211a8c5e3SJean-Philippe Brucker 
154311a8c5e3SJean-Philippe Brucker static inline const struct iommu_ops *acpi_iommu_fwspec_ops(struct device *dev)
154411a8c5e3SJean-Philippe Brucker {
154511a8c5e3SJean-Philippe Brucker 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
154611a8c5e3SJean-Philippe Brucker 
154711a8c5e3SJean-Philippe Brucker 	return fwspec ? fwspec->ops : NULL;
154811a8c5e3SJean-Philippe Brucker }
154911a8c5e3SJean-Philippe Brucker 
155011a8c5e3SJean-Philippe Brucker static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
155111a8c5e3SJean-Philippe Brucker 						       const u32 *id_in)
155211a8c5e3SJean-Philippe Brucker {
155311a8c5e3SJean-Philippe Brucker 	int err;
155411a8c5e3SJean-Philippe Brucker 	const struct iommu_ops *ops;
155511a8c5e3SJean-Philippe Brucker 
155611a8c5e3SJean-Philippe Brucker 	/*
155711a8c5e3SJean-Philippe Brucker 	 * If we already translated the fwspec there is nothing left to do,
155811a8c5e3SJean-Philippe Brucker 	 * return the iommu_ops.
155911a8c5e3SJean-Philippe Brucker 	 */
156011a8c5e3SJean-Philippe Brucker 	ops = acpi_iommu_fwspec_ops(dev);
156111a8c5e3SJean-Philippe Brucker 	if (ops)
156211a8c5e3SJean-Philippe Brucker 		return ops;
156311a8c5e3SJean-Philippe Brucker 
156411a8c5e3SJean-Philippe Brucker 	err = iort_iommu_configure_id(dev, id_in);
15653cf48554SJean-Philippe Brucker 	if (err && err != -EPROBE_DEFER)
15663cf48554SJean-Philippe Brucker 		err = viot_iommu_configure(dev);
156711a8c5e3SJean-Philippe Brucker 
156811a8c5e3SJean-Philippe Brucker 	/*
156911a8c5e3SJean-Philippe Brucker 	 * If we have reason to believe the IOMMU driver missed the initial
157011a8c5e3SJean-Philippe Brucker 	 * iommu_probe_device() call for dev, replay it to get things in order.
157111a8c5e3SJean-Philippe Brucker 	 */
157211a8c5e3SJean-Philippe Brucker 	if (!err && dev->bus && !device_iommu_mapped(dev))
157311a8c5e3SJean-Philippe Brucker 		err = iommu_probe_device(dev);
157411a8c5e3SJean-Philippe Brucker 
157511a8c5e3SJean-Philippe Brucker 	/* Ignore all other errors apart from EPROBE_DEFER */
157611a8c5e3SJean-Philippe Brucker 	if (err == -EPROBE_DEFER) {
157711a8c5e3SJean-Philippe Brucker 		return ERR_PTR(err);
157811a8c5e3SJean-Philippe Brucker 	} else if (err) {
157911a8c5e3SJean-Philippe Brucker 		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
158011a8c5e3SJean-Philippe Brucker 		return NULL;
158111a8c5e3SJean-Philippe Brucker 	}
158211a8c5e3SJean-Philippe Brucker 	return acpi_iommu_fwspec_ops(dev);
158311a8c5e3SJean-Philippe Brucker }
158411a8c5e3SJean-Philippe Brucker 
158511a8c5e3SJean-Philippe Brucker #else /* !CONFIG_IOMMU_API */
158611a8c5e3SJean-Philippe Brucker 
158711a8c5e3SJean-Philippe Brucker int acpi_iommu_fwspec_init(struct device *dev, u32 id,
158811a8c5e3SJean-Philippe Brucker 			   struct fwnode_handle *fwnode,
158911a8c5e3SJean-Philippe Brucker 			   const struct iommu_ops *ops)
159011a8c5e3SJean-Philippe Brucker {
159111a8c5e3SJean-Philippe Brucker 	return -ENODEV;
159211a8c5e3SJean-Philippe Brucker }
159311a8c5e3SJean-Philippe Brucker 
159411a8c5e3SJean-Philippe Brucker static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
159511a8c5e3SJean-Philippe Brucker 						       const u32 *id_in)
159611a8c5e3SJean-Philippe Brucker {
159711a8c5e3SJean-Philippe Brucker 	return NULL;
159811a8c5e3SJean-Philippe Brucker }
159911a8c5e3SJean-Philippe Brucker 
160011a8c5e3SJean-Philippe Brucker #endif /* !CONFIG_IOMMU_API */
160111a8c5e3SJean-Philippe Brucker 
1602c04ac679SLorenzo Pieralisi /**
1603c1e97359SJohn Garry  * acpi_dma_configure_id - Set-up DMA configuration for the device.
1604d760a1baSLorenzo Pieralisi  * @dev: The pointer to the device
1605d760a1baSLorenzo Pieralisi  * @attr: device dma attributes
1606b8e069a2SLorenzo Pieralisi  * @input_id: input device id const value pointer
1607d760a1baSLorenzo Pieralisi  */
1608b8e069a2SLorenzo Pieralisi int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1609b8e069a2SLorenzo Pieralisi 			  const u32 *input_id)
1610d760a1baSLorenzo Pieralisi {
1611643b8e4dSLorenzo Pieralisi 	const struct iommu_ops *iommu;
16127ad42639SLorenzo Pieralisi 	u64 dma_addr = 0, size = 0;
1613643b8e4dSLorenzo Pieralisi 
1614e5361ca2SRobin Murphy 	if (attr == DEV_DMA_NOT_SUPPORTED) {
1615e5361ca2SRobin Murphy 		set_dma_ops(dev, &dma_dummy_ops);
1616e5361ca2SRobin Murphy 		return 0;
1617e5361ca2SRobin Murphy 	}
1618e5361ca2SRobin Murphy 
1619db59e1b6SJean-Philippe Brucker 	acpi_arch_dma_setup(dev, &dma_addr, &size);
1620d760a1baSLorenzo Pieralisi 
162111a8c5e3SJean-Philippe Brucker 	iommu = acpi_iommu_configure_id(dev, input_id);
162245586c70SMasahiro Yamada 	if (PTR_ERR(iommu) == -EPROBE_DEFER)
1623058f8c3fSSricharan R 		return -EPROBE_DEFER;
1624643b8e4dSLorenzo Pieralisi 
16257ad42639SLorenzo Pieralisi 	arch_setup_dma_ops(dev, dma_addr, size,
16267ad42639SLorenzo Pieralisi 				iommu, attr == DEV_DMA_COHERENT);
16275a1bb638SSricharan R 
16285a1bb638SSricharan R 	return 0;
1629d760a1baSLorenzo Pieralisi }
1630b8e069a2SLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1631d760a1baSLorenzo Pieralisi 
1632d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev)
1633d0562674SSuthikulpanit, Suravee {
1634d0562674SSuthikulpanit, Suravee 	unsigned long long cca = 0;
1635d0562674SSuthikulpanit, Suravee 	acpi_status status;
1636d0562674SSuthikulpanit, Suravee 	struct acpi_device *parent = adev->parent;
1637d0562674SSuthikulpanit, Suravee 
1638d0562674SSuthikulpanit, Suravee 	if (parent && parent->flags.cca_seen) {
1639d0562674SSuthikulpanit, Suravee 		/*
1640d0562674SSuthikulpanit, Suravee 		 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1641d0562674SSuthikulpanit, Suravee 		 * already saw one.
1642d0562674SSuthikulpanit, Suravee 		 */
1643d0562674SSuthikulpanit, Suravee 		adev->flags.cca_seen = 1;
1644d0562674SSuthikulpanit, Suravee 		cca = parent->flags.coherent_dma;
1645d0562674SSuthikulpanit, Suravee 	} else {
1646d0562674SSuthikulpanit, Suravee 		status = acpi_evaluate_integer(adev->handle, "_CCA",
1647d0562674SSuthikulpanit, Suravee 					       NULL, &cca);
1648d0562674SSuthikulpanit, Suravee 		if (ACPI_SUCCESS(status))
1649d0562674SSuthikulpanit, Suravee 			adev->flags.cca_seen = 1;
1650d0562674SSuthikulpanit, Suravee 		else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1651d0562674SSuthikulpanit, Suravee 			/*
1652d0562674SSuthikulpanit, Suravee 			 * If architecture does not specify that _CCA is
1653d0562674SSuthikulpanit, Suravee 			 * required for DMA-able devices (e.g. x86),
1654d0562674SSuthikulpanit, Suravee 			 * we default to _CCA=1.
1655d0562674SSuthikulpanit, Suravee 			 */
1656d0562674SSuthikulpanit, Suravee 			cca = 1;
1657d0562674SSuthikulpanit, Suravee 		else
1658d0562674SSuthikulpanit, Suravee 			acpi_handle_debug(adev->handle,
1659d0562674SSuthikulpanit, Suravee 					  "ACPI device is missing _CCA.\n");
1660d0562674SSuthikulpanit, Suravee 	}
1661d0562674SSuthikulpanit, Suravee 
1662d0562674SSuthikulpanit, Suravee 	adev->flags.coherent_dma = cca;
1663d0562674SSuthikulpanit, Suravee }
1664d0562674SSuthikulpanit, Suravee 
1665e361d1f8SFrédéric Danis static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1666e4330d8bSJarkko Nikula {
1667e361d1f8SFrédéric Danis 	bool *is_serial_bus_slave_p = data;
1668e4330d8bSJarkko Nikula 
1669e4330d8bSJarkko Nikula 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1670e4330d8bSJarkko Nikula 		return 1;
1671e4330d8bSJarkko Nikula 
1672e361d1f8SFrédéric Danis 	*is_serial_bus_slave_p = true;
1673e4330d8bSJarkko Nikula 
1674e4330d8bSJarkko Nikula 	 /* no need to do more checking */
1675e4330d8bSJarkko Nikula 	return -1;
1676e4330d8bSJarkko Nikula }
1677e4330d8bSJarkko Nikula 
1678dfda4492SJohn Garry static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1679dfda4492SJohn Garry {
1680dfda4492SJohn Garry 	struct acpi_device *parent = device->parent;
1681a6f07295SJohn Garry 	static const struct acpi_device_id indirect_io_hosts[] = {
1682dfda4492SJohn Garry 		{"HISI0191", 0},
1683dfda4492SJohn Garry 		{}
1684dfda4492SJohn Garry 	};
1685dfda4492SJohn Garry 
1686dfda4492SJohn Garry 	return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1687dfda4492SJohn Garry }
1688dfda4492SJohn Garry 
1689d87fb091SJohn Garry static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1690e4330d8bSJarkko Nikula {
1691e4330d8bSJarkko Nikula 	struct list_head resource_list;
1692e361d1f8SFrédéric Danis 	bool is_serial_bus_slave = false;
1693aba94139SHans de Goede 	/*
1694aba94139SHans de Goede 	 * These devices have multiple I2cSerialBus resources and an i2c-client
1695aba94139SHans de Goede 	 * must be instantiated for each, each with its own i2c_device_id.
1696aba94139SHans de Goede 	 * Normally we only instantiate an i2c-client for the first resource,
1697aba94139SHans de Goede 	 * using the ACPI HID as id. These special cases are handled by the
1698aba94139SHans de Goede 	 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1699aba94139SHans de Goede 	 * which i2c_device_id to use for each resource.
1700aba94139SHans de Goede 	 */
1701aba94139SHans de Goede 	static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1702aba94139SHans de Goede 		{"BSG1160", },
170396f984d3SHans de Goede 		{"BSG2150", },
1704589edb56SHans de Goede 		{"INT33FE", },
1705a3dd034aSAndy Shevchenko 		{"INT3515", },
1706aba94139SHans de Goede 		{}
1707aba94139SHans de Goede 	};
1708e4330d8bSJarkko Nikula 
1709dfda4492SJohn Garry 	if (acpi_is_indirect_io_slave(device))
1710dfda4492SJohn Garry 		return true;
1711dfda4492SJohn Garry 
1712ca9ef3abSLukas Wunner 	/* Macs use device properties in lieu of _CRS resources */
1713ca9ef3abSLukas Wunner 	if (x86_apple_machine &&
1714ca9ef3abSLukas Wunner 	    (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1715e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "i2cAddress") ||
1716e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "baud")))
1717ca9ef3abSLukas Wunner 		return true;
1718ca9ef3abSLukas Wunner 
1719aba94139SHans de Goede 	/* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1720aba94139SHans de Goede 	if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1721aba94139SHans de Goede 		return false;
1722aba94139SHans de Goede 
1723e4330d8bSJarkko Nikula 	INIT_LIST_HEAD(&resource_list);
1724e361d1f8SFrédéric Danis 	acpi_dev_get_resources(device, &resource_list,
1725e361d1f8SFrédéric Danis 			       acpi_check_serial_bus_slave,
1726e361d1f8SFrédéric Danis 			       &is_serial_bus_slave);
1727e4330d8bSJarkko Nikula 	acpi_dev_free_resource_list(&resource_list);
1728e4330d8bSJarkko Nikula 
1729e361d1f8SFrédéric Danis 	return is_serial_bus_slave;
1730e4330d8bSJarkko Nikula }
1731e4330d8bSJarkko Nikula 
173282c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1733c830dbcfSRafael J. Wysocki 			     int type)
17341da177e4SLinus Torvalds {
1735d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1736d43e167dSRafael J. Wysocki 	device->device_type = type;
1737d43e167dSRafael J. Wysocki 	device->handle = handle;
1738d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
173901bb86b3SSaravana Kannan 	fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1740f5d9ab1dSRafael J. Wysocki 	acpi_set_device_status(device, ACPI_STA_DEFAULT);
1741d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1742c830dbcfSRafael J. Wysocki 	acpi_set_pnp_ids(handle, &device->pnp, type);
1743ffdcd955SMika Westerberg 	acpi_init_properties(device);
1744d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
17452c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1746202317a5SRafael J. Wysocki 	device->flags.initialized = true;
1747d87fb091SJohn Garry 	device->flags.enumeration_by_parent =
1748d87fb091SJohn Garry 		acpi_device_enumeration_by_parent(device);
174910c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(device);
1750cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1751cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1752d0562674SSuthikulpanit, Suravee 	acpi_init_coherency(device);
17536d279758SRafael J. Wysocki }
17546d279758SRafael J. Wysocki 
17556d279758SRafael J. Wysocki static void acpi_scan_dep_init(struct acpi_device *adev)
17566d279758SRafael J. Wysocki {
17576d279758SRafael J. Wysocki 	struct acpi_dep_data *dep;
17586d279758SRafael J. Wysocki 
17596d279758SRafael J. Wysocki 	list_for_each_entry(dep, &acpi_dep_list, node) {
17606d279758SRafael J. Wysocki 		if (dep->consumer == adev->handle)
17616d279758SRafael J. Wysocki 			adev->dep_unmet++;
17626d279758SRafael J. Wysocki 	}
1763cf860be6SRafael J. Wysocki }
1764bc3b0772SBjorn Helgaas 
1765cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1766cf860be6SRafael J. Wysocki {
1767cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1768cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
1771f5d9ab1dSRafael J. Wysocki static void acpi_scan_init_status(struct acpi_device *adev)
17721da177e4SLinus Torvalds {
1773f5d9ab1dSRafael J. Wysocki 	if (acpi_bus_get_status(adev))
1774f5d9ab1dSRafael J. Wysocki 		acpi_set_device_status(adev, 0);
1775f5d9ab1dSRafael J. Wysocki }
1776f5d9ab1dSRafael J. Wysocki 
17771da177e4SLinus Torvalds static int acpi_add_single_object(struct acpi_device **child,
17786d279758SRafael J. Wysocki 				  acpi_handle handle, int type, bool dep_init)
17791da177e4SLinus Torvalds {
1780e52d9d8cSRafael J. Wysocki 	struct acpi_device *device;
17815f4ce260SRafael J. Wysocki 	bool release_dep_lock = false;
1782e52d9d8cSRafael J. Wysocki 	int result;
17836091b263SHans de Goede 
178436bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1785c830dbcfSRafael J. Wysocki 	if (!device)
1786d550d98dSPatrick Mochel 		return -ENOMEM;
17871da177e4SLinus Torvalds 
1788c830dbcfSRafael J. Wysocki 	acpi_init_device_object(device, handle, type);
178963347db0SHans de Goede 	/*
1790f5d9ab1dSRafael J. Wysocki 	 * Getting the status is delayed till here so that we can call
1791f5d9ab1dSRafael J. Wysocki 	 * acpi_bus_get_status() and use its quirk handling.  Note that
1792f5d9ab1dSRafael J. Wysocki 	 * this must be done before the get power-/wakeup_dev-flags calls.
179363347db0SHans de Goede 	 */
17946d279758SRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
17955f4ce260SRafael J. Wysocki 		if (dep_init) {
17965f4ce260SRafael J. Wysocki 			mutex_lock(&acpi_dep_list_lock);
17975f4ce260SRafael J. Wysocki 			/*
17985f4ce260SRafael J. Wysocki 			 * Hold the lock until the acpi_tie_acpi_dev() call
17995f4ce260SRafael J. Wysocki 			 * below to prevent concurrent acpi_scan_clear_dep()
18005f4ce260SRafael J. Wysocki 			 * from deleting a dependency list entry without
18015f4ce260SRafael J. Wysocki 			 * updating dep_unmet for the device.
18025f4ce260SRafael J. Wysocki 			 */
18035f4ce260SRafael J. Wysocki 			release_dep_lock = true;
18046d279758SRafael J. Wysocki 			acpi_scan_dep_init(device);
18055f4ce260SRafael J. Wysocki 		}
1806f5d9ab1dSRafael J. Wysocki 		acpi_scan_init_status(device);
18076d279758SRafael J. Wysocki 	}
180863347db0SHans de Goede 
1809d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1810d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
18111da177e4SLinus Torvalds 
18125f4ce260SRafael J. Wysocki 	result = acpi_tie_acpi_dev(device);
18135f4ce260SRafael J. Wysocki 
18145f4ce260SRafael J. Wysocki 	if (release_dep_lock)
18155f4ce260SRafael J. Wysocki 		mutex_unlock(&acpi_dep_list_lock);
18165f4ce260SRafael J. Wysocki 
18175f4ce260SRafael J. Wysocki 	if (!result)
18185f4ce260SRafael J. Wysocki 		result = __acpi_device_add(device, acpi_device_release);
18195f4ce260SRafael J. Wysocki 
1820d43e167dSRafael J. Wysocki 	if (result) {
1821718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1822d550d98dSPatrick Mochel 		return result;
18231da177e4SLinus Torvalds 	}
18241da177e4SLinus Torvalds 
1825bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1826cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
1827e52d9d8cSRafael J. Wysocki 
1828e52d9d8cSRafael J. Wysocki 	acpi_handle_debug(handle, "Added as %s, parent %s\n",
1829e52d9d8cSRafael J. Wysocki 			  dev_name(&device->dev), device->parent ?
1830e52d9d8cSRafael J. Wysocki 				dev_name(&device->parent->dev) : "(null)");
1831e52d9d8cSRafael J. Wysocki 
18321da177e4SLinus Torvalds 	*child = device;
1833d43e167dSRafael J. Wysocki 	return 0;
1834bf325f95SRafael J. Wysocki }
1835bf325f95SRafael J. Wysocki 
1836a4e081b0SShannon Zhao static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1837a4e081b0SShannon Zhao 					    void *context)
1838a4e081b0SShannon Zhao {
1839a4e081b0SShannon Zhao 	struct resource *res = context;
1840a4e081b0SShannon Zhao 
1841a4e081b0SShannon Zhao 	if (acpi_dev_resource_memory(ares, res))
1842a4e081b0SShannon Zhao 		return AE_CTRL_TERMINATE;
1843a4e081b0SShannon Zhao 
1844a4e081b0SShannon Zhao 	return AE_OK;
1845a4e081b0SShannon Zhao }
1846a4e081b0SShannon Zhao 
1847a4e081b0SShannon Zhao static bool acpi_device_should_be_hidden(acpi_handle handle)
1848a4e081b0SShannon Zhao {
1849a4e081b0SShannon Zhao 	acpi_status status;
1850a4e081b0SShannon Zhao 	struct resource res;
1851a4e081b0SShannon Zhao 
1852a4e081b0SShannon Zhao 	/* Check if it should ignore the UART device */
1853a4e081b0SShannon Zhao 	if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1854a4e081b0SShannon Zhao 		return false;
1855a4e081b0SShannon Zhao 
1856a4e081b0SShannon Zhao 	/*
1857a4e081b0SShannon Zhao 	 * The UART device described in SPCR table is assumed to have only one
1858a4e081b0SShannon Zhao 	 * memory resource present. So we only look for the first one here.
1859a4e081b0SShannon Zhao 	 */
1860a4e081b0SShannon Zhao 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1861a4e081b0SShannon Zhao 				     acpi_get_resource_memory, &res);
1862a4e081b0SShannon Zhao 	if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1863a4e081b0SShannon Zhao 		return false;
1864a4e081b0SShannon Zhao 
1865a4e081b0SShannon Zhao 	acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1866a4e081b0SShannon Zhao 			 &res.start);
1867a4e081b0SShannon Zhao 
1868a4e081b0SShannon Zhao 	return true;
1869a4e081b0SShannon Zhao }
1870a4e081b0SShannon Zhao 
1871cde1f95fSSakari Ailus bool acpi_device_is_present(const struct acpi_device *adev)
1872202317a5SRafael J. Wysocki {
1873cde1f95fSSakari Ailus 	return adev->status.present || adev->status.functional;
1874202317a5SRafael J. Wysocki }
1875202317a5SRafael J. Wysocki 
18764b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1877844142c3SRasmus Villemoes 				       const char *idstr,
1878c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1879c5698074SRafael J. Wysocki {
1880c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1881c5698074SRafael J. Wysocki 
1882aca0a4ebSRafael J. Wysocki 	if (handler->match)
1883aca0a4ebSRafael J. Wysocki 		return handler->match(idstr, matchid);
1884aca0a4ebSRafael J. Wysocki 
1885c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1886c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1887c5698074SRafael J. Wysocki 			if (matchid)
1888c5698074SRafael J. Wysocki 				*matchid = devid;
1889c5698074SRafael J. Wysocki 
18904b59cc1fSRafael J. Wysocki 			return true;
18914b59cc1fSRafael J. Wysocki 		}
18924b59cc1fSRafael J. Wysocki 
18934b59cc1fSRafael J. Wysocki 	return false;
18944b59cc1fSRafael J. Wysocki }
18954b59cc1fSRafael J. Wysocki 
1896844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
18976b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
18986b772e8fSToshi Kani {
18996b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
19006b772e8fSToshi Kani 
19016b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
19026b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
19036b772e8fSToshi Kani 			return handler;
19046b772e8fSToshi Kani 
19056b772e8fSToshi Kani 	return NULL;
19066b772e8fSToshi Kani }
19076b772e8fSToshi Kani 
19083f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
19093f8055c3SRafael J. Wysocki {
19103f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
19113f8055c3SRafael J. Wysocki 		return;
19123f8055c3SRafael J. Wysocki 
19133f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
19143f8055c3SRafael J. Wysocki 
19153f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
19163f8055c3SRafael J. Wysocki 
19173f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
19183f8055c3SRafael J. Wysocki }
19193f8055c3SRafael J. Wysocki 
19203c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev)
19214b59cc1fSRafael J. Wysocki {
19226b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
19233c2cc7ffSRafael J. Wysocki 
1924b43109faSRafael J. Wysocki 	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
19251e2380cdSRafael J. Wysocki 		acpi_dock_add(adev);
19261e2380cdSRafael J. Wysocki 		return;
19271e2380cdSRafael J. Wysocki 	}
19283c2cc7ffSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
19294b59cc1fSRafael J. Wysocki 		struct acpi_scan_handler *handler;
19304b59cc1fSRafael J. Wysocki 
19316b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
19321a699476SRafael J. Wysocki 		if (handler) {
19333c2cc7ffSRafael J. Wysocki 			adev->flags.hotplug_notify = true;
19346b772e8fSToshi Kani 			break;
19356b772e8fSToshi Kani 		}
19366b772e8fSToshi Kani 	}
19371a699476SRafael J. Wysocki }
19386b772e8fSToshi Kani 
193902056a4fSRafael J. Wysocki static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
194040e7fcb1SLan Tianyu {
194140e7fcb1SLan Tianyu 	struct acpi_handle_list dep_devices;
194240e7fcb1SLan Tianyu 	acpi_status status;
19436fc25088SRafael J. Wysocki 	u32 count;
194440e7fcb1SLan Tianyu 	int i;
194540e7fcb1SLan Tianyu 
194671da201fSRafael J. Wysocki 	/*
194771da201fSRafael J. Wysocki 	 * Check for _HID here to avoid deferring the enumeration of:
194871da201fSRafael J. Wysocki 	 * 1. PCI devices.
194971da201fSRafael J. Wysocki 	 * 2. ACPI nodes describing USB ports.
195071da201fSRafael J. Wysocki 	 * Still, checking for _HID catches more then just these cases ...
195171da201fSRafael J. Wysocki 	 */
195202056a4fSRafael J. Wysocki 	if (!check_dep || !acpi_has_method(handle, "_DEP") ||
195302056a4fSRafael J. Wysocki 	    !acpi_has_method(handle, "_HID"))
19546fc25088SRafael J. Wysocki 		return 0;
195563347db0SHans de Goede 
19566fc25088SRafael J. Wysocki 	status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
195740e7fcb1SLan Tianyu 	if (ACPI_FAILURE(status)) {
19586fc25088SRafael J. Wysocki 		acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
19596fc25088SRafael J. Wysocki 		return 0;
196040e7fcb1SLan Tianyu 	}
196140e7fcb1SLan Tianyu 
19626fc25088SRafael J. Wysocki 	for (count = 0, i = 0; i < dep_devices.count; i++) {
196340e7fcb1SLan Tianyu 		struct acpi_device_info *info;
19646fc25088SRafael J. Wysocki 		struct acpi_dep_data *dep;
19656fc25088SRafael J. Wysocki 		bool skip;
196640e7fcb1SLan Tianyu 
196740e7fcb1SLan Tianyu 		status = acpi_get_object_info(dep_devices.handles[i], &info);
196840e7fcb1SLan Tianyu 		if (ACPI_FAILURE(status)) {
19696fc25088SRafael J. Wysocki 			acpi_handle_debug(handle, "Error reading _DEP device info\n");
197040e7fcb1SLan Tianyu 			continue;
197140e7fcb1SLan Tianyu 		}
197240e7fcb1SLan Tianyu 
19732ef33ee7SRafael J. Wysocki 		skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
197440e7fcb1SLan Tianyu 		kfree(info);
197540e7fcb1SLan Tianyu 
197640e7fcb1SLan Tianyu 		if (skip)
197740e7fcb1SLan Tianyu 			continue;
197840e7fcb1SLan Tianyu 
19796fc25088SRafael J. Wysocki 		dep = kzalloc(sizeof(*dep), GFP_KERNEL);
198040e7fcb1SLan Tianyu 		if (!dep)
19816fc25088SRafael J. Wysocki 			continue;
19826fc25088SRafael J. Wysocki 
19836fc25088SRafael J. Wysocki 		count++;
198440e7fcb1SLan Tianyu 
198591438aebSRafael J. Wysocki 		dep->supplier = dep_devices.handles[i];
19866fc25088SRafael J. Wysocki 		dep->consumer = handle;
198740e7fcb1SLan Tianyu 
198840e7fcb1SLan Tianyu 		mutex_lock(&acpi_dep_list_lock);
198940e7fcb1SLan Tianyu 		list_add_tail(&dep->node , &acpi_dep_list);
199040e7fcb1SLan Tianyu 		mutex_unlock(&acpi_dep_list_lock);
199140e7fcb1SLan Tianyu 	}
19926fc25088SRafael J. Wysocki 
19936fc25088SRafael J. Wysocki 	return count;
199440e7fcb1SLan Tianyu }
199540e7fcb1SLan Tianyu 
19960de7fb7cSRafael J. Wysocki static bool acpi_bus_scan_second_pass;
19970de7fb7cSRafael J. Wysocki 
199871da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
199971da201fSRafael J. Wysocki 				      struct acpi_device **adev_p)
20001da177e4SLinus Torvalds {
2001805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
2002e6c1067dSRafael J. Wysocki 	acpi_object_type acpi_type;
20036fc25088SRafael J. Wysocki 	int type;
20041da177e4SLinus Torvalds 
20054002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
20064002bf38SRafael J. Wysocki 	if (device)
20074002bf38SRafael J. Wysocki 		goto out;
20084002bf38SRafael J. Wysocki 
2009e6c1067dSRafael J. Wysocki 	if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
201051a85fafSBjorn Helgaas 		return AE_OK;
20111da177e4SLinus Torvalds 
2012e6c1067dSRafael J. Wysocki 	switch (acpi_type) {
2013e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_DEVICE:
2014e6c1067dSRafael J. Wysocki 		if (acpi_device_should_be_hidden(handle))
201582c7d5efSRafael J. Wysocki 			return AE_OK;
201682c7d5efSRafael J. Wysocki 
201702056a4fSRafael J. Wysocki 		/* Bail out if there are dependencies. */
201802056a4fSRafael J. Wysocki 		if (acpi_scan_check_dep(handle, check_dep) > 0) {
20190de7fb7cSRafael J. Wysocki 			acpi_bus_scan_second_pass = true;
202071da201fSRafael J. Wysocki 			return AE_CTRL_DEPTH;
202171da201fSRafael J. Wysocki 		}
202202056a4fSRafael J. Wysocki 
2023e6c1067dSRafael J. Wysocki 		fallthrough;
2024e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_ANY:	/* for ACPI_ROOT_OBJECT */
2025e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_DEVICE;
2026e6c1067dSRafael J. Wysocki 		break;
2027e6c1067dSRafael J. Wysocki 
2028e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_PROCESSOR:
2029e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_PROCESSOR;
2030e6c1067dSRafael J. Wysocki 		break;
2031e6c1067dSRafael J. Wysocki 
2032e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_THERMAL:
2033e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_THERMAL;
2034e6c1067dSRafael J. Wysocki 		break;
2035e6c1067dSRafael J. Wysocki 
2036e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_POWER:
203702056a4fSRafael J. Wysocki 		acpi_add_power_resource(handle);
203802056a4fSRafael J. Wysocki 		fallthrough;
2039e6c1067dSRafael J. Wysocki 	default:
2040e6c1067dSRafael J. Wysocki 		return AE_OK;
20410de7fb7cSRafael J. Wysocki 	}
20426fc25088SRafael J. Wysocki 
20433e759425SHans de Goede 	/*
20443e759425SHans de Goede 	 * If check_dep is true at this point, the device has no dependencies,
20453e759425SHans de Goede 	 * or the creation of the device object would have been postponed above.
20463e759425SHans de Goede 	 */
20476d279758SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, !check_dep);
20486d279758SRafael J. Wysocki 	if (!device)
20496d279758SRafael J. Wysocki 		return AE_CTRL_DEPTH;
20506d279758SRafael J. Wysocki 
20516d279758SRafael J. Wysocki 	acpi_scan_init_hotplug(device);
20523c2cc7ffSRafael J. Wysocki 
20534002bf38SRafael J. Wysocki out:
205471da201fSRafael J. Wysocki 	if (!*adev_p)
205571da201fSRafael J. Wysocki 		*adev_p = device;
2056805d410fSRafael J. Wysocki 
205751a85fafSBjorn Helgaas 	return AE_OK;
20581da177e4SLinus Torvalds }
20591da177e4SLinus Torvalds 
206071da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
206171da201fSRafael J. Wysocki 					void *not_used, void **ret_p)
206271da201fSRafael J. Wysocki {
206371da201fSRafael J. Wysocki 	return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
206471da201fSRafael J. Wysocki }
206571da201fSRafael J. Wysocki 
206671da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
206771da201fSRafael J. Wysocki 					void *not_used, void **ret_p)
206871da201fSRafael J. Wysocki {
206971da201fSRafael J. Wysocki 	return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
207071da201fSRafael J. Wysocki }
207171da201fSRafael J. Wysocki 
207248459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device)
207348459340SZhang Rui {
207448459340SZhang Rui 	/*
2075d87fb091SJohn Garry 	 * Do not enumerate devices with enumeration_by_parent flag set as
2076d87fb091SJohn Garry 	 * they will be enumerated by their respective parents.
207748459340SZhang Rui 	 */
2078d87fb091SJohn Garry 	if (!device->flags.enumeration_by_parent) {
20791571875bSHeikki Krogerus 		acpi_create_platform_device(device, NULL);
208010c7e20bSOctavian Purdila 		acpi_device_set_enumerated(device);
208168bdb677SOctavian Purdila 	} else {
208268bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
208368bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_ADD, device);
208410c7e20bSOctavian Purdila 	}
208548459340SZhang Rui }
208648459340SZhang Rui 
20877d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = {
2088ee892094SRafael J. Wysocki 	{ACPI_DT_NAMESPACE_HID, },
20897d284352SRafael J. Wysocki 	{"", },
20907d284352SRafael J. Wysocki };
20917d284352SRafael J. Wysocki 
20927d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev,
20937d284352SRafael J. Wysocki 				      const struct acpi_device_id *not_used)
20947d284352SRafael J. Wysocki {
20957d284352SRafael J. Wysocki 	/*
2096ee892094SRafael J. Wysocki 	 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2097ee892094SRafael J. Wysocki 	 * below can be unconditional.
20987d284352SRafael J. Wysocki 	 */
20997d284352SRafael J. Wysocki 	if (adev->data.of_compatible)
21007d284352SRafael J. Wysocki 		acpi_default_enumeration(adev);
21017d284352SRafael J. Wysocki 
21027d284352SRafael J. Wysocki 	return 1;
21037d284352SRafael J. Wysocki }
21047d284352SRafael J. Wysocki 
21057d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = {
21067d284352SRafael J. Wysocki 	.ids = generic_device_ids,
21077d284352SRafael J. Wysocki 	.attach = acpi_generic_device_attach,
21087d284352SRafael J. Wysocki };
21097d284352SRafael J. Wysocki 
211087b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
211187b85b3cSRafael J. Wysocki {
211287b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
211387b85b3cSRafael J. Wysocki 	int ret = 0;
211487b85b3cSRafael J. Wysocki 
211587b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
2116c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
2117c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
211887b85b3cSRafael J. Wysocki 
2119c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
2120c5698074SRafael J. Wysocki 		if (handler) {
2121d34afa9dSRafael J. Wysocki 			if (!handler->attach) {
2122d34afa9dSRafael J. Wysocki 				device->pnp.type.platform_id = 0;
2123d34afa9dSRafael J. Wysocki 				continue;
2124d34afa9dSRafael J. Wysocki 			}
2125c5698074SRafael J. Wysocki 			device->handler = handler;
21269cb32acfSRafael J. Wysocki 			ret = handler->attach(device, devid);
21279cb32acfSRafael J. Wysocki 			if (ret > 0)
2128c5698074SRafael J. Wysocki 				break;
21299cb32acfSRafael J. Wysocki 
21309cb32acfSRafael J. Wysocki 			device->handler = NULL;
21319cb32acfSRafael J. Wysocki 			if (ret < 0)
2132c5698074SRafael J. Wysocki 				break;
2133c5698074SRafael J. Wysocki 		}
2134c5698074SRafael J. Wysocki 	}
213548459340SZhang Rui 
2136ca589f94SRafael J. Wysocki 	return ret;
2137ca589f94SRafael J. Wysocki }
2138ca589f94SRafael J. Wysocki 
213971da201fSRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
214051a85fafSBjorn Helgaas {
21412c22e652SRafael J. Wysocki 	struct acpi_device *child;
214271da201fSRafael J. Wysocki 	bool skip = !first_pass && device->flags.visited;
21431e2380cdSRafael J. Wysocki 	acpi_handle ejd;
2144ca589f94SRafael J. Wysocki 	int ret;
21451da177e4SLinus Torvalds 
214671da201fSRafael J. Wysocki 	if (skip)
214771da201fSRafael J. Wysocki 		goto ok;
214871da201fSRafael J. Wysocki 
21491e2380cdSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
21501e2380cdSRafael J. Wysocki 		register_dock_dependent_device(device, ejd);
21511e2380cdSRafael J. Wysocki 
21522c22e652SRafael J. Wysocki 	acpi_bus_get_status(device);
2153202317a5SRafael J. Wysocki 	/* Skip devices that are not present. */
21542c22e652SRafael J. Wysocki 	if (!acpi_device_is_present(device)) {
2155cde1f95fSSakari Ailus 		device->flags.initialized = false;
215610c7e20bSOctavian Purdila 		acpi_device_clear_enumerated(device);
21571b1f3e16SRafael J. Wysocki 		device->flags.power_manageable = 0;
21582c22e652SRafael J. Wysocki 		return;
21592c22e652SRafael J. Wysocki 	}
21603a391a39SRafael J. Wysocki 	if (device->handler)
21612c22e652SRafael J. Wysocki 		goto ok;
21623a391a39SRafael J. Wysocki 
2163202317a5SRafael J. Wysocki 	if (!device->flags.initialized) {
21641b1f3e16SRafael J. Wysocki 		device->flags.power_manageable =
21651b1f3e16SRafael J. Wysocki 			device->power.states[ACPI_STATE_D0].flags.valid;
21661b1f3e16SRafael J. Wysocki 		if (acpi_bus_init_power(device))
21671b1f3e16SRafael J. Wysocki 			device->flags.power_manageable = 0;
21681b1f3e16SRafael J. Wysocki 
2169202317a5SRafael J. Wysocki 		device->flags.initialized = true;
2170c381fc3aSRafael J. Wysocki 	} else if (device->flags.visited) {
2171c381fc3aSRafael J. Wysocki 		goto ok;
2172202317a5SRafael J. Wysocki 	}
217310c7e20bSOctavian Purdila 
2174ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
21756931007cSRafael J. Wysocki 	if (ret < 0)
21762c22e652SRafael J. Wysocki 		return;
21776931007cSRafael J. Wysocki 
21786931007cSRafael J. Wysocki 	device->flags.match_driver = true;
2179d87fb091SJohn Garry 	if (ret > 0 && !device->flags.enumeration_by_parent) {
2180f406270bSRafael J. Wysocki 		acpi_device_set_enumerated(device);
2181f406270bSRafael J. Wysocki 		goto ok;
2182f406270bSRafael J. Wysocki 	}
2183f406270bSRafael J. Wysocki 
2184ca589f94SRafael J. Wysocki 	ret = device_attach(&device->dev);
2185202317a5SRafael J. Wysocki 	if (ret < 0)
21862c22e652SRafael J. Wysocki 		return;
21877d284352SRafael J. Wysocki 
2188d87fb091SJohn Garry 	if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2189e4330d8bSJarkko Nikula 		acpi_default_enumeration(device);
2190d87fb091SJohn Garry 	else
2191d87fb091SJohn Garry 		acpi_device_set_enumerated(device);
2192202317a5SRafael J. Wysocki 
2193202317a5SRafael J. Wysocki  ok:
21942c22e652SRafael J. Wysocki 	list_for_each_entry(child, &device->children, node)
219571da201fSRafael J. Wysocki 		acpi_bus_attach(child, first_pass);
21968ab17fc9SRafael J. Wysocki 
219771da201fSRafael J. Wysocki 	if (!skip && device->handler && device->handler->hotplug.notify_online)
21988ab17fc9SRafael J. Wysocki 		device->handler->hotplug.notify_online(device);
2199805d410fSRafael J. Wysocki }
22003fb02738SRajesh Shah 
2201b83e2b30SDaniel Scally static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
220240e7fcb1SLan Tianyu {
220340e7fcb1SLan Tianyu 	struct acpi_device *adev;
220440e7fcb1SLan Tianyu 
2205b83e2b30SDaniel Scally 	adev = acpi_bus_get_acpi_device(dep->consumer);
2206ad4d451eSRafael J. Wysocki 	if (adev) {
2207b83e2b30SDaniel Scally 		*(struct acpi_device **)data = adev;
2208b83e2b30SDaniel Scally 		return 1;
2209b83e2b30SDaniel Scally 	}
2210ad4d451eSRafael J. Wysocki 	/* Continue parsing if the device object is not present. */
2211ad4d451eSRafael J. Wysocki 	return 0;
2212ad4d451eSRafael J. Wysocki }
2213b83e2b30SDaniel Scally 
2214dc612486SRafael J. Wysocki struct acpi_scan_clear_dep_work {
2215dc612486SRafael J. Wysocki 	struct work_struct work;
2216dc612486SRafael J. Wysocki 	struct acpi_device *adev;
2217dc612486SRafael J. Wysocki };
2218dc612486SRafael J. Wysocki 
2219dc612486SRafael J. Wysocki static void acpi_scan_clear_dep_fn(struct work_struct *work)
2220dc612486SRafael J. Wysocki {
2221dc612486SRafael J. Wysocki 	struct acpi_scan_clear_dep_work *cdw;
2222dc612486SRafael J. Wysocki 
2223dc612486SRafael J. Wysocki 	cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
2224dc612486SRafael J. Wysocki 
2225dc612486SRafael J. Wysocki 	acpi_scan_lock_acquire();
2226dc612486SRafael J. Wysocki 	acpi_bus_attach(cdw->adev, true);
2227dc612486SRafael J. Wysocki 	acpi_scan_lock_release();
2228dc612486SRafael J. Wysocki 
2229dc612486SRafael J. Wysocki 	acpi_dev_put(cdw->adev);
2230dc612486SRafael J. Wysocki 	kfree(cdw);
2231dc612486SRafael J. Wysocki }
2232dc612486SRafael J. Wysocki 
2233dc612486SRafael J. Wysocki static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2234dc612486SRafael J. Wysocki {
2235dc612486SRafael J. Wysocki 	struct acpi_scan_clear_dep_work *cdw;
2236dc612486SRafael J. Wysocki 
2237dc612486SRafael J. Wysocki 	if (adev->dep_unmet)
2238dc612486SRafael J. Wysocki 		return false;
2239dc612486SRafael J. Wysocki 
2240dc612486SRafael J. Wysocki 	cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2241dc612486SRafael J. Wysocki 	if (!cdw)
2242dc612486SRafael J. Wysocki 		return false;
2243dc612486SRafael J. Wysocki 
2244dc612486SRafael J. Wysocki 	cdw->adev = adev;
2245dc612486SRafael J. Wysocki 	INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2246dc612486SRafael J. Wysocki 	/*
2247dc612486SRafael J. Wysocki 	 * Since the work function may block on the lock until the entire
2248dc612486SRafael J. Wysocki 	 * initial enumeration of devices is complete, put it into the unbound
2249dc612486SRafael J. Wysocki 	 * workqueue.
2250dc612486SRafael J. Wysocki 	 */
2251dc612486SRafael J. Wysocki 	queue_work(system_unbound_wq, &cdw->work);
2252dc612486SRafael J. Wysocki 
2253dc612486SRafael J. Wysocki 	return true;
2254dc612486SRafael J. Wysocki }
2255dc612486SRafael J. Wysocki 
2256a9e10e58SDaniel Scally static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
225740e7fcb1SLan Tianyu {
2258dc612486SRafael J. Wysocki 	struct acpi_device *adev = acpi_bus_get_acpi_device(dep->consumer);
225940e7fcb1SLan Tianyu 
22600f347aa0SHans de Goede 	if (adev) {
226140e7fcb1SLan Tianyu 		adev->dep_unmet--;
2262dc612486SRafael J. Wysocki 		if (!acpi_scan_clear_dep_queue(adev))
2263dc612486SRafael J. Wysocki 			acpi_dev_put(adev);
22640f347aa0SHans de Goede 	}
226571da201fSRafael J. Wysocki 
226640e7fcb1SLan Tianyu 	list_del(&dep->node);
226740e7fcb1SLan Tianyu 	kfree(dep);
2268a9e10e58SDaniel Scally 
2269a9e10e58SDaniel Scally 	return 0;
2270a9e10e58SDaniel Scally }
2271a9e10e58SDaniel Scally 
2272a9e10e58SDaniel Scally /**
2273a9e10e58SDaniel Scally  * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2274a9e10e58SDaniel Scally  * @handle:	The ACPI handle of the supplier device
2275a9e10e58SDaniel Scally  * @callback:	Pointer to the callback function to apply
2276a9e10e58SDaniel Scally  * @data:	Pointer to some data to pass to the callback
2277a9e10e58SDaniel Scally  *
2278a9e10e58SDaniel Scally  * The return value of the callback determines this function's behaviour. If 0
2279a9e10e58SDaniel Scally  * is returned we continue to iterate over acpi_dep_list. If a positive value
2280a9e10e58SDaniel Scally  * is returned then the loop is broken but this function returns 0. If a
2281a9e10e58SDaniel Scally  * negative value is returned by the callback then the loop is broken and that
2282a9e10e58SDaniel Scally  * value is returned as the final error.
2283a9e10e58SDaniel Scally  */
2284aff0dbd0SRafael J. Wysocki static int acpi_walk_dep_device_list(acpi_handle handle,
2285a9e10e58SDaniel Scally 				int (*callback)(struct acpi_dep_data *, void *),
2286a9e10e58SDaniel Scally 				void *data)
2287a9e10e58SDaniel Scally {
2288a9e10e58SDaniel Scally 	struct acpi_dep_data *dep, *tmp;
228923db673dSColin Ian King 	int ret = 0;
2290a9e10e58SDaniel Scally 
2291a9e10e58SDaniel Scally 	mutex_lock(&acpi_dep_list_lock);
2292a9e10e58SDaniel Scally 	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2293a9e10e58SDaniel Scally 		if (dep->supplier == handle) {
2294a9e10e58SDaniel Scally 			ret = callback(dep, data);
2295a9e10e58SDaniel Scally 			if (ret)
2296a9e10e58SDaniel Scally 				break;
229740e7fcb1SLan Tianyu 		}
229840e7fcb1SLan Tianyu 	}
229940e7fcb1SLan Tianyu 	mutex_unlock(&acpi_dep_list_lock);
2300a9e10e58SDaniel Scally 
2301a9e10e58SDaniel Scally 	return ret > 0 ? 0 : ret;
230240e7fcb1SLan Tianyu }
230340e7fcb1SLan Tianyu 
2304b8bd759aSRafael J. Wysocki /**
2305a9e10e58SDaniel Scally  * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2306a9e10e58SDaniel Scally  * @supplier: Pointer to the supplier &struct acpi_device
2307a9e10e58SDaniel Scally  *
2308a9e10e58SDaniel Scally  * Clear dependencies on the given device.
2309a9e10e58SDaniel Scally  */
2310a9e10e58SDaniel Scally void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2311a9e10e58SDaniel Scally {
2312a9e10e58SDaniel Scally 	acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2313a9e10e58SDaniel Scally }
2314a9e10e58SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2315a9e10e58SDaniel Scally 
2316a9e10e58SDaniel Scally /**
2317b83e2b30SDaniel Scally  * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2318b83e2b30SDaniel Scally  * @supplier: Pointer to the dependee device
2319b83e2b30SDaniel Scally  *
2320b83e2b30SDaniel Scally  * Returns the first &struct acpi_device which declares itself dependent on
2321b83e2b30SDaniel Scally  * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2322b83e2b30SDaniel Scally  *
2323b83e2b30SDaniel Scally  * The caller is responsible for putting the reference to adev when it is no
2324b83e2b30SDaniel Scally  * longer needed.
2325b83e2b30SDaniel Scally  */
2326b83e2b30SDaniel Scally struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2327b83e2b30SDaniel Scally {
2328b83e2b30SDaniel Scally 	struct acpi_device *adev = NULL;
2329b83e2b30SDaniel Scally 
2330b83e2b30SDaniel Scally 	acpi_walk_dep_device_list(supplier->handle,
2331b83e2b30SDaniel Scally 				  acpi_dev_get_first_consumer_dev_cb, &adev);
2332b83e2b30SDaniel Scally 
2333b83e2b30SDaniel Scally 	return adev;
2334b83e2b30SDaniel Scally }
2335b83e2b30SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
2336c10d7a13SDaniel Drake 
23373fb02738SRajesh Shah /**
23381da177e4SLinus Torvalds  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
23392c0d4fe0SRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
23402c0d4fe0SRafael J. Wysocki  *
23412c0d4fe0SRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
23425c478f49SBjorn Helgaas  * found devices.
2343c4168bffSLi Shaohua  *
23442c0d4fe0SRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
23452c0d4fe0SRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
23462c0d4fe0SRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
23472c0d4fe0SRafael J. Wysocki  * appropriate driver.
234888346167SRafael J. Wysocki  *
23492c0d4fe0SRafael J. Wysocki  * Must be called under acpi_scan_lock.
23503fb02738SRajesh Shah  */
23511da177e4SLinus Torvalds int acpi_bus_scan(acpi_handle handle)
23522c0d4fe0SRafael J. Wysocki {
235371da201fSRafael J. Wysocki 	struct acpi_device *device = NULL;
23541da177e4SLinus Torvalds 
23550de7fb7cSRafael J. Wysocki 	acpi_bus_scan_second_pass = false;
23560de7fb7cSRafael J. Wysocki 
235771da201fSRafael J. Wysocki 	/* Pass 1: Avoid enumerating devices with missing dependencies. */
235871da201fSRafael J. Wysocki 
235971da201fSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
23601da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
236171da201fSRafael J. Wysocki 				    acpi_bus_check_add_1, NULL, NULL,
236271da201fSRafael J. Wysocki 				    (void **)&device);
23631da177e4SLinus Torvalds 
236471da201fSRafael J. Wysocki 	if (!device)
23651da177e4SLinus Torvalds 		return -ENODEV;
236671da201fSRafael J. Wysocki 
236771da201fSRafael J. Wysocki 	acpi_bus_attach(device, true);
236871da201fSRafael J. Wysocki 
23690de7fb7cSRafael J. Wysocki 	if (!acpi_bus_scan_second_pass)
23700de7fb7cSRafael J. Wysocki 		return 0;
23710de7fb7cSRafael J. Wysocki 
237271da201fSRafael J. Wysocki 	/* Pass 2: Enumerate all of the remaining devices. */
237371da201fSRafael J. Wysocki 
237471da201fSRafael J. Wysocki 	device = NULL;
237571da201fSRafael J. Wysocki 
237671da201fSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
237771da201fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
237871da201fSRafael J. Wysocki 				    acpi_bus_check_add_2, NULL, NULL,
237971da201fSRafael J. Wysocki 				    (void **)&device);
238071da201fSRafael J. Wysocki 
238171da201fSRafael J. Wysocki 	acpi_bus_attach(device, false);
238271da201fSRafael J. Wysocki 
238371da201fSRafael J. Wysocki 	return 0;
23841da177e4SLinus Torvalds }
23851da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_scan);
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds /**
23881da177e4SLinus Torvalds  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
23891da177e4SLinus Torvalds  * @adev: Root of the ACPI namespace scope to walk.
23901da177e4SLinus Torvalds  *
23911da177e4SLinus Torvalds  * Must be called under acpi_scan_lock.
23921da177e4SLinus Torvalds  */
23931da177e4SLinus Torvalds void acpi_bus_trim(struct acpi_device *adev)
23941da177e4SLinus Torvalds {
23951da177e4SLinus Torvalds 	struct acpi_scan_handler *handler = adev->handler;
23961da177e4SLinus Torvalds 	struct acpi_device *child;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	list_for_each_entry_reverse(child, &adev->children, node)
23991da177e4SLinus Torvalds 		acpi_bus_trim(child);
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	adev->flags.match_driver = false;
24021da177e4SLinus Torvalds 	if (handler) {
24031da177e4SLinus Torvalds 		if (handler->detach)
24041da177e4SLinus Torvalds 			handler->detach(adev);
24051da177e4SLinus Torvalds 
24061da177e4SLinus Torvalds 		adev->handler = NULL;
24071da177e4SLinus Torvalds 	} else {
24081da177e4SLinus Torvalds 		device_release_driver(&adev->dev);
24091da177e4SLinus Torvalds 	}
24101da177e4SLinus Torvalds 	/*
24111da177e4SLinus Torvalds 	 * Most likely, the device is going away, so put it into D3cold before
24121da177e4SLinus Torvalds 	 * that.
24131da177e4SLinus Torvalds 	 */
24141da177e4SLinus Torvalds 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
24151da177e4SLinus Torvalds 	adev->flags.initialized = false;
241610c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(adev);
24171da177e4SLinus Torvalds }
24181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(acpi_bus_trim);
24191da177e4SLinus Torvalds 
2420a64a62ceSLv Zheng int acpi_bus_register_early_device(int type)
2421a64a62ceSLv Zheng {
2422a64a62ceSLv Zheng 	struct acpi_device *device = NULL;
2423a64a62ceSLv Zheng 	int result;
2424a64a62ceSLv Zheng 
24256d279758SRafael J. Wysocki 	result = acpi_add_single_object(&device, NULL, type, false);
2426a64a62ceSLv Zheng 	if (result)
2427a64a62ceSLv Zheng 		return result;
2428a64a62ceSLv Zheng 
2429a64a62ceSLv Zheng 	device->flags.match_driver = true;
2430a64a62ceSLv Zheng 	return device_attach(&device->dev);
2431a64a62ceSLv Zheng }
2432a64a62ceSLv Zheng EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2433a64a62ceSLv Zheng 
24341da177e4SLinus Torvalds static int acpi_bus_scan_fixed(void)
24351da177e4SLinus Torvalds {
24361da177e4SLinus Torvalds 	int result = 0;
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 	/*
24391da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
24401da177e4SLinus Torvalds 	 */
24411da177e4SLinus Torvalds 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
24421da177e4SLinus Torvalds 		struct acpi_device *device = NULL;
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 		result = acpi_add_single_object(&device, NULL,
24456d279758SRafael J. Wysocki 						ACPI_BUS_TYPE_POWER_BUTTON, false);
24461da177e4SLinus Torvalds 		if (result)
24471da177e4SLinus Torvalds 			return result;
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 		device->flags.match_driver = true;
24501da177e4SLinus Torvalds 		result = device_attach(&device->dev);
24511da177e4SLinus Torvalds 		if (result < 0)
24521da177e4SLinus Torvalds 			return result;
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 		device_init_wakeup(&device->dev, true);
24551da177e4SLinus Torvalds 	}
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
24581da177e4SLinus Torvalds 		struct acpi_device *device = NULL;
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 		result = acpi_add_single_object(&device, NULL,
24616d279758SRafael J. Wysocki 						ACPI_BUS_TYPE_SLEEP_BUTTON, false);
24621da177e4SLinus Torvalds 		if (result)
24631da177e4SLinus Torvalds 			return result;
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds 		device->flags.match_driver = true;
24661da177e4SLinus Torvalds 		result = device_attach(&device->dev);
24671da177e4SLinus Torvalds 	}
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 	return result < 0 ? result : 0;
24701da177e4SLinus Torvalds }
24711da177e4SLinus Torvalds 
2472a4e081b0SShannon Zhao static void __init acpi_get_spcr_uart_addr(void)
2473a4e081b0SShannon Zhao {
2474a4e081b0SShannon Zhao 	acpi_status status;
2475a4e081b0SShannon Zhao 	struct acpi_table_spcr *spcr_ptr;
2476a4e081b0SShannon Zhao 
2477a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_SPCR, 0,
2478a4e081b0SShannon Zhao 				(struct acpi_table_header **)&spcr_ptr);
2479ead7ba13SHanjun Guo 	if (ACPI_FAILURE(status)) {
24808acf4108SHanjun Guo 		pr_warn("STAO table present, but SPCR is missing\n");
2481ead7ba13SHanjun Guo 		return;
2482ead7ba13SHanjun Guo 	}
2483ead7ba13SHanjun Guo 
2484a4e081b0SShannon Zhao 	spcr_uart_addr = spcr_ptr->serial_port.address;
2485ead7ba13SHanjun Guo 	acpi_put_table((struct acpi_table_header *)spcr_ptr);
2486a4e081b0SShannon Zhao }
2487a4e081b0SShannon Zhao 
248868bdb677SOctavian Purdila static bool acpi_scan_initialized;
248968bdb677SOctavian Purdila 
2490e747f274SBjorn Helgaas int __init acpi_scan_init(void)
24911da177e4SLinus Torvalds {
24921da177e4SLinus Torvalds 	int result;
2493a4e081b0SShannon Zhao 	acpi_status status;
2494a4e081b0SShannon Zhao 	struct acpi_table_stao *stao_ptr;
24951da177e4SLinus Torvalds 
249692ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
24974daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2498ac212b69SRafael J. Wysocki 	acpi_processor_init();
2499cb0701acSAndy Shevchenko 	acpi_platform_init();
2500f58b082aSRafael J. Wysocki 	acpi_lpss_init();
250192082a88SKen Xue 	acpi_apd_init();
25022fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2503737f1a9fSRafael J. Wysocki 	acpi_container_init();
25040a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
2505cc6a0e31SMika Westerberg 	acpi_watchdog_init();
2506eec15edbSZhang Rui 	acpi_pnp_init();
25073230bbfcSZhang Rui 	acpi_int340x_thermal_init();
25086ce2e188SGraeme Gregory 	acpi_amba_init();
2509eeb2d80dSSrinivas Pandruvada 	acpi_init_lpit();
251097d9a9e9SRafael J. Wysocki 
25117d284352SRafael J. Wysocki 	acpi_scan_add_handler(&generic_device_handler);
25127d284352SRafael J. Wysocki 
2513a4e081b0SShannon Zhao 	/*
2514a4e081b0SShannon Zhao 	 * If there is STAO table, check whether it needs to ignore the UART
2515a4e081b0SShannon Zhao 	 * device in SPCR table.
2516a4e081b0SShannon Zhao 	 */
2517a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_STAO, 0,
2518a4e081b0SShannon Zhao 				(struct acpi_table_header **)&stao_ptr);
2519a4e081b0SShannon Zhao 	if (ACPI_SUCCESS(status)) {
2520a4e081b0SShannon Zhao 		if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
25218acf4108SHanjun Guo 			pr_info("STAO Name List not yet supported.\n");
2522a4e081b0SShannon Zhao 
2523a4e081b0SShannon Zhao 		if (stao_ptr->ignore_uart)
2524a4e081b0SShannon Zhao 			acpi_get_spcr_uart_addr();
2525ead7ba13SHanjun Guo 
2526ead7ba13SHanjun Guo 		acpi_put_table((struct acpi_table_header *)stao_ptr);
2527a4e081b0SShannon Zhao 	}
2528a4e081b0SShannon Zhao 
2529eb7f43c4SRafael J. Wysocki 	acpi_gpe_apply_masked_gpes();
2530eb7f43c4SRafael J. Wysocki 	acpi_update_all_gpes();
2531eb7f43c4SRafael J. Wysocki 
25327291edcaSDavid Hildenbrand 	/*
25337291edcaSDavid Hildenbrand 	 * Although we call __add_memory() that is documented to require the
25347291edcaSDavid Hildenbrand 	 * device_hotplug_lock, it is not necessary here because this is an
25357291edcaSDavid Hildenbrand 	 * early code when userspace or any other code path cannot trigger
25367291edcaSDavid Hildenbrand 	 * hotplug/hotunplug operations.
25377291edcaSDavid Hildenbrand 	 */
25383757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
25391da177e4SLinus Torvalds 	/*
25401da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
25411da177e4SLinus Torvalds 	 */
25420cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
25431da177e4SLinus Torvalds 	if (result)
25443757b948SRafael J. Wysocki 		goto out;
25451da177e4SLinus Torvalds 
25460cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
25471da177e4SLinus Torvalds 	if (result)
25483757b948SRafael J. Wysocki 		goto out;
2549b8bd759aSRafael J. Wysocki 
25502650ef42SAaron Lu 	/* Fixed feature devices do not exist on HW-reduced platform */
25512650ef42SAaron Lu 	if (!acpi_gbl_reduced_hardware) {
2552b8bd759aSRafael J. Wysocki 		result = acpi_bus_scan_fixed();
2553b8bd759aSRafael J. Wysocki 		if (result) {
25542650ef42SAaron Lu 			acpi_detach_data(acpi_root->handle,
25552650ef42SAaron Lu 					 acpi_scan_drop_device);
2556202317a5SRafael J. Wysocki 			acpi_device_del(acpi_root);
25574cbaba4eSAndy Shevchenko 			acpi_bus_put_acpi_device(acpi_root);
25583757b948SRafael J. Wysocki 			goto out;
2559b8bd759aSRafael J. Wysocki 		}
25602650ef42SAaron Lu 	}
2561b8bd759aSRafael J. Wysocki 
2562*c10383e8SRafael J. Wysocki 	/*
2563*c10383e8SRafael J. Wysocki 	 * Make sure that power management resources are not blocked by ACPI
2564*c10383e8SRafael J. Wysocki 	 * device objects with no users.
2565*c10383e8SRafael J. Wysocki 	 */
2566*c10383e8SRafael J. Wysocki 	bus_for_each_dev(&acpi_bus_type, NULL, NULL, acpi_dev_turn_off_if_unused);
2567*c10383e8SRafael J. Wysocki 
25686381195aSRafael J. Wysocki 	acpi_turn_off_unused_power_resources();
256929038ae2SRafael J. Wysocki 
257068bdb677SOctavian Purdila 	acpi_scan_initialized = true;
257168bdb677SOctavian Purdila 
25723757b948SRafael J. Wysocki  out:
25733757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
25743757b948SRafael J. Wysocki 	return result;
25751da177e4SLinus Torvalds }
2576e647b532SMarc Zyngier 
2577e647b532SMarc Zyngier static struct acpi_probe_entry *ape;
2578e647b532SMarc Zyngier static int acpi_probe_count;
25795331d9caSLorenzo Pieralisi static DEFINE_MUTEX(acpi_probe_mutex);
2580e647b532SMarc Zyngier 
258160574d1eSKeith Busch static int __init acpi_match_madt(union acpi_subtable_headers *header,
2582e647b532SMarc Zyngier 				  const unsigned long end)
2583e647b532SMarc Zyngier {
258460574d1eSKeith Busch 	if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2585e647b532SMarc Zyngier 		if (!ape->probe_subtbl(header, end))
2586e647b532SMarc Zyngier 			acpi_probe_count++;
2587e647b532SMarc Zyngier 
2588e647b532SMarc Zyngier 	return 0;
2589e647b532SMarc Zyngier }
2590e647b532SMarc Zyngier 
2591e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2592e647b532SMarc Zyngier {
2593e647b532SMarc Zyngier 	int count = 0;
2594e647b532SMarc Zyngier 
2595e647b532SMarc Zyngier 	if (acpi_disabled)
2596e647b532SMarc Zyngier 		return 0;
2597e647b532SMarc Zyngier 
25985331d9caSLorenzo Pieralisi 	mutex_lock(&acpi_probe_mutex);
2599e647b532SMarc Zyngier 	for (ape = ap_head; nr; ape++, nr--) {
26005599fb69SBob Moore 		if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2601e647b532SMarc Zyngier 			acpi_probe_count = 0;
2602e647b532SMarc Zyngier 			acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2603e647b532SMarc Zyngier 			count += acpi_probe_count;
2604e647b532SMarc Zyngier 		} else {
2605e647b532SMarc Zyngier 			int res;
2606e647b532SMarc Zyngier 			res = acpi_table_parse(ape->id, ape->probe_table);
2607e647b532SMarc Zyngier 			if (!res)
2608e647b532SMarc Zyngier 				count++;
2609e647b532SMarc Zyngier 		}
2610e647b532SMarc Zyngier 	}
26115331d9caSLorenzo Pieralisi 	mutex_unlock(&acpi_probe_mutex);
2612e647b532SMarc Zyngier 
2613e647b532SMarc Zyngier 	return count;
2614e647b532SMarc Zyngier }
261568bdb677SOctavian Purdila 
261668bdb677SOctavian Purdila static void acpi_table_events_fn(struct work_struct *work)
261768bdb677SOctavian Purdila {
261868bdb677SOctavian Purdila 	acpi_scan_lock_acquire();
261968bdb677SOctavian Purdila 	acpi_bus_scan(ACPI_ROOT_OBJECT);
262068bdb677SOctavian Purdila 	acpi_scan_lock_release();
26218d287e82SRafael J. Wysocki 
26228d287e82SRafael J. Wysocki 	kfree(work);
262368bdb677SOctavian Purdila }
262468bdb677SOctavian Purdila 
26258d287e82SRafael J. Wysocki void acpi_scan_table_notify(void)
262668bdb677SOctavian Purdila {
26278d287e82SRafael J. Wysocki 	struct work_struct *work;
262868bdb677SOctavian Purdila 
262968bdb677SOctavian Purdila 	if (!acpi_scan_initialized)
263068bdb677SOctavian Purdila 		return;
263168bdb677SOctavian Purdila 
26328d287e82SRafael J. Wysocki 	work = kmalloc(sizeof(*work), GFP_KERNEL);
26338d287e82SRafael J. Wysocki 	if (!work)
263468bdb677SOctavian Purdila 		return;
263568bdb677SOctavian Purdila 
26368d287e82SRafael J. Wysocki 	INIT_WORK(work, acpi_table_events_fn);
26378d287e82SRafael J. Wysocki 	schedule_work(work);
263868bdb677SOctavian Purdila }
263968bdb677SOctavian Purdila 
264068bdb677SOctavian Purdila int acpi_reconfig_notifier_register(struct notifier_block *nb)
264168bdb677SOctavian Purdila {
264268bdb677SOctavian Purdila 	return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
264368bdb677SOctavian Purdila }
264468bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_register);
264568bdb677SOctavian Purdila 
264668bdb677SOctavian Purdila int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
264768bdb677SOctavian Purdila {
264868bdb677SOctavian Purdila 	return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
264968bdb677SOctavian Purdila }
265068bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);
2651