1 // SPDX-License-Identifier: GPL-2.0-only
3 * scan.c - support for transforming the ACPI namespace into individual objects
6 #define pr_fmt(fmt) "ACPI: " fmt
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/signal.h>
15 #include <linux/kthread.h>
16 #include <linux/dmi.h>
17 #include <linux/nls.h>
18 #include <linux/dma-map-ops.h>
19 #include <linux/platform_data/x86/apple.h>
20 #include <linux/pgtable.h>
24 extern struct acpi_device *acpi_root;
26 #define ACPI_BUS_CLASS "system_bus"
27 #define ACPI_BUS_HID "LNXSYBUS"
28 #define ACPI_BUS_DEVICE_NAME "System Bus"
30 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
32 #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
34 static const char *dummy_hid = "device";
36 static LIST_HEAD(acpi_dep_list);
37 static DEFINE_MUTEX(acpi_dep_list_lock);
38 LIST_HEAD(acpi_bus_id_list);
39 static DEFINE_MUTEX(acpi_scan_lock);
40 static LIST_HEAD(acpi_scan_handlers_list);
41 DEFINE_MUTEX(acpi_device_lock);
42 LIST_HEAD(acpi_wakeup_device_list);
43 static DEFINE_MUTEX(acpi_hp_context_lock);
46 * The UART device described by the SPCR table is the only object which needs
47 * special-casing. Everything else is covered by ACPI namespace paths in STAO
50 static u64 spcr_uart_addr;
52 struct acpi_dep_data {
53 struct list_head node;
58 void acpi_scan_lock_acquire(void)
60 mutex_lock(&acpi_scan_lock);
62 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
64 void acpi_scan_lock_release(void)
66 mutex_unlock(&acpi_scan_lock);
68 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
70 void acpi_lock_hp_context(void)
72 mutex_lock(&acpi_hp_context_lock);
75 void acpi_unlock_hp_context(void)
77 mutex_unlock(&acpi_hp_context_lock);
80 void acpi_initialize_hp_context(struct acpi_device *adev,
81 struct acpi_hotplug_context *hp,
82 int (*notify)(struct acpi_device *, u32),
83 void (*uevent)(struct acpi_device *, u32))
85 acpi_lock_hp_context();
88 acpi_set_hp_context(adev, hp);
89 acpi_unlock_hp_context();
91 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
93 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
98 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
102 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
103 const char *hotplug_profile_name)
107 error = acpi_scan_add_handler(handler);
111 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
115 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
117 struct acpi_device_physical_node *pn;
119 char *envp[] = { "EVENT=offline", NULL };
122 * acpi_container_offline() calls this for all of the container's
123 * children under the container's physical_node_lock lock.
125 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
127 list_for_each_entry(pn, &adev->physical_node_list, node)
128 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
130 kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
136 mutex_unlock(&adev->physical_node_lock);
140 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
143 struct acpi_device *device = NULL;
144 struct acpi_device_physical_node *pn;
145 bool second_pass = (bool)data;
146 acpi_status status = AE_OK;
148 if (acpi_bus_get_device(handle, &device))
151 if (device->handler && !device->handler->hotplug.enabled) {
152 *ret_p = &device->dev;
156 mutex_lock(&device->physical_node_lock);
158 list_for_each_entry(pn, &device->physical_node_list, node) {
162 /* Skip devices offlined by the first pass. */
166 pn->put_online = false;
168 ret = device_offline(pn->dev);
170 pn->put_online = !ret;
180 mutex_unlock(&device->physical_node_lock);
185 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
188 struct acpi_device *device = NULL;
189 struct acpi_device_physical_node *pn;
191 if (acpi_bus_get_device(handle, &device))
194 mutex_lock(&device->physical_node_lock);
196 list_for_each_entry(pn, &device->physical_node_list, node)
197 if (pn->put_online) {
198 device_online(pn->dev);
199 pn->put_online = false;
202 mutex_unlock(&device->physical_node_lock);
207 static int acpi_scan_try_to_offline(struct acpi_device *device)
209 acpi_handle handle = device->handle;
210 struct device *errdev = NULL;
214 * Carry out two passes here and ignore errors in the first pass,
215 * because if the devices in question are memory blocks and
216 * CONFIG_MEMCG is set, one of the blocks may hold data structures
217 * that the other blocks depend on, but it is not known in advance which
220 * If the first pass is successful, the second one isn't needed, though.
222 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
223 NULL, acpi_bus_offline, (void *)false,
225 if (status == AE_SUPPORT) {
226 dev_warn(errdev, "Offline disabled.\n");
227 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
228 acpi_bus_online, NULL, NULL, NULL);
231 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
234 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
235 NULL, acpi_bus_offline, (void *)true,
238 acpi_bus_offline(handle, 0, (void *)true,
242 dev_warn(errdev, "Offline failed.\n");
243 acpi_bus_online(handle, 0, NULL, NULL);
244 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
245 ACPI_UINT32_MAX, acpi_bus_online,
253 static int acpi_scan_hot_remove(struct acpi_device *device)
255 acpi_handle handle = device->handle;
256 unsigned long long sta;
259 if (device->handler && device->handler->hotplug.demand_offline) {
260 if (!acpi_scan_is_offline(device, true))
263 int error = acpi_scan_try_to_offline(device);
268 acpi_handle_debug(handle, "Ejecting\n");
270 acpi_bus_trim(device);
272 acpi_evaluate_lck(handle, 0);
276 status = acpi_evaluate_ej0(handle);
277 if (status == AE_NOT_FOUND)
279 else if (ACPI_FAILURE(status))
283 * Verify if eject was indeed successful. If not, log an error
284 * message. No need to call _OST since _EJ0 call was made OK.
286 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
287 if (ACPI_FAILURE(status)) {
288 acpi_handle_warn(handle,
289 "Status check after eject failed (0x%x)\n", status);
290 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
291 acpi_handle_warn(handle,
292 "Eject incomplete - status 0x%llx\n", sta);
298 static int acpi_scan_device_not_present(struct acpi_device *adev)
300 if (!acpi_device_enumerated(adev)) {
301 dev_warn(&adev->dev, "Still not present\n");
308 static int acpi_scan_device_check(struct acpi_device *adev)
312 acpi_bus_get_status(adev);
313 if (adev->status.present || adev->status.functional) {
315 * This function is only called for device objects for which
316 * matching scan handlers exist. The only situation in which
317 * the scan handler is not attached to this device object yet
318 * is when the device has just appeared (either it wasn't
319 * present at all before or it was removed and then added
323 dev_warn(&adev->dev, "Already enumerated\n");
326 error = acpi_bus_scan(adev->handle);
328 dev_warn(&adev->dev, "Namespace scan failure\n");
331 if (!adev->handler) {
332 dev_warn(&adev->dev, "Enumeration failure\n");
336 error = acpi_scan_device_not_present(adev);
341 static int acpi_scan_bus_check(struct acpi_device *adev)
343 struct acpi_scan_handler *handler = adev->handler;
344 struct acpi_device *child;
347 acpi_bus_get_status(adev);
348 if (!(adev->status.present || adev->status.functional)) {
349 acpi_scan_device_not_present(adev);
352 if (handler && handler->hotplug.scan_dependent)
353 return handler->hotplug.scan_dependent(adev);
355 error = acpi_bus_scan(adev->handle);
357 dev_warn(&adev->dev, "Namespace scan failure\n");
360 list_for_each_entry(child, &adev->children, node) {
361 error = acpi_scan_bus_check(child);
368 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
371 case ACPI_NOTIFY_BUS_CHECK:
372 return acpi_scan_bus_check(adev);
373 case ACPI_NOTIFY_DEVICE_CHECK:
374 return acpi_scan_device_check(adev);
375 case ACPI_NOTIFY_EJECT_REQUEST:
376 case ACPI_OST_EC_OSPM_EJECT:
377 if (adev->handler && !adev->handler->hotplug.enabled) {
378 dev_info(&adev->dev, "Eject disabled\n");
381 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
382 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
383 return acpi_scan_hot_remove(adev);
388 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
390 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
393 lock_device_hotplug();
394 mutex_lock(&acpi_scan_lock);
397 * The device object's ACPI handle cannot become invalid as long as we
398 * are holding acpi_scan_lock, but it might have become invalid before
399 * that lock was acquired.
401 if (adev->handle == INVALID_ACPI_HANDLE)
404 if (adev->flags.is_dock_station) {
405 error = dock_notify(adev, src);
406 } else if (adev->flags.hotplug_notify) {
407 error = acpi_generic_hotplug_event(adev, src);
409 int (*notify)(struct acpi_device *, u32);
411 acpi_lock_hp_context();
412 notify = adev->hp ? adev->hp->notify : NULL;
413 acpi_unlock_hp_context();
415 * There may be additional notify handlers for device objects
416 * without the .event() callback, so ignore them here.
419 error = notify(adev, src);
425 ost_code = ACPI_OST_SC_SUCCESS;
428 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
431 ost_code = ACPI_OST_SC_DEVICE_BUSY;
434 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
439 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
442 acpi_bus_put_acpi_device(adev);
443 mutex_unlock(&acpi_scan_lock);
444 unlock_device_hotplug();
447 static void acpi_free_power_resources_lists(struct acpi_device *device)
451 if (device->wakeup.flags.valid)
452 acpi_power_resources_list_free(&device->wakeup.resources);
454 if (!device->power.flags.power_resources)
457 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
458 struct acpi_device_power_state *ps = &device->power.states[i];
459 acpi_power_resources_list_free(&ps->resources);
463 static void acpi_device_release(struct device *dev)
465 struct acpi_device *acpi_dev = to_acpi_device(dev);
467 acpi_free_properties(acpi_dev);
468 acpi_free_pnp_ids(&acpi_dev->pnp);
469 acpi_free_power_resources_lists(acpi_dev);
473 static void acpi_device_del(struct acpi_device *device)
475 struct acpi_device_bus_id *acpi_device_bus_id;
477 mutex_lock(&acpi_device_lock);
479 list_del(&device->node);
481 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
482 if (!strcmp(acpi_device_bus_id->bus_id,
483 acpi_device_hid(device))) {
484 ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
485 if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
486 list_del(&acpi_device_bus_id->node);
487 kfree_const(acpi_device_bus_id->bus_id);
488 kfree(acpi_device_bus_id);
493 list_del(&device->wakeup_list);
494 mutex_unlock(&acpi_device_lock);
496 acpi_power_add_remove_device(device, false);
497 acpi_device_remove_files(device);
499 device->remove(device);
501 device_del(&device->dev);
504 static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
506 static LIST_HEAD(acpi_device_del_list);
507 static DEFINE_MUTEX(acpi_device_del_lock);
509 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
512 struct acpi_device *adev;
514 mutex_lock(&acpi_device_del_lock);
516 if (list_empty(&acpi_device_del_list)) {
517 mutex_unlock(&acpi_device_del_lock);
520 adev = list_first_entry(&acpi_device_del_list,
521 struct acpi_device, del_list);
522 list_del(&adev->del_list);
524 mutex_unlock(&acpi_device_del_lock);
526 blocking_notifier_call_chain(&acpi_reconfig_chain,
527 ACPI_RECONFIG_DEVICE_REMOVE, adev);
529 acpi_device_del(adev);
531 * Drop references to all power resources that might have been
532 * used by the device.
534 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
540 * acpi_scan_drop_device - Drop an ACPI device object.
541 * @handle: Handle of an ACPI namespace node, not used.
542 * @context: Address of the ACPI device object to drop.
544 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
545 * namespace node the device object pointed to by @context is attached to.
547 * The unregistration is carried out asynchronously to avoid running
548 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
549 * ensure the correct ordering (the device objects must be unregistered in the
550 * same order in which the corresponding namespace nodes are deleted).
552 static void acpi_scan_drop_device(acpi_handle handle, void *context)
554 static DECLARE_WORK(work, acpi_device_del_work_fn);
555 struct acpi_device *adev = context;
557 mutex_lock(&acpi_device_del_lock);
560 * Use the ACPI hotplug workqueue which is ordered, so this work item
561 * won't run after any hotplug work items submitted subsequently. That
562 * prevents attempts to register device objects identical to those being
563 * deleted from happening concurrently (such attempts result from
564 * hotplug events handled via the ACPI hotplug workqueue). It also will
565 * run after all of the work items submitted previously, which helps
566 * those work items to ensure that they are not accessing stale device
569 if (list_empty(&acpi_device_del_list))
570 acpi_queue_hotplug_work(&work);
572 list_add_tail(&adev->del_list, &acpi_device_del_list);
573 /* Make acpi_ns_validate_handle() return NULL for this handle. */
574 adev->handle = INVALID_ACPI_HANDLE;
576 mutex_unlock(&acpi_device_del_lock);
579 static struct acpi_device *handle_to_device(acpi_handle handle,
580 void (*callback)(void *))
582 struct acpi_device *adev = NULL;
585 status = acpi_get_data_full(handle, acpi_scan_drop_device,
586 (void **)&adev, callback);
587 if (ACPI_FAILURE(status) || !adev) {
588 acpi_handle_debug(handle, "No context!\n");
594 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
599 *device = handle_to_device(handle, NULL);
605 EXPORT_SYMBOL(acpi_bus_get_device);
607 static void get_acpi_device(void *dev)
612 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
614 return handle_to_device(handle, get_acpi_device);
617 void acpi_bus_put_acpi_device(struct acpi_device *adev)
622 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
624 struct acpi_device_bus_id *acpi_device_bus_id;
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;
634 static int acpi_device_set_name(struct acpi_device *device,
635 struct acpi_device_bus_id *acpi_device_bus_id)
637 struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
640 result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
644 device->pnp.instance_no = result;
645 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
649 int acpi_device_add(struct acpi_device *device,
650 void (*release)(struct device *))
652 struct acpi_device_bus_id *acpi_device_bus_id;
655 if (device->handle) {
658 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
660 if (ACPI_FAILURE(status)) {
661 acpi_handle_err(device->handle,
662 "Unable to attach device data\n");
670 * Link this device to its parent and siblings.
672 INIT_LIST_HEAD(&device->children);
673 INIT_LIST_HEAD(&device->node);
674 INIT_LIST_HEAD(&device->wakeup_list);
675 INIT_LIST_HEAD(&device->physical_node_list);
676 INIT_LIST_HEAD(&device->del_list);
677 mutex_init(&device->physical_node_lock);
679 mutex_lock(&acpi_device_lock);
681 acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
682 if (acpi_device_bus_id) {
683 result = acpi_device_set_name(device, acpi_device_bus_id);
687 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
689 if (!acpi_device_bus_id) {
693 acpi_device_bus_id->bus_id =
694 kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
695 if (!acpi_device_bus_id->bus_id) {
696 kfree(acpi_device_bus_id);
701 ida_init(&acpi_device_bus_id->instance_ida);
703 result = acpi_device_set_name(device, acpi_device_bus_id);
705 kfree_const(acpi_device_bus_id->bus_id);
706 kfree(acpi_device_bus_id);
710 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
714 list_add_tail(&device->node, &device->parent->children);
716 if (device->wakeup.flags.valid)
717 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
719 mutex_unlock(&acpi_device_lock);
722 device->dev.parent = &device->parent->dev;
724 device->dev.bus = &acpi_bus_type;
725 device->dev.release = release;
726 result = device_add(&device->dev);
728 dev_err(&device->dev, "Error registering device\n");
732 result = acpi_device_setup_files(device);
734 pr_err("Error creating sysfs interface for device %s\n",
735 dev_name(&device->dev));
740 mutex_lock(&acpi_device_lock);
743 list_del(&device->node);
745 list_del(&device->wakeup_list);
748 mutex_unlock(&acpi_device_lock);
750 acpi_detach_data(device->handle, acpi_scan_drop_device);
755 /* --------------------------------------------------------------------------
757 -------------------------------------------------------------------------- */
758 static bool acpi_info_matches_ids(struct acpi_device_info *info,
759 const char * const ids[])
761 struct acpi_pnp_device_id_list *cid_list = NULL;
764 if (!(info->valid & ACPI_VALID_HID))
767 index = match_string(ids, -1, info->hardware_id.string);
771 if (info->valid & ACPI_VALID_CID)
772 cid_list = &info->compatible_id_list;
777 for (i = 0; i < cid_list->count; i++) {
778 index = match_string(ids, -1, cid_list->ids[i].string);
786 /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
787 static const char * const acpi_ignore_dep_ids[] = {
788 "PNP0D80", /* Windows-compatible System Power Management Controller */
789 "INT33BD", /* Intel Baytrail Mailbox Device */
793 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
795 struct acpi_device *device = NULL;
799 * Fixed hardware devices do not appear in the namespace and do not
800 * have handles, but we fabricate acpi_devices for them, so we have
801 * to deal with them specially.
807 status = acpi_get_parent(handle, &handle);
808 if (ACPI_FAILURE(status))
809 return status == AE_NULL_ENTRY ? NULL : acpi_root;
810 } while (acpi_bus_get_device(handle, &device));
815 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
819 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
820 union acpi_object *obj;
822 status = acpi_get_handle(handle, "_EJD", &tmp);
823 if (ACPI_FAILURE(status))
826 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
827 if (ACPI_SUCCESS(status)) {
828 obj = buffer.pointer;
829 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
831 kfree(buffer.pointer);
835 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
837 static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
839 acpi_handle handle = dev->handle;
840 struct acpi_device_wakeup *wakeup = &dev->wakeup;
841 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
842 union acpi_object *package = NULL;
843 union acpi_object *element = NULL;
847 INIT_LIST_HEAD(&wakeup->resources);
850 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
851 if (ACPI_FAILURE(status)) {
852 acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
853 acpi_format_exception(status));
857 package = (union acpi_object *)buffer.pointer;
859 if (!package || package->package.count < 2)
862 element = &(package->package.elements[0]);
866 if (element->type == ACPI_TYPE_PACKAGE) {
867 if ((element->package.count < 2) ||
868 (element->package.elements[0].type !=
869 ACPI_TYPE_LOCAL_REFERENCE)
870 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
874 element->package.elements[0].reference.handle;
876 (u32) element->package.elements[1].integer.value;
877 } else if (element->type == ACPI_TYPE_INTEGER) {
878 wakeup->gpe_device = NULL;
879 wakeup->gpe_number = element->integer.value;
884 element = &(package->package.elements[1]);
885 if (element->type != ACPI_TYPE_INTEGER)
888 wakeup->sleep_state = element->integer.value;
890 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
894 if (!list_empty(&wakeup->resources)) {
897 err = acpi_power_wakeup_list_init(&wakeup->resources,
900 acpi_handle_warn(handle, "Retrieving current states "
901 "of wakeup power resources failed\n");
902 acpi_power_resources_list_free(&wakeup->resources);
905 if (sleep_state < wakeup->sleep_state) {
906 acpi_handle_warn(handle, "Overriding _PRW sleep state "
907 "(S%d) by S%d from power resources\n",
908 (int)wakeup->sleep_state, sleep_state);
909 wakeup->sleep_state = sleep_state;
914 kfree(buffer.pointer);
918 static bool acpi_wakeup_gpe_init(struct acpi_device *device)
920 static const struct acpi_device_id button_device_ids[] = {
921 {"PNP0C0C", 0}, /* Power button */
922 {"PNP0C0D", 0}, /* Lid */
923 {"PNP0C0E", 0}, /* Sleep button */
926 struct acpi_device_wakeup *wakeup = &device->wakeup;
929 wakeup->flags.notifier_present = 0;
931 /* Power button, Lid switch always enable wakeup */
932 if (!acpi_match_device_ids(device, button_device_ids)) {
933 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
934 /* Do not use Lid/sleep button for S5 wakeup */
935 if (wakeup->sleep_state == ACPI_STATE_S5)
936 wakeup->sleep_state = ACPI_STATE_S4;
938 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
939 device_set_wakeup_capable(&device->dev, true);
943 status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
945 return ACPI_SUCCESS(status);
948 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
952 /* Presence of _PRW indicates wake capable */
953 if (!acpi_has_method(device->handle, "_PRW"))
956 err = acpi_bus_extract_wakeup_device_power_package(device);
958 dev_err(&device->dev, "Unable to extract wakeup power resources");
962 device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
963 device->wakeup.prepare_count = 0;
965 * Call _PSW/_DSW object to disable its ability to wake the sleeping
966 * system for the ACPI device with the _PRW object.
967 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
968 * So it is necessary to call _DSW object first. Only when it is not
969 * present will the _PSW object used.
971 err = acpi_device_sleep_wake(device, 0, 0, 0);
973 pr_debug("error in _DSW or _PSW evaluation\n");
976 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
978 struct acpi_device_power_state *ps = &device->power.states[state];
979 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
980 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
983 INIT_LIST_HEAD(&ps->resources);
985 /* Evaluate "_PRx" to get referenced power resources */
986 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
987 if (ACPI_SUCCESS(status)) {
988 union acpi_object *package = buffer.pointer;
990 if (buffer.length && package
991 && package->type == ACPI_TYPE_PACKAGE
992 && package->package.count)
993 acpi_extract_power_resources(package, 0, &ps->resources);
995 ACPI_FREE(buffer.pointer);
998 /* Evaluate "_PSx" to see if we can do explicit sets */
1000 if (acpi_has_method(device->handle, pathname))
1001 ps->flags.explicit_set = 1;
1003 /* State is valid if there are means to put the device into it. */
1004 if (!list_empty(&ps->resources) || ps->flags.explicit_set)
1005 ps->flags.valid = 1;
1007 ps->power = -1; /* Unknown - driver assigned */
1008 ps->latency = -1; /* Unknown - driver assigned */
1011 static void acpi_bus_get_power_flags(struct acpi_device *device)
1015 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1016 if (!acpi_has_method(device->handle, "_PS0") &&
1017 !acpi_has_method(device->handle, "_PR0"))
1020 device->flags.power_manageable = 1;
1023 * Power Management Flags
1025 if (acpi_has_method(device->handle, "_PSC"))
1026 device->power.flags.explicit_get = 1;
1028 if (acpi_has_method(device->handle, "_IRC"))
1029 device->power.flags.inrush_current = 1;
1031 if (acpi_has_method(device->handle, "_DSW"))
1032 device->power.flags.dsw_present = 1;
1035 * Enumerate supported power management states
1037 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1038 acpi_bus_init_power_state(device, i);
1040 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1042 /* Set the defaults for D0 and D3hot (always supported). */
1043 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1044 device->power.states[ACPI_STATE_D0].power = 100;
1045 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1048 * Use power resources only if the D0 list of them is populated, because
1049 * some platforms may provide _PR3 only to indicate D3cold support and
1050 * in those cases the power resources list returned by it may be bogus.
1052 if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1053 device->power.flags.power_resources = 1;
1055 * D3cold is supported if the D3hot list of power resources is
1058 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1059 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1062 if (acpi_bus_init_power(device))
1063 device->flags.power_manageable = 0;
1066 static void acpi_bus_get_flags(struct acpi_device *device)
1068 /* Presence of _STA indicates 'dynamic_status' */
1069 if (acpi_has_method(device->handle, "_STA"))
1070 device->flags.dynamic_status = 1;
1072 /* Presence of _RMV indicates 'removable' */
1073 if (acpi_has_method(device->handle, "_RMV"))
1074 device->flags.removable = 1;
1076 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1077 if (acpi_has_method(device->handle, "_EJD") ||
1078 acpi_has_method(device->handle, "_EJ0"))
1079 device->flags.ejectable = 1;
1082 static void acpi_device_get_busid(struct acpi_device *device)
1084 char bus_id[5] = { '?', 0 };
1085 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1091 * The device's Bus ID is simply the object name.
1092 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1094 if (ACPI_IS_ROOT_DEVICE(device)) {
1095 strcpy(device->pnp.bus_id, "ACPI");
1099 switch (device->device_type) {
1100 case ACPI_BUS_TYPE_POWER_BUTTON:
1101 strcpy(device->pnp.bus_id, "PWRF");
1103 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1104 strcpy(device->pnp.bus_id, "SLPF");
1106 case ACPI_BUS_TYPE_ECDT_EC:
1107 strcpy(device->pnp.bus_id, "ECDT");
1110 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1111 /* Clean up trailing underscores (if any) */
1112 for (i = 3; i > 1; i--) {
1113 if (bus_id[i] == '_')
1118 strcpy(device->pnp.bus_id, bus_id);
1124 * acpi_ata_match - see if an acpi object is an ATA device
1126 * If an acpi object has one of the ACPI ATA methods defined,
1127 * then we can safely call it an ATA device.
1129 bool acpi_ata_match(acpi_handle handle)
1131 return acpi_has_method(handle, "_GTF") ||
1132 acpi_has_method(handle, "_GTM") ||
1133 acpi_has_method(handle, "_STM") ||
1134 acpi_has_method(handle, "_SDD");
1138 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1140 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1141 * then we can safely call it an ejectable drive bay
1143 bool acpi_bay_match(acpi_handle handle)
1145 acpi_handle phandle;
1147 if (!acpi_has_method(handle, "_EJ0"))
1149 if (acpi_ata_match(handle))
1151 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1154 return acpi_ata_match(phandle);
1157 bool acpi_device_is_battery(struct acpi_device *adev)
1159 struct acpi_hardware_id *hwid;
1161 list_for_each_entry(hwid, &adev->pnp.ids, list)
1162 if (!strcmp("PNP0C0A", hwid->id))
1168 static bool is_ejectable_bay(struct acpi_device *adev)
1170 acpi_handle handle = adev->handle;
1172 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1175 return acpi_bay_match(handle);
1179 * acpi_dock_match - see if an acpi object has a _DCK method
1181 bool acpi_dock_match(acpi_handle handle)
1183 return acpi_has_method(handle, "_DCK");
1187 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1188 void **return_value)
1190 long *cap = context;
1192 if (acpi_has_method(handle, "_BCM") &&
1193 acpi_has_method(handle, "_BCL")) {
1194 acpi_handle_debug(handle, "Found generic backlight support\n");
1195 *cap |= ACPI_VIDEO_BACKLIGHT;
1196 /* We have backlight support, no need to scan further */
1197 return AE_CTRL_TERMINATE;
1202 /* Returns true if the ACPI object is a video device which can be
1203 * handled by video.ko.
1204 * The device will get a Linux specific CID added in scan.c to
1205 * identify the device as an ACPI graphics device
1206 * Be aware that the graphics device may not be physically present
1207 * Use acpi_video_get_capabilities() to detect general ACPI video
1208 * capabilities of present cards
1210 long acpi_is_video_device(acpi_handle handle)
1212 long video_caps = 0;
1214 /* Is this device able to support video switching ? */
1215 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1216 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1218 /* Is this device able to retrieve a video ROM ? */
1219 if (acpi_has_method(handle, "_ROM"))
1220 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1222 /* Is this device able to configure which video head to be POSTed ? */
1223 if (acpi_has_method(handle, "_VPO") &&
1224 acpi_has_method(handle, "_GPD") &&
1225 acpi_has_method(handle, "_SPD"))
1226 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1228 /* Only check for backlight functionality if one of the above hit. */
1230 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1231 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1236 EXPORT_SYMBOL(acpi_is_video_device);
1238 const char *acpi_device_hid(struct acpi_device *device)
1240 struct acpi_hardware_id *hid;
1242 if (list_empty(&device->pnp.ids))
1245 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1248 EXPORT_SYMBOL(acpi_device_hid);
1250 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1252 struct acpi_hardware_id *id;
1254 id = kmalloc(sizeof(*id), GFP_KERNEL);
1258 id->id = kstrdup_const(dev_id, GFP_KERNEL);
1264 list_add_tail(&id->list, &pnp->ids);
1265 pnp->type.hardware_id = 1;
1269 * Old IBM workstations have a DSDT bug wherein the SMBus object
1270 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1271 * prefix. Work around this.
1273 static bool acpi_ibm_smbus_match(acpi_handle handle)
1275 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1276 struct acpi_buffer path = { sizeof(node_name), node_name };
1278 if (!dmi_name_in_vendors("IBM"))
1281 /* Look for SMBS object */
1282 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1283 strcmp("SMBS", path.pointer))
1286 /* Does it have the necessary (but misnamed) methods? */
1287 if (acpi_has_method(handle, "SBI") &&
1288 acpi_has_method(handle, "SBR") &&
1289 acpi_has_method(handle, "SBW"))
1295 static bool acpi_object_is_system_bus(acpi_handle handle)
1299 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1302 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1309 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1312 struct acpi_device_info *info = NULL;
1313 struct acpi_pnp_device_id_list *cid_list;
1316 switch (device_type) {
1317 case ACPI_BUS_TYPE_DEVICE:
1318 if (handle == ACPI_ROOT_OBJECT) {
1319 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1323 acpi_get_object_info(handle, &info);
1325 pr_err("%s: Error reading device info\n", __func__);
1329 if (info->valid & ACPI_VALID_HID) {
1330 acpi_add_id(pnp, info->hardware_id.string);
1331 pnp->type.platform_id = 1;
1333 if (info->valid & ACPI_VALID_CID) {
1334 cid_list = &info->compatible_id_list;
1335 for (i = 0; i < cid_list->count; i++)
1336 acpi_add_id(pnp, cid_list->ids[i].string);
1338 if (info->valid & ACPI_VALID_ADR) {
1339 pnp->bus_address = info->address;
1340 pnp->type.bus_address = 1;
1342 if (info->valid & ACPI_VALID_UID)
1343 pnp->unique_id = kstrdup(info->unique_id.string,
1345 if (info->valid & ACPI_VALID_CLS)
1346 acpi_add_id(pnp, info->class_code.string);
1351 * Some devices don't reliably have _HIDs & _CIDs, so add
1352 * synthetic HIDs to make sure drivers can find them.
1354 if (acpi_is_video_device(handle))
1355 acpi_add_id(pnp, ACPI_VIDEO_HID);
1356 else if (acpi_bay_match(handle))
1357 acpi_add_id(pnp, ACPI_BAY_HID);
1358 else if (acpi_dock_match(handle))
1359 acpi_add_id(pnp, ACPI_DOCK_HID);
1360 else if (acpi_ibm_smbus_match(handle))
1361 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1362 else if (list_empty(&pnp->ids) &&
1363 acpi_object_is_system_bus(handle)) {
1364 /* \_SB, \_TZ, LNXSYBUS */
1365 acpi_add_id(pnp, ACPI_BUS_HID);
1366 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1367 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1371 case ACPI_BUS_TYPE_POWER:
1372 acpi_add_id(pnp, ACPI_POWER_HID);
1374 case ACPI_BUS_TYPE_PROCESSOR:
1375 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1377 case ACPI_BUS_TYPE_THERMAL:
1378 acpi_add_id(pnp, ACPI_THERMAL_HID);
1380 case ACPI_BUS_TYPE_POWER_BUTTON:
1381 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1383 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1384 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1386 case ACPI_BUS_TYPE_ECDT_EC:
1387 acpi_add_id(pnp, ACPI_ECDT_HID);
1392 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1394 struct acpi_hardware_id *id, *tmp;
1396 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1397 kfree_const(id->id);
1400 kfree(pnp->unique_id);
1404 * acpi_dma_supported - Check DMA support for the specified device.
1405 * @adev: The pointer to acpi device
1407 * Return false if DMA is not supported. Otherwise, return true
1409 bool acpi_dma_supported(struct acpi_device *adev)
1414 if (adev->flags.cca_seen)
1418 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1419 * DMA on "Intel platforms". Presumably that includes all x86 and
1420 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1422 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1429 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1430 * @adev: The pointer to acpi device
1432 * Return enum dev_dma_attr.
1434 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1436 if (!acpi_dma_supported(adev))
1437 return DEV_DMA_NOT_SUPPORTED;
1439 if (adev->flags.coherent_dma)
1440 return DEV_DMA_COHERENT;
1442 return DEV_DMA_NON_COHERENT;
1446 * acpi_dma_get_range() - Get device DMA parameters.
1448 * @dev: device to configure
1449 * @dma_addr: pointer device DMA address result
1450 * @offset: pointer to the DMA offset result
1451 * @size: pointer to DMA range size result
1453 * Evaluate DMA regions and return respectively DMA region start, offset
1454 * and size in dma_addr, offset and size on parsing success; it does not
1455 * update the passed in values on failure.
1457 * Return 0 on success, < 0 on failure.
1459 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1462 struct acpi_device *adev;
1464 struct resource_entry *rentry;
1466 struct device *dma_dev = dev;
1467 u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1470 * Walk the device tree chasing an ACPI companion with a _DMA
1471 * object while we go. Stop if we find a device with an ACPI
1472 * companion containing a _DMA method.
1475 adev = ACPI_COMPANION(dma_dev);
1476 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1479 dma_dev = dma_dev->parent;
1485 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1486 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1490 ret = acpi_dev_get_dma_resources(adev, &list);
1492 list_for_each_entry(rentry, &list, node) {
1493 if (dma_offset && rentry->offset != dma_offset) {
1495 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1498 dma_offset = rentry->offset;
1500 /* Take lower and upper limits */
1501 if (rentry->res->start < dma_start)
1502 dma_start = rentry->res->start;
1503 if (rentry->res->end > dma_end)
1504 dma_end = rentry->res->end;
1507 if (dma_start >= dma_end) {
1509 dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1513 *dma_addr = dma_start - dma_offset;
1514 len = dma_end - dma_start;
1515 *size = max(len, len + 1);
1516 *offset = dma_offset;
1519 acpi_dev_free_resource_list(&list);
1521 return ret >= 0 ? 0 : ret;
1525 * acpi_dma_configure_id - Set-up DMA configuration for the device.
1526 * @dev: The pointer to the device
1527 * @attr: device dma attributes
1528 * @input_id: input device id const value pointer
1530 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1531 const u32 *input_id)
1533 const struct iommu_ops *iommu;
1534 u64 dma_addr = 0, size = 0;
1536 if (attr == DEV_DMA_NOT_SUPPORTED) {
1537 set_dma_ops(dev, &dma_dummy_ops);
1541 iort_dma_setup(dev, &dma_addr, &size);
1543 iommu = iort_iommu_configure_id(dev, input_id);
1544 if (PTR_ERR(iommu) == -EPROBE_DEFER)
1545 return -EPROBE_DEFER;
1547 arch_setup_dma_ops(dev, dma_addr, size,
1548 iommu, attr == DEV_DMA_COHERENT);
1552 EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1554 static void acpi_init_coherency(struct acpi_device *adev)
1556 unsigned long long cca = 0;
1558 struct acpi_device *parent = adev->parent;
1560 if (parent && parent->flags.cca_seen) {
1562 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1565 adev->flags.cca_seen = 1;
1566 cca = parent->flags.coherent_dma;
1568 status = acpi_evaluate_integer(adev->handle, "_CCA",
1570 if (ACPI_SUCCESS(status))
1571 adev->flags.cca_seen = 1;
1572 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1574 * If architecture does not specify that _CCA is
1575 * required for DMA-able devices (e.g. x86),
1576 * we default to _CCA=1.
1580 acpi_handle_debug(adev->handle,
1581 "ACPI device is missing _CCA.\n");
1584 adev->flags.coherent_dma = cca;
1587 static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1589 bool *is_serial_bus_slave_p = data;
1591 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1594 *is_serial_bus_slave_p = true;
1596 /* no need to do more checking */
1600 static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1602 struct acpi_device *parent = device->parent;
1603 static const struct acpi_device_id indirect_io_hosts[] = {
1608 return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1611 static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1613 struct list_head resource_list;
1614 bool is_serial_bus_slave = false;
1616 * These devices have multiple I2cSerialBus resources and an i2c-client
1617 * must be instantiated for each, each with its own i2c_device_id.
1618 * Normally we only instantiate an i2c-client for the first resource,
1619 * using the ACPI HID as id. These special cases are handled by the
1620 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1621 * which i2c_device_id to use for each resource.
1623 static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1631 if (acpi_is_indirect_io_slave(device))
1634 /* Macs use device properties in lieu of _CRS resources */
1635 if (x86_apple_machine &&
1636 (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1637 fwnode_property_present(&device->fwnode, "i2cAddress") ||
1638 fwnode_property_present(&device->fwnode, "baud")))
1641 /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1642 if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1645 INIT_LIST_HEAD(&resource_list);
1646 acpi_dev_get_resources(device, &resource_list,
1647 acpi_check_serial_bus_slave,
1648 &is_serial_bus_slave);
1649 acpi_dev_free_resource_list(&resource_list);
1651 return is_serial_bus_slave;
1654 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1657 INIT_LIST_HEAD(&device->pnp.ids);
1658 device->device_type = type;
1659 device->handle = handle;
1660 device->parent = acpi_bus_get_parent(handle);
1661 fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1662 acpi_set_device_status(device, ACPI_STA_DEFAULT);
1663 acpi_device_get_busid(device);
1664 acpi_set_pnp_ids(handle, &device->pnp, type);
1665 acpi_init_properties(device);
1666 acpi_bus_get_flags(device);
1667 device->flags.match_driver = false;
1668 device->flags.initialized = true;
1669 device->flags.enumeration_by_parent =
1670 acpi_device_enumeration_by_parent(device);
1671 acpi_device_clear_enumerated(device);
1672 device_initialize(&device->dev);
1673 dev_set_uevent_suppress(&device->dev, true);
1674 acpi_init_coherency(device);
1675 /* Assume there are unmet deps to start with. */
1676 device->dep_unmet = 1;
1679 void acpi_device_add_finalize(struct acpi_device *device)
1681 dev_set_uevent_suppress(&device->dev, false);
1682 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1685 static void acpi_scan_init_status(struct acpi_device *adev)
1687 if (acpi_bus_get_status(adev))
1688 acpi_set_device_status(adev, 0);
1691 static int acpi_add_single_object(struct acpi_device **child,
1692 acpi_handle handle, int type)
1694 struct acpi_device *device;
1697 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1701 acpi_init_device_object(device, handle, type);
1703 * Getting the status is delayed till here so that we can call
1704 * acpi_bus_get_status() and use its quirk handling. Note that
1705 * this must be done before the get power-/wakeup_dev-flags calls.
1707 if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR)
1708 acpi_scan_init_status(device);
1710 acpi_bus_get_power_flags(device);
1711 acpi_bus_get_wakeup_device_flags(device);
1713 result = acpi_device_add(device, acpi_device_release);
1715 acpi_device_release(&device->dev);
1719 acpi_power_add_remove_device(device, true);
1720 acpi_device_add_finalize(device);
1722 acpi_handle_debug(handle, "Added as %s, parent %s\n",
1723 dev_name(&device->dev), device->parent ?
1724 dev_name(&device->parent->dev) : "(null)");
1730 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1733 struct resource *res = context;
1735 if (acpi_dev_resource_memory(ares, res))
1736 return AE_CTRL_TERMINATE;
1741 static bool acpi_device_should_be_hidden(acpi_handle handle)
1744 struct resource res;
1746 /* Check if it should ignore the UART device */
1747 if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1751 * The UART device described in SPCR table is assumed to have only one
1752 * memory resource present. So we only look for the first one here.
1754 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1755 acpi_get_resource_memory, &res);
1756 if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1759 acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1765 bool acpi_device_is_present(const struct acpi_device *adev)
1767 return adev->status.present || adev->status.functional;
1770 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1772 const struct acpi_device_id **matchid)
1774 const struct acpi_device_id *devid;
1777 return handler->match(idstr, matchid);
1779 for (devid = handler->ids; devid->id[0]; devid++)
1780 if (!strcmp((char *)devid->id, idstr)) {
1790 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1791 const struct acpi_device_id **matchid)
1793 struct acpi_scan_handler *handler;
1795 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1796 if (acpi_scan_handler_matching(handler, idstr, matchid))
1802 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1804 if (!!hotplug->enabled == !!val)
1807 mutex_lock(&acpi_scan_lock);
1809 hotplug->enabled = val;
1811 mutex_unlock(&acpi_scan_lock);
1814 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1816 struct acpi_hardware_id *hwid;
1818 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1819 acpi_dock_add(adev);
1822 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1823 struct acpi_scan_handler *handler;
1825 handler = acpi_scan_match_handler(hwid->id, NULL);
1827 adev->flags.hotplug_notify = true;
1833 static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
1835 struct acpi_handle_list dep_devices;
1841 * Check for _HID here to avoid deferring the enumeration of:
1843 * 2. ACPI nodes describing USB ports.
1844 * Still, checking for _HID catches more then just these cases ...
1846 if (!check_dep || !acpi_has_method(handle, "_DEP") ||
1847 !acpi_has_method(handle, "_HID"))
1850 status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
1851 if (ACPI_FAILURE(status)) {
1852 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
1856 for (count = 0, i = 0; i < dep_devices.count; i++) {
1857 struct acpi_device_info *info;
1858 struct acpi_dep_data *dep;
1861 status = acpi_get_object_info(dep_devices.handles[i], &info);
1862 if (ACPI_FAILURE(status)) {
1863 acpi_handle_debug(handle, "Error reading _DEP device info\n");
1867 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
1873 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1879 dep->supplier = dep_devices.handles[i];
1880 dep->consumer = handle;
1882 mutex_lock(&acpi_dep_list_lock);
1883 list_add_tail(&dep->node , &acpi_dep_list);
1884 mutex_unlock(&acpi_dep_list_lock);
1890 static void acpi_scan_dep_init(struct acpi_device *adev)
1892 struct acpi_dep_data *dep;
1894 adev->dep_unmet = 0;
1896 mutex_lock(&acpi_dep_list_lock);
1898 list_for_each_entry(dep, &acpi_dep_list, node) {
1899 if (dep->consumer == adev->handle)
1903 mutex_unlock(&acpi_dep_list_lock);
1906 static bool acpi_bus_scan_second_pass;
1908 static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
1909 struct acpi_device **adev_p)
1911 struct acpi_device *device = NULL;
1912 acpi_object_type acpi_type;
1915 acpi_bus_get_device(handle, &device);
1919 if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
1922 switch (acpi_type) {
1923 case ACPI_TYPE_DEVICE:
1924 if (acpi_device_should_be_hidden(handle))
1927 /* Bail out if there are dependencies. */
1928 if (acpi_scan_check_dep(handle, check_dep) > 0) {
1929 acpi_bus_scan_second_pass = true;
1930 return AE_CTRL_DEPTH;
1934 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1935 type = ACPI_BUS_TYPE_DEVICE;
1938 case ACPI_TYPE_PROCESSOR:
1939 type = ACPI_BUS_TYPE_PROCESSOR;
1942 case ACPI_TYPE_THERMAL:
1943 type = ACPI_BUS_TYPE_THERMAL;
1946 case ACPI_TYPE_POWER:
1947 acpi_add_power_resource(handle);
1953 acpi_add_single_object(&device, handle, type);
1955 return AE_CTRL_DEPTH;
1957 acpi_scan_init_hotplug(device);
1959 * If check_dep is true at this point, the device has no dependencies,
1960 * or the creation of the device object would have been postponed above.
1963 device->dep_unmet = 0;
1965 acpi_scan_dep_init(device);
1974 static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
1975 void *not_used, void **ret_p)
1977 return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
1980 static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
1981 void *not_used, void **ret_p)
1983 return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
1986 static void acpi_default_enumeration(struct acpi_device *device)
1989 * Do not enumerate devices with enumeration_by_parent flag set as
1990 * they will be enumerated by their respective parents.
1992 if (!device->flags.enumeration_by_parent) {
1993 acpi_create_platform_device(device, NULL);
1994 acpi_device_set_enumerated(device);
1996 blocking_notifier_call_chain(&acpi_reconfig_chain,
1997 ACPI_RECONFIG_DEVICE_ADD, device);
2001 static const struct acpi_device_id generic_device_ids[] = {
2002 {ACPI_DT_NAMESPACE_HID, },
2006 static int acpi_generic_device_attach(struct acpi_device *adev,
2007 const struct acpi_device_id *not_used)
2010 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2011 * below can be unconditional.
2013 if (adev->data.of_compatible)
2014 acpi_default_enumeration(adev);
2019 static struct acpi_scan_handler generic_device_handler = {
2020 .ids = generic_device_ids,
2021 .attach = acpi_generic_device_attach,
2024 static int acpi_scan_attach_handler(struct acpi_device *device)
2026 struct acpi_hardware_id *hwid;
2029 list_for_each_entry(hwid, &device->pnp.ids, list) {
2030 const struct acpi_device_id *devid;
2031 struct acpi_scan_handler *handler;
2033 handler = acpi_scan_match_handler(hwid->id, &devid);
2035 if (!handler->attach) {
2036 device->pnp.type.platform_id = 0;
2039 device->handler = handler;
2040 ret = handler->attach(device, devid);
2044 device->handler = NULL;
2053 static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
2055 struct acpi_device *child;
2056 bool skip = !first_pass && device->flags.visited;
2063 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2064 register_dock_dependent_device(device, ejd);
2066 acpi_bus_get_status(device);
2067 /* Skip devices that are not present. */
2068 if (!acpi_device_is_present(device)) {
2069 device->flags.initialized = false;
2070 acpi_device_clear_enumerated(device);
2071 device->flags.power_manageable = 0;
2074 if (device->handler)
2077 if (!device->flags.initialized) {
2078 device->flags.power_manageable =
2079 device->power.states[ACPI_STATE_D0].flags.valid;
2080 if (acpi_bus_init_power(device))
2081 device->flags.power_manageable = 0;
2083 device->flags.initialized = true;
2084 } else if (device->flags.visited) {
2088 ret = acpi_scan_attach_handler(device);
2092 device->flags.match_driver = true;
2093 if (ret > 0 && !device->flags.enumeration_by_parent) {
2094 acpi_device_set_enumerated(device);
2098 ret = device_attach(&device->dev);
2102 if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2103 acpi_default_enumeration(device);
2105 acpi_device_set_enumerated(device);
2108 list_for_each_entry(child, &device->children, node)
2109 acpi_bus_attach(child, first_pass);
2111 if (!skip && device->handler && device->handler->hotplug.notify_online)
2112 device->handler->hotplug.notify_online(device);
2115 void acpi_walk_dep_device_list(acpi_handle handle)
2117 struct acpi_dep_data *dep, *tmp;
2118 struct acpi_device *adev;
2120 mutex_lock(&acpi_dep_list_lock);
2121 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2122 if (dep->supplier == handle) {
2123 acpi_bus_get_device(dep->consumer, &adev);
2127 if (!adev->dep_unmet)
2128 acpi_bus_attach(adev, true);
2131 list_del(&dep->node);
2135 mutex_unlock(&acpi_dep_list_lock);
2137 EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2140 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2141 * @handle: Root of the namespace scope to scan.
2143 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2146 * If no devices were found, -ENODEV is returned, but it does not mean that
2147 * there has been a real error. There just have been no suitable ACPI objects
2148 * in the table trunk from which the kernel could create a device and add an
2149 * appropriate driver.
2151 * Must be called under acpi_scan_lock.
2153 int acpi_bus_scan(acpi_handle handle)
2155 struct acpi_device *device = NULL;
2157 acpi_bus_scan_second_pass = false;
2159 /* Pass 1: Avoid enumerating devices with missing dependencies. */
2161 if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
2162 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2163 acpi_bus_check_add_1, NULL, NULL,
2169 acpi_bus_attach(device, true);
2171 if (!acpi_bus_scan_second_pass)
2174 /* Pass 2: Enumerate all of the remaining devices. */
2178 if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
2179 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2180 acpi_bus_check_add_2, NULL, NULL,
2183 acpi_bus_attach(device, false);
2187 EXPORT_SYMBOL(acpi_bus_scan);
2190 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2191 * @adev: Root of the ACPI namespace scope to walk.
2193 * Must be called under acpi_scan_lock.
2195 void acpi_bus_trim(struct acpi_device *adev)
2197 struct acpi_scan_handler *handler = adev->handler;
2198 struct acpi_device *child;
2200 list_for_each_entry_reverse(child, &adev->children, node)
2201 acpi_bus_trim(child);
2203 adev->flags.match_driver = false;
2205 if (handler->detach)
2206 handler->detach(adev);
2208 adev->handler = NULL;
2210 device_release_driver(&adev->dev);
2213 * Most likely, the device is going away, so put it into D3cold before
2216 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2217 adev->flags.initialized = false;
2218 acpi_device_clear_enumerated(adev);
2220 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2222 int acpi_bus_register_early_device(int type)
2224 struct acpi_device *device = NULL;
2227 result = acpi_add_single_object(&device, NULL, type);
2231 device->flags.match_driver = true;
2232 return device_attach(&device->dev);
2234 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2236 static int acpi_bus_scan_fixed(void)
2241 * Enumerate all fixed-feature devices.
2243 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2244 struct acpi_device *device = NULL;
2246 result = acpi_add_single_object(&device, NULL,
2247 ACPI_BUS_TYPE_POWER_BUTTON);
2251 device->flags.match_driver = true;
2252 result = device_attach(&device->dev);
2256 device_init_wakeup(&device->dev, true);
2259 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2260 struct acpi_device *device = NULL;
2262 result = acpi_add_single_object(&device, NULL,
2263 ACPI_BUS_TYPE_SLEEP_BUTTON);
2267 device->flags.match_driver = true;
2268 result = device_attach(&device->dev);
2271 return result < 0 ? result : 0;
2274 static void __init acpi_get_spcr_uart_addr(void)
2277 struct acpi_table_spcr *spcr_ptr;
2279 status = acpi_get_table(ACPI_SIG_SPCR, 0,
2280 (struct acpi_table_header **)&spcr_ptr);
2281 if (ACPI_FAILURE(status)) {
2282 pr_warn("STAO table present, but SPCR is missing\n");
2286 spcr_uart_addr = spcr_ptr->serial_port.address;
2287 acpi_put_table((struct acpi_table_header *)spcr_ptr);
2290 static bool acpi_scan_initialized;
2292 int __init acpi_scan_init(void)
2296 struct acpi_table_stao *stao_ptr;
2298 acpi_pci_root_init();
2299 acpi_pci_link_init();
2300 acpi_processor_init();
2301 acpi_platform_init();
2304 acpi_cmos_rtc_init();
2305 acpi_container_init();
2306 acpi_memory_hotplug_init();
2307 acpi_watchdog_init();
2309 acpi_int340x_thermal_init();
2313 acpi_scan_add_handler(&generic_device_handler);
2316 * If there is STAO table, check whether it needs to ignore the UART
2317 * device in SPCR table.
2319 status = acpi_get_table(ACPI_SIG_STAO, 0,
2320 (struct acpi_table_header **)&stao_ptr);
2321 if (ACPI_SUCCESS(status)) {
2322 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2323 pr_info("STAO Name List not yet supported.\n");
2325 if (stao_ptr->ignore_uart)
2326 acpi_get_spcr_uart_addr();
2328 acpi_put_table((struct acpi_table_header *)stao_ptr);
2331 acpi_gpe_apply_masked_gpes();
2332 acpi_update_all_gpes();
2335 * Although we call __add_memory() that is documented to require the
2336 * device_hotplug_lock, it is not necessary here because this is an
2337 * early code when userspace or any other code path cannot trigger
2338 * hotplug/hotunplug operations.
2340 mutex_lock(&acpi_scan_lock);
2342 * Enumerate devices in the ACPI namespace.
2344 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2348 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2352 /* Fixed feature devices do not exist on HW-reduced platform */
2353 if (!acpi_gbl_reduced_hardware) {
2354 result = acpi_bus_scan_fixed();
2356 acpi_detach_data(acpi_root->handle,
2357 acpi_scan_drop_device);
2358 acpi_device_del(acpi_root);
2359 acpi_bus_put_acpi_device(acpi_root);
2364 acpi_turn_off_unused_power_resources(true);
2366 acpi_scan_initialized = true;
2369 mutex_unlock(&acpi_scan_lock);
2373 static struct acpi_probe_entry *ape;
2374 static int acpi_probe_count;
2375 static DEFINE_MUTEX(acpi_probe_mutex);
2377 static int __init acpi_match_madt(union acpi_subtable_headers *header,
2378 const unsigned long end)
2380 if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2381 if (!ape->probe_subtbl(header, end))
2387 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2394 mutex_lock(&acpi_probe_mutex);
2395 for (ape = ap_head; nr; ape++, nr--) {
2396 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2397 acpi_probe_count = 0;
2398 acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2399 count += acpi_probe_count;
2402 res = acpi_table_parse(ape->id, ape->probe_table);
2407 mutex_unlock(&acpi_probe_mutex);
2412 struct acpi_table_events_work {
2413 struct work_struct work;
2418 static void acpi_table_events_fn(struct work_struct *work)
2420 struct acpi_table_events_work *tew;
2422 tew = container_of(work, struct acpi_table_events_work, work);
2424 if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2425 acpi_scan_lock_acquire();
2426 acpi_bus_scan(ACPI_ROOT_OBJECT);
2427 acpi_scan_lock_release();
2433 void acpi_scan_table_handler(u32 event, void *table, void *context)
2435 struct acpi_table_events_work *tew;
2437 if (!acpi_scan_initialized)
2440 if (event != ACPI_TABLE_EVENT_LOAD)
2443 tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2447 INIT_WORK(&tew->work, acpi_table_events_fn);
2451 schedule_work(&tew->work);
2454 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2456 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2458 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2460 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2462 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2464 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);