utownerid.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: utownerid - Support for Table/Method Owner IDs
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acnamesp.h"
  10. #define _COMPONENT ACPI_UTILITIES
  11. ACPI_MODULE_NAME("utownerid")
  12. /*******************************************************************************
  13. *
  14. * FUNCTION: acpi_ut_allocate_owner_id
  15. *
  16. * PARAMETERS: owner_id - Where the new owner ID is returned
  17. *
  18. * RETURN: Status
  19. *
  20. * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
  21. * track objects created by the table or method, to be deleted
  22. * when the method exits or the table is unloaded.
  23. *
  24. ******************************************************************************/
  25. acpi_status acpi_ut_allocate_owner_id(acpi_owner_id *owner_id)
  26. {
  27. u32 i;
  28. u32 j;
  29. u32 k;
  30. acpi_status status;
  31. ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
  32. /* Guard against multiple allocations of ID to the same location */
  33. if (*owner_id) {
  34. ACPI_ERROR((AE_INFO,
  35. "Owner ID [0x%3.3X] already exists", *owner_id));
  36. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  37. }
  38. /* Mutex for the global ID mask */
  39. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  40. if (ACPI_FAILURE(status)) {
  41. return_ACPI_STATUS(status);
  42. }
  43. /*
  44. * Find a free owner ID, cycle through all possible IDs on repeated
  45. * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index
  46. * may have to be scanned twice.
  47. */
  48. for (i = 0, j = acpi_gbl_last_owner_id_index;
  49. i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
  50. if (j >= ACPI_NUM_OWNERID_MASKS) {
  51. j = 0; /* Wraparound to start of mask array */
  52. }
  53. for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
  54. if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
  55. /* There are no free IDs in this mask */
  56. break;
  57. }
  58. /*
  59. * Note: the u32 cast ensures that 1 is stored as a unsigned
  60. * integer. Omitting the cast may result in 1 being stored as an
  61. * int. Some compilers or runtime error detection may flag this as
  62. * an error.
  63. */
  64. if (!(acpi_gbl_owner_id_mask[j] & ((u32)1 << k))) {
  65. /*
  66. * Found a free ID. The actual ID is the bit index plus one,
  67. * making zero an invalid Owner ID. Save this as the last ID
  68. * allocated and update the global ID mask.
  69. */
  70. acpi_gbl_owner_id_mask[j] |= ((u32)1 << k);
  71. acpi_gbl_last_owner_id_index = (u8)j;
  72. acpi_gbl_next_owner_id_offset = (u8)(k + 1);
  73. /*
  74. * Construct encoded ID from the index and bit position
  75. *
  76. * Note: Last [j].k (bit 4095) is never used and is marked
  77. * permanently allocated (prevents +1 overflow)
  78. */
  79. *owner_id =
  80. (acpi_owner_id)((k + 1) + ACPI_MUL_32(j));
  81. ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
  82. "Allocated OwnerId: 0x%3.3X\n",
  83. (unsigned int)*owner_id));
  84. goto exit;
  85. }
  86. }
  87. acpi_gbl_next_owner_id_offset = 0;
  88. }
  89. /*
  90. * All owner_ids have been allocated. This typically should
  91. * not happen since the IDs are reused after deallocation. The IDs are
  92. * allocated upon table load (one per table) and method execution, and
  93. * they are released when a table is unloaded or a method completes
  94. * execution.
  95. *
  96. * If this error happens, there may be very deep nesting of invoked
  97. * control methods, or there may be a bug where the IDs are not released.
  98. */
  99. status = AE_OWNER_ID_LIMIT;
  100. ACPI_ERROR((AE_INFO,
  101. "Could not allocate new OwnerId (4095 max), AE_OWNER_ID_LIMIT"));
  102. exit:
  103. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  104. return_ACPI_STATUS(status);
  105. }
  106. /*******************************************************************************
  107. *
  108. * FUNCTION: acpi_ut_release_owner_id
  109. *
  110. * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_ID
  111. *
  112. * RETURN: None. No error is returned because we are either exiting a
  113. * control method or unloading a table. Either way, we would
  114. * ignore any error anyway.
  115. *
  116. * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
  117. *
  118. ******************************************************************************/
  119. void acpi_ut_release_owner_id(acpi_owner_id *owner_id_ptr)
  120. {
  121. acpi_owner_id owner_id = *owner_id_ptr;
  122. acpi_status status;
  123. u32 index;
  124. u32 bit;
  125. ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
  126. /* Always clear the input owner_id (zero is an invalid ID) */
  127. *owner_id_ptr = 0;
  128. /* Zero is not a valid owner_ID */
  129. if (owner_id == 0) {
  130. ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%3.3X", owner_id));
  131. return_VOID;
  132. }
  133. /* Mutex for the global ID mask */
  134. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  135. if (ACPI_FAILURE(status)) {
  136. return_VOID;
  137. }
  138. /* Normalize the ID to zero */
  139. owner_id--;
  140. /* Decode ID to index/offset pair */
  141. index = ACPI_DIV_32(owner_id);
  142. bit = (u32)1 << ACPI_MOD_32(owner_id);
  143. /* Free the owner ID only if it is valid */
  144. if (acpi_gbl_owner_id_mask[index] & bit) {
  145. acpi_gbl_owner_id_mask[index] ^= bit;
  146. } else {
  147. ACPI_ERROR((AE_INFO,
  148. "Attempted release of non-allocated OwnerId: 0x%3.3X",
  149. owner_id + 1));
  150. }
  151. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  152. return_VOID;
  153. }