1 /*
2  *
3  * (C) COPYRIGHT 2011-2016 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15 
16 
17 
18 #ifndef _KBASE_CONTEXT_H_
19 #define _KBASE_CONTEXT_H_
20 
21 #include <linux/atomic.h>
22 
23 
24 int kbase_context_set_create_flags(struct kbase_context *kctx, u32 flags);
25 
26 /**
27  * kbase_ctx_flag - Check if @flag is set on @kctx
28  * @kctx: Pointer to kbase context to check
29  * @flag: Flag to check
30  *
31  * Return: true if @flag is set on @kctx, false if not.
32  */
kbase_ctx_flag(struct kbase_context *kctx, enum kbase_context_flags flag)33 static inline bool kbase_ctx_flag(struct kbase_context *kctx,
34 				      enum kbase_context_flags flag)
35 {
36 	return atomic_read(&kctx->flags) & flag;
37 }
38 
39 /**
40  * kbase_ctx_flag_clear - Clear @flag on @kctx
41  * @kctx: Pointer to kbase context
42  * @flag: Flag to clear
43  *
44  * Clear the @flag on @kctx. This is done atomically, so other flags being
45  * cleared or set at the same time will be safe.
46  *
47  * Some flags have locking requirements, check the documentation for the
48  * respective flags.
49  */
kbase_ctx_flag_clear(struct kbase_context *kctx, enum kbase_context_flags flag)50 static inline void kbase_ctx_flag_clear(struct kbase_context *kctx,
51 					enum kbase_context_flags flag)
52 {
53 #if KERNEL_VERSION(4, 3, 0) > LINUX_VERSION_CODE
54 	/*
55 	 * Earlier kernel versions doesn't have atomic_andnot() or
56 	 * atomic_and(). atomic_clear_mask() was only available on some
57 	 * architectures and removed on arm in v3.13 on arm and arm64.
58 	 *
59 	 * Use a compare-exchange loop to clear the flag on pre 4.3 kernels,
60 	 * when atomic_andnot() becomes available.
61 	 */
62 	int old, new;
63 
64 	do {
65 		old = atomic_read(&kctx->flags);
66 		new = old & ~flag;
67 
68 	} while (atomic_cmpxchg(&kctx->flags, old, new) != old);
69 #else
70 	atomic_andnot(flag, &kctx->flags);
71 #endif
72 }
73 
74 /**
75  * kbase_ctx_flag_set - Set @flag on @kctx
76  * @kctx: Pointer to kbase context
77  * @flag: Flag to clear
78  *
79  * Set the @flag on @kctx. This is done atomically, so other flags being
80  * cleared or set at the same time will be safe.
81  *
82  * Some flags have locking requirements, check the documentation for the
83  * respective flags.
84  */
kbase_ctx_flag_set(struct kbase_context *kctx, enum kbase_context_flags flag)85 static inline void kbase_ctx_flag_set(struct kbase_context *kctx,
86 				      enum kbase_context_flags flag)
87 {
88 	atomic_or(flag, &kctx->flags);
89 }
90 #endif /* _KBASE_CONTEXT_H_ */
91