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> |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 26 | #include <drm/drm_auth.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 27 | #include "amdgpu.h" |
Andres Rodriguez | 52c6a62 | 2017-06-26 16:17:13 -0400 | [diff] [blame] | 28 | #include "amdgpu_sched.h" |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 29 | |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 30 | static int amdgpu_ctx_priority_permit(struct drm_file *filp, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 31 | enum drm_sched_priority priority) |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 32 | { |
| 33 | /* NORMAL and below are accessible by everyone */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 34 | if (priority <= DRM_SCHED_PRIORITY_NORMAL) |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 35 | return 0; |
| 36 | |
| 37 | if (capable(CAP_SYS_NICE)) |
| 38 | return 0; |
| 39 | |
| 40 | if (drm_is_current_master(filp)) |
| 41 | return 0; |
| 42 | |
| 43 | return -EACCES; |
| 44 | } |
| 45 | |
| 46 | static int amdgpu_ctx_init(struct amdgpu_device *adev, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 47 | enum drm_sched_priority priority, |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 48 | struct drm_file *filp, |
| 49 | struct amdgpu_ctx *ctx) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 50 | { |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 51 | unsigned i, j; |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 52 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 53 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 54 | if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX) |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 55 | return -EINVAL; |
| 56 | |
| 57 | r = amdgpu_ctx_priority_permit(filp, priority); |
| 58 | if (r) |
| 59 | return r; |
| 60 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 61 | memset(ctx, 0, sizeof(*ctx)); |
Chunming Zhou | 9cb7e5a | 2015-07-21 13:17:19 +0800 | [diff] [blame] | 62 | ctx->adev = adev; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 63 | kref_init(&ctx->refcount); |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 64 | spin_lock_init(&ctx->ring_lock); |
Christian König | a750b47 | 2016-02-11 10:20:53 +0100 | [diff] [blame] | 65 | ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 66 | sizeof(struct dma_fence*), GFP_KERNEL); |
Chunming Zhou | 37cd0ca | 2015-12-10 15:45:11 +0800 | [diff] [blame] | 67 | if (!ctx->fences) |
| 68 | return -ENOMEM; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 69 | |
Andrey Grodzovsky | 0ae9444 | 2017-10-10 16:50:17 -0400 | [diff] [blame] | 70 | mutex_init(&ctx->lock); |
| 71 | |
Chunming Zhou | 37cd0ca | 2015-12-10 15:45:11 +0800 | [diff] [blame] | 72 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
| 73 | ctx->rings[i].sequence = 1; |
Christian König | a750b47 | 2016-02-11 10:20:53 +0100 | [diff] [blame] | 74 | ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i]; |
Chunming Zhou | 37cd0ca | 2015-12-10 15:45:11 +0800 | [diff] [blame] | 75 | } |
Nicolai Hähnle | ce199ad | 2016-10-04 09:43:30 +0200 | [diff] [blame] | 76 | |
| 77 | ctx->reset_counter = atomic_read(&adev->gpu_reset_counter); |
Monk Liu | 668ca1b | 2017-10-17 14:39:23 +0800 | [diff] [blame] | 78 | ctx->reset_counter_query = ctx->reset_counter; |
Christian König | e55f2b6 | 2017-10-09 15:18:43 +0200 | [diff] [blame] | 79 | ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter); |
Andres Rodriguez | c23be4a | 2017-06-06 20:20:38 -0400 | [diff] [blame] | 80 | ctx->init_priority = priority; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 81 | ctx->override_priority = DRM_SCHED_PRIORITY_UNSET; |
Nicolai Hähnle | ce199ad | 2016-10-04 09:43:30 +0200 | [diff] [blame] | 82 | |
Chunming Zhou | cadf97b | 2016-01-15 11:25:00 +0800 | [diff] [blame] | 83 | /* create context entity for each ring */ |
| 84 | for (i = 0; i < adev->num_rings; i++) { |
Christian König | 2087417 | 2016-02-11 09:56:44 +0100 | [diff] [blame] | 85 | struct amdgpu_ring *ring = adev->rings[i]; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 86 | struct drm_sched_rq *rq; |
Christian König | 2087417 | 2016-02-11 09:56:44 +0100 | [diff] [blame] | 87 | |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 88 | rq = &ring->sched.sched_rq[priority]; |
Monk Liu | 75fbed2 | 2017-05-11 13:36:33 +0800 | [diff] [blame] | 89 | |
| 90 | if (ring == &adev->gfx.kiq.ring) |
| 91 | continue; |
| 92 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 93 | r = drm_sched_entity_init(&ring->sched, &ctx->rings[i].entity, |
Monk Liu | 1102900 | 2017-10-23 12:25:24 +0800 | [diff] [blame] | 94 | rq, amdgpu_sched_jobs, &ctx->guilty); |
Chunming Zhou | cadf97b | 2016-01-15 11:25:00 +0800 | [diff] [blame] | 95 | if (r) |
Huang Rui | 8ed8147 | 2016-10-26 17:07:03 +0800 | [diff] [blame] | 96 | goto failed; |
Chunming Zhou | cadf97b | 2016-01-15 11:25:00 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Andres Rodriguez | effd924 | 2017-02-16 00:47:32 -0500 | [diff] [blame] | 99 | r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr); |
| 100 | if (r) |
| 101 | goto failed; |
| 102 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 103 | return 0; |
Huang Rui | 8ed8147 | 2016-10-26 17:07:03 +0800 | [diff] [blame] | 104 | |
| 105 | failed: |
| 106 | for (j = 0; j < i; j++) |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 107 | drm_sched_entity_fini(&adev->rings[j]->sched, |
Huang Rui | 8ed8147 | 2016-10-26 17:07:03 +0800 | [diff] [blame] | 108 | &ctx->rings[j].entity); |
| 109 | kfree(ctx->fences); |
| 110 | ctx->fences = NULL; |
| 111 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 114 | static void amdgpu_ctx_fini(struct kref *ref) |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 115 | { |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 116 | struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount); |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 117 | struct amdgpu_device *adev = ctx->adev; |
| 118 | unsigned i, j; |
| 119 | |
Dave Airlie | fe295b2 | 2015-11-03 11:07:11 -0500 | [diff] [blame] | 120 | if (!adev) |
| 121 | return; |
| 122 | |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 123 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
Chunming Zhou | 37cd0ca | 2015-12-10 15:45:11 +0800 | [diff] [blame] | 124 | for (j = 0; j < amdgpu_sched_jobs; ++j) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 125 | dma_fence_put(ctx->rings[i].fences[j]); |
Chunming Zhou | 37cd0ca | 2015-12-10 15:45:11 +0800 | [diff] [blame] | 126 | kfree(ctx->fences); |
Grazvydas Ignotas | 54ddf3a | 2016-09-25 23:34:46 +0300 | [diff] [blame] | 127 | ctx->fences = NULL; |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 128 | |
Andres Rodriguez | effd924 | 2017-02-16 00:47:32 -0500 | [diff] [blame] | 129 | amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr); |
Andrey Grodzovsky | 0ae9444 | 2017-10-10 16:50:17 -0400 | [diff] [blame] | 130 | |
| 131 | mutex_destroy(&ctx->lock); |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 132 | |
| 133 | kfree(ctx); |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static int amdgpu_ctx_alloc(struct amdgpu_device *adev, |
| 137 | struct amdgpu_fpriv *fpriv, |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 138 | struct drm_file *filp, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 139 | enum drm_sched_priority priority, |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 140 | uint32_t *id) |
| 141 | { |
| 142 | struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; |
| 143 | struct amdgpu_ctx *ctx; |
| 144 | int r; |
| 145 | |
| 146 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); |
| 147 | if (!ctx) |
| 148 | return -ENOMEM; |
| 149 | |
| 150 | mutex_lock(&mgr->lock); |
| 151 | r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL); |
| 152 | if (r < 0) { |
| 153 | mutex_unlock(&mgr->lock); |
| 154 | kfree(ctx); |
| 155 | return r; |
| 156 | } |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 157 | |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 158 | *id = (uint32_t)r; |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 159 | r = amdgpu_ctx_init(adev, priority, filp, ctx); |
Chunming Zhou | c648ed7 | 2015-12-10 15:50:02 +0800 | [diff] [blame] | 160 | if (r) { |
| 161 | idr_remove(&mgr->ctx_handles, *id); |
| 162 | *id = 0; |
| 163 | kfree(ctx); |
| 164 | } |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 165 | mutex_unlock(&mgr->lock); |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 166 | return r; |
| 167 | } |
| 168 | |
| 169 | static void amdgpu_ctx_do_release(struct kref *ref) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 170 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 171 | struct amdgpu_ctx *ctx; |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 172 | u32 i; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 173 | |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 174 | ctx = container_of(ref, struct amdgpu_ctx, refcount); |
| 175 | |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 176 | for (i = 0; i < ctx->adev->num_rings; i++) |
| 177 | drm_sched_entity_fini(&ctx->adev->rings[i]->sched, |
| 178 | &ctx->rings[i].entity); |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 179 | |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 180 | amdgpu_ctx_fini(ref); |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id) |
| 184 | { |
| 185 | struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr; |
| 186 | struct amdgpu_ctx *ctx; |
| 187 | |
| 188 | mutex_lock(&mgr->lock); |
Matthew Wilcox | d3e709e | 2016-12-22 13:30:22 -0500 | [diff] [blame] | 189 | ctx = idr_remove(&mgr->ctx_handles, id); |
| 190 | if (ctx) |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 191 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
Christian König | 47f3850 | 2015-08-04 17:51:05 +0200 | [diff] [blame] | 192 | mutex_unlock(&mgr->lock); |
Matthew Wilcox | d3e709e | 2016-12-22 13:30:22 -0500 | [diff] [blame] | 193 | return ctx ? 0 : -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 196 | static int amdgpu_ctx_query(struct amdgpu_device *adev, |
| 197 | struct amdgpu_fpriv *fpriv, uint32_t id, |
| 198 | union drm_amdgpu_ctx_out *out) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 199 | { |
| 200 | struct amdgpu_ctx *ctx; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 201 | struct amdgpu_ctx_mgr *mgr; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 202 | unsigned reset_counter; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 203 | |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 204 | if (!fpriv) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | mgr = &fpriv->ctx_mgr; |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 208 | mutex_lock(&mgr->lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 209 | ctx = idr_find(&mgr->ctx_handles, id); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 210 | if (!ctx) { |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 211 | mutex_unlock(&mgr->lock); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 212 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 213 | } |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 214 | |
| 215 | /* TODO: these two are always zero */ |
Alex Deucher | 0b492a4 | 2015-08-16 22:48:26 -0400 | [diff] [blame] | 216 | out->state.flags = 0x0; |
| 217 | out->state.hangs = 0x0; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 218 | |
| 219 | /* determine if a GPU reset has occured since the last call */ |
| 220 | reset_counter = atomic_read(&adev->gpu_reset_counter); |
| 221 | /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */ |
Monk Liu | 668ca1b | 2017-10-17 14:39:23 +0800 | [diff] [blame] | 222 | if (ctx->reset_counter_query == reset_counter) |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 223 | out->state.reset_status = AMDGPU_CTX_NO_RESET; |
| 224 | else |
| 225 | out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET; |
Monk Liu | 668ca1b | 2017-10-17 14:39:23 +0800 | [diff] [blame] | 226 | ctx->reset_counter_query = reset_counter; |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 227 | |
Marek Olšák | 0147ee0 | 2015-05-05 20:52:00 +0200 | [diff] [blame] | 228 | mutex_unlock(&mgr->lock); |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 229 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Monk Liu | bc1b1bf | 2017-10-17 14:58:01 +0800 | [diff] [blame] | 232 | static int amdgpu_ctx_query2(struct amdgpu_device *adev, |
| 233 | struct amdgpu_fpriv *fpriv, uint32_t id, |
| 234 | union drm_amdgpu_ctx_out *out) |
| 235 | { |
| 236 | struct amdgpu_ctx *ctx; |
| 237 | struct amdgpu_ctx_mgr *mgr; |
| 238 | |
| 239 | if (!fpriv) |
| 240 | return -EINVAL; |
| 241 | |
| 242 | mgr = &fpriv->ctx_mgr; |
| 243 | mutex_lock(&mgr->lock); |
| 244 | ctx = idr_find(&mgr->ctx_handles, id); |
| 245 | if (!ctx) { |
| 246 | mutex_unlock(&mgr->lock); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | out->state.flags = 0x0; |
| 251 | out->state.hangs = 0x0; |
| 252 | |
| 253 | if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter)) |
| 254 | out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET; |
| 255 | |
| 256 | if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter)) |
| 257 | out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST; |
| 258 | |
| 259 | if (atomic_read(&ctx->guilty)) |
| 260 | out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY; |
| 261 | |
| 262 | mutex_unlock(&mgr->lock); |
| 263 | return 0; |
| 264 | } |
| 265 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 266 | int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 267 | struct drm_file *filp) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 268 | { |
| 269 | int r; |
| 270 | uint32_t id; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 271 | enum drm_sched_priority priority; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 272 | |
| 273 | union drm_amdgpu_ctx *args = data; |
| 274 | struct amdgpu_device *adev = dev->dev_private; |
| 275 | struct amdgpu_fpriv *fpriv = filp->driver_priv; |
| 276 | |
| 277 | r = 0; |
| 278 | id = args->in.ctx_id; |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 279 | priority = amdgpu_to_sched_priority(args->in.priority); |
| 280 | |
Andres Rodriguez | b6d8a43 | 2017-05-24 17:00:10 -0400 | [diff] [blame] | 281 | /* For backwards compatibility reasons, we need to accept |
| 282 | * ioctls with garbage in the priority field */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 283 | if (priority == DRM_SCHED_PRIORITY_INVALID) |
| 284 | priority = DRM_SCHED_PRIORITY_NORMAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 285 | |
| 286 | switch (args->in.op) { |
Christian König | a750b47 | 2016-02-11 10:20:53 +0100 | [diff] [blame] | 287 | case AMDGPU_CTX_OP_ALLOC_CTX: |
Andres Rodriguez | c2636dc | 2016-12-22 17:06:50 -0500 | [diff] [blame] | 288 | r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id); |
Christian König | a750b47 | 2016-02-11 10:20:53 +0100 | [diff] [blame] | 289 | args->out.alloc.ctx_id = id; |
| 290 | break; |
| 291 | case AMDGPU_CTX_OP_FREE_CTX: |
| 292 | r = amdgpu_ctx_free(fpriv, id); |
| 293 | break; |
| 294 | case AMDGPU_CTX_OP_QUERY_STATE: |
| 295 | r = amdgpu_ctx_query(adev, fpriv, id, &args->out); |
| 296 | break; |
Monk Liu | bc1b1bf | 2017-10-17 14:58:01 +0800 | [diff] [blame] | 297 | case AMDGPU_CTX_OP_QUERY_STATE2: |
| 298 | r = amdgpu_ctx_query2(adev, fpriv, id, &args->out); |
| 299 | break; |
Christian König | a750b47 | 2016-02-11 10:20:53 +0100 | [diff] [blame] | 300 | default: |
| 301 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return r; |
| 305 | } |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 306 | |
| 307 | struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id) |
| 308 | { |
| 309 | struct amdgpu_ctx *ctx; |
Chunming Zhou | 23ca0e4 | 2015-07-06 13:42:58 +0800 | [diff] [blame] | 310 | struct amdgpu_ctx_mgr *mgr; |
| 311 | |
| 312 | if (!fpriv) |
| 313 | return NULL; |
| 314 | |
| 315 | mgr = &fpriv->ctx_mgr; |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 316 | |
| 317 | mutex_lock(&mgr->lock); |
| 318 | ctx = idr_find(&mgr->ctx_handles, id); |
| 319 | if (ctx) |
| 320 | kref_get(&ctx->refcount); |
| 321 | mutex_unlock(&mgr->lock); |
| 322 | return ctx; |
| 323 | } |
| 324 | |
| 325 | int amdgpu_ctx_put(struct amdgpu_ctx *ctx) |
| 326 | { |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 327 | if (ctx == NULL) |
| 328 | return -EINVAL; |
| 329 | |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 330 | kref_put(&ctx->refcount, amdgpu_ctx_do_release); |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 331 | return 0; |
| 332 | } |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 333 | |
Monk Liu | eb01abc | 2017-09-15 13:40:31 +0800 | [diff] [blame] | 334 | int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring, |
| 335 | struct dma_fence *fence, uint64_t* handler) |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 336 | { |
| 337 | struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx]; |
Christian König | ce882e6 | 2015-08-19 15:00:55 +0200 | [diff] [blame] | 338 | uint64_t seq = cring->sequence; |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 339 | unsigned idx = 0; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 340 | struct dma_fence *other = NULL; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 341 | |
Chunming Zhou | 5b01123 | 2015-12-10 17:34:33 +0800 | [diff] [blame] | 342 | idx = seq & (amdgpu_sched_jobs - 1); |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 343 | other = cring->fences[idx]; |
Andrey Grodzovsky | 0ae9444 | 2017-10-10 16:50:17 -0400 | [diff] [blame] | 344 | if (other) |
| 345 | BUG_ON(!dma_fence_is_signaled(other)); |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 346 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 347 | dma_fence_get(fence); |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 348 | |
| 349 | spin_lock(&ctx->ring_lock); |
| 350 | cring->fences[idx] = fence; |
Christian König | ce882e6 | 2015-08-19 15:00:55 +0200 | [diff] [blame] | 351 | cring->sequence++; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 352 | spin_unlock(&ctx->ring_lock); |
| 353 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 354 | dma_fence_put(other); |
Monk Liu | eb01abc | 2017-09-15 13:40:31 +0800 | [diff] [blame] | 355 | if (handler) |
| 356 | *handler = seq; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 357 | |
Monk Liu | eb01abc | 2017-09-15 13:40:31 +0800 | [diff] [blame] | 358 | return 0; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 361 | struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx, |
| 362 | struct amdgpu_ring *ring, uint64_t seq) |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 363 | { |
| 364 | struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx]; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 365 | struct dma_fence *fence; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 366 | |
| 367 | spin_lock(&ctx->ring_lock); |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 368 | |
Monk Liu | d7b1eeb | 2017-04-07 18:39:07 +0800 | [diff] [blame] | 369 | if (seq == ~0ull) |
| 370 | seq = ctx->rings[ring->idx].sequence - 1; |
| 371 | |
Christian König | ce882e6 | 2015-08-19 15:00:55 +0200 | [diff] [blame] | 372 | if (seq >= cring->sequence) { |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 373 | spin_unlock(&ctx->ring_lock); |
| 374 | return ERR_PTR(-EINVAL); |
| 375 | } |
| 376 | |
Chunming Zhou | b43a9a7 | 2015-07-21 15:13:53 +0800 | [diff] [blame] | 377 | |
Chunming Zhou | 37cd0ca | 2015-12-10 15:45:11 +0800 | [diff] [blame] | 378 | if (seq + amdgpu_sched_jobs < cring->sequence) { |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 379 | spin_unlock(&ctx->ring_lock); |
| 380 | return NULL; |
| 381 | } |
| 382 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 383 | fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]); |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 384 | spin_unlock(&ctx->ring_lock); |
| 385 | |
| 386 | return fence; |
| 387 | } |
Christian König | efd4ccb | 2015-08-04 16:20:31 +0200 | [diff] [blame] | 388 | |
Andres Rodriguez | c23be4a | 2017-06-06 20:20:38 -0400 | [diff] [blame] | 389 | void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 390 | enum drm_sched_priority priority) |
Andres Rodriguez | c23be4a | 2017-06-06 20:20:38 -0400 | [diff] [blame] | 391 | { |
| 392 | int i; |
| 393 | struct amdgpu_device *adev = ctx->adev; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 394 | struct drm_sched_rq *rq; |
| 395 | struct drm_sched_entity *entity; |
Andres Rodriguez | c23be4a | 2017-06-06 20:20:38 -0400 | [diff] [blame] | 396 | struct amdgpu_ring *ring; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 397 | enum drm_sched_priority ctx_prio; |
Andres Rodriguez | c23be4a | 2017-06-06 20:20:38 -0400 | [diff] [blame] | 398 | |
| 399 | ctx->override_priority = priority; |
| 400 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 401 | ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ? |
Andres Rodriguez | c23be4a | 2017-06-06 20:20:38 -0400 | [diff] [blame] | 402 | ctx->init_priority : ctx->override_priority; |
| 403 | |
| 404 | for (i = 0; i < adev->num_rings; i++) { |
| 405 | ring = adev->rings[i]; |
| 406 | entity = &ctx->rings[i].entity; |
| 407 | rq = &ring->sched.sched_rq[ctx_prio]; |
| 408 | |
| 409 | if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ) |
| 410 | continue; |
| 411 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 412 | drm_sched_entity_set_rq(entity, rq); |
Andres Rodriguez | c23be4a | 2017-06-06 20:20:38 -0400 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Andrey Grodzovsky | 0ae9444 | 2017-10-10 16:50:17 -0400 | [diff] [blame] | 416 | int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx, unsigned ring_id) |
| 417 | { |
| 418 | struct amdgpu_ctx_ring *cring = &ctx->rings[ring_id]; |
| 419 | unsigned idx = cring->sequence & (amdgpu_sched_jobs - 1); |
| 420 | struct dma_fence *other = cring->fences[idx]; |
| 421 | |
| 422 | if (other) { |
| 423 | signed long r; |
Andrey Grodzovsky | 719a39a | 2018-04-30 10:04:42 -0400 | [diff] [blame] | 424 | r = dma_fence_wait(other, true); |
Andrey Grodzovsky | 0ae9444 | 2017-10-10 16:50:17 -0400 | [diff] [blame] | 425 | if (r < 0) { |
Andrey Grodzovsky | 719a39a | 2018-04-30 10:04:42 -0400 | [diff] [blame] | 426 | if (r != -ERESTARTSYS) |
| 427 | DRM_ERROR("Error (%ld) waiting for fence!\n", r); |
| 428 | |
Andrey Grodzovsky | 0ae9444 | 2017-10-10 16:50:17 -0400 | [diff] [blame] | 429 | return r; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Christian König | efd4ccb | 2015-08-04 16:20:31 +0200 | [diff] [blame] | 436 | void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr) |
| 437 | { |
| 438 | mutex_init(&mgr->lock); |
| 439 | idr_init(&mgr->ctx_handles); |
| 440 | } |
| 441 | |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 442 | void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr) |
| 443 | { |
| 444 | struct amdgpu_ctx *ctx; |
| 445 | struct idr *idp; |
| 446 | uint32_t id, i; |
| 447 | |
| 448 | idp = &mgr->ctx_handles; |
| 449 | |
| 450 | idr_for_each_entry(idp, ctx, id) { |
| 451 | |
| 452 | if (!ctx->adev) |
| 453 | return; |
| 454 | |
| 455 | for (i = 0; i < ctx->adev->num_rings; i++) |
| 456 | if (kref_read(&ctx->refcount) == 1) |
| 457 | drm_sched_entity_do_release(&ctx->adev->rings[i]->sched, |
| 458 | &ctx->rings[i].entity); |
| 459 | else |
| 460 | DRM_ERROR("ctx %p is still alive\n", ctx); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | void amdgpu_ctx_mgr_entity_cleanup(struct amdgpu_ctx_mgr *mgr) |
| 465 | { |
| 466 | struct amdgpu_ctx *ctx; |
| 467 | struct idr *idp; |
| 468 | uint32_t id, i; |
| 469 | |
| 470 | idp = &mgr->ctx_handles; |
| 471 | |
| 472 | idr_for_each_entry(idp, ctx, id) { |
| 473 | |
| 474 | if (!ctx->adev) |
| 475 | return; |
| 476 | |
| 477 | for (i = 0; i < ctx->adev->num_rings; i++) |
| 478 | if (kref_read(&ctx->refcount) == 1) |
| 479 | drm_sched_entity_cleanup(&ctx->adev->rings[i]->sched, |
| 480 | &ctx->rings[i].entity); |
| 481 | else |
| 482 | DRM_ERROR("ctx %p is still alive\n", ctx); |
| 483 | } |
| 484 | } |
| 485 | |
Christian König | efd4ccb | 2015-08-04 16:20:31 +0200 | [diff] [blame] | 486 | void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr) |
| 487 | { |
| 488 | struct amdgpu_ctx *ctx; |
| 489 | struct idr *idp; |
| 490 | uint32_t id; |
| 491 | |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 492 | amdgpu_ctx_mgr_entity_cleanup(mgr); |
| 493 | |
Christian König | efd4ccb | 2015-08-04 16:20:31 +0200 | [diff] [blame] | 494 | idp = &mgr->ctx_handles; |
| 495 | |
| 496 | idr_for_each_entry(idp, ctx, id) { |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 497 | if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1) |
Christian König | efd4ccb | 2015-08-04 16:20:31 +0200 | [diff] [blame] | 498 | DRM_ERROR("ctx %p is still alive\n", ctx); |
| 499 | } |
| 500 | |
| 501 | idr_destroy(&mgr->ctx_handles); |
| 502 | mutex_destroy(&mgr->lock); |
| 503 | } |