1 #ifndef _ASM_IA64_INTRINSICS_H
2 #define _ASM_IA64_INTRINSICS_H
5 * Compiler-dependent intrinsics.
7 * Copyright (C) 2002-2003 Hewlett-Packard Co
8 * David Mosberger-Tang <davidm@hpl.hp.com>
11 #include <linux/config.h>
14 * Force an unresolved reference if someone tries to use
15 * ia64_fetch_and_add() with a bad value.
17 extern unsigned long __bad_size_for_ia64_fetch_and_add (void);
18 extern unsigned long __bad_increment_for_ia64_fetch_and_add (void);
20 #define IA64_FETCHADD(tmp,v,n,sz,sem) \
24 __asm__ __volatile__ ("fetchadd4."sem" %0=[%1],%2" \
25 : "=r"(tmp) : "r"(v), "i"(n) : "memory"); \
29 __asm__ __volatile__ ("fetchadd8."sem" %0=[%1],%2" \
30 : "=r"(tmp) : "r"(v), "i"(n) : "memory"); \
34 __bad_size_for_ia64_fetch_and_add(); \
38 #define ia64_fetchadd(i,v,sem) \
41 volatile __typeof__(*(v)) *_v = (v); \
42 /* Can't use a switch () here: gcc isn't always smart enough for that... */ \
44 IA64_FETCHADD(_tmp, _v, -16, sizeof(*(v)), sem); \
46 IA64_FETCHADD(_tmp, _v, -8, sizeof(*(v)), sem); \
48 IA64_FETCHADD(_tmp, _v, -4, sizeof(*(v)), sem); \
50 IA64_FETCHADD(_tmp, _v, -1, sizeof(*(v)), sem); \
52 IA64_FETCHADD(_tmp, _v, 1, sizeof(*(v)), sem); \
54 IA64_FETCHADD(_tmp, _v, 4, sizeof(*(v)), sem); \
56 IA64_FETCHADD(_tmp, _v, 8, sizeof(*(v)), sem); \
58 IA64_FETCHADD(_tmp, _v, 16, sizeof(*(v)), sem); \
60 _tmp = __bad_increment_for_ia64_fetch_and_add(); \
61 (__typeof__(*(v))) (_tmp); /* return old value */ \
64 #define ia64_fetch_and_add(i,v) (ia64_fetchadd(i, v, "rel") + (i)) /* return new value */
67 * This function doesn't exist, so you'll get a linker error if
68 * something tries to do an invalid xchg().
70 extern void __xchg_called_with_bad_pointer (void);
72 static __inline__ unsigned long
73 __xchg (unsigned long x, volatile void *ptr, int size)
79 __asm__ __volatile ("xchg1 %0=[%1],%2" : "=r" (result)
80 : "r" (ptr), "r" (x) : "memory");
84 __asm__ __volatile ("xchg2 %0=[%1],%2" : "=r" (result)
85 : "r" (ptr), "r" (x) : "memory");
89 __asm__ __volatile ("xchg4 %0=[%1],%2" : "=r" (result)
90 : "r" (ptr), "r" (x) : "memory");
94 __asm__ __volatile ("xchg8 %0=[%1],%2" : "=r" (result)
95 : "r" (ptr), "r" (x) : "memory");
98 __xchg_called_with_bad_pointer();
102 #define xchg(ptr,x) \
103 ((__typeof__(*(ptr))) __xchg ((unsigned long) (x), (ptr), sizeof(*(ptr))))
106 * Atomic compare and exchange. Compare OLD with MEM, if identical,
107 * store NEW in MEM. Return the initial value in MEM. Success is
108 * indicated by comparing RETURN with OLD.
111 #define __HAVE_ARCH_CMPXCHG 1
114 * This function doesn't exist, so you'll get a linker error
115 * if something tries to do an invalid cmpxchg().
117 extern long __cmpxchg_called_with_bad_pointer(void);
119 #define ia64_cmpxchg(sem,ptr,old,new,size) \
121 __typeof__(ptr) _p_ = (ptr); \
122 __typeof__(new) _n_ = (new); \
126 case 1: _o_ = (__u8 ) (long) (old); break; \
127 case 2: _o_ = (__u16) (long) (old); break; \
128 case 4: _o_ = (__u32) (long) (old); break; \
129 case 8: _o_ = (__u64) (long) (old); break; \
132 __asm__ __volatile__ ("mov ar.ccv=%0;;" :: "rO"(_o_)); \
135 __asm__ __volatile__ ("cmpxchg1."sem" %0=[%1],%2,ar.ccv" \
136 : "=r"(_r_) : "r"(_p_), "r"(_n_) : "memory"); \
140 __asm__ __volatile__ ("cmpxchg2."sem" %0=[%1],%2,ar.ccv" \
141 : "=r"(_r_) : "r"(_p_), "r"(_n_) : "memory"); \
145 __asm__ __volatile__ ("cmpxchg4."sem" %0=[%1],%2,ar.ccv" \
146 : "=r"(_r_) : "r"(_p_), "r"(_n_) : "memory"); \
150 __asm__ __volatile__ ("cmpxchg8."sem" %0=[%1],%2,ar.ccv" \
151 : "=r"(_r_) : "r"(_p_), "r"(_n_) : "memory"); \
155 _r_ = __cmpxchg_called_with_bad_pointer(); \
158 (__typeof__(old)) _r_; \
161 #define cmpxchg_acq(ptr,o,n) ia64_cmpxchg("acq", (ptr), (o), (n), sizeof(*(ptr)))
162 #define cmpxchg_rel(ptr,o,n) ia64_cmpxchg("rel", (ptr), (o), (n), sizeof(*(ptr)))
164 /* for compatibility with other platforms: */
165 #define cmpxchg(ptr,o,n) cmpxchg_acq(ptr,o,n)
167 #ifdef CONFIG_IA64_DEBUG_CMPXCHG
168 # define CMPXCHG_BUGCHECK_DECL int _cmpxchg_bugcheck_count = 128;
169 # define CMPXCHG_BUGCHECK(v) \
171 if (_cmpxchg_bugcheck_count-- <= 0) { \
173 extern int printk(const char *fmt, ...); \
174 asm ("mov %0=ip" : "=r"(ip)); \
175 printk("CMPXCHG_BUGCHECK: stuck at %p on word %p\n", ip, (v)); \
179 #else /* !CONFIG_IA64_DEBUG_CMPXCHG */
180 # define CMPXCHG_BUGCHECK_DECL
181 # define CMPXCHG_BUGCHECK(v)
182 #endif /* !CONFIG_IA64_DEBUG_CMPXCHG */
184 #endif /* _ASM_IA64_INTRINSICS_H */