0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) "ACPI: " fmt
0009
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/ioport.h>
0013 #include <linux/kernel.h>
0014 #include <linux/list.h>
0015 #include <linux/sched.h>
0016 #include <linux/pm.h>
0017 #include <linux/device.h>
0018 #include <linux/proc_fs.h>
0019 #include <linux/acpi.h>
0020 #include <linux/slab.h>
0021 #include <linux/regulator/machine.h>
0022 #include <linux/workqueue.h>
0023 #include <linux/reboot.h>
0024 #include <linux/delay.h>
0025 #ifdef CONFIG_X86
0026 #include <asm/mpspec.h>
0027 #include <linux/dmi.h>
0028 #endif
0029 #include <linux/acpi_agdi.h>
0030 #include <linux/acpi_iort.h>
0031 #include <linux/acpi_viot.h>
0032 #include <linux/pci.h>
0033 #include <acpi/apei.h>
0034 #include <linux/suspend.h>
0035 #include <linux/prmt.h>
0036
0037 #include "internal.h"
0038
0039 struct acpi_device *acpi_root;
0040 struct proc_dir_entry *acpi_root_dir;
0041 EXPORT_SYMBOL(acpi_root_dir);
0042
0043 #ifdef CONFIG_X86
0044 #ifdef CONFIG_ACPI_CUSTOM_DSDT
0045 static inline int set_copy_dsdt(const struct dmi_system_id *id)
0046 {
0047 return 0;
0048 }
0049 #else
0050 static int set_copy_dsdt(const struct dmi_system_id *id)
0051 {
0052 pr_notice("%s detected - force copy of DSDT to local memory\n", id->ident);
0053 acpi_gbl_copy_dsdt_locally = 1;
0054 return 0;
0055 }
0056 #endif
0057
0058 static const struct dmi_system_id dsdt_dmi_table[] __initconst = {
0059
0060
0061
0062
0063 {
0064 .callback = set_copy_dsdt,
0065 .ident = "TOSHIBA Satellite",
0066 .matches = {
0067 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
0068 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
0069 },
0070 },
0071 {}
0072 };
0073 #endif
0074
0075
0076
0077
0078
0079 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
0080 unsigned long long *sta)
0081 {
0082 acpi_status status;
0083
0084 status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
0085 if (ACPI_SUCCESS(status))
0086 return AE_OK;
0087
0088 if (status == AE_NOT_FOUND) {
0089 *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
0090 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
0091 return AE_OK;
0092 }
0093 return status;
0094 }
0095 EXPORT_SYMBOL_GPL(acpi_bus_get_status_handle);
0096
0097 int acpi_bus_get_status(struct acpi_device *device)
0098 {
0099 acpi_status status;
0100 unsigned long long sta;
0101
0102 if (acpi_device_override_status(device, &sta)) {
0103 acpi_set_device_status(device, sta);
0104 return 0;
0105 }
0106
0107
0108 if (acpi_device_is_battery(device) && device->dep_unmet) {
0109 acpi_set_device_status(device, 0);
0110 return 0;
0111 }
0112
0113 status = acpi_bus_get_status_handle(device->handle, &sta);
0114 if (ACPI_FAILURE(status))
0115 return -ENODEV;
0116
0117 acpi_set_device_status(device, sta);
0118
0119 if (device->status.functional && !device->status.present) {
0120 pr_debug("Device [%s] status [%08x]: functional but not present\n",
0121 device->pnp.bus_id, (u32)sta);
0122 }
0123
0124 pr_debug("Device [%s] status [%08x]\n", device->pnp.bus_id, (u32)sta);
0125 return 0;
0126 }
0127 EXPORT_SYMBOL(acpi_bus_get_status);
0128
0129 void acpi_bus_private_data_handler(acpi_handle handle,
0130 void *context)
0131 {
0132 return;
0133 }
0134 EXPORT_SYMBOL(acpi_bus_private_data_handler);
0135
0136 int acpi_bus_attach_private_data(acpi_handle handle, void *data)
0137 {
0138 acpi_status status;
0139
0140 status = acpi_attach_data(handle,
0141 acpi_bus_private_data_handler, data);
0142 if (ACPI_FAILURE(status)) {
0143 acpi_handle_debug(handle, "Error attaching device data\n");
0144 return -ENODEV;
0145 }
0146
0147 return 0;
0148 }
0149 EXPORT_SYMBOL_GPL(acpi_bus_attach_private_data);
0150
0151 int acpi_bus_get_private_data(acpi_handle handle, void **data)
0152 {
0153 acpi_status status;
0154
0155 if (!data)
0156 return -EINVAL;
0157
0158 status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
0159 if (ACPI_FAILURE(status)) {
0160 acpi_handle_debug(handle, "No context for object\n");
0161 return -ENODEV;
0162 }
0163
0164 return 0;
0165 }
0166 EXPORT_SYMBOL_GPL(acpi_bus_get_private_data);
0167
0168 void acpi_bus_detach_private_data(acpi_handle handle)
0169 {
0170 acpi_detach_data(handle, acpi_bus_private_data_handler);
0171 }
0172 EXPORT_SYMBOL_GPL(acpi_bus_detach_private_data);
0173
0174 static void acpi_print_osc_error(acpi_handle handle,
0175 struct acpi_osc_context *context, char *error)
0176 {
0177 int i;
0178
0179 acpi_handle_debug(handle, "(%s): %s\n", context->uuid_str, error);
0180
0181 pr_debug("_OSC request data:");
0182 for (i = 0; i < context->cap.length; i += sizeof(u32))
0183 pr_debug(" %x", *((u32 *)(context->cap.pointer + i)));
0184
0185 pr_debug("\n");
0186 }
0187
0188 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
0189 {
0190 acpi_status status;
0191 struct acpi_object_list input;
0192 union acpi_object in_params[4];
0193 union acpi_object *out_obj;
0194 guid_t guid;
0195 u32 errors;
0196 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
0197
0198 if (!context)
0199 return AE_ERROR;
0200 if (guid_parse(context->uuid_str, &guid))
0201 return AE_ERROR;
0202 context->ret.length = ACPI_ALLOCATE_BUFFER;
0203 context->ret.pointer = NULL;
0204
0205
0206 input.count = 4;
0207 input.pointer = in_params;
0208 in_params[0].type = ACPI_TYPE_BUFFER;
0209 in_params[0].buffer.length = 16;
0210 in_params[0].buffer.pointer = (u8 *)&guid;
0211 in_params[1].type = ACPI_TYPE_INTEGER;
0212 in_params[1].integer.value = context->rev;
0213 in_params[2].type = ACPI_TYPE_INTEGER;
0214 in_params[2].integer.value = context->cap.length/sizeof(u32);
0215 in_params[3].type = ACPI_TYPE_BUFFER;
0216 in_params[3].buffer.length = context->cap.length;
0217 in_params[3].buffer.pointer = context->cap.pointer;
0218
0219 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
0220 if (ACPI_FAILURE(status))
0221 return status;
0222
0223 if (!output.length)
0224 return AE_NULL_OBJECT;
0225
0226 out_obj = output.pointer;
0227 if (out_obj->type != ACPI_TYPE_BUFFER
0228 || out_obj->buffer.length != context->cap.length) {
0229 acpi_print_osc_error(handle, context,
0230 "_OSC evaluation returned wrong type");
0231 status = AE_TYPE;
0232 goto out_kfree;
0233 }
0234
0235 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
0236 if (errors) {
0237 if (errors & OSC_REQUEST_ERROR)
0238 acpi_print_osc_error(handle, context,
0239 "_OSC request failed");
0240 if (errors & OSC_INVALID_UUID_ERROR)
0241 acpi_print_osc_error(handle, context,
0242 "_OSC invalid UUID");
0243 if (errors & OSC_INVALID_REVISION_ERROR)
0244 acpi_print_osc_error(handle, context,
0245 "_OSC invalid revision");
0246 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
0247 if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD]
0248 & OSC_QUERY_ENABLE)
0249 goto out_success;
0250 status = AE_SUPPORT;
0251 goto out_kfree;
0252 }
0253 status = AE_ERROR;
0254 goto out_kfree;
0255 }
0256 out_success:
0257 context->ret.length = out_obj->buffer.length;
0258 context->ret.pointer = kmemdup(out_obj->buffer.pointer,
0259 context->ret.length, GFP_KERNEL);
0260 if (!context->ret.pointer) {
0261 status = AE_NO_MEMORY;
0262 goto out_kfree;
0263 }
0264 status = AE_OK;
0265
0266 out_kfree:
0267 kfree(output.pointer);
0268 return status;
0269 }
0270 EXPORT_SYMBOL(acpi_run_osc);
0271
0272 bool osc_sb_apei_support_acked;
0273
0274
0275
0276
0277
0278 bool osc_pc_lpi_support_confirmed;
0279 EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed);
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292 bool osc_cpc_flexible_adr_space_confirmed;
0293 EXPORT_SYMBOL_GPL(osc_cpc_flexible_adr_space_confirmed);
0294
0295
0296
0297
0298 bool osc_sb_native_usb4_support_confirmed;
0299 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed);
0300
0301 bool osc_sb_cppc2_support_acked;
0302
0303 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
0304 static void acpi_bus_osc_negotiate_platform_control(void)
0305 {
0306 u32 capbuf[2], *capbuf_ret;
0307 struct acpi_osc_context context = {
0308 .uuid_str = sb_uuid_str,
0309 .rev = 1,
0310 .cap.length = 8,
0311 .cap.pointer = capbuf,
0312 };
0313 acpi_handle handle;
0314
0315 capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
0316 capbuf[OSC_SUPPORT_DWORD] = OSC_SB_PR3_SUPPORT;
0317 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
0318 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PAD_SUPPORT;
0319 if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
0320 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PPC_OST_SUPPORT;
0321
0322 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT;
0323 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PCLPI_SUPPORT;
0324 if (IS_ENABLED(CONFIG_ACPI_PRMT))
0325 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PRM_SUPPORT;
0326
0327 #ifdef CONFIG_ARM64
0328 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
0329 #endif
0330 #ifdef CONFIG_X86
0331 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
0332 #endif
0333
0334 #ifdef CONFIG_ACPI_CPPC_LIB
0335 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_SUPPORT;
0336 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPCV2_SUPPORT;
0337 #endif
0338
0339 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
0340
0341 if (IS_ENABLED(CONFIG_SCHED_MC_PRIO))
0342 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_DIVERSE_HIGH_SUPPORT;
0343
0344 if (IS_ENABLED(CONFIG_USB4))
0345 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_NATIVE_USB4_SUPPORT;
0346
0347 if (!ghes_disable)
0348 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_APEI_SUPPORT;
0349 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
0350 return;
0351
0352 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
0353 return;
0354
0355 capbuf_ret = context.ret.pointer;
0356 if (context.ret.length <= OSC_SUPPORT_DWORD) {
0357 kfree(context.ret.pointer);
0358 return;
0359 }
0360
0361
0362
0363
0364
0365 capbuf[OSC_QUERY_DWORD] = 0;
0366 capbuf[OSC_SUPPORT_DWORD] = capbuf_ret[OSC_SUPPORT_DWORD];
0367 kfree(context.ret.pointer);
0368
0369 if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
0370 return;
0371
0372 capbuf_ret = context.ret.pointer;
0373 if (context.ret.length > OSC_SUPPORT_DWORD) {
0374 #ifdef CONFIG_ACPI_CPPC_LIB
0375 osc_sb_cppc2_support_acked = capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_CPCV2_SUPPORT;
0376 #endif
0377
0378 osc_sb_apei_support_acked =
0379 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT;
0380 osc_pc_lpi_support_confirmed =
0381 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_PCLPI_SUPPORT;
0382 osc_sb_native_usb4_support_confirmed =
0383 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_NATIVE_USB4_SUPPORT;
0384 osc_cpc_flexible_adr_space_confirmed =
0385 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
0386 }
0387
0388 kfree(context.ret.pointer);
0389 }
0390
0391
0392
0393
0394
0395
0396 u32 osc_sb_native_usb4_control;
0397 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control);
0398
0399 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits)
0400 {
0401 pr_info("%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg,
0402 (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-',
0403 (bits & OSC_USB_DP_TUNNELING) ? '+' : '-',
0404 (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-',
0405 (bits & OSC_USB_XDOMAIN) ? '+' : '-');
0406 }
0407
0408 static u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A";
0409 static void acpi_bus_osc_negotiate_usb_control(void)
0410 {
0411 u32 capbuf[3];
0412 struct acpi_osc_context context = {
0413 .uuid_str = sb_usb_uuid_str,
0414 .rev = 1,
0415 .cap.length = sizeof(capbuf),
0416 .cap.pointer = capbuf,
0417 };
0418 acpi_handle handle;
0419 acpi_status status;
0420 u32 control;
0421
0422 if (!osc_sb_native_usb4_support_confirmed)
0423 return;
0424
0425 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
0426 return;
0427
0428 control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING |
0429 OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN;
0430
0431 capbuf[OSC_QUERY_DWORD] = 0;
0432 capbuf[OSC_SUPPORT_DWORD] = 0;
0433 capbuf[OSC_CONTROL_DWORD] = control;
0434
0435 status = acpi_run_osc(handle, &context);
0436 if (ACPI_FAILURE(status))
0437 return;
0438
0439 if (context.ret.length != sizeof(capbuf)) {
0440 pr_info("USB4 _OSC: returned invalid length buffer\n");
0441 goto out_free;
0442 }
0443
0444 osc_sb_native_usb4_control =
0445 control & acpi_osc_ctx_get_pci_control(&context);
0446
0447 acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control);
0448 acpi_bus_decode_usb_osc("USB4 _OSC: OS controls",
0449 osc_sb_native_usb4_control);
0450
0451 out_free:
0452 kfree(context.ret.pointer);
0453 }
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
0465 {
0466 struct acpi_device *adev;
0467 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
0468 bool hotplug_event = false;
0469
0470 switch (type) {
0471 case ACPI_NOTIFY_BUS_CHECK:
0472 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
0473 hotplug_event = true;
0474 break;
0475
0476 case ACPI_NOTIFY_DEVICE_CHECK:
0477 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
0478 hotplug_event = true;
0479 break;
0480
0481 case ACPI_NOTIFY_DEVICE_WAKE:
0482 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
0483 break;
0484
0485 case ACPI_NOTIFY_EJECT_REQUEST:
0486 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
0487 hotplug_event = true;
0488 break;
0489
0490 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
0491 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
0492
0493 break;
0494
0495 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
0496 acpi_handle_err(handle, "Device cannot be configured due "
0497 "to a frequency mismatch\n");
0498 break;
0499
0500 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
0501 acpi_handle_err(handle, "Device cannot be configured due "
0502 "to a bus mode mismatch\n");
0503 break;
0504
0505 case ACPI_NOTIFY_POWER_FAULT:
0506 acpi_handle_err(handle, "Device has suffered a power fault\n");
0507 break;
0508
0509 default:
0510 acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
0511 break;
0512 }
0513
0514 adev = acpi_bus_get_acpi_device(handle);
0515 if (!adev)
0516 goto err;
0517
0518 if (adev->dev.driver) {
0519 struct acpi_driver *driver = to_acpi_driver(adev->dev.driver);
0520
0521 if (driver && driver->ops.notify &&
0522 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
0523 driver->ops.notify(adev, type);
0524 }
0525
0526 if (!hotplug_event) {
0527 acpi_bus_put_acpi_device(adev);
0528 return;
0529 }
0530
0531 if (ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
0532 return;
0533
0534 acpi_bus_put_acpi_device(adev);
0535
0536 err:
0537 acpi_evaluate_ost(handle, type, ost_code, NULL);
0538 }
0539
0540 static void acpi_notify_device(acpi_handle handle, u32 event, void *data)
0541 {
0542 struct acpi_device *device = data;
0543 struct acpi_driver *acpi_drv = to_acpi_driver(device->dev.driver);
0544
0545 acpi_drv->ops.notify(device, event);
0546 }
0547
0548 static void acpi_notify_device_fixed(void *data)
0549 {
0550 struct acpi_device *device = data;
0551
0552
0553 acpi_notify_device(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
0554 }
0555
0556 static u32 acpi_device_fixed_event(void *data)
0557 {
0558 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_notify_device_fixed, data);
0559 return ACPI_INTERRUPT_HANDLED;
0560 }
0561
0562 static int acpi_device_install_notify_handler(struct acpi_device *device)
0563 {
0564 acpi_status status;
0565
0566 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
0567 status =
0568 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
0569 acpi_device_fixed_event,
0570 device);
0571 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
0572 status =
0573 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
0574 acpi_device_fixed_event,
0575 device);
0576 else
0577 status = acpi_install_notify_handler(device->handle,
0578 ACPI_DEVICE_NOTIFY,
0579 acpi_notify_device,
0580 device);
0581
0582 if (ACPI_FAILURE(status))
0583 return -EINVAL;
0584 return 0;
0585 }
0586
0587 static void acpi_device_remove_notify_handler(struct acpi_device *device)
0588 {
0589 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
0590 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
0591 acpi_device_fixed_event);
0592 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
0593 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
0594 acpi_device_fixed_event);
0595 else
0596 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
0597 acpi_notify_device);
0598 }
0599
0600
0601
0602 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
0603 #define ACPI_SB_INDICATE_INTERVAL 10000
0604
0605 static void sb_notify_work(struct work_struct *dummy)
0606 {
0607 acpi_handle sb_handle;
0608
0609 orderly_poweroff(true);
0610
0611
0612
0613
0614
0615
0616 acpi_get_handle(NULL, "\\_SB", &sb_handle);
0617 while (1) {
0618 pr_info("Graceful shutdown in progress.\n");
0619 acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
0620 ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
0621 msleep(ACPI_SB_INDICATE_INTERVAL);
0622 }
0623 }
0624
0625 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
0626 {
0627 static DECLARE_WORK(acpi_sb_work, sb_notify_work);
0628
0629 if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
0630 if (!work_busy(&acpi_sb_work))
0631 schedule_work(&acpi_sb_work);
0632 } else
0633 pr_warn("event %x is not supported by \\_SB device\n", event);
0634 }
0635
0636 static int __init acpi_setup_sb_notify_handler(void)
0637 {
0638 acpi_handle sb_handle;
0639
0640 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
0641 return -ENXIO;
0642
0643 if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
0644 acpi_sb_notify, NULL)))
0645 return -EINVAL;
0646
0647 return 0;
0648 }
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660 struct device *acpi_get_first_physical_node(struct acpi_device *adev)
0661 {
0662 struct mutex *physical_node_lock = &adev->physical_node_lock;
0663 struct device *phys_dev;
0664
0665 mutex_lock(physical_node_lock);
0666 if (list_empty(&adev->physical_node_list)) {
0667 phys_dev = NULL;
0668 } else {
0669 const struct acpi_device_physical_node *node;
0670
0671 node = list_first_entry(&adev->physical_node_list,
0672 struct acpi_device_physical_node, node);
0673
0674 phys_dev = node->dev;
0675 }
0676 mutex_unlock(physical_node_lock);
0677 return phys_dev;
0678 }
0679 EXPORT_SYMBOL_GPL(acpi_get_first_physical_node);
0680
0681 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
0682 const struct device *dev)
0683 {
0684 const struct device *phys_dev = acpi_get_first_physical_node(adev);
0685
0686 return phys_dev && phys_dev == dev ? adev : NULL;
0687 }
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700 bool acpi_device_is_first_physical_node(struct acpi_device *adev,
0701 const struct device *dev)
0702 {
0703 return !!acpi_primary_dev_companion(adev, dev);
0704 }
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727 struct acpi_device *acpi_companion_match(const struct device *dev)
0728 {
0729 struct acpi_device *adev;
0730
0731 adev = ACPI_COMPANION(dev);
0732 if (!adev)
0733 return NULL;
0734
0735 if (list_empty(&adev->pnp.ids))
0736 return NULL;
0737
0738 return acpi_primary_dev_companion(adev, dev);
0739 }
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751 static bool acpi_of_match_device(struct acpi_device *adev,
0752 const struct of_device_id *of_match_table,
0753 const struct of_device_id **of_id)
0754 {
0755 const union acpi_object *of_compatible, *obj;
0756 int i, nval;
0757
0758 if (!adev)
0759 return false;
0760
0761 of_compatible = adev->data.of_compatible;
0762 if (!of_match_table || !of_compatible)
0763 return false;
0764
0765 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
0766 nval = of_compatible->package.count;
0767 obj = of_compatible->package.elements;
0768 } else {
0769 nval = 1;
0770 obj = of_compatible;
0771 }
0772
0773 for (i = 0; i < nval; i++, obj++) {
0774 const struct of_device_id *id;
0775
0776 for (id = of_match_table; id->compatible[0]; id++)
0777 if (!strcasecmp(obj->string.pointer, id->compatible)) {
0778 if (of_id)
0779 *of_id = id;
0780 return true;
0781 }
0782 }
0783
0784 return false;
0785 }
0786
0787 static bool acpi_of_modalias(struct acpi_device *adev,
0788 char *modalias, size_t len)
0789 {
0790 const union acpi_object *of_compatible;
0791 const union acpi_object *obj;
0792 const char *str, *chr;
0793
0794 of_compatible = adev->data.of_compatible;
0795 if (!of_compatible)
0796 return false;
0797
0798 if (of_compatible->type == ACPI_TYPE_PACKAGE)
0799 obj = of_compatible->package.elements;
0800 else
0801 obj = of_compatible;
0802
0803 str = obj->string.pointer;
0804 chr = strchr(str, ',');
0805 strlcpy(modalias, chr ? chr + 1 : str, len);
0806
0807 return true;
0808 }
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821 void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
0822 char *modalias, size_t len)
0823 {
0824 if (!acpi_of_modalias(adev, modalias, len))
0825 strlcpy(modalias, default_id, len);
0826 }
0827 EXPORT_SYMBOL_GPL(acpi_set_modalias);
0828
0829 static bool __acpi_match_device_cls(const struct acpi_device_id *id,
0830 struct acpi_hardware_id *hwid)
0831 {
0832 int i, msk, byte_shift;
0833 char buf[3];
0834
0835 if (!id->cls)
0836 return false;
0837
0838
0839 for (i = 1; i <= 3; i++) {
0840 byte_shift = 8 * (3 - i);
0841 msk = (id->cls_msk >> byte_shift) & 0xFF;
0842 if (!msk)
0843 continue;
0844
0845 sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
0846 if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
0847 return false;
0848 }
0849 return true;
0850 }
0851
0852 static bool __acpi_match_device(struct acpi_device *device,
0853 const struct acpi_device_id *acpi_ids,
0854 const struct of_device_id *of_ids,
0855 const struct acpi_device_id **acpi_id,
0856 const struct of_device_id **of_id)
0857 {
0858 const struct acpi_device_id *id;
0859 struct acpi_hardware_id *hwid;
0860
0861
0862
0863
0864
0865 if (!device || !device->status.present)
0866 return false;
0867
0868 list_for_each_entry(hwid, &device->pnp.ids, list) {
0869
0870 if (acpi_ids) {
0871 for (id = acpi_ids; id->id[0] || id->cls; id++) {
0872 if (id->id[0] && !strcmp((char *)id->id, hwid->id))
0873 goto out_acpi_match;
0874 if (id->cls && __acpi_match_device_cls(id, hwid))
0875 goto out_acpi_match;
0876 }
0877 }
0878
0879
0880
0881
0882
0883 if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id))
0884 return acpi_of_match_device(device, of_ids, of_id);
0885 }
0886 return false;
0887
0888 out_acpi_match:
0889 if (acpi_id)
0890 *acpi_id = id;
0891 return true;
0892 }
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
0906 const struct device *dev)
0907 {
0908 const struct acpi_device_id *id = NULL;
0909
0910 __acpi_match_device(acpi_companion_match(dev), ids, NULL, &id, NULL);
0911 return id;
0912 }
0913 EXPORT_SYMBOL_GPL(acpi_match_device);
0914
0915 static const void *acpi_of_device_get_match_data(const struct device *dev)
0916 {
0917 struct acpi_device *adev = ACPI_COMPANION(dev);
0918 const struct of_device_id *match = NULL;
0919
0920 if (!acpi_of_match_device(adev, dev->driver->of_match_table, &match))
0921 return NULL;
0922
0923 return match->data;
0924 }
0925
0926 const void *acpi_device_get_match_data(const struct device *dev)
0927 {
0928 const struct acpi_device_id *match;
0929
0930 if (!dev->driver->acpi_match_table)
0931 return acpi_of_device_get_match_data(dev);
0932
0933 match = acpi_match_device(dev->driver->acpi_match_table, dev);
0934 if (!match)
0935 return NULL;
0936
0937 return (const void *)match->driver_data;
0938 }
0939 EXPORT_SYMBOL_GPL(acpi_device_get_match_data);
0940
0941 int acpi_match_device_ids(struct acpi_device *device,
0942 const struct acpi_device_id *ids)
0943 {
0944 return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT;
0945 }
0946 EXPORT_SYMBOL(acpi_match_device_ids);
0947
0948 bool acpi_driver_match_device(struct device *dev,
0949 const struct device_driver *drv)
0950 {
0951 if (!drv->acpi_match_table)
0952 return acpi_of_match_device(ACPI_COMPANION(dev),
0953 drv->of_match_table,
0954 NULL);
0955
0956 return __acpi_match_device(acpi_companion_match(dev),
0957 drv->acpi_match_table, drv->of_match_table,
0958 NULL, NULL);
0959 }
0960 EXPORT_SYMBOL_GPL(acpi_driver_match_device);
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973
0974 int acpi_bus_register_driver(struct acpi_driver *driver)
0975 {
0976 int ret;
0977
0978 if (acpi_disabled)
0979 return -ENODEV;
0980 driver->drv.name = driver->name;
0981 driver->drv.bus = &acpi_bus_type;
0982 driver->drv.owner = driver->owner;
0983
0984 ret = driver_register(&driver->drv);
0985 return ret;
0986 }
0987
0988 EXPORT_SYMBOL(acpi_bus_register_driver);
0989
0990
0991
0992
0993
0994
0995
0996
0997 void acpi_bus_unregister_driver(struct acpi_driver *driver)
0998 {
0999 driver_unregister(&driver->drv);
1000 }
1001
1002 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1003
1004
1005
1006
1007
1008 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
1009 {
1010 struct acpi_device *acpi_dev = to_acpi_device(dev);
1011 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1012
1013 return acpi_dev->flags.match_driver
1014 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
1015 }
1016
1017 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1018 {
1019 return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
1020 }
1021
1022 static int acpi_device_probe(struct device *dev)
1023 {
1024 struct acpi_device *acpi_dev = to_acpi_device(dev);
1025 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1026 int ret;
1027
1028 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
1029 return -EINVAL;
1030
1031 if (!acpi_drv->ops.add)
1032 return -ENOSYS;
1033
1034 ret = acpi_drv->ops.add(acpi_dev);
1035 if (ret)
1036 return ret;
1037
1038 pr_debug("Driver [%s] successfully bound to device [%s]\n",
1039 acpi_drv->name, acpi_dev->pnp.bus_id);
1040
1041 if (acpi_drv->ops.notify) {
1042 ret = acpi_device_install_notify_handler(acpi_dev);
1043 if (ret) {
1044 if (acpi_drv->ops.remove)
1045 acpi_drv->ops.remove(acpi_dev);
1046
1047 acpi_dev->driver_data = NULL;
1048 return ret;
1049 }
1050 }
1051
1052 pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name,
1053 acpi_dev->pnp.bus_id);
1054
1055 get_device(dev);
1056 return 0;
1057 }
1058
1059 static void acpi_device_remove(struct device *dev)
1060 {
1061 struct acpi_device *acpi_dev = to_acpi_device(dev);
1062 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1063
1064 if (acpi_drv->ops.notify)
1065 acpi_device_remove_notify_handler(acpi_dev);
1066
1067 if (acpi_drv->ops.remove)
1068 acpi_drv->ops.remove(acpi_dev);
1069
1070 acpi_dev->driver_data = NULL;
1071
1072 put_device(dev);
1073 }
1074
1075 struct bus_type acpi_bus_type = {
1076 .name = "acpi",
1077 .match = acpi_bus_match,
1078 .probe = acpi_device_probe,
1079 .remove = acpi_device_remove,
1080 .uevent = acpi_device_uevent,
1081 };
1082
1083 int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data)
1084 {
1085 return bus_for_each_dev(&acpi_bus_type, NULL, data, fn);
1086 }
1087 EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
1088
1089 struct acpi_dev_walk_context {
1090 int (*fn)(struct acpi_device *, void *);
1091 void *data;
1092 };
1093
1094 static int acpi_dev_for_one_check(struct device *dev, void *context)
1095 {
1096 struct acpi_dev_walk_context *adwc = context;
1097
1098 if (dev->bus != &acpi_bus_type)
1099 return 0;
1100
1101 return adwc->fn(to_acpi_device(dev), adwc->data);
1102 }
1103 EXPORT_SYMBOL_GPL(acpi_dev_for_each_child);
1104
1105 int acpi_dev_for_each_child(struct acpi_device *adev,
1106 int (*fn)(struct acpi_device *, void *), void *data)
1107 {
1108 struct acpi_dev_walk_context adwc = {
1109 .fn = fn,
1110 .data = data,
1111 };
1112
1113 return device_for_each_child(&adev->dev, &adwc, acpi_dev_for_one_check);
1114 }
1115
1116 int acpi_dev_for_each_child_reverse(struct acpi_device *adev,
1117 int (*fn)(struct acpi_device *, void *),
1118 void *data)
1119 {
1120 struct acpi_dev_walk_context adwc = {
1121 .fn = fn,
1122 .data = data,
1123 };
1124
1125 return device_for_each_child_reverse(&adev->dev, &adwc, acpi_dev_for_one_check);
1126 }
1127
1128
1129
1130
1131
1132 static int __init acpi_bus_init_irq(void)
1133 {
1134 acpi_status status;
1135 char *message = NULL;
1136
1137
1138
1139
1140
1141
1142
1143 switch (acpi_irq_model) {
1144 case ACPI_IRQ_MODEL_PIC:
1145 message = "PIC";
1146 break;
1147 case ACPI_IRQ_MODEL_IOAPIC:
1148 message = "IOAPIC";
1149 break;
1150 case ACPI_IRQ_MODEL_IOSAPIC:
1151 message = "IOSAPIC";
1152 break;
1153 case ACPI_IRQ_MODEL_GIC:
1154 message = "GIC";
1155 break;
1156 case ACPI_IRQ_MODEL_PLATFORM:
1157 message = "platform specific model";
1158 break;
1159 case ACPI_IRQ_MODEL_LPIC:
1160 message = "LPIC";
1161 break;
1162 default:
1163 pr_info("Unknown interrupt routing model\n");
1164 return -ENODEV;
1165 }
1166
1167 pr_info("Using %s for interrupt routing\n", message);
1168
1169 status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
1170 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
1171 pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status));
1172 return -ENODEV;
1173 }
1174
1175 return 0;
1176 }
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188 void __init acpi_early_init(void)
1189 {
1190 acpi_status status;
1191
1192 if (acpi_disabled)
1193 return;
1194
1195 pr_info("Core revision %08x\n", ACPI_CA_VERSION);
1196
1197
1198 if (!acpi_strict)
1199 acpi_gbl_enable_interpreter_slack = TRUE;
1200
1201 acpi_permanent_mmap = true;
1202
1203 #ifdef CONFIG_X86
1204
1205
1206
1207
1208
1209
1210
1211 dmi_check_system(dsdt_dmi_table);
1212 #endif
1213
1214 status = acpi_reallocate_root_table();
1215 if (ACPI_FAILURE(status)) {
1216 pr_err("Unable to reallocate ACPI tables\n");
1217 goto error0;
1218 }
1219
1220 status = acpi_initialize_subsystem();
1221 if (ACPI_FAILURE(status)) {
1222 pr_err("Unable to initialize the ACPI Interpreter\n");
1223 goto error0;
1224 }
1225
1226 #ifdef CONFIG_X86
1227 if (!acpi_ioapic) {
1228
1229 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
1230 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
1231 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
1232 }
1233
1234 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
1235 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
1236 } else {
1237
1238
1239
1240
1241 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
1242 }
1243 #endif
1244 return;
1245
1246 error0:
1247 disable_acpi();
1248 }
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259 void __init acpi_subsystem_init(void)
1260 {
1261 acpi_status status;
1262
1263 if (acpi_disabled)
1264 return;
1265
1266 status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
1267 if (ACPI_FAILURE(status)) {
1268 pr_err("Unable to enable ACPI\n");
1269 disable_acpi();
1270 } else {
1271
1272
1273
1274
1275
1276
1277 regulator_has_full_constraints();
1278 }
1279 }
1280
1281 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
1282 {
1283 if (event == ACPI_TABLE_EVENT_LOAD)
1284 acpi_scan_table_notify();
1285
1286 return acpi_sysfs_table_handler(event, table, context);
1287 }
1288
1289 static int __init acpi_bus_init(void)
1290 {
1291 int result;
1292 acpi_status status;
1293
1294 acpi_os_initialize1();
1295
1296 status = acpi_load_tables();
1297 if (ACPI_FAILURE(status)) {
1298 pr_err("Unable to load the System Description Tables\n");
1299 goto error1;
1300 }
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312 acpi_ec_ecdt_probe();
1313
1314 status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
1315 if (ACPI_FAILURE(status)) {
1316 pr_err("Unable to start the ACPI Interpreter\n");
1317 goto error1;
1318 }
1319
1320 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1321 if (ACPI_FAILURE(status)) {
1322 pr_err("Unable to initialize ACPI objects\n");
1323 goto error1;
1324 }
1325
1326
1327 acpi_early_processor_osc();
1328
1329
1330
1331
1332
1333 acpi_bus_osc_negotiate_platform_control();
1334 acpi_bus_osc_negotiate_usb_control();
1335
1336
1337
1338
1339
1340 status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
1341
1342 acpi_sysfs_init();
1343
1344 acpi_early_processor_set_pdc();
1345
1346
1347
1348
1349
1350 acpi_ec_dsdt_probe();
1351
1352 pr_info("Interpreter enabled\n");
1353
1354
1355 acpi_sleep_init();
1356
1357
1358
1359
1360 result = acpi_bus_init_irq();
1361 if (result)
1362 goto error1;
1363
1364
1365
1366
1367 status =
1368 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
1369 &acpi_bus_notify, NULL);
1370 if (ACPI_FAILURE(status)) {
1371 pr_err("Unable to register for system notifications\n");
1372 goto error1;
1373 }
1374
1375
1376
1377
1378 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1379
1380 result = bus_register(&acpi_bus_type);
1381 if (!result)
1382 return 0;
1383
1384
1385 error1:
1386 acpi_terminate();
1387 return -ENODEV;
1388 }
1389
1390 struct kobject *acpi_kobj;
1391 EXPORT_SYMBOL_GPL(acpi_kobj);
1392
1393 static int __init acpi_init(void)
1394 {
1395 int result;
1396
1397 if (acpi_disabled) {
1398 pr_info("Interpreter disabled.\n");
1399 return -ENODEV;
1400 }
1401
1402 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1403 if (!acpi_kobj)
1404 pr_debug("%s: kset create error\n", __func__);
1405
1406 init_prmt();
1407 acpi_init_pcc();
1408 result = acpi_bus_init();
1409 if (result) {
1410 kobject_put(acpi_kobj);
1411 disable_acpi();
1412 return result;
1413 }
1414
1415 pci_mmcfg_late_init();
1416 acpi_iort_init();
1417 acpi_viot_early_init();
1418 acpi_hest_init();
1419 acpi_ghes_init();
1420 acpi_scan_init();
1421 acpi_ec_init();
1422 acpi_debugfs_init();
1423 acpi_sleep_proc_init();
1424 acpi_wakeup_device_init();
1425 acpi_debugger_init();
1426 acpi_setup_sb_notify_handler();
1427 acpi_viot_init();
1428 acpi_agdi_init();
1429 return 0;
1430 }
1431
1432 subsys_initcall(acpi_init);