utownerid.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*******************************************************************************
  2. *
  3. * Module Name: utownerid - Support for Table/Method Owner IDs
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #define _COMPONENT ACPI_UTILITIES
  46. ACPI_MODULE_NAME("utownerid")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ut_allocate_owner_id
  50. *
  51. * PARAMETERS: owner_id - Where the new owner ID is returned
  52. *
  53. * RETURN: Status
  54. *
  55. * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
  56. * track objects created by the table or method, to be deleted
  57. * when the method exits or the table is unloaded.
  58. *
  59. ******************************************************************************/
  60. acpi_status acpi_ut_allocate_owner_id(acpi_owner_id *owner_id)
  61. {
  62. u32 i;
  63. u32 j;
  64. u32 k;
  65. acpi_status status;
  66. ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
  67. /* Guard against multiple allocations of ID to the same location */
  68. if (*owner_id) {
  69. ACPI_ERROR((AE_INFO,
  70. "Owner ID [0x%2.2X] already exists", *owner_id));
  71. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  72. }
  73. /* Mutex for the global ID mask */
  74. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  75. if (ACPI_FAILURE(status)) {
  76. return_ACPI_STATUS(status);
  77. }
  78. /*
  79. * Find a free owner ID, cycle through all possible IDs on repeated
  80. * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index
  81. * may have to be scanned twice.
  82. */
  83. for (i = 0, j = acpi_gbl_last_owner_id_index;
  84. i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
  85. if (j >= ACPI_NUM_OWNERID_MASKS) {
  86. j = 0; /* Wraparound to start of mask array */
  87. }
  88. for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
  89. if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
  90. /* There are no free IDs in this mask */
  91. break;
  92. }
  93. /*
  94. * Note: the u32 cast ensures that 1 is stored as a unsigned
  95. * integer. Omitting the cast may result in 1 being stored as an
  96. * int. Some compilers or runtime error detection may flag this as
  97. * an error.
  98. */
  99. if (!(acpi_gbl_owner_id_mask[j] & ((u32)1 << k))) {
  100. /*
  101. * Found a free ID. The actual ID is the bit index plus one,
  102. * making zero an invalid Owner ID. Save this as the last ID
  103. * allocated and update the global ID mask.
  104. */
  105. acpi_gbl_owner_id_mask[j] |= ((u32)1 << k);
  106. acpi_gbl_last_owner_id_index = (u8)j;
  107. acpi_gbl_next_owner_id_offset = (u8)(k + 1);
  108. /*
  109. * Construct encoded ID from the index and bit position
  110. *
  111. * Note: Last [j].k (bit 255) is never used and is marked
  112. * permanently allocated (prevents +1 overflow)
  113. */
  114. *owner_id =
  115. (acpi_owner_id)((k + 1) + ACPI_MUL_32(j));
  116. ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
  117. "Allocated OwnerId: %2.2X\n",
  118. (unsigned int)*owner_id));
  119. goto exit;
  120. }
  121. }
  122. acpi_gbl_next_owner_id_offset = 0;
  123. }
  124. /*
  125. * All owner_ids have been allocated. This typically should
  126. * not happen since the IDs are reused after deallocation. The IDs are
  127. * allocated upon table load (one per table) and method execution, and
  128. * they are released when a table is unloaded or a method completes
  129. * execution.
  130. *
  131. * If this error happens, there may be very deep nesting of invoked
  132. * control methods, or there may be a bug where the IDs are not released.
  133. */
  134. status = AE_OWNER_ID_LIMIT;
  135. ACPI_ERROR((AE_INFO,
  136. "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
  137. exit:
  138. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  139. return_ACPI_STATUS(status);
  140. }
  141. /*******************************************************************************
  142. *
  143. * FUNCTION: acpi_ut_release_owner_id
  144. *
  145. * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_ID
  146. *
  147. * RETURN: None. No error is returned because we are either exiting a
  148. * control method or unloading a table. Either way, we would
  149. * ignore any error anyway.
  150. *
  151. * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
  152. *
  153. ******************************************************************************/
  154. void acpi_ut_release_owner_id(acpi_owner_id *owner_id_ptr)
  155. {
  156. acpi_owner_id owner_id = *owner_id_ptr;
  157. acpi_status status;
  158. u32 index;
  159. u32 bit;
  160. ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
  161. /* Always clear the input owner_id (zero is an invalid ID) */
  162. *owner_id_ptr = 0;
  163. /* Zero is not a valid owner_ID */
  164. if (owner_id == 0) {
  165. ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
  166. return_VOID;
  167. }
  168. /* Mutex for the global ID mask */
  169. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  170. if (ACPI_FAILURE(status)) {
  171. return_VOID;
  172. }
  173. /* Normalize the ID to zero */
  174. owner_id--;
  175. /* Decode ID to index/offset pair */
  176. index = ACPI_DIV_32(owner_id);
  177. bit = (u32)1 << ACPI_MOD_32(owner_id);
  178. /* Free the owner ID only if it is valid */
  179. if (acpi_gbl_owner_id_mask[index] & bit) {
  180. acpi_gbl_owner_id_mask[index] ^= bit;
  181. } else {
  182. ACPI_ERROR((AE_INFO,
  183. "Release of non-allocated OwnerId: 0x%2.2X",
  184. owner_id + 1));
  185. }
  186. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  187. return_VOID;
  188. }