Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [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 | * |
| 23 | */ |
| 24 | #include <linux/kthread.h> |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <drm/drmP.h> |
| 28 | #include "gpu_scheduler.h" |
| 29 | |
Chunming Zhou | 353da3c | 2015-09-07 16:06:53 +0800 | [diff] [blame] | 30 | #define CREATE_TRACE_POINTS |
| 31 | #include "gpu_sched_trace.h" |
| 32 | |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 33 | static struct amd_sched_job * |
| 34 | amd_sched_entity_pop_job(struct amd_sched_entity *entity); |
Christian König | 8807900 | 2015-08-24 14:29:40 +0200 | [diff] [blame] | 35 | static void amd_sched_wakeup(struct amd_gpu_scheduler *sched); |
| 36 | |
Chunming Zhou | f5617f9 | 2015-11-05 11:41:50 +0800 | [diff] [blame] | 37 | struct kmem_cache *sched_fence_slab; |
| 38 | atomic_t sched_fence_slab_ref = ATOMIC_INIT(0); |
| 39 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 40 | /* Initialize a given run queue struct */ |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 41 | static void amd_sched_rq_init(struct amd_sched_rq *rq) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 42 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 43 | spin_lock_init(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 44 | INIT_LIST_HEAD(&rq->entities); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 45 | rq->current_entity = NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 46 | } |
| 47 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 48 | static void amd_sched_rq_add_entity(struct amd_sched_rq *rq, |
| 49 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 50 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 51 | spin_lock(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 52 | list_add_tail(&entity->list, &rq->entities); |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 53 | spin_unlock(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq, |
| 57 | struct amd_sched_entity *entity) |
| 58 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 59 | spin_lock(&rq->lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 60 | list_del_init(&entity->list); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 61 | if (rq->current_entity == entity) |
| 62 | rq->current_entity = NULL; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 63 | spin_unlock(&rq->lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 67 | * Select next job from a specified run queue with round robin policy. |
| 68 | * Return NULL if nothing available. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 69 | */ |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 70 | static struct amd_sched_job * |
| 71 | amd_sched_rq_select_job(struct amd_sched_rq *rq) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 72 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 73 | struct amd_sched_entity *entity; |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 74 | struct amd_sched_job *sched_job; |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame] | 75 | |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 76 | spin_lock(&rq->lock); |
| 77 | |
| 78 | entity = rq->current_entity; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 79 | if (entity) { |
| 80 | list_for_each_entry_continue(entity, &rq->entities, list) { |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 81 | sched_job = amd_sched_entity_pop_job(entity); |
| 82 | if (sched_job) { |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 83 | rq->current_entity = entity; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 84 | spin_unlock(&rq->lock); |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 85 | return sched_job; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 86 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 87 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 88 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 89 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 90 | list_for_each_entry(entity, &rq->entities, list) { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 91 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 92 | sched_job = amd_sched_entity_pop_job(entity); |
| 93 | if (sched_job) { |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 94 | rq->current_entity = entity; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 95 | spin_unlock(&rq->lock); |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 96 | return sched_job; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 97 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 98 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 99 | if (entity == rq->current_entity) |
| 100 | break; |
| 101 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 102 | |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 103 | spin_unlock(&rq->lock); |
| 104 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 105 | return NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 109 | * Init a context entity used by scheduler when submit to HW ring. |
| 110 | * |
| 111 | * @sched The pointer to the scheduler |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 112 | * @entity The pointer to a valid amd_sched_entity |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 113 | * @rq The run queue this entity belongs |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame] | 114 | * @kernel If this is an entity for the kernel |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 115 | * @jobs The max number of jobs in the job queue |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 116 | * |
| 117 | * return 0 if succeed. negative error code on failure |
| 118 | */ |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 119 | int amd_sched_entity_init(struct amd_gpu_scheduler *sched, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 120 | struct amd_sched_entity *entity, |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 121 | struct amd_sched_rq *rq, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 122 | uint32_t jobs) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 123 | { |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 124 | int r; |
| 125 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 126 | if (!(sched && entity && rq)) |
| 127 | return -EINVAL; |
| 128 | |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 129 | memset(entity, 0, sizeof(struct amd_sched_entity)); |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 130 | INIT_LIST_HEAD(&entity->list); |
| 131 | entity->rq = rq; |
| 132 | entity->sched = sched; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 133 | |
| 134 | spin_lock_init(&entity->queue_lock); |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 135 | r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL); |
| 136 | if (r) |
| 137 | return r; |
| 138 | |
Christian König | ce882e6 | 2015-08-19 15:00:55 +0200 | [diff] [blame] | 139 | atomic_set(&entity->fence_seq, 0); |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 140 | entity->fence_context = fence_context_alloc(1); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 141 | |
| 142 | /* Add the entity to the run queue */ |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 143 | amd_sched_rq_add_entity(rq, entity); |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 144 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Query if entity is initialized |
| 150 | * |
| 151 | * @sched Pointer to scheduler instance |
| 152 | * @entity The pointer to a valid scheduler entity |
| 153 | * |
| 154 | * return true if entity is initialized, false otherwise |
| 155 | */ |
Christian König | d54fdb9 | 2015-08-20 17:03:48 +0200 | [diff] [blame] | 156 | static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched, |
| 157 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 158 | { |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 159 | return entity->sched == sched && |
| 160 | entity->rq != NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 163 | /** |
| 164 | * Check if entity is idle |
| 165 | * |
| 166 | * @entity The pointer to a valid scheduler entity |
| 167 | * |
| 168 | * Return true if entity don't has any unscheduled jobs. |
| 169 | */ |
| 170 | static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 171 | { |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 172 | rmb(); |
| 173 | if (kfifo_is_empty(&entity->job_queue)) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 174 | return true; |
| 175 | |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Destroy a context entity |
| 181 | * |
| 182 | * @sched Pointer to scheduler instance |
| 183 | * @entity The pointer to a valid scheduler entity |
| 184 | * |
Christian König | 062c7fb | 2015-08-21 15:46:43 +0200 | [diff] [blame] | 185 | * Cleanup and free the allocated resources. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 186 | */ |
Christian König | 062c7fb | 2015-08-21 15:46:43 +0200 | [diff] [blame] | 187 | void amd_sched_entity_fini(struct amd_gpu_scheduler *sched, |
| 188 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 189 | { |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 190 | struct amd_sched_rq *rq = entity->rq; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 191 | |
Christian König | d54fdb9 | 2015-08-20 17:03:48 +0200 | [diff] [blame] | 192 | if (!amd_sched_entity_is_initialized(sched, entity)) |
Christian König | 062c7fb | 2015-08-21 15:46:43 +0200 | [diff] [blame] | 193 | return; |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 194 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 195 | /** |
| 196 | * The client will not queue more IBs during this fini, consume existing |
| 197 | * queued IBs |
| 198 | */ |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 199 | wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity)); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 200 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 201 | amd_sched_rq_remove_entity(rq, entity); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 202 | kfifo_free(&entity->job_queue); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 203 | } |
| 204 | |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 205 | static void amd_sched_entity_wakeup(struct fence *f, struct fence_cb *cb) |
| 206 | { |
| 207 | struct amd_sched_entity *entity = |
| 208 | container_of(cb, struct amd_sched_entity, cb); |
| 209 | entity->dependency = NULL; |
| 210 | fence_put(f); |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 211 | amd_sched_wakeup(entity->sched); |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame^] | 214 | static bool amd_sched_entity_add_dependency_cb(struct amd_sched_entity *entity) |
| 215 | { |
| 216 | struct amd_gpu_scheduler *sched = entity->sched; |
| 217 | struct fence * fence = entity->dependency; |
| 218 | struct amd_sched_fence *s_fence; |
| 219 | |
| 220 | if (fence->context == entity->fence_context) { |
| 221 | /* We can ignore fences from ourself */ |
| 222 | fence_put(entity->dependency); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | s_fence = to_amd_sched_fence(fence); |
| 227 | if (s_fence && s_fence->sched == sched) { |
| 228 | /* Fence is from the same scheduler */ |
| 229 | if (test_bit(AMD_SCHED_FENCE_SCHEDULED_BIT, &fence->flags)) { |
| 230 | /* Ignore it when it is already scheduled */ |
| 231 | fence_put(entity->dependency); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | /* Wait for fence to be scheduled */ |
| 236 | entity->cb.func = amd_sched_entity_wakeup; |
| 237 | list_add_tail(&entity->cb.node, &s_fence->scheduled_cb); |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | if (!fence_add_callback(entity->dependency, &entity->cb, |
| 242 | amd_sched_entity_wakeup)) |
| 243 | return true; |
| 244 | |
| 245 | fence_put(entity->dependency); |
| 246 | return false; |
| 247 | } |
| 248 | |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 249 | static struct amd_sched_job * |
| 250 | amd_sched_entity_pop_job(struct amd_sched_entity *entity) |
| 251 | { |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 252 | struct amd_gpu_scheduler *sched = entity->sched; |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 253 | struct amd_sched_job *sched_job; |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 254 | |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 255 | if (ACCESS_ONCE(entity->dependency)) |
| 256 | return NULL; |
| 257 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 258 | if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job))) |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 259 | return NULL; |
| 260 | |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame^] | 261 | while ((entity->dependency = sched->ops->dependency(sched_job))) |
| 262 | if (amd_sched_entity_add_dependency_cb(entity)) |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 263 | return NULL; |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 264 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 265 | return sched_job; |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 268 | /** |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 269 | * Helper to submit a job to the job queue |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 270 | * |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 271 | * @sched_job The pointer to job required to submit |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 272 | * |
| 273 | * Returns true if we could submit the job. |
| 274 | */ |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 275 | static bool amd_sched_entity_in(struct amd_sched_job *sched_job) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 276 | { |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 277 | struct amd_sched_entity *entity = sched_job->s_entity; |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 278 | bool added, first = false; |
| 279 | |
| 280 | spin_lock(&entity->queue_lock); |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 281 | added = kfifo_in(&entity->job_queue, &sched_job, |
| 282 | sizeof(sched_job)) == sizeof(sched_job); |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 283 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 284 | if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job)) |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 285 | first = true; |
| 286 | |
| 287 | spin_unlock(&entity->queue_lock); |
| 288 | |
| 289 | /* first job wakes up scheduler */ |
| 290 | if (first) |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 291 | amd_sched_wakeup(sched_job->sched); |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 292 | |
| 293 | return added; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Submit a job to the job queue |
| 298 | * |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 299 | * @sched_job The pointer to job required to submit |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 300 | * |
| 301 | * Returns 0 for success, negative error code otherwise. |
| 302 | */ |
Christian König | e284022 | 2015-11-05 19:49:48 +0100 | [diff] [blame] | 303 | void amd_sched_entity_push_job(struct amd_sched_job *sched_job) |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 304 | { |
| 305 | struct amd_sched_entity *entity = sched_job->s_entity; |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 306 | |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 307 | wait_event(entity->sched->job_scheduled, |
Chunming Zhou | c9f0fe5 | 2015-08-31 15:46:12 +0800 | [diff] [blame] | 308 | amd_sched_entity_in(sched_job)); |
Chunming Zhou | 353da3c | 2015-09-07 16:06:53 +0800 | [diff] [blame] | 309 | trace_amd_sched_job(sched_job); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 310 | } |
| 311 | |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 312 | /** |
| 313 | * Return ture if we can push more jobs to the hw. |
| 314 | */ |
| 315 | static bool amd_sched_ready(struct amd_gpu_scheduler *sched) |
| 316 | { |
| 317 | return atomic_read(&sched->hw_rq_count) < |
| 318 | sched->hw_submission_limit; |
| 319 | } |
| 320 | |
| 321 | /** |
Christian König | 8807900 | 2015-08-24 14:29:40 +0200 | [diff] [blame] | 322 | * Wake up the scheduler when it is ready |
| 323 | */ |
| 324 | static void amd_sched_wakeup(struct amd_gpu_scheduler *sched) |
| 325 | { |
| 326 | if (amd_sched_ready(sched)) |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 327 | wake_up_interruptible(&sched->wake_up_worker); |
Christian König | 8807900 | 2015-08-24 14:29:40 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 331 | * Select next to run |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 332 | */ |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 333 | static struct amd_sched_job * |
| 334 | amd_sched_select_job(struct amd_gpu_scheduler *sched) |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 335 | { |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 336 | struct amd_sched_job *sched_job; |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 337 | |
| 338 | if (!amd_sched_ready(sched)) |
| 339 | return NULL; |
| 340 | |
| 341 | /* Kernel run queue has higher priority than normal run queue*/ |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 342 | sched_job = amd_sched_rq_select_job(&sched->kernel_rq); |
| 343 | if (sched_job == NULL) |
| 344 | sched_job = amd_sched_rq_select_job(&sched->sched_rq); |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 345 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 346 | return sched_job; |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 349 | static void amd_sched_process_job(struct fence *f, struct fence_cb *cb) |
| 350 | { |
Christian König | 258f3f9 | 2015-08-31 17:02:52 +0200 | [diff] [blame] | 351 | struct amd_sched_fence *s_fence = |
| 352 | container_of(cb, struct amd_sched_fence, cb); |
Christian König | 9b398fa | 2015-09-07 18:16:49 +0200 | [diff] [blame] | 353 | struct amd_gpu_scheduler *sched = s_fence->sched; |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 354 | unsigned long flags; |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 355 | |
Christian König | c746ba2 | 2015-08-19 16:12:15 +0200 | [diff] [blame] | 356 | atomic_dec(&sched->hw_rq_count); |
Christian König | 258f3f9 | 2015-08-31 17:02:52 +0200 | [diff] [blame] | 357 | amd_sched_fence_signal(s_fence); |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 358 | if (sched->timeout != MAX_SCHEDULE_TIMEOUT) { |
Christian König | 424839a | 2015-11-02 16:25:10 +0100 | [diff] [blame] | 359 | cancel_delayed_work(&s_fence->dwork); |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 360 | spin_lock_irqsave(&sched->fence_list_lock, flags); |
| 361 | list_del_init(&s_fence->list); |
| 362 | spin_unlock_irqrestore(&sched->fence_list_lock, flags); |
| 363 | } |
Chunming Zhou | 7034dec | 2015-11-11 14:56:00 +0800 | [diff] [blame] | 364 | trace_amd_sched_process_job(s_fence); |
Christian König | 258f3f9 | 2015-08-31 17:02:52 +0200 | [diff] [blame] | 365 | fence_put(&s_fence->base); |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 366 | wake_up_interruptible(&sched->wake_up_worker); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 369 | static void amd_sched_fence_work_func(struct work_struct *work) |
| 370 | { |
| 371 | struct amd_sched_fence *s_fence = |
| 372 | container_of(work, struct amd_sched_fence, dwork.work); |
| 373 | struct amd_gpu_scheduler *sched = s_fence->sched; |
| 374 | struct amd_sched_fence *entity, *tmp; |
| 375 | unsigned long flags; |
| 376 | |
| 377 | DRM_ERROR("[%s] scheduler is timeout!\n", sched->name); |
| 378 | |
| 379 | /* Clean all pending fences */ |
Junwei Zhang | 2fcef6e | 2015-10-13 11:14:23 +0800 | [diff] [blame] | 380 | spin_lock_irqsave(&sched->fence_list_lock, flags); |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 381 | list_for_each_entry_safe(entity, tmp, &sched->fence_list, list) { |
| 382 | DRM_ERROR(" fence no %d\n", entity->base.seqno); |
Junwei Zhang | 2fcef6e | 2015-10-13 11:14:23 +0800 | [diff] [blame] | 383 | cancel_delayed_work(&entity->dwork); |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 384 | list_del_init(&entity->list); |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 385 | fence_put(&entity->base); |
| 386 | } |
Junwei Zhang | 2fcef6e | 2015-10-13 11:14:23 +0800 | [diff] [blame] | 387 | spin_unlock_irqrestore(&sched->fence_list_lock, flags); |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 388 | } |
| 389 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 390 | static int amd_sched_main(void *param) |
| 391 | { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 392 | struct sched_param sparam = {.sched_priority = 1}; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 393 | struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param; |
Alex Deucher | 5134e99 | 2015-09-04 00:11:54 -0400 | [diff] [blame] | 394 | int r, count; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 395 | |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 396 | spin_lock_init(&sched->fence_list_lock); |
| 397 | INIT_LIST_HEAD(&sched->fence_list); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 398 | sched_setscheduler(current, SCHED_FIFO, &sparam); |
| 399 | |
| 400 | while (!kthread_should_stop()) { |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 401 | struct amd_sched_entity *entity; |
Christian König | 258f3f9 | 2015-08-31 17:02:52 +0200 | [diff] [blame] | 402 | struct amd_sched_fence *s_fence; |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 403 | struct amd_sched_job *sched_job; |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 404 | struct fence *fence; |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 405 | unsigned long flags; |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 406 | |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 407 | wait_event_interruptible(sched->wake_up_worker, |
Christian König | f85a6dd | 2015-08-19 17:37:52 +0200 | [diff] [blame] | 408 | kthread_should_stop() || |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 409 | (sched_job = amd_sched_select_job(sched))); |
Christian König | f85a6dd | 2015-08-19 17:37:52 +0200 | [diff] [blame] | 410 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 411 | if (!sched_job) |
Christian König | f85a6dd | 2015-08-19 17:37:52 +0200 | [diff] [blame] | 412 | continue; |
| 413 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 414 | entity = sched_job->s_entity; |
| 415 | s_fence = sched_job->s_fence; |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 416 | |
| 417 | if (sched->timeout != MAX_SCHEDULE_TIMEOUT) { |
| 418 | INIT_DELAYED_WORK(&s_fence->dwork, amd_sched_fence_work_func); |
| 419 | schedule_delayed_work(&s_fence->dwork, sched->timeout); |
| 420 | spin_lock_irqsave(&sched->fence_list_lock, flags); |
| 421 | list_add_tail(&s_fence->list, &sched->fence_list); |
| 422 | spin_unlock_irqrestore(&sched->fence_list_lock, flags); |
| 423 | } |
| 424 | |
Christian König | b034b57 | 2015-08-20 17:08:25 +0200 | [diff] [blame] | 425 | atomic_inc(&sched->hw_rq_count); |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 426 | fence = sched->ops->run_job(sched_job); |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame^] | 427 | amd_sched_fence_scheduled(s_fence); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 428 | if (fence) { |
Christian König | 258f3f9 | 2015-08-31 17:02:52 +0200 | [diff] [blame] | 429 | r = fence_add_callback(fence, &s_fence->cb, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 430 | amd_sched_process_job); |
| 431 | if (r == -ENOENT) |
Christian König | 258f3f9 | 2015-08-31 17:02:52 +0200 | [diff] [blame] | 432 | amd_sched_process_job(fence, &s_fence->cb); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 433 | else if (r) |
| 434 | DRM_ERROR("fence add callback failed (%d)\n", r); |
| 435 | fence_put(fence); |
Christian König | 27439fc | 2015-09-02 12:03:06 +0200 | [diff] [blame] | 436 | } else { |
| 437 | DRM_ERROR("Failed to run job!\n"); |
Christian König | 258f3f9 | 2015-08-31 17:02:52 +0200 | [diff] [blame] | 438 | amd_sched_process_job(NULL, &s_fence->cb); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 439 | } |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 440 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 441 | count = kfifo_out(&entity->job_queue, &sched_job, |
| 442 | sizeof(sched_job)); |
| 443 | WARN_ON(count != sizeof(sched_job)); |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 444 | wake_up(&sched->job_scheduled); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 445 | } |
| 446 | return 0; |
| 447 | } |
| 448 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 449 | /** |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 450 | * Init a gpu scheduler instance |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 451 | * |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 452 | * @sched The pointer to the scheduler |
Christian König | 69f7dd6 | 2015-08-20 17:24:40 +0200 | [diff] [blame] | 453 | * @ops The backend operations for this scheduler. |
Christian König | 69f7dd6 | 2015-08-20 17:24:40 +0200 | [diff] [blame] | 454 | * @hw_submissions Number of hw submissions to do. |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 455 | * @name Name used for debugging |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 456 | * |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 457 | * Return 0 on success, otherwise error code. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 458 | */ |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 459 | int amd_sched_init(struct amd_gpu_scheduler *sched, |
| 460 | struct amd_sched_backend_ops *ops, |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 461 | unsigned hw_submission, long timeout, const char *name) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 462 | { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 463 | sched->ops = ops; |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 464 | sched->hw_submission_limit = hw_submission; |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 465 | sched->name = name; |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 466 | sched->timeout = timeout; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 467 | amd_sched_rq_init(&sched->sched_rq); |
| 468 | amd_sched_rq_init(&sched->kernel_rq); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 469 | |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 470 | init_waitqueue_head(&sched->wake_up_worker); |
| 471 | init_waitqueue_head(&sched->job_scheduled); |
Christian König | c746ba2 | 2015-08-19 16:12:15 +0200 | [diff] [blame] | 472 | atomic_set(&sched->hw_rq_count, 0); |
Chunming Zhou | f5617f9 | 2015-11-05 11:41:50 +0800 | [diff] [blame] | 473 | if (atomic_inc_return(&sched_fence_slab_ref) == 1) { |
| 474 | sched_fence_slab = kmem_cache_create( |
| 475 | "amd_sched_fence", sizeof(struct amd_sched_fence), 0, |
| 476 | SLAB_HWCACHE_ALIGN, NULL); |
| 477 | if (!sched_fence_slab) |
| 478 | return -ENOMEM; |
| 479 | } |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 480 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 481 | /* Each scheduler will run on a seperate kernel thread */ |
Christian König | c14692f0 | 2015-08-21 15:18:47 +0200 | [diff] [blame] | 482 | sched->thread = kthread_run(amd_sched_main, sched, sched->name); |
Christian König | f495659 | 2015-08-20 16:59:38 +0200 | [diff] [blame] | 483 | if (IS_ERR(sched->thread)) { |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 484 | DRM_ERROR("Failed to create scheduler for %s.\n", name); |
| 485 | return PTR_ERR(sched->thread); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 486 | } |
| 487 | |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 488 | return 0; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Destroy a gpu scheduler |
| 493 | * |
| 494 | * @sched The pointer to the scheduler |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 495 | */ |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 496 | void amd_sched_fini(struct amd_gpu_scheduler *sched) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 497 | { |
Dave Airlie | 32544d0 | 2015-11-03 11:10:03 -0500 | [diff] [blame] | 498 | if (sched->thread) |
| 499 | kthread_stop(sched->thread); |
Chunming Zhou | f5617f9 | 2015-11-05 11:41:50 +0800 | [diff] [blame] | 500 | if (atomic_dec_and_test(&sched_fence_slab_ref)) |
| 501 | kmem_cache_destroy(sched_fence_slab); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 502 | } |