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 kfree(context.ret.pointer); 334 335 /* Now run _OSC again with query flag clear */ 336 capbuf[OSC_QUERY_DWORD] = 0; 337 338 if (ACPI_FAILURE(acpi_run_osc(handle, &context))) 339 return; 340 341 capbuf_ret = context.ret.pointer; 342 osc_sb_apei_support_acked = 343 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT; 344 osc_pc_lpi_support_confirmed = 345 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_PCLPI_SUPPORT; 346 osc_sb_native_usb4_support_confirmed = 347 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_NATIVE_USB4_SUPPORT; 348 349 kfree(context.ret.pointer); 350 } 351 352 /* 353 * Native control of USB4 capabilities. If any of the tunneling bits is 354 * set it means OS is in control and we use software based connection 355 * manager. 356 */ 357 u32 osc_sb_native_usb4_control; 358 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control); 359 360 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits) 361 { 362 printk(KERN_INFO PREFIX "%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg, 363 (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-', 364 (bits & OSC_USB_DP_TUNNELING) ? '+' : '-', 365 (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-', 366 (bits & OSC_USB_XDOMAIN) ? '+' : '-'); 367 } 368 369 static u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A"; 370 static void acpi_bus_osc_negotiate_usb_control(void) 371 { 372 u32 capbuf[3]; 373 struct acpi_osc_context context = { 374 .uuid_str = sb_usb_uuid_str, 375 .rev = 1, 376 .cap.length = sizeof(capbuf), 377 .cap.pointer = capbuf, 378 }; 379 acpi_handle handle; 380 acpi_status status; 381 u32 control; 382 383 if (!osc_sb_native_usb4_support_confirmed) 384 return; 385 386 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))) 387 return; 388 389 control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING | 390 OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN; 391 392 capbuf[OSC_QUERY_DWORD] = 0; 393 capbuf[OSC_SUPPORT_DWORD] = 0; 394 capbuf[OSC_CONTROL_DWORD] = control; 395 396 status = acpi_run_osc(handle, &context); 397 if (ACPI_FAILURE(status)) 398 return; 399 400 if (context.ret.length != sizeof(capbuf)) { 401 printk(KERN_INFO PREFIX "USB4 _OSC: returned invalid length buffer\n"); 402 goto out_free; 403 } 404 405 osc_sb_native_usb4_control = 406 control & ((u32 *)context.ret.pointer)[OSC_CONTROL_DWORD]; 407 408 acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control); 409 acpi_bus_decode_usb_osc("USB4 _OSC: OS controls", 410 osc_sb_native_usb4_control); 411 412 out_free: 413 kfree(context.ret.pointer); 414 } 415 416 /* -------------------------------------------------------------------------- 417 Notification Handling 418 -------------------------------------------------------------------------- */ 419 420 /** 421 * acpi_bus_notify 422 * --------------- 423 * Callback for all 'system-level' device notifications (values 0x00-0x7F). 424 */ 425 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data) 426 { 427 struct acpi_device *adev; 428 struct acpi_driver *driver; 429 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; 430 bool hotplug_event = false; 431 432 switch (type) { 433 case ACPI_NOTIFY_BUS_CHECK: 434 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n"); 435 hotplug_event = true; 436 break; 437 438 case ACPI_NOTIFY_DEVICE_CHECK: 439 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n"); 440 hotplug_event = true; 441 break; 442 443 case ACPI_NOTIFY_DEVICE_WAKE: 444 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n"); 445 break; 446 447 case ACPI_NOTIFY_EJECT_REQUEST: 448 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n"); 449 hotplug_event = true; 450 break; 451 452 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT: 453 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n"); 454 /* TBD: Exactly what does 'light' mean? */ 455 break; 456 457 case ACPI_NOTIFY_FREQUENCY_MISMATCH: 458 acpi_handle_err(handle, "Device cannot be configured due " 459 "to a frequency mismatch\n"); 460 break; 461 462 case ACPI_NOTIFY_BUS_MODE_MISMATCH: 463 acpi_handle_err(handle, "Device cannot be configured due " 464 "to a bus mode mismatch\n"); 465 break; 466 467 case ACPI_NOTIFY_POWER_FAULT: 468 acpi_handle_err(handle, "Device has suffered a power fault\n"); 469 break; 470 471 default: 472 acpi_handle_debug(handle, "Unknown event type 0x%x\n", type); 473 break; 474 } 475 476 adev = acpi_bus_get_acpi_device(handle); 477 if (!adev) 478 goto err; 479 480 driver = adev->driver; 481 if (driver && driver->ops.notify && 482 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS)) 483 driver->ops.notify(adev, type); 484 485 if (!hotplug_event) { 486 acpi_bus_put_acpi_device(adev); 487 return; 488 } 489 490 if (ACPI_SUCCESS(acpi_hotplug_schedule(adev, type))) 491 return; 492 493 acpi_bus_put_acpi_device(adev); 494 495 err: 496 acpi_evaluate_ost(handle, type, ost_code, NULL); 497 } 498 499 static void acpi_device_notify(acpi_handle handle, u32 event, void *data) 500 { 501 struct acpi_device *device = data; 502 503 device->driver->ops.notify(device, event); 504 } 505 506 static void acpi_device_notify_fixed(void *data) 507 { 508 struct acpi_device *device = data; 509 510 /* Fixed hardware devices have no handles */ 511 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device); 512 } 513 514 static u32 acpi_device_fixed_event(void *data) 515 { 516 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data); 517 return ACPI_INTERRUPT_HANDLED; 518 } 519 520 static int acpi_device_install_notify_handler(struct acpi_device *device) 521 { 522 acpi_status status; 523 524 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) 525 status = 526 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, 527 acpi_device_fixed_event, 528 device); 529 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) 530 status = 531 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, 532 acpi_device_fixed_event, 533 device); 534 else 535 status = acpi_install_notify_handler(device->handle, 536 ACPI_DEVICE_NOTIFY, 537 acpi_device_notify, 538 device); 539 540 if (ACPI_FAILURE(status)) 541 return -EINVAL; 542 return 0; 543 } 544 545 static void acpi_device_remove_notify_handler(struct acpi_device *device) 546 { 547 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) 548 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, 549 acpi_device_fixed_event); 550 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) 551 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, 552 acpi_device_fixed_event); 553 else 554 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY, 555 acpi_device_notify); 556 } 557 558 /* Handle events targeting \_SB device (at present only graceful shutdown) */ 559 560 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81 561 #define ACPI_SB_INDICATE_INTERVAL 10000 562 563 static void sb_notify_work(struct work_struct *dummy) 564 { 565 acpi_handle sb_handle; 566 567 orderly_poweroff(true); 568 569 /* 570 * After initiating graceful shutdown, the ACPI spec requires OSPM 571 * to evaluate _OST method once every 10seconds to indicate that 572 * the shutdown is in progress 573 */ 574 acpi_get_handle(NULL, "\\_SB", &sb_handle); 575 while (1) { 576 pr_info("Graceful shutdown in progress.\n"); 577 acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN, 578 ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL); 579 msleep(ACPI_SB_INDICATE_INTERVAL); 580 } 581 } 582 583 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data) 584 { 585 static DECLARE_WORK(acpi_sb_work, sb_notify_work); 586 587 if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) { 588 if (!work_busy(&acpi_sb_work)) 589 schedule_work(&acpi_sb_work); 590 } else 591 pr_warn("event %x is not supported by \\_SB device\n", event); 592 } 593 594 static int __init acpi_setup_sb_notify_handler(void) 595 { 596 acpi_handle sb_handle; 597 598 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle))) 599 return -ENXIO; 600 601 if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY, 602 acpi_sb_notify, NULL))) 603 return -EINVAL; 604 605 return 0; 606 } 607 608 /* -------------------------------------------------------------------------- 609 Device Matching 610 -------------------------------------------------------------------------- */ 611 612 /** 613 * acpi_get_first_physical_node - Get first physical node of an ACPI device 614 * @adev: ACPI device in question 615 * 616 * Return: First physical node of ACPI device @adev 617 */ 618 struct device *acpi_get_first_physical_node(struct acpi_device *adev) 619 { 620 struct mutex *physical_node_lock = &adev->physical_node_lock; 621 struct device *phys_dev; 622 623 mutex_lock(physical_node_lock); 624 if (list_empty(&adev->physical_node_list)) { 625 phys_dev = NULL; 626 } else { 627 const struct acpi_device_physical_node *node; 628 629 node = list_first_entry(&adev->physical_node_list, 630 struct acpi_device_physical_node, node); 631 632 phys_dev = node->dev; 633 } 634 mutex_unlock(physical_node_lock); 635 return phys_dev; 636 } 637 EXPORT_SYMBOL_GPL(acpi_get_first_physical_node); 638 639 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev, 640 const struct device *dev) 641 { 642 const struct device *phys_dev = acpi_get_first_physical_node(adev); 643 644 return phys_dev && phys_dev == dev ? adev : NULL; 645 } 646 647 /** 648 * acpi_device_is_first_physical_node - Is given dev first physical node 649 * @adev: ACPI companion device 650 * @dev: Physical device to check 651 * 652 * Function checks if given @dev is the first physical devices attached to 653 * the ACPI companion device. This distinction is needed in some cases 654 * where the same companion device is shared between many physical devices. 655 * 656 * Note that the caller have to provide valid @adev pointer. 657 */ 658 bool acpi_device_is_first_physical_node(struct acpi_device *adev, 659 const struct device *dev) 660 { 661 return !!acpi_primary_dev_companion(adev, dev); 662 } 663 664 /* 665 * acpi_companion_match() - Can we match via ACPI companion device 666 * @dev: Device in question 667 * 668 * Check if the given device has an ACPI companion and if that companion has 669 * a valid list of PNP IDs, and if the device is the first (primary) physical 670 * device associated with it. Return the companion pointer if that's the case 671 * or NULL otherwise. 672 * 673 * If multiple physical devices are attached to a single ACPI companion, we need 674 * to be careful. The usage scenario for this kind of relationship is that all 675 * of the physical devices in question use resources provided by the ACPI 676 * companion. A typical case is an MFD device where all the sub-devices share 677 * the parent's ACPI companion. In such cases we can only allow the primary 678 * (first) physical device to be matched with the help of the companion's PNP 679 * IDs. 680 * 681 * Additional physical devices sharing the ACPI companion can still use 682 * resources available from it but they will be matched normally using functions 683 * provided by their bus types (and analogously for their modalias). 684 */ 685 struct acpi_device *acpi_companion_match(const struct device *dev) 686 { 687 struct acpi_device *adev; 688 689 adev = ACPI_COMPANION(dev); 690 if (!adev) 691 return NULL; 692 693 if (list_empty(&adev->pnp.ids)) 694 return NULL; 695 696 return acpi_primary_dev_companion(adev, dev); 697 } 698 699 /** 700 * acpi_of_match_device - Match device object using the "compatible" property. 701 * @adev: ACPI device object to match. 702 * @of_match_table: List of device IDs to match against. 703 * @of_id: OF ID if matched 704 * 705 * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of 706 * identifiers and a _DSD object with the "compatible" property, use that 707 * property to match against the given list of identifiers. 708 */ 709 static bool acpi_of_match_device(struct acpi_device *adev, 710 const struct of_device_id *of_match_table, 711 const struct of_device_id **of_id) 712 { 713 const union acpi_object *of_compatible, *obj; 714 int i, nval; 715 716 if (!adev) 717 return false; 718 719 of_compatible = adev->data.of_compatible; 720 if (!of_match_table || !of_compatible) 721 return false; 722 723 if (of_compatible->type == ACPI_TYPE_PACKAGE) { 724 nval = of_compatible->package.count; 725 obj = of_compatible->package.elements; 726 } else { /* Must be ACPI_TYPE_STRING. */ 727 nval = 1; 728 obj = of_compatible; 729 } 730 /* Now we can look for the driver DT compatible strings */ 731 for (i = 0; i < nval; i++, obj++) { 732 const struct of_device_id *id; 733 734 for (id = of_match_table; id->compatible[0]; id++) 735 if (!strcasecmp(obj->string.pointer, id->compatible)) { 736 if (of_id) 737 *of_id = id; 738 return true; 739 } 740 } 741 742 return false; 743 } 744 745 static bool acpi_of_modalias(struct acpi_device *adev, 746 char *modalias, size_t len) 747 { 748 const union acpi_object *of_compatible; 749 const union acpi_object *obj; 750 const char *str, *chr; 751 752 of_compatible = adev->data.of_compatible; 753 if (!of_compatible) 754 return false; 755 756 if (of_compatible->type == ACPI_TYPE_PACKAGE) 757 obj = of_compatible->package.elements; 758 else /* Must be ACPI_TYPE_STRING. */ 759 obj = of_compatible; 760 761 str = obj->string.pointer; 762 chr = strchr(str, ','); 763 strlcpy(modalias, chr ? chr + 1 : str, len); 764 765 return true; 766 } 767 768 /** 769 * acpi_set_modalias - Set modalias using "compatible" property or supplied ID 770 * @adev: ACPI device object to match 771 * @default_id: ID string to use as default if no compatible string found 772 * @modalias: Pointer to buffer that modalias value will be copied into 773 * @len: Length of modalias buffer 774 * 775 * This is a counterpart of of_modalias_node() for struct acpi_device objects. 776 * If there is a compatible string for @adev, it will be copied to @modalias 777 * with the vendor prefix stripped; otherwise, @default_id will be used. 778 */ 779 void acpi_set_modalias(struct acpi_device *adev, const char *default_id, 780 char *modalias, size_t len) 781 { 782 if (!acpi_of_modalias(adev, modalias, len)) 783 strlcpy(modalias, default_id, len); 784 } 785 EXPORT_SYMBOL_GPL(acpi_set_modalias); 786 787 static bool __acpi_match_device_cls(const struct acpi_device_id *id, 788 struct acpi_hardware_id *hwid) 789 { 790 int i, msk, byte_shift; 791 char buf[3]; 792 793 if (!id->cls) 794 return false; 795 796 /* Apply class-code bitmask, before checking each class-code byte */ 797 for (i = 1; i <= 3; i++) { 798 byte_shift = 8 * (3 - i); 799 msk = (id->cls_msk >> byte_shift) & 0xFF; 800 if (!msk) 801 continue; 802 803 sprintf(buf, "%02x", (id->cls >> byte_shift) & msk); 804 if (strncmp(buf, &hwid->id[(i - 1) * 2], 2)) 805 return false; 806 } 807 return true; 808 } 809 810 static bool __acpi_match_device(struct acpi_device *device, 811 const struct acpi_device_id *acpi_ids, 812 const struct of_device_id *of_ids, 813 const struct acpi_device_id **acpi_id, 814 const struct of_device_id **of_id) 815 { 816 const struct acpi_device_id *id; 817 struct acpi_hardware_id *hwid; 818 819 /* 820 * If the device is not present, it is unnecessary to load device 821 * driver for it. 822 */ 823 if (!device || !device->status.present) 824 return false; 825 826 list_for_each_entry(hwid, &device->pnp.ids, list) { 827 /* First, check the ACPI/PNP IDs provided by the caller. */ 828 if (acpi_ids) { 829 for (id = acpi_ids; id->id[0] || id->cls; id++) { 830 if (id->id[0] && !strcmp((char *)id->id, hwid->id)) 831 goto out_acpi_match; 832 if (id->cls && __acpi_match_device_cls(id, hwid)) 833 goto out_acpi_match; 834 } 835 } 836 837 /* 838 * Next, check ACPI_DT_NAMESPACE_HID and try to match the 839 * "compatible" property if found. 840 */ 841 if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id)) 842 return acpi_of_match_device(device, of_ids, of_id); 843 } 844 return false; 845 846 out_acpi_match: 847 if (acpi_id) 848 *acpi_id = id; 849 return true; 850 } 851 852 /** 853 * acpi_match_device - Match a struct device against a given list of ACPI IDs 854 * @ids: Array of struct acpi_device_id object to match against. 855 * @dev: The device structure to match. 856 * 857 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device 858 * object for that handle and use that object to match against a given list of 859 * device IDs. 860 * 861 * Return a pointer to the first matching ID on success or %NULL on failure. 862 */ 863 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids, 864 const struct device *dev) 865 { 866 const struct acpi_device_id *id = NULL; 867 868 __acpi_match_device(acpi_companion_match(dev), ids, NULL, &id, NULL); 869 return id; 870 } 871 EXPORT_SYMBOL_GPL(acpi_match_device); 872 873 static const void *acpi_of_device_get_match_data(const struct device *dev) 874 { 875 struct acpi_device *adev = ACPI_COMPANION(dev); 876 const struct of_device_id *match = NULL; 877 878 if (!acpi_of_match_device(adev, dev->driver->of_match_table, &match)) 879 return NULL; 880 881 return match->data; 882 } 883 884 const void *acpi_device_get_match_data(const struct device *dev) 885 { 886 const struct acpi_device_id *match; 887 888 if (!dev->driver->acpi_match_table) 889 return acpi_of_device_get_match_data(dev); 890 891 match = acpi_match_device(dev->driver->acpi_match_table, dev); 892 if (!match) 893 return NULL; 894 895 return (const void *)match->driver_data; 896 } 897 EXPORT_SYMBOL_GPL(acpi_device_get_match_data); 898 899 int acpi_match_device_ids(struct acpi_device *device, 900 const struct acpi_device_id *ids) 901 { 902 return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT; 903 } 904 EXPORT_SYMBOL(acpi_match_device_ids); 905 906 bool acpi_driver_match_device(struct device *dev, 907 const struct device_driver *drv) 908 { 909 if (!drv->acpi_match_table) 910 return acpi_of_match_device(ACPI_COMPANION(dev), 911 drv->of_match_table, 912 NULL); 913 914 return __acpi_match_device(acpi_companion_match(dev), 915 drv->acpi_match_table, drv->of_match_table, 916 NULL, NULL); 917 } 918 EXPORT_SYMBOL_GPL(acpi_driver_match_device); 919 920 /* -------------------------------------------------------------------------- 921 ACPI Driver Management 922 -------------------------------------------------------------------------- */ 923 924 /** 925 * acpi_bus_register_driver - register a driver with the ACPI bus 926 * @driver: driver being registered 927 * 928 * Registers a driver with the ACPI bus. Searches the namespace for all 929 * devices that match the driver's criteria and binds. Returns zero for 930 * success or a negative error status for failure. 931 */ 932 int acpi_bus_register_driver(struct acpi_driver *driver) 933 { 934 int ret; 935 936 if (acpi_disabled) 937 return -ENODEV; 938 driver->drv.name = driver->name; 939 driver->drv.bus = &acpi_bus_type; 940 driver->drv.owner = driver->owner; 941 942 ret = driver_register(&driver->drv); 943 return ret; 944 } 945 946 EXPORT_SYMBOL(acpi_bus_register_driver); 947 948 /** 949 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus 950 * @driver: driver to unregister 951 * 952 * Unregisters a driver with the ACPI bus. Searches the namespace for all 953 * devices that match the driver's criteria and unbinds. 954 */ 955 void acpi_bus_unregister_driver(struct acpi_driver *driver) 956 { 957 driver_unregister(&driver->drv); 958 } 959 960 EXPORT_SYMBOL(acpi_bus_unregister_driver); 961 962 /* -------------------------------------------------------------------------- 963 ACPI Bus operations 964 -------------------------------------------------------------------------- */ 965 966 static int acpi_bus_match(struct device *dev, struct device_driver *drv) 967 { 968 struct acpi_device *acpi_dev = to_acpi_device(dev); 969 struct acpi_driver *acpi_drv = to_acpi_driver(drv); 970 971 return acpi_dev->flags.match_driver 972 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids); 973 } 974 975 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) 976 { 977 return __acpi_device_uevent_modalias(to_acpi_device(dev), env); 978 } 979 980 static int acpi_device_probe(struct device *dev) 981 { 982 struct acpi_device *acpi_dev = to_acpi_device(dev); 983 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); 984 int ret; 985 986 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev)) 987 return -EINVAL; 988 989 if (!acpi_drv->ops.add) 990 return -ENOSYS; 991 992 ret = acpi_drv->ops.add(acpi_dev); 993 if (ret) 994 return ret; 995 996 acpi_dev->driver = acpi_drv; 997 998 pr_debug("Driver [%s] successfully bound to device [%s]\n", 999 acpi_drv->name, acpi_dev->pnp.bus_id); 1000 1001 if (acpi_drv->ops.notify) { 1002 ret = acpi_device_install_notify_handler(acpi_dev); 1003 if (ret) { 1004 if (acpi_drv->ops.remove) 1005 acpi_drv->ops.remove(acpi_dev); 1006 1007 acpi_dev->driver = NULL; 1008 acpi_dev->driver_data = NULL; 1009 return ret; 1010 } 1011 } 1012 1013 pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name, 1014 acpi_dev->pnp.bus_id); 1015 1016 get_device(dev); 1017 return 0; 1018 } 1019 1020 static int acpi_device_remove(struct device *dev) 1021 { 1022 struct acpi_device *acpi_dev = to_acpi_device(dev); 1023 struct acpi_driver *acpi_drv = acpi_dev->driver; 1024 1025 if (acpi_drv) { 1026 if (acpi_drv->ops.notify) 1027 acpi_device_remove_notify_handler(acpi_dev); 1028 if (acpi_drv->ops.remove) 1029 acpi_drv->ops.remove(acpi_dev); 1030 } 1031 acpi_dev->driver = NULL; 1032 acpi_dev->driver_data = NULL; 1033 1034 put_device(dev); 1035 return 0; 1036 } 1037 1038 struct bus_type acpi_bus_type = { 1039 .name = "acpi", 1040 .match = acpi_bus_match, 1041 .probe = acpi_device_probe, 1042 .remove = acpi_device_remove, 1043 .uevent = acpi_device_uevent, 1044 }; 1045 1046 /* -------------------------------------------------------------------------- 1047 Initialization/Cleanup 1048 -------------------------------------------------------------------------- */ 1049 1050 static int __init acpi_bus_init_irq(void) 1051 { 1052 acpi_status status; 1053 char *message = NULL; 1054 1055 1056 /* 1057 * Let the system know what interrupt model we are using by 1058 * evaluating the \_PIC object, if exists. 1059 */ 1060 1061 switch (acpi_irq_model) { 1062 case ACPI_IRQ_MODEL_PIC: 1063 message = "PIC"; 1064 break; 1065 case ACPI_IRQ_MODEL_IOAPIC: 1066 message = "IOAPIC"; 1067 break; 1068 case ACPI_IRQ_MODEL_IOSAPIC: 1069 message = "IOSAPIC"; 1070 break; 1071 case ACPI_IRQ_MODEL_GIC: 1072 message = "GIC"; 1073 break; 1074 case ACPI_IRQ_MODEL_PLATFORM: 1075 message = "platform specific model"; 1076 break; 1077 default: 1078 pr_info("Unknown interrupt routing model\n"); 1079 return -ENODEV; 1080 } 1081 1082 pr_info("Using %s for interrupt routing\n", message); 1083 1084 status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model); 1085 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { 1086 pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status)); 1087 return -ENODEV; 1088 } 1089 1090 return 0; 1091 } 1092 1093 /** 1094 * acpi_early_init - Initialize ACPICA and populate the ACPI namespace. 1095 * 1096 * The ACPI tables are accessible after this, but the handling of events has not 1097 * been initialized and the global lock is not available yet, so AML should not 1098 * be executed at this point. 1099 * 1100 * Doing this before switching the EFI runtime services to virtual mode allows 1101 * the EfiBootServices memory to be freed slightly earlier on boot. 1102 */ 1103 void __init acpi_early_init(void) 1104 { 1105 acpi_status status; 1106 1107 if (acpi_disabled) 1108 return; 1109 1110 pr_info("Core revision %08x\n", ACPI_CA_VERSION); 1111 1112 /* enable workarounds, unless strict ACPI spec. compliance */ 1113 if (!acpi_strict) 1114 acpi_gbl_enable_interpreter_slack = TRUE; 1115 1116 acpi_permanent_mmap = true; 1117 1118 #ifdef CONFIG_X86 1119 /* 1120 * If the machine falls into the DMI check table, 1121 * DSDT will be copied to memory. 1122 * Note that calling dmi_check_system() here on other architectures 1123 * would not be OK because only x86 initializes dmi early enough. 1124 * Thankfully only x86 systems need such quirks for now. 1125 */ 1126 dmi_check_system(dsdt_dmi_table); 1127 #endif 1128 1129 status = acpi_reallocate_root_table(); 1130 if (ACPI_FAILURE(status)) { 1131 pr_err("Unable to reallocate ACPI tables\n"); 1132 goto error0; 1133 } 1134 1135 status = acpi_initialize_subsystem(); 1136 if (ACPI_FAILURE(status)) { 1137 pr_err("Unable to initialize the ACPI Interpreter\n"); 1138 goto error0; 1139 } 1140 1141 #ifdef CONFIG_X86 1142 if (!acpi_ioapic) { 1143 /* compatible (0) means level (3) */ 1144 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) { 1145 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK; 1146 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL; 1147 } 1148 /* Set PIC-mode SCI trigger type */ 1149 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt, 1150 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2); 1151 } else { 1152 /* 1153 * now that acpi_gbl_FADT is initialized, 1154 * update it with result from INT_SRC_OVR parsing 1155 */ 1156 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi; 1157 } 1158 #endif 1159 return; 1160 1161 error0: 1162 disable_acpi(); 1163 } 1164 1165 /** 1166 * acpi_subsystem_init - Finalize the early initialization of ACPI. 1167 * 1168 * Switch over the platform to the ACPI mode (if possible). 1169 * 1170 * Doing this too early is generally unsafe, but at the same time it needs to be 1171 * done before all things that really depend on ACPI. The right spot appears to 1172 * be before finalizing the EFI initialization. 1173 */ 1174 void __init acpi_subsystem_init(void) 1175 { 1176 acpi_status status; 1177 1178 if (acpi_disabled) 1179 return; 1180 1181 status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE); 1182 if (ACPI_FAILURE(status)) { 1183 pr_err("Unable to enable ACPI\n"); 1184 disable_acpi(); 1185 } else { 1186 /* 1187 * If the system is using ACPI then we can be reasonably 1188 * confident that any regulators are managed by the firmware 1189 * so tell the regulator core it has everything it needs to 1190 * know. 1191 */ 1192 regulator_has_full_constraints(); 1193 } 1194 } 1195 1196 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context) 1197 { 1198 acpi_scan_table_handler(event, table, context); 1199 1200 return acpi_sysfs_table_handler(event, table, context); 1201 } 1202 1203 static int __init acpi_bus_init(void) 1204 { 1205 int result; 1206 acpi_status status; 1207 1208 acpi_os_initialize1(); 1209 1210 status = acpi_load_tables(); 1211 if (ACPI_FAILURE(status)) { 1212 pr_err("Unable to load the System Description Tables\n"); 1213 goto error1; 1214 } 1215 1216 /* 1217 * ACPI 2.0 requires the EC driver to be loaded and work before the EC 1218 * device is found in the namespace. 1219 * 1220 * This is accomplished by looking for the ECDT table and getting the EC 1221 * parameters out of that. 1222 * 1223 * Do that before calling acpi_initialize_objects() which may trigger EC 1224 * address space accesses. 1225 */ 1226 acpi_ec_ecdt_probe(); 1227 1228 status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE); 1229 if (ACPI_FAILURE(status)) { 1230 pr_err("Unable to start the ACPI Interpreter\n"); 1231 goto error1; 1232 } 1233 1234 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION); 1235 if (ACPI_FAILURE(status)) { 1236 pr_err("Unable to initialize ACPI objects\n"); 1237 goto error1; 1238 } 1239 1240 /* Set capability bits for _OSC under processor scope */ 1241 acpi_early_processor_osc(); 1242 1243 /* 1244 * _OSC method may exist in module level code, 1245 * so it must be run after ACPI_FULL_INITIALIZATION 1246 */ 1247 acpi_bus_osc_negotiate_platform_control(); 1248 acpi_bus_osc_negotiate_usb_control(); 1249 1250 /* 1251 * _PDC control method may load dynamic SSDT tables, 1252 * and we need to install the table handler before that. 1253 */ 1254 status = acpi_install_table_handler(acpi_bus_table_handler, NULL); 1255 1256 acpi_sysfs_init(); 1257 1258 acpi_early_processor_set_pdc(); 1259 1260 /* 1261 * Maybe EC region is required at bus_scan/acpi_get_devices. So it 1262 * is necessary to enable it as early as possible. 1263 */ 1264 acpi_ec_dsdt_probe(); 1265 1266 pr_info("Interpreter enabled\n"); 1267 1268 /* Initialize sleep structures */ 1269 acpi_sleep_init(); 1270 1271 /* 1272 * Get the system interrupt model and evaluate \_PIC. 1273 */ 1274 result = acpi_bus_init_irq(); 1275 if (result) 1276 goto error1; 1277 1278 /* 1279 * Register the for all standard device notifications. 1280 */ 1281 status = 1282 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY, 1283 &acpi_bus_notify, NULL); 1284 if (ACPI_FAILURE(status)) { 1285 pr_err("Unable to register for system notifications\n"); 1286 goto error1; 1287 } 1288 1289 /* 1290 * Create the top ACPI proc directory 1291 */ 1292 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL); 1293 1294 result = bus_register(&acpi_bus_type); 1295 if (!result) 1296 return 0; 1297 1298 /* Mimic structured exception handling */ 1299 error1: 1300 acpi_terminate(); 1301 return -ENODEV; 1302 } 1303 1304 struct kobject *acpi_kobj; 1305 EXPORT_SYMBOL_GPL(acpi_kobj); 1306 1307 static int __init acpi_init(void) 1308 { 1309 int result; 1310 1311 if (acpi_disabled) { 1312 pr_info("Interpreter disabled.\n"); 1313 return -ENODEV; 1314 } 1315 1316 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj); 1317 if (!acpi_kobj) { 1318 pr_debug("%s: kset create error\n", __func__); 1319 acpi_kobj = NULL; 1320 } 1321 1322 result = acpi_bus_init(); 1323 if (result) { 1324 disable_acpi(); 1325 return result; 1326 } 1327 1328 pci_mmcfg_late_init(); 1329 acpi_iort_init(); 1330 acpi_scan_init(); 1331 acpi_ec_init(); 1332 acpi_debugfs_init(); 1333 acpi_sleep_proc_init(); 1334 acpi_wakeup_device_init(); 1335 acpi_debugger_init(); 1336 acpi_setup_sb_notify_handler(); 1337 return 0; 1338 } 1339 1340 subsys_initcall(acpi_init); 1341