blob: d99fe90991dc47344974f43e6d078f3314469308 [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
Christian König88079002015-08-24 14:29:40 +020030static void amd_sched_wakeup(struct amd_gpu_scheduler *sched);
31
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080032/* Initialize a given run queue struct */
Christian König432a4ff2015-08-12 11:46:04 +020033static void amd_sched_rq_init(struct amd_sched_rq *rq)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080034{
Christian König2b184d82015-08-18 14:41:25 +020035 spin_lock_init(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020036 INIT_LIST_HEAD(&rq->entities);
Christian König432a4ff2015-08-12 11:46:04 +020037 rq->current_entity = NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080038}
39
Christian König432a4ff2015-08-12 11:46:04 +020040static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
41 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080042{
Christian König2b184d82015-08-18 14:41:25 +020043 spin_lock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020044 list_add_tail(&entity->list, &rq->entities);
Christian König2b184d82015-08-18 14:41:25 +020045 spin_unlock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020046}
47
48static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
49 struct amd_sched_entity *entity)
50{
Christian König2b184d82015-08-18 14:41:25 +020051 spin_lock(&rq->lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080052 list_del_init(&entity->list);
Christian König432a4ff2015-08-12 11:46:04 +020053 if (rq->current_entity == entity)
54 rq->current_entity = NULL;
Christian König2b184d82015-08-18 14:41:25 +020055 spin_unlock(&rq->lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080056}
57
58/**
59 * Select next entity from a specified run queue with round robin policy.
60 * It could return the same entity as current one if current is the only
61 * available one in the queue. Return NULL if nothing available.
62 */
Christian König432a4ff2015-08-12 11:46:04 +020063static struct amd_sched_entity *
64amd_sched_rq_select_entity(struct amd_sched_rq *rq)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080065{
Christian König2b184d82015-08-18 14:41:25 +020066 struct amd_sched_entity *entity;
Christian König4cd7f42c2015-08-05 18:18:52 +020067
Christian König2b184d82015-08-18 14:41:25 +020068 spin_lock(&rq->lock);
69
70 entity = rq->current_entity;
Christian König432a4ff2015-08-12 11:46:04 +020071 if (entity) {
72 list_for_each_entry_continue(entity, &rq->entities, list) {
73 if (!kfifo_is_empty(&entity->job_queue)) {
74 rq->current_entity = entity;
Christian König2b184d82015-08-18 14:41:25 +020075 spin_unlock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020076 return rq->current_entity;
77 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080078 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080079 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080080
Christian König432a4ff2015-08-12 11:46:04 +020081 list_for_each_entry(entity, &rq->entities, list) {
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080082
Christian König432a4ff2015-08-12 11:46:04 +020083 if (!kfifo_is_empty(&entity->job_queue)) {
84 rq->current_entity = entity;
Christian König2b184d82015-08-18 14:41:25 +020085 spin_unlock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020086 return rq->current_entity;
87 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080088
Christian König432a4ff2015-08-12 11:46:04 +020089 if (entity == rq->current_entity)
90 break;
91 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080092
Christian König2b184d82015-08-18 14:41:25 +020093 spin_unlock(&rq->lock);
94
Christian König432a4ff2015-08-12 11:46:04 +020095 return NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080096}
97
98/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080099 * Init a context entity used by scheduler when submit to HW ring.
100 *
101 * @sched The pointer to the scheduler
Christian König91404fb2015-08-05 18:33:21 +0200102 * @entity The pointer to a valid amd_sched_entity
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800103 * @rq The run queue this entity belongs
Christian König0e89d0c2015-08-04 16:58:36 +0200104 * @kernel If this is an entity for the kernel
Jammy Zhou1333f722015-07-30 16:36:58 +0800105 * @jobs The max number of jobs in the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800106 *
107 * return 0 if succeed. negative error code on failure
108*/
Christian König91404fb2015-08-05 18:33:21 +0200109int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
Christian König6f0e54a2015-08-05 21:22:10 +0200110 struct amd_sched_entity *entity,
Christian König432a4ff2015-08-12 11:46:04 +0200111 struct amd_sched_rq *rq,
Christian König6f0e54a2015-08-05 21:22:10 +0200112 uint32_t jobs)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800113{
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800114 if (!(sched && entity && rq))
115 return -EINVAL;
116
Christian König91404fb2015-08-05 18:33:21 +0200117 memset(entity, 0, sizeof(struct amd_sched_entity));
Christian König91404fb2015-08-05 18:33:21 +0200118 entity->belongto_rq = rq;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800119 entity->scheduler = sched;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800120 entity->fence_context = fence_context_alloc(1);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800121 if(kfifo_alloc(&entity->job_queue,
Jammy Zhou1333f722015-07-30 16:36:58 +0800122 jobs * sizeof(void *),
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800123 GFP_KERNEL))
124 return -EINVAL;
125
126 spin_lock_init(&entity->queue_lock);
Christian Königce882e62015-08-19 15:00:55 +0200127 atomic_set(&entity->fence_seq, 0);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800128
129 /* Add the entity to the run queue */
Christian König432a4ff2015-08-12 11:46:04 +0200130 amd_sched_rq_add_entity(rq, entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800131 return 0;
132}
133
134/**
135 * Query if entity is initialized
136 *
137 * @sched Pointer to scheduler instance
138 * @entity The pointer to a valid scheduler entity
139 *
140 * return true if entity is initialized, false otherwise
141*/
Christian Königd54fdb92015-08-20 17:03:48 +0200142static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
143 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800144{
145 return entity->scheduler == sched &&
Christian König91404fb2015-08-05 18:33:21 +0200146 entity->belongto_rq != NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800147}
148
Christian Königaef48522015-08-20 14:47:46 +0200149/**
150 * Check if entity is idle
151 *
152 * @entity The pointer to a valid scheduler entity
153 *
154 * Return true if entity don't has any unscheduled jobs.
155 */
156static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800157{
Christian Königaef48522015-08-20 14:47:46 +0200158 rmb();
159 if (kfifo_is_empty(&entity->job_queue))
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800160 return true;
161
162 return false;
163}
164
165/**
166 * Destroy a context entity
167 *
168 * @sched Pointer to scheduler instance
169 * @entity The pointer to a valid scheduler entity
170 *
Christian König062c7fb2015-08-21 15:46:43 +0200171 * Cleanup and free the allocated resources.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800172 */
Christian König062c7fb2015-08-21 15:46:43 +0200173void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
174 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800175{
Christian König432a4ff2015-08-12 11:46:04 +0200176 struct amd_sched_rq *rq = entity->belongto_rq;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800177
Christian Königd54fdb92015-08-20 17:03:48 +0200178 if (!amd_sched_entity_is_initialized(sched, entity))
Christian König062c7fb2015-08-21 15:46:43 +0200179 return;
Christian König6c859272015-08-20 16:12:50 +0200180
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800181 /**
182 * The client will not queue more IBs during this fini, consume existing
183 * queued IBs
184 */
Christian Königc2b6bd72015-08-25 21:39:31 +0200185 wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800186
Christian König432a4ff2015-08-12 11:46:04 +0200187 amd_sched_rq_remove_entity(rq, entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800188 kfifo_free(&entity->job_queue);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800189}
190
191/**
Christian König6c859272015-08-20 16:12:50 +0200192 * Helper to submit a job to the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800193 *
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800194 * @job The pointer to job required to submit
Christian König6c859272015-08-20 16:12:50 +0200195 *
196 * Returns true if we could submit the job.
197 */
198static bool amd_sched_entity_in(struct amd_sched_job *job)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800199{
Christian König6c859272015-08-20 16:12:50 +0200200 struct amd_sched_entity *entity = job->s_entity;
201 bool added, first = false;
202
203 spin_lock(&entity->queue_lock);
204 added = kfifo_in(&entity->job_queue, &job, sizeof(job)) == sizeof(job);
205
206 if (added && kfifo_len(&entity->job_queue) == sizeof(job))
207 first = true;
208
209 spin_unlock(&entity->queue_lock);
210
211 /* first job wakes up scheduler */
212 if (first)
Christian König88079002015-08-24 14:29:40 +0200213 amd_sched_wakeup(job->sched);
Christian König6c859272015-08-20 16:12:50 +0200214
215 return added;
216}
217
218/**
219 * Submit a job to the job queue
220 *
221 * @job The pointer to job required to submit
222 *
223 * Returns 0 for success, negative error code otherwise.
224 */
225int amd_sched_entity_push_job(struct amd_sched_job *sched_job)
226{
227 struct amd_sched_entity *entity = sched_job->s_entity;
Chunming Zhou84f76ea2015-08-24 12:47:36 +0800228 struct amd_sched_fence *fence = amd_sched_fence_create(
229 entity, sched_job->owner);
Christian König6c859272015-08-20 16:12:50 +0200230 int r;
231
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800232 if (!fence)
Christian König6c859272015-08-20 16:12:50 +0200233 return -ENOMEM;
234
Chunming Zhoubb977d32015-08-18 15:16:40 +0800235 fence_get(&fence->base);
236 sched_job->s_fence = fence;
Christian König6c859272015-08-20 16:12:50 +0200237
Christian Königc2b6bd72015-08-25 21:39:31 +0200238 r = wait_event_interruptible(entity->scheduler->job_scheduled,
Christian König6c859272015-08-20 16:12:50 +0200239 amd_sched_entity_in(sched_job));
240
241 return r;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800242}
243
Christian Könige688b7282015-08-20 17:01:01 +0200244/**
245 * Return ture if we can push more jobs to the hw.
246 */
247static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
248{
249 return atomic_read(&sched->hw_rq_count) <
250 sched->hw_submission_limit;
251}
252
253/**
Christian König88079002015-08-24 14:29:40 +0200254 * Wake up the scheduler when it is ready
255 */
256static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
257{
258 if (amd_sched_ready(sched))
Christian Königc2b6bd72015-08-25 21:39:31 +0200259 wake_up_interruptible(&sched->wake_up_worker);
Christian König88079002015-08-24 14:29:40 +0200260}
261
262/**
Christian Könige688b7282015-08-20 17:01:01 +0200263 * Select next entity containing real IB submissions
264*/
265static struct amd_sched_entity *
266amd_sched_select_context(struct amd_gpu_scheduler *sched)
267{
268 struct amd_sched_entity *tmp;
269
270 if (!amd_sched_ready(sched))
271 return NULL;
272
273 /* Kernel run queue has higher priority than normal run queue*/
274 tmp = amd_sched_rq_select_entity(&sched->kernel_rq);
275 if (tmp == NULL)
276 tmp = amd_sched_rq_select_entity(&sched->sched_rq);
277
278 return tmp;
279}
280
Christian König6f0e54a2015-08-05 21:22:10 +0200281static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
282{
283 struct amd_sched_job *sched_job =
284 container_of(cb, struct amd_sched_job, cb);
285 struct amd_gpu_scheduler *sched;
Christian König6f0e54a2015-08-05 21:22:10 +0200286
287 sched = sched_job->sched;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800288 amd_sched_fence_signal(sched_job->s_fence);
Christian Königc746ba22015-08-19 16:12:15 +0200289 atomic_dec(&sched->hw_rq_count);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800290 fence_put(&sched_job->s_fence->base);
Christian Königbd755d02015-08-24 14:57:26 +0200291 sched->ops->process_job(sched_job);
Christian Königc2b6bd72015-08-25 21:39:31 +0200292 wake_up_interruptible(&sched->wake_up_worker);
Christian König6f0e54a2015-08-05 21:22:10 +0200293}
294
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800295static int amd_sched_main(void *param)
296{
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800297 struct sched_param sparam = {.sched_priority = 1};
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800298 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
Christian Königf85a6dd2015-08-19 17:37:52 +0200299 int r;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800300
301 sched_setscheduler(current, SCHED_FIFO, &sparam);
302
303 while (!kthread_should_stop()) {
Christian Königf85a6dd2015-08-19 17:37:52 +0200304 struct amd_sched_entity *c_entity = NULL;
305 struct amd_sched_job *job;
Christian König6f0e54a2015-08-05 21:22:10 +0200306 struct fence *fence;
307
Christian Königc2b6bd72015-08-25 21:39:31 +0200308 wait_event_interruptible(sched->wake_up_worker,
Christian Königf85a6dd2015-08-19 17:37:52 +0200309 kthread_should_stop() ||
310 (c_entity = amd_sched_select_context(sched)));
311
312 if (!c_entity)
313 continue;
314
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800315 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
316 if (r != sizeof(void *))
317 continue;
Christian Königb034b572015-08-20 17:08:25 +0200318 atomic_inc(&sched->hw_rq_count);
319
Christian Königbd755d02015-08-24 14:57:26 +0200320 fence = sched->ops->run_job(job);
Christian König6f0e54a2015-08-05 21:22:10 +0200321 if (fence) {
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800322 r = fence_add_callback(fence, &job->cb,
Christian König6f0e54a2015-08-05 21:22:10 +0200323 amd_sched_process_job);
324 if (r == -ENOENT)
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800325 amd_sched_process_job(fence, &job->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200326 else if (r)
327 DRM_ERROR("fence add callback failed (%d)\n", r);
328 fence_put(fence);
329 }
Christian Königaef48522015-08-20 14:47:46 +0200330
Christian Königc2b6bd72015-08-25 21:39:31 +0200331 wake_up(&sched->job_scheduled);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800332 }
333 return 0;
334}
335
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800336/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800337 * Create a gpu scheduler
338 *
Christian König69f7dd62015-08-20 17:24:40 +0200339 * @ops The backend operations for this scheduler.
340 * @ring The the ring id for the scheduler.
341 * @hw_submissions Number of hw submissions to do.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800342 *
Christian König69f7dd62015-08-20 17:24:40 +0200343 * Return the pointer to scheduler for success, otherwise return NULL
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800344*/
Christian König69f7dd62015-08-20 17:24:40 +0200345struct amd_gpu_scheduler *amd_sched_create(struct amd_sched_backend_ops *ops,
Chunming Zhouf38fdfd2015-08-24 11:35:26 +0800346 unsigned ring, unsigned hw_submission,
347 void *priv)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800348{
349 struct amd_gpu_scheduler *sched;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800350
351 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
352 if (!sched)
353 return NULL;
354
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800355 sched->ops = ops;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800356 sched->ring_id = ring;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800357 sched->hw_submission_limit = hw_submission;
Chunming Zhouf38fdfd2015-08-24 11:35:26 +0800358 sched->priv = priv;
Christian Königc14692f02015-08-21 15:18:47 +0200359 snprintf(sched->name, sizeof(sched->name), "amdgpu[%d]", ring);
Christian König432a4ff2015-08-12 11:46:04 +0200360 amd_sched_rq_init(&sched->sched_rq);
361 amd_sched_rq_init(&sched->kernel_rq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800362
Christian Königc2b6bd72015-08-25 21:39:31 +0200363 init_waitqueue_head(&sched->wake_up_worker);
364 init_waitqueue_head(&sched->job_scheduled);
Christian Königc746ba22015-08-19 16:12:15 +0200365 atomic_set(&sched->hw_rq_count, 0);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800366 /* Each scheduler will run on a seperate kernel thread */
Christian Königc14692f02015-08-21 15:18:47 +0200367 sched->thread = kthread_run(amd_sched_main, sched, sched->name);
Christian Königf4956592015-08-20 16:59:38 +0200368 if (IS_ERR(sched->thread)) {
369 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
370 kfree(sched);
371 return NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800372 }
373
Christian Königf4956592015-08-20 16:59:38 +0200374 return sched;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800375}
376
377/**
378 * Destroy a gpu scheduler
379 *
380 * @sched The pointer to the scheduler
381 *
382 * return 0 if succeed. -1 if failed.
383 */
384int amd_sched_destroy(struct amd_gpu_scheduler *sched)
385{
386 kthread_stop(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800387 kfree(sched);
388 return 0;
389}