blob: c23bfd8fe4143ce4a11376977a70630c1bcdb972 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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
28static void amdgpu_ctx_do_release(struct kref *ref)
29{
30 struct amdgpu_ctx *ctx;
Christian König21c16bf2015-07-07 17:24:49 +020031 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040032
33 ctx = container_of(ref, struct amdgpu_ctx, refcount);
Christian König21c16bf2015-07-07 17:24:49 +020034
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]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040038 kfree(ctx);
39}
40
Alex Deucher0b492a42015-08-16 22:48:26 -040041int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
42 uint32_t *id)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040043{
Alex Deucherd38ceaf2015-04-20 16:55:21 -040044 struct amdgpu_ctx *ctx;
45 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
Christian König21c16bf2015-07-07 17:24:49 +020046 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040047
48 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
49 if (!ctx)
50 return -ENOMEM;
51
Marek Olšák0147ee02015-05-05 20:52:00 +020052 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053 r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
54 if (r < 0) {
Marek Olšák0147ee02015-05-05 20:52:00 +020055 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040056 kfree(ctx);
57 return r;
58 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059 *id = (uint32_t)r;
60
61 memset(ctx, 0, sizeof(*ctx));
Alex Deucherd38ceaf2015-04-20 16:55:21 -040062 kref_init(&ctx->refcount);
Christian König21c16bf2015-07-07 17:24:49 +020063 spin_lock_init(&ctx->ring_lock);
64 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
65 ctx->rings[i].sequence = 1;
Marek Olšák0147ee02015-05-05 20:52:00 +020066 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040067
68 return 0;
69}
70
71int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
72{
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073 struct amdgpu_ctx *ctx;
74 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
75
Marek Olšák0147ee02015-05-05 20:52:00 +020076 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 ctx = idr_find(&mgr->ctx_handles, id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040078 if (ctx) {
Alex Deucher0b492a42015-08-16 22:48:26 -040079 idr_remove(&mgr->ctx_handles, id);
Marek Olšákf11358d2015-05-05 00:56:45 +020080 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Marek Olšák0147ee02015-05-05 20:52:00 +020081 mutex_unlock(&mgr->lock);
Marek Olšákf11358d2015-05-05 00:56:45 +020082 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 }
Marek Olšák0147ee02015-05-05 20:52:00 +020084 mutex_unlock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085 return -EINVAL;
86}
87
Marek Olšákd94aed52015-05-05 21:13:49 +020088static int amdgpu_ctx_query(struct amdgpu_device *adev,
89 struct amdgpu_fpriv *fpriv, uint32_t id,
90 union drm_amdgpu_ctx_out *out)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091{
92 struct amdgpu_ctx *ctx;
93 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
Marek Olšákd94aed52015-05-05 21:13:49 +020094 unsigned reset_counter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095
Marek Olšák0147ee02015-05-05 20:52:00 +020096 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040097 ctx = idr_find(&mgr->ctx_handles, id);
Marek Olšákd94aed52015-05-05 21:13:49 +020098 if (!ctx) {
Marek Olšák0147ee02015-05-05 20:52:00 +020099 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200100 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101 }
Marek Olšákd94aed52015-05-05 21:13:49 +0200102
103 /* TODO: these two are always zero */
Alex Deucher0b492a42015-08-16 22:48:26 -0400104 out->state.flags = 0x0;
105 out->state.hangs = 0x0;
Marek Olšákd94aed52015-05-05 21:13:49 +0200106
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
Marek Olšák0147ee02015-05-05 20:52:00 +0200116 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200117 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118}
119
120void 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)
Alex Deucher0b492a42015-08-16 22:48:26 -0400130 DRM_ERROR("ctx %p is still alive\n", ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 }
132
Marek Olšák0147ee02015-05-05 20:52:00 +0200133 mutex_destroy(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134}
135
136int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
Marek Olšákd94aed52015-05-05 21:13:49 +0200137 struct drm_file *filp)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138{
139 int r;
140 uint32_t id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141
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;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400148
149 switch (args->in.op) {
150 case AMDGPU_CTX_OP_ALLOC_CTX:
Alex Deucher0b492a42015-08-16 22:48:26 -0400151 r = amdgpu_ctx_alloc(adev, fpriv, &id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152 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:
Marek Olšákd94aed52015-05-05 21:13:49 +0200158 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159 break;
160 default:
161 return -EINVAL;
162 }
163
164 return r;
165}
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800166
167struct 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
180int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
181{
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800182 if (ctx == NULL)
183 return -EINVAL;
184
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800185 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800186 return 0;
187}
Christian König21c16bf2015-07-07 17:24:49 +0200188
189uint64_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
216struct 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}