blob: 265d3e2f63cc97903d1436089a80475aba5011e5 [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önig432a4ff2015-08-12 11:46:04 +020033 INIT_LIST_HEAD(&rq->entities);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080034 mutex_init(&rq->lock);
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önig432a4ff2015-08-12 11:46:04 +020041 mutex_lock(&rq->lock);
42 list_add_tail(&entity->list, &rq->entities);
43 mutex_unlock(&rq->lock);
44}
45
46static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
47 struct amd_sched_entity *entity)
48{
49 mutex_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;
53 mutex_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önig432a4ff2015-08-12 11:46:04 +020064 struct amd_sched_entity *entity = rq->current_entity;
Christian König4cd7f42c2015-08-05 18:18:52 +020065
Christian König432a4ff2015-08-12 11:46:04 +020066 if (entity) {
67 list_for_each_entry_continue(entity, &rq->entities, list) {
68 if (!kfifo_is_empty(&entity->job_queue)) {
69 rq->current_entity = entity;
70 return rq->current_entity;
71 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080072 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080073 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080074
Christian König432a4ff2015-08-12 11:46:04 +020075 list_for_each_entry(entity, &rq->entities, list) {
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080076
Christian König432a4ff2015-08-12 11:46:04 +020077 if (!kfifo_is_empty(&entity->job_queue)) {
78 rq->current_entity = entity;
79 return rq->current_entity;
80 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080081
Christian König432a4ff2015-08-12 11:46:04 +020082 if (entity == rq->current_entity)
83 break;
84 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080085
Christian König432a4ff2015-08-12 11:46:04 +020086 return NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080087}
88
89/**
90 * Note: This function should only been called inside scheduler main
91 * function for thread safety, there is no other protection here.
92 * return ture if scheduler has something ready to run.
93 *
94 * For active_hw_rq, there is only one producer(scheduler thread) and
95 * one consumer(ISR). It should be safe to use this function in scheduler
96 * main thread to decide whether to continue emit more IBs.
97*/
98static bool is_scheduler_ready(struct amd_gpu_scheduler *sched)
99{
Chunming Zhou4cef9262015-08-05 19:52:14 +0800100 unsigned long flags;
101 bool full;
Christian König4cd7f42c2015-08-05 18:18:52 +0200102
Chunming Zhou4cef9262015-08-05 19:52:14 +0800103 spin_lock_irqsave(&sched->queue_lock, flags);
104 full = atomic64_read(&sched->hw_rq_count) <
105 sched->hw_submission_limit ? true : false;
106 spin_unlock_irqrestore(&sched->queue_lock, flags);
107
108 return full;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800109}
110
111/**
112 * Select next entity from the kernel run queue, if not available,
113 * return null.
114*/
Christian König91404fb2015-08-05 18:33:21 +0200115static struct amd_sched_entity *
Christian König4cd7f42c2015-08-05 18:18:52 +0200116kernel_rq_select_context(struct amd_gpu_scheduler *sched)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800117{
Christian König4cd7f42c2015-08-05 18:18:52 +0200118 struct amd_sched_entity *sched_entity;
Christian König432a4ff2015-08-12 11:46:04 +0200119 struct amd_sched_rq *rq = &sched->kernel_rq;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800120
121 mutex_lock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +0200122 sched_entity = amd_sched_rq_select_entity(rq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800123 mutex_unlock(&rq->lock);
Christian König91404fb2015-08-05 18:33:21 +0200124 return sched_entity;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800125}
126
127/**
128 * Select next entity containing real IB submissions
129*/
Christian König91404fb2015-08-05 18:33:21 +0200130static struct amd_sched_entity *
Christian König4cd7f42c2015-08-05 18:18:52 +0200131select_context(struct amd_gpu_scheduler *sched)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800132{
Christian König91404fb2015-08-05 18:33:21 +0200133 struct amd_sched_entity *wake_entity = NULL;
134 struct amd_sched_entity *tmp;
Christian König432a4ff2015-08-12 11:46:04 +0200135 struct amd_sched_rq *rq;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800136
137 if (!is_scheduler_ready(sched))
138 return NULL;
139
140 /* Kernel run queue has higher priority than normal run queue*/
141 tmp = kernel_rq_select_context(sched);
142 if (tmp != NULL)
143 goto exit;
144
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800145 rq = &sched->sched_rq;
146 mutex_lock(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +0200147 tmp = amd_sched_rq_select_entity(rq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800148 mutex_unlock(&rq->lock);
149exit:
150 if (sched->current_entity && (sched->current_entity != tmp))
151 wake_entity = sched->current_entity;
152 sched->current_entity = tmp;
Chunming Zhou1c8f8052015-08-13 13:04:06 +0800153 if (wake_entity && wake_entity->need_wakeup)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800154 wake_up(&wake_entity->wait_queue);
155 return tmp;
156}
157
158/**
159 * Init a context entity used by scheduler when submit to HW ring.
160 *
161 * @sched The pointer to the scheduler
Christian König91404fb2015-08-05 18:33:21 +0200162 * @entity The pointer to a valid amd_sched_entity
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800163 * @rq The run queue this entity belongs
Christian König0e89d0c2015-08-04 16:58:36 +0200164 * @kernel If this is an entity for the kernel
Jammy Zhou1333f722015-07-30 16:36:58 +0800165 * @jobs The max number of jobs in the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800166 *
167 * return 0 if succeed. negative error code on failure
168*/
Christian König91404fb2015-08-05 18:33:21 +0200169int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
Christian König6f0e54a2015-08-05 21:22:10 +0200170 struct amd_sched_entity *entity,
Christian König432a4ff2015-08-12 11:46:04 +0200171 struct amd_sched_rq *rq,
Christian König6f0e54a2015-08-05 21:22:10 +0200172 uint32_t jobs)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800173{
174 uint64_t seq_ring = 0;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800175 char name[20];
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800176
177 if (!(sched && entity && rq))
178 return -EINVAL;
179
Christian König91404fb2015-08-05 18:33:21 +0200180 memset(entity, 0, sizeof(struct amd_sched_entity));
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800181 seq_ring = ((uint64_t)sched->ring_id) << 60;
182 spin_lock_init(&entity->lock);
Christian König91404fb2015-08-05 18:33:21 +0200183 entity->belongto_rq = rq;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800184 entity->scheduler = sched;
185 init_waitqueue_head(&entity->wait_queue);
186 init_waitqueue_head(&entity->wait_emit);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800187 entity->fence_context = fence_context_alloc(1);
188 snprintf(name, sizeof(name), "c_entity[%llu]", entity->fence_context);
189 memcpy(entity->name, name, 20);
Chunming Zhou1c8f8052015-08-13 13:04:06 +0800190 entity->need_wakeup = false;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800191 if(kfifo_alloc(&entity->job_queue,
Jammy Zhou1333f722015-07-30 16:36:58 +0800192 jobs * sizeof(void *),
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800193 GFP_KERNEL))
194 return -EINVAL;
195
196 spin_lock_init(&entity->queue_lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800197 atomic64_set(&entity->last_queued_v_seq, seq_ring);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800198 atomic64_set(&entity->last_signaled_v_seq, seq_ring);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800199
200 /* Add the entity to the run queue */
Christian König432a4ff2015-08-12 11:46:04 +0200201 amd_sched_rq_add_entity(rq, entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800202 return 0;
203}
204
205/**
206 * Query if entity is initialized
207 *
208 * @sched Pointer to scheduler instance
209 * @entity The pointer to a valid scheduler entity
210 *
211 * return true if entity is initialized, false otherwise
212*/
213static bool is_context_entity_initialized(struct amd_gpu_scheduler *sched,
Christian König91404fb2015-08-05 18:33:21 +0200214 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800215{
216 return entity->scheduler == sched &&
Christian König91404fb2015-08-05 18:33:21 +0200217 entity->belongto_rq != NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800218}
219
220static bool is_context_entity_idle(struct amd_gpu_scheduler *sched,
Christian König91404fb2015-08-05 18:33:21 +0200221 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800222{
223 /**
224 * Idle means no pending IBs, and the entity is not
225 * currently being used.
226 */
227 barrier();
228 if ((sched->current_entity != entity) &&
229 kfifo_is_empty(&entity->job_queue))
230 return true;
231
232 return false;
233}
234
235/**
236 * Destroy a context entity
237 *
238 * @sched Pointer to scheduler instance
239 * @entity The pointer to a valid scheduler entity
240 *
241 * return 0 if succeed. negative error code on failure
242 */
Christian König91404fb2015-08-05 18:33:21 +0200243int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
244 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800245{
246 int r = 0;
Christian König432a4ff2015-08-12 11:46:04 +0200247 struct amd_sched_rq *rq = entity->belongto_rq;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800248
249 if (!is_context_entity_initialized(sched, entity))
250 return 0;
Chunming Zhou1c8f8052015-08-13 13:04:06 +0800251 entity->need_wakeup = true;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800252 /**
253 * The client will not queue more IBs during this fini, consume existing
254 * queued IBs
255 */
256 r = wait_event_timeout(
257 entity->wait_queue,
258 is_context_entity_idle(sched, entity),
259 msecs_to_jiffies(AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS)
260 ) ? 0 : -1;
261
262 if (r) {
263 if (entity->is_pending)
Christian König0e89d0c2015-08-04 16:58:36 +0200264 DRM_INFO("Entity %p is in waiting state during fini,\
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800265 all pending ibs will be canceled.\n",
Christian König0e89d0c2015-08-04 16:58:36 +0200266 entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800267 }
268
Christian König432a4ff2015-08-12 11:46:04 +0200269 amd_sched_rq_remove_entity(rq, entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800270 kfifo_free(&entity->job_queue);
271 return r;
272}
273
274/**
275 * Submit a normal job to the job queue
276 *
277 * @sched The pointer to the scheduler
Christian König91404fb2015-08-05 18:33:21 +0200278 * @c_entity The pointer to amd_sched_entity
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800279 * @job The pointer to job required to submit
Chunming Zhou80de5912015-08-05 19:07:08 +0800280 * return 0 if succeed. -1 if failed.
281 * -2 indicate queue is full for this client, client should wait untill
282 * scheduler consum some queued command.
283 * -1 other fail.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800284*/
Chunming Zhou80de5912015-08-05 19:07:08 +0800285int amd_sched_push_job(struct amd_gpu_scheduler *sched,
Christian König91404fb2015-08-05 18:33:21 +0200286 struct amd_sched_entity *c_entity,
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800287 void *data,
288 struct amd_sched_fence **fence)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800289{
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800290 struct amd_sched_job *job;
291
292 if (!fence)
293 return -EINVAL;
294 job = kzalloc(sizeof(struct amd_sched_job), GFP_KERNEL);
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800295 if (!job)
296 return -ENOMEM;
297 job->sched = sched;
298 job->s_entity = c_entity;
299 job->data = data;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800300 *fence = amd_sched_fence_create(c_entity);
301 if ((*fence) == NULL) {
302 kfree(job);
303 return -EINVAL;
304 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800305 fence_get(&(*fence)->base);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800306 job->s_fence = *fence;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800307 while (kfifo_in_spinlocked(&c_entity->job_queue, &job, sizeof(void *),
308 &c_entity->queue_lock) != sizeof(void *)) {
309 /**
310 * Current context used up all its IB slots
311 * wait here, or need to check whether GPU is hung
312 */
313 schedule();
314 }
Chunming Zhou1c8f8052015-08-13 13:04:06 +0800315 /* first job wake up scheduler */
316 if ((kfifo_len(&c_entity->job_queue) / sizeof(void *)) == 1)
317 wake_up_interruptible(&sched->wait_queue);
Chunming Zhou80de5912015-08-05 19:07:08 +0800318 return 0;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800319}
320
Christian König6f0e54a2015-08-05 21:22:10 +0200321static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
322{
323 struct amd_sched_job *sched_job =
324 container_of(cb, struct amd_sched_job, cb);
325 struct amd_gpu_scheduler *sched;
326 unsigned long flags;
327
328 sched = sched_job->sched;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800329 atomic64_set(&sched_job->s_entity->last_signaled_v_seq,
330 sched_job->s_fence->v_seq);
331 amd_sched_fence_signal(sched_job->s_fence);
Christian König6f0e54a2015-08-05 21:22:10 +0200332 spin_lock_irqsave(&sched->queue_lock, flags);
333 list_del(&sched_job->list);
334 atomic64_dec(&sched->hw_rq_count);
335 spin_unlock_irqrestore(&sched->queue_lock, flags);
336
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800337 sched->ops->process_job(sched, sched_job);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800338 fence_put(&sched_job->s_fence->base);
Christian König6f0e54a2015-08-05 21:22:10 +0200339 kfree(sched_job);
340 wake_up_interruptible(&sched->wait_queue);
341}
342
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800343static int amd_sched_main(void *param)
344{
345 int r;
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800346 struct amd_sched_job *job;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800347 struct sched_param sparam = {.sched_priority = 1};
Christian König91404fb2015-08-05 18:33:21 +0200348 struct amd_sched_entity *c_entity = NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800349 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
350
351 sched_setscheduler(current, SCHED_FIFO, &sparam);
352
353 while (!kthread_should_stop()) {
Christian König6f0e54a2015-08-05 21:22:10 +0200354 struct fence *fence;
355
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800356 wait_event_interruptible(sched->wait_queue,
357 is_scheduler_ready(sched) &&
358 (c_entity = select_context(sched)));
359 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
360 if (r != sizeof(void *))
361 continue;
362 r = sched->ops->prepare_job(sched, c_entity, job);
Chunming Zhou4cef9262015-08-05 19:52:14 +0800363 if (!r) {
364 unsigned long flags;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800365 spin_lock_irqsave(&sched->queue_lock, flags);
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800366 list_add_tail(&job->list, &sched->active_hw_rq);
Chunming Zhou4cef9262015-08-05 19:52:14 +0800367 atomic64_inc(&sched->hw_rq_count);
368 spin_unlock_irqrestore(&sched->queue_lock, flags);
369 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800370 mutex_lock(&sched->sched_lock);
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800371 fence = sched->ops->run_job(sched, c_entity, job);
Christian König6f0e54a2015-08-05 21:22:10 +0200372 if (fence) {
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800373 r = fence_add_callback(fence, &job->cb,
Christian König6f0e54a2015-08-05 21:22:10 +0200374 amd_sched_process_job);
375 if (r == -ENOENT)
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800376 amd_sched_process_job(fence, &job->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200377 else if (r)
378 DRM_ERROR("fence add callback failed (%d)\n", r);
379 fence_put(fence);
380 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800381 mutex_unlock(&sched->sched_lock);
382 }
383 return 0;
384}
385
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800386/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800387 * Create a gpu scheduler
388 *
389 * @device The device context for this scheduler
390 * @ops The backend operations for this scheduler.
391 * @id The scheduler is per ring, here is ring id.
392 * @granularity The minumum ms unit the scheduler will scheduled.
393 * @preemption Indicate whether this ring support preemption, 0 is no.
394 *
395 * return the pointer to scheduler for success, otherwise return NULL
396*/
397struct amd_gpu_scheduler *amd_sched_create(void *device,
398 struct amd_sched_backend_ops *ops,
399 unsigned ring,
400 unsigned granularity,
Jammy Zhou4afcb302015-07-30 16:44:05 +0800401 unsigned preemption,
402 unsigned hw_submission)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800403{
404 struct amd_gpu_scheduler *sched;
Christian König4cd7f42c2015-08-05 18:18:52 +0200405 char name[20];
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800406
407 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
408 if (!sched)
409 return NULL;
410
411 sched->device = device;
412 sched->ops = ops;
413 sched->granularity = granularity;
414 sched->ring_id = ring;
415 sched->preemption = preemption;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800416 sched->hw_submission_limit = hw_submission;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800417 snprintf(name, sizeof(name), "gpu_sched[%d]", ring);
418 mutex_init(&sched->sched_lock);
419 spin_lock_init(&sched->queue_lock);
Christian König432a4ff2015-08-12 11:46:04 +0200420 amd_sched_rq_init(&sched->sched_rq);
421 amd_sched_rq_init(&sched->kernel_rq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800422
423 init_waitqueue_head(&sched->wait_queue);
Chunming Zhou4cef9262015-08-05 19:52:14 +0800424 INIT_LIST_HEAD(&sched->active_hw_rq);
425 atomic64_set(&sched->hw_rq_count, 0);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800426 /* Each scheduler will run on a seperate kernel thread */
427 sched->thread = kthread_create(amd_sched_main, sched, name);
428 if (sched->thread) {
429 wake_up_process(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800430 return sched;
431 }
432
433 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800434 kfree(sched);
435 return NULL;
436}
437
438/**
439 * Destroy a gpu scheduler
440 *
441 * @sched The pointer to the scheduler
442 *
443 * return 0 if succeed. -1 if failed.
444 */
445int amd_sched_destroy(struct amd_gpu_scheduler *sched)
446{
447 kthread_stop(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800448 kfree(sched);
449 return 0;
450}
451
Jammy Zhouf95b7e32015-07-31 17:18:15 +0800452/**
Jammy Zhou27f66422015-08-03 10:27:57 +0800453 * Get next queued sequence number
454 *
455 * @entity The context entity
456 *
457 * return the next queued sequence number
458*/
Christian König91404fb2015-08-05 18:33:21 +0200459uint64_t amd_sched_next_queued_seq(struct amd_sched_entity *c_entity)
Jammy Zhou27f66422015-08-03 10:27:57 +0800460{
461 return atomic64_read(&c_entity->last_queued_v_seq) + 1;
462}