1*d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 24baa9922SRussell King /* 34baa9922SRussell King * arch/arm/include/asm/atomic.h 44baa9922SRussell King * 54baa9922SRussell King * Copyright (C) 1996 Russell King. 64baa9922SRussell King * Copyright (C) 2002 Deep Blue Solutions Ltd. 74baa9922SRussell King */ 84baa9922SRussell King #ifndef __ASM_ARM_ATOMIC_H 94baa9922SRussell King #define __ASM_ARM_ATOMIC_H 104baa9922SRussell King 114baa9922SRussell King #include <linux/compiler.h> 12f38d999cSWill Deacon #include <linux/prefetch.h> 13ea435467SMatthew Wilcox #include <linux/types.h> 149f97da78SDavid Howells #include <linux/irqflags.h> 159f97da78SDavid Howells #include <asm/barrier.h> 169f97da78SDavid Howells #include <asm/cmpxchg.h> 174baa9922SRussell King 184baa9922SRussell King #define ATOMIC_INIT(i) { (i) } 194baa9922SRussell King 204baa9922SRussell King #ifdef __KERNEL__ 214baa9922SRussell King 22200b812dSCatalin Marinas /* 23200b812dSCatalin Marinas * On ARM, ordinary assignment (str instruction) doesn't clear the local 24200b812dSCatalin Marinas * strex/ldrex monitor on some implementations. The reason we can use it for 25200b812dSCatalin Marinas * atomic_set() is the clrex or dummy strex done on every exception return. 26200b812dSCatalin Marinas */ 2762e8a325SPeter Zijlstra #define atomic_read(v) READ_ONCE((v)->counter) 2862e8a325SPeter Zijlstra #define atomic_set(v,i) WRITE_ONCE(((v)->counter), (i)) 294baa9922SRussell King 304baa9922SRussell King #if __LINUX_ARM_ARCH__ >= 6 314baa9922SRussell King 324baa9922SRussell King /* 334baa9922SRussell King * ARMv6 UP and SMP safe atomic ops. We use load exclusive and 344baa9922SRussell King * store exclusive to ensure that these are atomic. We may loop 35200b812dSCatalin Marinas * to ensure that the update happens. 364baa9922SRussell King */ 37bac4e960SRussell King 38aee9a554SPeter Zijlstra #define ATOMIC_OP(op, c_op, asm_op) \ 39aee9a554SPeter Zijlstra static inline void atomic_##op(int i, atomic_t *v) \ 40aee9a554SPeter Zijlstra { \ 41aee9a554SPeter Zijlstra unsigned long tmp; \ 42aee9a554SPeter Zijlstra int result; \ 43aee9a554SPeter Zijlstra \ 44aee9a554SPeter Zijlstra prefetchw(&v->counter); \ 45aee9a554SPeter Zijlstra __asm__ __volatile__("@ atomic_" #op "\n" \ 46aee9a554SPeter Zijlstra "1: ldrex %0, [%3]\n" \ 47aee9a554SPeter Zijlstra " " #asm_op " %0, %0, %4\n" \ 48aee9a554SPeter Zijlstra " strex %1, %0, [%3]\n" \ 49aee9a554SPeter Zijlstra " teq %1, #0\n" \ 50aee9a554SPeter Zijlstra " bne 1b" \ 51aee9a554SPeter Zijlstra : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ 52aee9a554SPeter Zijlstra : "r" (&v->counter), "Ir" (i) \ 53aee9a554SPeter Zijlstra : "cc"); \ 54aee9a554SPeter Zijlstra } \ 55bac4e960SRussell King 56aee9a554SPeter Zijlstra #define ATOMIC_OP_RETURN(op, c_op, asm_op) \ 570ca326deSWill Deacon static inline int atomic_##op##_return_relaxed(int i, atomic_t *v) \ 58aee9a554SPeter Zijlstra { \ 59aee9a554SPeter Zijlstra unsigned long tmp; \ 60aee9a554SPeter Zijlstra int result; \ 61aee9a554SPeter Zijlstra \ 62aee9a554SPeter Zijlstra prefetchw(&v->counter); \ 63aee9a554SPeter Zijlstra \ 64aee9a554SPeter Zijlstra __asm__ __volatile__("@ atomic_" #op "_return\n" \ 65aee9a554SPeter Zijlstra "1: ldrex %0, [%3]\n" \ 66aee9a554SPeter Zijlstra " " #asm_op " %0, %0, %4\n" \ 67aee9a554SPeter Zijlstra " strex %1, %0, [%3]\n" \ 68aee9a554SPeter Zijlstra " teq %1, #0\n" \ 69aee9a554SPeter Zijlstra " bne 1b" \ 70aee9a554SPeter Zijlstra : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ 71aee9a554SPeter Zijlstra : "r" (&v->counter), "Ir" (i) \ 72aee9a554SPeter Zijlstra : "cc"); \ 73aee9a554SPeter Zijlstra \ 74aee9a554SPeter Zijlstra return result; \ 754baa9922SRussell King } 764baa9922SRussell King 776da068c1SPeter Zijlstra #define ATOMIC_FETCH_OP(op, c_op, asm_op) \ 786da068c1SPeter Zijlstra static inline int atomic_fetch_##op##_relaxed(int i, atomic_t *v) \ 796da068c1SPeter Zijlstra { \ 806da068c1SPeter Zijlstra unsigned long tmp; \ 816da068c1SPeter Zijlstra int result, val; \ 826da068c1SPeter Zijlstra \ 836da068c1SPeter Zijlstra prefetchw(&v->counter); \ 846da068c1SPeter Zijlstra \ 856da068c1SPeter Zijlstra __asm__ __volatile__("@ atomic_fetch_" #op "\n" \ 866da068c1SPeter Zijlstra "1: ldrex %0, [%4]\n" \ 876da068c1SPeter Zijlstra " " #asm_op " %1, %0, %5\n" \ 886da068c1SPeter Zijlstra " strex %2, %1, [%4]\n" \ 896da068c1SPeter Zijlstra " teq %2, #0\n" \ 906da068c1SPeter Zijlstra " bne 1b" \ 916da068c1SPeter Zijlstra : "=&r" (result), "=&r" (val), "=&r" (tmp), "+Qo" (v->counter) \ 926da068c1SPeter Zijlstra : "r" (&v->counter), "Ir" (i) \ 936da068c1SPeter Zijlstra : "cc"); \ 946da068c1SPeter Zijlstra \ 956da068c1SPeter Zijlstra return result; \ 966da068c1SPeter Zijlstra } 976da068c1SPeter Zijlstra 980ca326deSWill Deacon #define atomic_add_return_relaxed atomic_add_return_relaxed 990ca326deSWill Deacon #define atomic_sub_return_relaxed atomic_sub_return_relaxed 1006da068c1SPeter Zijlstra #define atomic_fetch_add_relaxed atomic_fetch_add_relaxed 1016da068c1SPeter Zijlstra #define atomic_fetch_sub_relaxed atomic_fetch_sub_relaxed 1026da068c1SPeter Zijlstra 1036da068c1SPeter Zijlstra #define atomic_fetch_and_relaxed atomic_fetch_and_relaxed 1046da068c1SPeter Zijlstra #define atomic_fetch_andnot_relaxed atomic_fetch_andnot_relaxed 1056da068c1SPeter Zijlstra #define atomic_fetch_or_relaxed atomic_fetch_or_relaxed 1066da068c1SPeter Zijlstra #define atomic_fetch_xor_relaxed atomic_fetch_xor_relaxed 1070ca326deSWill Deacon 1080ca326deSWill Deacon static inline int atomic_cmpxchg_relaxed(atomic_t *ptr, int old, int new) 1094baa9922SRussell King { 1104dcc1cf7SChen Gang int oldval; 1114dcc1cf7SChen Gang unsigned long res; 1124baa9922SRussell King 113c32ffce0SWill Deacon prefetchw(&ptr->counter); 114bac4e960SRussell King 1154baa9922SRussell King do { 1164baa9922SRussell King __asm__ __volatile__("@ atomic_cmpxchg\n" 117398aa668SWill Deacon "ldrex %1, [%3]\n" 1184baa9922SRussell King "mov %0, #0\n" 119398aa668SWill Deacon "teq %1, %4\n" 120398aa668SWill Deacon "strexeq %0, %5, [%3]\n" 121398aa668SWill Deacon : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter) 1224baa9922SRussell King : "r" (&ptr->counter), "Ir" (old), "r" (new) 1234baa9922SRussell King : "cc"); 1244baa9922SRussell King } while (res); 1254baa9922SRussell King 1264baa9922SRussell King return oldval; 1274baa9922SRussell King } 1280ca326deSWill Deacon #define atomic_cmpxchg_relaxed atomic_cmpxchg_relaxed 1294baa9922SRussell King 130bfc18e38SMark Rutland static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u) 131db38ee87SWill Deacon { 132db38ee87SWill Deacon int oldval, newval; 133db38ee87SWill Deacon unsigned long tmp; 134db38ee87SWill Deacon 135db38ee87SWill Deacon smp_mb(); 136db38ee87SWill Deacon prefetchw(&v->counter); 137db38ee87SWill Deacon 138db38ee87SWill Deacon __asm__ __volatile__ ("@ atomic_add_unless\n" 139db38ee87SWill Deacon "1: ldrex %0, [%4]\n" 140db38ee87SWill Deacon " teq %0, %5\n" 141db38ee87SWill Deacon " beq 2f\n" 142db38ee87SWill Deacon " add %1, %0, %6\n" 143db38ee87SWill Deacon " strex %2, %1, [%4]\n" 144db38ee87SWill Deacon " teq %2, #0\n" 145db38ee87SWill Deacon " bne 1b\n" 146db38ee87SWill Deacon "2:" 147db38ee87SWill Deacon : "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter) 148db38ee87SWill Deacon : "r" (&v->counter), "r" (u), "r" (a) 149db38ee87SWill Deacon : "cc"); 150db38ee87SWill Deacon 151db38ee87SWill Deacon if (oldval != u) 152db38ee87SWill Deacon smp_mb(); 153db38ee87SWill Deacon 154db38ee87SWill Deacon return oldval; 155db38ee87SWill Deacon } 156eccc2da8SMark Rutland #define atomic_fetch_add_unless atomic_fetch_add_unless 157db38ee87SWill Deacon 1584baa9922SRussell King #else /* ARM_ARCH_6 */ 1594baa9922SRussell King 1604baa9922SRussell King #ifdef CONFIG_SMP 1614baa9922SRussell King #error SMP not supported on pre-ARMv6 CPUs 1624baa9922SRussell King #endif 1634baa9922SRussell King 164aee9a554SPeter Zijlstra #define ATOMIC_OP(op, c_op, asm_op) \ 165aee9a554SPeter Zijlstra static inline void atomic_##op(int i, atomic_t *v) \ 166aee9a554SPeter Zijlstra { \ 167aee9a554SPeter Zijlstra unsigned long flags; \ 168aee9a554SPeter Zijlstra \ 169aee9a554SPeter Zijlstra raw_local_irq_save(flags); \ 170aee9a554SPeter Zijlstra v->counter c_op i; \ 171aee9a554SPeter Zijlstra raw_local_irq_restore(flags); \ 172aee9a554SPeter Zijlstra } \ 1734baa9922SRussell King 174aee9a554SPeter Zijlstra #define ATOMIC_OP_RETURN(op, c_op, asm_op) \ 175aee9a554SPeter Zijlstra static inline int atomic_##op##_return(int i, atomic_t *v) \ 176aee9a554SPeter Zijlstra { \ 177aee9a554SPeter Zijlstra unsigned long flags; \ 178aee9a554SPeter Zijlstra int val; \ 179aee9a554SPeter Zijlstra \ 180aee9a554SPeter Zijlstra raw_local_irq_save(flags); \ 181aee9a554SPeter Zijlstra v->counter c_op i; \ 182aee9a554SPeter Zijlstra val = v->counter; \ 183aee9a554SPeter Zijlstra raw_local_irq_restore(flags); \ 184aee9a554SPeter Zijlstra \ 185aee9a554SPeter Zijlstra return val; \ 1864baa9922SRussell King } 1874baa9922SRussell King 1886da068c1SPeter Zijlstra #define ATOMIC_FETCH_OP(op, c_op, asm_op) \ 1896da068c1SPeter Zijlstra static inline int atomic_fetch_##op(int i, atomic_t *v) \ 1906da068c1SPeter Zijlstra { \ 1916da068c1SPeter Zijlstra unsigned long flags; \ 1926da068c1SPeter Zijlstra int val; \ 1936da068c1SPeter Zijlstra \ 1946da068c1SPeter Zijlstra raw_local_irq_save(flags); \ 1956da068c1SPeter Zijlstra val = v->counter; \ 1966da068c1SPeter Zijlstra v->counter c_op i; \ 1976da068c1SPeter Zijlstra raw_local_irq_restore(flags); \ 1986da068c1SPeter Zijlstra \ 1996da068c1SPeter Zijlstra return val; \ 2006da068c1SPeter Zijlstra } 2016da068c1SPeter Zijlstra 2024baa9922SRussell King static inline int atomic_cmpxchg(atomic_t *v, int old, int new) 2034baa9922SRussell King { 2044baa9922SRussell King int ret; 2054baa9922SRussell King unsigned long flags; 2064baa9922SRussell King 2074baa9922SRussell King raw_local_irq_save(flags); 2084baa9922SRussell King ret = v->counter; 2094baa9922SRussell King if (likely(ret == old)) 2104baa9922SRussell King v->counter = new; 2114baa9922SRussell King raw_local_irq_restore(flags); 2124baa9922SRussell King 2134baa9922SRussell King return ret; 2144baa9922SRussell King } 2154baa9922SRussell King 2167cc7eaadSMark Rutland #define atomic_fetch_andnot atomic_fetch_andnot 2177cc7eaadSMark Rutland 218db38ee87SWill Deacon #endif /* __LINUX_ARM_ARCH__ */ 219db38ee87SWill Deacon 220aee9a554SPeter Zijlstra #define ATOMIC_OPS(op, c_op, asm_op) \ 221aee9a554SPeter Zijlstra ATOMIC_OP(op, c_op, asm_op) \ 2226da068c1SPeter Zijlstra ATOMIC_OP_RETURN(op, c_op, asm_op) \ 2236da068c1SPeter Zijlstra ATOMIC_FETCH_OP(op, c_op, asm_op) 224aee9a554SPeter Zijlstra 225aee9a554SPeter Zijlstra ATOMIC_OPS(add, +=, add) 226aee9a554SPeter Zijlstra ATOMIC_OPS(sub, -=, sub) 227aee9a554SPeter Zijlstra 22812589790SPeter Zijlstra #define atomic_andnot atomic_andnot 22912589790SPeter Zijlstra 2306da068c1SPeter Zijlstra #undef ATOMIC_OPS 2316da068c1SPeter Zijlstra #define ATOMIC_OPS(op, c_op, asm_op) \ 2326da068c1SPeter Zijlstra ATOMIC_OP(op, c_op, asm_op) \ 2336da068c1SPeter Zijlstra ATOMIC_FETCH_OP(op, c_op, asm_op) 2346da068c1SPeter Zijlstra 2356da068c1SPeter Zijlstra ATOMIC_OPS(and, &=, and) 2366da068c1SPeter Zijlstra ATOMIC_OPS(andnot, &= ~, bic) 2376da068c1SPeter Zijlstra ATOMIC_OPS(or, |=, orr) 2386da068c1SPeter Zijlstra ATOMIC_OPS(xor, ^=, eor) 23912589790SPeter Zijlstra 240aee9a554SPeter Zijlstra #undef ATOMIC_OPS 2416da068c1SPeter Zijlstra #undef ATOMIC_FETCH_OP 242aee9a554SPeter Zijlstra #undef ATOMIC_OP_RETURN 243aee9a554SPeter Zijlstra #undef ATOMIC_OP 244aee9a554SPeter Zijlstra 245db38ee87SWill Deacon #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) 246db38ee87SWill Deacon 24724b44a66SWill Deacon #ifndef CONFIG_GENERIC_ATOMIC64 24824b44a66SWill Deacon typedef struct { 249237f1233SChen Gang long long counter; 25024b44a66SWill Deacon } atomic64_t; 25124b44a66SWill Deacon 25224b44a66SWill Deacon #define ATOMIC64_INIT(i) { (i) } 25324b44a66SWill Deacon 2544fd75911SWill Deacon #ifdef CONFIG_ARM_LPAE 255237f1233SChen Gang static inline long long atomic64_read(const atomic64_t *v) 2564fd75911SWill Deacon { 257237f1233SChen Gang long long result; 2584fd75911SWill Deacon 2594fd75911SWill Deacon __asm__ __volatile__("@ atomic64_read\n" 2604fd75911SWill Deacon " ldrd %0, %H0, [%1]" 2614fd75911SWill Deacon : "=&r" (result) 2624fd75911SWill Deacon : "r" (&v->counter), "Qo" (v->counter) 2634fd75911SWill Deacon ); 2644fd75911SWill Deacon 2654fd75911SWill Deacon return result; 2664fd75911SWill Deacon } 2674fd75911SWill Deacon 268237f1233SChen Gang static inline void atomic64_set(atomic64_t *v, long long i) 2694fd75911SWill Deacon { 2704fd75911SWill Deacon __asm__ __volatile__("@ atomic64_set\n" 2714fd75911SWill Deacon " strd %2, %H2, [%1]" 2724fd75911SWill Deacon : "=Qo" (v->counter) 2734fd75911SWill Deacon : "r" (&v->counter), "r" (i) 2744fd75911SWill Deacon ); 2754fd75911SWill Deacon } 2764fd75911SWill Deacon #else 277237f1233SChen Gang static inline long long atomic64_read(const atomic64_t *v) 27824b44a66SWill Deacon { 279237f1233SChen Gang long long result; 28024b44a66SWill Deacon 28124b44a66SWill Deacon __asm__ __volatile__("@ atomic64_read\n" 28224b44a66SWill Deacon " ldrexd %0, %H0, [%1]" 28324b44a66SWill Deacon : "=&r" (result) 284398aa668SWill Deacon : "r" (&v->counter), "Qo" (v->counter) 28524b44a66SWill Deacon ); 28624b44a66SWill Deacon 28724b44a66SWill Deacon return result; 28824b44a66SWill Deacon } 28924b44a66SWill Deacon 290237f1233SChen Gang static inline void atomic64_set(atomic64_t *v, long long i) 29124b44a66SWill Deacon { 292237f1233SChen Gang long long tmp; 29324b44a66SWill Deacon 294f38d999cSWill Deacon prefetchw(&v->counter); 29524b44a66SWill Deacon __asm__ __volatile__("@ atomic64_set\n" 296398aa668SWill Deacon "1: ldrexd %0, %H0, [%2]\n" 297398aa668SWill Deacon " strexd %0, %3, %H3, [%2]\n" 29824b44a66SWill Deacon " teq %0, #0\n" 29924b44a66SWill Deacon " bne 1b" 300398aa668SWill Deacon : "=&r" (tmp), "=Qo" (v->counter) 30124b44a66SWill Deacon : "r" (&v->counter), "r" (i) 30224b44a66SWill Deacon : "cc"); 30324b44a66SWill Deacon } 3044fd75911SWill Deacon #endif 30524b44a66SWill Deacon 306aee9a554SPeter Zijlstra #define ATOMIC64_OP(op, op1, op2) \ 307aee9a554SPeter Zijlstra static inline void atomic64_##op(long long i, atomic64_t *v) \ 308aee9a554SPeter Zijlstra { \ 309aee9a554SPeter Zijlstra long long result; \ 310aee9a554SPeter Zijlstra unsigned long tmp; \ 311aee9a554SPeter Zijlstra \ 312aee9a554SPeter Zijlstra prefetchw(&v->counter); \ 313aee9a554SPeter Zijlstra __asm__ __volatile__("@ atomic64_" #op "\n" \ 314aee9a554SPeter Zijlstra "1: ldrexd %0, %H0, [%3]\n" \ 315aee9a554SPeter Zijlstra " " #op1 " %Q0, %Q0, %Q4\n" \ 316aee9a554SPeter Zijlstra " " #op2 " %R0, %R0, %R4\n" \ 317aee9a554SPeter Zijlstra " strexd %1, %0, %H0, [%3]\n" \ 318aee9a554SPeter Zijlstra " teq %1, #0\n" \ 319aee9a554SPeter Zijlstra " bne 1b" \ 320aee9a554SPeter Zijlstra : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ 321aee9a554SPeter Zijlstra : "r" (&v->counter), "r" (i) \ 322aee9a554SPeter Zijlstra : "cc"); \ 323aee9a554SPeter Zijlstra } \ 32424b44a66SWill Deacon 325aee9a554SPeter Zijlstra #define ATOMIC64_OP_RETURN(op, op1, op2) \ 3260ca326deSWill Deacon static inline long long \ 3270ca326deSWill Deacon atomic64_##op##_return_relaxed(long long i, atomic64_t *v) \ 328aee9a554SPeter Zijlstra { \ 329aee9a554SPeter Zijlstra long long result; \ 330aee9a554SPeter Zijlstra unsigned long tmp; \ 331aee9a554SPeter Zijlstra \ 332aee9a554SPeter Zijlstra prefetchw(&v->counter); \ 333aee9a554SPeter Zijlstra \ 334aee9a554SPeter Zijlstra __asm__ __volatile__("@ atomic64_" #op "_return\n" \ 335aee9a554SPeter Zijlstra "1: ldrexd %0, %H0, [%3]\n" \ 336aee9a554SPeter Zijlstra " " #op1 " %Q0, %Q0, %Q4\n" \ 337aee9a554SPeter Zijlstra " " #op2 " %R0, %R0, %R4\n" \ 338aee9a554SPeter Zijlstra " strexd %1, %0, %H0, [%3]\n" \ 339aee9a554SPeter Zijlstra " teq %1, #0\n" \ 340aee9a554SPeter Zijlstra " bne 1b" \ 341aee9a554SPeter Zijlstra : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \ 342aee9a554SPeter Zijlstra : "r" (&v->counter), "r" (i) \ 343aee9a554SPeter Zijlstra : "cc"); \ 344aee9a554SPeter Zijlstra \ 345aee9a554SPeter Zijlstra return result; \ 34624b44a66SWill Deacon } 34724b44a66SWill Deacon 3486da068c1SPeter Zijlstra #define ATOMIC64_FETCH_OP(op, op1, op2) \ 3496da068c1SPeter Zijlstra static inline long long \ 3506da068c1SPeter Zijlstra atomic64_fetch_##op##_relaxed(long long i, atomic64_t *v) \ 3516da068c1SPeter Zijlstra { \ 3526da068c1SPeter Zijlstra long long result, val; \ 3536da068c1SPeter Zijlstra unsigned long tmp; \ 3546da068c1SPeter Zijlstra \ 3556da068c1SPeter Zijlstra prefetchw(&v->counter); \ 3566da068c1SPeter Zijlstra \ 3576da068c1SPeter Zijlstra __asm__ __volatile__("@ atomic64_fetch_" #op "\n" \ 3586da068c1SPeter Zijlstra "1: ldrexd %0, %H0, [%4]\n" \ 3596da068c1SPeter Zijlstra " " #op1 " %Q1, %Q0, %Q5\n" \ 3606da068c1SPeter Zijlstra " " #op2 " %R1, %R0, %R5\n" \ 3616da068c1SPeter Zijlstra " strexd %2, %1, %H1, [%4]\n" \ 3626da068c1SPeter Zijlstra " teq %2, #0\n" \ 3636da068c1SPeter Zijlstra " bne 1b" \ 3646da068c1SPeter Zijlstra : "=&r" (result), "=&r" (val), "=&r" (tmp), "+Qo" (v->counter) \ 3656da068c1SPeter Zijlstra : "r" (&v->counter), "r" (i) \ 3666da068c1SPeter Zijlstra : "cc"); \ 3676da068c1SPeter Zijlstra \ 3686da068c1SPeter Zijlstra return result; \ 3696da068c1SPeter Zijlstra } 3706da068c1SPeter Zijlstra 371aee9a554SPeter Zijlstra #define ATOMIC64_OPS(op, op1, op2) \ 372aee9a554SPeter Zijlstra ATOMIC64_OP(op, op1, op2) \ 3736da068c1SPeter Zijlstra ATOMIC64_OP_RETURN(op, op1, op2) \ 3746da068c1SPeter Zijlstra ATOMIC64_FETCH_OP(op, op1, op2) 37524b44a66SWill Deacon 376aee9a554SPeter Zijlstra ATOMIC64_OPS(add, adds, adc) 377aee9a554SPeter Zijlstra ATOMIC64_OPS(sub, subs, sbc) 37824b44a66SWill Deacon 3790ca326deSWill Deacon #define atomic64_add_return_relaxed atomic64_add_return_relaxed 3800ca326deSWill Deacon #define atomic64_sub_return_relaxed atomic64_sub_return_relaxed 3816da068c1SPeter Zijlstra #define atomic64_fetch_add_relaxed atomic64_fetch_add_relaxed 3826da068c1SPeter Zijlstra #define atomic64_fetch_sub_relaxed atomic64_fetch_sub_relaxed 3836da068c1SPeter Zijlstra 3846da068c1SPeter Zijlstra #undef ATOMIC64_OPS 3856da068c1SPeter Zijlstra #define ATOMIC64_OPS(op, op1, op2) \ 3866da068c1SPeter Zijlstra ATOMIC64_OP(op, op1, op2) \ 3876da068c1SPeter Zijlstra ATOMIC64_FETCH_OP(op, op1, op2) 3880ca326deSWill Deacon 38912589790SPeter Zijlstra #define atomic64_andnot atomic64_andnot 39012589790SPeter Zijlstra 3916da068c1SPeter Zijlstra ATOMIC64_OPS(and, and, and) 3926da068c1SPeter Zijlstra ATOMIC64_OPS(andnot, bic, bic) 3936da068c1SPeter Zijlstra ATOMIC64_OPS(or, orr, orr) 3946da068c1SPeter Zijlstra ATOMIC64_OPS(xor, eor, eor) 3956da068c1SPeter Zijlstra 3966da068c1SPeter Zijlstra #define atomic64_fetch_and_relaxed atomic64_fetch_and_relaxed 3976da068c1SPeter Zijlstra #define atomic64_fetch_andnot_relaxed atomic64_fetch_andnot_relaxed 3986da068c1SPeter Zijlstra #define atomic64_fetch_or_relaxed atomic64_fetch_or_relaxed 3996da068c1SPeter Zijlstra #define atomic64_fetch_xor_relaxed atomic64_fetch_xor_relaxed 40012589790SPeter Zijlstra 401aee9a554SPeter Zijlstra #undef ATOMIC64_OPS 4026da068c1SPeter Zijlstra #undef ATOMIC64_FETCH_OP 403aee9a554SPeter Zijlstra #undef ATOMIC64_OP_RETURN 404aee9a554SPeter Zijlstra #undef ATOMIC64_OP 40524b44a66SWill Deacon 4060ca326deSWill Deacon static inline long long 4070ca326deSWill Deacon atomic64_cmpxchg_relaxed(atomic64_t *ptr, long long old, long long new) 40824b44a66SWill Deacon { 409237f1233SChen Gang long long oldval; 41024b44a66SWill Deacon unsigned long res; 41124b44a66SWill Deacon 412c32ffce0SWill Deacon prefetchw(&ptr->counter); 41324b44a66SWill Deacon 41424b44a66SWill Deacon do { 41524b44a66SWill Deacon __asm__ __volatile__("@ atomic64_cmpxchg\n" 416398aa668SWill Deacon "ldrexd %1, %H1, [%3]\n" 41724b44a66SWill Deacon "mov %0, #0\n" 418398aa668SWill Deacon "teq %1, %4\n" 419398aa668SWill Deacon "teqeq %H1, %H4\n" 420398aa668SWill Deacon "strexdeq %0, %5, %H5, [%3]" 421398aa668SWill Deacon : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter) 42224b44a66SWill Deacon : "r" (&ptr->counter), "r" (old), "r" (new) 42324b44a66SWill Deacon : "cc"); 42424b44a66SWill Deacon } while (res); 42524b44a66SWill Deacon 42624b44a66SWill Deacon return oldval; 42724b44a66SWill Deacon } 4280ca326deSWill Deacon #define atomic64_cmpxchg_relaxed atomic64_cmpxchg_relaxed 42924b44a66SWill Deacon 4300ca326deSWill Deacon static inline long long atomic64_xchg_relaxed(atomic64_t *ptr, long long new) 43124b44a66SWill Deacon { 432237f1233SChen Gang long long result; 43324b44a66SWill Deacon unsigned long tmp; 43424b44a66SWill Deacon 435c32ffce0SWill Deacon prefetchw(&ptr->counter); 43624b44a66SWill Deacon 43724b44a66SWill Deacon __asm__ __volatile__("@ atomic64_xchg\n" 438398aa668SWill Deacon "1: ldrexd %0, %H0, [%3]\n" 439398aa668SWill Deacon " strexd %1, %4, %H4, [%3]\n" 44024b44a66SWill Deacon " teq %1, #0\n" 44124b44a66SWill Deacon " bne 1b" 442398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter) 44324b44a66SWill Deacon : "r" (&ptr->counter), "r" (new) 44424b44a66SWill Deacon : "cc"); 44524b44a66SWill Deacon 44624b44a66SWill Deacon return result; 44724b44a66SWill Deacon } 4480ca326deSWill Deacon #define atomic64_xchg_relaxed atomic64_xchg_relaxed 44924b44a66SWill Deacon 450237f1233SChen Gang static inline long long atomic64_dec_if_positive(atomic64_t *v) 45124b44a66SWill Deacon { 452237f1233SChen Gang long long result; 45324b44a66SWill Deacon unsigned long tmp; 45424b44a66SWill Deacon 45524b44a66SWill Deacon smp_mb(); 456c32ffce0SWill Deacon prefetchw(&v->counter); 45724b44a66SWill Deacon 45824b44a66SWill Deacon __asm__ __volatile__("@ atomic64_dec_if_positive\n" 459398aa668SWill Deacon "1: ldrexd %0, %H0, [%3]\n" 4602245f924SVictor Kamensky " subs %Q0, %Q0, #1\n" 4612245f924SVictor Kamensky " sbc %R0, %R0, #0\n" 4622245f924SVictor Kamensky " teq %R0, #0\n" 46324b44a66SWill Deacon " bmi 2f\n" 464398aa668SWill Deacon " strexd %1, %0, %H0, [%3]\n" 46524b44a66SWill Deacon " teq %1, #0\n" 46624b44a66SWill Deacon " bne 1b\n" 46724b44a66SWill Deacon "2:" 468398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 46924b44a66SWill Deacon : "r" (&v->counter) 47024b44a66SWill Deacon : "cc"); 47124b44a66SWill Deacon 47224b44a66SWill Deacon smp_mb(); 47324b44a66SWill Deacon 47424b44a66SWill Deacon return result; 47524b44a66SWill Deacon } 476b3a2a05fSMark Rutland #define atomic64_dec_if_positive atomic64_dec_if_positive 47724b44a66SWill Deacon 478fee8ca9fSMark Rutland static inline long long atomic64_fetch_add_unless(atomic64_t *v, long long a, 479fee8ca9fSMark Rutland long long u) 48024b44a66SWill Deacon { 481fee8ca9fSMark Rutland long long oldval, newval; 48224b44a66SWill Deacon unsigned long tmp; 48324b44a66SWill Deacon 48424b44a66SWill Deacon smp_mb(); 485c32ffce0SWill Deacon prefetchw(&v->counter); 48624b44a66SWill Deacon 48724b44a66SWill Deacon __asm__ __volatile__("@ atomic64_add_unless\n" 488398aa668SWill Deacon "1: ldrexd %0, %H0, [%4]\n" 489398aa668SWill Deacon " teq %0, %5\n" 490398aa668SWill Deacon " teqeq %H0, %H5\n" 49124b44a66SWill Deacon " beq 2f\n" 492fee8ca9fSMark Rutland " adds %Q1, %Q0, %Q6\n" 493fee8ca9fSMark Rutland " adc %R1, %R0, %R6\n" 494fee8ca9fSMark Rutland " strexd %2, %1, %H1, [%4]\n" 49524b44a66SWill Deacon " teq %2, #0\n" 49624b44a66SWill Deacon " bne 1b\n" 49724b44a66SWill Deacon "2:" 498fee8ca9fSMark Rutland : "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter) 49924b44a66SWill Deacon : "r" (&v->counter), "r" (u), "r" (a) 50024b44a66SWill Deacon : "cc"); 50124b44a66SWill Deacon 502fee8ca9fSMark Rutland if (oldval != u) 50324b44a66SWill Deacon smp_mb(); 50424b44a66SWill Deacon 505fee8ca9fSMark Rutland return oldval; 50624b44a66SWill Deacon } 507fee8ca9fSMark Rutland #define atomic64_fetch_add_unless atomic64_fetch_add_unless 50824b44a66SWill Deacon 5097847777aSArun Sharma #endif /* !CONFIG_GENERIC_ATOMIC64 */ 5104baa9922SRussell King #endif 5114baa9922SRussell King #endif 512