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