1a8c21a54SThe etnaviv authors /*
2a8c21a54SThe etnaviv authors  * Copyright (C) 2015 Etnaviv Project
3a8c21a54SThe etnaviv authors  *
4a8c21a54SThe etnaviv authors  * This program is free software; you can redistribute it and/or modify it
5a8c21a54SThe etnaviv authors  * under the terms of the GNU General Public License version 2 as published by
6a8c21a54SThe etnaviv authors  * the Free Software Foundation.
7a8c21a54SThe etnaviv authors  *
8a8c21a54SThe etnaviv authors  * This program is distributed in the hope that it will be useful, but WITHOUT
9a8c21a54SThe etnaviv authors  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10a8c21a54SThe etnaviv authors  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11a8c21a54SThe etnaviv authors  * more details.
12a8c21a54SThe etnaviv authors  *
13a8c21a54SThe etnaviv authors  * You should have received a copy of the GNU General Public License along with
14a8c21a54SThe etnaviv authors  * this program.  If not, see <http://www.gnu.org/licenses/>.
15a8c21a54SThe etnaviv authors  */
16a8c21a54SThe etnaviv authors 
17a8c21a54SThe etnaviv authors #ifndef __ETNAVIV_GPU_H__
18a8c21a54SThe etnaviv authors #define __ETNAVIV_GPU_H__
19a8c21a54SThe etnaviv authors 
20a8c21a54SThe etnaviv authors #include <linux/clk.h>
21a8c21a54SThe etnaviv authors #include <linux/regulator/consumer.h>
22a8c21a54SThe etnaviv authors 
23a8c21a54SThe etnaviv authors #include "etnaviv_drv.h"
24a8c21a54SThe etnaviv authors 
25a8c21a54SThe etnaviv authors struct etnaviv_gem_submit;
26b6325f40SRussell King struct etnaviv_vram_mapping;
27a8c21a54SThe etnaviv authors 
28a8c21a54SThe etnaviv authors struct etnaviv_chip_identity {
29a8c21a54SThe etnaviv authors 	/* Chip model. */
30a8c21a54SThe etnaviv authors 	u32 model;
31a8c21a54SThe etnaviv authors 
32a8c21a54SThe etnaviv authors 	/* Revision value.*/
33a8c21a54SThe etnaviv authors 	u32 revision;
34a8c21a54SThe etnaviv authors 
35a8c21a54SThe etnaviv authors 	/* Supported feature fields. */
36a8c21a54SThe etnaviv authors 	u32 features;
37a8c21a54SThe etnaviv authors 
38a8c21a54SThe etnaviv authors 	/* Supported minor feature fields. */
39a8c21a54SThe etnaviv authors 	u32 minor_features0;
40a8c21a54SThe etnaviv authors 
41a8c21a54SThe etnaviv authors 	/* Supported minor feature 1 fields. */
42a8c21a54SThe etnaviv authors 	u32 minor_features1;
43a8c21a54SThe etnaviv authors 
44a8c21a54SThe etnaviv authors 	/* Supported minor feature 2 fields. */
45a8c21a54SThe etnaviv authors 	u32 minor_features2;
46a8c21a54SThe etnaviv authors 
47a8c21a54SThe etnaviv authors 	/* Supported minor feature 3 fields. */
48a8c21a54SThe etnaviv authors 	u32 minor_features3;
49a8c21a54SThe etnaviv authors 
50602eb489SRussell King 	/* Supported minor feature 4 fields. */
51602eb489SRussell King 	u32 minor_features4;
52602eb489SRussell King 
53602eb489SRussell King 	/* Supported minor feature 5 fields. */
54602eb489SRussell King 	u32 minor_features5;
55602eb489SRussell King 
56a8c21a54SThe etnaviv authors 	/* Number of streams supported. */
57a8c21a54SThe etnaviv authors 	u32 stream_count;
58a8c21a54SThe etnaviv authors 
59a8c21a54SThe etnaviv authors 	/* Total number of temporary registers per thread. */
60a8c21a54SThe etnaviv authors 	u32 register_max;
61a8c21a54SThe etnaviv authors 
62a8c21a54SThe etnaviv authors 	/* Maximum number of threads. */
63a8c21a54SThe etnaviv authors 	u32 thread_count;
64a8c21a54SThe etnaviv authors 
65a8c21a54SThe etnaviv authors 	/* Number of shader cores. */
66a8c21a54SThe etnaviv authors 	u32 shader_core_count;
67a8c21a54SThe etnaviv authors 
68a8c21a54SThe etnaviv authors 	/* Size of the vertex cache. */
69a8c21a54SThe etnaviv authors 	u32 vertex_cache_size;
70a8c21a54SThe etnaviv authors 
71a8c21a54SThe etnaviv authors 	/* Number of entries in the vertex output buffer. */
72a8c21a54SThe etnaviv authors 	u32 vertex_output_buffer_size;
73a8c21a54SThe etnaviv authors 
74a8c21a54SThe etnaviv authors 	/* Number of pixel pipes. */
75a8c21a54SThe etnaviv authors 	u32 pixel_pipes;
76a8c21a54SThe etnaviv authors 
77a8c21a54SThe etnaviv authors 	/* Number of instructions. */
78a8c21a54SThe etnaviv authors 	u32 instruction_count;
79a8c21a54SThe etnaviv authors 
80a8c21a54SThe etnaviv authors 	/* Number of constants. */
81a8c21a54SThe etnaviv authors 	u32 num_constants;
82a8c21a54SThe etnaviv authors 
83a8c21a54SThe etnaviv authors 	/* Buffer size */
84a8c21a54SThe etnaviv authors 	u32 buffer_size;
85602eb489SRussell King 
86602eb489SRussell King 	/* Number of varyings */
87602eb489SRussell King 	u8 varyings_count;
88a8c21a54SThe etnaviv authors };
89a8c21a54SThe etnaviv authors 
90a8c21a54SThe etnaviv authors struct etnaviv_event {
91a8c21a54SThe etnaviv authors 	bool used;
92a8c21a54SThe etnaviv authors 	struct fence *fence;
93a8c21a54SThe etnaviv authors };
94a8c21a54SThe etnaviv authors 
95a8c21a54SThe etnaviv authors struct etnaviv_cmdbuf;
96a8c21a54SThe etnaviv authors 
97a8c21a54SThe etnaviv authors struct etnaviv_gpu {
98a8c21a54SThe etnaviv authors 	struct drm_device *drm;
99a8c21a54SThe etnaviv authors 	struct device *dev;
100a8c21a54SThe etnaviv authors 	struct mutex lock;
101a8c21a54SThe etnaviv authors 	struct etnaviv_chip_identity identity;
102a8c21a54SThe etnaviv authors 	struct etnaviv_file_private *lastctx;
103a8c21a54SThe etnaviv authors 	bool switch_context;
104a8c21a54SThe etnaviv authors 
105a8c21a54SThe etnaviv authors 	/* 'ring'-buffer: */
106a8c21a54SThe etnaviv authors 	struct etnaviv_cmdbuf *buffer;
107f6086311SRussell King 	int exec_state;
108a8c21a54SThe etnaviv authors 
109a8c21a54SThe etnaviv authors 	/* bus base address of memory  */
110a8c21a54SThe etnaviv authors 	u32 memory_base;
111a8c21a54SThe etnaviv authors 
112a8c21a54SThe etnaviv authors 	/* event management: */
113a8c21a54SThe etnaviv authors 	struct etnaviv_event event[30];
114a8c21a54SThe etnaviv authors 	struct completion event_free;
115a8c21a54SThe etnaviv authors 	spinlock_t event_spinlock;
116a8c21a54SThe etnaviv authors 
117a8c21a54SThe etnaviv authors 	/* list of currently in-flight command buffers */
118a8c21a54SThe etnaviv authors 	struct list_head active_cmd_list;
119a8c21a54SThe etnaviv authors 
120a8c21a54SThe etnaviv authors 	u32 idle_mask;
121a8c21a54SThe etnaviv authors 
122a8c21a54SThe etnaviv authors 	/* Fencing support */
123a8c21a54SThe etnaviv authors 	u32 next_fence;
124a8c21a54SThe etnaviv authors 	u32 active_fence;
125a8c21a54SThe etnaviv authors 	u32 completed_fence;
126a8c21a54SThe etnaviv authors 	u32 retired_fence;
127a8c21a54SThe etnaviv authors 	wait_queue_head_t fence_event;
12876bf0db5SChristian König 	u64 fence_context;
129a8c21a54SThe etnaviv authors 	spinlock_t fence_spinlock;
130a8c21a54SThe etnaviv authors 
131a8c21a54SThe etnaviv authors 	/* worker for handling active-list retiring: */
132a8c21a54SThe etnaviv authors 	struct work_struct retire_work;
133a8c21a54SThe etnaviv authors 
134a8c21a54SThe etnaviv authors 	void __iomem *mmio;
135a8c21a54SThe etnaviv authors 	int irq;
136a8c21a54SThe etnaviv authors 
137a8c21a54SThe etnaviv authors 	struct etnaviv_iommu *mmu;
138a8c21a54SThe etnaviv authors 
139a8c21a54SThe etnaviv authors 	/* Power Control: */
140a8c21a54SThe etnaviv authors 	struct clk *clk_bus;
141a8c21a54SThe etnaviv authors 	struct clk *clk_core;
142a8c21a54SThe etnaviv authors 	struct clk *clk_shader;
143a8c21a54SThe etnaviv authors 
144a8c21a54SThe etnaviv authors 	/* Hang Detction: */
145a8c21a54SThe etnaviv authors #define DRM_ETNAVIV_HANGCHECK_PERIOD 500 /* in ms */
146a8c21a54SThe etnaviv authors #define DRM_ETNAVIV_HANGCHECK_JIFFIES msecs_to_jiffies(DRM_ETNAVIV_HANGCHECK_PERIOD)
147a8c21a54SThe etnaviv authors 	struct timer_list hangcheck_timer;
148a8c21a54SThe etnaviv authors 	u32 hangcheck_fence;
149a8c21a54SThe etnaviv authors 	u32 hangcheck_dma_addr;
150a8c21a54SThe etnaviv authors 	struct work_struct recover_work;
151a8c21a54SThe etnaviv authors };
152a8c21a54SThe etnaviv authors 
153a8c21a54SThe etnaviv authors struct etnaviv_cmdbuf {
154a8c21a54SThe etnaviv authors 	/* device this cmdbuf is allocated for */
155a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
156a8c21a54SThe etnaviv authors 	/* user context key, must be unique between all active users */
157a8c21a54SThe etnaviv authors 	struct etnaviv_file_private *ctx;
158a8c21a54SThe etnaviv authors 	/* cmdbuf properties */
159a8c21a54SThe etnaviv authors 	void *vaddr;
160a8c21a54SThe etnaviv authors 	dma_addr_t paddr;
161a8c21a54SThe etnaviv authors 	u32 size;
162a8c21a54SThe etnaviv authors 	u32 user_size;
163a8c21a54SThe etnaviv authors 	/* fence after which this buffer is to be disposed */
164a8c21a54SThe etnaviv authors 	struct fence *fence;
165a8c21a54SThe etnaviv authors 	/* target exec state */
166a8c21a54SThe etnaviv authors 	u32 exec_state;
167a8c21a54SThe etnaviv authors 	/* per GPU in-flight list */
168a8c21a54SThe etnaviv authors 	struct list_head node;
169a8c21a54SThe etnaviv authors 	/* BOs attached to this command buffer */
170a8c21a54SThe etnaviv authors 	unsigned int nr_bos;
171b6325f40SRussell King 	struct etnaviv_vram_mapping *bo_map[0];
172a8c21a54SThe etnaviv authors };
173a8c21a54SThe etnaviv authors 
174a8c21a54SThe etnaviv authors static inline void gpu_write(struct etnaviv_gpu *gpu, u32 reg, u32 data)
175a8c21a54SThe etnaviv authors {
176a8c21a54SThe etnaviv authors 	etnaviv_writel(data, gpu->mmio + reg);
177a8c21a54SThe etnaviv authors }
178a8c21a54SThe etnaviv authors 
179a8c21a54SThe etnaviv authors static inline u32 gpu_read(struct etnaviv_gpu *gpu, u32 reg)
180a8c21a54SThe etnaviv authors {
181a8c21a54SThe etnaviv authors 	return etnaviv_readl(gpu->mmio + reg);
182a8c21a54SThe etnaviv authors }
183a8c21a54SThe etnaviv authors 
184a8c21a54SThe etnaviv authors static inline bool fence_completed(struct etnaviv_gpu *gpu, u32 fence)
185a8c21a54SThe etnaviv authors {
186a8c21a54SThe etnaviv authors 	return fence_after_eq(gpu->completed_fence, fence);
187a8c21a54SThe etnaviv authors }
188a8c21a54SThe etnaviv authors 
189a8c21a54SThe etnaviv authors static inline bool fence_retired(struct etnaviv_gpu *gpu, u32 fence)
190a8c21a54SThe etnaviv authors {
191a8c21a54SThe etnaviv authors 	return fence_after_eq(gpu->retired_fence, fence);
192a8c21a54SThe etnaviv authors }
193a8c21a54SThe etnaviv authors 
194a8c21a54SThe etnaviv authors int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value);
195a8c21a54SThe etnaviv authors 
196a8c21a54SThe etnaviv authors int etnaviv_gpu_init(struct etnaviv_gpu *gpu);
197a8c21a54SThe etnaviv authors 
198a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
199a8c21a54SThe etnaviv authors int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m);
200a8c21a54SThe etnaviv authors #endif
201a8c21a54SThe etnaviv authors 
202a8c21a54SThe etnaviv authors int etnaviv_gpu_fence_sync_obj(struct etnaviv_gem_object *etnaviv_obj,
203a8c21a54SThe etnaviv authors 	unsigned int context, bool exclusive);
204a8c21a54SThe etnaviv authors 
205a8c21a54SThe etnaviv authors void etnaviv_gpu_retire(struct etnaviv_gpu *gpu);
206a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu,
207a8c21a54SThe etnaviv authors 	u32 fence, struct timespec *timeout);
208a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu,
209a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout);
210a8c21a54SThe etnaviv authors int etnaviv_gpu_submit(struct etnaviv_gpu *gpu,
211a8c21a54SThe etnaviv authors 	struct etnaviv_gem_submit *submit, struct etnaviv_cmdbuf *cmdbuf);
212a8c21a54SThe etnaviv authors struct etnaviv_cmdbuf *etnaviv_gpu_cmdbuf_new(struct etnaviv_gpu *gpu,
213a8c21a54SThe etnaviv authors 					      u32 size, size_t nr_bos);
214a8c21a54SThe etnaviv authors void etnaviv_gpu_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf);
215a8c21a54SThe etnaviv authors int etnaviv_gpu_pm_get_sync(struct etnaviv_gpu *gpu);
216a8c21a54SThe etnaviv authors void etnaviv_gpu_pm_put(struct etnaviv_gpu *gpu);
217b88163e3SLucas Stach int etnaviv_gpu_wait_idle(struct etnaviv_gpu *gpu, unsigned int timeout_ms);
218a8c21a54SThe etnaviv authors 
219a8c21a54SThe etnaviv authors extern struct platform_driver etnaviv_gpu_driver;
220a8c21a54SThe etnaviv authors 
221a8c21a54SThe etnaviv authors #endif /* __ETNAVIV_GPU_H__ */
222