1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
10 #include <linux/adreno-smmu-priv.h>
11 #include <linux/clk.h>
12 #include <linux/devfreq.h>
13 #include <linux/interconnect.h>
14 #include <linux/pm_opp.h>
15 #include <linux/regulator/consumer.h>
18 #include "msm_fence.h"
19 #include "msm_ringbuffer.h"
22 struct msm_gem_submit;
23 struct msm_gpu_perfcntr;
25 struct msm_file_private;
27 struct msm_gpu_config {
29 unsigned int nr_rings;
32 /* So far, with hardware that I've seen to date, we can have:
33 * + zero, one, or two z180 2d cores
34 * + a3xx or a2xx 3d core, which share a common CP (the firmware
35 * for the CP seems to implement some different PM4 packet types
36 * but the basics of cmdstream submission are the same)
38 * Which means that the eventual complete "class" hierarchy, once
39 * support for all past and present hw is in place, becomes:
46 struct msm_gpu_funcs {
47 int (*get_param)(struct msm_gpu *gpu, struct msm_file_private *ctx,
48 uint32_t param, uint64_t *value, uint32_t *len);
49 int (*set_param)(struct msm_gpu *gpu, struct msm_file_private *ctx,
50 uint32_t param, uint64_t value, uint32_t len);
51 int (*hw_init)(struct msm_gpu *gpu);
52 int (*pm_suspend)(struct msm_gpu *gpu);
53 int (*pm_resume)(struct msm_gpu *gpu);
54 void (*submit)(struct msm_gpu *gpu, struct msm_gem_submit *submit);
55 void (*flush)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
56 irqreturn_t (*irq)(struct msm_gpu *irq);
57 struct msm_ringbuffer *(*active_ring)(struct msm_gpu *gpu);
58 void (*recover)(struct msm_gpu *gpu);
59 void (*destroy)(struct msm_gpu *gpu);
60 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
61 /* show GPU status in debugfs: */
62 void (*show)(struct msm_gpu *gpu, struct msm_gpu_state *state,
63 struct drm_printer *p);
64 /* for generation specific debugfs: */
65 void (*debugfs_init)(struct msm_gpu *gpu, struct drm_minor *minor);
67 u64 (*gpu_busy)(struct msm_gpu *gpu, unsigned long *out_sample_rate);
68 struct msm_gpu_state *(*gpu_state_get)(struct msm_gpu *gpu);
69 int (*gpu_state_put)(struct msm_gpu_state *state);
70 unsigned long (*gpu_get_freq)(struct msm_gpu *gpu);
71 void (*gpu_set_freq)(struct msm_gpu *gpu, struct dev_pm_opp *opp);
72 struct msm_gem_address_space *(*create_address_space)
73 (struct msm_gpu *gpu, struct platform_device *pdev);
74 struct msm_gem_address_space *(*create_private_address_space)
75 (struct msm_gpu *gpu);
76 uint32_t (*get_rptr)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
79 /* Additional state for iommu faults: */
80 struct msm_gpu_fault_info {
89 * struct msm_gpu_devfreq - devfreq related state
91 struct msm_gpu_devfreq {
92 /** devfreq: devfreq instance */
93 struct devfreq *devfreq;
98 * A PM QoS constraint to limit max freq while the GPU is idle.
100 struct dev_pm_qos_request idle_freq;
105 * A PM QoS constraint to boost min freq for a period of time
106 * until the boost expires.
108 struct dev_pm_qos_request boost_freq;
111 * busy_cycles: Last busy counter value, for calculating elapsed busy
112 * cycles since last sampling period.
116 /** time: Time of last sampling period. */
119 /** idle_time: Time of last transition to idle: */
122 struct devfreq_dev_status average_status;
127 * Used to delay clamping to idle freq on active->idle transition.
129 struct msm_hrtimer_work idle_work;
134 * Used to reset the boost_constraint after the boost period has
137 struct msm_hrtimer_work boost_work;
142 struct drm_device *dev;
143 struct platform_device *pdev;
144 const struct msm_gpu_funcs *funcs;
146 struct adreno_smmu_priv adreno_smmu;
148 /* performance counters (hw & sw): */
149 spinlock_t perf_lock;
150 bool perfcntr_active;
155 uint32_t totaltime, activetime; /* sw counters */
156 uint32_t last_cntrs[5]; /* hw counters */
157 const struct msm_gpu_perfcntr *perfcntrs;
158 uint32_t num_perfcntrs;
160 struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
166 * The count of contexts that have enabled system profiling.
168 refcount_t sysprof_active;
173 * The ctx->seqno value of the last context to submit rendering,
174 * and the one with current pgtables installed (for generations
175 * that support per-context pgtables). Tracked by seqno rather
176 * than pointer value to avoid dangling pointers, and cases where
177 * a ctx can be freed and a new one created with the same address.
182 * List of GEM active objects on this gpu. Protected by
183 * msm_drm_private::mm_lock
185 struct list_head active_list;
190 * General lock for serializing all the gpu things.
192 * TODO move to per-ring locking where feasible (ie. submit/retire
200 * The number of submitted but not yet retired submits, used to
201 * determine transitions between active and idle.
203 * Protected by active_lock
207 /** lock: protects active_submits and idle/active transitions */
208 struct mutex active_lock;
210 /* does gpu need hw_init? */
214 * global_faults: number of GPU hangs not attributed to a particular
222 struct msm_gem_address_space *aspace;
225 struct regulator *gpu_reg, *gpu_cx;
226 struct clk_bulk_data *grp_clks;
228 struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
231 /* Hang and Inactivity Detection:
233 #define DRM_MSM_INACTIVE_PERIOD 66 /* in ms (roughly four frames) */
235 #define DRM_MSM_HANGCHECK_DEFAULT_PERIOD 500 /* in ms */
236 struct timer_list hangcheck_timer;
238 /* Fault info for most recent iova fault: */
239 struct msm_gpu_fault_info fault_info;
241 /* work for handling GPU ioval faults: */
242 struct kthread_work fault_work;
244 /* work for handling GPU recovery: */
245 struct kthread_work recover_work;
247 /** retire_event: notified when submits are retired: */
248 wait_queue_head_t retire_event;
250 /* work for handling active-list retiring: */
251 struct kthread_work retire_work;
253 /* worker for retire/recover: */
254 struct kthread_worker *worker;
256 struct drm_gem_object *memptrs_bo;
258 struct msm_gpu_devfreq devfreq;
260 uint32_t suspend_count;
262 struct msm_gpu_state *crashstate;
264 /* Enable clamping to idle freq when inactive: */
267 /* True if the hardware supports expanded apriv (a650 and newer) */
270 struct thermal_cooling_device *cooling;
273 static inline struct msm_gpu *dev_to_gpu(struct device *dev)
275 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
276 return container_of(adreno_smmu, struct msm_gpu, adreno_smmu);
279 /* It turns out that all targets use the same ringbuffer size */
280 #define MSM_GPU_RINGBUFFER_SZ SZ_32K
281 #define MSM_GPU_RINGBUFFER_BLKSIZE 32
283 #define MSM_GPU_RB_CNTL_DEFAULT \
284 (AXXX_CP_RB_CNTL_BUFSZ(ilog2(MSM_GPU_RINGBUFFER_SZ / 8)) | \
285 AXXX_CP_RB_CNTL_BLKSZ(ilog2(MSM_GPU_RINGBUFFER_BLKSIZE / 8)))
287 static inline bool msm_gpu_active(struct msm_gpu *gpu)
291 for (i = 0; i < gpu->nr_rings; i++) {
292 struct msm_ringbuffer *ring = gpu->rb[i];
294 if (fence_after(ring->fctx->last_fence, ring->memptrs->fence))
302 * The select_reg and select_val are just there for the benefit of the child
303 * class that actually enables the perf counter.. but msm_gpu base class
304 * will handle sampling/displaying the counters.
307 struct msm_gpu_perfcntr {
315 * The number of priority levels provided by drm gpu scheduler. The
316 * DRM_SCHED_PRIORITY_KERNEL priority level is treated specially in some
317 * cases, so we don't use it (no need for kernel generated jobs).
319 #define NR_SCHED_PRIORITIES (1 + DRM_SCHED_PRIORITY_HIGH - DRM_SCHED_PRIORITY_MIN)
322 * struct msm_file_private - per-drm_file context
324 * @queuelock: synchronizes access to submitqueues list
325 * @submitqueues: list of &msm_gpu_submitqueue created by userspace
326 * @queueid: counter incremented each time a submitqueue is created,
327 * used to assign &msm_gpu_submitqueue.id
328 * @aspace: the per-process GPU address-space
329 * @ref: reference count
330 * @seqno: unique per process seqno
332 struct msm_file_private {
334 struct list_head submitqueues;
336 struct msm_gem_address_space *aspace;
343 * The value of MSM_PARAM_SYSPROF set by userspace. This is
344 * intended to be used by system profiling tools like Mesa's
345 * pps-producer (perfetto), and restricted to CAP_SYS_ADMIN.
347 * Setting a value of 1 will preserve performance counters across
348 * context switches. Setting a value of 2 will in addition
349 * suppress suspend. (Performance counters lose state across
350 * power collapse, which is undesirable for profiling in some
353 * The value automatically reverts to zero when the drm device
358 /** comm: Overridden task comm, see MSM_PARAM_COMM */
361 /** cmdline: Overridden task cmdline, see MSM_PARAM_CMDLINE */
367 * Table of per-priority-level sched entities used by submitqueues
368 * associated with this &drm_file. Because some userspace apps
369 * make assumptions about rendering from multiple gl contexts
370 * (of the same priority) within the process happening in FIFO
371 * order without requiring any fencing beyond MakeCurrent(), we
372 * create at most one &drm_sched_entity per-process per-priority-
375 struct drm_sched_entity *entities[NR_SCHED_PRIORITIES * MSM_GPU_MAX_RINGS];
379 * msm_gpu_convert_priority - Map userspace priority to ring # and sched priority
381 * @gpu: the gpu instance
382 * @prio: the userspace priority level
383 * @ring_nr: [out] the ringbuffer the userspace priority maps to
384 * @sched_prio: [out] the gpu scheduler priority level which the userspace
387 * With drm/scheduler providing it's own level of prioritization, our total
388 * number of available priority levels is (nr_rings * NR_SCHED_PRIORITIES).
389 * Each ring is associated with it's own scheduler instance. However, our
390 * UABI is that lower numerical values are higher priority. So mapping the
391 * single userspace priority level into ring_nr and sched_prio takes some
392 * care. The userspace provided priority (when a submitqueue is created)
393 * is mapped to ring nr and scheduler priority as such:
395 * ring_nr = userspace_prio / NR_SCHED_PRIORITIES
396 * sched_prio = NR_SCHED_PRIORITIES -
397 * (userspace_prio % NR_SCHED_PRIORITIES) - 1
399 * This allows generations without preemption (nr_rings==1) to have some
400 * amount of prioritization, and provides more priority levels for gens
401 * that do have preemption.
403 static inline int msm_gpu_convert_priority(struct msm_gpu *gpu, int prio,
404 unsigned *ring_nr, enum drm_sched_priority *sched_prio)
408 rn = div_u64_rem(prio, NR_SCHED_PRIORITIES, &sp);
410 /* invert sched priority to map to higher-numeric-is-higher-
411 * priority convention
413 sp = NR_SCHED_PRIORITIES - sp - 1;
415 if (rn >= gpu->nr_rings)
425 * struct msm_gpu_submitqueues - Userspace created context.
427 * A submitqueue is associated with a gl context or vk queue (or equiv)
430 * @id: userspace id for the submitqueue, unique within the drm_file
431 * @flags: userspace flags for the submitqueue, specified at creation
432 * (currently unusued)
433 * @ring_nr: the ringbuffer used by this submitqueue, which is determined
434 * by the submitqueue's priority
435 * @faults: the number of GPU hangs associated with this submitqueue
436 * @last_fence: the sequence number of the last allocated fence (for error
438 * @ctx: the per-drm_file context associated with the submitqueue (ie.
439 * which set of pgtables do submits jobs associated with the
441 * @node: node in the context's list of submitqueues
442 * @fence_idr: maps fence-id to dma_fence for userspace visible fence
443 * seqno, protected by submitqueue lock
444 * @lock: submitqueue lock
445 * @ref: reference count
446 * @entity: the submit job-queue
448 struct msm_gpu_submitqueue {
454 struct msm_file_private *ctx;
455 struct list_head node;
456 struct idr fence_idr;
459 struct drm_sched_entity *entity;
462 struct msm_gpu_state_bo {
469 struct msm_gpu_state {
471 struct timespec64 time;
482 } ring[MSM_GPU_MAX_RINGS];
492 struct msm_gpu_fault_info fault_info;
495 struct msm_gpu_state_bo *bos;
498 static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
500 msm_writel(data, gpu->mmio + (reg << 2));
503 static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
505 return msm_readl(gpu->mmio + (reg << 2));
508 static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
510 msm_rmw(gpu->mmio + (reg << 2), mask, or);
513 static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
518 * Why not a readq here? Two reasons: 1) many of the LO registers are
519 * not quad word aligned and 2) the GPU hardware designers have a bit
520 * of a history of putting registers where they fit, especially in
521 * spins. The longer a GPU family goes the higher the chance that
522 * we'll get burned. We could do a series of validity checks if we
523 * wanted to, but really is a readq() that much better? Nah.
527 * For some lo/hi registers (like perfcounters), the hi value is latched
528 * when the lo is read, so make sure to read the lo first to trigger
531 val = (u64) msm_readl(gpu->mmio + (lo << 2));
532 val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
537 static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
539 /* Why not a writeq here? Read the screed above */
540 msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
541 msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
544 int msm_gpu_pm_suspend(struct msm_gpu *gpu);
545 int msm_gpu_pm_resume(struct msm_gpu *gpu);
547 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx);
548 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
550 int msm_submitqueue_create(struct drm_device *drm,
551 struct msm_file_private *ctx,
552 u32 prio, u32 flags, u32 *id);
553 int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
554 struct drm_msm_submitqueue_query *args);
555 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id);
556 void msm_submitqueue_close(struct msm_file_private *ctx);
558 void msm_submitqueue_destroy(struct kref *kref);
560 int msm_file_private_set_sysprof(struct msm_file_private *ctx,
561 struct msm_gpu *gpu, int sysprof);
562 void __msm_file_private_destroy(struct kref *kref);
564 static inline void msm_file_private_put(struct msm_file_private *ctx)
566 kref_put(&ctx->ref, __msm_file_private_destroy);
569 static inline struct msm_file_private *msm_file_private_get(
570 struct msm_file_private *ctx)
576 void msm_devfreq_init(struct msm_gpu *gpu);
577 void msm_devfreq_cleanup(struct msm_gpu *gpu);
578 void msm_devfreq_resume(struct msm_gpu *gpu);
579 void msm_devfreq_suspend(struct msm_gpu *gpu);
580 void msm_devfreq_boost(struct msm_gpu *gpu, unsigned factor);
581 void msm_devfreq_active(struct msm_gpu *gpu);
582 void msm_devfreq_idle(struct msm_gpu *gpu);
584 int msm_gpu_hw_init(struct msm_gpu *gpu);
586 void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
587 void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
588 int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
589 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
591 void msm_gpu_retire(struct msm_gpu *gpu);
592 void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit);
594 int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
595 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
596 const char *name, struct msm_gpu_config *config);
598 struct msm_gem_address_space *
599 msm_gpu_create_private_address_space(struct msm_gpu *gpu, struct task_struct *task);
601 void msm_gpu_cleanup(struct msm_gpu *gpu);
603 struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
604 void __init adreno_register(void);
605 void __exit adreno_unregister(void);
607 static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
610 kref_put(&queue->ref, msm_submitqueue_destroy);
613 static inline struct msm_gpu_state *msm_gpu_crashstate_get(struct msm_gpu *gpu)
615 struct msm_gpu_state *state = NULL;
617 mutex_lock(&gpu->lock);
619 if (gpu->crashstate) {
620 kref_get(&gpu->crashstate->ref);
621 state = gpu->crashstate;
624 mutex_unlock(&gpu->lock);
629 static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
631 mutex_lock(&gpu->lock);
633 if (gpu->crashstate) {
634 if (gpu->funcs->gpu_state_put(gpu->crashstate))
635 gpu->crashstate = NULL;
638 mutex_unlock(&gpu->lock);
642 * Simple macro to semi-cleanly add the MAP_PRIV flag for targets that can
643 * support expanded privileges
645 #define check_apriv(gpu, flags) \
646 (((gpu)->hw_apriv ? MSM_BO_MAP_PRIV : 0) | (flags))
649 #endif /* __MSM_GPU_H__ */