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 void acpi_scan_lock_acquire(void)
52 mutex_lock(&acpi_scan_lock);
54 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
56 void acpi_scan_lock_release(void)
58 mutex_unlock(&acpi_scan_lock);
60 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
62 void acpi_lock_hp_context(void)
64 mutex_lock(&acpi_hp_context_lock);
67 void acpi_unlock_hp_context(void)
69 mutex_unlock(&acpi_hp_context_lock);
72 void acpi_initialize_hp_context(struct acpi_device *adev,
73 struct acpi_hotplug_context *hp,
74 int (*notify)(struct acpi_device *, u32),
75 void (*uevent)(struct acpi_device *, u32))
77 acpi_lock_hp_context();
80 acpi_set_hp_context(adev, hp);
81 acpi_unlock_hp_context();
83 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
85 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
90 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
94 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
95 const char *hotplug_profile_name)
99 error = acpi_scan_add_handler(handler);
103 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
107 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
109 struct acpi_device_physical_node *pn;
111 char *envp[] = { "EVENT=offline", NULL };
114 * acpi_container_offline() calls this for all of the container's
115 * children under the container's physical_node_lock lock.
117 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
119 list_for_each_entry(pn, &adev->physical_node_list, node)
120 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
122 kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
128 mutex_unlock(&adev->physical_node_lock);
132 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
135 struct acpi_device *device = NULL;
136 struct acpi_device_physical_node *pn;
137 bool second_pass = (bool)data;
138 acpi_status status = AE_OK;
140 if (acpi_bus_get_device(handle, &device))
143 if (device->handler && !device->handler->hotplug.enabled) {
144 *ret_p = &device->dev;
148 mutex_lock(&device->physical_node_lock);
150 list_for_each_entry(pn, &device->physical_node_list, node) {
154 /* Skip devices offlined by the first pass. */
158 pn->put_online = false;
160 ret = device_offline(pn->dev);
162 pn->put_online = !ret;
172 mutex_unlock(&device->physical_node_lock);
177 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
180 struct acpi_device *device = NULL;
181 struct acpi_device_physical_node *pn;
183 if (acpi_bus_get_device(handle, &device))
186 mutex_lock(&device->physical_node_lock);
188 list_for_each_entry(pn, &device->physical_node_list, node)
189 if (pn->put_online) {
190 device_online(pn->dev);
191 pn->put_online = false;
194 mutex_unlock(&device->physical_node_lock);
199 static int acpi_scan_try_to_offline(struct acpi_device *device)
201 acpi_handle handle = device->handle;
202 struct device *errdev = NULL;
206 * Carry out two passes here and ignore errors in the first pass,
207 * because if the devices in question are memory blocks and
208 * CONFIG_MEMCG is set, one of the blocks may hold data structures
209 * that the other blocks depend on, but it is not known in advance which
212 * If the first pass is successful, the second one isn't needed, though.
214 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
215 NULL, acpi_bus_offline, (void *)false,
217 if (status == AE_SUPPORT) {
218 dev_warn(errdev, "Offline disabled.\n");
219 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
220 acpi_bus_online, NULL, NULL, NULL);
223 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
226 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
227 NULL, acpi_bus_offline, (void *)true,
230 acpi_bus_offline(handle, 0, (void *)true,
234 dev_warn(errdev, "Offline failed.\n");
235 acpi_bus_online(handle, 0, NULL, NULL);
236 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
237 ACPI_UINT32_MAX, acpi_bus_online,
245 static int acpi_scan_hot_remove(struct acpi_device *device)
247 acpi_handle handle = device->handle;
248 unsigned long long sta;
251 if (device->handler && device->handler->hotplug.demand_offline) {
252 if (!acpi_scan_is_offline(device, true))
255 int error = acpi_scan_try_to_offline(device);
260 acpi_handle_debug(handle, "Ejecting\n");
262 acpi_bus_trim(device);
264 acpi_evaluate_lck(handle, 0);
268 status = acpi_evaluate_ej0(handle);
269 if (status == AE_NOT_FOUND)
271 else if (ACPI_FAILURE(status))
275 * Verify if eject was indeed successful. If not, log an error
276 * message. No need to call _OST since _EJ0 call was made OK.
278 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
279 if (ACPI_FAILURE(status)) {
280 acpi_handle_warn(handle,
281 "Status check after eject failed (0x%x)\n", status);
282 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
283 acpi_handle_warn(handle,
284 "Eject incomplete - status 0x%llx\n", sta);
290 static int acpi_scan_device_not_present(struct acpi_device *adev)
292 if (!acpi_device_enumerated(adev)) {
293 dev_warn(&adev->dev, "Still not present\n");
300 static int acpi_scan_device_check(struct acpi_device *adev)
304 acpi_bus_get_status(adev);
305 if (adev->status.present || adev->status.functional) {
307 * This function is only called for device objects for which
308 * matching scan handlers exist. The only situation in which
309 * the scan handler is not attached to this device object yet
310 * is when the device has just appeared (either it wasn't
311 * present at all before or it was removed and then added
315 dev_warn(&adev->dev, "Already enumerated\n");
318 error = acpi_bus_scan(adev->handle);
320 dev_warn(&adev->dev, "Namespace scan failure\n");
323 if (!adev->handler) {
324 dev_warn(&adev->dev, "Enumeration failure\n");
328 error = acpi_scan_device_not_present(adev);
333 static int acpi_scan_bus_check(struct acpi_device *adev)
335 struct acpi_scan_handler *handler = adev->handler;
336 struct acpi_device *child;
339 acpi_bus_get_status(adev);
340 if (!(adev->status.present || adev->status.functional)) {
341 acpi_scan_device_not_present(adev);
344 if (handler && handler->hotplug.scan_dependent)
345 return handler->hotplug.scan_dependent(adev);
347 error = acpi_bus_scan(adev->handle);
349 dev_warn(&adev->dev, "Namespace scan failure\n");
352 list_for_each_entry(child, &adev->children, node) {
353 error = acpi_scan_bus_check(child);
360 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
363 case ACPI_NOTIFY_BUS_CHECK:
364 return acpi_scan_bus_check(adev);
365 case ACPI_NOTIFY_DEVICE_CHECK:
366 return acpi_scan_device_check(adev);
367 case ACPI_NOTIFY_EJECT_REQUEST:
368 case ACPI_OST_EC_OSPM_EJECT:
369 if (adev->handler && !adev->handler->hotplug.enabled) {
370 dev_info(&adev->dev, "Eject disabled\n");
373 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
374 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
375 return acpi_scan_hot_remove(adev);
380 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
382 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
385 lock_device_hotplug();
386 mutex_lock(&acpi_scan_lock);
389 * The device object's ACPI handle cannot become invalid as long as we
390 * are holding acpi_scan_lock, but it might have become invalid before
391 * that lock was acquired.
393 if (adev->handle == INVALID_ACPI_HANDLE)
396 if (adev->flags.is_dock_station) {
397 error = dock_notify(adev, src);
398 } else if (adev->flags.hotplug_notify) {
399 error = acpi_generic_hotplug_event(adev, src);
401 int (*notify)(struct acpi_device *, u32);
403 acpi_lock_hp_context();
404 notify = adev->hp ? adev->hp->notify : NULL;
405 acpi_unlock_hp_context();
407 * There may be additional notify handlers for device objects
408 * without the .event() callback, so ignore them here.
411 error = notify(adev, src);
417 ost_code = ACPI_OST_SC_SUCCESS;
420 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
423 ost_code = ACPI_OST_SC_DEVICE_BUSY;
426 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
431 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
434 acpi_bus_put_acpi_device(adev);
435 mutex_unlock(&acpi_scan_lock);
436 unlock_device_hotplug();
439 static void acpi_free_power_resources_lists(struct acpi_device *device)
443 if (device->wakeup.flags.valid)
444 acpi_power_resources_list_free(&device->wakeup.resources);
446 if (!device->power.flags.power_resources)
449 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
450 struct acpi_device_power_state *ps = &device->power.states[i];
451 acpi_power_resources_list_free(&ps->resources);
455 static void acpi_device_release(struct device *dev)
457 struct acpi_device *acpi_dev = to_acpi_device(dev);
459 acpi_free_properties(acpi_dev);
460 acpi_free_pnp_ids(&acpi_dev->pnp);
461 acpi_free_power_resources_lists(acpi_dev);
465 static void acpi_device_del(struct acpi_device *device)
467 struct acpi_device_bus_id *acpi_device_bus_id;
469 mutex_lock(&acpi_device_lock);
471 list_del(&device->node);
473 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
474 if (!strcmp(acpi_device_bus_id->bus_id,
475 acpi_device_hid(device))) {
476 ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
477 if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
478 list_del(&acpi_device_bus_id->node);
479 kfree_const(acpi_device_bus_id->bus_id);
480 kfree(acpi_device_bus_id);
485 list_del(&device->wakeup_list);
486 mutex_unlock(&acpi_device_lock);
488 acpi_power_add_remove_device(device, false);
489 acpi_device_remove_files(device);
491 device->remove(device);
493 device_del(&device->dev);
496 static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
498 static LIST_HEAD(acpi_device_del_list);
499 static DEFINE_MUTEX(acpi_device_del_lock);
501 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
504 struct acpi_device *adev;
506 mutex_lock(&acpi_device_del_lock);
508 if (list_empty(&acpi_device_del_list)) {
509 mutex_unlock(&acpi_device_del_lock);
512 adev = list_first_entry(&acpi_device_del_list,
513 struct acpi_device, del_list);
514 list_del(&adev->del_list);
516 mutex_unlock(&acpi_device_del_lock);
518 blocking_notifier_call_chain(&acpi_reconfig_chain,
519 ACPI_RECONFIG_DEVICE_REMOVE, adev);
521 acpi_device_del(adev);
523 * Drop references to all power resources that might have been
524 * used by the device.
526 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
532 * acpi_scan_drop_device - Drop an ACPI device object.
533 * @handle: Handle of an ACPI namespace node, not used.
534 * @context: Address of the ACPI device object to drop.
536 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
537 * namespace node the device object pointed to by @context is attached to.
539 * The unregistration is carried out asynchronously to avoid running
540 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
541 * ensure the correct ordering (the device objects must be unregistered in the
542 * same order in which the corresponding namespace nodes are deleted).
544 static void acpi_scan_drop_device(acpi_handle handle, void *context)
546 static DECLARE_WORK(work, acpi_device_del_work_fn);
547 struct acpi_device *adev = context;
549 mutex_lock(&acpi_device_del_lock);
552 * Use the ACPI hotplug workqueue which is ordered, so this work item
553 * won't run after any hotplug work items submitted subsequently. That
554 * prevents attempts to register device objects identical to those being
555 * deleted from happening concurrently (such attempts result from
556 * hotplug events handled via the ACPI hotplug workqueue). It also will
557 * run after all of the work items submitted previously, which helps
558 * those work items to ensure that they are not accessing stale device
561 if (list_empty(&acpi_device_del_list))
562 acpi_queue_hotplug_work(&work);
564 list_add_tail(&adev->del_list, &acpi_device_del_list);
565 /* Make acpi_ns_validate_handle() return NULL for this handle. */
566 adev->handle = INVALID_ACPI_HANDLE;
568 mutex_unlock(&acpi_device_del_lock);
571 static struct acpi_device *handle_to_device(acpi_handle handle,
572 void (*callback)(void *))
574 struct acpi_device *adev = NULL;
577 status = acpi_get_data_full(handle, acpi_scan_drop_device,
578 (void **)&adev, callback);
579 if (ACPI_FAILURE(status) || !adev) {
580 acpi_handle_debug(handle, "No context!\n");
586 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
591 *device = handle_to_device(handle, NULL);
597 EXPORT_SYMBOL(acpi_bus_get_device);
599 static void get_acpi_device(void *dev)
604 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
606 return handle_to_device(handle, get_acpi_device);
609 void acpi_bus_put_acpi_device(struct acpi_device *adev)
614 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
616 struct acpi_device_bus_id *acpi_device_bus_id;
618 /* Find suitable bus_id and instance number in acpi_bus_id_list. */
619 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
620 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
621 return acpi_device_bus_id;
626 static int acpi_device_set_name(struct acpi_device *device,
627 struct acpi_device_bus_id *acpi_device_bus_id)
629 struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
632 result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
636 device->pnp.instance_no = result;
637 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
641 int acpi_device_add(struct acpi_device *device,
642 void (*release)(struct device *))
644 struct acpi_device_bus_id *acpi_device_bus_id;
647 if (device->handle) {
650 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
652 if (ACPI_FAILURE(status)) {
653 acpi_handle_err(device->handle,
654 "Unable to attach device data\n");
662 * Link this device to its parent and siblings.
664 INIT_LIST_HEAD(&device->children);
665 INIT_LIST_HEAD(&device->node);
666 INIT_LIST_HEAD(&device->wakeup_list);
667 INIT_LIST_HEAD(&device->physical_node_list);
668 INIT_LIST_HEAD(&device->del_list);
669 mutex_init(&device->physical_node_lock);
671 mutex_lock(&acpi_device_lock);
673 acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
674 if (acpi_device_bus_id) {
675 result = acpi_device_set_name(device, acpi_device_bus_id);
679 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
681 if (!acpi_device_bus_id) {
685 acpi_device_bus_id->bus_id =
686 kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
687 if (!acpi_device_bus_id->bus_id) {
688 kfree(acpi_device_bus_id);
693 ida_init(&acpi_device_bus_id->instance_ida);
695 result = acpi_device_set_name(device, acpi_device_bus_id);
697 kfree_const(acpi_device_bus_id->bus_id);
698 kfree(acpi_device_bus_id);
702 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
706 list_add_tail(&device->node, &device->parent->children);
708 if (device->wakeup.flags.valid)
709 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
711 mutex_unlock(&acpi_device_lock);
714 device->dev.parent = &device->parent->dev;
716 device->dev.bus = &acpi_bus_type;
717 device->dev.release = release;
718 result = device_add(&device->dev);
720 dev_err(&device->dev, "Error registering device\n");
724 result = acpi_device_setup_files(device);
726 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
727 dev_name(&device->dev));
732 mutex_lock(&acpi_device_lock);
735 list_del(&device->node);
737 list_del(&device->wakeup_list);
740 mutex_unlock(&acpi_device_lock);
742 acpi_detach_data(device->handle, acpi_scan_drop_device);
747 /* --------------------------------------------------------------------------
749 -------------------------------------------------------------------------- */
750 static bool acpi_info_matches_ids(struct acpi_device_info *info,
751 const char * const ids[])
753 struct acpi_pnp_device_id_list *cid_list = NULL;
756 if (!(info->valid & ACPI_VALID_HID))
759 index = match_string(ids, -1, info->hardware_id.string);
763 if (info->valid & ACPI_VALID_CID)
764 cid_list = &info->compatible_id_list;
769 for (i = 0; i < cid_list->count; i++) {
770 index = match_string(ids, -1, cid_list->ids[i].string);
778 /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
779 static const char * const acpi_ignore_dep_ids[] = {
780 "PNP0D80", /* Windows-compatible System Power Management Controller */
781 "INT33BD", /* Intel Baytrail Mailbox Device */
785 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
787 struct acpi_device *device = NULL;
791 * Fixed hardware devices do not appear in the namespace and do not
792 * have handles, but we fabricate acpi_devices for them, so we have
793 * to deal with them specially.
799 status = acpi_get_parent(handle, &handle);
800 if (ACPI_FAILURE(status))
801 return status == AE_NULL_ENTRY ? NULL : acpi_root;
802 } while (acpi_bus_get_device(handle, &device));
807 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
811 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
812 union acpi_object *obj;
814 status = acpi_get_handle(handle, "_EJD", &tmp);
815 if (ACPI_FAILURE(status))
818 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
819 if (ACPI_SUCCESS(status)) {
820 obj = buffer.pointer;
821 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
823 kfree(buffer.pointer);
827 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
829 static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
831 acpi_handle handle = dev->handle;
832 struct acpi_device_wakeup *wakeup = &dev->wakeup;
833 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
834 union acpi_object *package = NULL;
835 union acpi_object *element = NULL;
839 INIT_LIST_HEAD(&wakeup->resources);
842 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
843 if (ACPI_FAILURE(status)) {
844 acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
845 acpi_format_exception(status));
849 package = (union acpi_object *)buffer.pointer;
851 if (!package || package->package.count < 2)
854 element = &(package->package.elements[0]);
858 if (element->type == ACPI_TYPE_PACKAGE) {
859 if ((element->package.count < 2) ||
860 (element->package.elements[0].type !=
861 ACPI_TYPE_LOCAL_REFERENCE)
862 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
866 element->package.elements[0].reference.handle;
868 (u32) element->package.elements[1].integer.value;
869 } else if (element->type == ACPI_TYPE_INTEGER) {
870 wakeup->gpe_device = NULL;
871 wakeup->gpe_number = element->integer.value;
876 element = &(package->package.elements[1]);
877 if (element->type != ACPI_TYPE_INTEGER)
880 wakeup->sleep_state = element->integer.value;
882 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
886 if (!list_empty(&wakeup->resources)) {
889 err = acpi_power_wakeup_list_init(&wakeup->resources,
892 acpi_handle_warn(handle, "Retrieving current states "
893 "of wakeup power resources failed\n");
894 acpi_power_resources_list_free(&wakeup->resources);
897 if (sleep_state < wakeup->sleep_state) {
898 acpi_handle_warn(handle, "Overriding _PRW sleep state "
899 "(S%d) by S%d from power resources\n",
900 (int)wakeup->sleep_state, sleep_state);
901 wakeup->sleep_state = sleep_state;
906 kfree(buffer.pointer);
910 static bool acpi_wakeup_gpe_init(struct acpi_device *device)
912 static const struct acpi_device_id button_device_ids[] = {
913 {"PNP0C0C", 0}, /* Power button */
914 {"PNP0C0D", 0}, /* Lid */
915 {"PNP0C0E", 0}, /* Sleep button */
918 struct acpi_device_wakeup *wakeup = &device->wakeup;
921 wakeup->flags.notifier_present = 0;
923 /* Power button, Lid switch always enable wakeup */
924 if (!acpi_match_device_ids(device, button_device_ids)) {
925 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
926 /* Do not use Lid/sleep button for S5 wakeup */
927 if (wakeup->sleep_state == ACPI_STATE_S5)
928 wakeup->sleep_state = ACPI_STATE_S4;
930 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
931 device_set_wakeup_capable(&device->dev, true);
935 status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
937 return ACPI_SUCCESS(status);
940 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
944 /* Presence of _PRW indicates wake capable */
945 if (!acpi_has_method(device->handle, "_PRW"))
948 err = acpi_bus_extract_wakeup_device_power_package(device);
950 dev_err(&device->dev, "Unable to extract wakeup power resources");
954 device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
955 device->wakeup.prepare_count = 0;
957 * Call _PSW/_DSW object to disable its ability to wake the sleeping
958 * system for the ACPI device with the _PRW object.
959 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
960 * So it is necessary to call _DSW object first. Only when it is not
961 * present will the _PSW object used.
963 err = acpi_device_sleep_wake(device, 0, 0, 0);
965 pr_debug("error in _DSW or _PSW evaluation\n");
968 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
970 struct acpi_device_power_state *ps = &device->power.states[state];
971 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
972 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
975 INIT_LIST_HEAD(&ps->resources);
977 /* Evaluate "_PRx" to get referenced power resources */
978 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
979 if (ACPI_SUCCESS(status)) {
980 union acpi_object *package = buffer.pointer;
982 if (buffer.length && package
983 && package->type == ACPI_TYPE_PACKAGE
984 && package->package.count)
985 acpi_extract_power_resources(package, 0, &ps->resources);
987 ACPI_FREE(buffer.pointer);
990 /* Evaluate "_PSx" to see if we can do explicit sets */
992 if (acpi_has_method(device->handle, pathname))
993 ps->flags.explicit_set = 1;
995 /* State is valid if there are means to put the device into it. */
996 if (!list_empty(&ps->resources) || ps->flags.explicit_set)
999 ps->power = -1; /* Unknown - driver assigned */
1000 ps->latency = -1; /* Unknown - driver assigned */
1003 static void acpi_bus_get_power_flags(struct acpi_device *device)
1007 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1008 if (!acpi_has_method(device->handle, "_PS0") &&
1009 !acpi_has_method(device->handle, "_PR0"))
1012 device->flags.power_manageable = 1;
1015 * Power Management Flags
1017 if (acpi_has_method(device->handle, "_PSC"))
1018 device->power.flags.explicit_get = 1;
1020 if (acpi_has_method(device->handle, "_IRC"))
1021 device->power.flags.inrush_current = 1;
1023 if (acpi_has_method(device->handle, "_DSW"))
1024 device->power.flags.dsw_present = 1;
1027 * Enumerate supported power management states
1029 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1030 acpi_bus_init_power_state(device, i);
1032 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1034 /* Set the defaults for D0 and D3hot (always supported). */
1035 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1036 device->power.states[ACPI_STATE_D0].power = 100;
1037 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1040 * Use power resources only if the D0 list of them is populated, because
1041 * some platforms may provide _PR3 only to indicate D3cold support and
1042 * in those cases the power resources list returned by it may be bogus.
1044 if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1045 device->power.flags.power_resources = 1;
1047 * D3cold is supported if the D3hot list of power resources is
1050 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1051 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1054 if (acpi_bus_init_power(device))
1055 device->flags.power_manageable = 0;
1058 static void acpi_bus_get_flags(struct acpi_device *device)
1060 /* Presence of _STA indicates 'dynamic_status' */
1061 if (acpi_has_method(device->handle, "_STA"))
1062 device->flags.dynamic_status = 1;
1064 /* Presence of _RMV indicates 'removable' */
1065 if (acpi_has_method(device->handle, "_RMV"))
1066 device->flags.removable = 1;
1068 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1069 if (acpi_has_method(device->handle, "_EJD") ||
1070 acpi_has_method(device->handle, "_EJ0"))
1071 device->flags.ejectable = 1;
1074 static void acpi_device_get_busid(struct acpi_device *device)
1076 char bus_id[5] = { '?', 0 };
1077 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1083 * The device's Bus ID is simply the object name.
1084 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1086 if (ACPI_IS_ROOT_DEVICE(device)) {
1087 strcpy(device->pnp.bus_id, "ACPI");
1091 switch (device->device_type) {
1092 case ACPI_BUS_TYPE_POWER_BUTTON:
1093 strcpy(device->pnp.bus_id, "PWRF");
1095 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1096 strcpy(device->pnp.bus_id, "SLPF");
1098 case ACPI_BUS_TYPE_ECDT_EC:
1099 strcpy(device->pnp.bus_id, "ECDT");
1102 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1103 /* Clean up trailing underscores (if any) */
1104 for (i = 3; i > 1; i--) {
1105 if (bus_id[i] == '_')
1110 strcpy(device->pnp.bus_id, bus_id);
1116 * acpi_ata_match - see if an acpi object is an ATA device
1118 * If an acpi object has one of the ACPI ATA methods defined,
1119 * then we can safely call it an ATA device.
1121 bool acpi_ata_match(acpi_handle handle)
1123 return acpi_has_method(handle, "_GTF") ||
1124 acpi_has_method(handle, "_GTM") ||
1125 acpi_has_method(handle, "_STM") ||
1126 acpi_has_method(handle, "_SDD");
1130 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1132 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1133 * then we can safely call it an ejectable drive bay
1135 bool acpi_bay_match(acpi_handle handle)
1137 acpi_handle phandle;
1139 if (!acpi_has_method(handle, "_EJ0"))
1141 if (acpi_ata_match(handle))
1143 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1146 return acpi_ata_match(phandle);
1149 bool acpi_device_is_battery(struct acpi_device *adev)
1151 struct acpi_hardware_id *hwid;
1153 list_for_each_entry(hwid, &adev->pnp.ids, list)
1154 if (!strcmp("PNP0C0A", hwid->id))
1160 static bool is_ejectable_bay(struct acpi_device *adev)
1162 acpi_handle handle = adev->handle;
1164 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1167 return acpi_bay_match(handle);
1171 * acpi_dock_match - see if an acpi object has a _DCK method
1173 bool acpi_dock_match(acpi_handle handle)
1175 return acpi_has_method(handle, "_DCK");
1179 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1180 void **return_value)
1182 long *cap = context;
1184 if (acpi_has_method(handle, "_BCM") &&
1185 acpi_has_method(handle, "_BCL")) {
1186 acpi_handle_debug(handle, "Found generic backlight support\n");
1187 *cap |= ACPI_VIDEO_BACKLIGHT;
1188 /* We have backlight support, no need to scan further */
1189 return AE_CTRL_TERMINATE;
1194 /* Returns true if the ACPI object is a video device which can be
1195 * handled by video.ko.
1196 * The device will get a Linux specific CID added in scan.c to
1197 * identify the device as an ACPI graphics device
1198 * Be aware that the graphics device may not be physically present
1199 * Use acpi_video_get_capabilities() to detect general ACPI video
1200 * capabilities of present cards
1202 long acpi_is_video_device(acpi_handle handle)
1204 long video_caps = 0;
1206 /* Is this device able to support video switching ? */
1207 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1208 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1210 /* Is this device able to retrieve a video ROM ? */
1211 if (acpi_has_method(handle, "_ROM"))
1212 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1214 /* Is this device able to configure which video head to be POSTed ? */
1215 if (acpi_has_method(handle, "_VPO") &&
1216 acpi_has_method(handle, "_GPD") &&
1217 acpi_has_method(handle, "_SPD"))
1218 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1220 /* Only check for backlight functionality if one of the above hit. */
1222 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1223 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1228 EXPORT_SYMBOL(acpi_is_video_device);
1230 const char *acpi_device_hid(struct acpi_device *device)
1232 struct acpi_hardware_id *hid;
1234 if (list_empty(&device->pnp.ids))
1237 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1240 EXPORT_SYMBOL(acpi_device_hid);
1242 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1244 struct acpi_hardware_id *id;
1246 id = kmalloc(sizeof(*id), GFP_KERNEL);
1250 id->id = kstrdup_const(dev_id, GFP_KERNEL);
1256 list_add_tail(&id->list, &pnp->ids);
1257 pnp->type.hardware_id = 1;
1261 * Old IBM workstations have a DSDT bug wherein the SMBus object
1262 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1263 * prefix. Work around this.
1265 static bool acpi_ibm_smbus_match(acpi_handle handle)
1267 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1268 struct acpi_buffer path = { sizeof(node_name), node_name };
1270 if (!dmi_name_in_vendors("IBM"))
1273 /* Look for SMBS object */
1274 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1275 strcmp("SMBS", path.pointer))
1278 /* Does it have the necessary (but misnamed) methods? */
1279 if (acpi_has_method(handle, "SBI") &&
1280 acpi_has_method(handle, "SBR") &&
1281 acpi_has_method(handle, "SBW"))
1287 static bool acpi_object_is_system_bus(acpi_handle handle)
1291 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1294 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1301 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1304 struct acpi_device_info *info = NULL;
1305 struct acpi_pnp_device_id_list *cid_list;
1308 switch (device_type) {
1309 case ACPI_BUS_TYPE_DEVICE:
1310 if (handle == ACPI_ROOT_OBJECT) {
1311 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1315 acpi_get_object_info(handle, &info);
1317 pr_err(PREFIX "%s: Error reading device info\n",
1322 if (info->valid & ACPI_VALID_HID) {
1323 acpi_add_id(pnp, info->hardware_id.string);
1324 pnp->type.platform_id = 1;
1326 if (info->valid & ACPI_VALID_CID) {
1327 cid_list = &info->compatible_id_list;
1328 for (i = 0; i < cid_list->count; i++)
1329 acpi_add_id(pnp, cid_list->ids[i].string);
1331 if (info->valid & ACPI_VALID_ADR) {
1332 pnp->bus_address = info->address;
1333 pnp->type.bus_address = 1;
1335 if (info->valid & ACPI_VALID_UID)
1336 pnp->unique_id = kstrdup(info->unique_id.string,
1338 if (info->valid & ACPI_VALID_CLS)
1339 acpi_add_id(pnp, info->class_code.string);
1344 * Some devices don't reliably have _HIDs & _CIDs, so add
1345 * synthetic HIDs to make sure drivers can find them.
1347 if (acpi_is_video_device(handle))
1348 acpi_add_id(pnp, ACPI_VIDEO_HID);
1349 else if (acpi_bay_match(handle))
1350 acpi_add_id(pnp, ACPI_BAY_HID);
1351 else if (acpi_dock_match(handle))
1352 acpi_add_id(pnp, ACPI_DOCK_HID);
1353 else if (acpi_ibm_smbus_match(handle))
1354 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1355 else if (list_empty(&pnp->ids) &&
1356 acpi_object_is_system_bus(handle)) {
1357 /* \_SB, \_TZ, LNXSYBUS */
1358 acpi_add_id(pnp, ACPI_BUS_HID);
1359 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1360 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1364 case ACPI_BUS_TYPE_POWER:
1365 acpi_add_id(pnp, ACPI_POWER_HID);
1367 case ACPI_BUS_TYPE_PROCESSOR:
1368 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1370 case ACPI_BUS_TYPE_THERMAL:
1371 acpi_add_id(pnp, ACPI_THERMAL_HID);
1373 case ACPI_BUS_TYPE_POWER_BUTTON:
1374 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1376 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1377 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1379 case ACPI_BUS_TYPE_ECDT_EC:
1380 acpi_add_id(pnp, ACPI_ECDT_HID);
1385 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1387 struct acpi_hardware_id *id, *tmp;
1389 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1390 kfree_const(id->id);
1393 kfree(pnp->unique_id);
1397 * acpi_dma_supported - Check DMA support for the specified device.
1398 * @adev: The pointer to acpi device
1400 * Return false if DMA is not supported. Otherwise, return true
1402 bool acpi_dma_supported(struct acpi_device *adev)
1407 if (adev->flags.cca_seen)
1411 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1412 * DMA on "Intel platforms". Presumably that includes all x86 and
1413 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1415 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1422 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1423 * @adev: The pointer to acpi device
1425 * Return enum dev_dma_attr.
1427 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1429 if (!acpi_dma_supported(adev))
1430 return DEV_DMA_NOT_SUPPORTED;
1432 if (adev->flags.coherent_dma)
1433 return DEV_DMA_COHERENT;
1435 return DEV_DMA_NON_COHERENT;
1439 * acpi_dma_get_range() - Get device DMA parameters.
1441 * @dev: device to configure
1442 * @dma_addr: pointer device DMA address result
1443 * @offset: pointer to the DMA offset result
1444 * @size: pointer to DMA range size result
1446 * Evaluate DMA regions and return respectively DMA region start, offset
1447 * and size in dma_addr, offset and size on parsing success; it does not
1448 * update the passed in values on failure.
1450 * Return 0 on success, < 0 on failure.
1452 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1455 struct acpi_device *adev;
1457 struct resource_entry *rentry;
1459 struct device *dma_dev = dev;
1460 u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1463 * Walk the device tree chasing an ACPI companion with a _DMA
1464 * object while we go. Stop if we find a device with an ACPI
1465 * companion containing a _DMA method.
1468 adev = ACPI_COMPANION(dma_dev);
1469 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1472 dma_dev = dma_dev->parent;
1478 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1479 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1483 ret = acpi_dev_get_dma_resources(adev, &list);
1485 list_for_each_entry(rentry, &list, node) {
1486 if (dma_offset && rentry->offset != dma_offset) {
1488 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1491 dma_offset = rentry->offset;
1493 /* Take lower and upper limits */
1494 if (rentry->res->start < dma_start)
1495 dma_start = rentry->res->start;
1496 if (rentry->res->end > dma_end)
1497 dma_end = rentry->res->end;
1500 if (dma_start >= dma_end) {
1502 dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1506 *dma_addr = dma_start - dma_offset;
1507 len = dma_end - dma_start;
1508 *size = max(len, len + 1);
1509 *offset = dma_offset;
1512 acpi_dev_free_resource_list(&list);
1514 return ret >= 0 ? 0 : ret;
1518 * acpi_dma_configure_id - Set-up DMA configuration for the device.
1519 * @dev: The pointer to the device
1520 * @attr: device dma attributes
1521 * @input_id: input device id const value pointer
1523 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1524 const u32 *input_id)
1526 const struct iommu_ops *iommu;
1527 u64 dma_addr = 0, size = 0;
1529 if (attr == DEV_DMA_NOT_SUPPORTED) {
1530 set_dma_ops(dev, &dma_dummy_ops);
1534 iort_dma_setup(dev, &dma_addr, &size);
1536 iommu = iort_iommu_configure_id(dev, input_id);
1537 if (PTR_ERR(iommu) == -EPROBE_DEFER)
1538 return -EPROBE_DEFER;
1540 arch_setup_dma_ops(dev, dma_addr, size,
1541 iommu, attr == DEV_DMA_COHERENT);
1545 EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1547 static void acpi_init_coherency(struct acpi_device *adev)
1549 unsigned long long cca = 0;
1551 struct acpi_device *parent = adev->parent;
1553 if (parent && parent->flags.cca_seen) {
1555 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1558 adev->flags.cca_seen = 1;
1559 cca = parent->flags.coherent_dma;
1561 status = acpi_evaluate_integer(adev->handle, "_CCA",
1563 if (ACPI_SUCCESS(status))
1564 adev->flags.cca_seen = 1;
1565 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1567 * If architecture does not specify that _CCA is
1568 * required for DMA-able devices (e.g. x86),
1569 * we default to _CCA=1.
1573 acpi_handle_debug(adev->handle,
1574 "ACPI device is missing _CCA.\n");
1577 adev->flags.coherent_dma = cca;
1580 static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1582 bool *is_serial_bus_slave_p = data;
1584 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1587 *is_serial_bus_slave_p = true;
1589 /* no need to do more checking */
1593 static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1595 struct acpi_device *parent = device->parent;
1596 static const struct acpi_device_id indirect_io_hosts[] = {
1601 return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1604 static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1606 struct list_head resource_list;
1607 bool is_serial_bus_slave = false;
1609 * These devices have multiple I2cSerialBus resources and an i2c-client
1610 * must be instantiated for each, each with its own i2c_device_id.
1611 * Normally we only instantiate an i2c-client for the first resource,
1612 * using the ACPI HID as id. These special cases are handled by the
1613 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1614 * which i2c_device_id to use for each resource.
1616 static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1624 if (acpi_is_indirect_io_slave(device))
1627 /* Macs use device properties in lieu of _CRS resources */
1628 if (x86_apple_machine &&
1629 (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1630 fwnode_property_present(&device->fwnode, "i2cAddress") ||
1631 fwnode_property_present(&device->fwnode, "baud")))
1634 /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1635 if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1638 INIT_LIST_HEAD(&resource_list);
1639 acpi_dev_get_resources(device, &resource_list,
1640 acpi_check_serial_bus_slave,
1641 &is_serial_bus_slave);
1642 acpi_dev_free_resource_list(&resource_list);
1644 return is_serial_bus_slave;
1647 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1650 INIT_LIST_HEAD(&device->pnp.ids);
1651 device->device_type = type;
1652 device->handle = handle;
1653 device->parent = acpi_bus_get_parent(handle);
1654 fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1655 acpi_set_device_status(device, ACPI_STA_DEFAULT);
1656 acpi_device_get_busid(device);
1657 acpi_set_pnp_ids(handle, &device->pnp, type);
1658 acpi_init_properties(device);
1659 acpi_bus_get_flags(device);
1660 device->flags.match_driver = false;
1661 device->flags.initialized = true;
1662 device->flags.enumeration_by_parent =
1663 acpi_device_enumeration_by_parent(device);
1664 acpi_device_clear_enumerated(device);
1665 device_initialize(&device->dev);
1666 dev_set_uevent_suppress(&device->dev, true);
1667 acpi_init_coherency(device);
1670 static void acpi_scan_dep_init(struct acpi_device *adev)
1672 struct acpi_dep_data *dep;
1674 mutex_lock(&acpi_dep_list_lock);
1676 list_for_each_entry(dep, &acpi_dep_list, node) {
1677 if (dep->consumer == adev->handle)
1681 mutex_unlock(&acpi_dep_list_lock);
1684 void acpi_device_add_finalize(struct acpi_device *device)
1686 dev_set_uevent_suppress(&device->dev, false);
1687 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1690 static void acpi_scan_init_status(struct acpi_device *adev)
1692 if (acpi_bus_get_status(adev))
1693 acpi_set_device_status(adev, 0);
1696 static int acpi_add_single_object(struct acpi_device **child,
1697 acpi_handle handle, int type, bool dep_init)
1699 struct acpi_device *device;
1702 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1706 acpi_init_device_object(device, handle, type);
1708 * Getting the status is delayed till here so that we can call
1709 * acpi_bus_get_status() and use its quirk handling. Note that
1710 * this must be done before the get power-/wakeup_dev-flags calls.
1712 if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
1714 acpi_scan_dep_init(device);
1716 acpi_scan_init_status(device);
1719 acpi_bus_get_power_flags(device);
1720 acpi_bus_get_wakeup_device_flags(device);
1722 result = acpi_device_add(device, acpi_device_release);
1724 acpi_device_release(&device->dev);
1728 acpi_power_add_remove_device(device, true);
1729 acpi_device_add_finalize(device);
1731 acpi_handle_debug(handle, "Added as %s, parent %s\n",
1732 dev_name(&device->dev), device->parent ?
1733 dev_name(&device->parent->dev) : "(null)");
1739 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1742 struct resource *res = context;
1744 if (acpi_dev_resource_memory(ares, res))
1745 return AE_CTRL_TERMINATE;
1750 static bool acpi_device_should_be_hidden(acpi_handle handle)
1753 struct resource res;
1755 /* Check if it should ignore the UART device */
1756 if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1760 * The UART device described in SPCR table is assumed to have only one
1761 * memory resource present. So we only look for the first one here.
1763 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1764 acpi_get_resource_memory, &res);
1765 if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1768 acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1774 bool acpi_device_is_present(const struct acpi_device *adev)
1776 return adev->status.present || adev->status.functional;
1779 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1781 const struct acpi_device_id **matchid)
1783 const struct acpi_device_id *devid;
1786 return handler->match(idstr, matchid);
1788 for (devid = handler->ids; devid->id[0]; devid++)
1789 if (!strcmp((char *)devid->id, idstr)) {
1799 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1800 const struct acpi_device_id **matchid)
1802 struct acpi_scan_handler *handler;
1804 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1805 if (acpi_scan_handler_matching(handler, idstr, matchid))
1811 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1813 if (!!hotplug->enabled == !!val)
1816 mutex_lock(&acpi_scan_lock);
1818 hotplug->enabled = val;
1820 mutex_unlock(&acpi_scan_lock);
1823 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1825 struct acpi_hardware_id *hwid;
1827 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1828 acpi_dock_add(adev);
1831 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1832 struct acpi_scan_handler *handler;
1834 handler = acpi_scan_match_handler(hwid->id, NULL);
1836 adev->flags.hotplug_notify = true;
1842 static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
1844 struct acpi_handle_list dep_devices;
1850 * Check for _HID here to avoid deferring the enumeration of:
1852 * 2. ACPI nodes describing USB ports.
1853 * Still, checking for _HID catches more then just these cases ...
1855 if (!check_dep || !acpi_has_method(handle, "_DEP") ||
1856 !acpi_has_method(handle, "_HID"))
1859 status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
1860 if (ACPI_FAILURE(status)) {
1861 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
1865 for (count = 0, i = 0; i < dep_devices.count; i++) {
1866 struct acpi_device_info *info;
1867 struct acpi_dep_data *dep;
1870 status = acpi_get_object_info(dep_devices.handles[i], &info);
1871 if (ACPI_FAILURE(status)) {
1872 acpi_handle_debug(handle, "Error reading _DEP device info\n");
1876 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
1882 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1888 dep->supplier = dep_devices.handles[i];
1889 dep->consumer = handle;
1891 mutex_lock(&acpi_dep_list_lock);
1892 list_add_tail(&dep->node , &acpi_dep_list);
1893 mutex_unlock(&acpi_dep_list_lock);
1899 static bool acpi_bus_scan_second_pass;
1901 static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
1902 struct acpi_device **adev_p)
1904 struct acpi_device *device = NULL;
1905 acpi_object_type acpi_type;
1908 acpi_bus_get_device(handle, &device);
1912 if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
1915 switch (acpi_type) {
1916 case ACPI_TYPE_DEVICE:
1917 if (acpi_device_should_be_hidden(handle))
1920 /* Bail out if there are dependencies. */
1921 if (acpi_scan_check_dep(handle, check_dep) > 0) {
1922 acpi_bus_scan_second_pass = true;
1923 return AE_CTRL_DEPTH;
1927 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1928 type = ACPI_BUS_TYPE_DEVICE;
1931 case ACPI_TYPE_PROCESSOR:
1932 type = ACPI_BUS_TYPE_PROCESSOR;
1935 case ACPI_TYPE_THERMAL:
1936 type = ACPI_BUS_TYPE_THERMAL;
1939 case ACPI_TYPE_POWER:
1940 acpi_add_power_resource(handle);
1947 * If check_dep is true at this point, the device has no dependencies,
1948 * or the creation of the device object would have been postponed above.
1950 acpi_add_single_object(&device, handle, type, !check_dep);
1952 return AE_CTRL_DEPTH;
1954 acpi_scan_init_hotplug(device);
1963 static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
1964 void *not_used, void **ret_p)
1966 return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
1969 static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
1970 void *not_used, void **ret_p)
1972 return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
1975 static void acpi_default_enumeration(struct acpi_device *device)
1978 * Do not enumerate devices with enumeration_by_parent flag set as
1979 * they will be enumerated by their respective parents.
1981 if (!device->flags.enumeration_by_parent) {
1982 acpi_create_platform_device(device, NULL);
1983 acpi_device_set_enumerated(device);
1985 blocking_notifier_call_chain(&acpi_reconfig_chain,
1986 ACPI_RECONFIG_DEVICE_ADD, device);
1990 static const struct acpi_device_id generic_device_ids[] = {
1991 {ACPI_DT_NAMESPACE_HID, },
1995 static int acpi_generic_device_attach(struct acpi_device *adev,
1996 const struct acpi_device_id *not_used)
1999 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2000 * below can be unconditional.
2002 if (adev->data.of_compatible)
2003 acpi_default_enumeration(adev);
2008 static struct acpi_scan_handler generic_device_handler = {
2009 .ids = generic_device_ids,
2010 .attach = acpi_generic_device_attach,
2013 static int acpi_scan_attach_handler(struct acpi_device *device)
2015 struct acpi_hardware_id *hwid;
2018 list_for_each_entry(hwid, &device->pnp.ids, list) {
2019 const struct acpi_device_id *devid;
2020 struct acpi_scan_handler *handler;
2022 handler = acpi_scan_match_handler(hwid->id, &devid);
2024 if (!handler->attach) {
2025 device->pnp.type.platform_id = 0;
2028 device->handler = handler;
2029 ret = handler->attach(device, devid);
2033 device->handler = NULL;
2042 static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
2044 struct acpi_device *child;
2045 bool skip = !first_pass && device->flags.visited;
2052 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2053 register_dock_dependent_device(device, ejd);
2055 acpi_bus_get_status(device);
2056 /* Skip devices that are not present. */
2057 if (!acpi_device_is_present(device)) {
2058 device->flags.initialized = false;
2059 acpi_device_clear_enumerated(device);
2060 device->flags.power_manageable = 0;
2063 if (device->handler)
2066 if (!device->flags.initialized) {
2067 device->flags.power_manageable =
2068 device->power.states[ACPI_STATE_D0].flags.valid;
2069 if (acpi_bus_init_power(device))
2070 device->flags.power_manageable = 0;
2072 device->flags.initialized = true;
2073 } else if (device->flags.visited) {
2077 ret = acpi_scan_attach_handler(device);
2081 device->flags.match_driver = true;
2082 if (ret > 0 && !device->flags.enumeration_by_parent) {
2083 acpi_device_set_enumerated(device);
2087 ret = device_attach(&device->dev);
2091 if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2092 acpi_default_enumeration(device);
2094 acpi_device_set_enumerated(device);
2097 list_for_each_entry(child, &device->children, node)
2098 acpi_bus_attach(child, first_pass);
2100 if (!skip && device->handler && device->handler->hotplug.notify_online)
2101 device->handler->hotplug.notify_online(device);
2104 static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
2106 struct acpi_device *adev;
2108 adev = acpi_bus_get_acpi_device(dep->consumer);
2110 /* If we don't find an adev then we want to continue parsing */
2113 *(struct acpi_device **)data = adev;
2118 static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
2120 struct acpi_device *adev;
2122 acpi_bus_get_device(dep->consumer, &adev);
2126 if (!adev->dep_unmet)
2127 acpi_bus_attach(adev, true);
2130 list_del(&dep->node);
2137 * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2138 * @handle: The ACPI handle of the supplier device
2139 * @callback: Pointer to the callback function to apply
2140 * @data: Pointer to some data to pass to the callback
2142 * The return value of the callback determines this function's behaviour. If 0
2143 * is returned we continue to iterate over acpi_dep_list. If a positive value
2144 * is returned then the loop is broken but this function returns 0. If a
2145 * negative value is returned by the callback then the loop is broken and that
2146 * value is returned as the final error.
2148 int acpi_walk_dep_device_list(acpi_handle handle,
2149 int (*callback)(struct acpi_dep_data *, void *),
2152 struct acpi_dep_data *dep, *tmp;
2155 mutex_lock(&acpi_dep_list_lock);
2156 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2157 if (dep->supplier == handle) {
2158 ret = callback(dep, data);
2163 mutex_unlock(&acpi_dep_list_lock);
2165 return ret > 0 ? 0 : ret;
2167 EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2170 * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2171 * @supplier: Pointer to the supplier &struct acpi_device
2173 * Clear dependencies on the given device.
2175 void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2177 acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2179 EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2182 * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2183 * @supplier: Pointer to the dependee device
2185 * Returns the first &struct acpi_device which declares itself dependent on
2186 * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2188 * The caller is responsible for putting the reference to adev when it is no
2191 struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2193 struct acpi_device *adev = NULL;
2195 acpi_walk_dep_device_list(supplier->handle,
2196 acpi_dev_get_first_consumer_dev_cb, &adev);
2200 EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
2203 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2204 * @handle: Root of the namespace scope to scan.
2206 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2209 * If no devices were found, -ENODEV is returned, but it does not mean that
2210 * there has been a real error. There just have been no suitable ACPI objects
2211 * in the table trunk from which the kernel could create a device and add an
2212 * appropriate driver.
2214 * Must be called under acpi_scan_lock.
2216 int acpi_bus_scan(acpi_handle handle)
2218 struct acpi_device *device = NULL;
2220 acpi_bus_scan_second_pass = false;
2222 /* Pass 1: Avoid enumerating devices with missing dependencies. */
2224 if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
2225 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2226 acpi_bus_check_add_1, NULL, NULL,
2232 acpi_bus_attach(device, true);
2234 if (!acpi_bus_scan_second_pass)
2237 /* Pass 2: Enumerate all of the remaining devices. */
2241 if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
2242 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2243 acpi_bus_check_add_2, NULL, NULL,
2246 acpi_bus_attach(device, false);
2250 EXPORT_SYMBOL(acpi_bus_scan);
2253 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2254 * @adev: Root of the ACPI namespace scope to walk.
2256 * Must be called under acpi_scan_lock.
2258 void acpi_bus_trim(struct acpi_device *adev)
2260 struct acpi_scan_handler *handler = adev->handler;
2261 struct acpi_device *child;
2263 list_for_each_entry_reverse(child, &adev->children, node)
2264 acpi_bus_trim(child);
2266 adev->flags.match_driver = false;
2268 if (handler->detach)
2269 handler->detach(adev);
2271 adev->handler = NULL;
2273 device_release_driver(&adev->dev);
2276 * Most likely, the device is going away, so put it into D3cold before
2279 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2280 adev->flags.initialized = false;
2281 acpi_device_clear_enumerated(adev);
2283 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2285 int acpi_bus_register_early_device(int type)
2287 struct acpi_device *device = NULL;
2290 result = acpi_add_single_object(&device, NULL, type, false);
2294 device->flags.match_driver = true;
2295 return device_attach(&device->dev);
2297 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2299 static int acpi_bus_scan_fixed(void)
2304 * Enumerate all fixed-feature devices.
2306 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2307 struct acpi_device *device = NULL;
2309 result = acpi_add_single_object(&device, NULL,
2310 ACPI_BUS_TYPE_POWER_BUTTON, false);
2314 device->flags.match_driver = true;
2315 result = device_attach(&device->dev);
2319 device_init_wakeup(&device->dev, true);
2322 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2323 struct acpi_device *device = NULL;
2325 result = acpi_add_single_object(&device, NULL,
2326 ACPI_BUS_TYPE_SLEEP_BUTTON, false);
2330 device->flags.match_driver = true;
2331 result = device_attach(&device->dev);
2334 return result < 0 ? result : 0;
2337 static void __init acpi_get_spcr_uart_addr(void)
2340 struct acpi_table_spcr *spcr_ptr;
2342 status = acpi_get_table(ACPI_SIG_SPCR, 0,
2343 (struct acpi_table_header **)&spcr_ptr);
2344 if (ACPI_FAILURE(status)) {
2345 pr_warn(PREFIX "STAO table present, but SPCR is missing\n");
2349 spcr_uart_addr = spcr_ptr->serial_port.address;
2350 acpi_put_table((struct acpi_table_header *)spcr_ptr);
2353 static bool acpi_scan_initialized;
2355 int __init acpi_scan_init(void)
2359 struct acpi_table_stao *stao_ptr;
2361 acpi_pci_root_init();
2362 acpi_pci_link_init();
2363 acpi_processor_init();
2364 acpi_platform_init();
2367 acpi_cmos_rtc_init();
2368 acpi_container_init();
2369 acpi_memory_hotplug_init();
2370 acpi_watchdog_init();
2372 acpi_int340x_thermal_init();
2376 acpi_scan_add_handler(&generic_device_handler);
2379 * If there is STAO table, check whether it needs to ignore the UART
2380 * device in SPCR table.
2382 status = acpi_get_table(ACPI_SIG_STAO, 0,
2383 (struct acpi_table_header **)&stao_ptr);
2384 if (ACPI_SUCCESS(status)) {
2385 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2386 pr_info(PREFIX "STAO Name List not yet supported.\n");
2388 if (stao_ptr->ignore_uart)
2389 acpi_get_spcr_uart_addr();
2391 acpi_put_table((struct acpi_table_header *)stao_ptr);
2394 acpi_gpe_apply_masked_gpes();
2395 acpi_update_all_gpes();
2398 * Although we call __add_memory() that is documented to require the
2399 * device_hotplug_lock, it is not necessary here because this is an
2400 * early code when userspace or any other code path cannot trigger
2401 * hotplug/hotunplug operations.
2403 mutex_lock(&acpi_scan_lock);
2405 * Enumerate devices in the ACPI namespace.
2407 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2411 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2415 /* Fixed feature devices do not exist on HW-reduced platform */
2416 if (!acpi_gbl_reduced_hardware) {
2417 result = acpi_bus_scan_fixed();
2419 acpi_detach_data(acpi_root->handle,
2420 acpi_scan_drop_device);
2421 acpi_device_del(acpi_root);
2422 acpi_bus_put_acpi_device(acpi_root);
2427 acpi_turn_off_unused_power_resources();
2429 acpi_scan_initialized = true;
2432 mutex_unlock(&acpi_scan_lock);
2436 static struct acpi_probe_entry *ape;
2437 static int acpi_probe_count;
2438 static DEFINE_MUTEX(acpi_probe_mutex);
2440 static int __init acpi_match_madt(union acpi_subtable_headers *header,
2441 const unsigned long end)
2443 if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2444 if (!ape->probe_subtbl(header, end))
2450 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2457 mutex_lock(&acpi_probe_mutex);
2458 for (ape = ap_head; nr; ape++, nr--) {
2459 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2460 acpi_probe_count = 0;
2461 acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2462 count += acpi_probe_count;
2465 res = acpi_table_parse(ape->id, ape->probe_table);
2470 mutex_unlock(&acpi_probe_mutex);
2475 struct acpi_table_events_work {
2476 struct work_struct work;
2481 static void acpi_table_events_fn(struct work_struct *work)
2483 struct acpi_table_events_work *tew;
2485 tew = container_of(work, struct acpi_table_events_work, work);
2487 if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2488 acpi_scan_lock_acquire();
2489 acpi_bus_scan(ACPI_ROOT_OBJECT);
2490 acpi_scan_lock_release();
2496 void acpi_scan_table_handler(u32 event, void *table, void *context)
2498 struct acpi_table_events_work *tew;
2500 if (!acpi_scan_initialized)
2503 if (event != ACPI_TABLE_EVENT_LOAD)
2506 tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2510 INIT_WORK(&tew->work, acpi_table_events_fn);
2514 schedule_work(&tew->work);
2517 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2519 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2521 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2523 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2525 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2527 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);