1145132Sanholt/**
2145132Sanholt * \file drm_atomic.h
3145132Sanholt * Atomic operations used in the DRM which may or may not be provided by the OS.
4145132Sanholt *
5145132Sanholt * \author Eric Anholt <anholt@FreeBSD.org>
6145132Sanholt */
7145132Sanholt
8145132Sanholt/*-
9145132Sanholt * Copyright 2004 Eric Anholt
10145132Sanholt * All Rights Reserved.
11145132Sanholt *
12145132Sanholt * Permission is hereby granted, free of charge, to any person obtaining a
13145132Sanholt * copy of this software and associated documentation files (the "Software"),
14145132Sanholt * to deal in the Software without restriction, including without limitation
15145132Sanholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16145132Sanholt * and/or sell copies of the Software, and to permit persons to whom the
17145132Sanholt * Software is furnished to do so, subject to the following conditions:
18145132Sanholt *
19145132Sanholt * The above copyright notice and this permission notice (including the next
20145132Sanholt * paragraph) shall be included in all copies or substantial portions of the
21145132Sanholt * Software.
22145132Sanholt *
23145132Sanholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24145132Sanholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25145132Sanholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26145132Sanholt * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27145132Sanholt * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28145132Sanholt * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29145132Sanholt * OTHER DEALINGS IN THE SOFTWARE.
30145132Sanholt */
31145132Sanholt
32152909Sanholt#include <sys/cdefs.h>
33152909Sanholt__FBSDID("$FreeBSD: releng/10.3/sys/dev/drm/drm_atomic.h 183573 2008-10-03 16:59:11Z rnoland $");
34152909Sanholt
35145132Sanholt/* Many of these implementations are rather fake, but good enough. */
36145132Sanholt
37145132Sanholttypedef u_int32_t atomic_t;
38145132Sanholt
39145132Sanholt#define atomic_set(p, v)	(*(p) = (v))
40145132Sanholt#define atomic_read(p)		(*(p))
41145132Sanholt#define atomic_inc(p)		atomic_add_int(p, 1)
42145132Sanholt#define atomic_dec(p)		atomic_subtract_int(p, 1)
43145132Sanholt#define atomic_add(n, p)	atomic_add_int(p, n)
44145132Sanholt#define atomic_sub(n, p)	atomic_subtract_int(p, n)
45145132Sanholt
46145132Sanholtstatic __inline atomic_t
47145132Sanholttest_and_set_bit(int b, volatile void *p)
48145132Sanholt{
49145132Sanholt	int s = splhigh();
50145132Sanholt	unsigned int m = 1<<b;
51145132Sanholt	unsigned int r = *(volatile int *)p & m;
52145132Sanholt	*(volatile int *)p |= m;
53145132Sanholt	splx(s);
54145132Sanholt	return r;
55145132Sanholt}
56145132Sanholt
57145132Sanholtstatic __inline void
58145132Sanholtclear_bit(int b, volatile void *p)
59145132Sanholt{
60145132Sanholt	atomic_clear_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
61145132Sanholt}
62145132Sanholt
63145132Sanholtstatic __inline void
64145132Sanholtset_bit(int b, volatile void *p)
65145132Sanholt{
66145132Sanholt	atomic_set_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
67145132Sanholt}
68145132Sanholt
69145132Sanholtstatic __inline int
70145132Sanholttest_bit(int b, volatile void *p)
71145132Sanholt{
72145132Sanholt	return ((volatile int *)p)[b >> 5] & (1 << (b & 0x1f));
73145132Sanholt}
74145132Sanholt
75145132Sanholtstatic __inline int
76145132Sanholtfind_first_zero_bit(volatile void *p, int max)
77145132Sanholt{
78145132Sanholt	int b;
79145132Sanholt	volatile int *ptr = (volatile int *)p;
80145132Sanholt
81145132Sanholt	for (b = 0; b < max; b += 32) {
82145132Sanholt		if (ptr[b >> 5] != ~0) {
83145132Sanholt			for (;;) {
84145132Sanholt				if ((ptr[b >> 5] & (1 << (b & 0x1f))) == 0)
85145132Sanholt					return b;
86145132Sanholt				b++;
87145132Sanholt			}
88145132Sanholt		}
89145132Sanholt	}
90145132Sanholt	return max;
91145132Sanholt}
92