Commit | Line | Data |
---|---|---|
ec4e58a3 MD |
1 | #ifndef _URCU_ARCH_ATOMIC_S390_H |
2 | #define _URCU_ARCH_ATOMIC_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 JB |
10 | * Author: Jan Blunck <jblunck@suse.de> |
11 | * | |
7039fa6f JB |
12 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
13 | * of this software and associated documentation files (the "Software"), to | |
14 | * deal in the Software without restriction, including without limitation the | |
15 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
16 | * sell copies of the Software, and to permit persons to whom the Software is | |
17 | * furnished to do so, subject to the following conditions: | |
ac26f1a8 | 18 | * |
7039fa6f JB |
19 | * The above copyright notice and this permission notice shall be included in |
20 | * all copies or substantial portions of the Software. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
28 | * IN THE SOFTWARE. | |
ac26f1a8 JB |
29 | */ |
30 | ||
31 | #ifndef __SIZEOF_LONG__ | |
32 | #ifdef __s390x__ | |
33 | #define __SIZEOF_LONG__ 8 | |
34 | #else | |
35 | #define __SIZEOF_LONG__ 4 | |
36 | #endif | |
37 | #endif | |
38 | ||
39 | #ifndef BITS_PER_LONG | |
40 | #define BITS_PER_LONG (__SIZEOF_LONG__ * 8) | |
41 | #endif | |
42 | ||
a9a05d42 | 43 | static inline __attribute__((always_inline)) |
ec4e58a3 | 44 | unsigned int uatomic_exchange_32(volatile unsigned int *addr, unsigned int val) |
ac26f1a8 JB |
45 | { |
46 | unsigned int result; | |
47 | ||
48 | __asm__ __volatile__( | |
49 | "0: cs %0,%2,%1\n" | |
50 | " brc 4,0b\n" | |
51 | : "=&r"(result), "=m" (*addr) | |
52 | : "r"(val), "m" (*addr) | |
53 | : "memory", "cc"); | |
54 | ||
55 | return result; | |
56 | } | |
57 | ||
58 | #if (BITS_PER_LONG == 64) | |
59 | ||
a9a05d42 | 60 | static inline __attribute__((always_inline)) |
ec4e58a3 | 61 | unsigned long uatomic_exchange_64(volatile unsigned long *addr, |
ac26f1a8 JB |
62 | unsigned long val) |
63 | { | |
64 | unsigned long result; | |
65 | ||
66 | __asm__ __volatile__( | |
67 | "0: csg %0,%2,%1\n" | |
68 | " brc 4,0b\n" | |
69 | : "=&r"(result), "=m" (*addr) | |
70 | : "r"(val), "m" (*addr) | |
71 | : "memory", "cc"); | |
72 | ||
73 | return result; | |
74 | } | |
75 | ||
76 | #endif | |
77 | ||
a9a05d42 | 78 | static inline __attribute__((always_inline)) |
ec4e58a3 | 79 | unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len) |
ac26f1a8 JB |
80 | { |
81 | switch (len) { | |
82 | case 4: | |
ec4e58a3 | 83 | return uatomic_exchange_32(addr, val); |
ac26f1a8 JB |
84 | #if (BITS_PER_LONG == 64) |
85 | case 8: | |
ec4e58a3 | 86 | return uatomic_exchange_64(addr, val); |
ac26f1a8 JB |
87 | #endif |
88 | default: | |
89 | __asm__ __volatile__(".long 0xd00d00"); | |
90 | } | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
ec4e58a3 MD |
95 | #define uatomic_xchg(addr, v) \ |
96 | (__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \ | |
a9a05d42 | 97 | sizeof(*(addr))) |
ac26f1a8 | 98 | |
ec4e58a3 | 99 | #endif /* _URCU_ARCH_ATOMIC_S390_H */ |