2 * Copyright (c) 2014, STMicroelectronics International N.V.
3 * Copyright (c) 2016, Linaro Limited
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
29 #ifndef KERNEL_SPINLOCK_H
30 #define KERNEL_SPINLOCK_H
32 #define SPINLOCK_LOCK 1
33 #define SPINLOCK_UNLOCK 0
39 #include <kernel/thread.h>
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)
47 assert(!have_spinlock());
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) { }
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);
60 static inline void cpu_spin_lock(unsigned int *lock)
62 assert(thread_irq_disabled());
63 __cpu_spin_lock(lock);
64 spinlock_count_incr();
67 static inline bool cpu_spin_trylock(unsigned int *lock)
71 assert(thread_irq_disabled());
72 rc = __cpu_spin_trylock(lock);
74 spinlock_count_incr();
78 static inline void cpu_spin_unlock(unsigned int *lock)
80 assert(thread_irq_disabled());
81 __cpu_spin_unlock(lock);
82 spinlock_count_decr();
86 #endif /* KERNEL_SPINLOCK_H */