xref: /openbmc/linux/arch/sh/include/asm/cmpxchg-xchg.h (revision 4f205687)
1 #ifndef __ASM_SH_CMPXCHG_XCHG_H
2 #define __ASM_SH_CMPXCHG_XCHG_H
3 
4 /*
5  * Copyright (C) 2016 Red Hat, Inc.
6  * Author: Michael S. Tsirkin <mst@redhat.com>
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2.  See the
9  * file "COPYING" in the main directory of this archive for more details.
10  */
11 #include <linux/bitops.h>
12 #include <asm/byteorder.h>
13 
14 /*
15  * Portable implementations of 1 and 2 byte xchg using a 4 byte cmpxchg.
16  * Note: this header isn't self-contained: before including it, __cmpxchg_u32
17  * must be defined first.
18  */
19 static inline u32 __xchg_cmpxchg(volatile void *ptr, u32 x, int size)
20 {
21 	int off = (unsigned long)ptr % sizeof(u32);
22 	volatile u32 *p = ptr - off;
23 #ifdef __BIG_ENDIAN
24 	int bitoff = (sizeof(u32) - 1 - off) * BITS_PER_BYTE;
25 #else
26 	int bitoff = off * BITS_PER_BYTE;
27 #endif
28 	u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
29 	u32 oldv, newv;
30 	u32 ret;
31 
32 	do {
33 		oldv = READ_ONCE(*p);
34 		ret = (oldv & bitmask) >> bitoff;
35 		newv = (oldv & ~bitmask) | (x << bitoff);
36 	} while (__cmpxchg_u32(p, oldv, newv) != oldv);
37 
38 	return ret;
39 }
40 
41 static inline unsigned long xchg_u16(volatile u16 *m, unsigned long val)
42 {
43 	return __xchg_cmpxchg(m, val, sizeof *m);
44 }
45 
46 static inline unsigned long xchg_u8(volatile u8 *m, unsigned long val)
47 {
48 	return __xchg_cmpxchg(m, val, sizeof *m);
49 }
50 
51 #endif /* __ASM_SH_CMPXCHG_XCHG_H */
52