xref: /openbmc/linux/drivers/acpi/scan.c (revision b340c7d6f619ad63d1896a6debceea1de3a78f29)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * scan.c - support for transforming the ACPI namespace into individual objects
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
68acf4108SHanjun Guo #define pr_fmt(fmt) "ACPI: " fmt
78acf4108SHanjun Guo 
81da177e4SLinus Torvalds #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/init.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
119b6d97b6SRandy Dunlap #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/acpi.h>
13643b8e4dSLorenzo Pieralisi #include <linux/acpi_iort.h>
143cf48554SJean-Philippe Brucker #include <linux/acpi_viot.h>
1511a8c5e3SJean-Philippe Brucker #include <linux/iommu.h>
1674523c90SAlok N Kataria #include <linux/signal.h>
1774523c90SAlok N Kataria #include <linux/kthread.h>
18222e82acSDarrick J. Wong #include <linux/dmi.h>
190a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
20ca9ef3abSLukas Wunner #include <linux/platform_data/x86/apple.h>
21ca5999fdSMike Rapoport #include <linux/pgtable.h>
22d783156eSRafael J. Wysocki 
23e60cc7a6SBjorn Helgaas #include "internal.h"
24e60cc7a6SBjorn Helgaas 
251da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2829b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
291da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
301da177e4SLinus Torvalds 
31859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
32859ac9a4SBjorn Helgaas 
33d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)
34d783156eSRafael J. Wysocki 
35620e112cSThomas Renninger static const char *dummy_hid = "device";
362b2ae7c7SThomas Renninger 
3740e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list);
3840e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock);
392d12b6b3SLukas Wunner LIST_HEAD(acpi_bus_id_list);
40c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
41ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
429090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
431da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
44e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock);
451da177e4SLinus Torvalds 
46a4e081b0SShannon Zhao /*
47a4e081b0SShannon Zhao  * The UART device described by the SPCR table is the only object which needs
48a4e081b0SShannon Zhao  * special-casing. Everything else is covered by ACPI namespace paths in STAO
49a4e081b0SShannon Zhao  * table.
50a4e081b0SShannon Zhao  */
51a4e081b0SShannon Zhao static u64 spcr_uart_addr;
52a4e081b0SShannon Zhao 
533757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
543757b948SRafael J. Wysocki {
553757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
563757b948SRafael J. Wysocki }
573757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
583757b948SRafael J. Wysocki 
593757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
603757b948SRafael J. Wysocki {
613757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
623757b948SRafael J. Wysocki }
633757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
643757b948SRafael J. Wysocki 
65e525506fSRafael J. Wysocki void acpi_lock_hp_context(void)
66e525506fSRafael J. Wysocki {
67e525506fSRafael J. Wysocki 	mutex_lock(&acpi_hp_context_lock);
68e525506fSRafael J. Wysocki }
69e525506fSRafael J. Wysocki 
70e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void)
71e525506fSRafael J. Wysocki {
72e525506fSRafael J. Wysocki 	mutex_unlock(&acpi_hp_context_lock);
73e525506fSRafael J. Wysocki }
74e525506fSRafael J. Wysocki 
755d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev,
765d513205SRafael J. Wysocki 				struct acpi_hotplug_context *hp,
775d513205SRafael J. Wysocki 				int (*notify)(struct acpi_device *, u32),
785d513205SRafael J. Wysocki 				void (*uevent)(struct acpi_device *, u32))
795d513205SRafael J. Wysocki {
805d513205SRafael J. Wysocki 	acpi_lock_hp_context();
81ba574dc8SRafael J. Wysocki 	hp->notify = notify;
82ba574dc8SRafael J. Wysocki 	hp->uevent = uevent;
83ba574dc8SRafael J. Wysocki 	acpi_set_hp_context(adev, hp);
845d513205SRafael J. Wysocki 	acpi_unlock_hp_context();
855d513205SRafael J. Wysocki }
865d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
875d513205SRafael J. Wysocki 
88ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
89ca589f94SRafael J. Wysocki {
90d34afa9dSRafael J. Wysocki 	if (!handler)
91ca589f94SRafael J. Wysocki 		return -EINVAL;
92ca589f94SRafael J. Wysocki 
93ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
94ca589f94SRafael J. Wysocki 	return 0;
95ca589f94SRafael J. Wysocki }
96ca589f94SRafael J. Wysocki 
973f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
983f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
993f8055c3SRafael J. Wysocki {
1003f8055c3SRafael J. Wysocki 	int error;
1013f8055c3SRafael J. Wysocki 
1023f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
1033f8055c3SRafael J. Wysocki 	if (error)
1043f8055c3SRafael J. Wysocki 		return error;
1053f8055c3SRafael J. Wysocki 
1063f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
1073f8055c3SRafael J. Wysocki 	return 0;
1083f8055c3SRafael J. Wysocki }
1093f8055c3SRafael J. Wysocki 
110caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
111d22ddcbcSRafael J. Wysocki {
112d22ddcbcSRafael J. Wysocki 	struct acpi_device_physical_node *pn;
113d22ddcbcSRafael J. Wysocki 	bool offline = true;
11427664c58SLee, Chun-Yi 	char *envp[] = { "EVENT=offline", NULL };
115d22ddcbcSRafael J. Wysocki 
1164c533c80SRafael J. Wysocki 	/*
1174c533c80SRafael J. Wysocki 	 * acpi_container_offline() calls this for all of the container's
1184c533c80SRafael J. Wysocki 	 * children under the container's physical_node_lock lock.
1194c533c80SRafael J. Wysocki 	 */
1204c533c80SRafael J. Wysocki 	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
121d22ddcbcSRafael J. Wysocki 
122d22ddcbcSRafael J. Wysocki 	list_for_each_entry(pn, &adev->physical_node_list, node)
123d22ddcbcSRafael J. Wysocki 		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
124caa73ea1SRafael J. Wysocki 			if (uevent)
12527664c58SLee, Chun-Yi 				kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
126caa73ea1SRafael J. Wysocki 
127d22ddcbcSRafael J. Wysocki 			offline = false;
128d22ddcbcSRafael J. Wysocki 			break;
129d22ddcbcSRafael J. Wysocki 		}
130d22ddcbcSRafael J. Wysocki 
131d22ddcbcSRafael J. Wysocki 	mutex_unlock(&adev->physical_node_lock);
132d22ddcbcSRafael J. Wysocki 	return offline;
133d22ddcbcSRafael J. Wysocki }
134d22ddcbcSRafael J. Wysocki 
1357f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
1367f28ddecSRafael J. Wysocki 				    void **ret_p)
137683058e3SRafael J. Wysocki {
138683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
139683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
140303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
141683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
142683058e3SRafael J. Wysocki 
143683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
144683058e3SRafael J. Wysocki 		return AE_OK;
145683058e3SRafael J. Wysocki 
1467f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
1477f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
1487f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
1497f28ddecSRafael J. Wysocki 	}
1507f28ddecSRafael J. Wysocki 
151683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
152683058e3SRafael J. Wysocki 
153683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
154683058e3SRafael J. Wysocki 		int ret;
155683058e3SRafael J. Wysocki 
156303bfdb1SRafael J. Wysocki 		if (second_pass) {
157303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
158303bfdb1SRafael J. Wysocki 			if (pn->put_online)
159303bfdb1SRafael J. Wysocki 				continue;
160303bfdb1SRafael J. Wysocki 		} else {
161303bfdb1SRafael J. Wysocki 			pn->put_online = false;
162303bfdb1SRafael J. Wysocki 		}
163683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
164303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
165303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
166303bfdb1SRafael J. Wysocki 		} else {
167303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
168303bfdb1SRafael J. Wysocki 			if (second_pass) {
169683058e3SRafael J. Wysocki 				status = AE_ERROR;
170683058e3SRafael J. Wysocki 				break;
171683058e3SRafael J. Wysocki 			}
172303bfdb1SRafael J. Wysocki 		}
173683058e3SRafael J. Wysocki 	}
174683058e3SRafael J. Wysocki 
175683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
176683058e3SRafael J. Wysocki 
177683058e3SRafael J. Wysocki 	return status;
178683058e3SRafael J. Wysocki }
179683058e3SRafael J. Wysocki 
1807f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
1817f28ddecSRafael J. Wysocki 				   void **ret_p)
182683058e3SRafael J. Wysocki {
183683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
184683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
185683058e3SRafael J. Wysocki 
186683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
187683058e3SRafael J. Wysocki 		return AE_OK;
188683058e3SRafael J. Wysocki 
189683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
190683058e3SRafael J. Wysocki 
191683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
192683058e3SRafael J. Wysocki 		if (pn->put_online) {
193683058e3SRafael J. Wysocki 			device_online(pn->dev);
194683058e3SRafael J. Wysocki 			pn->put_online = false;
195683058e3SRafael J. Wysocki 		}
196683058e3SRafael J. Wysocki 
197683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
198683058e3SRafael J. Wysocki 
199683058e3SRafael J. Wysocki 	return AE_OK;
200683058e3SRafael J. Wysocki }
201683058e3SRafael J. Wysocki 
202d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device)
2031da177e4SLinus Torvalds {
2045993c467SYinghai Lu 	acpi_handle handle = device->handle;
205d22ddcbcSRafael J. Wysocki 	struct device *errdev = NULL;
206a33ec399SRafael J. Wysocki 	acpi_status status;
207f058cdf4SRafael J. Wysocki 
208303bfdb1SRafael J. Wysocki 	/*
209303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
210303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
211303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
212303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
213303bfdb1SRafael J. Wysocki 	 * block holds them.
214303bfdb1SRafael J. Wysocki 	 *
215303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
216303bfdb1SRafael J. Wysocki 	 */
2177f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2187f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
2197f28ddecSRafael J. Wysocki 				     (void **)&errdev);
2207f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
2217f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
222683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2237f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
2247f28ddecSRafael J. Wysocki 		return -EPERM;
2257f28ddecSRafael J. Wysocki 	}
2267f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
227303bfdb1SRafael J. Wysocki 	if (errdev) {
228303bfdb1SRafael J. Wysocki 		errdev = NULL;
229303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2307f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
2317f28ddecSRafael J. Wysocki 				    (void **)&errdev);
232ffc10d82SMichal Hocko 		if (!errdev)
2337f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
234303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
235303bfdb1SRafael J. Wysocki 
236ffc10d82SMichal Hocko 		if (errdev) {
237303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
2387f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
239303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
2407f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
2417f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
242683058e3SRafael J. Wysocki 			return -EBUSY;
243683058e3SRafael J. Wysocki 		}
244303bfdb1SRafael J. Wysocki 	}
245d22ddcbcSRafael J. Wysocki 	return 0;
246d22ddcbcSRafael J. Wysocki }
247d22ddcbcSRafael J. Wysocki 
248d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
249d22ddcbcSRafael J. Wysocki {
250d22ddcbcSRafael J. Wysocki 	acpi_handle handle = device->handle;
251d22ddcbcSRafael J. Wysocki 	unsigned long long sta;
252d22ddcbcSRafael J. Wysocki 	acpi_status status;
253d22ddcbcSRafael J. Wysocki 
254ffc10d82SMichal Hocko 	if (device->handler && device->handler->hotplug.demand_offline) {
255caa73ea1SRafael J. Wysocki 		if (!acpi_scan_is_offline(device, true))
256d22ddcbcSRafael J. Wysocki 			return -EBUSY;
257d22ddcbcSRafael J. Wysocki 	} else {
258d22ddcbcSRafael J. Wysocki 		int error = acpi_scan_try_to_offline(device);
259d22ddcbcSRafael J. Wysocki 		if (error)
260d22ddcbcSRafael J. Wysocki 			return error;
261d22ddcbcSRafael J. Wysocki 	}
262683058e3SRafael J. Wysocki 
263e52d9d8cSRafael J. Wysocki 	acpi_handle_debug(handle, "Ejecting\n");
26426d46867SZhang Rui 
265bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
266683058e3SRafael J. Wysocki 
2677d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2681da177e4SLinus Torvalds 	/*
2691da177e4SLinus Torvalds 	 * TBD: _EJD support.
2701da177e4SLinus Torvalds 	 */
2717d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2727d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
273a33ec399SRafael J. Wysocki 		return -ENODEV;
2747d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
275a33ec399SRafael J. Wysocki 		return -EIO;
276f058cdf4SRafael J. Wysocki 
277882fd12eSToshi Kani 	/*
278882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
279882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
280882fd12eSToshi Kani 	 */
281882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
282882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
283882fd12eSToshi Kani 		acpi_handle_warn(handle,
284882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
285882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
286882fd12eSToshi Kani 		acpi_handle_warn(handle,
287882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
288882fd12eSToshi Kani 	}
289882fd12eSToshi Kani 
290a33ec399SRafael J. Wysocki 	return 0;
291a33ec399SRafael J. Wysocki }
292f058cdf4SRafael J. Wysocki 
293443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev)
294443fc820SRafael J. Wysocki {
295443fc820SRafael J. Wysocki 	if (!acpi_device_enumerated(adev)) {
296443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Still not present\n");
297443fc820SRafael J. Wysocki 		return -EALREADY;
298443fc820SRafael J. Wysocki 	}
299443fc820SRafael J. Wysocki 	acpi_bus_trim(adev);
300443fc820SRafael J. Wysocki 	return 0;
301443fc820SRafael J. Wysocki }
302443fc820SRafael J. Wysocki 
303c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev)
304a33ec399SRafael J. Wysocki {
305f943db40SRafael J. Wysocki 	int error;
306a33ec399SRafael J. Wysocki 
307443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
308443fc820SRafael J. Wysocki 	if (adev->status.present || adev->status.functional) {
309443fc820SRafael J. Wysocki 		/*
310443fc820SRafael J. Wysocki 		 * This function is only called for device objects for which
311443fc820SRafael J. Wysocki 		 * matching scan handlers exist.  The only situation in which
312443fc820SRafael J. Wysocki 		 * the scan handler is not attached to this device object yet
313443fc820SRafael J. Wysocki 		 * is when the device has just appeared (either it wasn't
314443fc820SRafael J. Wysocki 		 * present at all before or it was removed and then added
315443fc820SRafael J. Wysocki 		 * again).
316443fc820SRafael J. Wysocki 		 */
317443fc820SRafael J. Wysocki 		if (adev->handler) {
318443fc820SRafael J. Wysocki 			dev_warn(&adev->dev, "Already enumerated\n");
319443fc820SRafael J. Wysocki 			return -EALREADY;
320443fc820SRafael J. Wysocki 		}
321c27b2c33SRafael J. Wysocki 		error = acpi_bus_scan(adev->handle);
322a33ec399SRafael J. Wysocki 		if (error) {
323c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Namespace scan failure\n");
324c27b2c33SRafael J. Wysocki 			return error;
325a33ec399SRafael J. Wysocki 		}
32646394fd0SRafael J. Wysocki 		if (!adev->handler) {
327c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Enumeration failure\n");
328443fc820SRafael J. Wysocki 			error = -ENODEV;
329443fc820SRafael J. Wysocki 		}
330443fc820SRafael J. Wysocki 	} else {
331443fc820SRafael J. Wysocki 		error = acpi_scan_device_not_present(adev);
332443fc820SRafael J. Wysocki 	}
333443fc820SRafael J. Wysocki 	return error;
334443fc820SRafael J. Wysocki }
335443fc820SRafael J. Wysocki 
336443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev)
337443fc820SRafael J. Wysocki {
338443fc820SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
339443fc820SRafael J. Wysocki 	struct acpi_device *child;
340443fc820SRafael J. Wysocki 	int error;
341443fc820SRafael J. Wysocki 
342443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
343443fc820SRafael J. Wysocki 	if (!(adev->status.present || adev->status.functional)) {
344443fc820SRafael J. Wysocki 		acpi_scan_device_not_present(adev);
345443fc820SRafael J. Wysocki 		return 0;
346443fc820SRafael J. Wysocki 	}
347443fc820SRafael J. Wysocki 	if (handler && handler->hotplug.scan_dependent)
348443fc820SRafael J. Wysocki 		return handler->hotplug.scan_dependent(adev);
349443fc820SRafael J. Wysocki 
350443fc820SRafael J. Wysocki 	error = acpi_bus_scan(adev->handle);
351443fc820SRafael J. Wysocki 	if (error) {
352443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Namespace scan failure\n");
353443fc820SRafael J. Wysocki 		return error;
354443fc820SRafael J. Wysocki 	}
355443fc820SRafael J. Wysocki 	list_for_each_entry(child, &adev->children, node) {
356443fc820SRafael J. Wysocki 		error = acpi_scan_bus_check(child);
357443fc820SRafael J. Wysocki 		if (error)
358443fc820SRafael J. Wysocki 			return error;
359a33ec399SRafael J. Wysocki 	}
360c27b2c33SRafael J. Wysocki 	return 0;
361c27b2c33SRafael J. Wysocki }
362c27b2c33SRafael J. Wysocki 
3633c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
3643c2cc7ffSRafael J. Wysocki {
3653c2cc7ffSRafael J. Wysocki 	switch (type) {
3663c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
3673c2cc7ffSRafael J. Wysocki 		return acpi_scan_bus_check(adev);
3683c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
3693c2cc7ffSRafael J. Wysocki 		return acpi_scan_device_check(adev);
3703c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
3713c2cc7ffSRafael J. Wysocki 	case ACPI_OST_EC_OSPM_EJECT:
372dd2151beSRafael J. Wysocki 		if (adev->handler && !adev->handler->hotplug.enabled) {
373dd2151beSRafael J. Wysocki 			dev_info(&adev->dev, "Eject disabled\n");
374dd2151beSRafael J. Wysocki 			return -EPERM;
375dd2151beSRafael J. Wysocki 		}
376700b8422SRafael J. Wysocki 		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
377dd2151beSRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3783c2cc7ffSRafael J. Wysocki 		return acpi_scan_hot_remove(adev);
3793c2cc7ffSRafael J. Wysocki 	}
3803c2cc7ffSRafael J. Wysocki 	return -EINVAL;
3813c2cc7ffSRafael J. Wysocki }
3823c2cc7ffSRafael J. Wysocki 
3831e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src)
384c27b2c33SRafael J. Wysocki {
385c27b2c33SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
3863c2cc7ffSRafael J. Wysocki 	int error = -ENODEV;
387c27b2c33SRafael J. Wysocki 
388c27b2c33SRafael J. Wysocki 	lock_device_hotplug();
389c27b2c33SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
390c27b2c33SRafael J. Wysocki 
391c27b2c33SRafael J. Wysocki 	/*
392c27b2c33SRafael J. Wysocki 	 * The device object's ACPI handle cannot become invalid as long as we
3933c2cc7ffSRafael J. Wysocki 	 * are holding acpi_scan_lock, but it might have become invalid before
394c27b2c33SRafael J. Wysocki 	 * that lock was acquired.
395c27b2c33SRafael J. Wysocki 	 */
396c27b2c33SRafael J. Wysocki 	if (adev->handle == INVALID_ACPI_HANDLE)
3973c2cc7ffSRafael J. Wysocki 		goto err_out;
398c27b2c33SRafael J. Wysocki 
3991e2380cdSRafael J. Wysocki 	if (adev->flags.is_dock_station) {
4001e2380cdSRafael J. Wysocki 		error = dock_notify(adev, src);
4011e2380cdSRafael J. Wysocki 	} else if (adev->flags.hotplug_notify) {
4023c2cc7ffSRafael J. Wysocki 		error = acpi_generic_hotplug_event(adev, src);
4033c2cc7ffSRafael J. Wysocki 	} else {
404be27b3dcSRafael J. Wysocki 		int (*notify)(struct acpi_device *, u32);
4053c2cc7ffSRafael J. Wysocki 
4063c2cc7ffSRafael J. Wysocki 		acpi_lock_hp_context();
407be27b3dcSRafael J. Wysocki 		notify = adev->hp ? adev->hp->notify : NULL;
4083c2cc7ffSRafael J. Wysocki 		acpi_unlock_hp_context();
4093c2cc7ffSRafael J. Wysocki 		/*
4103c2cc7ffSRafael J. Wysocki 		 * There may be additional notify handlers for device objects
4113c2cc7ffSRafael J. Wysocki 		 * without the .event() callback, so ignore them here.
4123c2cc7ffSRafael J. Wysocki 		 */
413be27b3dcSRafael J. Wysocki 		if (notify)
414be27b3dcSRafael J. Wysocki 			error = notify(adev, src);
4153c2cc7ffSRafael J. Wysocki 		else
4163c2cc7ffSRafael J. Wysocki 			goto out;
417c27b2c33SRafael J. Wysocki 	}
418d429e5c1SLee, Chun-Yi 	switch (error) {
419d429e5c1SLee, Chun-Yi 	case 0:
420a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_SUCCESS;
421d429e5c1SLee, Chun-Yi 		break;
422d429e5c1SLee, Chun-Yi 	case -EPERM:
423d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
424d429e5c1SLee, Chun-Yi 		break;
425d429e5c1SLee, Chun-Yi 	case -EBUSY:
426d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_DEVICE_BUSY;
427d429e5c1SLee, Chun-Yi 		break;
428d429e5c1SLee, Chun-Yi 	default:
429d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
430d429e5c1SLee, Chun-Yi 		break;
431d429e5c1SLee, Chun-Yi 	}
432a33ec399SRafael J. Wysocki 
4333c2cc7ffSRafael J. Wysocki  err_out:
434700b8422SRafael J. Wysocki 	acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
4353c2cc7ffSRafael J. Wysocki 
4363c2cc7ffSRafael J. Wysocki  out:
43778ea4639SRafael J. Wysocki 	acpi_bus_put_acpi_device(adev);
438a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
439e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
440a33ec399SRafael J. Wysocki }
441a33ec399SRafael J. Wysocki 
4420b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
4430b224527SRafael J. Wysocki {
4440b224527SRafael J. Wysocki 	int i;
4450b224527SRafael J. Wysocki 
446993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
447993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
448993cbe59SRafael J. Wysocki 
4491b1f3e16SRafael J. Wysocki 	if (!device->power.flags.power_resources)
4500b224527SRafael J. Wysocki 		return;
4510b224527SRafael J. Wysocki 
4520b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
4530b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
4540b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
4550b224527SRafael J. Wysocki 	}
4567f47fa6cSBjorn Helgaas }
4577f47fa6cSBjorn Helgaas 
4581890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
4591890a97aSPatrick Mochel {
4601890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4611890a97aSPatrick Mochel 
462ffdcd955SMika Westerberg 	acpi_free_properties(acpi_dev);
463c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
4640b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
4651890a97aSPatrick Mochel 	kfree(acpi_dev);
4661890a97aSPatrick Mochel }
4671890a97aSPatrick Mochel 
468d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device)
469caf5c03fSRafael J. Wysocki {
470ca9dc8d4SLukas Wunner 	struct acpi_device_bus_id *acpi_device_bus_id;
471ca9dc8d4SLukas Wunner 
472d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_lock);
473d783156eSRafael J. Wysocki 	if (device->parent)
474d783156eSRafael J. Wysocki 		list_del(&device->node);
475d783156eSRafael J. Wysocki 
476ca9dc8d4SLukas Wunner 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
477ca9dc8d4SLukas Wunner 		if (!strcmp(acpi_device_bus_id->bus_id,
478ca9dc8d4SLukas Wunner 			    acpi_device_hid(device))) {
479eb50aaf9SAndy Shevchenko 			ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
480eb50aaf9SAndy Shevchenko 			if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
481ca9dc8d4SLukas Wunner 				list_del(&acpi_device_bus_id->node);
482a58015d6SDexuan Cui 				kfree_const(acpi_device_bus_id->bus_id);
483ca9dc8d4SLukas Wunner 				kfree(acpi_device_bus_id);
484ca9dc8d4SLukas Wunner 			}
485ca9dc8d4SLukas Wunner 			break;
486ca9dc8d4SLukas Wunner 		}
487ca9dc8d4SLukas Wunner 
488d783156eSRafael J. Wysocki 	list_del(&device->wakeup_list);
489d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_lock);
490d783156eSRafael J. Wysocki 
491d783156eSRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
492d783156eSRafael J. Wysocki 	acpi_device_remove_files(device);
493d783156eSRafael J. Wysocki 	if (device->remove)
494d783156eSRafael J. Wysocki 		device->remove(device);
495d783156eSRafael J. Wysocki 
496d783156eSRafael J. Wysocki 	device_del(&device->dev);
497d783156eSRafael J. Wysocki }
498d783156eSRafael J. Wysocki 
49968bdb677SOctavian Purdila static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
50068bdb677SOctavian Purdila 
501d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list);
502d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock);
503d783156eSRafael J. Wysocki 
504d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used)
505d783156eSRafael J. Wysocki {
506d783156eSRafael J. Wysocki 	for (;;) {
507d783156eSRafael J. Wysocki 		struct acpi_device *adev;
508d783156eSRafael J. Wysocki 
509d783156eSRafael J. Wysocki 		mutex_lock(&acpi_device_del_lock);
510d783156eSRafael J. Wysocki 
511d783156eSRafael J. Wysocki 		if (list_empty(&acpi_device_del_list)) {
512d783156eSRafael J. Wysocki 			mutex_unlock(&acpi_device_del_lock);
513d783156eSRafael J. Wysocki 			break;
514d783156eSRafael J. Wysocki 		}
515d783156eSRafael J. Wysocki 		adev = list_first_entry(&acpi_device_del_list,
516d783156eSRafael J. Wysocki 					struct acpi_device, del_list);
517d783156eSRafael J. Wysocki 		list_del(&adev->del_list);
518d783156eSRafael J. Wysocki 
519d783156eSRafael J. Wysocki 		mutex_unlock(&acpi_device_del_lock);
520d783156eSRafael J. Wysocki 
52168bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
52268bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_REMOVE, adev);
52368bdb677SOctavian Purdila 
524d783156eSRafael J. Wysocki 		acpi_device_del(adev);
525d783156eSRafael J. Wysocki 		/*
526d783156eSRafael J. Wysocki 		 * Drop references to all power resources that might have been
527d783156eSRafael J. Wysocki 		 * used by the device.
528d783156eSRafael J. Wysocki 		 */
529d783156eSRafael J. Wysocki 		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
5304cbaba4eSAndy Shevchenko 		acpi_dev_put(adev);
531d783156eSRafael J. Wysocki 	}
532d783156eSRafael J. Wysocki }
533d783156eSRafael J. Wysocki 
534d783156eSRafael J. Wysocki /**
535d783156eSRafael J. Wysocki  * acpi_scan_drop_device - Drop an ACPI device object.
536d783156eSRafael J. Wysocki  * @handle: Handle of an ACPI namespace node, not used.
537d783156eSRafael J. Wysocki  * @context: Address of the ACPI device object to drop.
538d783156eSRafael J. Wysocki  *
539d783156eSRafael J. Wysocki  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
540d783156eSRafael J. Wysocki  * namespace node the device object pointed to by @context is attached to.
541d783156eSRafael J. Wysocki  *
542d783156eSRafael J. Wysocki  * The unregistration is carried out asynchronously to avoid running
543d783156eSRafael J. Wysocki  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
544d783156eSRafael J. Wysocki  * ensure the correct ordering (the device objects must be unregistered in the
545d783156eSRafael J. Wysocki  * same order in which the corresponding namespace nodes are deleted).
546d783156eSRafael J. Wysocki  */
547d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context)
548d783156eSRafael J. Wysocki {
549d783156eSRafael J. Wysocki 	static DECLARE_WORK(work, acpi_device_del_work_fn);
550d783156eSRafael J. Wysocki 	struct acpi_device *adev = context;
551d783156eSRafael J. Wysocki 
552d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_del_lock);
553d783156eSRafael J. Wysocki 
554d783156eSRafael J. Wysocki 	/*
555d783156eSRafael J. Wysocki 	 * Use the ACPI hotplug workqueue which is ordered, so this work item
556d783156eSRafael J. Wysocki 	 * won't run after any hotplug work items submitted subsequently.  That
557d783156eSRafael J. Wysocki 	 * prevents attempts to register device objects identical to those being
558d783156eSRafael J. Wysocki 	 * deleted from happening concurrently (such attempts result from
559d783156eSRafael J. Wysocki 	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
560935ab850STom Saeger 	 * run after all of the work items submitted previously, which helps
561d783156eSRafael J. Wysocki 	 * those work items to ensure that they are not accessing stale device
562d783156eSRafael J. Wysocki 	 * objects.
563d783156eSRafael J. Wysocki 	 */
564d783156eSRafael J. Wysocki 	if (list_empty(&acpi_device_del_list))
565d783156eSRafael J. Wysocki 		acpi_queue_hotplug_work(&work);
566d783156eSRafael J. Wysocki 
567d783156eSRafael J. Wysocki 	list_add_tail(&adev->del_list, &acpi_device_del_list);
568d783156eSRafael J. Wysocki 	/* Make acpi_ns_validate_handle() return NULL for this handle. */
569d783156eSRafael J. Wysocki 	adev->handle = INVALID_ACPI_HANDLE;
570d783156eSRafael J. Wysocki 
571d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_del_lock);
572caf5c03fSRafael J. Wysocki }
573caf5c03fSRafael J. Wysocki 
57483e2c8fcSRafael J. Wysocki static struct acpi_device *handle_to_device(acpi_handle handle,
57578ea4639SRafael J. Wysocki 					    void (*callback)(void *))
576caf5c03fSRafael J. Wysocki {
57783e2c8fcSRafael J. Wysocki 	struct acpi_device *adev = NULL;
578caf5c03fSRafael J. Wysocki 	acpi_status status;
579caf5c03fSRafael J. Wysocki 
58078ea4639SRafael J. Wysocki 	status = acpi_get_data_full(handle, acpi_scan_drop_device,
58183e2c8fcSRafael J. Wysocki 				    (void **)&adev, callback);
58283e2c8fcSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !adev) {
58383e2c8fcSRafael J. Wysocki 		acpi_handle_debug(handle, "No context!\n");
58483e2c8fcSRafael J. Wysocki 		return NULL;
585caf5c03fSRafael J. Wysocki 	}
58683e2c8fcSRafael J. Wysocki 	return adev;
587caf5c03fSRafael J. Wysocki }
58878ea4639SRafael J. Wysocki 
58978ea4639SRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
59078ea4639SRafael J. Wysocki {
59183e2c8fcSRafael J. Wysocki 	if (!device)
59283e2c8fcSRafael J. Wysocki 		return -EINVAL;
59383e2c8fcSRafael J. Wysocki 
59483e2c8fcSRafael J. Wysocki 	*device = handle_to_device(handle, NULL);
59583e2c8fcSRafael J. Wysocki 	if (!*device)
59683e2c8fcSRafael J. Wysocki 		return -ENODEV;
59783e2c8fcSRafael J. Wysocki 
59883e2c8fcSRafael J. Wysocki 	return 0;
59978ea4639SRafael J. Wysocki }
6006585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device);
601caf5c03fSRafael J. Wysocki 
60278ea4639SRafael J. Wysocki static void get_acpi_device(void *dev)
60378ea4639SRafael J. Wysocki {
6044cbaba4eSAndy Shevchenko 	acpi_dev_get(dev);
60578ea4639SRafael J. Wysocki }
60678ea4639SRafael J. Wysocki 
60778ea4639SRafael J. Wysocki struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
60878ea4639SRafael J. Wysocki {
60983e2c8fcSRafael J. Wysocki 	return handle_to_device(handle, get_acpi_device);
61078ea4639SRafael J. Wysocki }
6112c59a32dSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_get_acpi_device);
61278ea4639SRafael J. Wysocki 
613c1013ff7SRafael J. Wysocki static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
614c1013ff7SRafael J. Wysocki {
615c1013ff7SRafael J. Wysocki 	struct acpi_device_bus_id *acpi_device_bus_id;
616c1013ff7SRafael J. Wysocki 
617c1013ff7SRafael J. Wysocki 	/* Find suitable bus_id and instance number in acpi_bus_id_list. */
618c1013ff7SRafael J. Wysocki 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
619c1013ff7SRafael J. Wysocki 		if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
620c1013ff7SRafael J. Wysocki 			return acpi_device_bus_id;
621c1013ff7SRafael J. Wysocki 	}
622c1013ff7SRafael J. Wysocki 	return NULL;
623c1013ff7SRafael J. Wysocki }
624c1013ff7SRafael J. Wysocki 
625eb50aaf9SAndy Shevchenko static int acpi_device_set_name(struct acpi_device *device,
626eb50aaf9SAndy Shevchenko 				struct acpi_device_bus_id *acpi_device_bus_id)
627eb50aaf9SAndy Shevchenko {
628eb50aaf9SAndy Shevchenko 	struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
629eb50aaf9SAndy Shevchenko 	int result;
630eb50aaf9SAndy Shevchenko 
631eb50aaf9SAndy Shevchenko 	result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
632eb50aaf9SAndy Shevchenko 	if (result < 0)
633eb50aaf9SAndy Shevchenko 		return result;
634eb50aaf9SAndy Shevchenko 
635eb50aaf9SAndy Shevchenko 	device->pnp.instance_no = result;
636eb50aaf9SAndy Shevchenko 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
637eb50aaf9SAndy Shevchenko 	return 0;
638eb50aaf9SAndy Shevchenko }
639eb50aaf9SAndy Shevchenko 
640c6a493a1SRafael J. Wysocki static int acpi_tie_acpi_dev(struct acpi_device *adev)
641c6a493a1SRafael J. Wysocki {
642c6a493a1SRafael J. Wysocki 	acpi_handle handle = adev->handle;
643c6a493a1SRafael J. Wysocki 	acpi_status status;
644c6a493a1SRafael J. Wysocki 
645c6a493a1SRafael J. Wysocki 	if (!handle)
646c6a493a1SRafael J. Wysocki 		return 0;
647c6a493a1SRafael J. Wysocki 
648c6a493a1SRafael J. Wysocki 	status = acpi_attach_data(handle, acpi_scan_drop_device, adev);
649c6a493a1SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
650c6a493a1SRafael J. Wysocki 		acpi_handle_err(handle, "Unable to attach device data\n");
651c6a493a1SRafael J. Wysocki 		return -ENODEV;
652c6a493a1SRafael J. Wysocki 	}
653c6a493a1SRafael J. Wysocki 
654c6a493a1SRafael J. Wysocki 	return 0;
655c6a493a1SRafael J. Wysocki }
656c6a493a1SRafael J. Wysocki 
6575f4ce260SRafael J. Wysocki static int __acpi_device_add(struct acpi_device *device,
65882c7d5efSRafael J. Wysocki 			     void (*release)(struct device *))
6591da177e4SLinus Torvalds {
660c1013ff7SRafael J. Wysocki 	struct acpi_device_bus_id *acpi_device_bus_id;
661e49bd2ddSZhang Rui 	int result;
66266b7ed40SBjorn Helgaas 
6631da177e4SLinus Torvalds 	/*
6641da177e4SLinus Torvalds 	 * Linkage
6651da177e4SLinus Torvalds 	 * -------
6661da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
6671da177e4SLinus Torvalds 	 */
6681da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
6691da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
6701da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
6711033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
672d783156eSRafael J. Wysocki 	INIT_LIST_HEAD(&device->del_list);
6731033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
6741da177e4SLinus Torvalds 
6759090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
676c1013ff7SRafael J. Wysocki 
677c1013ff7SRafael J. Wysocki 	acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
678c1013ff7SRafael J. Wysocki 	if (acpi_device_bus_id) {
679eb50aaf9SAndy Shevchenko 		result = acpi_device_set_name(device, acpi_device_bus_id);
680eb50aaf9SAndy Shevchenko 		if (result)
681eb50aaf9SAndy Shevchenko 			goto err_unlock;
682c1013ff7SRafael J. Wysocki 	} else {
683c1013ff7SRafael J. Wysocki 		acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
684c1013ff7SRafael J. Wysocki 					     GFP_KERNEL);
685c1013ff7SRafael J. Wysocki 		if (!acpi_device_bus_id) {
686c1013ff7SRafael J. Wysocki 			result = -ENOMEM;
687c1013ff7SRafael J. Wysocki 			goto err_unlock;
688e49bd2ddSZhang Rui 		}
689a58015d6SDexuan Cui 		acpi_device_bus_id->bus_id =
690a58015d6SDexuan Cui 			kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
691a58015d6SDexuan Cui 		if (!acpi_device_bus_id->bus_id) {
692c1013ff7SRafael J. Wysocki 			kfree(acpi_device_bus_id);
693a58015d6SDexuan Cui 			result = -ENOMEM;
694c1013ff7SRafael J. Wysocki 			goto err_unlock;
695a58015d6SDexuan Cui 		}
696a58015d6SDexuan Cui 
697eb50aaf9SAndy Shevchenko 		ida_init(&acpi_device_bus_id->instance_ida);
698eb50aaf9SAndy Shevchenko 
699eb50aaf9SAndy Shevchenko 		result = acpi_device_set_name(device, acpi_device_bus_id);
700eb50aaf9SAndy Shevchenko 		if (result) {
7010c8bd174SChristophe JAILLET 			kfree_const(acpi_device_bus_id->bus_id);
702eb50aaf9SAndy Shevchenko 			kfree(acpi_device_bus_id);
703eb50aaf9SAndy Shevchenko 			goto err_unlock;
704eb50aaf9SAndy Shevchenko 		}
705eb50aaf9SAndy Shevchenko 
706e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
707e49bd2ddSZhang Rui 	}
708e49bd2ddSZhang Rui 
70933b57150SLen Brown 	if (device->parent)
7101da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
71133b57150SLen Brown 
7121da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
7131da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
7145e73c518SRafael J. Wysocki 
7159090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
7161da177e4SLinus Torvalds 
7171890a97aSPatrick Mochel 	if (device->parent)
71866b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
7195e73c518SRafael J. Wysocki 
7201890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
72182c7d5efSRafael J. Wysocki 	device->dev.release = release;
722cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
723e49bd2ddSZhang Rui 	if (result) {
7248b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
725d43e167dSRafael J. Wysocki 		goto err;
726e49bd2ddSZhang Rui 	}
727f883d9dbSPatrick Mochel 
728e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
729e49bd2ddSZhang Rui 	if (result)
7308acf4108SHanjun Guo 		pr_err("Error creating sysfs interface for device %s\n",
7310794469dSKay Sievers 		       dev_name(&device->dev));
732e49bd2ddSZhang Rui 
733e49bd2ddSZhang Rui 	return 0;
734d43e167dSRafael J. Wysocki 
735d43e167dSRafael J. Wysocki err:
7369090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
7375e73c518SRafael J. Wysocki 
73833b57150SLen Brown 	if (device->parent)
739e49bd2ddSZhang Rui 		list_del(&device->node);
7405e73c518SRafael J. Wysocki 
741e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
742a58015d6SDexuan Cui 
743c1013ff7SRafael J. Wysocki err_unlock:
7449090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
745d43e167dSRafael J. Wysocki 
746d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
7475e73c518SRafael J. Wysocki 
748e49bd2ddSZhang Rui 	return result;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
7515f4ce260SRafael J. Wysocki int acpi_device_add(struct acpi_device *adev, void (*release)(struct device *))
7525f4ce260SRafael J. Wysocki {
7535f4ce260SRafael J. Wysocki 	int ret;
7545f4ce260SRafael J. Wysocki 
7555f4ce260SRafael J. Wysocki 	ret = acpi_tie_acpi_dev(adev);
7565f4ce260SRafael J. Wysocki 	if (ret)
7575f4ce260SRafael J. Wysocki 		return ret;
7585f4ce260SRafael J. Wysocki 
7595f4ce260SRafael J. Wysocki 	return __acpi_device_add(adev, release);
7605f4ce260SRafael J. Wysocki }
7615f4ce260SRafael J. Wysocki 
7629e89dde2SZhang Rui /* --------------------------------------------------------------------------
7631da177e4SLinus Torvalds                                  Device Enumeration
7641da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
7652ef33ee7SRafael J. Wysocki static bool acpi_info_matches_ids(struct acpi_device_info *info,
7662ef33ee7SRafael J. Wysocki 				  const char * const ids[])
767637b9f1aSHans de Goede {
7682ef33ee7SRafael J. Wysocki 	struct acpi_pnp_device_id_list *cid_list = NULL;
76902050558SAndy Shevchenko 	int i, index;
770637b9f1aSHans de Goede 
771637b9f1aSHans de Goede 	if (!(info->valid & ACPI_VALID_HID))
772637b9f1aSHans de Goede 		return false;
773637b9f1aSHans de Goede 
77402050558SAndy Shevchenko 	index = match_string(ids, -1, info->hardware_id.string);
77502050558SAndy Shevchenko 	if (index >= 0)
77602050558SAndy Shevchenko 		return true;
77702050558SAndy Shevchenko 
7782ef33ee7SRafael J. Wysocki 	if (info->valid & ACPI_VALID_CID)
7792ef33ee7SRafael J. Wysocki 		cid_list = &info->compatible_id_list;
7802ef33ee7SRafael J. Wysocki 
7812ef33ee7SRafael J. Wysocki 	if (!cid_list)
78202050558SAndy Shevchenko 		return false;
7832ef33ee7SRafael J. Wysocki 
78402050558SAndy Shevchenko 	for (i = 0; i < cid_list->count; i++) {
78502050558SAndy Shevchenko 		index = match_string(ids, -1, cid_list->ids[i].string);
78602050558SAndy Shevchenko 		if (index >= 0)
7872ef33ee7SRafael J. Wysocki 			return true;
7882ef33ee7SRafael J. Wysocki 	}
789637b9f1aSHans de Goede 
790637b9f1aSHans de Goede 	return false;
791637b9f1aSHans de Goede }
792637b9f1aSHans de Goede 
793637b9f1aSHans de Goede /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
7942ef33ee7SRafael J. Wysocki static const char * const acpi_ignore_dep_ids[] = {
7952ef33ee7SRafael J. Wysocki 	"PNP0D80", /* Windows-compatible System Power Management Controller */
7969272e97aSHans de Goede 	"INT33BD", /* Intel Baytrail Mailbox Device */
797637b9f1aSHans de Goede 	NULL
798637b9f1aSHans de Goede };
799637b9f1aSHans de Goede 
8005c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
8015c478f49SBjorn Helgaas {
802456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
8035c478f49SBjorn Helgaas 	acpi_status status;
8045c478f49SBjorn Helgaas 
8055c478f49SBjorn Helgaas 	/*
8065c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
8075c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
8085c478f49SBjorn Helgaas 	 * to deal with them specially.
8095c478f49SBjorn Helgaas 	 */
810456de893SRafael J. Wysocki 	if (!handle)
8115c478f49SBjorn Helgaas 		return acpi_root;
8125c478f49SBjorn Helgaas 
8135c478f49SBjorn Helgaas 	do {
8145c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
8155c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
816456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
817456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
8185c478f49SBjorn Helgaas 	return device;
8195c478f49SBjorn Helgaas }
8205c478f49SBjorn Helgaas 
821c8f7a62cSLen Brown acpi_status
822c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
823c8f7a62cSLen Brown {
824c8f7a62cSLen Brown 	acpi_status status;
825c8f7a62cSLen Brown 	acpi_handle tmp;
826c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
827c8f7a62cSLen Brown 	union acpi_object *obj;
828c8f7a62cSLen Brown 
829c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
830c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
831c8f7a62cSLen Brown 		return status;
832c8f7a62cSLen Brown 
833c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
834c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
835c8f7a62cSLen Brown 		obj = buffer.pointer;
8363b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
8373b5fee59SHolger Macht 					 ejd);
838c8f7a62cSLen Brown 		kfree(buffer.pointer);
839c8f7a62cSLen Brown 	}
840c8f7a62cSLen Brown 	return status;
841c8f7a62cSLen Brown }
842c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
843c8f7a62cSLen Brown 
84440381a3cSBjorn Helgaas static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
8451da177e4SLinus Torvalds {
84640381a3cSBjorn Helgaas 	acpi_handle handle = dev->handle;
84740381a3cSBjorn Helgaas 	struct acpi_device_wakeup *wakeup = &dev->wakeup;
848b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
849b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
8501da177e4SLinus Torvalds 	union acpi_object *element = NULL;
851b581a7f9SRafael J. Wysocki 	acpi_status status;
852e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
8531da177e4SLinus Torvalds 
854993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
8551da177e4SLinus Torvalds 
856b581a7f9SRafael J. Wysocki 	/* _PRW */
857b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
858b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
859e52d9d8cSRafael J. Wysocki 		acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
860e52d9d8cSRafael J. Wysocki 				 acpi_format_exception(status));
861e88c9c60SRafael J. Wysocki 		return err;
862b581a7f9SRafael J. Wysocki 	}
863b581a7f9SRafael J. Wysocki 
864b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
865b581a7f9SRafael J. Wysocki 
866e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
867b581a7f9SRafael J. Wysocki 		goto out;
868b581a7f9SRafael J. Wysocki 
8691da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
870e88c9c60SRafael J. Wysocki 	if (!element)
871b581a7f9SRafael J. Wysocki 		goto out;
872e88c9c60SRafael J. Wysocki 
8731da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
8741da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
8751da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
8761da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
877e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
878b581a7f9SRafael J. Wysocki 			goto out;
879e88c9c60SRafael J. Wysocki 
880b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
8811da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
882b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
8831da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
8841da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
885b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
886b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
887b581a7f9SRafael J. Wysocki 	} else {
888b581a7f9SRafael J. Wysocki 		goto out;
889b581a7f9SRafael J. Wysocki 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
892e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
893b581a7f9SRafael J. Wysocki 		goto out;
894e88c9c60SRafael J. Wysocki 
895b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
8961da177e4SLinus Torvalds 
897e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
898e88c9c60SRafael J. Wysocki 	if (err)
899b581a7f9SRafael J. Wysocki 		goto out;
9001da177e4SLinus Torvalds 
9010596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
9020596a52bSRafael J. Wysocki 		int sleep_state;
9039874647bSRafael J. Wysocki 
904b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
905b5d667ebSRafael J. Wysocki 						  &sleep_state);
906b5d667ebSRafael J. Wysocki 		if (err) {
907b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
908b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
909b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
910b5d667ebSRafael J. Wysocki 			goto out;
911b5d667ebSRafael J. Wysocki 		}
9120596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
9130596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
9140596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
9150596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
9160596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
9170596a52bSRafael J. Wysocki 		}
9180596a52bSRafael J. Wysocki 	}
919b581a7f9SRafael J. Wysocki 
920b581a7f9SRafael J. Wysocki  out:
921b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
922e88c9c60SRafael J. Wysocki 	return err;
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
925a1a66393SRafael J. Wysocki static bool acpi_wakeup_gpe_init(struct acpi_device *device)
9261da177e4SLinus Torvalds {
9270519ade7SMathias Krause 	static const struct acpi_device_id button_device_ids[] = {
9285ceb5f05SBjorn Helgaas 		{"PNP0C0C", 0},		/* Power button */
9295ceb5f05SBjorn Helgaas 		{"PNP0C0D", 0},		/* Lid */
9305ceb5f05SBjorn Helgaas 		{"PNP0C0E", 0},		/* Sleep button */
93129b71a1cSThomas Renninger 		{"", 0},
93229b71a1cSThomas Renninger 	};
933bd9b2f9aSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &device->wakeup;
934f517709dSRafael J. Wysocki 	acpi_status status;
935f517709dSRafael J. Wysocki 
936bd9b2f9aSRafael J. Wysocki 	wakeup->flags.notifier_present = 0;
937f517709dSRafael J. Wysocki 
938f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
939f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
940b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
941b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
942bd9b2f9aSRafael J. Wysocki 			if (wakeup->sleep_state == ACPI_STATE_S5)
943bd9b2f9aSRafael J. Wysocki 				wakeup->sleep_state = ACPI_STATE_S4;
944b7e38304SZhang Rui 		}
945bd9b2f9aSRafael J. Wysocki 		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
946f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
947a1a66393SRafael J. Wysocki 		return true;
948f517709dSRafael J. Wysocki 	}
949f517709dSRafael J. Wysocki 
950a1a66393SRafael J. Wysocki 	status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
951bd9b2f9aSRafael J. Wysocki 					 wakeup->gpe_number);
952a1a66393SRafael J. Wysocki 	return ACPI_SUCCESS(status);
953f517709dSRafael J. Wysocki }
954f517709dSRafael J. Wysocki 
955d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
956f517709dSRafael J. Wysocki {
957e88c9c60SRafael J. Wysocki 	int err;
95829b71a1cSThomas Renninger 
959d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
960952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
961d57d09a4SRafael J. Wysocki 		return;
962d57d09a4SRafael J. Wysocki 
96340381a3cSBjorn Helgaas 	err = acpi_bus_extract_wakeup_device_power_package(device);
964e88c9c60SRafael J. Wysocki 	if (err) {
965e52d9d8cSRafael J. Wysocki 		dev_err(&device->dev, "Unable to extract wakeup power resources");
966d57d09a4SRafael J. Wysocki 		return;
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 
969a1a66393SRafael J. Wysocki 	device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
9709b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
971a1a66393SRafael J. Wysocki 	/*
972a1a66393SRafael J. Wysocki 	 * Call _PSW/_DSW object to disable its ability to wake the sleeping
973729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
974603fadf3SBjorn Helgaas 	 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
975729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
976729b2bdbSZhao Yakui 	 * present will the _PSW object used.
977729b2bdbSZhao Yakui 	 */
978e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
979e88c9c60SRafael J. Wysocki 	if (err)
98005de0686STian Tao 		pr_debug("error in _DSW or _PSW evaluation\n");
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
983f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
9841da177e4SLinus Torvalds {
985f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
986ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
987ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
988f33ce568SRafael J. Wysocki 	acpi_status status;
9899e89dde2SZhang Rui 
990f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
991f33ce568SRafael J. Wysocki 
992ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
993ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
994ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
995ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
996f33ce568SRafael J. Wysocki 
997ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
998ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
999956ad9d9SRafael J. Wysocki 		    && package->package.count)
1000956ad9d9SRafael J. Wysocki 			acpi_extract_power_resources(package, 0, &ps->resources);
1001956ad9d9SRafael J. Wysocki 
1002ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
1003f33ce568SRafael J. Wysocki 	}
1004f33ce568SRafael J. Wysocki 
1005f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1006ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
1007952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
1008f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1009f33ce568SRafael J. Wysocki 
101020dacb71SRafael J. Wysocki 	/* State is valid if there are means to put the device into it. */
101120dacb71SRafael J. Wysocki 	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
1012f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1013f33ce568SRafael J. Wysocki 
1014f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1015f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1016f33ce568SRafael J. Wysocki }
1017f33ce568SRafael J. Wysocki 
1018d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
10191da177e4SLinus Torvalds {
1020*b340c7d6SSakari Ailus 	unsigned long long dsc = ACPI_STATE_D0;
10218bc5053bSRafael J. Wysocki 	u32 i;
10221da177e4SLinus Torvalds 
1023d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1024952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
1025952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
1026d43e167dSRafael J. Wysocki 		return;
1027d43e167dSRafael J. Wysocki 
1028d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	/*
10319e89dde2SZhang Rui 	 * Power Management Flags
10321da177e4SLinus Torvalds 	 */
1033952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
10349e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
1035f25c0ae2SRafael J. Wysocki 
1036952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
10379e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
10381da177e4SLinus Torvalds 
1039f25c0ae2SRafael J. Wysocki 	if (acpi_has_method(device->handle, "_DSW"))
1040f25c0ae2SRafael J. Wysocki 		device->power.flags.dsw_present = 1;
1041f25c0ae2SRafael J. Wysocki 
1042*b340c7d6SSakari Ailus 	acpi_evaluate_integer(device->handle, "_DSC", NULL, &dsc);
1043*b340c7d6SSakari Ailus 	device->power.state_for_enumeration = dsc;
1044*b340c7d6SSakari Ailus 
10451da177e4SLinus Torvalds 	/*
10469e89dde2SZhang Rui 	 * Enumerate supported power management states
10471da177e4SLinus Torvalds 	 */
1048f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1049f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
10501da177e4SLinus Torvalds 
10510b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
10529e89dde2SZhang Rui 
1053956ad9d9SRafael J. Wysocki 	/* Set the defaults for D0 and D3hot (always supported). */
10549e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
10559e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
105620dacb71SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
10571399dfcdSAaron Lu 
1058956ad9d9SRafael J. Wysocki 	/*
1059956ad9d9SRafael J. Wysocki 	 * Use power resources only if the D0 list of them is populated, because
1060956ad9d9SRafael J. Wysocki 	 * some platforms may provide _PR3 only to indicate D3cold support and
1061956ad9d9SRafael J. Wysocki 	 * in those cases the power resources list returned by it may be bogus.
1062956ad9d9SRafael J. Wysocki 	 */
1063956ad9d9SRafael J. Wysocki 	if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1064956ad9d9SRafael J. Wysocki 		device->power.flags.power_resources = 1;
1065956ad9d9SRafael J. Wysocki 		/*
1066956ad9d9SRafael J. Wysocki 		 * D3cold is supported if the D3hot list of power resources is
1067956ad9d9SRafael J. Wysocki 		 * not empty.
1068956ad9d9SRafael J. Wysocki 		 */
1069956ad9d9SRafael J. Wysocki 		if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1070956ad9d9SRafael J. Wysocki 			device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1071956ad9d9SRafael J. Wysocki 	}
1072956ad9d9SRafael J. Wysocki 
10731b1f3e16SRafael J. Wysocki 	if (acpi_bus_init_power(device))
1074b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1075b3785492SRafael J. Wysocki }
1076c8f7a62cSLen Brown 
1077d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
1080952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
10811da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
1084952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
10851da177e4SLinus Torvalds 		device->flags.removable = 1;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1088952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
1089952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
10901da177e4SLinus Torvalds 		device->flags.ejectable = 1;
10911da177e4SLinus Torvalds }
10921da177e4SLinus Torvalds 
1093c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
10961da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
10971da177e4SLinus Torvalds 	int i = 0;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	/*
11001da177e4SLinus Torvalds 	 * Bus ID
11011da177e4SLinus Torvalds 	 * ------
11021da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
11031da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
11041da177e4SLinus Torvalds 	 */
1105859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
11061da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1107859ac9a4SBjorn Helgaas 		return;
1108859ac9a4SBjorn Helgaas 	}
1109859ac9a4SBjorn Helgaas 
1110859ac9a4SBjorn Helgaas 	switch (device->device_type) {
11111da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
11121da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
11131da177e4SLinus Torvalds 		break;
11141da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
11151da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
11161da177e4SLinus Torvalds 		break;
1117a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1118a64a62ceSLv Zheng 		strcpy(device->pnp.bus_id, "ECDT");
1119a64a62ceSLv Zheng 		break;
11201da177e4SLinus Torvalds 	default:
112166b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
11221da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
11231da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
11241da177e4SLinus Torvalds 			if (bus_id[i] == '_')
11251da177e4SLinus Torvalds 				bus_id[i] = '\0';
11261da177e4SLinus Torvalds 			else
11271da177e4SLinus Torvalds 				break;
11281da177e4SLinus Torvalds 		}
11291da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
11301da177e4SLinus Torvalds 		break;
11311da177e4SLinus Torvalds 	}
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
113454735266SZhang Rui /*
1135ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1136ebf4df8dSJiang Liu  *
1137ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1138ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1139ebf4df8dSJiang Liu  */
1140ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1141ebf4df8dSJiang Liu {
1142ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1143ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1144ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1145ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1146ebf4df8dSJiang Liu }
1147ebf4df8dSJiang Liu 
1148ebf4df8dSJiang Liu /*
1149d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
115054735266SZhang Rui  *
115154735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
115254735266SZhang Rui  * then we can safely call it an ejectable drive bay
115354735266SZhang Rui  */
1154ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1155d4e1a692SToshi Kani {
115654735266SZhang Rui 	acpi_handle phandle;
115754735266SZhang Rui 
1158952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1159ebf4df8dSJiang Liu 		return false;
1160ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1161ebf4df8dSJiang Liu 		return true;
1162ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1163ebf4df8dSJiang Liu 		return false;
116454735266SZhang Rui 
1165ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
116654735266SZhang Rui }
116754735266SZhang Rui 
1168b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev)
11691e2380cdSRafael J. Wysocki {
1170b43109faSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
11711e2380cdSRafael J. Wysocki 
1172b43109faSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list)
1173b43109faSRafael J. Wysocki 		if (!strcmp("PNP0C0A", hwid->id))
1174b43109faSRafael J. Wysocki 			return true;
1175b43109faSRafael J. Wysocki 
11761e2380cdSRafael J. Wysocki 	return false;
11771e2380cdSRafael J. Wysocki }
11781e2380cdSRafael J. Wysocki 
1179b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev)
11801e2380cdSRafael J. Wysocki {
1181b43109faSRafael J. Wysocki 	acpi_handle handle = adev->handle;
1182b43109faSRafael J. Wysocki 
1183b43109faSRafael J. Wysocki 	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
11841e2380cdSRafael J. Wysocki 		return true;
11851e2380cdSRafael J. Wysocki 
11861e2380cdSRafael J. Wysocki 	return acpi_bay_match(handle);
11871e2380cdSRafael J. Wysocki }
11881e2380cdSRafael J. Wysocki 
11893620f2f2SFrank Seidel /*
1190d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
11913620f2f2SFrank Seidel  */
1192ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
11933620f2f2SFrank Seidel {
1194ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
11953620f2f2SFrank Seidel }
11963620f2f2SFrank Seidel 
1197adc8bb8eSHans de Goede static acpi_status
1198adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1199adc8bb8eSHans de Goede 			  void **return_value)
1200adc8bb8eSHans de Goede {
1201adc8bb8eSHans de Goede 	long *cap = context;
1202adc8bb8eSHans de Goede 
1203adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_BCM") &&
1204adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_BCL")) {
1205e52d9d8cSRafael J. Wysocki 		acpi_handle_debug(handle, "Found generic backlight support\n");
1206adc8bb8eSHans de Goede 		*cap |= ACPI_VIDEO_BACKLIGHT;
1207adc8bb8eSHans de Goede 		/* We have backlight support, no need to scan further */
1208adc8bb8eSHans de Goede 		return AE_CTRL_TERMINATE;
1209adc8bb8eSHans de Goede 	}
1210adc8bb8eSHans de Goede 	return 0;
1211adc8bb8eSHans de Goede }
1212adc8bb8eSHans de Goede 
1213adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be
1214adc8bb8eSHans de Goede  * handled by video.ko.
1215adc8bb8eSHans de Goede  * The device will get a Linux specific CID added in scan.c to
1216adc8bb8eSHans de Goede  * identify the device as an ACPI graphics device
1217adc8bb8eSHans de Goede  * Be aware that the graphics device may not be physically present
1218adc8bb8eSHans de Goede  * Use acpi_video_get_capabilities() to detect general ACPI video
1219adc8bb8eSHans de Goede  * capabilities of present cards
1220adc8bb8eSHans de Goede  */
1221adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle)
1222adc8bb8eSHans de Goede {
1223adc8bb8eSHans de Goede 	long video_caps = 0;
1224adc8bb8eSHans de Goede 
1225adc8bb8eSHans de Goede 	/* Is this device able to support video switching ? */
1226adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1227adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1228adc8bb8eSHans de Goede 
1229adc8bb8eSHans de Goede 	/* Is this device able to retrieve a video ROM ? */
1230adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_ROM"))
1231adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1232adc8bb8eSHans de Goede 
1233adc8bb8eSHans de Goede 	/* Is this device able to configure which video head to be POSTed ? */
1234adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_VPO") &&
1235adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_GPD") &&
1236adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_SPD"))
1237adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1238adc8bb8eSHans de Goede 
1239adc8bb8eSHans de Goede 	/* Only check for backlight functionality if one of the above hit. */
1240adc8bb8eSHans de Goede 	if (video_caps)
1241adc8bb8eSHans de Goede 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1242adc8bb8eSHans de Goede 				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1243adc8bb8eSHans de Goede 				    &video_caps, NULL);
1244adc8bb8eSHans de Goede 
1245adc8bb8eSHans de Goede 	return video_caps;
1246adc8bb8eSHans de Goede }
1247adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device);
1248adc8bb8eSHans de Goede 
1249620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
125015b8dd53SBob Moore {
12517f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
125215b8dd53SBob Moore 
12532b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
12542b2ae7c7SThomas Renninger 		return dummy_hid;
12552b2ae7c7SThomas Renninger 
12567f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
12577f47fa6cSBjorn Helgaas 	return hid->id;
12587f47fa6cSBjorn Helgaas }
12597f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
126015b8dd53SBob Moore 
1261d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
12627f47fa6cSBjorn Helgaas {
12637f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
126415b8dd53SBob Moore 
12657f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
12667f47fa6cSBjorn Helgaas 	if (!id)
12677f47fa6cSBjorn Helgaas 		return;
126815b8dd53SBob Moore 
12696a0d12efSRasmus Villemoes 	id->id = kstrdup_const(dev_id, GFP_KERNEL);
12707f47fa6cSBjorn Helgaas 	if (!id->id) {
12717f47fa6cSBjorn Helgaas 		kfree(id);
12727f47fa6cSBjorn Helgaas 		return;
127315b8dd53SBob Moore 	}
127415b8dd53SBob Moore 
1275d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1276d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
127715b8dd53SBob Moore }
127815b8dd53SBob Moore 
1279222e82acSDarrick J. Wong /*
1280222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1281222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1282222e82acSDarrick J. Wong  * prefix.  Work around this.
1283222e82acSDarrick J. Wong  */
1284ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1285222e82acSDarrick J. Wong {
1286ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1287ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1288222e82acSDarrick J. Wong 
1289222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1290ebf4df8dSJiang Liu 		return false;
1291222e82acSDarrick J. Wong 
1292222e82acSDarrick J. Wong 	/* Look for SMBS object */
1293ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1294ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1295ebf4df8dSJiang Liu 		return false;
1296222e82acSDarrick J. Wong 
1297222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1298952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1299952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1300952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1301ebf4df8dSJiang Liu 		return true;
1302ebf4df8dSJiang Liu 
1303ebf4df8dSJiang Liu 	return false;
1304222e82acSDarrick J. Wong }
1305222e82acSDarrick J. Wong 
1306ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle)
1307ae3caa80SJiang Liu {
1308ae3caa80SJiang Liu 	acpi_handle tmp;
1309ae3caa80SJiang Liu 
1310ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1311ae3caa80SJiang Liu 	    tmp == handle)
1312ae3caa80SJiang Liu 		return true;
1313ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1314ae3caa80SJiang Liu 	    tmp == handle)
1315ae3caa80SJiang Liu 		return true;
1316ae3caa80SJiang Liu 
1317ae3caa80SJiang Liu 	return false;
1318ae3caa80SJiang Liu }
1319ae3caa80SJiang Liu 
1320c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1321c830dbcfSRafael J. Wysocki 			     int device_type)
13221da177e4SLinus Torvalds {
1323c830dbcfSRafael J. Wysocki 	struct acpi_device_info *info = NULL;
132478e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
13257f47fa6cSBjorn Helgaas 	int i;
13261da177e4SLinus Torvalds 
1327c0af4175SToshi Kani 	switch (device_type) {
13281da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1329c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1330c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1331859ac9a4SBjorn Helgaas 			break;
1332859ac9a4SBjorn Helgaas 		}
1333859ac9a4SBjorn Helgaas 
1334c830dbcfSRafael J. Wysocki 		acpi_get_object_info(handle, &info);
13356091b263SHans de Goede 		if (!info) {
13368acf4108SHanjun Guo 			pr_err("%s: Error reading device info\n", __func__);
13371da177e4SLinus Torvalds 			return;
13381da177e4SLinus Torvalds 		}
13391da177e4SLinus Torvalds 
1340549e6845SRafael J. Wysocki 		if (info->valid & ACPI_VALID_HID) {
1341c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
1342549e6845SRafael J. Wysocki 			pnp->type.platform_id = 1;
1343549e6845SRafael J. Wysocki 		}
134457f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
134515b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
134657f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1347c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
134857f3674fSBjorn Helgaas 		}
13491da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1350c0af4175SToshi Kani 			pnp->bus_address = info->address;
1351c0af4175SToshi Kani 			pnp->type.bus_address = 1;
13521da177e4SLinus Torvalds 		}
1353ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1354c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1355ccf78040SLv Zheng 							GFP_KERNEL);
135626095a01SSuthikulpanit, Suravee 		if (info->valid & ACPI_VALID_CLS)
135726095a01SSuthikulpanit, Suravee 			acpi_add_id(pnp, info->class_code.string);
1358ae843332SZhang Rui 
1359c830dbcfSRafael J. Wysocki 		kfree(info);
1360c830dbcfSRafael J. Wysocki 
136157f3674fSBjorn Helgaas 		/*
136257f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
136357f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
13643620f2f2SFrank Seidel 		 */
1365c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1366c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1367ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1368c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1369ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1370c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1371ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1372c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1373ae3caa80SJiang Liu 		else if (list_empty(&pnp->ids) &&
1374ae3caa80SJiang Liu 			 acpi_object_is_system_bus(handle)) {
1375ae3caa80SJiang Liu 			/* \_SB, \_TZ, LNXSYBUS */
1376ae3caa80SJiang Liu 			acpi_add_id(pnp, ACPI_BUS_HID);
1377c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1378c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1379b7b30de5SBjorn Helgaas 		}
138054735266SZhang Rui 
13811da177e4SLinus Torvalds 		break;
13821da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1383c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
13841da177e4SLinus Torvalds 		break;
13851da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1386c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
13871da177e4SLinus Torvalds 		break;
13881da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1389c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
13901da177e4SLinus Torvalds 		break;
13911da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1392c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
13931da177e4SLinus Torvalds 		break;
13941da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1395c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
13961da177e4SLinus Torvalds 		break;
1397a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1398a64a62ceSLv Zheng 		acpi_add_id(pnp, ACPI_ECDT_HID);
1399a64a62ceSLv Zheng 		break;
14001da177e4SLinus Torvalds 	}
14011da177e4SLinus Torvalds }
14021da177e4SLinus Torvalds 
1403c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1404c0af4175SToshi Kani {
1405c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1406c0af4175SToshi Kani 
1407c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
14086a0d12efSRasmus Villemoes 		kfree_const(id->id);
1409c0af4175SToshi Kani 		kfree(id);
1410c0af4175SToshi Kani 	}
1411c0af4175SToshi Kani 	kfree(pnp->unique_id);
1412c0af4175SToshi Kani }
1413c0af4175SToshi Kani 
1414b84f196dSSuthikulpanit, Suravee /**
1415b84f196dSSuthikulpanit, Suravee  * acpi_dma_supported - Check DMA support for the specified device.
1416b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1417b84f196dSSuthikulpanit, Suravee  *
1418b84f196dSSuthikulpanit, Suravee  * Return false if DMA is not supported. Otherwise, return true
1419b84f196dSSuthikulpanit, Suravee  */
14203d7c821cSAndy Shevchenko bool acpi_dma_supported(const struct acpi_device *adev)
1421b84f196dSSuthikulpanit, Suravee {
1422b84f196dSSuthikulpanit, Suravee 	if (!adev)
1423b84f196dSSuthikulpanit, Suravee 		return false;
1424b84f196dSSuthikulpanit, Suravee 
1425b84f196dSSuthikulpanit, Suravee 	if (adev->flags.cca_seen)
1426b84f196dSSuthikulpanit, Suravee 		return true;
1427b84f196dSSuthikulpanit, Suravee 
1428b84f196dSSuthikulpanit, Suravee 	/*
1429b84f196dSSuthikulpanit, Suravee 	* Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1430b84f196dSSuthikulpanit, Suravee 	* DMA on "Intel platforms".  Presumably that includes all x86 and
1431b84f196dSSuthikulpanit, Suravee 	* ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1432b84f196dSSuthikulpanit, Suravee 	*/
1433b84f196dSSuthikulpanit, Suravee 	if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1434b84f196dSSuthikulpanit, Suravee 		return true;
1435b84f196dSSuthikulpanit, Suravee 
1436b84f196dSSuthikulpanit, Suravee 	return false;
1437b84f196dSSuthikulpanit, Suravee }
1438b84f196dSSuthikulpanit, Suravee 
1439b84f196dSSuthikulpanit, Suravee /**
1440b84f196dSSuthikulpanit, Suravee  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1441b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1442b84f196dSSuthikulpanit, Suravee  *
1443b84f196dSSuthikulpanit, Suravee  * Return enum dev_dma_attr.
1444b84f196dSSuthikulpanit, Suravee  */
1445b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1446b84f196dSSuthikulpanit, Suravee {
1447b84f196dSSuthikulpanit, Suravee 	if (!acpi_dma_supported(adev))
1448b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NOT_SUPPORTED;
1449b84f196dSSuthikulpanit, Suravee 
1450b84f196dSSuthikulpanit, Suravee 	if (adev->flags.coherent_dma)
1451b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_COHERENT;
1452b84f196dSSuthikulpanit, Suravee 	else
1453b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NON_COHERENT;
1454b84f196dSSuthikulpanit, Suravee }
1455b84f196dSSuthikulpanit, Suravee 
1456d760a1baSLorenzo Pieralisi /**
1457c04ac679SLorenzo Pieralisi  * acpi_dma_get_range() - Get device DMA parameters.
1458c04ac679SLorenzo Pieralisi  *
1459c04ac679SLorenzo Pieralisi  * @dev: device to configure
1460c04ac679SLorenzo Pieralisi  * @dma_addr: pointer device DMA address result
1461c04ac679SLorenzo Pieralisi  * @offset: pointer to the DMA offset result
1462c04ac679SLorenzo Pieralisi  * @size: pointer to DMA range size result
1463c04ac679SLorenzo Pieralisi  *
1464c04ac679SLorenzo Pieralisi  * Evaluate DMA regions and return respectively DMA region start, offset
1465c04ac679SLorenzo Pieralisi  * and size in dma_addr, offset and size on parsing success; it does not
1466c04ac679SLorenzo Pieralisi  * update the passed in values on failure.
1467c04ac679SLorenzo Pieralisi  *
1468c04ac679SLorenzo Pieralisi  * Return 0 on success, < 0 on failure.
1469c04ac679SLorenzo Pieralisi  */
1470c04ac679SLorenzo Pieralisi int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1471c04ac679SLorenzo Pieralisi 		       u64 *size)
1472c04ac679SLorenzo Pieralisi {
1473c04ac679SLorenzo Pieralisi 	struct acpi_device *adev;
1474c04ac679SLorenzo Pieralisi 	LIST_HEAD(list);
1475c04ac679SLorenzo Pieralisi 	struct resource_entry *rentry;
1476c04ac679SLorenzo Pieralisi 	int ret;
1477c04ac679SLorenzo Pieralisi 	struct device *dma_dev = dev;
1478c04ac679SLorenzo Pieralisi 	u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1479c04ac679SLorenzo Pieralisi 
1480c04ac679SLorenzo Pieralisi 	/*
1481c04ac679SLorenzo Pieralisi 	 * Walk the device tree chasing an ACPI companion with a _DMA
1482c04ac679SLorenzo Pieralisi 	 * object while we go. Stop if we find a device with an ACPI
1483c04ac679SLorenzo Pieralisi 	 * companion containing a _DMA method.
1484c04ac679SLorenzo Pieralisi 	 */
1485c04ac679SLorenzo Pieralisi 	do {
1486c04ac679SLorenzo Pieralisi 		adev = ACPI_COMPANION(dma_dev);
1487c04ac679SLorenzo Pieralisi 		if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1488c04ac679SLorenzo Pieralisi 			break;
1489c04ac679SLorenzo Pieralisi 
1490c04ac679SLorenzo Pieralisi 		dma_dev = dma_dev->parent;
1491c04ac679SLorenzo Pieralisi 	} while (dma_dev);
1492c04ac679SLorenzo Pieralisi 
1493c04ac679SLorenzo Pieralisi 	if (!dma_dev)
1494c04ac679SLorenzo Pieralisi 		return -ENODEV;
1495c04ac679SLorenzo Pieralisi 
1496c04ac679SLorenzo Pieralisi 	if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1497c04ac679SLorenzo Pieralisi 		acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1498c04ac679SLorenzo Pieralisi 		return -EINVAL;
1499c04ac679SLorenzo Pieralisi 	}
1500c04ac679SLorenzo Pieralisi 
1501c04ac679SLorenzo Pieralisi 	ret = acpi_dev_get_dma_resources(adev, &list);
1502c04ac679SLorenzo Pieralisi 	if (ret > 0) {
1503c04ac679SLorenzo Pieralisi 		list_for_each_entry(rentry, &list, node) {
1504c04ac679SLorenzo Pieralisi 			if (dma_offset && rentry->offset != dma_offset) {
1505c04ac679SLorenzo Pieralisi 				ret = -EINVAL;
1506c04ac679SLorenzo Pieralisi 				dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1507c04ac679SLorenzo Pieralisi 				goto out;
1508c04ac679SLorenzo Pieralisi 			}
1509c04ac679SLorenzo Pieralisi 			dma_offset = rentry->offset;
1510c04ac679SLorenzo Pieralisi 
1511c04ac679SLorenzo Pieralisi 			/* Take lower and upper limits */
1512c04ac679SLorenzo Pieralisi 			if (rentry->res->start < dma_start)
1513c04ac679SLorenzo Pieralisi 				dma_start = rentry->res->start;
1514c04ac679SLorenzo Pieralisi 			if (rentry->res->end > dma_end)
1515c04ac679SLorenzo Pieralisi 				dma_end = rentry->res->end;
1516c04ac679SLorenzo Pieralisi 		}
1517c04ac679SLorenzo Pieralisi 
1518c04ac679SLorenzo Pieralisi 		if (dma_start >= dma_end) {
1519c04ac679SLorenzo Pieralisi 			ret = -EINVAL;
1520c04ac679SLorenzo Pieralisi 			dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1521c04ac679SLorenzo Pieralisi 			goto out;
1522c04ac679SLorenzo Pieralisi 		}
1523c04ac679SLorenzo Pieralisi 
1524c04ac679SLorenzo Pieralisi 		*dma_addr = dma_start - dma_offset;
1525c04ac679SLorenzo Pieralisi 		len = dma_end - dma_start;
1526c04ac679SLorenzo Pieralisi 		*size = max(len, len + 1);
1527c04ac679SLorenzo Pieralisi 		*offset = dma_offset;
1528c04ac679SLorenzo Pieralisi 	}
1529c04ac679SLorenzo Pieralisi  out:
1530c04ac679SLorenzo Pieralisi 	acpi_dev_free_resource_list(&list);
1531c04ac679SLorenzo Pieralisi 
1532c04ac679SLorenzo Pieralisi 	return ret >= 0 ? 0 : ret;
1533c04ac679SLorenzo Pieralisi }
1534c04ac679SLorenzo Pieralisi 
153511a8c5e3SJean-Philippe Brucker #ifdef CONFIG_IOMMU_API
153611a8c5e3SJean-Philippe Brucker int acpi_iommu_fwspec_init(struct device *dev, u32 id,
153711a8c5e3SJean-Philippe Brucker 			   struct fwnode_handle *fwnode,
153811a8c5e3SJean-Philippe Brucker 			   const struct iommu_ops *ops)
153911a8c5e3SJean-Philippe Brucker {
154011a8c5e3SJean-Philippe Brucker 	int ret = iommu_fwspec_init(dev, fwnode, ops);
154111a8c5e3SJean-Philippe Brucker 
154211a8c5e3SJean-Philippe Brucker 	if (!ret)
154311a8c5e3SJean-Philippe Brucker 		ret = iommu_fwspec_add_ids(dev, &id, 1);
154411a8c5e3SJean-Philippe Brucker 
154511a8c5e3SJean-Philippe Brucker 	return ret;
154611a8c5e3SJean-Philippe Brucker }
154711a8c5e3SJean-Philippe Brucker 
154811a8c5e3SJean-Philippe Brucker static inline const struct iommu_ops *acpi_iommu_fwspec_ops(struct device *dev)
154911a8c5e3SJean-Philippe Brucker {
155011a8c5e3SJean-Philippe Brucker 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
155111a8c5e3SJean-Philippe Brucker 
155211a8c5e3SJean-Philippe Brucker 	return fwspec ? fwspec->ops : NULL;
155311a8c5e3SJean-Philippe Brucker }
155411a8c5e3SJean-Philippe Brucker 
155511a8c5e3SJean-Philippe Brucker static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
155611a8c5e3SJean-Philippe Brucker 						       const u32 *id_in)
155711a8c5e3SJean-Philippe Brucker {
155811a8c5e3SJean-Philippe Brucker 	int err;
155911a8c5e3SJean-Philippe Brucker 	const struct iommu_ops *ops;
156011a8c5e3SJean-Philippe Brucker 
156111a8c5e3SJean-Philippe Brucker 	/*
156211a8c5e3SJean-Philippe Brucker 	 * If we already translated the fwspec there is nothing left to do,
156311a8c5e3SJean-Philippe Brucker 	 * return the iommu_ops.
156411a8c5e3SJean-Philippe Brucker 	 */
156511a8c5e3SJean-Philippe Brucker 	ops = acpi_iommu_fwspec_ops(dev);
156611a8c5e3SJean-Philippe Brucker 	if (ops)
156711a8c5e3SJean-Philippe Brucker 		return ops;
156811a8c5e3SJean-Philippe Brucker 
156911a8c5e3SJean-Philippe Brucker 	err = iort_iommu_configure_id(dev, id_in);
15703cf48554SJean-Philippe Brucker 	if (err && err != -EPROBE_DEFER)
15713cf48554SJean-Philippe Brucker 		err = viot_iommu_configure(dev);
157211a8c5e3SJean-Philippe Brucker 
157311a8c5e3SJean-Philippe Brucker 	/*
157411a8c5e3SJean-Philippe Brucker 	 * If we have reason to believe the IOMMU driver missed the initial
157511a8c5e3SJean-Philippe Brucker 	 * iommu_probe_device() call for dev, replay it to get things in order.
157611a8c5e3SJean-Philippe Brucker 	 */
157711a8c5e3SJean-Philippe Brucker 	if (!err && dev->bus && !device_iommu_mapped(dev))
157811a8c5e3SJean-Philippe Brucker 		err = iommu_probe_device(dev);
157911a8c5e3SJean-Philippe Brucker 
158011a8c5e3SJean-Philippe Brucker 	/* Ignore all other errors apart from EPROBE_DEFER */
158111a8c5e3SJean-Philippe Brucker 	if (err == -EPROBE_DEFER) {
158211a8c5e3SJean-Philippe Brucker 		return ERR_PTR(err);
158311a8c5e3SJean-Philippe Brucker 	} else if (err) {
158411a8c5e3SJean-Philippe Brucker 		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
158511a8c5e3SJean-Philippe Brucker 		return NULL;
158611a8c5e3SJean-Philippe Brucker 	}
158711a8c5e3SJean-Philippe Brucker 	return acpi_iommu_fwspec_ops(dev);
158811a8c5e3SJean-Philippe Brucker }
158911a8c5e3SJean-Philippe Brucker 
159011a8c5e3SJean-Philippe Brucker #else /* !CONFIG_IOMMU_API */
159111a8c5e3SJean-Philippe Brucker 
159211a8c5e3SJean-Philippe Brucker int acpi_iommu_fwspec_init(struct device *dev, u32 id,
159311a8c5e3SJean-Philippe Brucker 			   struct fwnode_handle *fwnode,
159411a8c5e3SJean-Philippe Brucker 			   const struct iommu_ops *ops)
159511a8c5e3SJean-Philippe Brucker {
159611a8c5e3SJean-Philippe Brucker 	return -ENODEV;
159711a8c5e3SJean-Philippe Brucker }
159811a8c5e3SJean-Philippe Brucker 
159911a8c5e3SJean-Philippe Brucker static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
160011a8c5e3SJean-Philippe Brucker 						       const u32 *id_in)
160111a8c5e3SJean-Philippe Brucker {
160211a8c5e3SJean-Philippe Brucker 	return NULL;
160311a8c5e3SJean-Philippe Brucker }
160411a8c5e3SJean-Philippe Brucker 
160511a8c5e3SJean-Philippe Brucker #endif /* !CONFIG_IOMMU_API */
160611a8c5e3SJean-Philippe Brucker 
1607c04ac679SLorenzo Pieralisi /**
1608c1e97359SJohn Garry  * acpi_dma_configure_id - Set-up DMA configuration for the device.
1609d760a1baSLorenzo Pieralisi  * @dev: The pointer to the device
1610d760a1baSLorenzo Pieralisi  * @attr: device dma attributes
1611b8e069a2SLorenzo Pieralisi  * @input_id: input device id const value pointer
1612d760a1baSLorenzo Pieralisi  */
1613b8e069a2SLorenzo Pieralisi int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1614b8e069a2SLorenzo Pieralisi 			  const u32 *input_id)
1615d760a1baSLorenzo Pieralisi {
1616643b8e4dSLorenzo Pieralisi 	const struct iommu_ops *iommu;
16177ad42639SLorenzo Pieralisi 	u64 dma_addr = 0, size = 0;
1618643b8e4dSLorenzo Pieralisi 
1619e5361ca2SRobin Murphy 	if (attr == DEV_DMA_NOT_SUPPORTED) {
1620e5361ca2SRobin Murphy 		set_dma_ops(dev, &dma_dummy_ops);
1621e5361ca2SRobin Murphy 		return 0;
1622e5361ca2SRobin Murphy 	}
1623e5361ca2SRobin Murphy 
1624db59e1b6SJean-Philippe Brucker 	acpi_arch_dma_setup(dev, &dma_addr, &size);
1625d760a1baSLorenzo Pieralisi 
162611a8c5e3SJean-Philippe Brucker 	iommu = acpi_iommu_configure_id(dev, input_id);
162745586c70SMasahiro Yamada 	if (PTR_ERR(iommu) == -EPROBE_DEFER)
1628058f8c3fSSricharan R 		return -EPROBE_DEFER;
1629643b8e4dSLorenzo Pieralisi 
16307ad42639SLorenzo Pieralisi 	arch_setup_dma_ops(dev, dma_addr, size,
16317ad42639SLorenzo Pieralisi 				iommu, attr == DEV_DMA_COHERENT);
16325a1bb638SSricharan R 
16335a1bb638SSricharan R 	return 0;
1634d760a1baSLorenzo Pieralisi }
1635b8e069a2SLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1636d760a1baSLorenzo Pieralisi 
1637d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev)
1638d0562674SSuthikulpanit, Suravee {
1639d0562674SSuthikulpanit, Suravee 	unsigned long long cca = 0;
1640d0562674SSuthikulpanit, Suravee 	acpi_status status;
1641d0562674SSuthikulpanit, Suravee 	struct acpi_device *parent = adev->parent;
1642d0562674SSuthikulpanit, Suravee 
1643d0562674SSuthikulpanit, Suravee 	if (parent && parent->flags.cca_seen) {
1644d0562674SSuthikulpanit, Suravee 		/*
1645d0562674SSuthikulpanit, Suravee 		 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1646d0562674SSuthikulpanit, Suravee 		 * already saw one.
1647d0562674SSuthikulpanit, Suravee 		 */
1648d0562674SSuthikulpanit, Suravee 		adev->flags.cca_seen = 1;
1649d0562674SSuthikulpanit, Suravee 		cca = parent->flags.coherent_dma;
1650d0562674SSuthikulpanit, Suravee 	} else {
1651d0562674SSuthikulpanit, Suravee 		status = acpi_evaluate_integer(adev->handle, "_CCA",
1652d0562674SSuthikulpanit, Suravee 					       NULL, &cca);
1653d0562674SSuthikulpanit, Suravee 		if (ACPI_SUCCESS(status))
1654d0562674SSuthikulpanit, Suravee 			adev->flags.cca_seen = 1;
1655d0562674SSuthikulpanit, Suravee 		else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1656d0562674SSuthikulpanit, Suravee 			/*
1657d0562674SSuthikulpanit, Suravee 			 * If architecture does not specify that _CCA is
1658d0562674SSuthikulpanit, Suravee 			 * required for DMA-able devices (e.g. x86),
1659d0562674SSuthikulpanit, Suravee 			 * we default to _CCA=1.
1660d0562674SSuthikulpanit, Suravee 			 */
1661d0562674SSuthikulpanit, Suravee 			cca = 1;
1662d0562674SSuthikulpanit, Suravee 		else
1663d0562674SSuthikulpanit, Suravee 			acpi_handle_debug(adev->handle,
1664d0562674SSuthikulpanit, Suravee 					  "ACPI device is missing _CCA.\n");
1665d0562674SSuthikulpanit, Suravee 	}
1666d0562674SSuthikulpanit, Suravee 
1667d0562674SSuthikulpanit, Suravee 	adev->flags.coherent_dma = cca;
1668d0562674SSuthikulpanit, Suravee }
1669d0562674SSuthikulpanit, Suravee 
1670e361d1f8SFrédéric Danis static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1671e4330d8bSJarkko Nikula {
1672e361d1f8SFrédéric Danis 	bool *is_serial_bus_slave_p = data;
1673e4330d8bSJarkko Nikula 
1674e4330d8bSJarkko Nikula 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1675e4330d8bSJarkko Nikula 		return 1;
1676e4330d8bSJarkko Nikula 
1677e361d1f8SFrédéric Danis 	*is_serial_bus_slave_p = true;
1678e4330d8bSJarkko Nikula 
1679e4330d8bSJarkko Nikula 	 /* no need to do more checking */
1680e4330d8bSJarkko Nikula 	return -1;
1681e4330d8bSJarkko Nikula }
1682e4330d8bSJarkko Nikula 
1683dfda4492SJohn Garry static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1684dfda4492SJohn Garry {
1685dfda4492SJohn Garry 	struct acpi_device *parent = device->parent;
1686a6f07295SJohn Garry 	static const struct acpi_device_id indirect_io_hosts[] = {
1687dfda4492SJohn Garry 		{"HISI0191", 0},
1688dfda4492SJohn Garry 		{}
1689dfda4492SJohn Garry 	};
1690dfda4492SJohn Garry 
1691dfda4492SJohn Garry 	return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1692dfda4492SJohn Garry }
1693dfda4492SJohn Garry 
1694d87fb091SJohn Garry static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1695e4330d8bSJarkko Nikula {
1696e4330d8bSJarkko Nikula 	struct list_head resource_list;
1697e361d1f8SFrédéric Danis 	bool is_serial_bus_slave = false;
1698aba94139SHans de Goede 	/*
1699aba94139SHans de Goede 	 * These devices have multiple I2cSerialBus resources and an i2c-client
1700aba94139SHans de Goede 	 * must be instantiated for each, each with its own i2c_device_id.
1701aba94139SHans de Goede 	 * Normally we only instantiate an i2c-client for the first resource,
1702aba94139SHans de Goede 	 * using the ACPI HID as id. These special cases are handled by the
1703aba94139SHans de Goede 	 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1704aba94139SHans de Goede 	 * which i2c_device_id to use for each resource.
1705aba94139SHans de Goede 	 */
1706aba94139SHans de Goede 	static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1707aba94139SHans de Goede 		{"BSG1160", },
170896f984d3SHans de Goede 		{"BSG2150", },
1709589edb56SHans de Goede 		{"INT33FE", },
1710a3dd034aSAndy Shevchenko 		{"INT3515", },
1711aba94139SHans de Goede 		{}
1712aba94139SHans de Goede 	};
1713e4330d8bSJarkko Nikula 
1714dfda4492SJohn Garry 	if (acpi_is_indirect_io_slave(device))
1715dfda4492SJohn Garry 		return true;
1716dfda4492SJohn Garry 
1717ca9ef3abSLukas Wunner 	/* Macs use device properties in lieu of _CRS resources */
1718ca9ef3abSLukas Wunner 	if (x86_apple_machine &&
1719ca9ef3abSLukas Wunner 	    (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1720e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "i2cAddress") ||
1721e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "baud")))
1722ca9ef3abSLukas Wunner 		return true;
1723ca9ef3abSLukas Wunner 
1724aba94139SHans de Goede 	/* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1725aba94139SHans de Goede 	if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1726aba94139SHans de Goede 		return false;
1727aba94139SHans de Goede 
1728e4330d8bSJarkko Nikula 	INIT_LIST_HEAD(&resource_list);
1729e361d1f8SFrédéric Danis 	acpi_dev_get_resources(device, &resource_list,
1730e361d1f8SFrédéric Danis 			       acpi_check_serial_bus_slave,
1731e361d1f8SFrédéric Danis 			       &is_serial_bus_slave);
1732e4330d8bSJarkko Nikula 	acpi_dev_free_resource_list(&resource_list);
1733e4330d8bSJarkko Nikula 
1734e361d1f8SFrédéric Danis 	return is_serial_bus_slave;
1735e4330d8bSJarkko Nikula }
1736e4330d8bSJarkko Nikula 
173782c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1738c830dbcfSRafael J. Wysocki 			     int type)
17391da177e4SLinus Torvalds {
1740d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1741d43e167dSRafael J. Wysocki 	device->device_type = type;
1742d43e167dSRafael J. Wysocki 	device->handle = handle;
1743d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
174401bb86b3SSaravana Kannan 	fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1745f5d9ab1dSRafael J. Wysocki 	acpi_set_device_status(device, ACPI_STA_DEFAULT);
1746d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1747c830dbcfSRafael J. Wysocki 	acpi_set_pnp_ids(handle, &device->pnp, type);
1748ffdcd955SMika Westerberg 	acpi_init_properties(device);
1749d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
17502c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1751202317a5SRafael J. Wysocki 	device->flags.initialized = true;
1752d87fb091SJohn Garry 	device->flags.enumeration_by_parent =
1753d87fb091SJohn Garry 		acpi_device_enumeration_by_parent(device);
175410c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(device);
1755cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1756cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1757d0562674SSuthikulpanit, Suravee 	acpi_init_coherency(device);
17586d279758SRafael J. Wysocki }
17596d279758SRafael J. Wysocki 
17606d279758SRafael J. Wysocki static void acpi_scan_dep_init(struct acpi_device *adev)
17616d279758SRafael J. Wysocki {
17626d279758SRafael J. Wysocki 	struct acpi_dep_data *dep;
17636d279758SRafael J. Wysocki 
17646d279758SRafael J. Wysocki 	list_for_each_entry(dep, &acpi_dep_list, node) {
17656d279758SRafael J. Wysocki 		if (dep->consumer == adev->handle)
17666d279758SRafael J. Wysocki 			adev->dep_unmet++;
17676d279758SRafael J. Wysocki 	}
1768cf860be6SRafael J. Wysocki }
1769bc3b0772SBjorn Helgaas 
1770cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1771cf860be6SRafael J. Wysocki {
1772cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1773cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
1776f5d9ab1dSRafael J. Wysocki static void acpi_scan_init_status(struct acpi_device *adev)
17771da177e4SLinus Torvalds {
1778f5d9ab1dSRafael J. Wysocki 	if (acpi_bus_get_status(adev))
1779f5d9ab1dSRafael J. Wysocki 		acpi_set_device_status(adev, 0);
1780f5d9ab1dSRafael J. Wysocki }
1781f5d9ab1dSRafael J. Wysocki 
17821da177e4SLinus Torvalds static int acpi_add_single_object(struct acpi_device **child,
17836d279758SRafael J. Wysocki 				  acpi_handle handle, int type, bool dep_init)
17841da177e4SLinus Torvalds {
1785e52d9d8cSRafael J. Wysocki 	struct acpi_device *device;
17865f4ce260SRafael J. Wysocki 	bool release_dep_lock = false;
1787e52d9d8cSRafael J. Wysocki 	int result;
17886091b263SHans de Goede 
178936bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1790c830dbcfSRafael J. Wysocki 	if (!device)
1791d550d98dSPatrick Mochel 		return -ENOMEM;
17921da177e4SLinus Torvalds 
1793c830dbcfSRafael J. Wysocki 	acpi_init_device_object(device, handle, type);
179463347db0SHans de Goede 	/*
1795f5d9ab1dSRafael J. Wysocki 	 * Getting the status is delayed till here so that we can call
1796f5d9ab1dSRafael J. Wysocki 	 * acpi_bus_get_status() and use its quirk handling.  Note that
1797f5d9ab1dSRafael J. Wysocki 	 * this must be done before the get power-/wakeup_dev-flags calls.
179863347db0SHans de Goede 	 */
17996d279758SRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
18005f4ce260SRafael J. Wysocki 		if (dep_init) {
18015f4ce260SRafael J. Wysocki 			mutex_lock(&acpi_dep_list_lock);
18025f4ce260SRafael J. Wysocki 			/*
18035f4ce260SRafael J. Wysocki 			 * Hold the lock until the acpi_tie_acpi_dev() call
18045f4ce260SRafael J. Wysocki 			 * below to prevent concurrent acpi_scan_clear_dep()
18055f4ce260SRafael J. Wysocki 			 * from deleting a dependency list entry without
18065f4ce260SRafael J. Wysocki 			 * updating dep_unmet for the device.
18075f4ce260SRafael J. Wysocki 			 */
18085f4ce260SRafael J. Wysocki 			release_dep_lock = true;
18096d279758SRafael J. Wysocki 			acpi_scan_dep_init(device);
18105f4ce260SRafael J. Wysocki 		}
1811f5d9ab1dSRafael J. Wysocki 		acpi_scan_init_status(device);
18126d279758SRafael J. Wysocki 	}
181363347db0SHans de Goede 
1814d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1815d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
18161da177e4SLinus Torvalds 
18175f4ce260SRafael J. Wysocki 	result = acpi_tie_acpi_dev(device);
18185f4ce260SRafael J. Wysocki 
18195f4ce260SRafael J. Wysocki 	if (release_dep_lock)
18205f4ce260SRafael J. Wysocki 		mutex_unlock(&acpi_dep_list_lock);
18215f4ce260SRafael J. Wysocki 
18225f4ce260SRafael J. Wysocki 	if (!result)
18235f4ce260SRafael J. Wysocki 		result = __acpi_device_add(device, acpi_device_release);
18245f4ce260SRafael J. Wysocki 
1825d43e167dSRafael J. Wysocki 	if (result) {
1826718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1827d550d98dSPatrick Mochel 		return result;
18281da177e4SLinus Torvalds 	}
18291da177e4SLinus Torvalds 
1830bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1831cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
1832e52d9d8cSRafael J. Wysocki 
1833e52d9d8cSRafael J. Wysocki 	acpi_handle_debug(handle, "Added as %s, parent %s\n",
1834e52d9d8cSRafael J. Wysocki 			  dev_name(&device->dev), device->parent ?
1835e52d9d8cSRafael J. Wysocki 				dev_name(&device->parent->dev) : "(null)");
1836e52d9d8cSRafael J. Wysocki 
18371da177e4SLinus Torvalds 	*child = device;
1838d43e167dSRafael J. Wysocki 	return 0;
1839bf325f95SRafael J. Wysocki }
1840bf325f95SRafael J. Wysocki 
1841a4e081b0SShannon Zhao static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1842a4e081b0SShannon Zhao 					    void *context)
1843a4e081b0SShannon Zhao {
1844a4e081b0SShannon Zhao 	struct resource *res = context;
1845a4e081b0SShannon Zhao 
1846a4e081b0SShannon Zhao 	if (acpi_dev_resource_memory(ares, res))
1847a4e081b0SShannon Zhao 		return AE_CTRL_TERMINATE;
1848a4e081b0SShannon Zhao 
1849a4e081b0SShannon Zhao 	return AE_OK;
1850a4e081b0SShannon Zhao }
1851a4e081b0SShannon Zhao 
1852a4e081b0SShannon Zhao static bool acpi_device_should_be_hidden(acpi_handle handle)
1853a4e081b0SShannon Zhao {
1854a4e081b0SShannon Zhao 	acpi_status status;
1855a4e081b0SShannon Zhao 	struct resource res;
1856a4e081b0SShannon Zhao 
1857a4e081b0SShannon Zhao 	/* Check if it should ignore the UART device */
1858a4e081b0SShannon Zhao 	if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1859a4e081b0SShannon Zhao 		return false;
1860a4e081b0SShannon Zhao 
1861a4e081b0SShannon Zhao 	/*
1862a4e081b0SShannon Zhao 	 * The UART device described in SPCR table is assumed to have only one
1863a4e081b0SShannon Zhao 	 * memory resource present. So we only look for the first one here.
1864a4e081b0SShannon Zhao 	 */
1865a4e081b0SShannon Zhao 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1866a4e081b0SShannon Zhao 				     acpi_get_resource_memory, &res);
1867a4e081b0SShannon Zhao 	if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1868a4e081b0SShannon Zhao 		return false;
1869a4e081b0SShannon Zhao 
1870a4e081b0SShannon Zhao 	acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1871a4e081b0SShannon Zhao 			 &res.start);
1872a4e081b0SShannon Zhao 
1873a4e081b0SShannon Zhao 	return true;
1874a4e081b0SShannon Zhao }
1875a4e081b0SShannon Zhao 
1876cde1f95fSSakari Ailus bool acpi_device_is_present(const struct acpi_device *adev)
1877202317a5SRafael J. Wysocki {
1878cde1f95fSSakari Ailus 	return adev->status.present || adev->status.functional;
1879202317a5SRafael J. Wysocki }
1880202317a5SRafael J. Wysocki 
18814b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1882844142c3SRasmus Villemoes 				       const char *idstr,
1883c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1884c5698074SRafael J. Wysocki {
1885c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1886c5698074SRafael J. Wysocki 
1887aca0a4ebSRafael J. Wysocki 	if (handler->match)
1888aca0a4ebSRafael J. Wysocki 		return handler->match(idstr, matchid);
1889aca0a4ebSRafael J. Wysocki 
1890c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1891c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1892c5698074SRafael J. Wysocki 			if (matchid)
1893c5698074SRafael J. Wysocki 				*matchid = devid;
1894c5698074SRafael J. Wysocki 
18954b59cc1fSRafael J. Wysocki 			return true;
18964b59cc1fSRafael J. Wysocki 		}
18974b59cc1fSRafael J. Wysocki 
18984b59cc1fSRafael J. Wysocki 	return false;
18994b59cc1fSRafael J. Wysocki }
19004b59cc1fSRafael J. Wysocki 
1901844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
19026b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
19036b772e8fSToshi Kani {
19046b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
19056b772e8fSToshi Kani 
19066b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
19076b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
19086b772e8fSToshi Kani 			return handler;
19096b772e8fSToshi Kani 
19106b772e8fSToshi Kani 	return NULL;
19116b772e8fSToshi Kani }
19126b772e8fSToshi Kani 
19133f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
19143f8055c3SRafael J. Wysocki {
19153f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
19163f8055c3SRafael J. Wysocki 		return;
19173f8055c3SRafael J. Wysocki 
19183f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
19193f8055c3SRafael J. Wysocki 
19203f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
19213f8055c3SRafael J. Wysocki 
19223f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
19233f8055c3SRafael J. Wysocki }
19243f8055c3SRafael J. Wysocki 
19253c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev)
19264b59cc1fSRafael J. Wysocki {
19276b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
19283c2cc7ffSRafael J. Wysocki 
1929b43109faSRafael J. Wysocki 	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
19301e2380cdSRafael J. Wysocki 		acpi_dock_add(adev);
19311e2380cdSRafael J. Wysocki 		return;
19321e2380cdSRafael J. Wysocki 	}
19333c2cc7ffSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
19344b59cc1fSRafael J. Wysocki 		struct acpi_scan_handler *handler;
19354b59cc1fSRafael J. Wysocki 
19366b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
19371a699476SRafael J. Wysocki 		if (handler) {
19383c2cc7ffSRafael J. Wysocki 			adev->flags.hotplug_notify = true;
19396b772e8fSToshi Kani 			break;
19406b772e8fSToshi Kani 		}
19416b772e8fSToshi Kani 	}
19421a699476SRafael J. Wysocki }
19436b772e8fSToshi Kani 
194402056a4fSRafael J. Wysocki static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
194540e7fcb1SLan Tianyu {
194640e7fcb1SLan Tianyu 	struct acpi_handle_list dep_devices;
194740e7fcb1SLan Tianyu 	acpi_status status;
19486fc25088SRafael J. Wysocki 	u32 count;
194940e7fcb1SLan Tianyu 	int i;
195040e7fcb1SLan Tianyu 
195171da201fSRafael J. Wysocki 	/*
195271da201fSRafael J. Wysocki 	 * Check for _HID here to avoid deferring the enumeration of:
195371da201fSRafael J. Wysocki 	 * 1. PCI devices.
195471da201fSRafael J. Wysocki 	 * 2. ACPI nodes describing USB ports.
195571da201fSRafael J. Wysocki 	 * Still, checking for _HID catches more then just these cases ...
195671da201fSRafael J. Wysocki 	 */
195702056a4fSRafael J. Wysocki 	if (!check_dep || !acpi_has_method(handle, "_DEP") ||
195802056a4fSRafael J. Wysocki 	    !acpi_has_method(handle, "_HID"))
19596fc25088SRafael J. Wysocki 		return 0;
196063347db0SHans de Goede 
19616fc25088SRafael J. Wysocki 	status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
196240e7fcb1SLan Tianyu 	if (ACPI_FAILURE(status)) {
19636fc25088SRafael J. Wysocki 		acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
19646fc25088SRafael J. Wysocki 		return 0;
196540e7fcb1SLan Tianyu 	}
196640e7fcb1SLan Tianyu 
19676fc25088SRafael J. Wysocki 	for (count = 0, i = 0; i < dep_devices.count; i++) {
196840e7fcb1SLan Tianyu 		struct acpi_device_info *info;
19696fc25088SRafael J. Wysocki 		struct acpi_dep_data *dep;
19706fc25088SRafael J. Wysocki 		bool skip;
197140e7fcb1SLan Tianyu 
197240e7fcb1SLan Tianyu 		status = acpi_get_object_info(dep_devices.handles[i], &info);
197340e7fcb1SLan Tianyu 		if (ACPI_FAILURE(status)) {
19746fc25088SRafael J. Wysocki 			acpi_handle_debug(handle, "Error reading _DEP device info\n");
197540e7fcb1SLan Tianyu 			continue;
197640e7fcb1SLan Tianyu 		}
197740e7fcb1SLan Tianyu 
19782ef33ee7SRafael J. Wysocki 		skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
197940e7fcb1SLan Tianyu 		kfree(info);
198040e7fcb1SLan Tianyu 
198140e7fcb1SLan Tianyu 		if (skip)
198240e7fcb1SLan Tianyu 			continue;
198340e7fcb1SLan Tianyu 
19846fc25088SRafael J. Wysocki 		dep = kzalloc(sizeof(*dep), GFP_KERNEL);
198540e7fcb1SLan Tianyu 		if (!dep)
19866fc25088SRafael J. Wysocki 			continue;
19876fc25088SRafael J. Wysocki 
19886fc25088SRafael J. Wysocki 		count++;
198940e7fcb1SLan Tianyu 
199091438aebSRafael J. Wysocki 		dep->supplier = dep_devices.handles[i];
19916fc25088SRafael J. Wysocki 		dep->consumer = handle;
199240e7fcb1SLan Tianyu 
199340e7fcb1SLan Tianyu 		mutex_lock(&acpi_dep_list_lock);
199440e7fcb1SLan Tianyu 		list_add_tail(&dep->node , &acpi_dep_list);
199540e7fcb1SLan Tianyu 		mutex_unlock(&acpi_dep_list_lock);
199640e7fcb1SLan Tianyu 	}
19976fc25088SRafael J. Wysocki 
19986fc25088SRafael J. Wysocki 	return count;
199940e7fcb1SLan Tianyu }
200040e7fcb1SLan Tianyu 
20010de7fb7cSRafael J. Wysocki static bool acpi_bus_scan_second_pass;
20020de7fb7cSRafael J. Wysocki 
200371da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
200471da201fSRafael J. Wysocki 				      struct acpi_device **adev_p)
20051da177e4SLinus Torvalds {
2006805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
2007e6c1067dSRafael J. Wysocki 	acpi_object_type acpi_type;
20086fc25088SRafael J. Wysocki 	int type;
20091da177e4SLinus Torvalds 
20104002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
20114002bf38SRafael J. Wysocki 	if (device)
20124002bf38SRafael J. Wysocki 		goto out;
20134002bf38SRafael J. Wysocki 
2014e6c1067dSRafael J. Wysocki 	if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
201551a85fafSBjorn Helgaas 		return AE_OK;
20161da177e4SLinus Torvalds 
2017e6c1067dSRafael J. Wysocki 	switch (acpi_type) {
2018e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_DEVICE:
2019e6c1067dSRafael J. Wysocki 		if (acpi_device_should_be_hidden(handle))
202082c7d5efSRafael J. Wysocki 			return AE_OK;
202182c7d5efSRafael J. Wysocki 
202202056a4fSRafael J. Wysocki 		/* Bail out if there are dependencies. */
202302056a4fSRafael J. Wysocki 		if (acpi_scan_check_dep(handle, check_dep) > 0) {
20240de7fb7cSRafael J. Wysocki 			acpi_bus_scan_second_pass = true;
202571da201fSRafael J. Wysocki 			return AE_CTRL_DEPTH;
202671da201fSRafael J. Wysocki 		}
202702056a4fSRafael J. Wysocki 
2028e6c1067dSRafael J. Wysocki 		fallthrough;
2029e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_ANY:	/* for ACPI_ROOT_OBJECT */
2030e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_DEVICE;
2031e6c1067dSRafael J. Wysocki 		break;
2032e6c1067dSRafael J. Wysocki 
2033e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_PROCESSOR:
2034e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_PROCESSOR;
2035e6c1067dSRafael J. Wysocki 		break;
2036e6c1067dSRafael J. Wysocki 
2037e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_THERMAL:
2038e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_THERMAL;
2039e6c1067dSRafael J. Wysocki 		break;
2040e6c1067dSRafael J. Wysocki 
2041e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_POWER:
204202056a4fSRafael J. Wysocki 		acpi_add_power_resource(handle);
204302056a4fSRafael J. Wysocki 		fallthrough;
2044e6c1067dSRafael J. Wysocki 	default:
2045e6c1067dSRafael J. Wysocki 		return AE_OK;
20460de7fb7cSRafael J. Wysocki 	}
20476fc25088SRafael J. Wysocki 
20483e759425SHans de Goede 	/*
20493e759425SHans de Goede 	 * If check_dep is true at this point, the device has no dependencies,
20503e759425SHans de Goede 	 * or the creation of the device object would have been postponed above.
20513e759425SHans de Goede 	 */
20526d279758SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, !check_dep);
20536d279758SRafael J. Wysocki 	if (!device)
20546d279758SRafael J. Wysocki 		return AE_CTRL_DEPTH;
20556d279758SRafael J. Wysocki 
20566d279758SRafael J. Wysocki 	acpi_scan_init_hotplug(device);
20573c2cc7ffSRafael J. Wysocki 
20584002bf38SRafael J. Wysocki out:
205971da201fSRafael J. Wysocki 	if (!*adev_p)
206071da201fSRafael J. Wysocki 		*adev_p = device;
2061805d410fSRafael J. Wysocki 
206251a85fafSBjorn Helgaas 	return AE_OK;
20631da177e4SLinus Torvalds }
20641da177e4SLinus Torvalds 
206571da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
206671da201fSRafael J. Wysocki 					void *not_used, void **ret_p)
206771da201fSRafael J. Wysocki {
206871da201fSRafael J. Wysocki 	return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
206971da201fSRafael J. Wysocki }
207071da201fSRafael J. Wysocki 
207171da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
207271da201fSRafael J. Wysocki 					void *not_used, void **ret_p)
207371da201fSRafael J. Wysocki {
207471da201fSRafael J. Wysocki 	return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
207571da201fSRafael J. Wysocki }
207671da201fSRafael J. Wysocki 
207748459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device)
207848459340SZhang Rui {
207948459340SZhang Rui 	/*
2080d87fb091SJohn Garry 	 * Do not enumerate devices with enumeration_by_parent flag set as
2081d87fb091SJohn Garry 	 * they will be enumerated by their respective parents.
208248459340SZhang Rui 	 */
2083d87fb091SJohn Garry 	if (!device->flags.enumeration_by_parent) {
20841571875bSHeikki Krogerus 		acpi_create_platform_device(device, NULL);
208510c7e20bSOctavian Purdila 		acpi_device_set_enumerated(device);
208668bdb677SOctavian Purdila 	} else {
208768bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
208868bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_ADD, device);
208910c7e20bSOctavian Purdila 	}
209048459340SZhang Rui }
209148459340SZhang Rui 
20927d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = {
2093ee892094SRafael J. Wysocki 	{ACPI_DT_NAMESPACE_HID, },
20947d284352SRafael J. Wysocki 	{"", },
20957d284352SRafael J. Wysocki };
20967d284352SRafael J. Wysocki 
20977d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev,
20987d284352SRafael J. Wysocki 				      const struct acpi_device_id *not_used)
20997d284352SRafael J. Wysocki {
21007d284352SRafael J. Wysocki 	/*
2101ee892094SRafael J. Wysocki 	 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2102ee892094SRafael J. Wysocki 	 * below can be unconditional.
21037d284352SRafael J. Wysocki 	 */
21047d284352SRafael J. Wysocki 	if (adev->data.of_compatible)
21057d284352SRafael J. Wysocki 		acpi_default_enumeration(adev);
21067d284352SRafael J. Wysocki 
21077d284352SRafael J. Wysocki 	return 1;
21087d284352SRafael J. Wysocki }
21097d284352SRafael J. Wysocki 
21107d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = {
21117d284352SRafael J. Wysocki 	.ids = generic_device_ids,
21127d284352SRafael J. Wysocki 	.attach = acpi_generic_device_attach,
21137d284352SRafael J. Wysocki };
21147d284352SRafael J. Wysocki 
211587b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
211687b85b3cSRafael J. Wysocki {
211787b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
211887b85b3cSRafael J. Wysocki 	int ret = 0;
211987b85b3cSRafael J. Wysocki 
212087b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
2121c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
2122c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
212387b85b3cSRafael J. Wysocki 
2124c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
2125c5698074SRafael J. Wysocki 		if (handler) {
2126d34afa9dSRafael J. Wysocki 			if (!handler->attach) {
2127d34afa9dSRafael J. Wysocki 				device->pnp.type.platform_id = 0;
2128d34afa9dSRafael J. Wysocki 				continue;
2129d34afa9dSRafael J. Wysocki 			}
2130c5698074SRafael J. Wysocki 			device->handler = handler;
21319cb32acfSRafael J. Wysocki 			ret = handler->attach(device, devid);
21329cb32acfSRafael J. Wysocki 			if (ret > 0)
2133c5698074SRafael J. Wysocki 				break;
21349cb32acfSRafael J. Wysocki 
21359cb32acfSRafael J. Wysocki 			device->handler = NULL;
21369cb32acfSRafael J. Wysocki 			if (ret < 0)
2137c5698074SRafael J. Wysocki 				break;
2138c5698074SRafael J. Wysocki 		}
2139c5698074SRafael J. Wysocki 	}
214048459340SZhang Rui 
2141ca589f94SRafael J. Wysocki 	return ret;
2142ca589f94SRafael J. Wysocki }
2143ca589f94SRafael J. Wysocki 
214471da201fSRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
214551a85fafSBjorn Helgaas {
21462c22e652SRafael J. Wysocki 	struct acpi_device *child;
214771da201fSRafael J. Wysocki 	bool skip = !first_pass && device->flags.visited;
21481e2380cdSRafael J. Wysocki 	acpi_handle ejd;
2149ca589f94SRafael J. Wysocki 	int ret;
21501da177e4SLinus Torvalds 
215171da201fSRafael J. Wysocki 	if (skip)
215271da201fSRafael J. Wysocki 		goto ok;
215371da201fSRafael J. Wysocki 
21541e2380cdSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
21551e2380cdSRafael J. Wysocki 		register_dock_dependent_device(device, ejd);
21561e2380cdSRafael J. Wysocki 
21572c22e652SRafael J. Wysocki 	acpi_bus_get_status(device);
2158202317a5SRafael J. Wysocki 	/* Skip devices that are not present. */
21592c22e652SRafael J. Wysocki 	if (!acpi_device_is_present(device)) {
2160cde1f95fSSakari Ailus 		device->flags.initialized = false;
216110c7e20bSOctavian Purdila 		acpi_device_clear_enumerated(device);
21621b1f3e16SRafael J. Wysocki 		device->flags.power_manageable = 0;
21632c22e652SRafael J. Wysocki 		return;
21642c22e652SRafael J. Wysocki 	}
21653a391a39SRafael J. Wysocki 	if (device->handler)
21662c22e652SRafael J. Wysocki 		goto ok;
21673a391a39SRafael J. Wysocki 
2168202317a5SRafael J. Wysocki 	if (!device->flags.initialized) {
21691b1f3e16SRafael J. Wysocki 		device->flags.power_manageable =
21701b1f3e16SRafael J. Wysocki 			device->power.states[ACPI_STATE_D0].flags.valid;
21711b1f3e16SRafael J. Wysocki 		if (acpi_bus_init_power(device))
21721b1f3e16SRafael J. Wysocki 			device->flags.power_manageable = 0;
21731b1f3e16SRafael J. Wysocki 
2174202317a5SRafael J. Wysocki 		device->flags.initialized = true;
2175c381fc3aSRafael J. Wysocki 	} else if (device->flags.visited) {
2176c381fc3aSRafael J. Wysocki 		goto ok;
2177202317a5SRafael J. Wysocki 	}
217810c7e20bSOctavian Purdila 
2179ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
21806931007cSRafael J. Wysocki 	if (ret < 0)
21812c22e652SRafael J. Wysocki 		return;
21826931007cSRafael J. Wysocki 
21836931007cSRafael J. Wysocki 	device->flags.match_driver = true;
2184d87fb091SJohn Garry 	if (ret > 0 && !device->flags.enumeration_by_parent) {
2185f406270bSRafael J. Wysocki 		acpi_device_set_enumerated(device);
2186f406270bSRafael J. Wysocki 		goto ok;
2187f406270bSRafael J. Wysocki 	}
2188f406270bSRafael J. Wysocki 
2189ca589f94SRafael J. Wysocki 	ret = device_attach(&device->dev);
2190202317a5SRafael J. Wysocki 	if (ret < 0)
21912c22e652SRafael J. Wysocki 		return;
21927d284352SRafael J. Wysocki 
2193d87fb091SJohn Garry 	if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2194e4330d8bSJarkko Nikula 		acpi_default_enumeration(device);
2195d87fb091SJohn Garry 	else
2196d87fb091SJohn Garry 		acpi_device_set_enumerated(device);
2197202317a5SRafael J. Wysocki 
2198202317a5SRafael J. Wysocki  ok:
21992c22e652SRafael J. Wysocki 	list_for_each_entry(child, &device->children, node)
220071da201fSRafael J. Wysocki 		acpi_bus_attach(child, first_pass);
22018ab17fc9SRafael J. Wysocki 
220271da201fSRafael J. Wysocki 	if (!skip && device->handler && device->handler->hotplug.notify_online)
22038ab17fc9SRafael J. Wysocki 		device->handler->hotplug.notify_online(device);
2204805d410fSRafael J. Wysocki }
22053fb02738SRajesh Shah 
2206b83e2b30SDaniel Scally static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
220740e7fcb1SLan Tianyu {
220840e7fcb1SLan Tianyu 	struct acpi_device *adev;
220940e7fcb1SLan Tianyu 
2210b83e2b30SDaniel Scally 	adev = acpi_bus_get_acpi_device(dep->consumer);
2211ad4d451eSRafael J. Wysocki 	if (adev) {
2212b83e2b30SDaniel Scally 		*(struct acpi_device **)data = adev;
2213b83e2b30SDaniel Scally 		return 1;
2214b83e2b30SDaniel Scally 	}
2215ad4d451eSRafael J. Wysocki 	/* Continue parsing if the device object is not present. */
2216ad4d451eSRafael J. Wysocki 	return 0;
2217ad4d451eSRafael J. Wysocki }
2218b83e2b30SDaniel Scally 
2219dc612486SRafael J. Wysocki struct acpi_scan_clear_dep_work {
2220dc612486SRafael J. Wysocki 	struct work_struct work;
2221dc612486SRafael J. Wysocki 	struct acpi_device *adev;
2222dc612486SRafael J. Wysocki };
2223dc612486SRafael J. Wysocki 
2224dc612486SRafael J. Wysocki static void acpi_scan_clear_dep_fn(struct work_struct *work)
2225dc612486SRafael J. Wysocki {
2226dc612486SRafael J. Wysocki 	struct acpi_scan_clear_dep_work *cdw;
2227dc612486SRafael J. Wysocki 
2228dc612486SRafael J. Wysocki 	cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
2229dc612486SRafael J. Wysocki 
2230dc612486SRafael J. Wysocki 	acpi_scan_lock_acquire();
2231dc612486SRafael J. Wysocki 	acpi_bus_attach(cdw->adev, true);
2232dc612486SRafael J. Wysocki 	acpi_scan_lock_release();
2233dc612486SRafael J. Wysocki 
2234dc612486SRafael J. Wysocki 	acpi_dev_put(cdw->adev);
2235dc612486SRafael J. Wysocki 	kfree(cdw);
2236dc612486SRafael J. Wysocki }
2237dc612486SRafael J. Wysocki 
2238dc612486SRafael J. Wysocki static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2239dc612486SRafael J. Wysocki {
2240dc612486SRafael J. Wysocki 	struct acpi_scan_clear_dep_work *cdw;
2241dc612486SRafael J. Wysocki 
2242dc612486SRafael J. Wysocki 	if (adev->dep_unmet)
2243dc612486SRafael J. Wysocki 		return false;
2244dc612486SRafael J. Wysocki 
2245dc612486SRafael J. Wysocki 	cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2246dc612486SRafael J. Wysocki 	if (!cdw)
2247dc612486SRafael J. Wysocki 		return false;
2248dc612486SRafael J. Wysocki 
2249dc612486SRafael J. Wysocki 	cdw->adev = adev;
2250dc612486SRafael J. Wysocki 	INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2251dc612486SRafael J. Wysocki 	/*
2252dc612486SRafael J. Wysocki 	 * Since the work function may block on the lock until the entire
2253dc612486SRafael J. Wysocki 	 * initial enumeration of devices is complete, put it into the unbound
2254dc612486SRafael J. Wysocki 	 * workqueue.
2255dc612486SRafael J. Wysocki 	 */
2256dc612486SRafael J. Wysocki 	queue_work(system_unbound_wq, &cdw->work);
2257dc612486SRafael J. Wysocki 
2258dc612486SRafael J. Wysocki 	return true;
2259dc612486SRafael J. Wysocki }
2260dc612486SRafael J. Wysocki 
2261a9e10e58SDaniel Scally static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
226240e7fcb1SLan Tianyu {
2263dc612486SRafael J. Wysocki 	struct acpi_device *adev = acpi_bus_get_acpi_device(dep->consumer);
226440e7fcb1SLan Tianyu 
22650f347aa0SHans de Goede 	if (adev) {
226640e7fcb1SLan Tianyu 		adev->dep_unmet--;
2267dc612486SRafael J. Wysocki 		if (!acpi_scan_clear_dep_queue(adev))
2268dc612486SRafael J. Wysocki 			acpi_dev_put(adev);
22690f347aa0SHans de Goede 	}
227071da201fSRafael J. Wysocki 
227140e7fcb1SLan Tianyu 	list_del(&dep->node);
227240e7fcb1SLan Tianyu 	kfree(dep);
2273a9e10e58SDaniel Scally 
2274a9e10e58SDaniel Scally 	return 0;
2275a9e10e58SDaniel Scally }
2276a9e10e58SDaniel Scally 
2277a9e10e58SDaniel Scally /**
2278a9e10e58SDaniel Scally  * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2279a9e10e58SDaniel Scally  * @handle:	The ACPI handle of the supplier device
2280a9e10e58SDaniel Scally  * @callback:	Pointer to the callback function to apply
2281a9e10e58SDaniel Scally  * @data:	Pointer to some data to pass to the callback
2282a9e10e58SDaniel Scally  *
2283a9e10e58SDaniel Scally  * The return value of the callback determines this function's behaviour. If 0
2284a9e10e58SDaniel Scally  * is returned we continue to iterate over acpi_dep_list. If a positive value
2285a9e10e58SDaniel Scally  * is returned then the loop is broken but this function returns 0. If a
2286a9e10e58SDaniel Scally  * negative value is returned by the callback then the loop is broken and that
2287a9e10e58SDaniel Scally  * value is returned as the final error.
2288a9e10e58SDaniel Scally  */
2289aff0dbd0SRafael J. Wysocki static int acpi_walk_dep_device_list(acpi_handle handle,
2290a9e10e58SDaniel Scally 				int (*callback)(struct acpi_dep_data *, void *),
2291a9e10e58SDaniel Scally 				void *data)
2292a9e10e58SDaniel Scally {
2293a9e10e58SDaniel Scally 	struct acpi_dep_data *dep, *tmp;
229423db673dSColin Ian King 	int ret = 0;
2295a9e10e58SDaniel Scally 
2296a9e10e58SDaniel Scally 	mutex_lock(&acpi_dep_list_lock);
2297a9e10e58SDaniel Scally 	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2298a9e10e58SDaniel Scally 		if (dep->supplier == handle) {
2299a9e10e58SDaniel Scally 			ret = callback(dep, data);
2300a9e10e58SDaniel Scally 			if (ret)
2301a9e10e58SDaniel Scally 				break;
230240e7fcb1SLan Tianyu 		}
230340e7fcb1SLan Tianyu 	}
230440e7fcb1SLan Tianyu 	mutex_unlock(&acpi_dep_list_lock);
2305a9e10e58SDaniel Scally 
2306a9e10e58SDaniel Scally 	return ret > 0 ? 0 : ret;
230740e7fcb1SLan Tianyu }
230840e7fcb1SLan Tianyu 
2309b8bd759aSRafael J. Wysocki /**
2310a9e10e58SDaniel Scally  * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2311a9e10e58SDaniel Scally  * @supplier: Pointer to the supplier &struct acpi_device
2312a9e10e58SDaniel Scally  *
2313a9e10e58SDaniel Scally  * Clear dependencies on the given device.
2314a9e10e58SDaniel Scally  */
2315a9e10e58SDaniel Scally void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2316a9e10e58SDaniel Scally {
2317a9e10e58SDaniel Scally 	acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2318a9e10e58SDaniel Scally }
2319a9e10e58SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2320a9e10e58SDaniel Scally 
2321a9e10e58SDaniel Scally /**
2322b83e2b30SDaniel Scally  * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2323b83e2b30SDaniel Scally  * @supplier: Pointer to the dependee device
2324b83e2b30SDaniel Scally  *
2325b83e2b30SDaniel Scally  * Returns the first &struct acpi_device which declares itself dependent on
2326b83e2b30SDaniel Scally  * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2327b83e2b30SDaniel Scally  *
2328b83e2b30SDaniel Scally  * The caller is responsible for putting the reference to adev when it is no
2329b83e2b30SDaniel Scally  * longer needed.
2330b83e2b30SDaniel Scally  */
2331b83e2b30SDaniel Scally struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2332b83e2b30SDaniel Scally {
2333b83e2b30SDaniel Scally 	struct acpi_device *adev = NULL;
2334b83e2b30SDaniel Scally 
2335b83e2b30SDaniel Scally 	acpi_walk_dep_device_list(supplier->handle,
2336b83e2b30SDaniel Scally 				  acpi_dev_get_first_consumer_dev_cb, &adev);
2337b83e2b30SDaniel Scally 
2338b83e2b30SDaniel Scally 	return adev;
2339b83e2b30SDaniel Scally }
2340b83e2b30SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
2341c10d7a13SDaniel Drake 
23423fb02738SRajesh Shah /**
23431da177e4SLinus Torvalds  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
23442c0d4fe0SRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
23452c0d4fe0SRafael J. Wysocki  *
23462c0d4fe0SRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
23475c478f49SBjorn Helgaas  * found devices.
2348c4168bffSLi Shaohua  *
23492c0d4fe0SRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
23502c0d4fe0SRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
23512c0d4fe0SRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
23522c0d4fe0SRafael J. Wysocki  * appropriate driver.
235388346167SRafael J. Wysocki  *
23542c0d4fe0SRafael J. Wysocki  * Must be called under acpi_scan_lock.
23553fb02738SRajesh Shah  */
23561da177e4SLinus Torvalds int acpi_bus_scan(acpi_handle handle)
23572c0d4fe0SRafael J. Wysocki {
235871da201fSRafael J. Wysocki 	struct acpi_device *device = NULL;
23591da177e4SLinus Torvalds 
23600de7fb7cSRafael J. Wysocki 	acpi_bus_scan_second_pass = false;
23610de7fb7cSRafael J. Wysocki 
236271da201fSRafael J. Wysocki 	/* Pass 1: Avoid enumerating devices with missing dependencies. */
236371da201fSRafael J. Wysocki 
236471da201fSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
23651da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
236671da201fSRafael J. Wysocki 				    acpi_bus_check_add_1, NULL, NULL,
236771da201fSRafael J. Wysocki 				    (void **)&device);
23681da177e4SLinus Torvalds 
236971da201fSRafael J. Wysocki 	if (!device)
23701da177e4SLinus Torvalds 		return -ENODEV;
237171da201fSRafael J. Wysocki 
237271da201fSRafael J. Wysocki 	acpi_bus_attach(device, true);
237371da201fSRafael J. Wysocki 
23740de7fb7cSRafael J. Wysocki 	if (!acpi_bus_scan_second_pass)
23750de7fb7cSRafael J. Wysocki 		return 0;
23760de7fb7cSRafael J. Wysocki 
237771da201fSRafael J. Wysocki 	/* Pass 2: Enumerate all of the remaining devices. */
237871da201fSRafael J. Wysocki 
237971da201fSRafael J. Wysocki 	device = NULL;
238071da201fSRafael J. Wysocki 
238171da201fSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
238271da201fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
238371da201fSRafael J. Wysocki 				    acpi_bus_check_add_2, NULL, NULL,
238471da201fSRafael J. Wysocki 				    (void **)&device);
238571da201fSRafael J. Wysocki 
238671da201fSRafael J. Wysocki 	acpi_bus_attach(device, false);
238771da201fSRafael J. Wysocki 
238871da201fSRafael J. Wysocki 	return 0;
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_scan);
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds /**
23931da177e4SLinus Torvalds  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
23941da177e4SLinus Torvalds  * @adev: Root of the ACPI namespace scope to walk.
23951da177e4SLinus Torvalds  *
23961da177e4SLinus Torvalds  * Must be called under acpi_scan_lock.
23971da177e4SLinus Torvalds  */
23981da177e4SLinus Torvalds void acpi_bus_trim(struct acpi_device *adev)
23991da177e4SLinus Torvalds {
24001da177e4SLinus Torvalds 	struct acpi_scan_handler *handler = adev->handler;
24011da177e4SLinus Torvalds 	struct acpi_device *child;
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds 	list_for_each_entry_reverse(child, &adev->children, node)
24041da177e4SLinus Torvalds 		acpi_bus_trim(child);
24051da177e4SLinus Torvalds 
24061da177e4SLinus Torvalds 	adev->flags.match_driver = false;
24071da177e4SLinus Torvalds 	if (handler) {
24081da177e4SLinus Torvalds 		if (handler->detach)
24091da177e4SLinus Torvalds 			handler->detach(adev);
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 		adev->handler = NULL;
24121da177e4SLinus Torvalds 	} else {
24131da177e4SLinus Torvalds 		device_release_driver(&adev->dev);
24141da177e4SLinus Torvalds 	}
24151da177e4SLinus Torvalds 	/*
24161da177e4SLinus Torvalds 	 * Most likely, the device is going away, so put it into D3cold before
24171da177e4SLinus Torvalds 	 * that.
24181da177e4SLinus Torvalds 	 */
24191da177e4SLinus Torvalds 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
24201da177e4SLinus Torvalds 	adev->flags.initialized = false;
242110c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(adev);
24221da177e4SLinus Torvalds }
24231da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(acpi_bus_trim);
24241da177e4SLinus Torvalds 
2425a64a62ceSLv Zheng int acpi_bus_register_early_device(int type)
2426a64a62ceSLv Zheng {
2427a64a62ceSLv Zheng 	struct acpi_device *device = NULL;
2428a64a62ceSLv Zheng 	int result;
2429a64a62ceSLv Zheng 
24306d279758SRafael J. Wysocki 	result = acpi_add_single_object(&device, NULL, type, false);
2431a64a62ceSLv Zheng 	if (result)
2432a64a62ceSLv Zheng 		return result;
2433a64a62ceSLv Zheng 
2434a64a62ceSLv Zheng 	device->flags.match_driver = true;
2435a64a62ceSLv Zheng 	return device_attach(&device->dev);
2436a64a62ceSLv Zheng }
2437a64a62ceSLv Zheng EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2438a64a62ceSLv Zheng 
24391da177e4SLinus Torvalds static int acpi_bus_scan_fixed(void)
24401da177e4SLinus Torvalds {
24411da177e4SLinus Torvalds 	int result = 0;
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 	/*
24441da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
24451da177e4SLinus Torvalds 	 */
24461da177e4SLinus Torvalds 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
24471da177e4SLinus Torvalds 		struct acpi_device *device = NULL;
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 		result = acpi_add_single_object(&device, NULL,
24506d279758SRafael J. Wysocki 						ACPI_BUS_TYPE_POWER_BUTTON, false);
24511da177e4SLinus Torvalds 		if (result)
24521da177e4SLinus Torvalds 			return result;
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 		device->flags.match_driver = true;
24551da177e4SLinus Torvalds 		result = device_attach(&device->dev);
24561da177e4SLinus Torvalds 		if (result < 0)
24571da177e4SLinus Torvalds 			return result;
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds 		device_init_wakeup(&device->dev, true);
24601da177e4SLinus Torvalds 	}
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
24631da177e4SLinus Torvalds 		struct acpi_device *device = NULL;
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds 		result = acpi_add_single_object(&device, NULL,
24666d279758SRafael J. Wysocki 						ACPI_BUS_TYPE_SLEEP_BUTTON, false);
24671da177e4SLinus Torvalds 		if (result)
24681da177e4SLinus Torvalds 			return result;
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds 		device->flags.match_driver = true;
24711da177e4SLinus Torvalds 		result = device_attach(&device->dev);
24721da177e4SLinus Torvalds 	}
24731da177e4SLinus Torvalds 
24741da177e4SLinus Torvalds 	return result < 0 ? result : 0;
24751da177e4SLinus Torvalds }
24761da177e4SLinus Torvalds 
2477a4e081b0SShannon Zhao static void __init acpi_get_spcr_uart_addr(void)
2478a4e081b0SShannon Zhao {
2479a4e081b0SShannon Zhao 	acpi_status status;
2480a4e081b0SShannon Zhao 	struct acpi_table_spcr *spcr_ptr;
2481a4e081b0SShannon Zhao 
2482a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_SPCR, 0,
2483a4e081b0SShannon Zhao 				(struct acpi_table_header **)&spcr_ptr);
2484ead7ba13SHanjun Guo 	if (ACPI_FAILURE(status)) {
24858acf4108SHanjun Guo 		pr_warn("STAO table present, but SPCR is missing\n");
2486ead7ba13SHanjun Guo 		return;
2487ead7ba13SHanjun Guo 	}
2488ead7ba13SHanjun Guo 
2489a4e081b0SShannon Zhao 	spcr_uart_addr = spcr_ptr->serial_port.address;
2490ead7ba13SHanjun Guo 	acpi_put_table((struct acpi_table_header *)spcr_ptr);
2491a4e081b0SShannon Zhao }
2492a4e081b0SShannon Zhao 
249368bdb677SOctavian Purdila static bool acpi_scan_initialized;
249468bdb677SOctavian Purdila 
2495e747f274SBjorn Helgaas int __init acpi_scan_init(void)
24961da177e4SLinus Torvalds {
24971da177e4SLinus Torvalds 	int result;
2498a4e081b0SShannon Zhao 	acpi_status status;
2499a4e081b0SShannon Zhao 	struct acpi_table_stao *stao_ptr;
25001da177e4SLinus Torvalds 
250192ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
25024daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2503ac212b69SRafael J. Wysocki 	acpi_processor_init();
2504cb0701acSAndy Shevchenko 	acpi_platform_init();
2505f58b082aSRafael J. Wysocki 	acpi_lpss_init();
250692082a88SKen Xue 	acpi_apd_init();
25072fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2508737f1a9fSRafael J. Wysocki 	acpi_container_init();
25090a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
2510cc6a0e31SMika Westerberg 	acpi_watchdog_init();
2511eec15edbSZhang Rui 	acpi_pnp_init();
25123230bbfcSZhang Rui 	acpi_int340x_thermal_init();
25136ce2e188SGraeme Gregory 	acpi_amba_init();
2514eeb2d80dSSrinivas Pandruvada 	acpi_init_lpit();
251597d9a9e9SRafael J. Wysocki 
25167d284352SRafael J. Wysocki 	acpi_scan_add_handler(&generic_device_handler);
25177d284352SRafael J. Wysocki 
2518a4e081b0SShannon Zhao 	/*
2519a4e081b0SShannon Zhao 	 * If there is STAO table, check whether it needs to ignore the UART
2520a4e081b0SShannon Zhao 	 * device in SPCR table.
2521a4e081b0SShannon Zhao 	 */
2522a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_STAO, 0,
2523a4e081b0SShannon Zhao 				(struct acpi_table_header **)&stao_ptr);
2524a4e081b0SShannon Zhao 	if (ACPI_SUCCESS(status)) {
2525a4e081b0SShannon Zhao 		if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
25268acf4108SHanjun Guo 			pr_info("STAO Name List not yet supported.\n");
2527a4e081b0SShannon Zhao 
2528a4e081b0SShannon Zhao 		if (stao_ptr->ignore_uart)
2529a4e081b0SShannon Zhao 			acpi_get_spcr_uart_addr();
2530ead7ba13SHanjun Guo 
2531ead7ba13SHanjun Guo 		acpi_put_table((struct acpi_table_header *)stao_ptr);
2532a4e081b0SShannon Zhao 	}
2533a4e081b0SShannon Zhao 
2534eb7f43c4SRafael J. Wysocki 	acpi_gpe_apply_masked_gpes();
2535eb7f43c4SRafael J. Wysocki 	acpi_update_all_gpes();
2536eb7f43c4SRafael J. Wysocki 
25377291edcaSDavid Hildenbrand 	/*
25387291edcaSDavid Hildenbrand 	 * Although we call __add_memory() that is documented to require the
25397291edcaSDavid Hildenbrand 	 * device_hotplug_lock, it is not necessary here because this is an
25407291edcaSDavid Hildenbrand 	 * early code when userspace or any other code path cannot trigger
25417291edcaSDavid Hildenbrand 	 * hotplug/hotunplug operations.
25427291edcaSDavid Hildenbrand 	 */
25433757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
25441da177e4SLinus Torvalds 	/*
25451da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
25461da177e4SLinus Torvalds 	 */
25470cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
25481da177e4SLinus Torvalds 	if (result)
25493757b948SRafael J. Wysocki 		goto out;
25501da177e4SLinus Torvalds 
25510cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
25521da177e4SLinus Torvalds 	if (result)
25533757b948SRafael J. Wysocki 		goto out;
2554b8bd759aSRafael J. Wysocki 
25552650ef42SAaron Lu 	/* Fixed feature devices do not exist on HW-reduced platform */
25562650ef42SAaron Lu 	if (!acpi_gbl_reduced_hardware) {
2557b8bd759aSRafael J. Wysocki 		result = acpi_bus_scan_fixed();
2558b8bd759aSRafael J. Wysocki 		if (result) {
25592650ef42SAaron Lu 			acpi_detach_data(acpi_root->handle,
25602650ef42SAaron Lu 					 acpi_scan_drop_device);
2561202317a5SRafael J. Wysocki 			acpi_device_del(acpi_root);
25624cbaba4eSAndy Shevchenko 			acpi_bus_put_acpi_device(acpi_root);
25633757b948SRafael J. Wysocki 			goto out;
2564b8bd759aSRafael J. Wysocki 		}
25652650ef42SAaron Lu 	}
2566b8bd759aSRafael J. Wysocki 
2567c10383e8SRafael J. Wysocki 	/*
2568c10383e8SRafael J. Wysocki 	 * Make sure that power management resources are not blocked by ACPI
2569c10383e8SRafael J. Wysocki 	 * device objects with no users.
2570c10383e8SRafael J. Wysocki 	 */
2571c10383e8SRafael J. Wysocki 	bus_for_each_dev(&acpi_bus_type, NULL, NULL, acpi_dev_turn_off_if_unused);
2572c10383e8SRafael J. Wysocki 
25736381195aSRafael J. Wysocki 	acpi_turn_off_unused_power_resources();
257429038ae2SRafael J. Wysocki 
257568bdb677SOctavian Purdila 	acpi_scan_initialized = true;
257668bdb677SOctavian Purdila 
25773757b948SRafael J. Wysocki  out:
25783757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
25793757b948SRafael J. Wysocki 	return result;
25801da177e4SLinus Torvalds }
2581e647b532SMarc Zyngier 
2582e647b532SMarc Zyngier static struct acpi_probe_entry *ape;
2583e647b532SMarc Zyngier static int acpi_probe_count;
25845331d9caSLorenzo Pieralisi static DEFINE_MUTEX(acpi_probe_mutex);
2585e647b532SMarc Zyngier 
258660574d1eSKeith Busch static int __init acpi_match_madt(union acpi_subtable_headers *header,
2587e647b532SMarc Zyngier 				  const unsigned long end)
2588e647b532SMarc Zyngier {
258960574d1eSKeith Busch 	if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2590e647b532SMarc Zyngier 		if (!ape->probe_subtbl(header, end))
2591e647b532SMarc Zyngier 			acpi_probe_count++;
2592e647b532SMarc Zyngier 
2593e647b532SMarc Zyngier 	return 0;
2594e647b532SMarc Zyngier }
2595e647b532SMarc Zyngier 
2596e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2597e647b532SMarc Zyngier {
2598e647b532SMarc Zyngier 	int count = 0;
2599e647b532SMarc Zyngier 
2600e647b532SMarc Zyngier 	if (acpi_disabled)
2601e647b532SMarc Zyngier 		return 0;
2602e647b532SMarc Zyngier 
26035331d9caSLorenzo Pieralisi 	mutex_lock(&acpi_probe_mutex);
2604e647b532SMarc Zyngier 	for (ape = ap_head; nr; ape++, nr--) {
26055599fb69SBob Moore 		if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2606e647b532SMarc Zyngier 			acpi_probe_count = 0;
2607e647b532SMarc Zyngier 			acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2608e647b532SMarc Zyngier 			count += acpi_probe_count;
2609e647b532SMarc Zyngier 		} else {
2610e647b532SMarc Zyngier 			int res;
2611e647b532SMarc Zyngier 			res = acpi_table_parse(ape->id, ape->probe_table);
2612e647b532SMarc Zyngier 			if (!res)
2613e647b532SMarc Zyngier 				count++;
2614e647b532SMarc Zyngier 		}
2615e647b532SMarc Zyngier 	}
26165331d9caSLorenzo Pieralisi 	mutex_unlock(&acpi_probe_mutex);
2617e647b532SMarc Zyngier 
2618e647b532SMarc Zyngier 	return count;
2619e647b532SMarc Zyngier }
262068bdb677SOctavian Purdila 
262168bdb677SOctavian Purdila static void acpi_table_events_fn(struct work_struct *work)
262268bdb677SOctavian Purdila {
262368bdb677SOctavian Purdila 	acpi_scan_lock_acquire();
262468bdb677SOctavian Purdila 	acpi_bus_scan(ACPI_ROOT_OBJECT);
262568bdb677SOctavian Purdila 	acpi_scan_lock_release();
26268d287e82SRafael J. Wysocki 
26278d287e82SRafael J. Wysocki 	kfree(work);
262868bdb677SOctavian Purdila }
262968bdb677SOctavian Purdila 
26308d287e82SRafael J. Wysocki void acpi_scan_table_notify(void)
263168bdb677SOctavian Purdila {
26328d287e82SRafael J. Wysocki 	struct work_struct *work;
263368bdb677SOctavian Purdila 
263468bdb677SOctavian Purdila 	if (!acpi_scan_initialized)
263568bdb677SOctavian Purdila 		return;
263668bdb677SOctavian Purdila 
26378d287e82SRafael J. Wysocki 	work = kmalloc(sizeof(*work), GFP_KERNEL);
26388d287e82SRafael J. Wysocki 	if (!work)
263968bdb677SOctavian Purdila 		return;
264068bdb677SOctavian Purdila 
26418d287e82SRafael J. Wysocki 	INIT_WORK(work, acpi_table_events_fn);
26428d287e82SRafael J. Wysocki 	schedule_work(work);
264368bdb677SOctavian Purdila }
264468bdb677SOctavian Purdila 
264568bdb677SOctavian Purdila int acpi_reconfig_notifier_register(struct notifier_block *nb)
264668bdb677SOctavian Purdila {
264768bdb677SOctavian Purdila 	return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
264868bdb677SOctavian Purdila }
264968bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_register);
265068bdb677SOctavian Purdila 
265168bdb677SOctavian Purdila int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
265268bdb677SOctavian Purdila {
265368bdb677SOctavian Purdila 	return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
265468bdb677SOctavian Purdila }
265568bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);
2656