Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 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; |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 31 | struct amdgpu_device *adev; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 32 | unsigned i, j; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 33 | |
| 34 | ctx = container_of(ref, struct amdgpu_ctx, refcount); |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 35 | adev = ctx->adev; |
| 36 | |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 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]); |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 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 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 48 | kfree(ctx); |
| 49 | } |
| 50 | |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 51 | static void amdgpu_ctx_init(struct amdgpu_device *adev, |
| 52 | struct amdgpu_fpriv *fpriv, |
| 53 | struct amdgpu_ctx *ctx, |
| 54 | uint32_t id) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 55 | { |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 56 | int i; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 57 | memset(ctx, 0, sizeof(*ctx)); |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 58 | ctx->adev = adev; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 59 | kref_init(&ctx->refcount); |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 60 | spin_lock_init(&ctx->ring_lock); |
| 61 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 62 | ctx->rings[i].sequence = 1; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 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 | |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 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, |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 108 | NULL, rq, *id, |
| 109 | amdgpu_sched_jobs); |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 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 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id) |
| 127 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 128 | struct amdgpu_ctx *ctx; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 129 | |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 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 | } |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 140 | mutex_unlock(&mgr->lock); |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 141 | } else { |
| 142 | ctx = adev->kernel_ctx; |
| 143 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
Marek Olšák | f11358d | 2015-05-05 00:56:45 +0200 | [diff] [blame] | 144 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 145 | } |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 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) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 152 | { |
| 153 | struct amdgpu_ctx *ctx; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 154 | struct amdgpu_ctx_mgr *mgr; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 155 | unsigned reset_counter; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 156 | |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 157 | if (!fpriv) |
| 158 | return -EINVAL; |
| 159 | |
| 160 | mgr = &fpriv->ctx_mgr; |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 161 | mutex_lock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 162 | ctx = idr_find(&mgr->ctx_handles, id); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 163 | if (!ctx) { |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 164 | mutex_unlock(&mgr->lock); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 165 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 166 | } |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 167 | |
| 168 | /* TODO: these two are always zero */ |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 169 | out->state.flags = 0x0; |
| 170 | out->state.hangs = 0x0; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 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 | |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 181 | mutex_unlock(&mgr->lock); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 182 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv) |
| 186 | { |
| 187 | struct idr *idp; |
| 188 | struct amdgpu_ctx *ctx; |
| 189 | uint32_t id; |
| 190 | struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; |
| 191 | idp = &mgr->ctx_handles; |
| 192 | |
| 193 | idr_for_each_entry(idp,ctx,id) { |
| 194 | if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1) |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 195 | DRM_ERROR("ctx %p is still alive\n", ctx); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Christian König | cdecb65 | 2015-07-16 12:01:06 +0200 | [diff] [blame] | 198 | idr_destroy(&mgr->ctx_handles); |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 199 | mutex_destroy(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 203 | struct drm_file *filp) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 204 | { |
| 205 | int r; |
| 206 | uint32_t id; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 207 | |
| 208 | union drm_amdgpu_ctx *args = data; |
| 209 | struct amdgpu_device *adev = dev->dev_private; |
| 210 | struct amdgpu_fpriv *fpriv = filp->driver_priv; |
| 211 | |
| 212 | r = 0; |
| 213 | id = args->in.ctx_id; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 214 | |
| 215 | switch (args->in.op) { |
| 216 | case AMDGPU_CTX_OP_ALLOC_CTX: |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 217 | r = amdgpu_ctx_alloc(adev, fpriv, &id); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 218 | args->out.alloc.ctx_id = id; |
| 219 | break; |
| 220 | case AMDGPU_CTX_OP_FREE_CTX: |
| 221 | r = amdgpu_ctx_free(adev, fpriv, id); |
| 222 | break; |
| 223 | case AMDGPU_CTX_OP_QUERY_STATE: |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 224 | r = amdgpu_ctx_query(adev, fpriv, id, &args->out); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 225 | break; |
| 226 | default: |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | return r; |
| 231 | } |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 232 | |
| 233 | struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id) |
| 234 | { |
| 235 | struct amdgpu_ctx *ctx; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 236 | struct amdgpu_ctx_mgr *mgr; |
| 237 | |
| 238 | if (!fpriv) |
| 239 | return NULL; |
| 240 | |
| 241 | mgr = &fpriv->ctx_mgr; |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 242 | |
| 243 | mutex_lock(&mgr->lock); |
| 244 | ctx = idr_find(&mgr->ctx_handles, id); |
| 245 | if (ctx) |
| 246 | kref_get(&ctx->refcount); |
| 247 | mutex_unlock(&mgr->lock); |
| 248 | return ctx; |
| 249 | } |
| 250 | |
| 251 | int amdgpu_ctx_put(struct amdgpu_ctx *ctx) |
| 252 | { |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 253 | if (ctx == NULL) |
| 254 | return -EINVAL; |
| 255 | |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 256 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 257 | return 0; |
| 258 | } |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 259 | |
| 260 | uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring, |
Chunming Zhou | d1ff908 | 2015-07-30 17:59:43 +0800 | [diff] [blame] | 261 | struct fence *fence, uint64_t queued_seq) |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 262 | { |
| 263 | struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx]; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 264 | uint64_t seq = 0; |
| 265 | unsigned idx = 0; |
| 266 | struct fence *other = NULL; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 267 | |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 268 | if (amdgpu_enable_scheduler) |
Chunming Zhou | d1ff908 | 2015-07-30 17:59:43 +0800 | [diff] [blame] | 269 | seq = queued_seq; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 270 | else |
| 271 | seq = cring->sequence; |
| 272 | idx = seq % AMDGPU_CTX_MAX_CS_PENDING; |
| 273 | other = cring->fences[idx]; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 274 | if (other) { |
| 275 | signed long r; |
| 276 | r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT); |
| 277 | if (r < 0) |
| 278 | DRM_ERROR("Error (%ld) waiting for fence!\n", r); |
| 279 | } |
| 280 | |
| 281 | fence_get(fence); |
| 282 | |
| 283 | spin_lock(&ctx->ring_lock); |
| 284 | cring->fences[idx] = fence; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 285 | if (!amdgpu_enable_scheduler) |
| 286 | cring->sequence++; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 287 | spin_unlock(&ctx->ring_lock); |
| 288 | |
| 289 | fence_put(other); |
| 290 | |
| 291 | return seq; |
| 292 | } |
| 293 | |
| 294 | struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, |
| 295 | struct amdgpu_ring *ring, uint64_t seq) |
| 296 | { |
| 297 | struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx]; |
| 298 | struct fence *fence; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 299 | uint64_t queued_seq; |
Chunming Zhou | 4b559c9 | 2015-07-21 15:53:04 +0800 | [diff] [blame] | 300 | int r; |
| 301 | |
| 302 | if (amdgpu_enable_scheduler) { |
| 303 | r = amd_sched_wait_emit(&cring->c_entity, |
| 304 | seq, |
Chunming Zhou | 51b9db2 | 2015-07-28 17:31:04 +0800 | [diff] [blame] | 305 | false, |
| 306 | -1); |
Chunming Zhou | 4b559c9 | 2015-07-21 15:53:04 +0800 | [diff] [blame] | 307 | if (r) |
| 308 | return NULL; |
| 309 | } |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 310 | |
| 311 | spin_lock(&ctx->ring_lock); |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 312 | if (amdgpu_enable_scheduler) |
Jammy Zhou | 27f6642 | 2015-08-03 10:27:57 +0800 | [diff] [blame^] | 313 | queued_seq = amd_sched_next_queued_seq(&cring->c_entity); |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 314 | else |
| 315 | queued_seq = cring->sequence; |
| 316 | |
| 317 | if (seq >= queued_seq) { |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 318 | spin_unlock(&ctx->ring_lock); |
| 319 | return ERR_PTR(-EINVAL); |
| 320 | } |
| 321 | |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 322 | |
| 323 | if (seq + AMDGPU_CTX_MAX_CS_PENDING < queued_seq) { |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 324 | spin_unlock(&ctx->ring_lock); |
| 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]); |
| 329 | spin_unlock(&ctx->ring_lock); |
| 330 | |
| 331 | return fence; |
| 332 | } |