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