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