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