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