blob: b9aa572980d2a2710dd28bf97ab86a5986c2b420 [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
Jammy Zhouea199cc2015-07-31 16:47:28 +0800292 * return the virtual sequence number
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800293*/
Jammy Zhouea199cc2015-07-31 16:47:28 +0800294uint64_t amd_sched_push_job(struct amd_gpu_scheduler *sched,
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800295 struct amd_context_entity *c_entity,
296 void *job)
297{
298 while (kfifo_in_spinlocked(&c_entity->job_queue, &job, sizeof(void *),
299 &c_entity->queue_lock) != sizeof(void *)) {
300 /**
301 * Current context used up all its IB slots
302 * wait here, or need to check whether GPU is hung
303 */
304 schedule();
305 }
306
307 wake_up_interruptible(&sched->wait_queue);
Jammy Zhouea199cc2015-07-31 16:47:28 +0800308
309 return atomic64_inc_return(&c_entity->last_queued_v_seq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800310}
311
312/**
Christian König1d7dd222015-07-31 14:31:49 +0200313 * Wait for a virtual sequence number to be emitted.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800314 *
315 * @c_entity The pointer to a valid context entity
316 * @seq The virtual sequence number to wait
317 * @intr Interruptible or not
318 * @timeout Timeout in ms, wait infinitely if <0
319 * @emit wait for emit or signal
320 *
321 * return =0 signaled , <0 failed
322*/
Christian König1d7dd222015-07-31 14:31:49 +0200323int amd_sched_wait_emit(struct amd_context_entity *c_entity,
324 uint64_t seq,
325 bool intr,
326 long timeout)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800327{
Christian König1d7dd222015-07-31 14:31:49 +0200328 atomic64_t *v_seq = &c_entity->last_emitted_v_seq;
329 wait_queue_head_t *wait_queue = &c_entity->wait_emit;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800330
331 if (intr && (timeout < 0)) {
332 wait_event_interruptible(
333 *wait_queue,
334 seq <= atomic64_read(v_seq));
335 return 0;
336 } else if (intr && (timeout >= 0)) {
337 wait_event_interruptible_timeout(
338 *wait_queue,
339 seq <= atomic64_read(v_seq),
340 msecs_to_jiffies(timeout));
341 return (seq <= atomic64_read(v_seq)) ?
342 0 : -1;
343 } else if (!intr && (timeout < 0)) {
344 wait_event(
345 *wait_queue,
346 seq <= atomic64_read(v_seq));
347 return 0;
348 } else if (!intr && (timeout >= 0)) {
349 wait_event_timeout(
350 *wait_queue,
351 seq <= atomic64_read(v_seq),
352 msecs_to_jiffies(timeout));
353 return (seq <= atomic64_read(v_seq)) ?
354 0 : -1;
355 }
356 return 0;
357}
358
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800359static int amd_sched_main(void *param)
360{
361 int r;
362 void *job;
363 struct sched_param sparam = {.sched_priority = 1};
364 struct amd_context_entity *c_entity = NULL;
365 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
366
367 sched_setscheduler(current, SCHED_FIFO, &sparam);
368
369 while (!kthread_should_stop()) {
370 wait_event_interruptible(sched->wait_queue,
371 is_scheduler_ready(sched) &&
372 (c_entity = select_context(sched)));
373 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
374 if (r != sizeof(void *))
375 continue;
376 r = sched->ops->prepare_job(sched, c_entity, job);
377 if (!r)
378 WARN_ON(kfifo_in_spinlocked(
379 &sched->active_hw_rq,
380 &job,
381 sizeof(void *),
382 &sched->queue_lock) != sizeof(void *));
383 mutex_lock(&sched->sched_lock);
384 sched->ops->run_job(sched, c_entity, job);
385 mutex_unlock(&sched->sched_lock);
386 }
387 return 0;
388}
389
390uint64_t amd_sched_get_handled_seq(struct amd_gpu_scheduler *sched)
391{
Jammy Zhou63ad8d52015-07-31 17:54:29 +0800392 return atomic64_read(&sched->last_handled_seq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800393}
394
395/**
396 * ISR to handle EOP inetrrupts
397 *
398 * @sched: gpu scheduler
399 *
400*/
401void amd_sched_isr(struct amd_gpu_scheduler *sched)
402{
403 int r;
404 void *job;
405 r = kfifo_out_spinlocked(&sched->active_hw_rq,
406 &job, sizeof(void *),
407 &sched->queue_lock);
408
409 if (r != sizeof(void *))
410 job = NULL;
411
412 sched->ops->process_job(sched, job);
Jammy Zhou63ad8d52015-07-31 17:54:29 +0800413 atomic64_inc(&sched->last_handled_seq);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800414 wake_up_interruptible(&sched->wait_queue);
415}
416
417/**
418 * Create a gpu scheduler
419 *
420 * @device The device context for this scheduler
421 * @ops The backend operations for this scheduler.
422 * @id The scheduler is per ring, here is ring id.
423 * @granularity The minumum ms unit the scheduler will scheduled.
424 * @preemption Indicate whether this ring support preemption, 0 is no.
425 *
426 * return the pointer to scheduler for success, otherwise return NULL
427*/
428struct amd_gpu_scheduler *amd_sched_create(void *device,
429 struct amd_sched_backend_ops *ops,
430 unsigned ring,
431 unsigned granularity,
Jammy Zhou4afcb302015-07-30 16:44:05 +0800432 unsigned preemption,
433 unsigned hw_submission)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800434{
435 struct amd_gpu_scheduler *sched;
436 char name[20] = "gpu_sched[0]";
437
438 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
439 if (!sched)
440 return NULL;
441
442 sched->device = device;
443 sched->ops = ops;
444 sched->granularity = granularity;
445 sched->ring_id = ring;
446 sched->preemption = preemption;
Jammy Zhou63ad8d52015-07-31 17:54:29 +0800447 atomic64_set(&sched->last_handled_seq, 0);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800448
449 snprintf(name, sizeof(name), "gpu_sched[%d]", ring);
450 mutex_init(&sched->sched_lock);
451 spin_lock_init(&sched->queue_lock);
452 init_rq(&sched->sched_rq);
453 sched->sched_rq.check_entity_status = gpu_entity_check_status;
454
455 init_rq(&sched->kernel_rq);
456 sched->kernel_rq.check_entity_status = gpu_entity_check_status;
457
458 init_waitqueue_head(&sched->wait_queue);
459 if(kfifo_alloc(&sched->active_hw_rq,
Jammy Zhou4afcb302015-07-30 16:44:05 +0800460 hw_submission * sizeof(void *),
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800461 GFP_KERNEL)) {
462 kfree(sched);
463 return NULL;
464 }
465
466 /* Each scheduler will run on a seperate kernel thread */
467 sched->thread = kthread_create(amd_sched_main, sched, name);
468 if (sched->thread) {
469 wake_up_process(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800470 return sched;
471 }
472
473 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
474 kfifo_free(&sched->active_hw_rq);
475 kfree(sched);
476 return NULL;
477}
478
479/**
480 * Destroy a gpu scheduler
481 *
482 * @sched The pointer to the scheduler
483 *
484 * return 0 if succeed. -1 if failed.
485 */
486int amd_sched_destroy(struct amd_gpu_scheduler *sched)
487{
488 kthread_stop(sched->thread);
489 kfifo_free(&sched->active_hw_rq);
490 kfree(sched);
491 return 0;
492}
493
Jammy Zhouf95b7e32015-07-31 17:18:15 +0800494/**
495 * Update emitted sequence and wake up the waiters, called by run_job
496 * in driver side
497 *
498 * @entity The context entity
499 * @seq The sequence number for the latest emitted job
500*/
501void amd_sched_emit(struct amd_context_entity *c_entity, uint64_t seq)
502{
503 atomic64_set(&c_entity->last_emitted_v_seq, seq);
504 wake_up_all(&c_entity->wait_emit);
505}
Jammy Zhou27f66422015-08-03 10:27:57 +0800506
507/**
508 * Get next queued sequence number
509 *
510 * @entity The context entity
511 *
512 * return the next queued sequence number
513*/
514uint64_t amd_sched_next_queued_seq(struct amd_context_entity *c_entity)
515{
516 return atomic64_read(&c_entity->last_queued_v_seq) + 1;
517}