162306a36Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
262306a36Sopenharmony_ci/******************************************************************************
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Module Name: utxfinit - External interfaces for ACPICA initialization
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2000 - 2023, Intel Corp.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci *****************************************************************************/
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#define EXPORT_ACPI_INTERFACES
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <acpi/acpi.h>
1362306a36Sopenharmony_ci#include "accommon.h"
1462306a36Sopenharmony_ci#include "acevents.h"
1562306a36Sopenharmony_ci#include "acnamesp.h"
1662306a36Sopenharmony_ci#include "acdebug.h"
1762306a36Sopenharmony_ci#include "actables.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#define _COMPONENT          ACPI_UTILITIES
2062306a36Sopenharmony_ciACPI_MODULE_NAME("utxfinit")
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/* For acpi_exec only */
2362306a36Sopenharmony_civoid ae_do_object_overrides(void);
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*******************************************************************************
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * FUNCTION:    acpi_initialize_subsystem
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * PARAMETERS:  None
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * RETURN:      Status
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * DESCRIPTION: Initializes all global variables. This is the first function
3462306a36Sopenharmony_ci *              called, so any early initialization belongs here.
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci ******************************************************************************/
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciacpi_status ACPI_INIT_FUNCTION acpi_initialize_subsystem(void)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	acpi_status status;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	ACPI_FUNCTION_TRACE(acpi_initialize_subsystem);
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	acpi_gbl_startup_flags = ACPI_SUBSYSTEM_INITIALIZE;
4562306a36Sopenharmony_ci	ACPI_DEBUG_EXEC(acpi_ut_init_stack_ptr_trace());
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	/* Initialize the OS-Dependent layer */
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	status = acpi_os_initialize();
5062306a36Sopenharmony_ci	if (ACPI_FAILURE(status)) {
5162306a36Sopenharmony_ci		ACPI_EXCEPTION((AE_INFO, status, "During OSL initialization"));
5262306a36Sopenharmony_ci		return_ACPI_STATUS(status);
5362306a36Sopenharmony_ci	}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	/* Initialize all globals used by the subsystem */
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	status = acpi_ut_init_globals();
5862306a36Sopenharmony_ci	if (ACPI_FAILURE(status)) {
5962306a36Sopenharmony_ci		ACPI_EXCEPTION((AE_INFO, status,
6062306a36Sopenharmony_ci				"During initialization of globals"));
6162306a36Sopenharmony_ci		return_ACPI_STATUS(status);
6262306a36Sopenharmony_ci	}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	/* Create the default mutex objects */
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	status = acpi_ut_mutex_initialize();
6762306a36Sopenharmony_ci	if (ACPI_FAILURE(status)) {
6862306a36Sopenharmony_ci		ACPI_EXCEPTION((AE_INFO, status,
6962306a36Sopenharmony_ci				"During Global Mutex creation"));
7062306a36Sopenharmony_ci		return_ACPI_STATUS(status);
7162306a36Sopenharmony_ci	}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	/*
7462306a36Sopenharmony_ci	 * Initialize the namespace manager and
7562306a36Sopenharmony_ci	 * the root of the namespace tree
7662306a36Sopenharmony_ci	 */
7762306a36Sopenharmony_ci	status = acpi_ns_root_initialize();
7862306a36Sopenharmony_ci	if (ACPI_FAILURE(status)) {
7962306a36Sopenharmony_ci		ACPI_EXCEPTION((AE_INFO, status,
8062306a36Sopenharmony_ci				"During Namespace initialization"));
8162306a36Sopenharmony_ci		return_ACPI_STATUS(status);
8262306a36Sopenharmony_ci	}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	/* Initialize the global OSI interfaces list with the static names */
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	status = acpi_ut_initialize_interfaces();
8762306a36Sopenharmony_ci	if (ACPI_FAILURE(status)) {
8862306a36Sopenharmony_ci		ACPI_EXCEPTION((AE_INFO, status,
8962306a36Sopenharmony_ci				"During OSI interfaces initialization"));
9062306a36Sopenharmony_ci		return_ACPI_STATUS(status);
9162306a36Sopenharmony_ci	}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	return_ACPI_STATUS(AE_OK);
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciACPI_EXPORT_SYMBOL_INIT(acpi_initialize_subsystem)
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/*******************************************************************************
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * FUNCTION:    acpi_enable_subsystem
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * PARAMETERS:  flags               - Init/enable Options
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci * RETURN:      Status
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * DESCRIPTION: Completes the subsystem initialization including hardware.
10762306a36Sopenharmony_ci *              Puts system into ACPI mode if it isn't already.
10862306a36Sopenharmony_ci *
10962306a36Sopenharmony_ci ******************************************************************************/
11062306a36Sopenharmony_ciacpi_status ACPI_INIT_FUNCTION acpi_enable_subsystem(u32 flags)
11162306a36Sopenharmony_ci{
11262306a36Sopenharmony_ci	acpi_status status = AE_OK;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	ACPI_FUNCTION_TRACE(acpi_enable_subsystem);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	/*
11762306a36Sopenharmony_ci	 * The early initialization phase is complete. The namespace is loaded,
11862306a36Sopenharmony_ci	 * and we can now support address spaces other than Memory, I/O, and
11962306a36Sopenharmony_ci	 * PCI_Config.
12062306a36Sopenharmony_ci	 */
12162306a36Sopenharmony_ci	acpi_gbl_early_initialization = FALSE;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#if (!ACPI_REDUCED_HARDWARE)
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	/* Enable ACPI mode */
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	if (!(flags & ACPI_NO_ACPI_ENABLE)) {
12862306a36Sopenharmony_ci		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
12962306a36Sopenharmony_ci				  "[Init] Going into ACPI mode\n"));
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci		acpi_gbl_original_mode = acpi_hw_get_mode();
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci		status = acpi_enable();
13462306a36Sopenharmony_ci		if (ACPI_FAILURE(status)) {
13562306a36Sopenharmony_ci			ACPI_WARNING((AE_INFO, "AcpiEnable failed"));
13662306a36Sopenharmony_ci			return_ACPI_STATUS(status);
13762306a36Sopenharmony_ci		}
13862306a36Sopenharmony_ci	}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/*
14162306a36Sopenharmony_ci	 * Obtain a permanent mapping for the FACS. This is required for the
14262306a36Sopenharmony_ci	 * Global Lock and the Firmware Waking Vector
14362306a36Sopenharmony_ci	 */
14462306a36Sopenharmony_ci	if (!(flags & ACPI_NO_FACS_INIT)) {
14562306a36Sopenharmony_ci		status = acpi_tb_initialize_facs();
14662306a36Sopenharmony_ci		if (ACPI_FAILURE(status)) {
14762306a36Sopenharmony_ci			ACPI_WARNING((AE_INFO, "Could not map the FACS table"));
14862306a36Sopenharmony_ci			return_ACPI_STATUS(status);
14962306a36Sopenharmony_ci		}
15062306a36Sopenharmony_ci	}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	/*
15362306a36Sopenharmony_ci	 * Initialize ACPI Event handling (Fixed and General Purpose)
15462306a36Sopenharmony_ci	 *
15562306a36Sopenharmony_ci	 * Note1: We must have the hardware and events initialized before we can
15662306a36Sopenharmony_ci	 * execute any control methods safely. Any control method can require
15762306a36Sopenharmony_ci	 * ACPI hardware support, so the hardware must be fully initialized before
15862306a36Sopenharmony_ci	 * any method execution!
15962306a36Sopenharmony_ci	 *
16062306a36Sopenharmony_ci	 * Note2: Fixed events are initialized and enabled here. GPEs are
16162306a36Sopenharmony_ci	 * initialized, but cannot be enabled until after the hardware is
16262306a36Sopenharmony_ci	 * completely initialized (SCI and global_lock activated) and the various
16362306a36Sopenharmony_ci	 * initialization control methods are run (_REG, _STA, _INI) on the
16462306a36Sopenharmony_ci	 * entire namespace.
16562306a36Sopenharmony_ci	 */
16662306a36Sopenharmony_ci	if (!(flags & ACPI_NO_EVENT_INIT)) {
16762306a36Sopenharmony_ci		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
16862306a36Sopenharmony_ci				  "[Init] Initializing ACPI events\n"));
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci		status = acpi_ev_initialize_events();
17162306a36Sopenharmony_ci		if (ACPI_FAILURE(status)) {
17262306a36Sopenharmony_ci			return_ACPI_STATUS(status);
17362306a36Sopenharmony_ci		}
17462306a36Sopenharmony_ci	}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	/*
17762306a36Sopenharmony_ci	 * Install the SCI handler and Global Lock handler. This completes the
17862306a36Sopenharmony_ci	 * hardware initialization.
17962306a36Sopenharmony_ci	 */
18062306a36Sopenharmony_ci	if (!(flags & ACPI_NO_HANDLER_INIT)) {
18162306a36Sopenharmony_ci		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
18262306a36Sopenharmony_ci				  "[Init] Installing SCI/GL handlers\n"));
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci		status = acpi_ev_install_xrupt_handlers();
18562306a36Sopenharmony_ci		if (ACPI_FAILURE(status)) {
18662306a36Sopenharmony_ci			return_ACPI_STATUS(status);
18762306a36Sopenharmony_ci		}
18862306a36Sopenharmony_ci	}
18962306a36Sopenharmony_ci#endif				/* !ACPI_REDUCED_HARDWARE */
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	return_ACPI_STATUS(status);
19262306a36Sopenharmony_ci}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ciACPI_EXPORT_SYMBOL_INIT(acpi_enable_subsystem)
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci/*******************************************************************************
19762306a36Sopenharmony_ci *
19862306a36Sopenharmony_ci * FUNCTION:    acpi_initialize_objects
19962306a36Sopenharmony_ci *
20062306a36Sopenharmony_ci * PARAMETERS:  flags               - Init/enable Options
20162306a36Sopenharmony_ci *
20262306a36Sopenharmony_ci * RETURN:      Status
20362306a36Sopenharmony_ci *
20462306a36Sopenharmony_ci * DESCRIPTION: Completes namespace initialization by initializing device
20562306a36Sopenharmony_ci *              objects and executing AML code for Regions, buffers, etc.
20662306a36Sopenharmony_ci *
20762306a36Sopenharmony_ci ******************************************************************************/
20862306a36Sopenharmony_ciacpi_status ACPI_INIT_FUNCTION acpi_initialize_objects(u32 flags)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	acpi_status status = AE_OK;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	ACPI_FUNCTION_TRACE(acpi_initialize_objects);
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci#ifdef ACPI_OBSOLETE_BEHAVIOR
21562306a36Sopenharmony_ci	/*
21662306a36Sopenharmony_ci	 * 05/2019: Removed, initialization now happens at both object
21762306a36Sopenharmony_ci	 * creation and table load time
21862306a36Sopenharmony_ci	 */
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	/*
22162306a36Sopenharmony_ci	 * Initialize the objects that remain uninitialized. This
22262306a36Sopenharmony_ci	 * runs the executable AML that may be part of the
22362306a36Sopenharmony_ci	 * declaration of these objects: operation_regions, buffer_fields,
22462306a36Sopenharmony_ci	 * bank_fields, Buffers, and Packages.
22562306a36Sopenharmony_ci	 */
22662306a36Sopenharmony_ci	if (!(flags & ACPI_NO_OBJECT_INIT)) {
22762306a36Sopenharmony_ci		status = acpi_ns_initialize_objects();
22862306a36Sopenharmony_ci		if (ACPI_FAILURE(status)) {
22962306a36Sopenharmony_ci			return_ACPI_STATUS(status);
23062306a36Sopenharmony_ci		}
23162306a36Sopenharmony_ci	}
23262306a36Sopenharmony_ci#endif
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	/*
23562306a36Sopenharmony_ci	 * Initialize all device/region objects in the namespace. This runs
23662306a36Sopenharmony_ci	 * the device _STA and _INI methods and region _REG methods.
23762306a36Sopenharmony_ci	 */
23862306a36Sopenharmony_ci	if (!(flags & (ACPI_NO_DEVICE_INIT | ACPI_NO_ADDRESS_SPACE_INIT))) {
23962306a36Sopenharmony_ci		status = acpi_ns_initialize_devices(flags);
24062306a36Sopenharmony_ci		if (ACPI_FAILURE(status)) {
24162306a36Sopenharmony_ci			return_ACPI_STATUS(status);
24262306a36Sopenharmony_ci		}
24362306a36Sopenharmony_ci	}
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/*
24662306a36Sopenharmony_ci	 * Empty the caches (delete the cached objects) on the assumption that
24762306a36Sopenharmony_ci	 * the table load filled them up more than they will be at runtime --
24862306a36Sopenharmony_ci	 * thus wasting non-paged memory.
24962306a36Sopenharmony_ci	 */
25062306a36Sopenharmony_ci	status = acpi_purge_cached_objects();
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	acpi_gbl_startup_flags |= ACPI_INITIALIZED_OK;
25362306a36Sopenharmony_ci	return_ACPI_STATUS(status);
25462306a36Sopenharmony_ci}
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ciACPI_EXPORT_SYMBOL_INIT(acpi_initialize_objects)
257