s390: uatomic add missing xchg return
[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 */
f64acda4
MD
51
52static inline __attribute__((always_inline))
ec4e58a3 53unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len)
ac26f1a8
JB
54{
55 switch (len) {
56 case 4:
2837ec40 57 {
9c697e4d
MD
58 unsigned int old_val;
59
60 __asm__ __volatile__(
61 "0: cs %0,%2,%1\n"
62 " brc 4,0b\n"
63 : "=&r"(old_val), "=m" (*addr)
64 : "r"(val), "m" (*addr)
65 : "memory", "cc");
1cf421dc 66 return old_val;
2837ec40 67 }
ac26f1a8
JB
68#if (BITS_PER_LONG == 64)
69 case 8:
2837ec40 70 {
9c697e4d
MD
71 unsigned long old_val;
72
73 __asm__ __volatile__(
74 "0: csg %0,%2,%1\n"
75 " brc 4,0b\n"
76 : "=&r"(old_val), "=m" (*addr)
77 : "r"(val), "m" (*addr)
78 : "memory", "cc");
1cf421dc 79 return old_val;
2837ec40 80 }
ac26f1a8
JB
81#endif
82 default:
83 __asm__ __volatile__(".long 0xd00d00");
84 }
85
86 return 0;
87}
88
9c697e4d 89#define uatomic_xchg(addr, v) \
ec4e58a3 90 (__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
a9a05d42 91 sizeof(*(addr)))
ac26f1a8 92
9c697e4d 93/* cmpxchg */
8af57509
JB
94
95static inline __attribute__((always_inline))
9c697e4d 96unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
1cf421dc 97 unsigned long _new, int len)
8af57509
JB
98{
99 switch (len) {
100 case 4:
2837ec40 101 {
9c697e4d
MD
102 unsigned int old_val = (unsigned int)old;
103
104 __asm__ __volatile__(
105 " cs %0,%2,%1\n"
106 : "+r"(old_val), "+m"(*addr)
1cf421dc 107 : "r"(_new)
9c697e4d
MD
108 : "memory", "cc");
109 return old_val;
2837ec40 110 }
8af57509
JB
111#if (BITS_PER_LONG == 64)
112 case 8:
f64acda4 113 {
9c697e4d
MD
114 __asm__ __volatile__(
115 " csg %0,%2,%1\n"
116 : "+r"(old), "+m"(*addr)
1cf421dc 117 : "r"(_new)
9c697e4d
MD
118 : "memory", "cc");
119 return old;
f64acda4 120 }
8af57509
JB
121#endif
122 default:
123 __asm__ __volatile__(".long 0xd00d00");
124 }
125
9c697e4d 126 return 0;
8af57509
JB
127}
128
1cf421dc 129#define uatomic_cmpxchg(addr, old, _new) \
9c697e4d
MD
130 (__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
131 (unsigned long)(old), \
1cf421dc 132 (unsigned long)(_new), \
9c697e4d 133 sizeof(*(addr)))
8af57509 134
9c697e4d 135/* uatomic_add_return */
8af57509
JB
136
137static inline __attribute__((always_inline))
9c697e4d 138unsigned long _uatomic_add_return(void *addr, unsigned long val, int len)
8af57509
JB
139{
140 switch (len) {
141 case 4:
9c697e4d
MD
142 {
143 unsigned int old, oldt;
144
145 oldt = uatomic_read((unsigned int *)addr);
146 do {
147 old = oldt;
148 oldt = _uatomic_cmpxchg(addr, old, old + val, 4);
149 } while (oldt != old);
150
151 return old + val;
152 }
8af57509
JB
153#if (BITS_PER_LONG == 64)
154 case 8:
9c697e4d
MD
155 {
156 unsigned long old, oldt;
157
158 oldt = uatomic_read((unsigned long *)addr);
159 do {
160 old = oldt;
161 oldt = _uatomic_cmpxchg(addr, old, old + val, 8);
162 } while (oldt != old);
163
164 return old + val;
165 }
8af57509 166#endif
8af57509 167 }
9c697e4d 168 __builtin_trap();
8af57509
JB
169 return 0;
170}
171
9c697e4d
MD
172#define uatomic_add_return(addr, v) \
173 ((__typeof__(*(addr))) _uatomic_add_return((addr), \
174 (unsigned long)(v), \
175 sizeof(*(addr))))
176
177/* uatomic_sub_return, uatomic_add, uatomic_sub, uatomic_inc, uatomic_dec */
178
179#define uatomic_sub_return(addr, v) uatomic_add_return((addr), -(v))
180
181#define uatomic_add(addr, v) (void)uatomic_add_return((addr), (v))
182#define uatomic_sub(addr, v) (void)uatomic_sub_return((addr), (v))
183
184#define uatomic_inc(addr) uatomic_add((addr), 1)
185#define uatomic_dec(addr) uatomic_add((addr), -1)
8af57509 186
9c697e4d 187#define compat_uatomic_cmpxchg(ptr, old, _new) uatomic_cmpxchg(ptr, old, _new)
7d413817 188
9c697e4d 189#endif /* _URCU_UATOMIC_ARCH_S390_H */
This page took 0.031385 seconds and 4 git commands to generate.