]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
drm/amdgpu: add user fence context map v2
[linux.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 <[email protected]>
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         unsigned i, j;
32
33         ctx = container_of(ref, struct amdgpu_ctx, refcount);
34
35         for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
36                 for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
37                         fence_put(ctx->rings[i].fences[j]);
38         kfree(ctx);
39 }
40
41 int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
42                      uint32_t *id)
43 {
44         struct amdgpu_ctx *ctx;
45         struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
46         int i, r;
47
48         ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
49         if (!ctx)
50                 return -ENOMEM;
51
52         mutex_lock(&mgr->lock);
53         r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
54         if (r < 0) {
55                 mutex_unlock(&mgr->lock);
56                 kfree(ctx);
57                 return r;
58         }
59         *id = (uint32_t)r;
60
61         memset(ctx, 0, sizeof(*ctx));
62         kref_init(&ctx->refcount);
63         spin_lock_init(&ctx->ring_lock);
64         for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
65                 ctx->rings[i].sequence = 1;
66         mutex_unlock(&mgr->lock);
67
68         return 0;
69 }
70
71 int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
72 {
73         struct amdgpu_ctx *ctx;
74         struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
75
76         mutex_lock(&mgr->lock);
77         ctx = idr_find(&mgr->ctx_handles, id);
78         if (ctx) {
79                 idr_remove(&mgr->ctx_handles, id);
80                 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
81                 mutex_unlock(&mgr->lock);
82                 return 0;
83         }
84         mutex_unlock(&mgr->lock);
85         return -EINVAL;
86 }
87
88 static int amdgpu_ctx_query(struct amdgpu_device *adev,
89                             struct amdgpu_fpriv *fpriv, uint32_t id,
90                             union drm_amdgpu_ctx_out *out)
91 {
92         struct amdgpu_ctx *ctx;
93         struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
94         unsigned reset_counter;
95
96         mutex_lock(&mgr->lock);
97         ctx = idr_find(&mgr->ctx_handles, id);
98         if (!ctx) {
99                 mutex_unlock(&mgr->lock);
100                 return -EINVAL;
101         }
102
103         /* TODO: these two are always zero */
104         out->state.flags = 0x0;
105         out->state.hangs = 0x0;
106
107         /* determine if a GPU reset has occured since the last call */
108         reset_counter = atomic_read(&adev->gpu_reset_counter);
109         /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
110         if (ctx->reset_counter == reset_counter)
111                 out->state.reset_status = AMDGPU_CTX_NO_RESET;
112         else
113                 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
114         ctx->reset_counter = reset_counter;
115
116         mutex_unlock(&mgr->lock);
117         return 0;
118 }
119
120 void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
121 {
122         struct idr *idp;
123         struct amdgpu_ctx *ctx;
124         uint32_t id;
125         struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
126         idp = &mgr->ctx_handles;
127
128         idr_for_each_entry(idp,ctx,id) {
129                 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
130                         DRM_ERROR("ctx %p is still alive\n", ctx);
131         }
132
133         mutex_destroy(&mgr->lock);
134 }
135
136 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
137                      struct drm_file *filp)
138 {
139         int r;
140         uint32_t id;
141
142         union drm_amdgpu_ctx *args = data;
143         struct amdgpu_device *adev = dev->dev_private;
144         struct amdgpu_fpriv *fpriv = filp->driver_priv;
145
146         r = 0;
147         id = args->in.ctx_id;
148
149         switch (args->in.op) {
150                 case AMDGPU_CTX_OP_ALLOC_CTX:
151                         r = amdgpu_ctx_alloc(adev, fpriv, &id);
152                         args->out.alloc.ctx_id = id;
153                         break;
154                 case AMDGPU_CTX_OP_FREE_CTX:
155                         r = amdgpu_ctx_free(adev, fpriv, id);
156                         break;
157                 case AMDGPU_CTX_OP_QUERY_STATE:
158                         r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
159                         break;
160                 default:
161                         return -EINVAL;
162         }
163
164         return r;
165 }
166
167 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
168 {
169         struct amdgpu_ctx *ctx;
170         struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
171
172         mutex_lock(&mgr->lock);
173         ctx = idr_find(&mgr->ctx_handles, id);
174         if (ctx)
175                 kref_get(&ctx->refcount);
176         mutex_unlock(&mgr->lock);
177         return ctx;
178 }
179
180 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
181 {
182         if (ctx == NULL)
183                 return -EINVAL;
184
185         kref_put(&ctx->refcount, amdgpu_ctx_do_release);
186         return 0;
187 }
188
189 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
190                               struct fence *fence)
191 {
192         struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
193         uint64_t seq = cring->sequence;
194         unsigned idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
195         struct fence *other = cring->fences[idx];
196
197         if (other) {
198                 signed long r;
199                 r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
200                 if (r < 0)
201                         DRM_ERROR("Error (%ld) waiting for fence!\n", r);
202         }
203
204         fence_get(fence);
205
206         spin_lock(&ctx->ring_lock);
207         cring->fences[idx] = fence;
208         cring->sequence++;
209         spin_unlock(&ctx->ring_lock);
210
211         fence_put(other);
212
213         return seq;
214 }
215
216 struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
217                                    struct amdgpu_ring *ring, uint64_t seq)
218 {
219         struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
220         struct fence *fence;
221
222         spin_lock(&ctx->ring_lock);
223         if (seq >= cring->sequence) {
224                 spin_unlock(&ctx->ring_lock);
225                 return ERR_PTR(-EINVAL);
226         }
227
228         if (seq < cring->sequence - AMDGPU_CTX_MAX_CS_PENDING) {
229                 spin_unlock(&ctx->ring_lock);
230                 return NULL;
231         }
232
233         fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
234         spin_unlock(&ctx->ring_lock);
235
236         return fence;
237 }
This page took 0.04674 seconds and 4 git commands to generate.