nrfx_atomic_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * Copyright (c) 2016 - 2019, 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 core registers r0-r3 and on the stack.
  56. * Registers r4 and r5 must be saved on the stack. Note that only even number of register pushes are
  57. * allowed. This is a requirement of the Procedure Call Standard for the ARM Architecture [AAPCS].
  58. */
  59. push {r4, r5}
  60. mov r4, r0
  61. loop_mov
  62. ldrex r0, [r4]
  63. mov r5, r1
  64. strex r3, r5, [r4]
  65. cmp r3, #0
  66. bne loop_mov
  67. str r5, [r2]
  68. pop {r4, r5}
  69. bx lr
  70. }
  71. static __asm uint32_t nrfx_atomic_internal_orr(nrfx_atomic_u32_t * p_ptr,
  72. uint32_t value,
  73. uint32_t * p_new)
  74. {
  75. push {r4, r5}
  76. mov r4, r0
  77. loop_orr
  78. ldrex r0, [r4]
  79. orr r5, r0, r1
  80. strex r3, r5, [r4]
  81. cmp r3, #0
  82. bne loop_orr
  83. str r5, [r2]
  84. pop {r4, r5}
  85. bx lr
  86. }
  87. static __asm uint32_t nrfx_atomic_internal_and(nrfx_atomic_u32_t * p_ptr,
  88. uint32_t value,
  89. uint32_t * p_new)
  90. {
  91. push {r4, r5}
  92. mov r4, r0
  93. loop_and
  94. ldrex r0, [r4]
  95. and r5, r0, r1
  96. strex r3, r5, [r4]
  97. cmp r3, #0
  98. bne loop_and
  99. str r5, [r2]
  100. pop {r4, r5}
  101. bx lr
  102. }
  103. static __asm uint32_t nrfx_atomic_internal_eor(nrfx_atomic_u32_t * p_ptr,
  104. uint32_t value,
  105. uint32_t * p_new)
  106. {
  107. push {r4, r5}
  108. mov r4, r0
  109. loop_eor
  110. ldrex r0, [r4]
  111. eor r5, r0, r1
  112. strex r3, r5, [r4]
  113. cmp r3, #0
  114. bne loop_eor
  115. str r5, [r2]
  116. pop {r4, r5}
  117. bx lr
  118. }
  119. static __asm uint32_t nrfx_atomic_internal_add(nrfx_atomic_u32_t * p_ptr,
  120. uint32_t value,
  121. uint32_t * p_new)
  122. {
  123. push {r4, r5}
  124. mov r4, r0
  125. loop_add
  126. ldrex r0, [r4]
  127. add r5, r0, r1
  128. strex r3, r5, [r4]
  129. cmp r3, #0
  130. bne loop_add
  131. str r5, [r2]
  132. pop {r4, r5}
  133. bx lr
  134. }
  135. static __asm uint32_t nrfx_atomic_internal_sub(nrfx_atomic_u32_t * p_ptr,
  136. uint32_t value,
  137. uint32_t * p_new)
  138. {
  139. push {r4, r5}
  140. mov r4, r0
  141. loop_sub
  142. ldrex r0, [r4]
  143. sub r5, r0, r1
  144. strex r3, r5, [r4]
  145. cmp r3, #0
  146. bne loop_sub
  147. str r5, [r2]
  148. pop {r4, r5}
  149. bx lr
  150. }
  151. static __asm bool nrfx_atomic_internal_cmp_exch(nrfx_atomic_u32_t * p_data,
  152. uint32_t * p_expected,
  153. uint32_t value)
  154. {
  155. #define RET_REG r0
  156. #define P_EXPC r1
  157. #define VALUE r2
  158. #define STR_RES r3
  159. #define P_DATA r4
  160. #define EXPC_VAL r5
  161. #define ACT_VAL r6
  162. push {r4-r6}
  163. mov P_DATA, r0
  164. mov RET_REG, #0
  165. loop_cmp_exch
  166. ldrex ACT_VAL, [P_DATA]
  167. ldr EXPC_VAL, [P_EXPC]
  168. cmp ACT_VAL, EXPC_VAL
  169. ittee eq
  170. strexeq STR_RES, VALUE, [P_DATA]
  171. moveq RET_REG, #1
  172. strexne STR_RES, ACT_VAL, [P_DATA]
  173. strne ACT_VAL, [P_EXPC]
  174. cmp STR_RES, #0
  175. itt ne
  176. movne RET_REG, #0
  177. bne loop_cmp_exch
  178. pop {r4-r6}
  179. bx lr
  180. #undef RET_REG
  181. #undef P_EXPC
  182. #undef VALUE
  183. #undef STR_RES
  184. #undef P_DATA
  185. #undef EXPC_VAL
  186. #undef ACT_VAL
  187. }
  188. static __asm uint32_t nrfx_atomic_internal_sub_hs(nrfx_atomic_u32_t * p_ptr,
  189. uint32_t value,
  190. uint32_t * p_new)
  191. {
  192. push {r4, r5}
  193. mov r4, r0
  194. loop_sub_ge
  195. ldrex r0, [r4]
  196. cmp r0, r1
  197. ite hs
  198. subhs r5, r0, r1
  199. movlo r5, r0
  200. strex r3, r5, [r4]
  201. cmp r3, #0
  202. bne loop_sub_ge
  203. str r5, [r2]
  204. pop {r4, r5}
  205. bx lr
  206. }
  207. #define NRFX_ATOMIC_OP(asm_op, old_val, new_val, ptr, value) \
  208. old_val = nrfx_atomic_internal_##asm_op(ptr, value, &new_val)
  209. #elif defined ( __ICCARM__ ) || defined ( __GNUC__ )
  210. /**
  211. * @brief Atomic operation generic macro.
  212. *
  213. * @param[in] asm_op Operation: mov, orr, and, eor, add, sub.
  214. * @param[out] old_val Atomic object output (uint32_t), value before operation.
  215. * @param[out] new_val Atomic object output (uint32_t), value after operation.
  216. * @param[in] value Atomic operation operand.
  217. */
  218. #define NRFX_ATOMIC_OP(asm_op, old_val, new_val, ptr, value) \
  219. { \
  220. uint32_t tmp_reg; \
  221. __ASM volatile( \
  222. "1: ldrex %["#old_val"], [%["#ptr"]]\n" \
  223. NRFX_ATOMIC_OP_##asm_op(new_val, old_val, value) \
  224. " strex %[tmp_reg], %["#new_val"], [%["#ptr"]]\n" \
  225. " teq %[tmp_reg], #0\n" \
  226. " bne.n 1b" \
  227. : \
  228. [old_val] "=&r" (old_val), \
  229. [new_val] "=&r" (new_val), \
  230. [tmp_reg] "=&r" (tmp_reg) \
  231. : \
  232. [ptr] "r" (ptr), \
  233. [value] "r" (value) \
  234. : "cc"); \
  235. (void)tmp_reg; \
  236. }
  237. #define NRFX_ATOMIC_OP_mov(new_val, old_val, value) "mov %["#new_val"], %["#value"]\n"
  238. #define NRFX_ATOMIC_OP_orr(new_val, old_val, value) "orr %["#new_val"], %["#old_val"], %["#value"]\n"
  239. #define NRFX_ATOMIC_OP_and(new_val, old_val, value) "and %["#new_val"], %["#old_val"], %["#value"]\n"
  240. #define NRFX_ATOMIC_OP_eor(new_val, old_val, value) "eor %["#new_val"], %["#old_val"], %["#value"]\n"
  241. #define NRFX_ATOMIC_OP_add(new_val, old_val, value) "add %["#new_val"], %["#old_val"], %["#value"]\n"
  242. #define NRFX_ATOMIC_OP_sub(new_val, old_val, value) "sub %["#new_val"], %["#old_val"], %["#value"]\n"
  243. #define NRFX_ATOMIC_OP_sub_hs(new_val, old_val, value) \
  244. "cmp %["#old_val"], %["#value"]\n " \
  245. "ite hs\n" \
  246. "subhs %["#new_val"], %["#old_val"], %["#value"]\n" \
  247. "movlo %["#new_val"], %["#old_val"]\n"
  248. static inline bool nrfx_atomic_internal_cmp_exch(nrfx_atomic_u32_t * p_data,
  249. uint32_t * p_expected,
  250. uint32_t value)
  251. {
  252. bool res = false;
  253. /* Temporary register used in the inline asm code for getting the result
  254. * of the strex* operations (no need to initialize it).
  255. */
  256. uint32_t tmp_reg;
  257. uint32_t act_val = 0;
  258. uint32_t exp_val = 0;
  259. __ASM volatile(
  260. "1: ldrex %[act_val], [%[ptr]]\n"
  261. " ldr %[exp_val], [%[expc]]\n"
  262. " cmp %[act_val], %[exp_val]\n"
  263. " ittee eq\n"
  264. " strexeq %[tmp_reg], %[value], [%[ptr]]\n"
  265. " moveq %[res], #1\n"
  266. " strexne %[tmp_reg], %[act_val], [%[ptr]]\n"
  267. " strne %[act_val], [%[expc]]\n"
  268. " cmp %[tmp_reg], #0\n"
  269. " itt ne\n"
  270. " movne %[res], #0\n"
  271. " bne.n 1b"
  272. :
  273. [res] "=&r" (res),
  274. [exp_val] "=&r" (exp_val),
  275. [act_val] "=&r" (act_val),
  276. [tmp_reg] "=&r" (tmp_reg)
  277. :
  278. "0" (res),
  279. "1" (exp_val),
  280. "2" (act_val),
  281. [expc] "r" (p_expected),
  282. [ptr] "r" (p_data),
  283. [value] "r" (value)
  284. : "cc");
  285. (void)tmp_reg;
  286. return res;
  287. }
  288. #else
  289. #error "Unsupported compiler"
  290. #endif
  291. #ifdef __cplusplus
  292. }
  293. #endif
  294. #endif // NRFX_ATOMIC_INTERNAL_H__