c24867379b04f2f94355dc2c0db99bb2337c192e
[platform/core/security/tef-optee_os.git] / core / arch / arm / include / kernel / spinlock.h
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * Copyright (c) 2016, Linaro Limited
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef KERNEL_SPINLOCK_H
30 #define KERNEL_SPINLOCK_H
31
32 #define SPINLOCK_LOCK       1
33 #define SPINLOCK_UNLOCK     0
34
35 #ifndef ASM
36 #include <assert.h>
37 #include <compiler.h>
38 #include <stdbool.h>
39 #include <kernel/thread.h>
40
41 #ifdef CFG_TEE_CORE_DEBUG
42 void spinlock_count_incr(void);
43 void spinlock_count_decr(void);
44 bool have_spinlock(void);
45 static inline void assert_have_no_spinlock(void)
46 {
47         assert(!have_spinlock());
48 }
49 #else
50 static inline void spinlock_count_incr(void) { }
51 static inline void spinlock_count_decr(void) { }
52 static inline void assert_have_no_spinlock(void) { }
53 #endif
54
55 void __cpu_spin_lock(unsigned int *lock);
56 void __cpu_spin_unlock(unsigned int *lock);
57 /* returns 0 on locking success, non zero on failure */
58 unsigned int __cpu_spin_trylock(unsigned int *lock);
59
60 static inline void cpu_spin_lock(unsigned int *lock)
61 {
62         assert(thread_irq_disabled());
63         __cpu_spin_lock(lock);
64         spinlock_count_incr();
65 }
66
67 static inline bool cpu_spin_trylock(unsigned int *lock)
68 {
69         unsigned int rc;
70
71         assert(thread_irq_disabled());
72         rc = __cpu_spin_trylock(lock);
73         if (!rc)
74                 spinlock_count_incr();
75         return !rc;
76 }
77
78 static inline void cpu_spin_unlock(unsigned int *lock)
79 {
80         assert(thread_irq_disabled());
81         __cpu_spin_unlock(lock);
82         spinlock_count_decr();
83 }
84 #endif /* ASM */
85
86 #endif /* KERNEL_SPINLOCK_H */