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