drm/amdgpu: cleanup ctx_mgr init/fini
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_ctx.c
1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: monk liu <monk.liu@amd.com>
23  */
24
25 #include <drm/drmP.h>
26 #include "amdgpu.h"
27
28 static void amdgpu_ctx_do_release(struct kref *ref)
29 {
30         struct amdgpu_ctx *ctx;
31         struct amdgpu_device *adev;
32         unsigned i, j;
33
34         ctx = container_of(ref, struct amdgpu_ctx, refcount);
35         adev = ctx->adev;
36
37
38         for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
39                 for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
40                         fence_put(ctx->rings[i].fences[j]);
41
42         if (amdgpu_enable_scheduler) {
43                 for (i = 0; i < adev->num_rings; i++)
44                         amd_context_entity_fini(adev->rings[i]->scheduler,
45                                                 &ctx->rings[i].c_entity);
46         }
47
48         kfree(ctx);
49 }
50
51 static void amdgpu_ctx_init(struct amdgpu_device *adev,
52                             struct amdgpu_fpriv *fpriv,
53                             struct amdgpu_ctx *ctx,
54                             uint32_t id)
55 {
56         int i;
57         memset(ctx, 0, sizeof(*ctx));
58         ctx->adev = adev;
59         kref_init(&ctx->refcount);
60         spin_lock_init(&ctx->ring_lock);
61         for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
62                 ctx->rings[i].sequence = 1;
63 }
64
65 int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
66                      uint32_t *id)
67 {
68         struct amdgpu_ctx *ctx;
69         int i, j, r;
70
71         ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
72         if (!ctx)
73                 return -ENOMEM;
74         if (fpriv) {
75                 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
76                 mutex_lock(&mgr->lock);
77                 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
78                 if (r < 0) {
79                         mutex_unlock(&mgr->lock);
80                         kfree(ctx);
81                         return r;
82                 }
83                 *id = (uint32_t)r;
84                 amdgpu_ctx_init(adev, fpriv, ctx, *id);
85                 mutex_unlock(&mgr->lock);
86         } else {
87                 if (adev->kernel_ctx) {
88                         DRM_ERROR("kernel cnotext has been created.\n");
89                         kfree(ctx);
90                         return 0;
91                 }
92                 *id = AMD_KERNEL_CONTEXT_ID;
93                 amdgpu_ctx_init(adev, fpriv, ctx, *id);
94
95                 adev->kernel_ctx = ctx;
96         }
97
98         if (amdgpu_enable_scheduler) {
99                 /* create context entity for each ring */
100                 for (i = 0; i < adev->num_rings; i++) {
101                         struct amd_run_queue *rq;
102                         if (fpriv)
103                                 rq = &adev->rings[i]->scheduler->sched_rq;
104                         else
105                                 rq = &adev->rings[i]->scheduler->kernel_rq;
106                         r = amd_context_entity_init(adev->rings[i]->scheduler,
107                                                     &ctx->rings[i].c_entity,
108                                                     NULL, rq, *id,
109                                                     amdgpu_sched_jobs);
110                         if (r)
111                                 break;
112                 }
113
114                 if (i < adev->num_rings) {
115                         for (j = 0; j < i; j++)
116                                 amd_context_entity_fini(adev->rings[j]->scheduler,
117                                                         &ctx->rings[j].c_entity);
118                         kfree(ctx);
119                         return -EINVAL;
120                 }
121         }
122
123         return 0;
124 }
125
126 int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
127 {
128         struct amdgpu_ctx *ctx;
129
130         if (fpriv) {
131                 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
132                 mutex_lock(&mgr->lock);
133                 ctx = idr_find(&mgr->ctx_handles, id);
134                 if (ctx) {
135                         idr_remove(&mgr->ctx_handles, id);
136                         kref_put(&ctx->refcount, amdgpu_ctx_do_release);
137                         mutex_unlock(&mgr->lock);
138                         return 0;
139                 }
140                 mutex_unlock(&mgr->lock);
141         } else {
142                 ctx = adev->kernel_ctx;
143                 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
144                 return 0;
145         }
146         return -EINVAL;
147 }
148
149 static int amdgpu_ctx_query(struct amdgpu_device *adev,
150                             struct amdgpu_fpriv *fpriv, uint32_t id,
151                             union drm_amdgpu_ctx_out *out)
152 {
153         struct amdgpu_ctx *ctx;
154         struct amdgpu_ctx_mgr *mgr;
155         unsigned reset_counter;
156
157         if (!fpriv)
158                 return -EINVAL;
159
160         mgr = &fpriv->ctx_mgr;
161         mutex_lock(&mgr->lock);
162         ctx = idr_find(&mgr->ctx_handles, id);
163         if (!ctx) {
164                 mutex_unlock(&mgr->lock);
165                 return -EINVAL;
166         }
167
168         /* TODO: these two are always zero */
169         out->state.flags = 0x0;
170         out->state.hangs = 0x0;
171
172         /* determine if a GPU reset has occured since the last call */
173         reset_counter = atomic_read(&adev->gpu_reset_counter);
174         /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
175         if (ctx->reset_counter == reset_counter)
176                 out->state.reset_status = AMDGPU_CTX_NO_RESET;
177         else
178                 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
179         ctx->reset_counter = reset_counter;
180
181         mutex_unlock(&mgr->lock);
182         return 0;
183 }
184
185 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
186                      struct drm_file *filp)
187 {
188         int r;
189         uint32_t id;
190
191         union drm_amdgpu_ctx *args = data;
192         struct amdgpu_device *adev = dev->dev_private;
193         struct amdgpu_fpriv *fpriv = filp->driver_priv;
194
195         r = 0;
196         id = args->in.ctx_id;
197
198         switch (args->in.op) {
199                 case AMDGPU_CTX_OP_ALLOC_CTX:
200                         r = amdgpu_ctx_alloc(adev, fpriv, &id);
201                         args->out.alloc.ctx_id = id;
202                         break;
203                 case AMDGPU_CTX_OP_FREE_CTX:
204                         r = amdgpu_ctx_free(adev, fpriv, id);
205                         break;
206                 case AMDGPU_CTX_OP_QUERY_STATE:
207                         r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
208                         break;
209                 default:
210                         return -EINVAL;
211         }
212
213         return r;
214 }
215
216 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
217 {
218         struct amdgpu_ctx *ctx;
219         struct amdgpu_ctx_mgr *mgr;
220
221         if (!fpriv)
222                 return NULL;
223
224         mgr = &fpriv->ctx_mgr;
225
226         mutex_lock(&mgr->lock);
227         ctx = idr_find(&mgr->ctx_handles, id);
228         if (ctx)
229                 kref_get(&ctx->refcount);
230         mutex_unlock(&mgr->lock);
231         return ctx;
232 }
233
234 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
235 {
236         if (ctx == NULL)
237                 return -EINVAL;
238
239         kref_put(&ctx->refcount, amdgpu_ctx_do_release);
240         return 0;
241 }
242
243 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
244                               struct fence *fence, uint64_t queued_seq)
245 {
246         struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
247         uint64_t seq = 0;
248         unsigned idx = 0;
249         struct fence *other = NULL;
250
251         if (amdgpu_enable_scheduler)
252                 seq = queued_seq;
253         else
254                 seq = cring->sequence;
255         idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
256         other = cring->fences[idx];
257         if (other) {
258                 signed long r;
259                 r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
260                 if (r < 0)
261                         DRM_ERROR("Error (%ld) waiting for fence!\n", r);
262         }
263
264         fence_get(fence);
265
266         spin_lock(&ctx->ring_lock);
267         cring->fences[idx] = fence;
268         if (!amdgpu_enable_scheduler)
269                 cring->sequence++;
270         spin_unlock(&ctx->ring_lock);
271
272         fence_put(other);
273
274         return seq;
275 }
276
277 struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
278                                    struct amdgpu_ring *ring, uint64_t seq)
279 {
280         struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
281         struct fence *fence;
282         uint64_t queued_seq;
283         int r;
284
285         if (amdgpu_enable_scheduler) {
286                 r = amd_sched_wait_emit(&cring->c_entity,
287                                         seq,
288                                         false,
289                                         -1);
290                 if (r)
291                         return NULL;
292         }
293
294         spin_lock(&ctx->ring_lock);
295         if (amdgpu_enable_scheduler)
296                 queued_seq = amd_sched_next_queued_seq(&cring->c_entity);
297         else
298                 queued_seq = cring->sequence;
299
300         if (seq >= queued_seq) {
301                 spin_unlock(&ctx->ring_lock);
302                 return ERR_PTR(-EINVAL);
303         }
304
305
306         if (seq + AMDGPU_CTX_MAX_CS_PENDING < queued_seq) {
307                 spin_unlock(&ctx->ring_lock);
308                 return NULL;
309         }
310
311         fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
312         spin_unlock(&ctx->ring_lock);
313
314         return fence;
315 }
316
317 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
318 {
319         mutex_init(&mgr->lock);
320         idr_init(&mgr->ctx_handles);
321 }
322
323 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
324 {
325         struct amdgpu_ctx *ctx;
326         struct idr *idp;
327         uint32_t id;
328
329         idp = &mgr->ctx_handles;
330
331         idr_for_each_entry(idp, ctx, id) {
332                 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
333                         DRM_ERROR("ctx %p is still alive\n", ctx);
334         }
335
336         idr_destroy(&mgr->ctx_handles);
337         mutex_destroy(&mgr->lock);
338 }