1*592ffb21SWarner Losh /**
2*592ffb21SWarner Losh * \file drm_atomic.h
3*592ffb21SWarner Losh * Atomic operations used in the DRM which may or may not be provided by the OS.
4*592ffb21SWarner Losh *
5*592ffb21SWarner Losh * \author Eric Anholt <anholt@FreeBSD.org>
6*592ffb21SWarner Losh */
7*592ffb21SWarner Losh
8*592ffb21SWarner Losh /*-
9*592ffb21SWarner Losh * Copyright 2004 Eric Anholt
10*592ffb21SWarner Losh * Copyright 2013 Jung-uk Kim <jkim@FreeBSD.org>
11*592ffb21SWarner Losh * All Rights Reserved.
12*592ffb21SWarner Losh *
13*592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
14*592ffb21SWarner Losh * copy of this software and associated documentation files (the "Software"),
15*592ffb21SWarner Losh * to deal in the Software without restriction, including without limitation
16*592ffb21SWarner Losh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17*592ffb21SWarner Losh * and/or sell copies of the Software, and to permit persons to whom the
18*592ffb21SWarner Losh * Software is furnished to do so, subject to the following conditions:
19*592ffb21SWarner Losh *
20*592ffb21SWarner Losh * The above copyright notice and this permission notice (including the next
21*592ffb21SWarner Losh * paragraph) shall be included in all copies or substantial portions of the
22*592ffb21SWarner Losh * Software.
23*592ffb21SWarner Losh *
24*592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25*592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26*592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27*592ffb21SWarner Losh * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28*592ffb21SWarner Losh * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29*592ffb21SWarner Losh * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30*592ffb21SWarner Losh * OTHER DEALINGS IN THE SOFTWARE.
31*592ffb21SWarner Losh */
32*592ffb21SWarner Losh
33*592ffb21SWarner Losh #include <sys/cdefs.h>
34*592ffb21SWarner Losh typedef u_int atomic_t;
35*592ffb21SWarner Losh typedef uint64_t atomic64_t;
36*592ffb21SWarner Losh
37*592ffb21SWarner Losh #define NB_BITS_PER_LONG (sizeof(long) * NBBY)
38*592ffb21SWarner Losh #define BITS_TO_LONGS(x) howmany(x, NB_BITS_PER_LONG)
39*592ffb21SWarner Losh
40*592ffb21SWarner Losh #define atomic_read(p) atomic_load_acq_int(p)
41*592ffb21SWarner Losh #define atomic_set(p, v) atomic_store_rel_int(p, v)
42*592ffb21SWarner Losh
43*592ffb21SWarner Losh #define atomic64_read(p) atomic_load_acq_64(p)
44*592ffb21SWarner Losh #define atomic64_set(p, v) atomic_store_rel_64(p, v)
45*592ffb21SWarner Losh
46*592ffb21SWarner Losh #define atomic_add(v, p) atomic_add_int(p, v)
47*592ffb21SWarner Losh #define atomic_sub(v, p) atomic_subtract_int(p, v)
48*592ffb21SWarner Losh #define atomic_inc(p) atomic_add(1, p)
49*592ffb21SWarner Losh #define atomic_dec(p) atomic_sub(1, p)
50*592ffb21SWarner Losh
51*592ffb21SWarner Losh #define atomic_add_return(v, p) (atomic_fetchadd_int(p, v) + (v))
52*592ffb21SWarner Losh #define atomic_sub_return(v, p) (atomic_fetchadd_int(p, -(v)) - (v))
53*592ffb21SWarner Losh #define atomic_inc_return(p) atomic_add_return(1, p)
54*592ffb21SWarner Losh #define atomic_dec_return(p) atomic_sub_return(1, p)
55*592ffb21SWarner Losh
56*592ffb21SWarner Losh #define atomic_add_and_test(v, p) (atomic_add_return(v, p) == 0)
57*592ffb21SWarner Losh #define atomic_sub_and_test(v, p) (atomic_sub_return(v, p) == 0)
58*592ffb21SWarner Losh #define atomic_inc_and_test(p) (atomic_inc_return(p) == 0)
59*592ffb21SWarner Losh #define atomic_dec_and_test(p) (atomic_dec_return(p) == 0)
60*592ffb21SWarner Losh
61*592ffb21SWarner Losh #define atomic_xchg(p, v) atomic_swap_int(p, v)
62*592ffb21SWarner Losh #define atomic64_xchg(p, v) atomic_swap_64(p, v)
63*592ffb21SWarner Losh
64*592ffb21SWarner Losh #define __bit_word(b) ((b) / NB_BITS_PER_LONG)
65*592ffb21SWarner Losh #define __bit_mask(b) (1UL << (b) % NB_BITS_PER_LONG)
66*592ffb21SWarner Losh #define __bit_addr(p, b) ((volatile u_long *)(p) + __bit_word(b))
67*592ffb21SWarner Losh
68*592ffb21SWarner Losh #define clear_bit(b, p) \
69*592ffb21SWarner Losh atomic_clear_long(__bit_addr(p, b), __bit_mask(b))
70*592ffb21SWarner Losh #define set_bit(b, p) \
71*592ffb21SWarner Losh atomic_set_long(__bit_addr(p, b), __bit_mask(b))
72*592ffb21SWarner Losh #define test_bit(b, p) \
73*592ffb21SWarner Losh ((*__bit_addr(p, b) & __bit_mask(b)) != 0)
74*592ffb21SWarner Losh #define test_and_set_bit(b, p) \
75*592ffb21SWarner Losh (atomic_xchg((p), 1) != b)
76*592ffb21SWarner Losh #define cmpxchg(ptr, old, new) \
77*592ffb21SWarner Losh (atomic_cmpset_int((volatile u_int *)(ptr),(old),(new)) ? (old) : (0))
78*592ffb21SWarner Losh
79*592ffb21SWarner Losh #define atomic_inc_not_zero(p) atomic_inc(p)
80*592ffb21SWarner Losh #define atomic_clear_mask(b, p) atomic_clear_int((p), (b))
81*592ffb21SWarner Losh
82*592ffb21SWarner Losh static __inline u_long
find_first_zero_bit(const u_long * p,u_long max)83*592ffb21SWarner Losh find_first_zero_bit(const u_long *p, u_long max)
84*592ffb21SWarner Losh {
85*592ffb21SWarner Losh u_long i, n;
86*592ffb21SWarner Losh
87*592ffb21SWarner Losh KASSERT(max % NB_BITS_PER_LONG == 0, ("invalid bitmap size %lu", max));
88*592ffb21SWarner Losh for (i = 0; i < max / NB_BITS_PER_LONG; i++) {
89*592ffb21SWarner Losh n = ~p[i];
90*592ffb21SWarner Losh if (n != 0)
91*592ffb21SWarner Losh return (i * NB_BITS_PER_LONG + ffsl(n) - 1);
92*592ffb21SWarner Losh }
93*592ffb21SWarner Losh return (max);
94*592ffb21SWarner Losh }
95