1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 #define pr_fmt(fmt) "ACPI: " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/ioport.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/sched.h>
17 #include <linux/device.h>
18 #include <linux/proc_fs.h>
19 #include <linux/acpi.h>
20 #include <linux/slab.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/workqueue.h>
23 #include <linux/reboot.h>
24 #include <linux/delay.h>
26 #include <asm/mpspec.h>
27 #include <linux/dmi.h>
29 #include <linux/acpi_agdi.h>
30 #include <linux/acpi_iort.h>
31 #include <linux/acpi_viot.h>
32 #include <linux/pci.h>
33 #include <acpi/apei.h>
34 #include <linux/suspend.h>
35 #include <linux/prmt.h>
39 struct acpi_device *acpi_root;
40 struct proc_dir_entry *acpi_root_dir;
41 EXPORT_SYMBOL(acpi_root_dir);
44 #ifdef CONFIG_ACPI_CUSTOM_DSDT
45 static inline int set_copy_dsdt(const struct dmi_system_id *id)
50 static int set_copy_dsdt(const struct dmi_system_id *id)
52 pr_notice("%s detected - force copy of DSDT to local memory\n", id->ident);
53 acpi_gbl_copy_dsdt_locally = 1;
58 static const struct dmi_system_id dsdt_dmi_table[] __initconst = {
60 * Invoke DSDT corruption work-around on all Toshiba Satellite.
61 * https://bugzilla.kernel.org/show_bug.cgi?id=14679
64 .callback = set_copy_dsdt,
65 .ident = "TOSHIBA Satellite",
67 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
68 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
75 /* --------------------------------------------------------------------------
77 -------------------------------------------------------------------------- */
79 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
80 unsigned long long *sta)
84 status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
85 if (ACPI_SUCCESS(status))
88 if (status == AE_NOT_FOUND) {
89 *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
90 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
95 EXPORT_SYMBOL_GPL(acpi_bus_get_status_handle);
97 int acpi_bus_get_status(struct acpi_device *device)
100 unsigned long long sta;
102 if (acpi_device_override_status(device, &sta)) {
103 acpi_set_device_status(device, sta);
107 /* Battery devices must have their deps met before calling _STA */
108 if (acpi_device_is_battery(device) && device->dep_unmet) {
109 acpi_set_device_status(device, 0);
113 status = acpi_bus_get_status_handle(device->handle, &sta);
114 if (ACPI_FAILURE(status))
117 acpi_set_device_status(device, sta);
119 if (device->status.functional && !device->status.present) {
120 pr_debug("Device [%s] status [%08x]: functional but not present\n",
121 device->pnp.bus_id, (u32)sta);
124 pr_debug("Device [%s] status [%08x]\n", device->pnp.bus_id, (u32)sta);
127 EXPORT_SYMBOL(acpi_bus_get_status);
129 void acpi_bus_private_data_handler(acpi_handle handle,
134 EXPORT_SYMBOL(acpi_bus_private_data_handler);
136 int acpi_bus_attach_private_data(acpi_handle handle, void *data)
140 status = acpi_attach_data(handle,
141 acpi_bus_private_data_handler, data);
142 if (ACPI_FAILURE(status)) {
143 acpi_handle_debug(handle, "Error attaching device data\n");
149 EXPORT_SYMBOL_GPL(acpi_bus_attach_private_data);
151 int acpi_bus_get_private_data(acpi_handle handle, void **data)
158 status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
159 if (ACPI_FAILURE(status)) {
160 acpi_handle_debug(handle, "No context for object\n");
166 EXPORT_SYMBOL_GPL(acpi_bus_get_private_data);
168 void acpi_bus_detach_private_data(acpi_handle handle)
170 acpi_detach_data(handle, acpi_bus_private_data_handler);
172 EXPORT_SYMBOL_GPL(acpi_bus_detach_private_data);
174 static void acpi_print_osc_error(acpi_handle handle,
175 struct acpi_osc_context *context, char *error)
179 acpi_handle_debug(handle, "(%s): %s\n", context->uuid_str, error);
181 pr_debug("_OSC request data:");
182 for (i = 0; i < context->cap.length; i += sizeof(u32))
183 pr_debug(" %x", *((u32 *)(context->cap.pointer + i)));
188 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
191 struct acpi_object_list input;
192 union acpi_object in_params[4];
193 union acpi_object *out_obj;
196 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
200 if (guid_parse(context->uuid_str, &guid))
202 context->ret.length = ACPI_ALLOCATE_BUFFER;
203 context->ret.pointer = NULL;
205 /* Setting up input parameters */
207 input.pointer = in_params;
208 in_params[0].type = ACPI_TYPE_BUFFER;
209 in_params[0].buffer.length = 16;
210 in_params[0].buffer.pointer = (u8 *)&guid;
211 in_params[1].type = ACPI_TYPE_INTEGER;
212 in_params[1].integer.value = context->rev;
213 in_params[2].type = ACPI_TYPE_INTEGER;
214 in_params[2].integer.value = context->cap.length/sizeof(u32);
215 in_params[3].type = ACPI_TYPE_BUFFER;
216 in_params[3].buffer.length = context->cap.length;
217 in_params[3].buffer.pointer = context->cap.pointer;
219 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
220 if (ACPI_FAILURE(status))
224 return AE_NULL_OBJECT;
226 out_obj = output.pointer;
227 if (out_obj->type != ACPI_TYPE_BUFFER
228 || out_obj->buffer.length != context->cap.length) {
229 acpi_print_osc_error(handle, context,
230 "_OSC evaluation returned wrong type");
234 /* Need to ignore the bit0 in result code */
235 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
237 if (errors & OSC_REQUEST_ERROR)
238 acpi_print_osc_error(handle, context,
239 "_OSC request failed");
240 if (errors & OSC_INVALID_UUID_ERROR)
241 acpi_print_osc_error(handle, context,
242 "_OSC invalid UUID");
243 if (errors & OSC_INVALID_REVISION_ERROR)
244 acpi_print_osc_error(handle, context,
245 "_OSC invalid revision");
246 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
247 if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD]
257 context->ret.length = out_obj->buffer.length;
258 context->ret.pointer = kmemdup(out_obj->buffer.pointer,
259 context->ret.length, GFP_KERNEL);
260 if (!context->ret.pointer) {
261 status = AE_NO_MEMORY;
267 kfree(output.pointer);
270 EXPORT_SYMBOL(acpi_run_osc);
272 bool osc_sb_apei_support_acked;
275 * ACPI 6.0 Section 8.4.4.2 Idle State Coordination
276 * OSPM supports platform coordinated low power idle(LPI) states
278 bool osc_pc_lpi_support_confirmed;
279 EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed);
282 * ACPI 6.4 Operating System Capabilities for USB.
284 bool osc_sb_native_usb4_support_confirmed;
285 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed);
287 bool osc_sb_cppc_not_supported;
289 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
290 static void acpi_bus_osc_negotiate_platform_control(void)
292 u32 capbuf[2], *capbuf_ret;
293 struct acpi_osc_context context = {
294 .uuid_str = sb_uuid_str,
297 .cap.pointer = capbuf,
301 capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
302 capbuf[OSC_SUPPORT_DWORD] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
303 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
304 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PAD_SUPPORT;
305 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
306 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PPC_OST_SUPPORT;
308 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT;
309 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PCLPI_SUPPORT;
310 if (IS_ENABLED(CONFIG_ACPI_PRMT))
311 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PRM_SUPPORT;
314 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
317 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
318 if (boot_cpu_has(X86_FEATURE_HWP)) {
319 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_SUPPORT;
320 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPCV2_SUPPORT;
324 if (IS_ENABLED(CONFIG_SCHED_MC_PRIO))
325 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_DIVERSE_HIGH_SUPPORT;
327 if (IS_ENABLED(CONFIG_USB4))
328 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_NATIVE_USB4_SUPPORT;
331 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_APEI_SUPPORT;
332 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
335 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
338 capbuf_ret = context.ret.pointer;
339 if (context.ret.length <= OSC_SUPPORT_DWORD) {
340 kfree(context.ret.pointer);
345 if (boot_cpu_has(X86_FEATURE_HWP))
346 osc_sb_cppc_not_supported = !(capbuf_ret[OSC_SUPPORT_DWORD] &
347 (OSC_SB_CPC_SUPPORT | OSC_SB_CPCV2_SUPPORT));
351 * Now run _OSC again with query flag clear and with the caps
352 * supported by both the OS and the platform.
354 capbuf[OSC_QUERY_DWORD] = 0;
355 capbuf[OSC_SUPPORT_DWORD] = capbuf_ret[OSC_SUPPORT_DWORD];
356 kfree(context.ret.pointer);
358 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
361 capbuf_ret = context.ret.pointer;
362 if (context.ret.length > OSC_SUPPORT_DWORD) {
363 osc_sb_apei_support_acked =
364 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT;
365 osc_pc_lpi_support_confirmed =
366 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_PCLPI_SUPPORT;
367 osc_sb_native_usb4_support_confirmed =
368 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_NATIVE_USB4_SUPPORT;
371 kfree(context.ret.pointer);
375 * Native control of USB4 capabilities. If any of the tunneling bits is
376 * set it means OS is in control and we use software based connection
379 u32 osc_sb_native_usb4_control;
380 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control);
382 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits)
384 pr_info("%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg,
385 (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-',
386 (bits & OSC_USB_DP_TUNNELING) ? '+' : '-',
387 (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-',
388 (bits & OSC_USB_XDOMAIN) ? '+' : '-');
391 static u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A";
392 static void acpi_bus_osc_negotiate_usb_control(void)
395 struct acpi_osc_context context = {
396 .uuid_str = sb_usb_uuid_str,
398 .cap.length = sizeof(capbuf),
399 .cap.pointer = capbuf,
405 if (!osc_sb_native_usb4_support_confirmed)
408 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
411 control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING |
412 OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN;
414 capbuf[OSC_QUERY_DWORD] = 0;
415 capbuf[OSC_SUPPORT_DWORD] = 0;
416 capbuf[OSC_CONTROL_DWORD] = control;
418 status = acpi_run_osc(handle, &context);
419 if (ACPI_FAILURE(status))
422 if (context.ret.length != sizeof(capbuf)) {
423 pr_info("USB4 _OSC: returned invalid length buffer\n");
427 osc_sb_native_usb4_control =
428 control & ((u32 *)context.ret.pointer)[OSC_CONTROL_DWORD];
430 acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control);
431 acpi_bus_decode_usb_osc("USB4 _OSC: OS controls",
432 osc_sb_native_usb4_control);
435 kfree(context.ret.pointer);
438 /* --------------------------------------------------------------------------
439 Notification Handling
440 -------------------------------------------------------------------------- */
445 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
447 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
449 struct acpi_device *adev;
450 struct acpi_driver *driver;
451 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
452 bool hotplug_event = false;
455 case ACPI_NOTIFY_BUS_CHECK:
456 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
457 hotplug_event = true;
460 case ACPI_NOTIFY_DEVICE_CHECK:
461 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
462 hotplug_event = true;
465 case ACPI_NOTIFY_DEVICE_WAKE:
466 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
469 case ACPI_NOTIFY_EJECT_REQUEST:
470 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
471 hotplug_event = true;
474 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
475 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
476 /* TBD: Exactly what does 'light' mean? */
479 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
480 acpi_handle_err(handle, "Device cannot be configured due "
481 "to a frequency mismatch\n");
484 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
485 acpi_handle_err(handle, "Device cannot be configured due "
486 "to a bus mode mismatch\n");
489 case ACPI_NOTIFY_POWER_FAULT:
490 acpi_handle_err(handle, "Device has suffered a power fault\n");
494 acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
498 adev = acpi_bus_get_acpi_device(handle);
502 driver = adev->driver;
503 if (driver && driver->ops.notify &&
504 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
505 driver->ops.notify(adev, type);
507 if (!hotplug_event) {
508 acpi_bus_put_acpi_device(adev);
512 if (ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
515 acpi_bus_put_acpi_device(adev);
518 acpi_evaluate_ost(handle, type, ost_code, NULL);
521 static void acpi_notify_device(acpi_handle handle, u32 event, void *data)
523 struct acpi_device *device = data;
525 device->driver->ops.notify(device, event);
528 static void acpi_notify_device_fixed(void *data)
530 struct acpi_device *device = data;
532 /* Fixed hardware devices have no handles */
533 acpi_notify_device(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
536 static u32 acpi_device_fixed_event(void *data)
538 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_notify_device_fixed, data);
539 return ACPI_INTERRUPT_HANDLED;
542 static int acpi_device_install_notify_handler(struct acpi_device *device)
546 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
548 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
549 acpi_device_fixed_event,
551 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
553 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
554 acpi_device_fixed_event,
557 status = acpi_install_notify_handler(device->handle,
562 if (ACPI_FAILURE(status))
567 static void acpi_device_remove_notify_handler(struct acpi_device *device)
569 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
570 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
571 acpi_device_fixed_event);
572 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
573 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
574 acpi_device_fixed_event);
576 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
580 /* Handle events targeting \_SB device (at present only graceful shutdown) */
582 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
583 #define ACPI_SB_INDICATE_INTERVAL 10000
585 static void sb_notify_work(struct work_struct *dummy)
587 acpi_handle sb_handle;
589 orderly_poweroff(true);
592 * After initiating graceful shutdown, the ACPI spec requires OSPM
593 * to evaluate _OST method once every 10seconds to indicate that
594 * the shutdown is in progress
596 acpi_get_handle(NULL, "\\_SB", &sb_handle);
598 pr_info("Graceful shutdown in progress.\n");
599 acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
600 ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
601 msleep(ACPI_SB_INDICATE_INTERVAL);
605 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
607 static DECLARE_WORK(acpi_sb_work, sb_notify_work);
609 if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
610 if (!work_busy(&acpi_sb_work))
611 schedule_work(&acpi_sb_work);
613 pr_warn("event %x is not supported by \\_SB device\n", event);
616 static int __init acpi_setup_sb_notify_handler(void)
618 acpi_handle sb_handle;
620 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
623 if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
624 acpi_sb_notify, NULL)))
630 /* --------------------------------------------------------------------------
632 -------------------------------------------------------------------------- */
635 * acpi_get_first_physical_node - Get first physical node of an ACPI device
636 * @adev: ACPI device in question
638 * Return: First physical node of ACPI device @adev
640 struct device *acpi_get_first_physical_node(struct acpi_device *adev)
642 struct mutex *physical_node_lock = &adev->physical_node_lock;
643 struct device *phys_dev;
645 mutex_lock(physical_node_lock);
646 if (list_empty(&adev->physical_node_list)) {
649 const struct acpi_device_physical_node *node;
651 node = list_first_entry(&adev->physical_node_list,
652 struct acpi_device_physical_node, node);
654 phys_dev = node->dev;
656 mutex_unlock(physical_node_lock);
659 EXPORT_SYMBOL_GPL(acpi_get_first_physical_node);
661 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
662 const struct device *dev)
664 const struct device *phys_dev = acpi_get_first_physical_node(adev);
666 return phys_dev && phys_dev == dev ? adev : NULL;
670 * acpi_device_is_first_physical_node - Is given dev first physical node
671 * @adev: ACPI companion device
672 * @dev: Physical device to check
674 * Function checks if given @dev is the first physical devices attached to
675 * the ACPI companion device. This distinction is needed in some cases
676 * where the same companion device is shared between many physical devices.
678 * Note that the caller have to provide valid @adev pointer.
680 bool acpi_device_is_first_physical_node(struct acpi_device *adev,
681 const struct device *dev)
683 return !!acpi_primary_dev_companion(adev, dev);
687 * acpi_companion_match() - Can we match via ACPI companion device
688 * @dev: Device in question
690 * Check if the given device has an ACPI companion and if that companion has
691 * a valid list of PNP IDs, and if the device is the first (primary) physical
692 * device associated with it. Return the companion pointer if that's the case
695 * If multiple physical devices are attached to a single ACPI companion, we need
696 * to be careful. The usage scenario for this kind of relationship is that all
697 * of the physical devices in question use resources provided by the ACPI
698 * companion. A typical case is an MFD device where all the sub-devices share
699 * the parent's ACPI companion. In such cases we can only allow the primary
700 * (first) physical device to be matched with the help of the companion's PNP
703 * Additional physical devices sharing the ACPI companion can still use
704 * resources available from it but they will be matched normally using functions
705 * provided by their bus types (and analogously for their modalias).
707 struct acpi_device *acpi_companion_match(const struct device *dev)
709 struct acpi_device *adev;
711 adev = ACPI_COMPANION(dev);
715 if (list_empty(&adev->pnp.ids))
718 return acpi_primary_dev_companion(adev, dev);
722 * acpi_of_match_device - Match device object using the "compatible" property.
723 * @adev: ACPI device object to match.
724 * @of_match_table: List of device IDs to match against.
725 * @of_id: OF ID if matched
727 * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
728 * identifiers and a _DSD object with the "compatible" property, use that
729 * property to match against the given list of identifiers.
731 static bool acpi_of_match_device(struct acpi_device *adev,
732 const struct of_device_id *of_match_table,
733 const struct of_device_id **of_id)
735 const union acpi_object *of_compatible, *obj;
741 of_compatible = adev->data.of_compatible;
742 if (!of_match_table || !of_compatible)
745 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
746 nval = of_compatible->package.count;
747 obj = of_compatible->package.elements;
748 } else { /* Must be ACPI_TYPE_STRING. */
752 /* Now we can look for the driver DT compatible strings */
753 for (i = 0; i < nval; i++, obj++) {
754 const struct of_device_id *id;
756 for (id = of_match_table; id->compatible[0]; id++)
757 if (!strcasecmp(obj->string.pointer, id->compatible)) {
767 static bool acpi_of_modalias(struct acpi_device *adev,
768 char *modalias, size_t len)
770 const union acpi_object *of_compatible;
771 const union acpi_object *obj;
772 const char *str, *chr;
774 of_compatible = adev->data.of_compatible;
778 if (of_compatible->type == ACPI_TYPE_PACKAGE)
779 obj = of_compatible->package.elements;
780 else /* Must be ACPI_TYPE_STRING. */
783 str = obj->string.pointer;
784 chr = strchr(str, ',');
785 strlcpy(modalias, chr ? chr + 1 : str, len);
791 * acpi_set_modalias - Set modalias using "compatible" property or supplied ID
792 * @adev: ACPI device object to match
793 * @default_id: ID string to use as default if no compatible string found
794 * @modalias: Pointer to buffer that modalias value will be copied into
795 * @len: Length of modalias buffer
797 * This is a counterpart of of_modalias_node() for struct acpi_device objects.
798 * If there is a compatible string for @adev, it will be copied to @modalias
799 * with the vendor prefix stripped; otherwise, @default_id will be used.
801 void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
802 char *modalias, size_t len)
804 if (!acpi_of_modalias(adev, modalias, len))
805 strlcpy(modalias, default_id, len);
807 EXPORT_SYMBOL_GPL(acpi_set_modalias);
809 static bool __acpi_match_device_cls(const struct acpi_device_id *id,
810 struct acpi_hardware_id *hwid)
812 int i, msk, byte_shift;
818 /* Apply class-code bitmask, before checking each class-code byte */
819 for (i = 1; i <= 3; i++) {
820 byte_shift = 8 * (3 - i);
821 msk = (id->cls_msk >> byte_shift) & 0xFF;
825 sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
826 if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
832 static bool __acpi_match_device(struct acpi_device *device,
833 const struct acpi_device_id *acpi_ids,
834 const struct of_device_id *of_ids,
835 const struct acpi_device_id **acpi_id,
836 const struct of_device_id **of_id)
838 const struct acpi_device_id *id;
839 struct acpi_hardware_id *hwid;
842 * If the device is not present, it is unnecessary to load device
845 if (!device || !device->status.present)
848 list_for_each_entry(hwid, &device->pnp.ids, list) {
849 /* First, check the ACPI/PNP IDs provided by the caller. */
851 for (id = acpi_ids; id->id[0] || id->cls; id++) {
852 if (id->id[0] && !strcmp((char *)id->id, hwid->id))
854 if (id->cls && __acpi_match_device_cls(id, hwid))
860 * Next, check ACPI_DT_NAMESPACE_HID and try to match the
861 * "compatible" property if found.
863 if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id))
864 return acpi_of_match_device(device, of_ids, of_id);
875 * acpi_match_device - Match a struct device against a given list of ACPI IDs
876 * @ids: Array of struct acpi_device_id object to match against.
877 * @dev: The device structure to match.
879 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
880 * object for that handle and use that object to match against a given list of
883 * Return a pointer to the first matching ID on success or %NULL on failure.
885 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
886 const struct device *dev)
888 const struct acpi_device_id *id = NULL;
890 __acpi_match_device(acpi_companion_match(dev), ids, NULL, &id, NULL);
893 EXPORT_SYMBOL_GPL(acpi_match_device);
895 static const void *acpi_of_device_get_match_data(const struct device *dev)
897 struct acpi_device *adev = ACPI_COMPANION(dev);
898 const struct of_device_id *match = NULL;
900 if (!acpi_of_match_device(adev, dev->driver->of_match_table, &match))
906 const void *acpi_device_get_match_data(const struct device *dev)
908 const struct acpi_device_id *match;
910 if (!dev->driver->acpi_match_table)
911 return acpi_of_device_get_match_data(dev);
913 match = acpi_match_device(dev->driver->acpi_match_table, dev);
917 return (const void *)match->driver_data;
919 EXPORT_SYMBOL_GPL(acpi_device_get_match_data);
921 int acpi_match_device_ids(struct acpi_device *device,
922 const struct acpi_device_id *ids)
924 return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT;
926 EXPORT_SYMBOL(acpi_match_device_ids);
928 bool acpi_driver_match_device(struct device *dev,
929 const struct device_driver *drv)
931 if (!drv->acpi_match_table)
932 return acpi_of_match_device(ACPI_COMPANION(dev),
936 return __acpi_match_device(acpi_companion_match(dev),
937 drv->acpi_match_table, drv->of_match_table,
940 EXPORT_SYMBOL_GPL(acpi_driver_match_device);
942 /* --------------------------------------------------------------------------
943 ACPI Driver Management
944 -------------------------------------------------------------------------- */
947 * acpi_bus_register_driver - register a driver with the ACPI bus
948 * @driver: driver being registered
950 * Registers a driver with the ACPI bus. Searches the namespace for all
951 * devices that match the driver's criteria and binds. Returns zero for
952 * success or a negative error status for failure.
954 int acpi_bus_register_driver(struct acpi_driver *driver)
960 driver->drv.name = driver->name;
961 driver->drv.bus = &acpi_bus_type;
962 driver->drv.owner = driver->owner;
964 ret = driver_register(&driver->drv);
968 EXPORT_SYMBOL(acpi_bus_register_driver);
971 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
972 * @driver: driver to unregister
974 * Unregisters a driver with the ACPI bus. Searches the namespace for all
975 * devices that match the driver's criteria and unbinds.
977 void acpi_bus_unregister_driver(struct acpi_driver *driver)
979 driver_unregister(&driver->drv);
982 EXPORT_SYMBOL(acpi_bus_unregister_driver);
984 /* --------------------------------------------------------------------------
986 -------------------------------------------------------------------------- */
988 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
990 struct acpi_device *acpi_dev = to_acpi_device(dev);
991 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
993 return acpi_dev->flags.match_driver
994 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
997 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
999 return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
1002 static int acpi_device_probe(struct device *dev)
1004 struct acpi_device *acpi_dev = to_acpi_device(dev);
1005 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1008 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
1011 if (!acpi_drv->ops.add)
1014 ret = acpi_drv->ops.add(acpi_dev);
1018 acpi_dev->driver = acpi_drv;
1020 pr_debug("Driver [%s] successfully bound to device [%s]\n",
1021 acpi_drv->name, acpi_dev->pnp.bus_id);
1023 if (acpi_drv->ops.notify) {
1024 ret = acpi_device_install_notify_handler(acpi_dev);
1026 if (acpi_drv->ops.remove)
1027 acpi_drv->ops.remove(acpi_dev);
1029 acpi_dev->driver = NULL;
1030 acpi_dev->driver_data = NULL;
1035 pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name,
1036 acpi_dev->pnp.bus_id);
1042 static void acpi_device_remove(struct device *dev)
1044 struct acpi_device *acpi_dev = to_acpi_device(dev);
1045 struct acpi_driver *acpi_drv = acpi_dev->driver;
1048 if (acpi_drv->ops.notify)
1049 acpi_device_remove_notify_handler(acpi_dev);
1050 if (acpi_drv->ops.remove)
1051 acpi_drv->ops.remove(acpi_dev);
1053 acpi_dev->driver = NULL;
1054 acpi_dev->driver_data = NULL;
1059 struct bus_type acpi_bus_type = {
1061 .match = acpi_bus_match,
1062 .probe = acpi_device_probe,
1063 .remove = acpi_device_remove,
1064 .uevent = acpi_device_uevent,
1067 int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data)
1069 return bus_for_each_dev(&acpi_bus_type, NULL, data, fn);
1071 EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
1073 /* --------------------------------------------------------------------------
1074 Initialization/Cleanup
1075 -------------------------------------------------------------------------- */
1077 static int __init acpi_bus_init_irq(void)
1080 char *message = NULL;
1084 * Let the system know what interrupt model we are using by
1085 * evaluating the \_PIC object, if exists.
1088 switch (acpi_irq_model) {
1089 case ACPI_IRQ_MODEL_PIC:
1092 case ACPI_IRQ_MODEL_IOAPIC:
1095 case ACPI_IRQ_MODEL_IOSAPIC:
1096 message = "IOSAPIC";
1098 case ACPI_IRQ_MODEL_GIC:
1101 case ACPI_IRQ_MODEL_PLATFORM:
1102 message = "platform specific model";
1105 pr_info("Unknown interrupt routing model\n");
1109 pr_info("Using %s for interrupt routing\n", message);
1111 status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
1112 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
1113 pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status));
1121 * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
1123 * The ACPI tables are accessible after this, but the handling of events has not
1124 * been initialized and the global lock is not available yet, so AML should not
1125 * be executed at this point.
1127 * Doing this before switching the EFI runtime services to virtual mode allows
1128 * the EfiBootServices memory to be freed slightly earlier on boot.
1130 void __init acpi_early_init(void)
1137 pr_info("Core revision %08x\n", ACPI_CA_VERSION);
1139 /* enable workarounds, unless strict ACPI spec. compliance */
1141 acpi_gbl_enable_interpreter_slack = TRUE;
1143 acpi_permanent_mmap = true;
1147 * If the machine falls into the DMI check table,
1148 * DSDT will be copied to memory.
1149 * Note that calling dmi_check_system() here on other architectures
1150 * would not be OK because only x86 initializes dmi early enough.
1151 * Thankfully only x86 systems need such quirks for now.
1153 dmi_check_system(dsdt_dmi_table);
1156 status = acpi_reallocate_root_table();
1157 if (ACPI_FAILURE(status)) {
1158 pr_err("Unable to reallocate ACPI tables\n");
1162 status = acpi_initialize_subsystem();
1163 if (ACPI_FAILURE(status)) {
1164 pr_err("Unable to initialize the ACPI Interpreter\n");
1170 /* compatible (0) means level (3) */
1171 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
1172 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
1173 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
1175 /* Set PIC-mode SCI trigger type */
1176 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
1177 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
1180 * now that acpi_gbl_FADT is initialized,
1181 * update it with result from INT_SRC_OVR parsing
1183 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
1193 * acpi_subsystem_init - Finalize the early initialization of ACPI.
1195 * Switch over the platform to the ACPI mode (if possible).
1197 * Doing this too early is generally unsafe, but at the same time it needs to be
1198 * done before all things that really depend on ACPI. The right spot appears to
1199 * be before finalizing the EFI initialization.
1201 void __init acpi_subsystem_init(void)
1208 status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
1209 if (ACPI_FAILURE(status)) {
1210 pr_err("Unable to enable ACPI\n");
1214 * If the system is using ACPI then we can be reasonably
1215 * confident that any regulators are managed by the firmware
1216 * so tell the regulator core it has everything it needs to
1219 regulator_has_full_constraints();
1223 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
1225 if (event == ACPI_TABLE_EVENT_LOAD)
1226 acpi_scan_table_notify();
1228 return acpi_sysfs_table_handler(event, table, context);
1231 static int __init acpi_bus_init(void)
1236 acpi_os_initialize1();
1238 status = acpi_load_tables();
1239 if (ACPI_FAILURE(status)) {
1240 pr_err("Unable to load the System Description Tables\n");
1245 * ACPI 2.0 requires the EC driver to be loaded and work before the EC
1246 * device is found in the namespace.
1248 * This is accomplished by looking for the ECDT table and getting the EC
1249 * parameters out of that.
1251 * Do that before calling acpi_initialize_objects() which may trigger EC
1252 * address space accesses.
1254 acpi_ec_ecdt_probe();
1256 status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
1257 if (ACPI_FAILURE(status)) {
1258 pr_err("Unable to start the ACPI Interpreter\n");
1262 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1263 if (ACPI_FAILURE(status)) {
1264 pr_err("Unable to initialize ACPI objects\n");
1268 /* Set capability bits for _OSC under processor scope */
1269 acpi_early_processor_osc();
1272 * _OSC method may exist in module level code,
1273 * so it must be run after ACPI_FULL_INITIALIZATION
1275 acpi_bus_osc_negotiate_platform_control();
1276 acpi_bus_osc_negotiate_usb_control();
1279 * _PDC control method may load dynamic SSDT tables,
1280 * and we need to install the table handler before that.
1282 status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
1286 acpi_early_processor_set_pdc();
1289 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1290 * is necessary to enable it as early as possible.
1292 acpi_ec_dsdt_probe();
1294 pr_info("Interpreter enabled\n");
1296 /* Initialize sleep structures */
1300 * Get the system interrupt model and evaluate \_PIC.
1302 result = acpi_bus_init_irq();
1307 * Register the for all standard device notifications.
1310 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
1311 &acpi_bus_notify, NULL);
1312 if (ACPI_FAILURE(status)) {
1313 pr_err("Unable to register for system notifications\n");
1318 * Create the top ACPI proc directory
1320 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1322 result = bus_register(&acpi_bus_type);
1326 /* Mimic structured exception handling */
1332 struct kobject *acpi_kobj;
1333 EXPORT_SYMBOL_GPL(acpi_kobj);
1335 static int __init acpi_init(void)
1339 if (acpi_disabled) {
1340 pr_info("Interpreter disabled.\n");
1344 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1346 pr_debug("%s: kset create error\n", __func__);
1350 result = acpi_bus_init();
1352 kobject_put(acpi_kobj);
1357 pci_mmcfg_late_init();
1363 acpi_debugfs_init();
1364 acpi_sleep_proc_init();
1365 acpi_wakeup_device_init();
1366 acpi_debugger_init();
1367 acpi_setup_sb_notify_handler();
1373 subsys_initcall(acpi_init);