summaryrefslogtreecommitdiff
path: root/sysdeps/riscv/atomic-machine.h
blob: e375c6665e978393654040044f096ece2ce3e592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Low-level functions for atomic operations. RISC-V version.
   Copyright (C) 2014-2025 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library.  If not, see
   <https://www.gnu.org/licenses/>.  */

#ifndef _LINUX_RISCV_BITS_ATOMIC_H
#define _LINUX_RISCV_BITS_ATOMIC_H 1

#ifdef __riscv_atomic

#include_next <atomic-machine.h>

/* Miscellaneous.  */

# define asm_amo(which, ordering, mem, value) ({ 		\
  __atomic_check_size (mem);					\
  typeof (*mem) __tmp; 						\
  if (sizeof (__tmp) == 4)					\
    asm volatile (which ".w" ordering "\t%0, %z2, %1"		\
		  : "=r" (__tmp), "+A" (* (mem))		\
		  : "rJ" (value));				\
  else if (sizeof (__tmp) == 8)					\
    asm volatile (which ".d" ordering "\t%0, %z2, %1"		\
		  : "=r" (__tmp), "+A" (* (mem))		\
		  : "rJ" (value));				\
  else								\
    abort ();							\
  __tmp; })

# define atomic_max(mem, value) asm_amo ("amomaxu", ".aq", mem, value)
# define atomic_min(mem, value) asm_amo ("amominu", ".aq", mem, value)

#else /* __riscv_atomic */
# error "ISAs that do not subsume the A extension are not supported"
#endif /* !__riscv_atomic */

/* Execute a PAUSE hint when spinning.  */
#define atomic_spin_nop() __asm(".insn i 0x0f, 0, x0, x0, 0x010")

#endif /* bits/atomic.h */