blob: be5bc2e8425c579de32cd5015e3cc71bf005c1a0 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Rob Clark7198e6b2013-07-19 12:59:32 -04002/*
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
Rob Clark7198e6b2013-07-19 12:59:32 -04005 */
6
7#ifndef __MSM_GPU_H__
8#define __MSM_GPU_H__
9
10#include <linux/clk.h>
Jordan Crousefcf9d0b2019-02-12 11:52:38 +020011#include <linux/interconnect.h>
Rob Clark7198e6b2013-07-19 12:59:32 -040012#include <linux/regulator/consumer.h>
13
14#include "msm_drv.h"
Rob Clarkca762a82016-03-15 17:22:13 -040015#include "msm_fence.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040016#include "msm_ringbuffer.h"
17
18struct msm_gem_submit;
Rob Clark70c70f02014-05-30 14:49:43 -040019struct msm_gpu_perfcntr;
Jordan Crousee00e4732018-07-24 10:33:24 -060020struct msm_gpu_state;
Rob Clark7198e6b2013-07-19 12:59:32 -040021
Jordan Crouse5770fc72017-05-08 14:35:03 -060022struct msm_gpu_config {
23 const char *ioname;
Jordan Crouse5770fc72017-05-08 14:35:03 -060024 uint64_t va_start;
25 uint64_t va_end;
Jordan Crousef97deca2017-10-20 11:06:57 -060026 unsigned int nr_rings;
Jordan Crouse5770fc72017-05-08 14:35:03 -060027};
28
Rob Clark7198e6b2013-07-19 12:59:32 -040029/* So far, with hardware that I've seen to date, we can have:
30 * + zero, one, or two z180 2d cores
31 * + a3xx or a2xx 3d core, which share a common CP (the firmware
32 * for the CP seems to implement some different PM4 packet types
33 * but the basics of cmdstream submission are the same)
34 *
35 * Which means that the eventual complete "class" hierarchy, once
36 * support for all past and present hw is in place, becomes:
37 * + msm_gpu
38 * + adreno_gpu
39 * + a3xx_gpu
40 * + a2xx_gpu
41 * + z180_gpu
42 */
43struct msm_gpu_funcs {
44 int (*get_param)(struct msm_gpu *gpu, uint32_t param, uint64_t *value);
45 int (*hw_init)(struct msm_gpu *gpu);
46 int (*pm_suspend)(struct msm_gpu *gpu);
47 int (*pm_resume)(struct msm_gpu *gpu);
Rob Clark1193c3b2016-05-03 09:46:49 -040048 void (*submit)(struct msm_gpu *gpu, struct msm_gem_submit *submit,
Rob Clark7198e6b2013-07-19 12:59:32 -040049 struct msm_file_private *ctx);
Jordan Crousef97deca2017-10-20 11:06:57 -060050 void (*flush)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
Rob Clark7198e6b2013-07-19 12:59:32 -040051 irqreturn_t (*irq)(struct msm_gpu *irq);
Jordan Crousef97deca2017-10-20 11:06:57 -060052 struct msm_ringbuffer *(*active_ring)(struct msm_gpu *gpu);
Rob Clarkbd6f82d2013-08-24 14:20:38 -040053 void (*recover)(struct msm_gpu *gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -040054 void (*destroy)(struct msm_gpu *gpu);
Arnd Bergmannc878a622018-08-13 23:23:44 +020055#if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
Rob Clark7198e6b2013-07-19 12:59:32 -040056 /* show GPU status in debugfs: */
Jordan Crouse4f776f42018-07-24 10:33:25 -060057 void (*show)(struct msm_gpu *gpu, struct msm_gpu_state *state,
Jordan Crousec0fec7f2018-07-24 10:33:27 -060058 struct drm_printer *p);
Rob Clark331dc0b2017-12-13 15:12:56 -050059 /* for generation specific debugfs: */
60 int (*debugfs_init)(struct msm_gpu *gpu, struct drm_minor *minor);
Rob Clark7198e6b2013-07-19 12:59:32 -040061#endif
Sharat Masettyde0a3d092018-10-04 15:11:42 +053062 unsigned long (*gpu_busy)(struct msm_gpu *gpu);
Jordan Crousee00e4732018-07-24 10:33:24 -060063 struct msm_gpu_state *(*gpu_state_get)(struct msm_gpu *gpu);
Jordan Crousec0fec7f2018-07-24 10:33:27 -060064 int (*gpu_state_put)(struct msm_gpu_state *state);
Sharat Masettyde0a3d092018-10-04 15:11:42 +053065 unsigned long (*gpu_get_freq)(struct msm_gpu *gpu);
66 void (*gpu_set_freq)(struct msm_gpu *gpu, unsigned long freq);
Rob Clark7198e6b2013-07-19 12:59:32 -040067};
68
69struct msm_gpu {
70 const char *name;
71 struct drm_device *dev;
Rob Clarkeeb75472017-02-10 15:36:33 -050072 struct platform_device *pdev;
Rob Clark7198e6b2013-07-19 12:59:32 -040073 const struct msm_gpu_funcs *funcs;
74
Rob Clark70c70f02014-05-30 14:49:43 -040075 /* performance counters (hw & sw): */
76 spinlock_t perf_lock;
77 bool perfcntr_active;
78 struct {
79 bool active;
80 ktime_t time;
81 } last_sample;
82 uint32_t totaltime, activetime; /* sw counters */
83 uint32_t last_cntrs[5]; /* hw counters */
84 const struct msm_gpu_perfcntr *perfcntrs;
85 uint32_t num_perfcntrs;
86
Jordan Crousef97deca2017-10-20 11:06:57 -060087 struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
88 int nr_rings;
Rob Clark7198e6b2013-07-19 12:59:32 -040089
90 /* list of GEM active objects: */
91 struct list_head active_list;
92
Rob Clarkeeb75472017-02-10 15:36:33 -050093 /* does gpu need hw_init? */
94 bool needs_hw_init;
Rob Clark37d77c32014-01-11 16:25:08 -050095
Rob Clark48dc4242019-04-16 16:13:28 -070096 /* number of GPU hangs (for all contexts) */
97 int global_faults;
98
Rob Clark7198e6b2013-07-19 12:59:32 -040099 /* worker for handling active-list retiring: */
100 struct work_struct retire_work;
101
102 void __iomem *mmio;
103 int irq;
104
Rob Clark667ce332016-09-28 19:58:32 -0400105 struct msm_gem_address_space *aspace;
Rob Clark7198e6b2013-07-19 12:59:32 -0400106
107 /* Power Control: */
108 struct regulator *gpu_reg, *gpu_cx;
Jordan Crouse8e54eea2018-08-06 11:33:21 -0600109 struct clk_bulk_data *grp_clks;
Jordan Crouse98db8032017-03-07 10:02:56 -0700110 int nr_clocks;
111 struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
Jordan Crouse1babd702017-11-21 12:40:53 -0700112 uint32_t fast_rate;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400113
Brian Masney00bb9242019-11-21 20:26:43 -0500114 /* The gfx-mem interconnect path that's used by all GPU types. */
Jordan Crousefcf9d0b2019-02-12 11:52:38 +0200115 struct icc_path *icc_path;
116
Brian Masney00bb9242019-11-21 20:26:43 -0500117 /*
118 * Second interconnect path for some A3xx and all A4xx GPUs to the
119 * On Chip MEMory (OCMEM).
120 */
121 struct icc_path *ocmem_icc_path;
122
Rob Clark37d77c32014-01-11 16:25:08 -0500123 /* Hang and Inactivity Detection:
124 */
125#define DRM_MSM_INACTIVE_PERIOD 66 /* in ms (roughly four frames) */
Rob Clarkeeb75472017-02-10 15:36:33 -0500126
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400127#define DRM_MSM_HANGCHECK_PERIOD 500 /* in ms */
128#define DRM_MSM_HANGCHECK_JIFFIES msecs_to_jiffies(DRM_MSM_HANGCHECK_PERIOD)
129 struct timer_list hangcheck_timer;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400130 struct work_struct recover_work;
Rob Clark1a370be2015-06-07 13:46:04 -0400131
Jordan Crousecd414f32017-10-20 11:06:56 -0600132 struct drm_gem_object *memptrs_bo;
Jordan Crousef91c14a2018-01-10 10:41:54 -0700133
134 struct {
135 struct devfreq *devfreq;
136 u64 busy_cycles;
137 ktime_t time;
138 } devfreq;
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600139
140 struct msm_gpu_state *crashstate;
Rob Clark7198e6b2013-07-19 12:59:32 -0400141};
142
Jordan Crousef97deca2017-10-20 11:06:57 -0600143/* It turns out that all targets use the same ringbuffer size */
144#define MSM_GPU_RINGBUFFER_SZ SZ_32K
Jordan Crouse4d87fc32017-10-20 11:07:00 -0600145#define MSM_GPU_RINGBUFFER_BLKSIZE 32
146
147#define MSM_GPU_RB_CNTL_DEFAULT \
148 (AXXX_CP_RB_CNTL_BUFSZ(ilog2(MSM_GPU_RINGBUFFER_SZ / 8)) | \
149 AXXX_CP_RB_CNTL_BLKSZ(ilog2(MSM_GPU_RINGBUFFER_BLKSIZE / 8)))
Jordan Crousef97deca2017-10-20 11:06:57 -0600150
Rob Clark37d77c32014-01-11 16:25:08 -0500151static inline bool msm_gpu_active(struct msm_gpu *gpu)
152{
Jordan Crousef97deca2017-10-20 11:06:57 -0600153 int i;
154
155 for (i = 0; i < gpu->nr_rings; i++) {
156 struct msm_ringbuffer *ring = gpu->rb[i];
157
158 if (ring->seqno > ring->memptrs->fence)
159 return true;
160 }
161
162 return false;
Rob Clark37d77c32014-01-11 16:25:08 -0500163}
164
Rob Clark70c70f02014-05-30 14:49:43 -0400165/* Perf-Counters:
166 * The select_reg and select_val are just there for the benefit of the child
167 * class that actually enables the perf counter.. but msm_gpu base class
168 * will handle sampling/displaying the counters.
169 */
170
171struct msm_gpu_perfcntr {
172 uint32_t select_reg;
173 uint32_t sample_reg;
174 uint32_t select_val;
175 const char *name;
176};
177
Jordan Crousef7de1542017-10-20 11:06:55 -0600178struct msm_gpu_submitqueue {
179 int id;
180 u32 flags;
181 u32 prio;
182 int faults;
183 struct list_head node;
184 struct kref ref;
185};
186
Jordan Crousecdb95932018-07-24 10:33:31 -0600187struct msm_gpu_state_bo {
188 u64 iova;
189 size_t size;
190 void *data;
Sharat Masetty1df42892018-11-01 20:16:45 +0530191 bool encoded;
Jordan Crousecdb95932018-07-24 10:33:31 -0600192};
193
Jordan Crousee00e4732018-07-24 10:33:24 -0600194struct msm_gpu_state {
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600195 struct kref ref;
Arnd Bergmann3530a172018-07-26 14:39:25 +0200196 struct timespec64 time;
Jordan Crousee00e4732018-07-24 10:33:24 -0600197
198 struct {
199 u64 iova;
200 u32 fence;
201 u32 seqno;
202 u32 rptr;
203 u32 wptr;
Jordan Crouse43a56682018-07-24 10:33:29 -0600204 void *data;
205 int data_size;
Sharat Masetty1df42892018-11-01 20:16:45 +0530206 bool encoded;
Jordan Crousee00e4732018-07-24 10:33:24 -0600207 } ring[MSM_GPU_MAX_RINGS];
208
209 int nr_registers;
210 u32 *registers;
211
212 u32 rbbm_status;
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600213
214 char *comm;
215 char *cmd;
Jordan Crousecdb95932018-07-24 10:33:31 -0600216
217 int nr_bos;
218 struct msm_gpu_state_bo *bos;
Jordan Crousee00e4732018-07-24 10:33:24 -0600219};
220
Rob Clark7198e6b2013-07-19 12:59:32 -0400221static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
222{
223 msm_writel(data, gpu->mmio + (reg << 2));
224}
225
226static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
227{
228 return msm_readl(gpu->mmio + (reg << 2));
229}
230
Jordan Crouseae53a822016-11-28 12:28:28 -0700231static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
232{
233 uint32_t val = gpu_read(gpu, reg);
234
235 val &= ~mask;
236 gpu_write(gpu, reg, val | or);
237}
238
239static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
240{
241 u64 val;
242
243 /*
244 * Why not a readq here? Two reasons: 1) many of the LO registers are
245 * not quad word aligned and 2) the GPU hardware designers have a bit
246 * of a history of putting registers where they fit, especially in
247 * spins. The longer a GPU family goes the higher the chance that
248 * we'll get burned. We could do a series of validity checks if we
249 * wanted to, but really is a readq() that much better? Nah.
250 */
251
252 /*
253 * For some lo/hi registers (like perfcounters), the hi value is latched
254 * when the lo is read, so make sure to read the lo first to trigger
255 * that
256 */
257 val = (u64) msm_readl(gpu->mmio + (lo << 2));
258 val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
259
260 return val;
261}
262
263static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
264{
265 /* Why not a writeq here? Read the screed above */
266 msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
267 msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
268}
269
Rob Clark7198e6b2013-07-19 12:59:32 -0400270int msm_gpu_pm_suspend(struct msm_gpu *gpu);
271int msm_gpu_pm_resume(struct msm_gpu *gpu);
Sharat Masettyde0a3d092018-10-04 15:11:42 +0530272void msm_gpu_resume_devfreq(struct msm_gpu *gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400273
Rob Clarkeeb75472017-02-10 15:36:33 -0500274int msm_gpu_hw_init(struct msm_gpu *gpu);
275
Rob Clark70c70f02014-05-30 14:49:43 -0400276void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
277void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
278int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
279 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
280
Rob Clark7198e6b2013-07-19 12:59:32 -0400281void msm_gpu_retire(struct msm_gpu *gpu);
Rob Clarkf44d32c2016-06-16 16:37:38 -0400282void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
Rob Clark7198e6b2013-07-19 12:59:32 -0400283 struct msm_file_private *ctx);
284
285int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
286 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
Jordan Crouse5770fc72017-05-08 14:35:03 -0600287 const char *name, struct msm_gpu_config *config);
288
Rob Clark7198e6b2013-07-19 12:59:32 -0400289void msm_gpu_cleanup(struct msm_gpu *gpu);
290
Rob Clarke2550b72014-09-05 13:30:27 -0400291struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
Rob Clarkbfd28b12014-09-05 13:06:37 -0400292void __init adreno_register(void);
293void __exit adreno_unregister(void);
Rob Clark7198e6b2013-07-19 12:59:32 -0400294
Jordan Crousef7de1542017-10-20 11:06:55 -0600295static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
296{
297 if (queue)
298 kref_put(&queue->ref, msm_submitqueue_destroy);
299}
300
Jordan Crousec0fec7f2018-07-24 10:33:27 -0600301static inline struct msm_gpu_state *msm_gpu_crashstate_get(struct msm_gpu *gpu)
302{
303 struct msm_gpu_state *state = NULL;
304
305 mutex_lock(&gpu->dev->struct_mutex);
306
307 if (gpu->crashstate) {
308 kref_get(&gpu->crashstate->ref);
309 state = gpu->crashstate;
310 }
311
312 mutex_unlock(&gpu->dev->struct_mutex);
313
314 return state;
315}
316
317static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
318{
319 mutex_lock(&gpu->dev->struct_mutex);
320
321 if (gpu->crashstate) {
322 if (gpu->funcs->gpu_state_put(gpu->crashstate))
323 gpu->crashstate = NULL;
324 }
325
326 mutex_unlock(&gpu->dev->struct_mutex);
327}
328
Rob Clark7198e6b2013-07-19 12:59:32 -0400329#endif /* __MSM_GPU_H__ */