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