13f7e212dSArnd Bergmann /* 23f7e212dSArnd Bergmann * Generic C implementation of atomic counter operations 33f7e212dSArnd Bergmann * Originally implemented for MN10300. 43f7e212dSArnd Bergmann * 53f7e212dSArnd Bergmann * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 63f7e212dSArnd Bergmann * Written by David Howells (dhowells@redhat.com) 73f7e212dSArnd Bergmann * 83f7e212dSArnd Bergmann * This program is free software; you can redistribute it and/or 93f7e212dSArnd Bergmann * modify it under the terms of the GNU General Public Licence 103f7e212dSArnd Bergmann * as published by the Free Software Foundation; either version 113f7e212dSArnd Bergmann * 2 of the Licence, or (at your option) any later version. 123f7e212dSArnd Bergmann */ 133f7e212dSArnd Bergmann #ifndef __ASM_GENERIC_ATOMIC_H 143f7e212dSArnd Bergmann #define __ASM_GENERIC_ATOMIC_H 153f7e212dSArnd Bergmann 163f7e212dSArnd Bergmann #ifdef CONFIG_SMP 173f7e212dSArnd Bergmann #error not SMP safe 183f7e212dSArnd Bergmann #endif 193f7e212dSArnd Bergmann 203f7e212dSArnd Bergmann /* 213f7e212dSArnd Bergmann * Atomic operations that C can't guarantee us. Useful for 223f7e212dSArnd Bergmann * resource counting etc.. 233f7e212dSArnd Bergmann */ 243f7e212dSArnd Bergmann 253f7e212dSArnd Bergmann #define ATOMIC_INIT(i) { (i) } 263f7e212dSArnd Bergmann 273f7e212dSArnd Bergmann #ifdef __KERNEL__ 283f7e212dSArnd Bergmann 293f7e212dSArnd Bergmann /** 303f7e212dSArnd Bergmann * atomic_read - read atomic variable 313f7e212dSArnd Bergmann * @v: pointer of type atomic_t 323f7e212dSArnd Bergmann * 33*37682177SPeter Fritzsche * Atomically reads the value of @v. 343f7e212dSArnd Bergmann */ 35f3d46f9dSAnton Blanchard #define atomic_read(v) (*(volatile int *)&(v)->counter) 363f7e212dSArnd Bergmann 373f7e212dSArnd Bergmann /** 383f7e212dSArnd Bergmann * atomic_set - set atomic variable 393f7e212dSArnd Bergmann * @v: pointer of type atomic_t 403f7e212dSArnd Bergmann * @i: required value 413f7e212dSArnd Bergmann * 42*37682177SPeter Fritzsche * Atomically sets the value of @v to @i. 433f7e212dSArnd Bergmann */ 443f7e212dSArnd Bergmann #define atomic_set(v, i) (((v)->counter) = (i)) 453f7e212dSArnd Bergmann 463f7e212dSArnd Bergmann #include <asm/system.h> 473f7e212dSArnd Bergmann 483f7e212dSArnd Bergmann /** 493f7e212dSArnd Bergmann * atomic_add_return - add integer to atomic variable 503f7e212dSArnd Bergmann * @i: integer value to add 513f7e212dSArnd Bergmann * @v: pointer of type atomic_t 523f7e212dSArnd Bergmann * 533f7e212dSArnd Bergmann * Atomically adds @i to @v and returns the result 543f7e212dSArnd Bergmann */ 553f7e212dSArnd Bergmann static inline int atomic_add_return(int i, atomic_t *v) 563f7e212dSArnd Bergmann { 573f7e212dSArnd Bergmann unsigned long flags; 583f7e212dSArnd Bergmann int temp; 593f7e212dSArnd Bergmann 603f7e212dSArnd Bergmann local_irq_save(flags); 613f7e212dSArnd Bergmann temp = v->counter; 623f7e212dSArnd Bergmann temp += i; 633f7e212dSArnd Bergmann v->counter = temp; 643f7e212dSArnd Bergmann local_irq_restore(flags); 653f7e212dSArnd Bergmann 663f7e212dSArnd Bergmann return temp; 673f7e212dSArnd Bergmann } 683f7e212dSArnd Bergmann 693f7e212dSArnd Bergmann /** 703f7e212dSArnd Bergmann * atomic_sub_return - subtract integer from atomic variable 713f7e212dSArnd Bergmann * @i: integer value to subtract 723f7e212dSArnd Bergmann * @v: pointer of type atomic_t 733f7e212dSArnd Bergmann * 743f7e212dSArnd Bergmann * Atomically subtracts @i from @v and returns the result 753f7e212dSArnd Bergmann */ 763f7e212dSArnd Bergmann static inline int atomic_sub_return(int i, atomic_t *v) 773f7e212dSArnd Bergmann { 783f7e212dSArnd Bergmann unsigned long flags; 793f7e212dSArnd Bergmann int temp; 803f7e212dSArnd Bergmann 813f7e212dSArnd Bergmann local_irq_save(flags); 823f7e212dSArnd Bergmann temp = v->counter; 833f7e212dSArnd Bergmann temp -= i; 843f7e212dSArnd Bergmann v->counter = temp; 853f7e212dSArnd Bergmann local_irq_restore(flags); 863f7e212dSArnd Bergmann 873f7e212dSArnd Bergmann return temp; 883f7e212dSArnd Bergmann } 893f7e212dSArnd Bergmann 903f7e212dSArnd Bergmann static inline int atomic_add_negative(int i, atomic_t *v) 913f7e212dSArnd Bergmann { 923f7e212dSArnd Bergmann return atomic_add_return(i, v) < 0; 933f7e212dSArnd Bergmann } 943f7e212dSArnd Bergmann 953f7e212dSArnd Bergmann static inline void atomic_add(int i, atomic_t *v) 963f7e212dSArnd Bergmann { 973f7e212dSArnd Bergmann atomic_add_return(i, v); 983f7e212dSArnd Bergmann } 993f7e212dSArnd Bergmann 1003f7e212dSArnd Bergmann static inline void atomic_sub(int i, atomic_t *v) 1013f7e212dSArnd Bergmann { 1023f7e212dSArnd Bergmann atomic_sub_return(i, v); 1033f7e212dSArnd Bergmann } 1043f7e212dSArnd Bergmann 1053f7e212dSArnd Bergmann static inline void atomic_inc(atomic_t *v) 1063f7e212dSArnd Bergmann { 1073f7e212dSArnd Bergmann atomic_add_return(1, v); 1083f7e212dSArnd Bergmann } 1093f7e212dSArnd Bergmann 1103f7e212dSArnd Bergmann static inline void atomic_dec(atomic_t *v) 1113f7e212dSArnd Bergmann { 1123f7e212dSArnd Bergmann atomic_sub_return(1, v); 1133f7e212dSArnd Bergmann } 1143f7e212dSArnd Bergmann 1153f7e212dSArnd Bergmann #define atomic_dec_return(v) atomic_sub_return(1, (v)) 1163f7e212dSArnd Bergmann #define atomic_inc_return(v) atomic_add_return(1, (v)) 1173f7e212dSArnd Bergmann 1183f7e212dSArnd Bergmann #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0) 1193f7e212dSArnd Bergmann #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) 1203f7e212dSArnd Bergmann #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0) 1213f7e212dSArnd Bergmann 1223f7e212dSArnd Bergmann #define atomic_add_unless(v, a, u) \ 1233f7e212dSArnd Bergmann ({ \ 1243f7e212dSArnd Bergmann int c, old; \ 1253f7e212dSArnd Bergmann c = atomic_read(v); \ 1263f7e212dSArnd Bergmann while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \ 1273f7e212dSArnd Bergmann c = old; \ 1283f7e212dSArnd Bergmann c != (u); \ 1293f7e212dSArnd Bergmann }) 1303f7e212dSArnd Bergmann 1313f7e212dSArnd Bergmann #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) 1323f7e212dSArnd Bergmann 1333f7e212dSArnd Bergmann static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr) 1343f7e212dSArnd Bergmann { 1353f7e212dSArnd Bergmann unsigned long flags; 1363f7e212dSArnd Bergmann 1373f7e212dSArnd Bergmann mask = ~mask; 1383f7e212dSArnd Bergmann local_irq_save(flags); 1393f7e212dSArnd Bergmann *addr &= mask; 1403f7e212dSArnd Bergmann local_irq_restore(flags); 1413f7e212dSArnd Bergmann } 1423f7e212dSArnd Bergmann 1433f7e212dSArnd Bergmann #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v))) 1443f7e212dSArnd Bergmann #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) 1453f7e212dSArnd Bergmann 1463f7e212dSArnd Bergmann #define cmpxchg_local(ptr, o, n) \ 1473f7e212dSArnd Bergmann ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\ 1483f7e212dSArnd Bergmann (unsigned long)(n), sizeof(*(ptr)))) 1493f7e212dSArnd Bergmann 1503f7e212dSArnd Bergmann #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n)) 1513f7e212dSArnd Bergmann 1523f7e212dSArnd Bergmann /* Assume that atomic operations are already serializing */ 1533f7e212dSArnd Bergmann #define smp_mb__before_atomic_dec() barrier() 1543f7e212dSArnd Bergmann #define smp_mb__after_atomic_dec() barrier() 1553f7e212dSArnd Bergmann #define smp_mb__before_atomic_inc() barrier() 1563f7e212dSArnd Bergmann #define smp_mb__after_atomic_inc() barrier() 1573f7e212dSArnd Bergmann 1583f7e212dSArnd Bergmann #include <asm-generic/atomic-long.h> 1593f7e212dSArnd Bergmann 1603f7e212dSArnd Bergmann #endif /* __KERNEL__ */ 1613f7e212dSArnd Bergmann #endif /* __ASM_GENERIC_ATOMIC_H */ 162