aboutsummaryrefslogtreecommitdiffstats
path: root/arch/nios2/include/asm/cmpxchg.h
blob: 85938711542d89182369105c670ca785ad3f3fb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Copyright (C) 2004 Microtronix Datacom Ltd.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#ifndef _ASM_NIOS2_CMPXCHG_H
#define _ASM_NIOS2_CMPXCHG_H

#include <linux/irqflags.h>

#define xchg(ptr, x)	\
	((__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr), sizeof(*(ptr))))

struct __xchg_dummy { unsigned long a[100]; };
#define __xg(x)		((volatile struct __xchg_dummy *)(x))

static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
					int size)
{
	unsigned long tmp, flags;

	local_irq_save(flags);

	switch (size) {
	case 1:
		__asm__ __volatile__(
			"ldb	%0, %2\n"
			"stb	%1, %2\n"
			: "=&r" (tmp)
			: "r" (x), "m" (*__xg(ptr))
			: "memory");
		break;
	case 2:
		__asm__ __volatile__(
			"ldh	%0, %2\n"
			"sth	%1, %2\n"
			: "=&r" (tmp)
			: "r" (x), "m" (*__xg(ptr))
			: "memory");
		break;
	case 4:
		__asm__ __volatile__(
			"ldw	%0, %2\n"
			"stw	%1, %2\n"
			: "=&r" (tmp)
			: "r" (x), "m" (*__xg(ptr))
			: "memory");
		break;
	}

	local_irq_restore(flags);
	return tmp;
}

#include <asm-generic/cmpxchg.h>
#include <asm-generic/cmpxchg-local.h>

#endif /* _ASM_NIOS2_CMPXCHG_H */