1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
3 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
4 
5 #ifndef __PANFROST_DEVICE_H__
6 #define __PANFROST_DEVICE_H__
7 
8 #include <linux/atomic.h>
9 #include <linux/io-pgtable.h>
10 #include <linux/spinlock.h>
11 #include <drm/drm_device.h>
12 #include <drm/drm_mm.h>
13 #include <drm/gpu_scheduler.h>
14 
15 struct panfrost_device;
16 struct panfrost_mmu;
17 struct panfrost_job_slot;
18 struct panfrost_job;
19 struct panfrost_perfcnt;
20 
21 #define NUM_JOB_SLOTS 3
22 
23 struct panfrost_features {
24 	u16 id;
25 	u16 revision;
26 
27 	u64 shader_present;
28 	u64 tiler_present;
29 	u64 l2_present;
30 	u64 stack_present;
31 	u32 as_present;
32 	u32 js_present;
33 
34 	u32 l2_features;
35 	u32 core_features;
36 	u32 tiler_features;
37 	u32 mem_features;
38 	u32 mmu_features;
39 	u32 thread_features;
40 	u32 max_threads;
41 	u32 thread_max_workgroup_sz;
42 	u32 thread_max_barrier_sz;
43 	u32 coherency_features;
44 	u32 texture_features[4];
45 	u32 js_features[16];
46 
47 	u32 nr_core_groups;
48 	u32 thread_tls_alloc;
49 
50 	unsigned long hw_features[64 / BITS_PER_LONG];
51 	unsigned long hw_issues[64 / BITS_PER_LONG];
52 };
53 
54 struct panfrost_devfreq_slot {
55 	ktime_t busy_time;
56 	ktime_t idle_time;
57 	ktime_t time_last_update;
58 	bool busy;
59 };
60 
61 struct panfrost_device {
62 	struct device *dev;
63 	struct drm_device *ddev;
64 	struct platform_device *pdev;
65 
66 	spinlock_t hwaccess_lock;
67 
68 	void __iomem *iomem;
69 	struct clk *clock;
70 	struct clk *bus_clock;
71 	struct regulator *regulator;
72 	struct reset_control *rstc;
73 
74 	struct panfrost_features features;
75 
76 	spinlock_t as_lock;
77 	unsigned long as_in_use_mask;
78 	unsigned long as_alloc_mask;
79 	struct list_head as_lru_list;
80 
81 	struct panfrost_job_slot *js;
82 
83 	struct panfrost_job *jobs[NUM_JOB_SLOTS];
84 	struct list_head scheduled_jobs;
85 
86 	struct panfrost_perfcnt *perfcnt;
87 
88 	struct mutex sched_lock;
89 	struct mutex reset_lock;
90 
91 	struct mutex shrinker_lock;
92 	struct list_head shrinker_list;
93 	struct shrinker shrinker;
94 
95 	struct {
96 		struct devfreq *devfreq;
97 		struct thermal_cooling_device *cooling;
98 		unsigned long cur_freq;
99 		unsigned long cur_volt;
100 		struct panfrost_devfreq_slot slot[NUM_JOB_SLOTS];
101 	} devfreq;
102 };
103 
104 struct panfrost_mmu {
105 	struct io_pgtable_cfg pgtbl_cfg;
106 	struct io_pgtable_ops *pgtbl_ops;
107 	struct mutex lock;
108 	int as;
109 	atomic_t as_count;
110 	struct list_head list;
111 };
112 
113 struct panfrost_file_priv {
114 	struct panfrost_device *pfdev;
115 
116 	struct drm_sched_entity sched_entity[NUM_JOB_SLOTS];
117 
118 	struct panfrost_mmu mmu;
119 	struct drm_mm mm;
120 	spinlock_t mm_lock;
121 };
122 
123 static inline struct panfrost_device *to_panfrost_device(struct drm_device *ddev)
124 {
125 	return ddev->dev_private;
126 }
127 
128 static inline int panfrost_model_cmp(struct panfrost_device *pfdev, s32 id)
129 {
130 	s32 match_id = pfdev->features.id;
131 
132 	if (match_id & 0xf000)
133 		match_id &= 0xf00f;
134 	return match_id - id;
135 }
136 
137 static inline bool panfrost_model_is_bifrost(struct panfrost_device *pfdev)
138 {
139 	return panfrost_model_cmp(pfdev, 0x1000) >= 0;
140 }
141 
142 static inline bool panfrost_model_eq(struct panfrost_device *pfdev, s32 id)
143 {
144 	return !panfrost_model_cmp(pfdev, id);
145 }
146 
147 int panfrost_unstable_ioctl_check(void);
148 
149 int panfrost_device_init(struct panfrost_device *pfdev);
150 void panfrost_device_fini(struct panfrost_device *pfdev);
151 void panfrost_device_reset(struct panfrost_device *pfdev);
152 
153 int panfrost_device_resume(struct device *dev);
154 int panfrost_device_suspend(struct device *dev);
155 
156 const char *panfrost_exception_name(struct panfrost_device *pfdev, u32 exception_code);
157 
158 #endif
159