Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /*******************************************************************************
0003  *
0004  * Module Name: utownerid - Support for Table/Method Owner IDs
0005  *
0006  ******************************************************************************/
0007 
0008 #include <acpi/acpi.h>
0009 #include "accommon.h"
0010 #include "acnamesp.h"
0011 
0012 #define _COMPONENT          ACPI_UTILITIES
0013 ACPI_MODULE_NAME("utownerid")
0014 
0015 /*******************************************************************************
0016  *
0017  * FUNCTION:    acpi_ut_allocate_owner_id
0018  *
0019  * PARAMETERS:  owner_id        - Where the new owner ID is returned
0020  *
0021  * RETURN:      Status
0022  *
0023  * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
0024  *              track objects created by the table or method, to be deleted
0025  *              when the method exits or the table is unloaded.
0026  *
0027  ******************************************************************************/
0028 acpi_status acpi_ut_allocate_owner_id(acpi_owner_id *owner_id)
0029 {
0030     u32 i;
0031     u32 j;
0032     u32 k;
0033     acpi_status status;
0034 
0035     ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
0036 
0037     /* Guard against multiple allocations of ID to the same location */
0038 
0039     if (*owner_id) {
0040         ACPI_ERROR((AE_INFO,
0041                 "Owner ID [0x%3.3X] already exists", *owner_id));
0042         return_ACPI_STATUS(AE_ALREADY_EXISTS);
0043     }
0044 
0045     /* Mutex for the global ID mask */
0046 
0047     status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
0048     if (ACPI_FAILURE(status)) {
0049         return_ACPI_STATUS(status);
0050     }
0051 
0052     /*
0053      * Find a free owner ID, cycle through all possible IDs on repeated
0054      * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index
0055      * may have to be scanned twice.
0056      */
0057     for (i = 0, j = acpi_gbl_last_owner_id_index;
0058          i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
0059         if (j >= ACPI_NUM_OWNERID_MASKS) {
0060             j = 0;  /* Wraparound to start of mask array */
0061         }
0062 
0063         for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
0064             if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
0065 
0066                 /* There are no free IDs in this mask */
0067 
0068                 break;
0069             }
0070 
0071             /*
0072              * Note: the u32 cast ensures that 1 is stored as a unsigned
0073              * integer. Omitting the cast may result in 1 being stored as an
0074              * int. Some compilers or runtime error detection may flag this as
0075              * an error.
0076              */
0077             if (!(acpi_gbl_owner_id_mask[j] & ((u32)1 << k))) {
0078                 /*
0079                  * Found a free ID. The actual ID is the bit index plus one,
0080                  * making zero an invalid Owner ID. Save this as the last ID
0081                  * allocated and update the global ID mask.
0082                  */
0083                 acpi_gbl_owner_id_mask[j] |= ((u32)1 << k);
0084 
0085                 acpi_gbl_last_owner_id_index = (u8)j;
0086                 acpi_gbl_next_owner_id_offset = (u8)(k + 1);
0087 
0088                 /*
0089                  * Construct encoded ID from the index and bit position
0090                  *
0091                  * Note: Last [j].k (bit 4095) is never used and is marked
0092                  * permanently allocated (prevents +1 overflow)
0093                  */
0094                 *owner_id =
0095                     (acpi_owner_id)((k + 1) + ACPI_MUL_32(j));
0096 
0097                 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
0098                           "Allocated OwnerId: 0x%3.3X\n",
0099                           (unsigned int)*owner_id));
0100                 goto exit;
0101             }
0102         }
0103 
0104         acpi_gbl_next_owner_id_offset = 0;
0105     }
0106 
0107     /*
0108      * All owner_ids have been allocated. This typically should
0109      * not happen since the IDs are reused after deallocation. The IDs are
0110      * allocated upon table load (one per table) and method execution, and
0111      * they are released when a table is unloaded or a method completes
0112      * execution.
0113      *
0114      * If this error happens, there may be very deep nesting of invoked
0115      * control methods, or there may be a bug where the IDs are not released.
0116      */
0117     status = AE_OWNER_ID_LIMIT;
0118     ACPI_ERROR((AE_INFO,
0119             "Could not allocate new OwnerId (4095 max), AE_OWNER_ID_LIMIT"));
0120 
0121 exit:
0122     (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
0123     return_ACPI_STATUS(status);
0124 }
0125 
0126 /*******************************************************************************
0127  *
0128  * FUNCTION:    acpi_ut_release_owner_id
0129  *
0130  * PARAMETERS:  owner_id_ptr        - Pointer to a previously allocated owner_ID
0131  *
0132  * RETURN:      None. No error is returned because we are either exiting a
0133  *              control method or unloading a table. Either way, we would
0134  *              ignore any error anyway.
0135  *
0136  * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
0137  *
0138  ******************************************************************************/
0139 
0140 void acpi_ut_release_owner_id(acpi_owner_id *owner_id_ptr)
0141 {
0142     acpi_owner_id owner_id = *owner_id_ptr;
0143     acpi_status status;
0144     u32 index;
0145     u32 bit;
0146 
0147     ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
0148 
0149     /* Always clear the input owner_id (zero is an invalid ID) */
0150 
0151     *owner_id_ptr = 0;
0152 
0153     /* Zero is not a valid owner_ID */
0154 
0155     if (owner_id == 0) {
0156         ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%3.3X", owner_id));
0157         return_VOID;
0158     }
0159 
0160     /* Mutex for the global ID mask */
0161 
0162     status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
0163     if (ACPI_FAILURE(status)) {
0164         return_VOID;
0165     }
0166 
0167     /* Normalize the ID to zero */
0168 
0169     owner_id--;
0170 
0171     /* Decode ID to index/offset pair */
0172 
0173     index = ACPI_DIV_32(owner_id);
0174     bit = (u32)1 << ACPI_MOD_32(owner_id);
0175 
0176     /* Free the owner ID only if it is valid */
0177 
0178     if (acpi_gbl_owner_id_mask[index] & bit) {
0179         acpi_gbl_owner_id_mask[index] ^= bit;
0180     } else {
0181         ACPI_ERROR((AE_INFO,
0182                 "Attempted release of non-allocated OwnerId: 0x%3.3X",
0183                 owner_id + 1));
0184     }
0185 
0186     (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
0187     return_VOID;
0188 }