sparc,ppc,s390: uatomic ops update
[urcu.git] / urcu / uatomic_arch_s390.h
CommitLineData
9c697e4d
MD
1#ifndef _URCU_UATOMIC_ARCH_S390_H
2#define _URCU_UATOMIC_ARCH_S390_H
ac26f1a8
JB
3
4/*
7039fa6f
JB
5 * Atomic exchange operations for the S390 architecture. Based on information
6 * taken from the Principles of Operation Appendix A "Conditional Swapping
7 * Instructions (CS, CDS)".
ac26f1a8 8 *
7039fa6f 9 * Copyright (c) 2009 Novell, Inc.
ac26f1a8 10 * Author: Jan Blunck <jblunck@suse.de>
9c697e4d 11 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
ac26f1a8 12 *
7039fa6f
JB
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to
15 * deal in the Software without restriction, including without limitation the
16 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
17 * sell copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
ac26f1a8 19 *
7039fa6f
JB
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the 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 THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
ac26f1a8
JB
30 */
31
b46b23cb
MD
32#include <urcu/compiler.h>
33#include <urcu/system.h>
34
ac26f1a8
JB
35#ifndef __SIZEOF_LONG__
36#ifdef __s390x__
37#define __SIZEOF_LONG__ 8
38#else
39#define __SIZEOF_LONG__ 4
40#endif
41#endif
42
43#ifndef BITS_PER_LONG
44#define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
45#endif
46
b46b23cb
MD
47#define uatomic_set(addr, v) STORE_SHARED(*(addr), (v))
48#define uatomic_read(addr) LOAD_SHARED(*(addr))
8af57509 49
9c697e4d 50/* xchg */
ec4e58a3 51unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len)
ac26f1a8
JB
52{
53 switch (len) {
54 case 4:
9c697e4d
MD
55 unsigned int old_val;
56
57 __asm__ __volatile__(
58 "0: cs %0,%2,%1\n"
59 " brc 4,0b\n"
60 : "=&r"(old_val), "=m" (*addr)
61 : "r"(val), "m" (*addr)
62 : "memory", "cc");
ac26f1a8
JB
63#if (BITS_PER_LONG == 64)
64 case 8:
9c697e4d
MD
65 unsigned long old_val;
66
67 __asm__ __volatile__(
68 "0: csg %0,%2,%1\n"
69 " brc 4,0b\n"
70 : "=&r"(old_val), "=m" (*addr)
71 : "r"(val), "m" (*addr)
72 : "memory", "cc");
ac26f1a8
JB
73#endif
74 default:
75 __asm__ __volatile__(".long 0xd00d00");
76 }
77
78 return 0;
79}
80
9c697e4d 81#define uatomic_xchg(addr, v) \
ec4e58a3 82 (__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
a9a05d42 83 sizeof(*(addr)))
ac26f1a8 84
9c697e4d 85/* cmpxchg */
8af57509
JB
86
87static inline __attribute__((always_inline))
9c697e4d
MD
88unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
89 unsigned long new, int len)
8af57509
JB
90{
91 switch (len) {
92 case 4:
9c697e4d
MD
93 unsigned int old_val = (unsigned int)old;
94
95 __asm__ __volatile__(
96 " cs %0,%2,%1\n"
97 : "+r"(old_val), "+m"(*addr)
98 : "r"(new)
99 : "memory", "cc");
100 return old_val;
8af57509
JB
101#if (BITS_PER_LONG == 64)
102 case 8:
9c697e4d
MD
103 __asm__ __volatile__(
104 " csg %0,%2,%1\n"
105 : "+r"(old), "+m"(*addr)
106 : "r"(new)
107 : "memory", "cc");
108 return old;
8af57509
JB
109#endif
110 default:
111 __asm__ __volatile__(".long 0xd00d00");
112 }
113
9c697e4d 114 return 0;
8af57509
JB
115}
116
9c697e4d
MD
117#define uatomic_cmpxchg(addr, old, new) \
118 (__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
119 (unsigned long)(old), \
120 (unsigned long)(new), \
121 sizeof(*(addr)))
8af57509 122
9c697e4d 123/* uatomic_add_return */
8af57509
JB
124
125static inline __attribute__((always_inline))
9c697e4d 126unsigned long _uatomic_add_return(void *addr, unsigned long val, int len)
8af57509
JB
127{
128 switch (len) {
129 case 4:
9c697e4d
MD
130 {
131 unsigned int old, oldt;
132
133 oldt = uatomic_read((unsigned int *)addr);
134 do {
135 old = oldt;
136 oldt = _uatomic_cmpxchg(addr, old, old + val, 4);
137 } while (oldt != old);
138
139 return old + val;
140 }
8af57509
JB
141#if (BITS_PER_LONG == 64)
142 case 8:
9c697e4d
MD
143 {
144 unsigned long old, oldt;
145
146 oldt = uatomic_read((unsigned long *)addr);
147 do {
148 old = oldt;
149 oldt = _uatomic_cmpxchg(addr, old, old + val, 8);
150 } while (oldt != old);
151
152 return old + val;
153 }
8af57509 154#endif
8af57509 155 }
9c697e4d 156 __builtin_trap();
8af57509
JB
157 return 0;
158}
159
9c697e4d
MD
160#define uatomic_add_return(addr, v) \
161 ((__typeof__(*(addr))) _uatomic_add_return((addr), \
162 (unsigned long)(v), \
163 sizeof(*(addr))))
164
165/* uatomic_sub_return, uatomic_add, uatomic_sub, uatomic_inc, uatomic_dec */
166
167#define uatomic_sub_return(addr, v) uatomic_add_return((addr), -(v))
168
169#define uatomic_add(addr, v) (void)uatomic_add_return((addr), (v))
170#define uatomic_sub(addr, v) (void)uatomic_sub_return((addr), (v))
171
172#define uatomic_inc(addr) uatomic_add((addr), 1)
173#define uatomic_dec(addr) uatomic_add((addr), -1)
8af57509 174
9c697e4d 175#define compat_uatomic_cmpxchg(ptr, old, _new) uatomic_cmpxchg(ptr, old, _new)
7d413817 176
9c697e4d 177#endif /* _URCU_UATOMIC_ARCH_S390_H */
This page took 0.030425 seconds and 4 git commands to generate.