1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2013 Red Hat
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>
17 #include "msm_fence.h"
18 #include "msm_ringbuffer.h"
21 struct msm_gem_submit;
22 struct msm_gpu_perfcntr;
25 struct msm_gpu_config {
27 unsigned int nr_rings;
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)
36 * Which means that the eventual complete "class" hierarchy, once
37 * support for all past and present hw is in place, becomes:
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);
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);
77 /* Additional state for iommu faults: */
78 struct msm_gpu_fault_info {
87 * struct msm_gpu_devfreq - devfreq related state
89 struct msm_gpu_devfreq {
90 /** devfreq: devfreq instance */
91 struct devfreq *devfreq;
96 * A PM QoS constraint to limit max freq while the GPU is idle.
98 struct dev_pm_qos_request idle_freq;
103 * A PM QoS constraint to boost min freq for a period of time
104 * until the boost expires.
106 struct dev_pm_qos_request boost_freq;
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.
117 /** time: Time of last sampling period. */
120 /** idle_time: Time of last transition to idle: */
126 * Used to delay clamping to idle freq on active->idle transition.
128 struct msm_hrtimer_work idle_work;
133 * Used to reset the boost_constraint after the boost period has
136 struct msm_hrtimer_work boost_work;
141 struct drm_device *dev;
142 struct platform_device *pdev;
143 const struct msm_gpu_funcs *funcs;
145 struct adreno_smmu_priv adreno_smmu;
147 /* performance counters (hw & sw): */
148 spinlock_t perf_lock;
149 bool perfcntr_active;
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;
159 struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
165 * The count of contexts that have enabled system profiling.
167 refcount_t sysprof_active;
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.
181 * List of GEM active objects on this gpu. Protected by
182 * msm_drm_private::mm_lock
184 struct list_head active_list;
189 * General lock for serializing all the gpu things.
191 * TODO move to per-ring locking where feasible (ie. submit/retire
199 * The number of submitted but not yet retired submits, used to
200 * determine transitions between active and idle.
202 * Protected by active_lock
206 /** lock: protects active_submits and idle/active transitions */
207 struct mutex active_lock;
209 /* does gpu need hw_init? */
213 * global_faults: number of GPU hangs not attributed to a particular
221 struct msm_gem_address_space *aspace;
224 struct regulator *gpu_reg, *gpu_cx;
225 struct clk_bulk_data *grp_clks;
227 struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
230 /* Hang and Inactivity Detection:
232 #define DRM_MSM_INACTIVE_PERIOD 66 /* in ms (roughly four frames) */
234 #define DRM_MSM_HANGCHECK_DEFAULT_PERIOD 500 /* in ms */
235 struct timer_list hangcheck_timer;
237 /* Fault info for most recent iova fault: */
238 struct msm_gpu_fault_info fault_info;
240 /* work for handling GPU ioval faults: */
241 struct kthread_work fault_work;
243 /* work for handling GPU recovery: */
244 struct kthread_work recover_work;
246 /** retire_event: notified when submits are retired: */
247 wait_queue_head_t retire_event;
249 /* work for handling active-list retiring: */
250 struct kthread_work retire_work;
252 /* worker for retire/recover: */
253 struct kthread_worker *worker;
255 struct drm_gem_object *memptrs_bo;
257 struct msm_gpu_devfreq devfreq;
259 uint32_t suspend_count;
261 struct msm_gpu_state *crashstate;
263 /* Enable clamping to idle freq when inactive: */
266 /* True if the hardware supports expanded apriv (a650 and newer) */
269 struct thermal_cooling_device *cooling;
272 static inline struct msm_gpu *dev_to_gpu(struct device *dev)
274 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
275 return container_of(adreno_smmu, struct msm_gpu, adreno_smmu);
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
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)))
286 static inline bool msm_gpu_active(struct msm_gpu *gpu)
290 for (i = 0; i < gpu->nr_rings; i++) {
291 struct msm_ringbuffer *ring = gpu->rb[i];
293 if (fence_after(ring->seqno, ring->memptrs->fence))
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.
306 struct msm_gpu_perfcntr {
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).
318 #define NR_SCHED_PRIORITIES (1 + DRM_SCHED_PRIORITY_HIGH - DRM_SCHED_PRIORITY_MIN)
321 * struct msm_file_private - per-drm_file context
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
331 struct msm_file_private {
333 struct list_head submitqueues;
335 struct msm_gem_address_space *aspace;
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.
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
352 * The value automatically reverts to zero when the drm device
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-
368 struct drm_sched_entity *entities[NR_SCHED_PRIORITIES * MSM_GPU_MAX_RINGS];
372 * msm_gpu_convert_priority - Map userspace priority to ring # and sched priority
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
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:
388 * ring_nr = userspace_prio / NR_SCHED_PRIORITIES
389 * sched_prio = NR_SCHED_PRIORITIES -
390 * (userspace_prio % NR_SCHED_PRIORITIES) - 1
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.
396 static inline int msm_gpu_convert_priority(struct msm_gpu *gpu, int prio,
397 unsigned *ring_nr, enum drm_sched_priority *sched_prio)
401 rn = div_u64_rem(prio, NR_SCHED_PRIORITIES, &sp);
403 /* invert sched priority to map to higher-numeric-is-higher-
404 * priority convention
406 sp = NR_SCHED_PRIORITIES - sp - 1;
408 if (rn >= gpu->nr_rings)
418 * struct msm_gpu_submitqueues - Userspace created context.
420 * A submitqueue is associated with a gl context or vk queue (or equiv)
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
431 * @ctx: the per-drm_file context associated with the submitqueue (ie.
432 * which set of pgtables do submits jobs associated with the
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
441 struct msm_gpu_submitqueue {
447 struct msm_file_private *ctx;
448 struct list_head node;
449 struct idr fence_idr;
452 struct drm_sched_entity *entity;
455 struct msm_gpu_state_bo {
462 struct msm_gpu_state {
464 struct timespec64 time;
475 } ring[MSM_GPU_MAX_RINGS];
485 struct msm_gpu_fault_info fault_info;
488 struct msm_gpu_state_bo *bos;
491 static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
493 msm_writel(data, gpu->mmio + (reg << 2));
496 static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
498 return msm_readl(gpu->mmio + (reg << 2));
501 static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
503 msm_rmw(gpu->mmio + (reg << 2), mask, or);
506 static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
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.
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
524 val = (u64) msm_readl(gpu->mmio + (lo << 2));
525 val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
530 static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
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));
537 int msm_gpu_pm_suspend(struct msm_gpu *gpu);
538 int msm_gpu_pm_resume(struct msm_gpu *gpu);
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,
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);
551 void msm_submitqueue_destroy(struct kref *kref);
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);
557 static inline void msm_file_private_put(struct msm_file_private *ctx)
559 kref_put(&ctx->ref, __msm_file_private_destroy);
562 static inline struct msm_file_private *msm_file_private_get(
563 struct msm_file_private *ctx)
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);
577 int msm_gpu_hw_init(struct msm_gpu *gpu);
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);
584 void msm_gpu_retire(struct msm_gpu *gpu);
585 void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit);
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);
591 struct msm_gem_address_space *
592 msm_gpu_create_private_address_space(struct msm_gpu *gpu, struct task_struct *task);
594 void msm_gpu_cleanup(struct msm_gpu *gpu);
596 struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
597 void __init adreno_register(void);
598 void __exit adreno_unregister(void);
600 static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
603 kref_put(&queue->ref, msm_submitqueue_destroy);
606 static inline struct msm_gpu_state *msm_gpu_crashstate_get(struct msm_gpu *gpu)
608 struct msm_gpu_state *state = NULL;
610 mutex_lock(&gpu->lock);
612 if (gpu->crashstate) {
613 kref_get(&gpu->crashstate->ref);
614 state = gpu->crashstate;
617 mutex_unlock(&gpu->lock);
622 static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
624 mutex_lock(&gpu->lock);
626 if (gpu->crashstate) {
627 if (gpu->funcs->gpu_state_put(gpu->crashstate))
628 gpu->crashstate = NULL;
631 mutex_unlock(&gpu->lock);
635 * Simple macro to semi-cleanly add the MAP_PRIV flag for targets that can
636 * support expanded privileges
638 #define check_apriv(gpu, flags) \
639 (((gpu)->hw_apriv ? MSM_BO_MAP_PRIV : 0) | (flags))
642 #endif /* __MSM_GPU_H__ */