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