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