18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 64-bit atomic xchg() and cmpxchg() definitions. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 1996, 1997, 2000 David S. Miller (davem@redhat.com) 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#ifndef __ARCH_SPARC64_CMPXCHG__ 88c2ecf20Sopenharmony_ci#define __ARCH_SPARC64_CMPXCHG__ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_cistatic inline unsigned long 118c2ecf20Sopenharmony_ci__cmpxchg_u32(volatile int *m, int old, int new) 128c2ecf20Sopenharmony_ci{ 138c2ecf20Sopenharmony_ci __asm__ __volatile__("cas [%2], %3, %0" 148c2ecf20Sopenharmony_ci : "=&r" (new) 158c2ecf20Sopenharmony_ci : "0" (new), "r" (m), "r" (old) 168c2ecf20Sopenharmony_ci : "memory"); 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci return new; 198c2ecf20Sopenharmony_ci} 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic inline unsigned long xchg32(__volatile__ unsigned int *m, unsigned int val) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci unsigned long tmp1, tmp2; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci __asm__ __volatile__( 268c2ecf20Sopenharmony_ci" mov %0, %1\n" 278c2ecf20Sopenharmony_ci"1: lduw [%4], %2\n" 288c2ecf20Sopenharmony_ci" cas [%4], %2, %0\n" 298c2ecf20Sopenharmony_ci" cmp %2, %0\n" 308c2ecf20Sopenharmony_ci" bne,a,pn %%icc, 1b\n" 318c2ecf20Sopenharmony_ci" mov %1, %0\n" 328c2ecf20Sopenharmony_ci : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2) 338c2ecf20Sopenharmony_ci : "0" (val), "r" (m) 348c2ecf20Sopenharmony_ci : "cc", "memory"); 358c2ecf20Sopenharmony_ci return val; 368c2ecf20Sopenharmony_ci} 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic inline unsigned long xchg64(__volatile__ unsigned long *m, unsigned long val) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci unsigned long tmp1, tmp2; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci __asm__ __volatile__( 438c2ecf20Sopenharmony_ci" mov %0, %1\n" 448c2ecf20Sopenharmony_ci"1: ldx [%4], %2\n" 458c2ecf20Sopenharmony_ci" casx [%4], %2, %0\n" 468c2ecf20Sopenharmony_ci" cmp %2, %0\n" 478c2ecf20Sopenharmony_ci" bne,a,pn %%xcc, 1b\n" 488c2ecf20Sopenharmony_ci" mov %1, %0\n" 498c2ecf20Sopenharmony_ci : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2) 508c2ecf20Sopenharmony_ci : "0" (val), "r" (m) 518c2ecf20Sopenharmony_ci : "cc", "memory"); 528c2ecf20Sopenharmony_ci return val; 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define xchg(ptr,x) \ 568c2ecf20Sopenharmony_ci({ __typeof__(*(ptr)) __ret; \ 578c2ecf20Sopenharmony_ci __ret = (__typeof__(*(ptr))) \ 588c2ecf20Sopenharmony_ci __xchg((unsigned long)(x), (ptr), sizeof(*(ptr))); \ 598c2ecf20Sopenharmony_ci __ret; \ 608c2ecf20Sopenharmony_ci}) 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_civoid __xchg_called_with_bad_pointer(void); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/* 658c2ecf20Sopenharmony_ci * Use 4 byte cas instruction to achieve 2 byte xchg. Main logic 668c2ecf20Sopenharmony_ci * here is to get the bit shift of the byte we are interested in. 678c2ecf20Sopenharmony_ci * The XOR is handy for reversing the bits for big-endian byte order. 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_cistatic inline unsigned long 708c2ecf20Sopenharmony_cixchg16(__volatile__ unsigned short *m, unsigned short val) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci unsigned long maddr = (unsigned long)m; 738c2ecf20Sopenharmony_ci int bit_shift = (((unsigned long)m & 2) ^ 2) << 3; 748c2ecf20Sopenharmony_ci unsigned int mask = 0xffff << bit_shift; 758c2ecf20Sopenharmony_ci unsigned int *ptr = (unsigned int *) (maddr & ~2); 768c2ecf20Sopenharmony_ci unsigned int old32, new32, load32; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci /* Read the old value */ 798c2ecf20Sopenharmony_ci load32 = *ptr; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci do { 828c2ecf20Sopenharmony_ci old32 = load32; 838c2ecf20Sopenharmony_ci new32 = (load32 & (~mask)) | val << bit_shift; 848c2ecf20Sopenharmony_ci load32 = __cmpxchg_u32(ptr, old32, new32); 858c2ecf20Sopenharmony_ci } while (load32 != old32); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci return (load32 & mask) >> bit_shift; 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistatic inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr, 918c2ecf20Sopenharmony_ci int size) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci switch (size) { 948c2ecf20Sopenharmony_ci case 2: 958c2ecf20Sopenharmony_ci return xchg16(ptr, x); 968c2ecf20Sopenharmony_ci case 4: 978c2ecf20Sopenharmony_ci return xchg32(ptr, x); 988c2ecf20Sopenharmony_ci case 8: 998c2ecf20Sopenharmony_ci return xchg64(ptr, x); 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci __xchg_called_with_bad_pointer(); 1028c2ecf20Sopenharmony_ci return x; 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci/* 1068c2ecf20Sopenharmony_ci * Atomic compare and exchange. Compare OLD with MEM, if identical, 1078c2ecf20Sopenharmony_ci * store NEW in MEM. Return the initial value in MEM. Success is 1088c2ecf20Sopenharmony_ci * indicated by comparing RETURN with OLD. 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci#include <asm-generic/cmpxchg-local.h> 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic inline unsigned long 1158c2ecf20Sopenharmony_ci__cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new) 1168c2ecf20Sopenharmony_ci{ 1178c2ecf20Sopenharmony_ci __asm__ __volatile__("casx [%2], %3, %0" 1188c2ecf20Sopenharmony_ci : "=&r" (new) 1198c2ecf20Sopenharmony_ci : "0" (new), "r" (m), "r" (old) 1208c2ecf20Sopenharmony_ci : "memory"); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci return new; 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci/* 1268c2ecf20Sopenharmony_ci * Use 4 byte cas instruction to achieve 1 byte cmpxchg. Main logic 1278c2ecf20Sopenharmony_ci * here is to get the bit shift of the byte we are interested in. 1288c2ecf20Sopenharmony_ci * The XOR is handy for reversing the bits for big-endian byte order 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_cistatic inline unsigned long 1318c2ecf20Sopenharmony_ci__cmpxchg_u8(volatile unsigned char *m, unsigned char old, unsigned char new) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci unsigned long maddr = (unsigned long)m; 1348c2ecf20Sopenharmony_ci int bit_shift = (((unsigned long)m & 3) ^ 3) << 3; 1358c2ecf20Sopenharmony_ci unsigned int mask = 0xff << bit_shift; 1368c2ecf20Sopenharmony_ci unsigned int *ptr = (unsigned int *) (maddr & ~3); 1378c2ecf20Sopenharmony_ci unsigned int old32, new32, load; 1388c2ecf20Sopenharmony_ci unsigned int load32 = *ptr; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci do { 1418c2ecf20Sopenharmony_ci new32 = (load32 & ~mask) | (new << bit_shift); 1428c2ecf20Sopenharmony_ci old32 = (load32 & ~mask) | (old << bit_shift); 1438c2ecf20Sopenharmony_ci load32 = __cmpxchg_u32(ptr, old32, new32); 1448c2ecf20Sopenharmony_ci if (load32 == old32) 1458c2ecf20Sopenharmony_ci return old; 1468c2ecf20Sopenharmony_ci load = (load32 & mask) >> bit_shift; 1478c2ecf20Sopenharmony_ci } while (load == old); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci return load; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci/* This function doesn't exist, so you'll get a linker error 1538c2ecf20Sopenharmony_ci if something tries to do an invalid cmpxchg(). */ 1548c2ecf20Sopenharmony_civoid __cmpxchg_called_with_bad_pointer(void); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic inline unsigned long 1578c2ecf20Sopenharmony_ci__cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci switch (size) { 1608c2ecf20Sopenharmony_ci case 1: 1618c2ecf20Sopenharmony_ci return __cmpxchg_u8(ptr, old, new); 1628c2ecf20Sopenharmony_ci case 4: 1638c2ecf20Sopenharmony_ci return __cmpxchg_u32(ptr, old, new); 1648c2ecf20Sopenharmony_ci case 8: 1658c2ecf20Sopenharmony_ci return __cmpxchg_u64(ptr, old, new); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci __cmpxchg_called_with_bad_pointer(); 1688c2ecf20Sopenharmony_ci return old; 1698c2ecf20Sopenharmony_ci} 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci#define cmpxchg(ptr,o,n) \ 1728c2ecf20Sopenharmony_ci ({ \ 1738c2ecf20Sopenharmony_ci __typeof__(*(ptr)) _o_ = (o); \ 1748c2ecf20Sopenharmony_ci __typeof__(*(ptr)) _n_ = (n); \ 1758c2ecf20Sopenharmony_ci (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ 1768c2ecf20Sopenharmony_ci (unsigned long)_n_, sizeof(*(ptr))); \ 1778c2ecf20Sopenharmony_ci }) 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci/* 1808c2ecf20Sopenharmony_ci * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make 1818c2ecf20Sopenharmony_ci * them available. 1828c2ecf20Sopenharmony_ci */ 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic inline unsigned long __cmpxchg_local(volatile void *ptr, 1858c2ecf20Sopenharmony_ci unsigned long old, 1868c2ecf20Sopenharmony_ci unsigned long new, int size) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci switch (size) { 1898c2ecf20Sopenharmony_ci case 4: 1908c2ecf20Sopenharmony_ci case 8: return __cmpxchg(ptr, old, new, size); 1918c2ecf20Sopenharmony_ci default: 1928c2ecf20Sopenharmony_ci return __cmpxchg_local_generic(ptr, old, new, size); 1938c2ecf20Sopenharmony_ci } 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci return old; 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci#define cmpxchg_local(ptr, o, n) \ 1998c2ecf20Sopenharmony_ci ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \ 2008c2ecf20Sopenharmony_ci (unsigned long)(n), sizeof(*(ptr)))) 2018c2ecf20Sopenharmony_ci#define cmpxchg64_local(ptr, o, n) \ 2028c2ecf20Sopenharmony_ci ({ \ 2038c2ecf20Sopenharmony_ci BUILD_BUG_ON(sizeof(*(ptr)) != 8); \ 2048c2ecf20Sopenharmony_ci cmpxchg_local((ptr), (o), (n)); \ 2058c2ecf20Sopenharmony_ci }) 2068c2ecf20Sopenharmony_ci#define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n)) 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci#endif /* __ARCH_SPARC64_CMPXCHG__ */ 209