diff options
author | Debarshi Dutta <ddutta@nvidia.com> | 2017-08-08 02:38:03 -0400 |
---|---|---|
committer | mobile promotions <svcmobile_promotions@nvidia.com> | 2017-08-24 04:10:37 -0400 |
commit | 3fa47b877db1edc16018d662e7b9915d92354745 (patch) | |
tree | c1d9a8734e7d92b5ae647fbc3f582a01207a23f6 /drivers/gpu/nvgpu/include | |
parent | 8662fae334f2419da2e7fd220f7734217ec52433 (diff) |
gpu: nvgpu: Replace kref for refcounting in nvgpu
- added wrapper struct nvgpu_ref over nvgpu_atomic_t
- added nvgpu_ref_* APIs to access the above struct
JIRA NVGPU-140
Change-Id: Id47f897995dd4721751f7610b6d4d4fbfe4d6b9a
Signed-off-by: Debarshi Dutta <ddutta@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/1540899
Reviewed-by: svc-mobile-coverity <svc-mobile-coverity@nvidia.com>
Reviewed-by: svccoveritychecker <svccoveritychecker@nvidia.com>
GVS: Gerrit_Virtual_Submit
Reviewed-by: Konsta Holtta <kholtta@nvidia.com>
Reviewed-by: Vijayakumar Subbu <vsubbu@nvidia.com>
Diffstat (limited to 'drivers/gpu/nvgpu/include')
-rw-r--r-- | drivers/gpu/nvgpu/include/nvgpu/atomic.h | 8 | ||||
-rw-r--r-- | drivers/gpu/nvgpu/include/nvgpu/kref.h | 63 | ||||
-rw-r--r-- | drivers/gpu/nvgpu/include/nvgpu/linux/atomic.h | 10 | ||||
-rw-r--r-- | drivers/gpu/nvgpu/include/nvgpu/semaphore.h | 4 | ||||
-rw-r--r-- | drivers/gpu/nvgpu/include/nvgpu/vm.h | 4 |
5 files changed, 82 insertions, 7 deletions
diff --git a/drivers/gpu/nvgpu/include/nvgpu/atomic.h b/drivers/gpu/nvgpu/include/nvgpu/atomic.h index c7a5fcd9..393a9d35 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/atomic.h +++ b/drivers/gpu/nvgpu/include/nvgpu/atomic.h | |||
@@ -61,10 +61,18 @@ static inline bool nvgpu_atomic_dec_and_test(nvgpu_atomic_t *v) | |||
61 | { | 61 | { |
62 | return __nvgpu_atomic_dec_and_test(v); | 62 | return __nvgpu_atomic_dec_and_test(v); |
63 | } | 63 | } |
64 | static inline bool nvgpu_atomic_sub_and_test(int i, nvgpu_atomic_t *v) | ||
65 | { | ||
66 | return __nvgpu_atomic_sub_and_test(i, v); | ||
67 | } | ||
64 | static inline int nvgpu_atomic_add_return(int i, nvgpu_atomic_t *v) | 68 | static inline int nvgpu_atomic_add_return(int i, nvgpu_atomic_t *v) |
65 | { | 69 | { |
66 | return __nvgpu_atomic_add_return(i, v); | 70 | return __nvgpu_atomic_add_return(i, v); |
67 | } | 71 | } |
72 | static inline int nvgpu_atomic_add_unless(nvgpu_atomic_t *v, int a, int u) | ||
73 | { | ||
74 | return __nvgpu_atomic_add_unless(v, a, u); | ||
75 | } | ||
68 | static inline void nvgpu_atomic64_set(nvgpu_atomic64_t *v, long i) | 76 | static inline void nvgpu_atomic64_set(nvgpu_atomic64_t *v, long i) |
69 | { | 77 | { |
70 | return __nvgpu_atomic64_set(v, i); | 78 | return __nvgpu_atomic64_set(v, i); |
diff --git a/drivers/gpu/nvgpu/include/nvgpu/kref.h b/drivers/gpu/nvgpu/include/nvgpu/kref.h index d24db603..fd2b456f 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/kref.h +++ b/drivers/gpu/nvgpu/include/nvgpu/kref.h | |||
@@ -10,11 +10,68 @@ | |||
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
11 | * more details. | 11 | * more details. |
12 | */ | 12 | */ |
13 | |||
14 | /* | ||
15 | * The following structure is used for reference counting of objects in nvgpu. | ||
16 | */ | ||
13 | #ifndef __NVGPU_KREF_H__ | 17 | #ifndef __NVGPU_KREF_H__ |
14 | #define __NVGPU_KREF_H__ | 18 | #define __NVGPU_KREF_H__ |
15 | 19 | ||
16 | #ifdef __KERNEL__ | 20 | #include <nvgpu/atomic.h> |
17 | #include <linux/kref.h> | 21 | |
18 | #endif | 22 | struct nvgpu_ref { |
23 | nvgpu_atomic_t refcount; | ||
24 | }; | ||
25 | |||
26 | /* | ||
27 | * Initialize object. | ||
28 | * @ref: the nvgpu_ref object to initialize | ||
29 | */ | ||
30 | static inline void nvgpu_ref_init(struct nvgpu_ref *ref) | ||
31 | { | ||
32 | nvgpu_atomic_set(&ref->refcount, 1); | ||
33 | } | ||
34 | |||
35 | /* | ||
36 | * Increment reference count for the object | ||
37 | * @ref: the nvgpu_ref object | ||
38 | */ | ||
39 | static inline void nvgpu_ref_get(struct nvgpu_ref *ref) | ||
40 | { | ||
41 | nvgpu_atomic_inc(&ref->refcount); | ||
42 | } | ||
43 | |||
44 | /* | ||
45 | * Decrement reference count for the object and call release() if it becomes | ||
46 | * zero. | ||
47 | * @ref: the nvgpu_ref object | ||
48 | * @release: pointer to the function that would be invoked to clean up the | ||
49 | * object when the reference count becomes zero, i.e. the last | ||
50 | * reference corresponding to this object is removed. | ||
51 | * Return 1 if object was removed, otherwise return 0. The user should not | ||
52 | * make any assumptions about the status of the object in the memory when | ||
53 | * the function returns 0 and should only use it to know that there are no | ||
54 | * further references to this object. | ||
55 | */ | ||
56 | static inline int nvgpu_ref_put(struct nvgpu_ref *ref, | ||
57 | void (*release)(struct nvgpu_ref *r)) | ||
58 | { | ||
59 | if (nvgpu_atomic_sub_and_test(1, &ref->refcount)) { | ||
60 | if (release != NULL) | ||
61 | release(ref); | ||
62 | return 1; | ||
63 | } | ||
64 | return 0; | ||
65 | } | ||
66 | |||
67 | /* | ||
68 | * Increment reference count for the object unless it is zero. | ||
69 | * @ref: the nvgpu_ref object | ||
70 | * Return non-zero if the increment succeeds, Otherwise return 0. | ||
71 | */ | ||
72 | static inline int __must_check nvgpu_ref_get_unless_zero(struct nvgpu_ref *ref) | ||
73 | { | ||
74 | return nvgpu_atomic_add_unless(&ref->refcount, 1, 0); | ||
75 | } | ||
19 | 76 | ||
20 | #endif /* __NVGPU_KREF_H__ */ | 77 | #endif /* __NVGPU_KREF_H__ */ |
diff --git a/drivers/gpu/nvgpu/include/nvgpu/linux/atomic.h b/drivers/gpu/nvgpu/include/nvgpu/linux/atomic.h index 1fdb2674..0734672e 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/linux/atomic.h +++ b/drivers/gpu/nvgpu/include/nvgpu/linux/atomic.h | |||
@@ -81,11 +81,21 @@ static inline bool __nvgpu_atomic_dec_and_test(nvgpu_atomic_t *v) | |||
81 | return atomic_dec_and_test(&v->atomic_var); | 81 | return atomic_dec_and_test(&v->atomic_var); |
82 | } | 82 | } |
83 | 83 | ||
84 | static inline bool __nvgpu_atomic_sub_and_test(int i, nvgpu_atomic_t *v) | ||
85 | { | ||
86 | return atomic_sub_and_test(i, &v->atomic_var); | ||
87 | } | ||
88 | |||
84 | static inline int __nvgpu_atomic_add_return(int i, nvgpu_atomic_t *v) | 89 | static inline int __nvgpu_atomic_add_return(int i, nvgpu_atomic_t *v) |
85 | { | 90 | { |
86 | return atomic_add_return(i, &v->atomic_var); | 91 | return atomic_add_return(i, &v->atomic_var); |
87 | } | 92 | } |
88 | 93 | ||
94 | static inline int __nvgpu_atomic_add_unless(nvgpu_atomic_t *v, int a, int u) | ||
95 | { | ||
96 | return atomic_add_unless(&v->atomic_var, a, u); | ||
97 | } | ||
98 | |||
89 | static inline void __nvgpu_atomic64_set(nvgpu_atomic64_t *v, long i) | 99 | static inline void __nvgpu_atomic64_set(nvgpu_atomic64_t *v, long i) |
90 | { | 100 | { |
91 | atomic64_set(&v->atomic_var, i); | 101 | atomic64_set(&v->atomic_var, i); |
diff --git a/drivers/gpu/nvgpu/include/nvgpu/semaphore.h b/drivers/gpu/nvgpu/include/nvgpu/semaphore.h index 90261d81..5c0019ae 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/semaphore.h +++ b/drivers/gpu/nvgpu/include/nvgpu/semaphore.h | |||
@@ -73,7 +73,7 @@ struct nvgpu_semaphore { | |||
73 | nvgpu_atomic_t value; | 73 | nvgpu_atomic_t value; |
74 | int incremented; | 74 | int incremented; |
75 | 75 | ||
76 | struct kref ref; | 76 | struct nvgpu_ref ref; |
77 | }; | 77 | }; |
78 | 78 | ||
79 | /* | 79 | /* |
@@ -106,7 +106,7 @@ struct nvgpu_semaphore_pool { | |||
106 | * done waiting on it. This ref count ensures that the pool doesn't | 106 | * done waiting on it. This ref count ensures that the pool doesn't |
107 | * go away until all semaphores using this pool are cleaned up first. | 107 | * go away until all semaphores using this pool are cleaned up first. |
108 | */ | 108 | */ |
109 | struct kref ref; | 109 | struct nvgpu_ref ref; |
110 | }; | 110 | }; |
111 | 111 | ||
112 | static inline struct nvgpu_semaphore_pool * | 112 | static inline struct nvgpu_semaphore_pool * |
diff --git a/drivers/gpu/nvgpu/include/nvgpu/vm.h b/drivers/gpu/nvgpu/include/nvgpu/vm.h index 255b4361..b5c64c99 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/vm.h +++ b/drivers/gpu/nvgpu/include/nvgpu/vm.h | |||
@@ -88,7 +88,7 @@ struct nvgpu_mapped_buf { | |||
88 | u64 size; | 88 | u64 size; |
89 | struct dma_buf *dmabuf; | 89 | struct dma_buf *dmabuf; |
90 | struct sg_table *sgt; | 90 | struct sg_table *sgt; |
91 | struct kref ref; | 91 | struct nvgpu_ref ref; |
92 | u32 user_mapped; | 92 | u32 user_mapped; |
93 | bool own_mem_ref; | 93 | bool own_mem_ref; |
94 | u32 pgsz_idx; | 94 | u32 pgsz_idx; |
@@ -142,7 +142,7 @@ struct vm_gk20a { | |||
142 | 142 | ||
143 | const struct gk20a_mmu_level *mmu_levels; | 143 | const struct gk20a_mmu_level *mmu_levels; |
144 | 144 | ||
145 | struct kref ref; | 145 | struct nvgpu_ref ref; |
146 | 146 | ||
147 | struct nvgpu_mutex update_gmmu_lock; | 147 | struct nvgpu_mutex update_gmmu_lock; |
148 | 148 | ||