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 __FBSDID("$FreeBSD$"); 35 36 typedef u_int atomic_t; 37 typedef uint64_t atomic64_t; 38 39 #define NB_BITS_PER_LONG (sizeof(long) * NBBY) 40 #define BITS_TO_LONGS(x) howmany(x, NB_BITS_PER_LONG) 41 42 #define atomic_read(p) atomic_load_acq_int(p) 43 #define atomic_set(p, v) atomic_store_rel_int(p, v) 44 45 #define atomic64_read(p) atomic_load_acq_64(p) 46 #define atomic64_set(p, v) atomic_store_rel_64(p, v) 47 48 #define atomic_add(v, p) atomic_add_int(p, v) 49 #define atomic_sub(v, p) atomic_subtract_int(p, v) 50 #define atomic_inc(p) atomic_add(1, p) 51 #define atomic_dec(p) atomic_sub(1, p) 52 53 #define atomic_add_return(v, p) (atomic_fetchadd_int(p, v) + (v)) 54 #define atomic_sub_return(v, p) (atomic_fetchadd_int(p, -(v)) - (v)) 55 #define atomic_inc_return(p) atomic_add_return(1, p) 56 #define atomic_dec_return(p) atomic_sub_return(1, p) 57 58 #define atomic_add_and_test(v, p) (atomic_add_return(v, p) == 0) 59 #define atomic_sub_and_test(v, p) (atomic_sub_return(v, p) == 0) 60 #define atomic_inc_and_test(p) (atomic_inc_return(p) == 0) 61 #define atomic_dec_and_test(p) (atomic_dec_return(p) == 0) 62 63 #define atomic_xchg(p, v) atomic_swap_int(p, v) 64 #define atomic64_xchg(p, v) atomic_swap_64(p, v) 65 66 #define __bit_word(b) ((b) / NB_BITS_PER_LONG) 67 #define __bit_mask(b) (1UL << (b) % NB_BITS_PER_LONG) 68 #define __bit_addr(p, b) ((volatile u_long *)(p) + __bit_word(b)) 69 70 #define clear_bit(b, p) \ 71 atomic_clear_long(__bit_addr(p, b), __bit_mask(b)) 72 #define set_bit(b, p) \ 73 atomic_set_long(__bit_addr(p, b), __bit_mask(b)) 74 #define test_bit(b, p) \ 75 ((*__bit_addr(p, b) & __bit_mask(b)) != 0) 76 #define test_and_set_bit(b, p) \ 77 (atomic_xchg((p), 1) != b) 78 #define cmpxchg(ptr, old, new) \ 79 (atomic_cmpset_int((volatile u_int *)(ptr),(old),(new)) ? (old) : (0)) 80 81 #define atomic_inc_not_zero(p) atomic_inc(p) 82 #define atomic_clear_mask(b, p) atomic_clear_int((p), (b)) 83 84 static __inline u_long 85 find_first_zero_bit(const u_long *p, u_long max) 86 { 87 u_long i, n; 88 89 KASSERT(max % NB_BITS_PER_LONG == 0, ("invalid bitmap size %lu", max)); 90 for (i = 0; i < max / NB_BITS_PER_LONG; i++) { 91 n = ~p[i]; 92 if (n != 0) 93 return (i * NB_BITS_PER_LONG + ffsl(n) - 1); 94 } 95 return (max); 96 } 97