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, |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame^] | 53 | struct amdgpu_ctx *ctx) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 54 | { |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 55 | int i; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 56 | memset(ctx, 0, sizeof(*ctx)); |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 57 | ctx->adev = adev; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 58 | kref_init(&ctx->refcount); |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 59 | spin_lock_init(&ctx->ring_lock); |
| 60 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 61 | ctx->rings[i].sequence = 1; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, |
| 65 | uint32_t *id) |
| 66 | { |
| 67 | struct amdgpu_ctx *ctx; |
| 68 | int i, j, r; |
| 69 | |
| 70 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); |
| 71 | if (!ctx) |
| 72 | return -ENOMEM; |
| 73 | if (fpriv) { |
| 74 | struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; |
| 75 | mutex_lock(&mgr->lock); |
| 76 | r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL); |
| 77 | if (r < 0) { |
| 78 | mutex_unlock(&mgr->lock); |
| 79 | kfree(ctx); |
| 80 | return r; |
| 81 | } |
| 82 | *id = (uint32_t)r; |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame^] | 83 | amdgpu_ctx_init(adev, fpriv, ctx); |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 84 | mutex_unlock(&mgr->lock); |
| 85 | } else { |
| 86 | if (adev->kernel_ctx) { |
| 87 | DRM_ERROR("kernel cnotext has been created.\n"); |
| 88 | kfree(ctx); |
| 89 | return 0; |
| 90 | } |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame^] | 91 | amdgpu_ctx_init(adev, fpriv, ctx); |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 92 | |
| 93 | adev->kernel_ctx = ctx; |
| 94 | } |
| 95 | |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 96 | if (amdgpu_enable_scheduler) { |
| 97 | /* create context entity for each ring */ |
| 98 | for (i = 0; i < adev->num_rings; i++) { |
| 99 | struct amd_run_queue *rq; |
| 100 | if (fpriv) |
| 101 | rq = &adev->rings[i]->scheduler->sched_rq; |
| 102 | else |
| 103 | rq = &adev->rings[i]->scheduler->kernel_rq; |
| 104 | r = amd_context_entity_init(adev->rings[i]->scheduler, |
| 105 | &ctx->rings[i].c_entity, |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame^] | 106 | NULL, rq, amdgpu_sched_jobs); |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 107 | if (r) |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | if (i < adev->num_rings) { |
| 112 | for (j = 0; j < i; j++) |
| 113 | amd_context_entity_fini(adev->rings[j]->scheduler, |
| 114 | &ctx->rings[j].c_entity); |
| 115 | kfree(ctx); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id) |
| 124 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 125 | struct amdgpu_ctx *ctx; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 126 | |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 127 | if (fpriv) { |
| 128 | struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; |
| 129 | mutex_lock(&mgr->lock); |
| 130 | ctx = idr_find(&mgr->ctx_handles, id); |
| 131 | if (ctx) { |
| 132 | idr_remove(&mgr->ctx_handles, id); |
| 133 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
| 134 | mutex_unlock(&mgr->lock); |
| 135 | return 0; |
| 136 | } |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 137 | mutex_unlock(&mgr->lock); |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 138 | } else { |
| 139 | ctx = adev->kernel_ctx; |
| 140 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
Marek Olšák | f11358d | 2015-05-05 00:56:45 +0200 | [diff] [blame] | 141 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 142 | } |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 146 | static int amdgpu_ctx_query(struct amdgpu_device *adev, |
| 147 | struct amdgpu_fpriv *fpriv, uint32_t id, |
| 148 | union drm_amdgpu_ctx_out *out) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 149 | { |
| 150 | struct amdgpu_ctx *ctx; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 151 | struct amdgpu_ctx_mgr *mgr; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 152 | unsigned reset_counter; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 153 | |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 154 | if (!fpriv) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | mgr = &fpriv->ctx_mgr; |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 158 | mutex_lock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 159 | ctx = idr_find(&mgr->ctx_handles, id); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 160 | if (!ctx) { |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 161 | mutex_unlock(&mgr->lock); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 162 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 163 | } |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 164 | |
| 165 | /* TODO: these two are always zero */ |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 166 | out->state.flags = 0x0; |
| 167 | out->state.hangs = 0x0; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 168 | |
| 169 | /* determine if a GPU reset has occured since the last call */ |
| 170 | reset_counter = atomic_read(&adev->gpu_reset_counter); |
| 171 | /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */ |
| 172 | if (ctx->reset_counter == reset_counter) |
| 173 | out->state.reset_status = AMDGPU_CTX_NO_RESET; |
| 174 | else |
| 175 | out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET; |
| 176 | ctx->reset_counter = reset_counter; |
| 177 | |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 178 | mutex_unlock(&mgr->lock); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 179 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 182 | int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 183 | struct drm_file *filp) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 184 | { |
| 185 | int r; |
| 186 | uint32_t id; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 187 | |
| 188 | union drm_amdgpu_ctx *args = data; |
| 189 | struct amdgpu_device *adev = dev->dev_private; |
| 190 | struct amdgpu_fpriv *fpriv = filp->driver_priv; |
| 191 | |
| 192 | r = 0; |
| 193 | id = args->in.ctx_id; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 194 | |
| 195 | switch (args->in.op) { |
| 196 | case AMDGPU_CTX_OP_ALLOC_CTX: |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 197 | r = amdgpu_ctx_alloc(adev, fpriv, &id); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 198 | args->out.alloc.ctx_id = id; |
| 199 | break; |
| 200 | case AMDGPU_CTX_OP_FREE_CTX: |
| 201 | r = amdgpu_ctx_free(adev, fpriv, id); |
| 202 | break; |
| 203 | case AMDGPU_CTX_OP_QUERY_STATE: |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 204 | r = amdgpu_ctx_query(adev, fpriv, id, &args->out); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 205 | break; |
| 206 | default: |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
| 210 | return r; |
| 211 | } |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 212 | |
| 213 | struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id) |
| 214 | { |
| 215 | struct amdgpu_ctx *ctx; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 216 | struct amdgpu_ctx_mgr *mgr; |
| 217 | |
| 218 | if (!fpriv) |
| 219 | return NULL; |
| 220 | |
| 221 | mgr = &fpriv->ctx_mgr; |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 222 | |
| 223 | mutex_lock(&mgr->lock); |
| 224 | ctx = idr_find(&mgr->ctx_handles, id); |
| 225 | if (ctx) |
| 226 | kref_get(&ctx->refcount); |
| 227 | mutex_unlock(&mgr->lock); |
| 228 | return ctx; |
| 229 | } |
| 230 | |
| 231 | int amdgpu_ctx_put(struct amdgpu_ctx *ctx) |
| 232 | { |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 233 | if (ctx == NULL) |
| 234 | return -EINVAL; |
| 235 | |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 236 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 237 | return 0; |
| 238 | } |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 239 | |
| 240 | 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] | 241 | struct fence *fence, uint64_t queued_seq) |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 242 | { |
| 243 | struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx]; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 244 | uint64_t seq = 0; |
| 245 | unsigned idx = 0; |
| 246 | struct fence *other = NULL; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 247 | |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 248 | if (amdgpu_enable_scheduler) |
Chunming Zhou | d1ff908 | 2015-07-30 17:59:43 +0800 | [diff] [blame] | 249 | seq = queued_seq; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 250 | else |
| 251 | seq = cring->sequence; |
| 252 | idx = seq % AMDGPU_CTX_MAX_CS_PENDING; |
| 253 | other = cring->fences[idx]; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 254 | if (other) { |
| 255 | signed long r; |
| 256 | r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT); |
| 257 | if (r < 0) |
| 258 | DRM_ERROR("Error (%ld) waiting for fence!\n", r); |
| 259 | } |
| 260 | |
| 261 | fence_get(fence); |
| 262 | |
| 263 | spin_lock(&ctx->ring_lock); |
| 264 | cring->fences[idx] = fence; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 265 | if (!amdgpu_enable_scheduler) |
| 266 | cring->sequence++; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 267 | spin_unlock(&ctx->ring_lock); |
| 268 | |
| 269 | fence_put(other); |
| 270 | |
| 271 | return seq; |
| 272 | } |
| 273 | |
| 274 | struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, |
| 275 | struct amdgpu_ring *ring, uint64_t seq) |
| 276 | { |
| 277 | struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx]; |
| 278 | struct fence *fence; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 279 | uint64_t queued_seq; |
Chunming Zhou | 4b559c9 | 2015-07-21 15:53:04 +0800 | [diff] [blame] | 280 | int r; |
| 281 | |
| 282 | if (amdgpu_enable_scheduler) { |
| 283 | r = amd_sched_wait_emit(&cring->c_entity, |
| 284 | seq, |
Chunming Zhou | 51b9db2 | 2015-07-28 17:31:04 +0800 | [diff] [blame] | 285 | false, |
| 286 | -1); |
Chunming Zhou | 4b559c9 | 2015-07-21 15:53:04 +0800 | [diff] [blame] | 287 | if (r) |
| 288 | return NULL; |
| 289 | } |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 290 | |
| 291 | spin_lock(&ctx->ring_lock); |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 292 | if (amdgpu_enable_scheduler) |
Jammy Zhou | 27f6642 | 2015-08-03 10:27:57 +0800 | [diff] [blame] | 293 | queued_seq = amd_sched_next_queued_seq(&cring->c_entity); |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 294 | else |
| 295 | queued_seq = cring->sequence; |
| 296 | |
| 297 | if (seq >= queued_seq) { |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 298 | spin_unlock(&ctx->ring_lock); |
| 299 | return ERR_PTR(-EINVAL); |
| 300 | } |
| 301 | |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 302 | |
| 303 | if (seq + AMDGPU_CTX_MAX_CS_PENDING < queued_seq) { |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 304 | spin_unlock(&ctx->ring_lock); |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]); |
| 309 | spin_unlock(&ctx->ring_lock); |
| 310 | |
| 311 | return fence; |
| 312 | } |
Christian König | efd4ccb | 2015-08-04 16:20:31 +0200 | [diff] [blame] | 313 | |
| 314 | void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr) |
| 315 | { |
| 316 | mutex_init(&mgr->lock); |
| 317 | idr_init(&mgr->ctx_handles); |
| 318 | } |
| 319 | |
| 320 | void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr) |
| 321 | { |
| 322 | struct amdgpu_ctx *ctx; |
| 323 | struct idr *idp; |
| 324 | uint32_t id; |
| 325 | |
| 326 | idp = &mgr->ctx_handles; |
| 327 | |
| 328 | idr_for_each_entry(idp, ctx, id) { |
| 329 | if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1) |
| 330 | DRM_ERROR("ctx %p is still alive\n", ctx); |
| 331 | } |
| 332 | |
| 333 | idr_destroy(&mgr->ctx_handles); |
| 334 | mutex_destroy(&mgr->lock); |
| 335 | } |