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