utownerid.c revision 245582
1254721Semaste/*******************************************************************************
2254721Semaste *
3254721Semaste * Module Name: utownerid - Support for Table/Method Owner IDs
4254721Semaste *
5254721Semaste ******************************************************************************/
6254721Semaste
7254721Semaste/*
8254721Semaste * Copyright (C) 2000 - 2013, Intel Corp.
9254721Semaste * All rights reserved.
10254721Semaste *
11254721Semaste * Redistribution and use in source and binary forms, with or without
12254721Semaste * modification, are permitted provided that the following conditions
13254721Semaste * are met:
14254721Semaste * 1. Redistributions of source code must retain the above copyright
15254721Semaste *    notice, this list of conditions, and the following disclaimer,
16254721Semaste *    without modification.
17254721Semaste * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18254721Semaste *    substantially similar to the "NO WARRANTY" disclaimer below
19254721Semaste *    ("Disclaimer") and any redistribution must be conditioned upon
20254721Semaste *    including a substantially similar Disclaimer requirement for further
21254721Semaste *    binary redistribution.
22254721Semaste * 3. Neither the names of the above-listed copyright holders nor the names
23254721Semaste *    of any contributors may be used to endorse or promote products derived
24254721Semaste *    from this software without specific prior written permission.
25254721Semaste *
26254721Semaste * Alternatively, this software may be distributed under the terms of the
27254721Semaste * GNU General Public License ("GPL") version 2 as published by the Free
28254721Semaste * Software Foundation.
29254721Semaste *
30254721Semaste * NO WARRANTY
31254721Semaste * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32254721Semaste * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33254721Semaste * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34254721Semaste * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35254721Semaste * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39254721Semaste * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40254721Semaste * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41254721Semaste * POSSIBILITY OF SUCH DAMAGES.
42254721Semaste */
43276479Sdim
44276479Sdim
45276479Sdim#define __UTOWNERID_C__
46276479Sdim
47254721Semaste#include <contrib/dev/acpica/include/acpi.h>
48254721Semaste#include <contrib/dev/acpica/include/accommon.h>
49254721Semaste#include <contrib/dev/acpica/include/acnamesp.h>
50254721Semaste
51254721Semaste
52254721Semaste#define _COMPONENT          ACPI_UTILITIES
53254721Semaste        ACPI_MODULE_NAME    ("utownerid")
54254721Semaste
55254721Semaste
56254721Semaste/*******************************************************************************
57254721Semaste *
58254721Semaste * FUNCTION:    AcpiUtAllocateOwnerId
59254721Semaste *
60254721Semaste * PARAMETERS:  OwnerId         - Where the new owner ID is returned
61254721Semaste *
62254721Semaste * RETURN:      Status
63254721Semaste *
64254721Semaste * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
65254721Semaste *              track objects created by the table or method, to be deleted
66254721Semaste *              when the method exits or the table is unloaded.
67254721Semaste *
68254721Semaste ******************************************************************************/
69254721Semaste
70254721SemasteACPI_STATUS
71254721SemasteAcpiUtAllocateOwnerId (
72254721Semaste    ACPI_OWNER_ID           *OwnerId)
73254721Semaste{
74254721Semaste    UINT32                  i;
75254721Semaste    UINT32                  j;
76254721Semaste    UINT32                  k;
77254721Semaste    ACPI_STATUS             Status;
78254721Semaste
79288943Sdim
80254721Semaste    ACPI_FUNCTION_TRACE (UtAllocateOwnerId);
81254721Semaste
82254721Semaste
83254721Semaste    /* Guard against multiple allocations of ID to the same location */
84254721Semaste
85254721Semaste    if (*OwnerId)
86254721Semaste    {
87254721Semaste        ACPI_ERROR ((AE_INFO, "Owner ID [0x%2.2X] already exists", *OwnerId));
88254721Semaste        return_ACPI_STATUS (AE_ALREADY_EXISTS);
89288943Sdim    }
90254721Semaste
91254721Semaste    /* Mutex for the global ID mask */
92254721Semaste
93254721Semaste    Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
94254721Semaste    if (ACPI_FAILURE (Status))
95254721Semaste    {
96254721Semaste        return_ACPI_STATUS (Status);
97254721Semaste    }
98254721Semaste
99254721Semaste    /*
100254721Semaste     * Find a free owner ID, cycle through all possible IDs on repeated
101254721Semaste     * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
102288943Sdim     * to be scanned twice.
103254721Semaste     */
104254721Semaste    for (i = 0, j = AcpiGbl_LastOwnerIdIndex;
105254721Semaste         i < (ACPI_NUM_OWNERID_MASKS + 1);
106254721Semaste         i++, j++)
107254721Semaste    {
108254721Semaste        if (j >= ACPI_NUM_OWNERID_MASKS)
109254721Semaste        {
110276479Sdim            j = 0;  /* Wraparound to start of mask array */
111254721Semaste        }
112254721Semaste
113254721Semaste        for (k = AcpiGbl_NextOwnerIdOffset; k < 32; k++)
114254721Semaste        {
115254721Semaste            if (AcpiGbl_OwnerIdMask[j] == ACPI_UINT32_MAX)
116254721Semaste            {
117254721Semaste                /* There are no free IDs in this mask */
118254721Semaste
119254721Semaste                break;
120254721Semaste            }
121254721Semaste
122254721Semaste            if (!(AcpiGbl_OwnerIdMask[j] & (1 << k)))
123254721Semaste            {
124254721Semaste                /*
125254721Semaste                 * Found a free ID. The actual ID is the bit index plus one,
126254721Semaste                 * making zero an invalid Owner ID. Save this as the last ID
127254721Semaste                 * allocated and update the global ID mask.
128254721Semaste                 */
129254721Semaste                AcpiGbl_OwnerIdMask[j] |= (1 << k);
130254721Semaste
131254721Semaste                AcpiGbl_LastOwnerIdIndex = (UINT8) j;
132254721Semaste                AcpiGbl_NextOwnerIdOffset = (UINT8) (k + 1);
133254721Semaste
134254721Semaste                /*
135254721Semaste                 * Construct encoded ID from the index and bit position
136254721Semaste                 *
137254721Semaste                 * Note: Last [j].k (bit 255) is never used and is marked
138254721Semaste                 * permanently allocated (prevents +1 overflow)
139254721Semaste                 */
140254721Semaste                *OwnerId = (ACPI_OWNER_ID) ((k + 1) + ACPI_MUL_32 (j));
141254721Semaste
142254721Semaste                ACPI_DEBUG_PRINT ((ACPI_DB_VALUES,
143254721Semaste                    "Allocated OwnerId: %2.2X\n", (unsigned int) *OwnerId));
144254721Semaste                goto Exit;
145254721Semaste            }
146254721Semaste        }
147254721Semaste
148254721Semaste        AcpiGbl_NextOwnerIdOffset = 0;
149254721Semaste    }
150254721Semaste
151254721Semaste    /*
152254721Semaste     * All OwnerIds have been allocated. This typically should
153254721Semaste     * not happen since the IDs are reused after deallocation. The IDs are
154254721Semaste     * allocated upon table load (one per table) and method execution, and
155254721Semaste     * they are released when a table is unloaded or a method completes
156254721Semaste     * execution.
157254721Semaste     *
158254721Semaste     * If this error happens, there may be very deep nesting of invoked control
159258054Semaste     * methods, or there may be a bug where the IDs are not released.
160258054Semaste     */
161258054Semaste    Status = AE_OWNER_ID_LIMIT;
162254721Semaste    ACPI_ERROR ((AE_INFO,
163254721Semaste        "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
164254721Semaste
165254721SemasteExit:
166254721Semaste    (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
167254721Semaste    return_ACPI_STATUS (Status);
168254721Semaste}
169254721Semaste
170254721Semaste
171254721Semaste/*******************************************************************************
172254721Semaste *
173254721Semaste * FUNCTION:    AcpiUtReleaseOwnerId
174254721Semaste *
175254721Semaste * PARAMETERS:  OwnerIdPtr          - Pointer to a previously allocated OwnerID
176254721Semaste *
177254721Semaste * RETURN:      None. No error is returned because we are either exiting a
178254721Semaste *              control method or unloading a table. Either way, we would
179254721Semaste *              ignore any error anyway.
180254721Semaste *
181254721Semaste * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
182254721Semaste *
183254721Semaste ******************************************************************************/
184254721Semaste
185254721Semastevoid
186254721SemasteAcpiUtReleaseOwnerId (
187254721Semaste    ACPI_OWNER_ID           *OwnerIdPtr)
188254721Semaste{
189254721Semaste    ACPI_OWNER_ID           OwnerId = *OwnerIdPtr;
190254721Semaste    ACPI_STATUS             Status;
191254721Semaste    UINT32                  Index;
192254721Semaste    UINT32                  Bit;
193254721Semaste
194254721Semaste
195254721Semaste    ACPI_FUNCTION_TRACE_U32 (UtReleaseOwnerId, OwnerId);
196254721Semaste
197254721Semaste
198254721Semaste    /* Always clear the input OwnerId (zero is an invalid ID) */
199254721Semaste
200254721Semaste    *OwnerIdPtr = 0;
201254721Semaste
202254721Semaste    /* Zero is not a valid OwnerID */
203254721Semaste
204254721Semaste    if (OwnerId == 0)
205254721Semaste    {
206254721Semaste        ACPI_ERROR ((AE_INFO, "Invalid OwnerId: 0x%2.2X", OwnerId));
207254721Semaste        return_VOID;
208254721Semaste    }
209254721Semaste
210254721Semaste    /* Mutex for the global ID mask */
211254721Semaste
212254721Semaste    Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
213254721Semaste    if (ACPI_FAILURE (Status))
214254721Semaste    {
215254721Semaste        return_VOID;
216254721Semaste    }
217254721Semaste
218254721Semaste    /* Normalize the ID to zero */
219254721Semaste
220254721Semaste    OwnerId--;
221254721Semaste
222254721Semaste    /* Decode ID to index/offset pair */
223254721Semaste
224254721Semaste    Index = ACPI_DIV_32 (OwnerId);
225254721Semaste    Bit = 1 << ACPI_MOD_32 (OwnerId);
226254721Semaste
227254721Semaste    /* Free the owner ID only if it is valid */
228254721Semaste
229254721Semaste    if (AcpiGbl_OwnerIdMask[Index] & Bit)
230254721Semaste    {
231254721Semaste        AcpiGbl_OwnerIdMask[Index] ^= Bit;
232254721Semaste    }
233254721Semaste    else
234254721Semaste    {
235254721Semaste        ACPI_ERROR ((AE_INFO,
236254721Semaste            "Release of non-allocated OwnerId: 0x%2.2X", OwnerId + 1));
237254721Semaste    }
238254721Semaste
239254721Semaste    (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
240254721Semaste    return_VOID;
241254721Semaste}
242254721Semaste