xref: /linux-6.15/drivers/gpu/drm/msm/msm_gpu.h (revision 15c41198)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <[email protected]>
5  */
6 
7 #ifndef __MSM_GPU_H__
8 #define __MSM_GPU_H__
9 
10 #include <linux/adreno-smmu-priv.h>
11 #include <linux/clk.h>
12 #include <linux/interconnect.h>
13 #include <linux/pm_opp.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include "msm_drv.h"
17 #include "msm_fence.h"
18 #include "msm_ringbuffer.h"
19 #include "msm_gem.h"
20 
21 struct msm_gem_submit;
22 struct msm_gpu_perfcntr;
23 struct msm_gpu_state;
24 struct msm_file_private;
25 
26 struct msm_gpu_config {
27 	const char *ioname;
28 	unsigned int nr_rings;
29 };
30 
31 /* So far, with hardware that I've seen to date, we can have:
32  *  + zero, one, or two z180 2d cores
33  *  + a3xx or a2xx 3d core, which share a common CP (the firmware
34  *    for the CP seems to implement some different PM4 packet types
35  *    but the basics of cmdstream submission are the same)
36  *
37  * Which means that the eventual complete "class" hierarchy, once
38  * support for all past and present hw is in place, becomes:
39  *  + msm_gpu
40  *    + adreno_gpu
41  *      + a3xx_gpu
42  *      + a2xx_gpu
43  *    + z180_gpu
44  */
45 struct msm_gpu_funcs {
46 	int (*get_param)(struct msm_gpu *gpu, struct msm_file_private *ctx,
47 			 uint32_t param, uint64_t *value, uint32_t *len);
48 	int (*set_param)(struct msm_gpu *gpu, struct msm_file_private *ctx,
49 			 uint32_t param, uint64_t value, uint32_t len);
50 	int (*hw_init)(struct msm_gpu *gpu);
51 	int (*pm_suspend)(struct msm_gpu *gpu);
52 	int (*pm_resume)(struct msm_gpu *gpu);
53 	void (*submit)(struct msm_gpu *gpu, struct msm_gem_submit *submit);
54 	void (*flush)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
55 	irqreturn_t (*irq)(struct msm_gpu *irq);
56 	struct msm_ringbuffer *(*active_ring)(struct msm_gpu *gpu);
57 	void (*recover)(struct msm_gpu *gpu);
58 	void (*destroy)(struct msm_gpu *gpu);
59 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
60 	/* show GPU status in debugfs: */
61 	void (*show)(struct msm_gpu *gpu, struct msm_gpu_state *state,
62 			struct drm_printer *p);
63 	/* for generation specific debugfs: */
64 	void (*debugfs_init)(struct msm_gpu *gpu, struct drm_minor *minor);
65 #endif
66 	u64 (*gpu_busy)(struct msm_gpu *gpu, unsigned long *out_sample_rate);
67 	struct msm_gpu_state *(*gpu_state_get)(struct msm_gpu *gpu);
68 	int (*gpu_state_put)(struct msm_gpu_state *state);
69 	unsigned long (*gpu_get_freq)(struct msm_gpu *gpu);
70 	void (*gpu_set_freq)(struct msm_gpu *gpu, struct dev_pm_opp *opp);
71 	struct msm_gem_address_space *(*create_address_space)
72 		(struct msm_gpu *gpu, struct platform_device *pdev);
73 	struct msm_gem_address_space *(*create_private_address_space)
74 		(struct msm_gpu *gpu);
75 	uint32_t (*get_rptr)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
76 };
77 
78 /* Additional state for iommu faults: */
79 struct msm_gpu_fault_info {
80 	u64 ttbr0;
81 	unsigned long iova;
82 	int flags;
83 	const char *type;
84 	const char *block;
85 };
86 
87 /**
88  * struct msm_gpu_devfreq - devfreq related state
89  */
90 struct msm_gpu_devfreq {
91 	/** devfreq: devfreq instance */
92 	struct devfreq *devfreq;
93 
94 	/**
95 	 * idle_constraint:
96 	 *
97 	 * A PM QoS constraint to limit max freq while the GPU is idle.
98 	 */
99 	struct dev_pm_qos_request idle_freq;
100 
101 	/**
102 	 * boost_constraint:
103 	 *
104 	 * A PM QoS constraint to boost min freq for a period of time
105 	 * until the boost expires.
106 	 */
107 	struct dev_pm_qos_request boost_freq;
108 
109 	/**
110 	 * busy_cycles: Last busy counter value, for calculating elapsed busy
111 	 * cycles since last sampling period.
112 	 */
113 	u64 busy_cycles;
114 
115 	/** time: Time of last sampling period. */
116 	ktime_t time;
117 
118 	/** idle_time: Time of last transition to idle: */
119 	ktime_t idle_time;
120 
121 	/**
122 	 * idle_work:
123 	 *
124 	 * Used to delay clamping to idle freq on active->idle transition.
125 	 */
126 	struct msm_hrtimer_work idle_work;
127 
128 	/**
129 	 * boost_work:
130 	 *
131 	 * Used to reset the boost_constraint after the boost period has
132 	 * elapsed
133 	 */
134 	struct msm_hrtimer_work boost_work;
135 };
136 
137 struct msm_gpu {
138 	const char *name;
139 	struct drm_device *dev;
140 	struct platform_device *pdev;
141 	const struct msm_gpu_funcs *funcs;
142 
143 	struct adreno_smmu_priv adreno_smmu;
144 
145 	/* performance counters (hw & sw): */
146 	spinlock_t perf_lock;
147 	bool perfcntr_active;
148 	struct {
149 		bool active;
150 		ktime_t time;
151 	} last_sample;
152 	uint32_t totaltime, activetime;    /* sw counters */
153 	uint32_t last_cntrs[5];            /* hw counters */
154 	const struct msm_gpu_perfcntr *perfcntrs;
155 	uint32_t num_perfcntrs;
156 
157 	struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
158 	int nr_rings;
159 
160 	/**
161 	 * sysprof_active:
162 	 *
163 	 * The count of contexts that have enabled system profiling.
164 	 */
165 	refcount_t sysprof_active;
166 
167 	/**
168 	 * cur_ctx_seqno:
169 	 *
170 	 * The ctx->seqno value of the last context to submit rendering,
171 	 * and the one with current pgtables installed (for generations
172 	 * that support per-context pgtables).  Tracked by seqno rather
173 	 * than pointer value to avoid dangling pointers, and cases where
174 	 * a ctx can be freed and a new one created with the same address.
175 	 */
176 	int cur_ctx_seqno;
177 
178 	/*
179 	 * List of GEM active objects on this gpu.  Protected by
180 	 * msm_drm_private::mm_lock
181 	 */
182 	struct list_head active_list;
183 
184 	/**
185 	 * lock:
186 	 *
187 	 * General lock for serializing all the gpu things.
188 	 *
189 	 * TODO move to per-ring locking where feasible (ie. submit/retire
190 	 * path, etc)
191 	 */
192 	struct mutex lock;
193 
194 	/**
195 	 * active_submits:
196 	 *
197 	 * The number of submitted but not yet retired submits, used to
198 	 * determine transitions between active and idle.
199 	 *
200 	 * Protected by active_lock
201 	 */
202 	int active_submits;
203 
204 	/** lock: protects active_submits and idle/active transitions */
205 	struct mutex active_lock;
206 
207 	/* does gpu need hw_init? */
208 	bool needs_hw_init;
209 
210 	/**
211 	 * global_faults: number of GPU hangs not attributed to a particular
212 	 * address space
213 	 */
214 	int global_faults;
215 
216 	void __iomem *mmio;
217 	int irq;
218 
219 	struct msm_gem_address_space *aspace;
220 
221 	/* Power Control: */
222 	struct regulator *gpu_reg, *gpu_cx;
223 	struct clk_bulk_data *grp_clks;
224 	int nr_clocks;
225 	struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
226 	uint32_t fast_rate;
227 
228 	/* Hang and Inactivity Detection:
229 	 */
230 #define DRM_MSM_INACTIVE_PERIOD   66 /* in ms (roughly four frames) */
231 
232 #define DRM_MSM_HANGCHECK_DEFAULT_PERIOD 500 /* in ms */
233 	struct timer_list hangcheck_timer;
234 
235 	/* Fault info for most recent iova fault: */
236 	struct msm_gpu_fault_info fault_info;
237 
238 	/* work for handling GPU ioval faults: */
239 	struct kthread_work fault_work;
240 
241 	/* work for handling GPU recovery: */
242 	struct kthread_work recover_work;
243 
244 	/** retire_event: notified when submits are retired: */
245 	wait_queue_head_t retire_event;
246 
247 	/* work for handling active-list retiring: */
248 	struct kthread_work retire_work;
249 
250 	/* worker for retire/recover: */
251 	struct kthread_worker *worker;
252 
253 	struct drm_gem_object *memptrs_bo;
254 
255 	struct msm_gpu_devfreq devfreq;
256 
257 	uint32_t suspend_count;
258 
259 	struct msm_gpu_state *crashstate;
260 
261 	/* Enable clamping to idle freq when inactive: */
262 	bool clamp_to_idle;
263 
264 	/* True if the hardware supports expanded apriv (a650 and newer) */
265 	bool hw_apriv;
266 
267 	struct thermal_cooling_device *cooling;
268 };
269 
270 static inline struct msm_gpu *dev_to_gpu(struct device *dev)
271 {
272 	struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
273 	return container_of(adreno_smmu, struct msm_gpu, adreno_smmu);
274 }
275 
276 /* It turns out that all targets use the same ringbuffer size */
277 #define MSM_GPU_RINGBUFFER_SZ SZ_32K
278 #define MSM_GPU_RINGBUFFER_BLKSIZE 32
279 
280 #define MSM_GPU_RB_CNTL_DEFAULT \
281 		(AXXX_CP_RB_CNTL_BUFSZ(ilog2(MSM_GPU_RINGBUFFER_SZ / 8)) | \
282 		AXXX_CP_RB_CNTL_BLKSZ(ilog2(MSM_GPU_RINGBUFFER_BLKSIZE / 8)))
283 
284 static inline bool msm_gpu_active(struct msm_gpu *gpu)
285 {
286 	int i;
287 
288 	for (i = 0; i < gpu->nr_rings; i++) {
289 		struct msm_ringbuffer *ring = gpu->rb[i];
290 
291 		if (fence_after(ring->fctx->last_fence, ring->memptrs->fence))
292 			return true;
293 	}
294 
295 	return false;
296 }
297 
298 /* Perf-Counters:
299  * The select_reg and select_val are just there for the benefit of the child
300  * class that actually enables the perf counter..  but msm_gpu base class
301  * will handle sampling/displaying the counters.
302  */
303 
304 struct msm_gpu_perfcntr {
305 	uint32_t select_reg;
306 	uint32_t sample_reg;
307 	uint32_t select_val;
308 	const char *name;
309 };
310 
311 /*
312  * The number of priority levels provided by drm gpu scheduler.  The
313  * DRM_SCHED_PRIORITY_KERNEL priority level is treated specially in some
314  * cases, so we don't use it (no need for kernel generated jobs).
315  */
316 #define NR_SCHED_PRIORITIES (1 + DRM_SCHED_PRIORITY_HIGH - DRM_SCHED_PRIORITY_MIN)
317 
318 /**
319  * struct msm_file_private - per-drm_file context
320  *
321  * @queuelock:    synchronizes access to submitqueues list
322  * @submitqueues: list of &msm_gpu_submitqueue created by userspace
323  * @queueid:      counter incremented each time a submitqueue is created,
324  *                used to assign &msm_gpu_submitqueue.id
325  * @aspace:       the per-process GPU address-space
326  * @ref:          reference count
327  * @seqno:        unique per process seqno
328  */
329 struct msm_file_private {
330 	rwlock_t queuelock;
331 	struct list_head submitqueues;
332 	int queueid;
333 	struct msm_gem_address_space *aspace;
334 	struct kref ref;
335 	int seqno;
336 
337 	/**
338 	 * sysprof:
339 	 *
340 	 * The value of MSM_PARAM_SYSPROF set by userspace.  This is
341 	 * intended to be used by system profiling tools like Mesa's
342 	 * pps-producer (perfetto), and restricted to CAP_SYS_ADMIN.
343 	 *
344 	 * Setting a value of 1 will preserve performance counters across
345 	 * context switches.  Setting a value of 2 will in addition
346 	 * suppress suspend.  (Performance counters lose state across
347 	 * power collapse, which is undesirable for profiling in some
348 	 * cases.)
349 	 *
350 	 * The value automatically reverts to zero when the drm device
351 	 * file is closed.
352 	 */
353 	int sysprof;
354 
355 	/** comm: Overridden task comm, see MSM_PARAM_COMM */
356 	char *comm;
357 
358 	/** cmdline: Overridden task cmdline, see MSM_PARAM_CMDLINE */
359 	char *cmdline;
360 
361 	/**
362 	 * entities:
363 	 *
364 	 * Table of per-priority-level sched entities used by submitqueues
365 	 * associated with this &drm_file.  Because some userspace apps
366 	 * make assumptions about rendering from multiple gl contexts
367 	 * (of the same priority) within the process happening in FIFO
368 	 * order without requiring any fencing beyond MakeCurrent(), we
369 	 * create at most one &drm_sched_entity per-process per-priority-
370 	 * level.
371 	 */
372 	struct drm_sched_entity *entities[NR_SCHED_PRIORITIES * MSM_GPU_MAX_RINGS];
373 };
374 
375 /**
376  * msm_gpu_convert_priority - Map userspace priority to ring # and sched priority
377  *
378  * @gpu:        the gpu instance
379  * @prio:       the userspace priority level
380  * @ring_nr:    [out] the ringbuffer the userspace priority maps to
381  * @sched_prio: [out] the gpu scheduler priority level which the userspace
382  *              priority maps to
383  *
384  * With drm/scheduler providing it's own level of prioritization, our total
385  * number of available priority levels is (nr_rings * NR_SCHED_PRIORITIES).
386  * Each ring is associated with it's own scheduler instance.  However, our
387  * UABI is that lower numerical values are higher priority.  So mapping the
388  * single userspace priority level into ring_nr and sched_prio takes some
389  * care.  The userspace provided priority (when a submitqueue is created)
390  * is mapped to ring nr and scheduler priority as such:
391  *
392  *   ring_nr    = userspace_prio / NR_SCHED_PRIORITIES
393  *   sched_prio = NR_SCHED_PRIORITIES -
394  *                (userspace_prio % NR_SCHED_PRIORITIES) - 1
395  *
396  * This allows generations without preemption (nr_rings==1) to have some
397  * amount of prioritization, and provides more priority levels for gens
398  * that do have preemption.
399  */
400 static inline int msm_gpu_convert_priority(struct msm_gpu *gpu, int prio,
401 		unsigned *ring_nr, enum drm_sched_priority *sched_prio)
402 {
403 	unsigned rn, sp;
404 
405 	rn = div_u64_rem(prio, NR_SCHED_PRIORITIES, &sp);
406 
407 	/* invert sched priority to map to higher-numeric-is-higher-
408 	 * priority convention
409 	 */
410 	sp = NR_SCHED_PRIORITIES - sp - 1;
411 
412 	if (rn >= gpu->nr_rings)
413 		return -EINVAL;
414 
415 	*ring_nr = rn;
416 	*sched_prio = sp;
417 
418 	return 0;
419 }
420 
421 /**
422  * struct msm_gpu_submitqueues - Userspace created context.
423  *
424  * A submitqueue is associated with a gl context or vk queue (or equiv)
425  * in userspace.
426  *
427  * @id:        userspace id for the submitqueue, unique within the drm_file
428  * @flags:     userspace flags for the submitqueue, specified at creation
429  *             (currently unusued)
430  * @ring_nr:   the ringbuffer used by this submitqueue, which is determined
431  *             by the submitqueue's priority
432  * @faults:    the number of GPU hangs associated with this submitqueue
433  * @last_fence: the sequence number of the last allocated fence (for error
434  *             checking)
435  * @ctx:       the per-drm_file context associated with the submitqueue (ie.
436  *             which set of pgtables do submits jobs associated with the
437  *             submitqueue use)
438  * @node:      node in the context's list of submitqueues
439  * @fence_idr: maps fence-id to dma_fence for userspace visible fence
440  *             seqno, protected by submitqueue lock
441  * @lock:      submitqueue lock
442  * @ref:       reference count
443  * @entity:    the submit job-queue
444  */
445 struct msm_gpu_submitqueue {
446 	int id;
447 	u32 flags;
448 	u32 ring_nr;
449 	int faults;
450 	uint32_t last_fence;
451 	struct msm_file_private *ctx;
452 	struct list_head node;
453 	struct idr fence_idr;
454 	struct mutex lock;
455 	struct kref ref;
456 	struct drm_sched_entity *entity;
457 };
458 
459 struct msm_gpu_state_bo {
460 	u64 iova;
461 	size_t size;
462 	void *data;
463 	bool encoded;
464 };
465 
466 struct msm_gpu_state {
467 	struct kref ref;
468 	struct timespec64 time;
469 
470 	struct {
471 		u64 iova;
472 		u32 fence;
473 		u32 seqno;
474 		u32 rptr;
475 		u32 wptr;
476 		void *data;
477 		int data_size;
478 		bool encoded;
479 	} ring[MSM_GPU_MAX_RINGS];
480 
481 	int nr_registers;
482 	u32 *registers;
483 
484 	u32 rbbm_status;
485 
486 	char *comm;
487 	char *cmd;
488 
489 	struct msm_gpu_fault_info fault_info;
490 
491 	int nr_bos;
492 	struct msm_gpu_state_bo *bos;
493 };
494 
495 static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
496 {
497 	msm_writel(data, gpu->mmio + (reg << 2));
498 }
499 
500 static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
501 {
502 	return msm_readl(gpu->mmio + (reg << 2));
503 }
504 
505 static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
506 {
507 	msm_rmw(gpu->mmio + (reg << 2), mask, or);
508 }
509 
510 static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
511 {
512 	u64 val;
513 
514 	/*
515 	 * Why not a readq here? Two reasons: 1) many of the LO registers are
516 	 * not quad word aligned and 2) the GPU hardware designers have a bit
517 	 * of a history of putting registers where they fit, especially in
518 	 * spins. The longer a GPU family goes the higher the chance that
519 	 * we'll get burned.  We could do a series of validity checks if we
520 	 * wanted to, but really is a readq() that much better? Nah.
521 	 */
522 
523 	/*
524 	 * For some lo/hi registers (like perfcounters), the hi value is latched
525 	 * when the lo is read, so make sure to read the lo first to trigger
526 	 * that
527 	 */
528 	val = (u64) msm_readl(gpu->mmio + (lo << 2));
529 	val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
530 
531 	return val;
532 }
533 
534 static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
535 {
536 	/* Why not a writeq here? Read the screed above */
537 	msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
538 	msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
539 }
540 
541 int msm_gpu_pm_suspend(struct msm_gpu *gpu);
542 int msm_gpu_pm_resume(struct msm_gpu *gpu);
543 
544 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx);
545 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
546 		u32 id);
547 int msm_submitqueue_create(struct drm_device *drm,
548 		struct msm_file_private *ctx,
549 		u32 prio, u32 flags, u32 *id);
550 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
551 		struct drm_msm_submitqueue_query *args);
552 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id);
553 void msm_submitqueue_close(struct msm_file_private *ctx);
554 
555 void msm_submitqueue_destroy(struct kref *kref);
556 
557 int msm_file_private_set_sysprof(struct msm_file_private *ctx,
558 				 struct msm_gpu *gpu, int sysprof);
559 void __msm_file_private_destroy(struct kref *kref);
560 
561 static inline void msm_file_private_put(struct msm_file_private *ctx)
562 {
563 	kref_put(&ctx->ref, __msm_file_private_destroy);
564 }
565 
566 static inline struct msm_file_private *msm_file_private_get(
567 	struct msm_file_private *ctx)
568 {
569 	kref_get(&ctx->ref);
570 	return ctx;
571 }
572 
573 void msm_devfreq_init(struct msm_gpu *gpu);
574 void msm_devfreq_cleanup(struct msm_gpu *gpu);
575 void msm_devfreq_resume(struct msm_gpu *gpu);
576 void msm_devfreq_suspend(struct msm_gpu *gpu);
577 void msm_devfreq_boost(struct msm_gpu *gpu, unsigned factor);
578 void msm_devfreq_active(struct msm_gpu *gpu);
579 void msm_devfreq_idle(struct msm_gpu *gpu);
580 
581 int msm_gpu_hw_init(struct msm_gpu *gpu);
582 
583 void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
584 void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
585 int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
586 		uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
587 
588 void msm_gpu_retire(struct msm_gpu *gpu);
589 void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit);
590 
591 int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
592 		struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
593 		const char *name, struct msm_gpu_config *config);
594 
595 struct msm_gem_address_space *
596 msm_gpu_create_private_address_space(struct msm_gpu *gpu, struct task_struct *task);
597 
598 void msm_gpu_cleanup(struct msm_gpu *gpu);
599 
600 struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
601 void __init adreno_register(void);
602 void __exit adreno_unregister(void);
603 
604 static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
605 {
606 	if (queue)
607 		kref_put(&queue->ref, msm_submitqueue_destroy);
608 }
609 
610 static inline struct msm_gpu_state *msm_gpu_crashstate_get(struct msm_gpu *gpu)
611 {
612 	struct msm_gpu_state *state = NULL;
613 
614 	mutex_lock(&gpu->lock);
615 
616 	if (gpu->crashstate) {
617 		kref_get(&gpu->crashstate->ref);
618 		state = gpu->crashstate;
619 	}
620 
621 	mutex_unlock(&gpu->lock);
622 
623 	return state;
624 }
625 
626 static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
627 {
628 	mutex_lock(&gpu->lock);
629 
630 	if (gpu->crashstate) {
631 		if (gpu->funcs->gpu_state_put(gpu->crashstate))
632 			gpu->crashstate = NULL;
633 	}
634 
635 	mutex_unlock(&gpu->lock);
636 }
637 
638 /*
639  * Simple macro to semi-cleanly add the MAP_PRIV flag for targets that can
640  * support expanded privileges
641  */
642 #define check_apriv(gpu, flags) \
643 	(((gpu)->hw_apriv ? MSM_BO_MAP_PRIV : 0) | (flags))
644 
645 
646 #endif /* __MSM_GPU_H__ */
647