1 /*******************************************************************************
2  *
3  * Module Name: utownerid - Support for Table/Method Owner IDs
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2019, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
25  *
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.
29  *
30  * NO WARRANTY
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.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acnamesp.h"
47 
48 
49 #define _COMPONENT          ACPI_UTILITIES
50         ACPI_MODULE_NAME    ("utownerid")
51 
52 
53 /*******************************************************************************
54  *
55  * FUNCTION:    AcpiUtAllocateOwnerId
56  *
57  * PARAMETERS:  OwnerId         - Where the new owner ID is returned
58  *
59  * RETURN:      Status
60  *
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.
64  *
65  ******************************************************************************/
66 
67 ACPI_STATUS
68 AcpiUtAllocateOwnerId (
69     ACPI_OWNER_ID           *OwnerId)
70 {
71     UINT32                  i;
72     UINT32                  j;
73     UINT32                  k;
74     ACPI_STATUS             Status;
75 
76 
77     ACPI_FUNCTION_TRACE (UtAllocateOwnerId);
78 
79 
80     /* Guard against multiple allocations of ID to the same location */
81 
82     if (*OwnerId)
83     {
84         ACPI_ERROR ((AE_INFO,
85             "Owner ID [0x%3.3X] already exists", *OwnerId));
86         return_ACPI_STATUS (AE_ALREADY_EXISTS);
87     }
88 
89     /* Mutex for the global ID mask */
90 
91     Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
92     if (ACPI_FAILURE (Status))
93     {
94         return_ACPI_STATUS (Status);
95     }
96 
97     /*
98      * Find a free owner ID, cycle through all possible IDs on repeated
99      * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index
100      * may have to be scanned twice.
101      */
102     for (i = 0, j = AcpiGbl_LastOwnerIdIndex;
103          i < (ACPI_NUM_OWNERID_MASKS + 1);
104          i++, j++)
105     {
106         if (j >= ACPI_NUM_OWNERID_MASKS)
107         {
108             j = 0;  /* Wraparound to start of mask array */
109         }
110 
111         for (k = AcpiGbl_NextOwnerIdOffset; k < 32; k++)
112         {
113             if (AcpiGbl_OwnerIdMask[j] == ACPI_UINT32_MAX)
114             {
115                 /* There are no free IDs in this mask */
116 
117                 break;
118             }
119 
120             /*
121              * Note: the UINT32 cast ensures that 1 is stored as a unsigned
122              * integer. Omitting the cast may result in 1 being stored as an
123              * int. Some compilers or runtime error detection may flag this as
124              * an error.
125              */
126             if (!(AcpiGbl_OwnerIdMask[j] & ((UINT32) 1 << k)))
127             {
128                 /*
129                  * Found a free ID. The actual ID is the bit index plus one,
130                  * making zero an invalid Owner ID. Save this as the last ID
131                  * allocated and update the global ID mask.
132                  */
133                 AcpiGbl_OwnerIdMask[j] |= ((UINT32) 1 << k);
134 
135                 AcpiGbl_LastOwnerIdIndex = (UINT8) j;
136                 AcpiGbl_NextOwnerIdOffset = (UINT8) (k + 1);
137 
138                 /*
139                  * Construct encoded ID from the index and bit position
140                  *
141                  * Note: Last [j].k (bit 4095) is never used and is marked
142                  * permanently allocated (prevents +1 overflow)
143                  */
144                 *OwnerId = (ACPI_OWNER_ID) ((k + 1) + ACPI_MUL_32 (j));
145 
146                 ACPI_DEBUG_PRINT ((ACPI_DB_VALUES,
147                     "Allocated OwnerId: 0x%3.3X\n", (unsigned int) *OwnerId));
148                 goto Exit;
149             }
150         }
151 
152         AcpiGbl_NextOwnerIdOffset = 0;
153     }
154 
155     /*
156      * All OwnerIds have been allocated. This typically should
157      * not happen since the IDs are reused after deallocation. The IDs are
158      * allocated upon table load (one per table) and method execution, and
159      * they are released when a table is unloaded or a method completes
160      * execution.
161      *
162      * If this error happens, there may be very deep nesting of invoked
163      * control methods, or there may be a bug where the IDs are not released.
164      */
165     Status = AE_OWNER_ID_LIMIT;
166     ACPI_ERROR ((AE_INFO,
167         "Could not allocate new OwnerId (4095 max), AE_OWNER_ID_LIMIT"));
168 
169 Exit:
170     (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
171     return_ACPI_STATUS (Status);
172 }
173 
174 
175 /*******************************************************************************
176  *
177  * FUNCTION:    AcpiUtReleaseOwnerId
178  *
179  * PARAMETERS:  OwnerIdPtr          - Pointer to a previously allocated OwnerID
180  *
181  * RETURN:      None. No error is returned because we are either exiting a
182  *              control method or unloading a table. Either way, we would
183  *              ignore any error anyway.
184  *
185  * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
186  *
187  ******************************************************************************/
188 
189 void
190 AcpiUtReleaseOwnerId (
191     ACPI_OWNER_ID           *OwnerIdPtr)
192 {
193     ACPI_OWNER_ID           OwnerId = *OwnerIdPtr;
194     ACPI_STATUS             Status;
195     UINT32                  Index;
196     UINT32                  Bit;
197 
198 
199     ACPI_FUNCTION_TRACE_U32 (UtReleaseOwnerId, OwnerId);
200 
201 
202     /* Always clear the input OwnerId (zero is an invalid ID) */
203 
204     *OwnerIdPtr = 0;
205 
206     /* Zero is not a valid OwnerID */
207 
208     if (OwnerId == 0)
209     {
210         ACPI_ERROR ((AE_INFO, "Invalid OwnerId: 0x%3.3X", OwnerId));
211         return_VOID;
212     }
213 
214     /* Mutex for the global ID mask */
215 
216     Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
217     if (ACPI_FAILURE (Status))
218     {
219         return_VOID;
220     }
221 
222     /* Normalize the ID to zero */
223 
224     OwnerId--;
225 
226     /* Decode ID to index/offset pair */
227 
228     Index = ACPI_DIV_32 (OwnerId);
229     Bit = (UINT32) 1 << ACPI_MOD_32 (OwnerId);
230 
231     /* Free the owner ID only if it is valid */
232 
233     if (AcpiGbl_OwnerIdMask[Index] & Bit)
234     {
235         AcpiGbl_OwnerIdMask[Index] ^= Bit;
236     }
237     else
238     {
239         ACPI_ERROR ((AE_INFO,
240             "Attempted release of non-allocated OwnerId: 0x%3.3X", OwnerId + 1));
241     }
242 
243     (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
244     return_VOID;
245 }
246