blob: 462c1617d56ede1d5feeaad54b12c3009ba3867a [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 Zhoubb977d32015-08-18 15:16:40 +0800285int amd_sched_push_job(struct amd_sched_job *sched_job)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800286{
Chunming Zhoubb977d32015-08-18 15:16:40 +0800287 struct amd_sched_fence *fence =
288 amd_sched_fence_create(sched_job->s_entity);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800289 if (!fence)
290 return -EINVAL;
Chunming Zhoubb977d32015-08-18 15:16:40 +0800291 fence_get(&fence->base);
292 sched_job->s_fence = fence;
293 while (kfifo_in_spinlocked(&sched_job->s_entity->job_queue,
294 &sched_job, sizeof(void *),
295 &sched_job->s_entity->queue_lock) !=
296 sizeof(void *)) {
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800297 /**
298 * Current context used up all its IB slots
299 * wait here, or need to check whether GPU is hung
300 */
301 schedule();
302 }
Chunming Zhou1c8f8052015-08-13 13:04:06 +0800303 /* first job wake up scheduler */
Chunming Zhoubb977d32015-08-18 15:16:40 +0800304 if ((kfifo_len(&sched_job->s_entity->job_queue) / sizeof(void *)) == 1)
305 wake_up_interruptible(&sched_job->sched->wait_queue);
Chunming Zhou80de5912015-08-05 19:07:08 +0800306 return 0;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800307}
308
Christian König6f0e54a2015-08-05 21:22:10 +0200309static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
310{
311 struct amd_sched_job *sched_job =
312 container_of(cb, struct amd_sched_job, cb);
313 struct amd_gpu_scheduler *sched;
314 unsigned long flags;
315
316 sched = sched_job->sched;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800317 atomic64_set(&sched_job->s_entity->last_signaled_v_seq,
318 sched_job->s_fence->v_seq);
319 amd_sched_fence_signal(sched_job->s_fence);
Christian König6f0e54a2015-08-05 21:22:10 +0200320 spin_lock_irqsave(&sched->queue_lock, flags);
321 list_del(&sched_job->list);
322 atomic64_dec(&sched->hw_rq_count);
323 spin_unlock_irqrestore(&sched->queue_lock, flags);
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800324 fence_put(&sched_job->s_fence->base);
Chunming Zhoubb977d32015-08-18 15:16:40 +0800325 sched->ops->process_job(sched, sched_job);
Christian König6f0e54a2015-08-05 21:22:10 +0200326 wake_up_interruptible(&sched->wait_queue);
327}
328
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800329static int amd_sched_main(void *param)
330{
331 int r;
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800332 struct amd_sched_job *job;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800333 struct sched_param sparam = {.sched_priority = 1};
Christian König91404fb2015-08-05 18:33:21 +0200334 struct amd_sched_entity *c_entity = NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800335 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
336
337 sched_setscheduler(current, SCHED_FIFO, &sparam);
338
339 while (!kthread_should_stop()) {
Christian König6f0e54a2015-08-05 21:22:10 +0200340 struct fence *fence;
341
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800342 wait_event_interruptible(sched->wait_queue,
343 is_scheduler_ready(sched) &&
344 (c_entity = select_context(sched)));
345 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
346 if (r != sizeof(void *))
347 continue;
Chunming Zhoubb977d32015-08-18 15:16:40 +0800348 r = 0;
349 if (sched->ops->prepare_job)
350 r = sched->ops->prepare_job(sched, c_entity, job);
Chunming Zhou4cef9262015-08-05 19:52:14 +0800351 if (!r) {
352 unsigned long flags;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800353 spin_lock_irqsave(&sched->queue_lock, flags);
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800354 list_add_tail(&job->list, &sched->active_hw_rq);
Chunming Zhou4cef9262015-08-05 19:52:14 +0800355 atomic64_inc(&sched->hw_rq_count);
356 spin_unlock_irqrestore(&sched->queue_lock, flags);
357 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800358 mutex_lock(&sched->sched_lock);
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800359 fence = sched->ops->run_job(sched, c_entity, job);
Christian König6f0e54a2015-08-05 21:22:10 +0200360 if (fence) {
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800361 r = fence_add_callback(fence, &job->cb,
Christian König6f0e54a2015-08-05 21:22:10 +0200362 amd_sched_process_job);
363 if (r == -ENOENT)
Chunming Zhou953e8fd2015-08-06 15:19:12 +0800364 amd_sched_process_job(fence, &job->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200365 else if (r)
366 DRM_ERROR("fence add callback failed (%d)\n", r);
367 fence_put(fence);
368 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800369 mutex_unlock(&sched->sched_lock);
370 }
371 return 0;
372}
373
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800374/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800375 * Create a gpu scheduler
376 *
377 * @device The device context for this scheduler
378 * @ops The backend operations for this scheduler.
379 * @id The scheduler is per ring, here is ring id.
380 * @granularity The minumum ms unit the scheduler will scheduled.
381 * @preemption Indicate whether this ring support preemption, 0 is no.
382 *
383 * return the pointer to scheduler for success, otherwise return NULL
384*/
385struct amd_gpu_scheduler *amd_sched_create(void *device,
386 struct amd_sched_backend_ops *ops,
387 unsigned ring,
388 unsigned granularity,
Jammy Zhou4afcb302015-07-30 16:44:05 +0800389 unsigned preemption,
390 unsigned hw_submission)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800391{
392 struct amd_gpu_scheduler *sched;
Christian König4cd7f42c2015-08-05 18:18:52 +0200393 char name[20];
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800394
395 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
396 if (!sched)
397 return NULL;
398
399 sched->device = device;
400 sched->ops = ops;
401 sched->granularity = granularity;
402 sched->ring_id = ring;
403 sched->preemption = preemption;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800404 sched->hw_submission_limit = hw_submission;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800405 snprintf(name, sizeof(name), "gpu_sched[%d]", ring);
406 mutex_init(&sched->sched_lock);
407 spin_lock_init(&sched->queue_lock);
Christian König432a4ff2015-08-12 11:46:04 +0200408 amd_sched_rq_init(&sched->sched_rq);
409 amd_sched_rq_init(&sched->kernel_rq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800410
411 init_waitqueue_head(&sched->wait_queue);
Chunming Zhou4cef9262015-08-05 19:52:14 +0800412 INIT_LIST_HEAD(&sched->active_hw_rq);
413 atomic64_set(&sched->hw_rq_count, 0);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800414 /* Each scheduler will run on a seperate kernel thread */
415 sched->thread = kthread_create(amd_sched_main, sched, name);
416 if (sched->thread) {
417 wake_up_process(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800418 return sched;
419 }
420
421 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800422 kfree(sched);
423 return NULL;
424}
425
426/**
427 * Destroy a gpu scheduler
428 *
429 * @sched The pointer to the scheduler
430 *
431 * return 0 if succeed. -1 if failed.
432 */
433int amd_sched_destroy(struct amd_gpu_scheduler *sched)
434{
435 kthread_stop(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800436 kfree(sched);
437 return 0;
438}
439
Jammy Zhouf95b7e32015-07-31 17:18:15 +0800440/**
Jammy Zhou27f66422015-08-03 10:27:57 +0800441 * Get next queued sequence number
442 *
443 * @entity The context entity
444 *
445 * return the next queued sequence number
446*/
Christian König91404fb2015-08-05 18:33:21 +0200447uint64_t amd_sched_next_queued_seq(struct amd_sched_entity *c_entity)
Jammy Zhou27f66422015-08-03 10:27:57 +0800448{
449 return atomic64_read(&c_entity->last_queued_v_seq) + 1;
450}