789 lines · 25.8 KB · C
RawDownload
/* * Copyright 2015 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */ #ifndef _DRM_GPU_SCHEDULER_H_#define _DRM_GPU_SCHEDULER_H_ #include <drm/spsc_queue.h>#include <linux/average.h>#include <linux/dma-fence.h>#include <linux/completion.h>#include <linux/xarray.h>#include <linux/workqueue.h> DECLARE_EWMA(drm_sched_avgtime, 6, 4); #define MAX_WAIT_SCHED_ENTITY_Q_EMPTY msecs_to_jiffies(1000) /** * DRM_SCHED_FENCE_DONT_PIPELINE - Prevent dependency pipelining * * Setting this flag on a scheduler fence prevents pipelining of jobs depending * on this fence. In other words we always insert a full CPU round trip before * dependent jobs are pushed to the hw queue. */#define DRM_SCHED_FENCE_DONT_PIPELINE	DMA_FENCE_FLAG_USER_BITS /** * DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT - A fence deadline hint has been set * * Because we could have a deadline hint can be set before the backing hw * fence is created, we need to keep track of whether a deadline has already * been set. */#define DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT	(DMA_FENCE_FLAG_USER_BITS + 1) enum dma_resv_usage;struct dma_resv;struct drm_gem_object; struct drm_gpu_scheduler;struct drm_sched_rq; struct drm_file; /* These are often used as an (initial) index * to an array, and as such should start at 0. */enum drm_sched_priority {	DRM_SCHED_PRIORITY_INVALID = -1, /* Internal marker - do not use. */	DRM_SCHED_PRIORITY_KERNEL,	DRM_SCHED_PRIORITY_HIGH,	DRM_SCHED_PRIORITY_NORMAL,	DRM_SCHED_PRIORITY_LOW, 	DRM_SCHED_PRIORITY_COUNT}; struct drm_sched_entity_stats; /** * struct drm_sched_entity - A wrapper around a job queue (typically * attached to the DRM file_priv). * * Entities will emit jobs in order to their corresponding hardware * ring, and the scheduler will alternate between entities based on * scheduling policy. */struct drm_sched_entity {	/**	 * @list:	 *	 * Used to append this struct to the list of entities in the runqueue	 * @rq under &drm_sched_rq.entities.	 *	 * Protected by &drm_sched_rq.lock of @rq.	 */	struct list_head		list; 	/**	 * @lock:	 *	 * Lock protecting the run-queue (@rq) to which this entity belongs,	 * @priority, the list of schedulers (@sched_list, @num_sched_list) and	 * the @rr_ts field.	 */	spinlock_t			lock; 	/**	 * @rq:	 *	 * Runqueue on which this entity is currently scheduled.	 *	 * FIXME: Locking is very unclear for this. Writers are protected by	 * @lock, but readers are generally lockless and seem to just race with	 * not even a READ_ONCE.	 */	struct drm_sched_rq		*rq; 	/**	 * @stats: Stats object reference held by the entity and jobs.	 */	struct drm_sched_entity_stats	*stats; 	/**	 * @sched_list:	 *	 * A list of schedulers (struct drm_gpu_scheduler).  Jobs from this entity can	 * be scheduled on any scheduler on this list.	 *	 * This can be modified by calling drm_sched_entity_modify_sched().	 * Locking is entirely up to the driver, see the above function for more	 * details.	 *	 * This will be set to NULL if &num_sched_list equals 1 and @rq has been	 * set already.	 *	 * FIXME: This means priority changes through	 * drm_sched_entity_set_priority() will be lost henceforth in this case.	 */	struct drm_gpu_scheduler        **sched_list; 	/**	 * @num_sched_list:	 *	 * Number of drm_gpu_schedulers in the @sched_list.	 */	unsigned int                    num_sched_list; 	/**	 * @priority:	 *	 * Priority of the entity. This can be modified by calling	 * drm_sched_entity_set_priority(). Protected by @lock.	 */	enum drm_sched_priority         priority; 	/**	 * @rq_priority: Run-queue priority	 */	enum drm_sched_priority         rq_priority; 	/**	 * @rr_ts:	 *	 * Fake timestamp of the last popped job from the entity.	 */	ktime_t				rr_ts; 	/**	 * @job_queue: the list of jobs of this entity.	 */	struct spsc_queue		job_queue; 	/**	 * @fence_seq:	 *	 * A linearly increasing seqno incremented with each new	 * &drm_sched_fence which is part of the entity.	 *	 * FIXME: Callers of drm_sched_job_arm() need to ensure correct locking,	 * this doesn't need to be atomic.	 */	atomic_t			fence_seq; 	/**	 * @fence_context:	 *	 * A unique context for all the fences which belong to this entity.  The	 * &drm_sched_fence.scheduled uses the fence_context but	 * &drm_sched_fence.finished uses fence_context + 1.	 */	uint64_t			fence_context; 	/**	 * @dependency:	 *	 * The dependency fence of the job which is on the top of the job queue.	 */	struct dma_fence		*dependency; 	/**	 * @cb:	 *	 * Callback for the dependency fence above.	 */	struct dma_fence_cb		cb; 	/**	 * @guilty:	 *	 * Points to entities' guilty.	 */	atomic_t			*guilty; 	/**	 * @last_scheduled:	 *	 * Points to the finished fence of the last scheduled job. Only written	 * by drm_sched_entity_pop_job(). Can be accessed locklessly from	 * drm_sched_job_arm() if the queue is empty.	 */	struct dma_fence __rcu		*last_scheduled; 	/**	 * @last_user: last group leader pushing a job into the entity.	 */	struct task_struct		*last_user; 	/**	 * @stopped:	 *	 * Marks the enity as removed from rq and destined for	 * termination. This is set by calling drm_sched_entity_flush().	 */	bool 				stopped; 	/**	 * @entity_idle:	 *	 * Signals when entity is not in use, used to sequence entity cleanup in	 * drm_sched_entity_fini().	 */	struct completion		entity_idle; 	/**	 * @oldest_job_waiting:	 *	 * Marks earliest job waiting in SW queue	 */	ktime_t				oldest_job_waiting; 	/**	 * @rb_tree_node:	 *	 * The node used to insert this entity into time based priority queue	 */	struct rb_node			rb_tree_node; }; /** * struct drm_sched_rq - queue of entities to be scheduled. * * @sched: the scheduler to which this rq belongs to. * @lock: protects @entities, @rb_tree_root, @rr_ts and @head_prio. * @rr_ts: monotonically incrementing fake timestamp for RR mode. * @entities: list of the entities to be scheduled. * @rb_tree_root: root of time based priority queue of entities for FIFO scheduling * @head_prio: priority of the top tree element. * * Run queue is a set of entities scheduling command submissions for * one specific ring. It implements the scheduling policy that selects * the next entity to emit commands from. */struct drm_sched_rq {	struct drm_gpu_scheduler	*sched; 	spinlock_t			lock;	/* Following members are protected by the @lock: */	ktime_t				rr_ts;	struct list_head		entities;	struct rb_root_cached		rb_tree_root;	enum drm_sched_priority		head_prio;}; /** * struct drm_sched_fence - fences corresponding to the scheduling of a job. */struct drm_sched_fence {        /**         * @scheduled: this fence is what will be signaled by the scheduler         * when the job is scheduled.         */	struct dma_fence		scheduled;         /**         * @finished: this fence is what will be signaled by the scheduler         * when the job is completed.         *         * When setting up an out fence for the job, you should use         * this, since it's available immediately upon         * drm_sched_job_init(), and the fence returned by the driver         * from run_job() won't be created until the dependencies have         * resolved.         */	struct dma_fence		finished; 	/**	 * @deadline: deadline set on &drm_sched_fence.finished which	 * potentially needs to be propagated to &drm_sched_fence.parent	 */	ktime_t				deadline;         /**         * @parent: the fence returned by &drm_sched_backend_ops.run_job         * when scheduling the job on hardware. We signal the         * &drm_sched_fence.finished fence once parent is signalled.         */	struct dma_fence		*parent;        /**         * @sched: the scheduler instance to which the job having this struct         * belongs to.         */	struct drm_gpu_scheduler	*sched;        /**         * @lock: the lock used by the scheduled and the finished fences.         */	spinlock_t			lock;        /**         * @owner: job owner for debugging         */	void				*owner; 	/**	 * @drm_client_id:	 *	 * The client_id of the drm_file which owns the job.	 */	uint64_t			drm_client_id;}; struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f); /** * struct drm_sched_job - A job to be run by an entity. * * @queue_node: used to append this struct to the queue of jobs in an entity. * @list: a job participates in a "pending" and "done" lists. * @sched: the scheduler instance on which this job is scheduled. * @s_fence: contains the fences for the scheduling of job. * @finish_cb: the callback for the finished fence. * @credits: the number of credits this job contributes to the scheduler * @work: Helper to reschedule job kill to different context. * @karma: increment on every hang caused by this job. If this exceeds the hang *         limit of the scheduler then the job is marked guilty and will not *         be scheduled further. * @s_priority: the priority of the job. * @entity: the entity to which this job belongs. * @cb: the callback for the parent fence in s_fence. * * A job is created by the driver using drm_sched_job_init(), and * should call drm_sched_entity_push_job() once it wants the scheduler * to schedule the job. */struct drm_sched_job {	/**	 * @submit_ts:	 *	 * When the job was pushed into the entity queue.	 */	ktime_t                         submit_ts; 	/**	 * @sched:	 *	 * The scheduler this job is or will be scheduled on. Gets set by	 * drm_sched_job_arm(). Valid until drm_sched_backend_ops.free_job()	 * has finished.	 */	struct drm_gpu_scheduler	*sched; 	struct drm_sched_fence		*s_fence;	struct drm_sched_entity         *entity; 	/**	 * @entity_stats: Stats object reference held by the job and entity.	 */	struct drm_sched_entity_stats	*entity_stats; 	enum drm_sched_priority		s_priority;	u32				credits;	/** @last_dependency: tracks @dependencies as they signal */	unsigned int			last_dependency;	atomic_t			karma; 	struct spsc_node		queue_node;	struct list_head		list; 	/*	 * work is used only after finish_cb has been used and will not be	 * accessed anymore.	 */	union {		struct dma_fence_cb	finish_cb;		struct work_struct	work;	}; 	struct dma_fence_cb		cb; 	/**	 * @dependencies:	 *	 * Contains the dependencies as struct dma_fence for this job, see	 * drm_sched_job_add_dependency() and	 * drm_sched_job_add_implicit_dependencies().	 */	struct xarray			dependencies;}; /** * enum drm_gpu_sched_stat - the scheduler's status * * @DRM_GPU_SCHED_STAT_NONE: Reserved. Do not use. * @DRM_GPU_SCHED_STAT_RESET: The GPU hung and successfully reset. * @DRM_GPU_SCHED_STAT_ENODEV: Error: Device is not available anymore. * @DRM_GPU_SCHED_STAT_NO_HANG: Contrary to scheduler's assumption, the GPU * did not hang and is still running. */enum drm_gpu_sched_stat {	DRM_GPU_SCHED_STAT_NONE,	DRM_GPU_SCHED_STAT_RESET,	DRM_GPU_SCHED_STAT_ENODEV,	DRM_GPU_SCHED_STAT_NO_HANG,}; /** * struct drm_sched_backend_ops - Define the backend operations *	called by the scheduler * * These functions should be implemented in the driver side. */struct drm_sched_backend_ops {	/**	 * @prepare_job:	 *	 * Called when the scheduler is considering scheduling this job next, to	 * get another struct dma_fence for this job to block on.  Once it	 * returns NULL, run_job() may be called.	 *	 * Can be NULL if no additional preparation to the dependencies are	 * necessary. Skipped when jobs are killed instead of run.	 */	struct dma_fence *(*prepare_job)(struct drm_sched_job *sched_job,					 struct drm_sched_entity *s_entity); 	/**	 * @run_job: Called to execute the job once all of the dependencies	 * have been resolved.	 *	 * @sched_job: the job to run	 *	 * The deprecated drm_sched_resubmit_jobs() (called by &struct	 * drm_sched_backend_ops.timedout_job) can invoke this again with the	 * same parameters. Using this is discouraged because it violates	 * dma_fence rules, notably dma_fence_init() has to be called on	 * already initialized fences for a second time. Moreover, this is	 * dangerous because attempts to allocate memory might deadlock with	 * memory management code waiting for the reset to complete.	 *	 * TODO: Document what drivers should do / use instead.	 *	 * This method is called in a workqueue context - either from the	 * submit_wq the driver passed through drm_sched_init(), or, if the	 * driver passed NULL, a separate, ordered workqueue the scheduler	 * allocated.	 *	 * Note that the scheduler expects to 'inherit' its own reference to	 * this fence from the callback. It does not invoke an extra	 * dma_fence_get() on it. Consequently, this callback must take a	 * reference for the scheduler, and additional ones for the driver's	 * respective needs.	 *	 * Return:	 * * On success: dma_fence the driver must signal once the hardware has	 * completed the job ("hardware fence").	 * * On failure: NULL or an ERR_PTR.	 */	struct dma_fence *(*run_job)(struct drm_sched_job *sched_job); 	/**	 * @timedout_job: Called when a job has taken too long to execute,	 * to trigger GPU recovery.	 *	 * @sched_job: The job that has timed out	 *	 * Drivers typically issue a reset to recover from GPU hangs.	 * This procedure looks very different depending on whether a firmware	 * or a hardware scheduler is being used.	 *	 * For a FIRMWARE SCHEDULER, each ring has one scheduler, and each

Lines 1–500 of 789.