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