xref: /kernel/linux/linux-6.6/drivers/acpi/scan.c (revision 62306a36)
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * scan.c - support for transforming the ACPI namespace into individual objects
4 */
5
6#define pr_fmt(fmt) "ACPI: " fmt
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/kernel.h>
12#include <linux/acpi.h>
13#include <linux/acpi_iort.h>
14#include <linux/acpi_viot.h>
15#include <linux/iommu.h>
16#include <linux/signal.h>
17#include <linux/kthread.h>
18#include <linux/dmi.h>
19#include <linux/dma-map-ops.h>
20#include <linux/platform_data/x86/apple.h>
21#include <linux/pgtable.h>
22#include <linux/crc32.h>
23#include <linux/dma-direct.h>
24
25#include "internal.h"
26#include "sleep.h"
27
28#define ACPI_BUS_CLASS			"system_bus"
29#define ACPI_BUS_HID			"LNXSYBUS"
30#define ACPI_BUS_DEVICE_NAME		"System Bus"
31
32#define INVALID_ACPI_HANDLE	((acpi_handle)ZERO_PAGE(0))
33
34static const char *dummy_hid = "device";
35
36static LIST_HEAD(acpi_dep_list);
37static DEFINE_MUTEX(acpi_dep_list_lock);
38LIST_HEAD(acpi_bus_id_list);
39static DEFINE_MUTEX(acpi_scan_lock);
40static LIST_HEAD(acpi_scan_handlers_list);
41DEFINE_MUTEX(acpi_device_lock);
42LIST_HEAD(acpi_wakeup_device_list);
43static DEFINE_MUTEX(acpi_hp_context_lock);
44
45/*
46 * The UART device described by the SPCR table is the only object which needs
47 * special-casing. Everything else is covered by ACPI namespace paths in STAO
48 * table.
49 */
50static u64 spcr_uart_addr;
51
52void acpi_scan_lock_acquire(void)
53{
54	mutex_lock(&acpi_scan_lock);
55}
56EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
57
58void acpi_scan_lock_release(void)
59{
60	mutex_unlock(&acpi_scan_lock);
61}
62EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
63
64void acpi_lock_hp_context(void)
65{
66	mutex_lock(&acpi_hp_context_lock);
67}
68
69void acpi_unlock_hp_context(void)
70{
71	mutex_unlock(&acpi_hp_context_lock);
72}
73
74void acpi_initialize_hp_context(struct acpi_device *adev,
75				struct acpi_hotplug_context *hp,
76				int (*notify)(struct acpi_device *, u32),
77				void (*uevent)(struct acpi_device *, u32))
78{
79	acpi_lock_hp_context();
80	hp->notify = notify;
81	hp->uevent = uevent;
82	acpi_set_hp_context(adev, hp);
83	acpi_unlock_hp_context();
84}
85EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
86
87int acpi_scan_add_handler(struct acpi_scan_handler *handler)
88{
89	if (!handler)
90		return -EINVAL;
91
92	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
93	return 0;
94}
95
96int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
97				       const char *hotplug_profile_name)
98{
99	int error;
100
101	error = acpi_scan_add_handler(handler);
102	if (error)
103		return error;
104
105	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
106	return 0;
107}
108
109bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
110{
111	struct acpi_device_physical_node *pn;
112	bool offline = true;
113	char *envp[] = { "EVENT=offline", NULL };
114
115	/*
116	 * acpi_container_offline() calls this for all of the container's
117	 * children under the container's physical_node_lock lock.
118	 */
119	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
120
121	list_for_each_entry(pn, &adev->physical_node_list, node)
122		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
123			if (uevent)
124				kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
125
126			offline = false;
127			break;
128		}
129
130	mutex_unlock(&adev->physical_node_lock);
131	return offline;
132}
133
134static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
135				    void **ret_p)
136{
137	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
138	struct acpi_device_physical_node *pn;
139	bool second_pass = (bool)data;
140	acpi_status status = AE_OK;
141
142	if (!device)
143		return AE_OK;
144
145	if (device->handler && !device->handler->hotplug.enabled) {
146		*ret_p = &device->dev;
147		return AE_SUPPORT;
148	}
149
150	mutex_lock(&device->physical_node_lock);
151
152	list_for_each_entry(pn, &device->physical_node_list, node) {
153		int ret;
154
155		if (second_pass) {
156			/* Skip devices offlined by the first pass. */
157			if (pn->put_online)
158				continue;
159		} else {
160			pn->put_online = false;
161		}
162		ret = device_offline(pn->dev);
163		if (ret >= 0) {
164			pn->put_online = !ret;
165		} else {
166			*ret_p = pn->dev;
167			if (second_pass) {
168				status = AE_ERROR;
169				break;
170			}
171		}
172	}
173
174	mutex_unlock(&device->physical_node_lock);
175
176	return status;
177}
178
179static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
180				   void **ret_p)
181{
182	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
183	struct acpi_device_physical_node *pn;
184
185	if (!device)
186		return AE_OK;
187
188	mutex_lock(&device->physical_node_lock);
189
190	list_for_each_entry(pn, &device->physical_node_list, node)
191		if (pn->put_online) {
192			device_online(pn->dev);
193			pn->put_online = false;
194		}
195
196	mutex_unlock(&device->physical_node_lock);
197
198	return AE_OK;
199}
200
201static int acpi_scan_try_to_offline(struct acpi_device *device)
202{
203	acpi_handle handle = device->handle;
204	struct device *errdev = NULL;
205	acpi_status status;
206
207	/*
208	 * Carry out two passes here and ignore errors in the first pass,
209	 * because if the devices in question are memory blocks and
210	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
211	 * that the other blocks depend on, but it is not known in advance which
212	 * block holds them.
213	 *
214	 * If the first pass is successful, the second one isn't needed, though.
215	 */
216	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
217				     NULL, acpi_bus_offline, (void *)false,
218				     (void **)&errdev);
219	if (status == AE_SUPPORT) {
220		dev_warn(errdev, "Offline disabled.\n");
221		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
222				    acpi_bus_online, NULL, NULL, NULL);
223		return -EPERM;
224	}
225	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
226	if (errdev) {
227		errdev = NULL;
228		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
229				    NULL, acpi_bus_offline, (void *)true,
230				    (void **)&errdev);
231		if (!errdev)
232			acpi_bus_offline(handle, 0, (void *)true,
233					 (void **)&errdev);
234
235		if (errdev) {
236			dev_warn(errdev, "Offline failed.\n");
237			acpi_bus_online(handle, 0, NULL, NULL);
238			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
239					    ACPI_UINT32_MAX, acpi_bus_online,
240					    NULL, NULL, NULL);
241			return -EBUSY;
242		}
243	}
244	return 0;
245}
246
247static int acpi_scan_hot_remove(struct acpi_device *device)
248{
249	acpi_handle handle = device->handle;
250	unsigned long long sta;
251	acpi_status status;
252
253	if (device->handler && device->handler->hotplug.demand_offline) {
254		if (!acpi_scan_is_offline(device, true))
255			return -EBUSY;
256	} else {
257		int error = acpi_scan_try_to_offline(device);
258		if (error)
259			return error;
260	}
261
262	acpi_handle_debug(handle, "Ejecting\n");
263
264	acpi_bus_trim(device);
265
266	acpi_evaluate_lck(handle, 0);
267	/*
268	 * TBD: _EJD support.
269	 */
270	status = acpi_evaluate_ej0(handle);
271	if (status == AE_NOT_FOUND)
272		return -ENODEV;
273	else if (ACPI_FAILURE(status))
274		return -EIO;
275
276	/*
277	 * Verify if eject was indeed successful.  If not, log an error
278	 * message.  No need to call _OST since _EJ0 call was made OK.
279	 */
280	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
281	if (ACPI_FAILURE(status)) {
282		acpi_handle_warn(handle,
283			"Status check after eject failed (0x%x)\n", status);
284	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
285		acpi_handle_warn(handle,
286			"Eject incomplete - status 0x%llx\n", sta);
287	}
288
289	return 0;
290}
291
292static int acpi_scan_device_not_present(struct acpi_device *adev)
293{
294	if (!acpi_device_enumerated(adev)) {
295		dev_warn(&adev->dev, "Still not present\n");
296		return -EALREADY;
297	}
298	acpi_bus_trim(adev);
299	return 0;
300}
301
302static int acpi_scan_device_check(struct acpi_device *adev)
303{
304	int error;
305
306	acpi_bus_get_status(adev);
307	if (adev->status.present || adev->status.functional) {
308		/*
309		 * This function is only called for device objects for which
310		 * matching scan handlers exist.  The only situation in which
311		 * the scan handler is not attached to this device object yet
312		 * is when the device has just appeared (either it wasn't
313		 * present at all before or it was removed and then added
314		 * again).
315		 */
316		if (adev->handler) {
317			dev_dbg(&adev->dev, "Already enumerated\n");
318			return 0;
319		}
320		error = acpi_bus_scan(adev->handle);
321		if (error) {
322			dev_warn(&adev->dev, "Namespace scan failure\n");
323			return error;
324		}
325	} else {
326		error = acpi_scan_device_not_present(adev);
327	}
328	return error;
329}
330
331static int acpi_scan_bus_check(struct acpi_device *adev, void *not_used)
332{
333	struct acpi_scan_handler *handler = adev->handler;
334	int error;
335
336	acpi_bus_get_status(adev);
337	if (!(adev->status.present || adev->status.functional)) {
338		acpi_scan_device_not_present(adev);
339		return 0;
340	}
341	if (handler && handler->hotplug.scan_dependent)
342		return handler->hotplug.scan_dependent(adev);
343
344	error = acpi_bus_scan(adev->handle);
345	if (error) {
346		dev_warn(&adev->dev, "Namespace scan failure\n");
347		return error;
348	}
349	return acpi_dev_for_each_child(adev, acpi_scan_bus_check, NULL);
350}
351
352static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
353{
354	switch (type) {
355	case ACPI_NOTIFY_BUS_CHECK:
356		return acpi_scan_bus_check(adev, NULL);
357	case ACPI_NOTIFY_DEVICE_CHECK:
358		return acpi_scan_device_check(adev);
359	case ACPI_NOTIFY_EJECT_REQUEST:
360	case ACPI_OST_EC_OSPM_EJECT:
361		if (adev->handler && !adev->handler->hotplug.enabled) {
362			dev_info(&adev->dev, "Eject disabled\n");
363			return -EPERM;
364		}
365		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
366				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
367		return acpi_scan_hot_remove(adev);
368	}
369	return -EINVAL;
370}
371
372void acpi_device_hotplug(struct acpi_device *adev, u32 src)
373{
374	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
375	int error = -ENODEV;
376
377	lock_device_hotplug();
378	mutex_lock(&acpi_scan_lock);
379
380	/*
381	 * The device object's ACPI handle cannot become invalid as long as we
382	 * are holding acpi_scan_lock, but it might have become invalid before
383	 * that lock was acquired.
384	 */
385	if (adev->handle == INVALID_ACPI_HANDLE)
386		goto err_out;
387
388	if (adev->flags.is_dock_station) {
389		error = dock_notify(adev, src);
390	} else if (adev->flags.hotplug_notify) {
391		error = acpi_generic_hotplug_event(adev, src);
392	} else {
393		int (*notify)(struct acpi_device *, u32);
394
395		acpi_lock_hp_context();
396		notify = adev->hp ? adev->hp->notify : NULL;
397		acpi_unlock_hp_context();
398		/*
399		 * There may be additional notify handlers for device objects
400		 * without the .event() callback, so ignore them here.
401		 */
402		if (notify)
403			error = notify(adev, src);
404		else
405			goto out;
406	}
407	switch (error) {
408	case 0:
409		ost_code = ACPI_OST_SC_SUCCESS;
410		break;
411	case -EPERM:
412		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
413		break;
414	case -EBUSY:
415		ost_code = ACPI_OST_SC_DEVICE_BUSY;
416		break;
417	default:
418		ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
419		break;
420	}
421
422 err_out:
423	acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
424
425 out:
426	acpi_put_acpi_dev(adev);
427	mutex_unlock(&acpi_scan_lock);
428	unlock_device_hotplug();
429}
430
431static void acpi_free_power_resources_lists(struct acpi_device *device)
432{
433	int i;
434
435	if (device->wakeup.flags.valid)
436		acpi_power_resources_list_free(&device->wakeup.resources);
437
438	if (!device->power.flags.power_resources)
439		return;
440
441	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
442		struct acpi_device_power_state *ps = &device->power.states[i];
443		acpi_power_resources_list_free(&ps->resources);
444	}
445}
446
447static void acpi_device_release(struct device *dev)
448{
449	struct acpi_device *acpi_dev = to_acpi_device(dev);
450
451	acpi_free_properties(acpi_dev);
452	acpi_free_pnp_ids(&acpi_dev->pnp);
453	acpi_free_power_resources_lists(acpi_dev);
454	kfree(acpi_dev);
455}
456
457static void acpi_device_del(struct acpi_device *device)
458{
459	struct acpi_device_bus_id *acpi_device_bus_id;
460
461	mutex_lock(&acpi_device_lock);
462
463	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
464		if (!strcmp(acpi_device_bus_id->bus_id,
465			    acpi_device_hid(device))) {
466			ida_free(&acpi_device_bus_id->instance_ida,
467				 device->pnp.instance_no);
468			if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
469				list_del(&acpi_device_bus_id->node);
470				kfree_const(acpi_device_bus_id->bus_id);
471				kfree(acpi_device_bus_id);
472			}
473			break;
474		}
475
476	list_del(&device->wakeup_list);
477
478	mutex_unlock(&acpi_device_lock);
479
480	acpi_power_add_remove_device(device, false);
481	acpi_device_remove_files(device);
482	if (device->remove)
483		device->remove(device);
484
485	device_del(&device->dev);
486}
487
488static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
489
490static LIST_HEAD(acpi_device_del_list);
491static DEFINE_MUTEX(acpi_device_del_lock);
492
493static void acpi_device_del_work_fn(struct work_struct *work_not_used)
494{
495	for (;;) {
496		struct acpi_device *adev;
497
498		mutex_lock(&acpi_device_del_lock);
499
500		if (list_empty(&acpi_device_del_list)) {
501			mutex_unlock(&acpi_device_del_lock);
502			break;
503		}
504		adev = list_first_entry(&acpi_device_del_list,
505					struct acpi_device, del_list);
506		list_del(&adev->del_list);
507
508		mutex_unlock(&acpi_device_del_lock);
509
510		blocking_notifier_call_chain(&acpi_reconfig_chain,
511					     ACPI_RECONFIG_DEVICE_REMOVE, adev);
512
513		acpi_device_del(adev);
514		/*
515		 * Drop references to all power resources that might have been
516		 * used by the device.
517		 */
518		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
519		acpi_dev_put(adev);
520	}
521}
522
523/**
524 * acpi_scan_drop_device - Drop an ACPI device object.
525 * @handle: Handle of an ACPI namespace node, not used.
526 * @context: Address of the ACPI device object to drop.
527 *
528 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
529 * namespace node the device object pointed to by @context is attached to.
530 *
531 * The unregistration is carried out asynchronously to avoid running
532 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
533 * ensure the correct ordering (the device objects must be unregistered in the
534 * same order in which the corresponding namespace nodes are deleted).
535 */
536static void acpi_scan_drop_device(acpi_handle handle, void *context)
537{
538	static DECLARE_WORK(work, acpi_device_del_work_fn);
539	struct acpi_device *adev = context;
540
541	mutex_lock(&acpi_device_del_lock);
542
543	/*
544	 * Use the ACPI hotplug workqueue which is ordered, so this work item
545	 * won't run after any hotplug work items submitted subsequently.  That
546	 * prevents attempts to register device objects identical to those being
547	 * deleted from happening concurrently (such attempts result from
548	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
549	 * run after all of the work items submitted previously, which helps
550	 * those work items to ensure that they are not accessing stale device
551	 * objects.
552	 */
553	if (list_empty(&acpi_device_del_list))
554		acpi_queue_hotplug_work(&work);
555
556	list_add_tail(&adev->del_list, &acpi_device_del_list);
557	/* Make acpi_ns_validate_handle() return NULL for this handle. */
558	adev->handle = INVALID_ACPI_HANDLE;
559
560	mutex_unlock(&acpi_device_del_lock);
561}
562
563static struct acpi_device *handle_to_device(acpi_handle handle,
564					    void (*callback)(void *))
565{
566	struct acpi_device *adev = NULL;
567	acpi_status status;
568
569	status = acpi_get_data_full(handle, acpi_scan_drop_device,
570				    (void **)&adev, callback);
571	if (ACPI_FAILURE(status) || !adev) {
572		acpi_handle_debug(handle, "No context!\n");
573		return NULL;
574	}
575	return adev;
576}
577
578/**
579 * acpi_fetch_acpi_dev - Retrieve ACPI device object.
580 * @handle: ACPI handle associated with the requested ACPI device object.
581 *
582 * Return a pointer to the ACPI device object associated with @handle, if
583 * present, or NULL otherwise.
584 */
585struct acpi_device *acpi_fetch_acpi_dev(acpi_handle handle)
586{
587	return handle_to_device(handle, NULL);
588}
589EXPORT_SYMBOL_GPL(acpi_fetch_acpi_dev);
590
591static void get_acpi_device(void *dev)
592{
593	acpi_dev_get(dev);
594}
595
596/**
597 * acpi_get_acpi_dev - Retrieve ACPI device object and reference count it.
598 * @handle: ACPI handle associated with the requested ACPI device object.
599 *
600 * Return a pointer to the ACPI device object associated with @handle and bump
601 * up that object's reference counter (under the ACPI Namespace lock), if
602 * present, or return NULL otherwise.
603 *
604 * The ACPI device object reference acquired by this function needs to be
605 * dropped via acpi_dev_put().
606 */
607struct acpi_device *acpi_get_acpi_dev(acpi_handle handle)
608{
609	return handle_to_device(handle, get_acpi_device);
610}
611EXPORT_SYMBOL_GPL(acpi_get_acpi_dev);
612
613static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
614{
615	struct acpi_device_bus_id *acpi_device_bus_id;
616
617	/* Find suitable bus_id and instance number in acpi_bus_id_list. */
618	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
619		if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
620			return acpi_device_bus_id;
621	}
622	return NULL;
623}
624
625static int acpi_device_set_name(struct acpi_device *device,
626				struct acpi_device_bus_id *acpi_device_bus_id)
627{
628	struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
629	int result;
630
631	result = ida_alloc(instance_ida, GFP_KERNEL);
632	if (result < 0)
633		return result;
634
635	device->pnp.instance_no = result;
636	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
637	return 0;
638}
639
640int acpi_tie_acpi_dev(struct acpi_device *adev)
641{
642	acpi_handle handle = adev->handle;
643	acpi_status status;
644
645	if (!handle)
646		return 0;
647
648	status = acpi_attach_data(handle, acpi_scan_drop_device, adev);
649	if (ACPI_FAILURE(status)) {
650		acpi_handle_err(handle, "Unable to attach device data\n");
651		return -ENODEV;
652	}
653
654	return 0;
655}
656
657static void acpi_store_pld_crc(struct acpi_device *adev)
658{
659	struct acpi_pld_info *pld;
660	acpi_status status;
661
662	status = acpi_get_physical_device_location(adev->handle, &pld);
663	if (ACPI_FAILURE(status))
664		return;
665
666	adev->pld_crc = crc32(~0, pld, sizeof(*pld));
667	ACPI_FREE(pld);
668}
669
670int acpi_device_add(struct acpi_device *device)
671{
672	struct acpi_device_bus_id *acpi_device_bus_id;
673	int result;
674
675	/*
676	 * Linkage
677	 * -------
678	 * Link this device to its parent and siblings.
679	 */
680	INIT_LIST_HEAD(&device->wakeup_list);
681	INIT_LIST_HEAD(&device->physical_node_list);
682	INIT_LIST_HEAD(&device->del_list);
683	mutex_init(&device->physical_node_lock);
684
685	mutex_lock(&acpi_device_lock);
686
687	acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
688	if (acpi_device_bus_id) {
689		result = acpi_device_set_name(device, acpi_device_bus_id);
690		if (result)
691			goto err_unlock;
692	} else {
693		acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
694					     GFP_KERNEL);
695		if (!acpi_device_bus_id) {
696			result = -ENOMEM;
697			goto err_unlock;
698		}
699		acpi_device_bus_id->bus_id =
700			kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
701		if (!acpi_device_bus_id->bus_id) {
702			kfree(acpi_device_bus_id);
703			result = -ENOMEM;
704			goto err_unlock;
705		}
706
707		ida_init(&acpi_device_bus_id->instance_ida);
708
709		result = acpi_device_set_name(device, acpi_device_bus_id);
710		if (result) {
711			kfree_const(acpi_device_bus_id->bus_id);
712			kfree(acpi_device_bus_id);
713			goto err_unlock;
714		}
715
716		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
717	}
718
719	if (device->wakeup.flags.valid)
720		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
721
722	acpi_store_pld_crc(device);
723
724	mutex_unlock(&acpi_device_lock);
725
726	result = device_add(&device->dev);
727	if (result) {
728		dev_err(&device->dev, "Error registering device\n");
729		goto err;
730	}
731
732	result = acpi_device_setup_files(device);
733	if (result)
734		pr_err("Error creating sysfs interface for device %s\n",
735		       dev_name(&device->dev));
736
737	return 0;
738
739err:
740	mutex_lock(&acpi_device_lock);
741
742	list_del(&device->wakeup_list);
743
744err_unlock:
745	mutex_unlock(&acpi_device_lock);
746
747	acpi_detach_data(device->handle, acpi_scan_drop_device);
748
749	return result;
750}
751
752/* --------------------------------------------------------------------------
753                                 Device Enumeration
754   -------------------------------------------------------------------------- */
755static bool acpi_info_matches_ids(struct acpi_device_info *info,
756				  const char * const ids[])
757{
758	struct acpi_pnp_device_id_list *cid_list = NULL;
759	int i, index;
760
761	if (!(info->valid & ACPI_VALID_HID))
762		return false;
763
764	index = match_string(ids, -1, info->hardware_id.string);
765	if (index >= 0)
766		return true;
767
768	if (info->valid & ACPI_VALID_CID)
769		cid_list = &info->compatible_id_list;
770
771	if (!cid_list)
772		return false;
773
774	for (i = 0; i < cid_list->count; i++) {
775		index = match_string(ids, -1, cid_list->ids[i].string);
776		if (index >= 0)
777			return true;
778	}
779
780	return false;
781}
782
783/* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
784static const char * const acpi_ignore_dep_ids[] = {
785	"PNP0D80", /* Windows-compatible System Power Management Controller */
786	"INT33BD", /* Intel Baytrail Mailbox Device */
787	"LATT2021", /* Lattice FW Update Client Driver */
788	NULL
789};
790
791/* List of HIDs for which we honor deps of matching ACPI devs, when checking _DEP lists. */
792static const char * const acpi_honor_dep_ids[] = {
793	"INT3472", /* Camera sensor PMIC / clk and regulator info */
794	"INTC1059", /* IVSC (TGL) driver must be loaded to allow i2c access to camera sensors */
795	"INTC1095", /* IVSC (ADL) driver must be loaded to allow i2c access to camera sensors */
796	"INTC100A", /* IVSC (RPL) driver must be loaded to allow i2c access to camera sensors */
797	NULL
798};
799
800static struct acpi_device *acpi_find_parent_acpi_dev(acpi_handle handle)
801{
802	struct acpi_device *adev;
803
804	/*
805	 * Fixed hardware devices do not appear in the namespace and do not
806	 * have handles, but we fabricate acpi_devices for them, so we have
807	 * to deal with them specially.
808	 */
809	if (!handle)
810		return acpi_root;
811
812	do {
813		acpi_status status;
814
815		status = acpi_get_parent(handle, &handle);
816		if (ACPI_FAILURE(status)) {
817			if (status != AE_NULL_ENTRY)
818				return acpi_root;
819
820			return NULL;
821		}
822		adev = acpi_fetch_acpi_dev(handle);
823	} while (!adev);
824	return adev;
825}
826
827acpi_status
828acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
829{
830	acpi_status status;
831	acpi_handle tmp;
832	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
833	union acpi_object *obj;
834
835	status = acpi_get_handle(handle, "_EJD", &tmp);
836	if (ACPI_FAILURE(status))
837		return status;
838
839	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
840	if (ACPI_SUCCESS(status)) {
841		obj = buffer.pointer;
842		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
843					 ejd);
844		kfree(buffer.pointer);
845	}
846	return status;
847}
848EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
849
850static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
851{
852	acpi_handle handle = dev->handle;
853	struct acpi_device_wakeup *wakeup = &dev->wakeup;
854	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
855	union acpi_object *package = NULL;
856	union acpi_object *element = NULL;
857	acpi_status status;
858	int err = -ENODATA;
859
860	INIT_LIST_HEAD(&wakeup->resources);
861
862	/* _PRW */
863	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
864	if (ACPI_FAILURE(status)) {
865		acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
866				 acpi_format_exception(status));
867		return err;
868	}
869
870	package = (union acpi_object *)buffer.pointer;
871
872	if (!package || package->package.count < 2)
873		goto out;
874
875	element = &(package->package.elements[0]);
876	if (!element)
877		goto out;
878
879	if (element->type == ACPI_TYPE_PACKAGE) {
880		if ((element->package.count < 2) ||
881		    (element->package.elements[0].type !=
882		     ACPI_TYPE_LOCAL_REFERENCE)
883		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
884			goto out;
885
886		wakeup->gpe_device =
887		    element->package.elements[0].reference.handle;
888		wakeup->gpe_number =
889		    (u32) element->package.elements[1].integer.value;
890	} else if (element->type == ACPI_TYPE_INTEGER) {
891		wakeup->gpe_device = NULL;
892		wakeup->gpe_number = element->integer.value;
893	} else {
894		goto out;
895	}
896
897	element = &(package->package.elements[1]);
898	if (element->type != ACPI_TYPE_INTEGER)
899		goto out;
900
901	wakeup->sleep_state = element->integer.value;
902
903	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
904	if (err)
905		goto out;
906
907	if (!list_empty(&wakeup->resources)) {
908		int sleep_state;
909
910		err = acpi_power_wakeup_list_init(&wakeup->resources,
911						  &sleep_state);
912		if (err) {
913			acpi_handle_warn(handle, "Retrieving current states "
914					 "of wakeup power resources failed\n");
915			acpi_power_resources_list_free(&wakeup->resources);
916			goto out;
917		}
918		if (sleep_state < wakeup->sleep_state) {
919			acpi_handle_warn(handle, "Overriding _PRW sleep state "
920					 "(S%d) by S%d from power resources\n",
921					 (int)wakeup->sleep_state, sleep_state);
922			wakeup->sleep_state = sleep_state;
923		}
924	}
925
926 out:
927	kfree(buffer.pointer);
928	return err;
929}
930
931/* Do not use a button for S5 wakeup */
932#define ACPI_AVOID_WAKE_FROM_S5		BIT(0)
933
934static bool acpi_wakeup_gpe_init(struct acpi_device *device)
935{
936	static const struct acpi_device_id button_device_ids[] = {
937		{"PNP0C0C", 0},				/* Power button */
938		{"PNP0C0D", ACPI_AVOID_WAKE_FROM_S5},	/* Lid */
939		{"PNP0C0E", ACPI_AVOID_WAKE_FROM_S5},	/* Sleep button */
940		{"", 0},
941	};
942	struct acpi_device_wakeup *wakeup = &device->wakeup;
943	const struct acpi_device_id *match;
944	acpi_status status;
945
946	wakeup->flags.notifier_present = 0;
947
948	/* Power button, Lid switch always enable wakeup */
949	match = acpi_match_acpi_device(button_device_ids, device);
950	if (match) {
951		if ((match->driver_data & ACPI_AVOID_WAKE_FROM_S5) &&
952		    wakeup->sleep_state == ACPI_STATE_S5)
953			wakeup->sleep_state = ACPI_STATE_S4;
954		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
955		device_set_wakeup_capable(&device->dev, true);
956		return true;
957	}
958
959	status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
960					 wakeup->gpe_number);
961	return ACPI_SUCCESS(status);
962}
963
964static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
965{
966	int err;
967
968	/* Presence of _PRW indicates wake capable */
969	if (!acpi_has_method(device->handle, "_PRW"))
970		return;
971
972	err = acpi_bus_extract_wakeup_device_power_package(device);
973	if (err) {
974		dev_err(&device->dev, "Unable to extract wakeup power resources");
975		return;
976	}
977
978	device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
979	device->wakeup.prepare_count = 0;
980	/*
981	 * Call _PSW/_DSW object to disable its ability to wake the sleeping
982	 * system for the ACPI device with the _PRW object.
983	 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
984	 * So it is necessary to call _DSW object first. Only when it is not
985	 * present will the _PSW object used.
986	 */
987	err = acpi_device_sleep_wake(device, 0, 0, 0);
988	if (err)
989		pr_debug("error in _DSW or _PSW evaluation\n");
990}
991
992static void acpi_bus_init_power_state(struct acpi_device *device, int state)
993{
994	struct acpi_device_power_state *ps = &device->power.states[state];
995	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
996	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
997	acpi_status status;
998
999	INIT_LIST_HEAD(&ps->resources);
1000
1001	/* Evaluate "_PRx" to get referenced power resources */
1002	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1003	if (ACPI_SUCCESS(status)) {
1004		union acpi_object *package = buffer.pointer;
1005
1006		if (buffer.length && package
1007		    && package->type == ACPI_TYPE_PACKAGE
1008		    && package->package.count)
1009			acpi_extract_power_resources(package, 0, &ps->resources);
1010
1011		ACPI_FREE(buffer.pointer);
1012	}
1013
1014	/* Evaluate "_PSx" to see if we can do explicit sets */
1015	pathname[2] = 'S';
1016	if (acpi_has_method(device->handle, pathname))
1017		ps->flags.explicit_set = 1;
1018
1019	/* State is valid if there are means to put the device into it. */
1020	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
1021		ps->flags.valid = 1;
1022
1023	ps->power = -1;		/* Unknown - driver assigned */
1024	ps->latency = -1;	/* Unknown - driver assigned */
1025}
1026
1027static void acpi_bus_get_power_flags(struct acpi_device *device)
1028{
1029	unsigned long long dsc = ACPI_STATE_D0;
1030	u32 i;
1031
1032	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1033	if (!acpi_has_method(device->handle, "_PS0") &&
1034	    !acpi_has_method(device->handle, "_PR0"))
1035		return;
1036
1037	device->flags.power_manageable = 1;
1038
1039	/*
1040	 * Power Management Flags
1041	 */
1042	if (acpi_has_method(device->handle, "_PSC"))
1043		device->power.flags.explicit_get = 1;
1044
1045	if (acpi_has_method(device->handle, "_IRC"))
1046		device->power.flags.inrush_current = 1;
1047
1048	if (acpi_has_method(device->handle, "_DSW"))
1049		device->power.flags.dsw_present = 1;
1050
1051	acpi_evaluate_integer(device->handle, "_DSC", NULL, &dsc);
1052	device->power.state_for_enumeration = dsc;
1053
1054	/*
1055	 * Enumerate supported power management states
1056	 */
1057	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1058		acpi_bus_init_power_state(device, i);
1059
1060	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1061
1062	/* Set the defaults for D0 and D3hot (always supported). */
1063	device->power.states[ACPI_STATE_D0].flags.valid = 1;
1064	device->power.states[ACPI_STATE_D0].power = 100;
1065	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1066
1067	/*
1068	 * Use power resources only if the D0 list of them is populated, because
1069	 * some platforms may provide _PR3 only to indicate D3cold support and
1070	 * in those cases the power resources list returned by it may be bogus.
1071	 */
1072	if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1073		device->power.flags.power_resources = 1;
1074		/*
1075		 * D3cold is supported if the D3hot list of power resources is
1076		 * not empty.
1077		 */
1078		if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1079			device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1080	}
1081
1082	if (acpi_bus_init_power(device))
1083		device->flags.power_manageable = 0;
1084}
1085
1086static void acpi_bus_get_flags(struct acpi_device *device)
1087{
1088	/* Presence of _STA indicates 'dynamic_status' */
1089	if (acpi_has_method(device->handle, "_STA"))
1090		device->flags.dynamic_status = 1;
1091
1092	/* Presence of _RMV indicates 'removable' */
1093	if (acpi_has_method(device->handle, "_RMV"))
1094		device->flags.removable = 1;
1095
1096	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1097	if (acpi_has_method(device->handle, "_EJD") ||
1098	    acpi_has_method(device->handle, "_EJ0"))
1099		device->flags.ejectable = 1;
1100}
1101
1102static void acpi_device_get_busid(struct acpi_device *device)
1103{
1104	char bus_id[5] = { '?', 0 };
1105	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1106	int i = 0;
1107
1108	/*
1109	 * Bus ID
1110	 * ------
1111	 * The device's Bus ID is simply the object name.
1112	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1113	 */
1114	if (!acpi_dev_parent(device)) {
1115		strcpy(device->pnp.bus_id, "ACPI");
1116		return;
1117	}
1118
1119	switch (device->device_type) {
1120	case ACPI_BUS_TYPE_POWER_BUTTON:
1121		strcpy(device->pnp.bus_id, "PWRF");
1122		break;
1123	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1124		strcpy(device->pnp.bus_id, "SLPF");
1125		break;
1126	case ACPI_BUS_TYPE_ECDT_EC:
1127		strcpy(device->pnp.bus_id, "ECDT");
1128		break;
1129	default:
1130		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1131		/* Clean up trailing underscores (if any) */
1132		for (i = 3; i > 1; i--) {
1133			if (bus_id[i] == '_')
1134				bus_id[i] = '\0';
1135			else
1136				break;
1137		}
1138		strcpy(device->pnp.bus_id, bus_id);
1139		break;
1140	}
1141}
1142
1143/*
1144 * acpi_ata_match - see if an acpi object is an ATA device
1145 *
1146 * If an acpi object has one of the ACPI ATA methods defined,
1147 * then we can safely call it an ATA device.
1148 */
1149bool acpi_ata_match(acpi_handle handle)
1150{
1151	return acpi_has_method(handle, "_GTF") ||
1152	       acpi_has_method(handle, "_GTM") ||
1153	       acpi_has_method(handle, "_STM") ||
1154	       acpi_has_method(handle, "_SDD");
1155}
1156
1157/*
1158 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1159 *
1160 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1161 * then we can safely call it an ejectable drive bay
1162 */
1163bool acpi_bay_match(acpi_handle handle)
1164{
1165	acpi_handle phandle;
1166
1167	if (!acpi_has_method(handle, "_EJ0"))
1168		return false;
1169	if (acpi_ata_match(handle))
1170		return true;
1171	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1172		return false;
1173
1174	return acpi_ata_match(phandle);
1175}
1176
1177bool acpi_device_is_battery(struct acpi_device *adev)
1178{
1179	struct acpi_hardware_id *hwid;
1180
1181	list_for_each_entry(hwid, &adev->pnp.ids, list)
1182		if (!strcmp("PNP0C0A", hwid->id))
1183			return true;
1184
1185	return false;
1186}
1187
1188static bool is_ejectable_bay(struct acpi_device *adev)
1189{
1190	acpi_handle handle = adev->handle;
1191
1192	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1193		return true;
1194
1195	return acpi_bay_match(handle);
1196}
1197
1198/*
1199 * acpi_dock_match - see if an acpi object has a _DCK method
1200 */
1201bool acpi_dock_match(acpi_handle handle)
1202{
1203	return acpi_has_method(handle, "_DCK");
1204}
1205
1206static acpi_status
1207acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1208			  void **return_value)
1209{
1210	long *cap = context;
1211
1212	if (acpi_has_method(handle, "_BCM") &&
1213	    acpi_has_method(handle, "_BCL")) {
1214		acpi_handle_debug(handle, "Found generic backlight support\n");
1215		*cap |= ACPI_VIDEO_BACKLIGHT;
1216		/* We have backlight support, no need to scan further */
1217		return AE_CTRL_TERMINATE;
1218	}
1219	return 0;
1220}
1221
1222/* Returns true if the ACPI object is a video device which can be
1223 * handled by video.ko.
1224 * The device will get a Linux specific CID added in scan.c to
1225 * identify the device as an ACPI graphics device
1226 * Be aware that the graphics device may not be physically present
1227 * Use acpi_video_get_capabilities() to detect general ACPI video
1228 * capabilities of present cards
1229 */
1230long acpi_is_video_device(acpi_handle handle)
1231{
1232	long video_caps = 0;
1233
1234	/* Is this device able to support video switching ? */
1235	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1236		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1237
1238	/* Is this device able to retrieve a video ROM ? */
1239	if (acpi_has_method(handle, "_ROM"))
1240		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1241
1242	/* Is this device able to configure which video head to be POSTed ? */
1243	if (acpi_has_method(handle, "_VPO") &&
1244	    acpi_has_method(handle, "_GPD") &&
1245	    acpi_has_method(handle, "_SPD"))
1246		video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1247
1248	/* Only check for backlight functionality if one of the above hit. */
1249	if (video_caps)
1250		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1251				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1252				    &video_caps, NULL);
1253
1254	return video_caps;
1255}
1256EXPORT_SYMBOL(acpi_is_video_device);
1257
1258const char *acpi_device_hid(struct acpi_device *device)
1259{
1260	struct acpi_hardware_id *hid;
1261
1262	if (list_empty(&device->pnp.ids))
1263		return dummy_hid;
1264
1265	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1266	return hid->id;
1267}
1268EXPORT_SYMBOL(acpi_device_hid);
1269
1270static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1271{
1272	struct acpi_hardware_id *id;
1273
1274	id = kmalloc(sizeof(*id), GFP_KERNEL);
1275	if (!id)
1276		return;
1277
1278	id->id = kstrdup_const(dev_id, GFP_KERNEL);
1279	if (!id->id) {
1280		kfree(id);
1281		return;
1282	}
1283
1284	list_add_tail(&id->list, &pnp->ids);
1285	pnp->type.hardware_id = 1;
1286}
1287
1288/*
1289 * Old IBM workstations have a DSDT bug wherein the SMBus object
1290 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1291 * prefix.  Work around this.
1292 */
1293static bool acpi_ibm_smbus_match(acpi_handle handle)
1294{
1295	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1296	struct acpi_buffer path = { sizeof(node_name), node_name };
1297
1298	if (!dmi_name_in_vendors("IBM"))
1299		return false;
1300
1301	/* Look for SMBS object */
1302	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1303	    strcmp("SMBS", path.pointer))
1304		return false;
1305
1306	/* Does it have the necessary (but misnamed) methods? */
1307	if (acpi_has_method(handle, "SBI") &&
1308	    acpi_has_method(handle, "SBR") &&
1309	    acpi_has_method(handle, "SBW"))
1310		return true;
1311
1312	return false;
1313}
1314
1315static bool acpi_object_is_system_bus(acpi_handle handle)
1316{
1317	acpi_handle tmp;
1318
1319	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1320	    tmp == handle)
1321		return true;
1322	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1323	    tmp == handle)
1324		return true;
1325
1326	return false;
1327}
1328
1329static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1330			     int device_type)
1331{
1332	struct acpi_device_info *info = NULL;
1333	struct acpi_pnp_device_id_list *cid_list;
1334	int i;
1335
1336	switch (device_type) {
1337	case ACPI_BUS_TYPE_DEVICE:
1338		if (handle == ACPI_ROOT_OBJECT) {
1339			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1340			break;
1341		}
1342
1343		acpi_get_object_info(handle, &info);
1344		if (!info) {
1345			pr_err("%s: Error reading device info\n", __func__);
1346			return;
1347		}
1348
1349		if (info->valid & ACPI_VALID_HID) {
1350			acpi_add_id(pnp, info->hardware_id.string);
1351			pnp->type.platform_id = 1;
1352		}
1353		if (info->valid & ACPI_VALID_CID) {
1354			cid_list = &info->compatible_id_list;
1355			for (i = 0; i < cid_list->count; i++)
1356				acpi_add_id(pnp, cid_list->ids[i].string);
1357		}
1358		if (info->valid & ACPI_VALID_ADR) {
1359			pnp->bus_address = info->address;
1360			pnp->type.bus_address = 1;
1361		}
1362		if (info->valid & ACPI_VALID_UID)
1363			pnp->unique_id = kstrdup(info->unique_id.string,
1364							GFP_KERNEL);
1365		if (info->valid & ACPI_VALID_CLS)
1366			acpi_add_id(pnp, info->class_code.string);
1367
1368		kfree(info);
1369
1370		/*
1371		 * Some devices don't reliably have _HIDs & _CIDs, so add
1372		 * synthetic HIDs to make sure drivers can find them.
1373		 */
1374		if (acpi_is_video_device(handle)) {
1375			acpi_add_id(pnp, ACPI_VIDEO_HID);
1376			pnp->type.backlight = 1;
1377			break;
1378		}
1379		if (acpi_bay_match(handle))
1380			acpi_add_id(pnp, ACPI_BAY_HID);
1381		else if (acpi_dock_match(handle))
1382			acpi_add_id(pnp, ACPI_DOCK_HID);
1383		else if (acpi_ibm_smbus_match(handle))
1384			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1385		else if (list_empty(&pnp->ids) &&
1386			 acpi_object_is_system_bus(handle)) {
1387			/* \_SB, \_TZ, LNXSYBUS */
1388			acpi_add_id(pnp, ACPI_BUS_HID);
1389			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1390			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1391		}
1392
1393		break;
1394	case ACPI_BUS_TYPE_POWER:
1395		acpi_add_id(pnp, ACPI_POWER_HID);
1396		break;
1397	case ACPI_BUS_TYPE_PROCESSOR:
1398		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1399		break;
1400	case ACPI_BUS_TYPE_THERMAL:
1401		acpi_add_id(pnp, ACPI_THERMAL_HID);
1402		break;
1403	case ACPI_BUS_TYPE_POWER_BUTTON:
1404		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1405		break;
1406	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1407		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1408		break;
1409	case ACPI_BUS_TYPE_ECDT_EC:
1410		acpi_add_id(pnp, ACPI_ECDT_HID);
1411		break;
1412	}
1413}
1414
1415void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1416{
1417	struct acpi_hardware_id *id, *tmp;
1418
1419	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1420		kfree_const(id->id);
1421		kfree(id);
1422	}
1423	kfree(pnp->unique_id);
1424}
1425
1426/**
1427 * acpi_dma_supported - Check DMA support for the specified device.
1428 * @adev: The pointer to acpi device
1429 *
1430 * Return false if DMA is not supported. Otherwise, return true
1431 */
1432bool acpi_dma_supported(const struct acpi_device *adev)
1433{
1434	if (!adev)
1435		return false;
1436
1437	if (adev->flags.cca_seen)
1438		return true;
1439
1440	/*
1441	* Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1442	* DMA on "Intel platforms".  Presumably that includes all x86 and
1443	* ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1444	*/
1445	if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1446		return true;
1447
1448	return false;
1449}
1450
1451/**
1452 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1453 * @adev: The pointer to acpi device
1454 *
1455 * Return enum dev_dma_attr.
1456 */
1457enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1458{
1459	if (!acpi_dma_supported(adev))
1460		return DEV_DMA_NOT_SUPPORTED;
1461
1462	if (adev->flags.coherent_dma)
1463		return DEV_DMA_COHERENT;
1464	else
1465		return DEV_DMA_NON_COHERENT;
1466}
1467
1468/**
1469 * acpi_dma_get_range() - Get device DMA parameters.
1470 *
1471 * @dev: device to configure
1472 * @map: pointer to DMA ranges result
1473 *
1474 * Evaluate DMA regions and return pointer to DMA regions on
1475 * parsing success; it does not update the passed in values on failure.
1476 *
1477 * Return 0 on success, < 0 on failure.
1478 */
1479int acpi_dma_get_range(struct device *dev, const struct bus_dma_region **map)
1480{
1481	struct acpi_device *adev;
1482	LIST_HEAD(list);
1483	struct resource_entry *rentry;
1484	int ret;
1485	struct device *dma_dev = dev;
1486	struct bus_dma_region *r;
1487
1488	/*
1489	 * Walk the device tree chasing an ACPI companion with a _DMA
1490	 * object while we go. Stop if we find a device with an ACPI
1491	 * companion containing a _DMA method.
1492	 */
1493	do {
1494		adev = ACPI_COMPANION(dma_dev);
1495		if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1496			break;
1497
1498		dma_dev = dma_dev->parent;
1499	} while (dma_dev);
1500
1501	if (!dma_dev)
1502		return -ENODEV;
1503
1504	if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1505		acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1506		return -EINVAL;
1507	}
1508
1509	ret = acpi_dev_get_dma_resources(adev, &list);
1510	if (ret > 0) {
1511		r = kcalloc(ret + 1, sizeof(*r), GFP_KERNEL);
1512		if (!r) {
1513			ret = -ENOMEM;
1514			goto out;
1515		}
1516
1517		*map = r;
1518
1519		list_for_each_entry(rentry, &list, node) {
1520			if (rentry->res->start >= rentry->res->end) {
1521				kfree(*map);
1522				*map = NULL;
1523				ret = -EINVAL;
1524				dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1525				goto out;
1526			}
1527
1528			r->cpu_start = rentry->res->start;
1529			r->dma_start = rentry->res->start - rentry->offset;
1530			r->size = resource_size(rentry->res);
1531			r->offset = rentry->offset;
1532			r++;
1533		}
1534	}
1535 out:
1536	acpi_dev_free_resource_list(&list);
1537
1538	return ret >= 0 ? 0 : ret;
1539}
1540
1541#ifdef CONFIG_IOMMU_API
1542int acpi_iommu_fwspec_init(struct device *dev, u32 id,
1543			   struct fwnode_handle *fwnode,
1544			   const struct iommu_ops *ops)
1545{
1546	int ret = iommu_fwspec_init(dev, fwnode, ops);
1547
1548	if (!ret)
1549		ret = iommu_fwspec_add_ids(dev, &id, 1);
1550
1551	return ret;
1552}
1553
1554static inline const struct iommu_ops *acpi_iommu_fwspec_ops(struct device *dev)
1555{
1556	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1557
1558	return fwspec ? fwspec->ops : NULL;
1559}
1560
1561static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
1562						       const u32 *id_in)
1563{
1564	int err;
1565	const struct iommu_ops *ops;
1566
1567	/* Serialise to make dev->iommu stable under our potential fwspec */
1568	mutex_lock(&iommu_probe_device_lock);
1569	/*
1570	 * If we already translated the fwspec there is nothing left to do,
1571	 * return the iommu_ops.
1572	 */
1573	ops = acpi_iommu_fwspec_ops(dev);
1574	if (ops) {
1575		mutex_unlock(&iommu_probe_device_lock);
1576		return ops;
1577	}
1578
1579	err = iort_iommu_configure_id(dev, id_in);
1580	if (err && err != -EPROBE_DEFER)
1581		err = viot_iommu_configure(dev);
1582	mutex_unlock(&iommu_probe_device_lock);
1583
1584	/*
1585	 * If we have reason to believe the IOMMU driver missed the initial
1586	 * iommu_probe_device() call for dev, replay it to get things in order.
1587	 */
1588	if (!err && dev->bus)
1589		err = iommu_probe_device(dev);
1590
1591	/* Ignore all other errors apart from EPROBE_DEFER */
1592	if (err == -EPROBE_DEFER) {
1593		return ERR_PTR(err);
1594	} else if (err) {
1595		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1596		return NULL;
1597	}
1598	return acpi_iommu_fwspec_ops(dev);
1599}
1600
1601#else /* !CONFIG_IOMMU_API */
1602
1603int acpi_iommu_fwspec_init(struct device *dev, u32 id,
1604			   struct fwnode_handle *fwnode,
1605			   const struct iommu_ops *ops)
1606{
1607	return -ENODEV;
1608}
1609
1610static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
1611						       const u32 *id_in)
1612{
1613	return NULL;
1614}
1615
1616#endif /* !CONFIG_IOMMU_API */
1617
1618/**
1619 * acpi_dma_configure_id - Set-up DMA configuration for the device.
1620 * @dev: The pointer to the device
1621 * @attr: device dma attributes
1622 * @input_id: input device id const value pointer
1623 */
1624int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1625			  const u32 *input_id)
1626{
1627	const struct iommu_ops *iommu;
1628
1629	if (attr == DEV_DMA_NOT_SUPPORTED) {
1630		set_dma_ops(dev, &dma_dummy_ops);
1631		return 0;
1632	}
1633
1634	acpi_arch_dma_setup(dev);
1635
1636	iommu = acpi_iommu_configure_id(dev, input_id);
1637	if (PTR_ERR(iommu) == -EPROBE_DEFER)
1638		return -EPROBE_DEFER;
1639
1640	arch_setup_dma_ops(dev, 0, U64_MAX,
1641				iommu, attr == DEV_DMA_COHERENT);
1642
1643	return 0;
1644}
1645EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1646
1647static void acpi_init_coherency(struct acpi_device *adev)
1648{
1649	unsigned long long cca = 0;
1650	acpi_status status;
1651	struct acpi_device *parent = acpi_dev_parent(adev);
1652
1653	if (parent && parent->flags.cca_seen) {
1654		/*
1655		 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1656		 * already saw one.
1657		 */
1658		adev->flags.cca_seen = 1;
1659		cca = parent->flags.coherent_dma;
1660	} else {
1661		status = acpi_evaluate_integer(adev->handle, "_CCA",
1662					       NULL, &cca);
1663		if (ACPI_SUCCESS(status))
1664			adev->flags.cca_seen = 1;
1665		else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1666			/*
1667			 * If architecture does not specify that _CCA is
1668			 * required for DMA-able devices (e.g. x86),
1669			 * we default to _CCA=1.
1670			 */
1671			cca = 1;
1672		else
1673			acpi_handle_debug(adev->handle,
1674					  "ACPI device is missing _CCA.\n");
1675	}
1676
1677	adev->flags.coherent_dma = cca;
1678}
1679
1680static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1681{
1682	bool *is_serial_bus_slave_p = data;
1683
1684	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1685		return 1;
1686
1687	*is_serial_bus_slave_p = true;
1688
1689	 /* no need to do more checking */
1690	return -1;
1691}
1692
1693static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1694{
1695	struct acpi_device *parent = acpi_dev_parent(device);
1696	static const struct acpi_device_id indirect_io_hosts[] = {
1697		{"HISI0191", 0},
1698		{}
1699	};
1700
1701	return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1702}
1703
1704static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1705{
1706	struct list_head resource_list;
1707	bool is_serial_bus_slave = false;
1708	static const struct acpi_device_id ignore_serial_bus_ids[] = {
1709	/*
1710	 * These devices have multiple SerialBus resources and a client
1711	 * device must be instantiated for each of them, each with
1712	 * its own device id.
1713	 * Normally we only instantiate one client device for the first
1714	 * resource, using the ACPI HID as id. These special cases are handled
1715	 * by the drivers/platform/x86/serial-multi-instantiate.c driver, which
1716	 * knows which client device id to use for each resource.
1717	 */
1718		{"BSG1160", },
1719		{"BSG2150", },
1720		{"CSC3551", },
1721		{"CSC3556", },
1722		{"INT33FE", },
1723		{"INT3515", },
1724		/* Non-conforming _HID for Cirrus Logic already released */
1725		{"CLSA0100", },
1726		{"CLSA0101", },
1727	/*
1728	 * Some ACPI devs contain SerialBus resources even though they are not
1729	 * attached to a serial bus at all.
1730	 */
1731		{"MSHW0028", },
1732	/*
1733	 * HIDs of device with an UartSerialBusV2 resource for which userspace
1734	 * expects a regular tty cdev to be created (instead of the in kernel
1735	 * serdev) and which have a kernel driver which expects a platform_dev
1736	 * such as the rfkill-gpio driver.
1737	 */
1738		{"BCM4752", },
1739		{"LNV4752", },
1740		{}
1741	};
1742
1743	if (acpi_is_indirect_io_slave(device))
1744		return true;
1745
1746	/* Macs use device properties in lieu of _CRS resources */
1747	if (x86_apple_machine &&
1748	    (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1749	     fwnode_property_present(&device->fwnode, "i2cAddress") ||
1750	     fwnode_property_present(&device->fwnode, "baud")))
1751		return true;
1752
1753	if (!acpi_match_device_ids(device, ignore_serial_bus_ids))
1754		return false;
1755
1756	INIT_LIST_HEAD(&resource_list);
1757	acpi_dev_get_resources(device, &resource_list,
1758			       acpi_check_serial_bus_slave,
1759			       &is_serial_bus_slave);
1760	acpi_dev_free_resource_list(&resource_list);
1761
1762	return is_serial_bus_slave;
1763}
1764
1765void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1766			     int type, void (*release)(struct device *))
1767{
1768	struct acpi_device *parent = acpi_find_parent_acpi_dev(handle);
1769
1770	INIT_LIST_HEAD(&device->pnp.ids);
1771	device->device_type = type;
1772	device->handle = handle;
1773	device->dev.parent = parent ? &parent->dev : NULL;
1774	device->dev.release = release;
1775	device->dev.bus = &acpi_bus_type;
1776	fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1777	acpi_set_device_status(device, ACPI_STA_DEFAULT);
1778	acpi_device_get_busid(device);
1779	acpi_set_pnp_ids(handle, &device->pnp, type);
1780	acpi_init_properties(device);
1781	acpi_bus_get_flags(device);
1782	device->flags.match_driver = false;
1783	device->flags.initialized = true;
1784	device->flags.enumeration_by_parent =
1785		acpi_device_enumeration_by_parent(device);
1786	acpi_device_clear_enumerated(device);
1787	device_initialize(&device->dev);
1788	dev_set_uevent_suppress(&device->dev, true);
1789	acpi_init_coherency(device);
1790}
1791
1792static void acpi_scan_dep_init(struct acpi_device *adev)
1793{
1794	struct acpi_dep_data *dep;
1795
1796	list_for_each_entry(dep, &acpi_dep_list, node) {
1797		if (dep->consumer == adev->handle) {
1798			if (dep->honor_dep)
1799				adev->flags.honor_deps = 1;
1800
1801			adev->dep_unmet++;
1802		}
1803	}
1804}
1805
1806void acpi_device_add_finalize(struct acpi_device *device)
1807{
1808	dev_set_uevent_suppress(&device->dev, false);
1809	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1810}
1811
1812static void acpi_scan_init_status(struct acpi_device *adev)
1813{
1814	if (acpi_bus_get_status(adev))
1815		acpi_set_device_status(adev, 0);
1816}
1817
1818static int acpi_add_single_object(struct acpi_device **child,
1819				  acpi_handle handle, int type, bool dep_init)
1820{
1821	struct acpi_device *device;
1822	bool release_dep_lock = false;
1823	int result;
1824
1825	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1826	if (!device)
1827		return -ENOMEM;
1828
1829	acpi_init_device_object(device, handle, type, acpi_device_release);
1830	/*
1831	 * Getting the status is delayed till here so that we can call
1832	 * acpi_bus_get_status() and use its quirk handling.  Note that
1833	 * this must be done before the get power-/wakeup_dev-flags calls.
1834	 */
1835	if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
1836		if (dep_init) {
1837			mutex_lock(&acpi_dep_list_lock);
1838			/*
1839			 * Hold the lock until the acpi_tie_acpi_dev() call
1840			 * below to prevent concurrent acpi_scan_clear_dep()
1841			 * from deleting a dependency list entry without
1842			 * updating dep_unmet for the device.
1843			 */
1844			release_dep_lock = true;
1845			acpi_scan_dep_init(device);
1846		}
1847		acpi_scan_init_status(device);
1848	}
1849
1850	acpi_bus_get_power_flags(device);
1851	acpi_bus_get_wakeup_device_flags(device);
1852
1853	result = acpi_tie_acpi_dev(device);
1854
1855	if (release_dep_lock)
1856		mutex_unlock(&acpi_dep_list_lock);
1857
1858	if (!result)
1859		result = acpi_device_add(device);
1860
1861	if (result) {
1862		acpi_device_release(&device->dev);
1863		return result;
1864	}
1865
1866	acpi_power_add_remove_device(device, true);
1867	acpi_device_add_finalize(device);
1868
1869	acpi_handle_debug(handle, "Added as %s, parent %s\n",
1870			  dev_name(&device->dev), device->dev.parent ?
1871				dev_name(device->dev.parent) : "(null)");
1872
1873	*child = device;
1874	return 0;
1875}
1876
1877static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1878					    void *context)
1879{
1880	struct resource *res = context;
1881
1882	if (acpi_dev_resource_memory(ares, res))
1883		return AE_CTRL_TERMINATE;
1884
1885	return AE_OK;
1886}
1887
1888static bool acpi_device_should_be_hidden(acpi_handle handle)
1889{
1890	acpi_status status;
1891	struct resource res;
1892
1893	/* Check if it should ignore the UART device */
1894	if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1895		return false;
1896
1897	/*
1898	 * The UART device described in SPCR table is assumed to have only one
1899	 * memory resource present. So we only look for the first one here.
1900	 */
1901	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1902				     acpi_get_resource_memory, &res);
1903	if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1904		return false;
1905
1906	acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1907			 &res.start);
1908
1909	return true;
1910}
1911
1912bool acpi_device_is_present(const struct acpi_device *adev)
1913{
1914	return adev->status.present || adev->status.functional;
1915}
1916
1917static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1918				       const char *idstr,
1919				       const struct acpi_device_id **matchid)
1920{
1921	const struct acpi_device_id *devid;
1922
1923	if (handler->match)
1924		return handler->match(idstr, matchid);
1925
1926	for (devid = handler->ids; devid->id[0]; devid++)
1927		if (!strcmp((char *)devid->id, idstr)) {
1928			if (matchid)
1929				*matchid = devid;
1930
1931			return true;
1932		}
1933
1934	return false;
1935}
1936
1937static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1938					const struct acpi_device_id **matchid)
1939{
1940	struct acpi_scan_handler *handler;
1941
1942	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1943		if (acpi_scan_handler_matching(handler, idstr, matchid))
1944			return handler;
1945
1946	return NULL;
1947}
1948
1949void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1950{
1951	if (!!hotplug->enabled == !!val)
1952		return;
1953
1954	mutex_lock(&acpi_scan_lock);
1955
1956	hotplug->enabled = val;
1957
1958	mutex_unlock(&acpi_scan_lock);
1959}
1960
1961static void acpi_scan_init_hotplug(struct acpi_device *adev)
1962{
1963	struct acpi_hardware_id *hwid;
1964
1965	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1966		acpi_dock_add(adev);
1967		return;
1968	}
1969	list_for_each_entry(hwid, &adev->pnp.ids, list) {
1970		struct acpi_scan_handler *handler;
1971
1972		handler = acpi_scan_match_handler(hwid->id, NULL);
1973		if (handler) {
1974			adev->flags.hotplug_notify = true;
1975			break;
1976		}
1977	}
1978}
1979
1980static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
1981{
1982	struct acpi_handle_list dep_devices;
1983	acpi_status status;
1984	u32 count;
1985	int i;
1986
1987	/*
1988	 * Check for _HID here to avoid deferring the enumeration of:
1989	 * 1. PCI devices.
1990	 * 2. ACPI nodes describing USB ports.
1991	 * Still, checking for _HID catches more then just these cases ...
1992	 */
1993	if (!check_dep || !acpi_has_method(handle, "_DEP") ||
1994	    !acpi_has_method(handle, "_HID"))
1995		return 0;
1996
1997	status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
1998	if (ACPI_FAILURE(status)) {
1999		acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
2000		return 0;
2001	}
2002
2003	for (count = 0, i = 0; i < dep_devices.count; i++) {
2004		struct acpi_device_info *info;
2005		struct acpi_dep_data *dep;
2006		bool skip, honor_dep;
2007
2008		status = acpi_get_object_info(dep_devices.handles[i], &info);
2009		if (ACPI_FAILURE(status)) {
2010			acpi_handle_debug(handle, "Error reading _DEP device info\n");
2011			continue;
2012		}
2013
2014		skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
2015		honor_dep = acpi_info_matches_ids(info, acpi_honor_dep_ids);
2016		kfree(info);
2017
2018		if (skip)
2019			continue;
2020
2021		dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2022		if (!dep)
2023			continue;
2024
2025		count++;
2026
2027		dep->supplier = dep_devices.handles[i];
2028		dep->consumer = handle;
2029		dep->honor_dep = honor_dep;
2030
2031		mutex_lock(&acpi_dep_list_lock);
2032		list_add_tail(&dep->node , &acpi_dep_list);
2033		mutex_unlock(&acpi_dep_list_lock);
2034	}
2035
2036	return count;
2037}
2038
2039static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
2040				      struct acpi_device **adev_p)
2041{
2042	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
2043	acpi_object_type acpi_type;
2044	int type;
2045
2046	if (device)
2047		goto out;
2048
2049	if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
2050		return AE_OK;
2051
2052	switch (acpi_type) {
2053	case ACPI_TYPE_DEVICE:
2054		if (acpi_device_should_be_hidden(handle))
2055			return AE_OK;
2056
2057		/* Bail out if there are dependencies. */
2058		if (acpi_scan_check_dep(handle, check_dep) > 0)
2059			return AE_CTRL_DEPTH;
2060
2061		fallthrough;
2062	case ACPI_TYPE_ANY:	/* for ACPI_ROOT_OBJECT */
2063		type = ACPI_BUS_TYPE_DEVICE;
2064		break;
2065
2066	case ACPI_TYPE_PROCESSOR:
2067		type = ACPI_BUS_TYPE_PROCESSOR;
2068		break;
2069
2070	case ACPI_TYPE_THERMAL:
2071		type = ACPI_BUS_TYPE_THERMAL;
2072		break;
2073
2074	case ACPI_TYPE_POWER:
2075		acpi_add_power_resource(handle);
2076		fallthrough;
2077	default:
2078		return AE_OK;
2079	}
2080
2081	/*
2082	 * If check_dep is true at this point, the device has no dependencies,
2083	 * or the creation of the device object would have been postponed above.
2084	 */
2085	acpi_add_single_object(&device, handle, type, !check_dep);
2086	if (!device)
2087		return AE_CTRL_DEPTH;
2088
2089	acpi_scan_init_hotplug(device);
2090
2091out:
2092	if (!*adev_p)
2093		*adev_p = device;
2094
2095	return AE_OK;
2096}
2097
2098static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
2099					void *not_used, void **ret_p)
2100{
2101	return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
2102}
2103
2104static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
2105					void *not_used, void **ret_p)
2106{
2107	return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
2108}
2109
2110static void acpi_default_enumeration(struct acpi_device *device)
2111{
2112	/*
2113	 * Do not enumerate devices with enumeration_by_parent flag set as
2114	 * they will be enumerated by their respective parents.
2115	 */
2116	if (!device->flags.enumeration_by_parent) {
2117		acpi_create_platform_device(device, NULL);
2118		acpi_device_set_enumerated(device);
2119	} else {
2120		blocking_notifier_call_chain(&acpi_reconfig_chain,
2121					     ACPI_RECONFIG_DEVICE_ADD, device);
2122	}
2123}
2124
2125static const struct acpi_device_id generic_device_ids[] = {
2126	{ACPI_DT_NAMESPACE_HID, },
2127	{"", },
2128};
2129
2130static int acpi_generic_device_attach(struct acpi_device *adev,
2131				      const struct acpi_device_id *not_used)
2132{
2133	/*
2134	 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2135	 * below can be unconditional.
2136	 */
2137	if (adev->data.of_compatible)
2138		acpi_default_enumeration(adev);
2139
2140	return 1;
2141}
2142
2143static struct acpi_scan_handler generic_device_handler = {
2144	.ids = generic_device_ids,
2145	.attach = acpi_generic_device_attach,
2146};
2147
2148static int acpi_scan_attach_handler(struct acpi_device *device)
2149{
2150	struct acpi_hardware_id *hwid;
2151	int ret = 0;
2152
2153	list_for_each_entry(hwid, &device->pnp.ids, list) {
2154		const struct acpi_device_id *devid;
2155		struct acpi_scan_handler *handler;
2156
2157		handler = acpi_scan_match_handler(hwid->id, &devid);
2158		if (handler) {
2159			if (!handler->attach) {
2160				device->pnp.type.platform_id = 0;
2161				continue;
2162			}
2163			device->handler = handler;
2164			ret = handler->attach(device, devid);
2165			if (ret > 0)
2166				break;
2167
2168			device->handler = NULL;
2169			if (ret < 0)
2170				break;
2171		}
2172	}
2173
2174	return ret;
2175}
2176
2177static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
2178{
2179	bool skip = !first_pass && device->flags.visited;
2180	acpi_handle ejd;
2181	int ret;
2182
2183	if (skip)
2184		goto ok;
2185
2186	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2187		register_dock_dependent_device(device, ejd);
2188
2189	acpi_bus_get_status(device);
2190	/* Skip devices that are not ready for enumeration (e.g. not present) */
2191	if (!acpi_dev_ready_for_enumeration(device)) {
2192		device->flags.initialized = false;
2193		acpi_device_clear_enumerated(device);
2194		device->flags.power_manageable = 0;
2195		return 0;
2196	}
2197	if (device->handler)
2198		goto ok;
2199
2200	if (!device->flags.initialized) {
2201		device->flags.power_manageable =
2202			device->power.states[ACPI_STATE_D0].flags.valid;
2203		if (acpi_bus_init_power(device))
2204			device->flags.power_manageable = 0;
2205
2206		device->flags.initialized = true;
2207	} else if (device->flags.visited) {
2208		goto ok;
2209	}
2210
2211	ret = acpi_scan_attach_handler(device);
2212	if (ret < 0)
2213		return 0;
2214
2215	device->flags.match_driver = true;
2216	if (ret > 0 && !device->flags.enumeration_by_parent) {
2217		acpi_device_set_enumerated(device);
2218		goto ok;
2219	}
2220
2221	ret = device_attach(&device->dev);
2222	if (ret < 0)
2223		return 0;
2224
2225	if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2226		acpi_default_enumeration(device);
2227	else
2228		acpi_device_set_enumerated(device);
2229
2230ok:
2231	acpi_dev_for_each_child(device, acpi_bus_attach, first_pass);
2232
2233	if (!skip && device->handler && device->handler->hotplug.notify_online)
2234		device->handler->hotplug.notify_online(device);
2235
2236	return 0;
2237}
2238
2239static int acpi_dev_get_next_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
2240{
2241	struct acpi_device **adev_p = data;
2242	struct acpi_device *adev = *adev_p;
2243
2244	/*
2245	 * If we're passed a 'previous' consumer device then we need to skip
2246	 * any consumers until we meet the previous one, and then NULL @data
2247	 * so the next one can be returned.
2248	 */
2249	if (adev) {
2250		if (dep->consumer == adev->handle)
2251			*adev_p = NULL;
2252
2253		return 0;
2254	}
2255
2256	adev = acpi_get_acpi_dev(dep->consumer);
2257	if (adev) {
2258		*(struct acpi_device **)data = adev;
2259		return 1;
2260	}
2261	/* Continue parsing if the device object is not present. */
2262	return 0;
2263}
2264
2265struct acpi_scan_clear_dep_work {
2266	struct work_struct work;
2267	struct acpi_device *adev;
2268};
2269
2270static void acpi_scan_clear_dep_fn(struct work_struct *work)
2271{
2272	struct acpi_scan_clear_dep_work *cdw;
2273
2274	cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
2275
2276	acpi_scan_lock_acquire();
2277	acpi_bus_attach(cdw->adev, (void *)true);
2278	acpi_scan_lock_release();
2279
2280	acpi_dev_put(cdw->adev);
2281	kfree(cdw);
2282}
2283
2284static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2285{
2286	struct acpi_scan_clear_dep_work *cdw;
2287
2288	if (adev->dep_unmet)
2289		return false;
2290
2291	cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2292	if (!cdw)
2293		return false;
2294
2295	cdw->adev = adev;
2296	INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2297	/*
2298	 * Since the work function may block on the lock until the entire
2299	 * initial enumeration of devices is complete, put it into the unbound
2300	 * workqueue.
2301	 */
2302	queue_work(system_unbound_wq, &cdw->work);
2303
2304	return true;
2305}
2306
2307static void acpi_scan_delete_dep_data(struct acpi_dep_data *dep)
2308{
2309	list_del(&dep->node);
2310	kfree(dep);
2311}
2312
2313static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
2314{
2315	struct acpi_device *adev = acpi_get_acpi_dev(dep->consumer);
2316
2317	if (adev) {
2318		adev->dep_unmet--;
2319		if (!acpi_scan_clear_dep_queue(adev))
2320			acpi_dev_put(adev);
2321	}
2322
2323	if (dep->free_when_met)
2324		acpi_scan_delete_dep_data(dep);
2325	else
2326		dep->met = true;
2327
2328	return 0;
2329}
2330
2331/**
2332 * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2333 * @handle:	The ACPI handle of the supplier device
2334 * @callback:	Pointer to the callback function to apply
2335 * @data:	Pointer to some data to pass to the callback
2336 *
2337 * The return value of the callback determines this function's behaviour. If 0
2338 * is returned we continue to iterate over acpi_dep_list. If a positive value
2339 * is returned then the loop is broken but this function returns 0. If a
2340 * negative value is returned by the callback then the loop is broken and that
2341 * value is returned as the final error.
2342 */
2343static int acpi_walk_dep_device_list(acpi_handle handle,
2344				int (*callback)(struct acpi_dep_data *, void *),
2345				void *data)
2346{
2347	struct acpi_dep_data *dep, *tmp;
2348	int ret = 0;
2349
2350	mutex_lock(&acpi_dep_list_lock);
2351	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2352		if (dep->supplier == handle) {
2353			ret = callback(dep, data);
2354			if (ret)
2355				break;
2356		}
2357	}
2358	mutex_unlock(&acpi_dep_list_lock);
2359
2360	return ret > 0 ? 0 : ret;
2361}
2362
2363/**
2364 * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2365 * @supplier: Pointer to the supplier &struct acpi_device
2366 *
2367 * Clear dependencies on the given device.
2368 */
2369void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2370{
2371	acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2372}
2373EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2374
2375/**
2376 * acpi_dev_ready_for_enumeration - Check if the ACPI device is ready for enumeration
2377 * @device: Pointer to the &struct acpi_device to check
2378 *
2379 * Check if the device is present and has no unmet dependencies.
2380 *
2381 * Return true if the device is ready for enumeratino. Otherwise, return false.
2382 */
2383bool acpi_dev_ready_for_enumeration(const struct acpi_device *device)
2384{
2385	if (device->flags.honor_deps && device->dep_unmet)
2386		return false;
2387
2388	return acpi_device_is_present(device);
2389}
2390EXPORT_SYMBOL_GPL(acpi_dev_ready_for_enumeration);
2391
2392/**
2393 * acpi_dev_get_next_consumer_dev - Return the next adev dependent on @supplier
2394 * @supplier: Pointer to the dependee device
2395 * @start: Pointer to the current dependent device
2396 *
2397 * Returns the next &struct acpi_device which declares itself dependent on
2398 * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2399 *
2400 * If the returned adev is not passed as @start to this function, the caller is
2401 * responsible for putting the reference to adev when it is no longer needed.
2402 */
2403struct acpi_device *acpi_dev_get_next_consumer_dev(struct acpi_device *supplier,
2404						   struct acpi_device *start)
2405{
2406	struct acpi_device *adev = start;
2407
2408	acpi_walk_dep_device_list(supplier->handle,
2409				  acpi_dev_get_next_consumer_dev_cb, &adev);
2410
2411	acpi_dev_put(start);
2412
2413	if (adev == start)
2414		return NULL;
2415
2416	return adev;
2417}
2418EXPORT_SYMBOL_GPL(acpi_dev_get_next_consumer_dev);
2419
2420static void acpi_scan_postponed_branch(acpi_handle handle)
2421{
2422	struct acpi_device *adev = NULL;
2423
2424	if (ACPI_FAILURE(acpi_bus_check_add(handle, false, &adev)))
2425		return;
2426
2427	acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2428			    acpi_bus_check_add_2, NULL, NULL, (void **)&adev);
2429	acpi_bus_attach(adev, NULL);
2430}
2431
2432static void acpi_scan_postponed(void)
2433{
2434	struct acpi_dep_data *dep, *tmp;
2435
2436	mutex_lock(&acpi_dep_list_lock);
2437
2438	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2439		acpi_handle handle = dep->consumer;
2440
2441		/*
2442		 * In case there are multiple acpi_dep_list entries with the
2443		 * same consumer, skip the current entry if the consumer device
2444		 * object corresponding to it is present already.
2445		 */
2446		if (!acpi_fetch_acpi_dev(handle)) {
2447			/*
2448			 * Even though the lock is released here, tmp is
2449			 * guaranteed to be valid, because none of the list
2450			 * entries following dep is marked as "free when met"
2451			 * and so they cannot be deleted.
2452			 */
2453			mutex_unlock(&acpi_dep_list_lock);
2454
2455			acpi_scan_postponed_branch(handle);
2456
2457			mutex_lock(&acpi_dep_list_lock);
2458		}
2459
2460		if (dep->met)
2461			acpi_scan_delete_dep_data(dep);
2462		else
2463			dep->free_when_met = true;
2464	}
2465
2466	mutex_unlock(&acpi_dep_list_lock);
2467}
2468
2469/**
2470 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2471 * @handle: Root of the namespace scope to scan.
2472 *
2473 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2474 * found devices.
2475 *
2476 * If no devices were found, -ENODEV is returned, but it does not mean that
2477 * there has been a real error.  There just have been no suitable ACPI objects
2478 * in the table trunk from which the kernel could create a device and add an
2479 * appropriate driver.
2480 *
2481 * Must be called under acpi_scan_lock.
2482 */
2483int acpi_bus_scan(acpi_handle handle)
2484{
2485	struct acpi_device *device = NULL;
2486
2487	/* Pass 1: Avoid enumerating devices with missing dependencies. */
2488
2489	if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
2490		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2491				    acpi_bus_check_add_1, NULL, NULL,
2492				    (void **)&device);
2493
2494	if (!device)
2495		return -ENODEV;
2496
2497	acpi_bus_attach(device, (void *)true);
2498
2499	/* Pass 2: Enumerate all of the remaining devices. */
2500
2501	acpi_scan_postponed();
2502
2503	return 0;
2504}
2505EXPORT_SYMBOL(acpi_bus_scan);
2506
2507static int acpi_bus_trim_one(struct acpi_device *adev, void *not_used)
2508{
2509	struct acpi_scan_handler *handler = adev->handler;
2510
2511	acpi_dev_for_each_child_reverse(adev, acpi_bus_trim_one, NULL);
2512
2513	adev->flags.match_driver = false;
2514	if (handler) {
2515		if (handler->detach)
2516			handler->detach(adev);
2517
2518		adev->handler = NULL;
2519	} else {
2520		device_release_driver(&adev->dev);
2521	}
2522	/*
2523	 * Most likely, the device is going away, so put it into D3cold before
2524	 * that.
2525	 */
2526	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2527	adev->flags.initialized = false;
2528	acpi_device_clear_enumerated(adev);
2529
2530	return 0;
2531}
2532
2533/**
2534 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2535 * @adev: Root of the ACPI namespace scope to walk.
2536 *
2537 * Must be called under acpi_scan_lock.
2538 */
2539void acpi_bus_trim(struct acpi_device *adev)
2540{
2541	acpi_bus_trim_one(adev, NULL);
2542}
2543EXPORT_SYMBOL_GPL(acpi_bus_trim);
2544
2545int acpi_bus_register_early_device(int type)
2546{
2547	struct acpi_device *device = NULL;
2548	int result;
2549
2550	result = acpi_add_single_object(&device, NULL, type, false);
2551	if (result)
2552		return result;
2553
2554	device->flags.match_driver = true;
2555	return device_attach(&device->dev);
2556}
2557EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2558
2559static void acpi_bus_scan_fixed(void)
2560{
2561	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2562		struct acpi_device *adev = NULL;
2563
2564		acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_POWER_BUTTON,
2565				       false);
2566		if (adev) {
2567			adev->flags.match_driver = true;
2568			if (device_attach(&adev->dev) >= 0)
2569				device_init_wakeup(&adev->dev, true);
2570			else
2571				dev_dbg(&adev->dev, "No driver\n");
2572		}
2573	}
2574
2575	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2576		struct acpi_device *adev = NULL;
2577
2578		acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_SLEEP_BUTTON,
2579				       false);
2580		if (adev) {
2581			adev->flags.match_driver = true;
2582			if (device_attach(&adev->dev) < 0)
2583				dev_dbg(&adev->dev, "No driver\n");
2584		}
2585	}
2586}
2587
2588static void __init acpi_get_spcr_uart_addr(void)
2589{
2590	acpi_status status;
2591	struct acpi_table_spcr *spcr_ptr;
2592
2593	status = acpi_get_table(ACPI_SIG_SPCR, 0,
2594				(struct acpi_table_header **)&spcr_ptr);
2595	if (ACPI_FAILURE(status)) {
2596		pr_warn("STAO table present, but SPCR is missing\n");
2597		return;
2598	}
2599
2600	spcr_uart_addr = spcr_ptr->serial_port.address;
2601	acpi_put_table((struct acpi_table_header *)spcr_ptr);
2602}
2603
2604static bool acpi_scan_initialized;
2605
2606void __init acpi_scan_init(void)
2607{
2608	acpi_status status;
2609	struct acpi_table_stao *stao_ptr;
2610
2611	acpi_pci_root_init();
2612	acpi_pci_link_init();
2613	acpi_processor_init();
2614	acpi_platform_init();
2615	acpi_lpss_init();
2616	acpi_apd_init();
2617	acpi_cmos_rtc_init();
2618	acpi_container_init();
2619	acpi_memory_hotplug_init();
2620	acpi_watchdog_init();
2621	acpi_pnp_init();
2622	acpi_int340x_thermal_init();
2623	acpi_init_lpit();
2624
2625	acpi_scan_add_handler(&generic_device_handler);
2626
2627	/*
2628	 * If there is STAO table, check whether it needs to ignore the UART
2629	 * device in SPCR table.
2630	 */
2631	status = acpi_get_table(ACPI_SIG_STAO, 0,
2632				(struct acpi_table_header **)&stao_ptr);
2633	if (ACPI_SUCCESS(status)) {
2634		if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2635			pr_info("STAO Name List not yet supported.\n");
2636
2637		if (stao_ptr->ignore_uart)
2638			acpi_get_spcr_uart_addr();
2639
2640		acpi_put_table((struct acpi_table_header *)stao_ptr);
2641	}
2642
2643	acpi_gpe_apply_masked_gpes();
2644	acpi_update_all_gpes();
2645
2646	/*
2647	 * Although we call __add_memory() that is documented to require the
2648	 * device_hotplug_lock, it is not necessary here because this is an
2649	 * early code when userspace or any other code path cannot trigger
2650	 * hotplug/hotunplug operations.
2651	 */
2652	mutex_lock(&acpi_scan_lock);
2653	/*
2654	 * Enumerate devices in the ACPI namespace.
2655	 */
2656	if (acpi_bus_scan(ACPI_ROOT_OBJECT))
2657		goto unlock;
2658
2659	acpi_root = acpi_fetch_acpi_dev(ACPI_ROOT_OBJECT);
2660	if (!acpi_root)
2661		goto unlock;
2662
2663	/* Fixed feature devices do not exist on HW-reduced platform */
2664	if (!acpi_gbl_reduced_hardware)
2665		acpi_bus_scan_fixed();
2666
2667	acpi_turn_off_unused_power_resources();
2668
2669	acpi_scan_initialized = true;
2670
2671unlock:
2672	mutex_unlock(&acpi_scan_lock);
2673}
2674
2675static struct acpi_probe_entry *ape;
2676static int acpi_probe_count;
2677static DEFINE_MUTEX(acpi_probe_mutex);
2678
2679static int __init acpi_match_madt(union acpi_subtable_headers *header,
2680				  const unsigned long end)
2681{
2682	if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2683		if (!ape->probe_subtbl(header, end))
2684			acpi_probe_count++;
2685
2686	return 0;
2687}
2688
2689int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2690{
2691	int count = 0;
2692
2693	if (acpi_disabled)
2694		return 0;
2695
2696	mutex_lock(&acpi_probe_mutex);
2697	for (ape = ap_head; nr; ape++, nr--) {
2698		if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2699			acpi_probe_count = 0;
2700			acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2701			count += acpi_probe_count;
2702		} else {
2703			int res;
2704			res = acpi_table_parse(ape->id, ape->probe_table);
2705			if (!res)
2706				count++;
2707		}
2708	}
2709	mutex_unlock(&acpi_probe_mutex);
2710
2711	return count;
2712}
2713
2714static void acpi_table_events_fn(struct work_struct *work)
2715{
2716	acpi_scan_lock_acquire();
2717	acpi_bus_scan(ACPI_ROOT_OBJECT);
2718	acpi_scan_lock_release();
2719
2720	kfree(work);
2721}
2722
2723void acpi_scan_table_notify(void)
2724{
2725	struct work_struct *work;
2726
2727	if (!acpi_scan_initialized)
2728		return;
2729
2730	work = kmalloc(sizeof(*work), GFP_KERNEL);
2731	if (!work)
2732		return;
2733
2734	INIT_WORK(work, acpi_table_events_fn);
2735	schedule_work(work);
2736}
2737
2738int acpi_reconfig_notifier_register(struct notifier_block *nb)
2739{
2740	return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2741}
2742EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2743
2744int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2745{
2746	return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2747}
2748EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);
2749