1 // SPDX-License-Identifier: GPL-2.0-only
3 * scan.c - support for transforming the ACPI namespace into individual objects
6 #define pr_fmt(fmt) "ACPI: " fmt
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/acpi.h>
13 #include <linux/acpi_iort.h>
14 #include <linux/signal.h>
15 #include <linux/kthread.h>
16 #include <linux/dmi.h>
17 #include <linux/nls.h>
18 #include <linux/dma-map-ops.h>
19 #include <linux/platform_data/x86/apple.h>
20 #include <linux/pgtable.h>
24 extern struct acpi_device *acpi_root;
26 #define ACPI_BUS_CLASS "system_bus"
27 #define ACPI_BUS_HID "LNXSYBUS"
28 #define ACPI_BUS_DEVICE_NAME "System Bus"
30 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
32 #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
34 static const char *dummy_hid = "device";
36 static LIST_HEAD(acpi_dep_list);
37 static DEFINE_MUTEX(acpi_dep_list_lock);
38 LIST_HEAD(acpi_bus_id_list);
39 static DEFINE_MUTEX(acpi_scan_lock);
40 static LIST_HEAD(acpi_scan_handlers_list);
41 DEFINE_MUTEX(acpi_device_lock);
42 LIST_HEAD(acpi_wakeup_device_list);
43 static DEFINE_MUTEX(acpi_hp_context_lock);
46 * The UART device described by the SPCR table is the only object which needs
47 * special-casing. Everything else is covered by ACPI namespace paths in STAO
50 static u64 spcr_uart_addr;
52 void acpi_scan_lock_acquire(void)
54 mutex_lock(&acpi_scan_lock);
56 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
58 void acpi_scan_lock_release(void)
60 mutex_unlock(&acpi_scan_lock);
62 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
64 void acpi_lock_hp_context(void)
66 mutex_lock(&acpi_hp_context_lock);
69 void acpi_unlock_hp_context(void)
71 mutex_unlock(&acpi_hp_context_lock);
74 void acpi_initialize_hp_context(struct acpi_device *adev,
75 struct acpi_hotplug_context *hp,
76 int (*notify)(struct acpi_device *, u32),
77 void (*uevent)(struct acpi_device *, u32))
79 acpi_lock_hp_context();
82 acpi_set_hp_context(adev, hp);
83 acpi_unlock_hp_context();
85 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
87 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
92 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
96 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
97 const char *hotplug_profile_name)
101 error = acpi_scan_add_handler(handler);
105 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
109 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
111 struct acpi_device_physical_node *pn;
113 char *envp[] = { "EVENT=offline", NULL };
116 * acpi_container_offline() calls this for all of the container's
117 * children under the container's physical_node_lock lock.
119 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
121 list_for_each_entry(pn, &adev->physical_node_list, node)
122 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
124 kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
130 mutex_unlock(&adev->physical_node_lock);
134 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
137 struct acpi_device *device = NULL;
138 struct acpi_device_physical_node *pn;
139 bool second_pass = (bool)data;
140 acpi_status status = AE_OK;
142 if (acpi_bus_get_device(handle, &device))
145 if (device->handler && !device->handler->hotplug.enabled) {
146 *ret_p = &device->dev;
150 mutex_lock(&device->physical_node_lock);
152 list_for_each_entry(pn, &device->physical_node_list, node) {
156 /* Skip devices offlined by the first pass. */
160 pn->put_online = false;
162 ret = device_offline(pn->dev);
164 pn->put_online = !ret;
174 mutex_unlock(&device->physical_node_lock);
179 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
182 struct acpi_device *device = NULL;
183 struct acpi_device_physical_node *pn;
185 if (acpi_bus_get_device(handle, &device))
188 mutex_lock(&device->physical_node_lock);
190 list_for_each_entry(pn, &device->physical_node_list, node)
191 if (pn->put_online) {
192 device_online(pn->dev);
193 pn->put_online = false;
196 mutex_unlock(&device->physical_node_lock);
201 static int acpi_scan_try_to_offline(struct acpi_device *device)
203 acpi_handle handle = device->handle;
204 struct device *errdev = NULL;
208 * Carry out two passes here and ignore errors in the first pass,
209 * because if the devices in question are memory blocks and
210 * CONFIG_MEMCG is set, one of the blocks may hold data structures
211 * that the other blocks depend on, but it is not known in advance which
214 * If the first pass is successful, the second one isn't needed, though.
216 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
217 NULL, acpi_bus_offline, (void *)false,
219 if (status == AE_SUPPORT) {
220 dev_warn(errdev, "Offline disabled.\n");
221 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
222 acpi_bus_online, NULL, NULL, NULL);
225 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
228 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
229 NULL, acpi_bus_offline, (void *)true,
232 acpi_bus_offline(handle, 0, (void *)true,
236 dev_warn(errdev, "Offline failed.\n");
237 acpi_bus_online(handle, 0, NULL, NULL);
238 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
239 ACPI_UINT32_MAX, acpi_bus_online,
247 static int acpi_scan_hot_remove(struct acpi_device *device)
249 acpi_handle handle = device->handle;
250 unsigned long long sta;
253 if (device->handler && device->handler->hotplug.demand_offline) {
254 if (!acpi_scan_is_offline(device, true))
257 int error = acpi_scan_try_to_offline(device);
262 acpi_handle_debug(handle, "Ejecting\n");
264 acpi_bus_trim(device);
266 acpi_evaluate_lck(handle, 0);
270 status = acpi_evaluate_ej0(handle);
271 if (status == AE_NOT_FOUND)
273 else if (ACPI_FAILURE(status))
277 * Verify if eject was indeed successful. If not, log an error
278 * message. No need to call _OST since _EJ0 call was made OK.
280 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
281 if (ACPI_FAILURE(status)) {
282 acpi_handle_warn(handle,
283 "Status check after eject failed (0x%x)\n", status);
284 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
285 acpi_handle_warn(handle,
286 "Eject incomplete - status 0x%llx\n", sta);
292 static int acpi_scan_device_not_present(struct acpi_device *adev)
294 if (!acpi_device_enumerated(adev)) {
295 dev_warn(&adev->dev, "Still not present\n");
302 static int acpi_scan_device_check(struct acpi_device *adev)
306 acpi_bus_get_status(adev);
307 if (adev->status.present || adev->status.functional) {
309 * This function is only called for device objects for which
310 * matching scan handlers exist. The only situation in which
311 * the scan handler is not attached to this device object yet
312 * is when the device has just appeared (either it wasn't
313 * present at all before or it was removed and then added
317 dev_warn(&adev->dev, "Already enumerated\n");
320 error = acpi_bus_scan(adev->handle);
322 dev_warn(&adev->dev, "Namespace scan failure\n");
325 if (!adev->handler) {
326 dev_warn(&adev->dev, "Enumeration failure\n");
330 error = acpi_scan_device_not_present(adev);
335 static int acpi_scan_bus_check(struct acpi_device *adev)
337 struct acpi_scan_handler *handler = adev->handler;
338 struct acpi_device *child;
341 acpi_bus_get_status(adev);
342 if (!(adev->status.present || adev->status.functional)) {
343 acpi_scan_device_not_present(adev);
346 if (handler && handler->hotplug.scan_dependent)
347 return handler->hotplug.scan_dependent(adev);
349 error = acpi_bus_scan(adev->handle);
351 dev_warn(&adev->dev, "Namespace scan failure\n");
354 list_for_each_entry(child, &adev->children, node) {
355 error = acpi_scan_bus_check(child);
362 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
365 case ACPI_NOTIFY_BUS_CHECK:
366 return acpi_scan_bus_check(adev);
367 case ACPI_NOTIFY_DEVICE_CHECK:
368 return acpi_scan_device_check(adev);
369 case ACPI_NOTIFY_EJECT_REQUEST:
370 case ACPI_OST_EC_OSPM_EJECT:
371 if (adev->handler && !adev->handler->hotplug.enabled) {
372 dev_info(&adev->dev, "Eject disabled\n");
375 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
376 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
377 return acpi_scan_hot_remove(adev);
382 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
384 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
387 lock_device_hotplug();
388 mutex_lock(&acpi_scan_lock);
391 * The device object's ACPI handle cannot become invalid as long as we
392 * are holding acpi_scan_lock, but it might have become invalid before
393 * that lock was acquired.
395 if (adev->handle == INVALID_ACPI_HANDLE)
398 if (adev->flags.is_dock_station) {
399 error = dock_notify(adev, src);
400 } else if (adev->flags.hotplug_notify) {
401 error = acpi_generic_hotplug_event(adev, src);
403 int (*notify)(struct acpi_device *, u32);
405 acpi_lock_hp_context();
406 notify = adev->hp ? adev->hp->notify : NULL;
407 acpi_unlock_hp_context();
409 * There may be additional notify handlers for device objects
410 * without the .event() callback, so ignore them here.
413 error = notify(adev, src);
419 ost_code = ACPI_OST_SC_SUCCESS;
422 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
425 ost_code = ACPI_OST_SC_DEVICE_BUSY;
428 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
433 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
436 acpi_bus_put_acpi_device(adev);
437 mutex_unlock(&acpi_scan_lock);
438 unlock_device_hotplug();
441 static void acpi_free_power_resources_lists(struct acpi_device *device)
445 if (device->wakeup.flags.valid)
446 acpi_power_resources_list_free(&device->wakeup.resources);
448 if (!device->power.flags.power_resources)
451 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
452 struct acpi_device_power_state *ps = &device->power.states[i];
453 acpi_power_resources_list_free(&ps->resources);
457 static void acpi_device_release(struct device *dev)
459 struct acpi_device *acpi_dev = to_acpi_device(dev);
461 acpi_free_properties(acpi_dev);
462 acpi_free_pnp_ids(&acpi_dev->pnp);
463 acpi_free_power_resources_lists(acpi_dev);
467 static void acpi_device_del(struct acpi_device *device)
469 struct acpi_device_bus_id *acpi_device_bus_id;
471 mutex_lock(&acpi_device_lock);
473 list_del(&device->node);
475 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
476 if (!strcmp(acpi_device_bus_id->bus_id,
477 acpi_device_hid(device))) {
478 ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
479 if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
480 list_del(&acpi_device_bus_id->node);
481 kfree_const(acpi_device_bus_id->bus_id);
482 kfree(acpi_device_bus_id);
487 list_del(&device->wakeup_list);
488 mutex_unlock(&acpi_device_lock);
490 acpi_power_add_remove_device(device, false);
491 acpi_device_remove_files(device);
493 device->remove(device);
495 device_del(&device->dev);
498 static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
500 static LIST_HEAD(acpi_device_del_list);
501 static DEFINE_MUTEX(acpi_device_del_lock);
503 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
506 struct acpi_device *adev;
508 mutex_lock(&acpi_device_del_lock);
510 if (list_empty(&acpi_device_del_list)) {
511 mutex_unlock(&acpi_device_del_lock);
514 adev = list_first_entry(&acpi_device_del_list,
515 struct acpi_device, del_list);
516 list_del(&adev->del_list);
518 mutex_unlock(&acpi_device_del_lock);
520 blocking_notifier_call_chain(&acpi_reconfig_chain,
521 ACPI_RECONFIG_DEVICE_REMOVE, adev);
523 acpi_device_del(adev);
525 * Drop references to all power resources that might have been
526 * used by the device.
528 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
534 * acpi_scan_drop_device - Drop an ACPI device object.
535 * @handle: Handle of an ACPI namespace node, not used.
536 * @context: Address of the ACPI device object to drop.
538 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
539 * namespace node the device object pointed to by @context is attached to.
541 * The unregistration is carried out asynchronously to avoid running
542 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
543 * ensure the correct ordering (the device objects must be unregistered in the
544 * same order in which the corresponding namespace nodes are deleted).
546 static void acpi_scan_drop_device(acpi_handle handle, void *context)
548 static DECLARE_WORK(work, acpi_device_del_work_fn);
549 struct acpi_device *adev = context;
551 mutex_lock(&acpi_device_del_lock);
554 * Use the ACPI hotplug workqueue which is ordered, so this work item
555 * won't run after any hotplug work items submitted subsequently. That
556 * prevents attempts to register device objects identical to those being
557 * deleted from happening concurrently (such attempts result from
558 * hotplug events handled via the ACPI hotplug workqueue). It also will
559 * run after all of the work items submitted previously, which helps
560 * those work items to ensure that they are not accessing stale device
563 if (list_empty(&acpi_device_del_list))
564 acpi_queue_hotplug_work(&work);
566 list_add_tail(&adev->del_list, &acpi_device_del_list);
567 /* Make acpi_ns_validate_handle() return NULL for this handle. */
568 adev->handle = INVALID_ACPI_HANDLE;
570 mutex_unlock(&acpi_device_del_lock);
573 static struct acpi_device *handle_to_device(acpi_handle handle,
574 void (*callback)(void *))
576 struct acpi_device *adev = NULL;
579 status = acpi_get_data_full(handle, acpi_scan_drop_device,
580 (void **)&adev, callback);
581 if (ACPI_FAILURE(status) || !adev) {
582 acpi_handle_debug(handle, "No context!\n");
588 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
593 *device = handle_to_device(handle, NULL);
599 EXPORT_SYMBOL(acpi_bus_get_device);
601 static void get_acpi_device(void *dev)
606 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
608 return handle_to_device(handle, get_acpi_device);
611 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
613 struct acpi_device_bus_id *acpi_device_bus_id;
615 /* Find suitable bus_id and instance number in acpi_bus_id_list. */
616 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
617 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
618 return acpi_device_bus_id;
623 static int acpi_device_set_name(struct acpi_device *device,
624 struct acpi_device_bus_id *acpi_device_bus_id)
626 struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
629 result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
633 device->pnp.instance_no = result;
634 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
638 static int acpi_tie_acpi_dev(struct acpi_device *adev)
640 acpi_handle handle = adev->handle;
646 status = acpi_attach_data(handle, acpi_scan_drop_device, adev);
647 if (ACPI_FAILURE(status)) {
648 acpi_handle_err(handle, "Unable to attach device data\n");
655 static int __acpi_device_add(struct acpi_device *device,
656 void (*release)(struct device *))
658 struct acpi_device_bus_id *acpi_device_bus_id;
664 * Link this device to its parent and siblings.
666 INIT_LIST_HEAD(&device->children);
667 INIT_LIST_HEAD(&device->node);
668 INIT_LIST_HEAD(&device->wakeup_list);
669 INIT_LIST_HEAD(&device->physical_node_list);
670 INIT_LIST_HEAD(&device->del_list);
671 mutex_init(&device->physical_node_lock);
673 mutex_lock(&acpi_device_lock);
675 acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
676 if (acpi_device_bus_id) {
677 result = acpi_device_set_name(device, acpi_device_bus_id);
681 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
683 if (!acpi_device_bus_id) {
687 acpi_device_bus_id->bus_id =
688 kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
689 if (!acpi_device_bus_id->bus_id) {
690 kfree(acpi_device_bus_id);
695 ida_init(&acpi_device_bus_id->instance_ida);
697 result = acpi_device_set_name(device, acpi_device_bus_id);
699 kfree_const(acpi_device_bus_id->bus_id);
700 kfree(acpi_device_bus_id);
704 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
708 list_add_tail(&device->node, &device->parent->children);
710 if (device->wakeup.flags.valid)
711 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
713 mutex_unlock(&acpi_device_lock);
716 device->dev.parent = &device->parent->dev;
718 device->dev.bus = &acpi_bus_type;
719 device->dev.release = release;
720 result = device_add(&device->dev);
722 dev_err(&device->dev, "Error registering device\n");
726 result = acpi_device_setup_files(device);
728 pr_err("Error creating sysfs interface for device %s\n",
729 dev_name(&device->dev));
734 mutex_lock(&acpi_device_lock);
737 list_del(&device->node);
739 list_del(&device->wakeup_list);
742 mutex_unlock(&acpi_device_lock);
744 acpi_detach_data(device->handle, acpi_scan_drop_device);
749 int acpi_device_add(struct acpi_device *adev, void (*release)(struct device *))
753 ret = acpi_tie_acpi_dev(adev);
757 return __acpi_device_add(adev, release);
760 /* --------------------------------------------------------------------------
762 -------------------------------------------------------------------------- */
763 static bool acpi_info_matches_ids(struct acpi_device_info *info,
764 const char * const ids[])
766 struct acpi_pnp_device_id_list *cid_list = NULL;
769 if (!(info->valid & ACPI_VALID_HID))
772 index = match_string(ids, -1, info->hardware_id.string);
776 if (info->valid & ACPI_VALID_CID)
777 cid_list = &info->compatible_id_list;
782 for (i = 0; i < cid_list->count; i++) {
783 index = match_string(ids, -1, cid_list->ids[i].string);
791 /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
792 static const char * const acpi_ignore_dep_ids[] = {
793 "PNP0D80", /* Windows-compatible System Power Management Controller */
794 "INT33BD", /* Intel Baytrail Mailbox Device */
798 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
800 struct acpi_device *device = NULL;
804 * Fixed hardware devices do not appear in the namespace and do not
805 * have handles, but we fabricate acpi_devices for them, so we have
806 * to deal with them specially.
812 status = acpi_get_parent(handle, &handle);
813 if (ACPI_FAILURE(status))
814 return status == AE_NULL_ENTRY ? NULL : acpi_root;
815 } while (acpi_bus_get_device(handle, &device));
820 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
824 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
825 union acpi_object *obj;
827 status = acpi_get_handle(handle, "_EJD", &tmp);
828 if (ACPI_FAILURE(status))
831 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
832 if (ACPI_SUCCESS(status)) {
833 obj = buffer.pointer;
834 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
836 kfree(buffer.pointer);
840 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
842 static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
844 acpi_handle handle = dev->handle;
845 struct acpi_device_wakeup *wakeup = &dev->wakeup;
846 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
847 union acpi_object *package = NULL;
848 union acpi_object *element = NULL;
852 INIT_LIST_HEAD(&wakeup->resources);
855 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
856 if (ACPI_FAILURE(status)) {
857 acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
858 acpi_format_exception(status));
862 package = (union acpi_object *)buffer.pointer;
864 if (!package || package->package.count < 2)
867 element = &(package->package.elements[0]);
871 if (element->type == ACPI_TYPE_PACKAGE) {
872 if ((element->package.count < 2) ||
873 (element->package.elements[0].type !=
874 ACPI_TYPE_LOCAL_REFERENCE)
875 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
879 element->package.elements[0].reference.handle;
881 (u32) element->package.elements[1].integer.value;
882 } else if (element->type == ACPI_TYPE_INTEGER) {
883 wakeup->gpe_device = NULL;
884 wakeup->gpe_number = element->integer.value;
889 element = &(package->package.elements[1]);
890 if (element->type != ACPI_TYPE_INTEGER)
893 wakeup->sleep_state = element->integer.value;
895 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
899 if (!list_empty(&wakeup->resources)) {
902 err = acpi_power_wakeup_list_init(&wakeup->resources,
905 acpi_handle_warn(handle, "Retrieving current states "
906 "of wakeup power resources failed\n");
907 acpi_power_resources_list_free(&wakeup->resources);
910 if (sleep_state < wakeup->sleep_state) {
911 acpi_handle_warn(handle, "Overriding _PRW sleep state "
912 "(S%d) by S%d from power resources\n",
913 (int)wakeup->sleep_state, sleep_state);
914 wakeup->sleep_state = sleep_state;
919 kfree(buffer.pointer);
923 static bool acpi_wakeup_gpe_init(struct acpi_device *device)
925 static const struct acpi_device_id button_device_ids[] = {
926 {"PNP0C0C", 0}, /* Power button */
927 {"PNP0C0D", 0}, /* Lid */
928 {"PNP0C0E", 0}, /* Sleep button */
931 struct acpi_device_wakeup *wakeup = &device->wakeup;
934 wakeup->flags.notifier_present = 0;
936 /* Power button, Lid switch always enable wakeup */
937 if (!acpi_match_device_ids(device, button_device_ids)) {
938 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
939 /* Do not use Lid/sleep button for S5 wakeup */
940 if (wakeup->sleep_state == ACPI_STATE_S5)
941 wakeup->sleep_state = ACPI_STATE_S4;
943 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
944 device_set_wakeup_capable(&device->dev, true);
948 status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
950 return ACPI_SUCCESS(status);
953 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
957 /* Presence of _PRW indicates wake capable */
958 if (!acpi_has_method(device->handle, "_PRW"))
961 err = acpi_bus_extract_wakeup_device_power_package(device);
963 dev_err(&device->dev, "Unable to extract wakeup power resources");
967 device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
968 device->wakeup.prepare_count = 0;
970 * Call _PSW/_DSW object to disable its ability to wake the sleeping
971 * system for the ACPI device with the _PRW object.
972 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
973 * So it is necessary to call _DSW object first. Only when it is not
974 * present will the _PSW object used.
976 err = acpi_device_sleep_wake(device, 0, 0, 0);
978 pr_debug("error in _DSW or _PSW evaluation\n");
981 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
983 struct acpi_device_power_state *ps = &device->power.states[state];
984 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
985 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
988 INIT_LIST_HEAD(&ps->resources);
990 /* Evaluate "_PRx" to get referenced power resources */
991 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
992 if (ACPI_SUCCESS(status)) {
993 union acpi_object *package = buffer.pointer;
995 if (buffer.length && package
996 && package->type == ACPI_TYPE_PACKAGE
997 && package->package.count)
998 acpi_extract_power_resources(package, 0, &ps->resources);
1000 ACPI_FREE(buffer.pointer);
1003 /* Evaluate "_PSx" to see if we can do explicit sets */
1005 if (acpi_has_method(device->handle, pathname))
1006 ps->flags.explicit_set = 1;
1008 /* State is valid if there are means to put the device into it. */
1009 if (!list_empty(&ps->resources) || ps->flags.explicit_set)
1010 ps->flags.valid = 1;
1012 ps->power = -1; /* Unknown - driver assigned */
1013 ps->latency = -1; /* Unknown - driver assigned */
1016 static void acpi_bus_get_power_flags(struct acpi_device *device)
1020 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1021 if (!acpi_has_method(device->handle, "_PS0") &&
1022 !acpi_has_method(device->handle, "_PR0"))
1025 device->flags.power_manageable = 1;
1028 * Power Management Flags
1030 if (acpi_has_method(device->handle, "_PSC"))
1031 device->power.flags.explicit_get = 1;
1033 if (acpi_has_method(device->handle, "_IRC"))
1034 device->power.flags.inrush_current = 1;
1036 if (acpi_has_method(device->handle, "_DSW"))
1037 device->power.flags.dsw_present = 1;
1040 * Enumerate supported power management states
1042 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1043 acpi_bus_init_power_state(device, i);
1045 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1047 /* Set the defaults for D0 and D3hot (always supported). */
1048 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1049 device->power.states[ACPI_STATE_D0].power = 100;
1050 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1053 * Use power resources only if the D0 list of them is populated, because
1054 * some platforms may provide _PR3 only to indicate D3cold support and
1055 * in those cases the power resources list returned by it may be bogus.
1057 if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1058 device->power.flags.power_resources = 1;
1060 * D3cold is supported if the D3hot list of power resources is
1063 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1064 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1067 if (acpi_bus_init_power(device))
1068 device->flags.power_manageable = 0;
1071 static void acpi_bus_get_flags(struct acpi_device *device)
1073 /* Presence of _STA indicates 'dynamic_status' */
1074 if (acpi_has_method(device->handle, "_STA"))
1075 device->flags.dynamic_status = 1;
1077 /* Presence of _RMV indicates 'removable' */
1078 if (acpi_has_method(device->handle, "_RMV"))
1079 device->flags.removable = 1;
1081 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1082 if (acpi_has_method(device->handle, "_EJD") ||
1083 acpi_has_method(device->handle, "_EJ0"))
1084 device->flags.ejectable = 1;
1087 static void acpi_device_get_busid(struct acpi_device *device)
1089 char bus_id[5] = { '?', 0 };
1090 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1096 * The device's Bus ID is simply the object name.
1097 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1099 if (ACPI_IS_ROOT_DEVICE(device)) {
1100 strcpy(device->pnp.bus_id, "ACPI");
1104 switch (device->device_type) {
1105 case ACPI_BUS_TYPE_POWER_BUTTON:
1106 strcpy(device->pnp.bus_id, "PWRF");
1108 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1109 strcpy(device->pnp.bus_id, "SLPF");
1111 case ACPI_BUS_TYPE_ECDT_EC:
1112 strcpy(device->pnp.bus_id, "ECDT");
1115 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1116 /* Clean up trailing underscores (if any) */
1117 for (i = 3; i > 1; i--) {
1118 if (bus_id[i] == '_')
1123 strcpy(device->pnp.bus_id, bus_id);
1129 * acpi_ata_match - see if an acpi object is an ATA device
1131 * If an acpi object has one of the ACPI ATA methods defined,
1132 * then we can safely call it an ATA device.
1134 bool acpi_ata_match(acpi_handle handle)
1136 return acpi_has_method(handle, "_GTF") ||
1137 acpi_has_method(handle, "_GTM") ||
1138 acpi_has_method(handle, "_STM") ||
1139 acpi_has_method(handle, "_SDD");
1143 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1145 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1146 * then we can safely call it an ejectable drive bay
1148 bool acpi_bay_match(acpi_handle handle)
1150 acpi_handle phandle;
1152 if (!acpi_has_method(handle, "_EJ0"))
1154 if (acpi_ata_match(handle))
1156 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1159 return acpi_ata_match(phandle);
1162 bool acpi_device_is_battery(struct acpi_device *adev)
1164 struct acpi_hardware_id *hwid;
1166 list_for_each_entry(hwid, &adev->pnp.ids, list)
1167 if (!strcmp("PNP0C0A", hwid->id))
1173 static bool is_ejectable_bay(struct acpi_device *adev)
1175 acpi_handle handle = adev->handle;
1177 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1180 return acpi_bay_match(handle);
1184 * acpi_dock_match - see if an acpi object has a _DCK method
1186 bool acpi_dock_match(acpi_handle handle)
1188 return acpi_has_method(handle, "_DCK");
1192 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1193 void **return_value)
1195 long *cap = context;
1197 if (acpi_has_method(handle, "_BCM") &&
1198 acpi_has_method(handle, "_BCL")) {
1199 acpi_handle_debug(handle, "Found generic backlight support\n");
1200 *cap |= ACPI_VIDEO_BACKLIGHT;
1201 /* We have backlight support, no need to scan further */
1202 return AE_CTRL_TERMINATE;
1207 /* Returns true if the ACPI object is a video device which can be
1208 * handled by video.ko.
1209 * The device will get a Linux specific CID added in scan.c to
1210 * identify the device as an ACPI graphics device
1211 * Be aware that the graphics device may not be physically present
1212 * Use acpi_video_get_capabilities() to detect general ACPI video
1213 * capabilities of present cards
1215 long acpi_is_video_device(acpi_handle handle)
1217 long video_caps = 0;
1219 /* Is this device able to support video switching ? */
1220 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1221 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1223 /* Is this device able to retrieve a video ROM ? */
1224 if (acpi_has_method(handle, "_ROM"))
1225 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1227 /* Is this device able to configure which video head to be POSTed ? */
1228 if (acpi_has_method(handle, "_VPO") &&
1229 acpi_has_method(handle, "_GPD") &&
1230 acpi_has_method(handle, "_SPD"))
1231 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1233 /* Only check for backlight functionality if one of the above hit. */
1235 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1236 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1241 EXPORT_SYMBOL(acpi_is_video_device);
1243 const char *acpi_device_hid(struct acpi_device *device)
1245 struct acpi_hardware_id *hid;
1247 if (list_empty(&device->pnp.ids))
1250 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1253 EXPORT_SYMBOL(acpi_device_hid);
1255 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1257 struct acpi_hardware_id *id;
1259 id = kmalloc(sizeof(*id), GFP_KERNEL);
1263 id->id = kstrdup_const(dev_id, GFP_KERNEL);
1269 list_add_tail(&id->list, &pnp->ids);
1270 pnp->type.hardware_id = 1;
1274 * Old IBM workstations have a DSDT bug wherein the SMBus object
1275 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1276 * prefix. Work around this.
1278 static bool acpi_ibm_smbus_match(acpi_handle handle)
1280 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1281 struct acpi_buffer path = { sizeof(node_name), node_name };
1283 if (!dmi_name_in_vendors("IBM"))
1286 /* Look for SMBS object */
1287 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1288 strcmp("SMBS", path.pointer))
1291 /* Does it have the necessary (but misnamed) methods? */
1292 if (acpi_has_method(handle, "SBI") &&
1293 acpi_has_method(handle, "SBR") &&
1294 acpi_has_method(handle, "SBW"))
1300 static bool acpi_object_is_system_bus(acpi_handle handle)
1304 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1307 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1314 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1317 struct acpi_device_info *info = NULL;
1318 struct acpi_pnp_device_id_list *cid_list;
1321 switch (device_type) {
1322 case ACPI_BUS_TYPE_DEVICE:
1323 if (handle == ACPI_ROOT_OBJECT) {
1324 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1328 acpi_get_object_info(handle, &info);
1330 pr_err("%s: Error reading device info\n", __func__);
1334 if (info->valid & ACPI_VALID_HID) {
1335 acpi_add_id(pnp, info->hardware_id.string);
1336 pnp->type.platform_id = 1;
1338 if (info->valid & ACPI_VALID_CID) {
1339 cid_list = &info->compatible_id_list;
1340 for (i = 0; i < cid_list->count; i++)
1341 acpi_add_id(pnp, cid_list->ids[i].string);
1343 if (info->valid & ACPI_VALID_ADR) {
1344 pnp->bus_address = info->address;
1345 pnp->type.bus_address = 1;
1347 if (info->valid & ACPI_VALID_UID)
1348 pnp->unique_id = kstrdup(info->unique_id.string,
1350 if (info->valid & ACPI_VALID_CLS)
1351 acpi_add_id(pnp, info->class_code.string);
1356 * Some devices don't reliably have _HIDs & _CIDs, so add
1357 * synthetic HIDs to make sure drivers can find them.
1359 if (acpi_is_video_device(handle))
1360 acpi_add_id(pnp, ACPI_VIDEO_HID);
1361 else if (acpi_bay_match(handle))
1362 acpi_add_id(pnp, ACPI_BAY_HID);
1363 else if (acpi_dock_match(handle))
1364 acpi_add_id(pnp, ACPI_DOCK_HID);
1365 else if (acpi_ibm_smbus_match(handle))
1366 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1367 else if (list_empty(&pnp->ids) &&
1368 acpi_object_is_system_bus(handle)) {
1369 /* \_SB, \_TZ, LNXSYBUS */
1370 acpi_add_id(pnp, ACPI_BUS_HID);
1371 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1372 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1376 case ACPI_BUS_TYPE_POWER:
1377 acpi_add_id(pnp, ACPI_POWER_HID);
1379 case ACPI_BUS_TYPE_PROCESSOR:
1380 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1382 case ACPI_BUS_TYPE_THERMAL:
1383 acpi_add_id(pnp, ACPI_THERMAL_HID);
1385 case ACPI_BUS_TYPE_POWER_BUTTON:
1386 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1388 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1389 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1391 case ACPI_BUS_TYPE_ECDT_EC:
1392 acpi_add_id(pnp, ACPI_ECDT_HID);
1397 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1399 struct acpi_hardware_id *id, *tmp;
1401 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1402 kfree_const(id->id);
1405 kfree(pnp->unique_id);
1409 * acpi_dma_supported - Check DMA support for the specified device.
1410 * @adev: The pointer to acpi device
1412 * Return false if DMA is not supported. Otherwise, return true
1414 bool acpi_dma_supported(struct acpi_device *adev)
1419 if (adev->flags.cca_seen)
1423 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1424 * DMA on "Intel platforms". Presumably that includes all x86 and
1425 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1427 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1434 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1435 * @adev: The pointer to acpi device
1437 * Return enum dev_dma_attr.
1439 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1441 if (!acpi_dma_supported(adev))
1442 return DEV_DMA_NOT_SUPPORTED;
1444 if (adev->flags.coherent_dma)
1445 return DEV_DMA_COHERENT;
1447 return DEV_DMA_NON_COHERENT;
1451 * acpi_dma_get_range() - Get device DMA parameters.
1453 * @dev: device to configure
1454 * @dma_addr: pointer device DMA address result
1455 * @offset: pointer to the DMA offset result
1456 * @size: pointer to DMA range size result
1458 * Evaluate DMA regions and return respectively DMA region start, offset
1459 * and size in dma_addr, offset and size on parsing success; it does not
1460 * update the passed in values on failure.
1462 * Return 0 on success, < 0 on failure.
1464 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1467 struct acpi_device *adev;
1469 struct resource_entry *rentry;
1471 struct device *dma_dev = dev;
1472 u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1475 * Walk the device tree chasing an ACPI companion with a _DMA
1476 * object while we go. Stop if we find a device with an ACPI
1477 * companion containing a _DMA method.
1480 adev = ACPI_COMPANION(dma_dev);
1481 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1484 dma_dev = dma_dev->parent;
1490 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1491 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1495 ret = acpi_dev_get_dma_resources(adev, &list);
1497 list_for_each_entry(rentry, &list, node) {
1498 if (dma_offset && rentry->offset != dma_offset) {
1500 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1503 dma_offset = rentry->offset;
1505 /* Take lower and upper limits */
1506 if (rentry->res->start < dma_start)
1507 dma_start = rentry->res->start;
1508 if (rentry->res->end > dma_end)
1509 dma_end = rentry->res->end;
1512 if (dma_start >= dma_end) {
1514 dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1518 *dma_addr = dma_start - dma_offset;
1519 len = dma_end - dma_start;
1520 *size = max(len, len + 1);
1521 *offset = dma_offset;
1524 acpi_dev_free_resource_list(&list);
1526 return ret >= 0 ? 0 : ret;
1530 * acpi_dma_configure_id - Set-up DMA configuration for the device.
1531 * @dev: The pointer to the device
1532 * @attr: device dma attributes
1533 * @input_id: input device id const value pointer
1535 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1536 const u32 *input_id)
1538 const struct iommu_ops *iommu;
1539 u64 dma_addr = 0, size = 0;
1541 if (attr == DEV_DMA_NOT_SUPPORTED) {
1542 set_dma_ops(dev, &dma_dummy_ops);
1546 iort_dma_setup(dev, &dma_addr, &size);
1548 iommu = iort_iommu_configure_id(dev, input_id);
1549 if (PTR_ERR(iommu) == -EPROBE_DEFER)
1550 return -EPROBE_DEFER;
1552 arch_setup_dma_ops(dev, dma_addr, size,
1553 iommu, attr == DEV_DMA_COHERENT);
1557 EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1559 static void acpi_init_coherency(struct acpi_device *adev)
1561 unsigned long long cca = 0;
1563 struct acpi_device *parent = adev->parent;
1565 if (parent && parent->flags.cca_seen) {
1567 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1570 adev->flags.cca_seen = 1;
1571 cca = parent->flags.coherent_dma;
1573 status = acpi_evaluate_integer(adev->handle, "_CCA",
1575 if (ACPI_SUCCESS(status))
1576 adev->flags.cca_seen = 1;
1577 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1579 * If architecture does not specify that _CCA is
1580 * required for DMA-able devices (e.g. x86),
1581 * we default to _CCA=1.
1585 acpi_handle_debug(adev->handle,
1586 "ACPI device is missing _CCA.\n");
1589 adev->flags.coherent_dma = cca;
1592 static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1594 bool *is_serial_bus_slave_p = data;
1596 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1599 *is_serial_bus_slave_p = true;
1601 /* no need to do more checking */
1605 static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1607 struct acpi_device *parent = device->parent;
1608 static const struct acpi_device_id indirect_io_hosts[] = {
1613 return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1616 static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1618 struct list_head resource_list;
1619 bool is_serial_bus_slave = false;
1621 * These devices have multiple I2cSerialBus resources and an i2c-client
1622 * must be instantiated for each, each with its own i2c_device_id.
1623 * Normally we only instantiate an i2c-client for the first resource,
1624 * using the ACPI HID as id. These special cases are handled by the
1625 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1626 * which i2c_device_id to use for each resource.
1628 static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1636 if (acpi_is_indirect_io_slave(device))
1639 /* Macs use device properties in lieu of _CRS resources */
1640 if (x86_apple_machine &&
1641 (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1642 fwnode_property_present(&device->fwnode, "i2cAddress") ||
1643 fwnode_property_present(&device->fwnode, "baud")))
1646 /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1647 if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1650 INIT_LIST_HEAD(&resource_list);
1651 acpi_dev_get_resources(device, &resource_list,
1652 acpi_check_serial_bus_slave,
1653 &is_serial_bus_slave);
1654 acpi_dev_free_resource_list(&resource_list);
1656 return is_serial_bus_slave;
1659 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1662 INIT_LIST_HEAD(&device->pnp.ids);
1663 device->device_type = type;
1664 device->handle = handle;
1665 device->parent = acpi_bus_get_parent(handle);
1666 fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1667 acpi_set_device_status(device, ACPI_STA_DEFAULT);
1668 acpi_device_get_busid(device);
1669 acpi_set_pnp_ids(handle, &device->pnp, type);
1670 acpi_init_properties(device);
1671 acpi_bus_get_flags(device);
1672 device->flags.match_driver = false;
1673 device->flags.initialized = true;
1674 device->flags.enumeration_by_parent =
1675 acpi_device_enumeration_by_parent(device);
1676 acpi_device_clear_enumerated(device);
1677 device_initialize(&device->dev);
1678 dev_set_uevent_suppress(&device->dev, true);
1679 acpi_init_coherency(device);
1682 static void acpi_scan_dep_init(struct acpi_device *adev)
1684 struct acpi_dep_data *dep;
1686 list_for_each_entry(dep, &acpi_dep_list, node) {
1687 if (dep->consumer == adev->handle)
1692 void acpi_device_add_finalize(struct acpi_device *device)
1694 dev_set_uevent_suppress(&device->dev, false);
1695 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1698 static void acpi_scan_init_status(struct acpi_device *adev)
1700 if (acpi_bus_get_status(adev))
1701 acpi_set_device_status(adev, 0);
1704 static int acpi_add_single_object(struct acpi_device **child,
1705 acpi_handle handle, int type, bool dep_init)
1707 struct acpi_device *device;
1708 bool release_dep_lock = false;
1711 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1715 acpi_init_device_object(device, handle, type);
1717 * Getting the status is delayed till here so that we can call
1718 * acpi_bus_get_status() and use its quirk handling. Note that
1719 * this must be done before the get power-/wakeup_dev-flags calls.
1721 if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
1723 mutex_lock(&acpi_dep_list_lock);
1725 * Hold the lock until the acpi_tie_acpi_dev() call
1726 * below to prevent concurrent acpi_scan_clear_dep()
1727 * from deleting a dependency list entry without
1728 * updating dep_unmet for the device.
1730 release_dep_lock = true;
1731 acpi_scan_dep_init(device);
1733 acpi_scan_init_status(device);
1736 acpi_bus_get_power_flags(device);
1737 acpi_bus_get_wakeup_device_flags(device);
1739 result = acpi_tie_acpi_dev(device);
1741 if (release_dep_lock)
1742 mutex_unlock(&acpi_dep_list_lock);
1745 result = __acpi_device_add(device, acpi_device_release);
1748 acpi_device_release(&device->dev);
1752 acpi_power_add_remove_device(device, true);
1753 acpi_device_add_finalize(device);
1755 acpi_handle_debug(handle, "Added as %s, parent %s\n",
1756 dev_name(&device->dev), device->parent ?
1757 dev_name(&device->parent->dev) : "(null)");
1763 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1766 struct resource *res = context;
1768 if (acpi_dev_resource_memory(ares, res))
1769 return AE_CTRL_TERMINATE;
1774 static bool acpi_device_should_be_hidden(acpi_handle handle)
1777 struct resource res;
1779 /* Check if it should ignore the UART device */
1780 if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1784 * The UART device described in SPCR table is assumed to have only one
1785 * memory resource present. So we only look for the first one here.
1787 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1788 acpi_get_resource_memory, &res);
1789 if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1792 acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1798 bool acpi_device_is_present(const struct acpi_device *adev)
1800 return adev->status.present || adev->status.functional;
1803 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1805 const struct acpi_device_id **matchid)
1807 const struct acpi_device_id *devid;
1810 return handler->match(idstr, matchid);
1812 for (devid = handler->ids; devid->id[0]; devid++)
1813 if (!strcmp((char *)devid->id, idstr)) {
1823 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1824 const struct acpi_device_id **matchid)
1826 struct acpi_scan_handler *handler;
1828 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1829 if (acpi_scan_handler_matching(handler, idstr, matchid))
1835 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1837 if (!!hotplug->enabled == !!val)
1840 mutex_lock(&acpi_scan_lock);
1842 hotplug->enabled = val;
1844 mutex_unlock(&acpi_scan_lock);
1847 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1849 struct acpi_hardware_id *hwid;
1851 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1852 acpi_dock_add(adev);
1855 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1856 struct acpi_scan_handler *handler;
1858 handler = acpi_scan_match_handler(hwid->id, NULL);
1860 adev->flags.hotplug_notify = true;
1866 static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
1868 struct acpi_handle_list dep_devices;
1874 * Check for _HID here to avoid deferring the enumeration of:
1876 * 2. ACPI nodes describing USB ports.
1877 * Still, checking for _HID catches more then just these cases ...
1879 if (!check_dep || !acpi_has_method(handle, "_DEP") ||
1880 !acpi_has_method(handle, "_HID"))
1883 status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
1884 if (ACPI_FAILURE(status)) {
1885 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
1889 for (count = 0, i = 0; i < dep_devices.count; i++) {
1890 struct acpi_device_info *info;
1891 struct acpi_dep_data *dep;
1894 status = acpi_get_object_info(dep_devices.handles[i], &info);
1895 if (ACPI_FAILURE(status)) {
1896 acpi_handle_debug(handle, "Error reading _DEP device info\n");
1900 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
1906 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1912 dep->supplier = dep_devices.handles[i];
1913 dep->consumer = handle;
1915 mutex_lock(&acpi_dep_list_lock);
1916 list_add_tail(&dep->node , &acpi_dep_list);
1917 mutex_unlock(&acpi_dep_list_lock);
1923 static bool acpi_bus_scan_second_pass;
1925 static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
1926 struct acpi_device **adev_p)
1928 struct acpi_device *device = NULL;
1929 acpi_object_type acpi_type;
1932 acpi_bus_get_device(handle, &device);
1936 if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
1939 switch (acpi_type) {
1940 case ACPI_TYPE_DEVICE:
1941 if (acpi_device_should_be_hidden(handle))
1944 /* Bail out if there are dependencies. */
1945 if (acpi_scan_check_dep(handle, check_dep) > 0) {
1946 acpi_bus_scan_second_pass = true;
1947 return AE_CTRL_DEPTH;
1951 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1952 type = ACPI_BUS_TYPE_DEVICE;
1955 case ACPI_TYPE_PROCESSOR:
1956 type = ACPI_BUS_TYPE_PROCESSOR;
1959 case ACPI_TYPE_THERMAL:
1960 type = ACPI_BUS_TYPE_THERMAL;
1963 case ACPI_TYPE_POWER:
1964 acpi_add_power_resource(handle);
1971 * If check_dep is true at this point, the device has no dependencies,
1972 * or the creation of the device object would have been postponed above.
1974 acpi_add_single_object(&device, handle, type, !check_dep);
1976 return AE_CTRL_DEPTH;
1978 acpi_scan_init_hotplug(device);
1987 static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
1988 void *not_used, void **ret_p)
1990 return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
1993 static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
1994 void *not_used, void **ret_p)
1996 return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
1999 static void acpi_default_enumeration(struct acpi_device *device)
2002 * Do not enumerate devices with enumeration_by_parent flag set as
2003 * they will be enumerated by their respective parents.
2005 if (!device->flags.enumeration_by_parent) {
2006 acpi_create_platform_device(device, NULL);
2007 acpi_device_set_enumerated(device);
2009 blocking_notifier_call_chain(&acpi_reconfig_chain,
2010 ACPI_RECONFIG_DEVICE_ADD, device);
2014 static const struct acpi_device_id generic_device_ids[] = {
2015 {ACPI_DT_NAMESPACE_HID, },
2019 static int acpi_generic_device_attach(struct acpi_device *adev,
2020 const struct acpi_device_id *not_used)
2023 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2024 * below can be unconditional.
2026 if (adev->data.of_compatible)
2027 acpi_default_enumeration(adev);
2032 static struct acpi_scan_handler generic_device_handler = {
2033 .ids = generic_device_ids,
2034 .attach = acpi_generic_device_attach,
2037 static int acpi_scan_attach_handler(struct acpi_device *device)
2039 struct acpi_hardware_id *hwid;
2042 list_for_each_entry(hwid, &device->pnp.ids, list) {
2043 const struct acpi_device_id *devid;
2044 struct acpi_scan_handler *handler;
2046 handler = acpi_scan_match_handler(hwid->id, &devid);
2048 if (!handler->attach) {
2049 device->pnp.type.platform_id = 0;
2052 device->handler = handler;
2053 ret = handler->attach(device, devid);
2057 device->handler = NULL;
2066 static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
2068 struct acpi_device *child;
2069 bool skip = !first_pass && device->flags.visited;
2076 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2077 register_dock_dependent_device(device, ejd);
2079 acpi_bus_get_status(device);
2080 /* Skip devices that are not present. */
2081 if (!acpi_device_is_present(device)) {
2082 device->flags.initialized = false;
2083 acpi_device_clear_enumerated(device);
2084 device->flags.power_manageable = 0;
2087 if (device->handler)
2090 if (!device->flags.initialized) {
2091 device->flags.power_manageable =
2092 device->power.states[ACPI_STATE_D0].flags.valid;
2093 if (acpi_bus_init_power(device))
2094 device->flags.power_manageable = 0;
2096 device->flags.initialized = true;
2097 } else if (device->flags.visited) {
2101 ret = acpi_scan_attach_handler(device);
2105 device->flags.match_driver = true;
2106 if (ret > 0 && !device->flags.enumeration_by_parent) {
2107 acpi_device_set_enumerated(device);
2111 ret = device_attach(&device->dev);
2115 if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2116 acpi_default_enumeration(device);
2118 acpi_device_set_enumerated(device);
2121 list_for_each_entry(child, &device->children, node)
2122 acpi_bus_attach(child, first_pass);
2124 if (!skip && device->handler && device->handler->hotplug.notify_online)
2125 device->handler->hotplug.notify_online(device);
2128 static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
2130 struct acpi_device *adev;
2132 adev = acpi_bus_get_acpi_device(dep->consumer);
2134 *(struct acpi_device **)data = adev;
2137 /* Continue parsing if the device object is not present. */
2141 struct acpi_scan_clear_dep_work {
2142 struct work_struct work;
2143 struct acpi_device *adev;
2146 static void acpi_scan_clear_dep_fn(struct work_struct *work)
2148 struct acpi_scan_clear_dep_work *cdw;
2150 cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
2152 acpi_scan_lock_acquire();
2153 acpi_bus_attach(cdw->adev, true);
2154 acpi_scan_lock_release();
2156 acpi_dev_put(cdw->adev);
2160 static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2162 struct acpi_scan_clear_dep_work *cdw;
2164 if (adev->dep_unmet)
2167 cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2172 INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2174 * Since the work function may block on the lock until the entire
2175 * initial enumeration of devices is complete, put it into the unbound
2178 queue_work(system_unbound_wq, &cdw->work);
2183 static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
2185 struct acpi_device *adev = acpi_bus_get_acpi_device(dep->consumer);
2189 if (!acpi_scan_clear_dep_queue(adev))
2193 list_del(&dep->node);
2200 * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2201 * @handle: The ACPI handle of the supplier device
2202 * @callback: Pointer to the callback function to apply
2203 * @data: Pointer to some data to pass to the callback
2205 * The return value of the callback determines this function's behaviour. If 0
2206 * is returned we continue to iterate over acpi_dep_list. If a positive value
2207 * is returned then the loop is broken but this function returns 0. If a
2208 * negative value is returned by the callback then the loop is broken and that
2209 * value is returned as the final error.
2211 static int acpi_walk_dep_device_list(acpi_handle handle,
2212 int (*callback)(struct acpi_dep_data *, void *),
2215 struct acpi_dep_data *dep, *tmp;
2218 mutex_lock(&acpi_dep_list_lock);
2219 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2220 if (dep->supplier == handle) {
2221 ret = callback(dep, data);
2226 mutex_unlock(&acpi_dep_list_lock);
2228 return ret > 0 ? 0 : ret;
2232 * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2233 * @supplier: Pointer to the supplier &struct acpi_device
2235 * Clear dependencies on the given device.
2237 void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2239 acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2241 EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2244 * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2245 * @supplier: Pointer to the dependee device
2247 * Returns the first &struct acpi_device which declares itself dependent on
2248 * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2250 * The caller is responsible for putting the reference to adev when it is no
2253 struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2255 struct acpi_device *adev = NULL;
2257 acpi_walk_dep_device_list(supplier->handle,
2258 acpi_dev_get_first_consumer_dev_cb, &adev);
2262 EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
2265 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2266 * @handle: Root of the namespace scope to scan.
2268 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2271 * If no devices were found, -ENODEV is returned, but it does not mean that
2272 * there has been a real error. There just have been no suitable ACPI objects
2273 * in the table trunk from which the kernel could create a device and add an
2274 * appropriate driver.
2276 * Must be called under acpi_scan_lock.
2278 int acpi_bus_scan(acpi_handle handle)
2280 struct acpi_device *device = NULL;
2282 acpi_bus_scan_second_pass = false;
2284 /* Pass 1: Avoid enumerating devices with missing dependencies. */
2286 if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
2287 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2288 acpi_bus_check_add_1, NULL, NULL,
2294 acpi_bus_attach(device, true);
2296 if (!acpi_bus_scan_second_pass)
2299 /* Pass 2: Enumerate all of the remaining devices. */
2303 if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
2304 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2305 acpi_bus_check_add_2, NULL, NULL,
2308 acpi_bus_attach(device, false);
2312 EXPORT_SYMBOL(acpi_bus_scan);
2315 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2316 * @adev: Root of the ACPI namespace scope to walk.
2318 * Must be called under acpi_scan_lock.
2320 void acpi_bus_trim(struct acpi_device *adev)
2322 struct acpi_scan_handler *handler = adev->handler;
2323 struct acpi_device *child;
2325 list_for_each_entry_reverse(child, &adev->children, node)
2326 acpi_bus_trim(child);
2328 adev->flags.match_driver = false;
2330 if (handler->detach)
2331 handler->detach(adev);
2333 adev->handler = NULL;
2335 device_release_driver(&adev->dev);
2338 * Most likely, the device is going away, so put it into D3cold before
2341 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2342 adev->flags.initialized = false;
2343 acpi_device_clear_enumerated(adev);
2345 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2347 int acpi_bus_register_early_device(int type)
2349 struct acpi_device *device = NULL;
2352 result = acpi_add_single_object(&device, NULL, type, false);
2356 device->flags.match_driver = true;
2357 return device_attach(&device->dev);
2359 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2361 static int acpi_bus_scan_fixed(void)
2366 * Enumerate all fixed-feature devices.
2368 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2369 struct acpi_device *device = NULL;
2371 result = acpi_add_single_object(&device, NULL,
2372 ACPI_BUS_TYPE_POWER_BUTTON, false);
2376 device->flags.match_driver = true;
2377 result = device_attach(&device->dev);
2381 device_init_wakeup(&device->dev, true);
2384 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2385 struct acpi_device *device = NULL;
2387 result = acpi_add_single_object(&device, NULL,
2388 ACPI_BUS_TYPE_SLEEP_BUTTON, false);
2392 device->flags.match_driver = true;
2393 result = device_attach(&device->dev);
2396 return result < 0 ? result : 0;
2399 static void __init acpi_get_spcr_uart_addr(void)
2402 struct acpi_table_spcr *spcr_ptr;
2404 status = acpi_get_table(ACPI_SIG_SPCR, 0,
2405 (struct acpi_table_header **)&spcr_ptr);
2406 if (ACPI_FAILURE(status)) {
2407 pr_warn("STAO table present, but SPCR is missing\n");
2411 spcr_uart_addr = spcr_ptr->serial_port.address;
2412 acpi_put_table((struct acpi_table_header *)spcr_ptr);
2415 static bool acpi_scan_initialized;
2417 int __init acpi_scan_init(void)
2421 struct acpi_table_stao *stao_ptr;
2423 acpi_pci_root_init();
2424 acpi_pci_link_init();
2425 acpi_processor_init();
2426 acpi_platform_init();
2429 acpi_cmos_rtc_init();
2430 acpi_container_init();
2431 acpi_memory_hotplug_init();
2432 acpi_watchdog_init();
2434 acpi_int340x_thermal_init();
2438 acpi_scan_add_handler(&generic_device_handler);
2441 * If there is STAO table, check whether it needs to ignore the UART
2442 * device in SPCR table.
2444 status = acpi_get_table(ACPI_SIG_STAO, 0,
2445 (struct acpi_table_header **)&stao_ptr);
2446 if (ACPI_SUCCESS(status)) {
2447 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2448 pr_info("STAO Name List not yet supported.\n");
2450 if (stao_ptr->ignore_uart)
2451 acpi_get_spcr_uart_addr();
2453 acpi_put_table((struct acpi_table_header *)stao_ptr);
2456 acpi_gpe_apply_masked_gpes();
2457 acpi_update_all_gpes();
2460 * Although we call __add_memory() that is documented to require the
2461 * device_hotplug_lock, it is not necessary here because this is an
2462 * early code when userspace or any other code path cannot trigger
2463 * hotplug/hotunplug operations.
2465 mutex_lock(&acpi_scan_lock);
2467 * Enumerate devices in the ACPI namespace.
2469 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2473 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2477 /* Fixed feature devices do not exist on HW-reduced platform */
2478 if (!acpi_gbl_reduced_hardware) {
2479 result = acpi_bus_scan_fixed();
2481 acpi_detach_data(acpi_root->handle,
2482 acpi_scan_drop_device);
2483 acpi_device_del(acpi_root);
2484 acpi_bus_put_acpi_device(acpi_root);
2489 acpi_turn_off_unused_power_resources();
2491 acpi_scan_initialized = true;
2494 mutex_unlock(&acpi_scan_lock);
2498 static struct acpi_probe_entry *ape;
2499 static int acpi_probe_count;
2500 static DEFINE_MUTEX(acpi_probe_mutex);
2502 static int __init acpi_match_madt(union acpi_subtable_headers *header,
2503 const unsigned long end)
2505 if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2506 if (!ape->probe_subtbl(header, end))
2512 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2519 mutex_lock(&acpi_probe_mutex);
2520 for (ape = ap_head; nr; ape++, nr--) {
2521 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2522 acpi_probe_count = 0;
2523 acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2524 count += acpi_probe_count;
2527 res = acpi_table_parse(ape->id, ape->probe_table);
2532 mutex_unlock(&acpi_probe_mutex);
2537 static void acpi_table_events_fn(struct work_struct *work)
2539 acpi_scan_lock_acquire();
2540 acpi_bus_scan(ACPI_ROOT_OBJECT);
2541 acpi_scan_lock_release();
2546 void acpi_scan_table_notify(void)
2548 struct work_struct *work;
2550 if (!acpi_scan_initialized)
2553 work = kmalloc(sizeof(*work), GFP_KERNEL);
2557 INIT_WORK(work, acpi_table_events_fn);
2558 schedule_work(work);
2561 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2563 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2565 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2567 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2569 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2571 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);