bakery_lock_coherent.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2013-2018, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <arch_helpers.h>
  9. #include <lib/bakery_lock.h>
  10. #include <lib/el3_runtime/cpu_data.h>
  11. #include <plat/common/platform.h>
  12. /*
  13. * Functions in this file implement Bakery Algorithm for mutual exclusion with the
  14. * bakery lock data structures in coherent memory.
  15. *
  16. * ARM architecture offers a family of exclusive access instructions to
  17. * efficiently implement mutual exclusion with hardware support. However, as
  18. * well as depending on external hardware, the these instructions have defined
  19. * behavior only on certain memory types (cacheable and Normal memory in
  20. * particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
  21. * in trusted firmware are such that mutual exclusion implementation cannot
  22. * expect that accesses to the lock have the specific type required by the
  23. * architecture for these primitives to function (for example, not all
  24. * contenders may have address translation enabled).
  25. *
  26. * This implementation does not use mutual exclusion primitives. It expects
  27. * memory regions where the locks reside to be fully ordered and coherent
  28. * (either by disabling address translation, or by assigning proper attributes
  29. * when translation is enabled).
  30. *
  31. * Note that the ARM architecture guarantees single-copy atomicity for aligned
  32. * accesses regardless of status of address translation.
  33. */
  34. #define assert_bakery_entry_valid(_entry, _bakery) do { \
  35. assert((_bakery) != NULL); \
  36. assert((_entry) < BAKERY_LOCK_MAX_CPUS); \
  37. } while (false)
  38. /* Obtain a ticket for a given CPU */
  39. static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me)
  40. {
  41. unsigned int my_ticket, their_ticket;
  42. unsigned int they;
  43. /* Prevent recursive acquisition */
  44. assert(bakery_ticket_number(bakery->lock_data[me]) == 0U);
  45. /*
  46. * Flag that we're busy getting our ticket. All CPUs are iterated in the
  47. * order of their ordinal position to decide the maximum ticket value
  48. * observed so far. Our priority is set to be greater than the maximum
  49. * observed priority
  50. *
  51. * Note that it's possible that more than one contender gets the same
  52. * ticket value. That's OK as the lock is acquired based on the priority
  53. * value, not the ticket value alone.
  54. */
  55. my_ticket = 0U;
  56. bakery->lock_data[me] = make_bakery_data(CHOOSING_TICKET, my_ticket);
  57. for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
  58. their_ticket = bakery_ticket_number(bakery->lock_data[they]);
  59. if (their_ticket > my_ticket)
  60. my_ticket = their_ticket;
  61. }
  62. /*
  63. * Compute ticket; then signal to other contenders waiting for us to
  64. * finish calculating our ticket value that we're done
  65. */
  66. ++my_ticket;
  67. bakery->lock_data[me] = make_bakery_data(CHOSEN_TICKET, my_ticket);
  68. return my_ticket;
  69. }
  70. /*
  71. * Acquire bakery lock
  72. *
  73. * Contending CPUs need first obtain a non-zero ticket and then calculate
  74. * priority value. A contending CPU iterate over all other CPUs in the platform,
  75. * which may be contending for the same lock, in the order of their ordinal
  76. * position (CPU0, CPU1 and so on). A non-contending CPU will have its ticket
  77. * (and priority) value as 0. The contending CPU compares its priority with that
  78. * of others'. The CPU with the highest priority (lowest numerical value)
  79. * acquires the lock
  80. */
  81. void bakery_lock_get(bakery_lock_t *bakery)
  82. {
  83. unsigned int they, me;
  84. unsigned int my_ticket, my_prio, their_ticket;
  85. unsigned int their_bakery_data;
  86. me = plat_my_core_pos();
  87. assert_bakery_entry_valid(me, bakery);
  88. /* Get a ticket */
  89. my_ticket = bakery_get_ticket(bakery, me);
  90. /*
  91. * Now that we got our ticket, compute our priority value, then compare
  92. * with that of others, and proceed to acquire the lock
  93. */
  94. my_prio = bakery_get_priority(my_ticket, me);
  95. for (they = 0U; they < BAKERY_LOCK_MAX_CPUS; they++) {
  96. if (me == they)
  97. continue;
  98. /* Wait for the contender to get their ticket */
  99. do {
  100. their_bakery_data = bakery->lock_data[they];
  101. } while (bakery_is_choosing(their_bakery_data));
  102. /*
  103. * If the other party is a contender, they'll have non-zero
  104. * (valid) ticket value. If they do, compare priorities
  105. */
  106. their_ticket = bakery_ticket_number(their_bakery_data);
  107. if ((their_ticket != 0U) &&
  108. (bakery_get_priority(their_ticket, they) < my_prio)) {
  109. /*
  110. * They have higher priority (lower value). Wait for
  111. * their ticket value to change (either release the lock
  112. * to have it dropped to 0; or drop and probably content
  113. * again for the same lock to have an even higher value)
  114. */
  115. do {
  116. wfe();
  117. } while (their_ticket ==
  118. bakery_ticket_number(bakery->lock_data[they]));
  119. }
  120. }
  121. /*
  122. * Lock acquired. Ensure that any reads and writes from a shared
  123. * resource in the critical section read/write values after the lock is
  124. * acquired.
  125. */
  126. dmbish();
  127. }
  128. /* Release the lock and signal contenders */
  129. void bakery_lock_release(bakery_lock_t *bakery)
  130. {
  131. unsigned int me = plat_my_core_pos();
  132. assert_bakery_entry_valid(me, bakery);
  133. assert(bakery_ticket_number(bakery->lock_data[me]) != 0U);
  134. /*
  135. * Ensure that other observers see any stores in the critical section
  136. * before releasing the lock. Also ensure all loads in the critical
  137. * section are complete before releasing the lock. Release the lock by
  138. * resetting ticket. Then signal other waiting contenders.
  139. */
  140. dmbish();
  141. bakery->lock_data[me] = 0U;
  142. /* Required to ensure ordering of the following sev */
  143. dsb();
  144. sev();
  145. }