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