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