1235783Skib/**
2235783Skib * \file drm_atomic.h
3235783Skib * Atomic operations used in the DRM which may or may not be provided by the OS.
4235783Skib *
5235783Skib * \author Eric Anholt <anholt@FreeBSD.org>
6235783Skib */
7235783Skib
8235783Skib/*-
9235783Skib * Copyright 2004 Eric Anholt
10255041Sjkim * Copyright 2013 Jung-uk Kim <jkim@FreeBSD.org>
11235783Skib * All Rights Reserved.
12235783Skib *
13235783Skib * Permission is hereby granted, free of charge, to any person obtaining a
14235783Skib * copy of this software and associated documentation files (the "Software"),
15235783Skib * to deal in the Software without restriction, including without limitation
16235783Skib * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17235783Skib * and/or sell copies of the Software, and to permit persons to whom the
18235783Skib * Software is furnished to do so, subject to the following conditions:
19235783Skib *
20235783Skib * The above copyright notice and this permission notice (including the next
21235783Skib * paragraph) shall be included in all copies or substantial portions of the
22235783Skib * Software.
23235783Skib *
24235783Skib * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25235783Skib * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26235783Skib * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
27235783Skib * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28235783Skib * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29235783Skib * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30235783Skib * OTHER DEALINGS IN THE SOFTWARE.
31235783Skib */
32235783Skib
33235783Skib#include <sys/cdefs.h>
34235783Skib__FBSDID("$FreeBSD$");
35235783Skib
36255041Sjkimtypedef u_int		atomic_t;
37254860Sdumbbelltypedef uint64_t	atomic64_t;
38235783Skib
39282199Sdumbbell#define	NB_BITS_PER_LONG		(sizeof(long) * NBBY)
40282199Sdumbbell#define	BITS_TO_LONGS(x)		howmany(x, NB_BITS_PER_LONG)
41235783Skib
42255039Sjkim#define	atomic_read(p)			(*(volatile u_int *)(p))
43255039Sjkim#define	atomic_set(p, v)		do { *(u_int *)(p) = (v); } while (0)
44235783Skib
45282199Sdumbbell#define	atomic64_read(p)		atomic_load_acq_64(p)
46282199Sdumbbell#define	atomic64_set(p, v)		atomic_store_rel_64(p, v)
47282199Sdumbbell
48254860Sdumbbell#define	atomic_add(v, p)		atomic_add_int(p, v)
49254860Sdumbbell#define	atomic_sub(v, p)		atomic_subtract_int(p, v)
50254860Sdumbbell#define	atomic_inc(p)			atomic_add(1, p)
51254860Sdumbbell#define	atomic_dec(p)			atomic_sub(1, p)
52235783Skib
53254860Sdumbbell#define	atomic_add_return(v, p)		(atomic_fetchadd_int(p, v) + (v))
54254860Sdumbbell#define	atomic_sub_return(v, p)		(atomic_fetchadd_int(p, -(v)) - (v))
55254860Sdumbbell#define	atomic_inc_return(p)		atomic_add_return(1, p)
56254860Sdumbbell#define	atomic_dec_return(p)		atomic_sub_return(1, p)
57235783Skib
58254860Sdumbbell#define	atomic_add_and_test(v, p)	(atomic_add_return(v, p) == 0)
59254860Sdumbbell#define	atomic_sub_and_test(v, p)	(atomic_sub_return(v, p) == 0)
60254860Sdumbbell#define	atomic_inc_and_test(p)		(atomic_inc_return(p) == 0)
61254860Sdumbbell#define	atomic_dec_and_test(p)		(atomic_dec_return(p) == 0)
62235783Skib
63254860Sdumbbell#define	atomic_xchg(p, v)		atomic_swap_int(p, v)
64254860Sdumbbell#define	atomic64_xchg(p, v)		atomic_swap_64(p, v)
65235783Skib
66282199Sdumbbell#define	__bit_word(b)			((b) / NB_BITS_PER_LONG)
67282199Sdumbbell#define	__bit_mask(b)			(1UL << (b) % NB_BITS_PER_LONG)
68255041Sjkim#define	__bit_addr(p, b)		((volatile u_long *)(p) + __bit_word(b))
69255041Sjkim
70254860Sdumbbell#define	clear_bit(b, p) \
71255041Sjkim    atomic_clear_long(__bit_addr(p, b), __bit_mask(b))
72254860Sdumbbell#define	set_bit(b, p) \
73255041Sjkim    atomic_set_long(__bit_addr(p, b), __bit_mask(b))
74254860Sdumbbell#define	test_bit(b, p) \
75255042Sjkim    ((*__bit_addr(p, b) & __bit_mask(b)) != 0)
76282199Sdumbbell#define	test_and_set_bit(b, p) \
77282199Sdumbbell    (atomic_xchg((p), 1) != b)
78282199Sdumbbell#define	cmpxchg(ptr, old, new) \
79282199Sdumbbell    (atomic_cmpset_int((volatile u_int *)(ptr),(old),(new)) ? (old) : (0))
80254860Sdumbbell
81255041Sjkimstatic __inline u_long
82255041Sjkimfind_first_zero_bit(const u_long *p, u_long max)
83235783Skib{
84255041Sjkim	u_long i, n;
85235783Skib
86282199Sdumbbell	KASSERT(max % NB_BITS_PER_LONG == 0, ("invalid bitmap size %lu", max));
87282199Sdumbbell	for (i = 0; i < max / NB_BITS_PER_LONG; i++) {
88255041Sjkim		n = ~p[i];
89254860Sdumbbell		if (n != 0)
90282199Sdumbbell			return (i * NB_BITS_PER_LONG + ffsl(n) - 1);
91235783Skib	}
92254860Sdumbbell	return (max);
93235783Skib}
94