1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
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.2 Section 6.2.11.2 'Platform-Wide OSPM Capabilities':
283 * Starting with ACPI Specification 6.2, all _CPC registers can be in
284 * PCC, System Memory, System IO, or Functional Fixed Hardware address
285 * spaces. OSPM support for this more flexible register space scheme is
286 * indicated by the “Flexible Address Space for CPPC Registers” _OSC bit.
288 * Otherwise (cf ACPI 6.1, s8.4.7.1.1.X), _CPC registers must be in:
289 * - PCC or Functional Fixed Hardware address space if defined
290 * - SystemMemory address space (NULL register) if not defined
292 bool osc_cpc_flexible_adr_space_confirmed;
293 EXPORT_SYMBOL_GPL(osc_cpc_flexible_adr_space_confirmed);
296 * ACPI 6.4 Operating System Capabilities for USB.
298 bool osc_sb_native_usb4_support_confirmed;
299 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed);
301 bool osc_sb_cppc_not_supported;
303 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
304 static void acpi_bus_osc_negotiate_platform_control(void)
306 u32 capbuf[2], *capbuf_ret;
307 struct acpi_osc_context context = {
308 .uuid_str = sb_uuid_str,
311 .cap.pointer = capbuf,
315 capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
316 capbuf[OSC_SUPPORT_DWORD] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
317 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
318 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PAD_SUPPORT;
319 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
320 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PPC_OST_SUPPORT;
322 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT;
323 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PCLPI_SUPPORT;
324 if (IS_ENABLED(CONFIG_ACPI_PRMT))
325 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PRM_SUPPORT;
328 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
331 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
334 #ifdef CONFIG_ACPI_CPPC_LIB
335 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_SUPPORT;
336 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPCV2_SUPPORT;
339 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
341 if (IS_ENABLED(CONFIG_SCHED_MC_PRIO))
342 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_DIVERSE_HIGH_SUPPORT;
344 if (IS_ENABLED(CONFIG_USB4))
345 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_NATIVE_USB4_SUPPORT;
348 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_APEI_SUPPORT;
349 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
352 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
355 capbuf_ret = context.ret.pointer;
356 if (context.ret.length <= OSC_SUPPORT_DWORD) {
357 kfree(context.ret.pointer);
361 #ifdef CONFIG_ACPI_CPPC_LIB
362 osc_sb_cppc_not_supported = !(capbuf_ret[OSC_SUPPORT_DWORD] &
363 (OSC_SB_CPC_SUPPORT | OSC_SB_CPCV2_SUPPORT));
367 * Now run _OSC again with query flag clear and with the caps
368 * supported by both the OS and the platform.
370 capbuf[OSC_QUERY_DWORD] = 0;
371 capbuf[OSC_SUPPORT_DWORD] = capbuf_ret[OSC_SUPPORT_DWORD];
372 kfree(context.ret.pointer);
374 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
377 capbuf_ret = context.ret.pointer;
378 if (context.ret.length > OSC_SUPPORT_DWORD) {
379 osc_sb_apei_support_acked =
380 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT;
381 osc_pc_lpi_support_confirmed =
382 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_PCLPI_SUPPORT;
383 osc_sb_native_usb4_support_confirmed =
384 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_NATIVE_USB4_SUPPORT;
385 osc_cpc_flexible_adr_space_confirmed =
386 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
389 kfree(context.ret.pointer);
393 * Native control of USB4 capabilities. If any of the tunneling bits is
394 * set it means OS is in control and we use software based connection
397 u32 osc_sb_native_usb4_control;
398 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control);
400 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits)
402 pr_info("%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg,
403 (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-',
404 (bits & OSC_USB_DP_TUNNELING) ? '+' : '-',
405 (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-',
406 (bits & OSC_USB_XDOMAIN) ? '+' : '-');
409 static u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A";
410 static void acpi_bus_osc_negotiate_usb_control(void)
413 struct acpi_osc_context context = {
414 .uuid_str = sb_usb_uuid_str,
416 .cap.length = sizeof(capbuf),
417 .cap.pointer = capbuf,
423 if (!osc_sb_native_usb4_support_confirmed)
426 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
429 control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING |
430 OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN;
432 capbuf[OSC_QUERY_DWORD] = 0;
433 capbuf[OSC_SUPPORT_DWORD] = 0;
434 capbuf[OSC_CONTROL_DWORD] = control;
436 status = acpi_run_osc(handle, &context);
437 if (ACPI_FAILURE(status))
440 if (context.ret.length != sizeof(capbuf)) {
441 pr_info("USB4 _OSC: returned invalid length buffer\n");
445 osc_sb_native_usb4_control =
446 control & ((u32 *)context.ret.pointer)[OSC_CONTROL_DWORD];
448 acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control);
449 acpi_bus_decode_usb_osc("USB4 _OSC: OS controls",
450 osc_sb_native_usb4_control);
453 kfree(context.ret.pointer);
456 /* --------------------------------------------------------------------------
457 Notification Handling
458 -------------------------------------------------------------------------- */
463 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
465 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
467 struct acpi_device *adev;
468 struct acpi_driver *driver;
469 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
470 bool hotplug_event = false;
473 case ACPI_NOTIFY_BUS_CHECK:
474 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
475 hotplug_event = true;
478 case ACPI_NOTIFY_DEVICE_CHECK:
479 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
480 hotplug_event = true;
483 case ACPI_NOTIFY_DEVICE_WAKE:
484 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
487 case ACPI_NOTIFY_EJECT_REQUEST:
488 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
489 hotplug_event = true;
492 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
493 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
494 /* TBD: Exactly what does 'light' mean? */
497 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
498 acpi_handle_err(handle, "Device cannot be configured due "
499 "to a frequency mismatch\n");
502 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
503 acpi_handle_err(handle, "Device cannot be configured due "
504 "to a bus mode mismatch\n");
507 case ACPI_NOTIFY_POWER_FAULT:
508 acpi_handle_err(handle, "Device has suffered a power fault\n");
512 acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
516 adev = acpi_bus_get_acpi_device(handle);
520 driver = adev->driver;
521 if (driver && driver->ops.notify &&
522 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
523 driver->ops.notify(adev, type);
525 if (!hotplug_event) {
526 acpi_bus_put_acpi_device(adev);
530 if (ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
533 acpi_bus_put_acpi_device(adev);
536 acpi_evaluate_ost(handle, type, ost_code, NULL);
539 static void acpi_notify_device(acpi_handle handle, u32 event, void *data)
541 struct acpi_device *device = data;
543 device->driver->ops.notify(device, event);
546 static void acpi_notify_device_fixed(void *data)
548 struct acpi_device *device = data;
550 /* Fixed hardware devices have no handles */
551 acpi_notify_device(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
554 static u32 acpi_device_fixed_event(void *data)
556 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_notify_device_fixed, data);
557 return ACPI_INTERRUPT_HANDLED;
560 static int acpi_device_install_notify_handler(struct acpi_device *device)
564 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
566 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
567 acpi_device_fixed_event,
569 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
571 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
572 acpi_device_fixed_event,
575 status = acpi_install_notify_handler(device->handle,
580 if (ACPI_FAILURE(status))
585 static void acpi_device_remove_notify_handler(struct acpi_device *device)
587 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
588 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
589 acpi_device_fixed_event);
590 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
591 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
592 acpi_device_fixed_event);
594 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
598 /* Handle events targeting \_SB device (at present only graceful shutdown) */
600 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
601 #define ACPI_SB_INDICATE_INTERVAL 10000
603 static void sb_notify_work(struct work_struct *dummy)
605 acpi_handle sb_handle;
607 orderly_poweroff(true);
610 * After initiating graceful shutdown, the ACPI spec requires OSPM
611 * to evaluate _OST method once every 10seconds to indicate that
612 * the shutdown is in progress
614 acpi_get_handle(NULL, "\\_SB", &sb_handle);
616 pr_info("Graceful shutdown in progress.\n");
617 acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
618 ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
619 msleep(ACPI_SB_INDICATE_INTERVAL);
623 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
625 static DECLARE_WORK(acpi_sb_work, sb_notify_work);
627 if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
628 if (!work_busy(&acpi_sb_work))
629 schedule_work(&acpi_sb_work);
631 pr_warn("event %x is not supported by \\_SB device\n", event);
634 static int __init acpi_setup_sb_notify_handler(void)
636 acpi_handle sb_handle;
638 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
641 if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
642 acpi_sb_notify, NULL)))
648 /* --------------------------------------------------------------------------
650 -------------------------------------------------------------------------- */
653 * acpi_get_first_physical_node - Get first physical node of an ACPI device
654 * @adev: ACPI device in question
656 * Return: First physical node of ACPI device @adev
658 struct device *acpi_get_first_physical_node(struct acpi_device *adev)
660 struct mutex *physical_node_lock = &adev->physical_node_lock;
661 struct device *phys_dev;
663 mutex_lock(physical_node_lock);
664 if (list_empty(&adev->physical_node_list)) {
667 const struct acpi_device_physical_node *node;
669 node = list_first_entry(&adev->physical_node_list,
670 struct acpi_device_physical_node, node);
672 phys_dev = node->dev;
674 mutex_unlock(physical_node_lock);
677 EXPORT_SYMBOL_GPL(acpi_get_first_physical_node);
679 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
680 const struct device *dev)
682 const struct device *phys_dev = acpi_get_first_physical_node(adev);
684 return phys_dev && phys_dev == dev ? adev : NULL;
688 * acpi_device_is_first_physical_node - Is given dev first physical node
689 * @adev: ACPI companion device
690 * @dev: Physical device to check
692 * Function checks if given @dev is the first physical devices attached to
693 * the ACPI companion device. This distinction is needed in some cases
694 * where the same companion device is shared between many physical devices.
696 * Note that the caller have to provide valid @adev pointer.
698 bool acpi_device_is_first_physical_node(struct acpi_device *adev,
699 const struct device *dev)
701 return !!acpi_primary_dev_companion(adev, dev);
705 * acpi_companion_match() - Can we match via ACPI companion device
706 * @dev: Device in question
708 * Check if the given device has an ACPI companion and if that companion has
709 * a valid list of PNP IDs, and if the device is the first (primary) physical
710 * device associated with it. Return the companion pointer if that's the case
713 * If multiple physical devices are attached to a single ACPI companion, we need
714 * to be careful. The usage scenario for this kind of relationship is that all
715 * of the physical devices in question use resources provided by the ACPI
716 * companion. A typical case is an MFD device where all the sub-devices share
717 * the parent's ACPI companion. In such cases we can only allow the primary
718 * (first) physical device to be matched with the help of the companion's PNP
721 * Additional physical devices sharing the ACPI companion can still use
722 * resources available from it but they will be matched normally using functions
723 * provided by their bus types (and analogously for their modalias).
725 struct acpi_device *acpi_companion_match(const struct device *dev)
727 struct acpi_device *adev;
729 adev = ACPI_COMPANION(dev);
733 if (list_empty(&adev->pnp.ids))
736 return acpi_primary_dev_companion(adev, dev);
740 * acpi_of_match_device - Match device object using the "compatible" property.
741 * @adev: ACPI device object to match.
742 * @of_match_table: List of device IDs to match against.
743 * @of_id: OF ID if matched
745 * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
746 * identifiers and a _DSD object with the "compatible" property, use that
747 * property to match against the given list of identifiers.
749 static bool acpi_of_match_device(struct acpi_device *adev,
750 const struct of_device_id *of_match_table,
751 const struct of_device_id **of_id)
753 const union acpi_object *of_compatible, *obj;
759 of_compatible = adev->data.of_compatible;
760 if (!of_match_table || !of_compatible)
763 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
764 nval = of_compatible->package.count;
765 obj = of_compatible->package.elements;
766 } else { /* Must be ACPI_TYPE_STRING. */
770 /* Now we can look for the driver DT compatible strings */
771 for (i = 0; i < nval; i++, obj++) {
772 const struct of_device_id *id;
774 for (id = of_match_table; id->compatible[0]; id++)
775 if (!strcasecmp(obj->string.pointer, id->compatible)) {
785 static bool acpi_of_modalias(struct acpi_device *adev,
786 char *modalias, size_t len)
788 const union acpi_object *of_compatible;
789 const union acpi_object *obj;
790 const char *str, *chr;
792 of_compatible = adev->data.of_compatible;
796 if (of_compatible->type == ACPI_TYPE_PACKAGE)
797 obj = of_compatible->package.elements;
798 else /* Must be ACPI_TYPE_STRING. */
801 str = obj->string.pointer;
802 chr = strchr(str, ',');
803 strlcpy(modalias, chr ? chr + 1 : str, len);
809 * acpi_set_modalias - Set modalias using "compatible" property or supplied ID
810 * @adev: ACPI device object to match
811 * @default_id: ID string to use as default if no compatible string found
812 * @modalias: Pointer to buffer that modalias value will be copied into
813 * @len: Length of modalias buffer
815 * This is a counterpart of of_modalias_node() for struct acpi_device objects.
816 * If there is a compatible string for @adev, it will be copied to @modalias
817 * with the vendor prefix stripped; otherwise, @default_id will be used.
819 void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
820 char *modalias, size_t len)
822 if (!acpi_of_modalias(adev, modalias, len))
823 strlcpy(modalias, default_id, len);
825 EXPORT_SYMBOL_GPL(acpi_set_modalias);
827 static bool __acpi_match_device_cls(const struct acpi_device_id *id,
828 struct acpi_hardware_id *hwid)
830 int i, msk, byte_shift;
836 /* Apply class-code bitmask, before checking each class-code byte */
837 for (i = 1; i <= 3; i++) {
838 byte_shift = 8 * (3 - i);
839 msk = (id->cls_msk >> byte_shift) & 0xFF;
843 sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
844 if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
850 static bool __acpi_match_device(struct acpi_device *device,
851 const struct acpi_device_id *acpi_ids,
852 const struct of_device_id *of_ids,
853 const struct acpi_device_id **acpi_id,
854 const struct of_device_id **of_id)
856 const struct acpi_device_id *id;
857 struct acpi_hardware_id *hwid;
860 * If the device is not present, it is unnecessary to load device
863 if (!device || !device->status.present)
866 list_for_each_entry(hwid, &device->pnp.ids, list) {
867 /* First, check the ACPI/PNP IDs provided by the caller. */
869 for (id = acpi_ids; id->id[0] || id->cls; id++) {
870 if (id->id[0] && !strcmp((char *)id->id, hwid->id))
872 if (id->cls && __acpi_match_device_cls(id, hwid))
878 * Next, check ACPI_DT_NAMESPACE_HID and try to match the
879 * "compatible" property if found.
881 if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id))
882 return acpi_of_match_device(device, of_ids, of_id);
893 * acpi_match_device - Match a struct device against a given list of ACPI IDs
894 * @ids: Array of struct acpi_device_id object to match against.
895 * @dev: The device structure to match.
897 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
898 * object for that handle and use that object to match against a given list of
901 * Return a pointer to the first matching ID on success or %NULL on failure.
903 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
904 const struct device *dev)
906 const struct acpi_device_id *id = NULL;
908 __acpi_match_device(acpi_companion_match(dev), ids, NULL, &id, NULL);
911 EXPORT_SYMBOL_GPL(acpi_match_device);
913 static const void *acpi_of_device_get_match_data(const struct device *dev)
915 struct acpi_device *adev = ACPI_COMPANION(dev);
916 const struct of_device_id *match = NULL;
918 if (!acpi_of_match_device(adev, dev->driver->of_match_table, &match))
924 const void *acpi_device_get_match_data(const struct device *dev)
926 const struct acpi_device_id *match;
928 if (!dev->driver->acpi_match_table)
929 return acpi_of_device_get_match_data(dev);
931 match = acpi_match_device(dev->driver->acpi_match_table, dev);
935 return (const void *)match->driver_data;
937 EXPORT_SYMBOL_GPL(acpi_device_get_match_data);
939 int acpi_match_device_ids(struct acpi_device *device,
940 const struct acpi_device_id *ids)
942 return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT;
944 EXPORT_SYMBOL(acpi_match_device_ids);
946 bool acpi_driver_match_device(struct device *dev,
947 const struct device_driver *drv)
949 if (!drv->acpi_match_table)
950 return acpi_of_match_device(ACPI_COMPANION(dev),
954 return __acpi_match_device(acpi_companion_match(dev),
955 drv->acpi_match_table, drv->of_match_table,
958 EXPORT_SYMBOL_GPL(acpi_driver_match_device);
960 /* --------------------------------------------------------------------------
961 ACPI Driver Management
962 -------------------------------------------------------------------------- */
965 * acpi_bus_register_driver - register a driver with the ACPI bus
966 * @driver: driver being registered
968 * Registers a driver with the ACPI bus. Searches the namespace for all
969 * devices that match the driver's criteria and binds. Returns zero for
970 * success or a negative error status for failure.
972 int acpi_bus_register_driver(struct acpi_driver *driver)
978 driver->drv.name = driver->name;
979 driver->drv.bus = &acpi_bus_type;
980 driver->drv.owner = driver->owner;
982 ret = driver_register(&driver->drv);
986 EXPORT_SYMBOL(acpi_bus_register_driver);
989 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
990 * @driver: driver to unregister
992 * Unregisters a driver with the ACPI bus. Searches the namespace for all
993 * devices that match the driver's criteria and unbinds.
995 void acpi_bus_unregister_driver(struct acpi_driver *driver)
997 driver_unregister(&driver->drv);
1000 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1002 /* --------------------------------------------------------------------------
1004 -------------------------------------------------------------------------- */
1006 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
1008 struct acpi_device *acpi_dev = to_acpi_device(dev);
1009 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1011 return acpi_dev->flags.match_driver
1012 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
1015 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1017 return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
1020 static int acpi_device_probe(struct device *dev)
1022 struct acpi_device *acpi_dev = to_acpi_device(dev);
1023 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1026 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
1029 if (!acpi_drv->ops.add)
1032 ret = acpi_drv->ops.add(acpi_dev);
1036 acpi_dev->driver = acpi_drv;
1038 pr_debug("Driver [%s] successfully bound to device [%s]\n",
1039 acpi_drv->name, acpi_dev->pnp.bus_id);
1041 if (acpi_drv->ops.notify) {
1042 ret = acpi_device_install_notify_handler(acpi_dev);
1044 if (acpi_drv->ops.remove)
1045 acpi_drv->ops.remove(acpi_dev);
1047 acpi_dev->driver = NULL;
1048 acpi_dev->driver_data = NULL;
1053 pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name,
1054 acpi_dev->pnp.bus_id);
1060 static void acpi_device_remove(struct device *dev)
1062 struct acpi_device *acpi_dev = to_acpi_device(dev);
1063 struct acpi_driver *acpi_drv = acpi_dev->driver;
1066 if (acpi_drv->ops.notify)
1067 acpi_device_remove_notify_handler(acpi_dev);
1068 if (acpi_drv->ops.remove)
1069 acpi_drv->ops.remove(acpi_dev);
1071 acpi_dev->driver = NULL;
1072 acpi_dev->driver_data = NULL;
1077 struct bus_type acpi_bus_type = {
1079 .match = acpi_bus_match,
1080 .probe = acpi_device_probe,
1081 .remove = acpi_device_remove,
1082 .uevent = acpi_device_uevent,
1085 int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data)
1087 return bus_for_each_dev(&acpi_bus_type, NULL, data, fn);
1089 EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
1091 struct acpi_dev_walk_context {
1092 int (*fn)(struct acpi_device *, void *);
1096 static int acpi_dev_for_one_check(struct device *dev, void *context)
1098 struct acpi_dev_walk_context *adwc = context;
1100 if (dev->bus != &acpi_bus_type)
1103 return adwc->fn(to_acpi_device(dev), adwc->data);
1106 int acpi_dev_for_each_child(struct acpi_device *adev,
1107 int (*fn)(struct acpi_device *, void *), void *data)
1109 struct acpi_dev_walk_context adwc = {
1114 return device_for_each_child(&adev->dev, &adwc, acpi_dev_for_one_check);
1117 /* --------------------------------------------------------------------------
1118 Initialization/Cleanup
1119 -------------------------------------------------------------------------- */
1121 static int __init acpi_bus_init_irq(void)
1124 char *message = NULL;
1128 * Let the system know what interrupt model we are using by
1129 * evaluating the \_PIC object, if exists.
1132 switch (acpi_irq_model) {
1133 case ACPI_IRQ_MODEL_PIC:
1136 case ACPI_IRQ_MODEL_IOAPIC:
1139 case ACPI_IRQ_MODEL_IOSAPIC:
1140 message = "IOSAPIC";
1142 case ACPI_IRQ_MODEL_GIC:
1145 case ACPI_IRQ_MODEL_PLATFORM:
1146 message = "platform specific model";
1149 pr_info("Unknown interrupt routing model\n");
1153 pr_info("Using %s for interrupt routing\n", message);
1155 status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
1156 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
1157 pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status));
1165 * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
1167 * The ACPI tables are accessible after this, but the handling of events has not
1168 * been initialized and the global lock is not available yet, so AML should not
1169 * be executed at this point.
1171 * Doing this before switching the EFI runtime services to virtual mode allows
1172 * the EfiBootServices memory to be freed slightly earlier on boot.
1174 void __init acpi_early_init(void)
1181 pr_info("Core revision %08x\n", ACPI_CA_VERSION);
1183 /* enable workarounds, unless strict ACPI spec. compliance */
1185 acpi_gbl_enable_interpreter_slack = TRUE;
1187 acpi_permanent_mmap = true;
1191 * If the machine falls into the DMI check table,
1192 * DSDT will be copied to memory.
1193 * Note that calling dmi_check_system() here on other architectures
1194 * would not be OK because only x86 initializes dmi early enough.
1195 * Thankfully only x86 systems need such quirks for now.
1197 dmi_check_system(dsdt_dmi_table);
1200 status = acpi_reallocate_root_table();
1201 if (ACPI_FAILURE(status)) {
1202 pr_err("Unable to reallocate ACPI tables\n");
1206 status = acpi_initialize_subsystem();
1207 if (ACPI_FAILURE(status)) {
1208 pr_err("Unable to initialize the ACPI Interpreter\n");
1214 /* compatible (0) means level (3) */
1215 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
1216 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
1217 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
1219 /* Set PIC-mode SCI trigger type */
1220 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
1221 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
1224 * now that acpi_gbl_FADT is initialized,
1225 * update it with result from INT_SRC_OVR parsing
1227 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
1237 * acpi_subsystem_init - Finalize the early initialization of ACPI.
1239 * Switch over the platform to the ACPI mode (if possible).
1241 * Doing this too early is generally unsafe, but at the same time it needs to be
1242 * done before all things that really depend on ACPI. The right spot appears to
1243 * be before finalizing the EFI initialization.
1245 void __init acpi_subsystem_init(void)
1252 status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
1253 if (ACPI_FAILURE(status)) {
1254 pr_err("Unable to enable ACPI\n");
1258 * If the system is using ACPI then we can be reasonably
1259 * confident that any regulators are managed by the firmware
1260 * so tell the regulator core it has everything it needs to
1263 regulator_has_full_constraints();
1267 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
1269 if (event == ACPI_TABLE_EVENT_LOAD)
1270 acpi_scan_table_notify();
1272 return acpi_sysfs_table_handler(event, table, context);
1275 static int __init acpi_bus_init(void)
1280 acpi_os_initialize1();
1282 status = acpi_load_tables();
1283 if (ACPI_FAILURE(status)) {
1284 pr_err("Unable to load the System Description Tables\n");
1289 * ACPI 2.0 requires the EC driver to be loaded and work before the EC
1290 * device is found in the namespace.
1292 * This is accomplished by looking for the ECDT table and getting the EC
1293 * parameters out of that.
1295 * Do that before calling acpi_initialize_objects() which may trigger EC
1296 * address space accesses.
1298 acpi_ec_ecdt_probe();
1300 status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
1301 if (ACPI_FAILURE(status)) {
1302 pr_err("Unable to start the ACPI Interpreter\n");
1306 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1307 if (ACPI_FAILURE(status)) {
1308 pr_err("Unable to initialize ACPI objects\n");
1312 /* Set capability bits for _OSC under processor scope */
1313 acpi_early_processor_osc();
1316 * _OSC method may exist in module level code,
1317 * so it must be run after ACPI_FULL_INITIALIZATION
1319 acpi_bus_osc_negotiate_platform_control();
1320 acpi_bus_osc_negotiate_usb_control();
1323 * _PDC control method may load dynamic SSDT tables,
1324 * and we need to install the table handler before that.
1326 status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
1330 acpi_early_processor_set_pdc();
1333 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1334 * is necessary to enable it as early as possible.
1336 acpi_ec_dsdt_probe();
1338 pr_info("Interpreter enabled\n");
1340 /* Initialize sleep structures */
1344 * Get the system interrupt model and evaluate \_PIC.
1346 result = acpi_bus_init_irq();
1351 * Register the for all standard device notifications.
1354 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
1355 &acpi_bus_notify, NULL);
1356 if (ACPI_FAILURE(status)) {
1357 pr_err("Unable to register for system notifications\n");
1362 * Create the top ACPI proc directory
1364 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1366 result = bus_register(&acpi_bus_type);
1370 /* Mimic structured exception handling */
1376 struct kobject *acpi_kobj;
1377 EXPORT_SYMBOL_GPL(acpi_kobj);
1379 static int __init acpi_init(void)
1383 if (acpi_disabled) {
1384 pr_info("Interpreter disabled.\n");
1388 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1390 pr_debug("%s: kset create error\n", __func__);
1394 result = acpi_bus_init();
1396 kobject_put(acpi_kobj);
1401 pci_mmcfg_late_init();
1407 acpi_debugfs_init();
1408 acpi_sleep_proc_init();
1409 acpi_wakeup_device_init();
1410 acpi_debugger_init();
1411 acpi_setup_sb_notify_handler();
1417 subsys_initcall(acpi_init);