spinlock.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "sdkconfig.h"
  10. #include "hal/cpu_hal.h"
  11. #include "compare_set.h"
  12. #if __XTENSA__
  13. #include "xtensa/xtruntime.h"
  14. #endif
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #ifdef CONFIG_SPIRAM_WORKAROUND_NEED_VOLATILE_SPINLOCK
  19. #define NEED_VOLATILE_MUX volatile
  20. #else
  21. #define NEED_VOLATILE_MUX
  22. #endif
  23. #define SPINLOCK_FREE 0xB33FFFFF
  24. #define SPINLOCK_WAIT_FOREVER (-1)
  25. #define SPINLOCK_NO_WAIT 0
  26. #define SPINLOCK_INITIALIZER {.owner = SPINLOCK_FREE,.count = 0}
  27. #define CORE_ID_REGVAL_XOR_SWAP (0xCDCD ^ 0xABAB)
  28. typedef struct {
  29. NEED_VOLATILE_MUX uint32_t owner;
  30. NEED_VOLATILE_MUX uint32_t count;
  31. }spinlock_t;
  32. /**
  33. * @brief Initialize a lock to its default state - unlocked
  34. * @param lock - spinlock object to initialize
  35. */
  36. static inline void __attribute__((always_inline)) spinlock_initialize(spinlock_t *lock)
  37. {
  38. assert(lock);
  39. #if !CONFIG_FREERTOS_UNICORE
  40. lock->owner = SPINLOCK_FREE;
  41. lock->count = 0;
  42. #endif
  43. }
  44. /**
  45. * @brief Top level spinlock acquire function, spins until get the lock
  46. *
  47. * This function will:
  48. * - Save current interrupt state, then disable interrupts
  49. * - Spin until lock is acquired or until timeout occurs
  50. * - Restore interrupt state
  51. *
  52. * @note Spinlocks alone do no constitute true critical sections (as this
  53. * function reenables interrupts once the spinlock is acquired). For critical
  54. * sections, use the interface provided by the operating system.
  55. * @param lock - target spinlock object
  56. * @param timeout - cycles to wait, passing SPINLOCK_WAIT_FOREVER blocs indefinitely
  57. */
  58. static inline bool __attribute__((always_inline)) spinlock_acquire(spinlock_t *lock, int32_t timeout)
  59. {
  60. #if !CONFIG_FREERTOS_UNICORE && !BOOTLOADER_BUILD
  61. uint32_t result;
  62. uint32_t irq_status;
  63. uint32_t ccount_start;
  64. uint32_t core_id, other_core_id;
  65. assert(lock);
  66. irq_status = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL);
  67. if(timeout != SPINLOCK_WAIT_FOREVER){
  68. RSR(CCOUNT, ccount_start);
  69. }
  70. /*spin until we own a core */
  71. RSR(PRID, core_id);
  72. /* Note: coreID is the full 32 bit core ID (CORE_ID_REGVAL_PRO/CORE_ID_REGVAL_APP) */
  73. other_core_id = CORE_ID_REGVAL_XOR_SWAP ^ core_id;
  74. do {
  75. /* lock->owner should be one of SPINLOCK_FREE, CORE_ID_REGVAL_PRO,
  76. * CORE_ID_REGVAL_APP:
  77. * - If SPINLOCK_FREE, we want to atomically set to 'core_id'.
  78. * - If "our" core_id, we can drop through immediately.
  79. * - If "other_core_id", we spin here.
  80. */
  81. result = core_id;
  82. #if defined(CONFIG_ESP32_SPIRAM_SUPPORT)
  83. if (esp_ptr_external_ram(lock)) {
  84. compare_and_set_extram(&lock->owner, SPINLOCK_FREE, &result);
  85. } else {
  86. #endif
  87. compare_and_set_native(&lock->owner, SPINLOCK_FREE, &result);
  88. #if defined(CONFIG_ESP32_SPIRAM_SUPPORT)
  89. }
  90. #endif
  91. if(result != other_core_id) {
  92. break;
  93. }
  94. if (timeout != SPINLOCK_WAIT_FOREVER) {
  95. uint32_t ccount_now;
  96. ccount_now = cpu_hal_get_cycle_count();
  97. if (ccount_now - ccount_start > (unsigned)timeout) {
  98. XTOS_RESTORE_INTLEVEL(irq_status);
  99. return false;
  100. }
  101. }
  102. }while(1);
  103. /* any other value implies memory corruption or uninitialized mux */
  104. assert(result == core_id || result == SPINLOCK_FREE);
  105. assert((result == SPINLOCK_FREE) == (lock->count == 0)); /* we're first to lock iff count is zero */
  106. assert(lock->count < 0xFF); /* Bad count value implies memory corruption */
  107. lock->count++;
  108. XTOS_RESTORE_INTLEVEL(irq_status);
  109. return true;
  110. #else // !CONFIG_FREERTOS_UNICORE
  111. return true;
  112. #endif
  113. }
  114. /**
  115. * @brief Top level spinlock unlock function, unlocks a previously locked spinlock
  116. *
  117. * This function will:
  118. * - Save current interrupt state, then disable interrupts
  119. * - Release the spinlock
  120. * - Restore interrupt state
  121. *
  122. * @note Spinlocks alone do no constitute true critical sections (as this
  123. * function reenables interrupts once the spinlock is acquired). For critical
  124. * sections, use the interface provided by the operating system.
  125. * @param lock - target, locked before, spinlock object
  126. */
  127. static inline void __attribute__((always_inline)) spinlock_release(spinlock_t *lock)
  128. {
  129. #if !CONFIG_FREERTOS_UNICORE && !BOOTLOADER_BUILD
  130. uint32_t irq_status;
  131. uint32_t core_id;
  132. assert(lock);
  133. irq_status = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL);
  134. RSR(PRID, core_id);
  135. assert(core_id == lock->owner); // This is a mutex we didn't lock, or it's corrupt
  136. lock->count--;
  137. if(!lock->count) {
  138. lock->owner = SPINLOCK_FREE;
  139. } else {
  140. assert(lock->count < 0x100); // Indicates memory corruption
  141. }
  142. XTOS_RESTORE_INTLEVEL(irq_status);
  143. #endif
  144. }
  145. #ifdef __cplusplus
  146. }
  147. #endif