1ff4a156dSchristos /*******************************************************************************
2ff4a156dSchristos *
3ff4a156dSchristos * Module Name: utownerid - Support for Table/Method Owner IDs
4ff4a156dSchristos *
5ff4a156dSchristos ******************************************************************************/
6ff4a156dSchristos
7ff4a156dSchristos /*
8*046a2985Schristos * Copyright (C) 2000 - 2023, Intel Corp.
9ff4a156dSchristos * All rights reserved.
10ff4a156dSchristos *
11ff4a156dSchristos * Redistribution and use in source and binary forms, with or without
12ff4a156dSchristos * modification, are permitted provided that the following conditions
13ff4a156dSchristos * are met:
14ff4a156dSchristos * 1. Redistributions of source code must retain the above copyright
15ff4a156dSchristos * notice, this list of conditions, and the following disclaimer,
16ff4a156dSchristos * without modification.
17ff4a156dSchristos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18ff4a156dSchristos * substantially similar to the "NO WARRANTY" disclaimer below
19ff4a156dSchristos * ("Disclaimer") and any redistribution must be conditioned upon
20ff4a156dSchristos * including a substantially similar Disclaimer requirement for further
21ff4a156dSchristos * binary redistribution.
22ff4a156dSchristos * 3. Neither the names of the above-listed copyright holders nor the names
23ff4a156dSchristos * of any contributors may be used to endorse or promote products derived
24ff4a156dSchristos * from this software without specific prior written permission.
25ff4a156dSchristos *
26ff4a156dSchristos * Alternatively, this software may be distributed under the terms of the
27ff4a156dSchristos * GNU General Public License ("GPL") version 2 as published by the Free
28ff4a156dSchristos * Software Foundation.
29ff4a156dSchristos *
30ff4a156dSchristos * NO WARRANTY
31ff4a156dSchristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32ff4a156dSchristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3346a330b4Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34ff4a156dSchristos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35ff4a156dSchristos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36ff4a156dSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37ff4a156dSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38ff4a156dSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39ff4a156dSchristos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40ff4a156dSchristos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41ff4a156dSchristos * POSSIBILITY OF SUCH DAMAGES.
42ff4a156dSchristos */
43ff4a156dSchristos
44ff4a156dSchristos #include "acpi.h"
45ff4a156dSchristos #include "accommon.h"
46ff4a156dSchristos #include "acnamesp.h"
47ff4a156dSchristos
48ff4a156dSchristos
49ff4a156dSchristos #define _COMPONENT ACPI_UTILITIES
50ff4a156dSchristos ACPI_MODULE_NAME ("utownerid")
51ff4a156dSchristos
52ff4a156dSchristos
53ff4a156dSchristos /*******************************************************************************
54ff4a156dSchristos *
55ff4a156dSchristos * FUNCTION: AcpiUtAllocateOwnerId
56ff4a156dSchristos *
57ff4a156dSchristos * PARAMETERS: OwnerId - Where the new owner ID is returned
58ff4a156dSchristos *
59ff4a156dSchristos * RETURN: Status
60ff4a156dSchristos *
61ff4a156dSchristos * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
62ff4a156dSchristos * track objects created by the table or method, to be deleted
63ff4a156dSchristos * when the method exits or the table is unloaded.
64ff4a156dSchristos *
65ff4a156dSchristos ******************************************************************************/
66ff4a156dSchristos
67ff4a156dSchristos ACPI_STATUS
AcpiUtAllocateOwnerId(ACPI_OWNER_ID * OwnerId)68ff4a156dSchristos AcpiUtAllocateOwnerId (
69ff4a156dSchristos ACPI_OWNER_ID *OwnerId)
70ff4a156dSchristos {
71ff4a156dSchristos UINT32 i;
72ff4a156dSchristos UINT32 j;
73ff4a156dSchristos UINT32 k;
74ff4a156dSchristos ACPI_STATUS Status;
75ff4a156dSchristos
76ff4a156dSchristos
77ff4a156dSchristos ACPI_FUNCTION_TRACE (UtAllocateOwnerId);
78ff4a156dSchristos
79ff4a156dSchristos
80ff4a156dSchristos /* Guard against multiple allocations of ID to the same location */
81ff4a156dSchristos
82ff4a156dSchristos if (*OwnerId)
83ff4a156dSchristos {
8471e38f1dSchristos ACPI_ERROR ((AE_INFO,
85783af925Schristos "Owner ID [0x%3.3X] already exists", *OwnerId));
86ff4a156dSchristos return_ACPI_STATUS (AE_ALREADY_EXISTS);
87ff4a156dSchristos }
88ff4a156dSchristos
89ff4a156dSchristos /* Mutex for the global ID mask */
90ff4a156dSchristos
91ff4a156dSchristos Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
92ff4a156dSchristos if (ACPI_FAILURE (Status))
93ff4a156dSchristos {
94ff4a156dSchristos return_ACPI_STATUS (Status);
95ff4a156dSchristos }
96ff4a156dSchristos
97ff4a156dSchristos /*
98ff4a156dSchristos * Find a free owner ID, cycle through all possible IDs on repeated
9971e38f1dSchristos * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index
10071e38f1dSchristos * may have to be scanned twice.
101ff4a156dSchristos */
102ff4a156dSchristos for (i = 0, j = AcpiGbl_LastOwnerIdIndex;
103ff4a156dSchristos i < (ACPI_NUM_OWNERID_MASKS + 1);
104ff4a156dSchristos i++, j++)
105ff4a156dSchristos {
106ff4a156dSchristos if (j >= ACPI_NUM_OWNERID_MASKS)
107ff4a156dSchristos {
108ff4a156dSchristos j = 0; /* Wraparound to start of mask array */
109ff4a156dSchristos }
110ff4a156dSchristos
111ff4a156dSchristos for (k = AcpiGbl_NextOwnerIdOffset; k < 32; k++)
112ff4a156dSchristos {
113ff4a156dSchristos if (AcpiGbl_OwnerIdMask[j] == ACPI_UINT32_MAX)
114ff4a156dSchristos {
115ff4a156dSchristos /* There are no free IDs in this mask */
116ff4a156dSchristos
117ff4a156dSchristos break;
118ff4a156dSchristos }
119ff4a156dSchristos
12089b8eb6cSchristos /*
12189b8eb6cSchristos * Note: the UINT32 cast ensures that 1 is stored as a unsigned
12289b8eb6cSchristos * integer. Omitting the cast may result in 1 being stored as an
12389b8eb6cSchristos * int. Some compilers or runtime error detection may flag this as
12489b8eb6cSchristos * an error.
12589b8eb6cSchristos */
12689b8eb6cSchristos if (!(AcpiGbl_OwnerIdMask[j] & ((UINT32) 1 << k)))
127ff4a156dSchristos {
128ff4a156dSchristos /*
129ff4a156dSchristos * Found a free ID. The actual ID is the bit index plus one,
130ff4a156dSchristos * making zero an invalid Owner ID. Save this as the last ID
131ff4a156dSchristos * allocated and update the global ID mask.
132ff4a156dSchristos */
13389b8eb6cSchristos AcpiGbl_OwnerIdMask[j] |= ((UINT32) 1 << k);
134ff4a156dSchristos
135ff4a156dSchristos AcpiGbl_LastOwnerIdIndex = (UINT8) j;
136ff4a156dSchristos AcpiGbl_NextOwnerIdOffset = (UINT8) (k + 1);
137ff4a156dSchristos
138ff4a156dSchristos /*
139ff4a156dSchristos * Construct encoded ID from the index and bit position
140ff4a156dSchristos *
141783af925Schristos * Note: Last [j].k (bit 4095) is never used and is marked
142ff4a156dSchristos * permanently allocated (prevents +1 overflow)
143ff4a156dSchristos */
144ff4a156dSchristos *OwnerId = (ACPI_OWNER_ID) ((k + 1) + ACPI_MUL_32 (j));
145ff4a156dSchristos
146ff4a156dSchristos ACPI_DEBUG_PRINT ((ACPI_DB_VALUES,
147783af925Schristos "Allocated OwnerId: 0x%3.3X\n", (unsigned int) *OwnerId));
148ff4a156dSchristos goto Exit;
149ff4a156dSchristos }
150ff4a156dSchristos }
151ff4a156dSchristos
152ff4a156dSchristos AcpiGbl_NextOwnerIdOffset = 0;
153ff4a156dSchristos }
154ff4a156dSchristos
155ff4a156dSchristos /*
156ff4a156dSchristos * All OwnerIds have been allocated. This typically should
157ff4a156dSchristos * not happen since the IDs are reused after deallocation. The IDs are
158ff4a156dSchristos * allocated upon table load (one per table) and method execution, and
159ff4a156dSchristos * they are released when a table is unloaded or a method completes
160ff4a156dSchristos * execution.
161ff4a156dSchristos *
16271e38f1dSchristos * If this error happens, there may be very deep nesting of invoked
16371e38f1dSchristos * control methods, or there may be a bug where the IDs are not released.
164ff4a156dSchristos */
165ff4a156dSchristos Status = AE_OWNER_ID_LIMIT;
166ff4a156dSchristos ACPI_ERROR ((AE_INFO,
167783af925Schristos "Could not allocate new OwnerId (4095 max), AE_OWNER_ID_LIMIT"));
168ff4a156dSchristos
169ff4a156dSchristos Exit:
170ff4a156dSchristos (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
171ff4a156dSchristos return_ACPI_STATUS (Status);
172ff4a156dSchristos }
173ff4a156dSchristos
174ff4a156dSchristos
175ff4a156dSchristos /*******************************************************************************
176ff4a156dSchristos *
177ff4a156dSchristos * FUNCTION: AcpiUtReleaseOwnerId
178ff4a156dSchristos *
179ff4a156dSchristos * PARAMETERS: OwnerIdPtr - Pointer to a previously allocated OwnerID
180ff4a156dSchristos *
181ff4a156dSchristos * RETURN: None. No error is returned because we are either exiting a
182ff4a156dSchristos * control method or unloading a table. Either way, we would
183ff4a156dSchristos * ignore any error anyway.
184ff4a156dSchristos *
185ff4a156dSchristos * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
186ff4a156dSchristos *
187ff4a156dSchristos ******************************************************************************/
188ff4a156dSchristos
189ff4a156dSchristos void
AcpiUtReleaseOwnerId(ACPI_OWNER_ID * OwnerIdPtr)190ff4a156dSchristos AcpiUtReleaseOwnerId (
191ff4a156dSchristos ACPI_OWNER_ID *OwnerIdPtr)
192ff4a156dSchristos {
193ff4a156dSchristos ACPI_OWNER_ID OwnerId = *OwnerIdPtr;
194ff4a156dSchristos ACPI_STATUS Status;
195ff4a156dSchristos UINT32 Index;
196ff4a156dSchristos UINT32 Bit;
197ff4a156dSchristos
198ff4a156dSchristos
199ff4a156dSchristos ACPI_FUNCTION_TRACE_U32 (UtReleaseOwnerId, OwnerId);
200ff4a156dSchristos
201ff4a156dSchristos
202ff4a156dSchristos /* Always clear the input OwnerId (zero is an invalid ID) */
203ff4a156dSchristos
204ff4a156dSchristos *OwnerIdPtr = 0;
205ff4a156dSchristos
206ff4a156dSchristos /* Zero is not a valid OwnerID */
207ff4a156dSchristos
208ff4a156dSchristos if (OwnerId == 0)
209ff4a156dSchristos {
210783af925Schristos ACPI_ERROR ((AE_INFO, "Invalid OwnerId: 0x%3.3X", OwnerId));
211ff4a156dSchristos return_VOID;
212ff4a156dSchristos }
213ff4a156dSchristos
214ff4a156dSchristos /* Mutex for the global ID mask */
215ff4a156dSchristos
216ff4a156dSchristos Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
217ff4a156dSchristos if (ACPI_FAILURE (Status))
218ff4a156dSchristos {
219ff4a156dSchristos return_VOID;
220ff4a156dSchristos }
221ff4a156dSchristos
222ff4a156dSchristos /* Normalize the ID to zero */
223ff4a156dSchristos
224ff4a156dSchristos OwnerId--;
225ff4a156dSchristos
226ff4a156dSchristos /* Decode ID to index/offset pair */
227ff4a156dSchristos
228ff4a156dSchristos Index = ACPI_DIV_32 (OwnerId);
22989b8eb6cSchristos Bit = (UINT32) 1 << ACPI_MOD_32 (OwnerId);
230ff4a156dSchristos
231ff4a156dSchristos /* Free the owner ID only if it is valid */
232ff4a156dSchristos
233ff4a156dSchristos if (AcpiGbl_OwnerIdMask[Index] & Bit)
234ff4a156dSchristos {
235ff4a156dSchristos AcpiGbl_OwnerIdMask[Index] ^= Bit;
236ff4a156dSchristos }
237ff4a156dSchristos else
238ff4a156dSchristos {
239ff4a156dSchristos ACPI_ERROR ((AE_INFO,
240783af925Schristos "Attempted release of non-allocated OwnerId: 0x%3.3X", OwnerId + 1));
241ff4a156dSchristos }
242ff4a156dSchristos
243ff4a156dSchristos (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
244ff4a156dSchristos return_VOID;
245ff4a156dSchristos }
246