blob: 38cea6fb25a8b9221d64b43da04c4268a2c986b8 [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;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800208
Christian Königd54fdb92015-08-20 17:03:48 +0200209 if (!amd_sched_entity_is_initialized(sched, entity))
Christian König062c7fb2015-08-21 15:46:43 +0200210 return;
Alex Deucherc9450122017-10-12 13:08:48 -0400211
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800212 /**
213 * The client will not queue more IBs during this fini, consume existing
Alex Deucherc9450122017-10-12 13:08:48 -0400214 * queued IBs
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800215 */
Alex Deucherc9450122017-10-12 13:08:48 -0400216 wait_event(sched->job_scheduled, amd_sched_entity_is_idle(entity));
217
Christian König432a4ff2015-08-12 11:46:04 +0200218 amd_sched_rq_remove_entity(rq, entity);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800219 kfifo_free(&entity->job_queue);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800220}
221
Chris Wilsonf54d1862016-10-25 13:00:45 +0100222static void amd_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
Christian Könige61235d2015-08-25 11:05:36 +0200223{
224 struct amd_sched_entity *entity =
225 container_of(cb, struct amd_sched_entity, cb);
226 entity->dependency = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100227 dma_fence_put(f);
Christian König0f75aee2015-09-07 18:07:14 +0200228 amd_sched_wakeup(entity->sched);
Christian Könige61235d2015-08-25 11:05:36 +0200229}
230
Chris Wilsonf54d1862016-10-25 13:00:45 +0100231static void amd_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
Monk Liu777dbd42016-01-26 14:59:57 +0800232{
233 struct amd_sched_entity *entity =
234 container_of(cb, struct amd_sched_entity, cb);
235 entity->dependency = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100236 dma_fence_put(f);
Monk Liu777dbd42016-01-26 14:59:57 +0800237}
238
Chunming Zhou30514de2017-05-09 13:39:40 +0800239bool amd_sched_dependency_optimized(struct dma_fence* fence,
240 struct amd_sched_entity *entity)
241{
242 struct amd_gpu_scheduler *sched = entity->sched;
243 struct amd_sched_fence *s_fence;
244
245 if (!fence || dma_fence_is_signaled(fence))
246 return false;
247 if (fence->context == entity->fence_context)
248 return true;
249 s_fence = to_amd_sched_fence(fence);
250 if (s_fence && s_fence->sched == sched)
251 return true;
252
253 return false;
254}
255
Christian König393a0bd2015-11-05 12:57:10 +0100256static bool amd_sched_entity_add_dependency_cb(struct amd_sched_entity *entity)
257{
258 struct amd_gpu_scheduler *sched = entity->sched;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100259 struct dma_fence * fence = entity->dependency;
Christian König393a0bd2015-11-05 12:57:10 +0100260 struct amd_sched_fence *s_fence;
261
262 if (fence->context == entity->fence_context) {
263 /* We can ignore fences from ourself */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100264 dma_fence_put(entity->dependency);
Christian König393a0bd2015-11-05 12:57:10 +0100265 return false;
266 }
267
268 s_fence = to_amd_sched_fence(fence);
269 if (s_fence && s_fence->sched == sched) {
Christian König393a0bd2015-11-05 12:57:10 +0100270
Christian König6fc13672016-05-20 12:53:52 +0200271 /*
272 * Fence is from the same scheduler, only need to wait for
273 * it to be scheduled
274 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100275 fence = dma_fence_get(&s_fence->scheduled);
276 dma_fence_put(entity->dependency);
Christian König6fc13672016-05-20 12:53:52 +0200277 entity->dependency = fence;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100278 if (!dma_fence_add_callback(fence, &entity->cb,
279 amd_sched_entity_clear_dep))
Christian König6fc13672016-05-20 12:53:52 +0200280 return true;
281
282 /* Ignore it when it is already scheduled */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100283 dma_fence_put(fence);
Christian König6fc13672016-05-20 12:53:52 +0200284 return false;
Christian König393a0bd2015-11-05 12:57:10 +0100285 }
286
Chris Wilsonf54d1862016-10-25 13:00:45 +0100287 if (!dma_fence_add_callback(entity->dependency, &entity->cb,
288 amd_sched_entity_wakeup))
Christian König393a0bd2015-11-05 12:57:10 +0100289 return true;
290
Chris Wilsonf54d1862016-10-25 13:00:45 +0100291 dma_fence_put(entity->dependency);
Christian König393a0bd2015-11-05 12:57:10 +0100292 return false;
293}
294
Christian König69bd5bf2015-08-26 11:31:23 +0200295static struct amd_sched_job *
296amd_sched_entity_pop_job(struct amd_sched_entity *entity)
297{
Christian König0f75aee2015-09-07 18:07:14 +0200298 struct amd_gpu_scheduler *sched = entity->sched;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800299 struct amd_sched_job *sched_job;
Christian König69bd5bf2015-08-26 11:31:23 +0200300
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800301 if (!kfifo_out_peek(&entity->job_queue, &sched_job, sizeof(sched_job)))
Christian König69bd5bf2015-08-26 11:31:23 +0200302 return NULL;
303
Christian König393a0bd2015-11-05 12:57:10 +0100304 while ((entity->dependency = sched->ops->dependency(sched_job)))
305 if (amd_sched_entity_add_dependency_cb(entity))
Christian Könige61235d2015-08-25 11:05:36 +0200306 return NULL;
Christian Könige61235d2015-08-25 11:05:36 +0200307
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800308 return sched_job;
Christian König69bd5bf2015-08-26 11:31:23 +0200309}
310
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800311/**
Christian König6c859272015-08-20 16:12:50 +0200312 * Helper to submit a job to the job queue
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800313 *
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800314 * @sched_job The pointer to job required to submit
Christian König6c859272015-08-20 16:12:50 +0200315 *
316 * Returns true if we could submit the job.
317 */
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800318static bool amd_sched_entity_in(struct amd_sched_job *sched_job)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800319{
Nicolai Hähnle786b5212015-12-02 17:35:12 +0100320 struct amd_gpu_scheduler *sched = sched_job->sched;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800321 struct amd_sched_entity *entity = sched_job->s_entity;
Christian König6c859272015-08-20 16:12:50 +0200322 bool added, first = false;
323
324 spin_lock(&entity->queue_lock);
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800325 added = kfifo_in(&entity->job_queue, &sched_job,
326 sizeof(sched_job)) == sizeof(sched_job);
Christian König6c859272015-08-20 16:12:50 +0200327
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800328 if (added && kfifo_len(&entity->job_queue) == sizeof(sched_job))
Christian König6c859272015-08-20 16:12:50 +0200329 first = true;
330
331 spin_unlock(&entity->queue_lock);
332
333 /* first job wakes up scheduler */
Chunming Zhoue8deea22015-12-11 18:22:52 +0800334 if (first) {
335 /* Add the entity to the run queue */
336 amd_sched_rq_add_entity(entity->rq, entity);
Nicolai Hähnle786b5212015-12-02 17:35:12 +0100337 amd_sched_wakeup(sched);
Chunming Zhoue8deea22015-12-11 18:22:52 +0800338 }
Christian König6c859272015-08-20 16:12:50 +0200339 return added;
340}
341
Monk Liu0de24792016-03-04 18:51:02 +0800342/* job_finish is called after hw fence signaled, and
343 * the job had already been deleted from ring_mirror_list
344 */
Christian Königc5f74f72016-05-19 09:54:15 +0200345static void amd_sched_job_finish(struct work_struct *work)
Monk Liu0de24792016-03-04 18:51:02 +0800346{
Christian Königc5f74f72016-05-19 09:54:15 +0200347 struct amd_sched_job *s_job = container_of(work, struct amd_sched_job,
348 finish_work);
Monk Liu0de24792016-03-04 18:51:02 +0800349 struct amd_gpu_scheduler *sched = s_job->sched;
350
Christian Königf42d20a92016-05-18 15:40:58 +0200351 /* remove job from ring_mirror_list */
Christian König1059e112016-06-13 16:12:43 +0200352 spin_lock(&sched->job_list_lock);
Christian Königf42d20a92016-05-18 15:40:58 +0200353 list_del_init(&s_job->node);
Monk Liu0de24792016-03-04 18:51:02 +0800354 if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
Christian Königc5f74f72016-05-19 09:54:15 +0200355 struct amd_sched_job *next;
356
Christian König1059e112016-06-13 16:12:43 +0200357 spin_unlock(&sched->job_list_lock);
Christian Königc5f74f72016-05-19 09:54:15 +0200358 cancel_delayed_work_sync(&s_job->work_tdr);
Christian König1059e112016-06-13 16:12:43 +0200359 spin_lock(&sched->job_list_lock);
Monk Liu0de24792016-03-04 18:51:02 +0800360
361 /* queue TDR for next job */
362 next = list_first_entry_or_null(&sched->ring_mirror_list,
363 struct amd_sched_job, node);
364
Christian Königc5f74f72016-05-19 09:54:15 +0200365 if (next)
Monk Liu0de24792016-03-04 18:51:02 +0800366 schedule_delayed_work(&next->work_tdr, sched->timeout);
Monk Liu0de24792016-03-04 18:51:02 +0800367 }
Christian König1059e112016-06-13 16:12:43 +0200368 spin_unlock(&sched->job_list_lock);
Christian Königc5f74f72016-05-19 09:54:15 +0200369 sched->ops->free_job(s_job);
370}
371
Chris Wilsonf54d1862016-10-25 13:00:45 +0100372static void amd_sched_job_finish_cb(struct dma_fence *f,
373 struct dma_fence_cb *cb)
Christian Königc5f74f72016-05-19 09:54:15 +0200374{
375 struct amd_sched_job *job = container_of(cb, struct amd_sched_job,
376 finish_cb);
377 schedule_work(&job->finish_work);
Monk Liu0de24792016-03-04 18:51:02 +0800378}
379
Christian König7392c322016-05-18 13:00:38 +0200380static void amd_sched_job_begin(struct amd_sched_job *s_job)
Monk Liu0de24792016-03-04 18:51:02 +0800381{
382 struct amd_gpu_scheduler *sched = s_job->sched;
383
Christian König1059e112016-06-13 16:12:43 +0200384 spin_lock(&sched->job_list_lock);
Christian Königf42d20a92016-05-18 15:40:58 +0200385 list_add_tail(&s_job->node, &sched->ring_mirror_list);
Monk Liu0de24792016-03-04 18:51:02 +0800386 if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
Christian König16a71332016-05-18 09:43:07 +0200387 list_first_entry_or_null(&sched->ring_mirror_list,
388 struct amd_sched_job, node) == s_job)
Monk Liu0de24792016-03-04 18:51:02 +0800389 schedule_delayed_work(&s_job->work_tdr, sched->timeout);
Christian König1059e112016-06-13 16:12:43 +0200390 spin_unlock(&sched->job_list_lock);
Monk Liu0de24792016-03-04 18:51:02 +0800391}
392
Christian König0e51a772016-05-18 14:19:32 +0200393static void amd_sched_job_timedout(struct work_struct *work)
394{
395 struct amd_sched_job *job = container_of(work, struct amd_sched_job,
396 work_tdr.work);
397
398 job->sched->ops->timedout_job(job);
399}
400
Chunming Zhoue686e752016-06-30 11:30:37 +0800401void amd_sched_hw_job_reset(struct amd_gpu_scheduler *sched)
402{
403 struct amd_sched_job *s_job;
404
405 spin_lock(&sched->job_list_lock);
406 list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
Chunming Zhoua6bef672017-04-24 17:39:00 +0800407 if (s_job->s_fence->parent &&
408 dma_fence_remove_callback(s_job->s_fence->parent,
409 &s_job->s_fence->cb)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100410 dma_fence_put(s_job->s_fence->parent);
Chunming Zhoue686e752016-06-30 11:30:37 +0800411 s_job->s_fence->parent = NULL;
Monk Liu65781c72017-05-11 13:36:44 +0800412 atomic_dec(&sched->hw_rq_count);
Chunming Zhoue686e752016-06-30 11:30:37 +0800413 }
414 }
Monk Liu65781c72017-05-11 13:36:44 +0800415 spin_unlock(&sched->job_list_lock);
416}
417
418void amd_sched_job_kickout(struct amd_sched_job *s_job)
419{
420 struct amd_gpu_scheduler *sched = s_job->sched;
421
422 spin_lock(&sched->job_list_lock);
423 list_del_init(&s_job->node);
Chunming Zhoue686e752016-06-30 11:30:37 +0800424 spin_unlock(&sched->job_list_lock);
425}
426
Chunming Zhouec75f572016-06-29 15:23:55 +0800427void amd_sched_job_recovery(struct amd_gpu_scheduler *sched)
428{
Chunming Zhou1c62cf92016-07-25 13:55:35 +0800429 struct amd_sched_job *s_job, *tmp;
Chunming Zhouec75f572016-06-29 15:23:55 +0800430 int r;
431
432 spin_lock(&sched->job_list_lock);
433 s_job = list_first_entry_or_null(&sched->ring_mirror_list,
434 struct amd_sched_job, node);
Christian Königbdf00132016-08-16 19:52:35 +0200435 if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT)
Chunming Zhouec75f572016-06-29 15:23:55 +0800436 schedule_delayed_work(&s_job->work_tdr, sched->timeout);
437
Chunming Zhou1c62cf92016-07-25 13:55:35 +0800438 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
Chunming Zhouec75f572016-06-29 15:23:55 +0800439 struct amd_sched_fence *s_fence = s_job->s_fence;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100440 struct dma_fence *fence;
Chunming Zhoubdc2eea2016-07-22 13:01:02 +0800441
Chunming Zhou1c62cf92016-07-25 13:55:35 +0800442 spin_unlock(&sched->job_list_lock);
443 fence = sched->ops->run_job(s_job);
Chunming Zhoubdc2eea2016-07-22 13:01:02 +0800444 atomic_inc(&sched->hw_rq_count);
Chunming Zhouec75f572016-06-29 15:23:55 +0800445 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100446 s_fence->parent = dma_fence_get(fence);
447 r = dma_fence_add_callback(fence, &s_fence->cb,
448 amd_sched_process_job);
Chunming Zhouec75f572016-06-29 15:23:55 +0800449 if (r == -ENOENT)
450 amd_sched_process_job(fence, &s_fence->cb);
451 else if (r)
452 DRM_ERROR("fence add callback failed (%d)\n",
453 r);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100454 dma_fence_put(fence);
Chunming Zhouec75f572016-06-29 15:23:55 +0800455 } else {
456 DRM_ERROR("Failed to run job!\n");
457 amd_sched_process_job(NULL, &s_fence->cb);
458 }
Chunming Zhou1c62cf92016-07-25 13:55:35 +0800459 spin_lock(&sched->job_list_lock);
Chunming Zhouec75f572016-06-29 15:23:55 +0800460 }
461 spin_unlock(&sched->job_list_lock);
462}
463
Christian König6c859272015-08-20 16:12:50 +0200464/**
465 * Submit a job to the job queue
466 *
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800467 * @sched_job The pointer to job required to submit
Christian König6c859272015-08-20 16:12:50 +0200468 *
469 * Returns 0 for success, negative error code otherwise.
470 */
Christian Könige2840222015-11-05 19:49:48 +0100471void amd_sched_entity_push_job(struct amd_sched_job *sched_job)
Christian König6c859272015-08-20 16:12:50 +0200472{
473 struct amd_sched_entity *entity = sched_job->s_entity;
Christian König6c859272015-08-20 16:12:50 +0200474
Nicolai Hähnle786b5212015-12-02 17:35:12 +0100475 trace_amd_sched_job(sched_job);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100476 dma_fence_add_callback(&sched_job->s_fence->finished, &sched_job->finish_cb,
477 amd_sched_job_finish_cb);
Christian König0f75aee2015-09-07 18:07:14 +0200478 wait_event(entity->sched->job_scheduled,
Chunming Zhouc9f0fe52015-08-31 15:46:12 +0800479 amd_sched_entity_in(sched_job));
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800480}
481
Monk Liue6869412016-03-07 12:49:55 +0800482/* init a sched_job with basic field */
483int amd_sched_job_init(struct amd_sched_job *job,
Christian König16a71332016-05-18 09:43:07 +0200484 struct amd_gpu_scheduler *sched,
485 struct amd_sched_entity *entity,
Christian König595a9cd2016-06-30 10:52:03 +0200486 void *owner)
Monk Liue6869412016-03-07 12:49:55 +0800487{
488 job->sched = sched;
489 job->s_entity = entity;
490 job->s_fence = amd_sched_fence_create(entity, owner);
491 if (!job->s_fence)
492 return -ENOMEM;
Chunming Zhoucb3696f2017-05-09 15:34:07 +0800493 job->id = atomic64_inc_return(&sched->job_id_count);
Monk Liue6869412016-03-07 12:49:55 +0800494
Christian Königc5f74f72016-05-19 09:54:15 +0200495 INIT_WORK(&job->finish_work, amd_sched_job_finish);
496 INIT_LIST_HEAD(&job->node);
Christian König0e51a772016-05-18 14:19:32 +0200497 INIT_DELAYED_WORK(&job->work_tdr, amd_sched_job_timedout);
Monk Liu48350962016-03-04 14:33:44 +0800498
Monk Liue6869412016-03-07 12:49:55 +0800499 return 0;
500}
501
Christian Könige688b7282015-08-20 17:01:01 +0200502/**
503 * Return ture if we can push more jobs to the hw.
504 */
505static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
506{
507 return atomic_read(&sched->hw_rq_count) <
508 sched->hw_submission_limit;
509}
510
511/**
Christian König88079002015-08-24 14:29:40 +0200512 * Wake up the scheduler when it is ready
513 */
514static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
515{
516 if (amd_sched_ready(sched))
Christian Königc2b6bd72015-08-25 21:39:31 +0200517 wake_up_interruptible(&sched->wake_up_worker);
Christian König88079002015-08-24 14:29:40 +0200518}
519
520/**
Christian König3d651932015-11-12 21:10:35 +0100521 * Select next entity to process
Christian Könige688b7282015-08-20 17:01:01 +0200522*/
Christian König3d651932015-11-12 21:10:35 +0100523static struct amd_sched_entity *
524amd_sched_select_entity(struct amd_gpu_scheduler *sched)
Christian Könige688b7282015-08-20 17:01:01 +0200525{
Christian König3d651932015-11-12 21:10:35 +0100526 struct amd_sched_entity *entity;
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800527 int i;
Christian Könige688b7282015-08-20 17:01:01 +0200528
529 if (!amd_sched_ready(sched))
530 return NULL;
531
532 /* Kernel run queue has higher priority than normal run queue*/
Chunming Zhou153de9d2017-03-16 11:44:49 +0800533 for (i = AMD_SCHED_PRIORITY_MAX - 1; i >= AMD_SCHED_PRIORITY_MIN; i--) {
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800534 entity = amd_sched_rq_select_entity(&sched->sched_rq[i]);
535 if (entity)
536 break;
537 }
Christian Könige688b7282015-08-20 17:01:01 +0200538
Christian König3d651932015-11-12 21:10:35 +0100539 return entity;
Christian Könige688b7282015-08-20 17:01:01 +0200540}
541
Chris Wilsonf54d1862016-10-25 13:00:45 +0100542static void amd_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
Christian König6f0e54a2015-08-05 21:22:10 +0200543{
Christian König258f3f92015-08-31 17:02:52 +0200544 struct amd_sched_fence *s_fence =
545 container_of(cb, struct amd_sched_fence, cb);
Christian König9b398fa2015-09-07 18:16:49 +0200546 struct amd_gpu_scheduler *sched = s_fence->sched;
Christian König6f0e54a2015-08-05 21:22:10 +0200547
Christian Königc746ba22015-08-19 16:12:15 +0200548 atomic_dec(&sched->hw_rq_count);
Christian König6fc13672016-05-20 12:53:52 +0200549 amd_sched_fence_finished(s_fence);
Monk Liucccd9bc2016-03-04 14:42:26 +0800550
Chunming Zhou7034dec2015-11-11 14:56:00 +0800551 trace_amd_sched_process_job(s_fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100552 dma_fence_put(&s_fence->finished);
Christian Königc2b6bd72015-08-25 21:39:31 +0200553 wake_up_interruptible(&sched->wake_up_worker);
Christian König6f0e54a2015-08-05 21:22:10 +0200554}
555
Chunming Zhou0875dc92016-06-12 15:41:58 +0800556static bool amd_sched_blocked(struct amd_gpu_scheduler *sched)
557{
558 if (kthread_should_park()) {
559 kthread_parkme();
560 return true;
561 }
562
563 return false;
564}
565
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800566static int amd_sched_main(void *param)
567{
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800568 struct sched_param sparam = {.sched_priority = 1};
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800569 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
Alex Deucher5134e992015-09-04 00:11:54 -0400570 int r, count;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800571
572 sched_setscheduler(current, SCHED_FIFO, &sparam);
573
574 while (!kthread_should_stop()) {
Chunming Zhou0875dc92016-06-12 15:41:58 +0800575 struct amd_sched_entity *entity = NULL;
Christian König258f3f92015-08-31 17:02:52 +0200576 struct amd_sched_fence *s_fence;
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800577 struct amd_sched_job *sched_job;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100578 struct dma_fence *fence;
Christian König6f0e54a2015-08-05 21:22:10 +0200579
Christian Königc2b6bd72015-08-25 21:39:31 +0200580 wait_event_interruptible(sched->wake_up_worker,
Chunming Zhou0875dc92016-06-12 15:41:58 +0800581 (!amd_sched_blocked(sched) &&
582 (entity = amd_sched_select_entity(sched))) ||
583 kthread_should_stop());
Christian Königf85a6dd2015-08-19 17:37:52 +0200584
Christian König3d651932015-11-12 21:10:35 +0100585 if (!entity)
586 continue;
587
588 sched_job = amd_sched_entity_pop_job(entity);
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800589 if (!sched_job)
Christian Königf85a6dd2015-08-19 17:37:52 +0200590 continue;
591
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800592 s_fence = sched_job->s_fence;
Junwei Zhang2440ff22015-10-10 08:48:42 +0800593
Christian Königb034b572015-08-20 17:08:25 +0200594 atomic_inc(&sched->hw_rq_count);
Christian König7392c322016-05-18 13:00:38 +0200595 amd_sched_job_begin(sched_job);
Christian König7392c322016-05-18 13:00:38 +0200596
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800597 fence = sched->ops->run_job(sched_job);
Christian König393a0bd2015-11-05 12:57:10 +0100598 amd_sched_fence_scheduled(s_fence);
Christian König6f0e54a2015-08-05 21:22:10 +0200599 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100600 s_fence->parent = dma_fence_get(fence);
601 r = dma_fence_add_callback(fence, &s_fence->cb,
602 amd_sched_process_job);
Christian König6f0e54a2015-08-05 21:22:10 +0200603 if (r == -ENOENT)
Christian König258f3f92015-08-31 17:02:52 +0200604 amd_sched_process_job(fence, &s_fence->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200605 else if (r)
Christian König16a71332016-05-18 09:43:07 +0200606 DRM_ERROR("fence add callback failed (%d)\n",
607 r);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100608 dma_fence_put(fence);
Christian König27439fc2015-09-02 12:03:06 +0200609 } else {
610 DRM_ERROR("Failed to run job!\n");
Christian König258f3f92015-08-31 17:02:52 +0200611 amd_sched_process_job(NULL, &s_fence->cb);
Christian König6f0e54a2015-08-05 21:22:10 +0200612 }
Christian Königaef48522015-08-20 14:47:46 +0200613
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800614 count = kfifo_out(&entity->job_queue, &sched_job,
615 sizeof(sched_job));
616 WARN_ON(count != sizeof(sched_job));
Christian Königc2b6bd72015-08-25 21:39:31 +0200617 wake_up(&sched->job_scheduled);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800618 }
619 return 0;
620}
621
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800622/**
Christian König4f839a22015-09-08 20:22:31 +0200623 * Init a gpu scheduler instance
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800624 *
Christian König4f839a22015-09-08 20:22:31 +0200625 * @sched The pointer to the scheduler
Christian König69f7dd62015-08-20 17:24:40 +0200626 * @ops The backend operations for this scheduler.
Christian König69f7dd62015-08-20 17:24:40 +0200627 * @hw_submissions Number of hw submissions to do.
Christian König4f839a22015-09-08 20:22:31 +0200628 * @name Name used for debugging
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800629 *
Christian König4f839a22015-09-08 20:22:31 +0200630 * Return 0 on success, otherwise error code.
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800631*/
Christian König4f839a22015-09-08 20:22:31 +0200632int amd_sched_init(struct amd_gpu_scheduler *sched,
Nils Wallménius62250a92016-04-10 16:30:00 +0200633 const struct amd_sched_backend_ops *ops,
Junwei Zhang2440ff22015-10-10 08:48:42 +0800634 unsigned hw_submission, long timeout, const char *name)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800635{
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800636 int i;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800637 sched->ops = ops;
Chunming Zhou4cef9262015-08-05 19:52:14 +0800638 sched->hw_submission_limit = hw_submission;
Christian König4f839a22015-09-08 20:22:31 +0200639 sched->name = name;
Junwei Zhang2440ff22015-10-10 08:48:42 +0800640 sched->timeout = timeout;
Chunming Zhou153de9d2017-03-16 11:44:49 +0800641 for (i = AMD_SCHED_PRIORITY_MIN; i < AMD_SCHED_PRIORITY_MAX; i++)
Chunming Zhoud033a6d2015-11-05 15:23:09 +0800642 amd_sched_rq_init(&sched->sched_rq[i]);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800643
Christian Königc2b6bd72015-08-25 21:39:31 +0200644 init_waitqueue_head(&sched->wake_up_worker);
645 init_waitqueue_head(&sched->job_scheduled);
Monk Liu48350962016-03-04 14:33:44 +0800646 INIT_LIST_HEAD(&sched->ring_mirror_list);
647 spin_lock_init(&sched->job_list_lock);
Christian Königc746ba22015-08-19 16:12:15 +0200648 atomic_set(&sched->hw_rq_count, 0);
Andres Rodriguez93f8b362017-03-09 21:25:50 -0500649 atomic64_set(&sched->job_id_count, 0);
Christian König4f839a22015-09-08 20:22:31 +0200650
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800651 /* Each scheduler will run on a seperate kernel thread */
Christian Königc14692f02015-08-21 15:18:47 +0200652 sched->thread = kthread_run(amd_sched_main, sched, sched->name);
Christian Königf4956592015-08-20 16:59:38 +0200653 if (IS_ERR(sched->thread)) {
Christian König4f839a22015-09-08 20:22:31 +0200654 DRM_ERROR("Failed to create scheduler for %s.\n", name);
655 return PTR_ERR(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800656 }
657
Christian König4f839a22015-09-08 20:22:31 +0200658 return 0;
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800659}
660
661/**
662 * Destroy a gpu scheduler
663 *
664 * @sched The pointer to the scheduler
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800665 */
Christian König4f839a22015-09-08 20:22:31 +0200666void amd_sched_fini(struct amd_gpu_scheduler *sched)
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800667{
Dave Airlie32544d02015-11-03 11:10:03 -0500668 if (sched->thread)
669 kthread_stop(sched->thread);
Jammy Zhoua72ce6f2015-05-22 18:55:07 +0800670}