utownerid.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*******************************************************************************
  2. *
  3. * Module Name: utownerid - Support for Table/Method Owner IDs
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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, "Owner ID [0x%2.2X] already exists",
  70. *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 may have
  81. * 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. if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
  94. /*
  95. * Found a free ID. The actual ID is the bit index plus one,
  96. * making zero an invalid Owner ID. Save this as the last ID
  97. * allocated and update the global ID mask.
  98. */
  99. acpi_gbl_owner_id_mask[j] |= (1 << k);
  100. acpi_gbl_last_owner_id_index = (u8)j;
  101. acpi_gbl_next_owner_id_offset = (u8)(k + 1);
  102. /*
  103. * Construct encoded ID from the index and bit position
  104. *
  105. * Note: Last [j].k (bit 255) is never used and is marked
  106. * permanently allocated (prevents +1 overflow)
  107. */
  108. *owner_id =
  109. (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
  110. ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
  111. "Allocated OwnerId: %2.2X\n",
  112. (unsigned int)*owner_id));
  113. goto exit;
  114. }
  115. }
  116. acpi_gbl_next_owner_id_offset = 0;
  117. }
  118. /*
  119. * All owner_ids have been allocated. This typically should
  120. * not happen since the IDs are reused after deallocation. The IDs are
  121. * allocated upon table load (one per table) and method execution, and
  122. * they are released when a table is unloaded or a method completes
  123. * execution.
  124. *
  125. * If this error happens, there may be very deep nesting of invoked control
  126. * methods, or there may be a bug where the IDs are not released.
  127. */
  128. status = AE_OWNER_ID_LIMIT;
  129. ACPI_ERROR((AE_INFO,
  130. "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
  131. exit:
  132. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  133. return_ACPI_STATUS(status);
  134. }
  135. /*******************************************************************************
  136. *
  137. * FUNCTION: acpi_ut_release_owner_id
  138. *
  139. * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_ID
  140. *
  141. * RETURN: None. No error is returned because we are either exiting a
  142. * control method or unloading a table. Either way, we would
  143. * ignore any error anyway.
  144. *
  145. * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
  146. *
  147. ******************************************************************************/
  148. void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
  149. {
  150. acpi_owner_id owner_id = *owner_id_ptr;
  151. acpi_status status;
  152. u32 index;
  153. u32 bit;
  154. ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
  155. /* Always clear the input owner_id (zero is an invalid ID) */
  156. *owner_id_ptr = 0;
  157. /* Zero is not a valid owner_ID */
  158. if (owner_id == 0) {
  159. ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
  160. return_VOID;
  161. }
  162. /* Mutex for the global ID mask */
  163. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  164. if (ACPI_FAILURE(status)) {
  165. return_VOID;
  166. }
  167. /* Normalize the ID to zero */
  168. owner_id--;
  169. /* Decode ID to index/offset pair */
  170. index = ACPI_DIV_32(owner_id);
  171. bit = 1 << ACPI_MOD_32(owner_id);
  172. /* Free the owner ID only if it is valid */
  173. if (acpi_gbl_owner_id_mask[index] & bit) {
  174. acpi_gbl_owner_id_mask[index] ^= bit;
  175. } else {
  176. ACPI_ERROR((AE_INFO,
  177. "Release of non-allocated OwnerId: 0x%2.2X",
  178. owner_id + 1));
  179. }
  180. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  181. return_VOID;
  182. }