xref: /dflybsd-src/sys/dev/drm/include/asm-generic/atomic-long.h (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1a7f15a6cSFrançois Tigeot /*
2a7f15a6cSFrançois Tigeot  * Copyright (c) 2016 Mellanox Technologies, Ltd.
3a7f15a6cSFrançois Tigeot  * All rights reserved.
4a7f15a6cSFrançois Tigeot  *
5a7f15a6cSFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
6a7f15a6cSFrançois Tigeot  * modification, are permitted provided that the following conditions
7a7f15a6cSFrançois Tigeot  * are met:
8a7f15a6cSFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
9a7f15a6cSFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
10a7f15a6cSFrançois Tigeot  *    disclaimer.
11a7f15a6cSFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
12a7f15a6cSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
13a7f15a6cSFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
14a7f15a6cSFrançois Tigeot  *
15a7f15a6cSFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16a7f15a6cSFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17a7f15a6cSFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18a7f15a6cSFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19a7f15a6cSFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20a7f15a6cSFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21a7f15a6cSFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22a7f15a6cSFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23a7f15a6cSFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24a7f15a6cSFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a7f15a6cSFrançois Tigeot  */
26a7f15a6cSFrançois Tigeot 
27a7f15a6cSFrançois Tigeot #ifndef _ASM_ATOMIC_LONG_H_
28a7f15a6cSFrançois Tigeot #define _ASM_ATOMIC_LONG_H_
29a7f15a6cSFrançois Tigeot 
30*78973132SSergey Zigachev typedef struct {
31*78973132SSergey Zigachev         volatile long counter;
32*78973132SSergey Zigachev } atomic_long_t;
33*78973132SSergey Zigachev 
34*78973132SSergey Zigachev static inline void
atomic_long_set(atomic_long_t * v,long i)35*78973132SSergey Zigachev atomic_long_set(atomic_long_t *v, long i)
36*78973132SSergey Zigachev {
37*78973132SSergey Zigachev         WRITE_ONCE(v->counter, i);
38*78973132SSergey Zigachev }
39*78973132SSergey Zigachev 
40*78973132SSergey Zigachev static inline long
atomic_long_xchg(atomic_long_t * v,long val)41*78973132SSergey Zigachev atomic_long_xchg(atomic_long_t *v, long val)
42*78973132SSergey Zigachev {
43*78973132SSergey Zigachev         return atomic_swap_long(&v->counter, val);
44*78973132SSergey Zigachev }
45*78973132SSergey Zigachev 
46*78973132SSergey Zigachev static inline long
atomic_long_cmpxchg(atomic_long_t * v,long old,long new)47*78973132SSergey Zigachev atomic_long_cmpxchg(atomic_long_t *v, long old, long new)
48*78973132SSergey Zigachev {
49*78973132SSergey Zigachev         long ret = old;
50*78973132SSergey Zigachev 
51*78973132SSergey Zigachev         for (;;) {
52*78973132SSergey Zigachev                 if (atomic_fcmpset_long(&v->counter, &ret, new))
53*78973132SSergey Zigachev                         break;
54*78973132SSergey Zigachev                 if (ret != old)
55*78973132SSergey Zigachev                         break;
56*78973132SSergey Zigachev         }
57*78973132SSergey Zigachev         return (ret);
58*78973132SSergey Zigachev }
59*78973132SSergey Zigachev 
60a7f15a6cSFrançois Tigeot static inline int
atomic_long_add_unless(atomic64_t * v,long a,long u)61a7f15a6cSFrançois Tigeot atomic_long_add_unless(atomic64_t *v, long a, long u)
62a7f15a6cSFrançois Tigeot {
63a7f15a6cSFrançois Tigeot 	long c = atomic64_read(v);
64a7f15a6cSFrançois Tigeot 
65a7f15a6cSFrançois Tigeot 	for (;;) {
66a7f15a6cSFrançois Tigeot 		if (unlikely(c == u))
67a7f15a6cSFrançois Tigeot 			break;
68a7f15a6cSFrançois Tigeot 		if (likely(atomic_fcmpset_long(&v->counter, &c, c + a)))
69a7f15a6cSFrançois Tigeot 			break;
70a7f15a6cSFrançois Tigeot 	}
71a7f15a6cSFrançois Tigeot 	return (c != u);
72a7f15a6cSFrançois Tigeot }
73a7f15a6cSFrançois Tigeot 
74a7f15a6cSFrançois Tigeot #define atomic_long_inc_not_zero(v)	atomic_long_add_unless((v), 1, 0)
75a7f15a6cSFrançois Tigeot 
76a7f15a6cSFrançois Tigeot #endif	/* _ASM_ATOMIC_LONG_H_ */
77