File Index
Files and directories
Sign in
FILE

scan.c

File preview with syntax highlighting.

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