blob: b7cd108212c2bc8ce812a6a2d68cfa9d0094ba14 [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
Chunming Zhou353da3c2015-09-07 16:06:53 +080030#define CREATE_TRACE_POINTS
31#include "gpu_sched_trace.h"
32
Christian König69bd5bf2015-08-26 11:31:23 +020033static struct amd_sched_job *
34amd_sched_entity_pop_job(struct amd_sched_entity *entity);
Christian König88079002015-08-24 14:29:40 +020035static void amd_sched_wakeup(struct amd_gpu_scheduler *sched);
36
Chunming Zhouf5617f92015-11-05 11:41:50 +080037struct kmem_cache *sched_fence_slab;
38atomic_t sched_fence_slab_ref = ATOMIC_INIT(0);
39
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080040/* Initialize a given run queue struct */
Christian König432a4ff2015-08-12 11:46:04 +020041static void amd_sched_rq_init(struct amd_sched_rq *rq)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080042{
Christian König2b184d82015-08-18 14:41:25 +020043 spin_lock_init(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020044 INIT_LIST_HEAD(&rq->entities);
Christian König432a4ff2015-08-12 11:46:04 +020045 rq->current_entity = NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080046}
47
Christian König432a4ff2015-08-12 11:46:04 +020048static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
49 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080050{
Christian König2b184d82015-08-18 14:41:25 +020051 spin_lock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020052 list_add_tail(&entity->list, &rq->entities);
Christian König2b184d82015-08-18 14:41:25 +020053 spin_unlock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020054}
55
56static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
57 struct amd_sched_entity *entity)
58{
Christian König2b184d82015-08-18 14:41:25 +020059 spin_lock(&rq->lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080060 list_del_init(&entity->list);
Christian König432a4ff2015-08-12 11:46:04 +020061 if (rq->current_entity == entity)
62 rq->current_entity = NULL;
Christian König2b184d82015-08-18 14:41:25 +020063 spin_unlock(&rq->lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080064}
65
66/**
Christian König69bd5bf2015-08-26 11:31:23 +020067 * Select next job from a specified run queue with round robin policy.
68 * Return NULL if nothing available.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080069 */
Christian König69bd5bf2015-08-26 11:31:23 +020070static struct amd_sched_job *
71amd_sched_rq_select_job(struct amd_sched_rq *rq)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080072{
Christian König2b184d82015-08-18 14:41:25 +020073 struct amd_sched_entity *entity;
Junwei Zhang4c7eb912015-09-09 09:05:55 +080074 struct amd_sched_job *sched_job;
Christian König4cd7f42c2015-08-05 18:18:52 +020075
Christian König2b184d82015-08-18 14:41:25 +020076 spin_lock(&rq->lock);
77
78 entity = rq->current_entity;
Christian König432a4ff2015-08-12 11:46:04 +020079 if (entity) {
80 list_for_each_entry_continue(entity, &rq->entities, list) {
Junwei Zhang4c7eb912015-09-09 09:05:55 +080081 sched_job = amd_sched_entity_pop_job(entity);
82 if (sched_job) {
Christian König432a4ff2015-08-12 11:46:04 +020083 rq->current_entity = entity;
Christian König2b184d82015-08-18 14:41:25 +020084 spin_unlock(&rq->lock);
Junwei Zhang4c7eb912015-09-09 09:05:55 +080085 return sched_job;
Christian König432a4ff2015-08-12 11:46:04 +020086 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080087 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080088 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080089
Christian König432a4ff2015-08-12 11:46:04 +020090 list_for_each_entry(entity, &rq->entities, list) {
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080091
Junwei Zhang4c7eb912015-09-09 09:05:55 +080092 sched_job = amd_sched_entity_pop_job(entity);
93 if (sched_job) {
Christian König432a4ff2015-08-12 11:46:04 +020094 rq->current_entity = entity;
Christian König2b184d82015-08-18 14:41:25 +020095 spin_unlock(&rq->lock);
Junwei Zhang4c7eb912015-09-09 09:05:55 +080096 return sched_job;
Christian König432a4ff2015-08-12 11:46:04 +020097 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080098
Christian König432a4ff2015-08-12 11:46:04 +020099 if (entity == rq->current_entity)
100 break;
101 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800102
Christian König2b184d82015-08-18 14:41:25 +0200103 spin_unlock(&rq->lock);
104
Christian König432a4ff2015-08-12 11:46:04 +0200105 return NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800106}
107
108/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800109 * Init a context entity used by scheduler when submit to HW ring.
110 *
111 * @sched The pointer to the scheduler
Christian König91404fb2015-08-05 18:33:21 +0200112 * @entity The pointer to a valid amd_sched_entity
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800113 * @rq The run queue this entity belongs
Christian König0e89d0c2015-08-04 16:58:36 +0200114 * @kernel If this is an entity for the kernel
Jammy Zhou1333f722015-07-30 16:36:58 +0800115 * @jobs The max number of jobs in the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800116 *
117 * return 0 if succeed. negative error code on failure
118*/
Christian König91404fb2015-08-05 18:33:21 +0200119int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
Christian König6f0e54a2015-08-05 21:22:10 +0200120 struct amd_sched_entity *entity,
Christian König432a4ff2015-08-12 11:46:04 +0200121 struct amd_sched_rq *rq,
Christian König6f0e54a2015-08-05 21:22:10 +0200122 uint32_t jobs)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800123{
Christian König0f75aee2015-09-07 18:07:14 +0200124 int r;
125
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800126 if (!(sched && entity && rq))
127 return -EINVAL;
128
Christian König91404fb2015-08-05 18:33:21 +0200129 memset(entity, 0, sizeof(struct amd_sched_entity));
Christian König0f75aee2015-09-07 18:07:14 +0200130 INIT_LIST_HEAD(&entity->list);
131 entity->rq = rq;
132 entity->sched = sched;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800133
134 spin_lock_init(&entity->queue_lock);
Christian König0f75aee2015-09-07 18:07:14 +0200135 r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
136 if (r)
137 return r;
138
Christian Königce882e62015-08-19 15:00:55 +0200139 atomic_set(&entity->fence_seq, 0);
Christian König0f75aee2015-09-07 18:07:14 +0200140 entity->fence_context = fence_context_alloc(1);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800141
142 /* Add the entity to the run queue */
Christian König432a4ff2015-08-12 11:46:04 +0200143 amd_sched_rq_add_entity(rq, entity);
Christian König0f75aee2015-09-07 18:07:14 +0200144
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800145 return 0;
146}
147
148/**
149 * Query if entity is initialized
150 *
151 * @sched Pointer to scheduler instance
152 * @entity The pointer to a valid scheduler entity
153 *
154 * return true if entity is initialized, false otherwise
155*/
Christian Königd54fdb92015-08-20 17:03:48 +0200156static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
157 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800158{
Christian König0f75aee2015-09-07 18:07:14 +0200159 return entity->sched == sched &&
160 entity->rq != NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800161}
162
Christian Königaef48522015-08-20 14:47:46 +0200163/**
164 * Check if entity is idle
165 *
166 * @entity The pointer to a valid scheduler entity
167 *
168 * Return true if entity don't has any unscheduled jobs.
169 */
170static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800171{
Christian Königaef48522015-08-20 14:47:46 +0200172 rmb();
173 if (kfifo_is_empty(&entity->job_queue))
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800174 return true;
175
176 return false;
177}
178
179/**
180 * Destroy a context entity
181 *
182 * @sched Pointer to scheduler instance
183 * @entity The pointer to a valid scheduler entity
184 *
Christian König062c7fb2015-08-21 15:46:43 +0200185 * Cleanup and free the allocated resources.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800186 */
Christian König062c7fb2015-08-21 15:46:43 +0200187void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
188 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800189{
Christian König0f75aee2015-09-07 18:07:14 +0200190 struct amd_sched_rq *rq = entity->rq;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800191
Christian Königd54fdb92015-08-20 17:03:48 +0200192 if (!amd_sched_entity_is_initialized(sched, entity))
Christian König062c7fb2015-08-21 15:46:43 +0200193 return;
Christian König6c859272015-08-20 16:12:50 +0200194
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800195 /**
196 * The client will not queue more IBs during this fini, consume existing
197 * queued IBs
198 */
Christian Königc2b6bd72015-08-25 21:39:31 +0200199 wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800200
Christian König432a4ff2015-08-12 11:46:04 +0200201 amd_sched_rq_remove_entity(rq, entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800202 kfifo_free(&entity->job_queue);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800203}
204
Christian Könige61235d2015-08-25 11:05:36 +0200205static void amd_sched_entity_wakeup(struct fence *f, struct fence_cb *cb)
206{
207 struct amd_sched_entity *entity =
208 container_of(cb, struct amd_sched_entity, cb);
209 entity->dependency = NULL;
210 fence_put(f);
Christian König0f75aee2015-09-07 18:07:14 +0200211 amd_sched_wakeup(entity->sched);
Christian Könige61235d2015-08-25 11:05:36 +0200212}
213
Christian König393a0bd2015-11-05 12:57:10 +0100214static bool amd_sched_entity_add_dependency_cb(struct amd_sched_entity *entity)
215{
216 struct amd_gpu_scheduler *sched = entity->sched;
217 struct fence * fence = entity->dependency;
218 struct amd_sched_fence *s_fence;
219
220 if (fence->context == entity->fence_context) {
221 /* We can ignore fences from ourself */
222 fence_put(entity->dependency);
223 return false;
224 }
225
226 s_fence = to_amd_sched_fence(fence);
227 if (s_fence && s_fence->sched == sched) {
228 /* Fence is from the same scheduler */
229 if (test_bit(AMD_SCHED_FENCE_SCHEDULED_BIT, &fence->flags)) {
230 /* Ignore it when it is already scheduled */
231 fence_put(entity->dependency);
232 return false;
233 }
234
235 /* Wait for fence to be scheduled */
236 entity->cb.func = amd_sched_entity_wakeup;
237 list_add_tail(&entity->cb.node, &s_fence->scheduled_cb);
238 return true;
239 }
240
241 if (!fence_add_callback(entity->dependency, &entity->cb,
242 amd_sched_entity_wakeup))
243 return true;
244
245 fence_put(entity->dependency);
246 return false;
247}
248
Christian König69bd5bf2015-08-26 11:31:23 +0200249static struct amd_sched_job *
250amd_sched_entity_pop_job(struct amd_sched_entity *entity)
251{
Christian König0f75aee2015-09-07 18:07:14 +0200252 struct amd_gpu_scheduler *sched = entity->sched;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800253 struct amd_sched_job *sched_job;
Christian König69bd5bf2015-08-26 11:31:23 +0200254
Christian Könige61235d2015-08-25 11:05:36 +0200255 if (ACCESS_ONCE(entity->dependency))
256 return NULL;
257
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800258 if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job)))
Christian König69bd5bf2015-08-26 11:31:23 +0200259 return NULL;
260
Christian König393a0bd2015-11-05 12:57:10 +0100261 while ((entity->dependency = sched->ops->dependency(sched_job)))
262 if (amd_sched_entity_add_dependency_cb(entity))
Christian Könige61235d2015-08-25 11:05:36 +0200263 return NULL;
Christian Könige61235d2015-08-25 11:05:36 +0200264
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800265 return sched_job;
Christian König69bd5bf2015-08-26 11:31:23 +0200266}
267
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800268/**
Christian König6c859272015-08-20 16:12:50 +0200269 * Helper to submit a job to the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800270 *
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800271 * @sched_job The pointer to job required to submit
Christian König6c859272015-08-20 16:12:50 +0200272 *
273 * Returns true if we could submit the job.
274 */
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800275static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800276{
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800277 struct amd_sched_entity *entity = sched_job->s_entity;
Christian König6c859272015-08-20 16:12:50 +0200278 bool added, first = false;
279
280 spin_lock(&entity->queue_lock);
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800281 added = kfifo_in(&entity->job_queue, &sched_job,
282 sizeof(sched_job)) == sizeof(sched_job);
Christian König6c859272015-08-20 16:12:50 +0200283
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800284 if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
Christian König6c859272015-08-20 16:12:50 +0200285 first = true;
286
287 spin_unlock(&entity->queue_lock);
288
289 /* first job wakes up scheduler */
290 if (first)
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800291 amd_sched_wakeup(sched_job->sched);
Christian König6c859272015-08-20 16:12:50 +0200292
293 return added;
294}
295
296/**
297 * Submit a job to the job queue
298 *
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800299 * @sched_job The pointer to job required to submit
Christian König6c859272015-08-20 16:12:50 +0200300 *
301 * Returns 0 for success, negative error code otherwise.
302 */
Christian Könige2840222015-11-05 19:49:48 +0100303void amd_sched_entity_push_job(struct amd_sched_job *sched_job)
Christian König6c859272015-08-20 16:12:50 +0200304{
305 struct amd_sched_entity *entity = sched_job->s_entity;
Christian König6c859272015-08-20 16:12:50 +0200306
Christian König0f75aee2015-09-07 18:07:14 +0200307 wait_event(entity->sched->job_scheduled,
Chunming Zhouc9f0fe52015-08-31 15:46:12 +0800308 amd_sched_entity_in(sched_job));
Chunming Zhou353da3c2015-09-07 16:06:53 +0800309 trace_amd_sched_job(sched_job);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800310}
311
Christian Könige688b7282015-08-20 17:01:01 +0200312/**
313 * Return ture if we can push more jobs to the hw.
314 */
315static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
316{
317 return atomic_read(&sched->hw_rq_count) <
318 sched->hw_submission_limit;
319}
320
321/**
Christian König88079002015-08-24 14:29:40 +0200322 * Wake up the scheduler when it is ready
323 */
324static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
325{
326 if (amd_sched_ready(sched))
Christian Königc2b6bd72015-08-25 21:39:31 +0200327 wake_up_interruptible(&sched->wake_up_worker);
Christian König88079002015-08-24 14:29:40 +0200328}
329
330/**
Christian König69bd5bf2015-08-26 11:31:23 +0200331 * Select next to run
Christian Könige688b7282015-08-20 17:01:01 +0200332*/
Christian König69bd5bf2015-08-26 11:31:23 +0200333static struct amd_sched_job *
334amd_sched_select_job(struct amd_gpu_scheduler *sched)
Christian Könige688b7282015-08-20 17:01:01 +0200335{
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800336 struct amd_sched_job *sched_job;
Christian Könige688b7282015-08-20 17:01:01 +0200337
338 if (!amd_sched_ready(sched))
339 return NULL;
340
341 /* Kernel run queue has higher priority than normal run queue*/
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800342 sched_job = amd_sched_rq_select_job(&sched->kernel_rq);
343 if (sched_job == NULL)
344 sched_job = amd_sched_rq_select_job(&sched->sched_rq);
Christian Könige688b7282015-08-20 17:01:01 +0200345
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800346 return sched_job;
Christian Könige688b7282015-08-20 17:01:01 +0200347}
348
Christian König6f0e54a2015-08-05 21:22:10 +0200349static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
350{
Christian König258f3f92015-08-31 17:02:52 +0200351 struct amd_sched_fence *s_fence =
352 container_of(cb, struct amd_sched_fence, cb);
Christian König9b398fa2015-09-07 18:16:49 +0200353 struct amd_gpu_scheduler *sched = s_fence->sched;
Junwei Zhang2440ff22015-10-10 08:48:42 +0800354 unsigned long flags;
Christian König6f0e54a2015-08-05 21:22:10 +0200355
Christian Königc746ba22015-08-19 16:12:15 +0200356 atomic_dec(&sched->hw_rq_count);
Christian König258f3f92015-08-31 17:02:52 +0200357 amd_sched_fence_signal(s_fence);
Junwei Zhang2440ff22015-10-10 08:48:42 +0800358 if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
Christian König424839a2015-11-02 16:25:10 +0100359 cancel_delayed_work(&s_fence->dwork);
Junwei Zhang2440ff22015-10-10 08:48:42 +0800360 spin_lock_irqsave(&sched->fence_list_lock, flags);
361 list_del_init(&s_fence->list);
362 spin_unlock_irqrestore(&sched->fence_list_lock, flags);
363 }
Chunming Zhou7034dec2015-11-11 14:56:00 +0800364 trace_amd_sched_process_job(s_fence);
Christian König258f3f92015-08-31 17:02:52 +0200365 fence_put(&s_fence->base);
Christian Königc2b6bd72015-08-25 21:39:31 +0200366 wake_up_interruptible(&sched->wake_up_worker);
Christian König6f0e54a2015-08-05 21:22:10 +0200367}
368
Junwei Zhang2440ff22015-10-10 08:48:42 +0800369static void amd_sched_fence_work_func(struct work_struct *work)
370{
371 struct amd_sched_fence *s_fence =
372 container_of(work, struct amd_sched_fence, dwork.work);
373 struct amd_gpu_scheduler *sched = s_fence->sched;
374 struct amd_sched_fence *entity, *tmp;
375 unsigned long flags;
376
377 DRM_ERROR("[%s] scheduler is timeout!\n", sched->name);
378
379 /* Clean all pending fences */
Junwei Zhang2fcef6e2015-10-13 11:14:23 +0800380 spin_lock_irqsave(&sched->fence_list_lock, flags);
Junwei Zhang2440ff22015-10-10 08:48:42 +0800381 list_for_each_entry_safe(entity, tmp, &sched->fence_list, list) {
382 DRM_ERROR(" fence no %d\n", entity->base.seqno);
Junwei Zhang2fcef6e2015-10-13 11:14:23 +0800383 cancel_delayed_work(&entity->dwork);
Junwei Zhang2440ff22015-10-10 08:48:42 +0800384 list_del_init(&entity->list);
Junwei Zhang2440ff22015-10-10 08:48:42 +0800385 fence_put(&entity->base);
386 }
Junwei Zhang2fcef6e2015-10-13 11:14:23 +0800387 spin_unlock_irqrestore(&sched->fence_list_lock, flags);
Junwei Zhang2440ff22015-10-10 08:48:42 +0800388}
389
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800390static int amd_sched_main(void *param)
391{
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800392 struct sched_param sparam = {.sched_priority = 1};
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800393 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
Alex Deucher5134e992015-09-04 00:11:54 -0400394 int r, count;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800395
Junwei Zhang2440ff22015-10-10 08:48:42 +0800396 spin_lock_init(&sched->fence_list_lock);
397 INIT_LIST_HEAD(&sched->fence_list);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800398 sched_setscheduler(current, SCHED_FIFO, &sparam);
399
400 while (!kthread_should_stop()) {
Christian König69bd5bf2015-08-26 11:31:23 +0200401 struct amd_sched_entity *entity;
Christian König258f3f92015-08-31 17:02:52 +0200402 struct amd_sched_fence *s_fence;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800403 struct amd_sched_job *sched_job;
Christian König6f0e54a2015-08-05 21:22:10 +0200404 struct fence *fence;
Junwei Zhang2440ff22015-10-10 08:48:42 +0800405 unsigned long flags;
Christian König6f0e54a2015-08-05 21:22:10 +0200406
Christian Königc2b6bd72015-08-25 21:39:31 +0200407 wait_event_interruptible(sched->wake_up_worker,
Christian Königf85a6dd2015-08-19 17:37:52 +0200408 kthread_should_stop() ||
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800409 (sched_job = amd_sched_select_job(sched)));
Christian Königf85a6dd2015-08-19 17:37:52 +0200410
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800411 if (!sched_job)
Christian Königf85a6dd2015-08-19 17:37:52 +0200412 continue;
413
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800414 entity = sched_job->s_entity;
415 s_fence = sched_job->s_fence;
Junwei Zhang2440ff22015-10-10 08:48:42 +0800416
417 if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
418 INIT_DELAYED_WORK(&s_fence->dwork, amd_sched_fence_work_func);
419 schedule_delayed_work(&s_fence->dwork, sched->timeout);
420 spin_lock_irqsave(&sched->fence_list_lock, flags);
421 list_add_tail(&s_fence->list, &sched->fence_list);
422 spin_unlock_irqrestore(&sched->fence_list_lock, flags);
423 }
424
Christian Königb034b572015-08-20 17:08:25 +0200425 atomic_inc(&sched->hw_rq_count);
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800426 fence = sched->ops->run_job(sched_job);
Christian König393a0bd2015-11-05 12:57:10 +0100427 amd_sched_fence_scheduled(s_fence);
Christian König6f0e54a2015-08-05 21:22:10 +0200428 if (fence) {
Christian König258f3f92015-08-31 17:02:52 +0200429 r = fence_add_callback(fence, &s_fence->cb,
Christian König6f0e54a2015-08-05 21:22:10 +0200430 amd_sched_process_job);
431 if (r == -ENOENT)
Christian König258f3f92015-08-31 17:02:52 +0200432 amd_sched_process_job(fence, &s_fence->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200433 else if (r)
434 DRM_ERROR("fence add callback failed (%d)\n", r);
435 fence_put(fence);
Christian König27439fc2015-09-02 12:03:06 +0200436 } else {
437 DRM_ERROR("Failed to run job!\n");
Christian König258f3f92015-08-31 17:02:52 +0200438 amd_sched_process_job(NULL, &s_fence->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200439 }
Christian Königaef48522015-08-20 14:47:46 +0200440
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800441 count = kfifo_out(&entity->job_queue, &sched_job,
442 sizeof(sched_job));
443 WARN_ON(count != sizeof(sched_job));
Christian Königc2b6bd72015-08-25 21:39:31 +0200444 wake_up(&sched->job_scheduled);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800445 }
446 return 0;
447}
448
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800449/**
Christian König4f839a22015-09-08 20:22:31 +0200450 * Init a gpu scheduler instance
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800451 *
Christian König4f839a22015-09-08 20:22:31 +0200452 * @sched The pointer to the scheduler
Christian König69f7dd62015-08-20 17:24:40 +0200453 * @ops The backend operations for this scheduler.
Christian König69f7dd62015-08-20 17:24:40 +0200454 * @hw_submissions Number of hw submissions to do.
Christian König4f839a22015-09-08 20:22:31 +0200455 * @name Name used for debugging
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800456 *
Christian König4f839a22015-09-08 20:22:31 +0200457 * Return 0 on success, otherwise error code.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800458*/
Christian König4f839a22015-09-08 20:22:31 +0200459int amd_sched_init(struct amd_gpu_scheduler *sched,
460 struct amd_sched_backend_ops *ops,
Junwei Zhang2440ff22015-10-10 08:48:42 +0800461 unsigned hw_submission, long timeout, const char *name)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800462{
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800463 sched->ops = ops;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800464 sched->hw_submission_limit = hw_submission;
Christian König4f839a22015-09-08 20:22:31 +0200465 sched->name = name;
Junwei Zhang2440ff22015-10-10 08:48:42 +0800466 sched->timeout = timeout;
Christian König432a4ff2015-08-12 11:46:04 +0200467 amd_sched_rq_init(&sched->sched_rq);
468 amd_sched_rq_init(&sched->kernel_rq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800469
Christian Königc2b6bd72015-08-25 21:39:31 +0200470 init_waitqueue_head(&sched->wake_up_worker);
471 init_waitqueue_head(&sched->job_scheduled);
Christian Königc746ba22015-08-19 16:12:15 +0200472 atomic_set(&sched->hw_rq_count, 0);
Chunming Zhouf5617f92015-11-05 11:41:50 +0800473 if (atomic_inc_return(&sched_fence_slab_ref) == 1) {
474 sched_fence_slab = kmem_cache_create(
475 "amd_sched_fence", sizeof(struct amd_sched_fence), 0,
476 SLAB_HWCACHE_ALIGN, NULL);
477 if (!sched_fence_slab)
478 return -ENOMEM;
479 }
Christian König4f839a22015-09-08 20:22:31 +0200480
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800481 /* Each scheduler will run on a seperate kernel thread */
Christian Königc14692f02015-08-21 15:18:47 +0200482 sched->thread = kthread_run(amd_sched_main, sched, sched->name);
Christian Königf4956592015-08-20 16:59:38 +0200483 if (IS_ERR(sched->thread)) {
Christian König4f839a22015-09-08 20:22:31 +0200484 DRM_ERROR("Failed to create scheduler for %s.\n", name);
485 return PTR_ERR(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800486 }
487
Christian König4f839a22015-09-08 20:22:31 +0200488 return 0;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800489}
490
491/**
492 * Destroy a gpu scheduler
493 *
494 * @sched The pointer to the scheduler
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800495 */
Christian König4f839a22015-09-08 20:22:31 +0200496void amd_sched_fini(struct amd_gpu_scheduler *sched)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800497{
Dave Airlie32544d02015-11-03 11:10:03 -0500498 if (sched->thread)
499 kthread_stop(sched->thread);
Chunming Zhouf5617f92015-11-05 11:41:50 +0800500 if (atomic_dec_and_test(&sched_fence_slab_ref))
501 kmem_cache_destroy(sched_fence_slab);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800502}