1 // SPDX-License-Identifier: GPL-2.0-only
3 * scan.c - support for transforming the ACPI namespace into individual objects
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/kernel.h>
10 #include <linux/acpi.h>
11 #include <linux/acpi_iort.h>
12 #include <linux/signal.h>
13 #include <linux/kthread.h>
14 #include <linux/dmi.h>
15 #include <linux/nls.h>
16 #include <linux/dma-map-ops.h>
17 #include <linux/platform_data/x86/apple.h>
18 #include <linux/pgtable.h>
22 extern struct acpi_device *acpi_root;
24 #define ACPI_BUS_CLASS "system_bus"
25 #define ACPI_BUS_HID "LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME "System Bus"
28 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
30 #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
32 static const char *dummy_hid = "device";
34 static LIST_HEAD(acpi_dep_list);
35 static DEFINE_MUTEX(acpi_dep_list_lock);
36 LIST_HEAD(acpi_bus_id_list);
37 static DEFINE_MUTEX(acpi_scan_lock);
38 static LIST_HEAD(acpi_scan_handlers_list);
39 DEFINE_MUTEX(acpi_device_lock);
40 LIST_HEAD(acpi_wakeup_device_list);
41 static DEFINE_MUTEX(acpi_hp_context_lock);
44 * The UART device described by the SPCR table is the only object which needs
45 * special-casing. Everything else is covered by ACPI namespace paths in STAO
48 static u64 spcr_uart_addr;
50 struct acpi_dep_data {
51 struct list_head node;
56 void acpi_scan_lock_acquire(void)
58 mutex_lock(&acpi_scan_lock);
60 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
62 void acpi_scan_lock_release(void)
64 mutex_unlock(&acpi_scan_lock);
66 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
68 void acpi_lock_hp_context(void)
70 mutex_lock(&acpi_hp_context_lock);
73 void acpi_unlock_hp_context(void)
75 mutex_unlock(&acpi_hp_context_lock);
78 void acpi_initialize_hp_context(struct acpi_device *adev,
79 struct acpi_hotplug_context *hp,
80 int (*notify)(struct acpi_device *, u32),
81 void (*uevent)(struct acpi_device *, u32))
83 acpi_lock_hp_context();
86 acpi_set_hp_context(adev, hp);
87 acpi_unlock_hp_context();
89 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
91 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
96 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
100 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
101 const char *hotplug_profile_name)
105 error = acpi_scan_add_handler(handler);
109 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
113 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
115 struct acpi_device_physical_node *pn;
117 char *envp[] = { "EVENT=offline", NULL };
120 * acpi_container_offline() calls this for all of the container's
121 * children under the container's physical_node_lock lock.
123 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
125 list_for_each_entry(pn, &adev->physical_node_list, node)
126 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
128 kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
134 mutex_unlock(&adev->physical_node_lock);
138 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
141 struct acpi_device *device = NULL;
142 struct acpi_device_physical_node *pn;
143 bool second_pass = (bool)data;
144 acpi_status status = AE_OK;
146 if (acpi_bus_get_device(handle, &device))
149 if (device->handler && !device->handler->hotplug.enabled) {
150 *ret_p = &device->dev;
154 mutex_lock(&device->physical_node_lock);
156 list_for_each_entry(pn, &device->physical_node_list, node) {
160 /* Skip devices offlined by the first pass. */
164 pn->put_online = false;
166 ret = device_offline(pn->dev);
168 pn->put_online = !ret;
178 mutex_unlock(&device->physical_node_lock);
183 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
186 struct acpi_device *device = NULL;
187 struct acpi_device_physical_node *pn;
189 if (acpi_bus_get_device(handle, &device))
192 mutex_lock(&device->physical_node_lock);
194 list_for_each_entry(pn, &device->physical_node_list, node)
195 if (pn->put_online) {
196 device_online(pn->dev);
197 pn->put_online = false;
200 mutex_unlock(&device->physical_node_lock);
205 static int acpi_scan_try_to_offline(struct acpi_device *device)
207 acpi_handle handle = device->handle;
208 struct device *errdev = NULL;
212 * Carry out two passes here and ignore errors in the first pass,
213 * because if the devices in question are memory blocks and
214 * CONFIG_MEMCG is set, one of the blocks may hold data structures
215 * that the other blocks depend on, but it is not known in advance which
218 * If the first pass is successful, the second one isn't needed, though.
220 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
221 NULL, acpi_bus_offline, (void *)false,
223 if (status == AE_SUPPORT) {
224 dev_warn(errdev, "Offline disabled.\n");
225 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
226 acpi_bus_online, NULL, NULL, NULL);
229 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
232 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
233 NULL, acpi_bus_offline, (void *)true,
236 acpi_bus_offline(handle, 0, (void *)true,
240 dev_warn(errdev, "Offline failed.\n");
241 acpi_bus_online(handle, 0, NULL, NULL);
242 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
243 ACPI_UINT32_MAX, acpi_bus_online,
251 static int acpi_scan_hot_remove(struct acpi_device *device)
253 acpi_handle handle = device->handle;
254 unsigned long long sta;
257 if (device->handler && device->handler->hotplug.demand_offline) {
258 if (!acpi_scan_is_offline(device, true))
261 int error = acpi_scan_try_to_offline(device);
266 acpi_handle_debug(handle, "Ejecting\n");
268 acpi_bus_trim(device);
270 acpi_evaluate_lck(handle, 0);
274 status = acpi_evaluate_ej0(handle);
275 if (status == AE_NOT_FOUND)
277 else if (ACPI_FAILURE(status))
281 * Verify if eject was indeed successful. If not, log an error
282 * message. No need to call _OST since _EJ0 call was made OK.
284 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
285 if (ACPI_FAILURE(status)) {
286 acpi_handle_warn(handle,
287 "Status check after eject failed (0x%x)\n", status);
288 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
289 acpi_handle_warn(handle,
290 "Eject incomplete - status 0x%llx\n", sta);
296 static int acpi_scan_device_not_present(struct acpi_device *adev)
298 if (!acpi_device_enumerated(adev)) {
299 dev_warn(&adev->dev, "Still not present\n");
306 static int acpi_scan_device_check(struct acpi_device *adev)
310 acpi_bus_get_status(adev);
311 if (adev->status.present || adev->status.functional) {
313 * This function is only called for device objects for which
314 * matching scan handlers exist. The only situation in which
315 * the scan handler is not attached to this device object yet
316 * is when the device has just appeared (either it wasn't
317 * present at all before or it was removed and then added
321 dev_warn(&adev->dev, "Already enumerated\n");
324 error = acpi_bus_scan(adev->handle);
326 dev_warn(&adev->dev, "Namespace scan failure\n");
329 if (!adev->handler) {
330 dev_warn(&adev->dev, "Enumeration failure\n");
334 error = acpi_scan_device_not_present(adev);
339 static int acpi_scan_bus_check(struct acpi_device *adev)
341 struct acpi_scan_handler *handler = adev->handler;
342 struct acpi_device *child;
345 acpi_bus_get_status(adev);
346 if (!(adev->status.present || adev->status.functional)) {
347 acpi_scan_device_not_present(adev);
350 if (handler && handler->hotplug.scan_dependent)
351 return handler->hotplug.scan_dependent(adev);
353 error = acpi_bus_scan(adev->handle);
355 dev_warn(&adev->dev, "Namespace scan failure\n");
358 list_for_each_entry(child, &adev->children, node) {
359 error = acpi_scan_bus_check(child);
366 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
369 case ACPI_NOTIFY_BUS_CHECK:
370 return acpi_scan_bus_check(adev);
371 case ACPI_NOTIFY_DEVICE_CHECK:
372 return acpi_scan_device_check(adev);
373 case ACPI_NOTIFY_EJECT_REQUEST:
374 case ACPI_OST_EC_OSPM_EJECT:
375 if (adev->handler && !adev->handler->hotplug.enabled) {
376 dev_info(&adev->dev, "Eject disabled\n");
379 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
380 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
381 return acpi_scan_hot_remove(adev);
386 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
388 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
391 lock_device_hotplug();
392 mutex_lock(&acpi_scan_lock);
395 * The device object's ACPI handle cannot become invalid as long as we
396 * are holding acpi_scan_lock, but it might have become invalid before
397 * that lock was acquired.
399 if (adev->handle == INVALID_ACPI_HANDLE)
402 if (adev->flags.is_dock_station) {
403 error = dock_notify(adev, src);
404 } else if (adev->flags.hotplug_notify) {
405 error = acpi_generic_hotplug_event(adev, src);
407 int (*notify)(struct acpi_device *, u32);
409 acpi_lock_hp_context();
410 notify = adev->hp ? adev->hp->notify : NULL;
411 acpi_unlock_hp_context();
413 * There may be additional notify handlers for device objects
414 * without the .event() callback, so ignore them here.
417 error = notify(adev, src);
423 ost_code = ACPI_OST_SC_SUCCESS;
426 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
429 ost_code = ACPI_OST_SC_DEVICE_BUSY;
432 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
437 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
440 acpi_bus_put_acpi_device(adev);
441 mutex_unlock(&acpi_scan_lock);
442 unlock_device_hotplug();
445 static void acpi_free_power_resources_lists(struct acpi_device *device)
449 if (device->wakeup.flags.valid)
450 acpi_power_resources_list_free(&device->wakeup.resources);
452 if (!device->power.flags.power_resources)
455 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
456 struct acpi_device_power_state *ps = &device->power.states[i];
457 acpi_power_resources_list_free(&ps->resources);
461 static void acpi_device_release(struct device *dev)
463 struct acpi_device *acpi_dev = to_acpi_device(dev);
465 acpi_free_properties(acpi_dev);
466 acpi_free_pnp_ids(&acpi_dev->pnp);
467 acpi_free_power_resources_lists(acpi_dev);
471 static void acpi_device_del(struct acpi_device *device)
473 struct acpi_device_bus_id *acpi_device_bus_id;
475 mutex_lock(&acpi_device_lock);
477 list_del(&device->node);
479 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
480 if (!strcmp(acpi_device_bus_id->bus_id,
481 acpi_device_hid(device))) {
482 ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
483 if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
484 list_del(&acpi_device_bus_id->node);
485 kfree_const(acpi_device_bus_id->bus_id);
486 kfree(acpi_device_bus_id);
491 list_del(&device->wakeup_list);
492 mutex_unlock(&acpi_device_lock);
494 acpi_power_add_remove_device(device, false);
495 acpi_device_remove_files(device);
497 device->remove(device);
499 device_del(&device->dev);
502 static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
504 static LIST_HEAD(acpi_device_del_list);
505 static DEFINE_MUTEX(acpi_device_del_lock);
507 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
510 struct acpi_device *adev;
512 mutex_lock(&acpi_device_del_lock);
514 if (list_empty(&acpi_device_del_list)) {
515 mutex_unlock(&acpi_device_del_lock);
518 adev = list_first_entry(&acpi_device_del_list,
519 struct acpi_device, del_list);
520 list_del(&adev->del_list);
522 mutex_unlock(&acpi_device_del_lock);
524 blocking_notifier_call_chain(&acpi_reconfig_chain,
525 ACPI_RECONFIG_DEVICE_REMOVE, adev);
527 acpi_device_del(adev);
529 * Drop references to all power resources that might have been
530 * used by the device.
532 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
538 * acpi_scan_drop_device - Drop an ACPI device object.
539 * @handle: Handle of an ACPI namespace node, not used.
540 * @context: Address of the ACPI device object to drop.
542 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
543 * namespace node the device object pointed to by @context is attached to.
545 * The unregistration is carried out asynchronously to avoid running
546 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
547 * ensure the correct ordering (the device objects must be unregistered in the
548 * same order in which the corresponding namespace nodes are deleted).
550 static void acpi_scan_drop_device(acpi_handle handle, void *context)
552 static DECLARE_WORK(work, acpi_device_del_work_fn);
553 struct acpi_device *adev = context;
555 mutex_lock(&acpi_device_del_lock);
558 * Use the ACPI hotplug workqueue which is ordered, so this work item
559 * won't run after any hotplug work items submitted subsequently. That
560 * prevents attempts to register device objects identical to those being
561 * deleted from happening concurrently (such attempts result from
562 * hotplug events handled via the ACPI hotplug workqueue). It also will
563 * run after all of the work items submitted previously, which helps
564 * those work items to ensure that they are not accessing stale device
567 if (list_empty(&acpi_device_del_list))
568 acpi_queue_hotplug_work(&work);
570 list_add_tail(&adev->del_list, &acpi_device_del_list);
571 /* Make acpi_ns_validate_handle() return NULL for this handle. */
572 adev->handle = INVALID_ACPI_HANDLE;
574 mutex_unlock(&acpi_device_del_lock);
577 static struct acpi_device *handle_to_device(acpi_handle handle,
578 void (*callback)(void *))
580 struct acpi_device *adev = NULL;
583 status = acpi_get_data_full(handle, acpi_scan_drop_device,
584 (void **)&adev, callback);
585 if (ACPI_FAILURE(status) || !adev) {
586 acpi_handle_debug(handle, "No context!\n");
592 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
597 *device = handle_to_device(handle, NULL);
603 EXPORT_SYMBOL(acpi_bus_get_device);
605 static void get_acpi_device(void *dev)
610 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
612 return handle_to_device(handle, get_acpi_device);
615 void acpi_bus_put_acpi_device(struct acpi_device *adev)
620 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
622 struct acpi_device_bus_id *acpi_device_bus_id;
624 /* Find suitable bus_id and instance number in acpi_bus_id_list. */
625 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
626 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
627 return acpi_device_bus_id;
632 static int acpi_device_set_name(struct acpi_device *device,
633 struct acpi_device_bus_id *acpi_device_bus_id)
635 struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
638 result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
642 device->pnp.instance_no = result;
643 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
647 int acpi_device_add(struct acpi_device *device,
648 void (*release)(struct device *))
650 struct acpi_device_bus_id *acpi_device_bus_id;
653 if (device->handle) {
656 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
658 if (ACPI_FAILURE(status)) {
659 acpi_handle_err(device->handle,
660 "Unable to attach device data\n");
668 * Link this device to its parent and siblings.
670 INIT_LIST_HEAD(&device->children);
671 INIT_LIST_HEAD(&device->node);
672 INIT_LIST_HEAD(&device->wakeup_list);
673 INIT_LIST_HEAD(&device->physical_node_list);
674 INIT_LIST_HEAD(&device->del_list);
675 mutex_init(&device->physical_node_lock);
677 mutex_lock(&acpi_device_lock);
679 acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
680 if (acpi_device_bus_id) {
681 result = acpi_device_set_name(device, acpi_device_bus_id);
685 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
687 if (!acpi_device_bus_id) {
691 acpi_device_bus_id->bus_id =
692 kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
693 if (!acpi_device_bus_id->bus_id) {
694 kfree(acpi_device_bus_id);
699 ida_init(&acpi_device_bus_id->instance_ida);
701 result = acpi_device_set_name(device, acpi_device_bus_id);
703 kfree(acpi_device_bus_id);
707 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
711 list_add_tail(&device->node, &device->parent->children);
713 if (device->wakeup.flags.valid)
714 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
716 mutex_unlock(&acpi_device_lock);
719 device->dev.parent = &device->parent->dev;
721 device->dev.bus = &acpi_bus_type;
722 device->dev.release = release;
723 result = device_add(&device->dev);
725 dev_err(&device->dev, "Error registering device\n");
729 result = acpi_device_setup_files(device);
731 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
732 dev_name(&device->dev));
737 mutex_lock(&acpi_device_lock);
740 list_del(&device->node);
742 list_del(&device->wakeup_list);
745 mutex_unlock(&acpi_device_lock);
747 acpi_detach_data(device->handle, acpi_scan_drop_device);
752 /* --------------------------------------------------------------------------
754 -------------------------------------------------------------------------- */
755 static bool acpi_info_matches_ids(struct acpi_device_info *info,
756 const char * const ids[])
758 struct acpi_pnp_device_id_list *cid_list = NULL;
761 if (!(info->valid & ACPI_VALID_HID))
764 index = match_string(ids, -1, info->hardware_id.string);
768 if (info->valid & ACPI_VALID_CID)
769 cid_list = &info->compatible_id_list;
774 for (i = 0; i < cid_list->count; i++) {
775 index = match_string(ids, -1, cid_list->ids[i].string);
783 /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
784 static const char * const acpi_ignore_dep_ids[] = {
785 "PNP0D80", /* Windows-compatible System Power Management Controller */
786 "INT33BD", /* Intel Baytrail Mailbox Device */
790 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
792 struct acpi_device *device = NULL;
796 * Fixed hardware devices do not appear in the namespace and do not
797 * have handles, but we fabricate acpi_devices for them, so we have
798 * to deal with them specially.
804 status = acpi_get_parent(handle, &handle);
805 if (ACPI_FAILURE(status))
806 return status == AE_NULL_ENTRY ? NULL : acpi_root;
807 } while (acpi_bus_get_device(handle, &device));
812 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
816 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
817 union acpi_object *obj;
819 status = acpi_get_handle(handle, "_EJD", &tmp);
820 if (ACPI_FAILURE(status))
823 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
824 if (ACPI_SUCCESS(status)) {
825 obj = buffer.pointer;
826 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
828 kfree(buffer.pointer);
832 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
834 static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
836 acpi_handle handle = dev->handle;
837 struct acpi_device_wakeup *wakeup = &dev->wakeup;
838 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
839 union acpi_object *package = NULL;
840 union acpi_object *element = NULL;
844 INIT_LIST_HEAD(&wakeup->resources);
847 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
848 if (ACPI_FAILURE(status)) {
849 acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
850 acpi_format_exception(status));
854 package = (union acpi_object *)buffer.pointer;
856 if (!package || package->package.count < 2)
859 element = &(package->package.elements[0]);
863 if (element->type == ACPI_TYPE_PACKAGE) {
864 if ((element->package.count < 2) ||
865 (element->package.elements[0].type !=
866 ACPI_TYPE_LOCAL_REFERENCE)
867 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
871 element->package.elements[0].reference.handle;
873 (u32) element->package.elements[1].integer.value;
874 } else if (element->type == ACPI_TYPE_INTEGER) {
875 wakeup->gpe_device = NULL;
876 wakeup->gpe_number = element->integer.value;
881 element = &(package->package.elements[1]);
882 if (element->type != ACPI_TYPE_INTEGER)
885 wakeup->sleep_state = element->integer.value;
887 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
891 if (!list_empty(&wakeup->resources)) {
894 err = acpi_power_wakeup_list_init(&wakeup->resources,
897 acpi_handle_warn(handle, "Retrieving current states "
898 "of wakeup power resources failed\n");
899 acpi_power_resources_list_free(&wakeup->resources);
902 if (sleep_state < wakeup->sleep_state) {
903 acpi_handle_warn(handle, "Overriding _PRW sleep state "
904 "(S%d) by S%d from power resources\n",
905 (int)wakeup->sleep_state, sleep_state);
906 wakeup->sleep_state = sleep_state;
911 kfree(buffer.pointer);
915 static bool acpi_wakeup_gpe_init(struct acpi_device *device)
917 static const struct acpi_device_id button_device_ids[] = {
918 {"PNP0C0C", 0}, /* Power button */
919 {"PNP0C0D", 0}, /* Lid */
920 {"PNP0C0E", 0}, /* Sleep button */
923 struct acpi_device_wakeup *wakeup = &device->wakeup;
926 wakeup->flags.notifier_present = 0;
928 /* Power button, Lid switch always enable wakeup */
929 if (!acpi_match_device_ids(device, button_device_ids)) {
930 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
931 /* Do not use Lid/sleep button for S5 wakeup */
932 if (wakeup->sleep_state == ACPI_STATE_S5)
933 wakeup->sleep_state = ACPI_STATE_S4;
935 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
936 device_set_wakeup_capable(&device->dev, true);
940 status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
942 return ACPI_SUCCESS(status);
945 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
949 /* Presence of _PRW indicates wake capable */
950 if (!acpi_has_method(device->handle, "_PRW"))
953 err = acpi_bus_extract_wakeup_device_power_package(device);
955 dev_err(&device->dev, "Unable to extract wakeup power resources");
959 device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
960 device->wakeup.prepare_count = 0;
962 * Call _PSW/_DSW object to disable its ability to wake the sleeping
963 * system for the ACPI device with the _PRW object.
964 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
965 * So it is necessary to call _DSW object first. Only when it is not
966 * present will the _PSW object used.
968 err = acpi_device_sleep_wake(device, 0, 0, 0);
970 pr_debug("error in _DSW or _PSW evaluation\n");
973 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
975 struct acpi_device_power_state *ps = &device->power.states[state];
976 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
977 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
980 INIT_LIST_HEAD(&ps->resources);
982 /* Evaluate "_PRx" to get referenced power resources */
983 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
984 if (ACPI_SUCCESS(status)) {
985 union acpi_object *package = buffer.pointer;
987 if (buffer.length && package
988 && package->type == ACPI_TYPE_PACKAGE
989 && package->package.count)
990 acpi_extract_power_resources(package, 0, &ps->resources);
992 ACPI_FREE(buffer.pointer);
995 /* Evaluate "_PSx" to see if we can do explicit sets */
997 if (acpi_has_method(device->handle, pathname))
998 ps->flags.explicit_set = 1;
1000 /* State is valid if there are means to put the device into it. */
1001 if (!list_empty(&ps->resources) || ps->flags.explicit_set)
1002 ps->flags.valid = 1;
1004 ps->power = -1; /* Unknown - driver assigned */
1005 ps->latency = -1; /* Unknown - driver assigned */
1008 static void acpi_bus_get_power_flags(struct acpi_device *device)
1012 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1013 if (!acpi_has_method(device->handle, "_PS0") &&
1014 !acpi_has_method(device->handle, "_PR0"))
1017 device->flags.power_manageable = 1;
1020 * Power Management Flags
1022 if (acpi_has_method(device->handle, "_PSC"))
1023 device->power.flags.explicit_get = 1;
1025 if (acpi_has_method(device->handle, "_IRC"))
1026 device->power.flags.inrush_current = 1;
1028 if (acpi_has_method(device->handle, "_DSW"))
1029 device->power.flags.dsw_present = 1;
1032 * Enumerate supported power management states
1034 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1035 acpi_bus_init_power_state(device, i);
1037 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1039 /* Set the defaults for D0 and D3hot (always supported). */
1040 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1041 device->power.states[ACPI_STATE_D0].power = 100;
1042 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1045 * Use power resources only if the D0 list of them is populated, because
1046 * some platforms may provide _PR3 only to indicate D3cold support and
1047 * in those cases the power resources list returned by it may be bogus.
1049 if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1050 device->power.flags.power_resources = 1;
1052 * D3cold is supported if the D3hot list of power resources is
1055 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1056 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1059 if (acpi_bus_init_power(device))
1060 device->flags.power_manageable = 0;
1063 static void acpi_bus_get_flags(struct acpi_device *device)
1065 /* Presence of _STA indicates 'dynamic_status' */
1066 if (acpi_has_method(device->handle, "_STA"))
1067 device->flags.dynamic_status = 1;
1069 /* Presence of _RMV indicates 'removable' */
1070 if (acpi_has_method(device->handle, "_RMV"))
1071 device->flags.removable = 1;
1073 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1074 if (acpi_has_method(device->handle, "_EJD") ||
1075 acpi_has_method(device->handle, "_EJ0"))
1076 device->flags.ejectable = 1;
1079 static void acpi_device_get_busid(struct acpi_device *device)
1081 char bus_id[5] = { '?', 0 };
1082 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1088 * The device's Bus ID is simply the object name.
1089 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1091 if (ACPI_IS_ROOT_DEVICE(device)) {
1092 strcpy(device->pnp.bus_id, "ACPI");
1096 switch (device->device_type) {
1097 case ACPI_BUS_TYPE_POWER_BUTTON:
1098 strcpy(device->pnp.bus_id, "PWRF");
1100 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1101 strcpy(device->pnp.bus_id, "SLPF");
1103 case ACPI_BUS_TYPE_ECDT_EC:
1104 strcpy(device->pnp.bus_id, "ECDT");
1107 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1108 /* Clean up trailing underscores (if any) */
1109 for (i = 3; i > 1; i--) {
1110 if (bus_id[i] == '_')
1115 strcpy(device->pnp.bus_id, bus_id);
1121 * acpi_ata_match - see if an acpi object is an ATA device
1123 * If an acpi object has one of the ACPI ATA methods defined,
1124 * then we can safely call it an ATA device.
1126 bool acpi_ata_match(acpi_handle handle)
1128 return acpi_has_method(handle, "_GTF") ||
1129 acpi_has_method(handle, "_GTM") ||
1130 acpi_has_method(handle, "_STM") ||
1131 acpi_has_method(handle, "_SDD");
1135 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1137 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1138 * then we can safely call it an ejectable drive bay
1140 bool acpi_bay_match(acpi_handle handle)
1142 acpi_handle phandle;
1144 if (!acpi_has_method(handle, "_EJ0"))
1146 if (acpi_ata_match(handle))
1148 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1151 return acpi_ata_match(phandle);
1154 bool acpi_device_is_battery(struct acpi_device *adev)
1156 struct acpi_hardware_id *hwid;
1158 list_for_each_entry(hwid, &adev->pnp.ids, list)
1159 if (!strcmp("PNP0C0A", hwid->id))
1165 static bool is_ejectable_bay(struct acpi_device *adev)
1167 acpi_handle handle = adev->handle;
1169 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1172 return acpi_bay_match(handle);
1176 * acpi_dock_match - see if an acpi object has a _DCK method
1178 bool acpi_dock_match(acpi_handle handle)
1180 return acpi_has_method(handle, "_DCK");
1184 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1185 void **return_value)
1187 long *cap = context;
1189 if (acpi_has_method(handle, "_BCM") &&
1190 acpi_has_method(handle, "_BCL")) {
1191 acpi_handle_debug(handle, "Found generic backlight support\n");
1192 *cap |= ACPI_VIDEO_BACKLIGHT;
1193 /* We have backlight support, no need to scan further */
1194 return AE_CTRL_TERMINATE;
1199 /* Returns true if the ACPI object is a video device which can be
1200 * handled by video.ko.
1201 * The device will get a Linux specific CID added in scan.c to
1202 * identify the device as an ACPI graphics device
1203 * Be aware that the graphics device may not be physically present
1204 * Use acpi_video_get_capabilities() to detect general ACPI video
1205 * capabilities of present cards
1207 long acpi_is_video_device(acpi_handle handle)
1209 long video_caps = 0;
1211 /* Is this device able to support video switching ? */
1212 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1213 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1215 /* Is this device able to retrieve a video ROM ? */
1216 if (acpi_has_method(handle, "_ROM"))
1217 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1219 /* Is this device able to configure which video head to be POSTed ? */
1220 if (acpi_has_method(handle, "_VPO") &&
1221 acpi_has_method(handle, "_GPD") &&
1222 acpi_has_method(handle, "_SPD"))
1223 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1225 /* Only check for backlight functionality if one of the above hit. */
1227 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1228 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1233 EXPORT_SYMBOL(acpi_is_video_device);
1235 const char *acpi_device_hid(struct acpi_device *device)
1237 struct acpi_hardware_id *hid;
1239 if (list_empty(&device->pnp.ids))
1242 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1245 EXPORT_SYMBOL(acpi_device_hid);
1247 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1249 struct acpi_hardware_id *id;
1251 id = kmalloc(sizeof(*id), GFP_KERNEL);
1255 id->id = kstrdup_const(dev_id, GFP_KERNEL);
1261 list_add_tail(&id->list, &pnp->ids);
1262 pnp->type.hardware_id = 1;
1266 * Old IBM workstations have a DSDT bug wherein the SMBus object
1267 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1268 * prefix. Work around this.
1270 static bool acpi_ibm_smbus_match(acpi_handle handle)
1272 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1273 struct acpi_buffer path = { sizeof(node_name), node_name };
1275 if (!dmi_name_in_vendors("IBM"))
1278 /* Look for SMBS object */
1279 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1280 strcmp("SMBS", path.pointer))
1283 /* Does it have the necessary (but misnamed) methods? */
1284 if (acpi_has_method(handle, "SBI") &&
1285 acpi_has_method(handle, "SBR") &&
1286 acpi_has_method(handle, "SBW"))
1292 static bool acpi_object_is_system_bus(acpi_handle handle)
1296 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1299 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1306 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1309 struct acpi_device_info *info = NULL;
1310 struct acpi_pnp_device_id_list *cid_list;
1313 switch (device_type) {
1314 case ACPI_BUS_TYPE_DEVICE:
1315 if (handle == ACPI_ROOT_OBJECT) {
1316 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1320 acpi_get_object_info(handle, &info);
1322 pr_err(PREFIX "%s: Error reading device info\n",
1327 if (info->valid & ACPI_VALID_HID) {
1328 acpi_add_id(pnp, info->hardware_id.string);
1329 pnp->type.platform_id = 1;
1331 if (info->valid & ACPI_VALID_CID) {
1332 cid_list = &info->compatible_id_list;
1333 for (i = 0; i < cid_list->count; i++)
1334 acpi_add_id(pnp, cid_list->ids[i].string);
1336 if (info->valid & ACPI_VALID_ADR) {
1337 pnp->bus_address = info->address;
1338 pnp->type.bus_address = 1;
1340 if (info->valid & ACPI_VALID_UID)
1341 pnp->unique_id = kstrdup(info->unique_id.string,
1343 if (info->valid & ACPI_VALID_CLS)
1344 acpi_add_id(pnp, info->class_code.string);
1349 * Some devices don't reliably have _HIDs & _CIDs, so add
1350 * synthetic HIDs to make sure drivers can find them.
1352 if (acpi_is_video_device(handle))
1353 acpi_add_id(pnp, ACPI_VIDEO_HID);
1354 else if (acpi_bay_match(handle))
1355 acpi_add_id(pnp, ACPI_BAY_HID);
1356 else if (acpi_dock_match(handle))
1357 acpi_add_id(pnp, ACPI_DOCK_HID);
1358 else if (acpi_ibm_smbus_match(handle))
1359 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1360 else if (list_empty(&pnp->ids) &&
1361 acpi_object_is_system_bus(handle)) {
1362 /* \_SB, \_TZ, LNXSYBUS */
1363 acpi_add_id(pnp, ACPI_BUS_HID);
1364 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1365 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1369 case ACPI_BUS_TYPE_POWER:
1370 acpi_add_id(pnp, ACPI_POWER_HID);
1372 case ACPI_BUS_TYPE_PROCESSOR:
1373 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1375 case ACPI_BUS_TYPE_THERMAL:
1376 acpi_add_id(pnp, ACPI_THERMAL_HID);
1378 case ACPI_BUS_TYPE_POWER_BUTTON:
1379 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1381 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1382 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1384 case ACPI_BUS_TYPE_ECDT_EC:
1385 acpi_add_id(pnp, ACPI_ECDT_HID);
1390 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1392 struct acpi_hardware_id *id, *tmp;
1394 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1395 kfree_const(id->id);
1398 kfree(pnp->unique_id);
1402 * acpi_dma_supported - Check DMA support for the specified device.
1403 * @adev: The pointer to acpi device
1405 * Return false if DMA is not supported. Otherwise, return true
1407 bool acpi_dma_supported(struct acpi_device *adev)
1412 if (adev->flags.cca_seen)
1416 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1417 * DMA on "Intel platforms". Presumably that includes all x86 and
1418 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1420 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1427 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1428 * @adev: The pointer to acpi device
1430 * Return enum dev_dma_attr.
1432 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1434 if (!acpi_dma_supported(adev))
1435 return DEV_DMA_NOT_SUPPORTED;
1437 if (adev->flags.coherent_dma)
1438 return DEV_DMA_COHERENT;
1440 return DEV_DMA_NON_COHERENT;
1444 * acpi_dma_get_range() - Get device DMA parameters.
1446 * @dev: device to configure
1447 * @dma_addr: pointer device DMA address result
1448 * @offset: pointer to the DMA offset result
1449 * @size: pointer to DMA range size result
1451 * Evaluate DMA regions and return respectively DMA region start, offset
1452 * and size in dma_addr, offset and size on parsing success; it does not
1453 * update the passed in values on failure.
1455 * Return 0 on success, < 0 on failure.
1457 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1460 struct acpi_device *adev;
1462 struct resource_entry *rentry;
1464 struct device *dma_dev = dev;
1465 u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1468 * Walk the device tree chasing an ACPI companion with a _DMA
1469 * object while we go. Stop if we find a device with an ACPI
1470 * companion containing a _DMA method.
1473 adev = ACPI_COMPANION(dma_dev);
1474 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1477 dma_dev = dma_dev->parent;
1483 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1484 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1488 ret = acpi_dev_get_dma_resources(adev, &list);
1490 list_for_each_entry(rentry, &list, node) {
1491 if (dma_offset && rentry->offset != dma_offset) {
1493 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1496 dma_offset = rentry->offset;
1498 /* Take lower and upper limits */
1499 if (rentry->res->start < dma_start)
1500 dma_start = rentry->res->start;
1501 if (rentry->res->end > dma_end)
1502 dma_end = rentry->res->end;
1505 if (dma_start >= dma_end) {
1507 dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1511 *dma_addr = dma_start - dma_offset;
1512 len = dma_end - dma_start;
1513 *size = max(len, len + 1);
1514 *offset = dma_offset;
1517 acpi_dev_free_resource_list(&list);
1519 return ret >= 0 ? 0 : ret;
1523 * acpi_dma_configure_id - Set-up DMA configuration for the device.
1524 * @dev: The pointer to the device
1525 * @attr: device dma attributes
1526 * @input_id: input device id const value pointer
1528 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1529 const u32 *input_id)
1531 const struct iommu_ops *iommu;
1532 u64 dma_addr = 0, size = 0;
1534 if (attr == DEV_DMA_NOT_SUPPORTED) {
1535 set_dma_ops(dev, &dma_dummy_ops);
1539 iort_dma_setup(dev, &dma_addr, &size);
1541 iommu = iort_iommu_configure_id(dev, input_id);
1542 if (PTR_ERR(iommu) == -EPROBE_DEFER)
1543 return -EPROBE_DEFER;
1545 arch_setup_dma_ops(dev, dma_addr, size,
1546 iommu, attr == DEV_DMA_COHERENT);
1550 EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1552 static void acpi_init_coherency(struct acpi_device *adev)
1554 unsigned long long cca = 0;
1556 struct acpi_device *parent = adev->parent;
1558 if (parent && parent->flags.cca_seen) {
1560 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1563 adev->flags.cca_seen = 1;
1564 cca = parent->flags.coherent_dma;
1566 status = acpi_evaluate_integer(adev->handle, "_CCA",
1568 if (ACPI_SUCCESS(status))
1569 adev->flags.cca_seen = 1;
1570 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1572 * If architecture does not specify that _CCA is
1573 * required for DMA-able devices (e.g. x86),
1574 * we default to _CCA=1.
1578 acpi_handle_debug(adev->handle,
1579 "ACPI device is missing _CCA.\n");
1582 adev->flags.coherent_dma = cca;
1585 static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1587 bool *is_serial_bus_slave_p = data;
1589 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1592 *is_serial_bus_slave_p = true;
1594 /* no need to do more checking */
1598 static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1600 struct acpi_device *parent = device->parent;
1601 static const struct acpi_device_id indirect_io_hosts[] = {
1606 return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1609 static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1611 struct list_head resource_list;
1612 bool is_serial_bus_slave = false;
1614 * These devices have multiple I2cSerialBus resources and an i2c-client
1615 * must be instantiated for each, each with its own i2c_device_id.
1616 * Normally we only instantiate an i2c-client for the first resource,
1617 * using the ACPI HID as id. These special cases are handled by the
1618 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1619 * which i2c_device_id to use for each resource.
1621 static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1629 if (acpi_is_indirect_io_slave(device))
1632 /* Macs use device properties in lieu of _CRS resources */
1633 if (x86_apple_machine &&
1634 (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1635 fwnode_property_present(&device->fwnode, "i2cAddress") ||
1636 fwnode_property_present(&device->fwnode, "baud")))
1639 /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1640 if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1643 INIT_LIST_HEAD(&resource_list);
1644 acpi_dev_get_resources(device, &resource_list,
1645 acpi_check_serial_bus_slave,
1646 &is_serial_bus_slave);
1647 acpi_dev_free_resource_list(&resource_list);
1649 return is_serial_bus_slave;
1652 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1655 INIT_LIST_HEAD(&device->pnp.ids);
1656 device->device_type = type;
1657 device->handle = handle;
1658 device->parent = acpi_bus_get_parent(handle);
1659 fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1660 acpi_set_device_status(device, ACPI_STA_DEFAULT);
1661 acpi_device_get_busid(device);
1662 acpi_set_pnp_ids(handle, &device->pnp, type);
1663 acpi_init_properties(device);
1664 acpi_bus_get_flags(device);
1665 device->flags.match_driver = false;
1666 device->flags.initialized = true;
1667 device->flags.enumeration_by_parent =
1668 acpi_device_enumeration_by_parent(device);
1669 acpi_device_clear_enumerated(device);
1670 device_initialize(&device->dev);
1671 dev_set_uevent_suppress(&device->dev, true);
1672 acpi_init_coherency(device);
1673 /* Assume there are unmet deps to start with. */
1674 device->dep_unmet = 1;
1677 void acpi_device_add_finalize(struct acpi_device *device)
1679 dev_set_uevent_suppress(&device->dev, false);
1680 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1683 static void acpi_scan_init_status(struct acpi_device *adev)
1685 if (acpi_bus_get_status(adev))
1686 acpi_set_device_status(adev, 0);
1689 static int acpi_add_single_object(struct acpi_device **child,
1690 acpi_handle handle, int type)
1692 struct acpi_device *device;
1695 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1699 acpi_init_device_object(device, handle, type);
1701 * Getting the status is delayed till here so that we can call
1702 * acpi_bus_get_status() and use its quirk handling. Note that
1703 * this must be done before the get power-/wakeup_dev-flags calls.
1705 if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR)
1706 acpi_scan_init_status(device);
1708 acpi_bus_get_power_flags(device);
1709 acpi_bus_get_wakeup_device_flags(device);
1711 result = acpi_device_add(device, acpi_device_release);
1713 acpi_device_release(&device->dev);
1717 acpi_power_add_remove_device(device, true);
1718 acpi_device_add_finalize(device);
1720 acpi_handle_debug(handle, "Added as %s, parent %s\n",
1721 dev_name(&device->dev), device->parent ?
1722 dev_name(&device->parent->dev) : "(null)");
1728 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1731 struct resource *res = context;
1733 if (acpi_dev_resource_memory(ares, res))
1734 return AE_CTRL_TERMINATE;
1739 static bool acpi_device_should_be_hidden(acpi_handle handle)
1742 struct resource res;
1744 /* Check if it should ignore the UART device */
1745 if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1749 * The UART device described in SPCR table is assumed to have only one
1750 * memory resource present. So we only look for the first one here.
1752 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1753 acpi_get_resource_memory, &res);
1754 if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1757 acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1763 bool acpi_device_is_present(const struct acpi_device *adev)
1765 return adev->status.present || adev->status.functional;
1768 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1770 const struct acpi_device_id **matchid)
1772 const struct acpi_device_id *devid;
1775 return handler->match(idstr, matchid);
1777 for (devid = handler->ids; devid->id[0]; devid++)
1778 if (!strcmp((char *)devid->id, idstr)) {
1788 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1789 const struct acpi_device_id **matchid)
1791 struct acpi_scan_handler *handler;
1793 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1794 if (acpi_scan_handler_matching(handler, idstr, matchid))
1800 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1802 if (!!hotplug->enabled == !!val)
1805 mutex_lock(&acpi_scan_lock);
1807 hotplug->enabled = val;
1809 mutex_unlock(&acpi_scan_lock);
1812 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1814 struct acpi_hardware_id *hwid;
1816 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1817 acpi_dock_add(adev);
1820 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1821 struct acpi_scan_handler *handler;
1823 handler = acpi_scan_match_handler(hwid->id, NULL);
1825 adev->flags.hotplug_notify = true;
1831 static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
1833 struct acpi_handle_list dep_devices;
1839 * Check for _HID here to avoid deferring the enumeration of:
1841 * 2. ACPI nodes describing USB ports.
1842 * Still, checking for _HID catches more then just these cases ...
1844 if (!check_dep || !acpi_has_method(handle, "_DEP") ||
1845 !acpi_has_method(handle, "_HID"))
1848 status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
1849 if (ACPI_FAILURE(status)) {
1850 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
1854 for (count = 0, i = 0; i < dep_devices.count; i++) {
1855 struct acpi_device_info *info;
1856 struct acpi_dep_data *dep;
1859 status = acpi_get_object_info(dep_devices.handles[i], &info);
1860 if (ACPI_FAILURE(status)) {
1861 acpi_handle_debug(handle, "Error reading _DEP device info\n");
1865 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
1871 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1877 dep->supplier = dep_devices.handles[i];
1878 dep->consumer = handle;
1880 mutex_lock(&acpi_dep_list_lock);
1881 list_add_tail(&dep->node , &acpi_dep_list);
1882 mutex_unlock(&acpi_dep_list_lock);
1888 static void acpi_scan_dep_init(struct acpi_device *adev)
1890 struct acpi_dep_data *dep;
1892 adev->dep_unmet = 0;
1894 mutex_lock(&acpi_dep_list_lock);
1896 list_for_each_entry(dep, &acpi_dep_list, node) {
1897 if (dep->consumer == adev->handle)
1901 mutex_unlock(&acpi_dep_list_lock);
1904 static bool acpi_bus_scan_second_pass;
1906 static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
1907 struct acpi_device **adev_p)
1909 struct acpi_device *device = NULL;
1910 acpi_object_type acpi_type;
1913 acpi_bus_get_device(handle, &device);
1917 if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
1920 switch (acpi_type) {
1921 case ACPI_TYPE_DEVICE:
1922 if (acpi_device_should_be_hidden(handle))
1925 /* Bail out if there are dependencies. */
1926 if (acpi_scan_check_dep(handle, check_dep) > 0) {
1927 acpi_bus_scan_second_pass = true;
1928 return AE_CTRL_DEPTH;
1932 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1933 type = ACPI_BUS_TYPE_DEVICE;
1936 case ACPI_TYPE_PROCESSOR:
1937 type = ACPI_BUS_TYPE_PROCESSOR;
1940 case ACPI_TYPE_THERMAL:
1941 type = ACPI_BUS_TYPE_THERMAL;
1944 case ACPI_TYPE_POWER:
1945 acpi_add_power_resource(handle);
1951 acpi_add_single_object(&device, handle, type);
1953 return AE_CTRL_DEPTH;
1955 acpi_scan_init_hotplug(device);
1957 * If check_dep is true at this point, the device has no dependencies,
1958 * or the creation of the device object would have been postponed above.
1961 device->dep_unmet = 0;
1963 acpi_scan_dep_init(device);
1972 static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
1973 void *not_used, void **ret_p)
1975 return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
1978 static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
1979 void *not_used, void **ret_p)
1981 return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
1984 static void acpi_default_enumeration(struct acpi_device *device)
1987 * Do not enumerate devices with enumeration_by_parent flag set as
1988 * they will be enumerated by their respective parents.
1990 if (!device->flags.enumeration_by_parent) {
1991 acpi_create_platform_device(device, NULL);
1992 acpi_device_set_enumerated(device);
1994 blocking_notifier_call_chain(&acpi_reconfig_chain,
1995 ACPI_RECONFIG_DEVICE_ADD, device);
1999 static const struct acpi_device_id generic_device_ids[] = {
2000 {ACPI_DT_NAMESPACE_HID, },
2004 static int acpi_generic_device_attach(struct acpi_device *adev,
2005 const struct acpi_device_id *not_used)
2008 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2009 * below can be unconditional.
2011 if (adev->data.of_compatible)
2012 acpi_default_enumeration(adev);
2017 static struct acpi_scan_handler generic_device_handler = {
2018 .ids = generic_device_ids,
2019 .attach = acpi_generic_device_attach,
2022 static int acpi_scan_attach_handler(struct acpi_device *device)
2024 struct acpi_hardware_id *hwid;
2027 list_for_each_entry(hwid, &device->pnp.ids, list) {
2028 const struct acpi_device_id *devid;
2029 struct acpi_scan_handler *handler;
2031 handler = acpi_scan_match_handler(hwid->id, &devid);
2033 if (!handler->attach) {
2034 device->pnp.type.platform_id = 0;
2037 device->handler = handler;
2038 ret = handler->attach(device, devid);
2042 device->handler = NULL;
2051 static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
2053 struct acpi_device *child;
2054 bool skip = !first_pass && device->flags.visited;
2061 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2062 register_dock_dependent_device(device, ejd);
2064 acpi_bus_get_status(device);
2065 /* Skip devices that are not present. */
2066 if (!acpi_device_is_present(device)) {
2067 device->flags.initialized = false;
2068 acpi_device_clear_enumerated(device);
2069 device->flags.power_manageable = 0;
2072 if (device->handler)
2075 if (!device->flags.initialized) {
2076 device->flags.power_manageable =
2077 device->power.states[ACPI_STATE_D0].flags.valid;
2078 if (acpi_bus_init_power(device))
2079 device->flags.power_manageable = 0;
2081 device->flags.initialized = true;
2082 } else if (device->flags.visited) {
2086 ret = acpi_scan_attach_handler(device);
2090 device->flags.match_driver = true;
2091 if (ret > 0 && !device->flags.enumeration_by_parent) {
2092 acpi_device_set_enumerated(device);
2096 ret = device_attach(&device->dev);
2100 if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2101 acpi_default_enumeration(device);
2103 acpi_device_set_enumerated(device);
2106 list_for_each_entry(child, &device->children, node)
2107 acpi_bus_attach(child, first_pass);
2109 if (!skip && device->handler && device->handler->hotplug.notify_online)
2110 device->handler->hotplug.notify_online(device);
2113 void acpi_walk_dep_device_list(acpi_handle handle)
2115 struct acpi_dep_data *dep, *tmp;
2116 struct acpi_device *adev;
2118 mutex_lock(&acpi_dep_list_lock);
2119 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2120 if (dep->supplier == handle) {
2121 acpi_bus_get_device(dep->consumer, &adev);
2125 if (!adev->dep_unmet)
2126 acpi_bus_attach(adev, true);
2129 list_del(&dep->node);
2133 mutex_unlock(&acpi_dep_list_lock);
2135 EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2138 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2139 * @handle: Root of the namespace scope to scan.
2141 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2144 * If no devices were found, -ENODEV is returned, but it does not mean that
2145 * there has been a real error. There just have been no suitable ACPI objects
2146 * in the table trunk from which the kernel could create a device and add an
2147 * appropriate driver.
2149 * Must be called under acpi_scan_lock.
2151 int acpi_bus_scan(acpi_handle handle)
2153 struct acpi_device *device = NULL;
2155 acpi_bus_scan_second_pass = false;
2157 /* Pass 1: Avoid enumerating devices with missing dependencies. */
2159 if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
2160 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2161 acpi_bus_check_add_1, NULL, NULL,
2167 acpi_bus_attach(device, true);
2169 if (!acpi_bus_scan_second_pass)
2172 /* Pass 2: Enumerate all of the remaining devices. */
2176 if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
2177 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2178 acpi_bus_check_add_2, NULL, NULL,
2181 acpi_bus_attach(device, false);
2185 EXPORT_SYMBOL(acpi_bus_scan);
2188 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2189 * @adev: Root of the ACPI namespace scope to walk.
2191 * Must be called under acpi_scan_lock.
2193 void acpi_bus_trim(struct acpi_device *adev)
2195 struct acpi_scan_handler *handler = adev->handler;
2196 struct acpi_device *child;
2198 list_for_each_entry_reverse(child, &adev->children, node)
2199 acpi_bus_trim(child);
2201 adev->flags.match_driver = false;
2203 if (handler->detach)
2204 handler->detach(adev);
2206 adev->handler = NULL;
2208 device_release_driver(&adev->dev);
2211 * Most likely, the device is going away, so put it into D3cold before
2214 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2215 adev->flags.initialized = false;
2216 acpi_device_clear_enumerated(adev);
2218 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2220 int acpi_bus_register_early_device(int type)
2222 struct acpi_device *device = NULL;
2225 result = acpi_add_single_object(&device, NULL, type);
2229 device->flags.match_driver = true;
2230 return device_attach(&device->dev);
2232 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2234 static int acpi_bus_scan_fixed(void)
2239 * Enumerate all fixed-feature devices.
2241 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2242 struct acpi_device *device = NULL;
2244 result = acpi_add_single_object(&device, NULL,
2245 ACPI_BUS_TYPE_POWER_BUTTON);
2249 device->flags.match_driver = true;
2250 result = device_attach(&device->dev);
2254 device_init_wakeup(&device->dev, true);
2257 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2258 struct acpi_device *device = NULL;
2260 result = acpi_add_single_object(&device, NULL,
2261 ACPI_BUS_TYPE_SLEEP_BUTTON);
2265 device->flags.match_driver = true;
2266 result = device_attach(&device->dev);
2269 return result < 0 ? result : 0;
2272 static void __init acpi_get_spcr_uart_addr(void)
2275 struct acpi_table_spcr *spcr_ptr;
2277 status = acpi_get_table(ACPI_SIG_SPCR, 0,
2278 (struct acpi_table_header **)&spcr_ptr);
2279 if (ACPI_FAILURE(status)) {
2280 pr_warn(PREFIX "STAO table present, but SPCR is missing\n");
2284 spcr_uart_addr = spcr_ptr->serial_port.address;
2285 acpi_put_table((struct acpi_table_header *)spcr_ptr);
2288 static bool acpi_scan_initialized;
2290 int __init acpi_scan_init(void)
2294 struct acpi_table_stao *stao_ptr;
2296 acpi_pci_root_init();
2297 acpi_pci_link_init();
2298 acpi_processor_init();
2299 acpi_platform_init();
2302 acpi_cmos_rtc_init();
2303 acpi_container_init();
2304 acpi_memory_hotplug_init();
2305 acpi_watchdog_init();
2307 acpi_int340x_thermal_init();
2311 acpi_scan_add_handler(&generic_device_handler);
2314 * If there is STAO table, check whether it needs to ignore the UART
2315 * device in SPCR table.
2317 status = acpi_get_table(ACPI_SIG_STAO, 0,
2318 (struct acpi_table_header **)&stao_ptr);
2319 if (ACPI_SUCCESS(status)) {
2320 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2321 pr_info(PREFIX "STAO Name List not yet supported.\n");
2323 if (stao_ptr->ignore_uart)
2324 acpi_get_spcr_uart_addr();
2326 acpi_put_table((struct acpi_table_header *)stao_ptr);
2329 acpi_gpe_apply_masked_gpes();
2330 acpi_update_all_gpes();
2333 * Although we call __add_memory() that is documented to require the
2334 * device_hotplug_lock, it is not necessary here because this is an
2335 * early code when userspace or any other code path cannot trigger
2336 * hotplug/hotunplug operations.
2338 mutex_lock(&acpi_scan_lock);
2340 * Enumerate devices in the ACPI namespace.
2342 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2346 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2350 /* Fixed feature devices do not exist on HW-reduced platform */
2351 if (!acpi_gbl_reduced_hardware) {
2352 result = acpi_bus_scan_fixed();
2354 acpi_detach_data(acpi_root->handle,
2355 acpi_scan_drop_device);
2356 acpi_device_del(acpi_root);
2357 acpi_bus_put_acpi_device(acpi_root);
2362 acpi_scan_initialized = true;
2365 mutex_unlock(&acpi_scan_lock);
2369 static struct acpi_probe_entry *ape;
2370 static int acpi_probe_count;
2371 static DEFINE_MUTEX(acpi_probe_mutex);
2373 static int __init acpi_match_madt(union acpi_subtable_headers *header,
2374 const unsigned long end)
2376 if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2377 if (!ape->probe_subtbl(header, end))
2383 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2390 mutex_lock(&acpi_probe_mutex);
2391 for (ape = ap_head; nr; ape++, nr--) {
2392 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2393 acpi_probe_count = 0;
2394 acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2395 count += acpi_probe_count;
2398 res = acpi_table_parse(ape->id, ape->probe_table);
2403 mutex_unlock(&acpi_probe_mutex);
2408 struct acpi_table_events_work {
2409 struct work_struct work;
2414 static void acpi_table_events_fn(struct work_struct *work)
2416 struct acpi_table_events_work *tew;
2418 tew = container_of(work, struct acpi_table_events_work, work);
2420 if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2421 acpi_scan_lock_acquire();
2422 acpi_bus_scan(ACPI_ROOT_OBJECT);
2423 acpi_scan_lock_release();
2429 void acpi_scan_table_handler(u32 event, void *table, void *context)
2431 struct acpi_table_events_work *tew;
2433 if (!acpi_scan_initialized)
2436 if (event != ACPI_TABLE_EVENT_LOAD)
2439 tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2443 INIT_WORK(&tew->work, acpi_table_events_fn);
2447 schedule_work(&tew->work);
2450 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2452 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2454 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2456 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2458 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2460 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);