Merge branch 'for-linus' into for-next
[sfrench/cifs-2.6.git] / drivers / acpi / acpica / utownerid.c
1 /*******************************************************************************
2  *
3  * Module Name: utownerid - Support for Table/Method Owner IDs
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2017, 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/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47
48 #define _COMPONENT          ACPI_UTILITIES
49 ACPI_MODULE_NAME("utownerid")
50
51 /*******************************************************************************
52  *
53  * FUNCTION:    acpi_ut_allocate_owner_id
54  *
55  * PARAMETERS:  owner_id        - Where the new owner ID is returned
56  *
57  * RETURN:      Status
58  *
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.
62  *
63  ******************************************************************************/
64 acpi_status acpi_ut_allocate_owner_id(acpi_owner_id *owner_id)
65 {
66         u32 i;
67         u32 j;
68         u32 k;
69         acpi_status status;
70
71         ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
72
73         /* Guard against multiple allocations of ID to the same location */
74
75         if (*owner_id) {
76                 ACPI_ERROR((AE_INFO,
77                             "Owner ID [0x%2.2X] already exists", *owner_id));
78                 return_ACPI_STATUS(AE_ALREADY_EXISTS);
79         }
80
81         /* Mutex for the global ID mask */
82
83         status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
84         if (ACPI_FAILURE(status)) {
85                 return_ACPI_STATUS(status);
86         }
87
88         /*
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.
92          */
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 */
97                 }
98
99                 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
100                         if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
101
102                                 /* There are no free IDs in this mask */
103
104                                 break;
105                         }
106
107                         /*
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
111                          * an error.
112                          */
113                         if (!(acpi_gbl_owner_id_mask[j] & ((u32)1 << k))) {
114                                 /*
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.
118                                  */
119                                 acpi_gbl_owner_id_mask[j] |= ((u32)1 << k);
120
121                                 acpi_gbl_last_owner_id_index = (u8)j;
122                                 acpi_gbl_next_owner_id_offset = (u8)(k + 1);
123
124                                 /*
125                                  * Construct encoded ID from the index and bit position
126                                  *
127                                  * Note: Last [j].k (bit 255) is never used and is marked
128                                  * permanently allocated (prevents +1 overflow)
129                                  */
130                                 *owner_id =
131                                     (acpi_owner_id)((k + 1) + ACPI_MUL_32(j));
132
133                                 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
134                                                   "Allocated OwnerId: %2.2X\n",
135                                                   (unsigned int)*owner_id));
136                                 goto exit;
137                         }
138                 }
139
140                 acpi_gbl_next_owner_id_offset = 0;
141         }
142
143         /*
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
148          * execution.
149          *
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.
152          */
153         status = AE_OWNER_ID_LIMIT;
154         ACPI_ERROR((AE_INFO,
155                     "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
156
157 exit:
158         (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
159         return_ACPI_STATUS(status);
160 }
161
162 /*******************************************************************************
163  *
164  * FUNCTION:    acpi_ut_release_owner_id
165  *
166  * PARAMETERS:  owner_id_ptr        - Pointer to a previously allocated owner_ID
167  *
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.
171  *
172  * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
173  *
174  ******************************************************************************/
175
176 void acpi_ut_release_owner_id(acpi_owner_id *owner_id_ptr)
177 {
178         acpi_owner_id owner_id = *owner_id_ptr;
179         acpi_status status;
180         u32 index;
181         u32 bit;
182
183         ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
184
185         /* Always clear the input owner_id (zero is an invalid ID) */
186
187         *owner_id_ptr = 0;
188
189         /* Zero is not a valid owner_ID */
190
191         if (owner_id == 0) {
192                 ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
193                 return_VOID;
194         }
195
196         /* Mutex for the global ID mask */
197
198         status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
199         if (ACPI_FAILURE(status)) {
200                 return_VOID;
201         }
202
203         /* Normalize the ID to zero */
204
205         owner_id--;
206
207         /* Decode ID to index/offset pair */
208
209         index = ACPI_DIV_32(owner_id);
210         bit = (u32)1 << ACPI_MOD_32(owner_id);
211
212         /* Free the owner ID only if it is valid */
213
214         if (acpi_gbl_owner_id_mask[index] & bit) {
215                 acpi_gbl_owner_id_mask[index] ^= bit;
216         } else {
217                 ACPI_ERROR((AE_INFO,
218                             "Release of non-allocated OwnerId: 0x%2.2X",
219                             owner_id + 1));
220         }
221
222         (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
223         return_VOID;
224 }