1 /*******************************************************************************
3 * Module Name: utownerid - Support for Table/Method Owner IDs
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2018, 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 <acpi/acpi.h>
48 #define _COMPONENT ACPI_UTILITIES
49 ACPI_MODULE_NAME("utownerid")
51 /*******************************************************************************
53 * FUNCTION: acpi_ut_allocate_owner_id
55 * PARAMETERS: owner_id - Where the new owner ID is returned
59 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
60 * track objects created by the table or method, to be deleted
61 * when the method exits or the table is unloaded.
63 ******************************************************************************/
64 acpi_status
acpi_ut_allocate_owner_id(acpi_owner_id
*owner_id
)
71 ACPI_FUNCTION_TRACE(ut_allocate_owner_id
);
73 /* Guard against multiple allocations of ID to the same location */
77 "Owner ID [0x%2.2X] already exists", *owner_id
));
78 return_ACPI_STATUS(AE_ALREADY_EXISTS
);
81 /* Mutex for the global ID mask */
83 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
84 if (ACPI_FAILURE(status
)) {
85 return_ACPI_STATUS(status
);
89 * Find a free owner ID, cycle through all possible IDs on repeated
90 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index
91 * may have to be scanned twice.
93 for (i
= 0, j
= acpi_gbl_last_owner_id_index
;
94 i
< (ACPI_NUM_OWNERID_MASKS
+ 1); i
++, j
++) {
95 if (j
>= ACPI_NUM_OWNERID_MASKS
) {
96 j
= 0; /* Wraparound to start of mask array */
99 for (k
= acpi_gbl_next_owner_id_offset
; k
< 32; k
++) {
100 if (acpi_gbl_owner_id_mask
[j
] == ACPI_UINT32_MAX
) {
102 /* There are no free IDs in this mask */
108 * Note: the u32 cast ensures that 1 is stored as a unsigned
109 * integer. Omitting the cast may result in 1 being stored as an
110 * int. Some compilers or runtime error detection may flag this as
113 if (!(acpi_gbl_owner_id_mask
[j
] & ((u32
)1 << k
))) {
115 * Found a free ID. The actual ID is the bit index plus one,
116 * making zero an invalid Owner ID. Save this as the last ID
117 * allocated and update the global ID mask.
119 acpi_gbl_owner_id_mask
[j
] |= ((u32
)1 << k
);
121 acpi_gbl_last_owner_id_index
= (u8
)j
;
122 acpi_gbl_next_owner_id_offset
= (u8
)(k
+ 1);
125 * Construct encoded ID from the index and bit position
127 * Note: Last [j].k (bit 255) is never used and is marked
128 * permanently allocated (prevents +1 overflow)
131 (acpi_owner_id
)((k
+ 1) + ACPI_MUL_32(j
));
133 ACPI_DEBUG_PRINT((ACPI_DB_VALUES
,
134 "Allocated OwnerId: %2.2X\n",
135 (unsigned int)*owner_id
));
140 acpi_gbl_next_owner_id_offset
= 0;
144 * All owner_ids have been allocated. This typically should
145 * not happen since the IDs are reused after deallocation. The IDs are
146 * allocated upon table load (one per table) and method execution, and
147 * they are released when a table is unloaded or a method completes
150 * If this error happens, there may be very deep nesting of invoked
151 * control methods, or there may be a bug where the IDs are not released.
153 status
= AE_OWNER_ID_LIMIT
;
155 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
158 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
159 return_ACPI_STATUS(status
);
162 /*******************************************************************************
164 * FUNCTION: acpi_ut_release_owner_id
166 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_ID
168 * RETURN: None. No error is returned because we are either exiting a
169 * control method or unloading a table. Either way, we would
170 * ignore any error anyway.
172 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
174 ******************************************************************************/
176 void acpi_ut_release_owner_id(acpi_owner_id
*owner_id_ptr
)
178 acpi_owner_id owner_id
= *owner_id_ptr
;
183 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id
, owner_id
);
185 /* Always clear the input owner_id (zero is an invalid ID) */
189 /* Zero is not a valid owner_ID */
192 ACPI_ERROR((AE_INFO
, "Invalid OwnerId: 0x%2.2X", owner_id
));
196 /* Mutex for the global ID mask */
198 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
199 if (ACPI_FAILURE(status
)) {
203 /* Normalize the ID to zero */
207 /* Decode ID to index/offset pair */
209 index
= ACPI_DIV_32(owner_id
);
210 bit
= (u32
)1 << ACPI_MOD_32(owner_id
);
212 /* Free the owner ID only if it is valid */
214 if (acpi_gbl_owner_id_mask
[index
] & bit
) {
215 acpi_gbl_owner_id_mask
[index
] ^= bit
;
218 "Release of non-allocated OwnerId: 0x%2.2X",
222 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);