1
2
3
4
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
21struct msm_gem_submit;
22struct msm_gpu_perfcntr;
23struct msm_gpu_state;
24
25struct msm_gpu_config {
26 const char *ioname;
27 unsigned int nr_rings;
28};
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44struct 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
60 void (*show)(struct msm_gpu *gpu, struct msm_gpu_state *state,
61 struct drm_printer *p);
62
63 void (*debugfs_init)(struct msm_gpu *gpu, struct drm_minor *minor);
64#endif
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);
75};
76
77
78struct msm_gpu_fault_info {
79 u64 ttbr0;
80 unsigned long iova;
81 int flags;
82 const char *type;
83 const char *block;
84};
85
86
87
88
89struct msm_gpu_devfreq {
90
91 struct devfreq *devfreq;
92
93
94
95
96
97
98 struct dev_pm_qos_request idle_freq;
99
100
101
102
103
104
105
106 struct dev_pm_qos_request boost_freq;
107
108
109
110
111
112
113
114
115 u64 busy_cycles;
116
117
118 ktime_t time;
119
120
121 ktime_t idle_time;
122
123
124
125
126
127
128 struct msm_hrtimer_work idle_work;
129
130
131
132
133
134
135
136 struct msm_hrtimer_work boost_work;
137};
138
139struct msm_gpu {
140 const char *name;
141 struct drm_device *dev;
142 struct platform_device *pdev;
143 const struct msm_gpu_funcs *funcs;
144
145 struct adreno_smmu_priv adreno_smmu;
146
147
148 spinlock_t perf_lock;
149 bool perfcntr_active;
150 struct {
151 bool active;
152 ktime_t time;
153 } last_sample;
154 uint32_t totaltime, activetime;
155 uint32_t last_cntrs[5];
156 const struct msm_gpu_perfcntr *perfcntrs;
157 uint32_t num_perfcntrs;
158
159 struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
160 int nr_rings;
161
162
163
164
165
166
167 refcount_t sysprof_active;
168
169
170
171
172
173
174
175
176
177
178 int cur_ctx_seqno;
179
180
181
182
183
184 struct list_head active_list;
185
186
187
188
189
190
191
192
193
194 struct mutex lock;
195
196
197
198
199
200
201
202
203
204 int active_submits;
205
206
207 struct mutex active_lock;
208
209
210 bool needs_hw_init;
211
212
213
214
215
216 int global_faults;
217
218 void __iomem *mmio;
219 int irq;
220
221 struct msm_gem_address_space *aspace;
222
223
224 struct regulator *gpu_reg, *gpu_cx;
225 struct clk_bulk_data *grp_clks;
226 int nr_clocks;
227 struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
228 uint32_t fast_rate;
229
230
231
232#define DRM_MSM_INACTIVE_PERIOD 66
233
234#define DRM_MSM_HANGCHECK_DEFAULT_PERIOD 500
235 struct timer_list hangcheck_timer;
236
237
238 struct msm_gpu_fault_info fault_info;
239
240
241 struct kthread_work fault_work;
242
243
244 struct kthread_work recover_work;
245
246
247 wait_queue_head_t retire_event;
248
249
250 struct kthread_work retire_work;
251
252
253 struct kthread_worker *worker;
254
255 struct drm_gem_object *memptrs_bo;
256
257 struct msm_gpu_devfreq devfreq;
258
259 uint32_t suspend_count;
260
261 struct msm_gpu_state *crashstate;
262
263
264 bool clamp_to_idle;
265
266
267 bool hw_apriv;
268
269 struct thermal_cooling_device *cooling;
270};
271
272static inline struct msm_gpu *dev_to_gpu(struct device *dev)
273{
274 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
275 return container_of(adreno_smmu, struct msm_gpu, adreno_smmu);
276}
277
278
279#define MSM_GPU_RINGBUFFER_SZ SZ_32K
280#define MSM_GPU_RINGBUFFER_BLKSIZE 32
281
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)))
285
286static inline bool msm_gpu_active(struct msm_gpu *gpu)
287{
288 int i;
289
290 for (i = 0; i < gpu->nr_rings; i++) {
291 struct msm_ringbuffer *ring = gpu->rb[i];
292
293 if (fence_after(ring->seqno, ring->memptrs->fence))
294 return true;
295 }
296
297 return false;
298}
299
300
301
302
303
304
305
306struct msm_gpu_perfcntr {
307 uint32_t select_reg;
308 uint32_t sample_reg;
309 uint32_t select_val;
310 const char *name;
311};
312
313
314
315
316
317
318#define NR_SCHED_PRIORITIES (1 + DRM_SCHED_PRIORITY_HIGH - DRM_SCHED_PRIORITY_MIN)
319
320
321
322
323
324
325
326
327
328
329
330
331struct msm_file_private {
332 rwlock_t queuelock;
333 struct list_head submitqueues;
334 int queueid;
335 struct msm_gem_address_space *aspace;
336 struct kref ref;
337 int seqno;
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 int sysprof;
356
357
358
359
360
361
362
363
364
365
366
367
368 struct drm_sched_entity *entities[NR_SCHED_PRIORITIES * MSM_GPU_MAX_RINGS];
369};
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396static inline int msm_gpu_convert_priority(struct msm_gpu *gpu, int prio,
397 unsigned *ring_nr, enum drm_sched_priority *sched_prio)
398{
399 unsigned rn, sp;
400
401 rn = div_u64_rem(prio, NR_SCHED_PRIORITIES, &sp);
402
403
404
405
406 sp = NR_SCHED_PRIORITIES - sp - 1;
407
408 if (rn >= gpu->nr_rings)
409 return -EINVAL;
410
411 *ring_nr = rn;
412 *sched_prio = sp;
413
414 return 0;
415}
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441struct msm_gpu_submitqueue {
442 int id;
443 u32 flags;
444 u32 ring_nr;
445 int faults;
446 uint32_t last_fence;
447 struct msm_file_private *ctx;
448 struct list_head node;
449 struct idr fence_idr;
450 struct mutex lock;
451 struct kref ref;
452 struct drm_sched_entity *entity;
453};
454
455struct msm_gpu_state_bo {
456 u64 iova;
457 size_t size;
458 void *data;
459 bool encoded;
460};
461
462struct msm_gpu_state {
463 struct kref ref;
464 struct timespec64 time;
465
466 struct {
467 u64 iova;
468 u32 fence;
469 u32 seqno;
470 u32 rptr;
471 u32 wptr;
472 void *data;
473 int data_size;
474 bool encoded;
475 } ring[MSM_GPU_MAX_RINGS];
476
477 int nr_registers;
478 u32 *registers;
479
480 u32 rbbm_status;
481
482 char *comm;
483 char *cmd;
484
485 struct msm_gpu_fault_info fault_info;
486
487 int nr_bos;
488 struct msm_gpu_state_bo *bos;
489};
490
491static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
492{
493 msm_writel(data, gpu->mmio + (reg << 2));
494}
495
496static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
497{
498 return msm_readl(gpu->mmio + (reg << 2));
499}
500
501static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
502{
503 msm_rmw(gpu->mmio + (reg << 2), mask, or);
504}
505
506static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
507{
508 u64 val;
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524 val = (u64) msm_readl(gpu->mmio + (lo << 2));
525 val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
526
527 return val;
528}
529
530static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
531{
532
533 msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
534 msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
535}
536
537int msm_gpu_pm_suspend(struct msm_gpu *gpu);
538int msm_gpu_pm_resume(struct msm_gpu *gpu);
539
540int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx);
541struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
542 u32 id);
543int msm_submitqueue_create(struct drm_device *drm,
544 struct msm_file_private *ctx,
545 u32 prio, u32 flags, u32 *id);
546int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
547 struct drm_msm_submitqueue_query *args);
548int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id);
549void msm_submitqueue_close(struct msm_file_private *ctx);
550
551void msm_submitqueue_destroy(struct kref *kref);
552
553int msm_file_private_set_sysprof(struct msm_file_private *ctx,
554 struct msm_gpu *gpu, int sysprof);
555void __msm_file_private_destroy(struct kref *kref);
556
557static inline void msm_file_private_put(struct msm_file_private *ctx)
558{
559 kref_put(&ctx->ref, __msm_file_private_destroy);
560}
561
562static inline struct msm_file_private *msm_file_private_get(
563 struct msm_file_private *ctx)
564{
565 kref_get(&ctx->ref);
566 return ctx;
567}
568
569void msm_devfreq_init(struct msm_gpu *gpu);
570void msm_devfreq_cleanup(struct msm_gpu *gpu);
571void msm_devfreq_resume(struct msm_gpu *gpu);
572void msm_devfreq_suspend(struct msm_gpu *gpu);
573void msm_devfreq_boost(struct msm_gpu *gpu, unsigned factor);
574void msm_devfreq_active(struct msm_gpu *gpu);
575void msm_devfreq_idle(struct msm_gpu *gpu);
576
577int msm_gpu_hw_init(struct msm_gpu *gpu);
578
579void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
580void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
581int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
582 uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
583
584void msm_gpu_retire(struct msm_gpu *gpu);
585void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit);
586
587int 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);
590
591struct msm_gem_address_space *
592msm_gpu_create_private_address_space(struct msm_gpu *gpu, struct task_struct *task);
593
594void msm_gpu_cleanup(struct msm_gpu *gpu);
595
596struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
597void __init adreno_register(void);
598void __exit adreno_unregister(void);
599
600static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
601{
602 if (queue)
603 kref_put(&queue->ref, msm_submitqueue_destroy);
604}
605
606static inline struct msm_gpu_state *msm_gpu_crashstate_get(struct msm_gpu *gpu)
607{
608 struct msm_gpu_state *state = NULL;
609
610 mutex_lock(&gpu->lock);
611
612 if (gpu->crashstate) {
613 kref_get(&gpu->crashstate->ref);
614 state = gpu->crashstate;
615 }
616
617 mutex_unlock(&gpu->lock);
618
619 return state;
620}
621
622static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
623{
624 mutex_lock(&gpu->lock);
625
626 if (gpu->crashstate) {
627 if (gpu->funcs->gpu_state_put(gpu->crashstate))
628 gpu->crashstate = NULL;
629 }
630
631 mutex_unlock(&gpu->lock);
632}
633
634
635
636
637
638#define check_apriv(gpu, flags) \
639 (((gpu)->hw_apriv ? MSM_BO_MAP_PRIV : 0) | (flags))
640
641
642#endif
643