1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * scan.c - support for transforming the ACPI namespace into individual objects 4 */ 5 6 #define pr_fmt(fmt) "ACPI: " fmt 7 8 #include <linux/async.h> 9 #include <linux/auxiliary_bus.h> 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/slab.h> 13 #include <linux/kernel.h> 14 #include <linux/acpi.h> 15 #include <linux/acpi_iort.h> 16 #include <linux/acpi_iovt.h> 17 #include <linux/acpi_rimt.h> 18 #include <linux/acpi_viot.h> 19 #include <linux/iommu.h> 20 #include <linux/signal.h> 21 #include <linux/kthread.h> 22 #include <linux/dmi.h> 23 #include <linux/dma-map-ops.h> 24 #include <linux/pci.h> 25 #include <linux/platform_data/x86/apple.h> 26 #include <linux/pgtable.h> 27 #include <linux/crc32.h> 28 #include <linux/dma-direct.h> 29 30 #include "internal.h" 31 #include "sleep.h" 32 33 #define ACPI_BUS_HID "LNXSYBUS" 34 35 #define INVALID_ACPI_HANDLE ((acpi_handle)ZERO_PAGE(0)) 36 37 static const char *dummy_hid = "device"; 38 39 static LIST_HEAD(acpi_dep_list); 40 static DEFINE_MUTEX(acpi_dep_list_lock); 41 LIST_HEAD(acpi_bus_id_list); 42 static DEFINE_MUTEX(acpi_scan_lock); 43 static LIST_HEAD(acpi_scan_handlers_list); 44 DEFINE_MUTEX(acpi_device_lock); 45 LIST_HEAD(acpi_wakeup_device_list); 46 static DEFINE_MUTEX(acpi_hp_context_lock); 47 static LIST_HEAD(acpi_scan_system_dev_list); 48 49 /* 50 * The UART device described by the SPCR table is the only object which needs 51 * special-casing. Everything else is covered by ACPI namespace paths in STAO 52 * table. 53 */ 54 static u64 spcr_uart_addr; 55 56 void acpi_scan_lock_acquire(void) 57 { 58 mutex_lock(&acpi_scan_lock); 59 } 60 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire); 61 62 void acpi_scan_lock_release(void) 63 { 64 mutex_unlock(&acpi_scan_lock); 65 } 66 EXPORT_SYMBOL_GPL(acpi_scan_lock_release); 67 68 void acpi_lock_hp_context(void) 69 { 70 mutex_lock(&acpi_hp_context_lock); 71 } 72 73 void acpi_unlock_hp_context(void) 74 { 75 mutex_unlock(&acpi_hp_context_lock); 76 } 77 78 void acpi_initialize_hp_context(struct acpi_device *adev, 79 struct acpi_hotplug_context *hp, 80 acpi_hp_notify notify, acpi_hp_uevent uevent) 81 { 82 acpi_lock_hp_context(); 83 hp->notify = notify; 84 hp->uevent = uevent; 85 acpi_set_hp_context(adev, hp); 86 acpi_unlock_hp_context(); 87 } 88 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context); 89 90 int acpi_scan_add_handler(struct acpi_scan_handler *handler) 91 { 92 if (!handler) 93 return -EINVAL; 94 95 list_add_tail(&handler->list_node, &acpi_scan_handlers_list); 96 return 0; 97 } 98 99 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler, 100 const char *hotplug_profile_name) 101 { 102 int error; 103 104 error = acpi_scan_add_handler(handler); 105 if (error) 106 return error; 107 108 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name); 109 return 0; 110 } 111 112 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent) 113 { 114 struct acpi_device_physical_node *pn; 115 bool offline = true; 116 char *envp[] = { "EVENT=offline", NULL }; 117 118 mutex_lock(&adev->physical_node_lock); 119 120 list_for_each_entry(pn, &adev->physical_node_list, node) 121 if (device_supports_offline(pn->dev) && !dev_offline(pn->dev)) { 122 if (uevent) 123 kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp); 124 125 offline = false; 126 break; 127 } 128 129 mutex_unlock(&adev->physical_node_lock); 130 return offline; 131 } 132 133 static struct acpi_device_physical_node * 134 acpi_find_physical_node(struct acpi_device *adev, struct device *dev) 135 { 136 struct acpi_device_physical_node *pn; 137 138 lockdep_assert_held(&adev->physical_node_lock); 139 140 list_for_each_entry(pn, &adev->physical_node_list, node) 141 if (pn->dev == dev) 142 return pn; 143 144 return NULL; 145 } 146 147 static struct device *acpi_next_offline_pending(struct acpi_device *adev) 148 { 149 struct acpi_device_physical_node *pn; 150 151 lockdep_assert_held(&adev->physical_node_lock); 152 153 /* 154 * Find the first physical node that is still to be offlined, clear its 155 * offline_pending flag and return its device with a reference held. 156 */ 157 list_for_each_entry(pn, &adev->physical_node_list, node) 158 if (pn->offline_pending) { 159 pn->offline_pending = false; 160 return get_device(pn->dev); 161 } 162 163 return NULL; 164 } 165 166 static struct device *acpi_next_put_online(struct acpi_device *adev) 167 { 168 struct acpi_device_physical_node *pn; 169 170 lockdep_assert_held(&adev->physical_node_lock); 171 172 /* 173 * Find the first physical node that has been offlined by 174 * acpi_bus_offline(), clear its put_online flag and return its device 175 * with a reference held. 176 */ 177 list_for_each_entry(pn, &adev->physical_node_list, node) 178 if (pn->put_online) { 179 pn->put_online = false; 180 return get_device(pn->dev); 181 } 182 183 return NULL; 184 } 185 186 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data, 187 void **ret_p) 188 { 189 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 190 struct acpi_device_physical_node *pn; 191 bool second_pass = (bool)data; 192 acpi_status status = AE_OK; 193 struct device *dev; 194 195 if (!device) 196 return AE_OK; 197 198 if (device->handler && !device->handler->hotplug.enabled) { 199 *ret_p = &device->dev; 200 return AE_SUPPORT; 201 } 202 203 mutex_lock(&device->physical_node_lock); 204 205 list_for_each_entry(pn, &device->physical_node_list, node) { 206 if (second_pass) { 207 /* Skip devices offlined by the first pass. */ 208 pn->offline_pending = !pn->put_online; 209 } else { 210 pn->offline_pending = true; 211 pn->put_online = false; 212 } 213 } 214 215 /* 216 * Drop physical_node_lock around device_offline(), because the latter 217 * acquires locks, like the CPU hotplug ones, that must not nest under 218 * it. 219 */ 220 while ((dev = acpi_next_offline_pending(device))) { 221 int ret; 222 223 mutex_unlock(&device->physical_node_lock); 224 ret = device_offline(dev); 225 mutex_lock(&device->physical_node_lock); 226 227 if (ret >= 0) { 228 /* The device may have been unbound in the meantime. */ 229 pn = acpi_find_physical_node(device, dev); 230 if (pn) 231 pn->put_online = !ret; 232 } else { 233 *ret_p = &device->dev; 234 if (second_pass) { 235 dev_warn(dev, "Offline failed.\n"); 236 status = AE_ERROR; 237 } 238 } 239 240 put_device(dev); 241 if (ACPI_FAILURE(status)) 242 break; 243 } 244 245 mutex_unlock(&device->physical_node_lock); 246 247 return status; 248 } 249 250 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data, 251 void **ret_p) 252 { 253 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 254 struct device *dev; 255 256 if (!device) 257 return AE_OK; 258 259 mutex_lock(&device->physical_node_lock); 260 261 while ((dev = acpi_next_put_online(device))) { 262 mutex_unlock(&device->physical_node_lock); 263 device_online(dev); 264 mutex_lock(&device->physical_node_lock); 265 put_device(dev); 266 } 267 268 mutex_unlock(&device->physical_node_lock); 269 270 return AE_OK; 271 } 272 273 static int acpi_scan_try_to_offline(struct acpi_device *device) 274 { 275 acpi_handle handle = device->handle; 276 struct device *errdev = NULL; 277 acpi_status status; 278 279 /* 280 * Carry out two passes here and ignore errors in the first pass, 281 * because if the devices in question are memory blocks and 282 * CONFIG_MEMCG is set, one of the blocks may hold data structures 283 * that the other blocks depend on, but it is not known in advance which 284 * block holds them. 285 * 286 * If the first pass is successful, the second one isn't needed, though. 287 */ 288 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 289 NULL, acpi_bus_offline, (void *)false, 290 (void **)&errdev); 291 if (status == AE_SUPPORT) { 292 dev_warn(errdev, "Offline disabled.\n"); 293 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 294 acpi_bus_online, NULL, NULL, NULL); 295 return -EPERM; 296 } 297 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev); 298 if (errdev) { 299 errdev = NULL; 300 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 301 NULL, acpi_bus_offline, (void *)true, 302 (void **)&errdev); 303 if (!errdev) 304 acpi_bus_offline(handle, 0, (void *)true, 305 (void **)&errdev); 306 307 if (errdev) { 308 acpi_bus_online(handle, 0, NULL, NULL); 309 acpi_walk_namespace(ACPI_TYPE_ANY, handle, 310 ACPI_UINT32_MAX, acpi_bus_online, 311 NULL, NULL, NULL); 312 return -EBUSY; 313 } 314 } 315 return 0; 316 } 317 318 #define ACPI_SCAN_CHECK_FLAG_STATUS BIT(0) 319 #define ACPI_SCAN_CHECK_FLAG_EJECT BIT(1) 320 321 static int acpi_scan_check_and_detach(struct acpi_device *adev, void *p) 322 { 323 struct acpi_scan_handler *handler = adev->handler; 324 uintptr_t flags = (uintptr_t)p; 325 326 acpi_dev_for_each_child_reverse(adev, acpi_scan_check_and_detach, p); 327 328 if (flags & ACPI_SCAN_CHECK_FLAG_STATUS) { 329 acpi_bus_get_status(adev); 330 /* 331 * Skip devices that are still there and take the enabled 332 * flag into account. 333 */ 334 if (acpi_device_is_enabled(adev)) 335 return 0; 336 337 /* Skip device that have not been enumerated. */ 338 if (!acpi_device_enumerated(adev)) { 339 dev_dbg(&adev->dev, "Still not enumerated\n"); 340 return 0; 341 } 342 } 343 344 if (handler && handler->detach) 345 handler->detach(adev); 346 347 /* 348 * Most likely, the device is going away, so put it into D3cold before 349 * that. 350 */ 351 acpi_device_set_power(adev, ACPI_STATE_D3_COLD); 352 adev->flags.initialized = false; 353 354 /* For eject this is deferred to acpi_bus_post_eject() */ 355 if (!(flags & ACPI_SCAN_CHECK_FLAG_EJECT)) { 356 adev->handler = NULL; 357 acpi_device_clear_enumerated(adev); 358 } 359 return 0; 360 } 361 362 static int acpi_bus_post_eject(struct acpi_device *adev, void *not_used) 363 { 364 struct acpi_scan_handler *handler = adev->handler; 365 366 acpi_dev_for_each_child_reverse(adev, acpi_bus_post_eject, NULL); 367 368 if (handler) { 369 if (handler->post_eject) 370 handler->post_eject(adev); 371 372 adev->handler = NULL; 373 } 374 375 acpi_device_clear_enumerated(adev); 376 377 return 0; 378 } 379 380 static void acpi_scan_check_subtree(struct acpi_device *adev) 381 { 382 uintptr_t flags = ACPI_SCAN_CHECK_FLAG_STATUS; 383 384 acpi_scan_check_and_detach(adev, (void *)flags); 385 } 386 387 static int acpi_scan_hot_remove(struct acpi_device *device) 388 { 389 acpi_handle handle = device->handle; 390 unsigned long long sta; 391 acpi_status status; 392 uintptr_t flags = ACPI_SCAN_CHECK_FLAG_EJECT; 393 394 if (device->handler && device->handler->hotplug.demand_offline) { 395 if (!acpi_scan_is_offline(device, true)) 396 return -EBUSY; 397 } else { 398 int error = acpi_scan_try_to_offline(device); 399 if (error) 400 return error; 401 } 402 403 acpi_handle_debug(handle, "Ejecting\n"); 404 405 acpi_scan_check_and_detach(device, (void *)flags); 406 407 acpi_evaluate_lck(handle, 0); 408 /* 409 * TBD: _EJD support. 410 */ 411 status = acpi_evaluate_ej0(handle); 412 if (status == AE_NOT_FOUND) 413 return -ENODEV; 414 else if (ACPI_FAILURE(status)) 415 return -EIO; 416 417 /* 418 * Verify if eject was indeed successful. If not, log an error 419 * message. No need to call _OST since _EJ0 call was made OK. 420 */ 421 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); 422 if (ACPI_FAILURE(status)) { 423 acpi_handle_warn(handle, 424 "Status check after eject failed (0x%x)\n", status); 425 } else if (sta & ACPI_STA_DEVICE_ENABLED) { 426 acpi_handle_warn(handle, 427 "Eject incomplete - status 0x%llx\n", sta); 428 } else { 429 acpi_bus_post_eject(device, NULL); 430 } 431 432 return 0; 433 } 434 435 static int acpi_scan_rescan_bus(struct acpi_device *adev) 436 { 437 struct acpi_scan_handler *handler = adev->handler; 438 int ret; 439 440 if (handler && handler->hotplug.scan_dependent) 441 ret = handler->hotplug.scan_dependent(adev); 442 else 443 ret = acpi_bus_scan(adev->handle); 444 445 if (ret) 446 dev_info(&adev->dev, "Namespace scan failure\n"); 447 448 return ret; 449 } 450 451 static int acpi_scan_device_check(struct acpi_device *adev) 452 { 453 struct acpi_device *parent; 454 455 acpi_scan_check_subtree(adev); 456 457 if (!acpi_device_is_present(adev)) 458 return 0; 459 460 /* 461 * This function is only called for device objects for which matching 462 * scan handlers exist. The only situation in which the scan handler 463 * is not attached to this device object yet is when the device has 464 * just appeared (either it wasn't present at all before or it was 465 * removed and then added again). 466 */ 467 if (adev->handler) { 468 dev_dbg(&adev->dev, "Already enumerated\n"); 469 return 0; 470 } 471 472 parent = acpi_dev_parent(adev); 473 if (!parent) 474 parent = adev; 475 476 return acpi_scan_rescan_bus(parent); 477 } 478 479 static int acpi_scan_bus_check(struct acpi_device *adev) 480 { 481 acpi_scan_check_subtree(adev); 482 483 return acpi_scan_rescan_bus(adev); 484 } 485 486 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type) 487 { 488 switch (type) { 489 case ACPI_NOTIFY_BUS_CHECK: 490 return acpi_scan_bus_check(adev); 491 case ACPI_NOTIFY_DEVICE_CHECK: 492 return acpi_scan_device_check(adev); 493 case ACPI_NOTIFY_EJECT_REQUEST: 494 case ACPI_OST_EC_OSPM_EJECT: 495 if (adev->handler && !adev->handler->hotplug.enabled) { 496 dev_info(&adev->dev, "Eject disabled\n"); 497 return -EPERM; 498 } 499 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST, 500 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); 501 return acpi_scan_hot_remove(adev); 502 } 503 return -EINVAL; 504 } 505 506 void acpi_device_hotplug(struct acpi_device *adev, u32 src) 507 { 508 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; 509 int error = -ENODEV; 510 511 lock_device_hotplug(); 512 mutex_lock(&acpi_scan_lock); 513 514 /* 515 * The device object's ACPI handle cannot become invalid as long as we 516 * are holding acpi_scan_lock, but it might have become invalid before 517 * that lock was acquired. 518 */ 519 if (adev->handle == INVALID_ACPI_HANDLE) 520 goto err_out; 521 522 if (adev->flags.is_dock_station) { 523 error = dock_notify(adev, src); 524 } else if (adev->flags.hotplug_notify) { 525 error = acpi_generic_hotplug_event(adev, src); 526 } else { 527 acpi_hp_notify notify; 528 529 acpi_lock_hp_context(); 530 notify = adev->hp ? adev->hp->notify : NULL; 531 acpi_unlock_hp_context(); 532 /* 533 * There may be additional notify handlers for device objects 534 * without the .event() callback, so ignore them here. 535 */ 536 if (notify) 537 error = notify(adev, src); 538 else 539 goto out; 540 } 541 switch (error) { 542 case 0: 543 ost_code = ACPI_OST_SC_SUCCESS; 544 break; 545 case -EPERM: 546 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED; 547 break; 548 case -EBUSY: 549 ost_code = ACPI_OST_SC_DEVICE_BUSY; 550 break; 551 default: 552 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; 553 break; 554 } 555 556 err_out: 557 acpi_evaluate_ost(adev->handle, src, ost_code, NULL); 558 559 out: 560 acpi_put_acpi_dev(adev); 561 mutex_unlock(&acpi_scan_lock); 562 unlock_device_hotplug(); 563 } 564 565 static void acpi_free_power_resources_lists(struct acpi_device *device) 566 { 567 int i; 568 569 if (device->wakeup.flags.valid) 570 acpi_power_resources_list_free(&device->wakeup.resources); 571 572 if (!device->power.flags.power_resources) 573 return; 574 575 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { 576 struct acpi_device_power_state *ps = &device->power.states[i]; 577 acpi_power_resources_list_free(&ps->resources); 578 } 579 } 580 581 static void acpi_device_release(struct device *dev) 582 { 583 struct acpi_device *acpi_dev = to_acpi_device(dev); 584 585 acpi_free_properties(acpi_dev); 586 acpi_free_pnp_ids(&acpi_dev->pnp); 587 acpi_free_power_resources_lists(acpi_dev); 588 kfree(acpi_dev); 589 } 590 591 static void acpi_device_cleanup(struct acpi_device *device) 592 { 593 struct acpi_device_bus_id *acpi_device_bus_id; 594 595 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) 596 if (!strcmp(acpi_device_bus_id->bus_id, 597 acpi_device_hid(device))) { 598 ida_free(&acpi_device_bus_id->instance_ida, 599 device->pnp.instance_no); 600 if (ida_is_empty(&acpi_device_bus_id->instance_ida)) { 601 list_del(&acpi_device_bus_id->node); 602 kfree_const(acpi_device_bus_id->bus_id); 603 kfree(acpi_device_bus_id); 604 } 605 break; 606 } 607 608 list_del(&device->wakeup_list); 609 } 610 611 static void acpi_device_del(struct acpi_device *device) 612 { 613 mutex_lock(&acpi_device_lock); 614 615 acpi_device_cleanup(device); 616 617 mutex_unlock(&acpi_device_lock); 618 619 acpi_power_add_remove_device(device, false); 620 acpi_device_remove_files(device); 621 if (device->remove) 622 device->remove(device); 623 624 device_del(&device->dev); 625 } 626 627 static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain); 628 629 static LIST_HEAD(acpi_device_del_list); 630 static DEFINE_MUTEX(acpi_device_del_lock); 631 632 static void acpi_device_del_work_fn(struct work_struct *work_not_used) 633 { 634 for (;;) { 635 struct acpi_device *adev; 636 637 mutex_lock(&acpi_device_del_lock); 638 639 if (list_empty(&acpi_device_del_list)) { 640 mutex_unlock(&acpi_device_del_lock); 641 break; 642 } 643 adev = list_first_entry(&acpi_device_del_list, 644 struct acpi_device, del_list); 645 list_del(&adev->del_list); 646 647 mutex_unlock(&acpi_device_del_lock); 648 649 blocking_notifier_call_chain(&acpi_reconfig_chain, 650 ACPI_RECONFIG_DEVICE_REMOVE, adev); 651 652 acpi_device_del(adev); 653 /* 654 * Drop references to all power resources that might have been 655 * used by the device. 656 */ 657 acpi_power_transition(adev, ACPI_STATE_D3_COLD); 658 acpi_dev_put(adev); 659 } 660 } 661 662 /** 663 * acpi_scan_drop_device - Drop an ACPI device object. 664 * @handle: Handle of an ACPI namespace node, not used. 665 * @context: Address of the ACPI device object to drop. 666 * 667 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI 668 * namespace node the device object pointed to by @context is attached to. 669 * 670 * The unregistration is carried out asynchronously to avoid running 671 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to 672 * ensure the correct ordering (the device objects must be unregistered in the 673 * same order in which the corresponding namespace nodes are deleted). 674 */ 675 static void acpi_scan_drop_device(acpi_handle handle, void *context) 676 { 677 static DECLARE_WORK(work, acpi_device_del_work_fn); 678 struct acpi_device *adev = context; 679 680 mutex_lock(&acpi_device_del_lock); 681 682 /* 683 * Use the ACPI hotplug workqueue which is ordered, so this work item 684 * won't run after any hotplug work items submitted subsequently. That 685 * prevents attempts to register device objects identical to those being 686 * deleted from happening concurrently (such attempts result from 687 * hotplug events handled via the ACPI hotplug workqueue). It also will 688 * run after all of the work items submitted previously, which helps 689 * those work items to ensure that they are not accessing stale device 690 * objects. 691 */ 692 if (list_empty(&acpi_device_del_list)) 693 acpi_queue_hotplug_work(&work); 694 695 list_add_tail(&adev->del_list, &acpi_device_del_list); 696 /* Make acpi_ns_validate_handle() return NULL for this handle. */ 697 adev->handle = INVALID_ACPI_HANDLE; 698 699 mutex_unlock(&acpi_device_del_lock); 700 } 701 702 static struct acpi_device *handle_to_device(acpi_handle handle, 703 void (*callback)(void *)) 704 { 705 struct acpi_device *adev = NULL; 706 acpi_status status; 707 708 status = acpi_get_data_full(handle, acpi_scan_drop_device, 709 (void **)&adev, callback); 710 if (ACPI_FAILURE(status) || !adev) 711 return NULL; 712 713 return adev; 714 } 715 716 /** 717 * acpi_fetch_acpi_dev - Retrieve ACPI device object. 718 * @handle: ACPI handle associated with the requested ACPI device object. 719 * 720 * Return a pointer to the ACPI device object associated with @handle, if 721 * present, or NULL otherwise. 722 */ 723 struct acpi_device *acpi_fetch_acpi_dev(acpi_handle handle) 724 { 725 return handle_to_device(handle, NULL); 726 } 727 EXPORT_SYMBOL_GPL(acpi_fetch_acpi_dev); 728 729 static void get_acpi_device(void *dev) 730 { 731 acpi_dev_get(dev); 732 } 733 734 /** 735 * acpi_get_acpi_dev - Retrieve ACPI device object and reference count it. 736 * @handle: ACPI handle associated with the requested ACPI device object. 737 * 738 * Return a pointer to the ACPI device object associated with @handle and bump 739 * up that object's reference counter (under the ACPI Namespace lock), if 740 * present, or return NULL otherwise. 741 * 742 * The ACPI device object reference acquired by this function needs to be 743 * dropped via acpi_dev_put(). 744 */ 745 struct acpi_device *acpi_get_acpi_dev(acpi_handle handle) 746 { 747 return handle_to_device(handle, get_acpi_device); 748 } 749 EXPORT_SYMBOL_GPL(acpi_get_acpi_dev); 750 751 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id) 752 { 753 struct acpi_device_bus_id *acpi_device_bus_id; 754 755 /* Find suitable bus_id and instance number in acpi_bus_id_list. */ 756 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { 757 if (!strcmp(acpi_device_bus_id->bus_id, dev_id)) 758 return acpi_device_bus_id; 759 } 760 return NULL; 761 } 762 763 static int acpi_device_set_name(struct acpi_device *device, 764 struct acpi_device_bus_id *acpi_device_bus_id) 765 { 766 struct ida *instance_ida = &acpi_device_bus_id->instance_ida; 767 int result; 768 769 result = ida_alloc(instance_ida, GFP_KERNEL); 770 if (result < 0) 771 return result; 772 773 device->pnp.instance_no = result; 774 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result); 775 return 0; 776 } 777 778 int acpi_tie_acpi_dev(struct acpi_device *adev) 779 { 780 acpi_handle handle = adev->handle; 781 acpi_status status; 782 783 if (!handle) 784 return 0; 785 786 status = acpi_attach_data(handle, acpi_scan_drop_device, adev); 787 if (ACPI_FAILURE(status)) { 788 acpi_handle_err(handle, "Unable to attach device data\n"); 789 return -ENODEV; 790 } 791 792 return 0; 793 } 794 795 static void acpi_store_pld_crc(struct acpi_device *adev) 796 { 797 struct acpi_pld_info *pld; 798 799 if (!acpi_get_physical_device_location(adev->handle, &pld)) 800 return; 801 802 adev->pld_crc = crc32(~0, pld, sizeof(*pld)); 803 ACPI_FREE(pld); 804 } 805 806 int acpi_device_add(struct acpi_device *device) 807 { 808 struct acpi_device_bus_id *acpi_device_bus_id; 809 int result; 810 811 /* 812 * Linkage 813 * ------- 814 * Link this device to its parent and siblings. 815 */ 816 INIT_LIST_HEAD(&device->wakeup_list); 817 INIT_LIST_HEAD(&device->physical_node_list); 818 INIT_LIST_HEAD(&device->del_list); 819 mutex_init(&device->physical_node_lock); 820 821 mutex_lock(&acpi_device_lock); 822 823 acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device)); 824 if (acpi_device_bus_id) { 825 result = acpi_device_set_name(device, acpi_device_bus_id); 826 if (result) 827 goto err_unlock; 828 } else { 829 acpi_device_bus_id = kzalloc_obj(*acpi_device_bus_id); 830 if (!acpi_device_bus_id) { 831 result = -ENOMEM; 832 goto err_unlock; 833 } 834 acpi_device_bus_id->bus_id = 835 kstrdup_const(acpi_device_hid(device), GFP_KERNEL); 836 if (!acpi_device_bus_id->bus_id) { 837 kfree(acpi_device_bus_id); 838 result = -ENOMEM; 839 goto err_unlock; 840 } 841 842 ida_init(&acpi_device_bus_id->instance_ida); 843 844 result = acpi_device_set_name(device, acpi_device_bus_id); 845 if (result) { 846 kfree_const(acpi_device_bus_id->bus_id); 847 kfree(acpi_device_bus_id); 848 goto err_unlock; 849 } 850 851 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); 852 } 853 854 if (device->wakeup.flags.valid) 855 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); 856 857 acpi_store_pld_crc(device); 858 859 mutex_unlock(&acpi_device_lock); 860 861 result = device_add(&device->dev); 862 if (result) { 863 dev_err(&device->dev, "Error registering device\n"); 864 goto err; 865 } 866 867 acpi_device_setup_files(device); 868 869 return 0; 870 871 err: 872 mutex_lock(&acpi_device_lock); 873 874 acpi_device_cleanup(device); 875 876 err_unlock: 877 mutex_unlock(&acpi_device_lock); 878 879 acpi_detach_data(device->handle, acpi_scan_drop_device); 880 881 return result; 882 } 883 884 /* -------------------------------------------------------------------------- 885 Device Enumeration 886 -------------------------------------------------------------------------- */ 887 static bool acpi_info_matches_ids(struct acpi_device_info *info, 888 const char * const ids[]) 889 { 890 struct acpi_pnp_device_id_list *cid_list = NULL; 891 int i, index; 892 893 if (!(info->valid & ACPI_VALID_HID)) 894 return false; 895 896 index = match_string(ids, -1, info->hardware_id.string); 897 if (index >= 0) 898 return true; 899 900 if (info->valid & ACPI_VALID_CID) 901 cid_list = &info->compatible_id_list; 902 903 if (!cid_list) 904 return false; 905 906 for (i = 0; i < cid_list->count; i++) { 907 index = match_string(ids, -1, cid_list->ids[i].string); 908 if (index >= 0) 909 return true; 910 } 911 912 return false; 913 } 914 915 /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */ 916 static const char * const acpi_ignore_dep_ids[] = { 917 "PNP0D80", /* Windows-compatible System Power Management Controller */ 918 "INT33BD", /* Intel Baytrail Mailbox Device */ 919 "LATT2021", /* Lattice FW Update Client Driver */ 920 NULL 921 }; 922 923 /* List of HIDs for which we honor deps of matching ACPI devs, when checking _DEP lists. */ 924 static const char * const acpi_honor_dep_ids[] = { 925 "ARMH0003", /* ARM GICv5 IWB */ 926 "INT3472", /* Camera sensor PMIC / clk and regulator info */ 927 "INTC1059", /* IVSC (TGL) driver must be loaded to allow i2c access to camera sensors */ 928 "INTC1095", /* IVSC (ADL) driver must be loaded to allow i2c access to camera sensors */ 929 "INTC100A", /* IVSC (RPL) driver must be loaded to allow i2c access to camera sensors */ 930 "INTC10CF", /* IVSC (MTL) driver must be loaded to allow i2c access to camera sensors */ 931 "INTC10DE", /* CVS (LNL) driver must be loaded to allow camera streaming */ 932 "INTC10E0", /* CVS (ARL) driver must be loaded to allow camera streaming */ 933 "INTC10E1", /* CVS (PTL) driver must be loaded to allow camera streaming */ 934 "INTC10FA", /* CVS (NVL) driver must be loaded to allow camera streaming */ 935 "RSCV0001", /* RISC-V PLIC */ 936 "RSCV0002", /* RISC-V APLIC */ 937 "RSCV0005", /* RISC-V SBI MPXY MBOX */ 938 "RSCV0006", /* RISC-V RPMI SYSMSI */ 939 "PNP0C0F", /* PCI Link Device */ 940 "ACPI0016", /* CXL/PCIe host bridge: CXL root (ACPI0017) depends on PCI root attach */ 941 NULL 942 }; 943 944 static struct acpi_device *acpi_find_parent_acpi_dev(acpi_handle handle) 945 { 946 struct acpi_device *adev; 947 948 /* 949 * Fixed hardware devices do not appear in the namespace and do not 950 * have handles, but we fabricate acpi_devices for them, so we have 951 * to deal with them specially. 952 */ 953 if (!handle) 954 return acpi_root; 955 956 do { 957 acpi_status status; 958 959 status = acpi_get_parent(handle, &handle); 960 if (ACPI_FAILURE(status)) { 961 if (status != AE_NULL_ENTRY) 962 return acpi_root; 963 964 return NULL; 965 } 966 adev = acpi_fetch_acpi_dev(handle); 967 } while (!adev); 968 return adev; 969 } 970 971 acpi_status 972 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) 973 { 974 acpi_status status; 975 acpi_handle tmp; 976 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 977 union acpi_object *obj; 978 979 status = acpi_get_handle(handle, "_EJD", &tmp); 980 if (ACPI_FAILURE(status)) 981 return status; 982 983 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); 984 if (ACPI_SUCCESS(status)) { 985 obj = buffer.pointer; 986 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, 987 ejd); 988 kfree(buffer.pointer); 989 } 990 return status; 991 } 992 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); 993 994 static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev) 995 { 996 acpi_handle handle = dev->handle; 997 struct acpi_device_wakeup *wakeup = &dev->wakeup; 998 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 999 union acpi_object *package = NULL; 1000 union acpi_object *element = NULL; 1001 acpi_status status; 1002 int err = -ENODATA; 1003 1004 INIT_LIST_HEAD(&wakeup->resources); 1005 1006 /* _PRW */ 1007 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); 1008 if (ACPI_FAILURE(status)) { 1009 acpi_handle_info(handle, "_PRW evaluation failed: %s\n", 1010 acpi_format_exception(status)); 1011 return err; 1012 } 1013 1014 package = (union acpi_object *)buffer.pointer; 1015 1016 if (!package || package->package.count < 2) 1017 goto out; 1018 1019 element = &(package->package.elements[0]); 1020 if (!element) 1021 goto out; 1022 1023 if (element->type == ACPI_TYPE_PACKAGE) { 1024 if ((element->package.count < 2) || 1025 (element->package.elements[0].type != 1026 ACPI_TYPE_LOCAL_REFERENCE) 1027 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) 1028 goto out; 1029 1030 wakeup->gpe_device = 1031 element->package.elements[0].reference.handle; 1032 wakeup->gpe_number = 1033 (u32) element->package.elements[1].integer.value; 1034 } else if (element->type == ACPI_TYPE_INTEGER) { 1035 wakeup->gpe_device = NULL; 1036 wakeup->gpe_number = element->integer.value; 1037 } else { 1038 goto out; 1039 } 1040 1041 element = &(package->package.elements[1]); 1042 if (element->type != ACPI_TYPE_INTEGER) 1043 goto out; 1044 1045 wakeup->sleep_state = element->integer.value; 1046 1047 err = acpi_extract_power_resources(package, 2, &wakeup->resources); 1048 if (err) 1049 goto out; 1050 1051 if (!list_empty(&wakeup->resources)) { 1052 int sleep_state; 1053 1054 err = acpi_power_wakeup_list_init(&wakeup->resources, 1055 &sleep_state); 1056 if (err) { 1057 acpi_handle_warn(handle, "Retrieving current states " 1058 "of wakeup power resources failed\n"); 1059 acpi_power_resources_list_free(&wakeup->resources); 1060 goto out; 1061 } 1062 if (sleep_state < wakeup->sleep_state) { 1063 acpi_handle_warn(handle, "Overriding _PRW sleep state " 1064 "(S%d) by S%d from power resources\n", 1065 (int)wakeup->sleep_state, sleep_state); 1066 wakeup->sleep_state = sleep_state; 1067 } 1068 } 1069 1070 out: 1071 kfree(buffer.pointer); 1072 return err; 1073 } 1074 1075 static bool acpi_wakeup_gpe_init(struct acpi_device *device) 1076 { 1077 static const struct acpi_device_id button_device_ids[] = { 1078 {"PNP0C0D", 0}, /* Lid */ 1079 {"PNP0C0E", 0}, /* Sleep button */ 1080 {"", 0}, 1081 }; 1082 struct acpi_device_wakeup *wakeup = &device->wakeup; 1083 const struct acpi_device_id *match; 1084 acpi_status status; 1085 1086 wakeup->flags.notifier_present = 0; 1087 1088 match = acpi_match_acpi_device(button_device_ids, device); 1089 if (match && wakeup->sleep_state == ACPI_STATE_S5) 1090 wakeup->sleep_state = ACPI_STATE_S4; 1091 1092 status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device, 1093 wakeup->gpe_number); 1094 return ACPI_SUCCESS(status); 1095 } 1096 1097 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) 1098 { 1099 int err; 1100 1101 /* Presence of _PRW indicates wake capable */ 1102 if (!acpi_has_method(device->handle, "_PRW")) 1103 return; 1104 1105 err = acpi_bus_extract_wakeup_device_power_package(device); 1106 if (err) { 1107 dev_err(&device->dev, "Unable to extract wakeup power resources"); 1108 return; 1109 } 1110 1111 device->wakeup.flags.valid = acpi_wakeup_gpe_init(device); 1112 device->wakeup.prepare_count = 0; 1113 /* 1114 * Call _PSW/_DSW object to disable its ability to wake the sleeping 1115 * system for the ACPI device with the _PRW object. 1116 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW. 1117 * So it is necessary to call _DSW object first. Only when it is not 1118 * present will the _PSW object used. 1119 */ 1120 err = acpi_device_sleep_wake(device, 0, 0, 0); 1121 if (err) 1122 pr_debug("error in _DSW or _PSW evaluation\n"); 1123 } 1124 1125 static void acpi_bus_init_power_state(struct acpi_device *device, int state) 1126 { 1127 struct acpi_device_power_state *ps = &device->power.states[state]; 1128 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' }; 1129 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 1130 acpi_status status; 1131 1132 INIT_LIST_HEAD(&ps->resources); 1133 1134 /* Evaluate "_PRx" to get referenced power resources */ 1135 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer); 1136 if (ACPI_SUCCESS(status)) { 1137 union acpi_object *package = buffer.pointer; 1138 1139 if (buffer.length && package 1140 && package->type == ACPI_TYPE_PACKAGE 1141 && package->package.count) 1142 acpi_extract_power_resources(package, 0, &ps->resources); 1143 1144 ACPI_FREE(buffer.pointer); 1145 } 1146 1147 /* Evaluate "_PSx" to see if we can do explicit sets */ 1148 pathname[2] = 'S'; 1149 if (acpi_has_method(device->handle, pathname)) 1150 ps->flags.explicit_set = 1; 1151 1152 /* State is valid if there are means to put the device into it. */ 1153 if (!list_empty(&ps->resources) || ps->flags.explicit_set) 1154 ps->flags.valid = 1; 1155 1156 ps->power = -1; /* Unknown - driver assigned */ 1157 ps->latency = -1; /* Unknown - driver assigned */ 1158 } 1159 1160 static void acpi_bus_get_power_flags(struct acpi_device *device) 1161 { 1162 unsigned long long dsc = ACPI_STATE_D0; 1163 u32 i; 1164 1165 /* Presence of _PS0|_PR0 indicates 'power manageable' */ 1166 if (!acpi_has_method(device->handle, "_PS0") && 1167 !acpi_has_method(device->handle, "_PR0")) 1168 return; 1169 1170 device->flags.power_manageable = 1; 1171 1172 /* 1173 * Power Management Flags 1174 */ 1175 if (acpi_has_method(device->handle, "_PSC")) 1176 device->power.flags.explicit_get = 1; 1177 1178 if (acpi_has_method(device->handle, "_IRC")) 1179 device->power.flags.inrush_current = 1; 1180 1181 if (acpi_has_method(device->handle, "_DSW")) 1182 device->power.flags.dsw_present = 1; 1183 1184 acpi_evaluate_integer(device->handle, "_DSC", NULL, &dsc); 1185 device->power.state_for_enumeration = dsc; 1186 1187 /* 1188 * Enumerate supported power management states 1189 */ 1190 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) 1191 acpi_bus_init_power_state(device, i); 1192 1193 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources); 1194 1195 /* Set the defaults for D0 and D3hot (always supported). */ 1196 device->power.states[ACPI_STATE_D0].flags.valid = 1; 1197 device->power.states[ACPI_STATE_D0].power = 100; 1198 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1; 1199 1200 /* 1201 * Use power resources only if the D0 list of them is populated, because 1202 * some platforms may provide _PR3 only to indicate D3cold support and 1203 * in those cases the power resources list returned by it may be bogus. 1204 */ 1205 if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) { 1206 device->power.flags.power_resources = 1; 1207 /* 1208 * D3cold is supported if the D3hot list of power resources is 1209 * not empty. 1210 */ 1211 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources)) 1212 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1; 1213 } 1214 1215 device->power.state = ACPI_STATE_UNKNOWN; 1216 } 1217 1218 static void acpi_bus_get_flags(struct acpi_device *device) 1219 { 1220 /* Presence of _STA indicates 'dynamic_status' */ 1221 if (acpi_has_method(device->handle, "_STA")) 1222 device->flags.dynamic_status = 1; 1223 1224 /* Presence of _RMV indicates 'removable' */ 1225 if (acpi_has_method(device->handle, "_RMV")) 1226 device->flags.removable = 1; 1227 1228 /* Presence of _EJD|_EJ0 indicates 'ejectable' */ 1229 if (acpi_has_method(device->handle, "_EJD") || 1230 acpi_has_method(device->handle, "_EJ0")) 1231 device->flags.ejectable = 1; 1232 } 1233 1234 static void acpi_device_get_busid(struct acpi_device *device) 1235 { 1236 char bus_id[5] = { '?', 0 }; 1237 struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; 1238 int i = 0; 1239 1240 /* 1241 * Bus ID 1242 * ------ 1243 * The device's Bus ID is simply the object name. 1244 * TBD: Shouldn't this value be unique (within the ACPI namespace)? 1245 */ 1246 if (!acpi_dev_parent(device)) { 1247 strscpy(device->pnp.bus_id, "ACPI"); 1248 return; 1249 } 1250 1251 switch (device->device_type) { 1252 case ACPI_BUS_TYPE_POWER_BUTTON: 1253 strscpy(device->pnp.bus_id, "PWRF"); 1254 break; 1255 case ACPI_BUS_TYPE_SLEEP_BUTTON: 1256 strscpy(device->pnp.bus_id, "SLPF"); 1257 break; 1258 case ACPI_BUS_TYPE_ECDT_EC: 1259 strscpy(device->pnp.bus_id, "ECDT"); 1260 break; 1261 default: 1262 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer); 1263 /* Clean up trailing underscores (if any) */ 1264 for (i = 3; i > 1; i--) { 1265 if (bus_id[i] == '_') 1266 bus_id[i] = '\0'; 1267 else 1268 break; 1269 } 1270 strscpy(device->pnp.bus_id, bus_id); 1271 break; 1272 } 1273 } 1274 1275 /* 1276 * acpi_ata_match - see if an acpi object is an ATA device 1277 * 1278 * If an acpi object has one of the ACPI ATA methods defined, 1279 * then we can safely call it an ATA device. 1280 */ 1281 bool acpi_ata_match(acpi_handle handle) 1282 { 1283 return acpi_has_method(handle, "_GTF") || 1284 acpi_has_method(handle, "_GTM") || 1285 acpi_has_method(handle, "_STM") || 1286 acpi_has_method(handle, "_SDD"); 1287 } 1288 1289 /* 1290 * acpi_bay_match - see if an acpi object is an ejectable driver bay 1291 * 1292 * If an acpi object is ejectable and has one of the ACPI ATA methods defined, 1293 * then we can safely call it an ejectable drive bay 1294 */ 1295 bool acpi_bay_match(acpi_handle handle) 1296 { 1297 acpi_handle phandle; 1298 1299 if (!acpi_has_method(handle, "_EJ0")) 1300 return false; 1301 if (acpi_ata_match(handle)) 1302 return true; 1303 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle))) 1304 return false; 1305 1306 return acpi_ata_match(phandle); 1307 } 1308 1309 bool acpi_device_is_battery(struct acpi_device *adev) 1310 { 1311 struct acpi_hardware_id *hwid; 1312 1313 list_for_each_entry(hwid, &adev->pnp.ids, list) 1314 if (!strcmp("PNP0C0A", hwid->id)) 1315 return true; 1316 1317 return false; 1318 } 1319 1320 static bool is_ejectable_bay(struct acpi_device *adev) 1321 { 1322 acpi_handle handle = adev->handle; 1323 1324 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev)) 1325 return true; 1326 1327 return acpi_bay_match(handle); 1328 } 1329 1330 /* 1331 * acpi_dock_match - see if an acpi object has a _DCK method 1332 */ 1333 bool acpi_dock_match(acpi_handle handle) 1334 { 1335 return acpi_has_method(handle, "_DCK"); 1336 } 1337 1338 static acpi_status 1339 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context, 1340 void **return_value) 1341 { 1342 long *cap = context; 1343 1344 if (acpi_has_method(handle, "_BCM") && 1345 acpi_has_method(handle, "_BCL")) { 1346 acpi_handle_debug(handle, "Found generic backlight support\n"); 1347 *cap |= ACPI_VIDEO_BACKLIGHT; 1348 /* We have backlight support, no need to scan further */ 1349 return AE_CTRL_TERMINATE; 1350 } 1351 return 0; 1352 } 1353 1354 /* Returns true if the ACPI object is a video device which can be 1355 * handled by video.ko. 1356 * The device will get a Linux specific CID added in scan.c to 1357 * identify the device as an ACPI graphics device 1358 * Be aware that the graphics device may not be physically present 1359 */ 1360 long acpi_is_video_device(acpi_handle handle) 1361 { 1362 long video_caps = 0; 1363 1364 /* Is this device able to support video switching ? */ 1365 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS")) 1366 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING; 1367 1368 /* Is this device able to retrieve a video ROM ? */ 1369 if (acpi_has_method(handle, "_ROM")) 1370 video_caps |= ACPI_VIDEO_ROM_AVAILABLE; 1371 1372 /* Is this device able to configure which video head to be POSTed ? */ 1373 if (acpi_has_method(handle, "_VPO") && 1374 acpi_has_method(handle, "_GPD") && 1375 acpi_has_method(handle, "_SPD")) 1376 video_caps |= ACPI_VIDEO_DEVICE_POSTING; 1377 1378 /* Only check for backlight functionality if one of the above hit. */ 1379 if (video_caps) 1380 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1381 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL, 1382 &video_caps, NULL); 1383 1384 return video_caps; 1385 } 1386 EXPORT_SYMBOL(acpi_is_video_device); 1387 1388 const char *acpi_device_hid(struct acpi_device *device) 1389 { 1390 struct acpi_hardware_id *hid; 1391 1392 hid = list_first_entry_or_null(&device->pnp.ids, struct acpi_hardware_id, list); 1393 if (!hid) 1394 return dummy_hid; 1395 1396 return hid->id; 1397 } 1398 EXPORT_SYMBOL(acpi_device_hid); 1399 1400 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id) 1401 { 1402 struct acpi_hardware_id *id; 1403 1404 id = kmalloc_obj(*id); 1405 if (!id) 1406 return; 1407 1408 id->id = kstrdup_const(dev_id, GFP_KERNEL); 1409 if (!id->id) { 1410 kfree(id); 1411 return; 1412 } 1413 1414 list_add_tail(&id->list, &pnp->ids); 1415 pnp->type.hardware_id = 1; 1416 } 1417 1418 /* 1419 * Old IBM workstations have a DSDT bug wherein the SMBus object 1420 * lacks the SMBUS01 HID and the methods do not have the necessary "_" 1421 * prefix. Work around this. 1422 */ 1423 static bool acpi_ibm_smbus_match(acpi_handle handle) 1424 { 1425 char node_name[ACPI_PATH_SEGMENT_LENGTH]; 1426 struct acpi_buffer path = { sizeof(node_name), node_name }; 1427 1428 if (!dmi_name_in_vendors("IBM")) 1429 return false; 1430 1431 /* Look for SMBS object */ 1432 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) || 1433 strcmp("SMBS", path.pointer)) 1434 return false; 1435 1436 /* Does it have the necessary (but misnamed) methods? */ 1437 if (acpi_has_method(handle, "SBI") && 1438 acpi_has_method(handle, "SBR") && 1439 acpi_has_method(handle, "SBW")) 1440 return true; 1441 1442 return false; 1443 } 1444 1445 static bool acpi_object_is_system_bus(acpi_handle handle) 1446 { 1447 acpi_handle tmp; 1448 1449 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) && 1450 tmp == handle) 1451 return true; 1452 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) && 1453 tmp == handle) 1454 return true; 1455 1456 return false; 1457 } 1458 1459 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp, 1460 int device_type) 1461 { 1462 struct acpi_device_info *info = NULL; 1463 struct acpi_pnp_device_id_list *cid_list; 1464 int i; 1465 1466 switch (device_type) { 1467 case ACPI_BUS_TYPE_DEVICE: 1468 if (handle == ACPI_ROOT_OBJECT) { 1469 acpi_add_id(pnp, ACPI_SYSTEM_HID); 1470 break; 1471 } 1472 1473 acpi_get_object_info(handle, &info); 1474 if (!info) { 1475 pr_err("%s: Error reading device info\n", __func__); 1476 return; 1477 } 1478 1479 if (info->valid & ACPI_VALID_HID) { 1480 acpi_add_id(pnp, info->hardware_id.string); 1481 pnp->type.platform_id = 1; 1482 } 1483 if (info->valid & ACPI_VALID_CID) { 1484 cid_list = &info->compatible_id_list; 1485 for (i = 0; i < cid_list->count; i++) 1486 acpi_add_id(pnp, cid_list->ids[i].string); 1487 } 1488 if (info->valid & ACPI_VALID_ADR) { 1489 pnp->bus_address = info->address; 1490 pnp->type.bus_address = 1; 1491 } 1492 if (info->valid & ACPI_VALID_UID) 1493 pnp->unique_id = kstrdup(info->unique_id.string, 1494 GFP_KERNEL); 1495 if (info->valid & ACPI_VALID_CLS) 1496 acpi_add_id(pnp, info->class_code.string); 1497 1498 kfree(info); 1499 1500 /* 1501 * Some devices don't reliably have _HIDs & _CIDs, so add 1502 * synthetic HIDs to make sure drivers can find them. 1503 */ 1504 if (acpi_is_video_device(handle)) { 1505 acpi_add_id(pnp, ACPI_VIDEO_HID); 1506 pnp->type.backlight = 1; 1507 break; 1508 } 1509 if (acpi_bay_match(handle)) 1510 acpi_add_id(pnp, ACPI_BAY_HID); 1511 else if (acpi_dock_match(handle)) 1512 acpi_add_id(pnp, ACPI_DOCK_HID); 1513 else if (acpi_ibm_smbus_match(handle)) 1514 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID); 1515 else if (list_empty(&pnp->ids) && 1516 acpi_object_is_system_bus(handle)) { 1517 /* \_SB, \_TZ, LNXSYBUS */ 1518 acpi_add_id(pnp, ACPI_BUS_HID); 1519 } 1520 1521 break; 1522 case ACPI_BUS_TYPE_POWER: 1523 acpi_add_id(pnp, ACPI_POWER_HID); 1524 break; 1525 case ACPI_BUS_TYPE_PROCESSOR: 1526 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID); 1527 break; 1528 case ACPI_BUS_TYPE_THERMAL: 1529 acpi_add_id(pnp, ACPI_THERMAL_HID); 1530 pnp->type.platform_id = 1; 1531 break; 1532 case ACPI_BUS_TYPE_POWER_BUTTON: 1533 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF); 1534 break; 1535 case ACPI_BUS_TYPE_SLEEP_BUTTON: 1536 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF); 1537 break; 1538 case ACPI_BUS_TYPE_ECDT_EC: 1539 acpi_add_id(pnp, ACPI_ECDT_HID); 1540 break; 1541 } 1542 } 1543 1544 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp) 1545 { 1546 struct acpi_hardware_id *id, *tmp; 1547 1548 list_for_each_entry_safe(id, tmp, &pnp->ids, list) { 1549 kfree_const(id->id); 1550 kfree(id); 1551 } 1552 kfree(pnp->unique_id); 1553 } 1554 1555 /** 1556 * acpi_dma_supported - Check DMA support for the specified device. 1557 * @adev: The pointer to acpi device 1558 * 1559 * Return false if DMA is not supported. Otherwise, return true 1560 */ 1561 bool acpi_dma_supported(const struct acpi_device *adev) 1562 { 1563 if (!adev) 1564 return false; 1565 1566 if (adev->flags.cca_seen) 1567 return true; 1568 1569 /* 1570 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent 1571 * DMA on "Intel platforms". Presumably that includes all x86 and 1572 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y. 1573 */ 1574 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED)) 1575 return true; 1576 1577 return false; 1578 } 1579 1580 /** 1581 * acpi_get_dma_attr - Check the supported DMA attr for the specified device. 1582 * @adev: The pointer to acpi device 1583 * 1584 * Return enum dev_dma_attr. 1585 */ 1586 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev) 1587 { 1588 if (!acpi_dma_supported(adev)) 1589 return DEV_DMA_NOT_SUPPORTED; 1590 1591 if (adev->flags.coherent_dma) 1592 return DEV_DMA_COHERENT; 1593 else 1594 return DEV_DMA_NON_COHERENT; 1595 } 1596 1597 /** 1598 * acpi_dma_get_range() - Get device DMA parameters. 1599 * 1600 * @dev: device to configure 1601 * @map: pointer to DMA ranges result 1602 * 1603 * Evaluate DMA regions and return pointer to DMA regions on 1604 * parsing success; it does not update the passed in values on failure. 1605 * 1606 * Return 0 on success, < 0 on failure. 1607 */ 1608 int acpi_dma_get_range(struct device *dev, const struct bus_dma_region **map) 1609 { 1610 struct acpi_device *adev; 1611 LIST_HEAD(list); 1612 struct resource_entry *rentry; 1613 int ret; 1614 struct device *dma_dev = dev; 1615 struct bus_dma_region *r; 1616 1617 /* 1618 * Walk the device tree chasing an ACPI companion with a _DMA 1619 * object while we go. Stop if we find a device with an ACPI 1620 * companion containing a _DMA method. 1621 */ 1622 do { 1623 adev = ACPI_COMPANION(dma_dev); 1624 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA)) 1625 break; 1626 1627 dma_dev = dma_dev->parent; 1628 } while (dma_dev); 1629 1630 if (!dma_dev) 1631 return -ENODEV; 1632 1633 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) { 1634 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n"); 1635 return -EINVAL; 1636 } 1637 1638 ret = acpi_dev_get_dma_resources(adev, &list); 1639 if (ret > 0) { 1640 r = kzalloc_objs(*r, ret + 1); 1641 if (!r) { 1642 ret = -ENOMEM; 1643 goto out; 1644 } 1645 1646 *map = r; 1647 1648 list_for_each_entry(rentry, &list, node) { 1649 if (rentry->res->start >= rentry->res->end) { 1650 kfree(*map); 1651 *map = NULL; 1652 ret = -EINVAL; 1653 dev_dbg(dma_dev, "Invalid DMA regions configuration\n"); 1654 goto out; 1655 } 1656 1657 r->cpu_start = rentry->res->start; 1658 r->dma_start = rentry->res->start - rentry->offset; 1659 r->size = resource_size(rentry->res); 1660 r++; 1661 } 1662 } 1663 out: 1664 acpi_dev_free_resource_list(&list); 1665 1666 return ret >= 0 ? 0 : ret; 1667 } 1668 1669 #ifdef CONFIG_IOMMU_API 1670 int acpi_iommu_fwspec_init(struct device *dev, u32 id, 1671 struct fwnode_handle *fwnode) 1672 { 1673 int ret; 1674 1675 ret = iommu_fwspec_init(dev, fwnode); 1676 if (ret) 1677 return ret; 1678 1679 return iommu_fwspec_add_ids(dev, &id, 1); 1680 } 1681 1682 static int acpi_iommu_configure_id(struct device *dev, const u32 *id_in) 1683 { 1684 int err; 1685 1686 /* Serialise to make dev->iommu stable under our potential fwspec */ 1687 mutex_lock(&iommu_probe_device_lock); 1688 /* If we already translated the fwspec there is nothing left to do */ 1689 if (dev_iommu_fwspec_get(dev)) { 1690 mutex_unlock(&iommu_probe_device_lock); 1691 return 0; 1692 } 1693 1694 err = iort_iommu_configure_id(dev, id_in); 1695 if (err && err != -EPROBE_DEFER) 1696 err = rimt_iommu_configure_id(dev, id_in); 1697 if (err && err != -EPROBE_DEFER) 1698 err = iovt_iommu_configure_id(dev, id_in); 1699 if (err && err != -EPROBE_DEFER) 1700 err = viot_iommu_configure(dev); 1701 1702 mutex_unlock(&iommu_probe_device_lock); 1703 1704 return err; 1705 } 1706 1707 #else /* !CONFIG_IOMMU_API */ 1708 1709 int acpi_iommu_fwspec_init(struct device *dev, u32 id, 1710 struct fwnode_handle *fwnode) 1711 { 1712 return -ENODEV; 1713 } 1714 1715 static int acpi_iommu_configure_id(struct device *dev, const u32 *id_in) 1716 { 1717 return -ENODEV; 1718 } 1719 1720 #endif /* !CONFIG_IOMMU_API */ 1721 1722 /** 1723 * acpi_dma_configure_id - Set-up DMA configuration for the device. 1724 * @dev: The pointer to the device 1725 * @attr: device dma attributes 1726 * @input_id: input device id const value pointer 1727 */ 1728 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr, 1729 const u32 *input_id) 1730 { 1731 int ret; 1732 1733 if (attr == DEV_DMA_NOT_SUPPORTED) { 1734 set_dma_ops(dev, &dma_dummy_ops); 1735 return 0; 1736 } 1737 1738 acpi_arch_dma_setup(dev); 1739 1740 /* Ignore all other errors apart from EPROBE_DEFER */ 1741 ret = acpi_iommu_configure_id(dev, input_id); 1742 if (ret == -EPROBE_DEFER) 1743 return -EPROBE_DEFER; 1744 if (ret) 1745 dev_dbg(dev, "Adding to IOMMU failed: %d\n", ret); 1746 1747 arch_setup_dma_ops(dev, attr == DEV_DMA_COHERENT); 1748 1749 return 0; 1750 } 1751 EXPORT_SYMBOL_GPL(acpi_dma_configure_id); 1752 1753 static void acpi_init_coherency(struct acpi_device *adev) 1754 { 1755 unsigned long long cca = 0; 1756 acpi_status status; 1757 struct acpi_device *parent = acpi_dev_parent(adev); 1758 1759 if (parent && parent->flags.cca_seen) { 1760 /* 1761 * From ACPI spec, OSPM will ignore _CCA if an ancestor 1762 * already saw one. 1763 */ 1764 adev->flags.cca_seen = 1; 1765 cca = parent->flags.coherent_dma; 1766 } else { 1767 status = acpi_evaluate_integer(adev->handle, "_CCA", 1768 NULL, &cca); 1769 if (ACPI_SUCCESS(status)) 1770 adev->flags.cca_seen = 1; 1771 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED)) 1772 /* 1773 * If architecture does not specify that _CCA is 1774 * required for DMA-able devices (e.g. x86), 1775 * we default to _CCA=1. 1776 */ 1777 cca = 1; 1778 else 1779 acpi_handle_debug(adev->handle, 1780 "ACPI device is missing _CCA.\n"); 1781 } 1782 1783 adev->flags.coherent_dma = cca; 1784 } 1785 1786 static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data) 1787 { 1788 bool *is_serial_bus_slave_p = data; 1789 1790 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 1791 return 1; 1792 1793 *is_serial_bus_slave_p = true; 1794 1795 /* no need to do more checking */ 1796 return -1; 1797 } 1798 1799 static bool acpi_is_indirect_io_slave(struct acpi_device *device) 1800 { 1801 struct acpi_device *parent = acpi_dev_parent(device); 1802 static const struct acpi_device_id indirect_io_hosts[] = { 1803 {"HISI0191", 0}, 1804 {} 1805 }; 1806 1807 return parent && !acpi_match_device_ids(parent, indirect_io_hosts); 1808 } 1809 1810 static bool acpi_device_enumeration_by_parent(struct acpi_device *device) 1811 { 1812 struct list_head resource_list; 1813 bool is_serial_bus_slave = false; 1814 static const struct acpi_device_id ignore_serial_bus_ids[] = { 1815 /* 1816 * These devices have multiple SerialBus resources and a client 1817 * device must be instantiated for each of them, each with 1818 * its own device id. 1819 * Normally we only instantiate one client device for the first 1820 * resource, using the ACPI HID as id. These special cases are handled 1821 * by the drivers/platform/x86/serial-multi-instantiate.c driver, which 1822 * knows which client device id to use for each resource. 1823 */ 1824 {"AWDZ8399", }, 1825 {"BSG1160", }, 1826 {"BSG2150", }, 1827 {"CSC3551", }, 1828 {"CSC3554", }, 1829 {"CSC3556", }, 1830 {"CSC3557", }, 1831 {"INT33FE", }, 1832 {"INT3515", }, 1833 {"TXNW2781", }, 1834 /* Non-conforming _HID for Cirrus Logic already released */ 1835 {"CLSA0100", }, 1836 {"CLSA0101", }, 1837 {"CLSA0102", }, 1838 /* 1839 * Some ACPI devs contain SerialBus resources even though they are not 1840 * attached to a serial bus at all. 1841 */ 1842 {"MSHW0028", }, 1843 /* 1844 * HIDs of device with an UartSerialBusV2 resource for which userspace 1845 * expects a regular tty cdev to be created (instead of the in kernel 1846 * serdev) and which have a kernel driver which expects a platform_dev 1847 * such as the rfkill-gpio driver. 1848 */ 1849 {"BCM4752", }, 1850 {"LNV4752", }, 1851 {} 1852 }; 1853 1854 if (acpi_is_indirect_io_slave(device)) 1855 return true; 1856 1857 /* Macs use device properties in lieu of _CRS resources */ 1858 if (x86_apple_machine && 1859 (fwnode_property_present(&device->fwnode, "spiSclkPeriod") || 1860 fwnode_property_present(&device->fwnode, "i2cAddress") || 1861 fwnode_property_present(&device->fwnode, "baud"))) 1862 return true; 1863 1864 if (acpi_dev_is_video_device(device)) 1865 return false; 1866 1867 if (!acpi_match_device_ids(device, ignore_serial_bus_ids)) 1868 return false; 1869 1870 INIT_LIST_HEAD(&resource_list); 1871 acpi_dev_get_resources(device, &resource_list, 1872 acpi_check_serial_bus_slave, 1873 &is_serial_bus_slave); 1874 acpi_dev_free_resource_list(&resource_list); 1875 1876 return is_serial_bus_slave; 1877 } 1878 1879 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, 1880 int type, void (*release)(struct device *)) 1881 { 1882 struct acpi_device *parent = acpi_find_parent_acpi_dev(handle); 1883 1884 INIT_LIST_HEAD(&device->pnp.ids); 1885 device->device_type = type; 1886 device->handle = handle; 1887 device->dev.parent = parent ? &parent->dev : NULL; 1888 device->dev.release = release; 1889 device->dev.bus = &acpi_bus_type; 1890 device->dev.groups = acpi_groups; 1891 device_set_pm_not_required(&device->dev); 1892 fwnode_init(&device->fwnode, &acpi_device_fwnode_ops); 1893 acpi_set_device_status(device, ACPI_STA_DEFAULT); 1894 acpi_device_get_busid(device); 1895 acpi_set_pnp_ids(handle, &device->pnp, type); 1896 acpi_init_properties(device); 1897 acpi_bus_get_flags(device); 1898 device->flags.initialized = true; 1899 device->flags.enumeration_by_parent = 1900 acpi_device_enumeration_by_parent(device); 1901 acpi_device_clear_enumerated(device); 1902 device_initialize(&device->dev); 1903 dev_set_uevent_suppress(&device->dev, true); 1904 acpi_init_coherency(device); 1905 } 1906 1907 static void acpi_scan_dep_init(struct acpi_device *adev) 1908 { 1909 struct acpi_dep_data *dep; 1910 1911 list_for_each_entry(dep, &acpi_dep_list, node) { 1912 if (dep->consumer == adev->handle) { 1913 if (dep->honor_dep) 1914 adev->flags.honor_deps = 1; 1915 1916 if (!dep->met) 1917 adev->dep_unmet++; 1918 } 1919 } 1920 } 1921 1922 void acpi_device_add_finalize(struct acpi_device *device) 1923 { 1924 dev_set_uevent_suppress(&device->dev, false); 1925 kobject_uevent(&device->dev.kobj, KOBJ_ADD); 1926 } 1927 1928 static void acpi_scan_init_status(struct acpi_device *adev) 1929 { 1930 if (acpi_bus_get_status(adev)) 1931 acpi_set_device_status(adev, 0); 1932 } 1933 1934 static int acpi_add_single_object(struct acpi_device **child, 1935 acpi_handle handle, int type, bool dep_init) 1936 { 1937 struct acpi_device *device; 1938 bool release_dep_lock = false; 1939 int result; 1940 1941 device = kzalloc_obj(struct acpi_device); 1942 if (!device) 1943 return -ENOMEM; 1944 1945 acpi_init_device_object(device, handle, type, acpi_device_release); 1946 /* 1947 * Getting the status is delayed till here so that we can call 1948 * acpi_bus_get_status() and use its quirk handling. Note that 1949 * this must be done before the get power-/wakeup_dev-flags calls. 1950 */ 1951 if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) { 1952 if (dep_init) { 1953 mutex_lock(&acpi_dep_list_lock); 1954 /* 1955 * Hold the lock until the acpi_tie_acpi_dev() call 1956 * below to prevent concurrent acpi_scan_clear_dep() 1957 * from deleting a dependency list entry without 1958 * updating dep_unmet for the device. 1959 */ 1960 release_dep_lock = true; 1961 acpi_scan_dep_init(device); 1962 } 1963 acpi_scan_init_status(device); 1964 } 1965 1966 acpi_bus_get_power_flags(device); 1967 acpi_bus_get_wakeup_device_flags(device); 1968 1969 result = acpi_tie_acpi_dev(device); 1970 1971 if (release_dep_lock) 1972 mutex_unlock(&acpi_dep_list_lock); 1973 1974 if (!result) 1975 result = acpi_device_add(device); 1976 1977 if (result) { 1978 acpi_dev_put(device); 1979 return result; 1980 } 1981 1982 acpi_power_add_remove_device(device, true); 1983 acpi_device_add_finalize(device); 1984 1985 acpi_handle_debug(handle, "Added as %s, parent %s\n", 1986 dev_name(&device->dev), device->dev.parent ? 1987 dev_name(device->dev.parent) : "(null)"); 1988 1989 *child = device; 1990 return 0; 1991 } 1992 1993 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares, 1994 void *context) 1995 { 1996 struct resource *res = context; 1997 1998 if (acpi_dev_resource_memory(ares, res)) 1999 return AE_CTRL_TERMINATE; 2000 2001 return AE_OK; 2002 } 2003 2004 static bool acpi_device_should_be_hidden(acpi_handle handle) 2005 { 2006 acpi_status status; 2007 struct resource res; 2008 2009 /* Check if it should ignore the UART device */ 2010 if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS))) 2011 return false; 2012 2013 /* 2014 * The UART device described in SPCR table is assumed to have only one 2015 * memory resource present. So we only look for the first one here. 2016 */ 2017 status = acpi_walk_resources(handle, METHOD_NAME__CRS, 2018 acpi_get_resource_memory, &res); 2019 if (ACPI_FAILURE(status) || res.start != spcr_uart_addr) 2020 return false; 2021 2022 acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n", 2023 &res.start); 2024 2025 return true; 2026 } 2027 2028 bool acpi_device_is_present(const struct acpi_device *adev) 2029 { 2030 return adev->status.present || adev->status.functional; 2031 } 2032 2033 bool acpi_device_is_enabled(const struct acpi_device *adev) 2034 { 2035 return adev->status.enabled; 2036 } 2037 2038 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler, 2039 const char *idstr, 2040 const struct acpi_device_id **matchid) 2041 { 2042 const struct acpi_device_id *devid; 2043 2044 if (handler->match) 2045 return handler->match(idstr, matchid); 2046 2047 for (devid = handler->ids; devid->id[0]; devid++) 2048 if (!strcmp((char *)devid->id, idstr)) { 2049 if (matchid) 2050 *matchid = devid; 2051 2052 return true; 2053 } 2054 2055 return false; 2056 } 2057 2058 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr, 2059 const struct acpi_device_id **matchid) 2060 { 2061 struct acpi_scan_handler *handler; 2062 2063 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) 2064 if (acpi_scan_handler_matching(handler, idstr, matchid)) 2065 return handler; 2066 2067 return NULL; 2068 } 2069 2070 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val) 2071 { 2072 if (!!hotplug->enabled == !!val) 2073 return; 2074 2075 mutex_lock(&acpi_scan_lock); 2076 2077 hotplug->enabled = val; 2078 2079 mutex_unlock(&acpi_scan_lock); 2080 } 2081 2082 int acpi_scan_add_dep(acpi_handle handle, struct acpi_handle_list *dep_devices) 2083 { 2084 u32 count; 2085 int i; 2086 2087 for (count = 0, i = 0; i < dep_devices->count; i++) { 2088 struct acpi_device_info *info; 2089 struct acpi_dep_data *dep; 2090 bool skip, honor_dep; 2091 acpi_status status; 2092 2093 status = acpi_get_object_info(dep_devices->handles[i], &info); 2094 if (ACPI_FAILURE(status)) { 2095 acpi_handle_debug(handle, "Error reading _DEP device info\n"); 2096 continue; 2097 } 2098 2099 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids); 2100 honor_dep = acpi_info_matches_ids(info, acpi_honor_dep_ids); 2101 kfree(info); 2102 2103 if (skip) 2104 continue; 2105 2106 dep = kzalloc_obj(*dep); 2107 if (!dep) 2108 continue; 2109 2110 count++; 2111 2112 dep->supplier = dep_devices->handles[i]; 2113 dep->consumer = handle; 2114 dep->honor_dep = honor_dep; 2115 2116 mutex_lock(&acpi_dep_list_lock); 2117 list_add_tail(&dep->node, &acpi_dep_list); 2118 mutex_unlock(&acpi_dep_list_lock); 2119 } 2120 2121 acpi_handle_list_free(dep_devices); 2122 return count; 2123 } 2124 2125 static void acpi_scan_init_hotplug(struct acpi_device *adev) 2126 { 2127 struct acpi_hardware_id *hwid; 2128 2129 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) { 2130 acpi_dock_add(adev); 2131 return; 2132 } 2133 list_for_each_entry(hwid, &adev->pnp.ids, list) { 2134 struct acpi_scan_handler *handler; 2135 2136 handler = acpi_scan_match_handler(hwid->id, NULL); 2137 if (handler) { 2138 adev->flags.hotplug_notify = true; 2139 break; 2140 } 2141 } 2142 } 2143 2144 u32 __weak arch_acpi_add_auto_dep(acpi_handle handle) { return 0; } 2145 2146 static u32 acpi_scan_check_dep(acpi_handle handle) 2147 { 2148 struct acpi_handle_list dep_devices; 2149 u32 count = 0; 2150 2151 /* 2152 * Some architectures like RISC-V need to add dependencies for 2153 * all devices which use GSI to the interrupt controller so that 2154 * interrupt controller is probed before any of those devices. 2155 * Instead of mandating _DEP on all the devices, detect the 2156 * dependency and add automatically. 2157 */ 2158 count += arch_acpi_add_auto_dep(handle); 2159 2160 /* 2161 * Check for _HID here to avoid deferring the enumeration of: 2162 * 1. PCI devices. 2163 * 2. ACPI nodes describing USB ports. 2164 * Still, checking for _HID catches more then just these cases ... 2165 */ 2166 if (!acpi_has_method(handle, "_DEP") || !acpi_has_method(handle, "_HID")) 2167 return count; 2168 2169 if (!acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices)) { 2170 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n"); 2171 return count; 2172 } 2173 2174 count += acpi_scan_add_dep(handle, &dep_devices); 2175 return count; 2176 } 2177 2178 static acpi_status acpi_scan_check_crs_csi2_cb(acpi_handle handle, u32 a, void *b, void **c) 2179 { 2180 acpi_mipi_check_crs_csi2(handle); 2181 return AE_OK; 2182 } 2183 2184 static acpi_status acpi_bus_check_add(acpi_handle handle, bool first_pass, 2185 struct acpi_device **adev_p) 2186 { 2187 struct acpi_device *device = acpi_fetch_acpi_dev(handle); 2188 acpi_object_type acpi_type; 2189 int type; 2190 2191 if (device) 2192 goto out; 2193 2194 if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type))) 2195 return AE_OK; 2196 2197 switch (acpi_type) { 2198 case ACPI_TYPE_DEVICE: 2199 if (acpi_device_should_be_hidden(handle)) 2200 return AE_OK; 2201 2202 if (first_pass) { 2203 acpi_mipi_check_crs_csi2(handle); 2204 2205 /* Bail out if there are dependencies. */ 2206 if (acpi_scan_check_dep(handle) > 0) { 2207 /* 2208 * The entire CSI-2 connection graph needs to be 2209 * extracted before any drivers or scan handlers 2210 * are bound to struct device objects, so scan 2211 * _CRS CSI-2 resource descriptors for all 2212 * devices below the current handle. 2213 */ 2214 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 2215 ACPI_UINT32_MAX, 2216 acpi_scan_check_crs_csi2_cb, 2217 NULL, NULL, NULL); 2218 return AE_CTRL_DEPTH; 2219 } 2220 } 2221 2222 fallthrough; 2223 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ 2224 type = ACPI_BUS_TYPE_DEVICE; 2225 break; 2226 2227 case ACPI_TYPE_PROCESSOR: 2228 type = ACPI_BUS_TYPE_PROCESSOR; 2229 break; 2230 2231 case ACPI_TYPE_THERMAL: 2232 type = ACPI_BUS_TYPE_THERMAL; 2233 break; 2234 2235 case ACPI_TYPE_POWER: 2236 acpi_add_power_resource(handle); 2237 fallthrough; 2238 default: 2239 return AE_OK; 2240 } 2241 2242 /* 2243 * If first_pass is true at this point, the device has no dependencies, 2244 * or the creation of the device object would have been postponed above. 2245 */ 2246 acpi_add_single_object(&device, handle, type, !first_pass); 2247 if (!device) 2248 return AE_CTRL_DEPTH; 2249 2250 acpi_scan_init_hotplug(device); 2251 2252 out: 2253 if (!*adev_p) 2254 *adev_p = device; 2255 2256 return AE_OK; 2257 } 2258 2259 static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used, 2260 void *not_used, void **ret_p) 2261 { 2262 return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p); 2263 } 2264 2265 static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used, 2266 void *not_used, void **ret_p) 2267 { 2268 return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p); 2269 } 2270 2271 static void acpi_video_bus_device_release(struct device *dev) 2272 { 2273 struct auxiliary_device *aux_dev = to_auxiliary_dev(dev); 2274 2275 kfree(aux_dev); 2276 } 2277 2278 static void acpi_create_video_bus_device(struct acpi_device *adev, 2279 struct acpi_device *parent) 2280 { 2281 struct auxiliary_device *aux_dev; 2282 static unsigned int aux_dev_id; 2283 2284 struct device *phys_parent __free(put_device) = acpi_bus_get_primary_device(parent); 2285 if (!phys_parent) 2286 return; 2287 2288 aux_dev = kzalloc_obj(*aux_dev); 2289 if (!aux_dev) 2290 return; 2291 2292 aux_dev->id = aux_dev_id++; 2293 aux_dev->name = "video_bus"; 2294 aux_dev->dev.parent = phys_parent; 2295 aux_dev->dev.release = acpi_video_bus_device_release; 2296 2297 if (auxiliary_device_init(aux_dev)) { 2298 kfree(aux_dev); 2299 return; 2300 } 2301 2302 ACPI_COMPANION_SET(&aux_dev->dev, adev); 2303 if (__auxiliary_device_add(aux_dev, "acpi")) 2304 auxiliary_device_uninit(aux_dev); 2305 } 2306 2307 struct acpi_scan_system_dev { 2308 struct list_head node; 2309 struct acpi_device *adev; 2310 }; 2311 2312 static const char * const acpi_system_dev_ids[] = { 2313 "PNP0C01", /* Memory controller */ 2314 "PNP0C02", /* Motherboard resource */ 2315 NULL 2316 }; 2317 2318 static void acpi_default_enumeration(struct acpi_device *device) 2319 { 2320 /* 2321 * Do not enumerate devices with enumeration_by_parent flag set as 2322 * they will be enumerated by their respective parents. 2323 */ 2324 if (device->flags.enumeration_by_parent) { 2325 blocking_notifier_call_chain(&acpi_reconfig_chain, 2326 ACPI_RECONFIG_DEVICE_ADD, device); 2327 return; 2328 } 2329 if (match_string(acpi_system_dev_ids, -1, acpi_device_hid(device)) >= 0) { 2330 struct acpi_scan_system_dev *sd; 2331 2332 /* 2333 * This is a generic system device, so there is no need to 2334 * create a platform device for it, but its resources need to be 2335 * reserved. However, that needs to be done after all of the 2336 * other device objects have been processed and PCI has claimed 2337 * BARs in case there are resource conflicts. 2338 */ 2339 sd = kmalloc_obj(*sd); 2340 if (sd) { 2341 sd->adev = device; 2342 list_add_tail(&sd->node, &acpi_scan_system_dev_list); 2343 } 2344 } else if (device->pnp.type.backlight) { 2345 struct acpi_device *parent; 2346 2347 parent = acpi_dev_parent(device); 2348 if (parent) 2349 acpi_create_video_bus_device(device, parent); 2350 } else { 2351 /* For a regular device object, create a platform device. */ 2352 acpi_create_platform_device(device, NULL); 2353 } 2354 acpi_device_set_enumerated(device); 2355 } 2356 2357 static const struct acpi_device_id generic_device_ids[] = { 2358 {ACPI_DT_NAMESPACE_HID, }, 2359 {"", }, 2360 }; 2361 2362 static int acpi_generic_device_attach(struct acpi_device *adev, 2363 const struct acpi_device_id *not_used) 2364 { 2365 /* 2366 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test 2367 * below can be unconditional. 2368 */ 2369 if (adev->data.of_compatible) 2370 acpi_default_enumeration(adev); 2371 2372 return 1; 2373 } 2374 2375 static struct acpi_scan_handler generic_device_handler = { 2376 .ids = generic_device_ids, 2377 .attach = acpi_generic_device_attach, 2378 }; 2379 2380 static int acpi_scan_attach_handler(struct acpi_device *device) 2381 { 2382 struct acpi_hardware_id *hwid; 2383 int ret = 0; 2384 2385 list_for_each_entry(hwid, &device->pnp.ids, list) { 2386 const struct acpi_device_id *devid; 2387 struct acpi_scan_handler *handler; 2388 2389 handler = acpi_scan_match_handler(hwid->id, &devid); 2390 if (handler) { 2391 if (!handler->attach) { 2392 device->pnp.type.platform_id = 0; 2393 continue; 2394 } 2395 device->handler = handler; 2396 ret = handler->attach(device, devid); 2397 if (ret > 0) 2398 break; 2399 2400 device->handler = NULL; 2401 if (ret < 0) 2402 break; 2403 } 2404 } 2405 2406 return ret; 2407 } 2408 2409 static int acpi_bus_attach(struct acpi_device *device, void *first_pass) 2410 { 2411 bool skip = !first_pass && device->flags.visited; 2412 struct pci_dev *pci; 2413 acpi_handle ejd; 2414 int ret; 2415 2416 if (skip) 2417 goto ok; 2418 2419 device->flags.initialized = true; 2420 2421 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd))) 2422 register_dock_dependent_device(device, ejd); 2423 2424 acpi_bus_get_status(device); 2425 /* 2426 * If the given ACPI device object has been already associated with a 2427 * PCI device found on the bus, its status is effectively "present and 2428 * enabled", and dependencies are not tracked for PCI devices, so it is 2429 * not necessary or even useful to check the device's readiness in that 2430 * case. 2431 */ 2432 pci = acpi_dev_get_pci_dev(device); 2433 if (pci) { 2434 acpi_handle_debug(device->handle, "PCI companion %s found\n", 2435 pci_name(pci)); 2436 2437 if (!acpi_device_is_present(device)) 2438 pci_info(pci, FW_BUG "ACPI status differs from reality\n"); 2439 2440 pci_dev_put(pci); 2441 } else if (!acpi_dev_ready_for_enumeration(device)) { 2442 /* The device is not ready (e.g. not present), so skip it. */ 2443 acpi_device_clear_enumerated(device); 2444 return 0; 2445 } 2446 2447 if (device->handler) 2448 goto ok; 2449 2450 acpi_ec_register_opregions(device); 2451 2452 acpi_bus_init_power(device); 2453 2454 if (device->flags.visited) 2455 goto ok; 2456 2457 ret = acpi_scan_attach_handler(device); 2458 if (ret < 0) 2459 return 0; 2460 2461 if (device->flags.enumeration_by_parent || 2462 (!ret && (device->pnp.type.platform_id || device->pnp.type.backlight))) 2463 acpi_default_enumeration(device); 2464 else 2465 acpi_device_set_enumerated(device); 2466 2467 ok: 2468 acpi_dev_for_each_child(device, acpi_bus_attach, first_pass); 2469 2470 if (!skip && device->handler && device->handler->hotplug.notify_online) 2471 device->handler->hotplug.notify_online(device); 2472 2473 return 0; 2474 } 2475 2476 static int acpi_dev_get_next_consumer_dev_cb(struct acpi_dep_data *dep, void *data) 2477 { 2478 struct acpi_device **adev_p = data; 2479 struct acpi_device *adev = *adev_p; 2480 2481 /* 2482 * If we're passed a 'previous' consumer device then we need to skip 2483 * any consumers until we meet the previous one, and then NULL @data 2484 * so the next one can be returned. 2485 */ 2486 if (adev) { 2487 if (dep->consumer == adev->handle) 2488 *adev_p = NULL; 2489 2490 return 0; 2491 } 2492 2493 adev = acpi_get_acpi_dev(dep->consumer); 2494 if (adev) { 2495 *(struct acpi_device **)data = adev; 2496 return 1; 2497 } 2498 /* Continue parsing if the device object is not present. */ 2499 return 0; 2500 } 2501 2502 static void acpi_scan_clear_dep_fn(void *dev, async_cookie_t cookie) 2503 { 2504 struct acpi_device *adev = to_acpi_device(dev); 2505 2506 acpi_scan_lock_acquire(); 2507 acpi_bus_attach(adev, (void *)true); 2508 acpi_scan_lock_release(); 2509 2510 acpi_dev_put(adev); 2511 } 2512 2513 static bool acpi_scan_clear_dep_queue(struct acpi_device *adev) 2514 { 2515 if (adev->dep_unmet) 2516 return false; 2517 2518 /* 2519 * Async schedule the deferred acpi_scan_clear_dep_fn() since: 2520 * - acpi_bus_attach() needs to hold acpi_scan_lock which cannot 2521 * be acquired under acpi_dep_list_lock (held here) 2522 * - the deferred work at boot stage is ensured to be finished 2523 * before userspace init task by the async_synchronize_full() 2524 * barrier 2525 * 2526 * Use _nocall variant since it'll return on failure instead of 2527 * run the function synchronously. 2528 */ 2529 return async_schedule_dev_nocall(acpi_scan_clear_dep_fn, &adev->dev); 2530 } 2531 2532 static void acpi_scan_delete_dep_data(struct acpi_dep_data *dep) 2533 { 2534 list_del(&dep->node); 2535 kfree(dep); 2536 } 2537 2538 static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data) 2539 { 2540 struct acpi_device *adev = acpi_get_acpi_dev(dep->consumer); 2541 2542 if (adev) { 2543 adev->dep_unmet--; 2544 if (!acpi_scan_clear_dep_queue(adev)) 2545 acpi_dev_put(adev); 2546 } 2547 2548 if (dep->free_when_met) 2549 acpi_scan_delete_dep_data(dep); 2550 else 2551 dep->met = true; 2552 2553 return 0; 2554 } 2555 2556 /** 2557 * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list 2558 * @handle: The ACPI handle of the supplier device 2559 * @callback: Pointer to the callback function to apply 2560 * @data: Pointer to some data to pass to the callback 2561 * 2562 * The return value of the callback determines this function's behaviour. If 0 2563 * is returned we continue to iterate over acpi_dep_list. If a positive value 2564 * is returned then the loop is broken but this function returns 0. If a 2565 * negative value is returned by the callback then the loop is broken and that 2566 * value is returned as the final error. 2567 */ 2568 static int acpi_walk_dep_device_list(acpi_handle handle, 2569 int (*callback)(struct acpi_dep_data *, void *), 2570 void *data) 2571 { 2572 struct acpi_dep_data *dep, *tmp; 2573 int ret = 0; 2574 2575 mutex_lock(&acpi_dep_list_lock); 2576 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) { 2577 if (dep->supplier == handle) { 2578 ret = callback(dep, data); 2579 if (ret) 2580 break; 2581 } 2582 } 2583 mutex_unlock(&acpi_dep_list_lock); 2584 2585 return ret > 0 ? 0 : ret; 2586 } 2587 2588 /** 2589 * acpi_dev_clear_dependencies - Inform consumers that the device is now active 2590 * @supplier: Pointer to the supplier &struct acpi_device 2591 * 2592 * Clear dependencies on the given device. 2593 */ 2594 void acpi_dev_clear_dependencies(struct acpi_device *supplier) 2595 { 2596 acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL); 2597 } 2598 EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies); 2599 2600 /** 2601 * acpi_dev_ready_for_enumeration - Check if the ACPI device is ready for enumeration 2602 * @device: Pointer to the &struct acpi_device to check 2603 * 2604 * Check if the device is present and has no unmet dependencies. 2605 * 2606 * Return true if the device is ready for enumeratino. Otherwise, return false. 2607 */ 2608 bool acpi_dev_ready_for_enumeration(const struct acpi_device *device) 2609 { 2610 if (device->flags.honor_deps && device->dep_unmet) 2611 return false; 2612 2613 return acpi_device_is_present(device); 2614 } 2615 EXPORT_SYMBOL_GPL(acpi_dev_ready_for_enumeration); 2616 2617 /** 2618 * acpi_dev_get_next_consumer_dev - Return the next adev dependent on @supplier 2619 * @supplier: Pointer to the dependee device 2620 * @start: Pointer to the current dependent device 2621 * 2622 * Returns the next &struct acpi_device which declares itself dependent on 2623 * @supplier via the _DEP buffer, parsed from the acpi_dep_list. 2624 * 2625 * If the returned adev is not passed as @start to this function, the caller is 2626 * responsible for putting the reference to adev when it is no longer needed. 2627 */ 2628 struct acpi_device *acpi_dev_get_next_consumer_dev(struct acpi_device *supplier, 2629 struct acpi_device *start) 2630 { 2631 struct acpi_device *adev = start; 2632 2633 acpi_walk_dep_device_list(supplier->handle, 2634 acpi_dev_get_next_consumer_dev_cb, &adev); 2635 2636 acpi_dev_put(start); 2637 2638 if (adev == start) 2639 return NULL; 2640 2641 return adev; 2642 } 2643 EXPORT_SYMBOL_GPL(acpi_dev_get_next_consumer_dev); 2644 2645 static void acpi_scan_postponed_branch(acpi_handle handle) 2646 { 2647 struct acpi_device *adev = NULL; 2648 2649 if (ACPI_FAILURE(acpi_bus_check_add(handle, false, &adev))) 2650 return; 2651 2652 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 2653 acpi_bus_check_add_2, NULL, NULL, (void **)&adev); 2654 2655 /* 2656 * Populate the ACPI _CRS CSI-2 software nodes for the ACPI devices that 2657 * have been added above. 2658 */ 2659 acpi_mipi_init_crs_csi2_swnodes(); 2660 2661 acpi_bus_attach(adev, NULL); 2662 } 2663 2664 static void acpi_scan_postponed(void) 2665 { 2666 struct acpi_dep_data *dep, *tmp; 2667 2668 mutex_lock(&acpi_dep_list_lock); 2669 2670 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) { 2671 acpi_handle handle = dep->consumer; 2672 2673 /* 2674 * In case there are multiple acpi_dep_list entries with the 2675 * same consumer, skip the current entry if the consumer device 2676 * object corresponding to it is present already. 2677 */ 2678 if (!acpi_fetch_acpi_dev(handle)) { 2679 /* 2680 * Even though the lock is released here, tmp is 2681 * guaranteed to be valid, because none of the list 2682 * entries following dep is marked as "free when met" 2683 * and so they cannot be deleted. 2684 */ 2685 mutex_unlock(&acpi_dep_list_lock); 2686 2687 acpi_scan_postponed_branch(handle); 2688 2689 mutex_lock(&acpi_dep_list_lock); 2690 } 2691 2692 if (dep->met) 2693 acpi_scan_delete_dep_data(dep); 2694 else 2695 dep->free_when_met = true; 2696 } 2697 2698 mutex_unlock(&acpi_dep_list_lock); 2699 } 2700 2701 static void acpi_scan_claim_resources(struct acpi_device *adev) 2702 { 2703 struct resource_entry *rentry; 2704 LIST_HEAD(resource_list); 2705 unsigned int count = 0; 2706 const char *regionid; 2707 2708 if (acpi_dev_get_resources(adev, &resource_list, NULL, NULL) <= 0) 2709 return; 2710 2711 regionid = kstrdup(dev_name(&adev->dev), GFP_KERNEL); 2712 if (!regionid) 2713 goto exit; 2714 2715 list_for_each_entry(rentry, &resource_list, node) { 2716 struct resource *res = rentry->res; 2717 struct resource *r; 2718 2719 /* Skip disabled and invalid resources. */ 2720 if ((res->flags & IORESOURCE_DISABLED) || res->end < res->start) 2721 continue; 2722 2723 if (resource_type(res) == IORESOURCE_IO) { 2724 /* 2725 * Follow the PNP system driver and on x86 skip I/O 2726 * resources that start below 0x100 (the "standard PC 2727 * hardware" boundary). 2728 */ 2729 if (IS_ENABLED(CONFIG_X86) && res->start < 0x100) { 2730 dev_info(&adev->dev, "Skipped %pR\n", res); 2731 continue; 2732 } 2733 r = request_region(res->start, resource_size(res), regionid); 2734 } else if (resource_type(res) == IORESOURCE_MEM) { 2735 r = request_mem_region(res->start, resource_size(res), regionid); 2736 } else { 2737 continue; 2738 } 2739 2740 if (r) { 2741 r->flags &= ~IORESOURCE_BUSY; 2742 dev_info(&adev->dev, "Reserved %pR\n", r); 2743 count++; 2744 } else { 2745 /* 2746 * Failures at this point are usually harmless. PCI 2747 * quirks, for example, reserve resources they know 2748 * about too, so there may well be double reservations. 2749 */ 2750 dev_info(&adev->dev, "Could not reserve %pR\n", res); 2751 } 2752 } 2753 2754 if (!count) 2755 kfree(regionid); 2756 2757 exit: 2758 acpi_dev_free_resource_list(&resource_list); 2759 } 2760 2761 static int __init acpi_reserve_motherboard_resources(void) 2762 { 2763 struct acpi_scan_system_dev *sd, *tmp; 2764 2765 guard(mutex)(&acpi_scan_lock); 2766 2767 list_for_each_entry_safe(sd, tmp, &acpi_scan_system_dev_list, node) { 2768 acpi_scan_claim_resources(sd->adev); 2769 list_del(&sd->node); 2770 kfree(sd); 2771 } 2772 2773 return 0; 2774 } 2775 2776 /* 2777 * Reserve motherboard resources after PCI claims BARs, but before PCI assigns 2778 * resources for uninitialized PCI devices. 2779 */ 2780 fs_initcall(acpi_reserve_motherboard_resources); 2781 2782 /** 2783 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope. 2784 * @handle: Root of the namespace scope to scan. 2785 * 2786 * Scan a given ACPI tree (probably recently hot-plugged) and create and add 2787 * found devices. 2788 * 2789 * If no devices were found, -ENODEV is returned, but it does not mean that 2790 * there has been a real error. There just have been no suitable ACPI objects 2791 * in the table trunk from which the kernel could create a device and add an 2792 * appropriate driver. 2793 * 2794 * Must be called under acpi_scan_lock. 2795 */ 2796 int acpi_bus_scan(acpi_handle handle) 2797 { 2798 struct acpi_device *device = NULL; 2799 2800 /* Pass 1: Avoid enumerating devices with missing dependencies. */ 2801 2802 if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device))) 2803 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 2804 acpi_bus_check_add_1, NULL, NULL, 2805 (void **)&device); 2806 2807 if (!device) 2808 return -ENODEV; 2809 2810 /* 2811 * Set up ACPI _CRS CSI-2 software nodes using information extracted 2812 * from the _CRS CSI-2 resource descriptors during the ACPI namespace 2813 * walk above and MIPI DisCo for Imaging device properties. 2814 */ 2815 acpi_mipi_scan_crs_csi2(); 2816 acpi_mipi_init_crs_csi2_swnodes(); 2817 2818 acpi_bus_attach(device, (void *)true); 2819 2820 /* Pass 2: Enumerate all of the remaining devices. */ 2821 2822 acpi_scan_postponed(); 2823 2824 acpi_mipi_crs_csi2_cleanup(); 2825 2826 return 0; 2827 } 2828 EXPORT_SYMBOL(acpi_bus_scan); 2829 2830 /** 2831 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects. 2832 * @adev: Root of the ACPI namespace scope to walk. 2833 * 2834 * Must be called under acpi_scan_lock. 2835 */ 2836 void acpi_bus_trim(struct acpi_device *adev) 2837 { 2838 uintptr_t flags = 0; 2839 2840 acpi_scan_check_and_detach(adev, (void *)flags); 2841 } 2842 EXPORT_SYMBOL_GPL(acpi_bus_trim); 2843 2844 int acpi_bus_register_early_device(int type) 2845 { 2846 struct acpi_device *device = NULL; 2847 int result; 2848 2849 result = acpi_add_single_object(&device, NULL, type, false); 2850 if (result) 2851 return result; 2852 2853 acpi_default_enumeration(device); 2854 return 0; 2855 } 2856 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device); 2857 2858 static void acpi_bus_add_fixed_device_object(enum acpi_bus_device_type type) 2859 { 2860 struct acpi_device *adev = NULL; 2861 2862 acpi_add_single_object(&adev, NULL, type, false); 2863 if (adev) 2864 acpi_default_enumeration(adev); 2865 } 2866 2867 static void acpi_bus_scan_fixed(void) 2868 { 2869 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) 2870 acpi_bus_add_fixed_device_object(ACPI_BUS_TYPE_POWER_BUTTON); 2871 2872 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) 2873 acpi_bus_add_fixed_device_object(ACPI_BUS_TYPE_SLEEP_BUTTON); 2874 } 2875 2876 static void __init acpi_get_spcr_uart_addr(void) 2877 { 2878 acpi_status status; 2879 struct acpi_table_spcr *spcr_ptr; 2880 2881 status = acpi_get_table(ACPI_SIG_SPCR, 0, 2882 (struct acpi_table_header **)&spcr_ptr); 2883 if (ACPI_FAILURE(status)) { 2884 pr_warn("STAO table present, but SPCR is missing\n"); 2885 return; 2886 } 2887 2888 spcr_uart_addr = spcr_ptr->serial_port.address; 2889 acpi_put_table((struct acpi_table_header *)spcr_ptr); 2890 } 2891 2892 static bool acpi_scan_initialized; 2893 2894 void __init acpi_scan_init(void) 2895 { 2896 acpi_status status; 2897 struct acpi_table_stao *stao_ptr; 2898 2899 acpi_pci_root_init(); 2900 acpi_pci_link_init(); 2901 acpi_processor_init(); 2902 acpi_platform_init(); 2903 acpi_lpss_init(); 2904 acpi_apd_init(); 2905 acpi_cmos_rtc_init(); 2906 acpi_container_init(); 2907 acpi_memory_hotplug_init(); 2908 acpi_watchdog_init(); 2909 acpi_pnp_init(); 2910 acpi_power_resources_init(); 2911 acpi_init_lpit(); 2912 2913 acpi_scan_add_handler(&generic_device_handler); 2914 2915 /* 2916 * If there is STAO table, check whether it needs to ignore the UART 2917 * device in SPCR table. 2918 */ 2919 status = acpi_get_table(ACPI_SIG_STAO, 0, 2920 (struct acpi_table_header **)&stao_ptr); 2921 if (ACPI_SUCCESS(status)) { 2922 if (stao_ptr->header.length > sizeof(struct acpi_table_stao)) 2923 pr_info("STAO Name List not yet supported.\n"); 2924 2925 if (stao_ptr->ignore_uart) 2926 acpi_get_spcr_uart_addr(); 2927 2928 acpi_put_table((struct acpi_table_header *)stao_ptr); 2929 } 2930 2931 acpi_gpe_apply_masked_gpes(); 2932 acpi_update_all_gpes(); 2933 2934 /* 2935 * Although we call __add_memory() that is documented to require the 2936 * device_hotplug_lock, it is not necessary here because this is an 2937 * early code when userspace or any other code path cannot trigger 2938 * hotplug/hotunplug operations. 2939 */ 2940 mutex_lock(&acpi_scan_lock); 2941 /* 2942 * Enumerate devices in the ACPI namespace. 2943 */ 2944 if (acpi_bus_scan(ACPI_ROOT_OBJECT)) 2945 goto unlock; 2946 2947 acpi_root = acpi_fetch_acpi_dev(ACPI_ROOT_OBJECT); 2948 if (!acpi_root) 2949 goto unlock; 2950 2951 /* Fixed feature devices do not exist on HW-reduced platform */ 2952 if (!acpi_gbl_reduced_hardware) 2953 acpi_bus_scan_fixed(); 2954 2955 acpi_turn_off_unused_power_resources(); 2956 2957 acpi_scan_initialized = true; 2958 2959 unlock: 2960 mutex_unlock(&acpi_scan_lock); 2961 } 2962 2963 static struct acpi_probe_entry *ape; 2964 static int acpi_probe_count; 2965 static DEFINE_MUTEX(acpi_probe_mutex); 2966 2967 static int __init acpi_match_madt(union acpi_subtable_headers *header, 2968 const unsigned long end) 2969 { 2970 if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape)) 2971 if (!ape->probe_subtbl(header, end)) 2972 acpi_probe_count++; 2973 2974 return 0; 2975 } 2976 2977 void __weak arch_sort_irqchip_probe(struct acpi_probe_entry *ap_head, int nr) { } 2978 2979 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr) 2980 { 2981 int count = 0; 2982 2983 if (acpi_disabled) 2984 return 0; 2985 2986 mutex_lock(&acpi_probe_mutex); 2987 arch_sort_irqchip_probe(ap_head, nr); 2988 for (ape = ap_head; nr; ape++, nr--) { 2989 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) { 2990 acpi_probe_count = 0; 2991 acpi_table_parse_madt(ape->type, acpi_match_madt, 0); 2992 count += acpi_probe_count; 2993 } else { 2994 int res; 2995 res = acpi_table_parse(ape->id, ape->probe_table); 2996 if (!res) 2997 count++; 2998 } 2999 } 3000 mutex_unlock(&acpi_probe_mutex); 3001 3002 return count; 3003 } 3004 3005 static void acpi_table_events_fn(struct work_struct *work) 3006 { 3007 acpi_scan_lock_acquire(); 3008 acpi_bus_scan(ACPI_ROOT_OBJECT); 3009 acpi_scan_lock_release(); 3010 3011 kfree(work); 3012 } 3013 3014 void acpi_scan_table_notify(void) 3015 { 3016 struct work_struct *work; 3017 3018 if (!acpi_scan_initialized) 3019 return; 3020 3021 work = kmalloc_obj(*work); 3022 if (!work) 3023 return; 3024 3025 INIT_WORK(work, acpi_table_events_fn); 3026 schedule_work(work); 3027 } 3028 3029 int acpi_reconfig_notifier_register(struct notifier_block *nb) 3030 { 3031 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb); 3032 } 3033 EXPORT_SYMBOL(acpi_reconfig_notifier_register); 3034 3035 int acpi_reconfig_notifier_unregister(struct notifier_block *nb) 3036 { 3037 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb); 3038 } 3039 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister); 3040