1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /* Copyright 2017-2019 Qiang Yu <yuq825@gmail.com> */
4 #include <linux/kthread.h>
5 #include <linux/slab.h>
8 #include "lima_sched.h"
11 #include "lima_l2_cache.h"
12 #include "lima_object.h"
15 struct dma_fence base;
16 struct lima_sched_pipe *pipe;
19 static struct kmem_cache *lima_fence_slab;
20 static int lima_fence_slab_refcnt;
22 int lima_sched_slab_init(void)
24 if (!lima_fence_slab) {
25 lima_fence_slab = kmem_cache_create(
26 "lima_fence", sizeof(struct lima_fence), 0,
27 SLAB_HWCACHE_ALIGN, NULL);
32 lima_fence_slab_refcnt++;
36 void lima_sched_slab_fini(void)
38 if (!--lima_fence_slab_refcnt) {
39 kmem_cache_destroy(lima_fence_slab);
40 lima_fence_slab = NULL;
44 static inline struct lima_fence *to_lima_fence(struct dma_fence *fence)
46 return container_of(fence, struct lima_fence, base);
49 static const char *lima_fence_get_driver_name(struct dma_fence *fence)
54 static const char *lima_fence_get_timeline_name(struct dma_fence *fence)
56 struct lima_fence *f = to_lima_fence(fence);
58 return f->pipe->base.name;
61 static void lima_fence_release_rcu(struct rcu_head *rcu)
63 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
64 struct lima_fence *fence = to_lima_fence(f);
66 kmem_cache_free(lima_fence_slab, fence);
69 static void lima_fence_release(struct dma_fence *fence)
71 struct lima_fence *f = to_lima_fence(fence);
73 call_rcu(&f->base.rcu, lima_fence_release_rcu);
76 static const struct dma_fence_ops lima_fence_ops = {
77 .get_driver_name = lima_fence_get_driver_name,
78 .get_timeline_name = lima_fence_get_timeline_name,
79 .release = lima_fence_release,
82 static struct lima_fence *lima_fence_create(struct lima_sched_pipe *pipe)
84 struct lima_fence *fence;
86 fence = kmem_cache_zalloc(lima_fence_slab, GFP_KERNEL);
91 dma_fence_init(&fence->base, &lima_fence_ops, &pipe->fence_lock,
92 pipe->fence_context, ++pipe->fence_seqno);
97 static inline struct lima_sched_task *to_lima_task(struct drm_sched_job *job)
99 return container_of(job, struct lima_sched_task, base);
102 static inline struct lima_sched_pipe *to_lima_pipe(struct drm_gpu_scheduler *sched)
104 return container_of(sched, struct lima_sched_pipe, base);
107 int lima_sched_task_init(struct lima_sched_task *task,
108 struct lima_sched_context *context,
109 struct lima_bo **bos, int num_bos,
114 task->bos = kmemdup(bos, sizeof(*bos) * num_bos, GFP_KERNEL);
118 for (i = 0; i < num_bos; i++)
119 drm_gem_object_get(&bos[i]->gem);
121 err = drm_sched_job_init(&task->base, &context->base, vm);
127 task->num_bos = num_bos;
128 task->vm = lima_vm_get(vm);
132 void lima_sched_task_fini(struct lima_sched_task *task)
136 drm_sched_job_cleanup(&task->base);
138 for (i = 0; i < task->num_dep; i++)
139 dma_fence_put(task->dep[i]);
144 for (i = 0; i < task->num_bos; i++)
145 drm_gem_object_put_unlocked(&task->bos[i]->gem);
149 lima_vm_put(task->vm);
152 int lima_sched_task_add_dep(struct lima_sched_task *task, struct dma_fence *fence)
156 /* same context's fence is definitly earlier then this task */
157 if (fence->context == task->base.s_fence->finished.context) {
158 dma_fence_put(fence);
162 if (task->dep && task->num_dep == task->max_dep)
163 new_dep = task->max_dep * 2;
165 if (task->max_dep < new_dep) {
166 void *dep = krealloc(task->dep, sizeof(*task->dep) * new_dep, GFP_KERNEL);
171 task->max_dep = new_dep;
175 for (i = 0; i < task->num_dep; i++) {
176 if (task->dep[i]->context == fence->context &&
177 dma_fence_is_later(fence, task->dep[i])) {
178 dma_fence_put(task->dep[i]);
179 task->dep[i] = fence;
184 task->dep[task->num_dep++] = fence;
188 int lima_sched_context_init(struct lima_sched_pipe *pipe,
189 struct lima_sched_context *context,
192 struct drm_sched_rq *rq = pipe->base.sched_rq + DRM_SCHED_PRIORITY_NORMAL;
194 return drm_sched_entity_init(&context->base, &rq, 1, guilty);
197 void lima_sched_context_fini(struct lima_sched_pipe *pipe,
198 struct lima_sched_context *context)
200 drm_sched_entity_fini(&context->base);
203 struct dma_fence *lima_sched_context_queue_task(struct lima_sched_context *context,
204 struct lima_sched_task *task)
206 struct dma_fence *fence = dma_fence_get(&task->base.s_fence->finished);
208 drm_sched_entity_push_job(&task->base, &context->base);
212 static struct dma_fence *lima_sched_dependency(struct drm_sched_job *job,
213 struct drm_sched_entity *entity)
215 struct lima_sched_task *task = to_lima_task(job);
218 for (i = 0; i < task->num_dep; i++) {
219 struct dma_fence *fence = task->dep[i];
226 if (!dma_fence_is_signaled(fence))
229 dma_fence_put(fence);
235 static struct dma_fence *lima_sched_run_job(struct drm_sched_job *job)
237 struct lima_sched_task *task = to_lima_task(job);
238 struct lima_sched_pipe *pipe = to_lima_pipe(job->sched);
239 struct lima_fence *fence;
240 struct dma_fence *ret;
241 struct lima_vm *vm = NULL, *last_vm = NULL;
244 /* after GPU reset */
245 if (job->s_fence->finished.error < 0)
248 fence = lima_fence_create(pipe);
251 task->fence = &fence->base;
253 /* for caller usage of the fence, otherwise irq handler
254 * may consume the fence before caller use it
256 ret = dma_fence_get(task->fence);
258 pipe->current_task = task;
260 /* this is needed for MMU to work correctly, otherwise GP/PP
261 * will hang or page fault for unknown reason after running for
264 * Need to investigate:
265 * 1. is it related to TLB
266 * 2. how much performance will be affected by L2 cache flush
267 * 3. can we reduce the calling of this function because all
268 * GP/PP use the same L2 cache on mali400
271 * 1. move this to task fini to save some wait time?
272 * 2. when GP/PP use different l2 cache, need PP wait GP l2
275 for (i = 0; i < pipe->num_l2_cache; i++)
276 lima_l2_cache_flush(pipe->l2_cache[i]);
278 if (task->vm != pipe->current_vm) {
279 vm = lima_vm_get(task->vm);
280 last_vm = pipe->current_vm;
281 pipe->current_vm = task->vm;
285 lima_mmu_switch_vm(pipe->bcast_mmu, vm);
287 for (i = 0; i < pipe->num_mmu; i++)
288 lima_mmu_switch_vm(pipe->mmu[i], vm);
292 lima_vm_put(last_vm);
295 pipe->task_run(pipe, task);
300 static void lima_sched_handle_error_task(struct lima_sched_pipe *pipe,
301 struct lima_sched_task *task)
303 drm_sched_stop(&pipe->base);
306 drm_sched_increase_karma(&task->base);
308 pipe->task_error(pipe);
311 lima_mmu_page_fault_resume(pipe->bcast_mmu);
315 for (i = 0; i < pipe->num_mmu; i++)
316 lima_mmu_page_fault_resume(pipe->mmu[i]);
319 if (pipe->current_vm)
320 lima_vm_put(pipe->current_vm);
322 pipe->current_vm = NULL;
323 pipe->current_task = NULL;
325 drm_sched_resubmit_jobs(&pipe->base);
326 drm_sched_start(&pipe->base, true);
329 static void lima_sched_timedout_job(struct drm_sched_job *job)
331 struct lima_sched_pipe *pipe = to_lima_pipe(job->sched);
332 struct lima_sched_task *task = to_lima_task(job);
334 DRM_ERROR("lima job timeout\n");
336 lima_sched_handle_error_task(pipe, task);
339 static void lima_sched_free_job(struct drm_sched_job *job)
341 struct lima_sched_task *task = to_lima_task(job);
342 struct lima_sched_pipe *pipe = to_lima_pipe(job->sched);
343 struct lima_vm *vm = task->vm;
344 struct lima_bo **bos = task->bos;
347 dma_fence_put(task->fence);
349 for (i = 0; i < task->num_bos; i++)
350 lima_vm_bo_del(vm, bos[i]);
352 lima_sched_task_fini(task);
353 kmem_cache_free(pipe->task_slab, task);
356 const struct drm_sched_backend_ops lima_sched_ops = {
357 .dependency = lima_sched_dependency,
358 .run_job = lima_sched_run_job,
359 .timedout_job = lima_sched_timedout_job,
360 .free_job = lima_sched_free_job,
363 static void lima_sched_error_work(struct work_struct *work)
365 struct lima_sched_pipe *pipe =
366 container_of(work, struct lima_sched_pipe, error_work);
367 struct lima_sched_task *task = pipe->current_task;
369 lima_sched_handle_error_task(pipe, task);
372 int lima_sched_pipe_init(struct lima_sched_pipe *pipe, const char *name)
376 if (lima_sched_timeout_ms <= 0)
377 timeout = MAX_SCHEDULE_TIMEOUT;
379 timeout = msecs_to_jiffies(lima_sched_timeout_ms);
381 pipe->fence_context = dma_fence_context_alloc(1);
382 spin_lock_init(&pipe->fence_lock);
384 INIT_WORK(&pipe->error_work, lima_sched_error_work);
386 return drm_sched_init(&pipe->base, &lima_sched_ops, 1, 0, timeout, name);
389 void lima_sched_pipe_fini(struct lima_sched_pipe *pipe)
391 drm_sched_fini(&pipe->base);
394 void lima_sched_pipe_task_done(struct lima_sched_pipe *pipe)
397 schedule_work(&pipe->error_work);
399 struct lima_sched_task *task = pipe->current_task;
401 pipe->task_fini(pipe);
402 dma_fence_signal(task->fence);