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
39255041Sjkim#define	BITS_PER_LONG			(sizeof(long) * NBBY)
40255041Sjkim#define	BITS_TO_LONGS(x)		howmany(x, 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
45254860Sdumbbell#define	atomic_add(v, p)		atomic_add_int(p, v)
46254860Sdumbbell#define	atomic_sub(v, p)		atomic_subtract_int(p, v)
47254860Sdumbbell#define	atomic_inc(p)			atomic_add(1, p)
48254860Sdumbbell#define	atomic_dec(p)			atomic_sub(1, p)
49235783Skib
50254860Sdumbbell#define	atomic_add_return(v, p)		(atomic_fetchadd_int(p, v) + (v))
51254860Sdumbbell#define	atomic_sub_return(v, p)		(atomic_fetchadd_int(p, -(v)) - (v))
52254860Sdumbbell#define	atomic_inc_return(p)		atomic_add_return(1, p)
53254860Sdumbbell#define	atomic_dec_return(p)		atomic_sub_return(1, p)
54235783Skib
55254860Sdumbbell#define	atomic_add_and_test(v, p)	(atomic_add_return(v, p) == 0)
56254860Sdumbbell#define	atomic_sub_and_test(v, p)	(atomic_sub_return(v, p) == 0)
57254860Sdumbbell#define	atomic_inc_and_test(p)		(atomic_inc_return(p) == 0)
58254860Sdumbbell#define	atomic_dec_and_test(p)		(atomic_dec_return(p) == 0)
59235783Skib
60254860Sdumbbell#define	atomic_xchg(p, v)		atomic_swap_int(p, v)
61254860Sdumbbell#define	atomic64_xchg(p, v)		atomic_swap_64(p, v)
62235783Skib
63255041Sjkim#define	__bit_word(b)			((b) / BITS_PER_LONG)
64255041Sjkim#define	__bit_mask(b)			(1UL << (b) % BITS_PER_LONG)
65255041Sjkim#define	__bit_addr(p, b)		((volatile u_long *)(p) + __bit_word(b))
66255041Sjkim
67254860Sdumbbell#define	clear_bit(b, p) \
68255041Sjkim    atomic_clear_long(__bit_addr(p, b), __bit_mask(b))
69254860Sdumbbell#define	set_bit(b, p) \
70255041Sjkim    atomic_set_long(__bit_addr(p, b), __bit_mask(b))
71254860Sdumbbell#define	test_bit(b, p) \
72255042Sjkim    ((*__bit_addr(p, b) & __bit_mask(b)) != 0)
73254860Sdumbbell
74255041Sjkimstatic __inline u_long
75255041Sjkimfind_first_zero_bit(const u_long *p, u_long max)
76235783Skib{
77255041Sjkim	u_long i, n;
78235783Skib
79255041Sjkim	KASSERT(max % BITS_PER_LONG == 0, ("invalid bitmap size %lu", max));
80255041Sjkim	for (i = 0; i < max / BITS_PER_LONG; i++) {
81255041Sjkim		n = ~p[i];
82254860Sdumbbell		if (n != 0)
83255041Sjkim			return (i * BITS_PER_LONG + ffsl(n) - 1);
84235783Skib	}
85254860Sdumbbell	return (max);
86235783Skib}
87