Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / drm / drm_atomic.h
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  * All Rights Reserved.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice (including the next
20  * paragraph) shall be included in all copies or substantial portions of the
21  * Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  *
31  * $DragonFly: src/sys/dev/drm/drm_atomic.h,v 1.1 2008/04/05 18:12:29 hasso Exp $
32  */
33
34 /* Many of these implementations are rather fake, but good enough. */
35
36 typedef u_int32_t atomic_t;
37
38 #if defined(__FreeBSD__) || defined(__DragonFly__)
39 #define atomic_set(p, v)        (*(p) = (v))
40 #define atomic_read(p)          (*(p))
41 #define atomic_inc(p)           atomic_add_int(p, 1)
42 #define atomic_dec(p)           atomic_subtract_int(p, 1)
43 #define atomic_add(n, p)        atomic_add_int(p, n)
44 #define atomic_sub(n, p)        atomic_subtract_int(p, n)
45 #else /* __FreeBSD__ || __DragonFly__ */
46 /* FIXME */
47 #define atomic_set(p, v)        (*(p) = (v))
48 #define atomic_read(p)          (*(p))
49 #define atomic_inc(p)           (*(p) += 1)
50 #define atomic_dec(p)           (*(p) -= 1)
51 #define atomic_add(n, p)        (*(p) += (n))
52 #define atomic_sub(n, p)        (*(p) -= (n))
53 /* FIXME */
54 #define atomic_add_int(p, v)      *(p) += v
55 #define atomic_subtract_int(p, v) *(p) -= v
56 #define atomic_set_int(p, bits)   *(p) |= (bits)
57 #define atomic_clear_int(p, bits) *(p) &= ~(bits)
58 #endif /* !__FreeBSD__ && !__DragonFly__ */
59
60 #ifndef __DragonFly__
61 #if !defined(__FreeBSD_version) || (__FreeBSD_version < 500000)
62 #if defined(__i386__)
63 /* The extra atomic functions from 5.0 haven't been merged to 4.x */
64 static __inline int
65 atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
66 {
67         int res = exp;
68
69         __asm __volatile (
70         "       lock ;                  "
71         "       cmpxchgl %1,%2 ;        "
72         "       setz    %%al ;          "
73         "       movzbl  %%al,%0 ;       "
74         "1:                             "
75         "# atomic_cmpset_int"
76         : "+a" (res)                    /* 0 (result) */
77         : "r" (src),                    /* 1 */
78           "m" (*(dst))                  /* 2 */
79         : "memory");                             
80
81         return (res);
82 }
83 #else /* __i386__ */
84 static __inline int
85 atomic_cmpset_int(__volatile__ int *dst, int old, int new)
86 {
87         int s = splhigh();
88         if (*dst==old) {
89                 *dst = new;
90                 splx(s);
91                 return 1;
92         }
93         splx(s);
94         return 0;
95 }
96 #endif /* !__i386__ */
97 #endif /* !__FreeBSD_version || __FreeBSD_version < 500000 */
98
99 static __inline atomic_t
100 test_and_set_bit(int b, volatile void *p)
101 {
102         int s = splhigh();
103         unsigned int m = 1<<b;
104         unsigned int r = *(volatile int *)p & m;
105         *(volatile int *)p |= m;
106         splx(s);
107         return r;
108 }
109
110 #else  /* __DragonFly__ */
111
112 static __inline atomic_t
113 test_and_set_bit(int b, volatile void *p)
114 {
115         unsigned int m = 1<<b;
116         unsigned int o, n;
117
118         do {
119                 o = *(volatile int *)p;
120                 n = o | m;
121         } while (!atomic_cmpset_int(p, o, n));
122
123         return (o & m);
124 }
125
126 #endif /* __DragonFly__ */
127
128 static __inline void
129 clear_bit(int b, volatile void *p)
130 {
131         atomic_clear_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
132 }
133
134 static __inline void
135 set_bit(int b, volatile void *p)
136 {
137         atomic_set_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
138 }
139
140 static __inline int
141 test_bit(int b, volatile void *p)
142 {
143         return ((volatile int *)p)[b >> 5] & (1 << (b & 0x1f));
144 }
145
146 static __inline int
147 find_first_zero_bit(volatile void *p, int max)
148 {
149         int b;
150         volatile int *ptr = (volatile int *)p;
151
152         for (b = 0; b < max; b += 32) {
153                 if (ptr[b >> 5] != ~0) {
154                         for (;;) {
155                                 if ((ptr[b >> 5] & (1 << (b & 0x1f))) == 0)
156                                         return b;
157                                 b++;
158                         }
159                 }
160         }
161         return max;
162 }