2 * scan.c - support for transforming the ACPI namespace into individual objects
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/acpi.h>
10 #include <linux/acpi_iort.h>
11 #include <linux/signal.h>
12 #include <linux/kthread.h>
13 #include <linux/dmi.h>
14 #include <linux/nls.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/platform_data/x86/apple.h>
18 #include <asm/pgtable.h>
22 #define _COMPONENT ACPI_BUS_COMPONENT
23 ACPI_MODULE_NAME("scan");
24 extern struct acpi_device *acpi_root;
26 #define ACPI_BUS_CLASS "system_bus"
27 #define ACPI_BUS_HID "LNXSYBUS"
28 #define ACPI_BUS_DEVICE_NAME "System Bus"
30 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
32 #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
34 static const char *dummy_hid = "device";
36 static LIST_HEAD(acpi_dep_list);
37 static DEFINE_MUTEX(acpi_dep_list_lock);
38 LIST_HEAD(acpi_bus_id_list);
39 static DEFINE_MUTEX(acpi_scan_lock);
40 static LIST_HEAD(acpi_scan_handlers_list);
41 DEFINE_MUTEX(acpi_device_lock);
42 LIST_HEAD(acpi_wakeup_device_list);
43 static DEFINE_MUTEX(acpi_hp_context_lock);
46 * The UART device described by the SPCR table is the only object which needs
47 * special-casing. Everything else is covered by ACPI namespace paths in STAO
50 static u64 spcr_uart_addr;
52 struct acpi_dep_data {
53 struct list_head node;
58 void acpi_scan_lock_acquire(void)
60 mutex_lock(&acpi_scan_lock);
62 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
64 void acpi_scan_lock_release(void)
66 mutex_unlock(&acpi_scan_lock);
68 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
70 void acpi_lock_hp_context(void)
72 mutex_lock(&acpi_hp_context_lock);
75 void acpi_unlock_hp_context(void)
77 mutex_unlock(&acpi_hp_context_lock);
80 void acpi_initialize_hp_context(struct acpi_device *adev,
81 struct acpi_hotplug_context *hp,
82 int (*notify)(struct acpi_device *, u32),
83 void (*uevent)(struct acpi_device *, u32))
85 acpi_lock_hp_context();
88 acpi_set_hp_context(adev, hp);
89 acpi_unlock_hp_context();
91 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
93 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
98 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
102 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
103 const char *hotplug_profile_name)
107 error = acpi_scan_add_handler(handler);
111 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
115 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
117 struct acpi_device_physical_node *pn;
121 * acpi_container_offline() calls this for all of the container's
122 * children under the container's physical_node_lock lock.
124 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
126 list_for_each_entry(pn, &adev->physical_node_list, node)
127 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
129 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
135 mutex_unlock(&adev->physical_node_lock);
139 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
142 struct acpi_device *device = NULL;
143 struct acpi_device_physical_node *pn;
144 bool second_pass = (bool)data;
145 acpi_status status = AE_OK;
147 if (acpi_bus_get_device(handle, &device))
150 if (device->handler && !device->handler->hotplug.enabled) {
151 *ret_p = &device->dev;
155 mutex_lock(&device->physical_node_lock);
157 list_for_each_entry(pn, &device->physical_node_list, node) {
161 /* Skip devices offlined by the first pass. */
165 pn->put_online = false;
167 ret = device_offline(pn->dev);
169 pn->put_online = !ret;
179 mutex_unlock(&device->physical_node_lock);
184 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
187 struct acpi_device *device = NULL;
188 struct acpi_device_physical_node *pn;
190 if (acpi_bus_get_device(handle, &device))
193 mutex_lock(&device->physical_node_lock);
195 list_for_each_entry(pn, &device->physical_node_list, node)
196 if (pn->put_online) {
197 device_online(pn->dev);
198 pn->put_online = false;
201 mutex_unlock(&device->physical_node_lock);
206 static int acpi_scan_try_to_offline(struct acpi_device *device)
208 acpi_handle handle = device->handle;
209 struct device *errdev = NULL;
213 * Carry out two passes here and ignore errors in the first pass,
214 * because if the devices in question are memory blocks and
215 * CONFIG_MEMCG is set, one of the blocks may hold data structures
216 * that the other blocks depend on, but it is not known in advance which
219 * If the first pass is successful, the second one isn't needed, though.
221 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
222 NULL, acpi_bus_offline, (void *)false,
224 if (status == AE_SUPPORT) {
225 dev_warn(errdev, "Offline disabled.\n");
226 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
227 acpi_bus_online, NULL, NULL, NULL);
230 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
233 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
234 NULL, acpi_bus_offline, (void *)true,
237 acpi_bus_offline(handle, 0, (void *)true,
241 dev_warn(errdev, "Offline failed.\n");
242 acpi_bus_online(handle, 0, NULL, NULL);
243 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
244 ACPI_UINT32_MAX, acpi_bus_online,
252 static int acpi_scan_hot_remove(struct acpi_device *device)
254 acpi_handle handle = device->handle;
255 unsigned long long sta;
258 if (device->handler && device->handler->hotplug.demand_offline) {
259 if (!acpi_scan_is_offline(device, true))
262 int error = acpi_scan_try_to_offline(device);
267 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
268 "Hot-removing device %s...\n", dev_name(&device->dev)));
270 acpi_bus_trim(device);
272 acpi_evaluate_lck(handle, 0);
276 status = acpi_evaluate_ej0(handle);
277 if (status == AE_NOT_FOUND)
279 else if (ACPI_FAILURE(status))
283 * Verify if eject was indeed successful. If not, log an error
284 * message. No need to call _OST since _EJ0 call was made OK.
286 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
287 if (ACPI_FAILURE(status)) {
288 acpi_handle_warn(handle,
289 "Status check after eject failed (0x%x)\n", status);
290 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
291 acpi_handle_warn(handle,
292 "Eject incomplete - status 0x%llx\n", sta);
298 static int acpi_scan_device_not_present(struct acpi_device *adev)
300 if (!acpi_device_enumerated(adev)) {
301 dev_warn(&adev->dev, "Still not present\n");
308 static int acpi_scan_device_check(struct acpi_device *adev)
312 acpi_bus_get_status(adev);
313 if (adev->status.present || adev->status.functional) {
315 * This function is only called for device objects for which
316 * matching scan handlers exist. The only situation in which
317 * the scan handler is not attached to this device object yet
318 * is when the device has just appeared (either it wasn't
319 * present at all before or it was removed and then added
323 dev_warn(&adev->dev, "Already enumerated\n");
326 error = acpi_bus_scan(adev->handle);
328 dev_warn(&adev->dev, "Namespace scan failure\n");
331 if (!adev->handler) {
332 dev_warn(&adev->dev, "Enumeration failure\n");
336 error = acpi_scan_device_not_present(adev);
341 static int acpi_scan_bus_check(struct acpi_device *adev)
343 struct acpi_scan_handler *handler = adev->handler;
344 struct acpi_device *child;
347 acpi_bus_get_status(adev);
348 if (!(adev->status.present || adev->status.functional)) {
349 acpi_scan_device_not_present(adev);
352 if (handler && handler->hotplug.scan_dependent)
353 return handler->hotplug.scan_dependent(adev);
355 error = acpi_bus_scan(adev->handle);
357 dev_warn(&adev->dev, "Namespace scan failure\n");
360 list_for_each_entry(child, &adev->children, node) {
361 error = acpi_scan_bus_check(child);
368 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
371 case ACPI_NOTIFY_BUS_CHECK:
372 return acpi_scan_bus_check(adev);
373 case ACPI_NOTIFY_DEVICE_CHECK:
374 return acpi_scan_device_check(adev);
375 case ACPI_NOTIFY_EJECT_REQUEST:
376 case ACPI_OST_EC_OSPM_EJECT:
377 if (adev->handler && !adev->handler->hotplug.enabled) {
378 dev_info(&adev->dev, "Eject disabled\n");
381 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
382 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
383 return acpi_scan_hot_remove(adev);
388 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
390 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
393 lock_device_hotplug();
394 mutex_lock(&acpi_scan_lock);
397 * The device object's ACPI handle cannot become invalid as long as we
398 * are holding acpi_scan_lock, but it might have become invalid before
399 * that lock was acquired.
401 if (adev->handle == INVALID_ACPI_HANDLE)
404 if (adev->flags.is_dock_station) {
405 error = dock_notify(adev, src);
406 } else if (adev->flags.hotplug_notify) {
407 error = acpi_generic_hotplug_event(adev, src);
409 int (*notify)(struct acpi_device *, u32);
411 acpi_lock_hp_context();
412 notify = adev->hp ? adev->hp->notify : NULL;
413 acpi_unlock_hp_context();
415 * There may be additional notify handlers for device objects
416 * without the .event() callback, so ignore them here.
419 error = notify(adev, src);
425 ost_code = ACPI_OST_SC_SUCCESS;
428 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
431 ost_code = ACPI_OST_SC_DEVICE_BUSY;
434 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
439 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
442 acpi_bus_put_acpi_device(adev);
443 mutex_unlock(&acpi_scan_lock);
444 unlock_device_hotplug();
447 static void acpi_free_power_resources_lists(struct acpi_device *device)
451 if (device->wakeup.flags.valid)
452 acpi_power_resources_list_free(&device->wakeup.resources);
454 if (!device->power.flags.power_resources)
457 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
458 struct acpi_device_power_state *ps = &device->power.states[i];
459 acpi_power_resources_list_free(&ps->resources);
463 static void acpi_device_release(struct device *dev)
465 struct acpi_device *acpi_dev = to_acpi_device(dev);
467 acpi_free_properties(acpi_dev);
468 acpi_free_pnp_ids(&acpi_dev->pnp);
469 acpi_free_power_resources_lists(acpi_dev);
473 static void acpi_device_del(struct acpi_device *device)
475 struct acpi_device_bus_id *acpi_device_bus_id;
477 mutex_lock(&acpi_device_lock);
479 list_del(&device->node);
481 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
482 if (!strcmp(acpi_device_bus_id->bus_id,
483 acpi_device_hid(device))) {
484 ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
485 if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
486 list_del(&acpi_device_bus_id->node);
487 kfree_const(acpi_device_bus_id->bus_id);
488 kfree(acpi_device_bus_id);
493 list_del(&device->wakeup_list);
494 mutex_unlock(&acpi_device_lock);
496 acpi_power_add_remove_device(device, false);
497 acpi_device_remove_files(device);
499 device->remove(device);
501 device_del(&device->dev);
504 static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
506 static LIST_HEAD(acpi_device_del_list);
507 static DEFINE_MUTEX(acpi_device_del_lock);
509 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
512 struct acpi_device *adev;
514 mutex_lock(&acpi_device_del_lock);
516 if (list_empty(&acpi_device_del_list)) {
517 mutex_unlock(&acpi_device_del_lock);
520 adev = list_first_entry(&acpi_device_del_list,
521 struct acpi_device, del_list);
522 list_del(&adev->del_list);
524 mutex_unlock(&acpi_device_del_lock);
526 blocking_notifier_call_chain(&acpi_reconfig_chain,
527 ACPI_RECONFIG_DEVICE_REMOVE, adev);
529 acpi_device_del(adev);
531 * Drop references to all power resources that might have been
532 * used by the device.
534 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
535 put_device(&adev->dev);
540 * acpi_scan_drop_device - Drop an ACPI device object.
541 * @handle: Handle of an ACPI namespace node, not used.
542 * @context: Address of the ACPI device object to drop.
544 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
545 * namespace node the device object pointed to by @context is attached to.
547 * The unregistration is carried out asynchronously to avoid running
548 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
549 * ensure the correct ordering (the device objects must be unregistered in the
550 * same order in which the corresponding namespace nodes are deleted).
552 static void acpi_scan_drop_device(acpi_handle handle, void *context)
554 static DECLARE_WORK(work, acpi_device_del_work_fn);
555 struct acpi_device *adev = context;
557 mutex_lock(&acpi_device_del_lock);
560 * Use the ACPI hotplug workqueue which is ordered, so this work item
561 * won't run after any hotplug work items submitted subsequently. That
562 * prevents attempts to register device objects identical to those being
563 * deleted from happening concurrently (such attempts result from
564 * hotplug events handled via the ACPI hotplug workqueue). It also will
565 * run after all of the work items submitted previosuly, which helps
566 * those work items to ensure that they are not accessing stale device
569 if (list_empty(&acpi_device_del_list))
570 acpi_queue_hotplug_work(&work);
572 list_add_tail(&adev->del_list, &acpi_device_del_list);
573 /* Make acpi_ns_validate_handle() return NULL for this handle. */
574 adev->handle = INVALID_ACPI_HANDLE;
576 mutex_unlock(&acpi_device_del_lock);
579 static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
580 void (*callback)(void *))
589 status = acpi_get_data_full(handle, acpi_scan_drop_device,
590 (void **)device, callback);
591 if (ACPI_FAILURE(status) || !*device) {
592 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
599 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
601 return acpi_get_device_data(handle, device, NULL);
603 EXPORT_SYMBOL(acpi_bus_get_device);
605 static void get_acpi_device(void *dev)
608 get_device(&((struct acpi_device *)dev)->dev);
611 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
613 struct acpi_device *adev = NULL;
615 acpi_get_device_data(handle, &adev, get_acpi_device);
619 void acpi_bus_put_acpi_device(struct acpi_device *adev)
621 put_device(&adev->dev);
624 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
626 struct acpi_device_bus_id *acpi_device_bus_id;
628 /* Find suitable bus_id and instance number in acpi_bus_id_list. */
629 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
630 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
631 return acpi_device_bus_id;
636 static int acpi_device_set_name(struct acpi_device *device,
637 struct acpi_device_bus_id *acpi_device_bus_id)
639 struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
642 result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
646 device->pnp.instance_no = result;
647 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
651 int acpi_device_add(struct acpi_device *device,
652 void (*release)(struct device *))
654 struct acpi_device_bus_id *acpi_device_bus_id;
657 if (device->handle) {
660 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
662 if (ACPI_FAILURE(status)) {
663 acpi_handle_err(device->handle,
664 "Unable to attach device data\n");
672 * Link this device to its parent and siblings.
674 INIT_LIST_HEAD(&device->children);
675 INIT_LIST_HEAD(&device->node);
676 INIT_LIST_HEAD(&device->wakeup_list);
677 INIT_LIST_HEAD(&device->physical_node_list);
678 INIT_LIST_HEAD(&device->del_list);
679 mutex_init(&device->physical_node_lock);
681 mutex_lock(&acpi_device_lock);
683 acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
684 if (acpi_device_bus_id) {
685 result = acpi_device_set_name(device, acpi_device_bus_id);
689 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
691 if (!acpi_device_bus_id) {
695 acpi_device_bus_id->bus_id =
696 kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
697 if (!acpi_device_bus_id->bus_id) {
698 kfree(acpi_device_bus_id);
703 ida_init(&acpi_device_bus_id->instance_ida);
705 result = acpi_device_set_name(device, acpi_device_bus_id);
707 kfree_const(acpi_device_bus_id->bus_id);
708 kfree(acpi_device_bus_id);
712 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
716 list_add_tail(&device->node, &device->parent->children);
718 if (device->wakeup.flags.valid)
719 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
720 mutex_unlock(&acpi_device_lock);
723 device->dev.parent = &device->parent->dev;
724 device->dev.bus = &acpi_bus_type;
725 device->dev.release = release;
726 result = device_add(&device->dev);
728 dev_err(&device->dev, "Error registering device\n");
732 result = acpi_device_setup_files(device);
734 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
735 dev_name(&device->dev));
740 mutex_lock(&acpi_device_lock);
742 list_del(&device->node);
743 list_del(&device->wakeup_list);
746 mutex_unlock(&acpi_device_lock);
748 acpi_detach_data(device->handle, acpi_scan_drop_device);
752 /* --------------------------------------------------------------------------
754 -------------------------------------------------------------------------- */
755 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
757 struct acpi_device *device = NULL;
761 * Fixed hardware devices do not appear in the namespace and do not
762 * have handles, but we fabricate acpi_devices for them, so we have
763 * to deal with them specially.
769 status = acpi_get_parent(handle, &handle);
770 if (ACPI_FAILURE(status))
771 return status == AE_NULL_ENTRY ? NULL : acpi_root;
772 } while (acpi_bus_get_device(handle, &device));
777 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
781 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
782 union acpi_object *obj;
784 status = acpi_get_handle(handle, "_EJD", &tmp);
785 if (ACPI_FAILURE(status))
788 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
789 if (ACPI_SUCCESS(status)) {
790 obj = buffer.pointer;
791 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
793 kfree(buffer.pointer);
797 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
799 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
800 struct acpi_device_wakeup *wakeup)
802 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
803 union acpi_object *package = NULL;
804 union acpi_object *element = NULL;
811 INIT_LIST_HEAD(&wakeup->resources);
814 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
815 if (ACPI_FAILURE(status)) {
816 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
820 package = (union acpi_object *)buffer.pointer;
822 if (!package || package->package.count < 2)
825 element = &(package->package.elements[0]);
829 if (element->type == ACPI_TYPE_PACKAGE) {
830 if ((element->package.count < 2) ||
831 (element->package.elements[0].type !=
832 ACPI_TYPE_LOCAL_REFERENCE)
833 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
837 element->package.elements[0].reference.handle;
839 (u32) element->package.elements[1].integer.value;
840 } else if (element->type == ACPI_TYPE_INTEGER) {
841 wakeup->gpe_device = NULL;
842 wakeup->gpe_number = element->integer.value;
847 element = &(package->package.elements[1]);
848 if (element->type != ACPI_TYPE_INTEGER)
851 wakeup->sleep_state = element->integer.value;
853 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
857 if (!list_empty(&wakeup->resources)) {
860 err = acpi_power_wakeup_list_init(&wakeup->resources,
863 acpi_handle_warn(handle, "Retrieving current states "
864 "of wakeup power resources failed\n");
865 acpi_power_resources_list_free(&wakeup->resources);
868 if (sleep_state < wakeup->sleep_state) {
869 acpi_handle_warn(handle, "Overriding _PRW sleep state "
870 "(S%d) by S%d from power resources\n",
871 (int)wakeup->sleep_state, sleep_state);
872 wakeup->sleep_state = sleep_state;
877 kfree(buffer.pointer);
881 static bool acpi_wakeup_gpe_init(struct acpi_device *device)
883 static const struct acpi_device_id button_device_ids[] = {
889 struct acpi_device_wakeup *wakeup = &device->wakeup;
892 wakeup->flags.notifier_present = 0;
894 /* Power button, Lid switch always enable wakeup */
895 if (!acpi_match_device_ids(device, button_device_ids)) {
896 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
897 /* Do not use Lid/sleep button for S5 wakeup */
898 if (wakeup->sleep_state == ACPI_STATE_S5)
899 wakeup->sleep_state = ACPI_STATE_S4;
901 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
902 device_set_wakeup_capable(&device->dev, true);
906 status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
908 return ACPI_SUCCESS(status);
911 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
915 /* Presence of _PRW indicates wake capable */
916 if (!acpi_has_method(device->handle, "_PRW"))
919 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
922 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
926 device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
927 device->wakeup.prepare_count = 0;
929 * Call _PSW/_DSW object to disable its ability to wake the sleeping
930 * system for the ACPI device with the _PRW object.
931 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
932 * So it is necessary to call _DSW object first. Only when it is not
933 * present will the _PSW object used.
935 err = acpi_device_sleep_wake(device, 0, 0, 0);
937 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
938 "error in _DSW or _PSW evaluation\n"));
941 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
943 struct acpi_device_power_state *ps = &device->power.states[state];
944 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
945 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
948 INIT_LIST_HEAD(&ps->resources);
950 /* Evaluate "_PRx" to get referenced power resources */
951 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
952 if (ACPI_SUCCESS(status)) {
953 union acpi_object *package = buffer.pointer;
955 if (buffer.length && package
956 && package->type == ACPI_TYPE_PACKAGE
957 && package->package.count)
958 acpi_extract_power_resources(package, 0, &ps->resources);
960 ACPI_FREE(buffer.pointer);
963 /* Evaluate "_PSx" to see if we can do explicit sets */
965 if (acpi_has_method(device->handle, pathname))
966 ps->flags.explicit_set = 1;
968 /* State is valid if there are means to put the device into it. */
969 if (!list_empty(&ps->resources) || ps->flags.explicit_set)
972 ps->power = -1; /* Unknown - driver assigned */
973 ps->latency = -1; /* Unknown - driver assigned */
976 static void acpi_bus_get_power_flags(struct acpi_device *device)
980 /* Presence of _PS0|_PR0 indicates 'power manageable' */
981 if (!acpi_has_method(device->handle, "_PS0") &&
982 !acpi_has_method(device->handle, "_PR0"))
985 device->flags.power_manageable = 1;
988 * Power Management Flags
990 if (acpi_has_method(device->handle, "_PSC"))
991 device->power.flags.explicit_get = 1;
993 if (acpi_has_method(device->handle, "_IRC"))
994 device->power.flags.inrush_current = 1;
996 if (acpi_has_method(device->handle, "_DSW"))
997 device->power.flags.dsw_present = 1;
1000 * Enumerate supported power management states
1002 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1003 acpi_bus_init_power_state(device, i);
1005 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1007 /* Set the defaults for D0 and D3hot (always supported). */
1008 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1009 device->power.states[ACPI_STATE_D0].power = 100;
1010 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1013 * Use power resources only if the D0 list of them is populated, because
1014 * some platforms may provide _PR3 only to indicate D3cold support and
1015 * in those cases the power resources list returned by it may be bogus.
1017 if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1018 device->power.flags.power_resources = 1;
1020 * D3cold is supported if the D3hot list of power resources is
1023 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1024 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1027 if (acpi_bus_init_power(device))
1028 device->flags.power_manageable = 0;
1031 static void acpi_bus_get_flags(struct acpi_device *device)
1033 /* Presence of _STA indicates 'dynamic_status' */
1034 if (acpi_has_method(device->handle, "_STA"))
1035 device->flags.dynamic_status = 1;
1037 /* Presence of _RMV indicates 'removable' */
1038 if (acpi_has_method(device->handle, "_RMV"))
1039 device->flags.removable = 1;
1041 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1042 if (acpi_has_method(device->handle, "_EJD") ||
1043 acpi_has_method(device->handle, "_EJ0"))
1044 device->flags.ejectable = 1;
1047 static void acpi_device_get_busid(struct acpi_device *device)
1049 char bus_id[5] = { '?', 0 };
1050 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1056 * The device's Bus ID is simply the object name.
1057 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1059 if (ACPI_IS_ROOT_DEVICE(device)) {
1060 strcpy(device->pnp.bus_id, "ACPI");
1064 switch (device->device_type) {
1065 case ACPI_BUS_TYPE_POWER_BUTTON:
1066 strcpy(device->pnp.bus_id, "PWRF");
1068 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1069 strcpy(device->pnp.bus_id, "SLPF");
1071 case ACPI_BUS_TYPE_ECDT_EC:
1072 strcpy(device->pnp.bus_id, "ECDT");
1075 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1076 /* Clean up trailing underscores (if any) */
1077 for (i = 3; i > 1; i--) {
1078 if (bus_id[i] == '_')
1083 strcpy(device->pnp.bus_id, bus_id);
1089 * acpi_ata_match - see if an acpi object is an ATA device
1091 * If an acpi object has one of the ACPI ATA methods defined,
1092 * then we can safely call it an ATA device.
1094 bool acpi_ata_match(acpi_handle handle)
1096 return acpi_has_method(handle, "_GTF") ||
1097 acpi_has_method(handle, "_GTM") ||
1098 acpi_has_method(handle, "_STM") ||
1099 acpi_has_method(handle, "_SDD");
1103 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1105 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1106 * then we can safely call it an ejectable drive bay
1108 bool acpi_bay_match(acpi_handle handle)
1110 acpi_handle phandle;
1112 if (!acpi_has_method(handle, "_EJ0"))
1114 if (acpi_ata_match(handle))
1116 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1119 return acpi_ata_match(phandle);
1122 bool acpi_device_is_battery(struct acpi_device *adev)
1124 struct acpi_hardware_id *hwid;
1126 list_for_each_entry(hwid, &adev->pnp.ids, list)
1127 if (!strcmp("PNP0C0A", hwid->id))
1133 static bool is_ejectable_bay(struct acpi_device *adev)
1135 acpi_handle handle = adev->handle;
1137 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1140 return acpi_bay_match(handle);
1144 * acpi_dock_match - see if an acpi object has a _DCK method
1146 bool acpi_dock_match(acpi_handle handle)
1148 return acpi_has_method(handle, "_DCK");
1152 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1153 void **return_value)
1155 long *cap = context;
1157 if (acpi_has_method(handle, "_BCM") &&
1158 acpi_has_method(handle, "_BCL")) {
1159 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
1161 *cap |= ACPI_VIDEO_BACKLIGHT;
1162 /* We have backlight support, no need to scan further */
1163 return AE_CTRL_TERMINATE;
1168 /* Returns true if the ACPI object is a video device which can be
1169 * handled by video.ko.
1170 * The device will get a Linux specific CID added in scan.c to
1171 * identify the device as an ACPI graphics device
1172 * Be aware that the graphics device may not be physically present
1173 * Use acpi_video_get_capabilities() to detect general ACPI video
1174 * capabilities of present cards
1176 long acpi_is_video_device(acpi_handle handle)
1178 long video_caps = 0;
1180 /* Is this device able to support video switching ? */
1181 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1182 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1184 /* Is this device able to retrieve a video ROM ? */
1185 if (acpi_has_method(handle, "_ROM"))
1186 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1188 /* Is this device able to configure which video head to be POSTed ? */
1189 if (acpi_has_method(handle, "_VPO") &&
1190 acpi_has_method(handle, "_GPD") &&
1191 acpi_has_method(handle, "_SPD"))
1192 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1194 /* Only check for backlight functionality if one of the above hit. */
1196 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1197 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1202 EXPORT_SYMBOL(acpi_is_video_device);
1204 const char *acpi_device_hid(struct acpi_device *device)
1206 struct acpi_hardware_id *hid;
1208 if (list_empty(&device->pnp.ids))
1211 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1214 EXPORT_SYMBOL(acpi_device_hid);
1216 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1218 struct acpi_hardware_id *id;
1220 id = kmalloc(sizeof(*id), GFP_KERNEL);
1224 id->id = kstrdup_const(dev_id, GFP_KERNEL);
1230 list_add_tail(&id->list, &pnp->ids);
1231 pnp->type.hardware_id = 1;
1235 * Old IBM workstations have a DSDT bug wherein the SMBus object
1236 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1237 * prefix. Work around this.
1239 static bool acpi_ibm_smbus_match(acpi_handle handle)
1241 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1242 struct acpi_buffer path = { sizeof(node_name), node_name };
1244 if (!dmi_name_in_vendors("IBM"))
1247 /* Look for SMBS object */
1248 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1249 strcmp("SMBS", path.pointer))
1252 /* Does it have the necessary (but misnamed) methods? */
1253 if (acpi_has_method(handle, "SBI") &&
1254 acpi_has_method(handle, "SBR") &&
1255 acpi_has_method(handle, "SBW"))
1261 static bool acpi_object_is_system_bus(acpi_handle handle)
1265 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1268 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1275 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1279 struct acpi_device_info *info;
1280 struct acpi_pnp_device_id_list *cid_list;
1283 switch (device_type) {
1284 case ACPI_BUS_TYPE_DEVICE:
1285 if (handle == ACPI_ROOT_OBJECT) {
1286 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1290 status = acpi_get_object_info(handle, &info);
1291 if (ACPI_FAILURE(status)) {
1292 pr_err(PREFIX "%s: Error reading device info\n",
1297 if (info->valid & ACPI_VALID_HID) {
1298 acpi_add_id(pnp, info->hardware_id.string);
1299 pnp->type.platform_id = 1;
1301 if (info->valid & ACPI_VALID_CID) {
1302 cid_list = &info->compatible_id_list;
1303 for (i = 0; i < cid_list->count; i++)
1304 acpi_add_id(pnp, cid_list->ids[i].string);
1306 if (info->valid & ACPI_VALID_ADR) {
1307 pnp->bus_address = info->address;
1308 pnp->type.bus_address = 1;
1310 if (info->valid & ACPI_VALID_UID)
1311 pnp->unique_id = kstrdup(info->unique_id.string,
1313 if (info->valid & ACPI_VALID_CLS)
1314 acpi_add_id(pnp, info->class_code.string);
1319 * Some devices don't reliably have _HIDs & _CIDs, so add
1320 * synthetic HIDs to make sure drivers can find them.
1322 if (acpi_is_video_device(handle))
1323 acpi_add_id(pnp, ACPI_VIDEO_HID);
1324 else if (acpi_bay_match(handle))
1325 acpi_add_id(pnp, ACPI_BAY_HID);
1326 else if (acpi_dock_match(handle))
1327 acpi_add_id(pnp, ACPI_DOCK_HID);
1328 else if (acpi_ibm_smbus_match(handle))
1329 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1330 else if (list_empty(&pnp->ids) &&
1331 acpi_object_is_system_bus(handle)) {
1332 /* \_SB, \_TZ, LNXSYBUS */
1333 acpi_add_id(pnp, ACPI_BUS_HID);
1334 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1335 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1339 case ACPI_BUS_TYPE_POWER:
1340 acpi_add_id(pnp, ACPI_POWER_HID);
1342 case ACPI_BUS_TYPE_PROCESSOR:
1343 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1345 case ACPI_BUS_TYPE_THERMAL:
1346 acpi_add_id(pnp, ACPI_THERMAL_HID);
1348 case ACPI_BUS_TYPE_POWER_BUTTON:
1349 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1351 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1352 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1354 case ACPI_BUS_TYPE_ECDT_EC:
1355 acpi_add_id(pnp, ACPI_ECDT_HID);
1360 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1362 struct acpi_hardware_id *id, *tmp;
1364 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1365 kfree_const(id->id);
1368 kfree(pnp->unique_id);
1372 * acpi_dma_supported - Check DMA support for the specified device.
1373 * @adev: The pointer to acpi device
1375 * Return false if DMA is not supported. Otherwise, return true
1377 bool acpi_dma_supported(struct acpi_device *adev)
1382 if (adev->flags.cca_seen)
1386 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1387 * DMA on "Intel platforms". Presumably that includes all x86 and
1388 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1390 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1397 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1398 * @adev: The pointer to acpi device
1400 * Return enum dev_dma_attr.
1402 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1404 if (!acpi_dma_supported(adev))
1405 return DEV_DMA_NOT_SUPPORTED;
1407 if (adev->flags.coherent_dma)
1408 return DEV_DMA_COHERENT;
1410 return DEV_DMA_NON_COHERENT;
1414 * acpi_dma_get_range() - Get device DMA parameters.
1416 * @dev: device to configure
1417 * @dma_addr: pointer device DMA address result
1418 * @offset: pointer to the DMA offset result
1419 * @size: pointer to DMA range size result
1421 * Evaluate DMA regions and return respectively DMA region start, offset
1422 * and size in dma_addr, offset and size on parsing success; it does not
1423 * update the passed in values on failure.
1425 * Return 0 on success, < 0 on failure.
1427 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1430 struct acpi_device *adev;
1432 struct resource_entry *rentry;
1434 struct device *dma_dev = dev;
1435 u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1438 * Walk the device tree chasing an ACPI companion with a _DMA
1439 * object while we go. Stop if we find a device with an ACPI
1440 * companion containing a _DMA method.
1443 adev = ACPI_COMPANION(dma_dev);
1444 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1447 dma_dev = dma_dev->parent;
1453 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1454 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1458 ret = acpi_dev_get_dma_resources(adev, &list);
1460 list_for_each_entry(rentry, &list, node) {
1461 if (dma_offset && rentry->offset != dma_offset) {
1463 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1466 dma_offset = rentry->offset;
1468 /* Take lower and upper limits */
1469 if (rentry->res->start < dma_start)
1470 dma_start = rentry->res->start;
1471 if (rentry->res->end > dma_end)
1472 dma_end = rentry->res->end;
1475 if (dma_start >= dma_end) {
1477 dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1481 *dma_addr = dma_start - dma_offset;
1482 len = dma_end - dma_start;
1483 *size = max(len, len + 1);
1484 *offset = dma_offset;
1487 acpi_dev_free_resource_list(&list);
1489 return ret >= 0 ? 0 : ret;
1493 * acpi_dma_configure - Set-up DMA configuration for the device.
1494 * @dev: The pointer to the device
1495 * @attr: device dma attributes
1497 int acpi_dma_configure(struct device *dev, enum dev_dma_attr attr)
1499 const struct iommu_ops *iommu;
1500 u64 dma_addr = 0, size = 0;
1502 iort_dma_setup(dev, &dma_addr, &size);
1504 iommu = iort_iommu_configure(dev);
1505 if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
1506 return -EPROBE_DEFER;
1508 arch_setup_dma_ops(dev, dma_addr, size,
1509 iommu, attr == DEV_DMA_COHERENT);
1513 EXPORT_SYMBOL_GPL(acpi_dma_configure);
1516 * acpi_dma_deconfigure - Tear-down DMA configuration for the device.
1517 * @dev: The pointer to the device
1519 void acpi_dma_deconfigure(struct device *dev)
1521 arch_teardown_dma_ops(dev);
1523 EXPORT_SYMBOL_GPL(acpi_dma_deconfigure);
1525 static void acpi_init_coherency(struct acpi_device *adev)
1527 unsigned long long cca = 0;
1529 struct acpi_device *parent = adev->parent;
1531 if (parent && parent->flags.cca_seen) {
1533 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1536 adev->flags.cca_seen = 1;
1537 cca = parent->flags.coherent_dma;
1539 status = acpi_evaluate_integer(adev->handle, "_CCA",
1541 if (ACPI_SUCCESS(status))
1542 adev->flags.cca_seen = 1;
1543 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1545 * If architecture does not specify that _CCA is
1546 * required for DMA-able devices (e.g. x86),
1547 * we default to _CCA=1.
1551 acpi_handle_debug(adev->handle,
1552 "ACPI device is missing _CCA.\n");
1555 adev->flags.coherent_dma = cca;
1558 static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
1560 bool *is_spi_i2c_slave_p = data;
1562 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1566 * devices that are connected to UART still need to be enumerated to
1569 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
1570 *is_spi_i2c_slave_p = true;
1572 /* no need to do more checking */
1576 static bool acpi_is_spi_i2c_slave(struct acpi_device *device)
1578 struct list_head resource_list;
1579 bool is_spi_i2c_slave = false;
1581 /* Macs use device properties in lieu of _CRS resources */
1582 if (x86_apple_machine &&
1583 (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1584 fwnode_property_present(&device->fwnode, "i2cAddress")))
1587 INIT_LIST_HEAD(&resource_list);
1588 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
1590 acpi_dev_free_resource_list(&resource_list);
1592 return is_spi_i2c_slave;
1595 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1596 int type, unsigned long long sta)
1598 INIT_LIST_HEAD(&device->pnp.ids);
1599 device->device_type = type;
1600 device->handle = handle;
1601 device->parent = acpi_bus_get_parent(handle);
1602 device->fwnode.ops = &acpi_device_fwnode_ops;
1603 acpi_set_device_status(device, sta);
1604 acpi_device_get_busid(device);
1605 acpi_set_pnp_ids(handle, &device->pnp, type);
1606 acpi_init_properties(device);
1607 acpi_bus_get_flags(device);
1608 device->flags.match_driver = false;
1609 device->flags.initialized = true;
1610 device->flags.spi_i2c_slave = acpi_is_spi_i2c_slave(device);
1611 acpi_device_clear_enumerated(device);
1612 device_initialize(&device->dev);
1613 dev_set_uevent_suppress(&device->dev, true);
1614 acpi_init_coherency(device);
1615 /* Assume there are unmet deps until acpi_device_dep_initialize() runs */
1616 device->dep_unmet = 1;
1619 void acpi_device_add_finalize(struct acpi_device *device)
1621 dev_set_uevent_suppress(&device->dev, false);
1622 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1625 static int acpi_add_single_object(struct acpi_device **child,
1626 acpi_handle handle, int type,
1627 unsigned long long sta)
1630 struct acpi_device *device;
1631 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1633 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1635 printk(KERN_ERR PREFIX "Memory allocation error\n");
1639 acpi_init_device_object(device, handle, type, sta);
1641 * For ACPI_BUS_TYPE_DEVICE getting the status is delayed till here so
1642 * that we can call acpi_bus_get_status() and use its quirk handling.
1643 * Note this must be done before the get power-/wakeup_dev-flags calls.
1645 if (type == ACPI_BUS_TYPE_DEVICE)
1646 if (acpi_bus_get_status(device) < 0)
1647 acpi_set_device_status(device, 0);
1649 acpi_bus_get_power_flags(device);
1650 acpi_bus_get_wakeup_device_flags(device);
1652 result = acpi_device_add(device, acpi_device_release);
1654 acpi_device_release(&device->dev);
1658 acpi_power_add_remove_device(device, true);
1659 acpi_device_add_finalize(device);
1660 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1661 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1662 dev_name(&device->dev), (char *) buffer.pointer,
1663 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1664 kfree(buffer.pointer);
1669 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1672 struct resource *res = context;
1674 if (acpi_dev_resource_memory(ares, res))
1675 return AE_CTRL_TERMINATE;
1680 static bool acpi_device_should_be_hidden(acpi_handle handle)
1683 struct resource res;
1685 /* Check if it should ignore the UART device */
1686 if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1690 * The UART device described in SPCR table is assumed to have only one
1691 * memory resource present. So we only look for the first one here.
1693 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1694 acpi_get_resource_memory, &res);
1695 if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1698 acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1704 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1705 unsigned long long *sta)
1708 acpi_object_type acpi_type;
1710 status = acpi_get_type(handle, &acpi_type);
1711 if (ACPI_FAILURE(status))
1714 switch (acpi_type) {
1715 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1716 case ACPI_TYPE_DEVICE:
1717 if (acpi_device_should_be_hidden(handle))
1720 *type = ACPI_BUS_TYPE_DEVICE;
1722 * acpi_add_single_object updates this once we've an acpi_device
1723 * so that acpi_bus_get_status' quirk handling can be used.
1725 *sta = ACPI_STA_DEFAULT;
1727 case ACPI_TYPE_PROCESSOR:
1728 *type = ACPI_BUS_TYPE_PROCESSOR;
1729 status = acpi_bus_get_status_handle(handle, sta);
1730 if (ACPI_FAILURE(status))
1733 case ACPI_TYPE_THERMAL:
1734 *type = ACPI_BUS_TYPE_THERMAL;
1735 *sta = ACPI_STA_DEFAULT;
1737 case ACPI_TYPE_POWER:
1738 *type = ACPI_BUS_TYPE_POWER;
1739 *sta = ACPI_STA_DEFAULT;
1748 bool acpi_device_is_present(const struct acpi_device *adev)
1750 return adev->status.present || adev->status.functional;
1753 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1755 const struct acpi_device_id **matchid)
1757 const struct acpi_device_id *devid;
1760 return handler->match(idstr, matchid);
1762 for (devid = handler->ids; devid->id[0]; devid++)
1763 if (!strcmp((char *)devid->id, idstr)) {
1773 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1774 const struct acpi_device_id **matchid)
1776 struct acpi_scan_handler *handler;
1778 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1779 if (acpi_scan_handler_matching(handler, idstr, matchid))
1785 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1787 if (!!hotplug->enabled == !!val)
1790 mutex_lock(&acpi_scan_lock);
1792 hotplug->enabled = val;
1794 mutex_unlock(&acpi_scan_lock);
1797 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1799 struct acpi_hardware_id *hwid;
1801 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1802 acpi_dock_add(adev);
1805 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1806 struct acpi_scan_handler *handler;
1808 handler = acpi_scan_match_handler(hwid->id, NULL);
1810 adev->flags.hotplug_notify = true;
1816 static void acpi_device_dep_initialize(struct acpi_device *adev)
1818 struct acpi_dep_data *dep;
1819 struct acpi_handle_list dep_devices;
1823 adev->dep_unmet = 0;
1825 if (!acpi_has_method(adev->handle, "_DEP"))
1828 status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
1830 if (ACPI_FAILURE(status)) {
1831 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
1835 for (i = 0; i < dep_devices.count; i++) {
1836 struct acpi_device_info *info;
1839 status = acpi_get_object_info(dep_devices.handles[i], &info);
1840 if (ACPI_FAILURE(status)) {
1841 dev_dbg(&adev->dev, "Error reading _DEP device info\n");
1846 * Skip the dependency of Windows System Power
1847 * Management Controller
1849 skip = info->valid & ACPI_VALID_HID &&
1850 !strcmp(info->hardware_id.string, "INT3396");
1857 dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
1861 dep->master = dep_devices.handles[i];
1862 dep->slave = adev->handle;
1865 mutex_lock(&acpi_dep_list_lock);
1866 list_add_tail(&dep->node , &acpi_dep_list);
1867 mutex_unlock(&acpi_dep_list_lock);
1871 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1872 void *not_used, void **return_value)
1874 struct acpi_device *device = NULL;
1876 unsigned long long sta;
1879 acpi_bus_get_device(handle, &device);
1883 result = acpi_bus_type_and_status(handle, &type, &sta);
1887 if (type == ACPI_BUS_TYPE_POWER) {
1888 acpi_add_power_resource(handle);
1892 acpi_add_single_object(&device, handle, type, sta);
1894 return AE_CTRL_DEPTH;
1896 acpi_scan_init_hotplug(device);
1897 acpi_device_dep_initialize(device);
1901 *return_value = device;
1906 static void acpi_default_enumeration(struct acpi_device *device)
1909 * Do not enumerate SPI/I2C slaves as they will be enumerated by their
1910 * respective parents.
1912 if (!device->flags.spi_i2c_slave) {
1913 acpi_create_platform_device(device, NULL);
1914 acpi_device_set_enumerated(device);
1916 blocking_notifier_call_chain(&acpi_reconfig_chain,
1917 ACPI_RECONFIG_DEVICE_ADD, device);
1921 static const struct acpi_device_id generic_device_ids[] = {
1922 {ACPI_DT_NAMESPACE_HID, },
1926 static int acpi_generic_device_attach(struct acpi_device *adev,
1927 const struct acpi_device_id *not_used)
1930 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
1931 * below can be unconditional.
1933 if (adev->data.of_compatible)
1934 acpi_default_enumeration(adev);
1939 static struct acpi_scan_handler generic_device_handler = {
1940 .ids = generic_device_ids,
1941 .attach = acpi_generic_device_attach,
1944 static int acpi_scan_attach_handler(struct acpi_device *device)
1946 struct acpi_hardware_id *hwid;
1949 list_for_each_entry(hwid, &device->pnp.ids, list) {
1950 const struct acpi_device_id *devid;
1951 struct acpi_scan_handler *handler;
1953 handler = acpi_scan_match_handler(hwid->id, &devid);
1955 if (!handler->attach) {
1956 device->pnp.type.platform_id = 0;
1959 device->handler = handler;
1960 ret = handler->attach(device, devid);
1964 device->handler = NULL;
1973 static void acpi_bus_attach(struct acpi_device *device)
1975 struct acpi_device *child;
1979 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
1980 register_dock_dependent_device(device, ejd);
1982 acpi_bus_get_status(device);
1983 /* Skip devices that are not present. */
1984 if (!acpi_device_is_present(device)) {
1985 device->flags.initialized = false;
1986 acpi_device_clear_enumerated(device);
1987 device->flags.power_manageable = 0;
1990 if (device->handler)
1993 if (!device->flags.initialized) {
1994 device->flags.power_manageable =
1995 device->power.states[ACPI_STATE_D0].flags.valid;
1996 if (acpi_bus_init_power(device))
1997 device->flags.power_manageable = 0;
1999 device->flags.initialized = true;
2000 } else if (device->flags.visited) {
2004 ret = acpi_scan_attach_handler(device);
2008 device->flags.match_driver = true;
2009 if (ret > 0 && !device->flags.spi_i2c_slave) {
2010 acpi_device_set_enumerated(device);
2014 ret = device_attach(&device->dev);
2018 if (!device->pnp.type.platform_id && !device->flags.spi_i2c_slave)
2019 acpi_device_set_enumerated(device);
2021 acpi_default_enumeration(device);
2024 list_for_each_entry(child, &device->children, node)
2025 acpi_bus_attach(child);
2027 if (device->handler && device->handler->hotplug.notify_online)
2028 device->handler->hotplug.notify_online(device);
2031 void acpi_walk_dep_device_list(acpi_handle handle)
2033 struct acpi_dep_data *dep, *tmp;
2034 struct acpi_device *adev;
2036 mutex_lock(&acpi_dep_list_lock);
2037 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2038 if (dep->master == handle) {
2039 acpi_bus_get_device(dep->slave, &adev);
2044 if (!adev->dep_unmet)
2045 acpi_bus_attach(adev);
2046 list_del(&dep->node);
2050 mutex_unlock(&acpi_dep_list_lock);
2052 EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2055 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2056 * @handle: Root of the namespace scope to scan.
2058 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2061 * If no devices were found, -ENODEV is returned, but it does not mean that
2062 * there has been a real error. There just have been no suitable ACPI objects
2063 * in the table trunk from which the kernel could create a device and add an
2064 * appropriate driver.
2066 * Must be called under acpi_scan_lock.
2068 int acpi_bus_scan(acpi_handle handle)
2070 void *device = NULL;
2072 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
2073 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2074 acpi_bus_check_add, NULL, NULL, &device);
2077 acpi_bus_attach(device);
2082 EXPORT_SYMBOL(acpi_bus_scan);
2085 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2086 * @adev: Root of the ACPI namespace scope to walk.
2088 * Must be called under acpi_scan_lock.
2090 void acpi_bus_trim(struct acpi_device *adev)
2092 struct acpi_scan_handler *handler = adev->handler;
2093 struct acpi_device *child;
2095 list_for_each_entry_reverse(child, &adev->children, node)
2096 acpi_bus_trim(child);
2098 adev->flags.match_driver = false;
2100 if (handler->detach)
2101 handler->detach(adev);
2103 adev->handler = NULL;
2105 device_release_driver(&adev->dev);
2108 * Most likely, the device is going away, so put it into D3cold before
2111 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2112 adev->flags.initialized = false;
2113 acpi_device_clear_enumerated(adev);
2115 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2117 int acpi_bus_register_early_device(int type)
2119 struct acpi_device *device = NULL;
2122 result = acpi_add_single_object(&device, NULL,
2123 type, ACPI_STA_DEFAULT);
2127 device->flags.match_driver = true;
2128 return device_attach(&device->dev);
2130 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2132 static int acpi_bus_scan_fixed(void)
2137 * Enumerate all fixed-feature devices.
2139 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2140 struct acpi_device *device = NULL;
2142 result = acpi_add_single_object(&device, NULL,
2143 ACPI_BUS_TYPE_POWER_BUTTON,
2148 device->flags.match_driver = true;
2149 result = device_attach(&device->dev);
2153 device_init_wakeup(&device->dev, true);
2156 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2157 struct acpi_device *device = NULL;
2159 result = acpi_add_single_object(&device, NULL,
2160 ACPI_BUS_TYPE_SLEEP_BUTTON,
2165 device->flags.match_driver = true;
2166 result = device_attach(&device->dev);
2169 return result < 0 ? result : 0;
2172 static void __init acpi_get_spcr_uart_addr(void)
2175 struct acpi_table_spcr *spcr_ptr;
2177 status = acpi_get_table(ACPI_SIG_SPCR, 0,
2178 (struct acpi_table_header **)&spcr_ptr);
2179 if (ACPI_SUCCESS(status))
2180 spcr_uart_addr = spcr_ptr->serial_port.address;
2182 printk(KERN_WARNING PREFIX "STAO table present, but SPCR is missing\n");
2185 static bool acpi_scan_initialized;
2187 int __init acpi_scan_init(void)
2191 struct acpi_table_stao *stao_ptr;
2193 acpi_pci_root_init();
2194 acpi_pci_link_init();
2195 acpi_processor_init();
2198 acpi_cmos_rtc_init();
2199 acpi_container_init();
2200 acpi_memory_hotplug_init();
2202 acpi_int340x_thermal_init();
2204 acpi_watchdog_init();
2206 acpi_scan_add_handler(&generic_device_handler);
2209 * If there is STAO table, check whether it needs to ignore the UART
2210 * device in SPCR table.
2212 status = acpi_get_table(ACPI_SIG_STAO, 0,
2213 (struct acpi_table_header **)&stao_ptr);
2214 if (ACPI_SUCCESS(status)) {
2215 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2216 printk(KERN_INFO PREFIX "STAO Name List not yet supported.");
2218 if (stao_ptr->ignore_uart)
2219 acpi_get_spcr_uart_addr();
2222 acpi_gpe_apply_masked_gpes();
2223 acpi_update_all_gpes();
2225 mutex_lock(&acpi_scan_lock);
2227 * Enumerate devices in the ACPI namespace.
2229 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2233 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2237 /* Fixed feature devices do not exist on HW-reduced platform */
2238 if (!acpi_gbl_reduced_hardware) {
2239 result = acpi_bus_scan_fixed();
2241 acpi_detach_data(acpi_root->handle,
2242 acpi_scan_drop_device);
2243 acpi_device_del(acpi_root);
2244 put_device(&acpi_root->dev);
2249 acpi_scan_initialized = true;
2252 mutex_unlock(&acpi_scan_lock);
2256 static struct acpi_probe_entry *ape;
2257 static int acpi_probe_count;
2258 static DEFINE_MUTEX(acpi_probe_mutex);
2260 static int __init acpi_match_madt(struct acpi_subtable_header *header,
2261 const unsigned long end)
2263 if (!ape->subtable_valid || ape->subtable_valid(header, ape))
2264 if (!ape->probe_subtbl(header, end))
2270 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2277 mutex_lock(&acpi_probe_mutex);
2278 for (ape = ap_head; nr; ape++, nr--) {
2279 if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
2280 acpi_probe_count = 0;
2281 acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2282 count += acpi_probe_count;
2285 res = acpi_table_parse(ape->id, ape->probe_table);
2290 mutex_unlock(&acpi_probe_mutex);
2295 struct acpi_table_events_work {
2296 struct work_struct work;
2301 static void acpi_table_events_fn(struct work_struct *work)
2303 struct acpi_table_events_work *tew;
2305 tew = container_of(work, struct acpi_table_events_work, work);
2307 if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2308 acpi_scan_lock_acquire();
2309 acpi_bus_scan(ACPI_ROOT_OBJECT);
2310 acpi_scan_lock_release();
2316 void acpi_scan_table_handler(u32 event, void *table, void *context)
2318 struct acpi_table_events_work *tew;
2320 if (!acpi_scan_initialized)
2323 if (event != ACPI_TABLE_EVENT_LOAD)
2326 tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2330 INIT_WORK(&tew->work, acpi_table_events_fn);
2334 schedule_work(&tew->work);
2337 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2339 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2341 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2343 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2345 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2347 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);