summaryrefslogtreecommitdiff
path: root/src/arch/x86/include/arch/smp/atomic.h
blob: b12da12e54ee4ee9b08f661158b3ffcaea0ebeed (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * This file is part of the coreboot project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifndef ARCH_SMP_ATOMIC_H
#define ARCH_SMP_ATOMIC_H

#include <compiler.h>

/*
 * Make sure gcc doesn't try to be clever and move things around
 * on us. We need to use _exactly_ the address the user gave us,
 * not some alias that contains the same information.
 */
typedef struct { volatile int counter; } atomic_t;

#define ATOMIC_INIT(i)	{ (i) }

/** @file x86/include/arch/smp/atomic.h
 *
 * Atomic operations that C can't guarantee us.  Useful for
 * resource counting etc..
 */

/**
 * atomic_read - read atomic variable
 * @param v: pointer of type atomic_t
 *
 * Atomically reads the value of v.  Note that the guaranteed
 * useful range of an atomic_t is only 24 bits.
 */
#define atomic_read(v)		((v)->counter)

/**
 * atomic_set - set atomic variable
 * @param v: pointer of type atomic_t
 * @param i: required value
 *
 * Atomically sets the value of v to i.  Note that the guaranteed
 * useful range of an atomic_t is only 24 bits.
 */
#define atomic_set(v, i)	(((v)->counter) = (i))

/**
 * atomic_inc - increment atomic variable
 * @param v: pointer of type atomic_t
 *
 * Atomically increments v by 1.  Note that the guaranteed
 * useful range of an atomic_t is only 24 bits.
 */
static __always_inline void atomic_inc(atomic_t *v)
{
	__asm__ __volatile__(
		"lock ; incl %0"
		: "=m" (v->counter)
		: "m" (v->counter));
}

/**
 * atomic_dec - decrement atomic variable
 * @param v: pointer of type atomic_t
 *
 * Atomically decrements v by 1.  Note that the guaranteed
 * useful range of an atomic_t is only 24 bits.
 */
static __always_inline void atomic_dec(atomic_t *v)
{
	__asm__ __volatile__(
		"lock ; decl %0"
		: "=m" (v->counter)
		: "m" (v->counter));
}



#endif /* ARCH_SMP_ATOMIC_H */