nrfx_atomic_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRFX_ATOMIC_INTERNAL_H__
  41. #define NRFX_ATOMIC_INTERNAL_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /* Only Cortex-M cores > 3 support LDREX/STREX instructions. */
  47. #if ((__CORTEX_M >= 0x03U) || (__CORTEX_SC >= 300U)) == 0
  48. #error "Unsupported core version"
  49. #endif
  50. #if defined ( __CC_ARM )
  51. static __asm uint32_t nrfx_atomic_internal_mov(nrfx_atomic_u32_t * p_ptr,
  52. uint32_t value,
  53. uint32_t * p_new)
  54. {
  55. /* The base standard specifies that arguments are passed in the core registers
  56. * r0-r3 and on the stack. Registers r4 and r5 must be saved on the stack.
  57. * Only even number of register pushes are allowed. This is a requirement
  58. * of the Procedure Call Standard for the ARM Architecture [AAPCS].
  59. */
  60. push {r4, r5}
  61. mov r4, r0
  62. loop_mov
  63. ldrex r0, [r4]
  64. mov r5, r1
  65. strex r3, r5, [r4]
  66. cmp r3, #0
  67. bne loop_mov
  68. str r5, [r2]
  69. pop {r4, r5}
  70. bx lr
  71. }
  72. static __asm uint32_t nrfx_atomic_internal_orr(nrfx_atomic_u32_t * p_ptr,
  73. uint32_t value,
  74. uint32_t * p_new)
  75. {
  76. push {r4, r5}
  77. mov r4, r0
  78. loop_orr
  79. ldrex r0, [r4]
  80. orr r5, r0, r1
  81. strex r3, r5, [r4]
  82. cmp r3, #0
  83. bne loop_orr
  84. str r5, [r2]
  85. pop {r4, r5}
  86. bx lr
  87. }
  88. static __asm uint32_t nrfx_atomic_internal_and(nrfx_atomic_u32_t * p_ptr,
  89. uint32_t value,
  90. uint32_t * p_new)
  91. {
  92. push {r4, r5}
  93. mov r4, r0
  94. loop_and
  95. ldrex r0, [r4]
  96. and r5, r0, r1
  97. strex r3, r5, [r4]
  98. cmp r3, #0
  99. bne loop_and
  100. str r5, [r2]
  101. pop {r4, r5}
  102. bx lr
  103. }
  104. static __asm uint32_t nrfx_atomic_internal_eor(nrfx_atomic_u32_t * p_ptr,
  105. uint32_t value,
  106. uint32_t * p_new)
  107. {
  108. push {r4, r5}
  109. mov r4, r0
  110. loop_eor
  111. ldrex r0, [r4]
  112. eor r5, r0, r1
  113. strex r3, r5, [r4]
  114. cmp r3, #0
  115. bne loop_eor
  116. str r5, [r2]
  117. pop {r4, r5}
  118. bx lr
  119. }
  120. static __asm uint32_t nrfx_atomic_internal_add(nrfx_atomic_u32_t * p_ptr,
  121. uint32_t value,
  122. uint32_t * p_new)
  123. {
  124. push {r4, r5}
  125. mov r4, r0
  126. loop_add
  127. ldrex r0, [r4]
  128. add r5, r0, r1
  129. strex r3, r5, [r4]
  130. cmp r3, #0
  131. bne loop_add
  132. str r5, [r2]
  133. pop {r4, r5}
  134. bx lr
  135. }
  136. static __asm uint32_t nrfx_atomic_internal_sub(nrfx_atomic_u32_t * p_ptr,
  137. uint32_t value,
  138. uint32_t * p_new)
  139. {
  140. push {r4, r5}
  141. mov r4, r0
  142. loop_sub
  143. ldrex r0, [r4]
  144. sub r5, r0, r1
  145. strex r3, r5, [r4]
  146. cmp r3, #0
  147. bne loop_sub
  148. str r5, [r2]
  149. pop {r4, r5}
  150. bx lr
  151. }
  152. static __asm bool nrfx_atomic_internal_cmp_exch(nrfx_atomic_u32_t * p_data,
  153. uint32_t * p_expected,
  154. uint32_t value)
  155. {
  156. #define RET_REG r0
  157. #define P_EXPC r1
  158. #define VALUE r2
  159. #define STR_RES r3
  160. #define P_DATA r4
  161. #define EXPC_VAL r5
  162. #define ACT_VAL r6
  163. push {r4-r6}
  164. mov P_DATA, r0
  165. mov RET_REG, #0
  166. loop_cmp_exch
  167. ldrex ACT_VAL, [P_DATA]
  168. ldr EXPC_VAL, [P_EXPC]
  169. cmp ACT_VAL, EXPC_VAL
  170. ittee eq
  171. strexeq STR_RES, VALUE, [P_DATA]
  172. moveq RET_REG, #1
  173. strexne STR_RES, ACT_VAL, [P_DATA]
  174. strne ACT_VAL, [P_EXPC]
  175. cmp STR_RES, #0
  176. itt ne
  177. movne RET_REG, #0
  178. bne loop_cmp_exch
  179. pop {r4-r6}
  180. bx lr
  181. #undef RET_REG
  182. #undef P_EXPC
  183. #undef VALUE
  184. #undef STR_RES
  185. #undef P_DATA
  186. #undef EXPC_VAL
  187. #undef ACT_VAL
  188. }
  189. static __asm uint32_t nrfx_atomic_internal_sub_hs(nrfx_atomic_u32_t * p_ptr,
  190. uint32_t value,
  191. uint32_t * p_new)
  192. {
  193. push {r4, r5}
  194. mov r4, r0
  195. loop_sub_ge
  196. ldrex r0, [r4]
  197. cmp r0, r1
  198. ite hs
  199. subhs r5, r0, r1
  200. movlo r5, r0
  201. strex r3, r5, [r4]
  202. cmp r3, #0
  203. bne loop_sub_ge
  204. str r5, [r2]
  205. pop {r4, r5}
  206. bx lr
  207. }
  208. #define NRFX_ATOMIC_OP(asm_op, old_val, new_val, ptr, value) \
  209. old_val = nrfx_atomic_internal_##asm_op(ptr, value, &new_val)
  210. #elif defined ( __ICCARM__ ) || defined ( __GNUC__ )
  211. /**
  212. * @brief Atomic operation generic macro.
  213. *
  214. * @param[in] asm_op Operation: mov, orr, and, eor, add, sub.
  215. * @param[out] old_val Atomic object output (uint32_t); value before operation.
  216. * @param[out] new_val Atomic object output (uint32_t); value after operation.
  217. * @param[in] value Atomic operation operand.
  218. */
  219. #define NRFX_ATOMIC_OP(asm_op, old_val, new_val, ptr, value) \
  220. { \
  221. uint32_t tmp_reg; \
  222. __ASM volatile( \
  223. "1: ldrex %["#old_val"], [%["#ptr"]]\n" \
  224. NRFX_ATOMIC_OP_##asm_op(new_val, old_val, value) \
  225. " strex %[tmp_reg], %["#new_val"], [%["#ptr"]]\n" \
  226. " teq %[tmp_reg], #0\n" \
  227. " bne.n 1b" \
  228. : \
  229. [old_val] "=&r" (old_val), \
  230. [new_val] "=&r" (new_val), \
  231. [tmp_reg] "=&r" (tmp_reg) \
  232. : \
  233. [ptr] "r" (ptr), \
  234. [value] "r" (value) \
  235. : "cc"); \
  236. (void)tmp_reg; \
  237. }
  238. #define NRFX_ATOMIC_OP_mov(new_val, old_val, value) "mov %["#new_val"], %["#value"]\n"
  239. #define NRFX_ATOMIC_OP_orr(new_val, old_val, value) "orr %["#new_val"], %["#old_val"], %["#value"]\n"
  240. #define NRFX_ATOMIC_OP_and(new_val, old_val, value) "and %["#new_val"], %["#old_val"], %["#value"]\n"
  241. #define NRFX_ATOMIC_OP_eor(new_val, old_val, value) "eor %["#new_val"], %["#old_val"], %["#value"]\n"
  242. #define NRFX_ATOMIC_OP_add(new_val, old_val, value) "add %["#new_val"], %["#old_val"], %["#value"]\n"
  243. #define NRFX_ATOMIC_OP_sub(new_val, old_val, value) "sub %["#new_val"], %["#old_val"], %["#value"]\n"
  244. #define NRFX_ATOMIC_OP_sub_hs(new_val, old_val, value) \
  245. "cmp %["#old_val"], %["#value"]\n " \
  246. "ite hs\n" \
  247. "subhs %["#new_val"], %["#old_val"], %["#value"]\n" \
  248. "movlo %["#new_val"], %["#old_val"]\n"
  249. static inline bool nrfx_atomic_internal_cmp_exch(nrfx_atomic_u32_t * p_data,
  250. uint32_t * p_expected,
  251. uint32_t value)
  252. {
  253. bool res = false;
  254. /* Temporary register used in the inline asm code for getting the result
  255. * of the strex* operations (no need to initialize it).
  256. */
  257. uint32_t tmp_reg;
  258. uint32_t act_val = 0;
  259. uint32_t exp_val = 0;
  260. __ASM volatile(
  261. "1: ldrex %[act_val], [%[ptr]]\n"
  262. " ldr %[exp_val], [%[expc]]\n"
  263. " cmp %[act_val], %[exp_val]\n"
  264. " ittee eq\n"
  265. " strexeq %[tmp_reg], %[value], [%[ptr]]\n"
  266. " moveq %[res], #1\n"
  267. " strexne %[tmp_reg], %[act_val], [%[ptr]]\n"
  268. " strne %[act_val], [%[expc]]\n"
  269. " cmp %[tmp_reg], #0\n"
  270. " itt ne\n"
  271. " movne %[res], #0\n"
  272. " bne.n 1b"
  273. :
  274. [res] "=&r" (res),
  275. [exp_val] "=&r" (exp_val),
  276. [act_val] "=&r" (act_val),
  277. [tmp_reg] "=&r" (tmp_reg)
  278. :
  279. "0" (res),
  280. "1" (exp_val),
  281. "2" (act_val),
  282. [expc] "r" (p_expected),
  283. [ptr] "r" (p_data),
  284. [value] "r" (value)
  285. : "cc");
  286. (void)tmp_reg;
  287. return res;
  288. }
  289. #else
  290. #error "Unsupported compiler"
  291. #endif
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif // NRFX_ATOMIC_INTERNAL_H__