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