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