14baa9922SRussell King /* 24baa9922SRussell King * arch/arm/include/asm/atomic.h 34baa9922SRussell King * 44baa9922SRussell King * Copyright (C) 1996 Russell King. 54baa9922SRussell King * Copyright (C) 2002 Deep Blue Solutions Ltd. 64baa9922SRussell King * 74baa9922SRussell King * This program is free software; you can redistribute it and/or modify 84baa9922SRussell King * it under the terms of the GNU General Public License version 2 as 94baa9922SRussell King * published by the Free Software Foundation. 104baa9922SRussell King */ 114baa9922SRussell King #ifndef __ASM_ARM_ATOMIC_H 124baa9922SRussell King #define __ASM_ARM_ATOMIC_H 134baa9922SRussell King 144baa9922SRussell King #include <linux/compiler.h> 15f38d999cSWill Deacon #include <linux/prefetch.h> 16ea435467SMatthew Wilcox #include <linux/types.h> 179f97da78SDavid Howells #include <linux/irqflags.h> 189f97da78SDavid Howells #include <asm/barrier.h> 199f97da78SDavid Howells #include <asm/cmpxchg.h> 204baa9922SRussell King 214baa9922SRussell King #define ATOMIC_INIT(i) { (i) } 224baa9922SRussell King 234baa9922SRussell King #ifdef __KERNEL__ 244baa9922SRussell King 25200b812dSCatalin Marinas /* 26200b812dSCatalin Marinas * On ARM, ordinary assignment (str instruction) doesn't clear the local 27200b812dSCatalin Marinas * strex/ldrex monitor on some implementations. The reason we can use it for 28200b812dSCatalin Marinas * atomic_set() is the clrex or dummy strex done on every exception return. 29200b812dSCatalin Marinas */ 30f3d46f9dSAnton Blanchard #define atomic_read(v) (*(volatile int *)&(v)->counter) 31200b812dSCatalin Marinas #define atomic_set(v,i) (((v)->counter) = (i)) 324baa9922SRussell King 334baa9922SRussell King #if __LINUX_ARM_ARCH__ >= 6 344baa9922SRussell King 354baa9922SRussell King /* 364baa9922SRussell King * ARMv6 UP and SMP safe atomic ops. We use load exclusive and 374baa9922SRussell King * store exclusive to ensure that these are atomic. We may loop 38200b812dSCatalin Marinas * to ensure that the update happens. 394baa9922SRussell King */ 40bac4e960SRussell King static inline void atomic_add(int i, atomic_t *v) 41bac4e960SRussell King { 42bac4e960SRussell King unsigned long tmp; 43bac4e960SRussell King int result; 44bac4e960SRussell King 45f38d999cSWill Deacon prefetchw(&v->counter); 46bac4e960SRussell King __asm__ __volatile__("@ atomic_add\n" 47398aa668SWill Deacon "1: ldrex %0, [%3]\n" 48398aa668SWill Deacon " add %0, %0, %4\n" 49398aa668SWill Deacon " strex %1, %0, [%3]\n" 50bac4e960SRussell King " teq %1, #0\n" 51bac4e960SRussell King " bne 1b" 52398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 53bac4e960SRussell King : "r" (&v->counter), "Ir" (i) 54bac4e960SRussell King : "cc"); 55bac4e960SRussell King } 56bac4e960SRussell King 574baa9922SRussell King static inline int atomic_add_return(int i, atomic_t *v) 584baa9922SRussell King { 594baa9922SRussell King unsigned long tmp; 604baa9922SRussell King int result; 614baa9922SRussell King 62bac4e960SRussell King smp_mb(); 63c32ffce0SWill Deacon prefetchw(&v->counter); 64bac4e960SRussell King 654baa9922SRussell King __asm__ __volatile__("@ atomic_add_return\n" 66398aa668SWill Deacon "1: ldrex %0, [%3]\n" 67398aa668SWill Deacon " add %0, %0, %4\n" 68398aa668SWill Deacon " strex %1, %0, [%3]\n" 694baa9922SRussell King " teq %1, #0\n" 704baa9922SRussell King " bne 1b" 71398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 724baa9922SRussell King : "r" (&v->counter), "Ir" (i) 734baa9922SRussell King : "cc"); 744baa9922SRussell King 75bac4e960SRussell King smp_mb(); 76bac4e960SRussell King 774baa9922SRussell King return result; 784baa9922SRussell King } 794baa9922SRussell King 80bac4e960SRussell King static inline void atomic_sub(int i, atomic_t *v) 81bac4e960SRussell King { 82bac4e960SRussell King unsigned long tmp; 83bac4e960SRussell King int result; 84bac4e960SRussell King 85f38d999cSWill Deacon prefetchw(&v->counter); 86bac4e960SRussell King __asm__ __volatile__("@ atomic_sub\n" 87398aa668SWill Deacon "1: ldrex %0, [%3]\n" 88398aa668SWill Deacon " sub %0, %0, %4\n" 89398aa668SWill Deacon " strex %1, %0, [%3]\n" 90bac4e960SRussell King " teq %1, #0\n" 91bac4e960SRussell King " bne 1b" 92398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 93bac4e960SRussell King : "r" (&v->counter), "Ir" (i) 94bac4e960SRussell King : "cc"); 95bac4e960SRussell King } 96bac4e960SRussell King 974baa9922SRussell King static inline int atomic_sub_return(int i, atomic_t *v) 984baa9922SRussell King { 994baa9922SRussell King unsigned long tmp; 1004baa9922SRussell King int result; 1014baa9922SRussell King 102bac4e960SRussell King smp_mb(); 103c32ffce0SWill Deacon prefetchw(&v->counter); 104bac4e960SRussell King 1054baa9922SRussell King __asm__ __volatile__("@ atomic_sub_return\n" 106398aa668SWill Deacon "1: ldrex %0, [%3]\n" 107398aa668SWill Deacon " sub %0, %0, %4\n" 108398aa668SWill Deacon " strex %1, %0, [%3]\n" 1094baa9922SRussell King " teq %1, #0\n" 1104baa9922SRussell King " bne 1b" 111398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 1124baa9922SRussell King : "r" (&v->counter), "Ir" (i) 1134baa9922SRussell King : "cc"); 1144baa9922SRussell King 115bac4e960SRussell King smp_mb(); 116bac4e960SRussell King 1174baa9922SRussell King return result; 1184baa9922SRussell King } 1194baa9922SRussell King 1204baa9922SRussell King static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new) 1214baa9922SRussell King { 1224dcc1cf7SChen Gang int oldval; 1234dcc1cf7SChen Gang unsigned long res; 1244baa9922SRussell King 125bac4e960SRussell King smp_mb(); 126c32ffce0SWill Deacon prefetchw(&ptr->counter); 127bac4e960SRussell King 1284baa9922SRussell King do { 1294baa9922SRussell King __asm__ __volatile__("@ atomic_cmpxchg\n" 130398aa668SWill Deacon "ldrex %1, [%3]\n" 1314baa9922SRussell King "mov %0, #0\n" 132398aa668SWill Deacon "teq %1, %4\n" 133398aa668SWill Deacon "strexeq %0, %5, [%3]\n" 134398aa668SWill Deacon : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter) 1354baa9922SRussell King : "r" (&ptr->counter), "Ir" (old), "r" (new) 1364baa9922SRussell King : "cc"); 1374baa9922SRussell King } while (res); 1384baa9922SRussell King 139bac4e960SRussell King smp_mb(); 140bac4e960SRussell King 1414baa9922SRussell King return oldval; 1424baa9922SRussell King } 1434baa9922SRussell King 144*db38ee87SWill Deacon static inline int __atomic_add_unless(atomic_t *v, int a, int u) 145*db38ee87SWill Deacon { 146*db38ee87SWill Deacon int oldval, newval; 147*db38ee87SWill Deacon unsigned long tmp; 148*db38ee87SWill Deacon 149*db38ee87SWill Deacon smp_mb(); 150*db38ee87SWill Deacon prefetchw(&v->counter); 151*db38ee87SWill Deacon 152*db38ee87SWill Deacon __asm__ __volatile__ ("@ atomic_add_unless\n" 153*db38ee87SWill Deacon "1: ldrex %0, [%4]\n" 154*db38ee87SWill Deacon " teq %0, %5\n" 155*db38ee87SWill Deacon " beq 2f\n" 156*db38ee87SWill Deacon " add %1, %0, %6\n" 157*db38ee87SWill Deacon " strex %2, %1, [%4]\n" 158*db38ee87SWill Deacon " teq %2, #0\n" 159*db38ee87SWill Deacon " bne 1b\n" 160*db38ee87SWill Deacon "2:" 161*db38ee87SWill Deacon : "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter) 162*db38ee87SWill Deacon : "r" (&v->counter), "r" (u), "r" (a) 163*db38ee87SWill Deacon : "cc"); 164*db38ee87SWill Deacon 165*db38ee87SWill Deacon if (oldval != u) 166*db38ee87SWill Deacon smp_mb(); 167*db38ee87SWill Deacon 168*db38ee87SWill Deacon return oldval; 169*db38ee87SWill Deacon } 170*db38ee87SWill Deacon 1714baa9922SRussell King #else /* ARM_ARCH_6 */ 1724baa9922SRussell King 1734baa9922SRussell King #ifdef CONFIG_SMP 1744baa9922SRussell King #error SMP not supported on pre-ARMv6 CPUs 1754baa9922SRussell King #endif 1764baa9922SRussell King 1774baa9922SRussell King static inline int atomic_add_return(int i, atomic_t *v) 1784baa9922SRussell King { 1794baa9922SRussell King unsigned long flags; 1804baa9922SRussell King int val; 1814baa9922SRussell King 1824baa9922SRussell King raw_local_irq_save(flags); 1834baa9922SRussell King val = v->counter; 1844baa9922SRussell King v->counter = val += i; 1854baa9922SRussell King raw_local_irq_restore(flags); 1864baa9922SRussell King 1874baa9922SRussell King return val; 1884baa9922SRussell King } 189bac4e960SRussell King #define atomic_add(i, v) (void) atomic_add_return(i, v) 1904baa9922SRussell King 1914baa9922SRussell King static inline int atomic_sub_return(int i, atomic_t *v) 1924baa9922SRussell King { 1934baa9922SRussell King unsigned long flags; 1944baa9922SRussell King int val; 1954baa9922SRussell King 1964baa9922SRussell King raw_local_irq_save(flags); 1974baa9922SRussell King val = v->counter; 1984baa9922SRussell King v->counter = val -= i; 1994baa9922SRussell King raw_local_irq_restore(flags); 2004baa9922SRussell King 2014baa9922SRussell King return val; 2024baa9922SRussell King } 203bac4e960SRussell King #define atomic_sub(i, v) (void) atomic_sub_return(i, v) 2044baa9922SRussell King 2054baa9922SRussell King static inline int atomic_cmpxchg(atomic_t *v, int old, int new) 2064baa9922SRussell King { 2074baa9922SRussell King int ret; 2084baa9922SRussell King unsigned long flags; 2094baa9922SRussell King 2104baa9922SRussell King raw_local_irq_save(flags); 2114baa9922SRussell King ret = v->counter; 2124baa9922SRussell King if (likely(ret == old)) 2134baa9922SRussell King v->counter = new; 2144baa9922SRussell King raw_local_irq_restore(flags); 2154baa9922SRussell King 2164baa9922SRussell King return ret; 2174baa9922SRussell King } 2184baa9922SRussell King 219f24219b4SArun Sharma static inline int __atomic_add_unless(atomic_t *v, int a, int u) 2204baa9922SRussell King { 2214baa9922SRussell King int c, old; 2224baa9922SRussell King 2234baa9922SRussell King c = atomic_read(v); 2244baa9922SRussell King while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c) 2254baa9922SRussell King c = old; 226f24219b4SArun Sharma return c; 2274baa9922SRussell King } 2284baa9922SRussell King 229*db38ee87SWill Deacon #endif /* __LINUX_ARM_ARCH__ */ 230*db38ee87SWill Deacon 231*db38ee87SWill Deacon #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) 232*db38ee87SWill Deacon 233bac4e960SRussell King #define atomic_inc(v) atomic_add(1, v) 234bac4e960SRussell King #define atomic_dec(v) atomic_sub(1, v) 2354baa9922SRussell King 2364baa9922SRussell King #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0) 2374baa9922SRussell King #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0) 2384baa9922SRussell King #define atomic_inc_return(v) (atomic_add_return(1, v)) 2394baa9922SRussell King #define atomic_dec_return(v) (atomic_sub_return(1, v)) 2404baa9922SRussell King #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0) 2414baa9922SRussell King 2424baa9922SRussell King #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0) 2434baa9922SRussell King 244bac4e960SRussell King #define smp_mb__before_atomic_dec() smp_mb() 245bac4e960SRussell King #define smp_mb__after_atomic_dec() smp_mb() 246bac4e960SRussell King #define smp_mb__before_atomic_inc() smp_mb() 247bac4e960SRussell King #define smp_mb__after_atomic_inc() smp_mb() 2484baa9922SRussell King 24924b44a66SWill Deacon #ifndef CONFIG_GENERIC_ATOMIC64 25024b44a66SWill Deacon typedef struct { 251237f1233SChen Gang long long counter; 25224b44a66SWill Deacon } atomic64_t; 25324b44a66SWill Deacon 25424b44a66SWill Deacon #define ATOMIC64_INIT(i) { (i) } 25524b44a66SWill Deacon 2564fd75911SWill Deacon #ifdef CONFIG_ARM_LPAE 257237f1233SChen Gang static inline long long atomic64_read(const atomic64_t *v) 2584fd75911SWill Deacon { 259237f1233SChen Gang long long result; 2604fd75911SWill Deacon 2614fd75911SWill Deacon __asm__ __volatile__("@ atomic64_read\n" 2624fd75911SWill Deacon " ldrd %0, %H0, [%1]" 2634fd75911SWill Deacon : "=&r" (result) 2644fd75911SWill Deacon : "r" (&v->counter), "Qo" (v->counter) 2654fd75911SWill Deacon ); 2664fd75911SWill Deacon 2674fd75911SWill Deacon return result; 2684fd75911SWill Deacon } 2694fd75911SWill Deacon 270237f1233SChen Gang static inline void atomic64_set(atomic64_t *v, long long i) 2714fd75911SWill Deacon { 2724fd75911SWill Deacon __asm__ __volatile__("@ atomic64_set\n" 2734fd75911SWill Deacon " strd %2, %H2, [%1]" 2744fd75911SWill Deacon : "=Qo" (v->counter) 2754fd75911SWill Deacon : "r" (&v->counter), "r" (i) 2764fd75911SWill Deacon ); 2774fd75911SWill Deacon } 2784fd75911SWill Deacon #else 279237f1233SChen Gang static inline long long atomic64_read(const atomic64_t *v) 28024b44a66SWill Deacon { 281237f1233SChen Gang long long result; 28224b44a66SWill Deacon 28324b44a66SWill Deacon __asm__ __volatile__("@ atomic64_read\n" 28424b44a66SWill Deacon " ldrexd %0, %H0, [%1]" 28524b44a66SWill Deacon : "=&r" (result) 286398aa668SWill Deacon : "r" (&v->counter), "Qo" (v->counter) 28724b44a66SWill Deacon ); 28824b44a66SWill Deacon 28924b44a66SWill Deacon return result; 29024b44a66SWill Deacon } 29124b44a66SWill Deacon 292237f1233SChen Gang static inline void atomic64_set(atomic64_t *v, long long i) 29324b44a66SWill Deacon { 294237f1233SChen Gang long long tmp; 29524b44a66SWill Deacon 296f38d999cSWill Deacon prefetchw(&v->counter); 29724b44a66SWill Deacon __asm__ __volatile__("@ atomic64_set\n" 298398aa668SWill Deacon "1: ldrexd %0, %H0, [%2]\n" 299398aa668SWill Deacon " strexd %0, %3, %H3, [%2]\n" 30024b44a66SWill Deacon " teq %0, #0\n" 30124b44a66SWill Deacon " bne 1b" 302398aa668SWill Deacon : "=&r" (tmp), "=Qo" (v->counter) 30324b44a66SWill Deacon : "r" (&v->counter), "r" (i) 30424b44a66SWill Deacon : "cc"); 30524b44a66SWill Deacon } 3064fd75911SWill Deacon #endif 30724b44a66SWill Deacon 308237f1233SChen Gang static inline void atomic64_add(long long i, atomic64_t *v) 30924b44a66SWill Deacon { 310237f1233SChen Gang long long result; 31124b44a66SWill Deacon unsigned long tmp; 31224b44a66SWill Deacon 313f38d999cSWill Deacon prefetchw(&v->counter); 31424b44a66SWill Deacon __asm__ __volatile__("@ atomic64_add\n" 315398aa668SWill Deacon "1: ldrexd %0, %H0, [%3]\n" 3162245f924SVictor Kamensky " adds %Q0, %Q0, %Q4\n" 3172245f924SVictor Kamensky " adc %R0, %R0, %R4\n" 318398aa668SWill Deacon " strexd %1, %0, %H0, [%3]\n" 31924b44a66SWill Deacon " teq %1, #0\n" 32024b44a66SWill Deacon " bne 1b" 321398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 32224b44a66SWill Deacon : "r" (&v->counter), "r" (i) 32324b44a66SWill Deacon : "cc"); 32424b44a66SWill Deacon } 32524b44a66SWill Deacon 326237f1233SChen Gang static inline long long atomic64_add_return(long long i, atomic64_t *v) 32724b44a66SWill Deacon { 328237f1233SChen Gang long long result; 32924b44a66SWill Deacon unsigned long tmp; 33024b44a66SWill Deacon 33124b44a66SWill Deacon smp_mb(); 332c32ffce0SWill Deacon prefetchw(&v->counter); 33324b44a66SWill Deacon 33424b44a66SWill Deacon __asm__ __volatile__("@ atomic64_add_return\n" 335398aa668SWill Deacon "1: ldrexd %0, %H0, [%3]\n" 3362245f924SVictor Kamensky " adds %Q0, %Q0, %Q4\n" 3372245f924SVictor Kamensky " adc %R0, %R0, %R4\n" 338398aa668SWill Deacon " strexd %1, %0, %H0, [%3]\n" 33924b44a66SWill Deacon " teq %1, #0\n" 34024b44a66SWill Deacon " bne 1b" 341398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 34224b44a66SWill Deacon : "r" (&v->counter), "r" (i) 34324b44a66SWill Deacon : "cc"); 34424b44a66SWill Deacon 34524b44a66SWill Deacon smp_mb(); 34624b44a66SWill Deacon 34724b44a66SWill Deacon return result; 34824b44a66SWill Deacon } 34924b44a66SWill Deacon 350237f1233SChen Gang static inline void atomic64_sub(long long i, atomic64_t *v) 35124b44a66SWill Deacon { 352237f1233SChen Gang long long result; 35324b44a66SWill Deacon unsigned long tmp; 35424b44a66SWill Deacon 355f38d999cSWill Deacon prefetchw(&v->counter); 35624b44a66SWill Deacon __asm__ __volatile__("@ atomic64_sub\n" 357398aa668SWill Deacon "1: ldrexd %0, %H0, [%3]\n" 3582245f924SVictor Kamensky " subs %Q0, %Q0, %Q4\n" 3592245f924SVictor Kamensky " sbc %R0, %R0, %R4\n" 360398aa668SWill Deacon " strexd %1, %0, %H0, [%3]\n" 36124b44a66SWill Deacon " teq %1, #0\n" 36224b44a66SWill Deacon " bne 1b" 363398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 36424b44a66SWill Deacon : "r" (&v->counter), "r" (i) 36524b44a66SWill Deacon : "cc"); 36624b44a66SWill Deacon } 36724b44a66SWill Deacon 368237f1233SChen Gang static inline long long atomic64_sub_return(long long i, atomic64_t *v) 36924b44a66SWill Deacon { 370237f1233SChen Gang long long result; 37124b44a66SWill Deacon unsigned long tmp; 37224b44a66SWill Deacon 37324b44a66SWill Deacon smp_mb(); 374c32ffce0SWill Deacon prefetchw(&v->counter); 37524b44a66SWill Deacon 37624b44a66SWill Deacon __asm__ __volatile__("@ atomic64_sub_return\n" 377398aa668SWill Deacon "1: ldrexd %0, %H0, [%3]\n" 3782245f924SVictor Kamensky " subs %Q0, %Q0, %Q4\n" 3792245f924SVictor Kamensky " sbc %R0, %R0, %R4\n" 380398aa668SWill Deacon " strexd %1, %0, %H0, [%3]\n" 38124b44a66SWill Deacon " teq %1, #0\n" 38224b44a66SWill Deacon " bne 1b" 383398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 38424b44a66SWill Deacon : "r" (&v->counter), "r" (i) 38524b44a66SWill Deacon : "cc"); 38624b44a66SWill Deacon 38724b44a66SWill Deacon smp_mb(); 38824b44a66SWill Deacon 38924b44a66SWill Deacon return result; 39024b44a66SWill Deacon } 39124b44a66SWill Deacon 392237f1233SChen Gang static inline long long atomic64_cmpxchg(atomic64_t *ptr, long long old, 393237f1233SChen Gang long long new) 39424b44a66SWill Deacon { 395237f1233SChen Gang long long oldval; 39624b44a66SWill Deacon unsigned long res; 39724b44a66SWill Deacon 39824b44a66SWill Deacon smp_mb(); 399c32ffce0SWill Deacon prefetchw(&ptr->counter); 40024b44a66SWill Deacon 40124b44a66SWill Deacon do { 40224b44a66SWill Deacon __asm__ __volatile__("@ atomic64_cmpxchg\n" 403398aa668SWill Deacon "ldrexd %1, %H1, [%3]\n" 40424b44a66SWill Deacon "mov %0, #0\n" 405398aa668SWill Deacon "teq %1, %4\n" 406398aa668SWill Deacon "teqeq %H1, %H4\n" 407398aa668SWill Deacon "strexdeq %0, %5, %H5, [%3]" 408398aa668SWill Deacon : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter) 40924b44a66SWill Deacon : "r" (&ptr->counter), "r" (old), "r" (new) 41024b44a66SWill Deacon : "cc"); 41124b44a66SWill Deacon } while (res); 41224b44a66SWill Deacon 41324b44a66SWill Deacon smp_mb(); 41424b44a66SWill Deacon 41524b44a66SWill Deacon return oldval; 41624b44a66SWill Deacon } 41724b44a66SWill Deacon 418237f1233SChen Gang static inline long long atomic64_xchg(atomic64_t *ptr, long long new) 41924b44a66SWill Deacon { 420237f1233SChen Gang long long result; 42124b44a66SWill Deacon unsigned long tmp; 42224b44a66SWill Deacon 42324b44a66SWill Deacon smp_mb(); 424c32ffce0SWill Deacon prefetchw(&ptr->counter); 42524b44a66SWill Deacon 42624b44a66SWill Deacon __asm__ __volatile__("@ atomic64_xchg\n" 427398aa668SWill Deacon "1: ldrexd %0, %H0, [%3]\n" 428398aa668SWill Deacon " strexd %1, %4, %H4, [%3]\n" 42924b44a66SWill Deacon " teq %1, #0\n" 43024b44a66SWill Deacon " bne 1b" 431398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter) 43224b44a66SWill Deacon : "r" (&ptr->counter), "r" (new) 43324b44a66SWill Deacon : "cc"); 43424b44a66SWill Deacon 43524b44a66SWill Deacon smp_mb(); 43624b44a66SWill Deacon 43724b44a66SWill Deacon return result; 43824b44a66SWill Deacon } 43924b44a66SWill Deacon 440237f1233SChen Gang static inline long long atomic64_dec_if_positive(atomic64_t *v) 44124b44a66SWill Deacon { 442237f1233SChen Gang long long result; 44324b44a66SWill Deacon unsigned long tmp; 44424b44a66SWill Deacon 44524b44a66SWill Deacon smp_mb(); 446c32ffce0SWill Deacon prefetchw(&v->counter); 44724b44a66SWill Deacon 44824b44a66SWill Deacon __asm__ __volatile__("@ atomic64_dec_if_positive\n" 449398aa668SWill Deacon "1: ldrexd %0, %H0, [%3]\n" 4502245f924SVictor Kamensky " subs %Q0, %Q0, #1\n" 4512245f924SVictor Kamensky " sbc %R0, %R0, #0\n" 4522245f924SVictor Kamensky " teq %R0, #0\n" 45324b44a66SWill Deacon " bmi 2f\n" 454398aa668SWill Deacon " strexd %1, %0, %H0, [%3]\n" 45524b44a66SWill Deacon " teq %1, #0\n" 45624b44a66SWill Deacon " bne 1b\n" 45724b44a66SWill Deacon "2:" 458398aa668SWill Deacon : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 45924b44a66SWill Deacon : "r" (&v->counter) 46024b44a66SWill Deacon : "cc"); 46124b44a66SWill Deacon 46224b44a66SWill Deacon smp_mb(); 46324b44a66SWill Deacon 46424b44a66SWill Deacon return result; 46524b44a66SWill Deacon } 46624b44a66SWill Deacon 467237f1233SChen Gang static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u) 46824b44a66SWill Deacon { 469237f1233SChen Gang long long val; 47024b44a66SWill Deacon unsigned long tmp; 47124b44a66SWill Deacon int ret = 1; 47224b44a66SWill Deacon 47324b44a66SWill Deacon smp_mb(); 474c32ffce0SWill Deacon prefetchw(&v->counter); 47524b44a66SWill Deacon 47624b44a66SWill Deacon __asm__ __volatile__("@ atomic64_add_unless\n" 477398aa668SWill Deacon "1: ldrexd %0, %H0, [%4]\n" 478398aa668SWill Deacon " teq %0, %5\n" 479398aa668SWill Deacon " teqeq %H0, %H5\n" 48024b44a66SWill Deacon " moveq %1, #0\n" 48124b44a66SWill Deacon " beq 2f\n" 4822245f924SVictor Kamensky " adds %Q0, %Q0, %Q6\n" 4832245f924SVictor Kamensky " adc %R0, %R0, %R6\n" 484398aa668SWill Deacon " strexd %2, %0, %H0, [%4]\n" 48524b44a66SWill Deacon " teq %2, #0\n" 48624b44a66SWill Deacon " bne 1b\n" 48724b44a66SWill Deacon "2:" 488398aa668SWill Deacon : "=&r" (val), "+r" (ret), "=&r" (tmp), "+Qo" (v->counter) 48924b44a66SWill Deacon : "r" (&v->counter), "r" (u), "r" (a) 49024b44a66SWill Deacon : "cc"); 49124b44a66SWill Deacon 49224b44a66SWill Deacon if (ret) 49324b44a66SWill Deacon smp_mb(); 49424b44a66SWill Deacon 49524b44a66SWill Deacon return ret; 49624b44a66SWill Deacon } 49724b44a66SWill Deacon 49824b44a66SWill Deacon #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0) 49924b44a66SWill Deacon #define atomic64_inc(v) atomic64_add(1LL, (v)) 50024b44a66SWill Deacon #define atomic64_inc_return(v) atomic64_add_return(1LL, (v)) 50124b44a66SWill Deacon #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0) 50224b44a66SWill Deacon #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0) 50324b44a66SWill Deacon #define atomic64_dec(v) atomic64_sub(1LL, (v)) 50424b44a66SWill Deacon #define atomic64_dec_return(v) atomic64_sub_return(1LL, (v)) 50524b44a66SWill Deacon #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0) 50624b44a66SWill Deacon #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1LL, 0LL) 50724b44a66SWill Deacon 5087847777aSArun Sharma #endif /* !CONFIG_GENERIC_ATOMIC64 */ 5094baa9922SRussell King #endif 5104baa9922SRussell King #endif 511