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