13f7e212dSArnd Bergmann /* 2*acac43e2SArun Sharma * Generic C implementation of atomic counter operations. Usable on 3*acac43e2SArun Sharma * UP systems only. Do not include in machine independent code. 4*acac43e2SArun Sharma * 53f7e212dSArnd Bergmann * Originally implemented for MN10300. 63f7e212dSArnd Bergmann * 73f7e212dSArnd Bergmann * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 83f7e212dSArnd Bergmann * Written by David Howells (dhowells@redhat.com) 93f7e212dSArnd Bergmann * 103f7e212dSArnd Bergmann * This program is free software; you can redistribute it and/or 113f7e212dSArnd Bergmann * modify it under the terms of the GNU General Public Licence 123f7e212dSArnd Bergmann * as published by the Free Software Foundation; either version 133f7e212dSArnd Bergmann * 2 of the Licence, or (at your option) any later version. 143f7e212dSArnd Bergmann */ 153f7e212dSArnd Bergmann #ifndef __ASM_GENERIC_ATOMIC_H 163f7e212dSArnd Bergmann #define __ASM_GENERIC_ATOMIC_H 173f7e212dSArnd Bergmann 183f7e212dSArnd Bergmann #ifdef CONFIG_SMP 193f7e212dSArnd Bergmann #error not SMP safe 203f7e212dSArnd Bergmann #endif 213f7e212dSArnd Bergmann 223f7e212dSArnd Bergmann /* 233f7e212dSArnd Bergmann * Atomic operations that C can't guarantee us. Useful for 243f7e212dSArnd Bergmann * resource counting etc.. 253f7e212dSArnd Bergmann */ 263f7e212dSArnd Bergmann 273f7e212dSArnd Bergmann #define ATOMIC_INIT(i) { (i) } 283f7e212dSArnd Bergmann 293f7e212dSArnd Bergmann #ifdef __KERNEL__ 303f7e212dSArnd Bergmann 313f7e212dSArnd Bergmann /** 323f7e212dSArnd Bergmann * atomic_read - read atomic variable 333f7e212dSArnd Bergmann * @v: pointer of type atomic_t 343f7e212dSArnd Bergmann * 3537682177SPeter Fritzsche * Atomically reads the value of @v. 363f7e212dSArnd Bergmann */ 37f3d46f9dSAnton Blanchard #define atomic_read(v) (*(volatile int *)&(v)->counter) 383f7e212dSArnd Bergmann 393f7e212dSArnd Bergmann /** 403f7e212dSArnd Bergmann * atomic_set - set atomic variable 413f7e212dSArnd Bergmann * @v: pointer of type atomic_t 423f7e212dSArnd Bergmann * @i: required value 433f7e212dSArnd Bergmann * 4437682177SPeter Fritzsche * Atomically sets the value of @v to @i. 453f7e212dSArnd Bergmann */ 463f7e212dSArnd Bergmann #define atomic_set(v, i) (((v)->counter) = (i)) 473f7e212dSArnd Bergmann 48df9ee292SDavid Howells #include <linux/irqflags.h> 493f7e212dSArnd Bergmann #include <asm/system.h> 503f7e212dSArnd Bergmann 513f7e212dSArnd Bergmann /** 523f7e212dSArnd Bergmann * atomic_add_return - add integer to atomic variable 533f7e212dSArnd Bergmann * @i: integer value to add 543f7e212dSArnd Bergmann * @v: pointer of type atomic_t 553f7e212dSArnd Bergmann * 563f7e212dSArnd Bergmann * Atomically adds @i to @v and returns the result 573f7e212dSArnd Bergmann */ 583f7e212dSArnd Bergmann static inline int atomic_add_return(int i, atomic_t *v) 593f7e212dSArnd Bergmann { 603f7e212dSArnd Bergmann unsigned long flags; 613f7e212dSArnd Bergmann int temp; 623f7e212dSArnd Bergmann 63df9ee292SDavid Howells raw_local_irq_save(flags); /* Don't trace it in an irqsoff handler */ 643f7e212dSArnd Bergmann temp = v->counter; 653f7e212dSArnd Bergmann temp += i; 663f7e212dSArnd Bergmann v->counter = temp; 679e58143dSMichal Simek raw_local_irq_restore(flags); 683f7e212dSArnd Bergmann 693f7e212dSArnd Bergmann return temp; 703f7e212dSArnd Bergmann } 713f7e212dSArnd Bergmann 723f7e212dSArnd Bergmann /** 733f7e212dSArnd Bergmann * atomic_sub_return - subtract integer from atomic variable 743f7e212dSArnd Bergmann * @i: integer value to subtract 753f7e212dSArnd Bergmann * @v: pointer of type atomic_t 763f7e212dSArnd Bergmann * 773f7e212dSArnd Bergmann * Atomically subtracts @i from @v and returns the result 783f7e212dSArnd Bergmann */ 793f7e212dSArnd Bergmann static inline int atomic_sub_return(int i, atomic_t *v) 803f7e212dSArnd Bergmann { 813f7e212dSArnd Bergmann unsigned long flags; 823f7e212dSArnd Bergmann int temp; 833f7e212dSArnd Bergmann 84df9ee292SDavid Howells raw_local_irq_save(flags); /* Don't trace it in an irqsoff handler */ 853f7e212dSArnd Bergmann temp = v->counter; 863f7e212dSArnd Bergmann temp -= i; 873f7e212dSArnd Bergmann v->counter = temp; 889e58143dSMichal Simek raw_local_irq_restore(flags); 893f7e212dSArnd Bergmann 903f7e212dSArnd Bergmann return temp; 913f7e212dSArnd Bergmann } 923f7e212dSArnd Bergmann 933f7e212dSArnd Bergmann static inline int atomic_add_negative(int i, atomic_t *v) 943f7e212dSArnd Bergmann { 953f7e212dSArnd Bergmann return atomic_add_return(i, v) < 0; 963f7e212dSArnd Bergmann } 973f7e212dSArnd Bergmann 983f7e212dSArnd Bergmann static inline void atomic_add(int i, atomic_t *v) 993f7e212dSArnd Bergmann { 1003f7e212dSArnd Bergmann atomic_add_return(i, v); 1013f7e212dSArnd Bergmann } 1023f7e212dSArnd Bergmann 1033f7e212dSArnd Bergmann static inline void atomic_sub(int i, atomic_t *v) 1043f7e212dSArnd Bergmann { 1053f7e212dSArnd Bergmann atomic_sub_return(i, v); 1063f7e212dSArnd Bergmann } 1073f7e212dSArnd Bergmann 1083f7e212dSArnd Bergmann static inline void atomic_inc(atomic_t *v) 1093f7e212dSArnd Bergmann { 1103f7e212dSArnd Bergmann atomic_add_return(1, v); 1113f7e212dSArnd Bergmann } 1123f7e212dSArnd Bergmann 1133f7e212dSArnd Bergmann static inline void atomic_dec(atomic_t *v) 1143f7e212dSArnd Bergmann { 1153f7e212dSArnd Bergmann atomic_sub_return(1, v); 1163f7e212dSArnd Bergmann } 1173f7e212dSArnd Bergmann 1183f7e212dSArnd Bergmann #define atomic_dec_return(v) atomic_sub_return(1, (v)) 1193f7e212dSArnd Bergmann #define atomic_inc_return(v) atomic_add_return(1, (v)) 1203f7e212dSArnd Bergmann 1213f7e212dSArnd Bergmann #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0) 1223f7e212dSArnd Bergmann #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) 1233f7e212dSArnd Bergmann #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0) 1243f7e212dSArnd Bergmann 1258b9d4069SMathieu Lacage #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v))) 1268b9d4069SMathieu Lacage #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) 1278b9d4069SMathieu Lacage 1288b9d4069SMathieu Lacage #define cmpxchg_local(ptr, o, n) \ 1298b9d4069SMathieu Lacage ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\ 1308b9d4069SMathieu Lacage (unsigned long)(n), sizeof(*(ptr)))) 1318b9d4069SMathieu Lacage 1328b9d4069SMathieu Lacage #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n)) 1338b9d4069SMathieu Lacage 134f24219b4SArun Sharma static inline int __atomic_add_unless(atomic_t *v, int a, int u) 1358b9d4069SMathieu Lacage { 1368b9d4069SMathieu Lacage int c, old; 1378b9d4069SMathieu Lacage c = atomic_read(v); 1388b9d4069SMathieu Lacage while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c) 1398b9d4069SMathieu Lacage c = old; 140f24219b4SArun Sharma return c; 1418b9d4069SMathieu Lacage } 1423f7e212dSArnd Bergmann 1433f7e212dSArnd Bergmann static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr) 1443f7e212dSArnd Bergmann { 1453f7e212dSArnd Bergmann unsigned long flags; 1463f7e212dSArnd Bergmann 1473f7e212dSArnd Bergmann mask = ~mask; 1489e58143dSMichal Simek raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */ 1493f7e212dSArnd Bergmann *addr &= mask; 1509e58143dSMichal Simek raw_local_irq_restore(flags); 1513f7e212dSArnd Bergmann } 1523f7e212dSArnd Bergmann 1533f7e212dSArnd Bergmann /* Assume that atomic operations are already serializing */ 1543f7e212dSArnd Bergmann #define smp_mb__before_atomic_dec() barrier() 1553f7e212dSArnd Bergmann #define smp_mb__after_atomic_dec() barrier() 1563f7e212dSArnd Bergmann #define smp_mb__before_atomic_inc() barrier() 1573f7e212dSArnd Bergmann #define smp_mb__after_atomic_inc() barrier() 1583f7e212dSArnd Bergmann 1593f7e212dSArnd Bergmann #endif /* __KERNEL__ */ 1603f7e212dSArnd Bergmann #endif /* __ASM_GENERIC_ATOMIC_H */ 161