1 /* 2 * 1,2 and 4 byte cmpxchg and xchg implementations for OpenRISC. 3 * 4 * Copyright (C) 2014 Stefan Kristiansson <stefan.kristiansson@saunalahti.fi> 5 * Copyright (C) 2017 Stafford Horne <shorne@gmail.com> 6 * 7 * This file is licensed under the terms of the GNU General Public License 8 * version 2. This program is licensed "as is" without any warranty of any 9 * kind, whether express or implied. 10 * 11 * Note: 12 * The portable implementations of 1 and 2 byte xchg and cmpxchg using a 4 13 * byte cmpxchg is sourced heavily from the sh and mips implementations. 14 */ 15 16 #ifndef __ASM_OPENRISC_CMPXCHG_H 17 #define __ASM_OPENRISC_CMPXCHG_H 18 19 #include <linux/types.h> 20 #include <linux/bitops.h> 21 22 #define __HAVE_ARCH_CMPXCHG 1 23 24 static inline unsigned long cmpxchg_u32(volatile void *ptr, 25 unsigned long old, unsigned long new) 26 { 27 __asm__ __volatile__( 28 "1: l.lwa %0, 0(%1) \n" 29 " l.sfeq %0, %2 \n" 30 " l.bnf 2f \n" 31 " l.nop \n" 32 " l.swa 0(%1), %3 \n" 33 " l.bnf 1b \n" 34 " l.nop \n" 35 "2: \n" 36 : "=&r"(old) 37 : "r"(ptr), "r"(old), "r"(new) 38 : "cc", "memory"); 39 40 return old; 41 } 42 43 static inline unsigned long xchg_u32(volatile void *ptr, 44 unsigned long val) 45 { 46 __asm__ __volatile__( 47 "1: l.lwa %0, 0(%1) \n" 48 " l.swa 0(%1), %2 \n" 49 " l.bnf 1b \n" 50 " l.nop \n" 51 : "=&r"(val) 52 : "r"(ptr), "r"(val) 53 : "cc", "memory"); 54 55 return val; 56 } 57 58 static inline u32 cmpxchg_small(volatile void *ptr, u32 old, u32 new, 59 int size) 60 { 61 int off = (unsigned long)ptr % sizeof(u32); 62 volatile u32 *p = ptr - off; 63 #ifdef __BIG_ENDIAN 64 int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE; 65 #else 66 int bitoff = off * BITS_PER_BYTE; 67 #endif 68 u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff; 69 u32 load32, old32, new32; 70 u32 ret; 71 72 load32 = READ_ONCE(*p); 73 74 while (true) { 75 ret = (load32 & bitmask) >> bitoff; 76 if (old != ret) 77 return ret; 78 79 old32 = (load32 & ~bitmask) | (old << bitoff); 80 new32 = (load32 & ~bitmask) | (new << bitoff); 81 82 /* Do 32 bit cmpxchg */ 83 load32 = cmpxchg_u32(p, old32, new32); 84 if (load32 == old32) 85 return old; 86 } 87 } 88 89 /* xchg */ 90 91 static inline u32 xchg_small(volatile void *ptr, u32 x, int size) 92 { 93 int off = (unsigned long)ptr % sizeof(u32); 94 volatile u32 *p = ptr - off; 95 #ifdef __BIG_ENDIAN 96 int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE; 97 #else 98 int bitoff = off * BITS_PER_BYTE; 99 #endif 100 u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff; 101 u32 oldv, newv; 102 u32 ret; 103 104 do { 105 oldv = READ_ONCE(*p); 106 ret = (oldv & bitmask) >> bitoff; 107 newv = (oldv & ~bitmask) | (x << bitoff); 108 } while (cmpxchg_u32(p, oldv, newv) != oldv); 109 110 return ret; 111 } 112 113 /* 114 * This function doesn't exist, so you'll get a linker error 115 * if something tries to do an invalid cmpxchg(). 116 */ 117 extern unsigned long __cmpxchg_called_with_bad_pointer(void) 118 __compiletime_error("Bad argument size for cmpxchg"); 119 120 static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old, 121 unsigned long new, int size) 122 { 123 switch (size) { 124 case 1: 125 case 2: 126 return cmpxchg_small(ptr, old, new, size); 127 case 4: 128 return cmpxchg_u32(ptr, old, new); 129 default: 130 return __cmpxchg_called_with_bad_pointer(); 131 } 132 } 133 134 #define cmpxchg(ptr, o, n) \ 135 ({ \ 136 (__typeof__(*(ptr))) __cmpxchg((ptr), \ 137 (unsigned long)(o), \ 138 (unsigned long)(n), \ 139 sizeof(*(ptr))); \ 140 }) 141 142 /* 143 * This function doesn't exist, so you'll get a linker error if 144 * something tries to do an invalidly-sized xchg(). 145 */ 146 extern unsigned long __xchg_called_with_bad_pointer(void) 147 __compiletime_error("Bad argument size for xchg"); 148 149 static inline unsigned long __xchg(volatile void *ptr, unsigned long with, 150 int size) 151 { 152 switch (size) { 153 case 1: 154 case 2: 155 return xchg_small(ptr, with, size); 156 case 4: 157 return xchg_u32(ptr, with); 158 default: 159 return __xchg_called_with_bad_pointer(); 160 } 161 } 162 163 #define xchg(ptr, with) \ 164 ({ \ 165 (__typeof__(*(ptr))) __xchg((ptr), \ 166 (unsigned long)(with), \ 167 sizeof(*(ptr))); \ 168 }) 169 170 #endif /* __ASM_OPENRISC_CMPXCHG_H */ 171