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_iort.h>
30 #include <linux/pci.h>
31 #include <acpi/apei.h>
32 #include <linux/suspend.h>
33 #include <linux/prmt.h>
37 struct acpi_device *acpi_root;
38 struct proc_dir_entry *acpi_root_dir;
39 EXPORT_SYMBOL(acpi_root_dir);
42 #ifdef CONFIG_ACPI_CUSTOM_DSDT
43 static inline int set_copy_dsdt(const struct dmi_system_id *id)
48 static int set_copy_dsdt(const struct dmi_system_id *id)
50 pr_notice("%s detected - force copy of DSDT to local memory\n", id->ident);
51 acpi_gbl_copy_dsdt_locally = 1;
56 static const struct dmi_system_id dsdt_dmi_table[] __initconst = {
58 * Invoke DSDT corruption work-around on all Toshiba Satellite.
59 * https://bugzilla.kernel.org/show_bug.cgi?id=14679
62 .callback = set_copy_dsdt,
63 .ident = "TOSHIBA Satellite",
65 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
66 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
73 /* --------------------------------------------------------------------------
75 -------------------------------------------------------------------------- */
77 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
78 unsigned long long *sta)
82 status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
83 if (ACPI_SUCCESS(status))
86 if (status == AE_NOT_FOUND) {
87 *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
88 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
93 EXPORT_SYMBOL_GPL(acpi_bus_get_status_handle);
95 int acpi_bus_get_status(struct acpi_device *device)
98 unsigned long long sta;
100 if (acpi_device_always_present(device)) {
101 acpi_set_device_status(device, ACPI_STA_DEFAULT);
105 /* Battery devices must have their deps met before calling _STA */
106 if (acpi_device_is_battery(device) && device->dep_unmet) {
107 acpi_set_device_status(device, 0);
111 status = acpi_bus_get_status_handle(device->handle, &sta);
112 if (ACPI_FAILURE(status))
115 acpi_set_device_status(device, sta);
117 if (device->status.functional && !device->status.present) {
118 pr_debug("Device [%s] status [%08x]: functional but not present\n",
119 device->pnp.bus_id, (u32)sta);
122 pr_debug("Device [%s] status [%08x]\n", device->pnp.bus_id, (u32)sta);
125 EXPORT_SYMBOL(acpi_bus_get_status);
127 void acpi_bus_private_data_handler(acpi_handle handle,
132 EXPORT_SYMBOL(acpi_bus_private_data_handler);
134 int acpi_bus_attach_private_data(acpi_handle handle, void *data)
138 status = acpi_attach_data(handle,
139 acpi_bus_private_data_handler, data);
140 if (ACPI_FAILURE(status)) {
141 acpi_handle_debug(handle, "Error attaching device data\n");
147 EXPORT_SYMBOL_GPL(acpi_bus_attach_private_data);
149 int acpi_bus_get_private_data(acpi_handle handle, void **data)
156 status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
157 if (ACPI_FAILURE(status)) {
158 acpi_handle_debug(handle, "No context for object\n");
164 EXPORT_SYMBOL_GPL(acpi_bus_get_private_data);
166 void acpi_bus_detach_private_data(acpi_handle handle)
168 acpi_detach_data(handle, acpi_bus_private_data_handler);
170 EXPORT_SYMBOL_GPL(acpi_bus_detach_private_data);
172 static void acpi_print_osc_error(acpi_handle handle,
173 struct acpi_osc_context *context, char *error)
177 acpi_handle_debug(handle, "(%s): %s\n", context->uuid_str, error);
179 pr_debug("_OSC request data:");
180 for (i = 0; i < context->cap.length; i += sizeof(u32))
181 pr_debug(" %x", *((u32 *)(context->cap.pointer + i)));
186 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
189 struct acpi_object_list input;
190 union acpi_object in_params[4];
191 union acpi_object *out_obj;
194 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
198 if (guid_parse(context->uuid_str, &guid))
200 context->ret.length = ACPI_ALLOCATE_BUFFER;
201 context->ret.pointer = NULL;
203 /* Setting up input parameters */
205 input.pointer = in_params;
206 in_params[0].type = ACPI_TYPE_BUFFER;
207 in_params[0].buffer.length = 16;
208 in_params[0].buffer.pointer = (u8 *)&guid;
209 in_params[1].type = ACPI_TYPE_INTEGER;
210 in_params[1].integer.value = context->rev;
211 in_params[2].type = ACPI_TYPE_INTEGER;
212 in_params[2].integer.value = context->cap.length/sizeof(u32);
213 in_params[3].type = ACPI_TYPE_BUFFER;
214 in_params[3].buffer.length = context->cap.length;
215 in_params[3].buffer.pointer = context->cap.pointer;
217 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
218 if (ACPI_FAILURE(status))
222 return AE_NULL_OBJECT;
224 out_obj = output.pointer;
225 if (out_obj->type != ACPI_TYPE_BUFFER
226 || out_obj->buffer.length != context->cap.length) {
227 acpi_print_osc_error(handle, context,
228 "_OSC evaluation returned wrong type");
232 /* Need to ignore the bit0 in result code */
233 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
235 if (errors & OSC_REQUEST_ERROR)
236 acpi_print_osc_error(handle, context,
237 "_OSC request failed");
238 if (errors & OSC_INVALID_UUID_ERROR)
239 acpi_print_osc_error(handle, context,
240 "_OSC invalid UUID");
241 if (errors & OSC_INVALID_REVISION_ERROR)
242 acpi_print_osc_error(handle, context,
243 "_OSC invalid revision");
244 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
245 if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD]
255 context->ret.length = out_obj->buffer.length;
256 context->ret.pointer = kmemdup(out_obj->buffer.pointer,
257 context->ret.length, GFP_KERNEL);
258 if (!context->ret.pointer) {
259 status = AE_NO_MEMORY;
265 kfree(output.pointer);
268 EXPORT_SYMBOL(acpi_run_osc);
270 bool osc_sb_apei_support_acked;
273 * ACPI 6.0 Section 8.4.4.2 Idle State Coordination
274 * OSPM supports platform coordinated low power idle(LPI) states
276 bool osc_pc_lpi_support_confirmed;
277 EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed);
280 * ACPI 6.4 Operating System Capabilities for USB.
282 bool osc_sb_native_usb4_support_confirmed;
283 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed);
285 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
286 static void acpi_bus_osc_negotiate_platform_control(void)
288 u32 capbuf[2], *capbuf_ret;
289 struct acpi_osc_context context = {
290 .uuid_str = sb_uuid_str,
293 .cap.pointer = capbuf,
297 capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
298 capbuf[OSC_SUPPORT_DWORD] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
299 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
300 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PAD_SUPPORT;
301 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
302 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PPC_OST_SUPPORT;
304 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT;
305 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PCLPI_SUPPORT;
306 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PRM_SUPPORT;
309 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
312 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
313 if (boot_cpu_has(X86_FEATURE_HWP)) {
314 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_SUPPORT;
315 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPCV2_SUPPORT;
319 if (IS_ENABLED(CONFIG_SCHED_MC_PRIO))
320 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_DIVERSE_HIGH_SUPPORT;
322 if (IS_ENABLED(CONFIG_USB4))
323 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_NATIVE_USB4_SUPPORT;
326 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_APEI_SUPPORT;
327 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
330 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
333 kfree(context.ret.pointer);
335 /* Now run _OSC again with query flag clear */
336 capbuf[OSC_QUERY_DWORD] = 0;
338 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
341 capbuf_ret = context.ret.pointer;
342 osc_sb_apei_support_acked =
343 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT;
344 osc_pc_lpi_support_confirmed =
345 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_PCLPI_SUPPORT;
346 osc_sb_native_usb4_support_confirmed =
347 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_NATIVE_USB4_SUPPORT;
349 kfree(context.ret.pointer);
353 * Native control of USB4 capabilities. If any of the tunneling bits is
354 * set it means OS is in control and we use software based connection
357 u32 osc_sb_native_usb4_control;
358 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control);
360 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits)
362 pr_info("%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg,
363 (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-',
364 (bits & OSC_USB_DP_TUNNELING) ? '+' : '-',
365 (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-',
366 (bits & OSC_USB_XDOMAIN) ? '+' : '-');
369 static u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A";
370 static void acpi_bus_osc_negotiate_usb_control(void)
373 struct acpi_osc_context context = {
374 .uuid_str = sb_usb_uuid_str,
376 .cap.length = sizeof(capbuf),
377 .cap.pointer = capbuf,
383 if (!osc_sb_native_usb4_support_confirmed)
386 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
389 control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING |
390 OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN;
392 capbuf[OSC_QUERY_DWORD] = 0;
393 capbuf[OSC_SUPPORT_DWORD] = 0;
394 capbuf[OSC_CONTROL_DWORD] = control;
396 status = acpi_run_osc(handle, &context);
397 if (ACPI_FAILURE(status))
400 if (context.ret.length != sizeof(capbuf)) {
401 pr_info("USB4 _OSC: returned invalid length buffer\n");
405 osc_sb_native_usb4_control =
406 control & ((u32 *)context.ret.pointer)[OSC_CONTROL_DWORD];
408 acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control);
409 acpi_bus_decode_usb_osc("USB4 _OSC: OS controls",
410 osc_sb_native_usb4_control);
413 kfree(context.ret.pointer);
416 /* --------------------------------------------------------------------------
417 Notification Handling
418 -------------------------------------------------------------------------- */
423 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
425 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
427 struct acpi_device *adev;
428 struct acpi_driver *driver;
429 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
430 bool hotplug_event = false;
433 case ACPI_NOTIFY_BUS_CHECK:
434 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
435 hotplug_event = true;
438 case ACPI_NOTIFY_DEVICE_CHECK:
439 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
440 hotplug_event = true;
443 case ACPI_NOTIFY_DEVICE_WAKE:
444 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
447 case ACPI_NOTIFY_EJECT_REQUEST:
448 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
449 hotplug_event = true;
452 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
453 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
454 /* TBD: Exactly what does 'light' mean? */
457 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
458 acpi_handle_err(handle, "Device cannot be configured due "
459 "to a frequency mismatch\n");
462 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
463 acpi_handle_err(handle, "Device cannot be configured due "
464 "to a bus mode mismatch\n");
467 case ACPI_NOTIFY_POWER_FAULT:
468 acpi_handle_err(handle, "Device has suffered a power fault\n");
472 acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
476 adev = acpi_bus_get_acpi_device(handle);
480 driver = adev->driver;
481 if (driver && driver->ops.notify &&
482 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
483 driver->ops.notify(adev, type);
485 if (!hotplug_event) {
486 acpi_bus_put_acpi_device(adev);
490 if (ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
493 acpi_bus_put_acpi_device(adev);
496 acpi_evaluate_ost(handle, type, ost_code, NULL);
499 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
501 struct acpi_device *device = data;
503 device->driver->ops.notify(device, event);
506 static void acpi_device_notify_fixed(void *data)
508 struct acpi_device *device = data;
510 /* Fixed hardware devices have no handles */
511 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
514 static u32 acpi_device_fixed_event(void *data)
516 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
517 return ACPI_INTERRUPT_HANDLED;
520 static int acpi_device_install_notify_handler(struct acpi_device *device)
524 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
526 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
527 acpi_device_fixed_event,
529 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
531 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
532 acpi_device_fixed_event,
535 status = acpi_install_notify_handler(device->handle,
540 if (ACPI_FAILURE(status))
545 static void acpi_device_remove_notify_handler(struct acpi_device *device)
547 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
548 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
549 acpi_device_fixed_event);
550 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
551 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
552 acpi_device_fixed_event);
554 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
558 /* Handle events targeting \_SB device (at present only graceful shutdown) */
560 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
561 #define ACPI_SB_INDICATE_INTERVAL 10000
563 static void sb_notify_work(struct work_struct *dummy)
565 acpi_handle sb_handle;
567 orderly_poweroff(true);
570 * After initiating graceful shutdown, the ACPI spec requires OSPM
571 * to evaluate _OST method once every 10seconds to indicate that
572 * the shutdown is in progress
574 acpi_get_handle(NULL, "\\_SB", &sb_handle);
576 pr_info("Graceful shutdown in progress.\n");
577 acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
578 ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
579 msleep(ACPI_SB_INDICATE_INTERVAL);
583 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
585 static DECLARE_WORK(acpi_sb_work, sb_notify_work);
587 if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
588 if (!work_busy(&acpi_sb_work))
589 schedule_work(&acpi_sb_work);
591 pr_warn("event %x is not supported by \\_SB device\n", event);
594 static int __init acpi_setup_sb_notify_handler(void)
596 acpi_handle sb_handle;
598 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
601 if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
602 acpi_sb_notify, NULL)))
608 /* --------------------------------------------------------------------------
610 -------------------------------------------------------------------------- */
613 * acpi_get_first_physical_node - Get first physical node of an ACPI device
614 * @adev: ACPI device in question
616 * Return: First physical node of ACPI device @adev
618 struct device *acpi_get_first_physical_node(struct acpi_device *adev)
620 struct mutex *physical_node_lock = &adev->physical_node_lock;
621 struct device *phys_dev;
623 mutex_lock(physical_node_lock);
624 if (list_empty(&adev->physical_node_list)) {
627 const struct acpi_device_physical_node *node;
629 node = list_first_entry(&adev->physical_node_list,
630 struct acpi_device_physical_node, node);
632 phys_dev = node->dev;
634 mutex_unlock(physical_node_lock);
637 EXPORT_SYMBOL_GPL(acpi_get_first_physical_node);
639 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
640 const struct device *dev)
642 const struct device *phys_dev = acpi_get_first_physical_node(adev);
644 return phys_dev && phys_dev == dev ? adev : NULL;
648 * acpi_device_is_first_physical_node - Is given dev first physical node
649 * @adev: ACPI companion device
650 * @dev: Physical device to check
652 * Function checks if given @dev is the first physical devices attached to
653 * the ACPI companion device. This distinction is needed in some cases
654 * where the same companion device is shared between many physical devices.
656 * Note that the caller have to provide valid @adev pointer.
658 bool acpi_device_is_first_physical_node(struct acpi_device *adev,
659 const struct device *dev)
661 return !!acpi_primary_dev_companion(adev, dev);
665 * acpi_companion_match() - Can we match via ACPI companion device
666 * @dev: Device in question
668 * Check if the given device has an ACPI companion and if that companion has
669 * a valid list of PNP IDs, and if the device is the first (primary) physical
670 * device associated with it. Return the companion pointer if that's the case
673 * If multiple physical devices are attached to a single ACPI companion, we need
674 * to be careful. The usage scenario for this kind of relationship is that all
675 * of the physical devices in question use resources provided by the ACPI
676 * companion. A typical case is an MFD device where all the sub-devices share
677 * the parent's ACPI companion. In such cases we can only allow the primary
678 * (first) physical device to be matched with the help of the companion's PNP
681 * Additional physical devices sharing the ACPI companion can still use
682 * resources available from it but they will be matched normally using functions
683 * provided by their bus types (and analogously for their modalias).
685 struct acpi_device *acpi_companion_match(const struct device *dev)
687 struct acpi_device *adev;
689 adev = ACPI_COMPANION(dev);
693 if (list_empty(&adev->pnp.ids))
696 return acpi_primary_dev_companion(adev, dev);
700 * acpi_of_match_device - Match device object using the "compatible" property.
701 * @adev: ACPI device object to match.
702 * @of_match_table: List of device IDs to match against.
703 * @of_id: OF ID if matched
705 * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
706 * identifiers and a _DSD object with the "compatible" property, use that
707 * property to match against the given list of identifiers.
709 static bool acpi_of_match_device(struct acpi_device *adev,
710 const struct of_device_id *of_match_table,
711 const struct of_device_id **of_id)
713 const union acpi_object *of_compatible, *obj;
719 of_compatible = adev->data.of_compatible;
720 if (!of_match_table || !of_compatible)
723 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
724 nval = of_compatible->package.count;
725 obj = of_compatible->package.elements;
726 } else { /* Must be ACPI_TYPE_STRING. */
730 /* Now we can look for the driver DT compatible strings */
731 for (i = 0; i < nval; i++, obj++) {
732 const struct of_device_id *id;
734 for (id = of_match_table; id->compatible[0]; id++)
735 if (!strcasecmp(obj->string.pointer, id->compatible)) {
745 static bool acpi_of_modalias(struct acpi_device *adev,
746 char *modalias, size_t len)
748 const union acpi_object *of_compatible;
749 const union acpi_object *obj;
750 const char *str, *chr;
752 of_compatible = adev->data.of_compatible;
756 if (of_compatible->type == ACPI_TYPE_PACKAGE)
757 obj = of_compatible->package.elements;
758 else /* Must be ACPI_TYPE_STRING. */
761 str = obj->string.pointer;
762 chr = strchr(str, ',');
763 strlcpy(modalias, chr ? chr + 1 : str, len);
769 * acpi_set_modalias - Set modalias using "compatible" property or supplied ID
770 * @adev: ACPI device object to match
771 * @default_id: ID string to use as default if no compatible string found
772 * @modalias: Pointer to buffer that modalias value will be copied into
773 * @len: Length of modalias buffer
775 * This is a counterpart of of_modalias_node() for struct acpi_device objects.
776 * If there is a compatible string for @adev, it will be copied to @modalias
777 * with the vendor prefix stripped; otherwise, @default_id will be used.
779 void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
780 char *modalias, size_t len)
782 if (!acpi_of_modalias(adev, modalias, len))
783 strlcpy(modalias, default_id, len);
785 EXPORT_SYMBOL_GPL(acpi_set_modalias);
787 static bool __acpi_match_device_cls(const struct acpi_device_id *id,
788 struct acpi_hardware_id *hwid)
790 int i, msk, byte_shift;
796 /* Apply class-code bitmask, before checking each class-code byte */
797 for (i = 1; i <= 3; i++) {
798 byte_shift = 8 * (3 - i);
799 msk = (id->cls_msk >> byte_shift) & 0xFF;
803 sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
804 if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
810 static bool __acpi_match_device(struct acpi_device *device,
811 const struct acpi_device_id *acpi_ids,
812 const struct of_device_id *of_ids,
813 const struct acpi_device_id **acpi_id,
814 const struct of_device_id **of_id)
816 const struct acpi_device_id *id;
817 struct acpi_hardware_id *hwid;
820 * If the device is not present, it is unnecessary to load device
823 if (!device || !device->status.present)
826 list_for_each_entry(hwid, &device->pnp.ids, list) {
827 /* First, check the ACPI/PNP IDs provided by the caller. */
829 for (id = acpi_ids; id->id[0] || id->cls; id++) {
830 if (id->id[0] && !strcmp((char *)id->id, hwid->id))
832 if (id->cls && __acpi_match_device_cls(id, hwid))
838 * Next, check ACPI_DT_NAMESPACE_HID and try to match the
839 * "compatible" property if found.
841 if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id))
842 return acpi_of_match_device(device, of_ids, of_id);
853 * acpi_match_device - Match a struct device against a given list of ACPI IDs
854 * @ids: Array of struct acpi_device_id object to match against.
855 * @dev: The device structure to match.
857 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
858 * object for that handle and use that object to match against a given list of
861 * Return a pointer to the first matching ID on success or %NULL on failure.
863 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
864 const struct device *dev)
866 const struct acpi_device_id *id = NULL;
868 __acpi_match_device(acpi_companion_match(dev), ids, NULL, &id, NULL);
871 EXPORT_SYMBOL_GPL(acpi_match_device);
873 static const void *acpi_of_device_get_match_data(const struct device *dev)
875 struct acpi_device *adev = ACPI_COMPANION(dev);
876 const struct of_device_id *match = NULL;
878 if (!acpi_of_match_device(adev, dev->driver->of_match_table, &match))
884 const void *acpi_device_get_match_data(const struct device *dev)
886 const struct acpi_device_id *match;
888 if (!dev->driver->acpi_match_table)
889 return acpi_of_device_get_match_data(dev);
891 match = acpi_match_device(dev->driver->acpi_match_table, dev);
895 return (const void *)match->driver_data;
897 EXPORT_SYMBOL_GPL(acpi_device_get_match_data);
899 int acpi_match_device_ids(struct acpi_device *device,
900 const struct acpi_device_id *ids)
902 return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT;
904 EXPORT_SYMBOL(acpi_match_device_ids);
906 bool acpi_driver_match_device(struct device *dev,
907 const struct device_driver *drv)
909 if (!drv->acpi_match_table)
910 return acpi_of_match_device(ACPI_COMPANION(dev),
914 return __acpi_match_device(acpi_companion_match(dev),
915 drv->acpi_match_table, drv->of_match_table,
918 EXPORT_SYMBOL_GPL(acpi_driver_match_device);
920 /* --------------------------------------------------------------------------
921 ACPI Driver Management
922 -------------------------------------------------------------------------- */
925 * acpi_bus_register_driver - register a driver with the ACPI bus
926 * @driver: driver being registered
928 * Registers a driver with the ACPI bus. Searches the namespace for all
929 * devices that match the driver's criteria and binds. Returns zero for
930 * success or a negative error status for failure.
932 int acpi_bus_register_driver(struct acpi_driver *driver)
938 driver->drv.name = driver->name;
939 driver->drv.bus = &acpi_bus_type;
940 driver->drv.owner = driver->owner;
942 ret = driver_register(&driver->drv);
946 EXPORT_SYMBOL(acpi_bus_register_driver);
949 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
950 * @driver: driver to unregister
952 * Unregisters a driver with the ACPI bus. Searches the namespace for all
953 * devices that match the driver's criteria and unbinds.
955 void acpi_bus_unregister_driver(struct acpi_driver *driver)
957 driver_unregister(&driver->drv);
960 EXPORT_SYMBOL(acpi_bus_unregister_driver);
962 /* --------------------------------------------------------------------------
964 -------------------------------------------------------------------------- */
966 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
968 struct acpi_device *acpi_dev = to_acpi_device(dev);
969 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
971 return acpi_dev->flags.match_driver
972 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
975 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
977 return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
980 static int acpi_device_probe(struct device *dev)
982 struct acpi_device *acpi_dev = to_acpi_device(dev);
983 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
986 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
989 if (!acpi_drv->ops.add)
992 ret = acpi_drv->ops.add(acpi_dev);
996 acpi_dev->driver = acpi_drv;
998 pr_debug("Driver [%s] successfully bound to device [%s]\n",
999 acpi_drv->name, acpi_dev->pnp.bus_id);
1001 if (acpi_drv->ops.notify) {
1002 ret = acpi_device_install_notify_handler(acpi_dev);
1004 if (acpi_drv->ops.remove)
1005 acpi_drv->ops.remove(acpi_dev);
1007 acpi_dev->driver = NULL;
1008 acpi_dev->driver_data = NULL;
1013 pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name,
1014 acpi_dev->pnp.bus_id);
1020 static int acpi_device_remove(struct device *dev)
1022 struct acpi_device *acpi_dev = to_acpi_device(dev);
1023 struct acpi_driver *acpi_drv = acpi_dev->driver;
1026 if (acpi_drv->ops.notify)
1027 acpi_device_remove_notify_handler(acpi_dev);
1028 if (acpi_drv->ops.remove)
1029 acpi_drv->ops.remove(acpi_dev);
1031 acpi_dev->driver = NULL;
1032 acpi_dev->driver_data = NULL;
1038 struct bus_type acpi_bus_type = {
1040 .match = acpi_bus_match,
1041 .probe = acpi_device_probe,
1042 .remove = acpi_device_remove,
1043 .uevent = acpi_device_uevent,
1046 /* --------------------------------------------------------------------------
1047 Initialization/Cleanup
1048 -------------------------------------------------------------------------- */
1050 static int __init acpi_bus_init_irq(void)
1053 char *message = NULL;
1057 * Let the system know what interrupt model we are using by
1058 * evaluating the \_PIC object, if exists.
1061 switch (acpi_irq_model) {
1062 case ACPI_IRQ_MODEL_PIC:
1065 case ACPI_IRQ_MODEL_IOAPIC:
1068 case ACPI_IRQ_MODEL_IOSAPIC:
1069 message = "IOSAPIC";
1071 case ACPI_IRQ_MODEL_GIC:
1074 case ACPI_IRQ_MODEL_PLATFORM:
1075 message = "platform specific model";
1078 pr_info("Unknown interrupt routing model\n");
1082 pr_info("Using %s for interrupt routing\n", message);
1084 status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
1085 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
1086 pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status));
1094 * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
1096 * The ACPI tables are accessible after this, but the handling of events has not
1097 * been initialized and the global lock is not available yet, so AML should not
1098 * be executed at this point.
1100 * Doing this before switching the EFI runtime services to virtual mode allows
1101 * the EfiBootServices memory to be freed slightly earlier on boot.
1103 void __init acpi_early_init(void)
1110 pr_info("Core revision %08x\n", ACPI_CA_VERSION);
1112 /* enable workarounds, unless strict ACPI spec. compliance */
1114 acpi_gbl_enable_interpreter_slack = TRUE;
1116 acpi_permanent_mmap = true;
1120 * If the machine falls into the DMI check table,
1121 * DSDT will be copied to memory.
1122 * Note that calling dmi_check_system() here on other architectures
1123 * would not be OK because only x86 initializes dmi early enough.
1124 * Thankfully only x86 systems need such quirks for now.
1126 dmi_check_system(dsdt_dmi_table);
1129 status = acpi_reallocate_root_table();
1130 if (ACPI_FAILURE(status)) {
1131 pr_err("Unable to reallocate ACPI tables\n");
1135 status = acpi_initialize_subsystem();
1136 if (ACPI_FAILURE(status)) {
1137 pr_err("Unable to initialize the ACPI Interpreter\n");
1143 /* compatible (0) means level (3) */
1144 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
1145 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
1146 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
1148 /* Set PIC-mode SCI trigger type */
1149 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
1150 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
1153 * now that acpi_gbl_FADT is initialized,
1154 * update it with result from INT_SRC_OVR parsing
1156 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
1166 * acpi_subsystem_init - Finalize the early initialization of ACPI.
1168 * Switch over the platform to the ACPI mode (if possible).
1170 * Doing this too early is generally unsafe, but at the same time it needs to be
1171 * done before all things that really depend on ACPI. The right spot appears to
1172 * be before finalizing the EFI initialization.
1174 void __init acpi_subsystem_init(void)
1181 status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
1182 if (ACPI_FAILURE(status)) {
1183 pr_err("Unable to enable ACPI\n");
1187 * If the system is using ACPI then we can be reasonably
1188 * confident that any regulators are managed by the firmware
1189 * so tell the regulator core it has everything it needs to
1192 regulator_has_full_constraints();
1196 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
1198 if (event == ACPI_TABLE_EVENT_LOAD)
1199 acpi_scan_table_notify();
1201 return acpi_sysfs_table_handler(event, table, context);
1204 static int __init acpi_bus_init(void)
1209 acpi_os_initialize1();
1211 status = acpi_load_tables();
1212 if (ACPI_FAILURE(status)) {
1213 pr_err("Unable to load the System Description Tables\n");
1218 * ACPI 2.0 requires the EC driver to be loaded and work before the EC
1219 * device is found in the namespace.
1221 * This is accomplished by looking for the ECDT table and getting the EC
1222 * parameters out of that.
1224 * Do that before calling acpi_initialize_objects() which may trigger EC
1225 * address space accesses.
1227 acpi_ec_ecdt_probe();
1229 status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
1230 if (ACPI_FAILURE(status)) {
1231 pr_err("Unable to start the ACPI Interpreter\n");
1235 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1236 if (ACPI_FAILURE(status)) {
1237 pr_err("Unable to initialize ACPI objects\n");
1241 /* Set capability bits for _OSC under processor scope */
1242 acpi_early_processor_osc();
1245 * _OSC method may exist in module level code,
1246 * so it must be run after ACPI_FULL_INITIALIZATION
1248 acpi_bus_osc_negotiate_platform_control();
1249 acpi_bus_osc_negotiate_usb_control();
1252 * _PDC control method may load dynamic SSDT tables,
1253 * and we need to install the table handler before that.
1255 status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
1259 acpi_early_processor_set_pdc();
1262 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1263 * is necessary to enable it as early as possible.
1265 acpi_ec_dsdt_probe();
1267 pr_info("Interpreter enabled\n");
1269 /* Initialize sleep structures */
1273 * Get the system interrupt model and evaluate \_PIC.
1275 result = acpi_bus_init_irq();
1280 * Register the for all standard device notifications.
1283 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
1284 &acpi_bus_notify, NULL);
1285 if (ACPI_FAILURE(status)) {
1286 pr_err("Unable to register for system notifications\n");
1291 * Create the top ACPI proc directory
1293 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1295 result = bus_register(&acpi_bus_type);
1299 /* Mimic structured exception handling */
1305 struct kobject *acpi_kobj;
1306 EXPORT_SYMBOL_GPL(acpi_kobj);
1308 static int __init acpi_init(void)
1312 if (acpi_disabled) {
1313 pr_info("Interpreter disabled.\n");
1317 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1319 pr_debug("%s: kset create error\n", __func__);
1322 result = acpi_bus_init();
1324 kobject_put(acpi_kobj);
1329 pci_mmcfg_late_init();
1333 acpi_debugfs_init();
1334 acpi_sleep_proc_init();
1335 acpi_wakeup_device_init();
1336 acpi_debugger_init();
1337 acpi_setup_sb_notify_handler();
1341 subsys_initcall(acpi_init);