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