| 1 | #ifndef _ARCH_ATOMIC_S390_H |
| 2 | #define _ARCH_ATOMIC_S390_H |
| 3 | |
| 4 | /* |
| 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)". |
| 8 | * |
| 9 | * Copyright (c) 2009 Novell, Inc. |
| 10 | * Author: Jan Blunck <jblunck@suse.de> |
| 11 | * |
| 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: |
| 18 | * |
| 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. |
| 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 | |
| 43 | #ifndef _INCLUDE_API_H |
| 44 | |
| 45 | static inline __attribute__((always_inline)) |
| 46 | unsigned int atomic_exchange_32(volatile unsigned int *addr, unsigned int val) |
| 47 | { |
| 48 | unsigned int result; |
| 49 | |
| 50 | __asm__ __volatile__( |
| 51 | "0: cs %0,%2,%1\n" |
| 52 | " brc 4,0b\n" |
| 53 | : "=&r"(result), "=m" (*addr) |
| 54 | : "r"(val), "m" (*addr) |
| 55 | : "memory", "cc"); |
| 56 | |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | #if (BITS_PER_LONG == 64) |
| 61 | |
| 62 | static inline __attribute__((always_inline)) |
| 63 | unsigned long atomic_exchange_64(volatile unsigned long *addr, |
| 64 | unsigned long val) |
| 65 | { |
| 66 | unsigned long result; |
| 67 | |
| 68 | __asm__ __volatile__( |
| 69 | "0: csg %0,%2,%1\n" |
| 70 | " brc 4,0b\n" |
| 71 | : "=&r"(result), "=m" (*addr) |
| 72 | : "r"(val), "m" (*addr) |
| 73 | : "memory", "cc"); |
| 74 | |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | #endif |
| 79 | |
| 80 | static inline __attribute__((always_inline)) |
| 81 | unsigned long _atomic_exchange(volatile void *addr, unsigned long val, int len) |
| 82 | { |
| 83 | switch (len) { |
| 84 | case 4: |
| 85 | return atomic_exchange_32(addr, val); |
| 86 | #if (BITS_PER_LONG == 64) |
| 87 | case 8: |
| 88 | return atomic_exchange_64(addr, val); |
| 89 | #endif |
| 90 | default: |
| 91 | __asm__ __volatile__(".long 0xd00d00"); |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | #define xchg(addr, v) \ |
| 98 | (__typeof__(*(addr))) _atomic_exchange((addr), (unsigned long)(v), \ |
| 99 | sizeof(*(addr))) |
| 100 | |
| 101 | #endif /* #ifndef _INCLUDE_API_H */ |
| 102 | |
| 103 | #endif /* ARCH_ATOMIC_S390_H */ |