blob: de71cc04ecf3486b88f550e280676ec17709e764 [file] [log] [blame]
Rob Clark7198e6b2013-07-19 12:59:32 -04001/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __MSM_GPU_H__
19#define __MSM_GPU_H__
20
21#include <linux/clk.h>
22#include <linux/regulator/consumer.h>
23
24#include "msm_drv.h"
Rob Clarkca762a82016-03-15 17:22:13 -040025#include "msm_fence.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040026#include "msm_ringbuffer.h"
27
28struct msm_gem_submit;
Rob Clark70c70f02014-05-30 14:49:43 -040029struct msm_gpu_perfcntr;
Jordan Crousee00e4732018-07-24 10:33:24 -060030struct msm_gpu_state;
Rob Clark7198e6b2013-07-19 12:59:32 -040031
Jordan Crouse5770fc72017-05-08 14:35:03 -060032struct msm_gpu_config {
33 const char *ioname;
34 const char *irqname;
35 uint64_t va_start;
36 uint64_t va_end;
Jordan Crousef97deca2017-10-20 11:06:57 -060037 unsigned int nr_rings;
Jordan Crouse5770fc72017-05-08 14:35:03 -060038};
39
Rob Clark7198e6b2013-07-19 12:59:32 -040040/* So far, with hardware that I've seen to date, we can have:
41 * + zero, one, or two z180 2d cores
42 * + a3xx or a2xx 3d core, which share a common CP (the firmware
43 * for the CP seems to implement some different PM4 packet types
44 * but the basics of cmdstream submission are the same)
45 *
46 * Which means that the eventual complete "class" hierarchy, once
47 * support for all past and present hw is in place, becomes:
48 * + msm_gpu
49 * + adreno_gpu
50 * + a3xx_gpu
51 * + a2xx_gpu
52 * + z180_gpu
53 */
54struct msm_gpu_funcs {
55 int (*get_param)(struct msm_gpu *gpu, uint32_t param, uint64_t *value);
56 int (*hw_init)(struct msm_gpu *gpu);
57 int (*pm_suspend)(struct msm_gpu *gpu);
58 int (*pm_resume)(struct msm_gpu *gpu);
Rob Clark1193c3b2016-05-03 09:46:49 -040059 void (*submit)(struct msm_gpu *gpu, struct msm_gem_submit *submit,
Rob Clark7198e6b2013-07-19 12:59:32 -040060 struct msm_file_private *ctx);
Jordan Crousef97deca2017-10-20 11:06:57 -060061 void (*flush)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
Rob Clark7198e6b2013-07-19 12:59:32 -040062 irqreturn_t (*irq)(struct msm_gpu *irq);
Jordan Crousef97deca2017-10-20 11:06:57 -060063 struct msm_ringbuffer *(*active_ring)(struct msm_gpu *gpu);
Rob Clarkbd6f82d2013-08-24 14:20:38 -040064 void (*recover)(struct msm_gpu *gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -040065 void (*destroy)(struct msm_gpu *gpu);
66#ifdef CONFIG_DEBUG_FS
67 /* show GPU status in debugfs: */
68 void (*show)(struct msm_gpu *gpu, struct seq_file *m);
Rob Clark331dc0b2017-12-13 15:12:56 -050069 /* for generation specific debugfs: */
70 int (*debugfs_init)(struct msm_gpu *gpu, struct drm_minor *minor);
Rob Clark7198e6b2013-07-19 12:59:32 -040071#endif
Jordan Crousef91c14a2018-01-10 10:41:54 -070072 int (*gpu_busy)(struct msm_gpu *gpu, uint64_t *value);
Jordan Crousee00e4732018-07-24 10:33:24 -060073 struct msm_gpu_state *(*gpu_state_get)(struct msm_gpu *gpu);
74 void (*gpu_state_put)(struct msm_gpu_state *state);
Rob Clark7198e6b2013-07-19 12:59:32 -040075};
76
77struct msm_gpu {
78 const char *name;
79 struct drm_device *dev;
Rob Clarkeeb75472017-02-10 15:36:33 -050080 struct platform_device *pdev;
Rob Clark7198e6b2013-07-19 12:59:32 -040081 const struct msm_gpu_funcs *funcs;
82
Rob Clark70c70f02014-05-30 14:49:43 -040083 /* performance counters (hw & sw): */
84 spinlock_t perf_lock;
85 bool perfcntr_active;
86 struct {
87 bool active;
88 ktime_t time;
89 } last_sample;
90 uint32_t totaltime, activetime; /* sw counters */
91 uint32_t last_cntrs[5]; /* hw counters */
92 const struct msm_gpu_perfcntr *perfcntrs;
93 uint32_t num_perfcntrs;
94
Jordan Crousef97deca2017-10-20 11:06:57 -060095 struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
96 int nr_rings;
Rob Clark7198e6b2013-07-19 12:59:32 -040097
98 /* list of GEM active objects: */
99 struct list_head active_list;
100
Rob Clarkeeb75472017-02-10 15:36:33 -0500101 /* does gpu need hw_init? */
102 bool needs_hw_init;
Rob Clark37d77c32014-01-11 16:25:08 -0500103
Rob Clark7198e6b2013-07-19 12:59:32 -0400104 /* worker for handling active-list retiring: */
105 struct work_struct retire_work;
106
107 void __iomem *mmio;
108 int irq;
109
Rob Clark667ce332016-09-28 19:58:32 -0400110 struct msm_gem_address_space *aspace;
Rob Clark7198e6b2013-07-19 12:59:32 -0400111
112 /* Power Control: */
113 struct regulator *gpu_reg, *gpu_cx;
Jordan Crouse98db8032017-03-07 10:02:56 -0700114 struct clk **grp_clks;
115 int nr_clocks;
116 struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
Jordan Crouse1babd702017-11-21 12:40:53 -0700117 uint32_t fast_rate;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400118
Rob Clark37d77c32014-01-11 16:25:08 -0500119 /* Hang and Inactivity Detection:
120 */
121#define DRM_MSM_INACTIVE_PERIOD 66 /* in ms (roughly four frames) */
Rob Clarkeeb75472017-02-10 15:36:33 -0500122
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400123#define DRM_MSM_HANGCHECK_PERIOD 500 /* in ms */
124#define DRM_MSM_HANGCHECK_JIFFIES msecs_to_jiffies(DRM_MSM_HANGCHECK_PERIOD)
125 struct timer_list hangcheck_timer;
Rob Clarkbd6f82d2013-08-24 14:20:38 -0400126 struct work_struct recover_work;
Rob Clark1a370be2015-06-07 13:46:04 -0400127
Jordan Crousecd414f32017-10-20 11:06:56 -0600128 struct drm_gem_object *memptrs_bo;
Jordan Crousef91c14a2018-01-10 10:41:54 -0700129
130 struct {
131 struct devfreq *devfreq;
132 u64 busy_cycles;
133 ktime_t time;
134 } devfreq;
Rob Clark7198e6b2013-07-19 12:59:32 -0400135};
136
Jordan Crousef97deca2017-10-20 11:06:57 -0600137/* It turns out that all targets use the same ringbuffer size */
138#define MSM_GPU_RINGBUFFER_SZ SZ_32K
Jordan Crouse4d87fc32017-10-20 11:07:00 -0600139#define MSM_GPU_RINGBUFFER_BLKSIZE 32
140
141#define MSM_GPU_RB_CNTL_DEFAULT \
142 (AXXX_CP_RB_CNTL_BUFSZ(ilog2(MSM_GPU_RINGBUFFER_SZ / 8)) | \
143 AXXX_CP_RB_CNTL_BLKSZ(ilog2(MSM_GPU_RINGBUFFER_BLKSIZE / 8)))
Jordan Crousef97deca2017-10-20 11:06:57 -0600144
Rob Clark37d77c32014-01-11 16:25:08 -0500145static inline bool msm_gpu_active(struct msm_gpu *gpu)
146{
Jordan Crousef97deca2017-10-20 11:06:57 -0600147 int i;
148
149 for (i = 0; i < gpu->nr_rings; i++) {
150 struct msm_ringbuffer *ring = gpu->rb[i];
151
152 if (ring->seqno > ring->memptrs->fence)
153 return true;
154 }
155
156 return false;
Rob Clark37d77c32014-01-11 16:25:08 -0500157}
158
Rob Clark70c70f02014-05-30 14:49:43 -0400159/* Perf-Counters:
160 * The select_reg and select_val are just there for the benefit of the child
161 * class that actually enables the perf counter.. but msm_gpu base class
162 * will handle sampling/displaying the counters.
163 */
164
165struct msm_gpu_perfcntr {
166 uint32_t select_reg;
167 uint32_t sample_reg;
168 uint32_t select_val;
169 const char *name;
170};
171
Jordan Crousef7de1542017-10-20 11:06:55 -0600172struct msm_gpu_submitqueue {
173 int id;
174 u32 flags;
175 u32 prio;
176 int faults;
177 struct list_head node;
178 struct kref ref;
179};
180
Jordan Crousee00e4732018-07-24 10:33:24 -0600181struct msm_gpu_state {
182 struct timeval time;
183
184 struct {
185 u64 iova;
186 u32 fence;
187 u32 seqno;
188 u32 rptr;
189 u32 wptr;
190 } ring[MSM_GPU_MAX_RINGS];
191
192 int nr_registers;
193 u32 *registers;
194
195 u32 rbbm_status;
196};
197
Rob Clark7198e6b2013-07-19 12:59:32 -0400198static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
199{
200 msm_writel(data, gpu->mmio + (reg << 2));
201}
202
203static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
204{
205 return msm_readl(gpu->mmio + (reg << 2));
206}
207
Jordan Crouseae53a822016-11-28 12:28:28 -0700208static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
209{
210 uint32_t val = gpu_read(gpu, reg);
211
212 val &= ~mask;
213 gpu_write(gpu, reg, val | or);
214}
215
216static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
217{
218 u64 val;
219
220 /*
221 * Why not a readq here? Two reasons: 1) many of the LO registers are
222 * not quad word aligned and 2) the GPU hardware designers have a bit
223 * of a history of putting registers where they fit, especially in
224 * spins. The longer a GPU family goes the higher the chance that
225 * we'll get burned. We could do a series of validity checks if we
226 * wanted to, but really is a readq() that much better? Nah.
227 */
228
229 /*
230 * For some lo/hi registers (like perfcounters), the hi value is latched
231 * when the lo is read, so make sure to read the lo first to trigger
232 * that
233 */
234 val = (u64) msm_readl(gpu->mmio + (lo << 2));
235 val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
236
237 return val;
238}
239
240static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
241{
242 /* Why not a writeq here? Read the screed above */
243 msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
244 msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
245}
246
Rob Clark7198e6b2013-07-19 12:59:32 -0400247int msm_gpu_pm_suspend(struct msm_gpu *gpu);
248int msm_gpu_pm_resume(struct msm_gpu *gpu);
249
Rob Clarkeeb75472017-02-10 15:36:33 -0500250int msm_gpu_hw_init(struct msm_gpu *gpu);
251
Rob Clark70c70f02014-05-30 14:49:43 -0400252void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
253void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
254int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
255 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
256
Rob Clark7198e6b2013-07-19 12:59:32 -0400257void msm_gpu_retire(struct msm_gpu *gpu);
Rob Clarkf44d32c2016-06-16 16:37:38 -0400258void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
Rob Clark7198e6b2013-07-19 12:59:32 -0400259 struct msm_file_private *ctx);
260
261int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
262 struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
Jordan Crouse5770fc72017-05-08 14:35:03 -0600263 const char *name, struct msm_gpu_config *config);
264
Rob Clark7198e6b2013-07-19 12:59:32 -0400265void msm_gpu_cleanup(struct msm_gpu *gpu);
266
Rob Clarke2550b72014-09-05 13:30:27 -0400267struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
Rob Clarkbfd28b12014-09-05 13:06:37 -0400268void __init adreno_register(void);
269void __exit adreno_unregister(void);
Rob Clark7198e6b2013-07-19 12:59:32 -0400270
Jordan Crousef7de1542017-10-20 11:06:55 -0600271static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
272{
273 if (queue)
274 kref_put(&queue->ref, msm_submitqueue_destroy);
275}
276
Rob Clark7198e6b2013-07-19 12:59:32 -0400277#endif /* __MSM_GPU_H__ */