1*cc07d396Sriastradh /* $NetBSD: gpu_scheduler.h,v 1.4 2021/12/19 12:23:16 riastradh Exp $ */
24e390cabSriastradh
34e390cabSriastradh /*
44e390cabSriastradh * Copyright 2015 Advanced Micro Devices, Inc.
54e390cabSriastradh *
64e390cabSriastradh * Permission is hereby granted, free of charge, to any person obtaining a
74e390cabSriastradh * copy of this software and associated documentation files (the "Software"),
84e390cabSriastradh * to deal in the Software without restriction, including without limitation
94e390cabSriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
104e390cabSriastradh * and/or sell copies of the Software, and to permit persons to whom the
114e390cabSriastradh * Software is furnished to do so, subject to the following conditions:
124e390cabSriastradh *
134e390cabSriastradh * The above copyright notice and this permission notice shall be included in
144e390cabSriastradh * all copies or substantial portions of the Software.
154e390cabSriastradh *
164e390cabSriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
174e390cabSriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
184e390cabSriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
194e390cabSriastradh * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
204e390cabSriastradh * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
214e390cabSriastradh * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
224e390cabSriastradh * OTHER DEALINGS IN THE SOFTWARE.
234e390cabSriastradh *
244e390cabSriastradh */
254e390cabSriastradh
264e390cabSriastradh #ifndef _DRM_GPU_SCHEDULER_H_
274e390cabSriastradh #define _DRM_GPU_SCHEDULER_H_
284e390cabSriastradh
294e390cabSriastradh #include <drm/spsc_queue.h>
30e4a580baSriastradh #include <drm/drm_wait_netbsd.h>
314e390cabSriastradh #include <linux/dma-fence.h>
324e390cabSriastradh #include <linux/completion.h>
33e4a580baSriastradh #include <linux/workqueue.h>
344e390cabSriastradh
354e390cabSriastradh #define MAX_WAIT_SCHED_ENTITY_Q_EMPTY msecs_to_jiffies(1000)
364e390cabSriastradh
374e390cabSriastradh struct drm_gpu_scheduler;
384e390cabSriastradh struct drm_sched_rq;
394e390cabSriastradh
404e390cabSriastradh enum drm_sched_priority {
414e390cabSriastradh DRM_SCHED_PRIORITY_MIN,
424e390cabSriastradh DRM_SCHED_PRIORITY_LOW = DRM_SCHED_PRIORITY_MIN,
434e390cabSriastradh DRM_SCHED_PRIORITY_NORMAL,
444e390cabSriastradh DRM_SCHED_PRIORITY_HIGH_SW,
454e390cabSriastradh DRM_SCHED_PRIORITY_HIGH_HW,
464e390cabSriastradh DRM_SCHED_PRIORITY_KERNEL,
474e390cabSriastradh DRM_SCHED_PRIORITY_MAX,
484e390cabSriastradh DRM_SCHED_PRIORITY_INVALID = -1,
494e390cabSriastradh DRM_SCHED_PRIORITY_UNSET = -2
504e390cabSriastradh };
514e390cabSriastradh
524e390cabSriastradh /**
534e390cabSriastradh * struct drm_sched_entity - A wrapper around a job queue (typically
544e390cabSriastradh * attached to the DRM file_priv).
554e390cabSriastradh *
564e390cabSriastradh * @list: used to append this struct to the list of entities in the
574e390cabSriastradh * runqueue.
584e390cabSriastradh * @rq: runqueue on which this entity is currently scheduled.
594e390cabSriastradh * @sched_list: A list of schedulers (drm_gpu_schedulers).
604e390cabSriastradh * Jobs from this entity can be scheduled on any scheduler
614e390cabSriastradh * on this list.
624e390cabSriastradh * @num_sched_list: number of drm_gpu_schedulers in the sched_list.
634e390cabSriastradh * @rq_lock: lock to modify the runqueue to which this entity belongs.
644e390cabSriastradh * @job_queue: the list of jobs of this entity.
654e390cabSriastradh * @fence_seq: a linearly increasing seqno incremented with each
664e390cabSriastradh * new &drm_sched_fence which is part of the entity.
674e390cabSriastradh * @fence_context: a unique context for all the fences which belong
684e390cabSriastradh * to this entity.
694e390cabSriastradh * The &drm_sched_fence.scheduled uses the
704e390cabSriastradh * fence_context but &drm_sched_fence.finished uses
714e390cabSriastradh * fence_context + 1.
724e390cabSriastradh * @dependency: the dependency fence of the job which is on the top
734e390cabSriastradh * of the job queue.
744e390cabSriastradh * @cb: callback for the dependency fence above.
754e390cabSriastradh * @guilty: points to ctx's guilty.
764e390cabSriastradh * @fini_status: contains the exit status in case the process was signalled.
774e390cabSriastradh * @last_scheduled: points to the finished fence of the last scheduled job.
784e390cabSriastradh * @last_user: last group leader pushing a job into the entity.
794e390cabSriastradh * @stopped: Marks the enity as removed from rq and destined for termination.
804e390cabSriastradh * @entity_idle: Signals when enityt is not in use
814e390cabSriastradh *
824e390cabSriastradh * Entities will emit jobs in order to their corresponding hardware
834e390cabSriastradh * ring, and the scheduler will alternate between entities based on
844e390cabSriastradh * scheduling policy.
854e390cabSriastradh */
864e390cabSriastradh struct drm_sched_entity {
874e390cabSriastradh struct list_head list;
884e390cabSriastradh struct drm_sched_rq *rq;
894e390cabSriastradh struct drm_gpu_scheduler **sched_list;
904e390cabSriastradh unsigned int num_sched_list;
914e390cabSriastradh enum drm_sched_priority priority;
924e390cabSriastradh spinlock_t rq_lock;
934e390cabSriastradh
944e390cabSriastradh struct spsc_queue job_queue;
954e390cabSriastradh
964e390cabSriastradh atomic_t fence_seq;
974e390cabSriastradh uint64_t fence_context;
984e390cabSriastradh
994e390cabSriastradh struct dma_fence *dependency;
1004e390cabSriastradh struct dma_fence_cb cb;
1014e390cabSriastradh atomic_t *guilty;
1024e390cabSriastradh struct dma_fence *last_scheduled;
103*cc07d396Sriastradh #ifdef __NetBSD__
104*cc07d396Sriastradh struct proc *last_user;
105*cc07d396Sriastradh #else
1064e390cabSriastradh struct task_struct *last_user;
107*cc07d396Sriastradh #endif
1084e390cabSriastradh bool stopped;
1094e390cabSriastradh struct completion entity_idle;
1104e390cabSriastradh };
1114e390cabSriastradh
1124e390cabSriastradh /**
1134e390cabSriastradh * struct drm_sched_rq - queue of entities to be scheduled.
1144e390cabSriastradh *
1154e390cabSriastradh * @lock: to modify the entities list.
1164e390cabSriastradh * @sched: the scheduler to which this rq belongs to.
1174e390cabSriastradh * @entities: list of the entities to be scheduled.
1184e390cabSriastradh * @current_entity: the entity which is to be scheduled.
1194e390cabSriastradh *
1204e390cabSriastradh * Run queue is a set of entities scheduling command submissions for
1214e390cabSriastradh * one specific ring. It implements the scheduling policy that selects
1224e390cabSriastradh * the next entity to emit commands from.
1234e390cabSriastradh */
1244e390cabSriastradh struct drm_sched_rq {
1254e390cabSriastradh spinlock_t lock;
1264e390cabSriastradh struct drm_gpu_scheduler *sched;
1274e390cabSriastradh struct list_head entities;
1284e390cabSriastradh struct drm_sched_entity *current_entity;
1294e390cabSriastradh };
1304e390cabSriastradh
1314e390cabSriastradh /**
1324e390cabSriastradh * struct drm_sched_fence - fences corresponding to the scheduling of a job.
1334e390cabSriastradh */
1344e390cabSriastradh struct drm_sched_fence {
1354e390cabSriastradh /**
1364e390cabSriastradh * @scheduled: this fence is what will be signaled by the scheduler
1374e390cabSriastradh * when the job is scheduled.
1384e390cabSriastradh */
1394e390cabSriastradh struct dma_fence scheduled;
1404e390cabSriastradh
1414e390cabSriastradh /**
1424e390cabSriastradh * @finished: this fence is what will be signaled by the scheduler
1434e390cabSriastradh * when the job is completed.
1444e390cabSriastradh *
1454e390cabSriastradh * When setting up an out fence for the job, you should use
1464e390cabSriastradh * this, since it's available immediately upon
1474e390cabSriastradh * drm_sched_job_init(), and the fence returned by the driver
1484e390cabSriastradh * from run_job() won't be created until the dependencies have
1494e390cabSriastradh * resolved.
1504e390cabSriastradh */
1514e390cabSriastradh struct dma_fence finished;
1524e390cabSriastradh
1534e390cabSriastradh /**
1544e390cabSriastradh * @parent: the fence returned by &drm_sched_backend_ops.run_job
1554e390cabSriastradh * when scheduling the job on hardware. We signal the
1564e390cabSriastradh * &drm_sched_fence.finished fence once parent is signalled.
1574e390cabSriastradh */
1584e390cabSriastradh struct dma_fence *parent;
1594e390cabSriastradh /**
1604e390cabSriastradh * @sched: the scheduler instance to which the job having this struct
1614e390cabSriastradh * belongs to.
1624e390cabSriastradh */
1634e390cabSriastradh struct drm_gpu_scheduler *sched;
1644e390cabSriastradh /**
1654e390cabSriastradh * @lock: the lock used by the scheduled and the finished fences.
1664e390cabSriastradh */
1674e390cabSriastradh spinlock_t lock;
1684e390cabSriastradh /**
1694e390cabSriastradh * @owner: job owner for debugging
1704e390cabSriastradh */
1714e390cabSriastradh void *owner;
1724e390cabSriastradh };
1734e390cabSriastradh
1744e390cabSriastradh struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f);
1754e390cabSriastradh
1764e390cabSriastradh /**
1774e390cabSriastradh * struct drm_sched_job - A job to be run by an entity.
1784e390cabSriastradh *
1794e390cabSriastradh * @queue_node: used to append this struct to the queue of jobs in an entity.
1804e390cabSriastradh * @sched: the scheduler instance on which this job is scheduled.
1814e390cabSriastradh * @s_fence: contains the fences for the scheduling of job.
1824e390cabSriastradh * @finish_cb: the callback for the finished fence.
1834e390cabSriastradh * @node: used to append this struct to the @drm_gpu_scheduler.ring_mirror_list.
1844e390cabSriastradh * @id: a unique id assigned to each job scheduled on the scheduler.
1854e390cabSriastradh * @karma: increment on every hang caused by this job. If this exceeds the hang
1864e390cabSriastradh * limit of the scheduler then the job is marked guilty and will not
1874e390cabSriastradh * be scheduled further.
1884e390cabSriastradh * @s_priority: the priority of the job.
1894e390cabSriastradh * @entity: the entity to which this job belongs.
1904e390cabSriastradh * @cb: the callback for the parent fence in s_fence.
1914e390cabSriastradh *
1924e390cabSriastradh * A job is created by the driver using drm_sched_job_init(), and
1934e390cabSriastradh * should call drm_sched_entity_push_job() once it wants the scheduler
1944e390cabSriastradh * to schedule the job.
1954e390cabSriastradh */
1964e390cabSriastradh struct drm_sched_job {
1974e390cabSriastradh struct spsc_node queue_node;
1984e390cabSriastradh struct drm_gpu_scheduler *sched;
1994e390cabSriastradh struct drm_sched_fence *s_fence;
2004e390cabSriastradh struct dma_fence_cb finish_cb;
2014e390cabSriastradh struct list_head node;
2024e390cabSriastradh uint64_t id;
2034e390cabSriastradh atomic_t karma;
2044e390cabSriastradh enum drm_sched_priority s_priority;
2054e390cabSriastradh struct drm_sched_entity *entity;
2064e390cabSriastradh struct dma_fence_cb cb;
2074e390cabSriastradh };
2084e390cabSriastradh
drm_sched_invalidate_job(struct drm_sched_job * s_job,int threshold)2094e390cabSriastradh static inline bool drm_sched_invalidate_job(struct drm_sched_job *s_job,
2104e390cabSriastradh int threshold)
2114e390cabSriastradh {
2124e390cabSriastradh return (s_job && atomic_inc_return(&s_job->karma) > threshold);
2134e390cabSriastradh }
2144e390cabSriastradh
2154e390cabSriastradh /**
2164e390cabSriastradh * struct drm_sched_backend_ops
2174e390cabSriastradh *
2184e390cabSriastradh * Define the backend operations called by the scheduler,
2194e390cabSriastradh * these functions should be implemented in driver side.
2204e390cabSriastradh */
2214e390cabSriastradh struct drm_sched_backend_ops {
2224e390cabSriastradh /**
2234e390cabSriastradh * @dependency: Called when the scheduler is considering scheduling
2244e390cabSriastradh * this job next, to get another struct dma_fence for this job to
2254e390cabSriastradh * block on. Once it returns NULL, run_job() may be called.
2264e390cabSriastradh */
2274e390cabSriastradh struct dma_fence *(*dependency)(struct drm_sched_job *sched_job,
2284e390cabSriastradh struct drm_sched_entity *s_entity);
2294e390cabSriastradh
2304e390cabSriastradh /**
2314e390cabSriastradh * @run_job: Called to execute the job once all of the dependencies
2324e390cabSriastradh * have been resolved. This may be called multiple times, if
2334e390cabSriastradh * timedout_job() has happened and drm_sched_job_recovery()
2344e390cabSriastradh * decides to try it again.
2354e390cabSriastradh */
2364e390cabSriastradh struct dma_fence *(*run_job)(struct drm_sched_job *sched_job);
2374e390cabSriastradh
2384e390cabSriastradh /**
2394e390cabSriastradh * @timedout_job: Called when a job has taken too long to execute,
2404e390cabSriastradh * to trigger GPU recovery.
2414e390cabSriastradh */
2424e390cabSriastradh void (*timedout_job)(struct drm_sched_job *sched_job);
2434e390cabSriastradh
2444e390cabSriastradh /**
2454e390cabSriastradh * @free_job: Called once the job's finished fence has been signaled
2464e390cabSriastradh * and it's time to clean it up.
2474e390cabSriastradh */
2484e390cabSriastradh void (*free_job)(struct drm_sched_job *sched_job);
2494e390cabSriastradh };
2504e390cabSriastradh
2514e390cabSriastradh /**
2524e390cabSriastradh * struct drm_gpu_scheduler
2534e390cabSriastradh *
2544e390cabSriastradh * @ops: backend operations provided by the driver.
2554e390cabSriastradh * @hw_submission_limit: the max size of the hardware queue.
2564e390cabSriastradh * @timeout: the time after which a job is removed from the scheduler.
2574e390cabSriastradh * @name: name of the ring for which this scheduler is being used.
2584e390cabSriastradh * @sched_rq: priority wise array of run queues.
2594e390cabSriastradh * @wake_up_worker: the wait queue on which the scheduler sleeps until a job
2604e390cabSriastradh * is ready to be scheduled.
2614e390cabSriastradh * @job_scheduled: once @drm_sched_entity_do_release is called the scheduler
2624e390cabSriastradh * waits on this wait queue until all the scheduled jobs are
2634e390cabSriastradh * finished.
2644e390cabSriastradh * @hw_rq_count: the number of jobs currently in the hardware queue.
2654e390cabSriastradh * @job_id_count: used to assign unique id to the each job.
2664e390cabSriastradh * @work_tdr: schedules a delayed call to @drm_sched_job_timedout after the
2674e390cabSriastradh * timeout interval is over.
2684e390cabSriastradh * @thread: the kthread on which the scheduler which run.
2694e390cabSriastradh * @ring_mirror_list: the list of jobs which are currently in the job queue.
2704e390cabSriastradh * @job_list_lock: lock to protect the ring_mirror_list.
2714e390cabSriastradh * @hang_limit: once the hangs by a job crosses this limit then it is marked
2724e390cabSriastradh * guilty and it will be considered for scheduling further.
2734e390cabSriastradh * @score: score to help loadbalancer pick a idle sched
2744e390cabSriastradh * @ready: marks if the underlying HW is ready to work
2754e390cabSriastradh * @free_guilty: A hit to time out handler to free the guilty job.
2764e390cabSriastradh *
2774e390cabSriastradh * One scheduler is implemented for each hardware ring.
2784e390cabSriastradh */
2794e390cabSriastradh struct drm_gpu_scheduler {
2804e390cabSriastradh const struct drm_sched_backend_ops *ops;
2814e390cabSriastradh uint32_t hw_submission_limit;
2824e390cabSriastradh long timeout;
2834e390cabSriastradh const char *name;
2844e390cabSriastradh struct drm_sched_rq sched_rq[DRM_SCHED_PRIORITY_MAX];
285e4a580baSriastradh drm_waitqueue_t wake_up_worker;
286e4a580baSriastradh drm_waitqueue_t job_scheduled;
2874e390cabSriastradh atomic_t hw_rq_count;
2884e390cabSriastradh atomic64_t job_id_count;
2894e390cabSriastradh struct delayed_work work_tdr;
2904e390cabSriastradh struct task_struct *thread;
2914e390cabSriastradh struct list_head ring_mirror_list;
2924e390cabSriastradh spinlock_t job_list_lock;
2934e390cabSriastradh int hang_limit;
2944e390cabSriastradh atomic_t score;
2954e390cabSriastradh bool ready;
2964e390cabSriastradh bool free_guilty;
2974e390cabSriastradh };
2984e390cabSriastradh
2994e390cabSriastradh int drm_sched_init(struct drm_gpu_scheduler *sched,
3004e390cabSriastradh const struct drm_sched_backend_ops *ops,
3014e390cabSriastradh uint32_t hw_submission, unsigned hang_limit, long timeout,
3024e390cabSriastradh const char *name);
3034e390cabSriastradh
3044e390cabSriastradh void drm_sched_fini(struct drm_gpu_scheduler *sched);
3054e390cabSriastradh int drm_sched_job_init(struct drm_sched_job *job,
3064e390cabSriastradh struct drm_sched_entity *entity,
3074e390cabSriastradh void *owner);
3084e390cabSriastradh void drm_sched_job_cleanup(struct drm_sched_job *job);
3094e390cabSriastradh void drm_sched_wakeup(struct drm_gpu_scheduler *sched);
3104e390cabSriastradh void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad);
3114e390cabSriastradh void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery);
3124e390cabSriastradh void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched);
3134e390cabSriastradh void drm_sched_increase_karma(struct drm_sched_job *bad);
3144e390cabSriastradh bool drm_sched_dependency_optimized(struct dma_fence* fence,
3154e390cabSriastradh struct drm_sched_entity *entity);
3164e390cabSriastradh void drm_sched_fault(struct drm_gpu_scheduler *sched);
3174e390cabSriastradh void drm_sched_job_kickout(struct drm_sched_job *s_job);
3184e390cabSriastradh
3194e390cabSriastradh void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
3204e390cabSriastradh struct drm_sched_entity *entity);
3214e390cabSriastradh void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
3224e390cabSriastradh struct drm_sched_entity *entity);
3234e390cabSriastradh
3244e390cabSriastradh int drm_sched_entity_init(struct drm_sched_entity *entity,
3254e390cabSriastradh enum drm_sched_priority priority,
3264e390cabSriastradh struct drm_gpu_scheduler **sched_list,
3274e390cabSriastradh unsigned int num_sched_list,
3284e390cabSriastradh atomic_t *guilty);
3294e390cabSriastradh long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout);
3304e390cabSriastradh void drm_sched_entity_fini(struct drm_sched_entity *entity);
3314e390cabSriastradh void drm_sched_entity_destroy(struct drm_sched_entity *entity);
3324e390cabSriastradh void drm_sched_entity_select_rq(struct drm_sched_entity *entity);
3334e390cabSriastradh struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity);
3344e390cabSriastradh void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
3354e390cabSriastradh struct drm_sched_entity *entity);
3364e390cabSriastradh void drm_sched_entity_set_priority(struct drm_sched_entity *entity,
3374e390cabSriastradh enum drm_sched_priority priority);
3384e390cabSriastradh bool drm_sched_entity_is_ready(struct drm_sched_entity *entity);
3394e390cabSriastradh
3404e390cabSriastradh struct drm_sched_fence *drm_sched_fence_create(
3414e390cabSriastradh struct drm_sched_entity *s_entity, void *owner);
3424e390cabSriastradh void drm_sched_fence_scheduled(struct drm_sched_fence *fence);
3434e390cabSriastradh void drm_sched_fence_finished(struct drm_sched_fence *fence);
3444e390cabSriastradh
3454e390cabSriastradh unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched);
3464e390cabSriastradh void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
3474e390cabSriastradh unsigned long remaining);
3484e390cabSriastradh
3494e390cabSriastradh #endif
350