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 */ |
| 31 | static void init_rq(struct amd_run_queue *rq) |
| 32 | { |
| 33 | INIT_LIST_HEAD(&rq->head.list); |
| 34 | rq->head.belongto_rq = rq; |
| 35 | mutex_init(&rq->lock); |
| 36 | atomic_set(&rq->nr_entity, 0); |
| 37 | rq->current_entity = &rq->head; |
| 38 | } |
| 39 | |
| 40 | /* Note: caller must hold the lock or in a atomic context */ |
| 41 | static void rq_remove_entity(struct amd_run_queue *rq, |
| 42 | struct amd_sched_entity *entity) |
| 43 | { |
| 44 | if (rq->current_entity == entity) |
| 45 | rq->current_entity = list_entry(entity->list.prev, |
| 46 | typeof(*entity), list); |
| 47 | list_del_init(&entity->list); |
| 48 | atomic_dec(&rq->nr_entity); |
| 49 | } |
| 50 | |
| 51 | static void rq_add_entity(struct amd_run_queue *rq, |
| 52 | struct amd_sched_entity *entity) |
| 53 | { |
| 54 | list_add_tail(&entity->list, &rq->head.list); |
| 55 | atomic_inc(&rq->nr_entity); |
| 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 | */ |
| 63 | static struct amd_sched_entity *rq_select_entity(struct amd_run_queue *rq) |
| 64 | { |
| 65 | struct amd_sched_entity *p = rq->current_entity; |
| 66 | int i = atomic_read(&rq->nr_entity) + 1; /*real count + dummy head*/ |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame^] | 67 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 68 | while (i) { |
| 69 | p = list_entry(p->list.next, typeof(*p), list); |
| 70 | if (!rq->check_entity_status(p)) { |
| 71 | rq->current_entity = p; |
| 72 | break; |
| 73 | } |
| 74 | i--; |
| 75 | } |
| 76 | return i ? p : NULL; |
| 77 | } |
| 78 | |
| 79 | static bool context_entity_is_waiting(struct amd_context_entity *entity) |
| 80 | { |
| 81 | /* TODO: sync obj for multi-ring synchronization */ |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | static int gpu_entity_check_status(struct amd_sched_entity *entity) |
| 86 | { |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame^] | 87 | struct amd_context_entity *tmp; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 88 | |
| 89 | if (entity == &entity->belongto_rq->head) |
| 90 | return -1; |
| 91 | |
| 92 | tmp = container_of(entity, typeof(*tmp), generic_entity); |
| 93 | if (kfifo_is_empty(&tmp->job_queue) || |
| 94 | context_entity_is_waiting(tmp)) |
| 95 | return -1; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Note: This function should only been called inside scheduler main |
| 102 | * function for thread safety, there is no other protection here. |
| 103 | * return ture if scheduler has something ready to run. |
| 104 | * |
| 105 | * For active_hw_rq, there is only one producer(scheduler thread) and |
| 106 | * one consumer(ISR). It should be safe to use this function in scheduler |
| 107 | * main thread to decide whether to continue emit more IBs. |
| 108 | */ |
| 109 | static bool is_scheduler_ready(struct amd_gpu_scheduler *sched) |
| 110 | { |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 111 | unsigned long flags; |
| 112 | bool full; |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame^] | 113 | |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 114 | spin_lock_irqsave(&sched->queue_lock, flags); |
| 115 | full = atomic64_read(&sched->hw_rq_count) < |
| 116 | sched->hw_submission_limit ? true : false; |
| 117 | spin_unlock_irqrestore(&sched->queue_lock, flags); |
| 118 | |
| 119 | return full; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Select next entity from the kernel run queue, if not available, |
| 124 | * return null. |
| 125 | */ |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame^] | 126 | static struct amd_context_entity * |
| 127 | kernel_rq_select_context(struct amd_gpu_scheduler *sched) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 128 | { |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame^] | 129 | struct amd_sched_entity *sched_entity; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 130 | struct amd_context_entity *tmp = NULL; |
| 131 | struct amd_run_queue *rq = &sched->kernel_rq; |
| 132 | |
| 133 | mutex_lock(&rq->lock); |
| 134 | sched_entity = rq_select_entity(rq); |
| 135 | if (sched_entity) |
| 136 | tmp = container_of(sched_entity, |
| 137 | typeof(*tmp), |
| 138 | generic_entity); |
| 139 | mutex_unlock(&rq->lock); |
| 140 | return tmp; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Select next entity containing real IB submissions |
| 145 | */ |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame^] | 146 | static struct amd_context_entity * |
| 147 | select_context(struct amd_gpu_scheduler *sched) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 148 | { |
| 149 | struct amd_context_entity *wake_entity = NULL; |
| 150 | struct amd_context_entity *tmp; |
| 151 | struct amd_run_queue *rq; |
| 152 | |
| 153 | if (!is_scheduler_ready(sched)) |
| 154 | return NULL; |
| 155 | |
| 156 | /* Kernel run queue has higher priority than normal run queue*/ |
| 157 | tmp = kernel_rq_select_context(sched); |
| 158 | if (tmp != NULL) |
| 159 | goto exit; |
| 160 | |
| 161 | WARN_ON(offsetof(struct amd_context_entity, generic_entity) != 0); |
| 162 | |
| 163 | rq = &sched->sched_rq; |
| 164 | mutex_lock(&rq->lock); |
| 165 | tmp = container_of(rq_select_entity(rq), |
| 166 | typeof(*tmp), generic_entity); |
| 167 | mutex_unlock(&rq->lock); |
| 168 | exit: |
| 169 | if (sched->current_entity && (sched->current_entity != tmp)) |
| 170 | wake_entity = sched->current_entity; |
| 171 | sched->current_entity = tmp; |
| 172 | if (wake_entity) |
| 173 | wake_up(&wake_entity->wait_queue); |
| 174 | return tmp; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Init a context entity used by scheduler when submit to HW ring. |
| 179 | * |
| 180 | * @sched The pointer to the scheduler |
| 181 | * @entity The pointer to a valid amd_context_entity |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 182 | * @rq The run queue this entity belongs |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame] | 183 | * @kernel If this is an entity for the kernel |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 184 | * @jobs The max number of jobs in the job queue |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 185 | * |
| 186 | * return 0 if succeed. negative error code on failure |
| 187 | */ |
| 188 | int amd_context_entity_init(struct amd_gpu_scheduler *sched, |
| 189 | struct amd_context_entity *entity, |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 190 | struct amd_run_queue *rq, |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 191 | uint32_t jobs) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 192 | { |
| 193 | uint64_t seq_ring = 0; |
| 194 | |
| 195 | if (!(sched && entity && rq)) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | memset(entity, 0, sizeof(struct amd_context_entity)); |
| 199 | seq_ring = ((uint64_t)sched->ring_id) << 60; |
| 200 | spin_lock_init(&entity->lock); |
| 201 | entity->generic_entity.belongto_rq = rq; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 202 | entity->scheduler = sched; |
| 203 | init_waitqueue_head(&entity->wait_queue); |
| 204 | init_waitqueue_head(&entity->wait_emit); |
| 205 | if(kfifo_alloc(&entity->job_queue, |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 206 | jobs * sizeof(void *), |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 207 | GFP_KERNEL)) |
| 208 | return -EINVAL; |
| 209 | |
| 210 | spin_lock_init(&entity->queue_lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 211 | atomic64_set(&entity->last_emitted_v_seq, seq_ring); |
| 212 | atomic64_set(&entity->last_queued_v_seq, seq_ring); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 213 | |
| 214 | /* Add the entity to the run queue */ |
| 215 | mutex_lock(&rq->lock); |
| 216 | rq_add_entity(rq, &entity->generic_entity); |
| 217 | mutex_unlock(&rq->lock); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Query if entity is initialized |
| 223 | * |
| 224 | * @sched Pointer to scheduler instance |
| 225 | * @entity The pointer to a valid scheduler entity |
| 226 | * |
| 227 | * return true if entity is initialized, false otherwise |
| 228 | */ |
| 229 | static bool is_context_entity_initialized(struct amd_gpu_scheduler *sched, |
| 230 | struct amd_context_entity *entity) |
| 231 | { |
| 232 | return entity->scheduler == sched && |
| 233 | entity->generic_entity.belongto_rq != NULL; |
| 234 | } |
| 235 | |
| 236 | static bool is_context_entity_idle(struct amd_gpu_scheduler *sched, |
| 237 | struct amd_context_entity *entity) |
| 238 | { |
| 239 | /** |
| 240 | * Idle means no pending IBs, and the entity is not |
| 241 | * currently being used. |
| 242 | */ |
| 243 | barrier(); |
| 244 | if ((sched->current_entity != entity) && |
| 245 | kfifo_is_empty(&entity->job_queue)) |
| 246 | return true; |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Destroy a context entity |
| 253 | * |
| 254 | * @sched Pointer to scheduler instance |
| 255 | * @entity The pointer to a valid scheduler entity |
| 256 | * |
| 257 | * return 0 if succeed. negative error code on failure |
| 258 | */ |
| 259 | int amd_context_entity_fini(struct amd_gpu_scheduler *sched, |
| 260 | struct amd_context_entity *entity) |
| 261 | { |
| 262 | int r = 0; |
| 263 | struct amd_run_queue *rq = entity->generic_entity.belongto_rq; |
| 264 | |
| 265 | if (!is_context_entity_initialized(sched, entity)) |
| 266 | return 0; |
| 267 | |
| 268 | /** |
| 269 | * The client will not queue more IBs during this fini, consume existing |
| 270 | * queued IBs |
| 271 | */ |
| 272 | r = wait_event_timeout( |
| 273 | entity->wait_queue, |
| 274 | is_context_entity_idle(sched, entity), |
| 275 | msecs_to_jiffies(AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS) |
| 276 | ) ? 0 : -1; |
| 277 | |
| 278 | if (r) { |
| 279 | if (entity->is_pending) |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame] | 280 | DRM_INFO("Entity %p is in waiting state during fini,\ |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 281 | all pending ibs will be canceled.\n", |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame] | 282 | entity); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | mutex_lock(&rq->lock); |
| 286 | rq_remove_entity(rq, &entity->generic_entity); |
| 287 | mutex_unlock(&rq->lock); |
| 288 | kfifo_free(&entity->job_queue); |
| 289 | return r; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Submit a normal job to the job queue |
| 294 | * |
| 295 | * @sched The pointer to the scheduler |
| 296 | * @c_entity The pointer to amd_context_entity |
| 297 | * @job The pointer to job required to submit |
Chunming Zhou | 80de591 | 2015-08-05 19:07:08 +0800 | [diff] [blame] | 298 | * return 0 if succeed. -1 if failed. |
| 299 | * -2 indicate queue is full for this client, client should wait untill |
| 300 | * scheduler consum some queued command. |
| 301 | * -1 other fail. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 302 | */ |
Chunming Zhou | 80de591 | 2015-08-05 19:07:08 +0800 | [diff] [blame] | 303 | int amd_sched_push_job(struct amd_gpu_scheduler *sched, |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 304 | struct amd_context_entity *c_entity, |
| 305 | void *job) |
| 306 | { |
| 307 | while (kfifo_in_spinlocked(&c_entity->job_queue, &job, sizeof(void *), |
| 308 | &c_entity->queue_lock) != sizeof(void *)) { |
| 309 | /** |
| 310 | * Current context used up all its IB slots |
| 311 | * wait here, or need to check whether GPU is hung |
| 312 | */ |
| 313 | schedule(); |
| 314 | } |
| 315 | |
| 316 | wake_up_interruptible(&sched->wait_queue); |
Chunming Zhou | 80de591 | 2015-08-05 19:07:08 +0800 | [diff] [blame] | 317 | return 0; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
Christian König | 1d7dd22 | 2015-07-31 14:31:49 +0200 | [diff] [blame] | 321 | * Wait for a virtual sequence number to be emitted. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 322 | * |
| 323 | * @c_entity The pointer to a valid context entity |
| 324 | * @seq The virtual sequence number to wait |
| 325 | * @intr Interruptible or not |
| 326 | * @timeout Timeout in ms, wait infinitely if <0 |
| 327 | * @emit wait for emit or signal |
| 328 | * |
| 329 | * return =0 signaled , <0 failed |
| 330 | */ |
Christian König | 1d7dd22 | 2015-07-31 14:31:49 +0200 | [diff] [blame] | 331 | int amd_sched_wait_emit(struct amd_context_entity *c_entity, |
| 332 | uint64_t seq, |
| 333 | bool intr, |
| 334 | long timeout) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 335 | { |
Christian König | 1d7dd22 | 2015-07-31 14:31:49 +0200 | [diff] [blame] | 336 | atomic64_t *v_seq = &c_entity->last_emitted_v_seq; |
| 337 | wait_queue_head_t *wait_queue = &c_entity->wait_emit; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 338 | |
| 339 | if (intr && (timeout < 0)) { |
| 340 | wait_event_interruptible( |
| 341 | *wait_queue, |
| 342 | seq <= atomic64_read(v_seq)); |
| 343 | return 0; |
| 344 | } else if (intr && (timeout >= 0)) { |
| 345 | wait_event_interruptible_timeout( |
| 346 | *wait_queue, |
| 347 | seq <= atomic64_read(v_seq), |
| 348 | msecs_to_jiffies(timeout)); |
| 349 | return (seq <= atomic64_read(v_seq)) ? |
| 350 | 0 : -1; |
| 351 | } else if (!intr && (timeout < 0)) { |
| 352 | wait_event( |
| 353 | *wait_queue, |
| 354 | seq <= atomic64_read(v_seq)); |
| 355 | return 0; |
| 356 | } else if (!intr && (timeout >= 0)) { |
| 357 | wait_event_timeout( |
| 358 | *wait_queue, |
| 359 | seq <= atomic64_read(v_seq), |
| 360 | msecs_to_jiffies(timeout)); |
| 361 | return (seq <= atomic64_read(v_seq)) ? |
| 362 | 0 : -1; |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 367 | static int amd_sched_main(void *param) |
| 368 | { |
| 369 | int r; |
| 370 | void *job; |
| 371 | struct sched_param sparam = {.sched_priority = 1}; |
| 372 | struct amd_context_entity *c_entity = NULL; |
| 373 | struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param; |
| 374 | |
| 375 | sched_setscheduler(current, SCHED_FIFO, &sparam); |
| 376 | |
| 377 | while (!kthread_should_stop()) { |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 378 | struct amd_sched_job *sched_job = NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 379 | wait_event_interruptible(sched->wait_queue, |
| 380 | is_scheduler_ready(sched) && |
| 381 | (c_entity = select_context(sched))); |
| 382 | r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *)); |
| 383 | if (r != sizeof(void *)) |
| 384 | continue; |
| 385 | r = sched->ops->prepare_job(sched, c_entity, job); |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 386 | if (!r) { |
| 387 | unsigned long flags; |
| 388 | sched_job = kzalloc(sizeof(struct amd_sched_job), |
| 389 | GFP_KERNEL); |
| 390 | if (!sched_job) { |
| 391 | WARN(true, "No memory to allocate\n"); |
| 392 | continue; |
| 393 | } |
| 394 | sched_job->job = job; |
| 395 | sched_job->sched = sched; |
| 396 | spin_lock_irqsave(&sched->queue_lock, flags); |
| 397 | list_add_tail(&sched_job->list, &sched->active_hw_rq); |
| 398 | atomic64_inc(&sched->hw_rq_count); |
| 399 | spin_unlock_irqrestore(&sched->queue_lock, flags); |
| 400 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 401 | mutex_lock(&sched->sched_lock); |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 402 | sched->ops->run_job(sched, c_entity, sched_job); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 403 | mutex_unlock(&sched->sched_lock); |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 408 | /** |
| 409 | * ISR to handle EOP inetrrupts |
| 410 | * |
| 411 | * @sched: gpu scheduler |
| 412 | * |
| 413 | */ |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 414 | void amd_sched_process_job(struct amd_sched_job *sched_job) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 415 | { |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 416 | unsigned long flags; |
| 417 | struct amd_gpu_scheduler *sched; |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame^] | 418 | |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 419 | if (!sched_job) |
| 420 | return; |
| 421 | sched = sched_job->sched; |
| 422 | spin_lock_irqsave(&sched->queue_lock, flags); |
| 423 | list_del(&sched_job->list); |
| 424 | atomic64_dec(&sched->hw_rq_count); |
| 425 | spin_unlock_irqrestore(&sched->queue_lock, flags); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 426 | |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 427 | sched->ops->process_job(sched, sched_job->job); |
| 428 | kfree(sched_job); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 429 | wake_up_interruptible(&sched->wait_queue); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Create a gpu scheduler |
| 434 | * |
| 435 | * @device The device context for this scheduler |
| 436 | * @ops The backend operations for this scheduler. |
| 437 | * @id The scheduler is per ring, here is ring id. |
| 438 | * @granularity The minumum ms unit the scheduler will scheduled. |
| 439 | * @preemption Indicate whether this ring support preemption, 0 is no. |
| 440 | * |
| 441 | * return the pointer to scheduler for success, otherwise return NULL |
| 442 | */ |
| 443 | struct amd_gpu_scheduler *amd_sched_create(void *device, |
| 444 | struct amd_sched_backend_ops *ops, |
| 445 | unsigned ring, |
| 446 | unsigned granularity, |
Jammy Zhou | 4afcb30 | 2015-07-30 16:44:05 +0800 | [diff] [blame] | 447 | unsigned preemption, |
| 448 | unsigned hw_submission) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 449 | { |
| 450 | struct amd_gpu_scheduler *sched; |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame^] | 451 | char name[20]; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 452 | |
| 453 | sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL); |
| 454 | if (!sched) |
| 455 | return NULL; |
| 456 | |
| 457 | sched->device = device; |
| 458 | sched->ops = ops; |
| 459 | sched->granularity = granularity; |
| 460 | sched->ring_id = ring; |
| 461 | sched->preemption = preemption; |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 462 | sched->hw_submission_limit = hw_submission; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 463 | snprintf(name, sizeof(name), "gpu_sched[%d]", ring); |
| 464 | mutex_init(&sched->sched_lock); |
| 465 | spin_lock_init(&sched->queue_lock); |
| 466 | init_rq(&sched->sched_rq); |
| 467 | sched->sched_rq.check_entity_status = gpu_entity_check_status; |
| 468 | |
| 469 | init_rq(&sched->kernel_rq); |
| 470 | sched->kernel_rq.check_entity_status = gpu_entity_check_status; |
| 471 | |
| 472 | init_waitqueue_head(&sched->wait_queue); |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 473 | INIT_LIST_HEAD(&sched->active_hw_rq); |
| 474 | atomic64_set(&sched->hw_rq_count, 0); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 475 | /* Each scheduler will run on a seperate kernel thread */ |
| 476 | sched->thread = kthread_create(amd_sched_main, sched, name); |
| 477 | if (sched->thread) { |
| 478 | wake_up_process(sched->thread); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 479 | return sched; |
| 480 | } |
| 481 | |
| 482 | DRM_ERROR("Failed to create scheduler for id %d.\n", ring); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 483 | kfree(sched); |
| 484 | return NULL; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Destroy a gpu scheduler |
| 489 | * |
| 490 | * @sched The pointer to the scheduler |
| 491 | * |
| 492 | * return 0 if succeed. -1 if failed. |
| 493 | */ |
| 494 | int amd_sched_destroy(struct amd_gpu_scheduler *sched) |
| 495 | { |
| 496 | kthread_stop(sched->thread); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 497 | kfree(sched); |
| 498 | return 0; |
| 499 | } |
| 500 | |
Jammy Zhou | f95b7e3 | 2015-07-31 17:18:15 +0800 | [diff] [blame] | 501 | /** |
| 502 | * Update emitted sequence and wake up the waiters, called by run_job |
| 503 | * in driver side |
| 504 | * |
| 505 | * @entity The context entity |
| 506 | * @seq The sequence number for the latest emitted job |
| 507 | */ |
| 508 | void amd_sched_emit(struct amd_context_entity *c_entity, uint64_t seq) |
| 509 | { |
| 510 | atomic64_set(&c_entity->last_emitted_v_seq, seq); |
| 511 | wake_up_all(&c_entity->wait_emit); |
| 512 | } |
Jammy Zhou | 27f6642 | 2015-08-03 10:27:57 +0800 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * Get next queued sequence number |
| 516 | * |
| 517 | * @entity The context entity |
| 518 | * |
| 519 | * return the next queued sequence number |
| 520 | */ |
| 521 | uint64_t amd_sched_next_queued_seq(struct amd_context_entity *c_entity) |
| 522 | { |
| 523 | return atomic64_read(&c_entity->last_queued_v_seq) + 1; |
| 524 | } |