1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $) 4 * 5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 6 */ 7 8 #define pr_fmt(fmt) "ACPI: " fmt 9 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> 16 #include <linux/pm.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> 25 #ifdef CONFIG_X86 26 #include <asm/mpspec.h> 27 #include <linux/dmi.h> 28 #endif 29 #include <linux/acpi_iort.h> 30 #include <linux/pci.h> 31 #include <acpi/apei.h> 32 #include <linux/suspend.h> 33 34 #include "internal.h" 35 36 struct acpi_device *acpi_root; 37 struct proc_dir_entry *acpi_root_dir; 38 EXPORT_SYMBOL(acpi_root_dir); 39 40 #ifdef CONFIG_X86 41 #ifdef CONFIG_ACPI_CUSTOM_DSDT 42 static inline int set_copy_dsdt(const struct dmi_system_id *id) 43 { 44 return 0; 45 } 46 #else 47 static int set_copy_dsdt(const struct dmi_system_id *id) 48 { 49 pr_notice("%s detected - force copy of DSDT to local memory\n", id->ident); 50 acpi_gbl_copy_dsdt_locally = 1; 51 return 0; 52 } 53 #endif 54 55 static const struct dmi_system_id dsdt_dmi_table[] __initconst = { 56 /* 57 * Invoke DSDT corruption work-around on all Toshiba Satellite. 58 * https://bugzilla.kernel.org/show_bug.cgi?id=14679 59 */ 60 { 61 .callback = set_copy_dsdt, 62 .ident = "TOSHIBA Satellite", 63 .matches = { 64 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"), 65 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"), 66 }, 67 }, 68 {} 69 }; 70 #endif 71 72 /* -------------------------------------------------------------------------- 73 Device Management 74 -------------------------------------------------------------------------- */ 75 76 acpi_status acpi_bus_get_status_handle(acpi_handle handle, 77 unsigned long long *sta) 78 { 79 acpi_status status; 80 81 status = acpi_evaluate_integer(handle, "_STA", NULL, sta); 82 if (ACPI_SUCCESS(status)) 83 return AE_OK; 84 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; 88 return AE_OK; 89 } 90 return status; 91 } 92 EXPORT_SYMBOL_GPL(acpi_bus_get_status_handle); 93 94 int acpi_bus_get_status(struct acpi_device *device) 95 { 96 acpi_status status; 97 unsigned long long sta; 98 99 if (acpi_device_always_present(device)) { 100 acpi_set_device_status(device, ACPI_STA_DEFAULT); 101 return 0; 102 } 103 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); 107 return 0; 108 } 109 110 status = acpi_bus_get_status_handle(device->handle, &sta); 111 if (ACPI_FAILURE(status)) 112 return -ENODEV; 113 114 acpi_set_device_status(device, sta); 115 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); 119 } 120 121 pr_debug("Device [%s] status [%08x]\n", device->pnp.bus_id, (u32)sta); 122 return 0; 123 } 124 EXPORT_SYMBOL(acpi_bus_get_status); 125 126 void acpi_bus_private_data_handler(acpi_handle handle, 127 void *context) 128 { 129 return; 130 } 131 EXPORT_SYMBOL(acpi_bus_private_data_handler); 132 133 int acpi_bus_attach_private_data(acpi_handle handle, void *data) 134 { 135 acpi_status status; 136 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"); 141 return -ENODEV; 142 } 143 144 return 0; 145 } 146 EXPORT_SYMBOL_GPL(acpi_bus_attach_private_data); 147 148 int acpi_bus_get_private_data(acpi_handle handle, void **data) 149 { 150 acpi_status status; 151 152 if (!data) 153 return -EINVAL; 154 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"); 158 return -ENODEV; 159 } 160 161 return 0; 162 } 163 EXPORT_SYMBOL_GPL(acpi_bus_get_private_data); 164 165 void acpi_bus_detach_private_data(acpi_handle handle) 166 { 167 acpi_detach_data(handle, acpi_bus_private_data_handler); 168 } 169 EXPORT_SYMBOL_GPL(acpi_bus_detach_private_data); 170 171 static void acpi_print_osc_error(acpi_handle handle, 172 struct acpi_osc_context *context, char *error) 173 { 174 int i; 175 176 acpi_handle_debug(handle, "(%s): %s\n", context->uuid_str, error); 177 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))); 181 182 pr_debug("\n"); 183 } 184 185 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context) 186 { 187 acpi_status status; 188 struct acpi_object_list input; 189 union acpi_object in_params[4]; 190 union acpi_object *out_obj; 191 guid_t guid; 192 u32 errors; 193 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL}; 194 195 if (!context) 196 return AE_ERROR; 197 if (guid_parse(context->uuid_str, &guid)) 198 return AE_ERROR; 199 context->ret.length = ACPI_ALLOCATE_BUFFER; 200 context->ret.pointer = NULL; 201 202 /* Setting up input parameters */ 203 input.count = 4; 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; 215 216 status = acpi_evaluate_object(handle, "_OSC", &input, &output); 217 if (ACPI_FAILURE(status)) 218 return status; 219 220 if (!output.length) 221 return AE_NULL_OBJECT; 222 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"); 228 status = AE_TYPE; 229 goto out_kfree; 230 } 231 /* Need to ignore the bit0 in result code */ 232 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0); 233 if (errors) { 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] 245 & OSC_QUERY_ENABLE) 246 goto out_success; 247 status = AE_SUPPORT; 248 goto out_kfree; 249 } 250 status = AE_ERROR; 251 goto out_kfree; 252 } 253 out_success: 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; 259 goto out_kfree; 260 } 261 status = AE_OK; 262 263 out_kfree: 264 kfree(output.pointer); 265 if (status != AE_OK) 266 context->ret.pointer = NULL; 267 return status; 268 } 269 EXPORT_SYMBOL(acpi_run_osc); 270 271 bool osc_sb_apei_support_acked; 272 273 /* 274 * ACPI 6.0 Section 8.4.4.2 Idle State Coordination 275 * OSPM supports platform coordinated low power idle(LPI) states 276 */ 277 bool osc_pc_lpi_support_confirmed; 278 EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed); 279 280 /* 281 * ACPI 6.4 Operating System Capabilities for USB. 282 */ 283 bool osc_sb_native_usb4_support_confirmed; 284 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed); 285 286 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48"; 287 static void acpi_bus_osc_negotiate_platform_control(void) 288 { 289 u32 capbuf[2], *capbuf_ret; 290 struct acpi_osc_context context = { 291 .uuid_str = sb_uuid_str, 292 .rev = 1, 293 .cap.length = 8, 294 .cap.pointer = capbuf, 295 }; 296 acpi_handle handle; 297 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; 304 305 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT; 306 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PCLPI_SUPPORT; 307 308 #ifdef CONFIG_ARM64 309 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT; 310 #endif 311 #ifdef CONFIG_X86 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; 316 } 317 #endif 318 319 if (IS_ENABLED(CONFIG_SCHED_MC_PRIO)) 320 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_DIVERSE_HIGH_SUPPORT; 321 322 if (IS_ENABLED(CONFIG_USB4)) 323 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_NATIVE_USB4_SUPPORT; 324 325 if (!ghes_disable) 326 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_APEI_SUPPORT; 327 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))) 328 return; 329 330 if (ACPI_FAILURE(acpi_run_osc(handle, &context))) 331 return; 332 333 capbuf_ret = context.ret.pointer; 334 if (context.ret.length <= OSC_SUPPORT_DWORD) { 335 kfree(context.ret.pointer); 336 return; 337 } 338 339 /* 340 * Now run _OSC again with query flag clear and with the caps 341 * supported by both the OS and the platform. 342 */ 343 capbuf[OSC_QUERY_DWORD] = 0; 344 capbuf[OSC_SUPPORT_DWORD] = capbuf_ret[OSC_SUPPORT_DWORD]; 345 kfree(context.ret.pointer); 346 347 if (ACPI_FAILURE(acpi_run_osc(handle, &context))) 348 return; 349 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; 358 } 359 360 kfree(context.ret.pointer); 361 } 362 363 /* 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 366 * manager. 367 */ 368 u32 osc_sb_native_usb4_control; 369 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control); 370 371 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits) 372 { 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) ? '+' : '-'); 378 } 379 380 static u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A"; 381 static void acpi_bus_osc_negotiate_usb_control(void) 382 { 383 u32 capbuf[3]; 384 struct acpi_osc_context context = { 385 .uuid_str = sb_usb_uuid_str, 386 .rev = 1, 387 .cap.length = sizeof(capbuf), 388 .cap.pointer = capbuf, 389 }; 390 acpi_handle handle; 391 acpi_status status; 392 u32 control; 393 394 if (!osc_sb_native_usb4_support_confirmed) 395 return; 396 397 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))) 398 return; 399 400 control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING | 401 OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN; 402 403 capbuf[OSC_QUERY_DWORD] = 0; 404 capbuf[OSC_SUPPORT_DWORD] = 0; 405 capbuf[OSC_CONTROL_DWORD] = control; 406 407 status = acpi_run_osc(handle, &context); 408 if (ACPI_FAILURE(status)) 409 return; 410 411 if (context.ret.length != sizeof(capbuf)) { 412 printk(KERN_INFO PREFIX "USB4 _OSC: returned invalid length buffer\n"); 413 goto out_free; 414 } 415 416 osc_sb_native_usb4_control = 417 control & ((u32 *)context.ret.pointer)[OSC_CONTROL_DWORD]; 418 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); 422 423 out_free: 424 kfree(context.ret.pointer); 425 } 426 427 /* -------------------------------------------------------------------------- 428 Notification Handling 429 -------------------------------------------------------------------------- */ 430 431 /** 432 * acpi_bus_notify 433 * --------------- 434 * Callback for all 'system-level' device notifications (values 0x00-0x7F). 435 */ 436 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data) 437 { 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; 442 443 switch (type) { 444 case ACPI_NOTIFY_BUS_CHECK: 445 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n"); 446 hotplug_event = true; 447 break; 448 449 case ACPI_NOTIFY_DEVICE_CHECK: 450 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n"); 451 hotplug_event = true; 452 break; 453 454 case ACPI_NOTIFY_DEVICE_WAKE: 455 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n"); 456 break; 457 458 case ACPI_NOTIFY_EJECT_REQUEST: 459 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n"); 460 hotplug_event = true; 461 break; 462 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? */ 466 break; 467 468 case ACPI_NOTIFY_FREQUENCY_MISMATCH: 469 acpi_handle_err(handle, "Device cannot be configured due " 470 "to a frequency mismatch\n"); 471 break; 472 473 case ACPI_NOTIFY_BUS_MODE_MISMATCH: 474 acpi_handle_err(handle, "Device cannot be configured due " 475 "to a bus mode mismatch\n"); 476 break; 477 478 case ACPI_NOTIFY_POWER_FAULT: 479 acpi_handle_err(handle, "Device has suffered a power fault\n"); 480 break; 481 482 default: 483 acpi_handle_debug(handle, "Unknown event type 0x%x\n", type); 484 break; 485 } 486 487 adev = acpi_bus_get_acpi_device(handle); 488 if (!adev) 489 goto err; 490 491 driver = adev->driver; 492 if (driver && driver->ops.notify && 493 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS)) 494 driver->ops.notify(adev, type); 495 496 if (!hotplug_event) { 497 acpi_bus_put_acpi_device(adev); 498 return; 499 } 500 501 if (ACPI_SUCCESS(acpi_hotplug_schedule(adev, type))) 502 return; 503 504 acpi_bus_put_acpi_device(adev); 505 506 err: 507 acpi_evaluate_ost(handle, type, ost_code, NULL); 508 } 509 510 static void acpi_device_notify(acpi_handle handle, u32 event, void *data) 511 { 512 struct acpi_device *device = data; 513 514 device->driver->ops.notify(device, event); 515 } 516 517 static void acpi_device_notify_fixed(void *data) 518 { 519 struct acpi_device *device = data; 520 521 /* Fixed hardware devices have no handles */ 522 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device); 523 } 524 525 static u32 acpi_device_fixed_event(void *data) 526 { 527 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data); 528 return ACPI_INTERRUPT_HANDLED; 529 } 530 531 static int acpi_device_install_notify_handler(struct acpi_device *device) 532 { 533 acpi_status status; 534 535 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) 536 status = 537 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, 538 acpi_device_fixed_event, 539 device); 540 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) 541 status = 542 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, 543 acpi_device_fixed_event, 544 device); 545 else 546 status = acpi_install_notify_handler(device->handle, 547 ACPI_DEVICE_NOTIFY, 548 acpi_device_notify, 549 device); 550 551 if (ACPI_FAILURE(status)) 552 return -EINVAL; 553 return 0; 554 } 555 556 static void acpi_device_remove_notify_handler(struct acpi_device *device) 557 { 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); 564 else 565 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY, 566 acpi_device_notify); 567 } 568 569 /* Handle events targeting \_SB device (at present only graceful shutdown) */ 570 571 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81 572 #define ACPI_SB_INDICATE_INTERVAL 10000 573 574 static void sb_notify_work(struct work_struct *dummy) 575 { 576 acpi_handle sb_handle; 577 578 orderly_poweroff(true); 579 580 /* 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 584 */ 585 acpi_get_handle(NULL, "\\_SB", &sb_handle); 586 while (1) { 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); 591 } 592 } 593 594 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data) 595 { 596 static DECLARE_WORK(acpi_sb_work, sb_notify_work); 597 598 if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) { 599 if (!work_busy(&acpi_sb_work)) 600 schedule_work(&acpi_sb_work); 601 } else 602 pr_warn("event %x is not supported by \\_SB device\n", event); 603 } 604 605 static int __init acpi_setup_sb_notify_handler(void) 606 { 607 acpi_handle sb_handle; 608 609 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle))) 610 return -ENXIO; 611 612 if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY, 613 acpi_sb_notify, NULL))) 614 return -EINVAL; 615 616 return 0; 617 } 618 619 /* -------------------------------------------------------------------------- 620 Device Matching 621 -------------------------------------------------------------------------- */ 622 623 /** 624 * acpi_get_first_physical_node - Get first physical node of an ACPI device 625 * @adev: ACPI device in question 626 * 627 * Return: First physical node of ACPI device @adev 628 */ 629 struct device *acpi_get_first_physical_node(struct acpi_device *adev) 630 { 631 struct mutex *physical_node_lock = &adev->physical_node_lock; 632 struct device *phys_dev; 633 634 mutex_lock(physical_node_lock); 635 if (list_empty(&adev->physical_node_list)) { 636 phys_dev = NULL; 637 } else { 638 const struct acpi_device_physical_node *node; 639 640 node = list_first_entry(&adev->physical_node_list, 641 struct acpi_device_physical_node, node); 642 643 phys_dev = node->dev; 644 } 645 mutex_unlock(physical_node_lock); 646 return phys_dev; 647 } 648 EXPORT_SYMBOL_GPL(acpi_get_first_physical_node); 649 650 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev, 651 const struct device *dev) 652 { 653 const struct device *phys_dev = acpi_get_first_physical_node(adev); 654 655 return phys_dev && phys_dev == dev ? adev : NULL; 656 } 657 658 /** 659 * acpi_device_is_first_physical_node - Is given dev first physical node 660 * @adev: ACPI companion device 661 * @dev: Physical device to check 662 * 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. 666 * 667 * Note that the caller have to provide valid @adev pointer. 668 */ 669 bool acpi_device_is_first_physical_node(struct acpi_device *adev, 670 const struct device *dev) 671 { 672 return !!acpi_primary_dev_companion(adev, dev); 673 } 674 675 /* 676 * acpi_companion_match() - Can we match via ACPI companion device 677 * @dev: Device in question 678 * 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 682 * or NULL otherwise. 683 * 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 690 * IDs. 691 * 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). 695 */ 696 struct acpi_device *acpi_companion_match(const struct device *dev) 697 { 698 struct acpi_device *adev; 699 700 adev = ACPI_COMPANION(dev); 701 if (!adev) 702 return NULL; 703 704 if (list_empty(&adev->pnp.ids)) 705 return NULL; 706 707 return acpi_primary_dev_companion(adev, dev); 708 } 709 710 /** 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 715 * 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. 719 */ 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) 723 { 724 const union acpi_object *of_compatible, *obj; 725 int i, nval; 726 727 if (!adev) 728 return false; 729 730 of_compatible = adev->data.of_compatible; 731 if (!of_match_table || !of_compatible) 732 return false; 733 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. */ 738 nval = 1; 739 obj = of_compatible; 740 } 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; 744 745 for (id = of_match_table; id->compatible[0]; id++) 746 if (!strcasecmp(obj->string.pointer, id->compatible)) { 747 if (of_id) 748 *of_id = id; 749 return true; 750 } 751 } 752 753 return false; 754 } 755 756 static bool acpi_of_modalias(struct acpi_device *adev, 757 char *modalias, size_t len) 758 { 759 const union acpi_object *of_compatible; 760 const union acpi_object *obj; 761 const char *str, *chr; 762 763 of_compatible = adev->data.of_compatible; 764 if (!of_compatible) 765 return false; 766 767 if (of_compatible->type == ACPI_TYPE_PACKAGE) 768 obj = of_compatible->package.elements; 769 else /* Must be ACPI_TYPE_STRING. */ 770 obj = of_compatible; 771 772 str = obj->string.pointer; 773 chr = strchr(str, ','); 774 strlcpy(modalias, chr ? chr + 1 : str, len); 775 776 return true; 777 } 778 779 /** 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 785 * 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. 789 */ 790 void acpi_set_modalias(struct acpi_device *adev, const char *default_id, 791 char *modalias, size_t len) 792 { 793 if (!acpi_of_modalias(adev, modalias, len)) 794 strlcpy(modalias, default_id, len); 795 } 796 EXPORT_SYMBOL_GPL(acpi_set_modalias); 797 798 static bool __acpi_match_device_cls(const struct acpi_device_id *id, 799 struct acpi_hardware_id *hwid) 800 { 801 int i, msk, byte_shift; 802 char buf[3]; 803 804 if (!id->cls) 805 return false; 806 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; 811 if (!msk) 812 continue; 813 814 sprintf(buf, "%02x", (id->cls >> byte_shift) & msk); 815 if (strncmp(buf, &hwid->id[(i - 1) * 2], 2)) 816 return false; 817 } 818 return true; 819 } 820 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) 826 { 827 const struct acpi_device_id *id; 828 struct acpi_hardware_id *hwid; 829 830 /* 831 * If the device is not present, it is unnecessary to load device 832 * driver for it. 833 */ 834 if (!device || !device->status.present) 835 return false; 836 837 list_for_each_entry(hwid, &device->pnp.ids, list) { 838 /* First, check the ACPI/PNP IDs provided by the caller. */ 839 if (acpi_ids) { 840 for (id = acpi_ids; id->id[0] || id->cls; id++) { 841 if (id->id[0] && !strcmp((char *)id->id, hwid->id)) 842 goto out_acpi_match; 843 if (id->cls && __acpi_match_device_cls(id, hwid)) 844 goto out_acpi_match; 845 } 846 } 847 848 /* 849 * Next, check ACPI_DT_NAMESPACE_HID and try to match the 850 * "compatible" property if found. 851 */ 852 if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id)) 853 return acpi_of_match_device(device, of_ids, of_id); 854 } 855 return false; 856 857 out_acpi_match: 858 if (acpi_id) 859 *acpi_id = id; 860 return true; 861 } 862 863 /** 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. 867 * 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 870 * device IDs. 871 * 872 * Return a pointer to the first matching ID on success or %NULL on failure. 873 */ 874 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids, 875 const struct device *dev) 876 { 877 const struct acpi_device_id *id = NULL; 878 879 __acpi_match_device(acpi_companion_match(dev), ids, NULL, &id, NULL); 880 return id; 881 } 882 EXPORT_SYMBOL_GPL(acpi_match_device); 883 884 static const void *acpi_of_device_get_match_data(const struct device *dev) 885 { 886 struct acpi_device *adev = ACPI_COMPANION(dev); 887 const struct of_device_id *match = NULL; 888 889 if (!acpi_of_match_device(adev, dev->driver->of_match_table, &match)) 890 return NULL; 891 892 return match->data; 893 } 894 895 const void *acpi_device_get_match_data(const struct device *dev) 896 { 897 const struct acpi_device_id *match; 898 899 if (!dev->driver->acpi_match_table) 900 return acpi_of_device_get_match_data(dev); 901 902 match = acpi_match_device(dev->driver->acpi_match_table, dev); 903 if (!match) 904 return NULL; 905 906 return (const void *)match->driver_data; 907 } 908 EXPORT_SYMBOL_GPL(acpi_device_get_match_data); 909 910 int acpi_match_device_ids(struct acpi_device *device, 911 const struct acpi_device_id *ids) 912 { 913 return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT; 914 } 915 EXPORT_SYMBOL(acpi_match_device_ids); 916 917 bool acpi_driver_match_device(struct device *dev, 918 const struct device_driver *drv) 919 { 920 if (!drv->acpi_match_table) 921 return acpi_of_match_device(ACPI_COMPANION(dev), 922 drv->of_match_table, 923 NULL); 924 925 return __acpi_match_device(acpi_companion_match(dev), 926 drv->acpi_match_table, drv->of_match_table, 927 NULL, NULL); 928 } 929 EXPORT_SYMBOL_GPL(acpi_driver_match_device); 930 931 /* -------------------------------------------------------------------------- 932 ACPI Driver Management 933 -------------------------------------------------------------------------- */ 934 935 /** 936 * acpi_bus_register_driver - register a driver with the ACPI bus 937 * @driver: driver being registered 938 * 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. 942 */ 943 int acpi_bus_register_driver(struct acpi_driver *driver) 944 { 945 int ret; 946 947 if (acpi_disabled) 948 return -ENODEV; 949 driver->drv.name = driver->name; 950 driver->drv.bus = &acpi_bus_type; 951 driver->drv.owner = driver->owner; 952 953 ret = driver_register(&driver->drv); 954 return ret; 955 } 956 957 EXPORT_SYMBOL(acpi_bus_register_driver); 958 959 /** 960 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus 961 * @driver: driver to unregister 962 * 963 * Unregisters a driver with the ACPI bus. Searches the namespace for all 964 * devices that match the driver's criteria and unbinds. 965 */ 966 void acpi_bus_unregister_driver(struct acpi_driver *driver) 967 { 968 driver_unregister(&driver->drv); 969 } 970 971 EXPORT_SYMBOL(acpi_bus_unregister_driver); 972 973 /* -------------------------------------------------------------------------- 974 ACPI Bus operations 975 -------------------------------------------------------------------------- */ 976 977 static int acpi_bus_match(struct device *dev, struct device_driver *drv) 978 { 979 struct acpi_device *acpi_dev = to_acpi_device(dev); 980 struct acpi_driver *acpi_drv = to_acpi_driver(drv); 981 982 return acpi_dev->flags.match_driver 983 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids); 984 } 985 986 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) 987 { 988 return __acpi_device_uevent_modalias(to_acpi_device(dev), env); 989 } 990 991 static int acpi_device_probe(struct device *dev) 992 { 993 struct acpi_device *acpi_dev = to_acpi_device(dev); 994 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); 995 int ret; 996 997 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev)) 998 return -EINVAL; 999 1000 if (!acpi_drv->ops.add) 1001 return -ENOSYS; 1002 1003 ret = acpi_drv->ops.add(acpi_dev); 1004 if (ret) 1005 return ret; 1006 1007 acpi_dev->driver = acpi_drv; 1008 1009 pr_debug("Driver [%s] successfully bound to device [%s]\n", 1010 acpi_drv->name, acpi_dev->pnp.bus_id); 1011 1012 if (acpi_drv->ops.notify) { 1013 ret = acpi_device_install_notify_handler(acpi_dev); 1014 if (ret) { 1015 if (acpi_drv->ops.remove) 1016 acpi_drv->ops.remove(acpi_dev); 1017 1018 acpi_dev->driver = NULL; 1019 acpi_dev->driver_data = NULL; 1020 return ret; 1021 } 1022 } 1023 1024 pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name, 1025 acpi_dev->pnp.bus_id); 1026 1027 get_device(dev); 1028 return 0; 1029 } 1030 1031 static int acpi_device_remove(struct device *dev) 1032 { 1033 struct acpi_device *acpi_dev = to_acpi_device(dev); 1034 struct acpi_driver *acpi_drv = acpi_dev->driver; 1035 1036 if (acpi_drv) { 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); 1041 } 1042 acpi_dev->driver = NULL; 1043 acpi_dev->driver_data = NULL; 1044 1045 put_device(dev); 1046 return 0; 1047 } 1048 1049 struct bus_type acpi_bus_type = { 1050 .name = "acpi", 1051 .match = acpi_bus_match, 1052 .probe = acpi_device_probe, 1053 .remove = acpi_device_remove, 1054 .uevent = acpi_device_uevent, 1055 }; 1056 1057 /* -------------------------------------------------------------------------- 1058 Initialization/Cleanup 1059 -------------------------------------------------------------------------- */ 1060 1061 static int __init acpi_bus_init_irq(void) 1062 { 1063 acpi_status status; 1064 char *message = NULL; 1065 1066 1067 /* 1068 * Let the system know what interrupt model we are using by 1069 * evaluating the \_PIC object, if exists. 1070 */ 1071 1072 switch (acpi_irq_model) { 1073 case ACPI_IRQ_MODEL_PIC: 1074 message = "PIC"; 1075 break; 1076 case ACPI_IRQ_MODEL_IOAPIC: 1077 message = "IOAPIC"; 1078 break; 1079 case ACPI_IRQ_MODEL_IOSAPIC: 1080 message = "IOSAPIC"; 1081 break; 1082 case ACPI_IRQ_MODEL_GIC: 1083 message = "GIC"; 1084 break; 1085 case ACPI_IRQ_MODEL_PLATFORM: 1086 message = "platform specific model"; 1087 break; 1088 default: 1089 pr_info("Unknown interrupt routing model\n"); 1090 return -ENODEV; 1091 } 1092 1093 pr_info("Using %s for interrupt routing\n", message); 1094 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)); 1098 return -ENODEV; 1099 } 1100 1101 return 0; 1102 } 1103 1104 /** 1105 * acpi_early_init - Initialize ACPICA and populate the ACPI namespace. 1106 * 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. 1110 * 1111 * Doing this before switching the EFI runtime services to virtual mode allows 1112 * the EfiBootServices memory to be freed slightly earlier on boot. 1113 */ 1114 void __init acpi_early_init(void) 1115 { 1116 acpi_status status; 1117 1118 if (acpi_disabled) 1119 return; 1120 1121 pr_info("Core revision %08x\n", ACPI_CA_VERSION); 1122 1123 /* enable workarounds, unless strict ACPI spec. compliance */ 1124 if (!acpi_strict) 1125 acpi_gbl_enable_interpreter_slack = TRUE; 1126 1127 acpi_permanent_mmap = true; 1128 1129 #ifdef CONFIG_X86 1130 /* 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. 1136 */ 1137 dmi_check_system(dsdt_dmi_table); 1138 #endif 1139 1140 status = acpi_reallocate_root_table(); 1141 if (ACPI_FAILURE(status)) { 1142 pr_err("Unable to reallocate ACPI tables\n"); 1143 goto error0; 1144 } 1145 1146 status = acpi_initialize_subsystem(); 1147 if (ACPI_FAILURE(status)) { 1148 pr_err("Unable to initialize the ACPI Interpreter\n"); 1149 goto error0; 1150 } 1151 1152 #ifdef CONFIG_X86 1153 if (!acpi_ioapic) { 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; 1158 } 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); 1162 } else { 1163 /* 1164 * now that acpi_gbl_FADT is initialized, 1165 * update it with result from INT_SRC_OVR parsing 1166 */ 1167 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi; 1168 } 1169 #endif 1170 return; 1171 1172 error0: 1173 disable_acpi(); 1174 } 1175 1176 /** 1177 * acpi_subsystem_init - Finalize the early initialization of ACPI. 1178 * 1179 * Switch over the platform to the ACPI mode (if possible). 1180 * 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. 1184 */ 1185 void __init acpi_subsystem_init(void) 1186 { 1187 acpi_status status; 1188 1189 if (acpi_disabled) 1190 return; 1191 1192 status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE); 1193 if (ACPI_FAILURE(status)) { 1194 pr_err("Unable to enable ACPI\n"); 1195 disable_acpi(); 1196 } else { 1197 /* 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 1201 * know. 1202 */ 1203 regulator_has_full_constraints(); 1204 } 1205 } 1206 1207 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context) 1208 { 1209 acpi_scan_table_handler(event, table, context); 1210 1211 return acpi_sysfs_table_handler(event, table, context); 1212 } 1213 1214 static int __init acpi_bus_init(void) 1215 { 1216 int result; 1217 acpi_status status; 1218 1219 acpi_os_initialize1(); 1220 1221 status = acpi_load_tables(); 1222 if (ACPI_FAILURE(status)) { 1223 pr_err("Unable to load the System Description Tables\n"); 1224 goto error1; 1225 } 1226 1227 /* 1228 * ACPI 2.0 requires the EC driver to be loaded and work before the EC 1229 * device is found in the namespace. 1230 * 1231 * This is accomplished by looking for the ECDT table and getting the EC 1232 * parameters out of that. 1233 * 1234 * Do that before calling acpi_initialize_objects() which may trigger EC 1235 * address space accesses. 1236 */ 1237 acpi_ec_ecdt_probe(); 1238 1239 status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE); 1240 if (ACPI_FAILURE(status)) { 1241 pr_err("Unable to start the ACPI Interpreter\n"); 1242 goto error1; 1243 } 1244 1245 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION); 1246 if (ACPI_FAILURE(status)) { 1247 pr_err("Unable to initialize ACPI objects\n"); 1248 goto error1; 1249 } 1250 1251 /* Set capability bits for _OSC under processor scope */ 1252 acpi_early_processor_osc(); 1253 1254 /* 1255 * _OSC method may exist in module level code, 1256 * so it must be run after ACPI_FULL_INITIALIZATION 1257 */ 1258 acpi_bus_osc_negotiate_platform_control(); 1259 acpi_bus_osc_negotiate_usb_control(); 1260 1261 /* 1262 * _PDC control method may load dynamic SSDT tables, 1263 * and we need to install the table handler before that. 1264 */ 1265 status = acpi_install_table_handler(acpi_bus_table_handler, NULL); 1266 1267 acpi_sysfs_init(); 1268 1269 acpi_early_processor_set_pdc(); 1270 1271 /* 1272 * Maybe EC region is required at bus_scan/acpi_get_devices. So it 1273 * is necessary to enable it as early as possible. 1274 */ 1275 acpi_ec_dsdt_probe(); 1276 1277 pr_info("Interpreter enabled\n"); 1278 1279 /* Initialize sleep structures */ 1280 acpi_sleep_init(); 1281 1282 /* 1283 * Get the system interrupt model and evaluate \_PIC. 1284 */ 1285 result = acpi_bus_init_irq(); 1286 if (result) 1287 goto error1; 1288 1289 /* 1290 * Register the for all standard device notifications. 1291 */ 1292 status = 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"); 1297 goto error1; 1298 } 1299 1300 /* 1301 * Create the top ACPI proc directory 1302 */ 1303 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL); 1304 1305 result = bus_register(&acpi_bus_type); 1306 if (!result) 1307 return 0; 1308 1309 /* Mimic structured exception handling */ 1310 error1: 1311 acpi_terminate(); 1312 return -ENODEV; 1313 } 1314 1315 struct kobject *acpi_kobj; 1316 EXPORT_SYMBOL_GPL(acpi_kobj); 1317 1318 static int __init acpi_init(void) 1319 { 1320 int result; 1321 1322 if (acpi_disabled) { 1323 pr_info("Interpreter disabled.\n"); 1324 return -ENODEV; 1325 } 1326 1327 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj); 1328 if (!acpi_kobj) { 1329 pr_debug("%s: kset create error\n", __func__); 1330 acpi_kobj = NULL; 1331 } 1332 1333 result = acpi_bus_init(); 1334 if (result) { 1335 disable_acpi(); 1336 return result; 1337 } 1338 1339 pci_mmcfg_late_init(); 1340 acpi_iort_init(); 1341 acpi_scan_init(); 1342 acpi_ec_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(); 1348 return 0; 1349 } 1350 1351 subsys_initcall(acpi_init); 1352