MCAS changes from Matt
[openafs.git] / src / mcas / intel_defns.h
1 /*
2 Copyright (c) 2003, Keir Fraser All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7
8     * Redistributions of source code must retain the above copyright
9     * notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above
11     * copyright notice, this list of conditions and the following
12     * disclaimer in the documentation and/or other materials provided
13     * with the distribution.  Neither the name of the Keir Fraser
14     * nor the names of its contributors may be used to endorse or
15     * promote products derived from this software without specific
16     * prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef __INTEL_DEFNS_H__
32 #define __INTEL_DEFNS_H__
33
34 #include <pthread.h>
35 #include <sched.h>
36
37 #ifndef INTEL
38 #define INTEL
39 #endif
40
41 #define CACHE_LINE_SIZE 64
42
43 #if 0
44 #define pthread_mutex_init(_m,_i) \
45 ({ pthread_mutex_init(_m,_i); (_m)->__m_kind = PTHREAD_MUTEX_ADAPTIVE_NP; })
46 #endif
47
48
49 /*
50  * I. Compare-and-swap.
51  */
52
53 /*
54  * This is a strong barrier! Reads cannot be delayed beyond a later store.
55  * Reads cannot be hoisted beyond a LOCK prefix. Stores always in-order.
56  */
57 #define CAS(_a, _o, _n)                                    \
58 ({ __typeof__(_o) __o = _o;                                \
59    __asm__ __volatile__(                                   \
60        "lock cmpxchg %3,%1"                                \
61        : "=a" (__o), "=m" (*(volatile unsigned int *)(_a)) \
62        :  "0" (__o), "r" (_n) );                           \
63    __o;                                                    \
64 })
65
66 #define FAS(_a, _n)                                        \
67 ({ __typeof__(_n) __o;                                     \
68    __asm__ __volatile__(                                   \
69        "lock xchg %0,%1"                                   \
70        : "=r" (__o), "=m" (*(volatile unsigned int *)(_a)) \
71        :  "0" (_n) );                                      \
72    __o;                                                    \
73 })
74
75 #define CAS64(_a, _o, _n)                                        \
76 ({ __typeof__(_o) __o = _o;                                      \
77    __asm__ __volatile__(                                         \
78        "movl %3, %%ecx;"                                         \
79        "movl %4, %%ebx;"                                         \
80        "lock cmpxchg8b %1"                                       \
81        : "=A" (__o), "=m" (*(volatile unsigned long long *)(_a)) \
82        : "0" (__o), "m" (_n >> 32), "m" (_n)                     \
83        : "ebx", "ecx" );                                         \
84    __o;                                                          \
85 })
86
87 /* Update Integer location, return Old value. */
88 #define CASIO CAS
89 #define FASIO FAS
90 /* Update Pointer location, return Old value. */
91 #define CASPO CAS
92 #define FASPO FAS
93 /* Update 32/64-bit location, return Old value. */
94 #define CAS32O CAS
95 #define CAS64O CAS64
96
97 /*
98  * II. Memory barriers.
99  *  WMB(): All preceding write operations must commit before any later writes.
100  *  RMB(): All preceding read operations must commit before any later reads.
101  *  MB():  All preceding memory accesses must commit before any later accesses.
102  *
103  *  If the compiler does not observe these barriers (but any sane compiler
104  *  will!), then VOLATILE should be defined as 'volatile'.
105  */
106
107 #define MB()  __asm__ __volatile__ ("lock; addl $0,0(%%esp)" : : : "memory")
108 #define WMB() __asm__ __volatile__ ("" : : : "memory")
109 #define RMB() MB()
110 #define VOLATILE /*volatile*/
111
112 /* On Intel, CAS is a strong barrier, but not a compile barrier. */
113 #define RMB_NEAR_CAS() WMB()
114 #define WMB_NEAR_CAS() WMB()
115 #define MB_NEAR_CAS()  WMB()
116
117
118 /*
119  * III. Cycle counter access.
120  */
121
122 typedef unsigned long long tick_t;
123 #define RDTICK() \
124     ({ tick_t __t; __asm__ __volatile__ ("rdtsc" : "=A" (__t)); __t; })
125
126
127 /*
128  * IV. Types.
129  */
130
131 typedef unsigned char      _u8;
132 typedef unsigned short     _u16;
133 typedef unsigned int       _u32;
134 typedef unsigned long long _u64;
135
136 #endif /* __INTEL_DEFNS_H__ */