xref: /openbmc/linux/drivers/acpi/scan.c (revision 45e9aa1fdbb2ebafec88c64bc53fe45cf8935b49)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * scan.c - support for transforming the ACPI namespace into individual objects
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
68acf4108SHanjun Guo #define pr_fmt(fmt) "ACPI: " fmt
78acf4108SHanjun Guo 
81da177e4SLinus Torvalds #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/init.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
119b6d97b6SRandy Dunlap #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/acpi.h>
13643b8e4dSLorenzo Pieralisi #include <linux/acpi_iort.h>
143cf48554SJean-Philippe Brucker #include <linux/acpi_viot.h>
1511a8c5e3SJean-Philippe Brucker #include <linux/iommu.h>
1674523c90SAlok N Kataria #include <linux/signal.h>
1774523c90SAlok N Kataria #include <linux/kthread.h>
18222e82acSDarrick J. Wong #include <linux/dmi.h>
190a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
20ca9ef3abSLukas Wunner #include <linux/platform_data/x86/apple.h>
21ca5999fdSMike Rapoport #include <linux/pgtable.h>
22882c982dSHeikki Krogerus #include <linux/crc32.h>
23d783156eSRafael J. Wysocki 
24e60cc7a6SBjorn Helgaas #include "internal.h"
25e60cc7a6SBjorn Helgaas 
261da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2929b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
301da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
311da177e4SLinus Torvalds 
32859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
33859ac9a4SBjorn Helgaas 
34d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)
35d783156eSRafael J. Wysocki 
36620e112cSThomas Renninger static const char *dummy_hid = "device";
372b2ae7c7SThomas Renninger 
3840e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list);
3940e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock);
402d12b6b3SLukas Wunner LIST_HEAD(acpi_bus_id_list);
41c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
42ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
439090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
441da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
45e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock);
461da177e4SLinus Torvalds 
47a4e081b0SShannon Zhao /*
48a4e081b0SShannon Zhao  * The UART device described by the SPCR table is the only object which needs
49a4e081b0SShannon Zhao  * special-casing. Everything else is covered by ACPI namespace paths in STAO
50a4e081b0SShannon Zhao  * table.
51a4e081b0SShannon Zhao  */
52a4e081b0SShannon Zhao static u64 spcr_uart_addr;
53a4e081b0SShannon Zhao 
543757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
553757b948SRafael J. Wysocki {
563757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
573757b948SRafael J. Wysocki }
583757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
593757b948SRafael J. Wysocki 
603757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
613757b948SRafael J. Wysocki {
623757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
633757b948SRafael J. Wysocki }
643757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
653757b948SRafael J. Wysocki 
66e525506fSRafael J. Wysocki void acpi_lock_hp_context(void)
67e525506fSRafael J. Wysocki {
68e525506fSRafael J. Wysocki 	mutex_lock(&acpi_hp_context_lock);
69e525506fSRafael J. Wysocki }
70e525506fSRafael J. Wysocki 
71e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void)
72e525506fSRafael J. Wysocki {
73e525506fSRafael J. Wysocki 	mutex_unlock(&acpi_hp_context_lock);
74e525506fSRafael J. Wysocki }
75e525506fSRafael J. Wysocki 
765d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev,
775d513205SRafael J. Wysocki 				struct acpi_hotplug_context *hp,
785d513205SRafael J. Wysocki 				int (*notify)(struct acpi_device *, u32),
795d513205SRafael J. Wysocki 				void (*uevent)(struct acpi_device *, u32))
805d513205SRafael J. Wysocki {
815d513205SRafael J. Wysocki 	acpi_lock_hp_context();
82ba574dc8SRafael J. Wysocki 	hp->notify = notify;
83ba574dc8SRafael J. Wysocki 	hp->uevent = uevent;
84ba574dc8SRafael J. Wysocki 	acpi_set_hp_context(adev, hp);
855d513205SRafael J. Wysocki 	acpi_unlock_hp_context();
865d513205SRafael J. Wysocki }
875d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
885d513205SRafael J. Wysocki 
89ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
90ca589f94SRafael J. Wysocki {
91d34afa9dSRafael J. Wysocki 	if (!handler)
92ca589f94SRafael J. Wysocki 		return -EINVAL;
93ca589f94SRafael J. Wysocki 
94ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
95ca589f94SRafael J. Wysocki 	return 0;
96ca589f94SRafael J. Wysocki }
97ca589f94SRafael J. Wysocki 
983f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
993f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
1003f8055c3SRafael J. Wysocki {
1013f8055c3SRafael J. Wysocki 	int error;
1023f8055c3SRafael J. Wysocki 
1033f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
1043f8055c3SRafael J. Wysocki 	if (error)
1053f8055c3SRafael J. Wysocki 		return error;
1063f8055c3SRafael J. Wysocki 
1073f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
1083f8055c3SRafael J. Wysocki 	return 0;
1093f8055c3SRafael J. Wysocki }
1103f8055c3SRafael J. Wysocki 
111caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
112d22ddcbcSRafael J. Wysocki {
113d22ddcbcSRafael J. Wysocki 	struct acpi_device_physical_node *pn;
114d22ddcbcSRafael J. Wysocki 	bool offline = true;
11527664c58SLee, Chun-Yi 	char *envp[] = { "EVENT=offline", NULL };
116d22ddcbcSRafael J. Wysocki 
1174c533c80SRafael J. Wysocki 	/*
1184c533c80SRafael J. Wysocki 	 * acpi_container_offline() calls this for all of the container's
1194c533c80SRafael J. Wysocki 	 * children under the container's physical_node_lock lock.
1204c533c80SRafael J. Wysocki 	 */
1214c533c80SRafael J. Wysocki 	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
122d22ddcbcSRafael J. Wysocki 
123d22ddcbcSRafael J. Wysocki 	list_for_each_entry(pn, &adev->physical_node_list, node)
124d22ddcbcSRafael J. Wysocki 		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
125caa73ea1SRafael J. Wysocki 			if (uevent)
12627664c58SLee, Chun-Yi 				kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
127caa73ea1SRafael J. Wysocki 
128d22ddcbcSRafael J. Wysocki 			offline = false;
129d22ddcbcSRafael J. Wysocki 			break;
130d22ddcbcSRafael J. Wysocki 		}
131d22ddcbcSRafael J. Wysocki 
132d22ddcbcSRafael J. Wysocki 	mutex_unlock(&adev->physical_node_lock);
133d22ddcbcSRafael J. Wysocki 	return offline;
134d22ddcbcSRafael J. Wysocki }
135d22ddcbcSRafael J. Wysocki 
1367f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
1377f28ddecSRafael J. Wysocki 				    void **ret_p)
138683058e3SRafael J. Wysocki {
139e3c963c4SRafael J. Wysocki 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
140683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
141303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
142683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
143683058e3SRafael J. Wysocki 
144e3c963c4SRafael J. Wysocki 	if (!device)
145683058e3SRafael J. Wysocki 		return AE_OK;
146683058e3SRafael J. Wysocki 
1477f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
1487f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
1497f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
1507f28ddecSRafael J. Wysocki 	}
1517f28ddecSRafael J. Wysocki 
152683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
153683058e3SRafael J. Wysocki 
154683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
155683058e3SRafael J. Wysocki 		int ret;
156683058e3SRafael J. Wysocki 
157303bfdb1SRafael J. Wysocki 		if (second_pass) {
158303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
159303bfdb1SRafael J. Wysocki 			if (pn->put_online)
160303bfdb1SRafael J. Wysocki 				continue;
161303bfdb1SRafael J. Wysocki 		} else {
162303bfdb1SRafael J. Wysocki 			pn->put_online = false;
163303bfdb1SRafael J. Wysocki 		}
164683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
165303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
166303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
167303bfdb1SRafael J. Wysocki 		} else {
168303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
169303bfdb1SRafael J. Wysocki 			if (second_pass) {
170683058e3SRafael J. Wysocki 				status = AE_ERROR;
171683058e3SRafael J. Wysocki 				break;
172683058e3SRafael J. Wysocki 			}
173303bfdb1SRafael J. Wysocki 		}
174683058e3SRafael J. Wysocki 	}
175683058e3SRafael J. Wysocki 
176683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
177683058e3SRafael J. Wysocki 
178683058e3SRafael J. Wysocki 	return status;
179683058e3SRafael J. Wysocki }
180683058e3SRafael J. Wysocki 
1817f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
1827f28ddecSRafael J. Wysocki 				   void **ret_p)
183683058e3SRafael J. Wysocki {
184e3c963c4SRafael J. Wysocki 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
185683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
186683058e3SRafael J. Wysocki 
187e3c963c4SRafael J. Wysocki 	if (!device)
188683058e3SRafael J. Wysocki 		return AE_OK;
189683058e3SRafael J. Wysocki 
190683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
191683058e3SRafael J. Wysocki 
192683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
193683058e3SRafael J. Wysocki 		if (pn->put_online) {
194683058e3SRafael J. Wysocki 			device_online(pn->dev);
195683058e3SRafael J. Wysocki 			pn->put_online = false;
196683058e3SRafael J. Wysocki 		}
197683058e3SRafael J. Wysocki 
198683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
199683058e3SRafael J. Wysocki 
200683058e3SRafael J. Wysocki 	return AE_OK;
201683058e3SRafael J. Wysocki }
202683058e3SRafael J. Wysocki 
203d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device)
2041da177e4SLinus Torvalds {
2055993c467SYinghai Lu 	acpi_handle handle = device->handle;
206d22ddcbcSRafael J. Wysocki 	struct device *errdev = NULL;
207a33ec399SRafael J. Wysocki 	acpi_status status;
208f058cdf4SRafael J. Wysocki 
209303bfdb1SRafael J. Wysocki 	/*
210303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
211303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
212303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
213303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
214303bfdb1SRafael J. Wysocki 	 * block holds them.
215303bfdb1SRafael J. Wysocki 	 *
216303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
217303bfdb1SRafael J. Wysocki 	 */
2187f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2197f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
2207f28ddecSRafael J. Wysocki 				     (void **)&errdev);
2217f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
2227f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
223683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2247f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
2257f28ddecSRafael J. Wysocki 		return -EPERM;
2267f28ddecSRafael J. Wysocki 	}
2277f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
228303bfdb1SRafael J. Wysocki 	if (errdev) {
229303bfdb1SRafael J. Wysocki 		errdev = NULL;
230303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2317f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
2327f28ddecSRafael J. Wysocki 				    (void **)&errdev);
233ffc10d82SMichal Hocko 		if (!errdev)
2347f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
235303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
236303bfdb1SRafael J. Wysocki 
237ffc10d82SMichal Hocko 		if (errdev) {
238303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
2397f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
240303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
2417f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
2427f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
243683058e3SRafael J. Wysocki 			return -EBUSY;
244683058e3SRafael J. Wysocki 		}
245303bfdb1SRafael J. Wysocki 	}
246d22ddcbcSRafael J. Wysocki 	return 0;
247d22ddcbcSRafael J. Wysocki }
248d22ddcbcSRafael J. Wysocki 
249d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
250d22ddcbcSRafael J. Wysocki {
251d22ddcbcSRafael J. Wysocki 	acpi_handle handle = device->handle;
252d22ddcbcSRafael J. Wysocki 	unsigned long long sta;
253d22ddcbcSRafael J. Wysocki 	acpi_status status;
254d22ddcbcSRafael J. Wysocki 
255ffc10d82SMichal Hocko 	if (device->handler && device->handler->hotplug.demand_offline) {
256caa73ea1SRafael J. Wysocki 		if (!acpi_scan_is_offline(device, true))
257d22ddcbcSRafael J. Wysocki 			return -EBUSY;
258d22ddcbcSRafael J. Wysocki 	} else {
259d22ddcbcSRafael J. Wysocki 		int error = acpi_scan_try_to_offline(device);
260d22ddcbcSRafael J. Wysocki 		if (error)
261d22ddcbcSRafael J. Wysocki 			return error;
262d22ddcbcSRafael J. Wysocki 	}
263683058e3SRafael J. Wysocki 
264e52d9d8cSRafael J. Wysocki 	acpi_handle_debug(handle, "Ejecting\n");
26526d46867SZhang Rui 
266bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
267683058e3SRafael J. Wysocki 
2687d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2691da177e4SLinus Torvalds 	/*
2701da177e4SLinus Torvalds 	 * TBD: _EJD support.
2711da177e4SLinus Torvalds 	 */
2727d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2737d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
274a33ec399SRafael J. Wysocki 		return -ENODEV;
2757d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
276a33ec399SRafael J. Wysocki 		return -EIO;
277f058cdf4SRafael J. Wysocki 
278882fd12eSToshi Kani 	/*
279882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
280882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
281882fd12eSToshi Kani 	 */
282882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
283882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
284882fd12eSToshi Kani 		acpi_handle_warn(handle,
285882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
286882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
287882fd12eSToshi Kani 		acpi_handle_warn(handle,
288882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
289882fd12eSToshi Kani 	}
290882fd12eSToshi Kani 
291a33ec399SRafael J. Wysocki 	return 0;
292a33ec399SRafael J. Wysocki }
293f058cdf4SRafael J. Wysocki 
294443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev)
295443fc820SRafael J. Wysocki {
296443fc820SRafael J. Wysocki 	if (!acpi_device_enumerated(adev)) {
297443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Still not present\n");
298443fc820SRafael J. Wysocki 		return -EALREADY;
299443fc820SRafael J. Wysocki 	}
300443fc820SRafael J. Wysocki 	acpi_bus_trim(adev);
301443fc820SRafael J. Wysocki 	return 0;
302443fc820SRafael J. Wysocki }
303443fc820SRafael J. Wysocki 
304c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev)
305a33ec399SRafael J. Wysocki {
306f943db40SRafael J. Wysocki 	int error;
307a33ec399SRafael J. Wysocki 
308443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
309443fc820SRafael J. Wysocki 	if (adev->status.present || adev->status.functional) {
310443fc820SRafael J. Wysocki 		/*
311443fc820SRafael J. Wysocki 		 * This function is only called for device objects for which
312443fc820SRafael J. Wysocki 		 * matching scan handlers exist.  The only situation in which
313443fc820SRafael J. Wysocki 		 * the scan handler is not attached to this device object yet
314443fc820SRafael J. Wysocki 		 * is when the device has just appeared (either it wasn't
315443fc820SRafael J. Wysocki 		 * present at all before or it was removed and then added
316443fc820SRafael J. Wysocki 		 * again).
317443fc820SRafael J. Wysocki 		 */
318443fc820SRafael J. Wysocki 		if (adev->handler) {
319443fc820SRafael J. Wysocki 			dev_warn(&adev->dev, "Already enumerated\n");
320443fc820SRafael J. Wysocki 			return -EALREADY;
321443fc820SRafael J. Wysocki 		}
322c27b2c33SRafael J. Wysocki 		error = acpi_bus_scan(adev->handle);
323a33ec399SRafael J. Wysocki 		if (error) {
324c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Namespace scan failure\n");
325c27b2c33SRafael J. Wysocki 			return error;
326a33ec399SRafael J. Wysocki 		}
32746394fd0SRafael J. Wysocki 		if (!adev->handler) {
328c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Enumeration failure\n");
329443fc820SRafael J. Wysocki 			error = -ENODEV;
330443fc820SRafael J. Wysocki 		}
331443fc820SRafael J. Wysocki 	} else {
332443fc820SRafael J. Wysocki 		error = acpi_scan_device_not_present(adev);
333443fc820SRafael J. Wysocki 	}
334443fc820SRafael J. Wysocki 	return error;
335443fc820SRafael J. Wysocki }
336443fc820SRafael J. Wysocki 
337a976a2acSRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev, void *not_used)
338443fc820SRafael J. Wysocki {
339443fc820SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
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 	}
355a976a2acSRafael J. Wysocki 	return acpi_dev_for_each_child(adev, acpi_scan_bus_check, NULL);
356c27b2c33SRafael J. Wysocki }
357c27b2c33SRafael J. Wysocki 
3583c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
3593c2cc7ffSRafael J. Wysocki {
3603c2cc7ffSRafael J. Wysocki 	switch (type) {
3613c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
362a976a2acSRafael J. Wysocki 		return acpi_scan_bus_check(adev, NULL);
3633c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
3643c2cc7ffSRafael J. Wysocki 		return acpi_scan_device_check(adev);
3653c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
3663c2cc7ffSRafael J. Wysocki 	case ACPI_OST_EC_OSPM_EJECT:
367dd2151beSRafael J. Wysocki 		if (adev->handler && !adev->handler->hotplug.enabled) {
368dd2151beSRafael J. Wysocki 			dev_info(&adev->dev, "Eject disabled\n");
369dd2151beSRafael J. Wysocki 			return -EPERM;
370dd2151beSRafael J. Wysocki 		}
371700b8422SRafael J. Wysocki 		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
372dd2151beSRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3733c2cc7ffSRafael J. Wysocki 		return acpi_scan_hot_remove(adev);
3743c2cc7ffSRafael J. Wysocki 	}
3753c2cc7ffSRafael J. Wysocki 	return -EINVAL;
3763c2cc7ffSRafael J. Wysocki }
3773c2cc7ffSRafael J. Wysocki 
3781e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src)
379c27b2c33SRafael J. Wysocki {
380c27b2c33SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
3813c2cc7ffSRafael J. Wysocki 	int error = -ENODEV;
382c27b2c33SRafael J. Wysocki 
383c27b2c33SRafael J. Wysocki 	lock_device_hotplug();
384c27b2c33SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
385c27b2c33SRafael J. Wysocki 
386c27b2c33SRafael J. Wysocki 	/*
387c27b2c33SRafael J. Wysocki 	 * The device object's ACPI handle cannot become invalid as long as we
3883c2cc7ffSRafael J. Wysocki 	 * are holding acpi_scan_lock, but it might have become invalid before
389c27b2c33SRafael J. Wysocki 	 * that lock was acquired.
390c27b2c33SRafael J. Wysocki 	 */
391c27b2c33SRafael J. Wysocki 	if (adev->handle == INVALID_ACPI_HANDLE)
3923c2cc7ffSRafael J. Wysocki 		goto err_out;
393c27b2c33SRafael J. Wysocki 
3941e2380cdSRafael J. Wysocki 	if (adev->flags.is_dock_station) {
3951e2380cdSRafael J. Wysocki 		error = dock_notify(adev, src);
3961e2380cdSRafael J. Wysocki 	} else if (adev->flags.hotplug_notify) {
3973c2cc7ffSRafael J. Wysocki 		error = acpi_generic_hotplug_event(adev, src);
3983c2cc7ffSRafael J. Wysocki 	} else {
399be27b3dcSRafael J. Wysocki 		int (*notify)(struct acpi_device *, u32);
4003c2cc7ffSRafael J. Wysocki 
4013c2cc7ffSRafael J. Wysocki 		acpi_lock_hp_context();
402be27b3dcSRafael J. Wysocki 		notify = adev->hp ? adev->hp->notify : NULL;
4033c2cc7ffSRafael J. Wysocki 		acpi_unlock_hp_context();
4043c2cc7ffSRafael J. Wysocki 		/*
4053c2cc7ffSRafael J. Wysocki 		 * There may be additional notify handlers for device objects
4063c2cc7ffSRafael J. Wysocki 		 * without the .event() callback, so ignore them here.
4073c2cc7ffSRafael J. Wysocki 		 */
408be27b3dcSRafael J. Wysocki 		if (notify)
409be27b3dcSRafael J. Wysocki 			error = notify(adev, src);
4103c2cc7ffSRafael J. Wysocki 		else
4113c2cc7ffSRafael J. Wysocki 			goto out;
412c27b2c33SRafael J. Wysocki 	}
413d429e5c1SLee, Chun-Yi 	switch (error) {
414d429e5c1SLee, Chun-Yi 	case 0:
415a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_SUCCESS;
416d429e5c1SLee, Chun-Yi 		break;
417d429e5c1SLee, Chun-Yi 	case -EPERM:
418d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
419d429e5c1SLee, Chun-Yi 		break;
420d429e5c1SLee, Chun-Yi 	case -EBUSY:
421d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_DEVICE_BUSY;
422d429e5c1SLee, Chun-Yi 		break;
423d429e5c1SLee, Chun-Yi 	default:
424d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
425d429e5c1SLee, Chun-Yi 		break;
426d429e5c1SLee, Chun-Yi 	}
427a33ec399SRafael J. Wysocki 
4283c2cc7ffSRafael J. Wysocki  err_out:
429700b8422SRafael J. Wysocki 	acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
4303c2cc7ffSRafael J. Wysocki 
4313c2cc7ffSRafael J. Wysocki  out:
432*45e9aa1fSRafael J. Wysocki 	acpi_put_acpi_dev(adev);
433a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
434e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
435a33ec399SRafael J. Wysocki }
436a33ec399SRafael J. Wysocki 
4370b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
4380b224527SRafael J. Wysocki {
4390b224527SRafael J. Wysocki 	int i;
4400b224527SRafael J. Wysocki 
441993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
442993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
443993cbe59SRafael J. Wysocki 
4441b1f3e16SRafael J. Wysocki 	if (!device->power.flags.power_resources)
4450b224527SRafael J. Wysocki 		return;
4460b224527SRafael J. Wysocki 
4470b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
4480b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
4490b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
4500b224527SRafael J. Wysocki 	}
4517f47fa6cSBjorn Helgaas }
4527f47fa6cSBjorn Helgaas 
4531890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
4541890a97aSPatrick Mochel {
4551890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4561890a97aSPatrick Mochel 
457ffdcd955SMika Westerberg 	acpi_free_properties(acpi_dev);
458c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
4590b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
4601890a97aSPatrick Mochel 	kfree(acpi_dev);
4611890a97aSPatrick Mochel }
4621890a97aSPatrick Mochel 
463d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device)
464caf5c03fSRafael J. Wysocki {
465ca9dc8d4SLukas Wunner 	struct acpi_device_bus_id *acpi_device_bus_id;
466ca9dc8d4SLukas Wunner 
467d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_lock);
468d783156eSRafael J. Wysocki 
469ca9dc8d4SLukas Wunner 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
470ca9dc8d4SLukas Wunner 		if (!strcmp(acpi_device_bus_id->bus_id,
471ca9dc8d4SLukas Wunner 			    acpi_device_hid(device))) {
472ad2f3b08SRafael J. Wysocki 			ida_free(&acpi_device_bus_id->instance_ida,
473ad2f3b08SRafael J. Wysocki 				 device->pnp.instance_no);
474eb50aaf9SAndy Shevchenko 			if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
475ca9dc8d4SLukas Wunner 				list_del(&acpi_device_bus_id->node);
476a58015d6SDexuan Cui 				kfree_const(acpi_device_bus_id->bus_id);
477ca9dc8d4SLukas Wunner 				kfree(acpi_device_bus_id);
478ca9dc8d4SLukas Wunner 			}
479ca9dc8d4SLukas Wunner 			break;
480ca9dc8d4SLukas Wunner 		}
481ca9dc8d4SLukas Wunner 
482d783156eSRafael J. Wysocki 	list_del(&device->wakeup_list);
483e6bdbcc7SRafael J. Wysocki 
484d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_lock);
485d783156eSRafael J. Wysocki 
486d783156eSRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
487d783156eSRafael J. Wysocki 	acpi_device_remove_files(device);
488d783156eSRafael J. Wysocki 	if (device->remove)
489d783156eSRafael J. Wysocki 		device->remove(device);
490d783156eSRafael J. Wysocki 
491d783156eSRafael J. Wysocki 	device_del(&device->dev);
492d783156eSRafael J. Wysocki }
493d783156eSRafael J. Wysocki 
49468bdb677SOctavian Purdila static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
49568bdb677SOctavian Purdila 
496d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list);
497d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock);
498d783156eSRafael J. Wysocki 
499d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used)
500d783156eSRafael J. Wysocki {
501d783156eSRafael J. Wysocki 	for (;;) {
502d783156eSRafael J. Wysocki 		struct acpi_device *adev;
503d783156eSRafael J. Wysocki 
504d783156eSRafael J. Wysocki 		mutex_lock(&acpi_device_del_lock);
505d783156eSRafael J. Wysocki 
506d783156eSRafael J. Wysocki 		if (list_empty(&acpi_device_del_list)) {
507d783156eSRafael J. Wysocki 			mutex_unlock(&acpi_device_del_lock);
508d783156eSRafael J. Wysocki 			break;
509d783156eSRafael J. Wysocki 		}
510d783156eSRafael J. Wysocki 		adev = list_first_entry(&acpi_device_del_list,
511d783156eSRafael J. Wysocki 					struct acpi_device, del_list);
512d783156eSRafael J. Wysocki 		list_del(&adev->del_list);
513d783156eSRafael J. Wysocki 
514d783156eSRafael J. Wysocki 		mutex_unlock(&acpi_device_del_lock);
515d783156eSRafael J. Wysocki 
51668bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
51768bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_REMOVE, adev);
51868bdb677SOctavian Purdila 
519d783156eSRafael J. Wysocki 		acpi_device_del(adev);
520d783156eSRafael J. Wysocki 		/*
521d783156eSRafael J. Wysocki 		 * Drop references to all power resources that might have been
522d783156eSRafael J. Wysocki 		 * used by the device.
523d783156eSRafael J. Wysocki 		 */
524d783156eSRafael J. Wysocki 		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
5254cbaba4eSAndy Shevchenko 		acpi_dev_put(adev);
526d783156eSRafael J. Wysocki 	}
527d783156eSRafael J. Wysocki }
528d783156eSRafael J. Wysocki 
529d783156eSRafael J. Wysocki /**
530d783156eSRafael J. Wysocki  * acpi_scan_drop_device - Drop an ACPI device object.
531d783156eSRafael J. Wysocki  * @handle: Handle of an ACPI namespace node, not used.
532d783156eSRafael J. Wysocki  * @context: Address of the ACPI device object to drop.
533d783156eSRafael J. Wysocki  *
534d783156eSRafael J. Wysocki  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
535d783156eSRafael J. Wysocki  * namespace node the device object pointed to by @context is attached to.
536d783156eSRafael J. Wysocki  *
537d783156eSRafael J. Wysocki  * The unregistration is carried out asynchronously to avoid running
538d783156eSRafael J. Wysocki  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
539d783156eSRafael J. Wysocki  * ensure the correct ordering (the device objects must be unregistered in the
540d783156eSRafael J. Wysocki  * same order in which the corresponding namespace nodes are deleted).
541d783156eSRafael J. Wysocki  */
542d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context)
543d783156eSRafael J. Wysocki {
544d783156eSRafael J. Wysocki 	static DECLARE_WORK(work, acpi_device_del_work_fn);
545d783156eSRafael J. Wysocki 	struct acpi_device *adev = context;
546d783156eSRafael J. Wysocki 
547d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_del_lock);
548d783156eSRafael J. Wysocki 
549d783156eSRafael J. Wysocki 	/*
550d783156eSRafael J. Wysocki 	 * Use the ACPI hotplug workqueue which is ordered, so this work item
551d783156eSRafael J. Wysocki 	 * won't run after any hotplug work items submitted subsequently.  That
552d783156eSRafael J. Wysocki 	 * prevents attempts to register device objects identical to those being
553d783156eSRafael J. Wysocki 	 * deleted from happening concurrently (such attempts result from
554d783156eSRafael J. Wysocki 	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
555935ab850STom Saeger 	 * run after all of the work items submitted previously, which helps
556d783156eSRafael J. Wysocki 	 * those work items to ensure that they are not accessing stale device
557d783156eSRafael J. Wysocki 	 * objects.
558d783156eSRafael J. Wysocki 	 */
559d783156eSRafael J. Wysocki 	if (list_empty(&acpi_device_del_list))
560d783156eSRafael J. Wysocki 		acpi_queue_hotplug_work(&work);
561d783156eSRafael J. Wysocki 
562d783156eSRafael J. Wysocki 	list_add_tail(&adev->del_list, &acpi_device_del_list);
563d783156eSRafael J. Wysocki 	/* Make acpi_ns_validate_handle() return NULL for this handle. */
564d783156eSRafael J. Wysocki 	adev->handle = INVALID_ACPI_HANDLE;
565d783156eSRafael J. Wysocki 
566d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_del_lock);
567caf5c03fSRafael J. Wysocki }
568caf5c03fSRafael J. Wysocki 
56983e2c8fcSRafael J. Wysocki static struct acpi_device *handle_to_device(acpi_handle handle,
57078ea4639SRafael J. Wysocki 					    void (*callback)(void *))
571caf5c03fSRafael J. Wysocki {
57283e2c8fcSRafael J. Wysocki 	struct acpi_device *adev = NULL;
573caf5c03fSRafael J. Wysocki 	acpi_status status;
574caf5c03fSRafael J. Wysocki 
57578ea4639SRafael J. Wysocki 	status = acpi_get_data_full(handle, acpi_scan_drop_device,
57683e2c8fcSRafael J. Wysocki 				    (void **)&adev, callback);
57783e2c8fcSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !adev) {
57883e2c8fcSRafael J. Wysocki 		acpi_handle_debug(handle, "No context!\n");
57983e2c8fcSRafael J. Wysocki 		return NULL;
580caf5c03fSRafael J. Wysocki 	}
58183e2c8fcSRafael J. Wysocki 	return adev;
582caf5c03fSRafael J. Wysocki }
58378ea4639SRafael J. Wysocki 
584e3c963c4SRafael J. Wysocki /**
585e3c963c4SRafael J. Wysocki  * acpi_fetch_acpi_dev - Retrieve ACPI device object.
586e3c963c4SRafael J. Wysocki  * @handle: ACPI handle associated with the requested ACPI device object.
587e3c963c4SRafael J. Wysocki  *
588e3c963c4SRafael J. Wysocki  * Return a pointer to the ACPI device object associated with @handle, if
589e3c963c4SRafael J. Wysocki  * present, or NULL otherwise.
590e3c963c4SRafael J. Wysocki  */
591e3c963c4SRafael J. Wysocki struct acpi_device *acpi_fetch_acpi_dev(acpi_handle handle)
592e3c963c4SRafael J. Wysocki {
593e3c963c4SRafael J. Wysocki 	return handle_to_device(handle, NULL);
594e3c963c4SRafael J. Wysocki }
595e3c963c4SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_fetch_acpi_dev);
596e3c963c4SRafael J. Wysocki 
59778ea4639SRafael J. Wysocki static void get_acpi_device(void *dev)
59878ea4639SRafael J. Wysocki {
5994cbaba4eSAndy Shevchenko 	acpi_dev_get(dev);
60078ea4639SRafael J. Wysocki }
60178ea4639SRafael J. Wysocki 
602*45e9aa1fSRafael J. Wysocki /**
603*45e9aa1fSRafael J. Wysocki  * acpi_get_acpi_dev - Retrieve ACPI device object and reference count it.
604*45e9aa1fSRafael J. Wysocki  * @handle: ACPI handle associated with the requested ACPI device object.
605*45e9aa1fSRafael J. Wysocki  *
606*45e9aa1fSRafael J. Wysocki  * Return a pointer to the ACPI device object associated with @handle and bump
607*45e9aa1fSRafael J. Wysocki  * up that object's reference counter (under the ACPI Namespace lock), if
608*45e9aa1fSRafael J. Wysocki  * present, or return NULL otherwise.
609*45e9aa1fSRafael J. Wysocki  *
610*45e9aa1fSRafael J. Wysocki  * The ACPI device object reference acquired by this function needs to be
611*45e9aa1fSRafael J. Wysocki  * dropped via acpi_dev_put().
612*45e9aa1fSRafael J. Wysocki  */
613*45e9aa1fSRafael J. Wysocki struct acpi_device *acpi_get_acpi_dev(acpi_handle handle)
61478ea4639SRafael J. Wysocki {
61583e2c8fcSRafael J. Wysocki 	return handle_to_device(handle, get_acpi_device);
61678ea4639SRafael J. Wysocki }
617*45e9aa1fSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_get_acpi_dev);
61878ea4639SRafael J. Wysocki 
619c1013ff7SRafael J. Wysocki static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
620c1013ff7SRafael J. Wysocki {
621c1013ff7SRafael J. Wysocki 	struct acpi_device_bus_id *acpi_device_bus_id;
622c1013ff7SRafael J. Wysocki 
623c1013ff7SRafael J. Wysocki 	/* Find suitable bus_id and instance number in acpi_bus_id_list. */
624c1013ff7SRafael J. Wysocki 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
625c1013ff7SRafael J. Wysocki 		if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
626c1013ff7SRafael J. Wysocki 			return acpi_device_bus_id;
627c1013ff7SRafael J. Wysocki 	}
628c1013ff7SRafael J. Wysocki 	return NULL;
629c1013ff7SRafael J. Wysocki }
630c1013ff7SRafael J. Wysocki 
631eb50aaf9SAndy Shevchenko static int acpi_device_set_name(struct acpi_device *device,
632eb50aaf9SAndy Shevchenko 				struct acpi_device_bus_id *acpi_device_bus_id)
633eb50aaf9SAndy Shevchenko {
634eb50aaf9SAndy Shevchenko 	struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
635eb50aaf9SAndy Shevchenko 	int result;
636eb50aaf9SAndy Shevchenko 
637ad2f3b08SRafael J. Wysocki 	result = ida_alloc(instance_ida, GFP_KERNEL);
638eb50aaf9SAndy Shevchenko 	if (result < 0)
639eb50aaf9SAndy Shevchenko 		return result;
640eb50aaf9SAndy Shevchenko 
641eb50aaf9SAndy Shevchenko 	device->pnp.instance_no = result;
642eb50aaf9SAndy Shevchenko 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
643eb50aaf9SAndy Shevchenko 	return 0;
644eb50aaf9SAndy Shevchenko }
645eb50aaf9SAndy Shevchenko 
646c6a493a1SRafael J. Wysocki static int acpi_tie_acpi_dev(struct acpi_device *adev)
647c6a493a1SRafael J. Wysocki {
648c6a493a1SRafael J. Wysocki 	acpi_handle handle = adev->handle;
649c6a493a1SRafael J. Wysocki 	acpi_status status;
650c6a493a1SRafael J. Wysocki 
651c6a493a1SRafael J. Wysocki 	if (!handle)
652c6a493a1SRafael J. Wysocki 		return 0;
653c6a493a1SRafael J. Wysocki 
654c6a493a1SRafael J. Wysocki 	status = acpi_attach_data(handle, acpi_scan_drop_device, adev);
655c6a493a1SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
656c6a493a1SRafael J. Wysocki 		acpi_handle_err(handle, "Unable to attach device data\n");
657c6a493a1SRafael J. Wysocki 		return -ENODEV;
658c6a493a1SRafael J. Wysocki 	}
659c6a493a1SRafael J. Wysocki 
660c6a493a1SRafael J. Wysocki 	return 0;
661c6a493a1SRafael J. Wysocki }
662c6a493a1SRafael J. Wysocki 
663882c982dSHeikki Krogerus static void acpi_store_pld_crc(struct acpi_device *adev)
664882c982dSHeikki Krogerus {
665882c982dSHeikki Krogerus 	struct acpi_pld_info *pld;
666882c982dSHeikki Krogerus 	acpi_status status;
667882c982dSHeikki Krogerus 
668882c982dSHeikki Krogerus 	status = acpi_get_physical_device_location(adev->handle, &pld);
669882c982dSHeikki Krogerus 	if (ACPI_FAILURE(status))
670882c982dSHeikki Krogerus 		return;
671882c982dSHeikki Krogerus 
672882c982dSHeikki Krogerus 	adev->pld_crc = crc32(~0, pld, sizeof(*pld));
673882c982dSHeikki Krogerus 	ACPI_FREE(pld);
674882c982dSHeikki Krogerus }
675882c982dSHeikki Krogerus 
6765f4ce260SRafael J. Wysocki static int __acpi_device_add(struct acpi_device *device,
67782c7d5efSRafael J. Wysocki 			     void (*release)(struct device *))
6781da177e4SLinus Torvalds {
679c1013ff7SRafael J. Wysocki 	struct acpi_device_bus_id *acpi_device_bus_id;
680e49bd2ddSZhang Rui 	int result;
68166b7ed40SBjorn Helgaas 
6821da177e4SLinus Torvalds 	/*
6831da177e4SLinus Torvalds 	 * Linkage
6841da177e4SLinus Torvalds 	 * -------
6851da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
6861da177e4SLinus Torvalds 	 */
6871da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
6881033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
689d783156eSRafael J. Wysocki 	INIT_LIST_HEAD(&device->del_list);
6901033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
6911da177e4SLinus Torvalds 
6929090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
693c1013ff7SRafael J. Wysocki 
694c1013ff7SRafael J. Wysocki 	acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
695c1013ff7SRafael J. Wysocki 	if (acpi_device_bus_id) {
696eb50aaf9SAndy Shevchenko 		result = acpi_device_set_name(device, acpi_device_bus_id);
697eb50aaf9SAndy Shevchenko 		if (result)
698eb50aaf9SAndy Shevchenko 			goto err_unlock;
699c1013ff7SRafael J. Wysocki 	} else {
700c1013ff7SRafael J. Wysocki 		acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
701c1013ff7SRafael J. Wysocki 					     GFP_KERNEL);
702c1013ff7SRafael J. Wysocki 		if (!acpi_device_bus_id) {
703c1013ff7SRafael J. Wysocki 			result = -ENOMEM;
704c1013ff7SRafael J. Wysocki 			goto err_unlock;
705e49bd2ddSZhang Rui 		}
706a58015d6SDexuan Cui 		acpi_device_bus_id->bus_id =
707a58015d6SDexuan Cui 			kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
708a58015d6SDexuan Cui 		if (!acpi_device_bus_id->bus_id) {
709c1013ff7SRafael J. Wysocki 			kfree(acpi_device_bus_id);
710a58015d6SDexuan Cui 			result = -ENOMEM;
711c1013ff7SRafael J. Wysocki 			goto err_unlock;
712a58015d6SDexuan Cui 		}
713a58015d6SDexuan Cui 
714eb50aaf9SAndy Shevchenko 		ida_init(&acpi_device_bus_id->instance_ida);
715eb50aaf9SAndy Shevchenko 
716eb50aaf9SAndy Shevchenko 		result = acpi_device_set_name(device, acpi_device_bus_id);
717eb50aaf9SAndy Shevchenko 		if (result) {
7180c8bd174SChristophe JAILLET 			kfree_const(acpi_device_bus_id->bus_id);
719eb50aaf9SAndy Shevchenko 			kfree(acpi_device_bus_id);
720eb50aaf9SAndy Shevchenko 			goto err_unlock;
721eb50aaf9SAndy Shevchenko 		}
722eb50aaf9SAndy Shevchenko 
723e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
724e49bd2ddSZhang Rui 	}
725e49bd2ddSZhang Rui 
7261da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
7271da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
7285e73c518SRafael J. Wysocki 
729882c982dSHeikki Krogerus 	acpi_store_pld_crc(device);
730882c982dSHeikki Krogerus 
7319090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
7321da177e4SLinus Torvalds 
7331890a97aSPatrick Mochel 	if (device->parent)
73466b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
7355e73c518SRafael J. Wysocki 
7361890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
73782c7d5efSRafael J. Wysocki 	device->dev.release = release;
738cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
739e49bd2ddSZhang Rui 	if (result) {
7408b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
741d43e167dSRafael J. Wysocki 		goto err;
742e49bd2ddSZhang Rui 	}
743f883d9dbSPatrick Mochel 
744e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
745e49bd2ddSZhang Rui 	if (result)
7468acf4108SHanjun Guo 		pr_err("Error creating sysfs interface for device %s\n",
7470794469dSKay Sievers 		       dev_name(&device->dev));
748e49bd2ddSZhang Rui 
749e49bd2ddSZhang Rui 	return 0;
750d43e167dSRafael J. Wysocki 
751d43e167dSRafael J. Wysocki err:
7529090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
7535e73c518SRafael J. Wysocki 
754e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
755a58015d6SDexuan Cui 
756c1013ff7SRafael J. Wysocki err_unlock:
7579090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
758d43e167dSRafael J. Wysocki 
759d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
7605e73c518SRafael J. Wysocki 
761e49bd2ddSZhang Rui 	return result;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7645f4ce260SRafael J. Wysocki int acpi_device_add(struct acpi_device *adev, void (*release)(struct device *))
7655f4ce260SRafael J. Wysocki {
7665f4ce260SRafael J. Wysocki 	int ret;
7675f4ce260SRafael J. Wysocki 
7685f4ce260SRafael J. Wysocki 	ret = acpi_tie_acpi_dev(adev);
7695f4ce260SRafael J. Wysocki 	if (ret)
7705f4ce260SRafael J. Wysocki 		return ret;
7715f4ce260SRafael J. Wysocki 
7725f4ce260SRafael J. Wysocki 	return __acpi_device_add(adev, release);
7735f4ce260SRafael J. Wysocki }
7745f4ce260SRafael J. Wysocki 
7759e89dde2SZhang Rui /* --------------------------------------------------------------------------
7761da177e4SLinus Torvalds                                  Device Enumeration
7771da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
7782ef33ee7SRafael J. Wysocki static bool acpi_info_matches_ids(struct acpi_device_info *info,
7792ef33ee7SRafael J. Wysocki 				  const char * const ids[])
780637b9f1aSHans de Goede {
7812ef33ee7SRafael J. Wysocki 	struct acpi_pnp_device_id_list *cid_list = NULL;
78202050558SAndy Shevchenko 	int i, index;
783637b9f1aSHans de Goede 
784637b9f1aSHans de Goede 	if (!(info->valid & ACPI_VALID_HID))
785637b9f1aSHans de Goede 		return false;
786637b9f1aSHans de Goede 
78702050558SAndy Shevchenko 	index = match_string(ids, -1, info->hardware_id.string);
78802050558SAndy Shevchenko 	if (index >= 0)
78902050558SAndy Shevchenko 		return true;
79002050558SAndy Shevchenko 
7912ef33ee7SRafael J. Wysocki 	if (info->valid & ACPI_VALID_CID)
7922ef33ee7SRafael J. Wysocki 		cid_list = &info->compatible_id_list;
7932ef33ee7SRafael J. Wysocki 
7942ef33ee7SRafael J. Wysocki 	if (!cid_list)
79502050558SAndy Shevchenko 		return false;
7962ef33ee7SRafael J. Wysocki 
79702050558SAndy Shevchenko 	for (i = 0; i < cid_list->count; i++) {
79802050558SAndy Shevchenko 		index = match_string(ids, -1, cid_list->ids[i].string);
79902050558SAndy Shevchenko 		if (index >= 0)
8002ef33ee7SRafael J. Wysocki 			return true;
8012ef33ee7SRafael J. Wysocki 	}
802637b9f1aSHans de Goede 
803637b9f1aSHans de Goede 	return false;
804637b9f1aSHans de Goede }
805637b9f1aSHans de Goede 
806637b9f1aSHans de Goede /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
8072ef33ee7SRafael J. Wysocki static const char * const acpi_ignore_dep_ids[] = {
8082ef33ee7SRafael J. Wysocki 	"PNP0D80", /* Windows-compatible System Power Management Controller */
8099272e97aSHans de Goede 	"INT33BD", /* Intel Baytrail Mailbox Device */
810637b9f1aSHans de Goede 	NULL
811637b9f1aSHans de Goede };
812637b9f1aSHans de Goede 
8139d9bcae4SHans de Goede /* List of HIDs for which we honor deps of matching ACPI devs, when checking _DEP lists. */
8149d9bcae4SHans de Goede static const char * const acpi_honor_dep_ids[] = {
8159d9bcae4SHans de Goede 	"INT3472", /* Camera sensor PMIC / clk and regulator info */
8169d9bcae4SHans de Goede 	NULL
8179d9bcae4SHans de Goede };
8189d9bcae4SHans de Goede 
8195c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
8205c478f49SBjorn Helgaas {
821e3c963c4SRafael J. Wysocki 	struct acpi_device *device;
8225c478f49SBjorn Helgaas 	acpi_status status;
8235c478f49SBjorn Helgaas 
8245c478f49SBjorn Helgaas 	/*
8255c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
8265c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
8275c478f49SBjorn Helgaas 	 * to deal with them specially.
8285c478f49SBjorn Helgaas 	 */
829456de893SRafael J. Wysocki 	if (!handle)
8305c478f49SBjorn Helgaas 		return acpi_root;
8315c478f49SBjorn Helgaas 
8325c478f49SBjorn Helgaas 	do {
8335c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
8345c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
835456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
836e3c963c4SRafael J. Wysocki 
837e3c963c4SRafael J. Wysocki 		device = acpi_fetch_acpi_dev(handle);
838e3c963c4SRafael J. Wysocki 	} while (!device);
8395c478f49SBjorn Helgaas 	return device;
8405c478f49SBjorn Helgaas }
8415c478f49SBjorn Helgaas 
842c8f7a62cSLen Brown acpi_status
843c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
844c8f7a62cSLen Brown {
845c8f7a62cSLen Brown 	acpi_status status;
846c8f7a62cSLen Brown 	acpi_handle tmp;
847c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
848c8f7a62cSLen Brown 	union acpi_object *obj;
849c8f7a62cSLen Brown 
850c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
851c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
852c8f7a62cSLen Brown 		return status;
853c8f7a62cSLen Brown 
854c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
855c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
856c8f7a62cSLen Brown 		obj = buffer.pointer;
8573b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
8583b5fee59SHolger Macht 					 ejd);
859c8f7a62cSLen Brown 		kfree(buffer.pointer);
860c8f7a62cSLen Brown 	}
861c8f7a62cSLen Brown 	return status;
862c8f7a62cSLen Brown }
863c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
864c8f7a62cSLen Brown 
86540381a3cSBjorn Helgaas static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
8661da177e4SLinus Torvalds {
86740381a3cSBjorn Helgaas 	acpi_handle handle = dev->handle;
86840381a3cSBjorn Helgaas 	struct acpi_device_wakeup *wakeup = &dev->wakeup;
869b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
870b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
8711da177e4SLinus Torvalds 	union acpi_object *element = NULL;
872b581a7f9SRafael J. Wysocki 	acpi_status status;
873e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
8741da177e4SLinus Torvalds 
875993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
8761da177e4SLinus Torvalds 
877b581a7f9SRafael J. Wysocki 	/* _PRW */
878b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
879b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
880e52d9d8cSRafael J. Wysocki 		acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
881e52d9d8cSRafael J. Wysocki 				 acpi_format_exception(status));
882e88c9c60SRafael J. Wysocki 		return err;
883b581a7f9SRafael J. Wysocki 	}
884b581a7f9SRafael J. Wysocki 
885b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
886b581a7f9SRafael J. Wysocki 
887e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
888b581a7f9SRafael J. Wysocki 		goto out;
889b581a7f9SRafael J. Wysocki 
8901da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
891e88c9c60SRafael J. Wysocki 	if (!element)
892b581a7f9SRafael J. Wysocki 		goto out;
893e88c9c60SRafael J. Wysocki 
8941da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
8951da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
8961da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
8971da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
898e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
899b581a7f9SRafael J. Wysocki 			goto out;
900e88c9c60SRafael J. Wysocki 
901b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
9021da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
903b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
9041da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
9051da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
906b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
907b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
908b581a7f9SRafael J. Wysocki 	} else {
909b581a7f9SRafael J. Wysocki 		goto out;
910b581a7f9SRafael J. Wysocki 	}
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
913e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
914b581a7f9SRafael J. Wysocki 		goto out;
915e88c9c60SRafael J. Wysocki 
916b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
9171da177e4SLinus Torvalds 
918e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
919e88c9c60SRafael J. Wysocki 	if (err)
920b581a7f9SRafael J. Wysocki 		goto out;
9211da177e4SLinus Torvalds 
9220596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
9230596a52bSRafael J. Wysocki 		int sleep_state;
9249874647bSRafael J. Wysocki 
925b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
926b5d667ebSRafael J. Wysocki 						  &sleep_state);
927b5d667ebSRafael J. Wysocki 		if (err) {
928b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
929b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
930b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
931b5d667ebSRafael J. Wysocki 			goto out;
932b5d667ebSRafael J. Wysocki 		}
9330596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
9340596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
9350596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
9360596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
9370596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
9380596a52bSRafael J. Wysocki 		}
9390596a52bSRafael J. Wysocki 	}
940b581a7f9SRafael J. Wysocki 
941b581a7f9SRafael J. Wysocki  out:
942b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
943e88c9c60SRafael J. Wysocki 	return err;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
946a1a66393SRafael J. Wysocki static bool acpi_wakeup_gpe_init(struct acpi_device *device)
9471da177e4SLinus Torvalds {
9480519ade7SMathias Krause 	static const struct acpi_device_id button_device_ids[] = {
9495ceb5f05SBjorn Helgaas 		{"PNP0C0C", 0},		/* Power button */
9505ceb5f05SBjorn Helgaas 		{"PNP0C0D", 0},		/* Lid */
9515ceb5f05SBjorn Helgaas 		{"PNP0C0E", 0},		/* Sleep button */
95229b71a1cSThomas Renninger 		{"", 0},
95329b71a1cSThomas Renninger 	};
954bd9b2f9aSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &device->wakeup;
955f517709dSRafael J. Wysocki 	acpi_status status;
956f517709dSRafael J. Wysocki 
957bd9b2f9aSRafael J. Wysocki 	wakeup->flags.notifier_present = 0;
958f517709dSRafael J. Wysocki 
959f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
960f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
961b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
962b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
963bd9b2f9aSRafael J. Wysocki 			if (wakeup->sleep_state == ACPI_STATE_S5)
964bd9b2f9aSRafael J. Wysocki 				wakeup->sleep_state = ACPI_STATE_S4;
965b7e38304SZhang Rui 		}
966bd9b2f9aSRafael J. Wysocki 		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
967f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
968a1a66393SRafael J. Wysocki 		return true;
969f517709dSRafael J. Wysocki 	}
970f517709dSRafael J. Wysocki 
971a1a66393SRafael J. Wysocki 	status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
972bd9b2f9aSRafael J. Wysocki 					 wakeup->gpe_number);
973a1a66393SRafael J. Wysocki 	return ACPI_SUCCESS(status);
974f517709dSRafael J. Wysocki }
975f517709dSRafael J. Wysocki 
976d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
977f517709dSRafael J. Wysocki {
978e88c9c60SRafael J. Wysocki 	int err;
97929b71a1cSThomas Renninger 
980d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
981952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
982d57d09a4SRafael J. Wysocki 		return;
983d57d09a4SRafael J. Wysocki 
98440381a3cSBjorn Helgaas 	err = acpi_bus_extract_wakeup_device_power_package(device);
985e88c9c60SRafael J. Wysocki 	if (err) {
986e52d9d8cSRafael J. Wysocki 		dev_err(&device->dev, "Unable to extract wakeup power resources");
987d57d09a4SRafael J. Wysocki 		return;
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds 
990a1a66393SRafael J. Wysocki 	device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
9919b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
992a1a66393SRafael J. Wysocki 	/*
993a1a66393SRafael J. Wysocki 	 * Call _PSW/_DSW object to disable its ability to wake the sleeping
994729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
995603fadf3SBjorn Helgaas 	 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
996729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
997729b2bdbSZhao Yakui 	 * present will the _PSW object used.
998729b2bdbSZhao Yakui 	 */
999e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
1000e88c9c60SRafael J. Wysocki 	if (err)
100105de0686STian Tao 		pr_debug("error in _DSW or _PSW evaluation\n");
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
1004f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
10051da177e4SLinus Torvalds {
1006f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
1007ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1008ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1009f33ce568SRafael J. Wysocki 	acpi_status status;
10109e89dde2SZhang Rui 
1011f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
1012f33ce568SRafael J. Wysocki 
1013ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
1014ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1015ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
1016ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
1017f33ce568SRafael J. Wysocki 
1018ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
1019ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
1020956ad9d9SRafael J. Wysocki 		    && package->package.count)
1021956ad9d9SRafael J. Wysocki 			acpi_extract_power_resources(package, 0, &ps->resources);
1022956ad9d9SRafael J. Wysocki 
1023ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
1024f33ce568SRafael J. Wysocki 	}
1025f33ce568SRafael J. Wysocki 
1026f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1027ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
1028952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
1029f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1030f33ce568SRafael J. Wysocki 
103120dacb71SRafael J. Wysocki 	/* State is valid if there are means to put the device into it. */
103220dacb71SRafael J. Wysocki 	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
1033f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1034f33ce568SRafael J. Wysocki 
1035f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1036f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1037f33ce568SRafael J. Wysocki }
1038f33ce568SRafael J. Wysocki 
1039d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
10401da177e4SLinus Torvalds {
1041b340c7d6SSakari Ailus 	unsigned long long dsc = ACPI_STATE_D0;
10428bc5053bSRafael J. Wysocki 	u32 i;
10431da177e4SLinus Torvalds 
1044d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1045952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
1046952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
1047d43e167dSRafael J. Wysocki 		return;
1048d43e167dSRafael J. Wysocki 
1049d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	/*
10529e89dde2SZhang Rui 	 * Power Management Flags
10531da177e4SLinus Torvalds 	 */
1054952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
10559e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
1056f25c0ae2SRafael J. Wysocki 
1057952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
10589e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
10591da177e4SLinus Torvalds 
1060f25c0ae2SRafael J. Wysocki 	if (acpi_has_method(device->handle, "_DSW"))
1061f25c0ae2SRafael J. Wysocki 		device->power.flags.dsw_present = 1;
1062f25c0ae2SRafael J. Wysocki 
1063b340c7d6SSakari Ailus 	acpi_evaluate_integer(device->handle, "_DSC", NULL, &dsc);
1064b340c7d6SSakari Ailus 	device->power.state_for_enumeration = dsc;
1065b340c7d6SSakari Ailus 
10661da177e4SLinus Torvalds 	/*
10679e89dde2SZhang Rui 	 * Enumerate supported power management states
10681da177e4SLinus Torvalds 	 */
1069f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1070f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
10711da177e4SLinus Torvalds 
10720b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
10739e89dde2SZhang Rui 
1074956ad9d9SRafael J. Wysocki 	/* Set the defaults for D0 and D3hot (always supported). */
10759e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
10769e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
107720dacb71SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
10781399dfcdSAaron Lu 
1079956ad9d9SRafael J. Wysocki 	/*
1080956ad9d9SRafael J. Wysocki 	 * Use power resources only if the D0 list of them is populated, because
1081956ad9d9SRafael J. Wysocki 	 * some platforms may provide _PR3 only to indicate D3cold support and
1082956ad9d9SRafael J. Wysocki 	 * in those cases the power resources list returned by it may be bogus.
1083956ad9d9SRafael J. Wysocki 	 */
1084956ad9d9SRafael J. Wysocki 	if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1085956ad9d9SRafael J. Wysocki 		device->power.flags.power_resources = 1;
1086956ad9d9SRafael J. Wysocki 		/*
1087956ad9d9SRafael J. Wysocki 		 * D3cold is supported if the D3hot list of power resources is
1088956ad9d9SRafael J. Wysocki 		 * not empty.
1089956ad9d9SRafael J. Wysocki 		 */
1090956ad9d9SRafael J. Wysocki 		if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1091956ad9d9SRafael J. Wysocki 			device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1092956ad9d9SRafael J. Wysocki 	}
1093956ad9d9SRafael J. Wysocki 
10941b1f3e16SRafael J. Wysocki 	if (acpi_bus_init_power(device))
1095b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1096b3785492SRafael J. Wysocki }
1097c8f7a62cSLen Brown 
1098d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
1101952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
11021da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
1105952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
11061da177e4SLinus Torvalds 		device->flags.removable = 1;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1109952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
1110952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
11111da177e4SLinus Torvalds 		device->flags.ejectable = 1;
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds 
1114c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
11151da177e4SLinus Torvalds {
11161da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
11171da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
11181da177e4SLinus Torvalds 	int i = 0;
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	/*
11211da177e4SLinus Torvalds 	 * Bus ID
11221da177e4SLinus Torvalds 	 * ------
11231da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
11241da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
11251da177e4SLinus Torvalds 	 */
1126859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
11271da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1128859ac9a4SBjorn Helgaas 		return;
1129859ac9a4SBjorn Helgaas 	}
1130859ac9a4SBjorn Helgaas 
1131859ac9a4SBjorn Helgaas 	switch (device->device_type) {
11321da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
11331da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
11341da177e4SLinus Torvalds 		break;
11351da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
11361da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
11371da177e4SLinus Torvalds 		break;
1138a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1139a64a62ceSLv Zheng 		strcpy(device->pnp.bus_id, "ECDT");
1140a64a62ceSLv Zheng 		break;
11411da177e4SLinus Torvalds 	default:
114266b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
11431da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
11441da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
11451da177e4SLinus Torvalds 			if (bus_id[i] == '_')
11461da177e4SLinus Torvalds 				bus_id[i] = '\0';
11471da177e4SLinus Torvalds 			else
11481da177e4SLinus Torvalds 				break;
11491da177e4SLinus Torvalds 		}
11501da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
11511da177e4SLinus Torvalds 		break;
11521da177e4SLinus Torvalds 	}
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
115554735266SZhang Rui /*
1156ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1157ebf4df8dSJiang Liu  *
1158ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1159ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1160ebf4df8dSJiang Liu  */
1161ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1162ebf4df8dSJiang Liu {
1163ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1164ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1165ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1166ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1167ebf4df8dSJiang Liu }
1168ebf4df8dSJiang Liu 
1169ebf4df8dSJiang Liu /*
1170d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
117154735266SZhang Rui  *
117254735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
117354735266SZhang Rui  * then we can safely call it an ejectable drive bay
117454735266SZhang Rui  */
1175ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1176d4e1a692SToshi Kani {
117754735266SZhang Rui 	acpi_handle phandle;
117854735266SZhang Rui 
1179952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1180ebf4df8dSJiang Liu 		return false;
1181ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1182ebf4df8dSJiang Liu 		return true;
1183ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1184ebf4df8dSJiang Liu 		return false;
118554735266SZhang Rui 
1186ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
118754735266SZhang Rui }
118854735266SZhang Rui 
1189b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev)
11901e2380cdSRafael J. Wysocki {
1191b43109faSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
11921e2380cdSRafael J. Wysocki 
1193b43109faSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list)
1194b43109faSRafael J. Wysocki 		if (!strcmp("PNP0C0A", hwid->id))
1195b43109faSRafael J. Wysocki 			return true;
1196b43109faSRafael J. Wysocki 
11971e2380cdSRafael J. Wysocki 	return false;
11981e2380cdSRafael J. Wysocki }
11991e2380cdSRafael J. Wysocki 
1200b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev)
12011e2380cdSRafael J. Wysocki {
1202b43109faSRafael J. Wysocki 	acpi_handle handle = adev->handle;
1203b43109faSRafael J. Wysocki 
1204b43109faSRafael J. Wysocki 	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
12051e2380cdSRafael J. Wysocki 		return true;
12061e2380cdSRafael J. Wysocki 
12071e2380cdSRafael J. Wysocki 	return acpi_bay_match(handle);
12081e2380cdSRafael J. Wysocki }
12091e2380cdSRafael J. Wysocki 
12103620f2f2SFrank Seidel /*
1211d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
12123620f2f2SFrank Seidel  */
1213ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
12143620f2f2SFrank Seidel {
1215ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
12163620f2f2SFrank Seidel }
12173620f2f2SFrank Seidel 
1218adc8bb8eSHans de Goede static acpi_status
1219adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1220adc8bb8eSHans de Goede 			  void **return_value)
1221adc8bb8eSHans de Goede {
1222adc8bb8eSHans de Goede 	long *cap = context;
1223adc8bb8eSHans de Goede 
1224adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_BCM") &&
1225adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_BCL")) {
1226e52d9d8cSRafael J. Wysocki 		acpi_handle_debug(handle, "Found generic backlight support\n");
1227adc8bb8eSHans de Goede 		*cap |= ACPI_VIDEO_BACKLIGHT;
1228adc8bb8eSHans de Goede 		/* We have backlight support, no need to scan further */
1229adc8bb8eSHans de Goede 		return AE_CTRL_TERMINATE;
1230adc8bb8eSHans de Goede 	}
1231adc8bb8eSHans de Goede 	return 0;
1232adc8bb8eSHans de Goede }
1233adc8bb8eSHans de Goede 
1234adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be
1235adc8bb8eSHans de Goede  * handled by video.ko.
1236adc8bb8eSHans de Goede  * The device will get a Linux specific CID added in scan.c to
1237adc8bb8eSHans de Goede  * identify the device as an ACPI graphics device
1238adc8bb8eSHans de Goede  * Be aware that the graphics device may not be physically present
1239adc8bb8eSHans de Goede  * Use acpi_video_get_capabilities() to detect general ACPI video
1240adc8bb8eSHans de Goede  * capabilities of present cards
1241adc8bb8eSHans de Goede  */
1242adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle)
1243adc8bb8eSHans de Goede {
1244adc8bb8eSHans de Goede 	long video_caps = 0;
1245adc8bb8eSHans de Goede 
1246adc8bb8eSHans de Goede 	/* Is this device able to support video switching ? */
1247adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1248adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1249adc8bb8eSHans de Goede 
1250adc8bb8eSHans de Goede 	/* Is this device able to retrieve a video ROM ? */
1251adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_ROM"))
1252adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1253adc8bb8eSHans de Goede 
1254adc8bb8eSHans de Goede 	/* Is this device able to configure which video head to be POSTed ? */
1255adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_VPO") &&
1256adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_GPD") &&
1257adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_SPD"))
1258adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1259adc8bb8eSHans de Goede 
1260adc8bb8eSHans de Goede 	/* Only check for backlight functionality if one of the above hit. */
1261adc8bb8eSHans de Goede 	if (video_caps)
1262adc8bb8eSHans de Goede 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1263adc8bb8eSHans de Goede 				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1264adc8bb8eSHans de Goede 				    &video_caps, NULL);
1265adc8bb8eSHans de Goede 
1266adc8bb8eSHans de Goede 	return video_caps;
1267adc8bb8eSHans de Goede }
1268adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device);
1269adc8bb8eSHans de Goede 
1270620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
127115b8dd53SBob Moore {
12727f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
127315b8dd53SBob Moore 
12742b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
12752b2ae7c7SThomas Renninger 		return dummy_hid;
12762b2ae7c7SThomas Renninger 
12777f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
12787f47fa6cSBjorn Helgaas 	return hid->id;
12797f47fa6cSBjorn Helgaas }
12807f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
128115b8dd53SBob Moore 
1282d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
12837f47fa6cSBjorn Helgaas {
12847f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
128515b8dd53SBob Moore 
12867f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
12877f47fa6cSBjorn Helgaas 	if (!id)
12887f47fa6cSBjorn Helgaas 		return;
128915b8dd53SBob Moore 
12906a0d12efSRasmus Villemoes 	id->id = kstrdup_const(dev_id, GFP_KERNEL);
12917f47fa6cSBjorn Helgaas 	if (!id->id) {
12927f47fa6cSBjorn Helgaas 		kfree(id);
12937f47fa6cSBjorn Helgaas 		return;
129415b8dd53SBob Moore 	}
129515b8dd53SBob Moore 
1296d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1297d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
129815b8dd53SBob Moore }
129915b8dd53SBob Moore 
1300222e82acSDarrick J. Wong /*
1301222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1302222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1303222e82acSDarrick J. Wong  * prefix.  Work around this.
1304222e82acSDarrick J. Wong  */
1305ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1306222e82acSDarrick J. Wong {
1307ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1308ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1309222e82acSDarrick J. Wong 
1310222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1311ebf4df8dSJiang Liu 		return false;
1312222e82acSDarrick J. Wong 
1313222e82acSDarrick J. Wong 	/* Look for SMBS object */
1314ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1315ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1316ebf4df8dSJiang Liu 		return false;
1317222e82acSDarrick J. Wong 
1318222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1319952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1320952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1321952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1322ebf4df8dSJiang Liu 		return true;
1323ebf4df8dSJiang Liu 
1324ebf4df8dSJiang Liu 	return false;
1325222e82acSDarrick J. Wong }
1326222e82acSDarrick J. Wong 
1327ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle)
1328ae3caa80SJiang Liu {
1329ae3caa80SJiang Liu 	acpi_handle tmp;
1330ae3caa80SJiang Liu 
1331ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1332ae3caa80SJiang Liu 	    tmp == handle)
1333ae3caa80SJiang Liu 		return true;
1334ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1335ae3caa80SJiang Liu 	    tmp == handle)
1336ae3caa80SJiang Liu 		return true;
1337ae3caa80SJiang Liu 
1338ae3caa80SJiang Liu 	return false;
1339ae3caa80SJiang Liu }
1340ae3caa80SJiang Liu 
1341c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1342c830dbcfSRafael J. Wysocki 			     int device_type)
13431da177e4SLinus Torvalds {
1344c830dbcfSRafael J. Wysocki 	struct acpi_device_info *info = NULL;
134578e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
13467f47fa6cSBjorn Helgaas 	int i;
13471da177e4SLinus Torvalds 
1348c0af4175SToshi Kani 	switch (device_type) {
13491da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1350c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1351c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1352859ac9a4SBjorn Helgaas 			break;
1353859ac9a4SBjorn Helgaas 		}
1354859ac9a4SBjorn Helgaas 
1355c830dbcfSRafael J. Wysocki 		acpi_get_object_info(handle, &info);
13566091b263SHans de Goede 		if (!info) {
13578acf4108SHanjun Guo 			pr_err("%s: Error reading device info\n", __func__);
13581da177e4SLinus Torvalds 			return;
13591da177e4SLinus Torvalds 		}
13601da177e4SLinus Torvalds 
1361549e6845SRafael J. Wysocki 		if (info->valid & ACPI_VALID_HID) {
1362c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
1363549e6845SRafael J. Wysocki 			pnp->type.platform_id = 1;
1364462ccc35SRafael J. Wysocki 		}
136557f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
136615b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
136757f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1368c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
136957f3674fSBjorn Helgaas 		}
13701da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1371c0af4175SToshi Kani 			pnp->bus_address = info->address;
1372c0af4175SToshi Kani 			pnp->type.bus_address = 1;
13731da177e4SLinus Torvalds 		}
1374ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1375c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1376ccf78040SLv Zheng 							GFP_KERNEL);
137726095a01SSuthikulpanit, Suravee 		if (info->valid & ACPI_VALID_CLS)
137826095a01SSuthikulpanit, Suravee 			acpi_add_id(pnp, info->class_code.string);
1379ae843332SZhang Rui 
1380c830dbcfSRafael J. Wysocki 		kfree(info);
1381c830dbcfSRafael J. Wysocki 
138257f3674fSBjorn Helgaas 		/*
138357f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
138457f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
13853620f2f2SFrank Seidel 		 */
1386c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1387c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1388ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1389c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1390ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1391c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1392ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1393c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1394ae3caa80SJiang Liu 		else if (list_empty(&pnp->ids) &&
1395ae3caa80SJiang Liu 			 acpi_object_is_system_bus(handle)) {
1396ae3caa80SJiang Liu 			/* \_SB, \_TZ, LNXSYBUS */
1397ae3caa80SJiang Liu 			acpi_add_id(pnp, ACPI_BUS_HID);
1398c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1399c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1400b7b30de5SBjorn Helgaas 		}
140154735266SZhang Rui 
14021da177e4SLinus Torvalds 		break;
14031da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1404c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
14051da177e4SLinus Torvalds 		break;
14061da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1407c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
14081da177e4SLinus Torvalds 		break;
14091da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1410c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
14111da177e4SLinus Torvalds 		break;
14121da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1413c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
14141da177e4SLinus Torvalds 		break;
14151da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1416c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
14171da177e4SLinus Torvalds 		break;
1418a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1419a64a62ceSLv Zheng 		acpi_add_id(pnp, ACPI_ECDT_HID);
1420a64a62ceSLv Zheng 		break;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds 
1424c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1425c0af4175SToshi Kani {
1426c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1427c0af4175SToshi Kani 
1428c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
14296a0d12efSRasmus Villemoes 		kfree_const(id->id);
1430c0af4175SToshi Kani 		kfree(id);
1431c0af4175SToshi Kani 	}
1432c0af4175SToshi Kani 	kfree(pnp->unique_id);
1433c0af4175SToshi Kani }
1434c0af4175SToshi Kani 
1435b84f196dSSuthikulpanit, Suravee /**
1436b84f196dSSuthikulpanit, Suravee  * acpi_dma_supported - Check DMA support for the specified device.
1437b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1438b84f196dSSuthikulpanit, Suravee  *
1439b84f196dSSuthikulpanit, Suravee  * Return false if DMA is not supported. Otherwise, return true
1440b84f196dSSuthikulpanit, Suravee  */
14413d7c821cSAndy Shevchenko bool acpi_dma_supported(const struct acpi_device *adev)
1442b84f196dSSuthikulpanit, Suravee {
1443b84f196dSSuthikulpanit, Suravee 	if (!adev)
1444b84f196dSSuthikulpanit, Suravee 		return false;
1445b84f196dSSuthikulpanit, Suravee 
1446b84f196dSSuthikulpanit, Suravee 	if (adev->flags.cca_seen)
1447b84f196dSSuthikulpanit, Suravee 		return true;
1448b84f196dSSuthikulpanit, Suravee 
1449b84f196dSSuthikulpanit, Suravee 	/*
1450b84f196dSSuthikulpanit, Suravee 	* Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1451b84f196dSSuthikulpanit, Suravee 	* DMA on "Intel platforms".  Presumably that includes all x86 and
1452b84f196dSSuthikulpanit, Suravee 	* ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1453b84f196dSSuthikulpanit, Suravee 	*/
1454b84f196dSSuthikulpanit, Suravee 	if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1455b84f196dSSuthikulpanit, Suravee 		return true;
1456b84f196dSSuthikulpanit, Suravee 
1457b84f196dSSuthikulpanit, Suravee 	return false;
1458b84f196dSSuthikulpanit, Suravee }
1459b84f196dSSuthikulpanit, Suravee 
1460b84f196dSSuthikulpanit, Suravee /**
1461b84f196dSSuthikulpanit, Suravee  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1462b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1463b84f196dSSuthikulpanit, Suravee  *
1464b84f196dSSuthikulpanit, Suravee  * Return enum dev_dma_attr.
1465b84f196dSSuthikulpanit, Suravee  */
1466b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1467b84f196dSSuthikulpanit, Suravee {
1468b84f196dSSuthikulpanit, Suravee 	if (!acpi_dma_supported(adev))
1469b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NOT_SUPPORTED;
1470b84f196dSSuthikulpanit, Suravee 
1471b84f196dSSuthikulpanit, Suravee 	if (adev->flags.coherent_dma)
1472b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_COHERENT;
1473b84f196dSSuthikulpanit, Suravee 	else
1474b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NON_COHERENT;
1475b84f196dSSuthikulpanit, Suravee }
1476b84f196dSSuthikulpanit, Suravee 
1477d760a1baSLorenzo Pieralisi /**
1478c04ac679SLorenzo Pieralisi  * acpi_dma_get_range() - Get device DMA parameters.
1479c04ac679SLorenzo Pieralisi  *
1480c04ac679SLorenzo Pieralisi  * @dev: device to configure
1481c04ac679SLorenzo Pieralisi  * @dma_addr: pointer device DMA address result
1482c04ac679SLorenzo Pieralisi  * @offset: pointer to the DMA offset result
1483c04ac679SLorenzo Pieralisi  * @size: pointer to DMA range size result
1484c04ac679SLorenzo Pieralisi  *
1485c04ac679SLorenzo Pieralisi  * Evaluate DMA regions and return respectively DMA region start, offset
1486c04ac679SLorenzo Pieralisi  * and size in dma_addr, offset and size on parsing success; it does not
1487c04ac679SLorenzo Pieralisi  * update the passed in values on failure.
1488c04ac679SLorenzo Pieralisi  *
1489c04ac679SLorenzo Pieralisi  * Return 0 on success, < 0 on failure.
1490c04ac679SLorenzo Pieralisi  */
1491c04ac679SLorenzo Pieralisi int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1492c04ac679SLorenzo Pieralisi 		       u64 *size)
1493c04ac679SLorenzo Pieralisi {
1494c04ac679SLorenzo Pieralisi 	struct acpi_device *adev;
1495c04ac679SLorenzo Pieralisi 	LIST_HEAD(list);
1496c04ac679SLorenzo Pieralisi 	struct resource_entry *rentry;
1497c04ac679SLorenzo Pieralisi 	int ret;
1498c04ac679SLorenzo Pieralisi 	struct device *dma_dev = dev;
1499c04ac679SLorenzo Pieralisi 	u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1500c04ac679SLorenzo Pieralisi 
1501c04ac679SLorenzo Pieralisi 	/*
1502c04ac679SLorenzo Pieralisi 	 * Walk the device tree chasing an ACPI companion with a _DMA
1503c04ac679SLorenzo Pieralisi 	 * object while we go. Stop if we find a device with an ACPI
1504c04ac679SLorenzo Pieralisi 	 * companion containing a _DMA method.
1505c04ac679SLorenzo Pieralisi 	 */
1506c04ac679SLorenzo Pieralisi 	do {
1507c04ac679SLorenzo Pieralisi 		adev = ACPI_COMPANION(dma_dev);
1508c04ac679SLorenzo Pieralisi 		if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1509c04ac679SLorenzo Pieralisi 			break;
1510c04ac679SLorenzo Pieralisi 
1511c04ac679SLorenzo Pieralisi 		dma_dev = dma_dev->parent;
1512c04ac679SLorenzo Pieralisi 	} while (dma_dev);
1513c04ac679SLorenzo Pieralisi 
1514c04ac679SLorenzo Pieralisi 	if (!dma_dev)
1515c04ac679SLorenzo Pieralisi 		return -ENODEV;
1516c04ac679SLorenzo Pieralisi 
1517c04ac679SLorenzo Pieralisi 	if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1518c04ac679SLorenzo Pieralisi 		acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1519c04ac679SLorenzo Pieralisi 		return -EINVAL;
1520c04ac679SLorenzo Pieralisi 	}
1521c04ac679SLorenzo Pieralisi 
1522c04ac679SLorenzo Pieralisi 	ret = acpi_dev_get_dma_resources(adev, &list);
1523c04ac679SLorenzo Pieralisi 	if (ret > 0) {
1524c04ac679SLorenzo Pieralisi 		list_for_each_entry(rentry, &list, node) {
1525c04ac679SLorenzo Pieralisi 			if (dma_offset && rentry->offset != dma_offset) {
1526c04ac679SLorenzo Pieralisi 				ret = -EINVAL;
1527c04ac679SLorenzo Pieralisi 				dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1528c04ac679SLorenzo Pieralisi 				goto out;
1529c04ac679SLorenzo Pieralisi 			}
1530c04ac679SLorenzo Pieralisi 			dma_offset = rentry->offset;
1531c04ac679SLorenzo Pieralisi 
1532c04ac679SLorenzo Pieralisi 			/* Take lower and upper limits */
1533c04ac679SLorenzo Pieralisi 			if (rentry->res->start < dma_start)
1534c04ac679SLorenzo Pieralisi 				dma_start = rentry->res->start;
1535c04ac679SLorenzo Pieralisi 			if (rentry->res->end > dma_end)
1536c04ac679SLorenzo Pieralisi 				dma_end = rentry->res->end;
1537c04ac679SLorenzo Pieralisi 		}
1538c04ac679SLorenzo Pieralisi 
1539c04ac679SLorenzo Pieralisi 		if (dma_start >= dma_end) {
1540c04ac679SLorenzo Pieralisi 			ret = -EINVAL;
1541c04ac679SLorenzo Pieralisi 			dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1542c04ac679SLorenzo Pieralisi 			goto out;
1543c04ac679SLorenzo Pieralisi 		}
1544c04ac679SLorenzo Pieralisi 
1545c04ac679SLorenzo Pieralisi 		*dma_addr = dma_start - dma_offset;
1546c04ac679SLorenzo Pieralisi 		len = dma_end - dma_start;
1547c04ac679SLorenzo Pieralisi 		*size = max(len, len + 1);
1548c04ac679SLorenzo Pieralisi 		*offset = dma_offset;
1549c04ac679SLorenzo Pieralisi 	}
1550c04ac679SLorenzo Pieralisi  out:
1551c04ac679SLorenzo Pieralisi 	acpi_dev_free_resource_list(&list);
1552c04ac679SLorenzo Pieralisi 
1553c04ac679SLorenzo Pieralisi 	return ret >= 0 ? 0 : ret;
1554c04ac679SLorenzo Pieralisi }
1555c04ac679SLorenzo Pieralisi 
155611a8c5e3SJean-Philippe Brucker #ifdef CONFIG_IOMMU_API
155711a8c5e3SJean-Philippe Brucker int acpi_iommu_fwspec_init(struct device *dev, u32 id,
155811a8c5e3SJean-Philippe Brucker 			   struct fwnode_handle *fwnode,
155911a8c5e3SJean-Philippe Brucker 			   const struct iommu_ops *ops)
156011a8c5e3SJean-Philippe Brucker {
156111a8c5e3SJean-Philippe Brucker 	int ret = iommu_fwspec_init(dev, fwnode, ops);
156211a8c5e3SJean-Philippe Brucker 
156311a8c5e3SJean-Philippe Brucker 	if (!ret)
156411a8c5e3SJean-Philippe Brucker 		ret = iommu_fwspec_add_ids(dev, &id, 1);
156511a8c5e3SJean-Philippe Brucker 
156611a8c5e3SJean-Philippe Brucker 	return ret;
156711a8c5e3SJean-Philippe Brucker }
156811a8c5e3SJean-Philippe Brucker 
156911a8c5e3SJean-Philippe Brucker static inline const struct iommu_ops *acpi_iommu_fwspec_ops(struct device *dev)
157011a8c5e3SJean-Philippe Brucker {
157111a8c5e3SJean-Philippe Brucker 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
157211a8c5e3SJean-Philippe Brucker 
157311a8c5e3SJean-Philippe Brucker 	return fwspec ? fwspec->ops : NULL;
157411a8c5e3SJean-Philippe Brucker }
157511a8c5e3SJean-Philippe Brucker 
157611a8c5e3SJean-Philippe Brucker static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
157711a8c5e3SJean-Philippe Brucker 						       const u32 *id_in)
157811a8c5e3SJean-Philippe Brucker {
157911a8c5e3SJean-Philippe Brucker 	int err;
158011a8c5e3SJean-Philippe Brucker 	const struct iommu_ops *ops;
158111a8c5e3SJean-Philippe Brucker 
158211a8c5e3SJean-Philippe Brucker 	/*
158311a8c5e3SJean-Philippe Brucker 	 * If we already translated the fwspec there is nothing left to do,
158411a8c5e3SJean-Philippe Brucker 	 * return the iommu_ops.
158511a8c5e3SJean-Philippe Brucker 	 */
158611a8c5e3SJean-Philippe Brucker 	ops = acpi_iommu_fwspec_ops(dev);
158711a8c5e3SJean-Philippe Brucker 	if (ops)
158811a8c5e3SJean-Philippe Brucker 		return ops;
158911a8c5e3SJean-Philippe Brucker 
159011a8c5e3SJean-Philippe Brucker 	err = iort_iommu_configure_id(dev, id_in);
15913cf48554SJean-Philippe Brucker 	if (err && err != -EPROBE_DEFER)
15923cf48554SJean-Philippe Brucker 		err = viot_iommu_configure(dev);
159311a8c5e3SJean-Philippe Brucker 
159411a8c5e3SJean-Philippe Brucker 	/*
159511a8c5e3SJean-Philippe Brucker 	 * If we have reason to believe the IOMMU driver missed the initial
159611a8c5e3SJean-Philippe Brucker 	 * iommu_probe_device() call for dev, replay it to get things in order.
159711a8c5e3SJean-Philippe Brucker 	 */
159811a8c5e3SJean-Philippe Brucker 	if (!err && dev->bus && !device_iommu_mapped(dev))
159911a8c5e3SJean-Philippe Brucker 		err = iommu_probe_device(dev);
160011a8c5e3SJean-Philippe Brucker 
160111a8c5e3SJean-Philippe Brucker 	/* Ignore all other errors apart from EPROBE_DEFER */
160211a8c5e3SJean-Philippe Brucker 	if (err == -EPROBE_DEFER) {
160311a8c5e3SJean-Philippe Brucker 		return ERR_PTR(err);
160411a8c5e3SJean-Philippe Brucker 	} else if (err) {
160511a8c5e3SJean-Philippe Brucker 		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
160611a8c5e3SJean-Philippe Brucker 		return NULL;
160711a8c5e3SJean-Philippe Brucker 	}
160811a8c5e3SJean-Philippe Brucker 	return acpi_iommu_fwspec_ops(dev);
160911a8c5e3SJean-Philippe Brucker }
161011a8c5e3SJean-Philippe Brucker 
161111a8c5e3SJean-Philippe Brucker #else /* !CONFIG_IOMMU_API */
161211a8c5e3SJean-Philippe Brucker 
161311a8c5e3SJean-Philippe Brucker int acpi_iommu_fwspec_init(struct device *dev, u32 id,
161411a8c5e3SJean-Philippe Brucker 			   struct fwnode_handle *fwnode,
161511a8c5e3SJean-Philippe Brucker 			   const struct iommu_ops *ops)
161611a8c5e3SJean-Philippe Brucker {
161711a8c5e3SJean-Philippe Brucker 	return -ENODEV;
161811a8c5e3SJean-Philippe Brucker }
161911a8c5e3SJean-Philippe Brucker 
162011a8c5e3SJean-Philippe Brucker static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
162111a8c5e3SJean-Philippe Brucker 						       const u32 *id_in)
162211a8c5e3SJean-Philippe Brucker {
162311a8c5e3SJean-Philippe Brucker 	return NULL;
162411a8c5e3SJean-Philippe Brucker }
162511a8c5e3SJean-Philippe Brucker 
162611a8c5e3SJean-Philippe Brucker #endif /* !CONFIG_IOMMU_API */
162711a8c5e3SJean-Philippe Brucker 
1628c04ac679SLorenzo Pieralisi /**
1629c1e97359SJohn Garry  * acpi_dma_configure_id - Set-up DMA configuration for the device.
1630d760a1baSLorenzo Pieralisi  * @dev: The pointer to the device
1631d760a1baSLorenzo Pieralisi  * @attr: device dma attributes
1632b8e069a2SLorenzo Pieralisi  * @input_id: input device id const value pointer
1633d760a1baSLorenzo Pieralisi  */
1634b8e069a2SLorenzo Pieralisi int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1635b8e069a2SLorenzo Pieralisi 			  const u32 *input_id)
1636d760a1baSLorenzo Pieralisi {
1637643b8e4dSLorenzo Pieralisi 	const struct iommu_ops *iommu;
16387ad42639SLorenzo Pieralisi 	u64 dma_addr = 0, size = 0;
1639643b8e4dSLorenzo Pieralisi 
1640e5361ca2SRobin Murphy 	if (attr == DEV_DMA_NOT_SUPPORTED) {
1641e5361ca2SRobin Murphy 		set_dma_ops(dev, &dma_dummy_ops);
1642e5361ca2SRobin Murphy 		return 0;
1643e5361ca2SRobin Murphy 	}
1644e5361ca2SRobin Murphy 
1645db59e1b6SJean-Philippe Brucker 	acpi_arch_dma_setup(dev, &dma_addr, &size);
1646d760a1baSLorenzo Pieralisi 
164711a8c5e3SJean-Philippe Brucker 	iommu = acpi_iommu_configure_id(dev, input_id);
164845586c70SMasahiro Yamada 	if (PTR_ERR(iommu) == -EPROBE_DEFER)
1649058f8c3fSSricharan R 		return -EPROBE_DEFER;
1650643b8e4dSLorenzo Pieralisi 
16517ad42639SLorenzo Pieralisi 	arch_setup_dma_ops(dev, dma_addr, size,
16527ad42639SLorenzo Pieralisi 				iommu, attr == DEV_DMA_COHERENT);
16535a1bb638SSricharan R 
16545a1bb638SSricharan R 	return 0;
1655d760a1baSLorenzo Pieralisi }
1656b8e069a2SLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1657d760a1baSLorenzo Pieralisi 
1658d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev)
1659d0562674SSuthikulpanit, Suravee {
1660d0562674SSuthikulpanit, Suravee 	unsigned long long cca = 0;
1661d0562674SSuthikulpanit, Suravee 	acpi_status status;
1662d0562674SSuthikulpanit, Suravee 	struct acpi_device *parent = adev->parent;
1663d0562674SSuthikulpanit, Suravee 
1664d0562674SSuthikulpanit, Suravee 	if (parent && parent->flags.cca_seen) {
1665d0562674SSuthikulpanit, Suravee 		/*
1666d0562674SSuthikulpanit, Suravee 		 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1667d0562674SSuthikulpanit, Suravee 		 * already saw one.
1668d0562674SSuthikulpanit, Suravee 		 */
1669d0562674SSuthikulpanit, Suravee 		adev->flags.cca_seen = 1;
1670d0562674SSuthikulpanit, Suravee 		cca = parent->flags.coherent_dma;
1671d0562674SSuthikulpanit, Suravee 	} else {
1672d0562674SSuthikulpanit, Suravee 		status = acpi_evaluate_integer(adev->handle, "_CCA",
1673d0562674SSuthikulpanit, Suravee 					       NULL, &cca);
1674d0562674SSuthikulpanit, Suravee 		if (ACPI_SUCCESS(status))
1675d0562674SSuthikulpanit, Suravee 			adev->flags.cca_seen = 1;
1676d0562674SSuthikulpanit, Suravee 		else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1677d0562674SSuthikulpanit, Suravee 			/*
1678d0562674SSuthikulpanit, Suravee 			 * If architecture does not specify that _CCA is
1679d0562674SSuthikulpanit, Suravee 			 * required for DMA-able devices (e.g. x86),
1680d0562674SSuthikulpanit, Suravee 			 * we default to _CCA=1.
1681d0562674SSuthikulpanit, Suravee 			 */
1682d0562674SSuthikulpanit, Suravee 			cca = 1;
1683d0562674SSuthikulpanit, Suravee 		else
1684d0562674SSuthikulpanit, Suravee 			acpi_handle_debug(adev->handle,
1685d0562674SSuthikulpanit, Suravee 					  "ACPI device is missing _CCA.\n");
1686d0562674SSuthikulpanit, Suravee 	}
1687d0562674SSuthikulpanit, Suravee 
1688d0562674SSuthikulpanit, Suravee 	adev->flags.coherent_dma = cca;
1689d0562674SSuthikulpanit, Suravee }
1690d0562674SSuthikulpanit, Suravee 
1691e361d1f8SFrédéric Danis static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1692e4330d8bSJarkko Nikula {
1693e361d1f8SFrédéric Danis 	bool *is_serial_bus_slave_p = data;
1694e4330d8bSJarkko Nikula 
1695e4330d8bSJarkko Nikula 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1696e4330d8bSJarkko Nikula 		return 1;
1697e4330d8bSJarkko Nikula 
1698e361d1f8SFrédéric Danis 	*is_serial_bus_slave_p = true;
1699e4330d8bSJarkko Nikula 
1700e4330d8bSJarkko Nikula 	 /* no need to do more checking */
1701e4330d8bSJarkko Nikula 	return -1;
1702e4330d8bSJarkko Nikula }
1703e4330d8bSJarkko Nikula 
1704dfda4492SJohn Garry static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1705dfda4492SJohn Garry {
1706dfda4492SJohn Garry 	struct acpi_device *parent = device->parent;
1707a6f07295SJohn Garry 	static const struct acpi_device_id indirect_io_hosts[] = {
1708dfda4492SJohn Garry 		{"HISI0191", 0},
1709dfda4492SJohn Garry 		{}
1710dfda4492SJohn Garry 	};
1711dfda4492SJohn Garry 
1712dfda4492SJohn Garry 	return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1713dfda4492SJohn Garry }
1714dfda4492SJohn Garry 
1715d87fb091SJohn Garry static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1716e4330d8bSJarkko Nikula {
1717e4330d8bSJarkko Nikula 	struct list_head resource_list;
1718e361d1f8SFrédéric Danis 	bool is_serial_bus_slave = false;
1719f85196bdSHans de Goede 	static const struct acpi_device_id ignore_serial_bus_ids[] = {
1720aba94139SHans de Goede 	/*
17215e63b2eaSLucas Tanure 	 * These devices have multiple SerialBus resources and a client
17225e63b2eaSLucas Tanure 	 * device must be instantiated for each of them, each with
17235e63b2eaSLucas Tanure 	 * its own device id.
17245e63b2eaSLucas Tanure 	 * Normally we only instantiate one client device for the first
17255e63b2eaSLucas Tanure 	 * resource, using the ACPI HID as id. These special cases are handled
17265e63b2eaSLucas Tanure 	 * by the drivers/platform/x86/serial-multi-instantiate.c driver, which
17275e63b2eaSLucas Tanure 	 * knows which client device id to use for each resource.
1728aba94139SHans de Goede 	 */
1729aba94139SHans de Goede 		{"BSG1160", },
173096f984d3SHans de Goede 		{"BSG2150", },
1731d9c01c53SLucas Tanure 		{"CSC3551", },
1732589edb56SHans de Goede 		{"INT33FE", },
1733a3dd034aSAndy Shevchenko 		{"INT3515", },
1734d9c01c53SLucas Tanure 		/* Non-conforming _HID for Cirrus Logic already released */
1735d9c01c53SLucas Tanure 		{"CLSA0100", },
173687eb04bbSLucas Tanure 		{"CLSA0101", },
1737f85196bdSHans de Goede 	/*
173860c7353cSHans de Goede 	 * Some ACPI devs contain SerialBus resources even though they are not
173960c7353cSHans de Goede 	 * attached to a serial bus at all.
174060c7353cSHans de Goede 	 */
174160c7353cSHans de Goede 		{"MSHW0028", },
174260c7353cSHans de Goede 	/*
1743f85196bdSHans de Goede 	 * HIDs of device with an UartSerialBusV2 resource for which userspace
1744f85196bdSHans de Goede 	 * expects a regular tty cdev to be created (instead of the in kernel
1745f85196bdSHans de Goede 	 * serdev) and which have a kernel driver which expects a platform_dev
1746f85196bdSHans de Goede 	 * such as the rfkill-gpio driver.
1747f85196bdSHans de Goede 	 */
1748f85196bdSHans de Goede 		{"BCM4752", },
1749f85196bdSHans de Goede 		{"LNV4752", },
1750aba94139SHans de Goede 		{}
1751aba94139SHans de Goede 	};
1752e4330d8bSJarkko Nikula 
1753dfda4492SJohn Garry 	if (acpi_is_indirect_io_slave(device))
1754dfda4492SJohn Garry 		return true;
1755dfda4492SJohn Garry 
1756ca9ef3abSLukas Wunner 	/* Macs use device properties in lieu of _CRS resources */
1757ca9ef3abSLukas Wunner 	if (x86_apple_machine &&
1758ca9ef3abSLukas Wunner 	    (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1759e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "i2cAddress") ||
1760e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "baud")))
1761ca9ef3abSLukas Wunner 		return true;
1762ca9ef3abSLukas Wunner 
1763f85196bdSHans de Goede 	if (!acpi_match_device_ids(device, ignore_serial_bus_ids))
1764aba94139SHans de Goede 		return false;
1765aba94139SHans de Goede 
1766e4330d8bSJarkko Nikula 	INIT_LIST_HEAD(&resource_list);
1767e361d1f8SFrédéric Danis 	acpi_dev_get_resources(device, &resource_list,
1768e361d1f8SFrédéric Danis 			       acpi_check_serial_bus_slave,
1769e361d1f8SFrédéric Danis 			       &is_serial_bus_slave);
1770e4330d8bSJarkko Nikula 	acpi_dev_free_resource_list(&resource_list);
1771e4330d8bSJarkko Nikula 
1772e361d1f8SFrédéric Danis 	return is_serial_bus_slave;
1773e4330d8bSJarkko Nikula }
1774e4330d8bSJarkko Nikula 
177582c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1776c830dbcfSRafael J. Wysocki 			     int type)
17771da177e4SLinus Torvalds {
1778d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1779d43e167dSRafael J. Wysocki 	device->device_type = type;
1780d43e167dSRafael J. Wysocki 	device->handle = handle;
1781d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
178201bb86b3SSaravana Kannan 	fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1783f5d9ab1dSRafael J. Wysocki 	acpi_set_device_status(device, ACPI_STA_DEFAULT);
1784d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1785c830dbcfSRafael J. Wysocki 	acpi_set_pnp_ids(handle, &device->pnp, type);
1786ffdcd955SMika Westerberg 	acpi_init_properties(device);
1787d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
17882c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1789202317a5SRafael J. Wysocki 	device->flags.initialized = true;
1790d87fb091SJohn Garry 	device->flags.enumeration_by_parent =
1791d87fb091SJohn Garry 		acpi_device_enumeration_by_parent(device);
179210c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(device);
1793cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1794cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1795d0562674SSuthikulpanit, Suravee 	acpi_init_coherency(device);
17966d279758SRafael J. Wysocki }
17976d279758SRafael J. Wysocki 
17986d279758SRafael J. Wysocki static void acpi_scan_dep_init(struct acpi_device *adev)
17996d279758SRafael J. Wysocki {
18006d279758SRafael J. Wysocki 	struct acpi_dep_data *dep;
18016d279758SRafael J. Wysocki 
18026d279758SRafael J. Wysocki 	list_for_each_entry(dep, &acpi_dep_list, node) {
18039d9bcae4SHans de Goede 		if (dep->consumer == adev->handle) {
18049d9bcae4SHans de Goede 			if (dep->honor_dep)
18059d9bcae4SHans de Goede 				adev->flags.honor_deps = 1;
18069d9bcae4SHans de Goede 
18076d279758SRafael J. Wysocki 			adev->dep_unmet++;
18086d279758SRafael J. Wysocki 		}
1809cf860be6SRafael J. Wysocki 	}
18109d9bcae4SHans de Goede }
1811bc3b0772SBjorn Helgaas 
1812cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1813cf860be6SRafael J. Wysocki {
1814cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1815cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
18161da177e4SLinus Torvalds }
18171da177e4SLinus Torvalds 
1818f5d9ab1dSRafael J. Wysocki static void acpi_scan_init_status(struct acpi_device *adev)
18191da177e4SLinus Torvalds {
1820f5d9ab1dSRafael J. Wysocki 	if (acpi_bus_get_status(adev))
1821f5d9ab1dSRafael J. Wysocki 		acpi_set_device_status(adev, 0);
1822f5d9ab1dSRafael J. Wysocki }
1823f5d9ab1dSRafael J. Wysocki 
18241da177e4SLinus Torvalds static int acpi_add_single_object(struct acpi_device **child,
18256d279758SRafael J. Wysocki 				  acpi_handle handle, int type, bool dep_init)
18261da177e4SLinus Torvalds {
1827e52d9d8cSRafael J. Wysocki 	struct acpi_device *device;
18285f4ce260SRafael J. Wysocki 	bool release_dep_lock = false;
1829e52d9d8cSRafael J. Wysocki 	int result;
18306091b263SHans de Goede 
183136bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1832c830dbcfSRafael J. Wysocki 	if (!device)
1833d550d98dSPatrick Mochel 		return -ENOMEM;
18341da177e4SLinus Torvalds 
1835c830dbcfSRafael J. Wysocki 	acpi_init_device_object(device, handle, type);
183663347db0SHans de Goede 	/*
1837f5d9ab1dSRafael J. Wysocki 	 * Getting the status is delayed till here so that we can call
1838f5d9ab1dSRafael J. Wysocki 	 * acpi_bus_get_status() and use its quirk handling.  Note that
1839f5d9ab1dSRafael J. Wysocki 	 * this must be done before the get power-/wakeup_dev-flags calls.
184063347db0SHans de Goede 	 */
18416d279758SRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
18425f4ce260SRafael J. Wysocki 		if (dep_init) {
18435f4ce260SRafael J. Wysocki 			mutex_lock(&acpi_dep_list_lock);
18445f4ce260SRafael J. Wysocki 			/*
18455f4ce260SRafael J. Wysocki 			 * Hold the lock until the acpi_tie_acpi_dev() call
18465f4ce260SRafael J. Wysocki 			 * below to prevent concurrent acpi_scan_clear_dep()
18475f4ce260SRafael J. Wysocki 			 * from deleting a dependency list entry without
18485f4ce260SRafael J. Wysocki 			 * updating dep_unmet for the device.
18495f4ce260SRafael J. Wysocki 			 */
18505f4ce260SRafael J. Wysocki 			release_dep_lock = true;
18516d279758SRafael J. Wysocki 			acpi_scan_dep_init(device);
18525f4ce260SRafael J. Wysocki 		}
1853f5d9ab1dSRafael J. Wysocki 		acpi_scan_init_status(device);
18546d279758SRafael J. Wysocki 	}
185563347db0SHans de Goede 
1856d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1857d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
18581da177e4SLinus Torvalds 
18595f4ce260SRafael J. Wysocki 	result = acpi_tie_acpi_dev(device);
18605f4ce260SRafael J. Wysocki 
18615f4ce260SRafael J. Wysocki 	if (release_dep_lock)
18625f4ce260SRafael J. Wysocki 		mutex_unlock(&acpi_dep_list_lock);
18635f4ce260SRafael J. Wysocki 
18645f4ce260SRafael J. Wysocki 	if (!result)
18655f4ce260SRafael J. Wysocki 		result = __acpi_device_add(device, acpi_device_release);
18665f4ce260SRafael J. Wysocki 
1867d43e167dSRafael J. Wysocki 	if (result) {
1868718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1869d550d98dSPatrick Mochel 		return result;
18701da177e4SLinus Torvalds 	}
18711da177e4SLinus Torvalds 
1872bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1873cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
1874e52d9d8cSRafael J. Wysocki 
1875e52d9d8cSRafael J. Wysocki 	acpi_handle_debug(handle, "Added as %s, parent %s\n",
1876e52d9d8cSRafael J. Wysocki 			  dev_name(&device->dev), device->parent ?
1877e52d9d8cSRafael J. Wysocki 				dev_name(&device->parent->dev) : "(null)");
1878e52d9d8cSRafael J. Wysocki 
18791da177e4SLinus Torvalds 	*child = device;
1880d43e167dSRafael J. Wysocki 	return 0;
1881bf325f95SRafael J. Wysocki }
1882bf325f95SRafael J. Wysocki 
1883a4e081b0SShannon Zhao static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1884a4e081b0SShannon Zhao 					    void *context)
1885a4e081b0SShannon Zhao {
1886a4e081b0SShannon Zhao 	struct resource *res = context;
1887a4e081b0SShannon Zhao 
1888a4e081b0SShannon Zhao 	if (acpi_dev_resource_memory(ares, res))
1889a4e081b0SShannon Zhao 		return AE_CTRL_TERMINATE;
1890a4e081b0SShannon Zhao 
1891a4e081b0SShannon Zhao 	return AE_OK;
1892a4e081b0SShannon Zhao }
1893a4e081b0SShannon Zhao 
1894a4e081b0SShannon Zhao static bool acpi_device_should_be_hidden(acpi_handle handle)
1895a4e081b0SShannon Zhao {
1896a4e081b0SShannon Zhao 	acpi_status status;
1897a4e081b0SShannon Zhao 	struct resource res;
1898a4e081b0SShannon Zhao 
1899a4e081b0SShannon Zhao 	/* Check if it should ignore the UART device */
1900a4e081b0SShannon Zhao 	if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1901a4e081b0SShannon Zhao 		return false;
1902a4e081b0SShannon Zhao 
1903a4e081b0SShannon Zhao 	/*
1904a4e081b0SShannon Zhao 	 * The UART device described in SPCR table is assumed to have only one
1905a4e081b0SShannon Zhao 	 * memory resource present. So we only look for the first one here.
1906a4e081b0SShannon Zhao 	 */
1907a4e081b0SShannon Zhao 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1908a4e081b0SShannon Zhao 				     acpi_get_resource_memory, &res);
1909a4e081b0SShannon Zhao 	if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1910a4e081b0SShannon Zhao 		return false;
1911a4e081b0SShannon Zhao 
1912a4e081b0SShannon Zhao 	acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1913a4e081b0SShannon Zhao 			 &res.start);
1914a4e081b0SShannon Zhao 
1915a4e081b0SShannon Zhao 	return true;
1916a4e081b0SShannon Zhao }
1917a4e081b0SShannon Zhao 
1918cde1f95fSSakari Ailus bool acpi_device_is_present(const struct acpi_device *adev)
1919202317a5SRafael J. Wysocki {
1920cde1f95fSSakari Ailus 	return adev->status.present || adev->status.functional;
1921202317a5SRafael J. Wysocki }
1922202317a5SRafael J. Wysocki 
19234b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1924844142c3SRasmus Villemoes 				       const char *idstr,
1925c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1926c5698074SRafael J. Wysocki {
1927c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1928c5698074SRafael J. Wysocki 
1929aca0a4ebSRafael J. Wysocki 	if (handler->match)
1930aca0a4ebSRafael J. Wysocki 		return handler->match(idstr, matchid);
1931aca0a4ebSRafael J. Wysocki 
1932c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1933c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1934c5698074SRafael J. Wysocki 			if (matchid)
1935c5698074SRafael J. Wysocki 				*matchid = devid;
1936c5698074SRafael J. Wysocki 
19374b59cc1fSRafael J. Wysocki 			return true;
19384b59cc1fSRafael J. Wysocki 		}
19394b59cc1fSRafael J. Wysocki 
19404b59cc1fSRafael J. Wysocki 	return false;
19414b59cc1fSRafael J. Wysocki }
19424b59cc1fSRafael J. Wysocki 
1943844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
19446b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
19456b772e8fSToshi Kani {
19466b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
19476b772e8fSToshi Kani 
19486b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
19496b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
19506b772e8fSToshi Kani 			return handler;
19516b772e8fSToshi Kani 
19526b772e8fSToshi Kani 	return NULL;
19536b772e8fSToshi Kani }
19546b772e8fSToshi Kani 
19553f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
19563f8055c3SRafael J. Wysocki {
19573f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
19583f8055c3SRafael J. Wysocki 		return;
19593f8055c3SRafael J. Wysocki 
19603f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
19613f8055c3SRafael J. Wysocki 
19623f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
19633f8055c3SRafael J. Wysocki 
19643f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
19653f8055c3SRafael J. Wysocki }
19663f8055c3SRafael J. Wysocki 
19673c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev)
19684b59cc1fSRafael J. Wysocki {
19696b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
19703c2cc7ffSRafael J. Wysocki 
1971b43109faSRafael J. Wysocki 	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
19721e2380cdSRafael J. Wysocki 		acpi_dock_add(adev);
19731e2380cdSRafael J. Wysocki 		return;
19741e2380cdSRafael J. Wysocki 	}
19753c2cc7ffSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
19764b59cc1fSRafael J. Wysocki 		struct acpi_scan_handler *handler;
19774b59cc1fSRafael J. Wysocki 
19786b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
19791a699476SRafael J. Wysocki 		if (handler) {
19803c2cc7ffSRafael J. Wysocki 			adev->flags.hotplug_notify = true;
19816b772e8fSToshi Kani 			break;
19826b772e8fSToshi Kani 		}
19836b772e8fSToshi Kani 	}
19841a699476SRafael J. Wysocki }
19856b772e8fSToshi Kani 
198602056a4fSRafael J. Wysocki static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
198740e7fcb1SLan Tianyu {
198840e7fcb1SLan Tianyu 	struct acpi_handle_list dep_devices;
198940e7fcb1SLan Tianyu 	acpi_status status;
19906fc25088SRafael J. Wysocki 	u32 count;
199140e7fcb1SLan Tianyu 	int i;
199240e7fcb1SLan Tianyu 
199371da201fSRafael J. Wysocki 	/*
199471da201fSRafael J. Wysocki 	 * Check for _HID here to avoid deferring the enumeration of:
199571da201fSRafael J. Wysocki 	 * 1. PCI devices.
199671da201fSRafael J. Wysocki 	 * 2. ACPI nodes describing USB ports.
199771da201fSRafael J. Wysocki 	 * Still, checking for _HID catches more then just these cases ...
199871da201fSRafael J. Wysocki 	 */
199902056a4fSRafael J. Wysocki 	if (!check_dep || !acpi_has_method(handle, "_DEP") ||
200002056a4fSRafael J. Wysocki 	    !acpi_has_method(handle, "_HID"))
20016fc25088SRafael J. Wysocki 		return 0;
200263347db0SHans de Goede 
20036fc25088SRafael J. Wysocki 	status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
200440e7fcb1SLan Tianyu 	if (ACPI_FAILURE(status)) {
20056fc25088SRafael J. Wysocki 		acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
20066fc25088SRafael J. Wysocki 		return 0;
200740e7fcb1SLan Tianyu 	}
200840e7fcb1SLan Tianyu 
20096fc25088SRafael J. Wysocki 	for (count = 0, i = 0; i < dep_devices.count; i++) {
201040e7fcb1SLan Tianyu 		struct acpi_device_info *info;
20116fc25088SRafael J. Wysocki 		struct acpi_dep_data *dep;
20129d9bcae4SHans de Goede 		bool skip, honor_dep;
201340e7fcb1SLan Tianyu 
201440e7fcb1SLan Tianyu 		status = acpi_get_object_info(dep_devices.handles[i], &info);
201540e7fcb1SLan Tianyu 		if (ACPI_FAILURE(status)) {
20166fc25088SRafael J. Wysocki 			acpi_handle_debug(handle, "Error reading _DEP device info\n");
201740e7fcb1SLan Tianyu 			continue;
201840e7fcb1SLan Tianyu 		}
201940e7fcb1SLan Tianyu 
20202ef33ee7SRafael J. Wysocki 		skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
20219d9bcae4SHans de Goede 		honor_dep = acpi_info_matches_ids(info, acpi_honor_dep_ids);
202240e7fcb1SLan Tianyu 		kfree(info);
202340e7fcb1SLan Tianyu 
202440e7fcb1SLan Tianyu 		if (skip)
202540e7fcb1SLan Tianyu 			continue;
202640e7fcb1SLan Tianyu 
20276fc25088SRafael J. Wysocki 		dep = kzalloc(sizeof(*dep), GFP_KERNEL);
202840e7fcb1SLan Tianyu 		if (!dep)
20296fc25088SRafael J. Wysocki 			continue;
20306fc25088SRafael J. Wysocki 
20316fc25088SRafael J. Wysocki 		count++;
203240e7fcb1SLan Tianyu 
203391438aebSRafael J. Wysocki 		dep->supplier = dep_devices.handles[i];
20346fc25088SRafael J. Wysocki 		dep->consumer = handle;
20359d9bcae4SHans de Goede 		dep->honor_dep = honor_dep;
203640e7fcb1SLan Tianyu 
203740e7fcb1SLan Tianyu 		mutex_lock(&acpi_dep_list_lock);
203840e7fcb1SLan Tianyu 		list_add_tail(&dep->node , &acpi_dep_list);
203940e7fcb1SLan Tianyu 		mutex_unlock(&acpi_dep_list_lock);
204040e7fcb1SLan Tianyu 	}
20416fc25088SRafael J. Wysocki 
20426fc25088SRafael J. Wysocki 	return count;
204340e7fcb1SLan Tianyu }
204440e7fcb1SLan Tianyu 
20450de7fb7cSRafael J. Wysocki static bool acpi_bus_scan_second_pass;
20460de7fb7cSRafael J. Wysocki 
204771da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
204871da201fSRafael J. Wysocki 				      struct acpi_device **adev_p)
20491da177e4SLinus Torvalds {
2050e3c963c4SRafael J. Wysocki 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
2051e6c1067dSRafael J. Wysocki 	acpi_object_type acpi_type;
20526fc25088SRafael J. Wysocki 	int type;
20531da177e4SLinus Torvalds 
20544002bf38SRafael J. Wysocki 	if (device)
20554002bf38SRafael J. Wysocki 		goto out;
20564002bf38SRafael J. Wysocki 
2057e6c1067dSRafael J. Wysocki 	if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
205851a85fafSBjorn Helgaas 		return AE_OK;
20591da177e4SLinus Torvalds 
2060e6c1067dSRafael J. Wysocki 	switch (acpi_type) {
2061e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_DEVICE:
2062e6c1067dSRafael J. Wysocki 		if (acpi_device_should_be_hidden(handle))
206382c7d5efSRafael J. Wysocki 			return AE_OK;
206482c7d5efSRafael J. Wysocki 
206502056a4fSRafael J. Wysocki 		/* Bail out if there are dependencies. */
206602056a4fSRafael J. Wysocki 		if (acpi_scan_check_dep(handle, check_dep) > 0) {
20670de7fb7cSRafael J. Wysocki 			acpi_bus_scan_second_pass = true;
206871da201fSRafael J. Wysocki 			return AE_CTRL_DEPTH;
206971da201fSRafael J. Wysocki 		}
207002056a4fSRafael J. Wysocki 
2071e6c1067dSRafael J. Wysocki 		fallthrough;
2072e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_ANY:	/* for ACPI_ROOT_OBJECT */
2073e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_DEVICE;
2074e6c1067dSRafael J. Wysocki 		break;
2075e6c1067dSRafael J. Wysocki 
2076e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_PROCESSOR:
2077e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_PROCESSOR;
2078e6c1067dSRafael J. Wysocki 		break;
2079e6c1067dSRafael J. Wysocki 
2080e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_THERMAL:
2081e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_THERMAL;
2082e6c1067dSRafael J. Wysocki 		break;
2083e6c1067dSRafael J. Wysocki 
2084e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_POWER:
208502056a4fSRafael J. Wysocki 		acpi_add_power_resource(handle);
208602056a4fSRafael J. Wysocki 		fallthrough;
2087e6c1067dSRafael J. Wysocki 	default:
2088e6c1067dSRafael J. Wysocki 		return AE_OK;
20890de7fb7cSRafael J. Wysocki 	}
20906fc25088SRafael J. Wysocki 
20913e759425SHans de Goede 	/*
20923e759425SHans de Goede 	 * If check_dep is true at this point, the device has no dependencies,
20933e759425SHans de Goede 	 * or the creation of the device object would have been postponed above.
20943e759425SHans de Goede 	 */
20956d279758SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, !check_dep);
20966d279758SRafael J. Wysocki 	if (!device)
20976d279758SRafael J. Wysocki 		return AE_CTRL_DEPTH;
20986d279758SRafael J. Wysocki 
20996d279758SRafael J. Wysocki 	acpi_scan_init_hotplug(device);
21003c2cc7ffSRafael J. Wysocki 
21014002bf38SRafael J. Wysocki out:
210271da201fSRafael J. Wysocki 	if (!*adev_p)
210371da201fSRafael J. Wysocki 		*adev_p = device;
2104805d410fSRafael J. Wysocki 
210551a85fafSBjorn Helgaas 	return AE_OK;
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
210871da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
210971da201fSRafael J. Wysocki 					void *not_used, void **ret_p)
211071da201fSRafael J. Wysocki {
211171da201fSRafael J. Wysocki 	return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
211271da201fSRafael J. Wysocki }
211371da201fSRafael J. Wysocki 
211471da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
211571da201fSRafael J. Wysocki 					void *not_used, void **ret_p)
211671da201fSRafael J. Wysocki {
211771da201fSRafael J. Wysocki 	return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
211871da201fSRafael J. Wysocki }
211971da201fSRafael J. Wysocki 
212048459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device)
212148459340SZhang Rui {
212248459340SZhang Rui 	/*
2123d87fb091SJohn Garry 	 * Do not enumerate devices with enumeration_by_parent flag set as
2124d87fb091SJohn Garry 	 * they will be enumerated by their respective parents.
212548459340SZhang Rui 	 */
2126d87fb091SJohn Garry 	if (!device->flags.enumeration_by_parent) {
21271571875bSHeikki Krogerus 		acpi_create_platform_device(device, NULL);
212810c7e20bSOctavian Purdila 		acpi_device_set_enumerated(device);
212968bdb677SOctavian Purdila 	} else {
213068bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
213168bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_ADD, device);
213210c7e20bSOctavian Purdila 	}
213348459340SZhang Rui }
213448459340SZhang Rui 
21357d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = {
2136ee892094SRafael J. Wysocki 	{ACPI_DT_NAMESPACE_HID, },
21377d284352SRafael J. Wysocki 	{"", },
21387d284352SRafael J. Wysocki };
21397d284352SRafael J. Wysocki 
21407d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev,
21417d284352SRafael J. Wysocki 				      const struct acpi_device_id *not_used)
21427d284352SRafael J. Wysocki {
21437d284352SRafael J. Wysocki 	/*
2144ee892094SRafael J. Wysocki 	 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2145ee892094SRafael J. Wysocki 	 * below can be unconditional.
21467d284352SRafael J. Wysocki 	 */
21477d284352SRafael J. Wysocki 	if (adev->data.of_compatible)
21487d284352SRafael J. Wysocki 		acpi_default_enumeration(adev);
21497d284352SRafael J. Wysocki 
21507d284352SRafael J. Wysocki 	return 1;
21517d284352SRafael J. Wysocki }
21527d284352SRafael J. Wysocki 
21537d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = {
21547d284352SRafael J. Wysocki 	.ids = generic_device_ids,
21557d284352SRafael J. Wysocki 	.attach = acpi_generic_device_attach,
21567d284352SRafael J. Wysocki };
21577d284352SRafael J. Wysocki 
215887b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
215987b85b3cSRafael J. Wysocki {
216087b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
216187b85b3cSRafael J. Wysocki 	int ret = 0;
216287b85b3cSRafael J. Wysocki 
216387b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
2164c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
2165c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
216687b85b3cSRafael J. Wysocki 
2167c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
2168c5698074SRafael J. Wysocki 		if (handler) {
2169d34afa9dSRafael J. Wysocki 			if (!handler->attach) {
2170d34afa9dSRafael J. Wysocki 				device->pnp.type.platform_id = 0;
2171d34afa9dSRafael J. Wysocki 				continue;
2172d34afa9dSRafael J. Wysocki 			}
2173c5698074SRafael J. Wysocki 			device->handler = handler;
21749cb32acfSRafael J. Wysocki 			ret = handler->attach(device, devid);
21759cb32acfSRafael J. Wysocki 			if (ret > 0)
2176c5698074SRafael J. Wysocki 				break;
21779cb32acfSRafael J. Wysocki 
21789cb32acfSRafael J. Wysocki 			device->handler = NULL;
21799cb32acfSRafael J. Wysocki 			if (ret < 0)
2180c5698074SRafael J. Wysocki 				break;
2181c5698074SRafael J. Wysocki 		}
2182c5698074SRafael J. Wysocki 	}
218348459340SZhang Rui 
2184ca589f94SRafael J. Wysocki 	return ret;
2185ca589f94SRafael J. Wysocki }
2186ca589f94SRafael J. Wysocki 
2187a976a2acSRafael J. Wysocki static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
218851a85fafSBjorn Helgaas {
218971da201fSRafael J. Wysocki 	bool skip = !first_pass && device->flags.visited;
21901e2380cdSRafael J. Wysocki 	acpi_handle ejd;
2191ca589f94SRafael J. Wysocki 	int ret;
21921da177e4SLinus Torvalds 
219371da201fSRafael J. Wysocki 	if (skip)
219471da201fSRafael J. Wysocki 		goto ok;
219571da201fSRafael J. Wysocki 
21961e2380cdSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
21971e2380cdSRafael J. Wysocki 		register_dock_dependent_device(device, ejd);
21981e2380cdSRafael J. Wysocki 
21992c22e652SRafael J. Wysocki 	acpi_bus_get_status(device);
22009d9bcae4SHans de Goede 	/* Skip devices that are not ready for enumeration (e.g. not present) */
22019d9bcae4SHans de Goede 	if (!acpi_dev_ready_for_enumeration(device)) {
2202cde1f95fSSakari Ailus 		device->flags.initialized = false;
220310c7e20bSOctavian Purdila 		acpi_device_clear_enumerated(device);
22041b1f3e16SRafael J. Wysocki 		device->flags.power_manageable = 0;
2205a976a2acSRafael J. Wysocki 		return 0;
22062c22e652SRafael J. Wysocki 	}
22073a391a39SRafael J. Wysocki 	if (device->handler)
22082c22e652SRafael J. Wysocki 		goto ok;
22093a391a39SRafael J. Wysocki 
2210202317a5SRafael J. Wysocki 	if (!device->flags.initialized) {
22111b1f3e16SRafael J. Wysocki 		device->flags.power_manageable =
22121b1f3e16SRafael J. Wysocki 			device->power.states[ACPI_STATE_D0].flags.valid;
22131b1f3e16SRafael J. Wysocki 		if (acpi_bus_init_power(device))
22141b1f3e16SRafael J. Wysocki 			device->flags.power_manageable = 0;
22151b1f3e16SRafael J. Wysocki 
2216202317a5SRafael J. Wysocki 		device->flags.initialized = true;
2217c381fc3aSRafael J. Wysocki 	} else if (device->flags.visited) {
2218c381fc3aSRafael J. Wysocki 		goto ok;
2219202317a5SRafael J. Wysocki 	}
222010c7e20bSOctavian Purdila 
2221ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
22226931007cSRafael J. Wysocki 	if (ret < 0)
2223a976a2acSRafael J. Wysocki 		return 0;
22246931007cSRafael J. Wysocki 
22256931007cSRafael J. Wysocki 	device->flags.match_driver = true;
2226d87fb091SJohn Garry 	if (ret > 0 && !device->flags.enumeration_by_parent) {
2227f406270bSRafael J. Wysocki 		acpi_device_set_enumerated(device);
2228f406270bSRafael J. Wysocki 		goto ok;
2229f406270bSRafael J. Wysocki 	}
2230f406270bSRafael J. Wysocki 
2231ca589f94SRafael J. Wysocki 	ret = device_attach(&device->dev);
2232202317a5SRafael J. Wysocki 	if (ret < 0)
2233a976a2acSRafael J. Wysocki 		return 0;
22347d284352SRafael J. Wysocki 
2235d87fb091SJohn Garry 	if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2236e4330d8bSJarkko Nikula 		acpi_default_enumeration(device);
2237d87fb091SJohn Garry 	else
2238d87fb091SJohn Garry 		acpi_device_set_enumerated(device);
2239202317a5SRafael J. Wysocki 
2240202317a5SRafael J. Wysocki ok:
2241a976a2acSRafael J. Wysocki 	acpi_dev_for_each_child(device, acpi_bus_attach, first_pass);
22428ab17fc9SRafael J. Wysocki 
224371da201fSRafael J. Wysocki 	if (!skip && device->handler && device->handler->hotplug.notify_online)
22448ab17fc9SRafael J. Wysocki 		device->handler->hotplug.notify_online(device);
2245a976a2acSRafael J. Wysocki 
2246a976a2acSRafael J. Wysocki 	return 0;
2247805d410fSRafael J. Wysocki }
22483fb02738SRajesh Shah 
2249b83e2b30SDaniel Scally static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
225040e7fcb1SLan Tianyu {
225140e7fcb1SLan Tianyu 	struct acpi_device *adev;
225240e7fcb1SLan Tianyu 
2253*45e9aa1fSRafael J. Wysocki 	adev = acpi_get_acpi_dev(dep->consumer);
2254ad4d451eSRafael J. Wysocki 	if (adev) {
2255b83e2b30SDaniel Scally 		*(struct acpi_device **)data = adev;
2256b83e2b30SDaniel Scally 		return 1;
2257b83e2b30SDaniel Scally 	}
2258ad4d451eSRafael J. Wysocki 	/* Continue parsing if the device object is not present. */
2259ad4d451eSRafael J. Wysocki 	return 0;
2260ad4d451eSRafael J. Wysocki }
2261b83e2b30SDaniel Scally 
2262dc612486SRafael J. Wysocki struct acpi_scan_clear_dep_work {
2263dc612486SRafael J. Wysocki 	struct work_struct work;
2264dc612486SRafael J. Wysocki 	struct acpi_device *adev;
2265dc612486SRafael J. Wysocki };
2266dc612486SRafael J. Wysocki 
2267dc612486SRafael J. Wysocki static void acpi_scan_clear_dep_fn(struct work_struct *work)
2268dc612486SRafael J. Wysocki {
2269dc612486SRafael J. Wysocki 	struct acpi_scan_clear_dep_work *cdw;
2270dc612486SRafael J. Wysocki 
2271dc612486SRafael J. Wysocki 	cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
2272dc612486SRafael J. Wysocki 
2273dc612486SRafael J. Wysocki 	acpi_scan_lock_acquire();
2274a976a2acSRafael J. Wysocki 	acpi_bus_attach(cdw->adev, (void *)true);
2275dc612486SRafael J. Wysocki 	acpi_scan_lock_release();
2276dc612486SRafael J. Wysocki 
2277dc612486SRafael J. Wysocki 	acpi_dev_put(cdw->adev);
2278dc612486SRafael J. Wysocki 	kfree(cdw);
2279dc612486SRafael J. Wysocki }
2280dc612486SRafael J. Wysocki 
2281dc612486SRafael J. Wysocki static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2282dc612486SRafael J. Wysocki {
2283dc612486SRafael J. Wysocki 	struct acpi_scan_clear_dep_work *cdw;
2284dc612486SRafael J. Wysocki 
2285dc612486SRafael J. Wysocki 	if (adev->dep_unmet)
2286dc612486SRafael J. Wysocki 		return false;
2287dc612486SRafael J. Wysocki 
2288dc612486SRafael J. Wysocki 	cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2289dc612486SRafael J. Wysocki 	if (!cdw)
2290dc612486SRafael J. Wysocki 		return false;
2291dc612486SRafael J. Wysocki 
2292dc612486SRafael J. Wysocki 	cdw->adev = adev;
2293dc612486SRafael J. Wysocki 	INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2294dc612486SRafael J. Wysocki 	/*
2295dc612486SRafael J. Wysocki 	 * Since the work function may block on the lock until the entire
2296dc612486SRafael J. Wysocki 	 * initial enumeration of devices is complete, put it into the unbound
2297dc612486SRafael J. Wysocki 	 * workqueue.
2298dc612486SRafael J. Wysocki 	 */
2299dc612486SRafael J. Wysocki 	queue_work(system_unbound_wq, &cdw->work);
2300dc612486SRafael J. Wysocki 
2301dc612486SRafael J. Wysocki 	return true;
2302dc612486SRafael J. Wysocki }
2303dc612486SRafael J. Wysocki 
2304a9e10e58SDaniel Scally static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
230540e7fcb1SLan Tianyu {
2306*45e9aa1fSRafael J. Wysocki 	struct acpi_device *adev = acpi_get_acpi_dev(dep->consumer);
230740e7fcb1SLan Tianyu 
23080f347aa0SHans de Goede 	if (adev) {
230940e7fcb1SLan Tianyu 		adev->dep_unmet--;
2310dc612486SRafael J. Wysocki 		if (!acpi_scan_clear_dep_queue(adev))
2311dc612486SRafael J. Wysocki 			acpi_dev_put(adev);
23120f347aa0SHans de Goede 	}
231371da201fSRafael J. Wysocki 
231440e7fcb1SLan Tianyu 	list_del(&dep->node);
231540e7fcb1SLan Tianyu 	kfree(dep);
2316a9e10e58SDaniel Scally 
2317a9e10e58SDaniel Scally 	return 0;
2318a9e10e58SDaniel Scally }
2319a9e10e58SDaniel Scally 
2320a9e10e58SDaniel Scally /**
2321a9e10e58SDaniel Scally  * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2322a9e10e58SDaniel Scally  * @handle:	The ACPI handle of the supplier device
2323a9e10e58SDaniel Scally  * @callback:	Pointer to the callback function to apply
2324a9e10e58SDaniel Scally  * @data:	Pointer to some data to pass to the callback
2325a9e10e58SDaniel Scally  *
2326a9e10e58SDaniel Scally  * The return value of the callback determines this function's behaviour. If 0
2327a9e10e58SDaniel Scally  * is returned we continue to iterate over acpi_dep_list. If a positive value
2328a9e10e58SDaniel Scally  * is returned then the loop is broken but this function returns 0. If a
2329a9e10e58SDaniel Scally  * negative value is returned by the callback then the loop is broken and that
2330a9e10e58SDaniel Scally  * value is returned as the final error.
2331a9e10e58SDaniel Scally  */
2332aff0dbd0SRafael J. Wysocki static int acpi_walk_dep_device_list(acpi_handle handle,
2333a9e10e58SDaniel Scally 				int (*callback)(struct acpi_dep_data *, void *),
2334a9e10e58SDaniel Scally 				void *data)
2335a9e10e58SDaniel Scally {
2336a9e10e58SDaniel Scally 	struct acpi_dep_data *dep, *tmp;
233723db673dSColin Ian King 	int ret = 0;
2338a9e10e58SDaniel Scally 
2339a9e10e58SDaniel Scally 	mutex_lock(&acpi_dep_list_lock);
2340a9e10e58SDaniel Scally 	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2341a9e10e58SDaniel Scally 		if (dep->supplier == handle) {
2342a9e10e58SDaniel Scally 			ret = callback(dep, data);
2343a9e10e58SDaniel Scally 			if (ret)
2344a9e10e58SDaniel Scally 				break;
234540e7fcb1SLan Tianyu 		}
234640e7fcb1SLan Tianyu 	}
234740e7fcb1SLan Tianyu 	mutex_unlock(&acpi_dep_list_lock);
2348a9e10e58SDaniel Scally 
2349a9e10e58SDaniel Scally 	return ret > 0 ? 0 : ret;
235040e7fcb1SLan Tianyu }
235140e7fcb1SLan Tianyu 
2352b8bd759aSRafael J. Wysocki /**
2353a9e10e58SDaniel Scally  * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2354a9e10e58SDaniel Scally  * @supplier: Pointer to the supplier &struct acpi_device
2355a9e10e58SDaniel Scally  *
2356a9e10e58SDaniel Scally  * Clear dependencies on the given device.
2357a9e10e58SDaniel Scally  */
2358a9e10e58SDaniel Scally void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2359a9e10e58SDaniel Scally {
2360a9e10e58SDaniel Scally 	acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2361a9e10e58SDaniel Scally }
2362a9e10e58SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2363a9e10e58SDaniel Scally 
2364a9e10e58SDaniel Scally /**
23659d9bcae4SHans de Goede  * acpi_dev_ready_for_enumeration - Check if the ACPI device is ready for enumeration
23669d9bcae4SHans de Goede  * @device: Pointer to the &struct acpi_device to check
23679d9bcae4SHans de Goede  *
23689d9bcae4SHans de Goede  * Check if the device is present and has no unmet dependencies.
23699d9bcae4SHans de Goede  *
23709d9bcae4SHans de Goede  * Return true if the device is ready for enumeratino. Otherwise, return false.
23719d9bcae4SHans de Goede  */
23729d9bcae4SHans de Goede bool acpi_dev_ready_for_enumeration(const struct acpi_device *device)
23739d9bcae4SHans de Goede {
23749d9bcae4SHans de Goede 	if (device->flags.honor_deps && device->dep_unmet)
23759d9bcae4SHans de Goede 		return false;
23769d9bcae4SHans de Goede 
23779d9bcae4SHans de Goede 	return acpi_device_is_present(device);
23789d9bcae4SHans de Goede }
23799d9bcae4SHans de Goede EXPORT_SYMBOL_GPL(acpi_dev_ready_for_enumeration);
23809d9bcae4SHans de Goede 
23819d9bcae4SHans de Goede /**
2382b83e2b30SDaniel Scally  * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2383b83e2b30SDaniel Scally  * @supplier: Pointer to the dependee device
2384b83e2b30SDaniel Scally  *
2385b83e2b30SDaniel Scally  * Returns the first &struct acpi_device which declares itself dependent on
2386b83e2b30SDaniel Scally  * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2387b83e2b30SDaniel Scally  *
2388b83e2b30SDaniel Scally  * The caller is responsible for putting the reference to adev when it is no
2389b83e2b30SDaniel Scally  * longer needed.
2390b83e2b30SDaniel Scally  */
2391b83e2b30SDaniel Scally struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2392b83e2b30SDaniel Scally {
2393b83e2b30SDaniel Scally 	struct acpi_device *adev = NULL;
2394b83e2b30SDaniel Scally 
2395b83e2b30SDaniel Scally 	acpi_walk_dep_device_list(supplier->handle,
2396b83e2b30SDaniel Scally 				  acpi_dev_get_first_consumer_dev_cb, &adev);
2397b83e2b30SDaniel Scally 
2398b83e2b30SDaniel Scally 	return adev;
2399b83e2b30SDaniel Scally }
2400b83e2b30SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
2401c10d7a13SDaniel Drake 
24023fb02738SRajesh Shah /**
24031da177e4SLinus Torvalds  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
24042c0d4fe0SRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
24052c0d4fe0SRafael J. Wysocki  *
24062c0d4fe0SRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
24075c478f49SBjorn Helgaas  * found devices.
2408c4168bffSLi Shaohua  *
24092c0d4fe0SRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
24102c0d4fe0SRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
24112c0d4fe0SRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
24122c0d4fe0SRafael J. Wysocki  * appropriate driver.
241388346167SRafael J. Wysocki  *
24142c0d4fe0SRafael J. Wysocki  * Must be called under acpi_scan_lock.
24153fb02738SRajesh Shah  */
24161da177e4SLinus Torvalds int acpi_bus_scan(acpi_handle handle)
24172c0d4fe0SRafael J. Wysocki {
241871da201fSRafael J. Wysocki 	struct acpi_device *device = NULL;
24191da177e4SLinus Torvalds 
24200de7fb7cSRafael J. Wysocki 	acpi_bus_scan_second_pass = false;
24210de7fb7cSRafael J. Wysocki 
242271da201fSRafael J. Wysocki 	/* Pass 1: Avoid enumerating devices with missing dependencies. */
242371da201fSRafael J. Wysocki 
242471da201fSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
24251da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
242671da201fSRafael J. Wysocki 				    acpi_bus_check_add_1, NULL, NULL,
242771da201fSRafael J. Wysocki 				    (void **)&device);
24281da177e4SLinus Torvalds 
242971da201fSRafael J. Wysocki 	if (!device)
24301da177e4SLinus Torvalds 		return -ENODEV;
243171da201fSRafael J. Wysocki 
2432a976a2acSRafael J. Wysocki 	acpi_bus_attach(device, (void *)true);
243371da201fSRafael J. Wysocki 
24340de7fb7cSRafael J. Wysocki 	if (!acpi_bus_scan_second_pass)
24350de7fb7cSRafael J. Wysocki 		return 0;
24360de7fb7cSRafael J. Wysocki 
243771da201fSRafael J. Wysocki 	/* Pass 2: Enumerate all of the remaining devices. */
243871da201fSRafael J. Wysocki 
243971da201fSRafael J. Wysocki 	device = NULL;
244071da201fSRafael J. Wysocki 
244171da201fSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
244271da201fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
244371da201fSRafael J. Wysocki 				    acpi_bus_check_add_2, NULL, NULL,
244471da201fSRafael J. Wysocki 				    (void **)&device);
244571da201fSRafael J. Wysocki 
2446a976a2acSRafael J. Wysocki 	acpi_bus_attach(device, NULL);
244771da201fSRafael J. Wysocki 
244871da201fSRafael J. Wysocki 	return 0;
24491da177e4SLinus Torvalds }
24501da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_scan);
24511da177e4SLinus Torvalds 
2452a976a2acSRafael J. Wysocki static int acpi_bus_trim_one(struct acpi_device *adev, void *not_used)
24531da177e4SLinus Torvalds {
24541da177e4SLinus Torvalds 	struct acpi_scan_handler *handler = adev->handler;
24551da177e4SLinus Torvalds 
2456a976a2acSRafael J. Wysocki 	acpi_dev_for_each_child_reverse(adev, acpi_bus_trim_one, NULL);
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 	adev->flags.match_driver = false;
24591da177e4SLinus Torvalds 	if (handler) {
24601da177e4SLinus Torvalds 		if (handler->detach)
24611da177e4SLinus Torvalds 			handler->detach(adev);
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 		adev->handler = NULL;
24641da177e4SLinus Torvalds 	} else {
24651da177e4SLinus Torvalds 		device_release_driver(&adev->dev);
24661da177e4SLinus Torvalds 	}
24671da177e4SLinus Torvalds 	/*
24681da177e4SLinus Torvalds 	 * Most likely, the device is going away, so put it into D3cold before
24691da177e4SLinus Torvalds 	 * that.
24701da177e4SLinus Torvalds 	 */
24711da177e4SLinus Torvalds 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
24721da177e4SLinus Torvalds 	adev->flags.initialized = false;
247310c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(adev);
2474a976a2acSRafael J. Wysocki 
2475a976a2acSRafael J. Wysocki 	return 0;
2476a976a2acSRafael J. Wysocki }
2477a976a2acSRafael J. Wysocki 
2478a976a2acSRafael J. Wysocki /**
2479a976a2acSRafael J. Wysocki  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2480a976a2acSRafael J. Wysocki  * @adev: Root of the ACPI namespace scope to walk.
2481a976a2acSRafael J. Wysocki  *
2482a976a2acSRafael J. Wysocki  * Must be called under acpi_scan_lock.
2483a976a2acSRafael J. Wysocki  */
2484a976a2acSRafael J. Wysocki void acpi_bus_trim(struct acpi_device *adev)
2485a976a2acSRafael J. Wysocki {
2486a976a2acSRafael J. Wysocki 	acpi_bus_trim_one(adev, NULL);
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(acpi_bus_trim);
24891da177e4SLinus Torvalds 
2490a64a62ceSLv Zheng int acpi_bus_register_early_device(int type)
2491a64a62ceSLv Zheng {
2492a64a62ceSLv Zheng 	struct acpi_device *device = NULL;
2493a64a62ceSLv Zheng 	int result;
2494a64a62ceSLv Zheng 
24956d279758SRafael J. Wysocki 	result = acpi_add_single_object(&device, NULL, type, false);
2496a64a62ceSLv Zheng 	if (result)
2497a64a62ceSLv Zheng 		return result;
2498a64a62ceSLv Zheng 
2499a64a62ceSLv Zheng 	device->flags.match_driver = true;
2500a64a62ceSLv Zheng 	return device_attach(&device->dev);
2501a64a62ceSLv Zheng }
2502a64a62ceSLv Zheng EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2503a64a62ceSLv Zheng 
2504681e7187SRafael J. Wysocki static void acpi_bus_scan_fixed(void)
25051da177e4SLinus Torvalds {
25061da177e4SLinus Torvalds 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2507681e7187SRafael J. Wysocki 		struct acpi_device *adev = NULL;
25081da177e4SLinus Torvalds 
2509681e7187SRafael J. Wysocki 		acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_POWER_BUTTON,
2510681e7187SRafael J. Wysocki 				       false);
2511681e7187SRafael J. Wysocki 		if (adev) {
2512681e7187SRafael J. Wysocki 			adev->flags.match_driver = true;
2513681e7187SRafael J. Wysocki 			if (device_attach(&adev->dev) >= 0)
2514681e7187SRafael J. Wysocki 				device_init_wakeup(&adev->dev, true);
2515681e7187SRafael J. Wysocki 			else
2516681e7187SRafael J. Wysocki 				dev_dbg(&adev->dev, "No driver\n");
2517681e7187SRafael J. Wysocki 		}
25181da177e4SLinus Torvalds 	}
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2521681e7187SRafael J. Wysocki 		struct acpi_device *adev = NULL;
25221da177e4SLinus Torvalds 
2523681e7187SRafael J. Wysocki 		acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_SLEEP_BUTTON,
2524681e7187SRafael J. Wysocki 				       false);
2525681e7187SRafael J. Wysocki 		if (adev) {
2526681e7187SRafael J. Wysocki 			adev->flags.match_driver = true;
2527681e7187SRafael J. Wysocki 			if (device_attach(&adev->dev) < 0)
2528681e7187SRafael J. Wysocki 				dev_dbg(&adev->dev, "No driver\n");
25291da177e4SLinus Torvalds 		}
2530681e7187SRafael J. Wysocki 	}
25311da177e4SLinus Torvalds }
25321da177e4SLinus Torvalds 
2533a4e081b0SShannon Zhao static void __init acpi_get_spcr_uart_addr(void)
2534a4e081b0SShannon Zhao {
2535a4e081b0SShannon Zhao 	acpi_status status;
2536a4e081b0SShannon Zhao 	struct acpi_table_spcr *spcr_ptr;
2537a4e081b0SShannon Zhao 
2538a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_SPCR, 0,
2539a4e081b0SShannon Zhao 				(struct acpi_table_header **)&spcr_ptr);
2540ead7ba13SHanjun Guo 	if (ACPI_FAILURE(status)) {
25418acf4108SHanjun Guo 		pr_warn("STAO table present, but SPCR is missing\n");
2542ead7ba13SHanjun Guo 		return;
2543ead7ba13SHanjun Guo 	}
2544ead7ba13SHanjun Guo 
2545a4e081b0SShannon Zhao 	spcr_uart_addr = spcr_ptr->serial_port.address;
2546ead7ba13SHanjun Guo 	acpi_put_table((struct acpi_table_header *)spcr_ptr);
2547a4e081b0SShannon Zhao }
2548a4e081b0SShannon Zhao 
254968bdb677SOctavian Purdila static bool acpi_scan_initialized;
255068bdb677SOctavian Purdila 
2551b6c55b16SRafael J. Wysocki void __init acpi_scan_init(void)
25521da177e4SLinus Torvalds {
2553a4e081b0SShannon Zhao 	acpi_status status;
2554a4e081b0SShannon Zhao 	struct acpi_table_stao *stao_ptr;
25551da177e4SLinus Torvalds 
255692ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
25574daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2558ac212b69SRafael J. Wysocki 	acpi_processor_init();
2559cb0701acSAndy Shevchenko 	acpi_platform_init();
2560f58b082aSRafael J. Wysocki 	acpi_lpss_init();
256192082a88SKen Xue 	acpi_apd_init();
25622fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2563737f1a9fSRafael J. Wysocki 	acpi_container_init();
25640a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
2565cc6a0e31SMika Westerberg 	acpi_watchdog_init();
2566eec15edbSZhang Rui 	acpi_pnp_init();
25673230bbfcSZhang Rui 	acpi_int340x_thermal_init();
25686ce2e188SGraeme Gregory 	acpi_amba_init();
2569eeb2d80dSSrinivas Pandruvada 	acpi_init_lpit();
257097d9a9e9SRafael J. Wysocki 
25717d284352SRafael J. Wysocki 	acpi_scan_add_handler(&generic_device_handler);
25727d284352SRafael J. Wysocki 
2573a4e081b0SShannon Zhao 	/*
2574a4e081b0SShannon Zhao 	 * If there is STAO table, check whether it needs to ignore the UART
2575a4e081b0SShannon Zhao 	 * device in SPCR table.
2576a4e081b0SShannon Zhao 	 */
2577a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_STAO, 0,
2578a4e081b0SShannon Zhao 				(struct acpi_table_header **)&stao_ptr);
2579a4e081b0SShannon Zhao 	if (ACPI_SUCCESS(status)) {
2580a4e081b0SShannon Zhao 		if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
25818acf4108SHanjun Guo 			pr_info("STAO Name List not yet supported.\n");
2582a4e081b0SShannon Zhao 
2583a4e081b0SShannon Zhao 		if (stao_ptr->ignore_uart)
2584a4e081b0SShannon Zhao 			acpi_get_spcr_uart_addr();
2585ead7ba13SHanjun Guo 
2586ead7ba13SHanjun Guo 		acpi_put_table((struct acpi_table_header *)stao_ptr);
2587a4e081b0SShannon Zhao 	}
2588a4e081b0SShannon Zhao 
2589eb7f43c4SRafael J. Wysocki 	acpi_gpe_apply_masked_gpes();
2590eb7f43c4SRafael J. Wysocki 	acpi_update_all_gpes();
2591eb7f43c4SRafael J. Wysocki 
25927291edcaSDavid Hildenbrand 	/*
25937291edcaSDavid Hildenbrand 	 * Although we call __add_memory() that is documented to require the
25947291edcaSDavid Hildenbrand 	 * device_hotplug_lock, it is not necessary here because this is an
25957291edcaSDavid Hildenbrand 	 * early code when userspace or any other code path cannot trigger
25967291edcaSDavid Hildenbrand 	 * hotplug/hotunplug operations.
25977291edcaSDavid Hildenbrand 	 */
25983757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
25991da177e4SLinus Torvalds 	/*
26001da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
26011da177e4SLinus Torvalds 	 */
2602b6c55b16SRafael J. Wysocki 	if (acpi_bus_scan(ACPI_ROOT_OBJECT))
2603c96f195dSRafael J. Wysocki 		goto unlock;
2604c04209a7SAlexey Starikovskiy 
2605e3c963c4SRafael J. Wysocki 	acpi_root = acpi_fetch_acpi_dev(ACPI_ROOT_OBJECT);
2606e3c963c4SRafael J. Wysocki 	if (!acpi_root)
2607c96f195dSRafael J. Wysocki 		goto unlock;
2608b8bd759aSRafael J. Wysocki 
26092650ef42SAaron Lu 	/* Fixed feature devices do not exist on HW-reduced platform */
2610681e7187SRafael J. Wysocki 	if (!acpi_gbl_reduced_hardware)
2611681e7187SRafael J. Wysocki 		acpi_bus_scan_fixed();
2612b8bd759aSRafael J. Wysocki 
26136381195aSRafael J. Wysocki 	acpi_turn_off_unused_power_resources();
261429038ae2SRafael J. Wysocki 
261568bdb677SOctavian Purdila 	acpi_scan_initialized = true;
261668bdb677SOctavian Purdila 
2617c96f195dSRafael J. Wysocki unlock:
26183757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
26191da177e4SLinus Torvalds }
2620e647b532SMarc Zyngier 
2621e647b532SMarc Zyngier static struct acpi_probe_entry *ape;
2622e647b532SMarc Zyngier static int acpi_probe_count;
26235331d9caSLorenzo Pieralisi static DEFINE_MUTEX(acpi_probe_mutex);
2624e647b532SMarc Zyngier 
262560574d1eSKeith Busch static int __init acpi_match_madt(union acpi_subtable_headers *header,
2626e647b532SMarc Zyngier 				  const unsigned long end)
2627e647b532SMarc Zyngier {
262860574d1eSKeith Busch 	if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2629e647b532SMarc Zyngier 		if (!ape->probe_subtbl(header, end))
2630e647b532SMarc Zyngier 			acpi_probe_count++;
2631e647b532SMarc Zyngier 
2632e647b532SMarc Zyngier 	return 0;
2633e647b532SMarc Zyngier }
2634e647b532SMarc Zyngier 
2635e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2636e647b532SMarc Zyngier {
2637e647b532SMarc Zyngier 	int count = 0;
2638e647b532SMarc Zyngier 
2639e647b532SMarc Zyngier 	if (acpi_disabled)
2640e647b532SMarc Zyngier 		return 0;
2641e647b532SMarc Zyngier 
26425331d9caSLorenzo Pieralisi 	mutex_lock(&acpi_probe_mutex);
2643e647b532SMarc Zyngier 	for (ape = ap_head; nr; ape++, nr--) {
26445599fb69SBob Moore 		if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2645e647b532SMarc Zyngier 			acpi_probe_count = 0;
2646e647b532SMarc Zyngier 			acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2647e647b532SMarc Zyngier 			count += acpi_probe_count;
2648e647b532SMarc Zyngier 		} else {
2649e647b532SMarc Zyngier 			int res;
2650e647b532SMarc Zyngier 			res = acpi_table_parse(ape->id, ape->probe_table);
2651e647b532SMarc Zyngier 			if (!res)
2652e647b532SMarc Zyngier 				count++;
2653e647b532SMarc Zyngier 		}
2654e647b532SMarc Zyngier 	}
26555331d9caSLorenzo Pieralisi 	mutex_unlock(&acpi_probe_mutex);
2656e647b532SMarc Zyngier 
2657e647b532SMarc Zyngier 	return count;
2658e647b532SMarc Zyngier }
265968bdb677SOctavian Purdila 
266068bdb677SOctavian Purdila static void acpi_table_events_fn(struct work_struct *work)
266168bdb677SOctavian Purdila {
266268bdb677SOctavian Purdila 	acpi_scan_lock_acquire();
266368bdb677SOctavian Purdila 	acpi_bus_scan(ACPI_ROOT_OBJECT);
266468bdb677SOctavian Purdila 	acpi_scan_lock_release();
26658d287e82SRafael J. Wysocki 
26668d287e82SRafael J. Wysocki 	kfree(work);
266768bdb677SOctavian Purdila }
266868bdb677SOctavian Purdila 
26698d287e82SRafael J. Wysocki void acpi_scan_table_notify(void)
267068bdb677SOctavian Purdila {
26718d287e82SRafael J. Wysocki 	struct work_struct *work;
267268bdb677SOctavian Purdila 
267368bdb677SOctavian Purdila 	if (!acpi_scan_initialized)
267468bdb677SOctavian Purdila 		return;
267568bdb677SOctavian Purdila 
26768d287e82SRafael J. Wysocki 	work = kmalloc(sizeof(*work), GFP_KERNEL);
26778d287e82SRafael J. Wysocki 	if (!work)
267868bdb677SOctavian Purdila 		return;
267968bdb677SOctavian Purdila 
26808d287e82SRafael J. Wysocki 	INIT_WORK(work, acpi_table_events_fn);
26818d287e82SRafael J. Wysocki 	schedule_work(work);
268268bdb677SOctavian Purdila }
268368bdb677SOctavian Purdila 
268468bdb677SOctavian Purdila int acpi_reconfig_notifier_register(struct notifier_block *nb)
268568bdb677SOctavian Purdila {
268668bdb677SOctavian Purdila 	return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
268768bdb677SOctavian Purdila }
268868bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_register);
268968bdb677SOctavian Purdila 
269068bdb677SOctavian Purdila int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
269168bdb677SOctavian Purdila {
269268bdb677SOctavian Purdila 	return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
269368bdb677SOctavian Purdila }
269468bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);
2695