summaryrefslogtreecommitdiff
path: root/core/riscv-rv32i/atomic.h
diff options
context:
space:
mode:
authorDawid Niedzwiecki <dn@semihalf.com>2020-10-02 12:31:27 +0200
committerCommit Bot <commit-bot@chromium.org>2020-10-13 09:14:06 +0000
commitf471709ee0bd4f02f62eac6efb25a9656ec70ea9 (patch)
treec701c6ac89917d2e0f13c948af3bcb8c437a29b8 /core/riscv-rv32i/atomic.h
parent346dfd8a764d83f81a23c460586ea02e6ce7177d (diff)
downloadchrome-ec-f471709ee0bd4f02f62eac6efb25a9656ec70ea9.tar.gz
core/riscv-rv32i: 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. Use GCC builtin functions instead of the custom asm code in order be sure there are no bugs and be consistent with other architectures. atomic_read_add/sub work the same as atomic_add/sub. atomic_read_add/sub should be replaced and deleted in the next step - use atomic_* instead of deprecated_atomic_*. BUG=b:169151160 BRANCH=none TEST=buildall Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com> Change-Id: I1f4f4ef26acb9e388da75125a4dd8e2f0dba9fe8 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2444995 Reviewed-by: Jett Rink <jettrink@chromium.org>
Diffstat (limited to 'core/riscv-rv32i/atomic.h')
-rw-r--r--core/riscv-rv32i/atomic.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/core/riscv-rv32i/atomic.h b/core/riscv-rv32i/atomic.h
index 7a7431b740..8dec97df48 100644
--- a/core/riscv-rv32i/atomic.h
+++ b/core/riscv-rv32i/atomic.h
@@ -12,6 +12,9 @@
#include "cpu.h"
#include "task.h"
+typedef int atomic_t;
+typedef atomic_t atomic_val_t;
+
#define ATOMIC_OP(op, value, addr) \
({ \
uint32_t tmp; \
@@ -34,38 +37,73 @@ static inline void deprecated_atomic_clear_bits(volatile uint32_t *addr,
ATOMIC_OP(and, ~bits, addr);
}
+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(volatile uint32_t *addr, uint32_t bits)
{
ATOMIC_OP(or, bits, addr);
}
+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(volatile uint32_t *addr,
uint32_t value)
{
ATOMIC_OP(add, value, addr);
}
+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(volatile uint32_t *addr,
uint32_t value)
{
ATOMIC_OP(add, -value, addr);
}
+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(volatile uint32_t *addr)
{
return ATOMIC_OP(and, 0, addr);
}
+static inline atomic_val_t atomic_read_clear(atomic_t *addr)
+{
+ return __atomic_exchange_n(addr, 0, __ATOMIC_SEQ_CST);
+}
+
static inline uint32_t deprecated_atomic_read_add(volatile uint32_t *addr,
uint32_t value)
{
return ATOMIC_OP(add, value, addr);
}
+static inline atomic_val_t atomic_read_add(atomic_t *addr, atomic_val_t value)
+{
+ return __atomic_fetch_add(addr, value, __ATOMIC_SEQ_CST);
+}
+
static inline uint32_t deprecated_atomic_read_sub(volatile uint32_t *addr,
uint32_t value)
{
return ATOMIC_OP(add, -value, addr);
}
+static inline atomic_val_t atomic_read_sub(atomic_t *addr, atomic_val_t value)
+{
+ return __atomic_fetch_sub(addr, value, __ATOMIC_SEQ_CST);
+}
+
#endif /* __CROS_EC_ATOMIC_H */