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 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
611 struct acpi_device_bus_id *acpi_device_bus_id;
613 /* Find suitable bus_id and instance number in acpi_bus_id_list. */
614 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
615 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
616 return acpi_device_bus_id;
621 static int acpi_device_set_name(struct acpi_device *device,
622 struct acpi_device_bus_id *acpi_device_bus_id)
624 struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
627 result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
631 device->pnp.instance_no = result;
632 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
636 int acpi_device_add(struct acpi_device *device,
637 void (*release)(struct device *))
639 struct acpi_device_bus_id *acpi_device_bus_id;
642 if (device->handle) {
645 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
647 if (ACPI_FAILURE(status)) {
648 acpi_handle_err(device->handle,
649 "Unable to attach device data\n");
657 * Link this device to its parent and siblings.
659 INIT_LIST_HEAD(&device->children);
660 INIT_LIST_HEAD(&device->node);
661 INIT_LIST_HEAD(&device->wakeup_list);
662 INIT_LIST_HEAD(&device->physical_node_list);
663 INIT_LIST_HEAD(&device->del_list);
664 mutex_init(&device->physical_node_lock);
666 mutex_lock(&acpi_device_lock);
668 acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
669 if (acpi_device_bus_id) {
670 result = acpi_device_set_name(device, acpi_device_bus_id);
674 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
676 if (!acpi_device_bus_id) {
680 acpi_device_bus_id->bus_id =
681 kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
682 if (!acpi_device_bus_id->bus_id) {
683 kfree(acpi_device_bus_id);
688 ida_init(&acpi_device_bus_id->instance_ida);
690 result = acpi_device_set_name(device, acpi_device_bus_id);
692 kfree_const(acpi_device_bus_id->bus_id);
693 kfree(acpi_device_bus_id);
697 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
701 list_add_tail(&device->node, &device->parent->children);
703 if (device->wakeup.flags.valid)
704 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
706 mutex_unlock(&acpi_device_lock);
709 device->dev.parent = &device->parent->dev;
711 device->dev.bus = &acpi_bus_type;
712 device->dev.release = release;
713 result = device_add(&device->dev);
715 dev_err(&device->dev, "Error registering device\n");
719 result = acpi_device_setup_files(device);
721 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
722 dev_name(&device->dev));
727 mutex_lock(&acpi_device_lock);
730 list_del(&device->node);
732 list_del(&device->wakeup_list);
735 mutex_unlock(&acpi_device_lock);
737 acpi_detach_data(device->handle, acpi_scan_drop_device);
742 /* --------------------------------------------------------------------------
744 -------------------------------------------------------------------------- */
745 static bool acpi_info_matches_ids(struct acpi_device_info *info,
746 const char * const ids[])
748 struct acpi_pnp_device_id_list *cid_list = NULL;
751 if (!(info->valid & ACPI_VALID_HID))
754 index = match_string(ids, -1, info->hardware_id.string);
758 if (info->valid & ACPI_VALID_CID)
759 cid_list = &info->compatible_id_list;
764 for (i = 0; i < cid_list->count; i++) {
765 index = match_string(ids, -1, cid_list->ids[i].string);
773 /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
774 static const char * const acpi_ignore_dep_ids[] = {
775 "PNP0D80", /* Windows-compatible System Power Management Controller */
776 "INT33BD", /* Intel Baytrail Mailbox Device */
780 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
782 struct acpi_device *device = NULL;
786 * Fixed hardware devices do not appear in the namespace and do not
787 * have handles, but we fabricate acpi_devices for them, so we have
788 * to deal with them specially.
794 status = acpi_get_parent(handle, &handle);
795 if (ACPI_FAILURE(status))
796 return status == AE_NULL_ENTRY ? NULL : acpi_root;
797 } while (acpi_bus_get_device(handle, &device));
802 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
806 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
807 union acpi_object *obj;
809 status = acpi_get_handle(handle, "_EJD", &tmp);
810 if (ACPI_FAILURE(status))
813 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
814 if (ACPI_SUCCESS(status)) {
815 obj = buffer.pointer;
816 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
818 kfree(buffer.pointer);
822 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
824 static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
826 acpi_handle handle = dev->handle;
827 struct acpi_device_wakeup *wakeup = &dev->wakeup;
828 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
829 union acpi_object *package = NULL;
830 union acpi_object *element = NULL;
834 INIT_LIST_HEAD(&wakeup->resources);
837 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
838 if (ACPI_FAILURE(status)) {
839 acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
840 acpi_format_exception(status));
844 package = (union acpi_object *)buffer.pointer;
846 if (!package || package->package.count < 2)
849 element = &(package->package.elements[0]);
853 if (element->type == ACPI_TYPE_PACKAGE) {
854 if ((element->package.count < 2) ||
855 (element->package.elements[0].type !=
856 ACPI_TYPE_LOCAL_REFERENCE)
857 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
861 element->package.elements[0].reference.handle;
863 (u32) element->package.elements[1].integer.value;
864 } else if (element->type == ACPI_TYPE_INTEGER) {
865 wakeup->gpe_device = NULL;
866 wakeup->gpe_number = element->integer.value;
871 element = &(package->package.elements[1]);
872 if (element->type != ACPI_TYPE_INTEGER)
875 wakeup->sleep_state = element->integer.value;
877 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
881 if (!list_empty(&wakeup->resources)) {
884 err = acpi_power_wakeup_list_init(&wakeup->resources,
887 acpi_handle_warn(handle, "Retrieving current states "
888 "of wakeup power resources failed\n");
889 acpi_power_resources_list_free(&wakeup->resources);
892 if (sleep_state < wakeup->sleep_state) {
893 acpi_handle_warn(handle, "Overriding _PRW sleep state "
894 "(S%d) by S%d from power resources\n",
895 (int)wakeup->sleep_state, sleep_state);
896 wakeup->sleep_state = sleep_state;
901 kfree(buffer.pointer);
905 static bool acpi_wakeup_gpe_init(struct acpi_device *device)
907 static const struct acpi_device_id button_device_ids[] = {
908 {"PNP0C0C", 0}, /* Power button */
909 {"PNP0C0D", 0}, /* Lid */
910 {"PNP0C0E", 0}, /* Sleep button */
913 struct acpi_device_wakeup *wakeup = &device->wakeup;
916 wakeup->flags.notifier_present = 0;
918 /* Power button, Lid switch always enable wakeup */
919 if (!acpi_match_device_ids(device, button_device_ids)) {
920 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
921 /* Do not use Lid/sleep button for S5 wakeup */
922 if (wakeup->sleep_state == ACPI_STATE_S5)
923 wakeup->sleep_state = ACPI_STATE_S4;
925 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
926 device_set_wakeup_capable(&device->dev, true);
930 status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
932 return ACPI_SUCCESS(status);
935 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
939 /* Presence of _PRW indicates wake capable */
940 if (!acpi_has_method(device->handle, "_PRW"))
943 err = acpi_bus_extract_wakeup_device_power_package(device);
945 dev_err(&device->dev, "Unable to extract wakeup power resources");
949 device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
950 device->wakeup.prepare_count = 0;
952 * Call _PSW/_DSW object to disable its ability to wake the sleeping
953 * system for the ACPI device with the _PRW object.
954 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
955 * So it is necessary to call _DSW object first. Only when it is not
956 * present will the _PSW object used.
958 err = acpi_device_sleep_wake(device, 0, 0, 0);
960 pr_debug("error in _DSW or _PSW evaluation\n");
963 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
965 struct acpi_device_power_state *ps = &device->power.states[state];
966 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
967 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
970 INIT_LIST_HEAD(&ps->resources);
972 /* Evaluate "_PRx" to get referenced power resources */
973 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
974 if (ACPI_SUCCESS(status)) {
975 union acpi_object *package = buffer.pointer;
977 if (buffer.length && package
978 && package->type == ACPI_TYPE_PACKAGE
979 && package->package.count)
980 acpi_extract_power_resources(package, 0, &ps->resources);
982 ACPI_FREE(buffer.pointer);
985 /* Evaluate "_PSx" to see if we can do explicit sets */
987 if (acpi_has_method(device->handle, pathname))
988 ps->flags.explicit_set = 1;
990 /* State is valid if there are means to put the device into it. */
991 if (!list_empty(&ps->resources) || ps->flags.explicit_set)
994 ps->power = -1; /* Unknown - driver assigned */
995 ps->latency = -1; /* Unknown - driver assigned */
998 static void acpi_bus_get_power_flags(struct acpi_device *device)
1002 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1003 if (!acpi_has_method(device->handle, "_PS0") &&
1004 !acpi_has_method(device->handle, "_PR0"))
1007 device->flags.power_manageable = 1;
1010 * Power Management Flags
1012 if (acpi_has_method(device->handle, "_PSC"))
1013 device->power.flags.explicit_get = 1;
1015 if (acpi_has_method(device->handle, "_IRC"))
1016 device->power.flags.inrush_current = 1;
1018 if (acpi_has_method(device->handle, "_DSW"))
1019 device->power.flags.dsw_present = 1;
1022 * Enumerate supported power management states
1024 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1025 acpi_bus_init_power_state(device, i);
1027 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1029 /* Set the defaults for D0 and D3hot (always supported). */
1030 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1031 device->power.states[ACPI_STATE_D0].power = 100;
1032 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1035 * Use power resources only if the D0 list of them is populated, because
1036 * some platforms may provide _PR3 only to indicate D3cold support and
1037 * in those cases the power resources list returned by it may be bogus.
1039 if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1040 device->power.flags.power_resources = 1;
1042 * D3cold is supported if the D3hot list of power resources is
1045 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1046 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1049 if (acpi_bus_init_power(device))
1050 device->flags.power_manageable = 0;
1053 static void acpi_bus_get_flags(struct acpi_device *device)
1055 /* Presence of _STA indicates 'dynamic_status' */
1056 if (acpi_has_method(device->handle, "_STA"))
1057 device->flags.dynamic_status = 1;
1059 /* Presence of _RMV indicates 'removable' */
1060 if (acpi_has_method(device->handle, "_RMV"))
1061 device->flags.removable = 1;
1063 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1064 if (acpi_has_method(device->handle, "_EJD") ||
1065 acpi_has_method(device->handle, "_EJ0"))
1066 device->flags.ejectable = 1;
1069 static void acpi_device_get_busid(struct acpi_device *device)
1071 char bus_id[5] = { '?', 0 };
1072 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1078 * The device's Bus ID is simply the object name.
1079 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1081 if (ACPI_IS_ROOT_DEVICE(device)) {
1082 strcpy(device->pnp.bus_id, "ACPI");
1086 switch (device->device_type) {
1087 case ACPI_BUS_TYPE_POWER_BUTTON:
1088 strcpy(device->pnp.bus_id, "PWRF");
1090 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1091 strcpy(device->pnp.bus_id, "SLPF");
1093 case ACPI_BUS_TYPE_ECDT_EC:
1094 strcpy(device->pnp.bus_id, "ECDT");
1097 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1098 /* Clean up trailing underscores (if any) */
1099 for (i = 3; i > 1; i--) {
1100 if (bus_id[i] == '_')
1105 strcpy(device->pnp.bus_id, bus_id);
1111 * acpi_ata_match - see if an acpi object is an ATA device
1113 * If an acpi object has one of the ACPI ATA methods defined,
1114 * then we can safely call it an ATA device.
1116 bool acpi_ata_match(acpi_handle handle)
1118 return acpi_has_method(handle, "_GTF") ||
1119 acpi_has_method(handle, "_GTM") ||
1120 acpi_has_method(handle, "_STM") ||
1121 acpi_has_method(handle, "_SDD");
1125 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1127 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1128 * then we can safely call it an ejectable drive bay
1130 bool acpi_bay_match(acpi_handle handle)
1132 acpi_handle phandle;
1134 if (!acpi_has_method(handle, "_EJ0"))
1136 if (acpi_ata_match(handle))
1138 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1141 return acpi_ata_match(phandle);
1144 bool acpi_device_is_battery(struct acpi_device *adev)
1146 struct acpi_hardware_id *hwid;
1148 list_for_each_entry(hwid, &adev->pnp.ids, list)
1149 if (!strcmp("PNP0C0A", hwid->id))
1155 static bool is_ejectable_bay(struct acpi_device *adev)
1157 acpi_handle handle = adev->handle;
1159 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1162 return acpi_bay_match(handle);
1166 * acpi_dock_match - see if an acpi object has a _DCK method
1168 bool acpi_dock_match(acpi_handle handle)
1170 return acpi_has_method(handle, "_DCK");
1174 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1175 void **return_value)
1177 long *cap = context;
1179 if (acpi_has_method(handle, "_BCM") &&
1180 acpi_has_method(handle, "_BCL")) {
1181 acpi_handle_debug(handle, "Found generic backlight support\n");
1182 *cap |= ACPI_VIDEO_BACKLIGHT;
1183 /* We have backlight support, no need to scan further */
1184 return AE_CTRL_TERMINATE;
1189 /* Returns true if the ACPI object is a video device which can be
1190 * handled by video.ko.
1191 * The device will get a Linux specific CID added in scan.c to
1192 * identify the device as an ACPI graphics device
1193 * Be aware that the graphics device may not be physically present
1194 * Use acpi_video_get_capabilities() to detect general ACPI video
1195 * capabilities of present cards
1197 long acpi_is_video_device(acpi_handle handle)
1199 long video_caps = 0;
1201 /* Is this device able to support video switching ? */
1202 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1203 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1205 /* Is this device able to retrieve a video ROM ? */
1206 if (acpi_has_method(handle, "_ROM"))
1207 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1209 /* Is this device able to configure which video head to be POSTed ? */
1210 if (acpi_has_method(handle, "_VPO") &&
1211 acpi_has_method(handle, "_GPD") &&
1212 acpi_has_method(handle, "_SPD"))
1213 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1215 /* Only check for backlight functionality if one of the above hit. */
1217 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1218 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1223 EXPORT_SYMBOL(acpi_is_video_device);
1225 const char *acpi_device_hid(struct acpi_device *device)
1227 struct acpi_hardware_id *hid;
1229 if (list_empty(&device->pnp.ids))
1232 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1235 EXPORT_SYMBOL(acpi_device_hid);
1237 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1239 struct acpi_hardware_id *id;
1241 id = kmalloc(sizeof(*id), GFP_KERNEL);
1245 id->id = kstrdup_const(dev_id, GFP_KERNEL);
1251 list_add_tail(&id->list, &pnp->ids);
1252 pnp->type.hardware_id = 1;
1256 * Old IBM workstations have a DSDT bug wherein the SMBus object
1257 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1258 * prefix. Work around this.
1260 static bool acpi_ibm_smbus_match(acpi_handle handle)
1262 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1263 struct acpi_buffer path = { sizeof(node_name), node_name };
1265 if (!dmi_name_in_vendors("IBM"))
1268 /* Look for SMBS object */
1269 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1270 strcmp("SMBS", path.pointer))
1273 /* Does it have the necessary (but misnamed) methods? */
1274 if (acpi_has_method(handle, "SBI") &&
1275 acpi_has_method(handle, "SBR") &&
1276 acpi_has_method(handle, "SBW"))
1282 static bool acpi_object_is_system_bus(acpi_handle handle)
1286 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1289 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1296 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1299 struct acpi_device_info *info = NULL;
1300 struct acpi_pnp_device_id_list *cid_list;
1303 switch (device_type) {
1304 case ACPI_BUS_TYPE_DEVICE:
1305 if (handle == ACPI_ROOT_OBJECT) {
1306 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1310 acpi_get_object_info(handle, &info);
1312 pr_err(PREFIX "%s: Error reading device info\n",
1317 if (info->valid & ACPI_VALID_HID) {
1318 acpi_add_id(pnp, info->hardware_id.string);
1319 pnp->type.platform_id = 1;
1321 if (info->valid & ACPI_VALID_CID) {
1322 cid_list = &info->compatible_id_list;
1323 for (i = 0; i < cid_list->count; i++)
1324 acpi_add_id(pnp, cid_list->ids[i].string);
1326 if (info->valid & ACPI_VALID_ADR) {
1327 pnp->bus_address = info->address;
1328 pnp->type.bus_address = 1;
1330 if (info->valid & ACPI_VALID_UID)
1331 pnp->unique_id = kstrdup(info->unique_id.string,
1333 if (info->valid & ACPI_VALID_CLS)
1334 acpi_add_id(pnp, info->class_code.string);
1339 * Some devices don't reliably have _HIDs & _CIDs, so add
1340 * synthetic HIDs to make sure drivers can find them.
1342 if (acpi_is_video_device(handle))
1343 acpi_add_id(pnp, ACPI_VIDEO_HID);
1344 else if (acpi_bay_match(handle))
1345 acpi_add_id(pnp, ACPI_BAY_HID);
1346 else if (acpi_dock_match(handle))
1347 acpi_add_id(pnp, ACPI_DOCK_HID);
1348 else if (acpi_ibm_smbus_match(handle))
1349 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1350 else if (list_empty(&pnp->ids) &&
1351 acpi_object_is_system_bus(handle)) {
1352 /* \_SB, \_TZ, LNXSYBUS */
1353 acpi_add_id(pnp, ACPI_BUS_HID);
1354 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1355 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1359 case ACPI_BUS_TYPE_POWER:
1360 acpi_add_id(pnp, ACPI_POWER_HID);
1362 case ACPI_BUS_TYPE_PROCESSOR:
1363 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1365 case ACPI_BUS_TYPE_THERMAL:
1366 acpi_add_id(pnp, ACPI_THERMAL_HID);
1368 case ACPI_BUS_TYPE_POWER_BUTTON:
1369 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1371 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1372 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1374 case ACPI_BUS_TYPE_ECDT_EC:
1375 acpi_add_id(pnp, ACPI_ECDT_HID);
1380 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1382 struct acpi_hardware_id *id, *tmp;
1384 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1385 kfree_const(id->id);
1388 kfree(pnp->unique_id);
1392 * acpi_dma_supported - Check DMA support for the specified device.
1393 * @adev: The pointer to acpi device
1395 * Return false if DMA is not supported. Otherwise, return true
1397 bool acpi_dma_supported(struct acpi_device *adev)
1402 if (adev->flags.cca_seen)
1406 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1407 * DMA on "Intel platforms". Presumably that includes all x86 and
1408 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1410 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1417 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1418 * @adev: The pointer to acpi device
1420 * Return enum dev_dma_attr.
1422 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1424 if (!acpi_dma_supported(adev))
1425 return DEV_DMA_NOT_SUPPORTED;
1427 if (adev->flags.coherent_dma)
1428 return DEV_DMA_COHERENT;
1430 return DEV_DMA_NON_COHERENT;
1434 * acpi_dma_get_range() - Get device DMA parameters.
1436 * @dev: device to configure
1437 * @dma_addr: pointer device DMA address result
1438 * @offset: pointer to the DMA offset result
1439 * @size: pointer to DMA range size result
1441 * Evaluate DMA regions and return respectively DMA region start, offset
1442 * and size in dma_addr, offset and size on parsing success; it does not
1443 * update the passed in values on failure.
1445 * Return 0 on success, < 0 on failure.
1447 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1450 struct acpi_device *adev;
1452 struct resource_entry *rentry;
1454 struct device *dma_dev = dev;
1455 u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1458 * Walk the device tree chasing an ACPI companion with a _DMA
1459 * object while we go. Stop if we find a device with an ACPI
1460 * companion containing a _DMA method.
1463 adev = ACPI_COMPANION(dma_dev);
1464 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1467 dma_dev = dma_dev->parent;
1473 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1474 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1478 ret = acpi_dev_get_dma_resources(adev, &list);
1480 list_for_each_entry(rentry, &list, node) {
1481 if (dma_offset && rentry->offset != dma_offset) {
1483 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1486 dma_offset = rentry->offset;
1488 /* Take lower and upper limits */
1489 if (rentry->res->start < dma_start)
1490 dma_start = rentry->res->start;
1491 if (rentry->res->end > dma_end)
1492 dma_end = rentry->res->end;
1495 if (dma_start >= dma_end) {
1497 dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1501 *dma_addr = dma_start - dma_offset;
1502 len = dma_end - dma_start;
1503 *size = max(len, len + 1);
1504 *offset = dma_offset;
1507 acpi_dev_free_resource_list(&list);
1509 return ret >= 0 ? 0 : ret;
1513 * acpi_dma_configure_id - Set-up DMA configuration for the device.
1514 * @dev: The pointer to the device
1515 * @attr: device dma attributes
1516 * @input_id: input device id const value pointer
1518 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1519 const u32 *input_id)
1521 const struct iommu_ops *iommu;
1522 u64 dma_addr = 0, size = 0;
1524 if (attr == DEV_DMA_NOT_SUPPORTED) {
1525 set_dma_ops(dev, &dma_dummy_ops);
1529 iort_dma_setup(dev, &dma_addr, &size);
1531 iommu = iort_iommu_configure_id(dev, input_id);
1532 if (PTR_ERR(iommu) == -EPROBE_DEFER)
1533 return -EPROBE_DEFER;
1535 arch_setup_dma_ops(dev, dma_addr, size,
1536 iommu, attr == DEV_DMA_COHERENT);
1540 EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1542 static void acpi_init_coherency(struct acpi_device *adev)
1544 unsigned long long cca = 0;
1546 struct acpi_device *parent = adev->parent;
1548 if (parent && parent->flags.cca_seen) {
1550 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1553 adev->flags.cca_seen = 1;
1554 cca = parent->flags.coherent_dma;
1556 status = acpi_evaluate_integer(adev->handle, "_CCA",
1558 if (ACPI_SUCCESS(status))
1559 adev->flags.cca_seen = 1;
1560 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1562 * If architecture does not specify that _CCA is
1563 * required for DMA-able devices (e.g. x86),
1564 * we default to _CCA=1.
1568 acpi_handle_debug(adev->handle,
1569 "ACPI device is missing _CCA.\n");
1572 adev->flags.coherent_dma = cca;
1575 static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1577 bool *is_serial_bus_slave_p = data;
1579 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1582 *is_serial_bus_slave_p = true;
1584 /* no need to do more checking */
1588 static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1590 struct acpi_device *parent = device->parent;
1591 static const struct acpi_device_id indirect_io_hosts[] = {
1596 return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1599 static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1601 struct list_head resource_list;
1602 bool is_serial_bus_slave = false;
1604 * These devices have multiple I2cSerialBus resources and an i2c-client
1605 * must be instantiated for each, each with its own i2c_device_id.
1606 * Normally we only instantiate an i2c-client for the first resource,
1607 * using the ACPI HID as id. These special cases are handled by the
1608 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1609 * which i2c_device_id to use for each resource.
1611 static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1619 if (acpi_is_indirect_io_slave(device))
1622 /* Macs use device properties in lieu of _CRS resources */
1623 if (x86_apple_machine &&
1624 (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1625 fwnode_property_present(&device->fwnode, "i2cAddress") ||
1626 fwnode_property_present(&device->fwnode, "baud")))
1629 /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1630 if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1633 INIT_LIST_HEAD(&resource_list);
1634 acpi_dev_get_resources(device, &resource_list,
1635 acpi_check_serial_bus_slave,
1636 &is_serial_bus_slave);
1637 acpi_dev_free_resource_list(&resource_list);
1639 return is_serial_bus_slave;
1642 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1645 INIT_LIST_HEAD(&device->pnp.ids);
1646 device->device_type = type;
1647 device->handle = handle;
1648 device->parent = acpi_bus_get_parent(handle);
1649 fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1650 acpi_set_device_status(device, ACPI_STA_DEFAULT);
1651 acpi_device_get_busid(device);
1652 acpi_set_pnp_ids(handle, &device->pnp, type);
1653 acpi_init_properties(device);
1654 acpi_bus_get_flags(device);
1655 device->flags.match_driver = false;
1656 device->flags.initialized = true;
1657 device->flags.enumeration_by_parent =
1658 acpi_device_enumeration_by_parent(device);
1659 acpi_device_clear_enumerated(device);
1660 device_initialize(&device->dev);
1661 dev_set_uevent_suppress(&device->dev, true);
1662 acpi_init_coherency(device);
1665 static void acpi_scan_dep_init(struct acpi_device *adev)
1667 struct acpi_dep_data *dep;
1669 mutex_lock(&acpi_dep_list_lock);
1671 list_for_each_entry(dep, &acpi_dep_list, node) {
1672 if (dep->consumer == adev->handle)
1676 mutex_unlock(&acpi_dep_list_lock);
1679 void acpi_device_add_finalize(struct acpi_device *device)
1681 dev_set_uevent_suppress(&device->dev, false);
1682 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1685 static void acpi_scan_init_status(struct acpi_device *adev)
1687 if (acpi_bus_get_status(adev))
1688 acpi_set_device_status(adev, 0);
1691 static int acpi_add_single_object(struct acpi_device **child,
1692 acpi_handle handle, int type, bool dep_init)
1694 struct acpi_device *device;
1697 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1701 acpi_init_device_object(device, handle, type);
1703 * Getting the status is delayed till here so that we can call
1704 * acpi_bus_get_status() and use its quirk handling. Note that
1705 * this must be done before the get power-/wakeup_dev-flags calls.
1707 if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
1709 acpi_scan_dep_init(device);
1711 acpi_scan_init_status(device);
1714 acpi_bus_get_power_flags(device);
1715 acpi_bus_get_wakeup_device_flags(device);
1717 result = acpi_device_add(device, acpi_device_release);
1719 acpi_device_release(&device->dev);
1723 acpi_power_add_remove_device(device, true);
1724 acpi_device_add_finalize(device);
1726 acpi_handle_debug(handle, "Added as %s, parent %s\n",
1727 dev_name(&device->dev), device->parent ?
1728 dev_name(&device->parent->dev) : "(null)");
1734 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1737 struct resource *res = context;
1739 if (acpi_dev_resource_memory(ares, res))
1740 return AE_CTRL_TERMINATE;
1745 static bool acpi_device_should_be_hidden(acpi_handle handle)
1748 struct resource res;
1750 /* Check if it should ignore the UART device */
1751 if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1755 * The UART device described in SPCR table is assumed to have only one
1756 * memory resource present. So we only look for the first one here.
1758 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1759 acpi_get_resource_memory, &res);
1760 if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1763 acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1769 bool acpi_device_is_present(const struct acpi_device *adev)
1771 return adev->status.present || adev->status.functional;
1774 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1776 const struct acpi_device_id **matchid)
1778 const struct acpi_device_id *devid;
1781 return handler->match(idstr, matchid);
1783 for (devid = handler->ids; devid->id[0]; devid++)
1784 if (!strcmp((char *)devid->id, idstr)) {
1794 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1795 const struct acpi_device_id **matchid)
1797 struct acpi_scan_handler *handler;
1799 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1800 if (acpi_scan_handler_matching(handler, idstr, matchid))
1806 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1808 if (!!hotplug->enabled == !!val)
1811 mutex_lock(&acpi_scan_lock);
1813 hotplug->enabled = val;
1815 mutex_unlock(&acpi_scan_lock);
1818 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1820 struct acpi_hardware_id *hwid;
1822 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1823 acpi_dock_add(adev);
1826 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1827 struct acpi_scan_handler *handler;
1829 handler = acpi_scan_match_handler(hwid->id, NULL);
1831 adev->flags.hotplug_notify = true;
1837 static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
1839 struct acpi_handle_list dep_devices;
1845 * Check for _HID here to avoid deferring the enumeration of:
1847 * 2. ACPI nodes describing USB ports.
1848 * Still, checking for _HID catches more then just these cases ...
1850 if (!check_dep || !acpi_has_method(handle, "_DEP") ||
1851 !acpi_has_method(handle, "_HID"))
1854 status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
1855 if (ACPI_FAILURE(status)) {
1856 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
1860 for (count = 0, i = 0; i < dep_devices.count; i++) {
1861 struct acpi_device_info *info;
1862 struct acpi_dep_data *dep;
1865 status = acpi_get_object_info(dep_devices.handles[i], &info);
1866 if (ACPI_FAILURE(status)) {
1867 acpi_handle_debug(handle, "Error reading _DEP device info\n");
1871 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
1877 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1883 dep->supplier = dep_devices.handles[i];
1884 dep->consumer = handle;
1886 mutex_lock(&acpi_dep_list_lock);
1887 list_add_tail(&dep->node , &acpi_dep_list);
1888 mutex_unlock(&acpi_dep_list_lock);
1894 static bool acpi_bus_scan_second_pass;
1896 static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
1897 struct acpi_device **adev_p)
1899 struct acpi_device *device = NULL;
1900 acpi_object_type acpi_type;
1903 acpi_bus_get_device(handle, &device);
1907 if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
1910 switch (acpi_type) {
1911 case ACPI_TYPE_DEVICE:
1912 if (acpi_device_should_be_hidden(handle))
1915 /* Bail out if there are dependencies. */
1916 if (acpi_scan_check_dep(handle, check_dep) > 0) {
1917 acpi_bus_scan_second_pass = true;
1918 return AE_CTRL_DEPTH;
1922 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1923 type = ACPI_BUS_TYPE_DEVICE;
1926 case ACPI_TYPE_PROCESSOR:
1927 type = ACPI_BUS_TYPE_PROCESSOR;
1930 case ACPI_TYPE_THERMAL:
1931 type = ACPI_BUS_TYPE_THERMAL;
1934 case ACPI_TYPE_POWER:
1935 acpi_add_power_resource(handle);
1942 * If check_dep is true at this point, the device has no dependencies,
1943 * or the creation of the device object would have been postponed above.
1945 acpi_add_single_object(&device, handle, type, !check_dep);
1947 return AE_CTRL_DEPTH;
1949 acpi_scan_init_hotplug(device);
1958 static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
1959 void *not_used, void **ret_p)
1961 return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
1964 static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
1965 void *not_used, void **ret_p)
1967 return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
1970 static void acpi_default_enumeration(struct acpi_device *device)
1973 * Do not enumerate devices with enumeration_by_parent flag set as
1974 * they will be enumerated by their respective parents.
1976 if (!device->flags.enumeration_by_parent) {
1977 acpi_create_platform_device(device, NULL);
1978 acpi_device_set_enumerated(device);
1980 blocking_notifier_call_chain(&acpi_reconfig_chain,
1981 ACPI_RECONFIG_DEVICE_ADD, device);
1985 static const struct acpi_device_id generic_device_ids[] = {
1986 {ACPI_DT_NAMESPACE_HID, },
1990 static int acpi_generic_device_attach(struct acpi_device *adev,
1991 const struct acpi_device_id *not_used)
1994 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
1995 * below can be unconditional.
1997 if (adev->data.of_compatible)
1998 acpi_default_enumeration(adev);
2003 static struct acpi_scan_handler generic_device_handler = {
2004 .ids = generic_device_ids,
2005 .attach = acpi_generic_device_attach,
2008 static int acpi_scan_attach_handler(struct acpi_device *device)
2010 struct acpi_hardware_id *hwid;
2013 list_for_each_entry(hwid, &device->pnp.ids, list) {
2014 const struct acpi_device_id *devid;
2015 struct acpi_scan_handler *handler;
2017 handler = acpi_scan_match_handler(hwid->id, &devid);
2019 if (!handler->attach) {
2020 device->pnp.type.platform_id = 0;
2023 device->handler = handler;
2024 ret = handler->attach(device, devid);
2028 device->handler = NULL;
2037 static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
2039 struct acpi_device *child;
2040 bool skip = !first_pass && device->flags.visited;
2047 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2048 register_dock_dependent_device(device, ejd);
2050 acpi_bus_get_status(device);
2051 /* Skip devices that are not present. */
2052 if (!acpi_device_is_present(device)) {
2053 device->flags.initialized = false;
2054 acpi_device_clear_enumerated(device);
2055 device->flags.power_manageable = 0;
2058 if (device->handler)
2061 if (!device->flags.initialized) {
2062 device->flags.power_manageable =
2063 device->power.states[ACPI_STATE_D0].flags.valid;
2064 if (acpi_bus_init_power(device))
2065 device->flags.power_manageable = 0;
2067 device->flags.initialized = true;
2068 } else if (device->flags.visited) {
2072 ret = acpi_scan_attach_handler(device);
2076 device->flags.match_driver = true;
2077 if (ret > 0 && !device->flags.enumeration_by_parent) {
2078 acpi_device_set_enumerated(device);
2082 ret = device_attach(&device->dev);
2086 if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2087 acpi_default_enumeration(device);
2089 acpi_device_set_enumerated(device);
2092 list_for_each_entry(child, &device->children, node)
2093 acpi_bus_attach(child, first_pass);
2095 if (!skip && device->handler && device->handler->hotplug.notify_online)
2096 device->handler->hotplug.notify_online(device);
2099 static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
2101 struct acpi_device *adev;
2103 adev = acpi_bus_get_acpi_device(dep->consumer);
2105 *(struct acpi_device **)data = adev;
2108 /* Continue parsing if the device object is not present. */
2112 struct acpi_scan_clear_dep_work {
2113 struct work_struct work;
2114 struct acpi_device *adev;
2117 static void acpi_scan_clear_dep_fn(struct work_struct *work)
2119 struct acpi_scan_clear_dep_work *cdw;
2121 cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
2123 acpi_scan_lock_acquire();
2124 acpi_bus_attach(cdw->adev, true);
2125 acpi_scan_lock_release();
2127 acpi_dev_put(cdw->adev);
2131 static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2133 struct acpi_scan_clear_dep_work *cdw;
2135 if (adev->dep_unmet)
2138 cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2143 INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2145 * Since the work function may block on the lock until the entire
2146 * initial enumeration of devices is complete, put it into the unbound
2149 queue_work(system_unbound_wq, &cdw->work);
2154 static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
2156 struct acpi_device *adev = acpi_bus_get_acpi_device(dep->consumer);
2160 if (!acpi_scan_clear_dep_queue(adev))
2164 list_del(&dep->node);
2171 * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2172 * @handle: The ACPI handle of the supplier device
2173 * @callback: Pointer to the callback function to apply
2174 * @data: Pointer to some data to pass to the callback
2176 * The return value of the callback determines this function's behaviour. If 0
2177 * is returned we continue to iterate over acpi_dep_list. If a positive value
2178 * is returned then the loop is broken but this function returns 0. If a
2179 * negative value is returned by the callback then the loop is broken and that
2180 * value is returned as the final error.
2182 static int acpi_walk_dep_device_list(acpi_handle handle,
2183 int (*callback)(struct acpi_dep_data *, void *),
2186 struct acpi_dep_data *dep, *tmp;
2189 mutex_lock(&acpi_dep_list_lock);
2190 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2191 if (dep->supplier == handle) {
2192 ret = callback(dep, data);
2197 mutex_unlock(&acpi_dep_list_lock);
2199 return ret > 0 ? 0 : ret;
2203 * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2204 * @supplier: Pointer to the supplier &struct acpi_device
2206 * Clear dependencies on the given device.
2208 void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2210 acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2212 EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2215 * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2216 * @supplier: Pointer to the dependee device
2218 * Returns the first &struct acpi_device which declares itself dependent on
2219 * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2221 * The caller is responsible for putting the reference to adev when it is no
2224 struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2226 struct acpi_device *adev = NULL;
2228 acpi_walk_dep_device_list(supplier->handle,
2229 acpi_dev_get_first_consumer_dev_cb, &adev);
2233 EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
2236 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2237 * @handle: Root of the namespace scope to scan.
2239 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2242 * If no devices were found, -ENODEV is returned, but it does not mean that
2243 * there has been a real error. There just have been no suitable ACPI objects
2244 * in the table trunk from which the kernel could create a device and add an
2245 * appropriate driver.
2247 * Must be called under acpi_scan_lock.
2249 int acpi_bus_scan(acpi_handle handle)
2251 struct acpi_device *device = NULL;
2253 acpi_bus_scan_second_pass = false;
2255 /* Pass 1: Avoid enumerating devices with missing dependencies. */
2257 if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
2258 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2259 acpi_bus_check_add_1, NULL, NULL,
2265 acpi_bus_attach(device, true);
2267 if (!acpi_bus_scan_second_pass)
2270 /* Pass 2: Enumerate all of the remaining devices. */
2274 if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
2275 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2276 acpi_bus_check_add_2, NULL, NULL,
2279 acpi_bus_attach(device, false);
2283 EXPORT_SYMBOL(acpi_bus_scan);
2286 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2287 * @adev: Root of the ACPI namespace scope to walk.
2289 * Must be called under acpi_scan_lock.
2291 void acpi_bus_trim(struct acpi_device *adev)
2293 struct acpi_scan_handler *handler = adev->handler;
2294 struct acpi_device *child;
2296 list_for_each_entry_reverse(child, &adev->children, node)
2297 acpi_bus_trim(child);
2299 adev->flags.match_driver = false;
2301 if (handler->detach)
2302 handler->detach(adev);
2304 adev->handler = NULL;
2306 device_release_driver(&adev->dev);
2309 * Most likely, the device is going away, so put it into D3cold before
2312 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2313 adev->flags.initialized = false;
2314 acpi_device_clear_enumerated(adev);
2316 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2318 int acpi_bus_register_early_device(int type)
2320 struct acpi_device *device = NULL;
2323 result = acpi_add_single_object(&device, NULL, type, false);
2327 device->flags.match_driver = true;
2328 return device_attach(&device->dev);
2330 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2332 static int acpi_bus_scan_fixed(void)
2337 * Enumerate all fixed-feature devices.
2339 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2340 struct acpi_device *device = NULL;
2342 result = acpi_add_single_object(&device, NULL,
2343 ACPI_BUS_TYPE_POWER_BUTTON, false);
2347 device->flags.match_driver = true;
2348 result = device_attach(&device->dev);
2352 device_init_wakeup(&device->dev, true);
2355 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2356 struct acpi_device *device = NULL;
2358 result = acpi_add_single_object(&device, NULL,
2359 ACPI_BUS_TYPE_SLEEP_BUTTON, false);
2363 device->flags.match_driver = true;
2364 result = device_attach(&device->dev);
2367 return result < 0 ? result : 0;
2370 static void __init acpi_get_spcr_uart_addr(void)
2373 struct acpi_table_spcr *spcr_ptr;
2375 status = acpi_get_table(ACPI_SIG_SPCR, 0,
2376 (struct acpi_table_header **)&spcr_ptr);
2377 if (ACPI_FAILURE(status)) {
2378 pr_warn(PREFIX "STAO table present, but SPCR is missing\n");
2382 spcr_uart_addr = spcr_ptr->serial_port.address;
2383 acpi_put_table((struct acpi_table_header *)spcr_ptr);
2386 static bool acpi_scan_initialized;
2388 int __init acpi_scan_init(void)
2392 struct acpi_table_stao *stao_ptr;
2394 acpi_pci_root_init();
2395 acpi_pci_link_init();
2396 acpi_processor_init();
2397 acpi_platform_init();
2400 acpi_cmos_rtc_init();
2401 acpi_container_init();
2402 acpi_memory_hotplug_init();
2403 acpi_watchdog_init();
2405 acpi_int340x_thermal_init();
2409 acpi_scan_add_handler(&generic_device_handler);
2412 * If there is STAO table, check whether it needs to ignore the UART
2413 * device in SPCR table.
2415 status = acpi_get_table(ACPI_SIG_STAO, 0,
2416 (struct acpi_table_header **)&stao_ptr);
2417 if (ACPI_SUCCESS(status)) {
2418 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2419 pr_info(PREFIX "STAO Name List not yet supported.\n");
2421 if (stao_ptr->ignore_uart)
2422 acpi_get_spcr_uart_addr();
2424 acpi_put_table((struct acpi_table_header *)stao_ptr);
2427 acpi_gpe_apply_masked_gpes();
2428 acpi_update_all_gpes();
2431 * Although we call __add_memory() that is documented to require the
2432 * device_hotplug_lock, it is not necessary here because this is an
2433 * early code when userspace or any other code path cannot trigger
2434 * hotplug/hotunplug operations.
2436 mutex_lock(&acpi_scan_lock);
2438 * Enumerate devices in the ACPI namespace.
2440 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2444 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2448 /* Fixed feature devices do not exist on HW-reduced platform */
2449 if (!acpi_gbl_reduced_hardware) {
2450 result = acpi_bus_scan_fixed();
2452 acpi_detach_data(acpi_root->handle,
2453 acpi_scan_drop_device);
2454 acpi_device_del(acpi_root);
2455 acpi_bus_put_acpi_device(acpi_root);
2460 acpi_turn_off_unused_power_resources();
2462 acpi_scan_initialized = true;
2465 mutex_unlock(&acpi_scan_lock);
2469 static struct acpi_probe_entry *ape;
2470 static int acpi_probe_count;
2471 static DEFINE_MUTEX(acpi_probe_mutex);
2473 static int __init acpi_match_madt(union acpi_subtable_headers *header,
2474 const unsigned long end)
2476 if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2477 if (!ape->probe_subtbl(header, end))
2483 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2490 mutex_lock(&acpi_probe_mutex);
2491 for (ape = ap_head; nr; ape++, nr--) {
2492 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2493 acpi_probe_count = 0;
2494 acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2495 count += acpi_probe_count;
2498 res = acpi_table_parse(ape->id, ape->probe_table);
2503 mutex_unlock(&acpi_probe_mutex);
2508 struct acpi_table_events_work {
2509 struct work_struct work;
2514 static void acpi_table_events_fn(struct work_struct *work)
2516 struct acpi_table_events_work *tew;
2518 tew = container_of(work, struct acpi_table_events_work, work);
2520 if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2521 acpi_scan_lock_acquire();
2522 acpi_bus_scan(ACPI_ROOT_OBJECT);
2523 acpi_scan_lock_release();
2529 void acpi_scan_table_handler(u32 event, void *table, void *context)
2531 struct acpi_table_events_work *tew;
2533 if (!acpi_scan_initialized)
2536 if (event != ACPI_TABLE_EVENT_LOAD)
2539 tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2543 INIT_WORK(&tew->work, acpi_table_events_fn);
2547 schedule_work(&tew->work);
2550 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2552 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2554 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2556 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2558 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2560 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);