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