blob: 1204b7386b394bc8a53daf0544a2ecf73c08dde0 [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 */
31static void init_rq(struct amd_run_queue *rq)
32{
33 INIT_LIST_HEAD(&rq->head.list);
34 rq->head.belongto_rq = rq;
35 mutex_init(&rq->lock);
36 atomic_set(&rq->nr_entity, 0);
37 rq->current_entity = &rq->head;
38}
39
40/* Note: caller must hold the lock or in a atomic context */
41static void rq_remove_entity(struct amd_run_queue *rq,
42 struct amd_sched_entity *entity)
43{
44 if (rq->current_entity == entity)
45 rq->current_entity = list_entry(entity->list.prev,
46 typeof(*entity), list);
47 list_del_init(&entity->list);
48 atomic_dec(&rq->nr_entity);
49}
50
51static void rq_add_entity(struct amd_run_queue *rq,
52 struct amd_sched_entity *entity)
53{
54 list_add_tail(&entity->list, &rq->head.list);
55 atomic_inc(&rq->nr_entity);
56}
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 */
63static struct amd_sched_entity *rq_select_entity(struct amd_run_queue *rq)
64{
65 struct amd_sched_entity *p = rq->current_entity;
66 int i = atomic_read(&rq->nr_entity) + 1; /*real count + dummy head*/
67 while (i) {
68 p = list_entry(p->list.next, typeof(*p), list);
69 if (!rq->check_entity_status(p)) {
70 rq->current_entity = p;
71 break;
72 }
73 i--;
74 }
75 return i ? p : NULL;
76}
77
78static bool context_entity_is_waiting(struct amd_context_entity *entity)
79{
80 /* TODO: sync obj for multi-ring synchronization */
81 return false;
82}
83
84static int gpu_entity_check_status(struct amd_sched_entity *entity)
85{
86 struct amd_context_entity *tmp = NULL;
87
88 if (entity == &entity->belongto_rq->head)
89 return -1;
90
91 tmp = container_of(entity, typeof(*tmp), generic_entity);
92 if (kfifo_is_empty(&tmp->job_queue) ||
93 context_entity_is_waiting(tmp))
94 return -1;
95
96 return 0;
97}
98
99/**
100 * Note: This function should only been called inside scheduler main
101 * function for thread safety, there is no other protection here.
102 * return ture if scheduler has something ready to run.
103 *
104 * For active_hw_rq, there is only one producer(scheduler thread) and
105 * one consumer(ISR). It should be safe to use this function in scheduler
106 * main thread to decide whether to continue emit more IBs.
107*/
108static bool is_scheduler_ready(struct amd_gpu_scheduler *sched)
109{
110 return !kfifo_is_full(&sched->active_hw_rq);
111}
112
113/**
114 * Select next entity from the kernel run queue, if not available,
115 * return null.
116*/
117static struct amd_context_entity *kernel_rq_select_context(
118 struct amd_gpu_scheduler *sched)
119{
120 struct amd_sched_entity *sched_entity = NULL;
121 struct amd_context_entity *tmp = NULL;
122 struct amd_run_queue *rq = &sched->kernel_rq;
123
124 mutex_lock(&rq->lock);
125 sched_entity = rq_select_entity(rq);
126 if (sched_entity)
127 tmp = container_of(sched_entity,
128 typeof(*tmp),
129 generic_entity);
130 mutex_unlock(&rq->lock);
131 return tmp;
132}
133
134/**
135 * Select next entity containing real IB submissions
136*/
137static struct amd_context_entity *select_context(
138 struct amd_gpu_scheduler *sched)
139{
140 struct amd_context_entity *wake_entity = NULL;
141 struct amd_context_entity *tmp;
142 struct amd_run_queue *rq;
143
144 if (!is_scheduler_ready(sched))
145 return NULL;
146
147 /* Kernel run queue has higher priority than normal run queue*/
148 tmp = kernel_rq_select_context(sched);
149 if (tmp != NULL)
150 goto exit;
151
152 WARN_ON(offsetof(struct amd_context_entity, generic_entity) != 0);
153
154 rq = &sched->sched_rq;
155 mutex_lock(&rq->lock);
156 tmp = container_of(rq_select_entity(rq),
157 typeof(*tmp), generic_entity);
158 mutex_unlock(&rq->lock);
159exit:
160 if (sched->current_entity && (sched->current_entity != tmp))
161 wake_entity = sched->current_entity;
162 sched->current_entity = tmp;
163 if (wake_entity)
164 wake_up(&wake_entity->wait_queue);
165 return tmp;
166}
167
168/**
169 * Init a context entity used by scheduler when submit to HW ring.
170 *
171 * @sched The pointer to the scheduler
172 * @entity The pointer to a valid amd_context_entity
173 * @parent The parent entity of this amd_context_entity
174 * @rq The run queue this entity belongs
Christian König0e89d0c2015-08-04 16:58:36 +0200175 * @kernel If this is an entity for the kernel
Jammy Zhou1333f722015-07-30 16:36:58 +0800176 * @jobs The max number of jobs in the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800177 *
178 * return 0 if succeed. negative error code on failure
179*/
180int amd_context_entity_init(struct amd_gpu_scheduler *sched,
181 struct amd_context_entity *entity,
182 struct amd_sched_entity *parent,
183 struct amd_run_queue *rq,
Jammy Zhou1333f722015-07-30 16:36:58 +0800184 uint32_t jobs)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800185{
186 uint64_t seq_ring = 0;
187
188 if (!(sched && entity && rq))
189 return -EINVAL;
190
191 memset(entity, 0, sizeof(struct amd_context_entity));
192 seq_ring = ((uint64_t)sched->ring_id) << 60;
193 spin_lock_init(&entity->lock);
194 entity->generic_entity.belongto_rq = rq;
195 entity->generic_entity.parent = parent;
196 entity->scheduler = sched;
197 init_waitqueue_head(&entity->wait_queue);
198 init_waitqueue_head(&entity->wait_emit);
199 if(kfifo_alloc(&entity->job_queue,
Jammy Zhou1333f722015-07-30 16:36:58 +0800200 jobs * sizeof(void *),
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800201 GFP_KERNEL))
202 return -EINVAL;
203
204 spin_lock_init(&entity->queue_lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800205 atomic64_set(&entity->last_emitted_v_seq, seq_ring);
206 atomic64_set(&entity->last_queued_v_seq, seq_ring);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800207
208 /* Add the entity to the run queue */
209 mutex_lock(&rq->lock);
210 rq_add_entity(rq, &entity->generic_entity);
211 mutex_unlock(&rq->lock);
212 return 0;
213}
214
215/**
216 * Query if entity is initialized
217 *
218 * @sched Pointer to scheduler instance
219 * @entity The pointer to a valid scheduler entity
220 *
221 * return true if entity is initialized, false otherwise
222*/
223static bool is_context_entity_initialized(struct amd_gpu_scheduler *sched,
224 struct amd_context_entity *entity)
225{
226 return entity->scheduler == sched &&
227 entity->generic_entity.belongto_rq != NULL;
228}
229
230static bool is_context_entity_idle(struct amd_gpu_scheduler *sched,
231 struct amd_context_entity *entity)
232{
233 /**
234 * Idle means no pending IBs, and the entity is not
235 * currently being used.
236 */
237 barrier();
238 if ((sched->current_entity != entity) &&
239 kfifo_is_empty(&entity->job_queue))
240 return true;
241
242 return false;
243}
244
245/**
246 * Destroy a context entity
247 *
248 * @sched Pointer to scheduler instance
249 * @entity The pointer to a valid scheduler entity
250 *
251 * return 0 if succeed. negative error code on failure
252 */
253int amd_context_entity_fini(struct amd_gpu_scheduler *sched,
254 struct amd_context_entity *entity)
255{
256 int r = 0;
257 struct amd_run_queue *rq = entity->generic_entity.belongto_rq;
258
259 if (!is_context_entity_initialized(sched, entity))
260 return 0;
261
262 /**
263 * The client will not queue more IBs during this fini, consume existing
264 * queued IBs
265 */
266 r = wait_event_timeout(
267 entity->wait_queue,
268 is_context_entity_idle(sched, entity),
269 msecs_to_jiffies(AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS)
270 ) ? 0 : -1;
271
272 if (r) {
273 if (entity->is_pending)
Christian König0e89d0c2015-08-04 16:58:36 +0200274 DRM_INFO("Entity %p is in waiting state during fini,\
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800275 all pending ibs will be canceled.\n",
Christian König0e89d0c2015-08-04 16:58:36 +0200276 entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800277 }
278
279 mutex_lock(&rq->lock);
280 rq_remove_entity(rq, &entity->generic_entity);
281 mutex_unlock(&rq->lock);
282 kfifo_free(&entity->job_queue);
283 return r;
284}
285
286/**
287 * Submit a normal job to the job queue
288 *
289 * @sched The pointer to the scheduler
290 * @c_entity The pointer to amd_context_entity
291 * @job The pointer to job required to submit
Chunming Zhou80de5912015-08-05 19:07:08 +0800292 * return 0 if succeed. -1 if failed.
293 * -2 indicate queue is full for this client, client should wait untill
294 * scheduler consum some queued command.
295 * -1 other fail.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800296*/
Chunming Zhou80de5912015-08-05 19:07:08 +0800297int amd_sched_push_job(struct amd_gpu_scheduler *sched,
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800298 struct amd_context_entity *c_entity,
299 void *job)
300{
301 while (kfifo_in_spinlocked(&c_entity->job_queue, &job, sizeof(void *),
302 &c_entity->queue_lock) != sizeof(void *)) {
303 /**
304 * Current context used up all its IB slots
305 * wait here, or need to check whether GPU is hung
306 */
307 schedule();
308 }
309
310 wake_up_interruptible(&sched->wait_queue);
Chunming Zhou80de5912015-08-05 19:07:08 +0800311 return 0;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800312}
313
314/**
Christian König1d7dd222015-07-31 14:31:49 +0200315 * Wait for a virtual sequence number to be emitted.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800316 *
317 * @c_entity The pointer to a valid context entity
318 * @seq The virtual sequence number to wait
319 * @intr Interruptible or not
320 * @timeout Timeout in ms, wait infinitely if <0
321 * @emit wait for emit or signal
322 *
323 * return =0 signaled , <0 failed
324*/
Christian König1d7dd222015-07-31 14:31:49 +0200325int amd_sched_wait_emit(struct amd_context_entity *c_entity,
326 uint64_t seq,
327 bool intr,
328 long timeout)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800329{
Christian König1d7dd222015-07-31 14:31:49 +0200330 atomic64_t *v_seq = &c_entity->last_emitted_v_seq;
331 wait_queue_head_t *wait_queue = &c_entity->wait_emit;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800332
333 if (intr && (timeout < 0)) {
334 wait_event_interruptible(
335 *wait_queue,
336 seq <= atomic64_read(v_seq));
337 return 0;
338 } else if (intr && (timeout >= 0)) {
339 wait_event_interruptible_timeout(
340 *wait_queue,
341 seq <= atomic64_read(v_seq),
342 msecs_to_jiffies(timeout));
343 return (seq <= atomic64_read(v_seq)) ?
344 0 : -1;
345 } else if (!intr && (timeout < 0)) {
346 wait_event(
347 *wait_queue,
348 seq <= atomic64_read(v_seq));
349 return 0;
350 } else if (!intr && (timeout >= 0)) {
351 wait_event_timeout(
352 *wait_queue,
353 seq <= atomic64_read(v_seq),
354 msecs_to_jiffies(timeout));
355 return (seq <= atomic64_read(v_seq)) ?
356 0 : -1;
357 }
358 return 0;
359}
360
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800361static int amd_sched_main(void *param)
362{
363 int r;
364 void *job;
365 struct sched_param sparam = {.sched_priority = 1};
366 struct amd_context_entity *c_entity = NULL;
367 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
368
369 sched_setscheduler(current, SCHED_FIFO, &sparam);
370
371 while (!kthread_should_stop()) {
372 wait_event_interruptible(sched->wait_queue,
373 is_scheduler_ready(sched) &&
374 (c_entity = select_context(sched)));
375 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
376 if (r != sizeof(void *))
377 continue;
378 r = sched->ops->prepare_job(sched, c_entity, job);
379 if (!r)
380 WARN_ON(kfifo_in_spinlocked(
381 &sched->active_hw_rq,
382 &job,
383 sizeof(void *),
384 &sched->queue_lock) != sizeof(void *));
385 mutex_lock(&sched->sched_lock);
386 sched->ops->run_job(sched, c_entity, job);
387 mutex_unlock(&sched->sched_lock);
388 }
389 return 0;
390}
391
392uint64_t amd_sched_get_handled_seq(struct amd_gpu_scheduler *sched)
393{
Jammy Zhou63ad8d52015-07-31 17:54:29 +0800394 return atomic64_read(&sched->last_handled_seq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800395}
396
397/**
398 * ISR to handle EOP inetrrupts
399 *
400 * @sched: gpu scheduler
401 *
402*/
403void amd_sched_isr(struct amd_gpu_scheduler *sched)
404{
405 int r;
406 void *job;
407 r = kfifo_out_spinlocked(&sched->active_hw_rq,
408 &job, sizeof(void *),
409 &sched->queue_lock);
410
411 if (r != sizeof(void *))
412 job = NULL;
413
414 sched->ops->process_job(sched, job);
Jammy Zhou63ad8d52015-07-31 17:54:29 +0800415 atomic64_inc(&sched->last_handled_seq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800416 wake_up_interruptible(&sched->wait_queue);
417}
418
419/**
420 * Create a gpu scheduler
421 *
422 * @device The device context for this scheduler
423 * @ops The backend operations for this scheduler.
424 * @id The scheduler is per ring, here is ring id.
425 * @granularity The minumum ms unit the scheduler will scheduled.
426 * @preemption Indicate whether this ring support preemption, 0 is no.
427 *
428 * return the pointer to scheduler for success, otherwise return NULL
429*/
430struct amd_gpu_scheduler *amd_sched_create(void *device,
431 struct amd_sched_backend_ops *ops,
432 unsigned ring,
433 unsigned granularity,
Jammy Zhou4afcb302015-07-30 16:44:05 +0800434 unsigned preemption,
435 unsigned hw_submission)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800436{
437 struct amd_gpu_scheduler *sched;
438 char name[20] = "gpu_sched[0]";
439
440 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
441 if (!sched)
442 return NULL;
443
444 sched->device = device;
445 sched->ops = ops;
446 sched->granularity = granularity;
447 sched->ring_id = ring;
448 sched->preemption = preemption;
Jammy Zhou63ad8d52015-07-31 17:54:29 +0800449 atomic64_set(&sched->last_handled_seq, 0);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800450
451 snprintf(name, sizeof(name), "gpu_sched[%d]", ring);
452 mutex_init(&sched->sched_lock);
453 spin_lock_init(&sched->queue_lock);
454 init_rq(&sched->sched_rq);
455 sched->sched_rq.check_entity_status = gpu_entity_check_status;
456
457 init_rq(&sched->kernel_rq);
458 sched->kernel_rq.check_entity_status = gpu_entity_check_status;
459
460 init_waitqueue_head(&sched->wait_queue);
461 if(kfifo_alloc(&sched->active_hw_rq,
Jammy Zhou4afcb302015-07-30 16:44:05 +0800462 hw_submission * sizeof(void *),
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800463 GFP_KERNEL)) {
464 kfree(sched);
465 return NULL;
466 }
467
468 /* Each scheduler will run on a seperate kernel thread */
469 sched->thread = kthread_create(amd_sched_main, sched, name);
470 if (sched->thread) {
471 wake_up_process(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800472 return sched;
473 }
474
475 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
476 kfifo_free(&sched->active_hw_rq);
477 kfree(sched);
478 return NULL;
479}
480
481/**
482 * Destroy a gpu scheduler
483 *
484 * @sched The pointer to the scheduler
485 *
486 * return 0 if succeed. -1 if failed.
487 */
488int amd_sched_destroy(struct amd_gpu_scheduler *sched)
489{
490 kthread_stop(sched->thread);
491 kfifo_free(&sched->active_hw_rq);
492 kfree(sched);
493 return 0;
494}
495
Jammy Zhouf95b7e32015-07-31 17:18:15 +0800496/**
497 * Update emitted sequence and wake up the waiters, called by run_job
498 * in driver side
499 *
500 * @entity The context entity
501 * @seq The sequence number for the latest emitted job
502*/
503void amd_sched_emit(struct amd_context_entity *c_entity, uint64_t seq)
504{
505 atomic64_set(&c_entity->last_emitted_v_seq, seq);
506 wake_up_all(&c_entity->wait_emit);
507}
Jammy Zhou27f66422015-08-03 10:27:57 +0800508
509/**
510 * Get next queued sequence number
511 *
512 * @entity The context entity
513 *
514 * return the next queued sequence number
515*/
516uint64_t amd_sched_next_queued_seq(struct amd_context_entity *c_entity)
517{
518 return atomic64_read(&c_entity->last_queued_v_seq) + 1;
519}