blob: 0133697c127eb773d8c9ccbfb086390bfba59191 [file] [log] [blame]
Jammy Zhoua72ce6f2015-05-22 18:55:07 +08001/*
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önig432a4ff2015-08-12 11:46:04 +020031static void amd_sched_rq_init(struct amd_sched_rq *rq)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080032{
Christian König2b184d82015-08-18 14:41:25 +020033 spin_lock_init(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020034 INIT_LIST_HEAD(&rq->entities);
Christian König432a4ff2015-08-12 11:46:04 +020035 rq->current_entity = NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080036}
37
Christian König432a4ff2015-08-12 11:46:04 +020038static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
39 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080040{
Christian König2b184d82015-08-18 14:41:25 +020041 spin_lock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020042 list_add_tail(&entity->list, &rq->entities);
Christian König2b184d82015-08-18 14:41:25 +020043 spin_unlock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020044}
45
46static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
47 struct amd_sched_entity *entity)
48{
Christian König2b184d82015-08-18 14:41:25 +020049 spin_lock(&rq->lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080050 list_del_init(&entity->list);
Christian König432a4ff2015-08-12 11:46:04 +020051 if (rq->current_entity == entity)
52 rq->current_entity = NULL;
Christian König2b184d82015-08-18 14:41:25 +020053 spin_unlock(&rq->lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080054}
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önig432a4ff2015-08-12 11:46:04 +020061static struct amd_sched_entity *
62amd_sched_rq_select_entity(struct amd_sched_rq *rq)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080063{
Christian König2b184d82015-08-18 14:41:25 +020064 struct amd_sched_entity *entity;
Christian König4cd7f42c2015-08-05 18:18:52 +020065
Christian König2b184d82015-08-18 14:41:25 +020066 spin_lock(&rq->lock);
67
68 entity = rq->current_entity;
Christian König432a4ff2015-08-12 11:46:04 +020069 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önig2b184d82015-08-18 14:41:25 +020073 spin_unlock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020074 return rq->current_entity;
75 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080076 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080077 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080078
Christian König432a4ff2015-08-12 11:46:04 +020079 list_for_each_entry(entity, &rq->entities, list) {
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080080
Christian König432a4ff2015-08-12 11:46:04 +020081 if (!kfifo_is_empty(&entity->job_queue)) {
82 rq->current_entity = entity;
Christian König2b184d82015-08-18 14:41:25 +020083 spin_unlock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020084 return rq->current_entity;
85 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080086
Christian König432a4ff2015-08-12 11:46:04 +020087 if (entity == rq->current_entity)
88 break;
89 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080090
Christian König2b184d82015-08-18 14:41:25 +020091 spin_unlock(&rq->lock);
92
Christian König432a4ff2015-08-12 11:46:04 +020093 return NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080094}
95
96/**
Christian Königc746ba22015-08-19 16:12:15 +020097 * Return ture if we can push more jobs to the hw.
98 */
99static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800100{
Christian Königc746ba22015-08-19 16:12:15 +0200101 return atomic_read(&sched->hw_rq_count) <
102 sched->hw_submission_limit;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800103}
104
105/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800106 * Select next entity containing real IB submissions
107*/
Christian König91404fb2015-08-05 18:33:21 +0200108static struct amd_sched_entity *
Christian Königf85a6dd2015-08-19 17:37:52 +0200109amd_sched_select_context(struct amd_gpu_scheduler *sched)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800110{
Christian König91404fb2015-08-05 18:33:21 +0200111 struct amd_sched_entity *tmp;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800112
Christian Königc746ba22015-08-19 16:12:15 +0200113 if (!amd_sched_ready(sched))
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800114 return NULL;
115
116 /* Kernel run queue has higher priority than normal run queue*/
Christian König2b184d82015-08-18 14:41:25 +0200117 tmp = amd_sched_rq_select_entity(&sched->kernel_rq);
118 if (tmp == NULL)
119 tmp = amd_sched_rq_select_entity(&sched->sched_rq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800120
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800121 return tmp;
122}
123
124/**
125 * Init a context entity used by scheduler when submit to HW ring.
126 *
127 * @sched The pointer to the scheduler
Christian König91404fb2015-08-05 18:33:21 +0200128 * @entity The pointer to a valid amd_sched_entity
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800129 * @rq The run queue this entity belongs
Christian König0e89d0c2015-08-04 16:58:36 +0200130 * @kernel If this is an entity for the kernel
Jammy Zhou1333f722015-07-30 16:36:58 +0800131 * @jobs The max number of jobs in the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800132 *
133 * return 0 if succeed. negative error code on failure
134*/
Christian König91404fb2015-08-05 18:33:21 +0200135int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
Christian König6f0e54a2015-08-05 21:22:10 +0200136 struct amd_sched_entity *entity,
Christian König432a4ff2015-08-12 11:46:04 +0200137 struct amd_sched_rq *rq,
Christian König6f0e54a2015-08-05 21:22:10 +0200138 uint32_t jobs)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800139{
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800140 char name[20];
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800141
142 if (!(sched && entity && rq))
143 return -EINVAL;
144
Christian König91404fb2015-08-05 18:33:21 +0200145 memset(entity, 0, sizeof(struct amd_sched_entity));
Christian König91404fb2015-08-05 18:33:21 +0200146 entity->belongto_rq = rq;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800147 entity->scheduler = sched;
148 init_waitqueue_head(&entity->wait_queue);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800149 entity->fence_context = fence_context_alloc(1);
150 snprintf(name, sizeof(name), "c_entity[%llu]", entity->fence_context);
151 memcpy(entity->name, name, 20);
Chunming Zhou1c8f8052015-08-13 13:04:06 +0800152 entity->need_wakeup = false;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800153 if(kfifo_alloc(&entity->job_queue,
Jammy Zhou1333f722015-07-30 16:36:58 +0800154 jobs * sizeof(void *),
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800155 GFP_KERNEL))
156 return -EINVAL;
157
158 spin_lock_init(&entity->queue_lock);
Christian Königce882e62015-08-19 15:00:55 +0200159 atomic_set(&entity->fence_seq, 0);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800160
161 /* Add the entity to the run queue */
Christian König432a4ff2015-08-12 11:46:04 +0200162 amd_sched_rq_add_entity(rq, entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800163 return 0;
164}
165
166/**
167 * Query if entity is initialized
168 *
169 * @sched Pointer to scheduler instance
170 * @entity The pointer to a valid scheduler entity
171 *
172 * return true if entity is initialized, false otherwise
173*/
174static bool is_context_entity_initialized(struct amd_gpu_scheduler *sched,
Christian König91404fb2015-08-05 18:33:21 +0200175 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800176{
177 return entity->scheduler == sched &&
Christian König91404fb2015-08-05 18:33:21 +0200178 entity->belongto_rq != NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800179}
180
Christian Königaef48522015-08-20 14:47:46 +0200181/**
182 * Check if entity is idle
183 *
184 * @entity The pointer to a valid scheduler entity
185 *
186 * Return true if entity don't has any unscheduled jobs.
187 */
188static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800189{
Christian Königaef48522015-08-20 14:47:46 +0200190 rmb();
191 if (kfifo_is_empty(&entity->job_queue))
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800192 return true;
193
194 return false;
195}
196
197/**
198 * Destroy a context entity
199 *
200 * @sched Pointer to scheduler instance
201 * @entity The pointer to a valid scheduler entity
202 *
203 * return 0 if succeed. negative error code on failure
204 */
Christian König91404fb2015-08-05 18:33:21 +0200205int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
206 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800207{
Christian König432a4ff2015-08-12 11:46:04 +0200208 struct amd_sched_rq *rq = entity->belongto_rq;
Christian Königaef48522015-08-20 14:47:46 +0200209 long r;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800210
211 if (!is_context_entity_initialized(sched, entity))
212 return 0;
Chunming Zhou1c8f8052015-08-13 13:04:06 +0800213 entity->need_wakeup = true;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800214 /**
215 * The client will not queue more IBs during this fini, consume existing
216 * queued IBs
217 */
Christian Königaef48522015-08-20 14:47:46 +0200218 r = wait_event_timeout(entity->wait_queue,
219 amd_sched_entity_is_idle(entity),
220 msecs_to_jiffies(AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS));
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800221
Christian Königaef48522015-08-20 14:47:46 +0200222 if (r <= 0)
Christian König9788ec42015-08-19 17:34:20 +0200223 DRM_INFO("Entity %p is in waiting state during fini\n",
224 entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800225
Christian König432a4ff2015-08-12 11:46:04 +0200226 amd_sched_rq_remove_entity(rq, entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800227 kfifo_free(&entity->job_queue);
228 return r;
229}
230
231/**
232 * Submit a normal job to the job queue
233 *
234 * @sched The pointer to the scheduler
Christian König91404fb2015-08-05 18:33:21 +0200235 * @c_entity The pointer to amd_sched_entity
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800236 * @job The pointer to job required to submit
Chunming Zhou80de5912015-08-05 19:07:08 +0800237 * return 0 if succeed. -1 if failed.
238 * -2 indicate queue is full for this client, client should wait untill
239 * scheduler consum some queued command.
240 * -1 other fail.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800241*/
Chunming Zhoubb977d32015-08-18 15:16:40 +0800242int amd_sched_push_job(struct amd_sched_job *sched_job)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800243{
Chunming Zhoubb977d32015-08-18 15:16:40 +0800244 struct amd_sched_fence *fence =
245 amd_sched_fence_create(sched_job->s_entity);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800246 if (!fence)
247 return -EINVAL;
Chunming Zhoubb977d32015-08-18 15:16:40 +0800248 fence_get(&fence->base);
249 sched_job->s_fence = fence;
250 while (kfifo_in_spinlocked(&sched_job->s_entity->job_queue,
251 &sched_job, sizeof(void *),
252 &sched_job->s_entity->queue_lock) !=
253 sizeof(void *)) {
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800254 /**
255 * Current context used up all its IB slots
256 * wait here, or need to check whether GPU is hung
257 */
258 schedule();
259 }
Chunming Zhou1c8f8052015-08-13 13:04:06 +0800260 /* first job wake up scheduler */
Chunming Zhoubb977d32015-08-18 15:16:40 +0800261 if ((kfifo_len(&sched_job->s_entity->job_queue) / sizeof(void *)) == 1)
262 wake_up_interruptible(&sched_job->sched->wait_queue);
Chunming Zhou80de5912015-08-05 19:07:08 +0800263 return 0;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800264}
265
Christian König6f0e54a2015-08-05 21:22:10 +0200266static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
267{
268 struct amd_sched_job *sched_job =
269 container_of(cb, struct amd_sched_job, cb);
270 struct amd_gpu_scheduler *sched;
Christian König6f0e54a2015-08-05 21:22:10 +0200271
272 sched = sched_job->sched;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800273 amd_sched_fence_signal(sched_job->s_fence);
Christian Königc746ba22015-08-19 16:12:15 +0200274 atomic_dec(&sched->hw_rq_count);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800275 fence_put(&sched_job->s_fence->base);
Chunming Zhoubb977d32015-08-18 15:16:40 +0800276 sched->ops->process_job(sched, sched_job);
Christian König6f0e54a2015-08-05 21:22:10 +0200277 wake_up_interruptible(&sched->wait_queue);
278}
279
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800280static int amd_sched_main(void *param)
281{
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800282 struct sched_param sparam = {.sched_priority = 1};
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800283 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
Christian Königf85a6dd2015-08-19 17:37:52 +0200284 int r;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800285
286 sched_setscheduler(current, SCHED_FIFO, &sparam);
287
288 while (!kthread_should_stop()) {
Christian Königf85a6dd2015-08-19 17:37:52 +0200289 struct amd_sched_entity *c_entity = NULL;
290 struct amd_sched_job *job;
Christian König6f0e54a2015-08-05 21:22:10 +0200291 struct fence *fence;
292
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800293 wait_event_interruptible(sched->wait_queue,
Christian Königf85a6dd2015-08-19 17:37:52 +0200294 kthread_should_stop() ||
295 (c_entity = amd_sched_select_context(sched)));
296
297 if (!c_entity)
298 continue;
299
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800300 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
301 if (r != sizeof(void *))
302 continue;
Chunming Zhoubb977d32015-08-18 15:16:40 +0800303 r = 0;
304 if (sched->ops->prepare_job)
305 r = sched->ops->prepare_job(sched, c_entity, job);
Chunming Zhou4cef9262015-08-05 19:52:14 +0800306 if (!r) {
Christian Königc746ba22015-08-19 16:12:15 +0200307 atomic_inc(&sched->hw_rq_count);
Chunming Zhou4cef9262015-08-05 19:52:14 +0800308 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800309 mutex_lock(&sched->sched_lock);
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800310 fence = sched->ops->run_job(sched, c_entity, job);
Christian König6f0e54a2015-08-05 21:22:10 +0200311 if (fence) {
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800312 r = fence_add_callback(fence, &job->cb,
Christian König6f0e54a2015-08-05 21:22:10 +0200313 amd_sched_process_job);
314 if (r == -ENOENT)
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800315 amd_sched_process_job(fence, &job->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200316 else if (r)
317 DRM_ERROR("fence add callback failed (%d)\n", r);
318 fence_put(fence);
319 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800320 mutex_unlock(&sched->sched_lock);
Christian Königaef48522015-08-20 14:47:46 +0200321
322 if (c_entity->need_wakeup) {
323 c_entity->need_wakeup = false;
324 wake_up(&c_entity->wait_queue);
325 }
326
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800327 }
328 return 0;
329}
330
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800331/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800332 * Create a gpu scheduler
333 *
334 * @device The device context for this scheduler
335 * @ops The backend operations for this scheduler.
336 * @id The scheduler is per ring, here is ring id.
337 * @granularity The minumum ms unit the scheduler will scheduled.
338 * @preemption Indicate whether this ring support preemption, 0 is no.
339 *
340 * return the pointer to scheduler for success, otherwise return NULL
341*/
342struct amd_gpu_scheduler *amd_sched_create(void *device,
343 struct amd_sched_backend_ops *ops,
344 unsigned ring,
345 unsigned granularity,
Jammy Zhou4afcb302015-07-30 16:44:05 +0800346 unsigned preemption,
347 unsigned hw_submission)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800348{
349 struct amd_gpu_scheduler *sched;
Christian König4cd7f42c2015-08-05 18:18:52 +0200350 char name[20];
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800351
352 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
353 if (!sched)
354 return NULL;
355
356 sched->device = device;
357 sched->ops = ops;
358 sched->granularity = granularity;
359 sched->ring_id = ring;
360 sched->preemption = preemption;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800361 sched->hw_submission_limit = hw_submission;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800362 snprintf(name, sizeof(name), "gpu_sched[%d]", ring);
363 mutex_init(&sched->sched_lock);
Christian König432a4ff2015-08-12 11:46:04 +0200364 amd_sched_rq_init(&sched->sched_rq);
365 amd_sched_rq_init(&sched->kernel_rq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800366
367 init_waitqueue_head(&sched->wait_queue);
Christian Königc746ba22015-08-19 16:12:15 +0200368 atomic_set(&sched->hw_rq_count, 0);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800369 /* Each scheduler will run on a seperate kernel thread */
370 sched->thread = kthread_create(amd_sched_main, sched, name);
371 if (sched->thread) {
372 wake_up_process(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800373 return sched;
374 }
375
376 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800377 kfree(sched);
378 return NULL;
379}
380
381/**
382 * Destroy a gpu scheduler
383 *
384 * @sched The pointer to the scheduler
385 *
386 * return 0 if succeed. -1 if failed.
387 */
388int amd_sched_destroy(struct amd_gpu_scheduler *sched)
389{
390 kthread_stop(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800391 kfree(sched);
392 return 0;
393}