From a54cd39627e62e60af73e2d3ef51504f43d1fb0e Mon Sep 17 00:00:00 2001 From: Dawid Niedzwiecki Date: Thu, 24 Sep 2020 12:09:18 +0200 Subject: core/cortex-m: add Zephyr compatible atomic functions Add atomic functions with prototypes equal to the ones in Zephyr. It is done as a part of porting to Zephyr, the next step is to use in the code atomic_* instead of deprecated_atomic_*. Some atomic functions in Zephyr return a value e.g. atomic_add - it returns the value of the variable before the add operation. The current state of ATOMIC_OP macro is not designed to return such value so instead of reworking it or writing new custom asm code just use builtin functions. BUG=b:169151160 BRANCH=none TEST=buildall Signed-off-by: Dawid Niedzwiecki Change-Id: Iccd7a4d674601271f11f88834c8b2db08c537534 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2428945 Reviewed-by: Jett Rink --- core/cortex-m/atomic.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'core') diff --git a/core/cortex-m/atomic.h b/core/cortex-m/atomic.h index 736d5887b4..1f432e8d1e 100644 --- a/core/cortex-m/atomic.h +++ b/core/cortex-m/atomic.h @@ -10,6 +10,9 @@ #include "common.h" +typedef int atomic_t; +typedef atomic_t atomic_val_t; + /** * Implements atomic arithmetic operations on 32-bit integers. * @@ -42,23 +45,43 @@ static inline void deprecated_atomic_clear_bits(uint32_t volatile *addr, ATOMIC_OP(bic, addr, bits); } +static inline void atomic_clear_bits(atomic_t *addr, atomic_val_t bits) +{ + __atomic_fetch_and(addr, ~bits, __ATOMIC_SEQ_CST); +} + static inline void deprecated_atomic_or(uint32_t volatile *addr, uint32_t bits) { ATOMIC_OP(orr, addr, bits); } +static inline atomic_val_t atomic_or(atomic_t *addr, atomic_val_t bits) +{ + return __atomic_fetch_or(addr, bits, __ATOMIC_SEQ_CST); +} + static inline void deprecated_atomic_add(uint32_t volatile *addr, uint32_t value) { ATOMIC_OP(add, addr, value); } +static inline atomic_val_t atomic_add(atomic_t *addr, atomic_val_t value) +{ + return __atomic_fetch_add(addr, value, __ATOMIC_SEQ_CST); +} + static inline void deprecated_atomic_sub(uint32_t volatile *addr, uint32_t value) { ATOMIC_OP(sub, addr, value); } +static inline atomic_val_t atomic_sub(atomic_t *addr, atomic_val_t value) +{ + return __atomic_fetch_sub(addr, value, __ATOMIC_SEQ_CST); +} + static inline uint32_t deprecated_atomic_read_clear(uint32_t volatile *addr) { uint32_t ret, tmp; @@ -73,4 +96,10 @@ static inline uint32_t deprecated_atomic_read_clear(uint32_t volatile *addr) return ret; } + +static inline atomic_val_t atomic_read_clear(atomic_t *addr) +{ + return __atomic_exchange_n(addr, 0, __ATOMIC_SEQ_CST); +} + #endif /* __CROS_EC_ATOMIC_H */ -- cgit v1.2.1