1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 4 */ 5 6 #ifndef __ASM_ARC_CMPXCHG_H 7 #define __ASM_ARC_CMPXCHG_H 8 9 #include <linux/build_bug.h> 10 #include <linux/types.h> 11 #include <linux/cmpxchg-emu.h> 12 13 #include <asm/barrier.h> 14 #include <asm/smp.h> 15 16 #ifdef CONFIG_ARC_HAS_LLSC 17 18 /* 19 * if (*ptr == @old) 20 * *ptr = @new 21 */ 22 #define __cmpxchg(ptr, old, new) \ 23 ({ \ 24 __typeof__(*(ptr)) _prev; \ 25 \ 26 __asm__ __volatile__( \ 27 "1: llock %0, [%1] \n" \ 28 " brne %0, %2, 2f \n" \ 29 " scond %3, [%1] \n" \ 30 " bnz 1b \n" \ 31 "2: \n" \ 32 : "=&r"(_prev) /* Early clobber prevent reg reuse */ \ 33 : "r"(ptr), /* Not "m": llock only supports reg */ \ 34 "ir"(old), \ 35 "r"(new) /* Not "ir": scond can't take LIMM */ \ 36 : "cc", \ 37 "memory"); /* gcc knows memory is clobbered */ \ 38 \ 39 _prev; \ 40 }) 41 42 #define arch_cmpxchg_relaxed(ptr, old, new) \ 43 ({ \ 44 __typeof__(ptr) _p_ = (ptr); \ 45 __typeof__(*(ptr)) _o_ = (old); \ 46 __typeof__(*(ptr)) _n_ = (new); \ 47 __typeof__(*(ptr)) _prev_; \ 48 \ 49 switch (sizeof(*(_p_))) { \ 50 case 1: \ 51 _prev_ = (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *__force)_p_, (uintptr_t)_o_, (uintptr_t)_n_); \ 52 break; \ 53 case 2: \ 54 _prev_ = (__typeof__(*(ptr)))cmpxchg_emu_u16((volatile u16 *__force)_p_, (unsigned long)_o_, (unsigned long)_n_); \ 55 break; \ 56 case 4: \ 57 _prev_ = __cmpxchg(_p_, _o_, _n_); \ 58 break; \ 59 default: \ 60 BUILD_BUG(); \ 61 } \ 62 _prev_; \ 63 }) 64 65 #define arch_cmpxchg_local arch_cmpxchg_relaxed 66 67 #else 68 69 #define arch_cmpxchg(ptr, old, new) \ 70 ({ \ 71 volatile __typeof__(ptr) _p_ = (ptr); \ 72 __typeof__(*(ptr)) _o_ = (old); \ 73 __typeof__(*(ptr)) _n_ = (new); \ 74 __typeof__(*(ptr)) _prev_; \ 75 unsigned long __flags; \ 76 \ 77 /* \ 78 * spin lock/unlock provide the needed smp_mb() before/after \ 79 */ \ 80 atomic_ops_lock(__flags); \ 81 _prev_ = *_p_; \ 82 if (_prev_ == _o_) \ 83 *_p_ = _n_; \ 84 atomic_ops_unlock(__flags); \ 85 _prev_; \ 86 }) 87 88 #define arch_cmpxchg_local arch_cmpxchg 89 90 #endif 91 92 /* 93 * xchg 94 */ 95 #ifdef CONFIG_ARC_HAS_LLSC 96 97 #define __arch_xchg(ptr, val) \ 98 ({ \ 99 __asm__ __volatile__( \ 100 " ex %0, [%1] \n" /* set new value */ \ 101 : "+r"(val) \ 102 : "r"(ptr) \ 103 : "memory"); \ 104 _val_; /* get old value */ \ 105 }) 106 107 #define arch_xchg_relaxed(ptr, val) \ 108 ({ \ 109 __typeof__(ptr) _p_ = (ptr); \ 110 __typeof__(*(ptr)) _val_ = (val); \ 111 \ 112 switch (sizeof(*(_p_))) { \ 113 case 4: \ 114 _val_ = __arch_xchg(_p_, _val_); \ 115 break; \ 116 default: \ 117 BUILD_BUG(); \ 118 } \ 119 _val_; \ 120 }) 121 122 #else /* !CONFIG_ARC_HAS_LLSC */ 123 124 /* 125 * EX instructions is baseline and present in !LLSC too. But in this 126 * regime it still needs use @atomic_ops_lock spinlock to allow interop 127 * with cmpxchg() which uses spinlock in !LLSC 128 * (llist.h use xchg and cmpxchg on sama data) 129 */ 130 131 #define arch_xchg(ptr, val) \ 132 ({ \ 133 __typeof__(ptr) _p_ = (ptr); \ 134 __typeof__(*(ptr)) _val_ = (val); \ 135 \ 136 unsigned long __flags; \ 137 \ 138 atomic_ops_lock(__flags); \ 139 \ 140 __asm__ __volatile__( \ 141 " ex %0, [%1] \n" \ 142 : "+r"(_val_) \ 143 : "r"(_p_) \ 144 : "memory"); \ 145 \ 146 atomic_ops_unlock(__flags); \ 147 _val_; \ 148 }) 149 150 #endif 151 152 #endif 153