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