]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/gpu/drm/amd/scheduler/gpu_scheduler.c
drm/amdgpu: remove entity idle timeout v2
[mirror_ubuntu-focal-kernel.git] / drivers / gpu / drm / amd / scheduler / gpu_scheduler.c
CommitLineData
a72ce6f8
JZ
1/*
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 */
432a4ff8 31static void amd_sched_rq_init(struct amd_sched_rq *rq)
a72ce6f8 32{
2b184d8d 33 spin_lock_init(&rq->lock);
432a4ff8 34 INIT_LIST_HEAD(&rq->entities);
432a4ff8 35 rq->current_entity = NULL;
a72ce6f8
JZ
36}
37
432a4ff8
CK
38static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
39 struct amd_sched_entity *entity)
a72ce6f8 40{
2b184d8d 41 spin_lock(&rq->lock);
432a4ff8 42 list_add_tail(&entity->list, &rq->entities);
2b184d8d 43 spin_unlock(&rq->lock);
a72ce6f8
JZ
44}
45
432a4ff8
CK
46static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
47 struct amd_sched_entity *entity)
a72ce6f8 48{
2b184d8d 49 spin_lock(&rq->lock);
432a4ff8
CK
50 list_del_init(&entity->list);
51 if (rq->current_entity == entity)
52 rq->current_entity = NULL;
2b184d8d 53 spin_unlock(&rq->lock);
a72ce6f8
JZ
54}
55
56/**
57 * Select next entity from a specified run queue with round robin policy.
58 * It could return the same entity as current one if current is the only
59 * available one in the queue. Return NULL if nothing available.
60 */
432a4ff8
CK
61static struct amd_sched_entity *
62amd_sched_rq_select_entity(struct amd_sched_rq *rq)
a72ce6f8 63{
2b184d8d 64 struct amd_sched_entity *entity;
432a4ff8 65
2b184d8d
CK
66 spin_lock(&rq->lock);
67
68 entity = rq->current_entity;
432a4ff8
CK
69 if (entity) {
70 list_for_each_entry_continue(entity, &rq->entities, list) {
71 if (!kfifo_is_empty(&entity->job_queue)) {
72 rq->current_entity = entity;
2b184d8d 73 spin_unlock(&rq->lock);
432a4ff8
CK
74 return rq->current_entity;
75 }
a72ce6f8 76 }
a72ce6f8 77 }
a72ce6f8 78
432a4ff8 79 list_for_each_entry(entity, &rq->entities, list) {
a72ce6f8 80
432a4ff8
CK
81 if (!kfifo_is_empty(&entity->job_queue)) {
82 rq->current_entity = entity;
2b184d8d 83 spin_unlock(&rq->lock);
432a4ff8
CK
84 return rq->current_entity;
85 }
a72ce6f8 86
432a4ff8
CK
87 if (entity == rq->current_entity)
88 break;
89 }
a72ce6f8 90
2b184d8d
CK
91 spin_unlock(&rq->lock);
92
432a4ff8 93 return NULL;
a72ce6f8
JZ
94}
95
a72ce6f8
JZ
96/**
97 * Init a context entity used by scheduler when submit to HW ring.
98 *
99 * @sched The pointer to the scheduler
91404fb2 100 * @entity The pointer to a valid amd_sched_entity
a72ce6f8 101 * @rq The run queue this entity belongs
0e89d0c1 102 * @kernel If this is an entity for the kernel
1333f723 103 * @jobs The max number of jobs in the job queue
a72ce6f8
JZ
104 *
105 * return 0 if succeed. negative error code on failure
106*/
91404fb2 107int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
6f0e54a9 108 struct amd_sched_entity *entity,
432a4ff8 109 struct amd_sched_rq *rq,
6f0e54a9 110 uint32_t jobs)
a72ce6f8 111{
a72ce6f8
JZ
112 if (!(sched && entity && rq))
113 return -EINVAL;
114
91404fb2 115 memset(entity, 0, sizeof(struct amd_sched_entity));
91404fb2 116 entity->belongto_rq = rq;
a72ce6f8
JZ
117 entity->scheduler = sched;
118 init_waitqueue_head(&entity->wait_queue);
f556cb0c 119 entity->fence_context = fence_context_alloc(1);
a72ce6f8 120 if(kfifo_alloc(&entity->job_queue,
1333f723 121 jobs * sizeof(void *),
a72ce6f8
JZ
122 GFP_KERNEL))
123 return -EINVAL;
124
125 spin_lock_init(&entity->queue_lock);
ce882e6d 126 atomic_set(&entity->fence_seq, 0);
a72ce6f8
JZ
127
128 /* Add the entity to the run queue */
432a4ff8 129 amd_sched_rq_add_entity(rq, entity);
a72ce6f8
JZ
130 return 0;
131}
132
133/**
134 * Query if entity is initialized
135 *
136 * @sched Pointer to scheduler instance
137 * @entity The pointer to a valid scheduler entity
138 *
139 * return true if entity is initialized, false otherwise
140*/
d54fdb94
CK
141static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
142 struct amd_sched_entity *entity)
a72ce6f8
JZ
143{
144 return entity->scheduler == sched &&
91404fb2 145 entity->belongto_rq != NULL;
a72ce6f8
JZ
146}
147
aef4852e
CK
148/**
149 * Check if entity is idle
150 *
151 * @entity The pointer to a valid scheduler entity
152 *
153 * Return true if entity don't has any unscheduled jobs.
154 */
155static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
a72ce6f8 156{
aef4852e
CK
157 rmb();
158 if (kfifo_is_empty(&entity->job_queue))
a72ce6f8
JZ
159 return true;
160
161 return false;
162}
163
164/**
165 * Destroy a context entity
166 *
167 * @sched Pointer to scheduler instance
168 * @entity The pointer to a valid scheduler entity
169 *
062c7fb3 170 * Cleanup and free the allocated resources.
a72ce6f8 171 */
062c7fb3
CK
172void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
173 struct amd_sched_entity *entity)
a72ce6f8 174{
432a4ff8 175 struct amd_sched_rq *rq = entity->belongto_rq;
a72ce6f8 176
d54fdb94 177 if (!amd_sched_entity_is_initialized(sched, entity))
062c7fb3 178 return;
6c859274 179
a72ce6f8
JZ
180 /**
181 * The client will not queue more IBs during this fini, consume existing
182 * queued IBs
183 */
062c7fb3 184 wait_event(entity->wait_queue, amd_sched_entity_is_idle(entity));
a72ce6f8 185
432a4ff8 186 amd_sched_rq_remove_entity(rq, entity);
a72ce6f8 187 kfifo_free(&entity->job_queue);
a72ce6f8
JZ
188}
189
190/**
6c859274 191 * Helper to submit a job to the job queue
a72ce6f8 192 *
a72ce6f8 193 * @job The pointer to job required to submit
6c859274
CK
194 *
195 * Returns true if we could submit the job.
196 */
197static bool amd_sched_entity_in(struct amd_sched_job *job)
a72ce6f8 198{
6c859274
CK
199 struct amd_sched_entity *entity = job->s_entity;
200 bool added, first = false;
201
202 spin_lock(&entity->queue_lock);
203 added = kfifo_in(&entity->job_queue, &job, sizeof(job)) == sizeof(job);
204
205 if (added && kfifo_len(&entity->job_queue) == sizeof(job))
206 first = true;
207
208 spin_unlock(&entity->queue_lock);
209
210 /* first job wakes up scheduler */
211 if (first)
212 wake_up_interruptible(&job->sched->wait_queue);
213
214 return added;
215}
216
217/**
218 * Submit a job to the job queue
219 *
220 * @job The pointer to job required to submit
221 *
222 * Returns 0 for success, negative error code otherwise.
223 */
224int amd_sched_entity_push_job(struct amd_sched_job *sched_job)
225{
226 struct amd_sched_entity *entity = sched_job->s_entity;
84f76ea6
CZ
227 struct amd_sched_fence *fence = amd_sched_fence_create(
228 entity, sched_job->owner);
6c859274
CK
229 int r;
230
f556cb0c 231 if (!fence)
6c859274
CK
232 return -ENOMEM;
233
bb977d37
CZ
234 fence_get(&fence->base);
235 sched_job->s_fence = fence;
6c859274
CK
236
237 r = wait_event_interruptible(entity->wait_queue,
238 amd_sched_entity_in(sched_job));
239
240 return r;
a72ce6f8
JZ
241}
242
e688b728
CK
243/**
244 * Return ture if we can push more jobs to the hw.
245 */
246static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
247{
248 return atomic_read(&sched->hw_rq_count) <
249 sched->hw_submission_limit;
250}
251
252/**
253 * Select next entity containing real IB submissions
254*/
255static struct amd_sched_entity *
256amd_sched_select_context(struct amd_gpu_scheduler *sched)
257{
258 struct amd_sched_entity *tmp;
259
260 if (!amd_sched_ready(sched))
261 return NULL;
262
263 /* Kernel run queue has higher priority than normal run queue*/
264 tmp = amd_sched_rq_select_entity(&sched->kernel_rq);
265 if (tmp == NULL)
266 tmp = amd_sched_rq_select_entity(&sched->sched_rq);
267
268 return tmp;
269}
270
6f0e54a9
CK
271static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
272{
273 struct amd_sched_job *sched_job =
274 container_of(cb, struct amd_sched_job, cb);
275 struct amd_gpu_scheduler *sched;
6f0e54a9
CK
276
277 sched = sched_job->sched;
f556cb0c 278 amd_sched_fence_signal(sched_job->s_fence);
c746ba22 279 atomic_dec(&sched->hw_rq_count);
f556cb0c 280 fence_put(&sched_job->s_fence->base);
bb977d37 281 sched->ops->process_job(sched, sched_job);
6f0e54a9
CK
282 wake_up_interruptible(&sched->wait_queue);
283}
284
a72ce6f8
JZ
285static int amd_sched_main(void *param)
286{
a72ce6f8 287 struct sched_param sparam = {.sched_priority = 1};
a72ce6f8 288 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
f85a6dd9 289 int r;
a72ce6f8
JZ
290
291 sched_setscheduler(current, SCHED_FIFO, &sparam);
292
293 while (!kthread_should_stop()) {
f85a6dd9
CK
294 struct amd_sched_entity *c_entity = NULL;
295 struct amd_sched_job *job;
6f0e54a9
CK
296 struct fence *fence;
297
a72ce6f8 298 wait_event_interruptible(sched->wait_queue,
f85a6dd9
CK
299 kthread_should_stop() ||
300 (c_entity = amd_sched_select_context(sched)));
301
302 if (!c_entity)
303 continue;
304
a72ce6f8
JZ
305 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
306 if (r != sizeof(void *))
307 continue;
b034b572
CK
308 atomic_inc(&sched->hw_rq_count);
309
953e8fd4 310 fence = sched->ops->run_job(sched, c_entity, job);
6f0e54a9 311 if (fence) {
953e8fd4 312 r = fence_add_callback(fence, &job->cb,
6f0e54a9
CK
313 amd_sched_process_job);
314 if (r == -ENOENT)
953e8fd4 315 amd_sched_process_job(fence, &job->cb);
6f0e54a9
CK
316 else if (r)
317 DRM_ERROR("fence add callback failed (%d)\n", r);
318 fence_put(fence);
319 }
aef4852e 320
6c859274 321 wake_up(&c_entity->wait_queue);
a72ce6f8
JZ
322 }
323 return 0;
324}
325
a72ce6f8
JZ
326/**
327 * Create a gpu scheduler
328 *
69f7dd65
CK
329 * @ops The backend operations for this scheduler.
330 * @ring The the ring id for the scheduler.
331 * @hw_submissions Number of hw submissions to do.
a72ce6f8 332 *
69f7dd65 333 * Return the pointer to scheduler for success, otherwise return NULL
a72ce6f8 334*/
69f7dd65 335struct amd_gpu_scheduler *amd_sched_create(struct amd_sched_backend_ops *ops,
f38fdfdd
CZ
336 unsigned ring, unsigned hw_submission,
337 void *priv)
a72ce6f8
JZ
338{
339 struct amd_gpu_scheduler *sched;
a72ce6f8
JZ
340
341 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
342 if (!sched)
343 return NULL;
344
a72ce6f8 345 sched->ops = ops;
a72ce6f8 346 sched->ring_id = ring;
4cef9267 347 sched->hw_submission_limit = hw_submission;
f38fdfdd 348 sched->priv = priv;
c14692f0 349 snprintf(sched->name, sizeof(sched->name), "amdgpu[%d]", ring);
432a4ff8
CK
350 amd_sched_rq_init(&sched->sched_rq);
351 amd_sched_rq_init(&sched->kernel_rq);
a72ce6f8
JZ
352
353 init_waitqueue_head(&sched->wait_queue);
c746ba22 354 atomic_set(&sched->hw_rq_count, 0);
a72ce6f8 355 /* Each scheduler will run on a seperate kernel thread */
c14692f0 356 sched->thread = kthread_run(amd_sched_main, sched, sched->name);
f4956598
CK
357 if (IS_ERR(sched->thread)) {
358 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
359 kfree(sched);
360 return NULL;
a72ce6f8
JZ
361 }
362
f4956598 363 return sched;
a72ce6f8
JZ
364}
365
366/**
367 * Destroy a gpu scheduler
368 *
369 * @sched The pointer to the scheduler
370 *
371 * return 0 if succeed. -1 if failed.
372 */
373int amd_sched_destroy(struct amd_gpu_scheduler *sched)
374{
375 kthread_stop(sched->thread);
a72ce6f8
JZ
376 kfree(sched);
377 return 0;
378}