xref: /openbmc/linux/drivers/acpi/scan.c (revision 40381a3c1fa3ed37458c7f745e51fc81e9b48fe2)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * scan.c - support for transforming the ACPI namespace into individual objects
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/module.h>
61da177e4SLinus Torvalds #include <linux/init.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
89b6d97b6SRandy Dunlap #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/acpi.h>
10643b8e4dSLorenzo Pieralisi #include <linux/acpi_iort.h>
1174523c90SAlok N Kataria #include <linux/signal.h>
1274523c90SAlok N Kataria #include <linux/kthread.h>
13222e82acSDarrick J. Wong #include <linux/dmi.h>
14d1efe3c3SLance Ortiz #include <linux/nls.h>
15d0562674SSuthikulpanit, Suravee #include <linux/dma-mapping.h>
16ca9ef3abSLukas Wunner #include <linux/platform_data/x86/apple.h>
171da177e4SLinus Torvalds 
18d783156eSRafael J. Wysocki #include <asm/pgtable.h>
19d783156eSRafael J. Wysocki 
20e60cc7a6SBjorn Helgaas #include "internal.h"
21e60cc7a6SBjorn Helgaas 
221da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
23f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
241da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2729b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
281da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
291da177e4SLinus Torvalds 
30859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
31859ac9a4SBjorn Helgaas 
32d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)
33d783156eSRafael J. Wysocki 
34620e112cSThomas Renninger static const char *dummy_hid = "device";
352b2ae7c7SThomas Renninger 
3640e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list);
3740e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock);
382d12b6b3SLukas Wunner LIST_HEAD(acpi_bus_id_list);
39c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
40ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
419090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
421da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
43e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock);
441da177e4SLinus Torvalds 
45a4e081b0SShannon Zhao /*
46a4e081b0SShannon Zhao  * The UART device described by the SPCR table is the only object which needs
47a4e081b0SShannon Zhao  * special-casing. Everything else is covered by ACPI namespace paths in STAO
48a4e081b0SShannon Zhao  * table.
49a4e081b0SShannon Zhao  */
50a4e081b0SShannon Zhao static u64 spcr_uart_addr;
51a4e081b0SShannon Zhao 
5240e7fcb1SLan Tianyu struct acpi_dep_data {
5340e7fcb1SLan Tianyu 	struct list_head node;
5440e7fcb1SLan Tianyu 	acpi_handle master;
5540e7fcb1SLan Tianyu 	acpi_handle slave;
5640e7fcb1SLan Tianyu };
5740e7fcb1SLan Tianyu 
583757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
593757b948SRafael J. Wysocki {
603757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
613757b948SRafael J. Wysocki }
623757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
633757b948SRafael J. Wysocki 
643757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
653757b948SRafael J. Wysocki {
663757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
673757b948SRafael J. Wysocki }
683757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
693757b948SRafael J. Wysocki 
70e525506fSRafael J. Wysocki void acpi_lock_hp_context(void)
71e525506fSRafael J. Wysocki {
72e525506fSRafael J. Wysocki 	mutex_lock(&acpi_hp_context_lock);
73e525506fSRafael J. Wysocki }
74e525506fSRafael J. Wysocki 
75e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void)
76e525506fSRafael J. Wysocki {
77e525506fSRafael J. Wysocki 	mutex_unlock(&acpi_hp_context_lock);
78e525506fSRafael J. Wysocki }
79e525506fSRafael J. Wysocki 
805d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev,
815d513205SRafael J. Wysocki 				struct acpi_hotplug_context *hp,
825d513205SRafael J. Wysocki 				int (*notify)(struct acpi_device *, u32),
835d513205SRafael J. Wysocki 				void (*uevent)(struct acpi_device *, u32))
845d513205SRafael J. Wysocki {
855d513205SRafael J. Wysocki 	acpi_lock_hp_context();
86ba574dc8SRafael J. Wysocki 	hp->notify = notify;
87ba574dc8SRafael J. Wysocki 	hp->uevent = uevent;
88ba574dc8SRafael J. Wysocki 	acpi_set_hp_context(adev, hp);
895d513205SRafael J. Wysocki 	acpi_unlock_hp_context();
905d513205SRafael J. Wysocki }
915d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
925d513205SRafael J. Wysocki 
93ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
94ca589f94SRafael J. Wysocki {
95d34afa9dSRafael J. Wysocki 	if (!handler)
96ca589f94SRafael J. Wysocki 		return -EINVAL;
97ca589f94SRafael J. Wysocki 
98ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
99ca589f94SRafael J. Wysocki 	return 0;
100ca589f94SRafael J. Wysocki }
101ca589f94SRafael J. Wysocki 
1023f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
1033f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
1043f8055c3SRafael J. Wysocki {
1053f8055c3SRafael J. Wysocki 	int error;
1063f8055c3SRafael J. Wysocki 
1073f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
1083f8055c3SRafael J. Wysocki 	if (error)
1093f8055c3SRafael J. Wysocki 		return error;
1103f8055c3SRafael J. Wysocki 
1113f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
1123f8055c3SRafael J. Wysocki 	return 0;
1133f8055c3SRafael J. Wysocki }
1143f8055c3SRafael J. Wysocki 
115caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
116d22ddcbcSRafael J. Wysocki {
117d22ddcbcSRafael J. Wysocki 	struct acpi_device_physical_node *pn;
118d22ddcbcSRafael J. Wysocki 	bool offline = true;
11927664c58SLee, Chun-Yi 	char *envp[] = { "EVENT=offline", NULL };
120d22ddcbcSRafael J. Wysocki 
1214c533c80SRafael J. Wysocki 	/*
1224c533c80SRafael J. Wysocki 	 * acpi_container_offline() calls this for all of the container's
1234c533c80SRafael J. Wysocki 	 * children under the container's physical_node_lock lock.
1244c533c80SRafael J. Wysocki 	 */
1254c533c80SRafael J. Wysocki 	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
126d22ddcbcSRafael J. Wysocki 
127d22ddcbcSRafael J. Wysocki 	list_for_each_entry(pn, &adev->physical_node_list, node)
128d22ddcbcSRafael J. Wysocki 		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
129caa73ea1SRafael J. Wysocki 			if (uevent)
13027664c58SLee, Chun-Yi 				kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
131caa73ea1SRafael J. Wysocki 
132d22ddcbcSRafael J. Wysocki 			offline = false;
133d22ddcbcSRafael J. Wysocki 			break;
134d22ddcbcSRafael J. Wysocki 		}
135d22ddcbcSRafael J. Wysocki 
136d22ddcbcSRafael J. Wysocki 	mutex_unlock(&adev->physical_node_lock);
137d22ddcbcSRafael J. Wysocki 	return offline;
138d22ddcbcSRafael J. Wysocki }
139d22ddcbcSRafael J. Wysocki 
1407f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
1417f28ddecSRafael J. Wysocki 				    void **ret_p)
142683058e3SRafael J. Wysocki {
143683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
144683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
145303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
146683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
147683058e3SRafael J. Wysocki 
148683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
149683058e3SRafael J. Wysocki 		return AE_OK;
150683058e3SRafael J. Wysocki 
1517f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
1527f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
1537f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
1547f28ddecSRafael J. Wysocki 	}
1557f28ddecSRafael J. Wysocki 
156683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
157683058e3SRafael J. Wysocki 
158683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
159683058e3SRafael J. Wysocki 		int ret;
160683058e3SRafael J. Wysocki 
161303bfdb1SRafael J. Wysocki 		if (second_pass) {
162303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
163303bfdb1SRafael J. Wysocki 			if (pn->put_online)
164303bfdb1SRafael J. Wysocki 				continue;
165303bfdb1SRafael J. Wysocki 		} else {
166303bfdb1SRafael J. Wysocki 			pn->put_online = false;
167303bfdb1SRafael J. Wysocki 		}
168683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
169303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
170303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
171303bfdb1SRafael J. Wysocki 		} else {
172303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
173303bfdb1SRafael J. Wysocki 			if (second_pass) {
174683058e3SRafael J. Wysocki 				status = AE_ERROR;
175683058e3SRafael J. Wysocki 				break;
176683058e3SRafael J. Wysocki 			}
177303bfdb1SRafael J. Wysocki 		}
178683058e3SRafael J. Wysocki 	}
179683058e3SRafael J. Wysocki 
180683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
181683058e3SRafael J. Wysocki 
182683058e3SRafael J. Wysocki 	return status;
183683058e3SRafael J. Wysocki }
184683058e3SRafael J. Wysocki 
1857f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
1867f28ddecSRafael J. Wysocki 				   void **ret_p)
187683058e3SRafael J. Wysocki {
188683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
189683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
190683058e3SRafael J. Wysocki 
191683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
192683058e3SRafael J. Wysocki 		return AE_OK;
193683058e3SRafael J. Wysocki 
194683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
195683058e3SRafael J. Wysocki 
196683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
197683058e3SRafael J. Wysocki 		if (pn->put_online) {
198683058e3SRafael J. Wysocki 			device_online(pn->dev);
199683058e3SRafael J. Wysocki 			pn->put_online = false;
200683058e3SRafael J. Wysocki 		}
201683058e3SRafael J. Wysocki 
202683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
203683058e3SRafael J. Wysocki 
204683058e3SRafael J. Wysocki 	return AE_OK;
205683058e3SRafael J. Wysocki }
206683058e3SRafael J. Wysocki 
207d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device)
2081da177e4SLinus Torvalds {
2095993c467SYinghai Lu 	acpi_handle handle = device->handle;
210d22ddcbcSRafael J. Wysocki 	struct device *errdev = NULL;
211a33ec399SRafael J. Wysocki 	acpi_status status;
212f058cdf4SRafael J. Wysocki 
213303bfdb1SRafael J. Wysocki 	/*
214303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
215303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
216303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
217303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
218303bfdb1SRafael J. Wysocki 	 * block holds them.
219303bfdb1SRafael J. Wysocki 	 *
220303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
221303bfdb1SRafael J. Wysocki 	 */
2227f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2237f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
2247f28ddecSRafael J. Wysocki 				     (void **)&errdev);
2257f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
2267f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
227683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2287f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
2297f28ddecSRafael J. Wysocki 		return -EPERM;
2307f28ddecSRafael J. Wysocki 	}
2317f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
232303bfdb1SRafael J. Wysocki 	if (errdev) {
233303bfdb1SRafael J. Wysocki 		errdev = NULL;
234303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2357f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
2367f28ddecSRafael J. Wysocki 				    (void **)&errdev);
237ffc10d82SMichal Hocko 		if (!errdev)
2387f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
239303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
240303bfdb1SRafael J. Wysocki 
241ffc10d82SMichal Hocko 		if (errdev) {
242303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
2437f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
244303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
2457f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
2467f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
247683058e3SRafael J. Wysocki 			return -EBUSY;
248683058e3SRafael J. Wysocki 		}
249303bfdb1SRafael J. Wysocki 	}
250d22ddcbcSRafael J. Wysocki 	return 0;
251d22ddcbcSRafael J. Wysocki }
252d22ddcbcSRafael J. Wysocki 
253d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
254d22ddcbcSRafael J. Wysocki {
255d22ddcbcSRafael J. Wysocki 	acpi_handle handle = device->handle;
256d22ddcbcSRafael J. Wysocki 	unsigned long long sta;
257d22ddcbcSRafael J. Wysocki 	acpi_status status;
258d22ddcbcSRafael J. Wysocki 
259ffc10d82SMichal Hocko 	if (device->handler && device->handler->hotplug.demand_offline) {
260caa73ea1SRafael J. Wysocki 		if (!acpi_scan_is_offline(device, true))
261d22ddcbcSRafael J. Wysocki 			return -EBUSY;
262d22ddcbcSRafael J. Wysocki 	} else {
263d22ddcbcSRafael J. Wysocki 		int error = acpi_scan_try_to_offline(device);
264d22ddcbcSRafael J. Wysocki 		if (error)
265d22ddcbcSRafael J. Wysocki 			return error;
266d22ddcbcSRafael J. Wysocki 	}
267683058e3SRafael J. Wysocki 
26826d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2690794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
27026d46867SZhang Rui 
271bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
272683058e3SRafael J. Wysocki 
2737d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2741da177e4SLinus Torvalds 	/*
2751da177e4SLinus Torvalds 	 * TBD: _EJD support.
2761da177e4SLinus Torvalds 	 */
2777d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2787d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
279a33ec399SRafael J. Wysocki 		return -ENODEV;
2807d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
281a33ec399SRafael J. Wysocki 		return -EIO;
282f058cdf4SRafael J. Wysocki 
283882fd12eSToshi Kani 	/*
284882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
285882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
286882fd12eSToshi Kani 	 */
287882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
288882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
289882fd12eSToshi Kani 		acpi_handle_warn(handle,
290882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
291882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
292882fd12eSToshi Kani 		acpi_handle_warn(handle,
293882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
294882fd12eSToshi Kani 	}
295882fd12eSToshi Kani 
296a33ec399SRafael J. Wysocki 	return 0;
297a33ec399SRafael J. Wysocki }
298f058cdf4SRafael J. Wysocki 
299443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev)
300443fc820SRafael J. Wysocki {
301443fc820SRafael J. Wysocki 	if (!acpi_device_enumerated(adev)) {
302443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Still not present\n");
303443fc820SRafael J. Wysocki 		return -EALREADY;
304443fc820SRafael J. Wysocki 	}
305443fc820SRafael J. Wysocki 	acpi_bus_trim(adev);
306443fc820SRafael J. Wysocki 	return 0;
307443fc820SRafael J. Wysocki }
308443fc820SRafael J. Wysocki 
309c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev)
310a33ec399SRafael J. Wysocki {
311f943db40SRafael J. Wysocki 	int error;
312a33ec399SRafael J. Wysocki 
313443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
314443fc820SRafael J. Wysocki 	if (adev->status.present || adev->status.functional) {
315443fc820SRafael J. Wysocki 		/*
316443fc820SRafael J. Wysocki 		 * This function is only called for device objects for which
317443fc820SRafael J. Wysocki 		 * matching scan handlers exist.  The only situation in which
318443fc820SRafael J. Wysocki 		 * the scan handler is not attached to this device object yet
319443fc820SRafael J. Wysocki 		 * is when the device has just appeared (either it wasn't
320443fc820SRafael J. Wysocki 		 * present at all before or it was removed and then added
321443fc820SRafael J. Wysocki 		 * again).
322443fc820SRafael J. Wysocki 		 */
323443fc820SRafael J. Wysocki 		if (adev->handler) {
324443fc820SRafael J. Wysocki 			dev_warn(&adev->dev, "Already enumerated\n");
325443fc820SRafael J. Wysocki 			return -EALREADY;
326443fc820SRafael J. Wysocki 		}
327c27b2c33SRafael J. Wysocki 		error = acpi_bus_scan(adev->handle);
328a33ec399SRafael J. Wysocki 		if (error) {
329c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Namespace scan failure\n");
330c27b2c33SRafael J. Wysocki 			return error;
331a33ec399SRafael J. Wysocki 		}
33246394fd0SRafael J. Wysocki 		if (!adev->handler) {
333c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Enumeration failure\n");
334443fc820SRafael J. Wysocki 			error = -ENODEV;
335443fc820SRafael J. Wysocki 		}
336443fc820SRafael J. Wysocki 	} else {
337443fc820SRafael J. Wysocki 		error = acpi_scan_device_not_present(adev);
338443fc820SRafael J. Wysocki 	}
339443fc820SRafael J. Wysocki 	return error;
340443fc820SRafael J. Wysocki }
341443fc820SRafael J. Wysocki 
342443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev)
343443fc820SRafael J. Wysocki {
344443fc820SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
345443fc820SRafael J. Wysocki 	struct acpi_device *child;
346443fc820SRafael J. Wysocki 	int error;
347443fc820SRafael J. Wysocki 
348443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
349443fc820SRafael J. Wysocki 	if (!(adev->status.present || adev->status.functional)) {
350443fc820SRafael J. Wysocki 		acpi_scan_device_not_present(adev);
351443fc820SRafael J. Wysocki 		return 0;
352443fc820SRafael J. Wysocki 	}
353443fc820SRafael J. Wysocki 	if (handler && handler->hotplug.scan_dependent)
354443fc820SRafael J. Wysocki 		return handler->hotplug.scan_dependent(adev);
355443fc820SRafael J. Wysocki 
356443fc820SRafael J. Wysocki 	error = acpi_bus_scan(adev->handle);
357443fc820SRafael J. Wysocki 	if (error) {
358443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Namespace scan failure\n");
359443fc820SRafael J. Wysocki 		return error;
360443fc820SRafael J. Wysocki 	}
361443fc820SRafael J. Wysocki 	list_for_each_entry(child, &adev->children, node) {
362443fc820SRafael J. Wysocki 		error = acpi_scan_bus_check(child);
363443fc820SRafael J. Wysocki 		if (error)
364443fc820SRafael J. Wysocki 			return error;
365a33ec399SRafael J. Wysocki 	}
366c27b2c33SRafael J. Wysocki 	return 0;
367c27b2c33SRafael J. Wysocki }
368c27b2c33SRafael J. Wysocki 
3693c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
3703c2cc7ffSRafael J. Wysocki {
3713c2cc7ffSRafael J. Wysocki 	switch (type) {
3723c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
3733c2cc7ffSRafael J. Wysocki 		return acpi_scan_bus_check(adev);
3743c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
3753c2cc7ffSRafael J. Wysocki 		return acpi_scan_device_check(adev);
3763c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
3773c2cc7ffSRafael J. Wysocki 	case ACPI_OST_EC_OSPM_EJECT:
378dd2151beSRafael J. Wysocki 		if (adev->handler && !adev->handler->hotplug.enabled) {
379dd2151beSRafael J. Wysocki 			dev_info(&adev->dev, "Eject disabled\n");
380dd2151beSRafael J. Wysocki 			return -EPERM;
381dd2151beSRafael J. Wysocki 		}
382700b8422SRafael J. Wysocki 		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
383dd2151beSRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3843c2cc7ffSRafael J. Wysocki 		return acpi_scan_hot_remove(adev);
3853c2cc7ffSRafael J. Wysocki 	}
3863c2cc7ffSRafael J. Wysocki 	return -EINVAL;
3873c2cc7ffSRafael J. Wysocki }
3883c2cc7ffSRafael J. Wysocki 
3891e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src)
390c27b2c33SRafael J. Wysocki {
391c27b2c33SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
3923c2cc7ffSRafael J. Wysocki 	int error = -ENODEV;
393c27b2c33SRafael J. Wysocki 
394c27b2c33SRafael J. Wysocki 	lock_device_hotplug();
395c27b2c33SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
396c27b2c33SRafael J. Wysocki 
397c27b2c33SRafael J. Wysocki 	/*
398c27b2c33SRafael J. Wysocki 	 * The device object's ACPI handle cannot become invalid as long as we
3993c2cc7ffSRafael J. Wysocki 	 * are holding acpi_scan_lock, but it might have become invalid before
400c27b2c33SRafael J. Wysocki 	 * that lock was acquired.
401c27b2c33SRafael J. Wysocki 	 */
402c27b2c33SRafael J. Wysocki 	if (adev->handle == INVALID_ACPI_HANDLE)
4033c2cc7ffSRafael J. Wysocki 		goto err_out;
404c27b2c33SRafael J. Wysocki 
4051e2380cdSRafael J. Wysocki 	if (adev->flags.is_dock_station) {
4061e2380cdSRafael J. Wysocki 		error = dock_notify(adev, src);
4071e2380cdSRafael J. Wysocki 	} else if (adev->flags.hotplug_notify) {
4083c2cc7ffSRafael J. Wysocki 		error = acpi_generic_hotplug_event(adev, src);
4093c2cc7ffSRafael J. Wysocki 	} else {
410be27b3dcSRafael J. Wysocki 		int (*notify)(struct acpi_device *, u32);
4113c2cc7ffSRafael J. Wysocki 
4123c2cc7ffSRafael J. Wysocki 		acpi_lock_hp_context();
413be27b3dcSRafael J. Wysocki 		notify = adev->hp ? adev->hp->notify : NULL;
4143c2cc7ffSRafael J. Wysocki 		acpi_unlock_hp_context();
4153c2cc7ffSRafael J. Wysocki 		/*
4163c2cc7ffSRafael J. Wysocki 		 * There may be additional notify handlers for device objects
4173c2cc7ffSRafael J. Wysocki 		 * without the .event() callback, so ignore them here.
4183c2cc7ffSRafael J. Wysocki 		 */
419be27b3dcSRafael J. Wysocki 		if (notify)
420be27b3dcSRafael J. Wysocki 			error = notify(adev, src);
4213c2cc7ffSRafael J. Wysocki 		else
4223c2cc7ffSRafael J. Wysocki 			goto out;
423c27b2c33SRafael J. Wysocki 	}
424d429e5c1SLee, Chun-Yi 	switch (error) {
425d429e5c1SLee, Chun-Yi 	case 0:
426a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_SUCCESS;
427d429e5c1SLee, Chun-Yi 		break;
428d429e5c1SLee, Chun-Yi 	case -EPERM:
429d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
430d429e5c1SLee, Chun-Yi 		break;
431d429e5c1SLee, Chun-Yi 	case -EBUSY:
432d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_DEVICE_BUSY;
433d429e5c1SLee, Chun-Yi 		break;
434d429e5c1SLee, Chun-Yi 	default:
435d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
436d429e5c1SLee, Chun-Yi 		break;
437d429e5c1SLee, Chun-Yi 	}
438a33ec399SRafael J. Wysocki 
4393c2cc7ffSRafael J. Wysocki  err_out:
440700b8422SRafael J. Wysocki 	acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
4413c2cc7ffSRafael J. Wysocki 
4423c2cc7ffSRafael J. Wysocki  out:
44378ea4639SRafael J. Wysocki 	acpi_bus_put_acpi_device(adev);
444a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
445e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
446a33ec399SRafael J. Wysocki }
447a33ec399SRafael J. Wysocki 
4480b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
4490b224527SRafael J. Wysocki {
4500b224527SRafael J. Wysocki 	int i;
4510b224527SRafael J. Wysocki 
452993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
453993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
454993cbe59SRafael J. Wysocki 
4551b1f3e16SRafael J. Wysocki 	if (!device->power.flags.power_resources)
4560b224527SRafael J. Wysocki 		return;
4570b224527SRafael J. Wysocki 
4580b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
4590b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
4600b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
4610b224527SRafael J. Wysocki 	}
4627f47fa6cSBjorn Helgaas }
4637f47fa6cSBjorn Helgaas 
4641890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
4651890a97aSPatrick Mochel {
4661890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4671890a97aSPatrick Mochel 
468ffdcd955SMika Westerberg 	acpi_free_properties(acpi_dev);
469c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
4700b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
4711890a97aSPatrick Mochel 	kfree(acpi_dev);
4721890a97aSPatrick Mochel }
4731890a97aSPatrick Mochel 
474d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device)
475caf5c03fSRafael J. Wysocki {
476ca9dc8d4SLukas Wunner 	struct acpi_device_bus_id *acpi_device_bus_id;
477ca9dc8d4SLukas Wunner 
478d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_lock);
479d783156eSRafael J. Wysocki 	if (device->parent)
480d783156eSRafael J. Wysocki 		list_del(&device->node);
481d783156eSRafael J. Wysocki 
482ca9dc8d4SLukas Wunner 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
483ca9dc8d4SLukas Wunner 		if (!strcmp(acpi_device_bus_id->bus_id,
484ca9dc8d4SLukas Wunner 			    acpi_device_hid(device))) {
485ca9dc8d4SLukas Wunner 			if (acpi_device_bus_id->instance_no > 0)
486ca9dc8d4SLukas Wunner 				acpi_device_bus_id->instance_no--;
487ca9dc8d4SLukas Wunner 			else {
488ca9dc8d4SLukas Wunner 				list_del(&acpi_device_bus_id->node);
489ca9dc8d4SLukas Wunner 				kfree(acpi_device_bus_id);
490ca9dc8d4SLukas Wunner 			}
491ca9dc8d4SLukas Wunner 			break;
492ca9dc8d4SLukas Wunner 		}
493ca9dc8d4SLukas Wunner 
494d783156eSRafael J. Wysocki 	list_del(&device->wakeup_list);
495d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_lock);
496d783156eSRafael J. Wysocki 
497d783156eSRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
498d783156eSRafael J. Wysocki 	acpi_device_remove_files(device);
499d783156eSRafael J. Wysocki 	if (device->remove)
500d783156eSRafael J. Wysocki 		device->remove(device);
501d783156eSRafael J. Wysocki 
502d783156eSRafael J. Wysocki 	device_del(&device->dev);
503d783156eSRafael J. Wysocki }
504d783156eSRafael J. Wysocki 
50568bdb677SOctavian Purdila static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
50668bdb677SOctavian Purdila 
507d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list);
508d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock);
509d783156eSRafael J. Wysocki 
510d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used)
511d783156eSRafael J. Wysocki {
512d783156eSRafael J. Wysocki 	for (;;) {
513d783156eSRafael J. Wysocki 		struct acpi_device *adev;
514d783156eSRafael J. Wysocki 
515d783156eSRafael J. Wysocki 		mutex_lock(&acpi_device_del_lock);
516d783156eSRafael J. Wysocki 
517d783156eSRafael J. Wysocki 		if (list_empty(&acpi_device_del_list)) {
518d783156eSRafael J. Wysocki 			mutex_unlock(&acpi_device_del_lock);
519d783156eSRafael J. Wysocki 			break;
520d783156eSRafael J. Wysocki 		}
521d783156eSRafael J. Wysocki 		adev = list_first_entry(&acpi_device_del_list,
522d783156eSRafael J. Wysocki 					struct acpi_device, del_list);
523d783156eSRafael J. Wysocki 		list_del(&adev->del_list);
524d783156eSRafael J. Wysocki 
525d783156eSRafael J. Wysocki 		mutex_unlock(&acpi_device_del_lock);
526d783156eSRafael J. Wysocki 
52768bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
52868bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_REMOVE, adev);
52968bdb677SOctavian Purdila 
530d783156eSRafael J. Wysocki 		acpi_device_del(adev);
531d783156eSRafael J. Wysocki 		/*
532d783156eSRafael J. Wysocki 		 * Drop references to all power resources that might have been
533d783156eSRafael J. Wysocki 		 * used by the device.
534d783156eSRafael J. Wysocki 		 */
535d783156eSRafael J. Wysocki 		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
536d783156eSRafael J. Wysocki 		put_device(&adev->dev);
537d783156eSRafael J. Wysocki 	}
538d783156eSRafael J. Wysocki }
539d783156eSRafael J. Wysocki 
540d783156eSRafael J. Wysocki /**
541d783156eSRafael J. Wysocki  * acpi_scan_drop_device - Drop an ACPI device object.
542d783156eSRafael J. Wysocki  * @handle: Handle of an ACPI namespace node, not used.
543d783156eSRafael J. Wysocki  * @context: Address of the ACPI device object to drop.
544d783156eSRafael J. Wysocki  *
545d783156eSRafael J. Wysocki  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
546d783156eSRafael J. Wysocki  * namespace node the device object pointed to by @context is attached to.
547d783156eSRafael J. Wysocki  *
548d783156eSRafael J. Wysocki  * The unregistration is carried out asynchronously to avoid running
549d783156eSRafael J. Wysocki  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
550d783156eSRafael J. Wysocki  * ensure the correct ordering (the device objects must be unregistered in the
551d783156eSRafael J. Wysocki  * same order in which the corresponding namespace nodes are deleted).
552d783156eSRafael J. Wysocki  */
553d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context)
554d783156eSRafael J. Wysocki {
555d783156eSRafael J. Wysocki 	static DECLARE_WORK(work, acpi_device_del_work_fn);
556d783156eSRafael J. Wysocki 	struct acpi_device *adev = context;
557d783156eSRafael J. Wysocki 
558d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_del_lock);
559d783156eSRafael J. Wysocki 
560d783156eSRafael J. Wysocki 	/*
561d783156eSRafael J. Wysocki 	 * Use the ACPI hotplug workqueue which is ordered, so this work item
562d783156eSRafael J. Wysocki 	 * won't run after any hotplug work items submitted subsequently.  That
563d783156eSRafael J. Wysocki 	 * prevents attempts to register device objects identical to those being
564d783156eSRafael J. Wysocki 	 * deleted from happening concurrently (such attempts result from
565d783156eSRafael J. Wysocki 	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
566d783156eSRafael J. Wysocki 	 * run after all of the work items submitted previosuly, which helps
567d783156eSRafael J. Wysocki 	 * those work items to ensure that they are not accessing stale device
568d783156eSRafael J. Wysocki 	 * objects.
569d783156eSRafael J. Wysocki 	 */
570d783156eSRafael J. Wysocki 	if (list_empty(&acpi_device_del_list))
571d783156eSRafael J. Wysocki 		acpi_queue_hotplug_work(&work);
572d783156eSRafael J. Wysocki 
573d783156eSRafael J. Wysocki 	list_add_tail(&adev->del_list, &acpi_device_del_list);
574d783156eSRafael J. Wysocki 	/* Make acpi_ns_validate_handle() return NULL for this handle. */
575d783156eSRafael J. Wysocki 	adev->handle = INVALID_ACPI_HANDLE;
576d783156eSRafael J. Wysocki 
577d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_del_lock);
578caf5c03fSRafael J. Wysocki }
579caf5c03fSRafael J. Wysocki 
58078ea4639SRafael J. Wysocki static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
58178ea4639SRafael J. Wysocki 				void (*callback)(void *))
582caf5c03fSRafael J. Wysocki {
583caf5c03fSRafael J. Wysocki 	acpi_status status;
584caf5c03fSRafael J. Wysocki 
585caf5c03fSRafael J. Wysocki 	if (!device)
586caf5c03fSRafael J. Wysocki 		return -EINVAL;
587caf5c03fSRafael J. Wysocki 
58878ea4639SRafael J. Wysocki 	status = acpi_get_data_full(handle, acpi_scan_drop_device,
58978ea4639SRafael J. Wysocki 				    (void **)device, callback);
590caf5c03fSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !*device) {
591caf5c03fSRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
592caf5c03fSRafael J. Wysocki 				  handle));
593caf5c03fSRafael J. Wysocki 		return -ENODEV;
594caf5c03fSRafael J. Wysocki 	}
595caf5c03fSRafael J. Wysocki 	return 0;
596caf5c03fSRafael J. Wysocki }
59778ea4639SRafael J. Wysocki 
59878ea4639SRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
59978ea4639SRafael J. Wysocki {
60078ea4639SRafael J. Wysocki 	return acpi_get_device_data(handle, device, NULL);
60178ea4639SRafael J. Wysocki }
6026585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device);
603caf5c03fSRafael J. Wysocki 
60478ea4639SRafael J. Wysocki static void get_acpi_device(void *dev)
60578ea4639SRafael J. Wysocki {
60678ea4639SRafael J. Wysocki 	if (dev)
60778ea4639SRafael J. Wysocki 		get_device(&((struct acpi_device *)dev)->dev);
60878ea4639SRafael J. Wysocki }
60978ea4639SRafael J. Wysocki 
61078ea4639SRafael J. Wysocki struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
61178ea4639SRafael J. Wysocki {
61278ea4639SRafael J. Wysocki 	struct acpi_device *adev = NULL;
61378ea4639SRafael J. Wysocki 
61478ea4639SRafael J. Wysocki 	acpi_get_device_data(handle, &adev, get_acpi_device);
61578ea4639SRafael J. Wysocki 	return adev;
61678ea4639SRafael J. Wysocki }
61778ea4639SRafael J. Wysocki 
61878ea4639SRafael J. Wysocki void acpi_bus_put_acpi_device(struct acpi_device *adev)
61978ea4639SRafael J. Wysocki {
62078ea4639SRafael J. Wysocki 	put_device(&adev->dev);
62178ea4639SRafael J. Wysocki }
62278ea4639SRafael J. Wysocki 
623cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device,
62482c7d5efSRafael J. Wysocki 		    void (*release)(struct device *))
6251da177e4SLinus Torvalds {
626e49bd2ddSZhang Rui 	int result;
627e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
628e49bd2ddSZhang Rui 	int found = 0;
62966b7ed40SBjorn Helgaas 
630d43e167dSRafael J. Wysocki 	if (device->handle) {
631d43e167dSRafael J. Wysocki 		acpi_status status;
632d43e167dSRafael J. Wysocki 
633d783156eSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_scan_drop_device,
634d43e167dSRafael J. Wysocki 					  device);
635d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
636d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
637d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
638d43e167dSRafael J. Wysocki 			return -ENODEV;
639d43e167dSRafael J. Wysocki 		}
640d43e167dSRafael J. Wysocki 	}
641d43e167dSRafael J. Wysocki 
6421da177e4SLinus Torvalds 	/*
6431da177e4SLinus Torvalds 	 * Linkage
6441da177e4SLinus Torvalds 	 * -------
6451da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
6461da177e4SLinus Torvalds 	 */
6471da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
6481da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
6491da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
6501033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
651d783156eSRafael J. Wysocki 	INIT_LIST_HEAD(&device->del_list);
6521033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
6531da177e4SLinus Torvalds 
654e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
655e49bd2ddSZhang Rui 	if (!new_bus_id) {
656d43e167dSRafael J. Wysocki 		pr_err(PREFIX "Memory allocation error\n");
657d43e167dSRafael J. Wysocki 		result = -ENOMEM;
658d43e167dSRafael J. Wysocki 		goto err_detach;
659e49bd2ddSZhang Rui 	}
660e49bd2ddSZhang Rui 
6619090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
662e49bd2ddSZhang Rui 	/*
663e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
664e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
665e49bd2ddSZhang Rui 	 */
666e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
6671131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
6681131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
669e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
670e49bd2ddSZhang Rui 			found = 1;
671e49bd2ddSZhang Rui 			kfree(new_bus_id);
672e49bd2ddSZhang Rui 			break;
673e49bd2ddSZhang Rui 		}
674e49bd2ddSZhang Rui 	}
675e49bd2ddSZhang Rui 	if (!found) {
676e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
6771131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
678e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
679e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
680e49bd2ddSZhang Rui 	}
6810794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
682e49bd2ddSZhang Rui 
68333b57150SLen Brown 	if (device->parent)
6841da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
68533b57150SLen Brown 
6861da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
6871da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
6889090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
6891da177e4SLinus Torvalds 
6901890a97aSPatrick Mochel 	if (device->parent)
69166b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
6921890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
69382c7d5efSRafael J. Wysocki 	device->dev.release = release;
694cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
695e49bd2ddSZhang Rui 	if (result) {
6968b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
697d43e167dSRafael J. Wysocki 		goto err;
698e49bd2ddSZhang Rui 	}
699f883d9dbSPatrick Mochel 
700e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
701e49bd2ddSZhang Rui 	if (result)
7020794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
7030794469dSKay Sievers 		       dev_name(&device->dev));
704e49bd2ddSZhang Rui 
705e49bd2ddSZhang Rui 	return 0;
706d43e167dSRafael J. Wysocki 
707d43e167dSRafael J. Wysocki  err:
7089090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
70933b57150SLen Brown 	if (device->parent)
710e49bd2ddSZhang Rui 		list_del(&device->node);
711e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
7129090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
713d43e167dSRafael J. Wysocki 
714d43e167dSRafael J. Wysocki  err_detach:
715d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
716e49bd2ddSZhang Rui 	return result;
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7199e89dde2SZhang Rui /* --------------------------------------------------------------------------
7201da177e4SLinus Torvalds                                  Device Enumeration
7211da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
7225c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
7235c478f49SBjorn Helgaas {
724456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
7255c478f49SBjorn Helgaas 	acpi_status status;
7265c478f49SBjorn Helgaas 
7275c478f49SBjorn Helgaas 	/*
7285c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
7295c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
7305c478f49SBjorn Helgaas 	 * to deal with them specially.
7315c478f49SBjorn Helgaas 	 */
732456de893SRafael J. Wysocki 	if (!handle)
7335c478f49SBjorn Helgaas 		return acpi_root;
7345c478f49SBjorn Helgaas 
7355c478f49SBjorn Helgaas 	do {
7365c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
7375c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
738456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
739456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
7405c478f49SBjorn Helgaas 	return device;
7415c478f49SBjorn Helgaas }
7425c478f49SBjorn Helgaas 
743c8f7a62cSLen Brown acpi_status
744c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
745c8f7a62cSLen Brown {
746c8f7a62cSLen Brown 	acpi_status status;
747c8f7a62cSLen Brown 	acpi_handle tmp;
748c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
749c8f7a62cSLen Brown 	union acpi_object *obj;
750c8f7a62cSLen Brown 
751c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
752c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
753c8f7a62cSLen Brown 		return status;
754c8f7a62cSLen Brown 
755c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
756c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
757c8f7a62cSLen Brown 		obj = buffer.pointer;
7583b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
7593b5fee59SHolger Macht 					 ejd);
760c8f7a62cSLen Brown 		kfree(buffer.pointer);
761c8f7a62cSLen Brown 	}
762c8f7a62cSLen Brown 	return status;
763c8f7a62cSLen Brown }
764c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
765c8f7a62cSLen Brown 
766*40381a3cSBjorn Helgaas static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
7671da177e4SLinus Torvalds {
768*40381a3cSBjorn Helgaas 	acpi_handle handle = dev->handle;
769*40381a3cSBjorn Helgaas 	struct acpi_device_wakeup *wakeup = &dev->wakeup;
770b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
771b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
7721da177e4SLinus Torvalds 	union acpi_object *element = NULL;
773b581a7f9SRafael J. Wysocki 	acpi_status status;
774e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
7751da177e4SLinus Torvalds 
776993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
7771da177e4SLinus Torvalds 
778b581a7f9SRafael J. Wysocki 	/* _PRW */
779b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
780b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
781b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
782e88c9c60SRafael J. Wysocki 		return err;
783b581a7f9SRafael J. Wysocki 	}
784b581a7f9SRafael J. Wysocki 
785b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
786b581a7f9SRafael J. Wysocki 
787e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
788b581a7f9SRafael J. Wysocki 		goto out;
789b581a7f9SRafael J. Wysocki 
7901da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
791e88c9c60SRafael J. Wysocki 	if (!element)
792b581a7f9SRafael J. Wysocki 		goto out;
793e88c9c60SRafael J. Wysocki 
7941da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
7951da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
7961da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
7971da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
798e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
799b581a7f9SRafael J. Wysocki 			goto out;
800e88c9c60SRafael J. Wysocki 
801b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
8021da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
803b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
8041da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
8051da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
806b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
807b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
808b581a7f9SRafael J. Wysocki 	} else {
809b581a7f9SRafael J. Wysocki 		goto out;
810b581a7f9SRafael J. Wysocki 	}
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
813e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
814b581a7f9SRafael J. Wysocki 		goto out;
815e88c9c60SRafael J. Wysocki 
816b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
8171da177e4SLinus Torvalds 
818e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
819e88c9c60SRafael J. Wysocki 	if (err)
820b581a7f9SRafael J. Wysocki 		goto out;
8211da177e4SLinus Torvalds 
8220596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
8230596a52bSRafael J. Wysocki 		int sleep_state;
8249874647bSRafael J. Wysocki 
825b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
826b5d667ebSRafael J. Wysocki 						  &sleep_state);
827b5d667ebSRafael J. Wysocki 		if (err) {
828b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
829b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
830b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
831b5d667ebSRafael J. Wysocki 			goto out;
832b5d667ebSRafael J. Wysocki 		}
8330596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
8340596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
8350596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
8360596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
8370596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
8380596a52bSRafael J. Wysocki 		}
8390596a52bSRafael J. Wysocki 	}
840b581a7f9SRafael J. Wysocki 
841b581a7f9SRafael J. Wysocki  out:
842b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
843e88c9c60SRafael J. Wysocki 	return err;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
846a1a66393SRafael J. Wysocki static bool acpi_wakeup_gpe_init(struct acpi_device *device)
8471da177e4SLinus Torvalds {
8480519ade7SMathias Krause 	static const struct acpi_device_id button_device_ids[] = {
84929b71a1cSThomas Renninger 		{"PNP0C0C", 0},
850b7e38304SZhang Rui 		{"PNP0C0D", 0},
85129b71a1cSThomas Renninger 		{"PNP0C0E", 0},
85229b71a1cSThomas Renninger 		{"", 0},
85329b71a1cSThomas Renninger 	};
854bd9b2f9aSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &device->wakeup;
855f517709dSRafael J. Wysocki 	acpi_status status;
856f517709dSRafael J. Wysocki 
857bd9b2f9aSRafael J. Wysocki 	wakeup->flags.notifier_present = 0;
858f517709dSRafael J. Wysocki 
859f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
860f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
861b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
862b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
863bd9b2f9aSRafael J. Wysocki 			if (wakeup->sleep_state == ACPI_STATE_S5)
864bd9b2f9aSRafael J. Wysocki 				wakeup->sleep_state = ACPI_STATE_S4;
865b7e38304SZhang Rui 		}
866bd9b2f9aSRafael J. Wysocki 		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
867f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
868a1a66393SRafael J. Wysocki 		return true;
869f517709dSRafael J. Wysocki 	}
870f517709dSRafael J. Wysocki 
871a1a66393SRafael J. Wysocki 	status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
872bd9b2f9aSRafael J. Wysocki 					 wakeup->gpe_number);
873a1a66393SRafael J. Wysocki 	return ACPI_SUCCESS(status);
874f517709dSRafael J. Wysocki }
875f517709dSRafael J. Wysocki 
876d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
877f517709dSRafael J. Wysocki {
878e88c9c60SRafael J. Wysocki 	int err;
87929b71a1cSThomas Renninger 
880d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
881952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
882d57d09a4SRafael J. Wysocki 		return;
883d57d09a4SRafael J. Wysocki 
884*40381a3cSBjorn Helgaas 	err = acpi_bus_extract_wakeup_device_power_package(device);
885e88c9c60SRafael J. Wysocki 	if (err) {
886e88c9c60SRafael J. Wysocki 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
887d57d09a4SRafael J. Wysocki 		return;
8881da177e4SLinus Torvalds 	}
8891da177e4SLinus Torvalds 
890a1a66393SRafael J. Wysocki 	device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
8919b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
892a1a66393SRafael J. Wysocki 	/*
893a1a66393SRafael J. Wysocki 	 * Call _PSW/_DSW object to disable its ability to wake the sleeping
894729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
895729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
896729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
897729b2bdbSZhao Yakui 	 * present will the _PSW object used.
898729b2bdbSZhao Yakui 	 */
899e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
900e88c9c60SRafael J. Wysocki 	if (err)
90177e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
90277e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
905f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
9061da177e4SLinus Torvalds {
907f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
908ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
909ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
910f33ce568SRafael J. Wysocki 	acpi_status status;
9119e89dde2SZhang Rui 
912f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
913f33ce568SRafael J. Wysocki 
914ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
915ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
916ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
917ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
918f33ce568SRafael J. Wysocki 
919ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
920ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
921ef85bdbeSRafael J. Wysocki 		    && package->package.count) {
922e88c9c60SRafael J. Wysocki 			int err = acpi_extract_power_resources(package, 0,
923ef85bdbeSRafael J. Wysocki 							       &ps->resources);
924e88c9c60SRafael J. Wysocki 			if (!err)
925f33ce568SRafael J. Wysocki 				device->power.flags.power_resources = 1;
926f33ce568SRafael J. Wysocki 		}
927ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
928f33ce568SRafael J. Wysocki 	}
929f33ce568SRafael J. Wysocki 
930f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
931ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
932952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
933f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
934f33ce568SRafael J. Wysocki 
93520dacb71SRafael J. Wysocki 	/* State is valid if there are means to put the device into it. */
93620dacb71SRafael J. Wysocki 	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
937f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
938f33ce568SRafael J. Wysocki 
939f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
940f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
941f33ce568SRafael J. Wysocki }
942f33ce568SRafael J. Wysocki 
943d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
9441da177e4SLinus Torvalds {
9458bc5053bSRafael J. Wysocki 	u32 i;
9461da177e4SLinus Torvalds 
947d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
948952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
949952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
950d43e167dSRafael J. Wysocki 		return;
951d43e167dSRafael J. Wysocki 
952d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/*
9559e89dde2SZhang Rui 	 * Power Management Flags
9561da177e4SLinus Torvalds 	 */
957952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
9589e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
959f25c0ae2SRafael J. Wysocki 
960952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
9619e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
9621da177e4SLinus Torvalds 
963f25c0ae2SRafael J. Wysocki 	if (acpi_has_method(device->handle, "_DSW"))
964f25c0ae2SRafael J. Wysocki 		device->power.flags.dsw_present = 1;
965f25c0ae2SRafael J. Wysocki 
9661da177e4SLinus Torvalds 	/*
9679e89dde2SZhang Rui 	 * Enumerate supported power management states
9681da177e4SLinus Torvalds 	 */
969f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
970f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
9711da177e4SLinus Torvalds 
9720b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
97320dacb71SRafael J. Wysocki 	if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
97420dacb71SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
9759e89dde2SZhang Rui 
97620dacb71SRafael J. Wysocki 	/* Set defaults for D0 and D3hot states (always valid) */
9779e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
9789e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
97920dacb71SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
9801399dfcdSAaron Lu 
9811b1f3e16SRafael J. Wysocki 	if (acpi_bus_init_power(device))
982b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
983b3785492SRafael J. Wysocki }
984c8f7a62cSLen Brown 
985d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
988952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
9891da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
992952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
9931da177e4SLinus Torvalds 		device->flags.removable = 1;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
996952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
997952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
9981da177e4SLinus Torvalds 		device->flags.ejectable = 1;
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
1001c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
10021da177e4SLinus Torvalds {
10031da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
10041da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
10051da177e4SLinus Torvalds 	int i = 0;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	/*
10081da177e4SLinus Torvalds 	 * Bus ID
10091da177e4SLinus Torvalds 	 * ------
10101da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
10111da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
10121da177e4SLinus Torvalds 	 */
1013859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
10141da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1015859ac9a4SBjorn Helgaas 		return;
1016859ac9a4SBjorn Helgaas 	}
1017859ac9a4SBjorn Helgaas 
1018859ac9a4SBjorn Helgaas 	switch (device->device_type) {
10191da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
10201da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
10211da177e4SLinus Torvalds 		break;
10221da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
10231da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
10241da177e4SLinus Torvalds 		break;
1025a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1026a64a62ceSLv Zheng 		strcpy(device->pnp.bus_id, "ECDT");
1027a64a62ceSLv Zheng 		break;
10281da177e4SLinus Torvalds 	default:
102966b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
10301da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
10311da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
10321da177e4SLinus Torvalds 			if (bus_id[i] == '_')
10331da177e4SLinus Torvalds 				bus_id[i] = '\0';
10341da177e4SLinus Torvalds 			else
10351da177e4SLinus Torvalds 				break;
10361da177e4SLinus Torvalds 		}
10371da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
10381da177e4SLinus Torvalds 		break;
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
104254735266SZhang Rui /*
1043ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1044ebf4df8dSJiang Liu  *
1045ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1046ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1047ebf4df8dSJiang Liu  */
1048ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1049ebf4df8dSJiang Liu {
1050ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1051ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1052ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1053ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1054ebf4df8dSJiang Liu }
1055ebf4df8dSJiang Liu 
1056ebf4df8dSJiang Liu /*
1057d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
105854735266SZhang Rui  *
105954735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
106054735266SZhang Rui  * then we can safely call it an ejectable drive bay
106154735266SZhang Rui  */
1062ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1063d4e1a692SToshi Kani {
106454735266SZhang Rui 	acpi_handle phandle;
106554735266SZhang Rui 
1066952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1067ebf4df8dSJiang Liu 		return false;
1068ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1069ebf4df8dSJiang Liu 		return true;
1070ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1071ebf4df8dSJiang Liu 		return false;
107254735266SZhang Rui 
1073ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
107454735266SZhang Rui }
107554735266SZhang Rui 
1076b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev)
10771e2380cdSRafael J. Wysocki {
1078b43109faSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
10791e2380cdSRafael J. Wysocki 
1080b43109faSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list)
1081b43109faSRafael J. Wysocki 		if (!strcmp("PNP0C0A", hwid->id))
1082b43109faSRafael J. Wysocki 			return true;
1083b43109faSRafael J. Wysocki 
10841e2380cdSRafael J. Wysocki 	return false;
10851e2380cdSRafael J. Wysocki }
10861e2380cdSRafael J. Wysocki 
1087b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev)
10881e2380cdSRafael J. Wysocki {
1089b43109faSRafael J. Wysocki 	acpi_handle handle = adev->handle;
1090b43109faSRafael J. Wysocki 
1091b43109faSRafael J. Wysocki 	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
10921e2380cdSRafael J. Wysocki 		return true;
10931e2380cdSRafael J. Wysocki 
10941e2380cdSRafael J. Wysocki 	return acpi_bay_match(handle);
10951e2380cdSRafael J. Wysocki }
10961e2380cdSRafael J. Wysocki 
10973620f2f2SFrank Seidel /*
1098d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
10993620f2f2SFrank Seidel  */
1100ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
11013620f2f2SFrank Seidel {
1102ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
11033620f2f2SFrank Seidel }
11043620f2f2SFrank Seidel 
1105adc8bb8eSHans de Goede static acpi_status
1106adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1107adc8bb8eSHans de Goede 			  void **return_value)
1108adc8bb8eSHans de Goede {
1109adc8bb8eSHans de Goede 	long *cap = context;
1110adc8bb8eSHans de Goede 
1111adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_BCM") &&
1112adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_BCL")) {
1113adc8bb8eSHans de Goede 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
1114adc8bb8eSHans de Goede 				  "support\n"));
1115adc8bb8eSHans de Goede 		*cap |= ACPI_VIDEO_BACKLIGHT;
1116adc8bb8eSHans de Goede 		/* We have backlight support, no need to scan further */
1117adc8bb8eSHans de Goede 		return AE_CTRL_TERMINATE;
1118adc8bb8eSHans de Goede 	}
1119adc8bb8eSHans de Goede 	return 0;
1120adc8bb8eSHans de Goede }
1121adc8bb8eSHans de Goede 
1122adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be
1123adc8bb8eSHans de Goede  * handled by video.ko.
1124adc8bb8eSHans de Goede  * The device will get a Linux specific CID added in scan.c to
1125adc8bb8eSHans de Goede  * identify the device as an ACPI graphics device
1126adc8bb8eSHans de Goede  * Be aware that the graphics device may not be physically present
1127adc8bb8eSHans de Goede  * Use acpi_video_get_capabilities() to detect general ACPI video
1128adc8bb8eSHans de Goede  * capabilities of present cards
1129adc8bb8eSHans de Goede  */
1130adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle)
1131adc8bb8eSHans de Goede {
1132adc8bb8eSHans de Goede 	long video_caps = 0;
1133adc8bb8eSHans de Goede 
1134adc8bb8eSHans de Goede 	/* Is this device able to support video switching ? */
1135adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1136adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1137adc8bb8eSHans de Goede 
1138adc8bb8eSHans de Goede 	/* Is this device able to retrieve a video ROM ? */
1139adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_ROM"))
1140adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1141adc8bb8eSHans de Goede 
1142adc8bb8eSHans de Goede 	/* Is this device able to configure which video head to be POSTed ? */
1143adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_VPO") &&
1144adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_GPD") &&
1145adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_SPD"))
1146adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1147adc8bb8eSHans de Goede 
1148adc8bb8eSHans de Goede 	/* Only check for backlight functionality if one of the above hit. */
1149adc8bb8eSHans de Goede 	if (video_caps)
1150adc8bb8eSHans de Goede 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1151adc8bb8eSHans de Goede 				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1152adc8bb8eSHans de Goede 				    &video_caps, NULL);
1153adc8bb8eSHans de Goede 
1154adc8bb8eSHans de Goede 	return video_caps;
1155adc8bb8eSHans de Goede }
1156adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device);
1157adc8bb8eSHans de Goede 
1158620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
115915b8dd53SBob Moore {
11607f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
116115b8dd53SBob Moore 
11622b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
11632b2ae7c7SThomas Renninger 		return dummy_hid;
11642b2ae7c7SThomas Renninger 
11657f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
11667f47fa6cSBjorn Helgaas 	return hid->id;
11677f47fa6cSBjorn Helgaas }
11687f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
116915b8dd53SBob Moore 
1170d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
11717f47fa6cSBjorn Helgaas {
11727f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
117315b8dd53SBob Moore 
11747f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
11757f47fa6cSBjorn Helgaas 	if (!id)
11767f47fa6cSBjorn Helgaas 		return;
117715b8dd53SBob Moore 
11786a0d12efSRasmus Villemoes 	id->id = kstrdup_const(dev_id, GFP_KERNEL);
11797f47fa6cSBjorn Helgaas 	if (!id->id) {
11807f47fa6cSBjorn Helgaas 		kfree(id);
11817f47fa6cSBjorn Helgaas 		return;
118215b8dd53SBob Moore 	}
118315b8dd53SBob Moore 
1184d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1185d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
118615b8dd53SBob Moore }
118715b8dd53SBob Moore 
1188222e82acSDarrick J. Wong /*
1189222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1190222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1191222e82acSDarrick J. Wong  * prefix.  Work around this.
1192222e82acSDarrick J. Wong  */
1193ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1194222e82acSDarrick J. Wong {
1195ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1196ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1197222e82acSDarrick J. Wong 
1198222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1199ebf4df8dSJiang Liu 		return false;
1200222e82acSDarrick J. Wong 
1201222e82acSDarrick J. Wong 	/* Look for SMBS object */
1202ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1203ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1204ebf4df8dSJiang Liu 		return false;
1205222e82acSDarrick J. Wong 
1206222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1207952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1208952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1209952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1210ebf4df8dSJiang Liu 		return true;
1211ebf4df8dSJiang Liu 
1212ebf4df8dSJiang Liu 	return false;
1213222e82acSDarrick J. Wong }
1214222e82acSDarrick J. Wong 
1215ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle)
1216ae3caa80SJiang Liu {
1217ae3caa80SJiang Liu 	acpi_handle tmp;
1218ae3caa80SJiang Liu 
1219ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1220ae3caa80SJiang Liu 	    tmp == handle)
1221ae3caa80SJiang Liu 		return true;
1222ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1223ae3caa80SJiang Liu 	    tmp == handle)
1224ae3caa80SJiang Liu 		return true;
1225ae3caa80SJiang Liu 
1226ae3caa80SJiang Liu 	return false;
1227ae3caa80SJiang Liu }
1228ae3caa80SJiang Liu 
1229c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1230c0af4175SToshi Kani 				int device_type)
12311da177e4SLinus Torvalds {
12321da177e4SLinus Torvalds 	acpi_status status;
123357f3674fSBjorn Helgaas 	struct acpi_device_info *info;
123478e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
12357f47fa6cSBjorn Helgaas 	int i;
12361da177e4SLinus Torvalds 
1237c0af4175SToshi Kani 	switch (device_type) {
12381da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1239c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1240c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1241859ac9a4SBjorn Helgaas 			break;
1242859ac9a4SBjorn Helgaas 		}
1243859ac9a4SBjorn Helgaas 
1244c0af4175SToshi Kani 		status = acpi_get_object_info(handle, &info);
12451da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
1246c0af4175SToshi Kani 			pr_err(PREFIX "%s: Error reading device info\n",
1247c0af4175SToshi Kani 					__func__);
12481da177e4SLinus Torvalds 			return;
12491da177e4SLinus Torvalds 		}
12501da177e4SLinus Torvalds 
1251549e6845SRafael J. Wysocki 		if (info->valid & ACPI_VALID_HID) {
1252c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
1253549e6845SRafael J. Wysocki 			pnp->type.platform_id = 1;
1254549e6845SRafael J. Wysocki 		}
125557f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
125615b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
125757f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1258c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
125957f3674fSBjorn Helgaas 		}
12601da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1261c0af4175SToshi Kani 			pnp->bus_address = info->address;
1262c0af4175SToshi Kani 			pnp->type.bus_address = 1;
12631da177e4SLinus Torvalds 		}
1264ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1265c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1266ccf78040SLv Zheng 							GFP_KERNEL);
126726095a01SSuthikulpanit, Suravee 		if (info->valid & ACPI_VALID_CLS)
126826095a01SSuthikulpanit, Suravee 			acpi_add_id(pnp, info->class_code.string);
1269ae843332SZhang Rui 
1270a83893aeSBjorn Helgaas 		kfree(info);
1271a83893aeSBjorn Helgaas 
127257f3674fSBjorn Helgaas 		/*
127357f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
127457f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
12753620f2f2SFrank Seidel 		 */
1276c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1277c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1278ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1279c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1280ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1281c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1282ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1283c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1284ae3caa80SJiang Liu 		else if (list_empty(&pnp->ids) &&
1285ae3caa80SJiang Liu 			 acpi_object_is_system_bus(handle)) {
1286ae3caa80SJiang Liu 			/* \_SB, \_TZ, LNXSYBUS */
1287ae3caa80SJiang Liu 			acpi_add_id(pnp, ACPI_BUS_HID);
1288c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1289c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1290b7b30de5SBjorn Helgaas 		}
129154735266SZhang Rui 
12921da177e4SLinus Torvalds 		break;
12931da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1294c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
12951da177e4SLinus Torvalds 		break;
12961da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1297c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
12981da177e4SLinus Torvalds 		break;
12991da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1300c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
13011da177e4SLinus Torvalds 		break;
13021da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1303c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
13041da177e4SLinus Torvalds 		break;
13051da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1306c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
13071da177e4SLinus Torvalds 		break;
1308a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1309a64a62ceSLv Zheng 		acpi_add_id(pnp, ACPI_ECDT_HID);
1310a64a62ceSLv Zheng 		break;
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds 
1314c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1315c0af4175SToshi Kani {
1316c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1317c0af4175SToshi Kani 
1318c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
13196a0d12efSRasmus Villemoes 		kfree_const(id->id);
1320c0af4175SToshi Kani 		kfree(id);
1321c0af4175SToshi Kani 	}
1322c0af4175SToshi Kani 	kfree(pnp->unique_id);
1323c0af4175SToshi Kani }
1324c0af4175SToshi Kani 
1325b84f196dSSuthikulpanit, Suravee /**
1326b84f196dSSuthikulpanit, Suravee  * acpi_dma_supported - Check DMA support for the specified device.
1327b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1328b84f196dSSuthikulpanit, Suravee  *
1329b84f196dSSuthikulpanit, Suravee  * Return false if DMA is not supported. Otherwise, return true
1330b84f196dSSuthikulpanit, Suravee  */
1331b84f196dSSuthikulpanit, Suravee bool acpi_dma_supported(struct acpi_device *adev)
1332b84f196dSSuthikulpanit, Suravee {
1333b84f196dSSuthikulpanit, Suravee 	if (!adev)
1334b84f196dSSuthikulpanit, Suravee 		return false;
1335b84f196dSSuthikulpanit, Suravee 
1336b84f196dSSuthikulpanit, Suravee 	if (adev->flags.cca_seen)
1337b84f196dSSuthikulpanit, Suravee 		return true;
1338b84f196dSSuthikulpanit, Suravee 
1339b84f196dSSuthikulpanit, Suravee 	/*
1340b84f196dSSuthikulpanit, Suravee 	* Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1341b84f196dSSuthikulpanit, Suravee 	* DMA on "Intel platforms".  Presumably that includes all x86 and
1342b84f196dSSuthikulpanit, Suravee 	* ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1343b84f196dSSuthikulpanit, Suravee 	*/
1344b84f196dSSuthikulpanit, Suravee 	if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1345b84f196dSSuthikulpanit, Suravee 		return true;
1346b84f196dSSuthikulpanit, Suravee 
1347b84f196dSSuthikulpanit, Suravee 	return false;
1348b84f196dSSuthikulpanit, Suravee }
1349b84f196dSSuthikulpanit, Suravee 
1350b84f196dSSuthikulpanit, Suravee /**
1351b84f196dSSuthikulpanit, Suravee  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1352b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1353b84f196dSSuthikulpanit, Suravee  *
1354b84f196dSSuthikulpanit, Suravee  * Return enum dev_dma_attr.
1355b84f196dSSuthikulpanit, Suravee  */
1356b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1357b84f196dSSuthikulpanit, Suravee {
1358b84f196dSSuthikulpanit, Suravee 	if (!acpi_dma_supported(adev))
1359b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NOT_SUPPORTED;
1360b84f196dSSuthikulpanit, Suravee 
1361b84f196dSSuthikulpanit, Suravee 	if (adev->flags.coherent_dma)
1362b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_COHERENT;
1363b84f196dSSuthikulpanit, Suravee 	else
1364b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NON_COHERENT;
1365b84f196dSSuthikulpanit, Suravee }
1366b84f196dSSuthikulpanit, Suravee 
1367d760a1baSLorenzo Pieralisi /**
1368c04ac679SLorenzo Pieralisi  * acpi_dma_get_range() - Get device DMA parameters.
1369c04ac679SLorenzo Pieralisi  *
1370c04ac679SLorenzo Pieralisi  * @dev: device to configure
1371c04ac679SLorenzo Pieralisi  * @dma_addr: pointer device DMA address result
1372c04ac679SLorenzo Pieralisi  * @offset: pointer to the DMA offset result
1373c04ac679SLorenzo Pieralisi  * @size: pointer to DMA range size result
1374c04ac679SLorenzo Pieralisi  *
1375c04ac679SLorenzo Pieralisi  * Evaluate DMA regions and return respectively DMA region start, offset
1376c04ac679SLorenzo Pieralisi  * and size in dma_addr, offset and size on parsing success; it does not
1377c04ac679SLorenzo Pieralisi  * update the passed in values on failure.
1378c04ac679SLorenzo Pieralisi  *
1379c04ac679SLorenzo Pieralisi  * Return 0 on success, < 0 on failure.
1380c04ac679SLorenzo Pieralisi  */
1381c04ac679SLorenzo Pieralisi int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1382c04ac679SLorenzo Pieralisi 		       u64 *size)
1383c04ac679SLorenzo Pieralisi {
1384c04ac679SLorenzo Pieralisi 	struct acpi_device *adev;
1385c04ac679SLorenzo Pieralisi 	LIST_HEAD(list);
1386c04ac679SLorenzo Pieralisi 	struct resource_entry *rentry;
1387c04ac679SLorenzo Pieralisi 	int ret;
1388c04ac679SLorenzo Pieralisi 	struct device *dma_dev = dev;
1389c04ac679SLorenzo Pieralisi 	u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1390c04ac679SLorenzo Pieralisi 
1391c04ac679SLorenzo Pieralisi 	/*
1392c04ac679SLorenzo Pieralisi 	 * Walk the device tree chasing an ACPI companion with a _DMA
1393c04ac679SLorenzo Pieralisi 	 * object while we go. Stop if we find a device with an ACPI
1394c04ac679SLorenzo Pieralisi 	 * companion containing a _DMA method.
1395c04ac679SLorenzo Pieralisi 	 */
1396c04ac679SLorenzo Pieralisi 	do {
1397c04ac679SLorenzo Pieralisi 		adev = ACPI_COMPANION(dma_dev);
1398c04ac679SLorenzo Pieralisi 		if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1399c04ac679SLorenzo Pieralisi 			break;
1400c04ac679SLorenzo Pieralisi 
1401c04ac679SLorenzo Pieralisi 		dma_dev = dma_dev->parent;
1402c04ac679SLorenzo Pieralisi 	} while (dma_dev);
1403c04ac679SLorenzo Pieralisi 
1404c04ac679SLorenzo Pieralisi 	if (!dma_dev)
1405c04ac679SLorenzo Pieralisi 		return -ENODEV;
1406c04ac679SLorenzo Pieralisi 
1407c04ac679SLorenzo Pieralisi 	if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1408c04ac679SLorenzo Pieralisi 		acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1409c04ac679SLorenzo Pieralisi 		return -EINVAL;
1410c04ac679SLorenzo Pieralisi 	}
1411c04ac679SLorenzo Pieralisi 
1412c04ac679SLorenzo Pieralisi 	ret = acpi_dev_get_dma_resources(adev, &list);
1413c04ac679SLorenzo Pieralisi 	if (ret > 0) {
1414c04ac679SLorenzo Pieralisi 		list_for_each_entry(rentry, &list, node) {
1415c04ac679SLorenzo Pieralisi 			if (dma_offset && rentry->offset != dma_offset) {
1416c04ac679SLorenzo Pieralisi 				ret = -EINVAL;
1417c04ac679SLorenzo Pieralisi 				dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1418c04ac679SLorenzo Pieralisi 				goto out;
1419c04ac679SLorenzo Pieralisi 			}
1420c04ac679SLorenzo Pieralisi 			dma_offset = rentry->offset;
1421c04ac679SLorenzo Pieralisi 
1422c04ac679SLorenzo Pieralisi 			/* Take lower and upper limits */
1423c04ac679SLorenzo Pieralisi 			if (rentry->res->start < dma_start)
1424c04ac679SLorenzo Pieralisi 				dma_start = rentry->res->start;
1425c04ac679SLorenzo Pieralisi 			if (rentry->res->end > dma_end)
1426c04ac679SLorenzo Pieralisi 				dma_end = rentry->res->end;
1427c04ac679SLorenzo Pieralisi 		}
1428c04ac679SLorenzo Pieralisi 
1429c04ac679SLorenzo Pieralisi 		if (dma_start >= dma_end) {
1430c04ac679SLorenzo Pieralisi 			ret = -EINVAL;
1431c04ac679SLorenzo Pieralisi 			dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1432c04ac679SLorenzo Pieralisi 			goto out;
1433c04ac679SLorenzo Pieralisi 		}
1434c04ac679SLorenzo Pieralisi 
1435c04ac679SLorenzo Pieralisi 		*dma_addr = dma_start - dma_offset;
1436c04ac679SLorenzo Pieralisi 		len = dma_end - dma_start;
1437c04ac679SLorenzo Pieralisi 		*size = max(len, len + 1);
1438c04ac679SLorenzo Pieralisi 		*offset = dma_offset;
1439c04ac679SLorenzo Pieralisi 	}
1440c04ac679SLorenzo Pieralisi  out:
1441c04ac679SLorenzo Pieralisi 	acpi_dev_free_resource_list(&list);
1442c04ac679SLorenzo Pieralisi 
1443c04ac679SLorenzo Pieralisi 	return ret >= 0 ? 0 : ret;
1444c04ac679SLorenzo Pieralisi }
1445c04ac679SLorenzo Pieralisi 
1446c04ac679SLorenzo Pieralisi /**
1447d760a1baSLorenzo Pieralisi  * acpi_dma_configure - Set-up DMA configuration for the device.
1448d760a1baSLorenzo Pieralisi  * @dev: The pointer to the device
1449d760a1baSLorenzo Pieralisi  * @attr: device dma attributes
1450d760a1baSLorenzo Pieralisi  */
14515a1bb638SSricharan R int acpi_dma_configure(struct device *dev, enum dev_dma_attr attr)
1452d760a1baSLorenzo Pieralisi {
1453643b8e4dSLorenzo Pieralisi 	const struct iommu_ops *iommu;
14547ad42639SLorenzo Pieralisi 	u64 dma_addr = 0, size = 0;
1455643b8e4dSLorenzo Pieralisi 
1456e5361ca2SRobin Murphy 	if (attr == DEV_DMA_NOT_SUPPORTED) {
1457e5361ca2SRobin Murphy 		set_dma_ops(dev, &dma_dummy_ops);
1458e5361ca2SRobin Murphy 		return 0;
1459e5361ca2SRobin Murphy 	}
1460e5361ca2SRobin Murphy 
14617ad42639SLorenzo Pieralisi 	iort_dma_setup(dev, &dma_addr, &size);
1462d760a1baSLorenzo Pieralisi 
1463643b8e4dSLorenzo Pieralisi 	iommu = iort_iommu_configure(dev);
1464058f8c3fSSricharan R 	if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
1465058f8c3fSSricharan R 		return -EPROBE_DEFER;
1466643b8e4dSLorenzo Pieralisi 
14677ad42639SLorenzo Pieralisi 	arch_setup_dma_ops(dev, dma_addr, size,
14687ad42639SLorenzo Pieralisi 				iommu, attr == DEV_DMA_COHERENT);
14695a1bb638SSricharan R 
14705a1bb638SSricharan R 	return 0;
1471d760a1baSLorenzo Pieralisi }
1472d760a1baSLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_configure);
1473d760a1baSLorenzo Pieralisi 
1474d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev)
1475d0562674SSuthikulpanit, Suravee {
1476d0562674SSuthikulpanit, Suravee 	unsigned long long cca = 0;
1477d0562674SSuthikulpanit, Suravee 	acpi_status status;
1478d0562674SSuthikulpanit, Suravee 	struct acpi_device *parent = adev->parent;
1479d0562674SSuthikulpanit, Suravee 
1480d0562674SSuthikulpanit, Suravee 	if (parent && parent->flags.cca_seen) {
1481d0562674SSuthikulpanit, Suravee 		/*
1482d0562674SSuthikulpanit, Suravee 		 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1483d0562674SSuthikulpanit, Suravee 		 * already saw one.
1484d0562674SSuthikulpanit, Suravee 		 */
1485d0562674SSuthikulpanit, Suravee 		adev->flags.cca_seen = 1;
1486d0562674SSuthikulpanit, Suravee 		cca = parent->flags.coherent_dma;
1487d0562674SSuthikulpanit, Suravee 	} else {
1488d0562674SSuthikulpanit, Suravee 		status = acpi_evaluate_integer(adev->handle, "_CCA",
1489d0562674SSuthikulpanit, Suravee 					       NULL, &cca);
1490d0562674SSuthikulpanit, Suravee 		if (ACPI_SUCCESS(status))
1491d0562674SSuthikulpanit, Suravee 			adev->flags.cca_seen = 1;
1492d0562674SSuthikulpanit, Suravee 		else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1493d0562674SSuthikulpanit, Suravee 			/*
1494d0562674SSuthikulpanit, Suravee 			 * If architecture does not specify that _CCA is
1495d0562674SSuthikulpanit, Suravee 			 * required for DMA-able devices (e.g. x86),
1496d0562674SSuthikulpanit, Suravee 			 * we default to _CCA=1.
1497d0562674SSuthikulpanit, Suravee 			 */
1498d0562674SSuthikulpanit, Suravee 			cca = 1;
1499d0562674SSuthikulpanit, Suravee 		else
1500d0562674SSuthikulpanit, Suravee 			acpi_handle_debug(adev->handle,
1501d0562674SSuthikulpanit, Suravee 					  "ACPI device is missing _CCA.\n");
1502d0562674SSuthikulpanit, Suravee 	}
1503d0562674SSuthikulpanit, Suravee 
1504d0562674SSuthikulpanit, Suravee 	adev->flags.coherent_dma = cca;
1505d0562674SSuthikulpanit, Suravee }
1506d0562674SSuthikulpanit, Suravee 
1507e361d1f8SFrédéric Danis static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1508e4330d8bSJarkko Nikula {
1509e361d1f8SFrédéric Danis 	bool *is_serial_bus_slave_p = data;
1510e4330d8bSJarkko Nikula 
1511e4330d8bSJarkko Nikula 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1512e4330d8bSJarkko Nikula 		return 1;
1513e4330d8bSJarkko Nikula 
1514e361d1f8SFrédéric Danis 	*is_serial_bus_slave_p = true;
1515e4330d8bSJarkko Nikula 
1516e4330d8bSJarkko Nikula 	 /* no need to do more checking */
1517e4330d8bSJarkko Nikula 	return -1;
1518e4330d8bSJarkko Nikula }
1519e4330d8bSJarkko Nikula 
1520dfda4492SJohn Garry static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1521dfda4492SJohn Garry {
1522dfda4492SJohn Garry 	struct acpi_device *parent = device->parent;
1523a6f07295SJohn Garry 	static const struct acpi_device_id indirect_io_hosts[] = {
1524dfda4492SJohn Garry 		{"HISI0191", 0},
1525dfda4492SJohn Garry 		{}
1526dfda4492SJohn Garry 	};
1527dfda4492SJohn Garry 
1528dfda4492SJohn Garry 	return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1529dfda4492SJohn Garry }
1530dfda4492SJohn Garry 
1531d87fb091SJohn Garry static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1532e4330d8bSJarkko Nikula {
1533e4330d8bSJarkko Nikula 	struct list_head resource_list;
1534e361d1f8SFrédéric Danis 	bool is_serial_bus_slave = false;
1535aba94139SHans de Goede 	/*
1536aba94139SHans de Goede 	 * These devices have multiple I2cSerialBus resources and an i2c-client
1537aba94139SHans de Goede 	 * must be instantiated for each, each with its own i2c_device_id.
1538aba94139SHans de Goede 	 * Normally we only instantiate an i2c-client for the first resource,
1539aba94139SHans de Goede 	 * using the ACPI HID as id. These special cases are handled by the
1540aba94139SHans de Goede 	 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1541aba94139SHans de Goede 	 * which i2c_device_id to use for each resource.
1542aba94139SHans de Goede 	 */
1543aba94139SHans de Goede 	static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1544aba94139SHans de Goede 		{"BSG1160", },
154596f984d3SHans de Goede 		{"BSG2150", },
1546589edb56SHans de Goede 		{"INT33FE", },
1547a3dd034aSAndy Shevchenko 		{"INT3515", },
1548aba94139SHans de Goede 		{}
1549aba94139SHans de Goede 	};
1550e4330d8bSJarkko Nikula 
1551dfda4492SJohn Garry 	if (acpi_is_indirect_io_slave(device))
1552dfda4492SJohn Garry 		return true;
1553dfda4492SJohn Garry 
1554ca9ef3abSLukas Wunner 	/* Macs use device properties in lieu of _CRS resources */
1555ca9ef3abSLukas Wunner 	if (x86_apple_machine &&
1556ca9ef3abSLukas Wunner 	    (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1557e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "i2cAddress") ||
1558e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "baud")))
1559ca9ef3abSLukas Wunner 		return true;
1560ca9ef3abSLukas Wunner 
1561aba94139SHans de Goede 	/* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1562aba94139SHans de Goede 	if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1563aba94139SHans de Goede 		return false;
1564aba94139SHans de Goede 
1565e4330d8bSJarkko Nikula 	INIT_LIST_HEAD(&resource_list);
1566e361d1f8SFrédéric Danis 	acpi_dev_get_resources(device, &resource_list,
1567e361d1f8SFrédéric Danis 			       acpi_check_serial_bus_slave,
1568e361d1f8SFrédéric Danis 			       &is_serial_bus_slave);
1569e4330d8bSJarkko Nikula 	acpi_dev_free_resource_list(&resource_list);
1570e4330d8bSJarkko Nikula 
1571e361d1f8SFrédéric Danis 	return is_serial_bus_slave;
1572e4330d8bSJarkko Nikula }
1573e4330d8bSJarkko Nikula 
157482c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1575d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
15761da177e4SLinus Torvalds {
1577d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1578d43e167dSRafael J. Wysocki 	device->device_type = type;
1579d43e167dSRafael J. Wysocki 	device->handle = handle;
1580d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
1581db3e50f3SSakari Ailus 	device->fwnode.ops = &acpi_device_fwnode_ops;
158225db115bSRafael J. Wysocki 	acpi_set_device_status(device, sta);
1583d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1584c0af4175SToshi Kani 	acpi_set_pnp_ids(handle, &device->pnp, type);
1585ffdcd955SMika Westerberg 	acpi_init_properties(device);
1586d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
15872c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1588202317a5SRafael J. Wysocki 	device->flags.initialized = true;
1589d87fb091SJohn Garry 	device->flags.enumeration_by_parent =
1590d87fb091SJohn Garry 		acpi_device_enumeration_by_parent(device);
159110c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(device);
1592cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1593cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1594d0562674SSuthikulpanit, Suravee 	acpi_init_coherency(device);
159563347db0SHans de Goede 	/* Assume there are unmet deps until acpi_device_dep_initialize() runs */
159663347db0SHans de Goede 	device->dep_unmet = 1;
1597cf860be6SRafael J. Wysocki }
1598bc3b0772SBjorn Helgaas 
1599cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1600cf860be6SRafael J. Wysocki {
1601cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1602cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
16031da177e4SLinus Torvalds }
16041da177e4SLinus Torvalds 
16055c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
16065c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
16072c0d4fe0SRafael J. Wysocki 				  unsigned long long sta)
16081da177e4SLinus Torvalds {
160977c24888SBjorn Helgaas 	int result;
161077c24888SBjorn Helgaas 	struct acpi_device *device;
161129aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
16121da177e4SLinus Torvalds 
161336bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
16141da177e4SLinus Torvalds 	if (!device) {
16156468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1616d550d98dSPatrick Mochel 		return -ENOMEM;
16171da177e4SLinus Torvalds 	}
16181da177e4SLinus Torvalds 
1619d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
162063347db0SHans de Goede 	/*
162163347db0SHans de Goede 	 * For ACPI_BUS_TYPE_DEVICE getting the status is delayed till here so
162263347db0SHans de Goede 	 * that we can call acpi_bus_get_status() and use its quirk handling.
162363347db0SHans de Goede 	 * Note this must be done before the get power-/wakeup_dev-flags calls.
162463347db0SHans de Goede 	 */
162563347db0SHans de Goede 	if (type == ACPI_BUS_TYPE_DEVICE)
16265971b0c1SHans de Goede 		if (acpi_bus_get_status(device) < 0)
16275971b0c1SHans de Goede 			acpi_set_device_status(device, 0);
162863347db0SHans de Goede 
1629d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1630d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
16311da177e4SLinus Torvalds 
1632cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1633d43e167dSRafael J. Wysocki 	if (result) {
1634718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1635d550d98dSPatrick Mochel 		return result;
16361da177e4SLinus Torvalds 	}
16371da177e4SLinus Torvalds 
1638bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1639cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
16401da177e4SLinus Torvalds 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1641d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1642d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1643d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
16441da177e4SLinus Torvalds 	kfree(buffer.pointer);
16451da177e4SLinus Torvalds 	*child = device;
1646d43e167dSRafael J. Wysocki 	return 0;
1647bf325f95SRafael J. Wysocki }
1648bf325f95SRafael J. Wysocki 
1649a4e081b0SShannon Zhao static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1650a4e081b0SShannon Zhao 					    void *context)
1651a4e081b0SShannon Zhao {
1652a4e081b0SShannon Zhao 	struct resource *res = context;
1653a4e081b0SShannon Zhao 
1654a4e081b0SShannon Zhao 	if (acpi_dev_resource_memory(ares, res))
1655a4e081b0SShannon Zhao 		return AE_CTRL_TERMINATE;
1656a4e081b0SShannon Zhao 
1657a4e081b0SShannon Zhao 	return AE_OK;
1658a4e081b0SShannon Zhao }
1659a4e081b0SShannon Zhao 
1660a4e081b0SShannon Zhao static bool acpi_device_should_be_hidden(acpi_handle handle)
1661a4e081b0SShannon Zhao {
1662a4e081b0SShannon Zhao 	acpi_status status;
1663a4e081b0SShannon Zhao 	struct resource res;
1664a4e081b0SShannon Zhao 
1665a4e081b0SShannon Zhao 	/* Check if it should ignore the UART device */
1666a4e081b0SShannon Zhao 	if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1667a4e081b0SShannon Zhao 		return false;
1668a4e081b0SShannon Zhao 
1669a4e081b0SShannon Zhao 	/*
1670a4e081b0SShannon Zhao 	 * The UART device described in SPCR table is assumed to have only one
1671a4e081b0SShannon Zhao 	 * memory resource present. So we only look for the first one here.
1672a4e081b0SShannon Zhao 	 */
1673a4e081b0SShannon Zhao 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1674a4e081b0SShannon Zhao 				     acpi_get_resource_memory, &res);
1675a4e081b0SShannon Zhao 	if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1676a4e081b0SShannon Zhao 		return false;
1677a4e081b0SShannon Zhao 
1678a4e081b0SShannon Zhao 	acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1679a4e081b0SShannon Zhao 			 &res.start);
1680a4e081b0SShannon Zhao 
1681a4e081b0SShannon Zhao 	return true;
1682a4e081b0SShannon Zhao }
1683a4e081b0SShannon Zhao 
1684778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1685778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1686778cbc1dSBjorn Helgaas {
1687778cbc1dSBjorn Helgaas 	acpi_status status;
1688778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1689778cbc1dSBjorn Helgaas 
1690778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1691778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1692778cbc1dSBjorn Helgaas 		return -ENODEV;
1693778cbc1dSBjorn Helgaas 
1694778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1695778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1696778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1697a4e081b0SShannon Zhao 		if (acpi_device_should_be_hidden(handle))
1698a4e081b0SShannon Zhao 			return -ENODEV;
1699a4e081b0SShannon Zhao 
1700778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
170163347db0SHans de Goede 		/*
170263347db0SHans de Goede 		 * acpi_add_single_object updates this once we've an acpi_device
170363347db0SHans de Goede 		 * so that acpi_bus_get_status' quirk handling can be used.
170463347db0SHans de Goede 		 */
17055971b0c1SHans de Goede 		*sta = ACPI_STA_DEFAULT;
1706778cbc1dSBjorn Helgaas 		break;
1707778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1708778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1709778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1710778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1711778cbc1dSBjorn Helgaas 			return -ENODEV;
1712778cbc1dSBjorn Helgaas 		break;
1713778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1714778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1715778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1716778cbc1dSBjorn Helgaas 		break;
1717778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1718778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1719778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1720778cbc1dSBjorn Helgaas 		break;
1721778cbc1dSBjorn Helgaas 	default:
1722778cbc1dSBjorn Helgaas 		return -ENODEV;
1723778cbc1dSBjorn Helgaas 	}
1724778cbc1dSBjorn Helgaas 
1725778cbc1dSBjorn Helgaas 	return 0;
1726778cbc1dSBjorn Helgaas }
1727778cbc1dSBjorn Helgaas 
1728cde1f95fSSakari Ailus bool acpi_device_is_present(const struct acpi_device *adev)
1729202317a5SRafael J. Wysocki {
1730cde1f95fSSakari Ailus 	return adev->status.present || adev->status.functional;
1731202317a5SRafael J. Wysocki }
1732202317a5SRafael J. Wysocki 
17334b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1734844142c3SRasmus Villemoes 				       const char *idstr,
1735c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1736c5698074SRafael J. Wysocki {
1737c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1738c5698074SRafael J. Wysocki 
1739aca0a4ebSRafael J. Wysocki 	if (handler->match)
1740aca0a4ebSRafael J. Wysocki 		return handler->match(idstr, matchid);
1741aca0a4ebSRafael J. Wysocki 
1742c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1743c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1744c5698074SRafael J. Wysocki 			if (matchid)
1745c5698074SRafael J. Wysocki 				*matchid = devid;
1746c5698074SRafael J. Wysocki 
17474b59cc1fSRafael J. Wysocki 			return true;
17484b59cc1fSRafael J. Wysocki 		}
17494b59cc1fSRafael J. Wysocki 
17504b59cc1fSRafael J. Wysocki 	return false;
17514b59cc1fSRafael J. Wysocki }
17524b59cc1fSRafael J. Wysocki 
1753844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
17546b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
17556b772e8fSToshi Kani {
17566b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
17576b772e8fSToshi Kani 
17586b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
17596b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
17606b772e8fSToshi Kani 			return handler;
17616b772e8fSToshi Kani 
17626b772e8fSToshi Kani 	return NULL;
17636b772e8fSToshi Kani }
17646b772e8fSToshi Kani 
17653f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
17663f8055c3SRafael J. Wysocki {
17673f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
17683f8055c3SRafael J. Wysocki 		return;
17693f8055c3SRafael J. Wysocki 
17703f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
17713f8055c3SRafael J. Wysocki 
17723f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
17733f8055c3SRafael J. Wysocki 
17743f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
17753f8055c3SRafael J. Wysocki }
17763f8055c3SRafael J. Wysocki 
17773c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev)
17784b59cc1fSRafael J. Wysocki {
17796b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
17803c2cc7ffSRafael J. Wysocki 
1781b43109faSRafael J. Wysocki 	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
17821e2380cdSRafael J. Wysocki 		acpi_dock_add(adev);
17831e2380cdSRafael J. Wysocki 		return;
17841e2380cdSRafael J. Wysocki 	}
17853c2cc7ffSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
17864b59cc1fSRafael J. Wysocki 		struct acpi_scan_handler *handler;
17874b59cc1fSRafael J. Wysocki 
17886b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
17891a699476SRafael J. Wysocki 		if (handler) {
17903c2cc7ffSRafael J. Wysocki 			adev->flags.hotplug_notify = true;
17916b772e8fSToshi Kani 			break;
17926b772e8fSToshi Kani 		}
17936b772e8fSToshi Kani 	}
17941a699476SRafael J. Wysocki }
17956b772e8fSToshi Kani 
179640e7fcb1SLan Tianyu static void acpi_device_dep_initialize(struct acpi_device *adev)
179740e7fcb1SLan Tianyu {
179840e7fcb1SLan Tianyu 	struct acpi_dep_data *dep;
179940e7fcb1SLan Tianyu 	struct acpi_handle_list dep_devices;
180040e7fcb1SLan Tianyu 	acpi_status status;
180140e7fcb1SLan Tianyu 	int i;
180240e7fcb1SLan Tianyu 
180363347db0SHans de Goede 	adev->dep_unmet = 0;
180463347db0SHans de Goede 
180540e7fcb1SLan Tianyu 	if (!acpi_has_method(adev->handle, "_DEP"))
180640e7fcb1SLan Tianyu 		return;
180740e7fcb1SLan Tianyu 
180840e7fcb1SLan Tianyu 	status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
180940e7fcb1SLan Tianyu 					&dep_devices);
181040e7fcb1SLan Tianyu 	if (ACPI_FAILURE(status)) {
181180167a24SRafael J. Wysocki 		dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
181240e7fcb1SLan Tianyu 		return;
181340e7fcb1SLan Tianyu 	}
181440e7fcb1SLan Tianyu 
181540e7fcb1SLan Tianyu 	for (i = 0; i < dep_devices.count; i++) {
181640e7fcb1SLan Tianyu 		struct acpi_device_info *info;
181740e7fcb1SLan Tianyu 		int skip;
181840e7fcb1SLan Tianyu 
181940e7fcb1SLan Tianyu 		status = acpi_get_object_info(dep_devices.handles[i], &info);
182040e7fcb1SLan Tianyu 		if (ACPI_FAILURE(status)) {
182180167a24SRafael J. Wysocki 			dev_dbg(&adev->dev, "Error reading _DEP device info\n");
182240e7fcb1SLan Tianyu 			continue;
182340e7fcb1SLan Tianyu 		}
182440e7fcb1SLan Tianyu 
182540e7fcb1SLan Tianyu 		/*
182640e7fcb1SLan Tianyu 		 * Skip the dependency of Windows System Power
182740e7fcb1SLan Tianyu 		 * Management Controller
182840e7fcb1SLan Tianyu 		 */
182940e7fcb1SLan Tianyu 		skip = info->valid & ACPI_VALID_HID &&
183040e7fcb1SLan Tianyu 			!strcmp(info->hardware_id.string, "INT3396");
183140e7fcb1SLan Tianyu 
183240e7fcb1SLan Tianyu 		kfree(info);
183340e7fcb1SLan Tianyu 
183440e7fcb1SLan Tianyu 		if (skip)
183540e7fcb1SLan Tianyu 			continue;
183640e7fcb1SLan Tianyu 
183740e7fcb1SLan Tianyu 		dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
183840e7fcb1SLan Tianyu 		if (!dep)
183940e7fcb1SLan Tianyu 			return;
184040e7fcb1SLan Tianyu 
184140e7fcb1SLan Tianyu 		dep->master = dep_devices.handles[i];
184240e7fcb1SLan Tianyu 		dep->slave  = adev->handle;
184340e7fcb1SLan Tianyu 		adev->dep_unmet++;
184440e7fcb1SLan Tianyu 
184540e7fcb1SLan Tianyu 		mutex_lock(&acpi_dep_list_lock);
184640e7fcb1SLan Tianyu 		list_add_tail(&dep->node , &acpi_dep_list);
184740e7fcb1SLan Tianyu 		mutex_unlock(&acpi_dep_list_lock);
184840e7fcb1SLan Tianyu 	}
184940e7fcb1SLan Tianyu }
185040e7fcb1SLan Tianyu 
1851e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1852e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
18531da177e4SLinus Torvalds {
1854805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1855778cbc1dSBjorn Helgaas 	int type;
1856778cbc1dSBjorn Helgaas 	unsigned long long sta;
1857778cbc1dSBjorn Helgaas 	int result;
18581da177e4SLinus Torvalds 
18594002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
18604002bf38SRafael J. Wysocki 	if (device)
18614002bf38SRafael J. Wysocki 		goto out;
18624002bf38SRafael J. Wysocki 
1863778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1864778cbc1dSBjorn Helgaas 	if (result)
186551a85fafSBjorn Helgaas 		return AE_OK;
18661da177e4SLinus Torvalds 
186782c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
186882c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
186982c7d5efSRafael J. Wysocki 		return AE_OK;
187082c7d5efSRafael J. Wysocki 	}
187182c7d5efSRafael J. Wysocki 
18722c0d4fe0SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta);
1873e3b87f8aSBjorn Helgaas 	if (!device)
187451a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
18751da177e4SLinus Torvalds 
18763c2cc7ffSRafael J. Wysocki 	acpi_scan_init_hotplug(device);
187740e7fcb1SLan Tianyu 	acpi_device_dep_initialize(device);
18783c2cc7ffSRafael J. Wysocki 
18794002bf38SRafael J. Wysocki  out:
188051a85fafSBjorn Helgaas 	if (!*return_value)
188151a85fafSBjorn Helgaas 		*return_value = device;
1882805d410fSRafael J. Wysocki 
188351a85fafSBjorn Helgaas 	return AE_OK;
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds 
188648459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device)
188748459340SZhang Rui {
188848459340SZhang Rui 	/*
1889d87fb091SJohn Garry 	 * Do not enumerate devices with enumeration_by_parent flag set as
1890d87fb091SJohn Garry 	 * they will be enumerated by their respective parents.
189148459340SZhang Rui 	 */
1892d87fb091SJohn Garry 	if (!device->flags.enumeration_by_parent) {
18931571875bSHeikki Krogerus 		acpi_create_platform_device(device, NULL);
189410c7e20bSOctavian Purdila 		acpi_device_set_enumerated(device);
189568bdb677SOctavian Purdila 	} else {
189668bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
189768bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_ADD, device);
189810c7e20bSOctavian Purdila 	}
189948459340SZhang Rui }
190048459340SZhang Rui 
19017d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = {
1902ee892094SRafael J. Wysocki 	{ACPI_DT_NAMESPACE_HID, },
19037d284352SRafael J. Wysocki 	{"", },
19047d284352SRafael J. Wysocki };
19057d284352SRafael J. Wysocki 
19067d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev,
19077d284352SRafael J. Wysocki 				      const struct acpi_device_id *not_used)
19087d284352SRafael J. Wysocki {
19097d284352SRafael J. Wysocki 	/*
1910ee892094SRafael J. Wysocki 	 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
1911ee892094SRafael J. Wysocki 	 * below can be unconditional.
19127d284352SRafael J. Wysocki 	 */
19137d284352SRafael J. Wysocki 	if (adev->data.of_compatible)
19147d284352SRafael J. Wysocki 		acpi_default_enumeration(adev);
19157d284352SRafael J. Wysocki 
19167d284352SRafael J. Wysocki 	return 1;
19177d284352SRafael J. Wysocki }
19187d284352SRafael J. Wysocki 
19197d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = {
19207d284352SRafael J. Wysocki 	.ids = generic_device_ids,
19217d284352SRafael J. Wysocki 	.attach = acpi_generic_device_attach,
19227d284352SRafael J. Wysocki };
19237d284352SRafael J. Wysocki 
192487b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
192587b85b3cSRafael J. Wysocki {
192687b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
192787b85b3cSRafael J. Wysocki 	int ret = 0;
192887b85b3cSRafael J. Wysocki 
192987b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
1930c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
1931c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
193287b85b3cSRafael J. Wysocki 
1933c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
1934c5698074SRafael J. Wysocki 		if (handler) {
1935d34afa9dSRafael J. Wysocki 			if (!handler->attach) {
1936d34afa9dSRafael J. Wysocki 				device->pnp.type.platform_id = 0;
1937d34afa9dSRafael J. Wysocki 				continue;
1938d34afa9dSRafael J. Wysocki 			}
1939c5698074SRafael J. Wysocki 			device->handler = handler;
19409cb32acfSRafael J. Wysocki 			ret = handler->attach(device, devid);
19419cb32acfSRafael J. Wysocki 			if (ret > 0)
1942c5698074SRafael J. Wysocki 				break;
19439cb32acfSRafael J. Wysocki 
19449cb32acfSRafael J. Wysocki 			device->handler = NULL;
19459cb32acfSRafael J. Wysocki 			if (ret < 0)
1946c5698074SRafael J. Wysocki 				break;
1947c5698074SRafael J. Wysocki 		}
1948c5698074SRafael J. Wysocki 	}
194948459340SZhang Rui 
1950ca589f94SRafael J. Wysocki 	return ret;
1951ca589f94SRafael J. Wysocki }
1952ca589f94SRafael J. Wysocki 
19532c22e652SRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device)
195451a85fafSBjorn Helgaas {
19552c22e652SRafael J. Wysocki 	struct acpi_device *child;
19561e2380cdSRafael J. Wysocki 	acpi_handle ejd;
1957ca589f94SRafael J. Wysocki 	int ret;
19581da177e4SLinus Torvalds 
19591e2380cdSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
19601e2380cdSRafael J. Wysocki 		register_dock_dependent_device(device, ejd);
19611e2380cdSRafael J. Wysocki 
19622c22e652SRafael J. Wysocki 	acpi_bus_get_status(device);
1963202317a5SRafael J. Wysocki 	/* Skip devices that are not present. */
19642c22e652SRafael J. Wysocki 	if (!acpi_device_is_present(device)) {
1965cde1f95fSSakari Ailus 		device->flags.initialized = false;
196610c7e20bSOctavian Purdila 		acpi_device_clear_enumerated(device);
19671b1f3e16SRafael J. Wysocki 		device->flags.power_manageable = 0;
19682c22e652SRafael J. Wysocki 		return;
19692c22e652SRafael J. Wysocki 	}
19703a391a39SRafael J. Wysocki 	if (device->handler)
19712c22e652SRafael J. Wysocki 		goto ok;
19723a391a39SRafael J. Wysocki 
1973202317a5SRafael J. Wysocki 	if (!device->flags.initialized) {
19741b1f3e16SRafael J. Wysocki 		device->flags.power_manageable =
19751b1f3e16SRafael J. Wysocki 			device->power.states[ACPI_STATE_D0].flags.valid;
19761b1f3e16SRafael J. Wysocki 		if (acpi_bus_init_power(device))
19771b1f3e16SRafael J. Wysocki 			device->flags.power_manageable = 0;
19781b1f3e16SRafael J. Wysocki 
1979202317a5SRafael J. Wysocki 		device->flags.initialized = true;
1980c381fc3aSRafael J. Wysocki 	} else if (device->flags.visited) {
1981c381fc3aSRafael J. Wysocki 		goto ok;
1982202317a5SRafael J. Wysocki 	}
198310c7e20bSOctavian Purdila 
1984ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
19856931007cSRafael J. Wysocki 	if (ret < 0)
19862c22e652SRafael J. Wysocki 		return;
19876931007cSRafael J. Wysocki 
19886931007cSRafael J. Wysocki 	device->flags.match_driver = true;
1989d87fb091SJohn Garry 	if (ret > 0 && !device->flags.enumeration_by_parent) {
1990f406270bSRafael J. Wysocki 		acpi_device_set_enumerated(device);
1991f406270bSRafael J. Wysocki 		goto ok;
1992f406270bSRafael J. Wysocki 	}
1993f406270bSRafael J. Wysocki 
1994ca589f94SRafael J. Wysocki 	ret = device_attach(&device->dev);
1995202317a5SRafael J. Wysocki 	if (ret < 0)
19962c22e652SRafael J. Wysocki 		return;
19977d284352SRafael J. Wysocki 
1998d87fb091SJohn Garry 	if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
1999e4330d8bSJarkko Nikula 		acpi_default_enumeration(device);
2000d87fb091SJohn Garry 	else
2001d87fb091SJohn Garry 		acpi_device_set_enumerated(device);
2002202317a5SRafael J. Wysocki 
2003202317a5SRafael J. Wysocki  ok:
20042c22e652SRafael J. Wysocki 	list_for_each_entry(child, &device->children, node)
20052c22e652SRafael J. Wysocki 		acpi_bus_attach(child);
20068ab17fc9SRafael J. Wysocki 
20078ab17fc9SRafael J. Wysocki 	if (device->handler && device->handler->hotplug.notify_online)
20088ab17fc9SRafael J. Wysocki 		device->handler->hotplug.notify_online(device);
2009805d410fSRafael J. Wysocki }
20103fb02738SRajesh Shah 
201140e7fcb1SLan Tianyu void acpi_walk_dep_device_list(acpi_handle handle)
201240e7fcb1SLan Tianyu {
201340e7fcb1SLan Tianyu 	struct acpi_dep_data *dep, *tmp;
201440e7fcb1SLan Tianyu 	struct acpi_device *adev;
201540e7fcb1SLan Tianyu 
201640e7fcb1SLan Tianyu 	mutex_lock(&acpi_dep_list_lock);
201740e7fcb1SLan Tianyu 	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
201840e7fcb1SLan Tianyu 		if (dep->master == handle) {
201940e7fcb1SLan Tianyu 			acpi_bus_get_device(dep->slave, &adev);
202040e7fcb1SLan Tianyu 			if (!adev)
202140e7fcb1SLan Tianyu 				continue;
202240e7fcb1SLan Tianyu 
202340e7fcb1SLan Tianyu 			adev->dep_unmet--;
202440e7fcb1SLan Tianyu 			if (!adev->dep_unmet)
202540e7fcb1SLan Tianyu 				acpi_bus_attach(adev);
202640e7fcb1SLan Tianyu 			list_del(&dep->node);
202740e7fcb1SLan Tianyu 			kfree(dep);
202840e7fcb1SLan Tianyu 		}
202940e7fcb1SLan Tianyu 	}
203040e7fcb1SLan Tianyu 	mutex_unlock(&acpi_dep_list_lock);
203140e7fcb1SLan Tianyu }
203240e7fcb1SLan Tianyu EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
203340e7fcb1SLan Tianyu 
2034b8bd759aSRafael J. Wysocki /**
2035b8bd759aSRafael J. Wysocki  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2036b8bd759aSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
2037b8bd759aSRafael J. Wysocki  *
2038b8bd759aSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2039b8bd759aSRafael J. Wysocki  * found devices.
2040b8bd759aSRafael J. Wysocki  *
2041b8bd759aSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
2042b8bd759aSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
2043b8bd759aSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
2044b8bd759aSRafael J. Wysocki  * appropriate driver.
20453757b948SRafael J. Wysocki  *
20463757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
2047b8bd759aSRafael J. Wysocki  */
2048b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle)
20493fb02738SRajesh Shah {
20501da177e4SLinus Torvalds 	void *device = NULL;
2051c511cc19SRafael J. Wysocki 
20520cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
20531da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2054e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
20553fb02738SRajesh Shah 
20562c22e652SRafael J. Wysocki 	if (device) {
20572c22e652SRafael J. Wysocki 		acpi_bus_attach(device);
20582c22e652SRafael J. Wysocki 		return 0;
20592c22e652SRafael J. Wysocki 	}
20602c22e652SRafael J. Wysocki 	return -ENODEV;
20613fb02738SRajesh Shah }
2062b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan);
20631da177e4SLinus Torvalds 
20643757b948SRafael J. Wysocki /**
20652c22e652SRafael J. Wysocki  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
20662c22e652SRafael J. Wysocki  * @adev: Root of the ACPI namespace scope to walk.
20673757b948SRafael J. Wysocki  *
20683757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
20693757b948SRafael J. Wysocki  */
20702c22e652SRafael J. Wysocki void acpi_bus_trim(struct acpi_device *adev)
20711da177e4SLinus Torvalds {
20722c22e652SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
20732c22e652SRafael J. Wysocki 	struct acpi_device *child;
20742c22e652SRafael J. Wysocki 
20752c22e652SRafael J. Wysocki 	list_for_each_entry_reverse(child, &adev->children, node)
20762c22e652SRafael J. Wysocki 		acpi_bus_trim(child);
20772c22e652SRafael J. Wysocki 
2078a951c773SRafael J. Wysocki 	adev->flags.match_driver = false;
20792c22e652SRafael J. Wysocki 	if (handler) {
20802c22e652SRafael J. Wysocki 		if (handler->detach)
20812c22e652SRafael J. Wysocki 			handler->detach(adev);
20822c22e652SRafael J. Wysocki 
20832c22e652SRafael J. Wysocki 		adev->handler = NULL;
20842c22e652SRafael J. Wysocki 	} else {
20852c22e652SRafael J. Wysocki 		device_release_driver(&adev->dev);
20862c22e652SRafael J. Wysocki 	}
20871da177e4SLinus Torvalds 	/*
20882c22e652SRafael J. Wysocki 	 * Most likely, the device is going away, so put it into D3cold before
20892c22e652SRafael J. Wysocki 	 * that.
20901da177e4SLinus Torvalds 	 */
20912c22e652SRafael J. Wysocki 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
20922c22e652SRafael J. Wysocki 	adev->flags.initialized = false;
209310c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(adev);
20941da177e4SLinus Torvalds }
2095ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
2096ceaba663SKristen Accardi 
2097a64a62ceSLv Zheng int acpi_bus_register_early_device(int type)
2098a64a62ceSLv Zheng {
2099a64a62ceSLv Zheng 	struct acpi_device *device = NULL;
2100a64a62ceSLv Zheng 	int result;
2101a64a62ceSLv Zheng 
2102a64a62ceSLv Zheng 	result = acpi_add_single_object(&device, NULL,
2103a64a62ceSLv Zheng 					type, ACPI_STA_DEFAULT);
2104a64a62ceSLv Zheng 	if (result)
2105a64a62ceSLv Zheng 		return result;
2106a64a62ceSLv Zheng 
2107a64a62ceSLv Zheng 	device->flags.match_driver = true;
2108a64a62ceSLv Zheng 	return device_attach(&device->dev);
2109a64a62ceSLv Zheng }
2110a64a62ceSLv Zheng EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2111a64a62ceSLv Zheng 
2112e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
21131da177e4SLinus Torvalds {
21141da177e4SLinus Torvalds 	int result = 0;
2115c4168bffSLi Shaohua 
21161da177e4SLinus Torvalds 	/*
21171da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
21181da177e4SLinus Torvalds 	 */
21192c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
21202c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
21212c0d4fe0SRafael J. Wysocki 
21225c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2123c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
21242c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
21252c0d4fe0SRafael J. Wysocki 		if (result)
21262c0d4fe0SRafael J. Wysocki 			return result;
21272c0d4fe0SRafael J. Wysocki 
212888346167SRafael J. Wysocki 		device->flags.match_driver = true;
21292c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
21302c0d4fe0SRafael J. Wysocki 		if (result < 0)
21312c0d4fe0SRafael J. Wysocki 			return result;
21322c0d4fe0SRafael J. Wysocki 
2133c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
21343fb02738SRajesh Shah 	}
21351da177e4SLinus Torvalds 
21362c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
21372c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
21382c0d4fe0SRafael J. Wysocki 
21395c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2140c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
21412c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
21422c0d4fe0SRafael J. Wysocki 		if (result)
21432c0d4fe0SRafael J. Wysocki 			return result;
21442c0d4fe0SRafael J. Wysocki 
214588346167SRafael J. Wysocki 		device->flags.match_driver = true;
21462c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
21473fb02738SRajesh Shah 	}
21481da177e4SLinus Torvalds 
21492c0d4fe0SRafael J. Wysocki 	return result < 0 ? result : 0;
21501da177e4SLinus Torvalds }
21511da177e4SLinus Torvalds 
2152a4e081b0SShannon Zhao static void __init acpi_get_spcr_uart_addr(void)
2153a4e081b0SShannon Zhao {
2154a4e081b0SShannon Zhao 	acpi_status status;
2155a4e081b0SShannon Zhao 	struct acpi_table_spcr *spcr_ptr;
2156a4e081b0SShannon Zhao 
2157a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_SPCR, 0,
2158a4e081b0SShannon Zhao 				(struct acpi_table_header **)&spcr_ptr);
2159a4e081b0SShannon Zhao 	if (ACPI_SUCCESS(status))
2160a4e081b0SShannon Zhao 		spcr_uart_addr = spcr_ptr->serial_port.address;
2161a4e081b0SShannon Zhao 	else
2162a4e081b0SShannon Zhao 		printk(KERN_WARNING PREFIX "STAO table present, but SPCR is missing\n");
2163a4e081b0SShannon Zhao }
2164a4e081b0SShannon Zhao 
216568bdb677SOctavian Purdila static bool acpi_scan_initialized;
216668bdb677SOctavian Purdila 
2167e747f274SBjorn Helgaas int __init acpi_scan_init(void)
21681da177e4SLinus Torvalds {
21691da177e4SLinus Torvalds 	int result;
2170a4e081b0SShannon Zhao 	acpi_status status;
2171a4e081b0SShannon Zhao 	struct acpi_table_stao *stao_ptr;
21721da177e4SLinus Torvalds 
217392ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
21744daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2175ac212b69SRafael J. Wysocki 	acpi_processor_init();
2176f58b082aSRafael J. Wysocki 	acpi_lpss_init();
217792082a88SKen Xue 	acpi_apd_init();
21782fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2179737f1a9fSRafael J. Wysocki 	acpi_container_init();
21800a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
2181cc6a0e31SMika Westerberg 	acpi_watchdog_init();
2182eec15edbSZhang Rui 	acpi_pnp_init();
21833230bbfcSZhang Rui 	acpi_int340x_thermal_init();
21846ce2e188SGraeme Gregory 	acpi_amba_init();
2185eeb2d80dSSrinivas Pandruvada 	acpi_init_lpit();
218697d9a9e9SRafael J. Wysocki 
21877d284352SRafael J. Wysocki 	acpi_scan_add_handler(&generic_device_handler);
21887d284352SRafael J. Wysocki 
2189a4e081b0SShannon Zhao 	/*
2190a4e081b0SShannon Zhao 	 * If there is STAO table, check whether it needs to ignore the UART
2191a4e081b0SShannon Zhao 	 * device in SPCR table.
2192a4e081b0SShannon Zhao 	 */
2193a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_STAO, 0,
2194a4e081b0SShannon Zhao 				(struct acpi_table_header **)&stao_ptr);
2195a4e081b0SShannon Zhao 	if (ACPI_SUCCESS(status)) {
2196a4e081b0SShannon Zhao 		if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2197a4e081b0SShannon Zhao 			printk(KERN_INFO PREFIX "STAO Name List not yet supported.");
2198a4e081b0SShannon Zhao 
2199a4e081b0SShannon Zhao 		if (stao_ptr->ignore_uart)
2200a4e081b0SShannon Zhao 			acpi_get_spcr_uart_addr();
2201a4e081b0SShannon Zhao 	}
2202a4e081b0SShannon Zhao 
2203eb7f43c4SRafael J. Wysocki 	acpi_gpe_apply_masked_gpes();
2204eb7f43c4SRafael J. Wysocki 	acpi_update_all_gpes();
2205eb7f43c4SRafael J. Wysocki 
22063757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
22071da177e4SLinus Torvalds 	/*
22081da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
22091da177e4SLinus Torvalds 	 */
22100cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
22111da177e4SLinus Torvalds 	if (result)
22123757b948SRafael J. Wysocki 		goto out;
22131da177e4SLinus Torvalds 
22140cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
22151da177e4SLinus Torvalds 	if (result)
22163757b948SRafael J. Wysocki 		goto out;
2217b8bd759aSRafael J. Wysocki 
22182650ef42SAaron Lu 	/* Fixed feature devices do not exist on HW-reduced platform */
22192650ef42SAaron Lu 	if (!acpi_gbl_reduced_hardware) {
2220b8bd759aSRafael J. Wysocki 		result = acpi_bus_scan_fixed();
2221b8bd759aSRafael J. Wysocki 		if (result) {
22222650ef42SAaron Lu 			acpi_detach_data(acpi_root->handle,
22232650ef42SAaron Lu 					 acpi_scan_drop_device);
2224202317a5SRafael J. Wysocki 			acpi_device_del(acpi_root);
2225202317a5SRafael J. Wysocki 			put_device(&acpi_root->dev);
22263757b948SRafael J. Wysocki 			goto out;
2227b8bd759aSRafael J. Wysocki 		}
22282650ef42SAaron Lu 	}
2229b8bd759aSRafael J. Wysocki 
223068bdb677SOctavian Purdila 	acpi_scan_initialized = true;
223168bdb677SOctavian Purdila 
22323757b948SRafael J. Wysocki  out:
22333757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
22343757b948SRafael J. Wysocki 	return result;
22351da177e4SLinus Torvalds }
2236e647b532SMarc Zyngier 
2237e647b532SMarc Zyngier static struct acpi_probe_entry *ape;
2238e647b532SMarc Zyngier static int acpi_probe_count;
22395331d9caSLorenzo Pieralisi static DEFINE_MUTEX(acpi_probe_mutex);
2240e647b532SMarc Zyngier 
2241e647b532SMarc Zyngier static int __init acpi_match_madt(struct acpi_subtable_header *header,
2242e647b532SMarc Zyngier 				  const unsigned long end)
2243e647b532SMarc Zyngier {
2244e647b532SMarc Zyngier 	if (!ape->subtable_valid || ape->subtable_valid(header, ape))
2245e647b532SMarc Zyngier 		if (!ape->probe_subtbl(header, end))
2246e647b532SMarc Zyngier 			acpi_probe_count++;
2247e647b532SMarc Zyngier 
2248e647b532SMarc Zyngier 	return 0;
2249e647b532SMarc Zyngier }
2250e647b532SMarc Zyngier 
2251e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2252e647b532SMarc Zyngier {
2253e647b532SMarc Zyngier 	int count = 0;
2254e647b532SMarc Zyngier 
2255e647b532SMarc Zyngier 	if (acpi_disabled)
2256e647b532SMarc Zyngier 		return 0;
2257e647b532SMarc Zyngier 
22585331d9caSLorenzo Pieralisi 	mutex_lock(&acpi_probe_mutex);
2259e647b532SMarc Zyngier 	for (ape = ap_head; nr; ape++, nr--) {
2260e647b532SMarc Zyngier 		if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
2261e647b532SMarc Zyngier 			acpi_probe_count = 0;
2262e647b532SMarc Zyngier 			acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2263e647b532SMarc Zyngier 			count += acpi_probe_count;
2264e647b532SMarc Zyngier 		} else {
2265e647b532SMarc Zyngier 			int res;
2266e647b532SMarc Zyngier 			res = acpi_table_parse(ape->id, ape->probe_table);
2267e647b532SMarc Zyngier 			if (!res)
2268e647b532SMarc Zyngier 				count++;
2269e647b532SMarc Zyngier 		}
2270e647b532SMarc Zyngier 	}
22715331d9caSLorenzo Pieralisi 	mutex_unlock(&acpi_probe_mutex);
2272e647b532SMarc Zyngier 
2273e647b532SMarc Zyngier 	return count;
2274e647b532SMarc Zyngier }
227568bdb677SOctavian Purdila 
227668bdb677SOctavian Purdila struct acpi_table_events_work {
227768bdb677SOctavian Purdila 	struct work_struct work;
227868bdb677SOctavian Purdila 	void *table;
227968bdb677SOctavian Purdila 	u32 event;
228068bdb677SOctavian Purdila };
228168bdb677SOctavian Purdila 
228268bdb677SOctavian Purdila static void acpi_table_events_fn(struct work_struct *work)
228368bdb677SOctavian Purdila {
228468bdb677SOctavian Purdila 	struct acpi_table_events_work *tew;
228568bdb677SOctavian Purdila 
228668bdb677SOctavian Purdila 	tew = container_of(work, struct acpi_table_events_work, work);
228768bdb677SOctavian Purdila 
228868bdb677SOctavian Purdila 	if (tew->event == ACPI_TABLE_EVENT_LOAD) {
228968bdb677SOctavian Purdila 		acpi_scan_lock_acquire();
229068bdb677SOctavian Purdila 		acpi_bus_scan(ACPI_ROOT_OBJECT);
229168bdb677SOctavian Purdila 		acpi_scan_lock_release();
229268bdb677SOctavian Purdila 	}
229368bdb677SOctavian Purdila 
229468bdb677SOctavian Purdila 	kfree(tew);
229568bdb677SOctavian Purdila }
229668bdb677SOctavian Purdila 
229768bdb677SOctavian Purdila void acpi_scan_table_handler(u32 event, void *table, void *context)
229868bdb677SOctavian Purdila {
229968bdb677SOctavian Purdila 	struct acpi_table_events_work *tew;
230068bdb677SOctavian Purdila 
230168bdb677SOctavian Purdila 	if (!acpi_scan_initialized)
230268bdb677SOctavian Purdila 		return;
230368bdb677SOctavian Purdila 
230468bdb677SOctavian Purdila 	if (event != ACPI_TABLE_EVENT_LOAD)
230568bdb677SOctavian Purdila 		return;
230668bdb677SOctavian Purdila 
230768bdb677SOctavian Purdila 	tew = kmalloc(sizeof(*tew), GFP_KERNEL);
230868bdb677SOctavian Purdila 	if (!tew)
230968bdb677SOctavian Purdila 		return;
231068bdb677SOctavian Purdila 
231168bdb677SOctavian Purdila 	INIT_WORK(&tew->work, acpi_table_events_fn);
231268bdb677SOctavian Purdila 	tew->table = table;
231368bdb677SOctavian Purdila 	tew->event = event;
231468bdb677SOctavian Purdila 
231568bdb677SOctavian Purdila 	schedule_work(&tew->work);
231668bdb677SOctavian Purdila }
231768bdb677SOctavian Purdila 
231868bdb677SOctavian Purdila int acpi_reconfig_notifier_register(struct notifier_block *nb)
231968bdb677SOctavian Purdila {
232068bdb677SOctavian Purdila 	return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
232168bdb677SOctavian Purdila }
232268bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_register);
232368bdb677SOctavian Purdila 
232468bdb677SOctavian Purdila int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
232568bdb677SOctavian Purdila {
232668bdb677SOctavian Purdila 	return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
232768bdb677SOctavian Purdila }
232868bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);
2329