blob: 08e1332d814a2f090885f46c433f8446a3b87f0a [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>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010027#include <uapi/linux/sched/types.h>
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080028#include <drm/drmP.h>
29#include "gpu_scheduler.h"
30
Chunming Zhou353da3c2015-09-07 16:06:53 +080031#define CREATE_TRACE_POINTS
32#include "gpu_sched_trace.h"
33
Christian König3d651932015-11-12 21:10:35 +010034static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity);
Christian König88079002015-08-24 14:29:40 +020035static void amd_sched_wakeup(struct amd_gpu_scheduler *sched);
Chris Wilsonf54d1862016-10-25 13:00:45 +010036static void amd_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
Christian König88079002015-08-24 14:29:40 +020037
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080038/* Initialize a given run queue struct */
Christian König432a4ff2015-08-12 11:46:04 +020039static void amd_sched_rq_init(struct amd_sched_rq *rq)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080040{
Christian König2b184d82015-08-18 14:41:25 +020041 spin_lock_init(&rq->lock);
Christian König432a4ff2015-08-12 11:46:04 +020042 INIT_LIST_HEAD(&rq->entities);
Christian König432a4ff2015-08-12 11:46:04 +020043 rq->current_entity = NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080044}
45
Christian König432a4ff2015-08-12 11:46:04 +020046static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
47 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080048{
Chunming Zhoue8deea22015-12-11 18:22:52 +080049 if (!list_empty(&entity->list))
50 return;
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{
Chunming Zhoue8deea22015-12-11 18:22:52 +080059 if (list_empty(&entity->list))
60 return;
Christian König2b184d82015-08-18 14:41:25 +020061 spin_lock(&rq->lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080062 list_del_init(&entity->list);
Christian König432a4ff2015-08-12 11:46:04 +020063 if (rq->current_entity == entity)
64 rq->current_entity = NULL;
Christian König2b184d82015-08-18 14:41:25 +020065 spin_unlock(&rq->lock);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080066}
67
68/**
Christian König3d651932015-11-12 21:10:35 +010069 * Select an entity which could provide a job to run
70 *
71 * @rq The run queue to check.
72 *
73 * Try to find a ready entity, returns NULL if none found.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080074 */
Christian König3d651932015-11-12 21:10:35 +010075static struct amd_sched_entity *
76amd_sched_rq_select_entity(struct amd_sched_rq *rq)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080077{
Christian König2b184d82015-08-18 14:41:25 +020078 struct amd_sched_entity *entity;
Christian König4cd7f42c2015-08-05 18:18:52 +020079
Christian König2b184d82015-08-18 14:41:25 +020080 spin_lock(&rq->lock);
81
82 entity = rq->current_entity;
Christian König432a4ff2015-08-12 11:46:04 +020083 if (entity) {
84 list_for_each_entry_continue(entity, &rq->entities, list) {
Christian König3d651932015-11-12 21:10:35 +010085 if (amd_sched_entity_is_ready(entity)) {
Christian König432a4ff2015-08-12 11:46:04 +020086 rq->current_entity = entity;
Christian König2b184d82015-08-18 14:41:25 +020087 spin_unlock(&rq->lock);
Christian König3d651932015-11-12 21:10:35 +010088 return entity;
Christian König432a4ff2015-08-12 11:46:04 +020089 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080090 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080091 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080092
Christian König432a4ff2015-08-12 11:46:04 +020093 list_for_each_entry(entity, &rq->entities, list) {
Jammy Zhoua72ce6f2015-05-22 18:55:07 +080094
Christian König3d651932015-11-12 21:10:35 +010095 if (amd_sched_entity_is_ready(entity)) {
Christian König432a4ff2015-08-12 11:46:04 +020096 rq->current_entity = entity;
Christian König2b184d82015-08-18 14:41:25 +020097 spin_unlock(&rq->lock);
Christian König3d651932015-11-12 21:10:35 +010098 return entity;
Christian König432a4ff2015-08-12 11:46:04 +020099 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800100
Christian König432a4ff2015-08-12 11:46:04 +0200101 if (entity == rq->current_entity)
102 break;
103 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800104
Christian König2b184d82015-08-18 14:41:25 +0200105 spin_unlock(&rq->lock);
106
Christian König432a4ff2015-08-12 11:46:04 +0200107 return NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800108}
109
110/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800111 * Init a context entity used by scheduler when submit to HW ring.
112 *
113 * @sched The pointer to the scheduler
Christian König91404fb2015-08-05 18:33:21 +0200114 * @entity The pointer to a valid amd_sched_entity
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800115 * @rq The run queue this entity belongs
Christian König0e89d0c2015-08-04 16:58:36 +0200116 * @kernel If this is an entity for the kernel
Jammy Zhou1333f722015-07-30 16:36:58 +0800117 * @jobs The max number of jobs in the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800118 *
119 * return 0 if succeed. negative error code on failure
120*/
Christian König91404fb2015-08-05 18:33:21 +0200121int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
Christian König6f0e54a2015-08-05 21:22:10 +0200122 struct amd_sched_entity *entity,
Christian König432a4ff2015-08-12 11:46:04 +0200123 struct amd_sched_rq *rq,
Christian König6f0e54a2015-08-05 21:22:10 +0200124 uint32_t jobs)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800125{
Christian König0f75aee2015-09-07 18:07:14 +0200126 int r;
127
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800128 if (!(sched && entity && rq))
129 return -EINVAL;
130
Christian König91404fb2015-08-05 18:33:21 +0200131 memset(entity, 0, sizeof(struct amd_sched_entity));
Christian König0f75aee2015-09-07 18:07:14 +0200132 INIT_LIST_HEAD(&entity->list);
133 entity->rq = rq;
134 entity->sched = sched;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800135
136 spin_lock_init(&entity->queue_lock);
Christian König0f75aee2015-09-07 18:07:14 +0200137 r = kfifo_alloc(&entity->job_queue, jobs * sizeof(void *), GFP_KERNEL);
138 if (r)
139 return r;
140
Christian Königce882e62015-08-19 15:00:55 +0200141 atomic_set(&entity->fence_seq, 0);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100142 entity->fence_context = dma_fence_context_alloc(2);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800143
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800144 return 0;
145}
146
147/**
148 * Query if entity is initialized
149 *
150 * @sched Pointer to scheduler instance
151 * @entity The pointer to a valid scheduler entity
152 *
153 * return true if entity is initialized, false otherwise
154*/
Christian Königd54fdb92015-08-20 17:03:48 +0200155static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
156 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800157{
Christian König0f75aee2015-09-07 18:07:14 +0200158 return entity->sched == sched &&
159 entity->rq != NULL;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800160}
161
Christian Königaef48522015-08-20 14:47:46 +0200162/**
163 * Check if entity is idle
164 *
165 * @entity The pointer to a valid scheduler entity
166 *
167 * Return true if entity don't has any unscheduled jobs.
168 */
169static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800170{
Christian Königaef48522015-08-20 14:47:46 +0200171 rmb();
172 if (kfifo_is_empty(&entity->job_queue))
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800173 return true;
174
175 return false;
176}
177
178/**
Christian König3d651932015-11-12 21:10:35 +0100179 * Check if entity is ready
180 *
181 * @entity The pointer to a valid scheduler entity
182 *
183 * Return true if entity could provide a job.
184 */
185static bool amd_sched_entity_is_ready(struct amd_sched_entity *entity)
186{
187 if (kfifo_is_empty(&entity->job_queue))
188 return false;
189
190 if (ACCESS_ONCE(entity->dependency))
191 return false;
192
193 return true;
194}
195
196/**
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800197 * Destroy a context entity
198 *
199 * @sched Pointer to scheduler instance
200 * @entity The pointer to a valid scheduler entity
201 *
Christian König062c7fb2015-08-21 15:46:43 +0200202 * Cleanup and free the allocated resources.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800203 */
Christian König062c7fb2015-08-21 15:46:43 +0200204void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
205 struct amd_sched_entity *entity)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800206{
Christian König0f75aee2015-09-07 18:07:14 +0200207 struct amd_sched_rq *rq = entity->rq;
Christian Königf0694d32017-08-21 14:27:51 +0200208 int r;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800209
Christian Königd54fdb92015-08-20 17:03:48 +0200210 if (!amd_sched_entity_is_initialized(sched, entity))
Christian König062c7fb2015-08-21 15:46:43 +0200211 return;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800212 /**
213 * The client will not queue more IBs during this fini, consume existing
Christian Königf0694d32017-08-21 14:27:51 +0200214 * queued IBs or discard them on SIGKILL
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800215 */
Christian Königf0694d32017-08-21 14:27:51 +0200216 if ((current->flags & PF_SIGNALED) && current->exit_code == SIGKILL)
217 r = -ERESTARTSYS;
218 else
219 r = wait_event_killable(sched->job_scheduled,
220 amd_sched_entity_is_idle(entity));
Christian König432a4ff2015-08-12 11:46:04 +0200221 amd_sched_rq_remove_entity(rq, entity);
Christian Königf0694d32017-08-21 14:27:51 +0200222 if (r) {
223 struct amd_sched_job *job;
224
225 /* Park the kernel for a moment to make sure it isn't processing
226 * our enity.
227 */
228 kthread_park(sched->thread);
229 kthread_unpark(sched->thread);
Nicolai Hähnle79867462017-09-28 11:57:32 +0200230 while (kfifo_out(&entity->job_queue, &job, sizeof(job))) {
231 struct amd_sched_fence *s_fence = job->s_fence;
232 amd_sched_fence_scheduled(s_fence);
233 dma_fence_set_error(&s_fence->finished, -ESRCH);
234 amd_sched_fence_finished(s_fence);
235 dma_fence_put(&s_fence->finished);
Christian Königf0694d32017-08-21 14:27:51 +0200236 sched->ops->free_job(job);
Nicolai Hähnle79867462017-09-28 11:57:32 +0200237 }
Christian Königf0694d32017-08-21 14:27:51 +0200238
239 }
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800240 kfifo_free(&entity->job_queue);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800241}
242
Chris Wilsonf54d1862016-10-25 13:00:45 +0100243static void amd_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
Christian Könige61235d2015-08-25 11:05:36 +0200244{
245 struct amd_sched_entity *entity =
246 container_of(cb, struct amd_sched_entity, cb);
247 entity->dependency = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100248 dma_fence_put(f);
Christian König0f75aee2015-09-07 18:07:14 +0200249 amd_sched_wakeup(entity->sched);
Christian Könige61235d2015-08-25 11:05:36 +0200250}
251
Chris Wilsonf54d1862016-10-25 13:00:45 +0100252static void amd_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
Monk Liu777dbd42016-01-26 14:59:57 +0800253{
254 struct amd_sched_entity *entity =
255 container_of(cb, struct amd_sched_entity, cb);
256 entity->dependency = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100257 dma_fence_put(f);
Monk Liu777dbd42016-01-26 14:59:57 +0800258}
259
Chunming Zhou30514de2017-05-09 13:39:40 +0800260bool amd_sched_dependency_optimized(struct dma_fence* fence,
261 struct amd_sched_entity *entity)
262{
263 struct amd_gpu_scheduler *sched = entity->sched;
264 struct amd_sched_fence *s_fence;
265
266 if (!fence || dma_fence_is_signaled(fence))
267 return false;
268 if (fence->context == entity->fence_context)
269 return true;
270 s_fence = to_amd_sched_fence(fence);
271 if (s_fence && s_fence->sched == sched)
272 return true;
273
274 return false;
275}
276
Christian König393a0bd2015-11-05 12:57:10 +0100277static bool amd_sched_entity_add_dependency_cb(struct amd_sched_entity *entity)
278{
279 struct amd_gpu_scheduler *sched = entity->sched;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100280 struct dma_fence * fence = entity->dependency;
Christian König393a0bd2015-11-05 12:57:10 +0100281 struct amd_sched_fence *s_fence;
282
283 if (fence->context == entity->fence_context) {
284 /* We can ignore fences from ourself */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100285 dma_fence_put(entity->dependency);
Christian König393a0bd2015-11-05 12:57:10 +0100286 return false;
287 }
288
289 s_fence = to_amd_sched_fence(fence);
290 if (s_fence && s_fence->sched == sched) {
Christian König393a0bd2015-11-05 12:57:10 +0100291
Christian König6fc13672016-05-20 12:53:52 +0200292 /*
293 * Fence is from the same scheduler, only need to wait for
294 * it to be scheduled
295 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100296 fence = dma_fence_get(&s_fence->scheduled);
297 dma_fence_put(entity->dependency);
Christian König6fc13672016-05-20 12:53:52 +0200298 entity->dependency = fence;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100299 if (!dma_fence_add_callback(fence, &entity->cb,
300 amd_sched_entity_clear_dep))
Christian König6fc13672016-05-20 12:53:52 +0200301 return true;
302
303 /* Ignore it when it is already scheduled */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100304 dma_fence_put(fence);
Christian König6fc13672016-05-20 12:53:52 +0200305 return false;
Christian König393a0bd2015-11-05 12:57:10 +0100306 }
307
Chris Wilsonf54d1862016-10-25 13:00:45 +0100308 if (!dma_fence_add_callback(entity->dependency, &entity->cb,
309 amd_sched_entity_wakeup))
Christian König393a0bd2015-11-05 12:57:10 +0100310 return true;
311
Chris Wilsonf54d1862016-10-25 13:00:45 +0100312 dma_fence_put(entity->dependency);
Christian König393a0bd2015-11-05 12:57:10 +0100313 return false;
314}
315
Christian König69bd5bf2015-08-26 11:31:23 +0200316static struct amd_sched_job *
Nicolai Hähnle515c6fa2017-09-28 11:21:15 +0200317amd_sched_entity_peek_job(struct amd_sched_entity *entity)
Christian König69bd5bf2015-08-26 11:31:23 +0200318{
Christian König0f75aee2015-09-07 18:07:14 +0200319 struct amd_gpu_scheduler *sched = entity->sched;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800320 struct amd_sched_job *sched_job;
Christian König69bd5bf2015-08-26 11:31:23 +0200321
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800322 if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job)))
Christian König69bd5bf2015-08-26 11:31:23 +0200323 return NULL;
324
Christian König393a0bd2015-11-05 12:57:10 +0100325 while ((entity->dependency = sched->ops->dependency(sched_job)))
326 if (amd_sched_entity_add_dependency_cb(entity))
Christian Könige61235d2015-08-25 11:05:36 +0200327 return NULL;
Christian Könige61235d2015-08-25 11:05:36 +0200328
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800329 return sched_job;
Christian König69bd5bf2015-08-26 11:31:23 +0200330}
331
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800332/**
Christian König6c859272015-08-20 16:12:50 +0200333 * Helper to submit a job to the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800334 *
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800335 * @sched_job The pointer to job required to submit
Christian König6c859272015-08-20 16:12:50 +0200336 *
337 * Returns true if we could submit the job.
338 */
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800339static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800340{
Nicolai Hähnle786b5212015-12-02 17:35:12 +0100341 struct amd_gpu_scheduler *sched = sched_job->sched;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800342 struct amd_sched_entity *entity = sched_job->s_entity;
Christian König6c859272015-08-20 16:12:50 +0200343 bool added, first = false;
344
345 spin_lock(&entity->queue_lock);
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800346 added = kfifo_in(&entity->job_queue, &sched_job,
347 sizeof(sched_job)) == sizeof(sched_job);
Christian König6c859272015-08-20 16:12:50 +0200348
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800349 if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
Christian König6c859272015-08-20 16:12:50 +0200350 first = true;
351
352 spin_unlock(&entity->queue_lock);
353
354 /* first job wakes up scheduler */
Chunming Zhoue8deea22015-12-11 18:22:52 +0800355 if (first) {
356 /* Add the entity to the run queue */
357 amd_sched_rq_add_entity(entity->rq, entity);
Nicolai Hähnle786b5212015-12-02 17:35:12 +0100358 amd_sched_wakeup(sched);
Chunming Zhoue8deea22015-12-11 18:22:52 +0800359 }
Christian König6c859272015-08-20 16:12:50 +0200360 return added;
361}
362
Nicolai Hähnle1650c142017-09-28 11:35:05 +0200363/* job_finish is called after hw fence signaled
Monk Liu0de24792016-03-04 18:51:02 +0800364 */
Christian Königc5f74f72016-05-19 09:54:15 +0200365static void amd_sched_job_finish(struct work_struct *work)
Monk Liu0de24792016-03-04 18:51:02 +0800366{
Christian Königc5f74f72016-05-19 09:54:15 +0200367 struct amd_sched_job *s_job = container_of(work, struct amd_sched_job,
368 finish_work);
Monk Liu0de24792016-03-04 18:51:02 +0800369 struct amd_gpu_scheduler *sched = s_job->sched;
370
Christian Königf42d20a92016-05-18 15:40:58 +0200371 /* remove job from ring_mirror_list */
Christian König1059e112016-06-13 16:12:43 +0200372 spin_lock(&sched->job_list_lock);
Christian Königf42d20a92016-05-18 15:40:58 +0200373 list_del_init(&s_job->node);
Monk Liu0de24792016-03-04 18:51:02 +0800374 if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
Christian Königc5f74f72016-05-19 09:54:15 +0200375 struct amd_sched_job *next;
376
Christian König1059e112016-06-13 16:12:43 +0200377 spin_unlock(&sched->job_list_lock);
Christian Königc5f74f72016-05-19 09:54:15 +0200378 cancel_delayed_work_sync(&s_job->work_tdr);
Christian König1059e112016-06-13 16:12:43 +0200379 spin_lock(&sched->job_list_lock);
Monk Liu0de24792016-03-04 18:51:02 +0800380
381 /* queue TDR for next job */
382 next = list_first_entry_or_null(&sched->ring_mirror_list,
383 struct amd_sched_job, node);
384
Christian Königc5f74f72016-05-19 09:54:15 +0200385 if (next)
Monk Liu0de24792016-03-04 18:51:02 +0800386 schedule_delayed_work(&next->work_tdr, sched->timeout);
Monk Liu0de24792016-03-04 18:51:02 +0800387 }
Christian König1059e112016-06-13 16:12:43 +0200388 spin_unlock(&sched->job_list_lock);
Christian Königc5f74f72016-05-19 09:54:15 +0200389 sched->ops->free_job(s_job);
390}
391
Chris Wilsonf54d1862016-10-25 13:00:45 +0100392static void amd_sched_job_finish_cb(struct dma_fence *f,
393 struct dma_fence_cb *cb)
Christian Königc5f74f72016-05-19 09:54:15 +0200394{
395 struct amd_sched_job *job = container_of(cb, struct amd_sched_job,
396 finish_cb);
397 schedule_work(&job->finish_work);
Monk Liu0de24792016-03-04 18:51:02 +0800398}
399
Christian König7392c322016-05-18 13:00:38 +0200400static void amd_sched_job_begin(struct amd_sched_job *s_job)
Monk Liu0de24792016-03-04 18:51:02 +0800401{
402 struct amd_gpu_scheduler *sched = s_job->sched;
403
Nicolai Hähnle214a91e2017-09-28 11:37:02 +0200404 dma_fence_add_callback(&s_job->s_fence->finished, &s_job->finish_cb,
405 amd_sched_job_finish_cb);
406
Christian König1059e112016-06-13 16:12:43 +0200407 spin_lock(&sched->job_list_lock);
Christian Königf42d20a92016-05-18 15:40:58 +0200408 list_add_tail(&s_job->node, &sched->ring_mirror_list);
Monk Liu0de24792016-03-04 18:51:02 +0800409 if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
Christian König16a71332016-05-18 09:43:07 +0200410 list_first_entry_or_null(&sched->ring_mirror_list,
411 struct amd_sched_job, node) == s_job)
Monk Liu0de24792016-03-04 18:51:02 +0800412 schedule_delayed_work(&s_job->work_tdr, sched->timeout);
Christian König1059e112016-06-13 16:12:43 +0200413 spin_unlock(&sched->job_list_lock);
Monk Liu0de24792016-03-04 18:51:02 +0800414}
415
Christian König0e51a772016-05-18 14:19:32 +0200416static void amd_sched_job_timedout(struct work_struct *work)
417{
418 struct amd_sched_job *job = container_of(work, struct amd_sched_job,
419 work_tdr.work);
420
421 job->sched->ops->timedout_job(job);
422}
423
Chunming Zhoue686e752016-06-30 11:30:37 +0800424void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched)
425{
426 struct amd_sched_job *s_job;
427
428 spin_lock(&sched->job_list_lock);
429 list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
Chunming Zhoua6bef672017-04-24 17:39:00 +0800430 if (s_job->s_fence->parent &&
431 dma_fence_remove_callback(s_job->s_fence->parent,
432 &s_job->s_fence->cb)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100433 dma_fence_put(s_job->s_fence->parent);
Chunming Zhoue686e752016-06-30 11:30:37 +0800434 s_job->s_fence->parent = NULL;
Monk Liu65781c72017-05-11 13:36:44 +0800435 atomic_dec(&sched->hw_rq_count);
Chunming Zhoue686e752016-06-30 11:30:37 +0800436 }
437 }
Monk Liu65781c72017-05-11 13:36:44 +0800438 spin_unlock(&sched->job_list_lock);
439}
440
441void amd_sched_job_kickout(struct amd_sched_job *s_job)
442{
443 struct amd_gpu_scheduler *sched = s_job->sched;
444
445 spin_lock(&sched->job_list_lock);
446 list_del_init(&s_job->node);
Chunming Zhoue686e752016-06-30 11:30:37 +0800447 spin_unlock(&sched->job_list_lock);
448}
449
Chunming Zhouec75f572016-06-29 15:23:55 +0800450void amd_sched_job_recovery(struct amd_gpu_scheduler *sched)
451{
Chunming Zhou1c62cf92016-07-25 13:55:35 +0800452 struct amd_sched_job *s_job, *tmp;
Chunming Zhouec75f572016-06-29 15:23:55 +0800453 int r;
454
455 spin_lock(&sched->job_list_lock);
456 s_job = list_first_entry_or_null(&sched->ring_mirror_list,
457 struct amd_sched_job, node);
Christian Königbdf00132016-08-16 19:52:35 +0200458 if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT)
Chunming Zhouec75f572016-06-29 15:23:55 +0800459 schedule_delayed_work(&s_job->work_tdr, sched->timeout);
460
Chunming Zhou1c62cf92016-07-25 13:55:35 +0800461 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
Chunming Zhouec75f572016-06-29 15:23:55 +0800462 struct amd_sched_fence *s_fence = s_job->s_fence;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100463 struct dma_fence *fence;
Chunming Zhoubdc2eea2016-07-22 13:01:02 +0800464
Chunming Zhou1c62cf92016-07-25 13:55:35 +0800465 spin_unlock(&sched->job_list_lock);
466 fence = sched->ops->run_job(s_job);
Chunming Zhoubdc2eea2016-07-22 13:01:02 +0800467 atomic_inc(&sched->hw_rq_count);
Chunming Zhouec75f572016-06-29 15:23:55 +0800468 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100469 s_fence->parent = dma_fence_get(fence);
470 r = dma_fence_add_callback(fence, &s_fence->cb,
471 amd_sched_process_job);
Chunming Zhouec75f572016-06-29 15:23:55 +0800472 if (r == -ENOENT)
473 amd_sched_process_job(fence, &s_fence->cb);
474 else if (r)
475 DRM_ERROR("fence add callback failed (%d)\n",
476 r);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100477 dma_fence_put(fence);
Chunming Zhouec75f572016-06-29 15:23:55 +0800478 } else {
479 DRM_ERROR("Failed to run job!\n");
480 amd_sched_process_job(NULL, &s_fence->cb);
481 }
Chunming Zhou1c62cf92016-07-25 13:55:35 +0800482 spin_lock(&sched->job_list_lock);
Chunming Zhouec75f572016-06-29 15:23:55 +0800483 }
484 spin_unlock(&sched->job_list_lock);
485}
486
Christian König6c859272015-08-20 16:12:50 +0200487/**
488 * Submit a job to the job queue
489 *
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800490 * @sched_job The pointer to job required to submit
Christian König6c859272015-08-20 16:12:50 +0200491 *
492 * Returns 0 for success, negative error code otherwise.
493 */
Christian Könige2840222015-11-05 19:49:48 +0100494void amd_sched_entity_push_job(struct amd_sched_job *sched_job)
Christian König6c859272015-08-20 16:12:50 +0200495{
496 struct amd_sched_entity *entity = sched_job->s_entity;
Christian König6c859272015-08-20 16:12:50 +0200497
Nicolai Hähnle786b5212015-12-02 17:35:12 +0100498 trace_amd_sched_job(sched_job);
Christian König0f75aee2015-09-07 18:07:14 +0200499 wait_event(entity->sched->job_scheduled,
Chunming Zhouc9f0fe52015-08-31 15:46:12 +0800500 amd_sched_entity_in(sched_job));
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800501}
502
Monk Liue6869412016-03-07 12:49:55 +0800503/* init a sched_job with basic field */
504int amd_sched_job_init(struct amd_sched_job *job,
Christian König16a71332016-05-18 09:43:07 +0200505 struct amd_gpu_scheduler *sched,
506 struct amd_sched_entity *entity,
Christian König595a9cd2016-06-30 10:52:03 +0200507 void *owner)
Monk Liue6869412016-03-07 12:49:55 +0800508{
509 job->sched = sched;
510 job->s_entity = entity;
511 job->s_fence = amd_sched_fence_create(entity, owner);
512 if (!job->s_fence)
513 return -ENOMEM;
Chunming Zhoucb3696f2017-05-09 15:34:07 +0800514 job->id = atomic64_inc_return(&sched->job_id_count);
Monk Liue6869412016-03-07 12:49:55 +0800515
Christian Königc5f74f72016-05-19 09:54:15 +0200516 INIT_WORK(&job->finish_work, amd_sched_job_finish);
517 INIT_LIST_HEAD(&job->node);
Christian König0e51a772016-05-18 14:19:32 +0200518 INIT_DELAYED_WORK(&job->work_tdr, amd_sched_job_timedout);
Monk Liu48350962016-03-04 14:33:44 +0800519
Monk Liue6869412016-03-07 12:49:55 +0800520 return 0;
521}
522
Christian Könige688b7282015-08-20 17:01:01 +0200523/**
524 * Return ture if we can push more jobs to the hw.
525 */
526static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
527{
528 return atomic_read(&sched->hw_rq_count) <
529 sched->hw_submission_limit;
530}
531
532/**
Christian König88079002015-08-24 14:29:40 +0200533 * Wake up the scheduler when it is ready
534 */
535static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
536{
537 if (amd_sched_ready(sched))
Christian Königc2b6bd72015-08-25 21:39:31 +0200538 wake_up_interruptible(&sched->wake_up_worker);
Christian König88079002015-08-24 14:29:40 +0200539}
540
541/**
Christian König3d651932015-11-12 21:10:35 +0100542 * Select next entity to process
Christian Könige688b7282015-08-20 17:01:01 +0200543*/
Christian König3d651932015-11-12 21:10:35 +0100544static struct amd_sched_entity *
545amd_sched_select_entity(struct amd_gpu_scheduler *sched)
Christian Könige688b7282015-08-20 17:01:01 +0200546{
Christian König3d651932015-11-12 21:10:35 +0100547 struct amd_sched_entity *entity;
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800548 int i;
Christian Könige688b7282015-08-20 17:01:01 +0200549
550 if (!amd_sched_ready(sched))
551 return NULL;
552
553 /* Kernel run queue has higher priority than normal run queue*/
Chunming Zhou153de9d2017-03-16 11:44:49 +0800554 for (i = AMD_SCHED_PRIORITY_MAX - 1; i >= AMD_SCHED_PRIORITY_MIN; i--) {
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800555 entity = amd_sched_rq_select_entity(&sched->sched_rq[i]);
556 if (entity)
557 break;
558 }
Christian Könige688b7282015-08-20 17:01:01 +0200559
Christian König3d651932015-11-12 21:10:35 +0100560 return entity;
Christian Könige688b7282015-08-20 17:01:01 +0200561}
562
Chris Wilsonf54d1862016-10-25 13:00:45 +0100563static void amd_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
Christian König6f0e54a2015-08-05 21:22:10 +0200564{
Christian König258f3f92015-08-31 17:02:52 +0200565 struct amd_sched_fence *s_fence =
566 container_of(cb, struct amd_sched_fence, cb);
Christian König9b398fa2015-09-07 18:16:49 +0200567 struct amd_gpu_scheduler *sched = s_fence->sched;
Christian König6f0e54a2015-08-05 21:22:10 +0200568
Christian Königc746ba22015-08-19 16:12:15 +0200569 atomic_dec(&sched->hw_rq_count);
Christian König6fc13672016-05-20 12:53:52 +0200570 amd_sched_fence_finished(s_fence);
Monk Liucccd9bc2016-03-04 14:42:26 +0800571
Chunming Zhou7034dec2015-11-11 14:56:00 +0800572 trace_amd_sched_process_job(s_fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100573 dma_fence_put(&s_fence->finished);
Christian Königc2b6bd72015-08-25 21:39:31 +0200574 wake_up_interruptible(&sched->wake_up_worker);
Christian König6f0e54a2015-08-05 21:22:10 +0200575}
576
Chunming Zhou0875dc92016-06-12 15:41:58 +0800577static bool amd_sched_blocked(struct amd_gpu_scheduler *sched)
578{
579 if (kthread_should_park()) {
580 kthread_parkme();
581 return true;
582 }
583
584 return false;
585}
586
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800587static int amd_sched_main(void *param)
588{
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800589 struct sched_param sparam = {.sched_priority = 1};
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800590 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
Alex Deucher5134e992015-09-04 00:11:54 -0400591 int r, count;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800592
593 sched_setscheduler(current, SCHED_FIFO, &sparam);
594
595 while (!kthread_should_stop()) {
Chunming Zhou0875dc92016-06-12 15:41:58 +0800596 struct amd_sched_entity *entity = NULL;
Christian König258f3f92015-08-31 17:02:52 +0200597 struct amd_sched_fence *s_fence;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800598 struct amd_sched_job *sched_job;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100599 struct dma_fence *fence;
Christian König6f0e54a2015-08-05 21:22:10 +0200600
Christian Königc2b6bd72015-08-25 21:39:31 +0200601 wait_event_interruptible(sched->wake_up_worker,
Chunming Zhou0875dc92016-06-12 15:41:58 +0800602 (!amd_sched_blocked(sched) &&
603 (entity = amd_sched_select_entity(sched))) ||
604 kthread_should_stop());
Christian Königf85a6dd2015-08-19 17:37:52 +0200605
Christian König3d651932015-11-12 21:10:35 +0100606 if (!entity)
607 continue;
608
Nicolai Hähnle515c6fa2017-09-28 11:21:15 +0200609 sched_job = amd_sched_entity_peek_job(entity);
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800610 if (!sched_job)
Christian Königf85a6dd2015-08-19 17:37:52 +0200611 continue;
612
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800613 s_fence = sched_job->s_fence;
Junwei Zhang2440ff22015-10-10 08:48:42 +0800614
Christian Königb034b572015-08-20 17:08:25 +0200615 atomic_inc(&sched->hw_rq_count);
Christian König7392c322016-05-18 13:00:38 +0200616 amd_sched_job_begin(sched_job);
Christian König7392c322016-05-18 13:00:38 +0200617
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800618 fence = sched->ops->run_job(sched_job);
Christian König393a0bd2015-11-05 12:57:10 +0100619 amd_sched_fence_scheduled(s_fence);
Nicolai Hähnle29d25352017-09-28 11:51:32 +0200620
621 /* amd_sched_process_job drops the job's reference of the fence. */
622 sched_job->s_fence = NULL;
623
Christian König6f0e54a2015-08-05 21:22:10 +0200624 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100625 s_fence->parent = dma_fence_get(fence);
626 r = dma_fence_add_callback(fence, &s_fence->cb,
627 amd_sched_process_job);
Christian König6f0e54a2015-08-05 21:22:10 +0200628 if (r == -ENOENT)
Christian König258f3f92015-08-31 17:02:52 +0200629 amd_sched_process_job(fence, &s_fence->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200630 else if (r)
Christian König16a71332016-05-18 09:43:07 +0200631 DRM_ERROR("fence add callback failed (%d)\n",
632 r);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100633 dma_fence_put(fence);
Christian König27439fc2015-09-02 12:03:06 +0200634 } else {
635 DRM_ERROR("Failed to run job!\n");
Christian König258f3f92015-08-31 17:02:52 +0200636 amd_sched_process_job(NULL, &s_fence->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200637 }
Christian Königaef48522015-08-20 14:47:46 +0200638
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800639 count = kfifo_out(&entity->job_queue, &sched_job,
640 sizeof(sched_job));
641 WARN_ON(count != sizeof(sched_job));
Christian Königc2b6bd72015-08-25 21:39:31 +0200642 wake_up(&sched->job_scheduled);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800643 }
644 return 0;
645}
646
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800647/**
Christian König4f839a22015-09-08 20:22:31 +0200648 * Init a gpu scheduler instance
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800649 *
Christian König4f839a22015-09-08 20:22:31 +0200650 * @sched The pointer to the scheduler
Christian König69f7dd62015-08-20 17:24:40 +0200651 * @ops The backend operations for this scheduler.
Christian König69f7dd62015-08-20 17:24:40 +0200652 * @hw_submissions Number of hw submissions to do.
Christian König4f839a22015-09-08 20:22:31 +0200653 * @name Name used for debugging
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800654 *
Christian König4f839a22015-09-08 20:22:31 +0200655 * Return 0 on success, otherwise error code.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800656*/
Christian König4f839a22015-09-08 20:22:31 +0200657int amd_sched_init(struct amd_gpu_scheduler *sched,
Nils Wallménius62250a92016-04-10 16:30:00 +0200658 const struct amd_sched_backend_ops *ops,
Junwei Zhang2440ff22015-10-10 08:48:42 +0800659 unsigned hw_submission, long timeout, const char *name)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800660{
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800661 int i;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800662 sched->ops = ops;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800663 sched->hw_submission_limit = hw_submission;
Christian König4f839a22015-09-08 20:22:31 +0200664 sched->name = name;
Junwei Zhang2440ff22015-10-10 08:48:42 +0800665 sched->timeout = timeout;
Chunming Zhou153de9d2017-03-16 11:44:49 +0800666 for (i = AMD_SCHED_PRIORITY_MIN; i < AMD_SCHED_PRIORITY_MAX; i++)
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800667 amd_sched_rq_init(&sched->sched_rq[i]);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800668
Christian Königc2b6bd72015-08-25 21:39:31 +0200669 init_waitqueue_head(&sched->wake_up_worker);
670 init_waitqueue_head(&sched->job_scheduled);
Monk Liu48350962016-03-04 14:33:44 +0800671 INIT_LIST_HEAD(&sched->ring_mirror_list);
672 spin_lock_init(&sched->job_list_lock);
Christian Königc746ba22015-08-19 16:12:15 +0200673 atomic_set(&sched->hw_rq_count, 0);
Andres Rodriguez93f8b362017-03-09 21:25:50 -0500674 atomic64_set(&sched->job_id_count, 0);
Christian König4f839a22015-09-08 20:22:31 +0200675
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800676 /* Each scheduler will run on a seperate kernel thread */
Christian Königc14692f02015-08-21 15:18:47 +0200677 sched->thread = kthread_run(amd_sched_main, sched, sched->name);
Christian Königf4956592015-08-20 16:59:38 +0200678 if (IS_ERR(sched->thread)) {
Christian König4f839a22015-09-08 20:22:31 +0200679 DRM_ERROR("Failed to create scheduler for %s.\n", name);
680 return PTR_ERR(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800681 }
682
Christian König4f839a22015-09-08 20:22:31 +0200683 return 0;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800684}
685
686/**
687 * Destroy a gpu scheduler
688 *
689 * @sched The pointer to the scheduler
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800690 */
Christian König4f839a22015-09-08 20:22:31 +0200691void amd_sched_fini(struct amd_gpu_scheduler *sched)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800692{
Dave Airlie32544d02015-11-03 11:10:03 -0500693 if (sched->thread)
694 kthread_stop(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800695}