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