OSDN Git Service

[FreeBSD] Add drm_drawable_free_all()
[android-x86/external-libdrm.git] / bsd-core / 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
32 /* Many of these implementations are rather fake, but good enough. */
33
34 typedef u_int32_t atomic_t;
35
36 #ifdef __FreeBSD__
37 #define atomic_set(p, v)        (*(p) = (v))
38 #define atomic_read(p)          (*(p))
39 #define atomic_inc(p)           atomic_add_int(p, 1)
40 #define atomic_dec(p)           atomic_subtract_int(p, 1)
41 #define atomic_add(n, p)        atomic_add_int(p, n)
42 #define atomic_sub(n, p)        atomic_subtract_int(p, n)
43 #else /* __FreeBSD__ */
44 /* FIXME */
45 #define atomic_set(p, v)        (*(p) = (v))
46 #define atomic_read(p)          (*(p))
47 #define atomic_inc(p)           (*(p) += 1)
48 #define atomic_dec(p)           (*(p) -= 1)
49 #define atomic_add(n, p)        (*(p) += (n))
50 #define atomic_sub(n, p)        (*(p) -= (n))
51 /* FIXME */
52 #define atomic_add_int(p, v)      *(p) += v
53 #define atomic_subtract_int(p, v) *(p) -= v
54 #define atomic_set_int(p, bits)   *(p) |= (bits)
55 #define atomic_clear_int(p, bits) *(p) &= ~(bits)
56 #endif /* !__FreeBSD__ */
57
58 #if !defined(__FreeBSD_version) || (__FreeBSD_version < 500000)
59 #if defined(__i386__)
60 /* The extra atomic functions from 5.0 haven't been merged to 4.x */
61 static __inline int
62 atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
63 {
64         int res = exp;
65
66         __asm __volatile (
67         "       lock ;                  "
68         "       cmpxchgl %1,%2 ;        "
69         "       setz    %%al ;          "
70         "       movzbl  %%al,%0 ;       "
71         "1:                             "
72         "# atomic_cmpset_int"
73         : "+a" (res)                    /* 0 (result) */
74         : "r" (src),                    /* 1 */
75           "m" (*(dst))                  /* 2 */
76         : "memory");                             
77
78         return (res);
79 }
80 #else /* __i386__ */
81 static __inline int
82 atomic_cmpset_int(__volatile__ int *dst, int old, int new)
83 {
84         int s = splhigh();
85         if (*dst==old) {
86                 *dst = new;
87                 splx(s);
88                 return 1;
89         }
90         splx(s);
91         return 0;
92 }
93 #endif /* !__i386__ */
94 #endif /* !__FreeBSD_version || __FreeBSD_version < 500000 */
95
96 static __inline atomic_t
97 test_and_set_bit(int b, volatile void *p)
98 {
99         int s = splhigh();
100         unsigned int m = 1<<b;
101         unsigned int r = *(volatile int *)p & m;
102         *(volatile int *)p |= m;
103         splx(s);
104         return r;
105 }
106
107 static __inline void
108 clear_bit(int b, volatile void *p)
109 {
110         atomic_clear_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
111 }
112
113 static __inline void
114 set_bit(int b, volatile void *p)
115 {
116         atomic_set_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
117 }
118
119 static __inline int
120 test_bit(int b, volatile void *p)
121 {
122         return ((volatile int *)p)[b >> 5] & (1 << (b & 0x1f));
123 }
124
125 static __inline int
126 find_first_zero_bit(volatile void *p, int max)
127 {
128         int b;
129         volatile int *ptr = (volatile int *)p;
130
131         for (b = 0; b < max; b += 32) {
132                 if (ptr[b >> 5] != ~0) {
133                         for (;;) {
134                                 if ((ptr[b >> 5] & (1 << (b & 0x1f))) == 0)
135                                         return b;
136                                 b++;
137                         }
138                 }
139         }
140         return max;
141 }