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 | |
| 30 | /* Initialize a given run queue struct */ |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 31 | static void amd_sched_rq_init(struct amd_sched_rq *rq) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 32 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 33 | spin_lock_init(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 34 | INIT_LIST_HEAD(&rq->entities); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 35 | rq->current_entity = NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 36 | } |
| 37 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 38 | static void amd_sched_rq_add_entity(struct amd_sched_rq *rq, |
| 39 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 40 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 41 | spin_lock(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 42 | list_add_tail(&entity->list, &rq->entities); |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 43 | spin_unlock(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq, |
| 47 | struct amd_sched_entity *entity) |
| 48 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 49 | spin_lock(&rq->lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 50 | list_del_init(&entity->list); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 51 | if (rq->current_entity == entity) |
| 52 | rq->current_entity = NULL; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 53 | spin_unlock(&rq->lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Select next entity from a specified run queue with round robin policy. |
| 58 | * It could return the same entity as current one if current is the only |
| 59 | * available one in the queue. Return NULL if nothing available. |
| 60 | */ |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 61 | static struct amd_sched_entity * |
| 62 | amd_sched_rq_select_entity(struct amd_sched_rq *rq) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 63 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 64 | struct amd_sched_entity *entity; |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame] | 65 | |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 66 | spin_lock(&rq->lock); |
| 67 | |
| 68 | entity = rq->current_entity; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 69 | if (entity) { |
| 70 | list_for_each_entry_continue(entity, &rq->entities, list) { |
| 71 | if (!kfifo_is_empty(&entity->job_queue)) { |
| 72 | rq->current_entity = entity; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 73 | spin_unlock(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 74 | return rq->current_entity; |
| 75 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 76 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 77 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 78 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 79 | list_for_each_entry(entity, &rq->entities, list) { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 80 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 81 | if (!kfifo_is_empty(&entity->job_queue)) { |
| 82 | rq->current_entity = entity; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 83 | spin_unlock(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 84 | return rq->current_entity; |
| 85 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 86 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 87 | if (entity == rq->current_entity) |
| 88 | break; |
| 89 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 90 | |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 91 | spin_unlock(&rq->lock); |
| 92 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 93 | return NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 97 | * Init a context entity used by scheduler when submit to HW ring. |
| 98 | * |
| 99 | * @sched The pointer to the scheduler |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 100 | * @entity The pointer to a valid amd_sched_entity |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 101 | * @rq The run queue this entity belongs |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame] | 102 | * @kernel If this is an entity for the kernel |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 103 | * @jobs The max number of jobs in the job queue |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 104 | * |
| 105 | * return 0 if succeed. negative error code on failure |
| 106 | */ |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 107 | int amd_sched_entity_init(struct amd_gpu_scheduler *sched, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 108 | struct amd_sched_entity *entity, |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 109 | struct amd_sched_rq *rq, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 110 | uint32_t jobs) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 111 | { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 112 | if (!(sched && entity && rq)) |
| 113 | return -EINVAL; |
| 114 | |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 115 | memset(entity, 0, sizeof(struct amd_sched_entity)); |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 116 | entity->belongto_rq = rq; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 117 | entity->scheduler = sched; |
| 118 | init_waitqueue_head(&entity->wait_queue); |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 119 | entity->fence_context = fence_context_alloc(1); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 120 | if(kfifo_alloc(&entity->job_queue, |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 121 | jobs * sizeof(void *), |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 122 | GFP_KERNEL)) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | spin_lock_init(&entity->queue_lock); |
Christian König | ce882e6 | 2015-08-19 15:00:55 +0200 | [diff] [blame] | 126 | atomic_set(&entity->fence_seq, 0); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 127 | |
| 128 | /* Add the entity to the run queue */ |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 129 | amd_sched_rq_add_entity(rq, entity); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Query if entity is initialized |
| 135 | * |
| 136 | * @sched Pointer to scheduler instance |
| 137 | * @entity The pointer to a valid scheduler entity |
| 138 | * |
| 139 | * return true if entity is initialized, false otherwise |
| 140 | */ |
Christian König | d54fdb9 | 2015-08-20 17:03:48 +0200 | [diff] [blame] | 141 | static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched, |
| 142 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 143 | { |
| 144 | return entity->scheduler == sched && |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 145 | entity->belongto_rq != NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 146 | } |
| 147 | |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 148 | /** |
| 149 | * Check if entity is idle |
| 150 | * |
| 151 | * @entity The pointer to a valid scheduler entity |
| 152 | * |
| 153 | * Return true if entity don't has any unscheduled jobs. |
| 154 | */ |
| 155 | static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 156 | { |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 157 | rmb(); |
| 158 | if (kfifo_is_empty(&entity->job_queue)) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 159 | return true; |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Destroy a context entity |
| 166 | * |
| 167 | * @sched Pointer to scheduler instance |
| 168 | * @entity The pointer to a valid scheduler entity |
| 169 | * |
| 170 | * return 0 if succeed. negative error code on failure |
| 171 | */ |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 172 | int amd_sched_entity_fini(struct amd_gpu_scheduler *sched, |
| 173 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 174 | { |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 175 | struct amd_sched_rq *rq = entity->belongto_rq; |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 176 | long r; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 177 | |
Christian König | d54fdb9 | 2015-08-20 17:03:48 +0200 | [diff] [blame] | 178 | if (!amd_sched_entity_is_initialized(sched, entity)) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 179 | return 0; |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 180 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 181 | /** |
| 182 | * The client will not queue more IBs during this fini, consume existing |
| 183 | * queued IBs |
| 184 | */ |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 185 | r = wait_event_timeout(entity->wait_queue, |
| 186 | amd_sched_entity_is_idle(entity), |
| 187 | msecs_to_jiffies(AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS)); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 188 | |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 189 | if (r <= 0) |
Christian König | 9788ec4 | 2015-08-19 17:34:20 +0200 | [diff] [blame] | 190 | DRM_INFO("Entity %p is in waiting state during fini\n", |
| 191 | entity); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 192 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 193 | amd_sched_rq_remove_entity(rq, entity); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 194 | kfifo_free(&entity->job_queue); |
| 195 | return r; |
| 196 | } |
| 197 | |
| 198 | /** |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 199 | * Helper to submit a job to the job queue |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 200 | * |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 201 | * @job The pointer to job required to submit |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 202 | * |
| 203 | * Returns true if we could submit the job. |
| 204 | */ |
| 205 | static bool amd_sched_entity_in(struct amd_sched_job *job) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 206 | { |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 207 | struct amd_sched_entity *entity = job->s_entity; |
| 208 | bool added, first = false; |
| 209 | |
| 210 | spin_lock(&entity->queue_lock); |
| 211 | added = kfifo_in(&entity->job_queue, &job, sizeof(job)) == sizeof(job); |
| 212 | |
| 213 | if (added && kfifo_len(&entity->job_queue) == sizeof(job)) |
| 214 | first = true; |
| 215 | |
| 216 | spin_unlock(&entity->queue_lock); |
| 217 | |
| 218 | /* first job wakes up scheduler */ |
| 219 | if (first) |
| 220 | wake_up_interruptible(&job->sched->wait_queue); |
| 221 | |
| 222 | return added; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Submit a job to the job queue |
| 227 | * |
| 228 | * @job The pointer to job required to submit |
| 229 | * |
| 230 | * Returns 0 for success, negative error code otherwise. |
| 231 | */ |
| 232 | int amd_sched_entity_push_job(struct amd_sched_job *sched_job) |
| 233 | { |
| 234 | struct amd_sched_entity *entity = sched_job->s_entity; |
| 235 | struct amd_sched_fence *fence = amd_sched_fence_create(entity); |
| 236 | int r; |
| 237 | |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 238 | if (!fence) |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 239 | return -ENOMEM; |
| 240 | |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 241 | fence_get(&fence->base); |
| 242 | sched_job->s_fence = fence; |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 243 | |
| 244 | r = wait_event_interruptible(entity->wait_queue, |
| 245 | amd_sched_entity_in(sched_job)); |
| 246 | |
| 247 | return r; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 250 | /** |
| 251 | * Return ture if we can push more jobs to the hw. |
| 252 | */ |
| 253 | static bool amd_sched_ready(struct amd_gpu_scheduler *sched) |
| 254 | { |
| 255 | return atomic_read(&sched->hw_rq_count) < |
| 256 | sched->hw_submission_limit; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Select next entity containing real IB submissions |
| 261 | */ |
| 262 | static struct amd_sched_entity * |
| 263 | amd_sched_select_context(struct amd_gpu_scheduler *sched) |
| 264 | { |
| 265 | struct amd_sched_entity *tmp; |
| 266 | |
| 267 | if (!amd_sched_ready(sched)) |
| 268 | return NULL; |
| 269 | |
| 270 | /* Kernel run queue has higher priority than normal run queue*/ |
| 271 | tmp = amd_sched_rq_select_entity(&sched->kernel_rq); |
| 272 | if (tmp == NULL) |
| 273 | tmp = amd_sched_rq_select_entity(&sched->sched_rq); |
| 274 | |
| 275 | return tmp; |
| 276 | } |
| 277 | |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 278 | static void amd_sched_process_job(struct fence *f, struct fence_cb *cb) |
| 279 | { |
| 280 | struct amd_sched_job *sched_job = |
| 281 | container_of(cb, struct amd_sched_job, cb); |
| 282 | struct amd_gpu_scheduler *sched; |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 283 | |
| 284 | sched = sched_job->sched; |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 285 | amd_sched_fence_signal(sched_job->s_fence); |
Christian König | c746ba2 | 2015-08-19 16:12:15 +0200 | [diff] [blame] | 286 | atomic_dec(&sched->hw_rq_count); |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 287 | fence_put(&sched_job->s_fence->base); |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 288 | sched->ops->process_job(sched, sched_job); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 289 | wake_up_interruptible(&sched->wait_queue); |
| 290 | } |
| 291 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 292 | static int amd_sched_main(void *param) |
| 293 | { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 294 | struct sched_param sparam = {.sched_priority = 1}; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 295 | struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param; |
Christian König | f85a6dd | 2015-08-19 17:37:52 +0200 | [diff] [blame] | 296 | int r; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 297 | |
| 298 | sched_setscheduler(current, SCHED_FIFO, &sparam); |
| 299 | |
| 300 | while (!kthread_should_stop()) { |
Christian König | f85a6dd | 2015-08-19 17:37:52 +0200 | [diff] [blame] | 301 | struct amd_sched_entity *c_entity = NULL; |
| 302 | struct amd_sched_job *job; |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 303 | struct fence *fence; |
| 304 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 305 | wait_event_interruptible(sched->wait_queue, |
Christian König | f85a6dd | 2015-08-19 17:37:52 +0200 | [diff] [blame] | 306 | kthread_should_stop() || |
| 307 | (c_entity = amd_sched_select_context(sched))); |
| 308 | |
| 309 | if (!c_entity) |
| 310 | continue; |
| 311 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 312 | r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *)); |
| 313 | if (r != sizeof(void *)) |
| 314 | continue; |
Christian König | b034b57 | 2015-08-20 17:08:25 +0200 | [diff] [blame] | 315 | atomic_inc(&sched->hw_rq_count); |
| 316 | |
Chunming Zhou | 953e8fd | 2015-08-06 15:19:12 +0800 | [diff] [blame] | 317 | fence = sched->ops->run_job(sched, c_entity, job); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 318 | if (fence) { |
Chunming Zhou | 953e8fd | 2015-08-06 15:19:12 +0800 | [diff] [blame] | 319 | r = fence_add_callback(fence, &job->cb, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 320 | amd_sched_process_job); |
| 321 | if (r == -ENOENT) |
Chunming Zhou | 953e8fd | 2015-08-06 15:19:12 +0800 | [diff] [blame] | 322 | amd_sched_process_job(fence, &job->cb); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 323 | else if (r) |
| 324 | DRM_ERROR("fence add callback failed (%d)\n", r); |
| 325 | fence_put(fence); |
| 326 | } |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 327 | |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 328 | wake_up(&c_entity->wait_queue); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 333 | /** |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 334 | * Create a gpu scheduler |
| 335 | * |
Christian König | 69f7dd6 | 2015-08-20 17:24:40 +0200 | [diff] [blame] | 336 | * @ops The backend operations for this scheduler. |
| 337 | * @ring The the ring id for the scheduler. |
| 338 | * @hw_submissions Number of hw submissions to do. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 339 | * |
Christian König | 69f7dd6 | 2015-08-20 17:24:40 +0200 | [diff] [blame] | 340 | * Return the pointer to scheduler for success, otherwise return NULL |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 341 | */ |
Christian König | 69f7dd6 | 2015-08-20 17:24:40 +0200 | [diff] [blame] | 342 | struct amd_gpu_scheduler *amd_sched_create(struct amd_sched_backend_ops *ops, |
| 343 | unsigned ring, unsigned hw_submission) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 344 | { |
| 345 | struct amd_gpu_scheduler *sched; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 346 | |
| 347 | sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL); |
| 348 | if (!sched) |
| 349 | return NULL; |
| 350 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 351 | sched->ops = ops; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 352 | sched->ring_id = ring; |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 353 | sched->hw_submission_limit = hw_submission; |
Christian König | c14692f0 | 2015-08-21 15:18:47 +0200 | [diff] [blame^] | 354 | snprintf(sched->name, sizeof(sched->name), "amdgpu[%d]", ring); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 355 | amd_sched_rq_init(&sched->sched_rq); |
| 356 | amd_sched_rq_init(&sched->kernel_rq); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 357 | |
| 358 | init_waitqueue_head(&sched->wait_queue); |
Christian König | c746ba2 | 2015-08-19 16:12:15 +0200 | [diff] [blame] | 359 | atomic_set(&sched->hw_rq_count, 0); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 360 | /* Each scheduler will run on a seperate kernel thread */ |
Christian König | c14692f0 | 2015-08-21 15:18:47 +0200 | [diff] [blame^] | 361 | sched->thread = kthread_run(amd_sched_main, sched, sched->name); |
Christian König | f495659 | 2015-08-20 16:59:38 +0200 | [diff] [blame] | 362 | if (IS_ERR(sched->thread)) { |
| 363 | DRM_ERROR("Failed to create scheduler for id %d.\n", ring); |
| 364 | kfree(sched); |
| 365 | return NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 366 | } |
| 367 | |
Christian König | f495659 | 2015-08-20 16:59:38 +0200 | [diff] [blame] | 368 | return sched; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Destroy a gpu scheduler |
| 373 | * |
| 374 | * @sched The pointer to the scheduler |
| 375 | * |
| 376 | * return 0 if succeed. -1 if failed. |
| 377 | */ |
| 378 | int amd_sched_destroy(struct amd_gpu_scheduler *sched) |
| 379 | { |
| 380 | kthread_stop(sched->thread); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 381 | kfree(sched); |
| 382 | return 0; |
| 383 | } |