nrf_atomic_internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * Copyright (c) 2016 - 2018, 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 NRF_ATOMIC_INTERNAL_H__
  41. #define NRF_ATOMIC_INTERNAL_H__
  42. #include "sdk_common.h"
  43. #include <stdbool.h>
  44. #include <stdint.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. *
  50. * @defgroup nrf_atomic_internal Atomic operations internals
  51. * @ingroup nrf_atomic
  52. * @{
  53. *
  54. */
  55. /* Only Cortex M cores > 3 support LDREX/STREX instructions*/
  56. #if ((__CORTEX_M >= 0x03U) || (__CORTEX_SC >= 300U)) == 0
  57. #error "Unsupported core version"
  58. #endif
  59. #if defined ( __CC_ARM )
  60. static __asm uint32_t nrf_atomic_internal_mov(nrf_atomic_u32_t * p_ptr,
  61. uint32_t value,
  62. uint32_t * p_new)
  63. {
  64. /* The base standard provides for passing arguments in core registers (r0-r3) and on the stack.
  65. * Registers r4 and r5 have to be saved on stack. Note that only even number of register push are
  66. * allowed. This is a requirement of the Procedure Call Standard for the ARM Architecture [AAPCS].
  67. * */
  68. push {r4, r5}
  69. mov r4, r0
  70. loop_mov
  71. ldrex r0, [r4]
  72. mov r5, r1
  73. strex r3, r5, [r4]
  74. cmp r3, #0
  75. bne loop_mov
  76. str r5, [r2]
  77. pop {r4, r5}
  78. bx lr
  79. }
  80. static __asm uint32_t nrf_atomic_internal_orr(nrf_atomic_u32_t * p_ptr,
  81. uint32_t value,
  82. uint32_t * p_new)
  83. {
  84. push {r4, r5}
  85. mov r4, r0
  86. loop_orr
  87. ldrex r0, [r4]
  88. orr r5, r0, r1
  89. strex r3, r5, [r4]
  90. cmp r3, #0
  91. bne loop_orr
  92. str r5, [r2]
  93. pop {r4, r5}
  94. bx lr
  95. }
  96. static __asm uint32_t nrf_atomic_internal_and(nrf_atomic_u32_t * p_ptr,
  97. uint32_t value,
  98. uint32_t * p_new)
  99. {
  100. push {r4, r5}
  101. mov r4, r0
  102. loop_and
  103. ldrex r0, [r4]
  104. and r5, r0, r1
  105. strex r3, r5, [r4]
  106. cmp r3, #0
  107. bne loop_and
  108. str r5, [r2]
  109. pop {r4, r5}
  110. bx lr
  111. }
  112. static __asm uint32_t nrf_atomic_internal_eor(nrf_atomic_u32_t * p_ptr,
  113. uint32_t value,
  114. uint32_t * p_new)
  115. {
  116. push {r4, r5}
  117. mov r4, r0
  118. loop_eor
  119. ldrex r0, [r4]
  120. eor r5, r0, r1
  121. strex r3, r5, [r4]
  122. cmp r3, #0
  123. bne loop_eor
  124. str r5, [r2]
  125. pop {r4, r5}
  126. bx lr
  127. }
  128. static __asm uint32_t nrf_atomic_internal_add(nrf_atomic_u32_t * p_ptr,
  129. uint32_t value,
  130. uint32_t * p_new)
  131. {
  132. push {r4, r5}
  133. mov r4, r0
  134. loop_add
  135. ldrex r0, [r4]
  136. add r5, r0, r1
  137. strex r3, r5, [r4]
  138. cmp r3, #0
  139. bne loop_add
  140. str r5, [r2]
  141. pop {r4, r5}
  142. bx lr
  143. }
  144. static __asm uint32_t nrf_atomic_internal_sub(nrf_atomic_u32_t * p_ptr,
  145. uint32_t value,
  146. uint32_t * p_new)
  147. {
  148. push {r4, r5}
  149. mov r4, r0
  150. loop_sub
  151. ldrex r0, [r4]
  152. sub r5, r0, r1
  153. strex r3, r5, [r4]
  154. cmp r3, #0
  155. bne loop_sub
  156. str r5, [r2]
  157. pop {r4, r5}
  158. bx lr
  159. }
  160. static __asm bool nrf_atomic_internal_cmp_exch(nrf_atomic_u32_t * p_data,
  161. uint32_t * p_expected,
  162. uint32_t value)
  163. {
  164. #define RET_REG r0
  165. #define P_EXPC r1
  166. #define VALUE r2
  167. #define STR_RES r3
  168. #define P_DATA r4
  169. #define EXPC_VAL r5
  170. #define ACT_VAL r6
  171. push {r4-r6}
  172. mov P_DATA, r0
  173. mov RET_REG, #0
  174. loop_cmp_exch
  175. ldrex ACT_VAL, [P_DATA]
  176. ldr EXPC_VAL, [P_EXPC]
  177. cmp ACT_VAL, EXPC_VAL
  178. ittee eq
  179. strexeq STR_RES, VALUE, [P_DATA]
  180. moveq RET_REG, #1
  181. strexne STR_RES, ACT_VAL, [P_DATA]
  182. strne ACT_VAL, [P_EXPC]
  183. cmp STR_RES, #0
  184. itt ne
  185. movne RET_REG, #0
  186. bne loop_cmp_exch
  187. pop {r4-r6}
  188. bx lr
  189. #undef RET_REG
  190. #undef P_EXPC
  191. #undef VALUE
  192. #undef STR_RES
  193. #undef P_DATA
  194. #undef EXPC_VAL
  195. #undef ACT_VAL
  196. }
  197. static __asm uint32_t nrf_atomic_internal_sub_hs(nrf_atomic_u32_t * p_ptr,
  198. uint32_t value,
  199. uint32_t * p_new)
  200. {
  201. push {r4, r5}
  202. mov r4, r0
  203. loop_sub_ge
  204. ldrex r0, [r4]
  205. cmp r0, r1
  206. ite hs
  207. subhs r5, r0, r1
  208. movlo r5, r0
  209. strex r3, r5, [r4]
  210. cmp r3, #0
  211. bne loop_sub_ge
  212. str r5, [r2]
  213. pop {r4, r5}
  214. bx lr
  215. }
  216. #define NRF_ATOMIC_OP(asm_op, old_val, new_val, ptr, value) \
  217. old_val = nrf_atomic_internal_##asm_op(ptr, value, &new_val)
  218. #elif defined ( __ICCARM__ ) || defined ( __GNUC__ )
  219. /**
  220. * @brief Atomic operation generic macro
  221. * @param[in] asm_op operation: mov, orr, and, eor, add, sub
  222. * @param[out] old_val atomic object output (uint32_t), value before operation
  223. * @param[out] new_val atomic object output (uint32_t), value after operation
  224. * @param[in] value atomic operation operand
  225. * */
  226. #define NRF_ATOMIC_OP(asm_op, old_val, new_val, ptr, value) \
  227. { \
  228. uint32_t str_res; \
  229. __ASM volatile( \
  230. "1: ldrex %["#old_val"], [%["#ptr"]]\n" \
  231. NRF_ATOMIC_OP_##asm_op(new_val, old_val, value) \
  232. " strex %[str_res], %["#new_val"], [%["#ptr"]]\n" \
  233. " teq %[str_res], #0\n" \
  234. " bne.n 1b" \
  235. : \
  236. [old_val]"=&r" (old_val), \
  237. [new_val]"=&r" (new_val), \
  238. [str_res]"=&r" (str_res) \
  239. : \
  240. [ptr]"r" (ptr), \
  241. [value]"r" (value) \
  242. : "cc"); \
  243. UNUSED_PARAMETER(str_res); \
  244. }
  245. #define NRF_ATOMIC_OP_mov(new_val, old_val, value) "mov %["#new_val"], %["#value"]\n"
  246. #define NRF_ATOMIC_OP_orr(new_val, old_val, value) "orr %["#new_val"], %["#old_val"], %["#value"]\n"
  247. #define NRF_ATOMIC_OP_and(new_val, old_val, value) "and %["#new_val"], %["#old_val"], %["#value"]\n"
  248. #define NRF_ATOMIC_OP_eor(new_val, old_val, value) "eor %["#new_val"], %["#old_val"], %["#value"]\n"
  249. #define NRF_ATOMIC_OP_add(new_val, old_val, value) "add %["#new_val"], %["#old_val"], %["#value"]\n"
  250. #define NRF_ATOMIC_OP_sub(new_val, old_val, value) "sub %["#new_val"], %["#old_val"], %["#value"]\n"
  251. #define NRF_ATOMIC_OP_sub_hs(new_val, old_val, value) \
  252. "cmp %["#old_val"], %["#value"]\n " \
  253. "ite hs\n" \
  254. "subhs %["#new_val"], %["#old_val"], %["#value"]\n" \
  255. "movlo %["#new_val"], %["#old_val"]\n"
  256. static inline bool nrf_atomic_internal_cmp_exch(nrf_atomic_u32_t * p_data,
  257. uint32_t * p_expected,
  258. uint32_t value)
  259. {
  260. bool res = false;
  261. uint32_t str_res = 0;
  262. uint32_t act_val = 0;
  263. uint32_t exp_val = 0;
  264. UNUSED_VARIABLE(str_res);
  265. UNUSED_VARIABLE(act_val);
  266. UNUSED_VARIABLE(exp_val);
  267. __ASM volatile(
  268. "1: ldrex %[act_val], [%[ptr]]\n"
  269. " ldr %[exp_val], [%[expc]]\n"
  270. " cmp %[act_val], %[exp_val]\n"
  271. " ittee eq\n"
  272. " strexeq %[str_res], %[value], [%[ptr]]\n"
  273. " moveq %[res], #1\n"
  274. " strexne %[str_res], %[act_val], [%[ptr]]\n"
  275. " strne %[act_val], [%[expc]]\n"
  276. " cmp %[str_res], #0\n"
  277. " itt ne\n"
  278. " movne %[res], #0\n"
  279. " bne.n 1b"
  280. :
  281. [res] "=&r" (res),
  282. [exp_val] "=&r" (exp_val),
  283. [act_val] "=&r" (act_val),
  284. [str_res] "=&r" (str_res)
  285. :
  286. "0" (res),
  287. "1" (exp_val),
  288. "2" (act_val),
  289. [expc] "r" (p_expected),
  290. [ptr] "r" (p_data),
  291. [value] "r" (value)
  292. : "cc");
  293. return res;
  294. }
  295. #else
  296. #error "Unsupported compiler"
  297. #endif
  298. #ifdef __cplusplus
  299. }
  300. #endif
  301. #endif /* NRF_ATOMIC_INTERNAL_H__ */
  302. /** @} */