Path: blob/master/arch/loongarch/include/asm/atomic-llsc.h
38237 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* Atomic operations (LLSC).3*4* Copyright (C) 2024-2025 Loongson Technology Corporation Limited5*/67#ifndef _ASM_ATOMIC_LLSC_H8#define _ASM_ATOMIC_LLSC_H910#include <linux/types.h>11#include <asm/barrier.h>12#include <asm/cmpxchg.h>1314#define ATOMIC_OP(op, I, asm_op) \15static inline void arch_atomic_##op(int i, atomic_t *v) \16{ \17int temp; \18\19__asm__ __volatile__( \20"1: ll.w %0, %1 #atomic_" #op " \n" \21" " #asm_op " %0, %0, %2 \n" \22" sc.w %0, %1 \n" \23" beq %0, $r0, 1b \n" \24:"=&r" (temp) , "+ZC"(v->counter) \25:"r" (I) \26); \27}2829#define ATOMIC_OP_RETURN(op, I, asm_op) \30static inline int arch_atomic_##op##_return_relaxed(int i, atomic_t *v) \31{ \32int result, temp; \33\34__asm__ __volatile__( \35"1: ll.w %1, %2 # atomic_" #op "_return \n" \36" " #asm_op " %0, %1, %3 \n" \37" sc.w %0, %2 \n" \38" beq %0, $r0 ,1b \n" \39" " #asm_op " %0, %1, %3 \n" \40: "=&r" (result), "=&r" (temp), "+ZC"(v->counter) \41: "r" (I)); \42\43return result; \44}4546#define ATOMIC_FETCH_OP(op, I, asm_op) \47static inline int arch_atomic_fetch_##op##_relaxed(int i, atomic_t *v) \48{ \49int result, temp; \50\51__asm__ __volatile__( \52"1: ll.w %1, %2 # atomic_fetch_" #op " \n" \53" " #asm_op " %0, %1, %3 \n" \54" sc.w %0, %2 \n" \55" beq %0, $r0 ,1b \n" \56" add.w %0, %1 ,$r0 \n" \57: "=&r" (result), "=&r" (temp), "+ZC" (v->counter) \58: "r" (I)); \59\60return result; \61}6263#define ATOMIC_OPS(op,I ,asm_op, c_op) \64ATOMIC_OP(op, I, asm_op) \65ATOMIC_OP_RETURN(op, I , asm_op) \66ATOMIC_FETCH_OP(op, I, asm_op)6768ATOMIC_OPS(add, i , add.w ,+=)69ATOMIC_OPS(sub, -i , add.w ,+=)7071#define arch_atomic_add_return_relaxed arch_atomic_add_return_relaxed72#define arch_atomic_sub_return_relaxed arch_atomic_sub_return_relaxed73#define arch_atomic_fetch_add_relaxed arch_atomic_fetch_add_relaxed74#define arch_atomic_fetch_sub_relaxed arch_atomic_fetch_sub_relaxed7576#undef ATOMIC_OPS7778#define ATOMIC_OPS(op, I, asm_op) \79ATOMIC_OP(op, I, asm_op) \80ATOMIC_FETCH_OP(op, I, asm_op)8182ATOMIC_OPS(and, i, and)83ATOMIC_OPS(or, i, or)84ATOMIC_OPS(xor, i, xor)8586#define arch_atomic_fetch_and_relaxed arch_atomic_fetch_and_relaxed87#define arch_atomic_fetch_or_relaxed arch_atomic_fetch_or_relaxed88#define arch_atomic_fetch_xor_relaxed arch_atomic_fetch_xor_relaxed8990#undef ATOMIC_OPS91#undef ATOMIC_FETCH_OP92#undef ATOMIC_OP_RETURN93#undef ATOMIC_OP9495#ifdef CONFIG_64BIT96#error "64-bit LLSC atomic operations are not supported"97#endif9899#endif /* _ASM_ATOMIC_LLSC_H */100101102