1 /*******************************************************************************
3 * Module Name: utownerid - Support for Table/Method Owner IDs
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acnamesp.h>
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utownerid")
53 /*******************************************************************************
55 * FUNCTION: AcpiUtAllocateOwnerId
57 * PARAMETERS: OwnerId - Where the new owner ID is returned
61 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
62 * track objects created by the table or method, to be deleted
63 * when the method exits or the table is unloaded.
65 ******************************************************************************/
68 AcpiUtAllocateOwnerId (
69 ACPI_OWNER_ID *OwnerId)
77 ACPI_FUNCTION_TRACE (UtAllocateOwnerId);
80 /* Guard against multiple allocations of ID to the same location */
84 ACPI_ERROR ((AE_INFO, "Owner ID [0x%2.2X] already exists", *OwnerId));
85 return_ACPI_STATUS (AE_ALREADY_EXISTS);
88 /* Mutex for the global ID mask */
90 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
91 if (ACPI_FAILURE (Status))
93 return_ACPI_STATUS (Status);
97 * Find a free owner ID, cycle through all possible IDs on repeated
98 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
99 * to be scanned twice.
101 for (i = 0, j = AcpiGbl_LastOwnerIdIndex;
102 i < (ACPI_NUM_OWNERID_MASKS + 1);
105 if (j >= ACPI_NUM_OWNERID_MASKS)
107 j = 0; /* Wraparound to start of mask array */
110 for (k = AcpiGbl_NextOwnerIdOffset; k < 32; k++)
112 if (AcpiGbl_OwnerIdMask[j] == ACPI_UINT32_MAX)
114 /* There are no free IDs in this mask */
119 if (!(AcpiGbl_OwnerIdMask[j] & (1 << k)))
122 * Found a free ID. The actual ID is the bit index plus one,
123 * making zero an invalid Owner ID. Save this as the last ID
124 * allocated and update the global ID mask.
126 AcpiGbl_OwnerIdMask[j] |= (1 << k);
128 AcpiGbl_LastOwnerIdIndex = (UINT8) j;
129 AcpiGbl_NextOwnerIdOffset = (UINT8) (k + 1);
132 * Construct encoded ID from the index and bit position
134 * Note: Last [j].k (bit 255) is never used and is marked
135 * permanently allocated (prevents +1 overflow)
137 *OwnerId = (ACPI_OWNER_ID) ((k + 1) + ACPI_MUL_32 (j));
139 ACPI_DEBUG_PRINT ((ACPI_DB_VALUES,
140 "Allocated OwnerId: %2.2X\n", (unsigned int) *OwnerId));
145 AcpiGbl_NextOwnerIdOffset = 0;
149 * All OwnerIds have been allocated. This typically should
150 * not happen since the IDs are reused after deallocation. The IDs are
151 * allocated upon table load (one per table) and method execution, and
152 * they are released when a table is unloaded or a method completes
155 * If this error happens, there may be very deep nesting of invoked control
156 * methods, or there may be a bug where the IDs are not released.
158 Status = AE_OWNER_ID_LIMIT;
159 ACPI_ERROR ((AE_INFO,
160 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
163 (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
164 return_ACPI_STATUS (Status);
168 /*******************************************************************************
170 * FUNCTION: AcpiUtReleaseOwnerId
172 * PARAMETERS: OwnerIdPtr - Pointer to a previously allocated OwnerID
174 * RETURN: None. No error is returned because we are either exiting a
175 * control method or unloading a table. Either way, we would
176 * ignore any error anyway.
178 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
180 ******************************************************************************/
183 AcpiUtReleaseOwnerId (
184 ACPI_OWNER_ID *OwnerIdPtr)
186 ACPI_OWNER_ID OwnerId = *OwnerIdPtr;
192 ACPI_FUNCTION_TRACE_U32 (UtReleaseOwnerId, OwnerId);
195 /* Always clear the input OwnerId (zero is an invalid ID) */
199 /* Zero is not a valid OwnerID */
203 ACPI_ERROR ((AE_INFO, "Invalid OwnerId: 0x%2.2X", OwnerId));
207 /* Mutex for the global ID mask */
209 Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
210 if (ACPI_FAILURE (Status))
215 /* Normalize the ID to zero */
219 /* Decode ID to index/offset pair */
221 Index = ACPI_DIV_32 (OwnerId);
222 Bit = 1 << ACPI_MOD_32 (OwnerId);
224 /* Free the owner ID only if it is valid */
226 if (AcpiGbl_OwnerIdMask[Index] & Bit)
228 AcpiGbl_OwnerIdMask[Index] ^= Bit;
232 ACPI_ERROR ((AE_INFO,
233 "Release of non-allocated OwnerId: 0x%2.2X", OwnerId + 1));
236 (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);