lock.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright 2019 SiFive, Inc */
  2. /* SPDX-License-Identifier: Apache-2.0 */
  3. #ifndef METAL__LOCK_H
  4. #define METAL__LOCK_H
  5. #include <metal/machine.h>
  6. #include <metal/memory.h>
  7. #include <metal/compiler.h>
  8. /*!
  9. * @file lock.h
  10. * @brief An API for creating and using a software lock/mutex
  11. */
  12. /* TODO: How can we make the exception code platform-independant? */
  13. #define _METAL_STORE_AMO_ACCESS_FAULT 7
  14. #define METAL_LOCK_BACKOFF_CYCLES 32
  15. #define METAL_LOCK_BACKOFF_EXPONENT 2
  16. /*!
  17. * @def METAL_LOCK_DECLARE
  18. * @brief Declare a lock
  19. *
  20. * Locks must be declared with METAL_LOCK_DECLARE to ensure that the lock
  21. * is linked into a memory region which supports atomic memory operations.
  22. */
  23. #define METAL_LOCK_DECLARE(name) \
  24. __attribute__((section(".data.locks"))) \
  25. struct metal_lock name
  26. /*!
  27. * @brief A handle for a lock
  28. */
  29. struct metal_lock {
  30. int _state;
  31. };
  32. /*!
  33. * @brief Initialize a lock
  34. * @param lock The handle for a lock
  35. * @return 0 if the lock is successfully initialized. A non-zero code indicates failure.
  36. *
  37. * If the lock cannot be initialized, attempts to take or give the lock
  38. * will result in a Store/AMO access fault.
  39. */
  40. __inline__ int metal_lock_init(struct metal_lock *lock) {
  41. #ifdef __riscv_atomic
  42. /* Get a handle for the memory which holds the lock state */
  43. struct metal_memory *lock_mem = metal_get_memory_from_address((uintptr_t) &(lock->_state));
  44. if(!lock_mem) {
  45. return 1;
  46. }
  47. /* If the memory doesn't support atomics, report an error */
  48. if(!metal_memory_supports_atomics(lock_mem)) {
  49. return 2;
  50. }
  51. lock->_state = 0;
  52. return 0;
  53. #else
  54. return 3;
  55. #endif
  56. }
  57. /*!
  58. * @brief Take a lock
  59. * @param lock The handle for a lock
  60. * @return 0 if the lock is successfully taken
  61. *
  62. * If the lock initialization failed, attempts to take a lock will result in
  63. * a Store/AMO access fault.
  64. */
  65. __inline__ int metal_lock_take(struct metal_lock *lock) {
  66. #ifdef __riscv_atomic
  67. int old = 1;
  68. int new = 1;
  69. int backoff = 1;
  70. const int max_backoff = METAL_LOCK_BACKOFF_CYCLES * METAL_MAX_CORES;
  71. while(1) {
  72. __asm__ volatile("amoswap.w.aq %[old], %[new], (%[state])"
  73. : [old] "=r" (old)
  74. : [new] "r" (new), [state] "r" (&(lock->_state))
  75. : "memory");
  76. if (old == 0) {
  77. break;
  78. }
  79. for (int i = 0; i < backoff; i++) {
  80. __asm__ volatile("");
  81. }
  82. if (backoff < max_backoff) {
  83. backoff *= METAL_LOCK_BACKOFF_EXPONENT;
  84. }
  85. }
  86. return 0;
  87. #else
  88. /* Store the memory address in mtval like a normal store/amo access fault */
  89. __asm__ ("csrw mtval, %[state]"
  90. :: [state] "r" (&(lock->_state)));
  91. /* Trigger a Store/AMO access fault */
  92. _metal_trap(_METAL_STORE_AMO_ACCESS_FAULT);
  93. /* If execution returns, indicate failure */
  94. return 1;
  95. #endif
  96. }
  97. /*!
  98. * @brief Give back a held lock
  99. * @param lock The handle for a lock
  100. * @return 0 if the lock is successfully given
  101. *
  102. * If the lock initialization failed, attempts to give a lock will result in
  103. * a Store/AMO access fault.
  104. */
  105. __inline__ int metal_lock_give(struct metal_lock *lock) {
  106. #ifdef __riscv_atomic
  107. __asm__ volatile("amoswap.w.rl x0, x0, (%[state])"
  108. :: [state] "r" (&(lock->_state))
  109. : "memory");
  110. return 0;
  111. #else
  112. /* Store the memory address in mtval like a normal store/amo access fault */
  113. __asm__ ("csrw mtval, %[state]"
  114. :: [state] "r" (&(lock->_state)));
  115. /* Trigger a Store/AMO access fault */
  116. _metal_trap(_METAL_STORE_AMO_ACCESS_FAULT);
  117. /* If execution returns, indicate failure */
  118. return 1;
  119. #endif
  120. }
  121. #endif /* METAL__LOCK_H */