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 | /** |
| 97 | * Note: This function should only been called inside scheduler main |
| 98 | * function for thread safety, there is no other protection here. |
| 99 | * return ture if scheduler has something ready to run. |
| 100 | * |
| 101 | * For active_hw_rq, there is only one producer(scheduler thread) and |
| 102 | * one consumer(ISR). It should be safe to use this function in scheduler |
| 103 | * main thread to decide whether to continue emit more IBs. |
| 104 | */ |
| 105 | static bool is_scheduler_ready(struct amd_gpu_scheduler *sched) |
| 106 | { |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 107 | unsigned long flags; |
| 108 | bool full; |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame] | 109 | |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 110 | spin_lock_irqsave(&sched->queue_lock, flags); |
| 111 | full = atomic64_read(&sched->hw_rq_count) < |
| 112 | sched->hw_submission_limit ? true : false; |
| 113 | spin_unlock_irqrestore(&sched->queue_lock, flags); |
| 114 | |
| 115 | return full; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 119 | * Select next entity containing real IB submissions |
| 120 | */ |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 121 | static struct amd_sched_entity * |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame] | 122 | select_context(struct amd_gpu_scheduler *sched) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 123 | { |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 124 | struct amd_sched_entity *wake_entity = NULL; |
| 125 | struct amd_sched_entity *tmp; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 126 | |
| 127 | if (!is_scheduler_ready(sched)) |
| 128 | return NULL; |
| 129 | |
| 130 | /* Kernel run queue has higher priority than normal run queue*/ |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame^] | 131 | tmp = amd_sched_rq_select_entity(&sched->kernel_rq); |
| 132 | if (tmp == NULL) |
| 133 | tmp = amd_sched_rq_select_entity(&sched->sched_rq); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 134 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 135 | if (sched->current_entity && (sched->current_entity != tmp)) |
| 136 | wake_entity = sched->current_entity; |
| 137 | sched->current_entity = tmp; |
Chunming Zhou | 1c8f805 | 2015-08-13 13:04:06 +0800 | [diff] [blame] | 138 | if (wake_entity && wake_entity->need_wakeup) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 139 | wake_up(&wake_entity->wait_queue); |
| 140 | return tmp; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Init a context entity used by scheduler when submit to HW ring. |
| 145 | * |
| 146 | * @sched The pointer to the scheduler |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 147 | * @entity The pointer to a valid amd_sched_entity |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 148 | * @rq The run queue this entity belongs |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame] | 149 | * @kernel If this is an entity for the kernel |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 150 | * @jobs The max number of jobs in the job queue |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 151 | * |
| 152 | * return 0 if succeed. negative error code on failure |
| 153 | */ |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 154 | int amd_sched_entity_init(struct amd_gpu_scheduler *sched, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 155 | struct amd_sched_entity *entity, |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 156 | struct amd_sched_rq *rq, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 157 | uint32_t jobs) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 158 | { |
| 159 | uint64_t seq_ring = 0; |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 160 | char name[20]; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 161 | |
| 162 | if (!(sched && entity && rq)) |
| 163 | return -EINVAL; |
| 164 | |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 165 | memset(entity, 0, sizeof(struct amd_sched_entity)); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 166 | seq_ring = ((uint64_t)sched->ring_id) << 60; |
| 167 | spin_lock_init(&entity->lock); |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 168 | entity->belongto_rq = rq; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 169 | entity->scheduler = sched; |
| 170 | init_waitqueue_head(&entity->wait_queue); |
| 171 | init_waitqueue_head(&entity->wait_emit); |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 172 | entity->fence_context = fence_context_alloc(1); |
| 173 | snprintf(name, sizeof(name), "c_entity[%llu]", entity->fence_context); |
| 174 | memcpy(entity->name, name, 20); |
Chunming Zhou | 1c8f805 | 2015-08-13 13:04:06 +0800 | [diff] [blame] | 175 | entity->need_wakeup = false; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 176 | if(kfifo_alloc(&entity->job_queue, |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 177 | jobs * sizeof(void *), |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 178 | GFP_KERNEL)) |
| 179 | return -EINVAL; |
| 180 | |
| 181 | spin_lock_init(&entity->queue_lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 182 | atomic64_set(&entity->last_queued_v_seq, seq_ring); |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 183 | atomic64_set(&entity->last_signaled_v_seq, seq_ring); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 184 | |
| 185 | /* Add the entity to the run queue */ |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 186 | amd_sched_rq_add_entity(rq, entity); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Query if entity is initialized |
| 192 | * |
| 193 | * @sched Pointer to scheduler instance |
| 194 | * @entity The pointer to a valid scheduler entity |
| 195 | * |
| 196 | * return true if entity is initialized, false otherwise |
| 197 | */ |
| 198 | static bool is_context_entity_initialized(struct amd_gpu_scheduler *sched, |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 199 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 200 | { |
| 201 | return entity->scheduler == sched && |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 202 | entity->belongto_rq != NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static bool is_context_entity_idle(struct amd_gpu_scheduler *sched, |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 206 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 207 | { |
| 208 | /** |
| 209 | * Idle means no pending IBs, and the entity is not |
| 210 | * currently being used. |
| 211 | */ |
| 212 | barrier(); |
| 213 | if ((sched->current_entity != entity) && |
| 214 | kfifo_is_empty(&entity->job_queue)) |
| 215 | return true; |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Destroy a context entity |
| 222 | * |
| 223 | * @sched Pointer to scheduler instance |
| 224 | * @entity The pointer to a valid scheduler entity |
| 225 | * |
| 226 | * return 0 if succeed. negative error code on failure |
| 227 | */ |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 228 | int amd_sched_entity_fini(struct amd_gpu_scheduler *sched, |
| 229 | struct amd_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 230 | { |
| 231 | int r = 0; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 232 | struct amd_sched_rq *rq = entity->belongto_rq; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 233 | |
| 234 | if (!is_context_entity_initialized(sched, entity)) |
| 235 | return 0; |
Chunming Zhou | 1c8f805 | 2015-08-13 13:04:06 +0800 | [diff] [blame] | 236 | entity->need_wakeup = true; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 237 | /** |
| 238 | * The client will not queue more IBs during this fini, consume existing |
| 239 | * queued IBs |
| 240 | */ |
| 241 | r = wait_event_timeout( |
| 242 | entity->wait_queue, |
| 243 | is_context_entity_idle(sched, entity), |
| 244 | msecs_to_jiffies(AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS) |
| 245 | ) ? 0 : -1; |
| 246 | |
| 247 | if (r) { |
| 248 | if (entity->is_pending) |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame] | 249 | DRM_INFO("Entity %p is in waiting state during fini,\ |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 250 | all pending ibs will be canceled.\n", |
Christian König | 0e89d0c | 2015-08-04 16:58:36 +0200 | [diff] [blame] | 251 | entity); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 254 | amd_sched_rq_remove_entity(rq, entity); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 255 | kfifo_free(&entity->job_queue); |
| 256 | return r; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Submit a normal job to the job queue |
| 261 | * |
| 262 | * @sched The pointer to the scheduler |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 263 | * @c_entity The pointer to amd_sched_entity |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 264 | * @job The pointer to job required to submit |
Chunming Zhou | 80de591 | 2015-08-05 19:07:08 +0800 | [diff] [blame] | 265 | * return 0 if succeed. -1 if failed. |
| 266 | * -2 indicate queue is full for this client, client should wait untill |
| 267 | * scheduler consum some queued command. |
| 268 | * -1 other fail. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 269 | */ |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 270 | int amd_sched_push_job(struct amd_sched_job *sched_job) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 271 | { |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 272 | struct amd_sched_fence *fence = |
| 273 | amd_sched_fence_create(sched_job->s_entity); |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 274 | if (!fence) |
| 275 | return -EINVAL; |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 276 | fence_get(&fence->base); |
| 277 | sched_job->s_fence = fence; |
| 278 | while (kfifo_in_spinlocked(&sched_job->s_entity->job_queue, |
| 279 | &sched_job, sizeof(void *), |
| 280 | &sched_job->s_entity->queue_lock) != |
| 281 | sizeof(void *)) { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 282 | /** |
| 283 | * Current context used up all its IB slots |
| 284 | * wait here, or need to check whether GPU is hung |
| 285 | */ |
| 286 | schedule(); |
| 287 | } |
Chunming Zhou | 1c8f805 | 2015-08-13 13:04:06 +0800 | [diff] [blame] | 288 | /* first job wake up scheduler */ |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 289 | if ((kfifo_len(&sched_job->s_entity->job_queue) / sizeof(void *)) == 1) |
| 290 | wake_up_interruptible(&sched_job->sched->wait_queue); |
Chunming Zhou | 80de591 | 2015-08-05 19:07:08 +0800 | [diff] [blame] | 291 | return 0; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 292 | } |
| 293 | |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 294 | static void amd_sched_process_job(struct fence *f, struct fence_cb *cb) |
| 295 | { |
| 296 | struct amd_sched_job *sched_job = |
| 297 | container_of(cb, struct amd_sched_job, cb); |
| 298 | struct amd_gpu_scheduler *sched; |
| 299 | unsigned long flags; |
| 300 | |
| 301 | sched = sched_job->sched; |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 302 | atomic64_set(&sched_job->s_entity->last_signaled_v_seq, |
| 303 | sched_job->s_fence->v_seq); |
| 304 | amd_sched_fence_signal(sched_job->s_fence); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 305 | spin_lock_irqsave(&sched->queue_lock, flags); |
| 306 | list_del(&sched_job->list); |
| 307 | atomic64_dec(&sched->hw_rq_count); |
| 308 | spin_unlock_irqrestore(&sched->queue_lock, flags); |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 309 | fence_put(&sched_job->s_fence->base); |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 310 | sched->ops->process_job(sched, sched_job); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 311 | wake_up_interruptible(&sched->wait_queue); |
| 312 | } |
| 313 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 314 | static int amd_sched_main(void *param) |
| 315 | { |
| 316 | int r; |
Chunming Zhou | 953e8fd | 2015-08-06 15:19:12 +0800 | [diff] [blame] | 317 | struct amd_sched_job *job; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 318 | struct sched_param sparam = {.sched_priority = 1}; |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 319 | struct amd_sched_entity *c_entity = NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 320 | struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param; |
| 321 | |
| 322 | sched_setscheduler(current, SCHED_FIFO, &sparam); |
| 323 | |
| 324 | while (!kthread_should_stop()) { |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 325 | struct fence *fence; |
| 326 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 327 | wait_event_interruptible(sched->wait_queue, |
| 328 | is_scheduler_ready(sched) && |
| 329 | (c_entity = select_context(sched))); |
| 330 | r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *)); |
| 331 | if (r != sizeof(void *)) |
| 332 | continue; |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 333 | r = 0; |
| 334 | if (sched->ops->prepare_job) |
| 335 | r = sched->ops->prepare_job(sched, c_entity, job); |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 336 | if (!r) { |
| 337 | unsigned long flags; |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 338 | spin_lock_irqsave(&sched->queue_lock, flags); |
Chunming Zhou | 953e8fd | 2015-08-06 15:19:12 +0800 | [diff] [blame] | 339 | list_add_tail(&job->list, &sched->active_hw_rq); |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 340 | atomic64_inc(&sched->hw_rq_count); |
| 341 | spin_unlock_irqrestore(&sched->queue_lock, flags); |
| 342 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 343 | mutex_lock(&sched->sched_lock); |
Chunming Zhou | 953e8fd | 2015-08-06 15:19:12 +0800 | [diff] [blame] | 344 | fence = sched->ops->run_job(sched, c_entity, job); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 345 | if (fence) { |
Chunming Zhou | 953e8fd | 2015-08-06 15:19:12 +0800 | [diff] [blame] | 346 | r = fence_add_callback(fence, &job->cb, |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 347 | amd_sched_process_job); |
| 348 | if (r == -ENOENT) |
Chunming Zhou | 953e8fd | 2015-08-06 15:19:12 +0800 | [diff] [blame] | 349 | amd_sched_process_job(fence, &job->cb); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 350 | else if (r) |
| 351 | DRM_ERROR("fence add callback failed (%d)\n", r); |
| 352 | fence_put(fence); |
| 353 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 354 | mutex_unlock(&sched->sched_lock); |
| 355 | } |
| 356 | return 0; |
| 357 | } |
| 358 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 359 | /** |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 360 | * Create a gpu scheduler |
| 361 | * |
| 362 | * @device The device context for this scheduler |
| 363 | * @ops The backend operations for this scheduler. |
| 364 | * @id The scheduler is per ring, here is ring id. |
| 365 | * @granularity The minumum ms unit the scheduler will scheduled. |
| 366 | * @preemption Indicate whether this ring support preemption, 0 is no. |
| 367 | * |
| 368 | * return the pointer to scheduler for success, otherwise return NULL |
| 369 | */ |
| 370 | struct amd_gpu_scheduler *amd_sched_create(void *device, |
| 371 | struct amd_sched_backend_ops *ops, |
| 372 | unsigned ring, |
| 373 | unsigned granularity, |
Jammy Zhou | 4afcb30 | 2015-07-30 16:44:05 +0800 | [diff] [blame] | 374 | unsigned preemption, |
| 375 | unsigned hw_submission) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 376 | { |
| 377 | struct amd_gpu_scheduler *sched; |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame] | 378 | char name[20]; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 379 | |
| 380 | sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL); |
| 381 | if (!sched) |
| 382 | return NULL; |
| 383 | |
| 384 | sched->device = device; |
| 385 | sched->ops = ops; |
| 386 | sched->granularity = granularity; |
| 387 | sched->ring_id = ring; |
| 388 | sched->preemption = preemption; |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 389 | sched->hw_submission_limit = hw_submission; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 390 | snprintf(name, sizeof(name), "gpu_sched[%d]", ring); |
| 391 | mutex_init(&sched->sched_lock); |
| 392 | spin_lock_init(&sched->queue_lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 393 | amd_sched_rq_init(&sched->sched_rq); |
| 394 | amd_sched_rq_init(&sched->kernel_rq); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 395 | |
| 396 | init_waitqueue_head(&sched->wait_queue); |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 397 | INIT_LIST_HEAD(&sched->active_hw_rq); |
| 398 | atomic64_set(&sched->hw_rq_count, 0); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 399 | /* Each scheduler will run on a seperate kernel thread */ |
| 400 | sched->thread = kthread_create(amd_sched_main, sched, name); |
| 401 | if (sched->thread) { |
| 402 | wake_up_process(sched->thread); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 403 | return sched; |
| 404 | } |
| 405 | |
| 406 | DRM_ERROR("Failed to create scheduler for id %d.\n", ring); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 407 | kfree(sched); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Destroy a gpu scheduler |
| 413 | * |
| 414 | * @sched The pointer to the scheduler |
| 415 | * |
| 416 | * return 0 if succeed. -1 if failed. |
| 417 | */ |
| 418 | int amd_sched_destroy(struct amd_gpu_scheduler *sched) |
| 419 | { |
| 420 | kthread_stop(sched->thread); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 421 | kfree(sched); |
| 422 | return 0; |
| 423 | } |
| 424 | |
Jammy Zhou | f95b7e3 | 2015-07-31 17:18:15 +0800 | [diff] [blame] | 425 | /** |
Jammy Zhou | 27f6642 | 2015-08-03 10:27:57 +0800 | [diff] [blame] | 426 | * Get next queued sequence number |
| 427 | * |
| 428 | * @entity The context entity |
| 429 | * |
| 430 | * return the next queued sequence number |
| 431 | */ |
Christian König | 91404fb | 2015-08-05 18:33:21 +0200 | [diff] [blame] | 432 | 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] | 433 | { |
| 434 | return atomic64_read(&c_entity->last_queued_v_seq) + 1; |
| 435 | } |