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