xref: /openbmc/linux/drivers/acpi/acpica/utownerid.c (revision 67a72420)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
242f8fb75SBob Moore /*******************************************************************************
342f8fb75SBob Moore  *
442f8fb75SBob Moore  * Module Name: utownerid - Support for Table/Method Owner IDs
542f8fb75SBob Moore  *
642f8fb75SBob Moore  ******************************************************************************/
742f8fb75SBob Moore 
842f8fb75SBob Moore #include <acpi/acpi.h>
942f8fb75SBob Moore #include "accommon.h"
1042f8fb75SBob Moore #include "acnamesp.h"
1142f8fb75SBob Moore 
1242f8fb75SBob Moore #define _COMPONENT          ACPI_UTILITIES
1342f8fb75SBob Moore ACPI_MODULE_NAME("utownerid")
1442f8fb75SBob Moore 
1542f8fb75SBob Moore /*******************************************************************************
1642f8fb75SBob Moore  *
1742f8fb75SBob Moore  * FUNCTION:    acpi_ut_allocate_owner_id
1842f8fb75SBob Moore  *
1942f8fb75SBob Moore  * PARAMETERS:  owner_id        - Where the new owner ID is returned
2042f8fb75SBob Moore  *
2142f8fb75SBob Moore  * RETURN:      Status
2242f8fb75SBob Moore  *
2342f8fb75SBob Moore  * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
2442f8fb75SBob Moore  *              track objects created by the table or method, to be deleted
2542f8fb75SBob Moore  *              when the method exits or the table is unloaded.
2642f8fb75SBob Moore  *
2742f8fb75SBob Moore  ******************************************************************************/
acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)2842f8fb75SBob Moore acpi_status acpi_ut_allocate_owner_id(acpi_owner_id *owner_id)
2942f8fb75SBob Moore {
3042f8fb75SBob Moore 	u32 i;
3142f8fb75SBob Moore 	u32 j;
3242f8fb75SBob Moore 	u32 k;
3342f8fb75SBob Moore 	acpi_status status;
3442f8fb75SBob Moore 
3542f8fb75SBob Moore 	ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
3642f8fb75SBob Moore 
3742f8fb75SBob Moore 	/* Guard against multiple allocations of ID to the same location */
3842f8fb75SBob Moore 
3942f8fb75SBob Moore 	if (*owner_id) {
401fad8738SBob Moore 		ACPI_ERROR((AE_INFO,
4167a72420SBob Moore 			    "Owner ID [0x%3.3X] already exists", *owner_id));
4242f8fb75SBob Moore 		return_ACPI_STATUS(AE_ALREADY_EXISTS);
4342f8fb75SBob Moore 	}
4442f8fb75SBob Moore 
4542f8fb75SBob Moore 	/* Mutex for the global ID mask */
4642f8fb75SBob Moore 
4742f8fb75SBob Moore 	status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
4842f8fb75SBob Moore 	if (ACPI_FAILURE(status)) {
4942f8fb75SBob Moore 		return_ACPI_STATUS(status);
5042f8fb75SBob Moore 	}
5142f8fb75SBob Moore 
5242f8fb75SBob Moore 	/*
5342f8fb75SBob Moore 	 * Find a free owner ID, cycle through all possible IDs on repeated
541fad8738SBob Moore 	 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index
551fad8738SBob Moore 	 * may have to be scanned twice.
5642f8fb75SBob Moore 	 */
5742f8fb75SBob Moore 	for (i = 0, j = acpi_gbl_last_owner_id_index;
5842f8fb75SBob Moore 	     i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
5942f8fb75SBob Moore 		if (j >= ACPI_NUM_OWNERID_MASKS) {
6042f8fb75SBob Moore 			j = 0;	/* Wraparound to start of mask array */
6142f8fb75SBob Moore 		}
6242f8fb75SBob Moore 
6342f8fb75SBob Moore 		for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
6442f8fb75SBob Moore 			if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
6542f8fb75SBob Moore 
6642f8fb75SBob Moore 				/* There are no free IDs in this mask */
6742f8fb75SBob Moore 
6842f8fb75SBob Moore 				break;
6942f8fb75SBob Moore 			}
7042f8fb75SBob Moore 
71deb85f6cSErik Schmauss 			/*
72deb85f6cSErik Schmauss 			 * Note: the u32 cast ensures that 1 is stored as a unsigned
73deb85f6cSErik Schmauss 			 * integer. Omitting the cast may result in 1 being stored as an
74deb85f6cSErik Schmauss 			 * int. Some compilers or runtime error detection may flag this as
75deb85f6cSErik Schmauss 			 * an error.
76deb85f6cSErik Schmauss 			 */
77deb85f6cSErik Schmauss 			if (!(acpi_gbl_owner_id_mask[j] & ((u32)1 << k))) {
7842f8fb75SBob Moore 				/*
7942f8fb75SBob Moore 				 * Found a free ID. The actual ID is the bit index plus one,
8042f8fb75SBob Moore 				 * making zero an invalid Owner ID. Save this as the last ID
8142f8fb75SBob Moore 				 * allocated and update the global ID mask.
8242f8fb75SBob Moore 				 */
83deb85f6cSErik Schmauss 				acpi_gbl_owner_id_mask[j] |= ((u32)1 << k);
8442f8fb75SBob Moore 
8542f8fb75SBob Moore 				acpi_gbl_last_owner_id_index = (u8)j;
8642f8fb75SBob Moore 				acpi_gbl_next_owner_id_offset = (u8)(k + 1);
8742f8fb75SBob Moore 
8842f8fb75SBob Moore 				/*
8942f8fb75SBob Moore 				 * Construct encoded ID from the index and bit position
9042f8fb75SBob Moore 				 *
9167a72420SBob Moore 				 * Note: Last [j].k (bit 4095) is never used and is marked
9242f8fb75SBob Moore 				 * permanently allocated (prevents +1 overflow)
9342f8fb75SBob Moore 				 */
9442f8fb75SBob Moore 				*owner_id =
9542f8fb75SBob Moore 				    (acpi_owner_id)((k + 1) + ACPI_MUL_32(j));
9642f8fb75SBob Moore 
9742f8fb75SBob Moore 				ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
9867a72420SBob Moore 						  "Allocated OwnerId: 0x%3.3X\n",
9942f8fb75SBob Moore 						  (unsigned int)*owner_id));
10042f8fb75SBob Moore 				goto exit;
10142f8fb75SBob Moore 			}
10242f8fb75SBob Moore 		}
10342f8fb75SBob Moore 
10442f8fb75SBob Moore 		acpi_gbl_next_owner_id_offset = 0;
10542f8fb75SBob Moore 	}
10642f8fb75SBob Moore 
10742f8fb75SBob Moore 	/*
10842f8fb75SBob Moore 	 * All owner_ids have been allocated. This typically should
10942f8fb75SBob Moore 	 * not happen since the IDs are reused after deallocation. The IDs are
11042f8fb75SBob Moore 	 * allocated upon table load (one per table) and method execution, and
11142f8fb75SBob Moore 	 * they are released when a table is unloaded or a method completes
11242f8fb75SBob Moore 	 * execution.
11342f8fb75SBob Moore 	 *
1141fad8738SBob Moore 	 * If this error happens, there may be very deep nesting of invoked
1151fad8738SBob Moore 	 * control methods, or there may be a bug where the IDs are not released.
11642f8fb75SBob Moore 	 */
11742f8fb75SBob Moore 	status = AE_OWNER_ID_LIMIT;
11842f8fb75SBob Moore 	ACPI_ERROR((AE_INFO,
11967a72420SBob Moore 		    "Could not allocate new OwnerId (4095 max), AE_OWNER_ID_LIMIT"));
12042f8fb75SBob Moore 
12142f8fb75SBob Moore exit:
12242f8fb75SBob Moore 	(void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
12342f8fb75SBob Moore 	return_ACPI_STATUS(status);
12442f8fb75SBob Moore }
12542f8fb75SBob Moore 
12642f8fb75SBob Moore /*******************************************************************************
12742f8fb75SBob Moore  *
12842f8fb75SBob Moore  * FUNCTION:    acpi_ut_release_owner_id
12942f8fb75SBob Moore  *
13042f8fb75SBob Moore  * PARAMETERS:  owner_id_ptr        - Pointer to a previously allocated owner_ID
13142f8fb75SBob Moore  *
13242f8fb75SBob Moore  * RETURN:      None. No error is returned because we are either exiting a
13342f8fb75SBob Moore  *              control method or unloading a table. Either way, we would
13442f8fb75SBob Moore  *              ignore any error anyway.
13542f8fb75SBob Moore  *
13642f8fb75SBob Moore  * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
13742f8fb75SBob Moore  *
13842f8fb75SBob Moore  ******************************************************************************/
13942f8fb75SBob Moore 
acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)14042f8fb75SBob Moore void acpi_ut_release_owner_id(acpi_owner_id *owner_id_ptr)
14142f8fb75SBob Moore {
14242f8fb75SBob Moore 	acpi_owner_id owner_id = *owner_id_ptr;
14342f8fb75SBob Moore 	acpi_status status;
14442f8fb75SBob Moore 	u32 index;
14542f8fb75SBob Moore 	u32 bit;
14642f8fb75SBob Moore 
14742f8fb75SBob Moore 	ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
14842f8fb75SBob Moore 
14942f8fb75SBob Moore 	/* Always clear the input owner_id (zero is an invalid ID) */
15042f8fb75SBob Moore 
15142f8fb75SBob Moore 	*owner_id_ptr = 0;
15242f8fb75SBob Moore 
15342f8fb75SBob Moore 	/* Zero is not a valid owner_ID */
15442f8fb75SBob Moore 
15542f8fb75SBob Moore 	if (owner_id == 0) {
15667a72420SBob Moore 		ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%3.3X", owner_id));
15742f8fb75SBob Moore 		return_VOID;
15842f8fb75SBob Moore 	}
15942f8fb75SBob Moore 
16042f8fb75SBob Moore 	/* Mutex for the global ID mask */
16142f8fb75SBob Moore 
16242f8fb75SBob Moore 	status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
16342f8fb75SBob Moore 	if (ACPI_FAILURE(status)) {
16442f8fb75SBob Moore 		return_VOID;
16542f8fb75SBob Moore 	}
16642f8fb75SBob Moore 
16742f8fb75SBob Moore 	/* Normalize the ID to zero */
16842f8fb75SBob Moore 
16942f8fb75SBob Moore 	owner_id--;
17042f8fb75SBob Moore 
17142f8fb75SBob Moore 	/* Decode ID to index/offset pair */
17242f8fb75SBob Moore 
17342f8fb75SBob Moore 	index = ACPI_DIV_32(owner_id);
174deb85f6cSErik Schmauss 	bit = (u32)1 << ACPI_MOD_32(owner_id);
17542f8fb75SBob Moore 
17642f8fb75SBob Moore 	/* Free the owner ID only if it is valid */
17742f8fb75SBob Moore 
17842f8fb75SBob Moore 	if (acpi_gbl_owner_id_mask[index] & bit) {
17942f8fb75SBob Moore 		acpi_gbl_owner_id_mask[index] ^= bit;
18042f8fb75SBob Moore 	} else {
18142f8fb75SBob Moore 		ACPI_ERROR((AE_INFO,
18267a72420SBob Moore 			    "Attempted release of non-allocated OwnerId: 0x%3.3X",
18342f8fb75SBob Moore 			    owner_id + 1));
18442f8fb75SBob Moore 	}
18542f8fb75SBob Moore 
18642f8fb75SBob Moore 	(void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
18742f8fb75SBob Moore 	return_VOID;
18842f8fb75SBob Moore }
189