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