xref: /openbmc/linux/drivers/acpi/scan.c (revision 643b8e4d86f8b1a62cf5cd9ea221e9bc0d531d18)
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>
10*643b8e4dSLorenzo 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>
161da177e4SLinus Torvalds 
17d783156eSRafael J. Wysocki #include <asm/pgtable.h>
18d783156eSRafael J. Wysocki 
19e60cc7a6SBjorn Helgaas #include "internal.h"
20e60cc7a6SBjorn Helgaas 
211da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
22f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
231da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2629b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
271da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
281da177e4SLinus Torvalds 
29859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
30859ac9a4SBjorn Helgaas 
31d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)
32d783156eSRafael J. Wysocki 
33683058e3SRafael J. Wysocki /*
34683058e3SRafael J. Wysocki  * If set, devices will be hot-removed even if they cannot be put offline
35683058e3SRafael J. Wysocki  * gracefully (from the kernel's standpoint).
36683058e3SRafael J. Wysocki  */
37683058e3SRafael J. Wysocki bool acpi_force_hot_remove;
38683058e3SRafael J. Wysocki 
39620e112cSThomas Renninger static const char *dummy_hid = "device";
402b2ae7c7SThomas Renninger 
4140e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list);
4240e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock);
432d12b6b3SLukas Wunner LIST_HEAD(acpi_bus_id_list);
44c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
45ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
469090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
471da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
48e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock);
491da177e4SLinus Torvalds 
50a4e081b0SShannon Zhao /*
51a4e081b0SShannon Zhao  * The UART device described by the SPCR table is the only object which needs
52a4e081b0SShannon Zhao  * special-casing. Everything else is covered by ACPI namespace paths in STAO
53a4e081b0SShannon Zhao  * table.
54a4e081b0SShannon Zhao  */
55a4e081b0SShannon Zhao static u64 spcr_uart_addr;
56a4e081b0SShannon Zhao 
5740e7fcb1SLan Tianyu struct acpi_dep_data {
5840e7fcb1SLan Tianyu 	struct list_head node;
5940e7fcb1SLan Tianyu 	acpi_handle master;
6040e7fcb1SLan Tianyu 	acpi_handle slave;
6140e7fcb1SLan Tianyu };
6240e7fcb1SLan Tianyu 
633757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
643757b948SRafael J. Wysocki {
653757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
663757b948SRafael J. Wysocki }
673757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
683757b948SRafael J. Wysocki 
693757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
703757b948SRafael J. Wysocki {
713757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
723757b948SRafael J. Wysocki }
733757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
743757b948SRafael J. Wysocki 
75e525506fSRafael J. Wysocki void acpi_lock_hp_context(void)
76e525506fSRafael J. Wysocki {
77e525506fSRafael J. Wysocki 	mutex_lock(&acpi_hp_context_lock);
78e525506fSRafael J. Wysocki }
79e525506fSRafael J. Wysocki 
80e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void)
81e525506fSRafael J. Wysocki {
82e525506fSRafael J. Wysocki 	mutex_unlock(&acpi_hp_context_lock);
83e525506fSRafael J. Wysocki }
84e525506fSRafael J. Wysocki 
855d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev,
865d513205SRafael J. Wysocki 				struct acpi_hotplug_context *hp,
875d513205SRafael J. Wysocki 				int (*notify)(struct acpi_device *, u32),
885d513205SRafael J. Wysocki 				void (*uevent)(struct acpi_device *, u32))
895d513205SRafael J. Wysocki {
905d513205SRafael J. Wysocki 	acpi_lock_hp_context();
91ba574dc8SRafael J. Wysocki 	hp->notify = notify;
92ba574dc8SRafael J. Wysocki 	hp->uevent = uevent;
93ba574dc8SRafael J. Wysocki 	acpi_set_hp_context(adev, hp);
945d513205SRafael J. Wysocki 	acpi_unlock_hp_context();
955d513205SRafael J. Wysocki }
965d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
975d513205SRafael J. Wysocki 
98ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
99ca589f94SRafael J. Wysocki {
100d34afa9dSRafael J. Wysocki 	if (!handler)
101ca589f94SRafael J. Wysocki 		return -EINVAL;
102ca589f94SRafael J. Wysocki 
103ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
104ca589f94SRafael J. Wysocki 	return 0;
105ca589f94SRafael J. Wysocki }
106ca589f94SRafael J. Wysocki 
1073f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
1083f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
1093f8055c3SRafael J. Wysocki {
1103f8055c3SRafael J. Wysocki 	int error;
1113f8055c3SRafael J. Wysocki 
1123f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
1133f8055c3SRafael J. Wysocki 	if (error)
1143f8055c3SRafael J. Wysocki 		return error;
1153f8055c3SRafael J. Wysocki 
1163f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
1173f8055c3SRafael J. Wysocki 	return 0;
1183f8055c3SRafael J. Wysocki }
1193f8055c3SRafael J. Wysocki 
120caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
121d22ddcbcSRafael J. Wysocki {
122d22ddcbcSRafael J. Wysocki 	struct acpi_device_physical_node *pn;
123d22ddcbcSRafael J. Wysocki 	bool offline = true;
124d22ddcbcSRafael J. Wysocki 
1254c533c80SRafael J. Wysocki 	/*
1264c533c80SRafael J. Wysocki 	 * acpi_container_offline() calls this for all of the container's
1274c533c80SRafael J. Wysocki 	 * children under the container's physical_node_lock lock.
1284c533c80SRafael J. Wysocki 	 */
1294c533c80SRafael J. Wysocki 	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
130d22ddcbcSRafael J. Wysocki 
131d22ddcbcSRafael J. Wysocki 	list_for_each_entry(pn, &adev->physical_node_list, node)
132d22ddcbcSRafael J. Wysocki 		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
133caa73ea1SRafael J. Wysocki 			if (uevent)
134d22ddcbcSRafael J. Wysocki 				kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
135caa73ea1SRafael J. Wysocki 
136d22ddcbcSRafael J. Wysocki 			offline = false;
137d22ddcbcSRafael J. Wysocki 			break;
138d22ddcbcSRafael J. Wysocki 		}
139d22ddcbcSRafael J. Wysocki 
140d22ddcbcSRafael J. Wysocki 	mutex_unlock(&adev->physical_node_lock);
141d22ddcbcSRafael J. Wysocki 	return offline;
142d22ddcbcSRafael J. Wysocki }
143d22ddcbcSRafael J. Wysocki 
1447f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
1457f28ddecSRafael J. Wysocki 				    void **ret_p)
146683058e3SRafael J. Wysocki {
147683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
148683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
149303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
150683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
151683058e3SRafael J. Wysocki 
152683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
153683058e3SRafael J. Wysocki 		return AE_OK;
154683058e3SRafael J. Wysocki 
1557f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
1567f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
1577f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
1587f28ddecSRafael J. Wysocki 	}
1597f28ddecSRafael J. Wysocki 
160683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
161683058e3SRafael J. Wysocki 
162683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
163683058e3SRafael J. Wysocki 		int ret;
164683058e3SRafael J. Wysocki 
165303bfdb1SRafael J. Wysocki 		if (second_pass) {
166303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
167303bfdb1SRafael J. Wysocki 			if (pn->put_online)
168303bfdb1SRafael J. Wysocki 				continue;
169303bfdb1SRafael J. Wysocki 		} else {
170303bfdb1SRafael J. Wysocki 			pn->put_online = false;
171303bfdb1SRafael J. Wysocki 		}
172683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
173683058e3SRafael J. Wysocki 		if (acpi_force_hot_remove)
174683058e3SRafael J. Wysocki 			continue;
175683058e3SRafael J. Wysocki 
176303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
177303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
178303bfdb1SRafael J. Wysocki 		} else {
179303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
180303bfdb1SRafael J. Wysocki 			if (second_pass) {
181683058e3SRafael J. Wysocki 				status = AE_ERROR;
182683058e3SRafael J. Wysocki 				break;
183683058e3SRafael J. Wysocki 			}
184303bfdb1SRafael J. Wysocki 		}
185683058e3SRafael J. Wysocki 	}
186683058e3SRafael J. Wysocki 
187683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
188683058e3SRafael J. Wysocki 
189683058e3SRafael J. Wysocki 	return status;
190683058e3SRafael J. Wysocki }
191683058e3SRafael J. Wysocki 
1927f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
1937f28ddecSRafael J. Wysocki 				   void **ret_p)
194683058e3SRafael J. Wysocki {
195683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
196683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
197683058e3SRafael J. Wysocki 
198683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
199683058e3SRafael J. Wysocki 		return AE_OK;
200683058e3SRafael J. Wysocki 
201683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
202683058e3SRafael J. Wysocki 
203683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
204683058e3SRafael J. Wysocki 		if (pn->put_online) {
205683058e3SRafael J. Wysocki 			device_online(pn->dev);
206683058e3SRafael J. Wysocki 			pn->put_online = false;
207683058e3SRafael J. Wysocki 		}
208683058e3SRafael J. Wysocki 
209683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
210683058e3SRafael J. Wysocki 
211683058e3SRafael J. Wysocki 	return AE_OK;
212683058e3SRafael J. Wysocki }
213683058e3SRafael J. Wysocki 
214d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device)
2151da177e4SLinus Torvalds {
2165993c467SYinghai Lu 	acpi_handle handle = device->handle;
217d22ddcbcSRafael J. Wysocki 	struct device *errdev = NULL;
218a33ec399SRafael J. Wysocki 	acpi_status status;
219f058cdf4SRafael J. Wysocki 
220303bfdb1SRafael J. Wysocki 	/*
221303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
222303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
223303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
224303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
225303bfdb1SRafael J. Wysocki 	 * block holds them.
226303bfdb1SRafael J. Wysocki 	 *
227303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
228303bfdb1SRafael J. Wysocki 	 */
2297f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2307f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
2317f28ddecSRafael J. Wysocki 				     (void **)&errdev);
2327f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
2337f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
234683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2357f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
2367f28ddecSRafael J. Wysocki 		return -EPERM;
2377f28ddecSRafael J. Wysocki 	}
2387f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
239303bfdb1SRafael J. Wysocki 	if (errdev) {
240303bfdb1SRafael J. Wysocki 		errdev = NULL;
241303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2427f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
2437f28ddecSRafael J. Wysocki 				    (void **)&errdev);
244303bfdb1SRafael J. Wysocki 		if (!errdev || acpi_force_hot_remove)
2457f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
246303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
247303bfdb1SRafael J. Wysocki 
248303bfdb1SRafael J. Wysocki 		if (errdev && !acpi_force_hot_remove) {
249303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
2507f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
251303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
2527f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
2537f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
254683058e3SRafael J. Wysocki 			return -EBUSY;
255683058e3SRafael J. Wysocki 		}
256303bfdb1SRafael J. Wysocki 	}
257d22ddcbcSRafael J. Wysocki 	return 0;
258d22ddcbcSRafael J. Wysocki }
259d22ddcbcSRafael J. Wysocki 
260d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
261d22ddcbcSRafael J. Wysocki {
262d22ddcbcSRafael J. Wysocki 	acpi_handle handle = device->handle;
263d22ddcbcSRafael J. Wysocki 	unsigned long long sta;
264d22ddcbcSRafael J. Wysocki 	acpi_status status;
265d22ddcbcSRafael J. Wysocki 
266dee15926STang Chen 	if (device->handler && device->handler->hotplug.demand_offline
267dee15926STang Chen 	    && !acpi_force_hot_remove) {
268caa73ea1SRafael J. Wysocki 		if (!acpi_scan_is_offline(device, true))
269d22ddcbcSRafael J. Wysocki 			return -EBUSY;
270d22ddcbcSRafael J. Wysocki 	} else {
271d22ddcbcSRafael J. Wysocki 		int error = acpi_scan_try_to_offline(device);
272d22ddcbcSRafael J. Wysocki 		if (error)
273d22ddcbcSRafael J. Wysocki 			return error;
274d22ddcbcSRafael J. Wysocki 	}
275683058e3SRafael J. Wysocki 
27626d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2770794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
27826d46867SZhang Rui 
279bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
280683058e3SRafael J. Wysocki 
2817d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2821da177e4SLinus Torvalds 	/*
2831da177e4SLinus Torvalds 	 * TBD: _EJD support.
2841da177e4SLinus Torvalds 	 */
2857d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2867d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
287a33ec399SRafael J. Wysocki 		return -ENODEV;
2887d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
289a33ec399SRafael J. Wysocki 		return -EIO;
290f058cdf4SRafael J. Wysocki 
291882fd12eSToshi Kani 	/*
292882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
293882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
294882fd12eSToshi Kani 	 */
295882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
296882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
297882fd12eSToshi Kani 		acpi_handle_warn(handle,
298882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
299882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
300882fd12eSToshi Kani 		acpi_handle_warn(handle,
301882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
302882fd12eSToshi Kani 	}
303882fd12eSToshi Kani 
304a33ec399SRafael J. Wysocki 	return 0;
305a33ec399SRafael J. Wysocki }
306f058cdf4SRafael J. Wysocki 
307443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev)
308443fc820SRafael J. Wysocki {
309443fc820SRafael J. Wysocki 	if (!acpi_device_enumerated(adev)) {
310443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Still not present\n");
311443fc820SRafael J. Wysocki 		return -EALREADY;
312443fc820SRafael J. Wysocki 	}
313443fc820SRafael J. Wysocki 	acpi_bus_trim(adev);
314443fc820SRafael J. Wysocki 	return 0;
315443fc820SRafael J. Wysocki }
316443fc820SRafael J. Wysocki 
317c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev)
318a33ec399SRafael J. Wysocki {
319f943db40SRafael J. Wysocki 	int error;
320a33ec399SRafael J. Wysocki 
321443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
322443fc820SRafael J. Wysocki 	if (adev->status.present || adev->status.functional) {
323443fc820SRafael J. Wysocki 		/*
324443fc820SRafael J. Wysocki 		 * This function is only called for device objects for which
325443fc820SRafael J. Wysocki 		 * matching scan handlers exist.  The only situation in which
326443fc820SRafael J. Wysocki 		 * the scan handler is not attached to this device object yet
327443fc820SRafael J. Wysocki 		 * is when the device has just appeared (either it wasn't
328443fc820SRafael J. Wysocki 		 * present at all before or it was removed and then added
329443fc820SRafael J. Wysocki 		 * again).
330443fc820SRafael J. Wysocki 		 */
331443fc820SRafael J. Wysocki 		if (adev->handler) {
332443fc820SRafael J. Wysocki 			dev_warn(&adev->dev, "Already enumerated\n");
333443fc820SRafael J. Wysocki 			return -EALREADY;
334443fc820SRafael J. Wysocki 		}
335c27b2c33SRafael J. Wysocki 		error = acpi_bus_scan(adev->handle);
336a33ec399SRafael J. Wysocki 		if (error) {
337c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Namespace scan failure\n");
338c27b2c33SRafael J. Wysocki 			return error;
339a33ec399SRafael J. Wysocki 		}
34046394fd0SRafael J. Wysocki 		if (!adev->handler) {
341c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Enumeration failure\n");
342443fc820SRafael J. Wysocki 			error = -ENODEV;
343443fc820SRafael J. Wysocki 		}
344443fc820SRafael J. Wysocki 	} else {
345443fc820SRafael J. Wysocki 		error = acpi_scan_device_not_present(adev);
346443fc820SRafael J. Wysocki 	}
347443fc820SRafael J. Wysocki 	return error;
348443fc820SRafael J. Wysocki }
349443fc820SRafael J. Wysocki 
350443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev)
351443fc820SRafael J. Wysocki {
352443fc820SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
353443fc820SRafael J. Wysocki 	struct acpi_device *child;
354443fc820SRafael J. Wysocki 	int error;
355443fc820SRafael J. Wysocki 
356443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
357443fc820SRafael J. Wysocki 	if (!(adev->status.present || adev->status.functional)) {
358443fc820SRafael J. Wysocki 		acpi_scan_device_not_present(adev);
359443fc820SRafael J. Wysocki 		return 0;
360443fc820SRafael J. Wysocki 	}
361443fc820SRafael J. Wysocki 	if (handler && handler->hotplug.scan_dependent)
362443fc820SRafael J. Wysocki 		return handler->hotplug.scan_dependent(adev);
363443fc820SRafael J. Wysocki 
364443fc820SRafael J. Wysocki 	error = acpi_bus_scan(adev->handle);
365443fc820SRafael J. Wysocki 	if (error) {
366443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Namespace scan failure\n");
367443fc820SRafael J. Wysocki 		return error;
368443fc820SRafael J. Wysocki 	}
369443fc820SRafael J. Wysocki 	list_for_each_entry(child, &adev->children, node) {
370443fc820SRafael J. Wysocki 		error = acpi_scan_bus_check(child);
371443fc820SRafael J. Wysocki 		if (error)
372443fc820SRafael J. Wysocki 			return error;
373a33ec399SRafael J. Wysocki 	}
374c27b2c33SRafael J. Wysocki 	return 0;
375c27b2c33SRafael J. Wysocki }
376c27b2c33SRafael J. Wysocki 
3773c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
3783c2cc7ffSRafael J. Wysocki {
3793c2cc7ffSRafael J. Wysocki 	switch (type) {
3803c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
3813c2cc7ffSRafael J. Wysocki 		return acpi_scan_bus_check(adev);
3823c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
3833c2cc7ffSRafael J. Wysocki 		return acpi_scan_device_check(adev);
3843c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
3853c2cc7ffSRafael J. Wysocki 	case ACPI_OST_EC_OSPM_EJECT:
386dd2151beSRafael J. Wysocki 		if (adev->handler && !adev->handler->hotplug.enabled) {
387dd2151beSRafael J. Wysocki 			dev_info(&adev->dev, "Eject disabled\n");
388dd2151beSRafael J. Wysocki 			return -EPERM;
389dd2151beSRafael J. Wysocki 		}
390700b8422SRafael J. Wysocki 		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
391dd2151beSRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3923c2cc7ffSRafael J. Wysocki 		return acpi_scan_hot_remove(adev);
3933c2cc7ffSRafael J. Wysocki 	}
3943c2cc7ffSRafael J. Wysocki 	return -EINVAL;
3953c2cc7ffSRafael J. Wysocki }
3963c2cc7ffSRafael J. Wysocki 
3971e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src)
398c27b2c33SRafael J. Wysocki {
399c27b2c33SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
4003c2cc7ffSRafael J. Wysocki 	int error = -ENODEV;
401c27b2c33SRafael J. Wysocki 
402c27b2c33SRafael J. Wysocki 	lock_device_hotplug();
403c27b2c33SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
404c27b2c33SRafael J. Wysocki 
405c27b2c33SRafael J. Wysocki 	/*
406c27b2c33SRafael J. Wysocki 	 * The device object's ACPI handle cannot become invalid as long as we
4073c2cc7ffSRafael J. Wysocki 	 * are holding acpi_scan_lock, but it might have become invalid before
408c27b2c33SRafael J. Wysocki 	 * that lock was acquired.
409c27b2c33SRafael J. Wysocki 	 */
410c27b2c33SRafael J. Wysocki 	if (adev->handle == INVALID_ACPI_HANDLE)
4113c2cc7ffSRafael J. Wysocki 		goto err_out;
412c27b2c33SRafael J. Wysocki 
4131e2380cdSRafael J. Wysocki 	if (adev->flags.is_dock_station) {
4141e2380cdSRafael J. Wysocki 		error = dock_notify(adev, src);
4151e2380cdSRafael J. Wysocki 	} else if (adev->flags.hotplug_notify) {
4163c2cc7ffSRafael J. Wysocki 		error = acpi_generic_hotplug_event(adev, src);
417dd2151beSRafael J. Wysocki 		if (error == -EPERM) {
418dd2151beSRafael J. Wysocki 			ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
419dd2151beSRafael J. Wysocki 			goto err_out;
420dd2151beSRafael J. Wysocki 		}
4213c2cc7ffSRafael J. Wysocki 	} else {
422be27b3dcSRafael J. Wysocki 		int (*notify)(struct acpi_device *, u32);
4233c2cc7ffSRafael J. Wysocki 
4243c2cc7ffSRafael J. Wysocki 		acpi_lock_hp_context();
425be27b3dcSRafael J. Wysocki 		notify = adev->hp ? adev->hp->notify : NULL;
4263c2cc7ffSRafael J. Wysocki 		acpi_unlock_hp_context();
4273c2cc7ffSRafael J. Wysocki 		/*
4283c2cc7ffSRafael J. Wysocki 		 * There may be additional notify handlers for device objects
4293c2cc7ffSRafael J. Wysocki 		 * without the .event() callback, so ignore them here.
4303c2cc7ffSRafael J. Wysocki 		 */
431be27b3dcSRafael J. Wysocki 		if (notify)
432be27b3dcSRafael J. Wysocki 			error = notify(adev, src);
4333c2cc7ffSRafael J. Wysocki 		else
4343c2cc7ffSRafael J. Wysocki 			goto out;
435c27b2c33SRafael J. Wysocki 	}
436c27b2c33SRafael J. Wysocki 	if (!error)
437a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_SUCCESS;
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 
766e88c9c60SRafael J. Wysocki static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
767b581a7f9SRafael J. Wysocki 					struct acpi_device_wakeup *wakeup)
7681da177e4SLinus Torvalds {
769b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
770b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
7711da177e4SLinus Torvalds 	union acpi_object *element = NULL;
772b581a7f9SRafael J. Wysocki 	acpi_status status;
773e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
7741da177e4SLinus Torvalds 
775b581a7f9SRafael J. Wysocki 	if (!wakeup)
776e88c9c60SRafael J. Wysocki 		return -EINVAL;
7771da177e4SLinus Torvalds 
778993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
7791da177e4SLinus Torvalds 
780b581a7f9SRafael J. Wysocki 	/* _PRW */
781b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
782b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
783b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
784e88c9c60SRafael J. Wysocki 		return err;
785b581a7f9SRafael J. Wysocki 	}
786b581a7f9SRafael J. Wysocki 
787b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
788b581a7f9SRafael J. Wysocki 
789e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
790b581a7f9SRafael J. Wysocki 		goto out;
791b581a7f9SRafael J. Wysocki 
7921da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
793e88c9c60SRafael J. Wysocki 	if (!element)
794b581a7f9SRafael J. Wysocki 		goto out;
795e88c9c60SRafael J. Wysocki 
7961da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
7971da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
7981da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
7991da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
800e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
801b581a7f9SRafael J. Wysocki 			goto out;
802e88c9c60SRafael J. Wysocki 
803b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
8041da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
805b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
8061da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
8071da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
808b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
809b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
810b581a7f9SRafael J. Wysocki 	} else {
811b581a7f9SRafael J. Wysocki 		goto out;
812b581a7f9SRafael J. Wysocki 	}
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
815e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
816b581a7f9SRafael J. Wysocki 		goto out;
817e88c9c60SRafael J. Wysocki 
818b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
8191da177e4SLinus Torvalds 
820e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
821e88c9c60SRafael J. Wysocki 	if (err)
822b581a7f9SRafael J. Wysocki 		goto out;
8231da177e4SLinus Torvalds 
8240596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
8250596a52bSRafael J. Wysocki 		int sleep_state;
8269874647bSRafael J. Wysocki 
827b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
828b5d667ebSRafael J. Wysocki 						  &sleep_state);
829b5d667ebSRafael J. Wysocki 		if (err) {
830b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
831b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
832b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
833b5d667ebSRafael J. Wysocki 			goto out;
834b5d667ebSRafael J. Wysocki 		}
8350596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
8360596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
8370596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
8380596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
8390596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
8400596a52bSRafael J. Wysocki 		}
8410596a52bSRafael J. Wysocki 	}
842b581a7f9SRafael J. Wysocki 
843b581a7f9SRafael J. Wysocki  out:
844b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
845e88c9c60SRafael J. Wysocki 	return err;
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
848bd9b2f9aSRafael J. Wysocki static void acpi_wakeup_gpe_init(struct acpi_device *device)
8491da177e4SLinus Torvalds {
8500519ade7SMathias Krause 	static const struct acpi_device_id button_device_ids[] = {
85129b71a1cSThomas Renninger 		{"PNP0C0C", 0},
852b7e38304SZhang Rui 		{"PNP0C0D", 0},
85329b71a1cSThomas Renninger 		{"PNP0C0E", 0},
85429b71a1cSThomas Renninger 		{"", 0},
85529b71a1cSThomas Renninger 	};
856bd9b2f9aSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &device->wakeup;
857f517709dSRafael J. Wysocki 	acpi_status status;
858f517709dSRafael J. Wysocki 	acpi_event_status event_status;
859f517709dSRafael J. Wysocki 
860bd9b2f9aSRafael J. Wysocki 	wakeup->flags.notifier_present = 0;
861f517709dSRafael J. Wysocki 
862f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
863f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
864bd9b2f9aSRafael J. Wysocki 		wakeup->flags.run_wake = 1;
865b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
866b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
867bd9b2f9aSRafael J. Wysocki 			if (wakeup->sleep_state == ACPI_STATE_S5)
868bd9b2f9aSRafael J. Wysocki 				wakeup->sleep_state = ACPI_STATE_S4;
869b7e38304SZhang Rui 		}
870bd9b2f9aSRafael J. Wysocki 		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
871f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
872f517709dSRafael J. Wysocki 		return;
873f517709dSRafael J. Wysocki 	}
874f517709dSRafael J. Wysocki 
875bd9b2f9aSRafael J. Wysocki 	acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
876bd9b2f9aSRafael J. Wysocki 				wakeup->gpe_number);
877bd9b2f9aSRafael J. Wysocki 	status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
8780f849d2cSLin Ming 				     &event_status);
879bd9b2f9aSRafael J. Wysocki 	if (ACPI_FAILURE(status))
880bd9b2f9aSRafael J. Wysocki 		return;
881bd9b2f9aSRafael J. Wysocki 
8822f857234SLv Zheng 	wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
883f517709dSRafael J. Wysocki }
884f517709dSRafael J. Wysocki 
885d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
886f517709dSRafael J. Wysocki {
887e88c9c60SRafael J. Wysocki 	int err;
88829b71a1cSThomas Renninger 
889d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
890952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
891d57d09a4SRafael J. Wysocki 		return;
892d57d09a4SRafael J. Wysocki 
893e88c9c60SRafael J. Wysocki 	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
894b581a7f9SRafael J. Wysocki 							   &device->wakeup);
895e88c9c60SRafael J. Wysocki 	if (err) {
896e88c9c60SRafael J. Wysocki 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
897d57d09a4SRafael J. Wysocki 		return;
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
9019b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
902bd9b2f9aSRafael J. Wysocki 	acpi_wakeup_gpe_init(device);
903729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
904729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
905729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
906729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
907729b2bdbSZhao Yakui 	 * present will the _PSW object used.
908729b2bdbSZhao Yakui 	 */
909e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
910e88c9c60SRafael J. Wysocki 	if (err)
91177e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
91277e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
915f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
9161da177e4SLinus Torvalds {
917f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
918ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
919ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
920f33ce568SRafael J. Wysocki 	acpi_status status;
9219e89dde2SZhang Rui 
922f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
923f33ce568SRafael J. Wysocki 
924ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
925ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
926ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
927ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
928f33ce568SRafael J. Wysocki 
929ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
930ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
931ef85bdbeSRafael J. Wysocki 		    && package->package.count) {
932e88c9c60SRafael J. Wysocki 			int err = acpi_extract_power_resources(package, 0,
933ef85bdbeSRafael J. Wysocki 							       &ps->resources);
934e88c9c60SRafael J. Wysocki 			if (!err)
935f33ce568SRafael J. Wysocki 				device->power.flags.power_resources = 1;
936f33ce568SRafael J. Wysocki 		}
937ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
938f33ce568SRafael J. Wysocki 	}
939f33ce568SRafael J. Wysocki 
940f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
941ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
942952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
943f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
944f33ce568SRafael J. Wysocki 
94520dacb71SRafael J. Wysocki 	/* State is valid if there are means to put the device into it. */
94620dacb71SRafael J. Wysocki 	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
947f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
948f33ce568SRafael J. Wysocki 
949f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
950f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
951f33ce568SRafael J. Wysocki }
952f33ce568SRafael J. Wysocki 
953d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
9541da177e4SLinus Torvalds {
9558bc5053bSRafael J. Wysocki 	u32 i;
9561da177e4SLinus Torvalds 
957d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
958952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
959952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
960d43e167dSRafael J. Wysocki 		return;
961d43e167dSRafael J. Wysocki 
962d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	/*
9659e89dde2SZhang Rui 	 * Power Management Flags
9661da177e4SLinus Torvalds 	 */
967952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
9689e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
969f25c0ae2SRafael J. Wysocki 
970952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
9719e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
9721da177e4SLinus Torvalds 
973f25c0ae2SRafael J. Wysocki 	if (acpi_has_method(device->handle, "_DSW"))
974f25c0ae2SRafael J. Wysocki 		device->power.flags.dsw_present = 1;
975f25c0ae2SRafael J. Wysocki 
9761da177e4SLinus Torvalds 	/*
9779e89dde2SZhang Rui 	 * Enumerate supported power management states
9781da177e4SLinus Torvalds 	 */
979f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
980f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
9811da177e4SLinus Torvalds 
9820b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
98320dacb71SRafael J. Wysocki 	if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
98420dacb71SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
9859e89dde2SZhang Rui 
98620dacb71SRafael J. Wysocki 	/* Set defaults for D0 and D3hot states (always valid) */
9879e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
9889e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
98920dacb71SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
9901399dfcdSAaron Lu 
9911b1f3e16SRafael J. Wysocki 	if (acpi_bus_init_power(device))
992b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
993b3785492SRafael J. Wysocki }
994c8f7a62cSLen Brown 
995d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
9961da177e4SLinus Torvalds {
9971da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
998952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
9991da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
1002952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
10031da177e4SLinus Torvalds 		device->flags.removable = 1;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1006952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
1007952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
10081da177e4SLinus Torvalds 		device->flags.ejectable = 1;
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds 
1011c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
10121da177e4SLinus Torvalds {
10131da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
10141da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
10151da177e4SLinus Torvalds 	int i = 0;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	/*
10181da177e4SLinus Torvalds 	 * Bus ID
10191da177e4SLinus Torvalds 	 * ------
10201da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
10211da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
10221da177e4SLinus Torvalds 	 */
1023859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
10241da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1025859ac9a4SBjorn Helgaas 		return;
1026859ac9a4SBjorn Helgaas 	}
1027859ac9a4SBjorn Helgaas 
1028859ac9a4SBjorn Helgaas 	switch (device->device_type) {
10291da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
10301da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
10311da177e4SLinus Torvalds 		break;
10321da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
10331da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
10341da177e4SLinus Torvalds 		break;
10351da177e4SLinus Torvalds 	default:
103666b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
10371da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
10381da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
10391da177e4SLinus Torvalds 			if (bus_id[i] == '_')
10401da177e4SLinus Torvalds 				bus_id[i] = '\0';
10411da177e4SLinus Torvalds 			else
10421da177e4SLinus Torvalds 				break;
10431da177e4SLinus Torvalds 		}
10441da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
10451da177e4SLinus Torvalds 		break;
10461da177e4SLinus Torvalds 	}
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
104954735266SZhang Rui /*
1050ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1051ebf4df8dSJiang Liu  *
1052ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1053ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1054ebf4df8dSJiang Liu  */
1055ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1056ebf4df8dSJiang Liu {
1057ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1058ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1059ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1060ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1061ebf4df8dSJiang Liu }
1062ebf4df8dSJiang Liu 
1063ebf4df8dSJiang Liu /*
1064d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
106554735266SZhang Rui  *
106654735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
106754735266SZhang Rui  * then we can safely call it an ejectable drive bay
106854735266SZhang Rui  */
1069ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1070d4e1a692SToshi Kani {
107154735266SZhang Rui 	acpi_handle phandle;
107254735266SZhang Rui 
1073952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1074ebf4df8dSJiang Liu 		return false;
1075ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1076ebf4df8dSJiang Liu 		return true;
1077ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1078ebf4df8dSJiang Liu 		return false;
107954735266SZhang Rui 
1080ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
108154735266SZhang Rui }
108254735266SZhang Rui 
1083b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev)
10841e2380cdSRafael J. Wysocki {
1085b43109faSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
10861e2380cdSRafael J. Wysocki 
1087b43109faSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list)
1088b43109faSRafael J. Wysocki 		if (!strcmp("PNP0C0A", hwid->id))
1089b43109faSRafael J. Wysocki 			return true;
1090b43109faSRafael J. Wysocki 
10911e2380cdSRafael J. Wysocki 	return false;
10921e2380cdSRafael J. Wysocki }
10931e2380cdSRafael J. Wysocki 
1094b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev)
10951e2380cdSRafael J. Wysocki {
1096b43109faSRafael J. Wysocki 	acpi_handle handle = adev->handle;
1097b43109faSRafael J. Wysocki 
1098b43109faSRafael J. Wysocki 	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
10991e2380cdSRafael J. Wysocki 		return true;
11001e2380cdSRafael J. Wysocki 
11011e2380cdSRafael J. Wysocki 	return acpi_bay_match(handle);
11021e2380cdSRafael J. Wysocki }
11031e2380cdSRafael J. Wysocki 
11043620f2f2SFrank Seidel /*
1105d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
11063620f2f2SFrank Seidel  */
1107ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
11083620f2f2SFrank Seidel {
1109ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
11103620f2f2SFrank Seidel }
11113620f2f2SFrank Seidel 
1112adc8bb8eSHans de Goede static acpi_status
1113adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1114adc8bb8eSHans de Goede 			  void **return_value)
1115adc8bb8eSHans de Goede {
1116adc8bb8eSHans de Goede 	long *cap = context;
1117adc8bb8eSHans de Goede 
1118adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_BCM") &&
1119adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_BCL")) {
1120adc8bb8eSHans de Goede 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
1121adc8bb8eSHans de Goede 				  "support\n"));
1122adc8bb8eSHans de Goede 		*cap |= ACPI_VIDEO_BACKLIGHT;
1123adc8bb8eSHans de Goede 		if (!acpi_has_method(handle, "_BQC"))
1124adc8bb8eSHans de Goede 			printk(KERN_WARNING FW_BUG PREFIX "No _BQC method, "
1125adc8bb8eSHans de Goede 				"cannot determine initial brightness\n");
1126adc8bb8eSHans de Goede 		/* We have backlight support, no need to scan further */
1127adc8bb8eSHans de Goede 		return AE_CTRL_TERMINATE;
1128adc8bb8eSHans de Goede 	}
1129adc8bb8eSHans de Goede 	return 0;
1130adc8bb8eSHans de Goede }
1131adc8bb8eSHans de Goede 
1132adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be
1133adc8bb8eSHans de Goede  * handled by video.ko.
1134adc8bb8eSHans de Goede  * The device will get a Linux specific CID added in scan.c to
1135adc8bb8eSHans de Goede  * identify the device as an ACPI graphics device
1136adc8bb8eSHans de Goede  * Be aware that the graphics device may not be physically present
1137adc8bb8eSHans de Goede  * Use acpi_video_get_capabilities() to detect general ACPI video
1138adc8bb8eSHans de Goede  * capabilities of present cards
1139adc8bb8eSHans de Goede  */
1140adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle)
1141adc8bb8eSHans de Goede {
1142adc8bb8eSHans de Goede 	long video_caps = 0;
1143adc8bb8eSHans de Goede 
1144adc8bb8eSHans de Goede 	/* Is this device able to support video switching ? */
1145adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1146adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1147adc8bb8eSHans de Goede 
1148adc8bb8eSHans de Goede 	/* Is this device able to retrieve a video ROM ? */
1149adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_ROM"))
1150adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1151adc8bb8eSHans de Goede 
1152adc8bb8eSHans de Goede 	/* Is this device able to configure which video head to be POSTed ? */
1153adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_VPO") &&
1154adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_GPD") &&
1155adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_SPD"))
1156adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1157adc8bb8eSHans de Goede 
1158adc8bb8eSHans de Goede 	/* Only check for backlight functionality if one of the above hit. */
1159adc8bb8eSHans de Goede 	if (video_caps)
1160adc8bb8eSHans de Goede 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1161adc8bb8eSHans de Goede 				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1162adc8bb8eSHans de Goede 				    &video_caps, NULL);
1163adc8bb8eSHans de Goede 
1164adc8bb8eSHans de Goede 	return video_caps;
1165adc8bb8eSHans de Goede }
1166adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device);
1167adc8bb8eSHans de Goede 
1168620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
116915b8dd53SBob Moore {
11707f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
117115b8dd53SBob Moore 
11722b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
11732b2ae7c7SThomas Renninger 		return dummy_hid;
11742b2ae7c7SThomas Renninger 
11757f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
11767f47fa6cSBjorn Helgaas 	return hid->id;
11777f47fa6cSBjorn Helgaas }
11787f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
117915b8dd53SBob Moore 
1180d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
11817f47fa6cSBjorn Helgaas {
11827f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
118315b8dd53SBob Moore 
11847f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
11857f47fa6cSBjorn Helgaas 	if (!id)
11867f47fa6cSBjorn Helgaas 		return;
118715b8dd53SBob Moore 
11886a0d12efSRasmus Villemoes 	id->id = kstrdup_const(dev_id, GFP_KERNEL);
11897f47fa6cSBjorn Helgaas 	if (!id->id) {
11907f47fa6cSBjorn Helgaas 		kfree(id);
11917f47fa6cSBjorn Helgaas 		return;
119215b8dd53SBob Moore 	}
119315b8dd53SBob Moore 
1194d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1195d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
119615b8dd53SBob Moore }
119715b8dd53SBob Moore 
1198222e82acSDarrick J. Wong /*
1199222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1200222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1201222e82acSDarrick J. Wong  * prefix.  Work around this.
1202222e82acSDarrick J. Wong  */
1203ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1204222e82acSDarrick J. Wong {
1205ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1206ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1207222e82acSDarrick J. Wong 
1208222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1209ebf4df8dSJiang Liu 		return false;
1210222e82acSDarrick J. Wong 
1211222e82acSDarrick J. Wong 	/* Look for SMBS object */
1212ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1213ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1214ebf4df8dSJiang Liu 		return false;
1215222e82acSDarrick J. Wong 
1216222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1217952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1218952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1219952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1220ebf4df8dSJiang Liu 		return true;
1221ebf4df8dSJiang Liu 
1222ebf4df8dSJiang Liu 	return false;
1223222e82acSDarrick J. Wong }
1224222e82acSDarrick J. Wong 
1225ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle)
1226ae3caa80SJiang Liu {
1227ae3caa80SJiang Liu 	acpi_handle tmp;
1228ae3caa80SJiang Liu 
1229ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1230ae3caa80SJiang Liu 	    tmp == handle)
1231ae3caa80SJiang Liu 		return true;
1232ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1233ae3caa80SJiang Liu 	    tmp == handle)
1234ae3caa80SJiang Liu 		return true;
1235ae3caa80SJiang Liu 
1236ae3caa80SJiang Liu 	return false;
1237ae3caa80SJiang Liu }
1238ae3caa80SJiang Liu 
1239c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1240c0af4175SToshi Kani 				int device_type)
12411da177e4SLinus Torvalds {
12421da177e4SLinus Torvalds 	acpi_status status;
124357f3674fSBjorn Helgaas 	struct acpi_device_info *info;
124478e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
12457f47fa6cSBjorn Helgaas 	int i;
12461da177e4SLinus Torvalds 
1247c0af4175SToshi Kani 	switch (device_type) {
12481da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1249c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1250c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1251859ac9a4SBjorn Helgaas 			break;
1252859ac9a4SBjorn Helgaas 		}
1253859ac9a4SBjorn Helgaas 
1254c0af4175SToshi Kani 		status = acpi_get_object_info(handle, &info);
12551da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
1256c0af4175SToshi Kani 			pr_err(PREFIX "%s: Error reading device info\n",
1257c0af4175SToshi Kani 					__func__);
12581da177e4SLinus Torvalds 			return;
12591da177e4SLinus Torvalds 		}
12601da177e4SLinus Torvalds 
1261549e6845SRafael J. Wysocki 		if (info->valid & ACPI_VALID_HID) {
1262c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
1263549e6845SRafael J. Wysocki 			pnp->type.platform_id = 1;
1264549e6845SRafael J. Wysocki 		}
126557f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
126615b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
126757f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1268c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
126957f3674fSBjorn Helgaas 		}
12701da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1271c0af4175SToshi Kani 			pnp->bus_address = info->address;
1272c0af4175SToshi Kani 			pnp->type.bus_address = 1;
12731da177e4SLinus Torvalds 		}
1274ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1275c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1276ccf78040SLv Zheng 							GFP_KERNEL);
127726095a01SSuthikulpanit, Suravee 		if (info->valid & ACPI_VALID_CLS)
127826095a01SSuthikulpanit, Suravee 			acpi_add_id(pnp, info->class_code.string);
1279ae843332SZhang Rui 
1280a83893aeSBjorn Helgaas 		kfree(info);
1281a83893aeSBjorn Helgaas 
128257f3674fSBjorn Helgaas 		/*
128357f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
128457f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
12853620f2f2SFrank Seidel 		 */
1286c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1287c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1288ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1289c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1290ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1291c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1292ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1293c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1294ae3caa80SJiang Liu 		else if (list_empty(&pnp->ids) &&
1295ae3caa80SJiang Liu 			 acpi_object_is_system_bus(handle)) {
1296ae3caa80SJiang Liu 			/* \_SB, \_TZ, LNXSYBUS */
1297ae3caa80SJiang Liu 			acpi_add_id(pnp, ACPI_BUS_HID);
1298c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1299c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1300b7b30de5SBjorn Helgaas 		}
130154735266SZhang Rui 
13021da177e4SLinus Torvalds 		break;
13031da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1304c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
13051da177e4SLinus Torvalds 		break;
13061da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1307c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
13081da177e4SLinus Torvalds 		break;
13091da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1310c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
13111da177e4SLinus Torvalds 		break;
13121da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1313c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
13141da177e4SLinus Torvalds 		break;
13151da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1316c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
13171da177e4SLinus Torvalds 		break;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
1321c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1322c0af4175SToshi Kani {
1323c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1324c0af4175SToshi Kani 
1325c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
13266a0d12efSRasmus Villemoes 		kfree_const(id->id);
1327c0af4175SToshi Kani 		kfree(id);
1328c0af4175SToshi Kani 	}
1329c0af4175SToshi Kani 	kfree(pnp->unique_id);
1330c0af4175SToshi Kani }
1331c0af4175SToshi Kani 
1332b84f196dSSuthikulpanit, Suravee /**
1333b84f196dSSuthikulpanit, Suravee  * acpi_dma_supported - Check DMA support for the specified device.
1334b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1335b84f196dSSuthikulpanit, Suravee  *
1336b84f196dSSuthikulpanit, Suravee  * Return false if DMA is not supported. Otherwise, return true
1337b84f196dSSuthikulpanit, Suravee  */
1338b84f196dSSuthikulpanit, Suravee bool acpi_dma_supported(struct acpi_device *adev)
1339b84f196dSSuthikulpanit, Suravee {
1340b84f196dSSuthikulpanit, Suravee 	if (!adev)
1341b84f196dSSuthikulpanit, Suravee 		return false;
1342b84f196dSSuthikulpanit, Suravee 
1343b84f196dSSuthikulpanit, Suravee 	if (adev->flags.cca_seen)
1344b84f196dSSuthikulpanit, Suravee 		return true;
1345b84f196dSSuthikulpanit, Suravee 
1346b84f196dSSuthikulpanit, Suravee 	/*
1347b84f196dSSuthikulpanit, Suravee 	* Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1348b84f196dSSuthikulpanit, Suravee 	* DMA on "Intel platforms".  Presumably that includes all x86 and
1349b84f196dSSuthikulpanit, Suravee 	* ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1350b84f196dSSuthikulpanit, Suravee 	*/
1351b84f196dSSuthikulpanit, Suravee 	if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1352b84f196dSSuthikulpanit, Suravee 		return true;
1353b84f196dSSuthikulpanit, Suravee 
1354b84f196dSSuthikulpanit, Suravee 	return false;
1355b84f196dSSuthikulpanit, Suravee }
1356b84f196dSSuthikulpanit, Suravee 
1357b84f196dSSuthikulpanit, Suravee /**
1358b84f196dSSuthikulpanit, Suravee  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1359b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1360b84f196dSSuthikulpanit, Suravee  *
1361b84f196dSSuthikulpanit, Suravee  * Return enum dev_dma_attr.
1362b84f196dSSuthikulpanit, Suravee  */
1363b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1364b84f196dSSuthikulpanit, Suravee {
1365b84f196dSSuthikulpanit, Suravee 	if (!acpi_dma_supported(adev))
1366b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NOT_SUPPORTED;
1367b84f196dSSuthikulpanit, Suravee 
1368b84f196dSSuthikulpanit, Suravee 	if (adev->flags.coherent_dma)
1369b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_COHERENT;
1370b84f196dSSuthikulpanit, Suravee 	else
1371b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NON_COHERENT;
1372b84f196dSSuthikulpanit, Suravee }
1373b84f196dSSuthikulpanit, Suravee 
1374d760a1baSLorenzo Pieralisi /**
1375d760a1baSLorenzo Pieralisi  * acpi_dma_configure - Set-up DMA configuration for the device.
1376d760a1baSLorenzo Pieralisi  * @dev: The pointer to the device
1377d760a1baSLorenzo Pieralisi  * @attr: device dma attributes
1378d760a1baSLorenzo Pieralisi  */
1379d760a1baSLorenzo Pieralisi void acpi_dma_configure(struct device *dev, enum dev_dma_attr attr)
1380d760a1baSLorenzo Pieralisi {
1381*643b8e4dSLorenzo Pieralisi 	const struct iommu_ops *iommu;
1382*643b8e4dSLorenzo Pieralisi 
1383d760a1baSLorenzo Pieralisi 	/*
1384d760a1baSLorenzo Pieralisi 	 * Set default coherent_dma_mask to 32 bit.  Drivers are expected to
1385d760a1baSLorenzo Pieralisi 	 * setup the correct supported mask.
1386d760a1baSLorenzo Pieralisi 	 */
1387d760a1baSLorenzo Pieralisi 	if (!dev->coherent_dma_mask)
1388d760a1baSLorenzo Pieralisi 		dev->coherent_dma_mask = DMA_BIT_MASK(32);
1389d760a1baSLorenzo Pieralisi 
1390d760a1baSLorenzo Pieralisi 	/*
1391d760a1baSLorenzo Pieralisi 	 * Set it to coherent_dma_mask by default if the architecture
1392d760a1baSLorenzo Pieralisi 	 * code has not set it.
1393d760a1baSLorenzo Pieralisi 	 */
1394d760a1baSLorenzo Pieralisi 	if (!dev->dma_mask)
1395d760a1baSLorenzo Pieralisi 		dev->dma_mask = &dev->coherent_dma_mask;
1396d760a1baSLorenzo Pieralisi 
1397*643b8e4dSLorenzo Pieralisi 	iommu = iort_iommu_configure(dev);
1398*643b8e4dSLorenzo Pieralisi 
1399d760a1baSLorenzo Pieralisi 	/*
1400d760a1baSLorenzo Pieralisi 	 * Assume dma valid range starts at 0 and covers the whole
1401d760a1baSLorenzo Pieralisi 	 * coherent_dma_mask.
1402d760a1baSLorenzo Pieralisi 	 */
1403*643b8e4dSLorenzo Pieralisi 	arch_setup_dma_ops(dev, 0, dev->coherent_dma_mask + 1, iommu,
1404d760a1baSLorenzo Pieralisi 			   attr == DEV_DMA_COHERENT);
1405d760a1baSLorenzo Pieralisi }
1406d760a1baSLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_configure);
1407d760a1baSLorenzo Pieralisi 
1408d760a1baSLorenzo Pieralisi /**
1409d760a1baSLorenzo Pieralisi  * acpi_dma_deconfigure - Tear-down DMA configuration for the device.
1410d760a1baSLorenzo Pieralisi  * @dev: The pointer to the device
1411d760a1baSLorenzo Pieralisi  */
1412d760a1baSLorenzo Pieralisi void acpi_dma_deconfigure(struct device *dev)
1413d760a1baSLorenzo Pieralisi {
1414d760a1baSLorenzo Pieralisi 	arch_teardown_dma_ops(dev);
1415d760a1baSLorenzo Pieralisi }
1416d760a1baSLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_deconfigure);
1417d760a1baSLorenzo Pieralisi 
1418d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev)
1419d0562674SSuthikulpanit, Suravee {
1420d0562674SSuthikulpanit, Suravee 	unsigned long long cca = 0;
1421d0562674SSuthikulpanit, Suravee 	acpi_status status;
1422d0562674SSuthikulpanit, Suravee 	struct acpi_device *parent = adev->parent;
1423d0562674SSuthikulpanit, Suravee 
1424d0562674SSuthikulpanit, Suravee 	if (parent && parent->flags.cca_seen) {
1425d0562674SSuthikulpanit, Suravee 		/*
1426d0562674SSuthikulpanit, Suravee 		 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1427d0562674SSuthikulpanit, Suravee 		 * already saw one.
1428d0562674SSuthikulpanit, Suravee 		 */
1429d0562674SSuthikulpanit, Suravee 		adev->flags.cca_seen = 1;
1430d0562674SSuthikulpanit, Suravee 		cca = parent->flags.coherent_dma;
1431d0562674SSuthikulpanit, Suravee 	} else {
1432d0562674SSuthikulpanit, Suravee 		status = acpi_evaluate_integer(adev->handle, "_CCA",
1433d0562674SSuthikulpanit, Suravee 					       NULL, &cca);
1434d0562674SSuthikulpanit, Suravee 		if (ACPI_SUCCESS(status))
1435d0562674SSuthikulpanit, Suravee 			adev->flags.cca_seen = 1;
1436d0562674SSuthikulpanit, Suravee 		else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1437d0562674SSuthikulpanit, Suravee 			/*
1438d0562674SSuthikulpanit, Suravee 			 * If architecture does not specify that _CCA is
1439d0562674SSuthikulpanit, Suravee 			 * required for DMA-able devices (e.g. x86),
1440d0562674SSuthikulpanit, Suravee 			 * we default to _CCA=1.
1441d0562674SSuthikulpanit, Suravee 			 */
1442d0562674SSuthikulpanit, Suravee 			cca = 1;
1443d0562674SSuthikulpanit, Suravee 		else
1444d0562674SSuthikulpanit, Suravee 			acpi_handle_debug(adev->handle,
1445d0562674SSuthikulpanit, Suravee 					  "ACPI device is missing _CCA.\n");
1446d0562674SSuthikulpanit, Suravee 	}
1447d0562674SSuthikulpanit, Suravee 
1448d0562674SSuthikulpanit, Suravee 	adev->flags.coherent_dma = cca;
1449d0562674SSuthikulpanit, Suravee }
1450d0562674SSuthikulpanit, Suravee 
145182c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1452d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
14531da177e4SLinus Torvalds {
1454d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1455d43e167dSRafael J. Wysocki 	device->device_type = type;
1456d43e167dSRafael J. Wysocki 	device->handle = handle;
1457d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
14588a0662d9SRafael J. Wysocki 	device->fwnode.type = FWNODE_ACPI;
145925db115bSRafael J. Wysocki 	acpi_set_device_status(device, sta);
1460d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1461c0af4175SToshi Kani 	acpi_set_pnp_ids(handle, &device->pnp, type);
1462ffdcd955SMika Westerberg 	acpi_init_properties(device);
1463d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
14642c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1465202317a5SRafael J. Wysocki 	device->flags.initialized = true;
146610c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(device);
1467cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1468cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1469d0562674SSuthikulpanit, Suravee 	acpi_init_coherency(device);
1470cf860be6SRafael J. Wysocki }
1471bc3b0772SBjorn Helgaas 
1472cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1473cf860be6SRafael J. Wysocki {
1474cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1475cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
14785c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
14795c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
14802c0d4fe0SRafael J. Wysocki 				  unsigned long long sta)
14811da177e4SLinus Torvalds {
148277c24888SBjorn Helgaas 	int result;
148377c24888SBjorn Helgaas 	struct acpi_device *device;
148429aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
14851da177e4SLinus Torvalds 
148636bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
14871da177e4SLinus Torvalds 	if (!device) {
14886468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1489d550d98dSPatrick Mochel 		return -ENOMEM;
14901da177e4SLinus Torvalds 	}
14911da177e4SLinus Torvalds 
1492d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
1493d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1494d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
14951da177e4SLinus Torvalds 
1496cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1497d43e167dSRafael J. Wysocki 	if (result) {
1498718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1499d550d98dSPatrick Mochel 		return result;
15001da177e4SLinus Torvalds 	}
15011da177e4SLinus Torvalds 
1502bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1503cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
15041da177e4SLinus Torvalds 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1505d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1506d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1507d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
15081da177e4SLinus Torvalds 	kfree(buffer.pointer);
15091da177e4SLinus Torvalds 	*child = device;
1510d43e167dSRafael J. Wysocki 	return 0;
1511bf325f95SRafael J. Wysocki }
1512bf325f95SRafael J. Wysocki 
1513a4e081b0SShannon Zhao static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1514a4e081b0SShannon Zhao 					    void *context)
1515a4e081b0SShannon Zhao {
1516a4e081b0SShannon Zhao 	struct resource *res = context;
1517a4e081b0SShannon Zhao 
1518a4e081b0SShannon Zhao 	if (acpi_dev_resource_memory(ares, res))
1519a4e081b0SShannon Zhao 		return AE_CTRL_TERMINATE;
1520a4e081b0SShannon Zhao 
1521a4e081b0SShannon Zhao 	return AE_OK;
1522a4e081b0SShannon Zhao }
1523a4e081b0SShannon Zhao 
1524a4e081b0SShannon Zhao static bool acpi_device_should_be_hidden(acpi_handle handle)
1525a4e081b0SShannon Zhao {
1526a4e081b0SShannon Zhao 	acpi_status status;
1527a4e081b0SShannon Zhao 	struct resource res;
1528a4e081b0SShannon Zhao 
1529a4e081b0SShannon Zhao 	/* Check if it should ignore the UART device */
1530a4e081b0SShannon Zhao 	if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1531a4e081b0SShannon Zhao 		return false;
1532a4e081b0SShannon Zhao 
1533a4e081b0SShannon Zhao 	/*
1534a4e081b0SShannon Zhao 	 * The UART device described in SPCR table is assumed to have only one
1535a4e081b0SShannon Zhao 	 * memory resource present. So we only look for the first one here.
1536a4e081b0SShannon Zhao 	 */
1537a4e081b0SShannon Zhao 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1538a4e081b0SShannon Zhao 				     acpi_get_resource_memory, &res);
1539a4e081b0SShannon Zhao 	if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1540a4e081b0SShannon Zhao 		return false;
1541a4e081b0SShannon Zhao 
1542a4e081b0SShannon Zhao 	acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1543a4e081b0SShannon Zhao 			 &res.start);
1544a4e081b0SShannon Zhao 
1545a4e081b0SShannon Zhao 	return true;
1546a4e081b0SShannon Zhao }
1547a4e081b0SShannon Zhao 
1548778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1549778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1550778cbc1dSBjorn Helgaas {
1551778cbc1dSBjorn Helgaas 	acpi_status status;
1552778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1553778cbc1dSBjorn Helgaas 
1554778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1555778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1556778cbc1dSBjorn Helgaas 		return -ENODEV;
1557778cbc1dSBjorn Helgaas 
1558778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1559778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1560778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1561a4e081b0SShannon Zhao 		if (acpi_device_should_be_hidden(handle))
1562a4e081b0SShannon Zhao 			return -ENODEV;
1563a4e081b0SShannon Zhao 
1564778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1565778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1566778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1567e399037eSAaron Lu 			*sta = 0;
1568778cbc1dSBjorn Helgaas 		break;
1569778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1570778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1571778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1572778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1573778cbc1dSBjorn Helgaas 			return -ENODEV;
1574778cbc1dSBjorn Helgaas 		break;
1575778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1576778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1577778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1578778cbc1dSBjorn Helgaas 		break;
1579778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1580778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1581778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1582778cbc1dSBjorn Helgaas 		break;
1583778cbc1dSBjorn Helgaas 	default:
1584778cbc1dSBjorn Helgaas 		return -ENODEV;
1585778cbc1dSBjorn Helgaas 	}
1586778cbc1dSBjorn Helgaas 
1587778cbc1dSBjorn Helgaas 	return 0;
1588778cbc1dSBjorn Helgaas }
1589778cbc1dSBjorn Helgaas 
1590202317a5SRafael J. Wysocki bool acpi_device_is_present(struct acpi_device *adev)
1591202317a5SRafael J. Wysocki {
1592202317a5SRafael J. Wysocki 	if (adev->status.present || adev->status.functional)
1593202317a5SRafael J. Wysocki 		return true;
1594202317a5SRafael J. Wysocki 
1595202317a5SRafael J. Wysocki 	adev->flags.initialized = false;
1596202317a5SRafael J. Wysocki 	return false;
1597202317a5SRafael J. Wysocki }
1598202317a5SRafael J. Wysocki 
15994b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1600844142c3SRasmus Villemoes 				       const char *idstr,
1601c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1602c5698074SRafael J. Wysocki {
1603c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1604c5698074SRafael J. Wysocki 
1605aca0a4ebSRafael J. Wysocki 	if (handler->match)
1606aca0a4ebSRafael J. Wysocki 		return handler->match(idstr, matchid);
1607aca0a4ebSRafael J. Wysocki 
1608c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1609c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1610c5698074SRafael J. Wysocki 			if (matchid)
1611c5698074SRafael J. Wysocki 				*matchid = devid;
1612c5698074SRafael J. Wysocki 
16134b59cc1fSRafael J. Wysocki 			return true;
16144b59cc1fSRafael J. Wysocki 		}
16154b59cc1fSRafael J. Wysocki 
16164b59cc1fSRafael J. Wysocki 	return false;
16174b59cc1fSRafael J. Wysocki }
16184b59cc1fSRafael J. Wysocki 
1619844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
16206b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
16216b772e8fSToshi Kani {
16226b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
16236b772e8fSToshi Kani 
16246b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
16256b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
16266b772e8fSToshi Kani 			return handler;
16276b772e8fSToshi Kani 
16286b772e8fSToshi Kani 	return NULL;
16296b772e8fSToshi Kani }
16306b772e8fSToshi Kani 
16313f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
16323f8055c3SRafael J. Wysocki {
16333f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
16343f8055c3SRafael J. Wysocki 		return;
16353f8055c3SRafael J. Wysocki 
16363f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
16373f8055c3SRafael J. Wysocki 
16383f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
16393f8055c3SRafael J. Wysocki 
16403f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
16413f8055c3SRafael J. Wysocki }
16423f8055c3SRafael J. Wysocki 
16433c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev)
16444b59cc1fSRafael J. Wysocki {
16456b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
16463c2cc7ffSRafael J. Wysocki 
1647b43109faSRafael J. Wysocki 	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
16481e2380cdSRafael J. Wysocki 		acpi_dock_add(adev);
16491e2380cdSRafael J. Wysocki 		return;
16501e2380cdSRafael J. Wysocki 	}
16513c2cc7ffSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
16524b59cc1fSRafael J. Wysocki 		struct acpi_scan_handler *handler;
16534b59cc1fSRafael J. Wysocki 
16546b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
16551a699476SRafael J. Wysocki 		if (handler) {
16563c2cc7ffSRafael J. Wysocki 			adev->flags.hotplug_notify = true;
16576b772e8fSToshi Kani 			break;
16586b772e8fSToshi Kani 		}
16596b772e8fSToshi Kani 	}
16601a699476SRafael J. Wysocki }
16616b772e8fSToshi Kani 
166240e7fcb1SLan Tianyu static void acpi_device_dep_initialize(struct acpi_device *adev)
166340e7fcb1SLan Tianyu {
166440e7fcb1SLan Tianyu 	struct acpi_dep_data *dep;
166540e7fcb1SLan Tianyu 	struct acpi_handle_list dep_devices;
166640e7fcb1SLan Tianyu 	acpi_status status;
166740e7fcb1SLan Tianyu 	int i;
166840e7fcb1SLan Tianyu 
166940e7fcb1SLan Tianyu 	if (!acpi_has_method(adev->handle, "_DEP"))
167040e7fcb1SLan Tianyu 		return;
167140e7fcb1SLan Tianyu 
167240e7fcb1SLan Tianyu 	status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
167340e7fcb1SLan Tianyu 					&dep_devices);
167440e7fcb1SLan Tianyu 	if (ACPI_FAILURE(status)) {
167580167a24SRafael J. Wysocki 		dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
167640e7fcb1SLan Tianyu 		return;
167740e7fcb1SLan Tianyu 	}
167840e7fcb1SLan Tianyu 
167940e7fcb1SLan Tianyu 	for (i = 0; i < dep_devices.count; i++) {
168040e7fcb1SLan Tianyu 		struct acpi_device_info *info;
168140e7fcb1SLan Tianyu 		int skip;
168240e7fcb1SLan Tianyu 
168340e7fcb1SLan Tianyu 		status = acpi_get_object_info(dep_devices.handles[i], &info);
168440e7fcb1SLan Tianyu 		if (ACPI_FAILURE(status)) {
168580167a24SRafael J. Wysocki 			dev_dbg(&adev->dev, "Error reading _DEP device info\n");
168640e7fcb1SLan Tianyu 			continue;
168740e7fcb1SLan Tianyu 		}
168840e7fcb1SLan Tianyu 
168940e7fcb1SLan Tianyu 		/*
169040e7fcb1SLan Tianyu 		 * Skip the dependency of Windows System Power
169140e7fcb1SLan Tianyu 		 * Management Controller
169240e7fcb1SLan Tianyu 		 */
169340e7fcb1SLan Tianyu 		skip = info->valid & ACPI_VALID_HID &&
169440e7fcb1SLan Tianyu 			!strcmp(info->hardware_id.string, "INT3396");
169540e7fcb1SLan Tianyu 
169640e7fcb1SLan Tianyu 		kfree(info);
169740e7fcb1SLan Tianyu 
169840e7fcb1SLan Tianyu 		if (skip)
169940e7fcb1SLan Tianyu 			continue;
170040e7fcb1SLan Tianyu 
170140e7fcb1SLan Tianyu 		dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
170240e7fcb1SLan Tianyu 		if (!dep)
170340e7fcb1SLan Tianyu 			return;
170440e7fcb1SLan Tianyu 
170540e7fcb1SLan Tianyu 		dep->master = dep_devices.handles[i];
170640e7fcb1SLan Tianyu 		dep->slave  = adev->handle;
170740e7fcb1SLan Tianyu 		adev->dep_unmet++;
170840e7fcb1SLan Tianyu 
170940e7fcb1SLan Tianyu 		mutex_lock(&acpi_dep_list_lock);
171040e7fcb1SLan Tianyu 		list_add_tail(&dep->node , &acpi_dep_list);
171140e7fcb1SLan Tianyu 		mutex_unlock(&acpi_dep_list_lock);
171240e7fcb1SLan Tianyu 	}
171340e7fcb1SLan Tianyu }
171440e7fcb1SLan Tianyu 
1715e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1716e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
17171da177e4SLinus Torvalds {
1718805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1719778cbc1dSBjorn Helgaas 	int type;
1720778cbc1dSBjorn Helgaas 	unsigned long long sta;
1721778cbc1dSBjorn Helgaas 	int result;
17221da177e4SLinus Torvalds 
17234002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
17244002bf38SRafael J. Wysocki 	if (device)
17254002bf38SRafael J. Wysocki 		goto out;
17264002bf38SRafael J. Wysocki 
1727778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1728778cbc1dSBjorn Helgaas 	if (result)
172951a85fafSBjorn Helgaas 		return AE_OK;
17301da177e4SLinus Torvalds 
173182c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
173282c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
173382c7d5efSRafael J. Wysocki 		return AE_OK;
173482c7d5efSRafael J. Wysocki 	}
173582c7d5efSRafael J. Wysocki 
17362c0d4fe0SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta);
1737e3b87f8aSBjorn Helgaas 	if (!device)
173851a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
17391da177e4SLinus Torvalds 
17403c2cc7ffSRafael J. Wysocki 	acpi_scan_init_hotplug(device);
174140e7fcb1SLan Tianyu 	acpi_device_dep_initialize(device);
17423c2cc7ffSRafael J. Wysocki 
17434002bf38SRafael J. Wysocki  out:
174451a85fafSBjorn Helgaas 	if (!*return_value)
174551a85fafSBjorn Helgaas 		*return_value = device;
1746805d410fSRafael J. Wysocki 
174751a85fafSBjorn Helgaas 	return AE_OK;
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
175048459340SZhang Rui static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
175148459340SZhang Rui {
175248459340SZhang Rui 	bool *is_spi_i2c_slave_p = data;
175348459340SZhang Rui 
175448459340SZhang Rui 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
175548459340SZhang Rui 		return 1;
175648459340SZhang Rui 
175748459340SZhang Rui 	/*
175848459340SZhang Rui 	 * devices that are connected to UART still need to be enumerated to
175948459340SZhang Rui 	 * platform bus
176048459340SZhang Rui 	 */
176148459340SZhang Rui 	if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
176248459340SZhang Rui 		*is_spi_i2c_slave_p = true;
176348459340SZhang Rui 
176448459340SZhang Rui 	 /* no need to do more checking */
176548459340SZhang Rui 	return -1;
176648459340SZhang Rui }
176748459340SZhang Rui 
176848459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device)
176948459340SZhang Rui {
177048459340SZhang Rui 	struct list_head resource_list;
177148459340SZhang Rui 	bool is_spi_i2c_slave = false;
177248459340SZhang Rui 
177348459340SZhang Rui 	/*
177468bdb677SOctavian Purdila 	 * Do not enumerate SPI/I2C slaves as they will be enumerated by their
177548459340SZhang Rui 	 * respective parents.
177648459340SZhang Rui 	 */
177748459340SZhang Rui 	INIT_LIST_HEAD(&resource_list);
177848459340SZhang Rui 	acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
177948459340SZhang Rui 			       &is_spi_i2c_slave);
178048459340SZhang Rui 	acpi_dev_free_resource_list(&resource_list);
178110c7e20bSOctavian Purdila 	if (!is_spi_i2c_slave) {
17821571875bSHeikki Krogerus 		acpi_create_platform_device(device, NULL);
178310c7e20bSOctavian Purdila 		acpi_device_set_enumerated(device);
178468bdb677SOctavian Purdila 	} else {
178568bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
178668bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_ADD, device);
178710c7e20bSOctavian Purdila 	}
178848459340SZhang Rui }
178948459340SZhang Rui 
17907d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = {
1791ee892094SRafael J. Wysocki 	{ACPI_DT_NAMESPACE_HID, },
17927d284352SRafael J. Wysocki 	{"", },
17937d284352SRafael J. Wysocki };
17947d284352SRafael J. Wysocki 
17957d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev,
17967d284352SRafael J. Wysocki 				      const struct acpi_device_id *not_used)
17977d284352SRafael J. Wysocki {
17987d284352SRafael J. Wysocki 	/*
1799ee892094SRafael J. Wysocki 	 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
1800ee892094SRafael J. Wysocki 	 * below can be unconditional.
18017d284352SRafael J. Wysocki 	 */
18027d284352SRafael J. Wysocki 	if (adev->data.of_compatible)
18037d284352SRafael J. Wysocki 		acpi_default_enumeration(adev);
18047d284352SRafael J. Wysocki 
18057d284352SRafael J. Wysocki 	return 1;
18067d284352SRafael J. Wysocki }
18077d284352SRafael J. Wysocki 
18087d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = {
18097d284352SRafael J. Wysocki 	.ids = generic_device_ids,
18107d284352SRafael J. Wysocki 	.attach = acpi_generic_device_attach,
18117d284352SRafael J. Wysocki };
18127d284352SRafael J. Wysocki 
181387b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
181487b85b3cSRafael J. Wysocki {
181587b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
181687b85b3cSRafael J. Wysocki 	int ret = 0;
181787b85b3cSRafael J. Wysocki 
181887b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
1819c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
1820c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
182187b85b3cSRafael J. Wysocki 
1822c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
1823c5698074SRafael J. Wysocki 		if (handler) {
1824d34afa9dSRafael J. Wysocki 			if (!handler->attach) {
1825d34afa9dSRafael J. Wysocki 				device->pnp.type.platform_id = 0;
1826d34afa9dSRafael J. Wysocki 				continue;
1827d34afa9dSRafael J. Wysocki 			}
1828c5698074SRafael J. Wysocki 			device->handler = handler;
18299cb32acfSRafael J. Wysocki 			ret = handler->attach(device, devid);
18309cb32acfSRafael J. Wysocki 			if (ret > 0)
1831c5698074SRafael J. Wysocki 				break;
18329cb32acfSRafael J. Wysocki 
18339cb32acfSRafael J. Wysocki 			device->handler = NULL;
18349cb32acfSRafael J. Wysocki 			if (ret < 0)
1835c5698074SRafael J. Wysocki 				break;
1836c5698074SRafael J. Wysocki 		}
1837c5698074SRafael J. Wysocki 	}
183848459340SZhang Rui 
1839ca589f94SRafael J. Wysocki 	return ret;
1840ca589f94SRafael J. Wysocki }
1841ca589f94SRafael J. Wysocki 
18422c22e652SRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device)
184351a85fafSBjorn Helgaas {
18442c22e652SRafael J. Wysocki 	struct acpi_device *child;
18451e2380cdSRafael J. Wysocki 	acpi_handle ejd;
1846ca589f94SRafael J. Wysocki 	int ret;
18471da177e4SLinus Torvalds 
18481e2380cdSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
18491e2380cdSRafael J. Wysocki 		register_dock_dependent_device(device, ejd);
18501e2380cdSRafael J. Wysocki 
18512c22e652SRafael J. Wysocki 	acpi_bus_get_status(device);
1852202317a5SRafael J. Wysocki 	/* Skip devices that are not present. */
18532c22e652SRafael J. Wysocki 	if (!acpi_device_is_present(device)) {
185410c7e20bSOctavian Purdila 		acpi_device_clear_enumerated(device);
18551b1f3e16SRafael J. Wysocki 		device->flags.power_manageable = 0;
18562c22e652SRafael J. Wysocki 		return;
18572c22e652SRafael J. Wysocki 	}
18583a391a39SRafael J. Wysocki 	if (device->handler)
18592c22e652SRafael J. Wysocki 		goto ok;
18603a391a39SRafael J. Wysocki 
1861202317a5SRafael J. Wysocki 	if (!device->flags.initialized) {
18621b1f3e16SRafael J. Wysocki 		device->flags.power_manageable =
18631b1f3e16SRafael J. Wysocki 			device->power.states[ACPI_STATE_D0].flags.valid;
18641b1f3e16SRafael J. Wysocki 		if (acpi_bus_init_power(device))
18651b1f3e16SRafael J. Wysocki 			device->flags.power_manageable = 0;
18661b1f3e16SRafael J. Wysocki 
1867202317a5SRafael J. Wysocki 		device->flags.initialized = true;
1868202317a5SRafael J. Wysocki 	}
186910c7e20bSOctavian Purdila 
1870ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
18716931007cSRafael J. Wysocki 	if (ret < 0)
18722c22e652SRafael J. Wysocki 		return;
18736931007cSRafael J. Wysocki 
18746931007cSRafael J. Wysocki 	device->flags.match_driver = true;
18752c22e652SRafael J. Wysocki 	if (!ret) {
1876ca589f94SRafael J. Wysocki 		ret = device_attach(&device->dev);
1877202317a5SRafael J. Wysocki 		if (ret < 0)
18782c22e652SRafael J. Wysocki 			return;
18797d284352SRafael J. Wysocki 
18807d284352SRafael J. Wysocki 		if (!ret && device->pnp.type.platform_id)
18817d284352SRafael J. Wysocki 			acpi_default_enumeration(device);
18822c22e652SRafael J. Wysocki 	}
1883202317a5SRafael J. Wysocki 
1884202317a5SRafael J. Wysocki  ok:
18852c22e652SRafael J. Wysocki 	list_for_each_entry(child, &device->children, node)
18862c22e652SRafael J. Wysocki 		acpi_bus_attach(child);
18878ab17fc9SRafael J. Wysocki 
18888ab17fc9SRafael J. Wysocki 	if (device->handler && device->handler->hotplug.notify_online)
18898ab17fc9SRafael J. Wysocki 		device->handler->hotplug.notify_online(device);
1890805d410fSRafael J. Wysocki }
18913fb02738SRajesh Shah 
189240e7fcb1SLan Tianyu void acpi_walk_dep_device_list(acpi_handle handle)
189340e7fcb1SLan Tianyu {
189440e7fcb1SLan Tianyu 	struct acpi_dep_data *dep, *tmp;
189540e7fcb1SLan Tianyu 	struct acpi_device *adev;
189640e7fcb1SLan Tianyu 
189740e7fcb1SLan Tianyu 	mutex_lock(&acpi_dep_list_lock);
189840e7fcb1SLan Tianyu 	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
189940e7fcb1SLan Tianyu 		if (dep->master == handle) {
190040e7fcb1SLan Tianyu 			acpi_bus_get_device(dep->slave, &adev);
190140e7fcb1SLan Tianyu 			if (!adev)
190240e7fcb1SLan Tianyu 				continue;
190340e7fcb1SLan Tianyu 
190440e7fcb1SLan Tianyu 			adev->dep_unmet--;
190540e7fcb1SLan Tianyu 			if (!adev->dep_unmet)
190640e7fcb1SLan Tianyu 				acpi_bus_attach(adev);
190740e7fcb1SLan Tianyu 			list_del(&dep->node);
190840e7fcb1SLan Tianyu 			kfree(dep);
190940e7fcb1SLan Tianyu 		}
191040e7fcb1SLan Tianyu 	}
191140e7fcb1SLan Tianyu 	mutex_unlock(&acpi_dep_list_lock);
191240e7fcb1SLan Tianyu }
191340e7fcb1SLan Tianyu EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
191440e7fcb1SLan Tianyu 
1915b8bd759aSRafael J. Wysocki /**
1916b8bd759aSRafael J. Wysocki  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1917b8bd759aSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
1918b8bd759aSRafael J. Wysocki  *
1919b8bd759aSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1920b8bd759aSRafael J. Wysocki  * found devices.
1921b8bd759aSRafael J. Wysocki  *
1922b8bd759aSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
1923b8bd759aSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
1924b8bd759aSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
1925b8bd759aSRafael J. Wysocki  * appropriate driver.
19263757b948SRafael J. Wysocki  *
19273757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
1928b8bd759aSRafael J. Wysocki  */
1929b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle)
19303fb02738SRajesh Shah {
19311da177e4SLinus Torvalds 	void *device = NULL;
1932c511cc19SRafael J. Wysocki 
19330cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
19341da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1935e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
19363fb02738SRajesh Shah 
19372c22e652SRafael J. Wysocki 	if (device) {
19382c22e652SRafael J. Wysocki 		acpi_bus_attach(device);
19392c22e652SRafael J. Wysocki 		return 0;
19402c22e652SRafael J. Wysocki 	}
19412c22e652SRafael J. Wysocki 	return -ENODEV;
19423fb02738SRajesh Shah }
1943b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan);
19441da177e4SLinus Torvalds 
19453757b948SRafael J. Wysocki /**
19462c22e652SRafael J. Wysocki  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
19472c22e652SRafael J. Wysocki  * @adev: Root of the ACPI namespace scope to walk.
19483757b948SRafael J. Wysocki  *
19493757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
19503757b948SRafael J. Wysocki  */
19512c22e652SRafael J. Wysocki void acpi_bus_trim(struct acpi_device *adev)
19521da177e4SLinus Torvalds {
19532c22e652SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
19542c22e652SRafael J. Wysocki 	struct acpi_device *child;
19552c22e652SRafael J. Wysocki 
19562c22e652SRafael J. Wysocki 	list_for_each_entry_reverse(child, &adev->children, node)
19572c22e652SRafael J. Wysocki 		acpi_bus_trim(child);
19582c22e652SRafael J. Wysocki 
1959a951c773SRafael J. Wysocki 	adev->flags.match_driver = false;
19602c22e652SRafael J. Wysocki 	if (handler) {
19612c22e652SRafael J. Wysocki 		if (handler->detach)
19622c22e652SRafael J. Wysocki 			handler->detach(adev);
19632c22e652SRafael J. Wysocki 
19642c22e652SRafael J. Wysocki 		adev->handler = NULL;
19652c22e652SRafael J. Wysocki 	} else {
19662c22e652SRafael J. Wysocki 		device_release_driver(&adev->dev);
19672c22e652SRafael J. Wysocki 	}
19681da177e4SLinus Torvalds 	/*
19692c22e652SRafael J. Wysocki 	 * Most likely, the device is going away, so put it into D3cold before
19702c22e652SRafael J. Wysocki 	 * that.
19711da177e4SLinus Torvalds 	 */
19722c22e652SRafael J. Wysocki 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
19732c22e652SRafael J. Wysocki 	adev->flags.initialized = false;
197410c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(adev);
19751da177e4SLinus Torvalds }
1976ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1977ceaba663SKristen Accardi 
1978e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
19791da177e4SLinus Torvalds {
19801da177e4SLinus Torvalds 	int result = 0;
1981c4168bffSLi Shaohua 
19821da177e4SLinus Torvalds 	/*
19831da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
19841da177e4SLinus Torvalds 	 */
19852c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
19862c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
19872c0d4fe0SRafael J. Wysocki 
19885c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1989c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
19902c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
19912c0d4fe0SRafael J. Wysocki 		if (result)
19922c0d4fe0SRafael J. Wysocki 			return result;
19932c0d4fe0SRafael J. Wysocki 
199488346167SRafael J. Wysocki 		device->flags.match_driver = true;
19952c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
19962c0d4fe0SRafael J. Wysocki 		if (result < 0)
19972c0d4fe0SRafael J. Wysocki 			return result;
19982c0d4fe0SRafael J. Wysocki 
1999c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
20003fb02738SRajesh Shah 	}
20011da177e4SLinus Torvalds 
20022c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
20032c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
20042c0d4fe0SRafael J. Wysocki 
20055c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2006c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
20072c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
20082c0d4fe0SRafael J. Wysocki 		if (result)
20092c0d4fe0SRafael J. Wysocki 			return result;
20102c0d4fe0SRafael J. Wysocki 
201188346167SRafael J. Wysocki 		device->flags.match_driver = true;
20122c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
20133fb02738SRajesh Shah 	}
20141da177e4SLinus Torvalds 
20152c0d4fe0SRafael J. Wysocki 	return result < 0 ? result : 0;
20161da177e4SLinus Torvalds }
20171da177e4SLinus Torvalds 
2018a4e081b0SShannon Zhao static void __init acpi_get_spcr_uart_addr(void)
2019a4e081b0SShannon Zhao {
2020a4e081b0SShannon Zhao 	acpi_status status;
2021a4e081b0SShannon Zhao 	struct acpi_table_spcr *spcr_ptr;
2022a4e081b0SShannon Zhao 
2023a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_SPCR, 0,
2024a4e081b0SShannon Zhao 				(struct acpi_table_header **)&spcr_ptr);
2025a4e081b0SShannon Zhao 	if (ACPI_SUCCESS(status))
2026a4e081b0SShannon Zhao 		spcr_uart_addr = spcr_ptr->serial_port.address;
2027a4e081b0SShannon Zhao 	else
2028a4e081b0SShannon Zhao 		printk(KERN_WARNING PREFIX "STAO table present, but SPCR is missing\n");
2029a4e081b0SShannon Zhao }
2030a4e081b0SShannon Zhao 
203168bdb677SOctavian Purdila static bool acpi_scan_initialized;
203268bdb677SOctavian Purdila 
2033e747f274SBjorn Helgaas int __init acpi_scan_init(void)
20341da177e4SLinus Torvalds {
20351da177e4SLinus Torvalds 	int result;
2036a4e081b0SShannon Zhao 	acpi_status status;
2037a4e081b0SShannon Zhao 	struct acpi_table_stao *stao_ptr;
20381da177e4SLinus Torvalds 
203992ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
20404daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2041ac212b69SRafael J. Wysocki 	acpi_processor_init();
2042f58b082aSRafael J. Wysocki 	acpi_lpss_init();
204392082a88SKen Xue 	acpi_apd_init();
20442fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2045737f1a9fSRafael J. Wysocki 	acpi_container_init();
20460a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
2047eec15edbSZhang Rui 	acpi_pnp_init();
20483230bbfcSZhang Rui 	acpi_int340x_thermal_init();
20496ce2e188SGraeme Gregory 	acpi_amba_init();
2050058dfc76SMika Westerberg 	acpi_watchdog_init();
205197d9a9e9SRafael J. Wysocki 
20527d284352SRafael J. Wysocki 	acpi_scan_add_handler(&generic_device_handler);
20537d284352SRafael J. Wysocki 
2054a4e081b0SShannon Zhao 	/*
2055a4e081b0SShannon Zhao 	 * If there is STAO table, check whether it needs to ignore the UART
2056a4e081b0SShannon Zhao 	 * device in SPCR table.
2057a4e081b0SShannon Zhao 	 */
2058a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_STAO, 0,
2059a4e081b0SShannon Zhao 				(struct acpi_table_header **)&stao_ptr);
2060a4e081b0SShannon Zhao 	if (ACPI_SUCCESS(status)) {
2061a4e081b0SShannon Zhao 		if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2062a4e081b0SShannon Zhao 			printk(KERN_INFO PREFIX "STAO Name List not yet supported.");
2063a4e081b0SShannon Zhao 
2064a4e081b0SShannon Zhao 		if (stao_ptr->ignore_uart)
2065a4e081b0SShannon Zhao 			acpi_get_spcr_uart_addr();
2066a4e081b0SShannon Zhao 	}
2067a4e081b0SShannon Zhao 
20683757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
20691da177e4SLinus Torvalds 	/*
20701da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
20711da177e4SLinus Torvalds 	 */
20720cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
20731da177e4SLinus Torvalds 	if (result)
20743757b948SRafael J. Wysocki 		goto out;
20751da177e4SLinus Torvalds 
20760cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
20771da177e4SLinus Torvalds 	if (result)
20783757b948SRafael J. Wysocki 		goto out;
2079b8bd759aSRafael J. Wysocki 
20802650ef42SAaron Lu 	/* Fixed feature devices do not exist on HW-reduced platform */
20812650ef42SAaron Lu 	if (!acpi_gbl_reduced_hardware) {
2082b8bd759aSRafael J. Wysocki 		result = acpi_bus_scan_fixed();
2083b8bd759aSRafael J. Wysocki 		if (result) {
20842650ef42SAaron Lu 			acpi_detach_data(acpi_root->handle,
20852650ef42SAaron Lu 					 acpi_scan_drop_device);
2086202317a5SRafael J. Wysocki 			acpi_device_del(acpi_root);
2087202317a5SRafael J. Wysocki 			put_device(&acpi_root->dev);
20883757b948SRafael J. Wysocki 			goto out;
2089b8bd759aSRafael J. Wysocki 		}
20902650ef42SAaron Lu 	}
2091b8bd759aSRafael J. Wysocki 
2092b8bd759aSRafael J. Wysocki 	acpi_update_all_gpes();
20932a570840SLv Zheng 	acpi_ec_ecdt_start();
20943757b948SRafael J. Wysocki 
209568bdb677SOctavian Purdila 	acpi_scan_initialized = true;
209668bdb677SOctavian Purdila 
20973757b948SRafael J. Wysocki  out:
20983757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
20993757b948SRafael J. Wysocki 	return result;
21001da177e4SLinus Torvalds }
2101e647b532SMarc Zyngier 
2102e647b532SMarc Zyngier static struct acpi_probe_entry *ape;
2103e647b532SMarc Zyngier static int acpi_probe_count;
21045331d9caSLorenzo Pieralisi static DEFINE_MUTEX(acpi_probe_mutex);
2105e647b532SMarc Zyngier 
2106e647b532SMarc Zyngier static int __init acpi_match_madt(struct acpi_subtable_header *header,
2107e647b532SMarc Zyngier 				  const unsigned long end)
2108e647b532SMarc Zyngier {
2109e647b532SMarc Zyngier 	if (!ape->subtable_valid || ape->subtable_valid(header, ape))
2110e647b532SMarc Zyngier 		if (!ape->probe_subtbl(header, end))
2111e647b532SMarc Zyngier 			acpi_probe_count++;
2112e647b532SMarc Zyngier 
2113e647b532SMarc Zyngier 	return 0;
2114e647b532SMarc Zyngier }
2115e647b532SMarc Zyngier 
2116e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2117e647b532SMarc Zyngier {
2118e647b532SMarc Zyngier 	int count = 0;
2119e647b532SMarc Zyngier 
2120e647b532SMarc Zyngier 	if (acpi_disabled)
2121e647b532SMarc Zyngier 		return 0;
2122e647b532SMarc Zyngier 
21235331d9caSLorenzo Pieralisi 	mutex_lock(&acpi_probe_mutex);
2124e647b532SMarc Zyngier 	for (ape = ap_head; nr; ape++, nr--) {
2125e647b532SMarc Zyngier 		if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
2126e647b532SMarc Zyngier 			acpi_probe_count = 0;
2127e647b532SMarc Zyngier 			acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2128e647b532SMarc Zyngier 			count += acpi_probe_count;
2129e647b532SMarc Zyngier 		} else {
2130e647b532SMarc Zyngier 			int res;
2131e647b532SMarc Zyngier 			res = acpi_table_parse(ape->id, ape->probe_table);
2132e647b532SMarc Zyngier 			if (!res)
2133e647b532SMarc Zyngier 				count++;
2134e647b532SMarc Zyngier 		}
2135e647b532SMarc Zyngier 	}
21365331d9caSLorenzo Pieralisi 	mutex_unlock(&acpi_probe_mutex);
2137e647b532SMarc Zyngier 
2138e647b532SMarc Zyngier 	return count;
2139e647b532SMarc Zyngier }
214068bdb677SOctavian Purdila 
214168bdb677SOctavian Purdila struct acpi_table_events_work {
214268bdb677SOctavian Purdila 	struct work_struct work;
214368bdb677SOctavian Purdila 	void *table;
214468bdb677SOctavian Purdila 	u32 event;
214568bdb677SOctavian Purdila };
214668bdb677SOctavian Purdila 
214768bdb677SOctavian Purdila static void acpi_table_events_fn(struct work_struct *work)
214868bdb677SOctavian Purdila {
214968bdb677SOctavian Purdila 	struct acpi_table_events_work *tew;
215068bdb677SOctavian Purdila 
215168bdb677SOctavian Purdila 	tew = container_of(work, struct acpi_table_events_work, work);
215268bdb677SOctavian Purdila 
215368bdb677SOctavian Purdila 	if (tew->event == ACPI_TABLE_EVENT_LOAD) {
215468bdb677SOctavian Purdila 		acpi_scan_lock_acquire();
215568bdb677SOctavian Purdila 		acpi_bus_scan(ACPI_ROOT_OBJECT);
215668bdb677SOctavian Purdila 		acpi_scan_lock_release();
215768bdb677SOctavian Purdila 	}
215868bdb677SOctavian Purdila 
215968bdb677SOctavian Purdila 	kfree(tew);
216068bdb677SOctavian Purdila }
216168bdb677SOctavian Purdila 
216268bdb677SOctavian Purdila void acpi_scan_table_handler(u32 event, void *table, void *context)
216368bdb677SOctavian Purdila {
216468bdb677SOctavian Purdila 	struct acpi_table_events_work *tew;
216568bdb677SOctavian Purdila 
216668bdb677SOctavian Purdila 	if (!acpi_scan_initialized)
216768bdb677SOctavian Purdila 		return;
216868bdb677SOctavian Purdila 
216968bdb677SOctavian Purdila 	if (event != ACPI_TABLE_EVENT_LOAD)
217068bdb677SOctavian Purdila 		return;
217168bdb677SOctavian Purdila 
217268bdb677SOctavian Purdila 	tew = kmalloc(sizeof(*tew), GFP_KERNEL);
217368bdb677SOctavian Purdila 	if (!tew)
217468bdb677SOctavian Purdila 		return;
217568bdb677SOctavian Purdila 
217668bdb677SOctavian Purdila 	INIT_WORK(&tew->work, acpi_table_events_fn);
217768bdb677SOctavian Purdila 	tew->table = table;
217868bdb677SOctavian Purdila 	tew->event = event;
217968bdb677SOctavian Purdila 
218068bdb677SOctavian Purdila 	schedule_work(&tew->work);
218168bdb677SOctavian Purdila }
218268bdb677SOctavian Purdila 
218368bdb677SOctavian Purdila int acpi_reconfig_notifier_register(struct notifier_block *nb)
218468bdb677SOctavian Purdila {
218568bdb677SOctavian Purdila 	return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
218668bdb677SOctavian Purdila }
218768bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_register);
218868bdb677SOctavian Purdila 
218968bdb677SOctavian Purdila int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
219068bdb677SOctavian Purdila {
219168bdb677SOctavian Purdila 	return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
219268bdb677SOctavian Purdila }
219368bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);
2194