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_iort.h>
30 #include <linux/pci.h>
31 #include <acpi/apei.h>
32 #include <linux/suspend.h>
36 struct acpi_device *acpi_root;
37 struct proc_dir_entry *acpi_root_dir;
38 EXPORT_SYMBOL(acpi_root_dir);
41 #ifdef CONFIG_ACPI_CUSTOM_DSDT
42 static inline int set_copy_dsdt(const struct dmi_system_id *id)
47 static int set_copy_dsdt(const struct dmi_system_id *id)
49 pr_notice("%s detected - force copy of DSDT to local memory\n", id->ident);
50 acpi_gbl_copy_dsdt_locally = 1;
55 static const struct dmi_system_id dsdt_dmi_table[] __initconst = {
57 * Invoke DSDT corruption work-around on all Toshiba Satellite.
58 * https://bugzilla.kernel.org/show_bug.cgi?id=14679
61 .callback = set_copy_dsdt,
62 .ident = "TOSHIBA Satellite",
64 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
65 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
72 /* --------------------------------------------------------------------------
74 -------------------------------------------------------------------------- */
76 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
77 unsigned long long *sta)
81 status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
82 if (ACPI_SUCCESS(status))
85 if (status == AE_NOT_FOUND) {
86 *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
87 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
92 EXPORT_SYMBOL_GPL(acpi_bus_get_status_handle);
94 int acpi_bus_get_status(struct acpi_device *device)
97 unsigned long long sta;
99 if (acpi_device_always_present(device)) {
100 acpi_set_device_status(device, ACPI_STA_DEFAULT);
104 /* Battery devices must have their deps met before calling _STA */
105 if (acpi_device_is_battery(device) && device->dep_unmet) {
106 acpi_set_device_status(device, 0);
110 status = acpi_bus_get_status_handle(device->handle, &sta);
111 if (ACPI_FAILURE(status))
114 acpi_set_device_status(device, sta);
116 if (device->status.functional && !device->status.present) {
117 pr_debug("Device [%s] status [%08x]: functional but not present\n",
118 device->pnp.bus_id, (u32)sta);
121 pr_debug("Device [%s] status [%08x]\n", device->pnp.bus_id, (u32)sta);
124 EXPORT_SYMBOL(acpi_bus_get_status);
126 void acpi_bus_private_data_handler(acpi_handle handle,
131 EXPORT_SYMBOL(acpi_bus_private_data_handler);
133 int acpi_bus_attach_private_data(acpi_handle handle, void *data)
137 status = acpi_attach_data(handle,
138 acpi_bus_private_data_handler, data);
139 if (ACPI_FAILURE(status)) {
140 acpi_handle_debug(handle, "Error attaching device data\n");
146 EXPORT_SYMBOL_GPL(acpi_bus_attach_private_data);
148 int acpi_bus_get_private_data(acpi_handle handle, void **data)
155 status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
156 if (ACPI_FAILURE(status)) {
157 acpi_handle_debug(handle, "No context for object\n");
163 EXPORT_SYMBOL_GPL(acpi_bus_get_private_data);
165 void acpi_bus_detach_private_data(acpi_handle handle)
167 acpi_detach_data(handle, acpi_bus_private_data_handler);
169 EXPORT_SYMBOL_GPL(acpi_bus_detach_private_data);
171 static void acpi_print_osc_error(acpi_handle handle,
172 struct acpi_osc_context *context, char *error)
176 acpi_handle_debug(handle, "(%s): %s\n", context->uuid_str, error);
178 pr_debug("_OSC request data:");
179 for (i = 0; i < context->cap.length; i += sizeof(u32))
180 pr_debug(" %x", *((u32 *)(context->cap.pointer + i)));
185 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
188 struct acpi_object_list input;
189 union acpi_object in_params[4];
190 union acpi_object *out_obj;
193 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
197 if (guid_parse(context->uuid_str, &guid))
199 context->ret.length = ACPI_ALLOCATE_BUFFER;
200 context->ret.pointer = NULL;
202 /* Setting up input parameters */
204 input.pointer = in_params;
205 in_params[0].type = ACPI_TYPE_BUFFER;
206 in_params[0].buffer.length = 16;
207 in_params[0].buffer.pointer = (u8 *)&guid;
208 in_params[1].type = ACPI_TYPE_INTEGER;
209 in_params[1].integer.value = context->rev;
210 in_params[2].type = ACPI_TYPE_INTEGER;
211 in_params[2].integer.value = context->cap.length/sizeof(u32);
212 in_params[3].type = ACPI_TYPE_BUFFER;
213 in_params[3].buffer.length = context->cap.length;
214 in_params[3].buffer.pointer = context->cap.pointer;
216 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
217 if (ACPI_FAILURE(status))
221 return AE_NULL_OBJECT;
223 out_obj = output.pointer;
224 if (out_obj->type != ACPI_TYPE_BUFFER
225 || out_obj->buffer.length != context->cap.length) {
226 acpi_print_osc_error(handle, context,
227 "_OSC evaluation returned wrong type");
231 /* Need to ignore the bit0 in result code */
232 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
234 if (errors & OSC_REQUEST_ERROR)
235 acpi_print_osc_error(handle, context,
236 "_OSC request failed");
237 if (errors & OSC_INVALID_UUID_ERROR)
238 acpi_print_osc_error(handle, context,
239 "_OSC invalid UUID");
240 if (errors & OSC_INVALID_REVISION_ERROR)
241 acpi_print_osc_error(handle, context,
242 "_OSC invalid revision");
243 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
244 if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD]
254 context->ret.length = out_obj->buffer.length;
255 context->ret.pointer = kmemdup(out_obj->buffer.pointer,
256 context->ret.length, GFP_KERNEL);
257 if (!context->ret.pointer) {
258 status = AE_NO_MEMORY;
264 kfree(output.pointer);
266 context->ret.pointer = NULL;
269 EXPORT_SYMBOL(acpi_run_osc);
271 bool osc_sb_apei_support_acked;
274 * ACPI 6.0 Section 8.4.4.2 Idle State Coordination
275 * OSPM supports platform coordinated low power idle(LPI) states
277 bool osc_pc_lpi_support_confirmed;
278 EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed);
281 * ACPI 6.4 Operating System Capabilities for USB.
283 bool osc_sb_native_usb4_support_confirmed;
284 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed);
286 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
287 static void acpi_bus_osc_negotiate_platform_control(void)
289 u32 capbuf[2], *capbuf_ret;
290 struct acpi_osc_context context = {
291 .uuid_str = sb_uuid_str,
294 .cap.pointer = capbuf,
298 capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
299 capbuf[OSC_SUPPORT_DWORD] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
300 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
301 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PAD_SUPPORT;
302 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
303 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PPC_OST_SUPPORT;
305 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT;
306 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PCLPI_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 capbuf_ret = context.ret.pointer;
334 if (context.ret.length <= OSC_SUPPORT_DWORD) {
335 kfree(context.ret.pointer);
340 * Now run _OSC again with query flag clear and with the caps
341 * supported by both the OS and the platform.
343 capbuf[OSC_QUERY_DWORD] = 0;
344 capbuf[OSC_SUPPORT_DWORD] = capbuf_ret[OSC_SUPPORT_DWORD];
345 kfree(context.ret.pointer);
347 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
350 capbuf_ret = context.ret.pointer;
351 if (context.ret.length > OSC_SUPPORT_DWORD) {
352 osc_sb_apei_support_acked =
353 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT;
354 osc_pc_lpi_support_confirmed =
355 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_PCLPI_SUPPORT;
356 osc_sb_native_usb4_support_confirmed =
357 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_NATIVE_USB4_SUPPORT;
360 kfree(context.ret.pointer);
364 * Native control of USB4 capabilities. If any of the tunneling bits is
365 * set it means OS is in control and we use software based connection
368 u32 osc_sb_native_usb4_control;
369 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control);
371 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits)
373 printk(KERN_INFO PREFIX "%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg,
374 (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-',
375 (bits & OSC_USB_DP_TUNNELING) ? '+' : '-',
376 (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-',
377 (bits & OSC_USB_XDOMAIN) ? '+' : '-');
380 static u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A";
381 static void acpi_bus_osc_negotiate_usb_control(void)
384 struct acpi_osc_context context = {
385 .uuid_str = sb_usb_uuid_str,
387 .cap.length = sizeof(capbuf),
388 .cap.pointer = capbuf,
394 if (!osc_sb_native_usb4_support_confirmed)
397 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
400 control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING |
401 OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN;
403 capbuf[OSC_QUERY_DWORD] = 0;
404 capbuf[OSC_SUPPORT_DWORD] = 0;
405 capbuf[OSC_CONTROL_DWORD] = control;
407 status = acpi_run_osc(handle, &context);
408 if (ACPI_FAILURE(status))
411 if (context.ret.length != sizeof(capbuf)) {
412 printk(KERN_INFO PREFIX "USB4 _OSC: returned invalid length buffer\n");
416 osc_sb_native_usb4_control =
417 control & ((u32 *)context.ret.pointer)[OSC_CONTROL_DWORD];
419 acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control);
420 acpi_bus_decode_usb_osc("USB4 _OSC: OS controls",
421 osc_sb_native_usb4_control);
424 kfree(context.ret.pointer);
427 /* --------------------------------------------------------------------------
428 Notification Handling
429 -------------------------------------------------------------------------- */
434 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
436 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
438 struct acpi_device *adev;
439 struct acpi_driver *driver;
440 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
441 bool hotplug_event = false;
444 case ACPI_NOTIFY_BUS_CHECK:
445 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
446 hotplug_event = true;
449 case ACPI_NOTIFY_DEVICE_CHECK:
450 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
451 hotplug_event = true;
454 case ACPI_NOTIFY_DEVICE_WAKE:
455 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
458 case ACPI_NOTIFY_EJECT_REQUEST:
459 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
460 hotplug_event = true;
463 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
464 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
465 /* TBD: Exactly what does 'light' mean? */
468 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
469 acpi_handle_err(handle, "Device cannot be configured due "
470 "to a frequency mismatch\n");
473 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
474 acpi_handle_err(handle, "Device cannot be configured due "
475 "to a bus mode mismatch\n");
478 case ACPI_NOTIFY_POWER_FAULT:
479 acpi_handle_err(handle, "Device has suffered a power fault\n");
483 acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
487 adev = acpi_bus_get_acpi_device(handle);
491 driver = adev->driver;
492 if (driver && driver->ops.notify &&
493 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
494 driver->ops.notify(adev, type);
496 if (!hotplug_event) {
497 acpi_bus_put_acpi_device(adev);
501 if (ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
504 acpi_bus_put_acpi_device(adev);
507 acpi_evaluate_ost(handle, type, ost_code, NULL);
510 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
512 struct acpi_device *device = data;
514 device->driver->ops.notify(device, event);
517 static void acpi_device_notify_fixed(void *data)
519 struct acpi_device *device = data;
521 /* Fixed hardware devices have no handles */
522 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
525 static u32 acpi_device_fixed_event(void *data)
527 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
528 return ACPI_INTERRUPT_HANDLED;
531 static int acpi_device_install_notify_handler(struct acpi_device *device)
535 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
537 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
538 acpi_device_fixed_event,
540 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
542 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
543 acpi_device_fixed_event,
546 status = acpi_install_notify_handler(device->handle,
551 if (ACPI_FAILURE(status))
556 static void acpi_device_remove_notify_handler(struct acpi_device *device)
558 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
559 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
560 acpi_device_fixed_event);
561 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
562 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
563 acpi_device_fixed_event);
565 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
569 /* Handle events targeting \_SB device (at present only graceful shutdown) */
571 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
572 #define ACPI_SB_INDICATE_INTERVAL 10000
574 static void sb_notify_work(struct work_struct *dummy)
576 acpi_handle sb_handle;
578 orderly_poweroff(true);
581 * After initiating graceful shutdown, the ACPI spec requires OSPM
582 * to evaluate _OST method once every 10seconds to indicate that
583 * the shutdown is in progress
585 acpi_get_handle(NULL, "\\_SB", &sb_handle);
587 pr_info("Graceful shutdown in progress.\n");
588 acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
589 ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
590 msleep(ACPI_SB_INDICATE_INTERVAL);
594 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
596 static DECLARE_WORK(acpi_sb_work, sb_notify_work);
598 if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
599 if (!work_busy(&acpi_sb_work))
600 schedule_work(&acpi_sb_work);
602 pr_warn("event %x is not supported by \\_SB device\n", event);
605 static int __init acpi_setup_sb_notify_handler(void)
607 acpi_handle sb_handle;
609 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
612 if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
613 acpi_sb_notify, NULL)))
619 /* --------------------------------------------------------------------------
621 -------------------------------------------------------------------------- */
624 * acpi_get_first_physical_node - Get first physical node of an ACPI device
625 * @adev: ACPI device in question
627 * Return: First physical node of ACPI device @adev
629 struct device *acpi_get_first_physical_node(struct acpi_device *adev)
631 struct mutex *physical_node_lock = &adev->physical_node_lock;
632 struct device *phys_dev;
634 mutex_lock(physical_node_lock);
635 if (list_empty(&adev->physical_node_list)) {
638 const struct acpi_device_physical_node *node;
640 node = list_first_entry(&adev->physical_node_list,
641 struct acpi_device_physical_node, node);
643 phys_dev = node->dev;
645 mutex_unlock(physical_node_lock);
648 EXPORT_SYMBOL_GPL(acpi_get_first_physical_node);
650 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
651 const struct device *dev)
653 const struct device *phys_dev = acpi_get_first_physical_node(adev);
655 return phys_dev && phys_dev == dev ? adev : NULL;
659 * acpi_device_is_first_physical_node - Is given dev first physical node
660 * @adev: ACPI companion device
661 * @dev: Physical device to check
663 * Function checks if given @dev is the first physical devices attached to
664 * the ACPI companion device. This distinction is needed in some cases
665 * where the same companion device is shared between many physical devices.
667 * Note that the caller have to provide valid @adev pointer.
669 bool acpi_device_is_first_physical_node(struct acpi_device *adev,
670 const struct device *dev)
672 return !!acpi_primary_dev_companion(adev, dev);
676 * acpi_companion_match() - Can we match via ACPI companion device
677 * @dev: Device in question
679 * Check if the given device has an ACPI companion and if that companion has
680 * a valid list of PNP IDs, and if the device is the first (primary) physical
681 * device associated with it. Return the companion pointer if that's the case
684 * If multiple physical devices are attached to a single ACPI companion, we need
685 * to be careful. The usage scenario for this kind of relationship is that all
686 * of the physical devices in question use resources provided by the ACPI
687 * companion. A typical case is an MFD device where all the sub-devices share
688 * the parent's ACPI companion. In such cases we can only allow the primary
689 * (first) physical device to be matched with the help of the companion's PNP
692 * Additional physical devices sharing the ACPI companion can still use
693 * resources available from it but they will be matched normally using functions
694 * provided by their bus types (and analogously for their modalias).
696 struct acpi_device *acpi_companion_match(const struct device *dev)
698 struct acpi_device *adev;
700 adev = ACPI_COMPANION(dev);
704 if (list_empty(&adev->pnp.ids))
707 return acpi_primary_dev_companion(adev, dev);
711 * acpi_of_match_device - Match device object using the "compatible" property.
712 * @adev: ACPI device object to match.
713 * @of_match_table: List of device IDs to match against.
714 * @of_id: OF ID if matched
716 * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
717 * identifiers and a _DSD object with the "compatible" property, use that
718 * property to match against the given list of identifiers.
720 static bool acpi_of_match_device(struct acpi_device *adev,
721 const struct of_device_id *of_match_table,
722 const struct of_device_id **of_id)
724 const union acpi_object *of_compatible, *obj;
730 of_compatible = adev->data.of_compatible;
731 if (!of_match_table || !of_compatible)
734 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
735 nval = of_compatible->package.count;
736 obj = of_compatible->package.elements;
737 } else { /* Must be ACPI_TYPE_STRING. */
741 /* Now we can look for the driver DT compatible strings */
742 for (i = 0; i < nval; i++, obj++) {
743 const struct of_device_id *id;
745 for (id = of_match_table; id->compatible[0]; id++)
746 if (!strcasecmp(obj->string.pointer, id->compatible)) {
756 static bool acpi_of_modalias(struct acpi_device *adev,
757 char *modalias, size_t len)
759 const union acpi_object *of_compatible;
760 const union acpi_object *obj;
761 const char *str, *chr;
763 of_compatible = adev->data.of_compatible;
767 if (of_compatible->type == ACPI_TYPE_PACKAGE)
768 obj = of_compatible->package.elements;
769 else /* Must be ACPI_TYPE_STRING. */
772 str = obj->string.pointer;
773 chr = strchr(str, ',');
774 strlcpy(modalias, chr ? chr + 1 : str, len);
780 * acpi_set_modalias - Set modalias using "compatible" property or supplied ID
781 * @adev: ACPI device object to match
782 * @default_id: ID string to use as default if no compatible string found
783 * @modalias: Pointer to buffer that modalias value will be copied into
784 * @len: Length of modalias buffer
786 * This is a counterpart of of_modalias_node() for struct acpi_device objects.
787 * If there is a compatible string for @adev, it will be copied to @modalias
788 * with the vendor prefix stripped; otherwise, @default_id will be used.
790 void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
791 char *modalias, size_t len)
793 if (!acpi_of_modalias(adev, modalias, len))
794 strlcpy(modalias, default_id, len);
796 EXPORT_SYMBOL_GPL(acpi_set_modalias);
798 static bool __acpi_match_device_cls(const struct acpi_device_id *id,
799 struct acpi_hardware_id *hwid)
801 int i, msk, byte_shift;
807 /* Apply class-code bitmask, before checking each class-code byte */
808 for (i = 1; i <= 3; i++) {
809 byte_shift = 8 * (3 - i);
810 msk = (id->cls_msk >> byte_shift) & 0xFF;
814 sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
815 if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
821 static bool __acpi_match_device(struct acpi_device *device,
822 const struct acpi_device_id *acpi_ids,
823 const struct of_device_id *of_ids,
824 const struct acpi_device_id **acpi_id,
825 const struct of_device_id **of_id)
827 const struct acpi_device_id *id;
828 struct acpi_hardware_id *hwid;
831 * If the device is not present, it is unnecessary to load device
834 if (!device || !device->status.present)
837 list_for_each_entry(hwid, &device->pnp.ids, list) {
838 /* First, check the ACPI/PNP IDs provided by the caller. */
840 for (id = acpi_ids; id->id[0] || id->cls; id++) {
841 if (id->id[0] && !strcmp((char *)id->id, hwid->id))
843 if (id->cls && __acpi_match_device_cls(id, hwid))
849 * Next, check ACPI_DT_NAMESPACE_HID and try to match the
850 * "compatible" property if found.
852 if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id))
853 return acpi_of_match_device(device, of_ids, of_id);
864 * acpi_match_device - Match a struct device against a given list of ACPI IDs
865 * @ids: Array of struct acpi_device_id object to match against.
866 * @dev: The device structure to match.
868 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
869 * object for that handle and use that object to match against a given list of
872 * Return a pointer to the first matching ID on success or %NULL on failure.
874 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
875 const struct device *dev)
877 const struct acpi_device_id *id = NULL;
879 __acpi_match_device(acpi_companion_match(dev), ids, NULL, &id, NULL);
882 EXPORT_SYMBOL_GPL(acpi_match_device);
884 static const void *acpi_of_device_get_match_data(const struct device *dev)
886 struct acpi_device *adev = ACPI_COMPANION(dev);
887 const struct of_device_id *match = NULL;
889 if (!acpi_of_match_device(adev, dev->driver->of_match_table, &match))
895 const void *acpi_device_get_match_data(const struct device *dev)
897 const struct acpi_device_id *match;
899 if (!dev->driver->acpi_match_table)
900 return acpi_of_device_get_match_data(dev);
902 match = acpi_match_device(dev->driver->acpi_match_table, dev);
906 return (const void *)match->driver_data;
908 EXPORT_SYMBOL_GPL(acpi_device_get_match_data);
910 int acpi_match_device_ids(struct acpi_device *device,
911 const struct acpi_device_id *ids)
913 return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT;
915 EXPORT_SYMBOL(acpi_match_device_ids);
917 bool acpi_driver_match_device(struct device *dev,
918 const struct device_driver *drv)
920 if (!drv->acpi_match_table)
921 return acpi_of_match_device(ACPI_COMPANION(dev),
925 return __acpi_match_device(acpi_companion_match(dev),
926 drv->acpi_match_table, drv->of_match_table,
929 EXPORT_SYMBOL_GPL(acpi_driver_match_device);
931 /* --------------------------------------------------------------------------
932 ACPI Driver Management
933 -------------------------------------------------------------------------- */
936 * acpi_bus_register_driver - register a driver with the ACPI bus
937 * @driver: driver being registered
939 * Registers a driver with the ACPI bus. Searches the namespace for all
940 * devices that match the driver's criteria and binds. Returns zero for
941 * success or a negative error status for failure.
943 int acpi_bus_register_driver(struct acpi_driver *driver)
949 driver->drv.name = driver->name;
950 driver->drv.bus = &acpi_bus_type;
951 driver->drv.owner = driver->owner;
953 ret = driver_register(&driver->drv);
957 EXPORT_SYMBOL(acpi_bus_register_driver);
960 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
961 * @driver: driver to unregister
963 * Unregisters a driver with the ACPI bus. Searches the namespace for all
964 * devices that match the driver's criteria and unbinds.
966 void acpi_bus_unregister_driver(struct acpi_driver *driver)
968 driver_unregister(&driver->drv);
971 EXPORT_SYMBOL(acpi_bus_unregister_driver);
973 /* --------------------------------------------------------------------------
975 -------------------------------------------------------------------------- */
977 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
979 struct acpi_device *acpi_dev = to_acpi_device(dev);
980 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
982 return acpi_dev->flags.match_driver
983 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
986 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
988 return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
991 static int acpi_device_probe(struct device *dev)
993 struct acpi_device *acpi_dev = to_acpi_device(dev);
994 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
997 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
1000 if (!acpi_drv->ops.add)
1003 ret = acpi_drv->ops.add(acpi_dev);
1007 acpi_dev->driver = acpi_drv;
1009 pr_debug("Driver [%s] successfully bound to device [%s]\n",
1010 acpi_drv->name, acpi_dev->pnp.bus_id);
1012 if (acpi_drv->ops.notify) {
1013 ret = acpi_device_install_notify_handler(acpi_dev);
1015 if (acpi_drv->ops.remove)
1016 acpi_drv->ops.remove(acpi_dev);
1018 acpi_dev->driver = NULL;
1019 acpi_dev->driver_data = NULL;
1024 pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name,
1025 acpi_dev->pnp.bus_id);
1031 static int acpi_device_remove(struct device *dev)
1033 struct acpi_device *acpi_dev = to_acpi_device(dev);
1034 struct acpi_driver *acpi_drv = acpi_dev->driver;
1037 if (acpi_drv->ops.notify)
1038 acpi_device_remove_notify_handler(acpi_dev);
1039 if (acpi_drv->ops.remove)
1040 acpi_drv->ops.remove(acpi_dev);
1042 acpi_dev->driver = NULL;
1043 acpi_dev->driver_data = NULL;
1049 struct bus_type acpi_bus_type = {
1051 .match = acpi_bus_match,
1052 .probe = acpi_device_probe,
1053 .remove = acpi_device_remove,
1054 .uevent = acpi_device_uevent,
1057 /* --------------------------------------------------------------------------
1058 Initialization/Cleanup
1059 -------------------------------------------------------------------------- */
1061 static int __init acpi_bus_init_irq(void)
1064 char *message = NULL;
1068 * Let the system know what interrupt model we are using by
1069 * evaluating the \_PIC object, if exists.
1072 switch (acpi_irq_model) {
1073 case ACPI_IRQ_MODEL_PIC:
1076 case ACPI_IRQ_MODEL_IOAPIC:
1079 case ACPI_IRQ_MODEL_IOSAPIC:
1080 message = "IOSAPIC";
1082 case ACPI_IRQ_MODEL_GIC:
1085 case ACPI_IRQ_MODEL_PLATFORM:
1086 message = "platform specific model";
1089 pr_info("Unknown interrupt routing model\n");
1093 pr_info("Using %s for interrupt routing\n", message);
1095 status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
1096 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
1097 pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status));
1105 * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
1107 * The ACPI tables are accessible after this, but the handling of events has not
1108 * been initialized and the global lock is not available yet, so AML should not
1109 * be executed at this point.
1111 * Doing this before switching the EFI runtime services to virtual mode allows
1112 * the EfiBootServices memory to be freed slightly earlier on boot.
1114 void __init acpi_early_init(void)
1121 pr_info("Core revision %08x\n", ACPI_CA_VERSION);
1123 /* enable workarounds, unless strict ACPI spec. compliance */
1125 acpi_gbl_enable_interpreter_slack = TRUE;
1127 acpi_permanent_mmap = true;
1131 * If the machine falls into the DMI check table,
1132 * DSDT will be copied to memory.
1133 * Note that calling dmi_check_system() here on other architectures
1134 * would not be OK because only x86 initializes dmi early enough.
1135 * Thankfully only x86 systems need such quirks for now.
1137 dmi_check_system(dsdt_dmi_table);
1140 status = acpi_reallocate_root_table();
1141 if (ACPI_FAILURE(status)) {
1142 pr_err("Unable to reallocate ACPI tables\n");
1146 status = acpi_initialize_subsystem();
1147 if (ACPI_FAILURE(status)) {
1148 pr_err("Unable to initialize the ACPI Interpreter\n");
1154 /* compatible (0) means level (3) */
1155 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
1156 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
1157 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
1159 /* Set PIC-mode SCI trigger type */
1160 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
1161 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
1164 * now that acpi_gbl_FADT is initialized,
1165 * update it with result from INT_SRC_OVR parsing
1167 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
1177 * acpi_subsystem_init - Finalize the early initialization of ACPI.
1179 * Switch over the platform to the ACPI mode (if possible).
1181 * Doing this too early is generally unsafe, but at the same time it needs to be
1182 * done before all things that really depend on ACPI. The right spot appears to
1183 * be before finalizing the EFI initialization.
1185 void __init acpi_subsystem_init(void)
1192 status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
1193 if (ACPI_FAILURE(status)) {
1194 pr_err("Unable to enable ACPI\n");
1198 * If the system is using ACPI then we can be reasonably
1199 * confident that any regulators are managed by the firmware
1200 * so tell the regulator core it has everything it needs to
1203 regulator_has_full_constraints();
1207 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
1209 acpi_scan_table_handler(event, table, context);
1211 return acpi_sysfs_table_handler(event, table, context);
1214 static int __init acpi_bus_init(void)
1219 acpi_os_initialize1();
1221 status = acpi_load_tables();
1222 if (ACPI_FAILURE(status)) {
1223 pr_err("Unable to load the System Description Tables\n");
1228 * ACPI 2.0 requires the EC driver to be loaded and work before the EC
1229 * device is found in the namespace.
1231 * This is accomplished by looking for the ECDT table and getting the EC
1232 * parameters out of that.
1234 * Do that before calling acpi_initialize_objects() which may trigger EC
1235 * address space accesses.
1237 acpi_ec_ecdt_probe();
1239 status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
1240 if (ACPI_FAILURE(status)) {
1241 pr_err("Unable to start the ACPI Interpreter\n");
1245 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1246 if (ACPI_FAILURE(status)) {
1247 pr_err("Unable to initialize ACPI objects\n");
1251 /* Set capability bits for _OSC under processor scope */
1252 acpi_early_processor_osc();
1255 * _OSC method may exist in module level code,
1256 * so it must be run after ACPI_FULL_INITIALIZATION
1258 acpi_bus_osc_negotiate_platform_control();
1259 acpi_bus_osc_negotiate_usb_control();
1262 * _PDC control method may load dynamic SSDT tables,
1263 * and we need to install the table handler before that.
1265 status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
1269 acpi_early_processor_set_pdc();
1272 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1273 * is necessary to enable it as early as possible.
1275 acpi_ec_dsdt_probe();
1277 pr_info("Interpreter enabled\n");
1279 /* Initialize sleep structures */
1283 * Get the system interrupt model and evaluate \_PIC.
1285 result = acpi_bus_init_irq();
1290 * Register the for all standard device notifications.
1293 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
1294 &acpi_bus_notify, NULL);
1295 if (ACPI_FAILURE(status)) {
1296 pr_err("Unable to register for system notifications\n");
1301 * Create the top ACPI proc directory
1303 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1305 result = bus_register(&acpi_bus_type);
1309 /* Mimic structured exception handling */
1315 struct kobject *acpi_kobj;
1316 EXPORT_SYMBOL_GPL(acpi_kobj);
1318 static int __init acpi_init(void)
1322 if (acpi_disabled) {
1323 pr_info("Interpreter disabled.\n");
1327 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1329 pr_debug("%s: kset create error\n", __func__);
1333 result = acpi_bus_init();
1339 pci_mmcfg_late_init();
1343 acpi_debugfs_init();
1344 acpi_sleep_proc_init();
1345 acpi_wakeup_device_init();
1346 acpi_debugger_init();
1347 acpi_setup_sb_notify_handler();
1351 subsys_initcall(acpi_init);