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 #include <linux/component.h>
18f54d1867SChris Wilson #include <linux/dma-fence.h>
19a8c21a54SThe etnaviv authors #include <linux/moduleparam.h>
20a8c21a54SThe etnaviv authors #include <linux/of_device.h>
21bcdfb5e5SRussell King #include <linux/thermal.h>
22ea1f5729SLucas Stach 
23ea1f5729SLucas Stach #include "etnaviv_cmdbuf.h"
24a8c21a54SThe etnaviv authors #include "etnaviv_dump.h"
25a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
26a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
27a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
28a8c21a54SThe etnaviv authors #include "common.xml.h"
29a8c21a54SThe etnaviv authors #include "state.xml.h"
30a8c21a54SThe etnaviv authors #include "state_hi.xml.h"
31a8c21a54SThe etnaviv authors #include "cmdstream.xml.h"
32a8c21a54SThe etnaviv authors 
33a8c21a54SThe etnaviv authors static const struct platform_device_id gpu_ids[] = {
34a8c21a54SThe etnaviv authors 	{ .name = "etnaviv-gpu,2d" },
35a8c21a54SThe etnaviv authors 	{ },
36a8c21a54SThe etnaviv authors };
37a8c21a54SThe etnaviv authors 
38a8c21a54SThe etnaviv authors static bool etnaviv_dump_core = true;
39a8c21a54SThe etnaviv authors module_param_named(dump_core, etnaviv_dump_core, bool, 0600);
40a8c21a54SThe etnaviv authors 
41a8c21a54SThe etnaviv authors /*
42a8c21a54SThe etnaviv authors  * Driver functions:
43a8c21a54SThe etnaviv authors  */
44a8c21a54SThe etnaviv authors 
45a8c21a54SThe etnaviv authors int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value)
46a8c21a54SThe etnaviv authors {
47a8c21a54SThe etnaviv authors 	switch (param) {
48a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_MODEL:
49a8c21a54SThe etnaviv authors 		*value = gpu->identity.model;
50a8c21a54SThe etnaviv authors 		break;
51a8c21a54SThe etnaviv authors 
52a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_REVISION:
53a8c21a54SThe etnaviv authors 		*value = gpu->identity.revision;
54a8c21a54SThe etnaviv authors 		break;
55a8c21a54SThe etnaviv authors 
56a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_0:
57a8c21a54SThe etnaviv authors 		*value = gpu->identity.features;
58a8c21a54SThe etnaviv authors 		break;
59a8c21a54SThe etnaviv authors 
60a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_1:
61a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features0;
62a8c21a54SThe etnaviv authors 		break;
63a8c21a54SThe etnaviv authors 
64a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_2:
65a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features1;
66a8c21a54SThe etnaviv authors 		break;
67a8c21a54SThe etnaviv authors 
68a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_3:
69a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features2;
70a8c21a54SThe etnaviv authors 		break;
71a8c21a54SThe etnaviv authors 
72a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_4:
73a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features3;
74a8c21a54SThe etnaviv authors 		break;
75a8c21a54SThe etnaviv authors 
76602eb489SRussell King 	case ETNAVIV_PARAM_GPU_FEATURES_5:
77602eb489SRussell King 		*value = gpu->identity.minor_features4;
78602eb489SRussell King 		break;
79602eb489SRussell King 
80602eb489SRussell King 	case ETNAVIV_PARAM_GPU_FEATURES_6:
81602eb489SRussell King 		*value = gpu->identity.minor_features5;
82602eb489SRussell King 		break;
83602eb489SRussell King 
84a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_STREAM_COUNT:
85a8c21a54SThe etnaviv authors 		*value = gpu->identity.stream_count;
86a8c21a54SThe etnaviv authors 		break;
87a8c21a54SThe etnaviv authors 
88a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_REGISTER_MAX:
89a8c21a54SThe etnaviv authors 		*value = gpu->identity.register_max;
90a8c21a54SThe etnaviv authors 		break;
91a8c21a54SThe etnaviv authors 
92a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_THREAD_COUNT:
93a8c21a54SThe etnaviv authors 		*value = gpu->identity.thread_count;
94a8c21a54SThe etnaviv authors 		break;
95a8c21a54SThe etnaviv authors 
96a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_VERTEX_CACHE_SIZE:
97a8c21a54SThe etnaviv authors 		*value = gpu->identity.vertex_cache_size;
98a8c21a54SThe etnaviv authors 		break;
99a8c21a54SThe etnaviv authors 
100a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_SHADER_CORE_COUNT:
101a8c21a54SThe etnaviv authors 		*value = gpu->identity.shader_core_count;
102a8c21a54SThe etnaviv authors 		break;
103a8c21a54SThe etnaviv authors 
104a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_PIXEL_PIPES:
105a8c21a54SThe etnaviv authors 		*value = gpu->identity.pixel_pipes;
106a8c21a54SThe etnaviv authors 		break;
107a8c21a54SThe etnaviv authors 
108a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_VERTEX_OUTPUT_BUFFER_SIZE:
109a8c21a54SThe etnaviv authors 		*value = gpu->identity.vertex_output_buffer_size;
110a8c21a54SThe etnaviv authors 		break;
111a8c21a54SThe etnaviv authors 
112a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_BUFFER_SIZE:
113a8c21a54SThe etnaviv authors 		*value = gpu->identity.buffer_size;
114a8c21a54SThe etnaviv authors 		break;
115a8c21a54SThe etnaviv authors 
116a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_INSTRUCTION_COUNT:
117a8c21a54SThe etnaviv authors 		*value = gpu->identity.instruction_count;
118a8c21a54SThe etnaviv authors 		break;
119a8c21a54SThe etnaviv authors 
120a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_NUM_CONSTANTS:
121a8c21a54SThe etnaviv authors 		*value = gpu->identity.num_constants;
122a8c21a54SThe etnaviv authors 		break;
123a8c21a54SThe etnaviv authors 
124602eb489SRussell King 	case ETNAVIV_PARAM_GPU_NUM_VARYINGS:
125602eb489SRussell King 		*value = gpu->identity.varyings_count;
126602eb489SRussell King 		break;
127602eb489SRussell King 
128a8c21a54SThe etnaviv authors 	default:
129a8c21a54SThe etnaviv authors 		DBG("%s: invalid param: %u", dev_name(gpu->dev), param);
130a8c21a54SThe etnaviv authors 		return -EINVAL;
131a8c21a54SThe etnaviv authors 	}
132a8c21a54SThe etnaviv authors 
133a8c21a54SThe etnaviv authors 	return 0;
134a8c21a54SThe etnaviv authors }
135a8c21a54SThe etnaviv authors 
136472f79dcSRussell King 
137472f79dcSRussell King #define etnaviv_is_model_rev(gpu, mod, rev) \
138472f79dcSRussell King 	((gpu)->identity.model == chipModel_##mod && \
139472f79dcSRussell King 	 (gpu)->identity.revision == rev)
14052f36ba1SRussell King #define etnaviv_field(val, field) \
14152f36ba1SRussell King 	(((val) & field##__MASK) >> field##__SHIFT)
14252f36ba1SRussell King 
143a8c21a54SThe etnaviv authors static void etnaviv_hw_specs(struct etnaviv_gpu *gpu)
144a8c21a54SThe etnaviv authors {
145a8c21a54SThe etnaviv authors 	if (gpu->identity.minor_features0 &
146a8c21a54SThe etnaviv authors 	    chipMinorFeatures0_MORE_MINOR_FEATURES) {
147602eb489SRussell King 		u32 specs[4];
148602eb489SRussell King 		unsigned int streams;
149a8c21a54SThe etnaviv authors 
150a8c21a54SThe etnaviv authors 		specs[0] = gpu_read(gpu, VIVS_HI_CHIP_SPECS);
151a8c21a54SThe etnaviv authors 		specs[1] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_2);
152602eb489SRussell King 		specs[2] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_3);
153602eb489SRussell King 		specs[3] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_4);
154a8c21a54SThe etnaviv authors 
15552f36ba1SRussell King 		gpu->identity.stream_count = etnaviv_field(specs[0],
15652f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_STREAM_COUNT);
15752f36ba1SRussell King 		gpu->identity.register_max = etnaviv_field(specs[0],
15852f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_REGISTER_MAX);
15952f36ba1SRussell King 		gpu->identity.thread_count = etnaviv_field(specs[0],
16052f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_THREAD_COUNT);
16152f36ba1SRussell King 		gpu->identity.vertex_cache_size = etnaviv_field(specs[0],
16252f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_VERTEX_CACHE_SIZE);
16352f36ba1SRussell King 		gpu->identity.shader_core_count = etnaviv_field(specs[0],
16452f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_SHADER_CORE_COUNT);
16552f36ba1SRussell King 		gpu->identity.pixel_pipes = etnaviv_field(specs[0],
16652f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_PIXEL_PIPES);
167a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size =
16852f36ba1SRussell King 			etnaviv_field(specs[0],
16952f36ba1SRussell King 				VIVS_HI_CHIP_SPECS_VERTEX_OUTPUT_BUFFER_SIZE);
170a8c21a54SThe etnaviv authors 
17152f36ba1SRussell King 		gpu->identity.buffer_size = etnaviv_field(specs[1],
17252f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_BUFFER_SIZE);
17352f36ba1SRussell King 		gpu->identity.instruction_count = etnaviv_field(specs[1],
17452f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_INSTRUCTION_COUNT);
17552f36ba1SRussell King 		gpu->identity.num_constants = etnaviv_field(specs[1],
17652f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_NUM_CONSTANTS);
177602eb489SRussell King 
178602eb489SRussell King 		gpu->identity.varyings_count = etnaviv_field(specs[2],
179602eb489SRussell King 					VIVS_HI_CHIP_SPECS_3_VARYINGS_COUNT);
180602eb489SRussell King 
181602eb489SRussell King 		/* This overrides the value from older register if non-zero */
182602eb489SRussell King 		streams = etnaviv_field(specs[3],
183602eb489SRussell King 					VIVS_HI_CHIP_SPECS_4_STREAM_COUNT);
184602eb489SRussell King 		if (streams)
185602eb489SRussell King 			gpu->identity.stream_count = streams;
186a8c21a54SThe etnaviv authors 	}
187a8c21a54SThe etnaviv authors 
188a8c21a54SThe etnaviv authors 	/* Fill in the stream count if not specified */
189a8c21a54SThe etnaviv authors 	if (gpu->identity.stream_count == 0) {
190a8c21a54SThe etnaviv authors 		if (gpu->identity.model >= 0x1000)
191a8c21a54SThe etnaviv authors 			gpu->identity.stream_count = 4;
192a8c21a54SThe etnaviv authors 		else
193a8c21a54SThe etnaviv authors 			gpu->identity.stream_count = 1;
194a8c21a54SThe etnaviv authors 	}
195a8c21a54SThe etnaviv authors 
196a8c21a54SThe etnaviv authors 	/* Convert the register max value */
197a8c21a54SThe etnaviv authors 	if (gpu->identity.register_max)
198a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 1 << gpu->identity.register_max;
199507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC400)
200a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 32;
201a8c21a54SThe etnaviv authors 	else
202a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 64;
203a8c21a54SThe etnaviv authors 
204a8c21a54SThe etnaviv authors 	/* Convert thread count */
205a8c21a54SThe etnaviv authors 	if (gpu->identity.thread_count)
206a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 1 << gpu->identity.thread_count;
207507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC400)
208a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 64;
209507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC500 ||
210507f8991SRussell King 		 gpu->identity.model == chipModel_GC530)
211a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 128;
212a8c21a54SThe etnaviv authors 	else
213a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 256;
214a8c21a54SThe etnaviv authors 
215a8c21a54SThe etnaviv authors 	if (gpu->identity.vertex_cache_size == 0)
216a8c21a54SThe etnaviv authors 		gpu->identity.vertex_cache_size = 8;
217a8c21a54SThe etnaviv authors 
218a8c21a54SThe etnaviv authors 	if (gpu->identity.shader_core_count == 0) {
219a8c21a54SThe etnaviv authors 		if (gpu->identity.model >= 0x1000)
220a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count = 2;
221a8c21a54SThe etnaviv authors 		else
222a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count = 1;
223a8c21a54SThe etnaviv authors 	}
224a8c21a54SThe etnaviv authors 
225a8c21a54SThe etnaviv authors 	if (gpu->identity.pixel_pipes == 0)
226a8c21a54SThe etnaviv authors 		gpu->identity.pixel_pipes = 1;
227a8c21a54SThe etnaviv authors 
228a8c21a54SThe etnaviv authors 	/* Convert virtex buffer size */
229a8c21a54SThe etnaviv authors 	if (gpu->identity.vertex_output_buffer_size) {
230a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size =
231a8c21a54SThe etnaviv authors 			1 << gpu->identity.vertex_output_buffer_size;
232507f8991SRussell King 	} else if (gpu->identity.model == chipModel_GC400) {
233a8c21a54SThe etnaviv authors 		if (gpu->identity.revision < 0x4000)
234a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 512;
235a8c21a54SThe etnaviv authors 		else if (gpu->identity.revision < 0x4200)
236a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 256;
237a8c21a54SThe etnaviv authors 		else
238a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 128;
239a8c21a54SThe etnaviv authors 	} else {
240a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size = 512;
241a8c21a54SThe etnaviv authors 	}
242a8c21a54SThe etnaviv authors 
243a8c21a54SThe etnaviv authors 	switch (gpu->identity.instruction_count) {
244a8c21a54SThe etnaviv authors 	case 0:
245472f79dcSRussell King 		if (etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
246507f8991SRussell King 		    gpu->identity.model == chipModel_GC880)
247a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count = 512;
248a8c21a54SThe etnaviv authors 		else
249a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count = 256;
250a8c21a54SThe etnaviv authors 		break;
251a8c21a54SThe etnaviv authors 
252a8c21a54SThe etnaviv authors 	case 1:
253a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 1024;
254a8c21a54SThe etnaviv authors 		break;
255a8c21a54SThe etnaviv authors 
256a8c21a54SThe etnaviv authors 	case 2:
257a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 2048;
258a8c21a54SThe etnaviv authors 		break;
259a8c21a54SThe etnaviv authors 
260a8c21a54SThe etnaviv authors 	default:
261a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 256;
262a8c21a54SThe etnaviv authors 		break;
263a8c21a54SThe etnaviv authors 	}
264a8c21a54SThe etnaviv authors 
265a8c21a54SThe etnaviv authors 	if (gpu->identity.num_constants == 0)
266a8c21a54SThe etnaviv authors 		gpu->identity.num_constants = 168;
267602eb489SRussell King 
268602eb489SRussell King 	if (gpu->identity.varyings_count == 0) {
269602eb489SRussell King 		if (gpu->identity.minor_features1 & chipMinorFeatures1_HALTI0)
270602eb489SRussell King 			gpu->identity.varyings_count = 12;
271602eb489SRussell King 		else
272602eb489SRussell King 			gpu->identity.varyings_count = 8;
273602eb489SRussell King 	}
274602eb489SRussell King 
275602eb489SRussell King 	/*
276602eb489SRussell King 	 * For some cores, two varyings are consumed for position, so the
277602eb489SRussell King 	 * maximum varying count needs to be reduced by one.
278602eb489SRussell King 	 */
279602eb489SRussell King 	if (etnaviv_is_model_rev(gpu, GC5000, 0x5434) ||
280602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
281602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5245) ||
282602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
283602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC3000, 0x5435) ||
284602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2200, 0x5244) ||
285602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2100, 0x5108) ||
286602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
287602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC1500, 0x5246) ||
288602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC880, 0x5107) ||
289602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC880, 0x5106))
290602eb489SRussell King 		gpu->identity.varyings_count -= 1;
291a8c21a54SThe etnaviv authors }
292a8c21a54SThe etnaviv authors 
293a8c21a54SThe etnaviv authors static void etnaviv_hw_identify(struct etnaviv_gpu *gpu)
294a8c21a54SThe etnaviv authors {
295a8c21a54SThe etnaviv authors 	u32 chipIdentity;
296a8c21a54SThe etnaviv authors 
297a8c21a54SThe etnaviv authors 	chipIdentity = gpu_read(gpu, VIVS_HI_CHIP_IDENTITY);
298a8c21a54SThe etnaviv authors 
299a8c21a54SThe etnaviv authors 	/* Special case for older graphic cores. */
30052f36ba1SRussell King 	if (etnaviv_field(chipIdentity, VIVS_HI_CHIP_IDENTITY_FAMILY) == 0x01) {
301507f8991SRussell King 		gpu->identity.model    = chipModel_GC500;
30252f36ba1SRussell King 		gpu->identity.revision = etnaviv_field(chipIdentity,
30352f36ba1SRussell King 					 VIVS_HI_CHIP_IDENTITY_REVISION);
304a8c21a54SThe etnaviv authors 	} else {
305a8c21a54SThe etnaviv authors 
306a8c21a54SThe etnaviv authors 		gpu->identity.model = gpu_read(gpu, VIVS_HI_CHIP_MODEL);
307a8c21a54SThe etnaviv authors 		gpu->identity.revision = gpu_read(gpu, VIVS_HI_CHIP_REV);
308a8c21a54SThe etnaviv authors 
309a8c21a54SThe etnaviv authors 		/*
310a8c21a54SThe etnaviv authors 		 * !!!! HACK ALERT !!!!
311a8c21a54SThe etnaviv authors 		 * Because people change device IDs without letting software
312a8c21a54SThe etnaviv authors 		 * know about it - here is the hack to make it all look the
313a8c21a54SThe etnaviv authors 		 * same.  Only for GC400 family.
314a8c21a54SThe etnaviv authors 		 */
315a8c21a54SThe etnaviv authors 		if ((gpu->identity.model & 0xff00) == 0x0400 &&
316507f8991SRussell King 		    gpu->identity.model != chipModel_GC420) {
317a8c21a54SThe etnaviv authors 			gpu->identity.model = gpu->identity.model & 0x0400;
318a8c21a54SThe etnaviv authors 		}
319a8c21a54SThe etnaviv authors 
320a8c21a54SThe etnaviv authors 		/* Another special case */
321472f79dcSRussell King 		if (etnaviv_is_model_rev(gpu, GC300, 0x2201)) {
322a8c21a54SThe etnaviv authors 			u32 chipDate = gpu_read(gpu, VIVS_HI_CHIP_DATE);
323a8c21a54SThe etnaviv authors 			u32 chipTime = gpu_read(gpu, VIVS_HI_CHIP_TIME);
324a8c21a54SThe etnaviv authors 
325a8c21a54SThe etnaviv authors 			if (chipDate == 0x20080814 && chipTime == 0x12051100) {
326a8c21a54SThe etnaviv authors 				/*
327a8c21a54SThe etnaviv authors 				 * This IP has an ECO; put the correct
328a8c21a54SThe etnaviv authors 				 * revision in it.
329a8c21a54SThe etnaviv authors 				 */
330a8c21a54SThe etnaviv authors 				gpu->identity.revision = 0x1051;
331a8c21a54SThe etnaviv authors 			}
332a8c21a54SThe etnaviv authors 		}
33312ff4bdeSLucas Stach 
33412ff4bdeSLucas Stach 		/*
33512ff4bdeSLucas Stach 		 * NXP likes to call the GPU on the i.MX6QP GC2000+, but in
33612ff4bdeSLucas Stach 		 * reality it's just a re-branded GC3000. We can identify this
33712ff4bdeSLucas Stach 		 * core by the upper half of the revision register being all 1.
33812ff4bdeSLucas Stach 		 * Fix model/rev here, so all other places can refer to this
33912ff4bdeSLucas Stach 		 * core by its real identity.
34012ff4bdeSLucas Stach 		 */
34112ff4bdeSLucas Stach 		if (etnaviv_is_model_rev(gpu, GC2000, 0xffff5450)) {
34212ff4bdeSLucas Stach 			gpu->identity.model = chipModel_GC3000;
34312ff4bdeSLucas Stach 			gpu->identity.revision &= 0xffff;
34412ff4bdeSLucas Stach 		}
345a8c21a54SThe etnaviv authors 	}
346a8c21a54SThe etnaviv authors 
347a8c21a54SThe etnaviv authors 	dev_info(gpu->dev, "model: GC%x, revision: %x\n",
348a8c21a54SThe etnaviv authors 		 gpu->identity.model, gpu->identity.revision);
349a8c21a54SThe etnaviv authors 
350a8c21a54SThe etnaviv authors 	gpu->identity.features = gpu_read(gpu, VIVS_HI_CHIP_FEATURE);
351a8c21a54SThe etnaviv authors 
352a8c21a54SThe etnaviv authors 	/* Disable fast clear on GC700. */
353507f8991SRussell King 	if (gpu->identity.model == chipModel_GC700)
354a8c21a54SThe etnaviv authors 		gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
355a8c21a54SThe etnaviv authors 
356507f8991SRussell King 	if ((gpu->identity.model == chipModel_GC500 &&
357507f8991SRussell King 	     gpu->identity.revision < 2) ||
358507f8991SRussell King 	    (gpu->identity.model == chipModel_GC300 &&
359507f8991SRussell King 	     gpu->identity.revision < 0x2000)) {
360a8c21a54SThe etnaviv authors 
361a8c21a54SThe etnaviv authors 		/*
362a8c21a54SThe etnaviv authors 		 * GC500 rev 1.x and GC300 rev < 2.0 doesn't have these
363a8c21a54SThe etnaviv authors 		 * registers.
364a8c21a54SThe etnaviv authors 		 */
365a8c21a54SThe etnaviv authors 		gpu->identity.minor_features0 = 0;
366a8c21a54SThe etnaviv authors 		gpu->identity.minor_features1 = 0;
367a8c21a54SThe etnaviv authors 		gpu->identity.minor_features2 = 0;
368a8c21a54SThe etnaviv authors 		gpu->identity.minor_features3 = 0;
369602eb489SRussell King 		gpu->identity.minor_features4 = 0;
370602eb489SRussell King 		gpu->identity.minor_features5 = 0;
371a8c21a54SThe etnaviv authors 	} else
372a8c21a54SThe etnaviv authors 		gpu->identity.minor_features0 =
373a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_0);
374a8c21a54SThe etnaviv authors 
375a8c21a54SThe etnaviv authors 	if (gpu->identity.minor_features0 &
376a8c21a54SThe etnaviv authors 	    chipMinorFeatures0_MORE_MINOR_FEATURES) {
377a8c21a54SThe etnaviv authors 		gpu->identity.minor_features1 =
378a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_1);
379a8c21a54SThe etnaviv authors 		gpu->identity.minor_features2 =
380a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_2);
381a8c21a54SThe etnaviv authors 		gpu->identity.minor_features3 =
382a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_3);
383602eb489SRussell King 		gpu->identity.minor_features4 =
384602eb489SRussell King 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_4);
385602eb489SRussell King 		gpu->identity.minor_features5 =
386602eb489SRussell King 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_5);
387a8c21a54SThe etnaviv authors 	}
388a8c21a54SThe etnaviv authors 
389a8c21a54SThe etnaviv authors 	/* GC600 idle register reports zero bits where modules aren't present */
390a8c21a54SThe etnaviv authors 	if (gpu->identity.model == chipModel_GC600) {
391a8c21a54SThe etnaviv authors 		gpu->idle_mask = VIVS_HI_IDLE_STATE_TX |
392a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_RA |
393a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_SE |
394a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_PA |
395a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_SH |
396a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_PE |
397a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_DE |
398a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_FE;
399a8c21a54SThe etnaviv authors 	} else {
400a8c21a54SThe etnaviv authors 		gpu->idle_mask = ~VIVS_HI_IDLE_STATE_AXI_LP;
401a8c21a54SThe etnaviv authors 	}
402a8c21a54SThe etnaviv authors 
403a8c21a54SThe etnaviv authors 	etnaviv_hw_specs(gpu);
404a8c21a54SThe etnaviv authors }
405a8c21a54SThe etnaviv authors 
406a8c21a54SThe etnaviv authors static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
407a8c21a54SThe etnaviv authors {
408a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock |
409a8c21a54SThe etnaviv authors 		  VIVS_HI_CLOCK_CONTROL_FSCALE_CMD_LOAD);
410a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
411a8c21a54SThe etnaviv authors }
412a8c21a54SThe etnaviv authors 
413bcdfb5e5SRussell King static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu)
414bcdfb5e5SRussell King {
415bcdfb5e5SRussell King 	unsigned int fscale = 1 << (6 - gpu->freq_scale);
416bcdfb5e5SRussell King 	u32 clock;
417bcdfb5e5SRussell King 
418bcdfb5e5SRussell King 	clock = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
419bcdfb5e5SRussell King 		VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
420bcdfb5e5SRussell King 
421bcdfb5e5SRussell King 	etnaviv_gpu_load_clock(gpu, clock);
422bcdfb5e5SRussell King }
423bcdfb5e5SRussell King 
424a8c21a54SThe etnaviv authors static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
425a8c21a54SThe etnaviv authors {
426a8c21a54SThe etnaviv authors 	u32 control, idle;
427a8c21a54SThe etnaviv authors 	unsigned long timeout;
428a8c21a54SThe etnaviv authors 	bool failed = true;
429a8c21a54SThe etnaviv authors 
430a8c21a54SThe etnaviv authors 	/* TODO
431a8c21a54SThe etnaviv authors 	 *
432a8c21a54SThe etnaviv authors 	 * - clock gating
433a8c21a54SThe etnaviv authors 	 * - puls eater
434a8c21a54SThe etnaviv authors 	 * - what about VG?
435a8c21a54SThe etnaviv authors 	 */
436a8c21a54SThe etnaviv authors 
437a8c21a54SThe etnaviv authors 	/* We hope that the GPU resets in under one second */
438a8c21a54SThe etnaviv authors 	timeout = jiffies + msecs_to_jiffies(1000);
439a8c21a54SThe etnaviv authors 
440a8c21a54SThe etnaviv authors 	while (time_is_after_jiffies(timeout)) {
441a8c21a54SThe etnaviv authors 		/* enable clock */
442bcdfb5e5SRussell King 		etnaviv_gpu_update_clock(gpu);
443bcdfb5e5SRussell King 
444bcdfb5e5SRussell King 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
445a8c21a54SThe etnaviv authors 
446a8c21a54SThe etnaviv authors 		/* Wait for stable clock.  Vivante's code waited for 1ms */
447a8c21a54SThe etnaviv authors 		usleep_range(1000, 10000);
448a8c21a54SThe etnaviv authors 
449a8c21a54SThe etnaviv authors 		/* isolate the GPU. */
450a8c21a54SThe etnaviv authors 		control |= VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
451a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
452a8c21a54SThe etnaviv authors 
453a8c21a54SThe etnaviv authors 		/* set soft reset. */
454a8c21a54SThe etnaviv authors 		control |= VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
455a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
456a8c21a54SThe etnaviv authors 
457a8c21a54SThe etnaviv authors 		/* wait for reset. */
458a8c21a54SThe etnaviv authors 		msleep(1);
459a8c21a54SThe etnaviv authors 
460a8c21a54SThe etnaviv authors 		/* reset soft reset bit. */
461a8c21a54SThe etnaviv authors 		control &= ~VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
462a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
463a8c21a54SThe etnaviv authors 
464a8c21a54SThe etnaviv authors 		/* reset GPU isolation. */
465a8c21a54SThe etnaviv authors 		control &= ~VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
466a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
467a8c21a54SThe etnaviv authors 
468a8c21a54SThe etnaviv authors 		/* read idle register. */
469a8c21a54SThe etnaviv authors 		idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
470a8c21a54SThe etnaviv authors 
471a8c21a54SThe etnaviv authors 		/* try reseting again if FE it not idle */
472a8c21a54SThe etnaviv authors 		if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) {
473a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "FE is not idle\n");
474a8c21a54SThe etnaviv authors 			continue;
475a8c21a54SThe etnaviv authors 		}
476a8c21a54SThe etnaviv authors 
477a8c21a54SThe etnaviv authors 		/* read reset register. */
478a8c21a54SThe etnaviv authors 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
479a8c21a54SThe etnaviv authors 
480a8c21a54SThe etnaviv authors 		/* is the GPU idle? */
481a8c21a54SThe etnaviv authors 		if (((control & VIVS_HI_CLOCK_CONTROL_IDLE_3D) == 0) ||
482a8c21a54SThe etnaviv authors 		    ((control & VIVS_HI_CLOCK_CONTROL_IDLE_2D) == 0)) {
483a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "GPU is not idle\n");
484a8c21a54SThe etnaviv authors 			continue;
485a8c21a54SThe etnaviv authors 		}
486a8c21a54SThe etnaviv authors 
487a8c21a54SThe etnaviv authors 		failed = false;
488a8c21a54SThe etnaviv authors 		break;
489a8c21a54SThe etnaviv authors 	}
490a8c21a54SThe etnaviv authors 
491a8c21a54SThe etnaviv authors 	if (failed) {
492a8c21a54SThe etnaviv authors 		idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
493a8c21a54SThe etnaviv authors 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
494a8c21a54SThe etnaviv authors 
495a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "GPU failed to reset: FE %sidle, 3D %sidle, 2D %sidle\n",
496a8c21a54SThe etnaviv authors 			idle & VIVS_HI_IDLE_STATE_FE ? "" : "not ",
497a8c21a54SThe etnaviv authors 			control & VIVS_HI_CLOCK_CONTROL_IDLE_3D ? "" : "not ",
498a8c21a54SThe etnaviv authors 			control & VIVS_HI_CLOCK_CONTROL_IDLE_2D ? "" : "not ");
499a8c21a54SThe etnaviv authors 
500a8c21a54SThe etnaviv authors 		return -EBUSY;
501a8c21a54SThe etnaviv authors 	}
502a8c21a54SThe etnaviv authors 
503a8c21a54SThe etnaviv authors 	/* We rely on the GPU running, so program the clock */
504bcdfb5e5SRussell King 	etnaviv_gpu_update_clock(gpu);
505a8c21a54SThe etnaviv authors 
506a8c21a54SThe etnaviv authors 	return 0;
507a8c21a54SThe etnaviv authors }
508a8c21a54SThe etnaviv authors 
5097d0c6e71SRussell King static void etnaviv_gpu_enable_mlcg(struct etnaviv_gpu *gpu)
5107d0c6e71SRussell King {
5117d0c6e71SRussell King 	u32 pmc, ppc;
5127d0c6e71SRussell King 
5137d0c6e71SRussell King 	/* enable clock gating */
5147d0c6e71SRussell King 	ppc = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
5157d0c6e71SRussell King 	ppc |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
5167d0c6e71SRussell King 
5177d0c6e71SRussell King 	/* Disable stall module clock gating for 4.3.0.1 and 4.3.0.2 revs */
5187d0c6e71SRussell King 	if (gpu->identity.revision == 0x4301 ||
5197d0c6e71SRussell King 	    gpu->identity.revision == 0x4302)
5207d0c6e71SRussell King 		ppc |= VIVS_PM_POWER_CONTROLS_DISABLE_STALL_MODULE_CLOCK_GATING;
5217d0c6e71SRussell King 
5227d0c6e71SRussell King 	gpu_write(gpu, VIVS_PM_POWER_CONTROLS, ppc);
5237d0c6e71SRussell King 
5247d0c6e71SRussell King 	pmc = gpu_read(gpu, VIVS_PM_MODULE_CONTROLS);
5257d0c6e71SRussell King 
5267d0c6e71SRussell King 	/* Disable PA clock gating for GC400+ except for GC420 */
5277d0c6e71SRussell King 	if (gpu->identity.model >= chipModel_GC400 &&
5287d0c6e71SRussell King 	    gpu->identity.model != chipModel_GC420)
5297d0c6e71SRussell King 		pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PA;
5307d0c6e71SRussell King 
5317d0c6e71SRussell King 	/*
5327d0c6e71SRussell King 	 * Disable PE clock gating on revs < 5.0.0.0 when HZ is
5337d0c6e71SRussell King 	 * present without a bug fix.
5347d0c6e71SRussell King 	 */
5357d0c6e71SRussell King 	if (gpu->identity.revision < 0x5000 &&
5367d0c6e71SRussell King 	    gpu->identity.minor_features0 & chipMinorFeatures0_HZ &&
5377d0c6e71SRussell King 	    !(gpu->identity.minor_features1 &
5387d0c6e71SRussell King 	      chipMinorFeatures1_DISABLE_PE_GATING))
5397d0c6e71SRussell King 		pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PE;
5407d0c6e71SRussell King 
5417d0c6e71SRussell King 	if (gpu->identity.revision < 0x5422)
5427d0c6e71SRussell King 		pmc |= BIT(15); /* Unknown bit */
5437d0c6e71SRussell King 
5447d0c6e71SRussell King 	pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_HZ;
5457d0c6e71SRussell King 	pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_EZ;
5467d0c6e71SRussell King 
5477d0c6e71SRussell King 	gpu_write(gpu, VIVS_PM_MODULE_CONTROLS, pmc);
5487d0c6e71SRussell King }
5497d0c6e71SRussell King 
550229855b6SLucas Stach void etnaviv_gpu_start_fe(struct etnaviv_gpu *gpu, u32 address, u16 prefetch)
551229855b6SLucas Stach {
552229855b6SLucas Stach 	gpu_write(gpu, VIVS_FE_COMMAND_ADDRESS, address);
553229855b6SLucas Stach 	gpu_write(gpu, VIVS_FE_COMMAND_CONTROL,
554229855b6SLucas Stach 		  VIVS_FE_COMMAND_CONTROL_ENABLE |
555229855b6SLucas Stach 		  VIVS_FE_COMMAND_CONTROL_PREFETCH(prefetch));
556229855b6SLucas Stach }
557229855b6SLucas Stach 
558e17a0dedSWladimir J. van der Laan static void etnaviv_gpu_setup_pulse_eater(struct etnaviv_gpu *gpu)
559e17a0dedSWladimir J. van der Laan {
560e17a0dedSWladimir J. van der Laan 	/*
561e17a0dedSWladimir J. van der Laan 	 * Base value for VIVS_PM_PULSE_EATER register on models where it
562e17a0dedSWladimir J. van der Laan 	 * cannot be read, extracted from vivante kernel driver.
563e17a0dedSWladimir J. van der Laan 	 */
564e17a0dedSWladimir J. van der Laan 	u32 pulse_eater = 0x01590880;
565e17a0dedSWladimir J. van der Laan 
566e17a0dedSWladimir J. van der Laan 	if (etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
567e17a0dedSWladimir J. van der Laan 	    etnaviv_is_model_rev(gpu, GC4000, 0x5222)) {
568e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(23);
569e17a0dedSWladimir J. van der Laan 
570e17a0dedSWladimir J. van der Laan 	}
571e17a0dedSWladimir J. van der Laan 
572e17a0dedSWladimir J. van der Laan 	if (etnaviv_is_model_rev(gpu, GC1000, 0x5039) ||
573e17a0dedSWladimir J. van der Laan 	    etnaviv_is_model_rev(gpu, GC1000, 0x5040)) {
574e17a0dedSWladimir J. van der Laan 		pulse_eater &= ~BIT(16);
575e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(17);
576e17a0dedSWladimir J. van der Laan 	}
577e17a0dedSWladimir J. van der Laan 
578e17a0dedSWladimir J. van der Laan 	if ((gpu->identity.revision > 0x5420) &&
579e17a0dedSWladimir J. van der Laan 	    (gpu->identity.features & chipFeatures_PIPE_3D))
580e17a0dedSWladimir J. van der Laan 	{
581e17a0dedSWladimir J. van der Laan 		/* Performance fix: disable internal DFS */
582e17a0dedSWladimir J. van der Laan 		pulse_eater = gpu_read(gpu, VIVS_PM_PULSE_EATER);
583e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(18);
584e17a0dedSWladimir J. van der Laan 	}
585e17a0dedSWladimir J. van der Laan 
586e17a0dedSWladimir J. van der Laan 	gpu_write(gpu, VIVS_PM_PULSE_EATER, pulse_eater);
587e17a0dedSWladimir J. van der Laan }
588e17a0dedSWladimir J. van der Laan 
589a8c21a54SThe etnaviv authors static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu)
590a8c21a54SThe etnaviv authors {
591a8c21a54SThe etnaviv authors 	u16 prefetch;
592a8c21a54SThe etnaviv authors 
593472f79dcSRussell King 	if ((etnaviv_is_model_rev(gpu, GC320, 0x5007) ||
594472f79dcSRussell King 	     etnaviv_is_model_rev(gpu, GC320, 0x5220)) &&
595472f79dcSRussell King 	    gpu_read(gpu, VIVS_HI_CHIP_TIME) != 0x2062400) {
596a8c21a54SThe etnaviv authors 		u32 mc_memory_debug;
597a8c21a54SThe etnaviv authors 
598a8c21a54SThe etnaviv authors 		mc_memory_debug = gpu_read(gpu, VIVS_MC_DEBUG_MEMORY) & ~0xff;
599a8c21a54SThe etnaviv authors 
600a8c21a54SThe etnaviv authors 		if (gpu->identity.revision == 0x5007)
601a8c21a54SThe etnaviv authors 			mc_memory_debug |= 0x0c;
602a8c21a54SThe etnaviv authors 		else
603a8c21a54SThe etnaviv authors 			mc_memory_debug |= 0x08;
604a8c21a54SThe etnaviv authors 
605a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_MC_DEBUG_MEMORY, mc_memory_debug);
606a8c21a54SThe etnaviv authors 	}
607a8c21a54SThe etnaviv authors 
6087d0c6e71SRussell King 	/* enable module-level clock gating */
6097d0c6e71SRussell King 	etnaviv_gpu_enable_mlcg(gpu);
6107d0c6e71SRussell King 
611a8c21a54SThe etnaviv authors 	/*
612a8c21a54SThe etnaviv authors 	 * Update GPU AXI cache atttribute to "cacheable, no allocate".
613a8c21a54SThe etnaviv authors 	 * This is necessary to prevent the iMX6 SoC locking up.
614a8c21a54SThe etnaviv authors 	 */
615a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_AXI_CONFIG,
616a8c21a54SThe etnaviv authors 		  VIVS_HI_AXI_CONFIG_AWCACHE(2) |
617a8c21a54SThe etnaviv authors 		  VIVS_HI_AXI_CONFIG_ARCACHE(2));
618a8c21a54SThe etnaviv authors 
619a8c21a54SThe etnaviv authors 	/* GC2000 rev 5108 needs a special bus config */
620472f79dcSRussell King 	if (etnaviv_is_model_rev(gpu, GC2000, 0x5108)) {
621a8c21a54SThe etnaviv authors 		u32 bus_config = gpu_read(gpu, VIVS_MC_BUS_CONFIG);
622a8c21a54SThe etnaviv authors 		bus_config &= ~(VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG__MASK |
623a8c21a54SThe etnaviv authors 				VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG__MASK);
624a8c21a54SThe etnaviv authors 		bus_config |= VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG(1) |
625a8c21a54SThe etnaviv authors 			      VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG(0);
626a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_MC_BUS_CONFIG, bus_config);
627a8c21a54SThe etnaviv authors 	}
628a8c21a54SThe etnaviv authors 
629e17a0dedSWladimir J. van der Laan 	/* setup the pulse eater */
630e17a0dedSWladimir J. van der Laan 	etnaviv_gpu_setup_pulse_eater(gpu);
631e17a0dedSWladimir J. van der Laan 
63299f861bcSLucas Stach 	/* setup the MMU */
633e095c8feSLucas Stach 	etnaviv_iommu_restore(gpu);
634a8c21a54SThe etnaviv authors 
635a8c21a54SThe etnaviv authors 	/* Start command processor */
636a8c21a54SThe etnaviv authors 	prefetch = etnaviv_buffer_init(gpu);
637a8c21a54SThe etnaviv authors 
638a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_INTR_ENBL, ~0U);
639c3ef4b8cSLucas Stach 	etnaviv_gpu_start_fe(gpu, etnaviv_cmdbuf_get_va(gpu->buffer),
640229855b6SLucas Stach 			     prefetch);
641a8c21a54SThe etnaviv authors }
642a8c21a54SThe etnaviv authors 
643a8c21a54SThe etnaviv authors int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
644a8c21a54SThe etnaviv authors {
645a8c21a54SThe etnaviv authors 	int ret, i;
646a8c21a54SThe etnaviv authors 
647a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
6481409df04SLucas Stach 	if (ret < 0) {
6491409df04SLucas Stach 		dev_err(gpu->dev, "Failed to enable GPU power domain\n");
650a8c21a54SThe etnaviv authors 		return ret;
6511409df04SLucas Stach 	}
652a8c21a54SThe etnaviv authors 
653a8c21a54SThe etnaviv authors 	etnaviv_hw_identify(gpu);
654a8c21a54SThe etnaviv authors 
655a8c21a54SThe etnaviv authors 	if (gpu->identity.model == 0) {
656a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "Unknown GPU model\n");
657f6427760SRussell King 		ret = -ENXIO;
658f6427760SRussell King 		goto fail;
659a8c21a54SThe etnaviv authors 	}
660a8c21a54SThe etnaviv authors 
661b98c6688SRussell King 	/* Exclude VG cores with FE2.0 */
662b98c6688SRussell King 	if (gpu->identity.features & chipFeatures_PIPE_VG &&
663b98c6688SRussell King 	    gpu->identity.features & chipFeatures_FE20) {
664b98c6688SRussell King 		dev_info(gpu->dev, "Ignoring GPU with VG and FE2.0\n");
665b98c6688SRussell King 		ret = -ENXIO;
666b98c6688SRussell King 		goto fail;
667b98c6688SRussell King 	}
668b98c6688SRussell King 
6692144fff7SLucas Stach 	/*
6702144fff7SLucas Stach 	 * Set the GPU linear window to be at the end of the DMA window, where
6712144fff7SLucas Stach 	 * the CMA area is likely to reside. This ensures that we are able to
6722144fff7SLucas Stach 	 * map the command buffers while having the linear window overlap as
6732144fff7SLucas Stach 	 * much RAM as possible, so we can optimize mappings for other buffers.
6742144fff7SLucas Stach 	 *
6752144fff7SLucas Stach 	 * For 3D cores only do this if MC2.0 is present, as with MC1.0 it leads
6762144fff7SLucas Stach 	 * to different views of the memory on the individual engines.
6772144fff7SLucas Stach 	 */
6782144fff7SLucas Stach 	if (!(gpu->identity.features & chipFeatures_PIPE_3D) ||
6792144fff7SLucas Stach 	    (gpu->identity.minor_features0 & chipMinorFeatures0_MC20)) {
6802144fff7SLucas Stach 		u32 dma_mask = (u32)dma_get_required_mask(gpu->dev);
6812144fff7SLucas Stach 		if (dma_mask < PHYS_OFFSET + SZ_2G)
6822144fff7SLucas Stach 			gpu->memory_base = PHYS_OFFSET;
6832144fff7SLucas Stach 		else
6842144fff7SLucas Stach 			gpu->memory_base = dma_mask - SZ_2G + 1;
6851db01279SLucas Stach 	} else if (PHYS_OFFSET >= SZ_2G) {
6861db01279SLucas Stach 		dev_info(gpu->dev, "Need to move linear window on MC1.0, disabling TS\n");
6871db01279SLucas Stach 		gpu->memory_base = PHYS_OFFSET;
6881db01279SLucas Stach 		gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
6892144fff7SLucas Stach 	}
6902144fff7SLucas Stach 
691a8c21a54SThe etnaviv authors 	ret = etnaviv_hw_reset(gpu);
6921409df04SLucas Stach 	if (ret) {
6931409df04SLucas Stach 		dev_err(gpu->dev, "GPU reset failed\n");
694a8c21a54SThe etnaviv authors 		goto fail;
6951409df04SLucas Stach 	}
696a8c21a54SThe etnaviv authors 
697dd34bb96SLucas Stach 	gpu->mmu = etnaviv_iommu_new(gpu);
698dd34bb96SLucas Stach 	if (IS_ERR(gpu->mmu)) {
6991409df04SLucas Stach 		dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n");
700dd34bb96SLucas Stach 		ret = PTR_ERR(gpu->mmu);
701a8c21a54SThe etnaviv authors 		goto fail;
702a8c21a54SThe etnaviv authors 	}
703a8c21a54SThe etnaviv authors 
704e66774ddSLucas Stach 	gpu->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(gpu);
705e66774ddSLucas Stach 	if (IS_ERR(gpu->cmdbuf_suballoc)) {
706e66774ddSLucas Stach 		dev_err(gpu->dev, "Failed to create cmdbuf suballocator\n");
707e66774ddSLucas Stach 		ret = PTR_ERR(gpu->cmdbuf_suballoc);
708e66774ddSLucas Stach 		goto fail;
709e66774ddSLucas Stach 	}
710e66774ddSLucas Stach 
711a8c21a54SThe etnaviv authors 	/* Create buffer: */
712e66774ddSLucas Stach 	gpu->buffer = etnaviv_cmdbuf_new(gpu->cmdbuf_suballoc, PAGE_SIZE, 0);
713a8c21a54SThe etnaviv authors 	if (!gpu->buffer) {
714a8c21a54SThe etnaviv authors 		ret = -ENOMEM;
715a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "could not create command buffer\n");
71645d16a6dSLucas Stach 		goto destroy_iommu;
717a8c21a54SThe etnaviv authors 	}
718acfee0ecSLucas Stach 
719acfee0ecSLucas Stach 	if (gpu->mmu->version == ETNAVIV_IOMMU_V1 &&
720c3ef4b8cSLucas Stach 	    etnaviv_cmdbuf_get_va(gpu->buffer) > 0x80000000) {
721a8c21a54SThe etnaviv authors 		ret = -EINVAL;
722a8c21a54SThe etnaviv authors 		dev_err(gpu->dev,
723a8c21a54SThe etnaviv authors 			"command buffer outside valid memory window\n");
724a8c21a54SThe etnaviv authors 		goto free_buffer;
725a8c21a54SThe etnaviv authors 	}
726a8c21a54SThe etnaviv authors 
727a8c21a54SThe etnaviv authors 	/* Setup event management */
728a8c21a54SThe etnaviv authors 	spin_lock_init(&gpu->event_spinlock);
729a8c21a54SThe etnaviv authors 	init_completion(&gpu->event_free);
730a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
731a8c21a54SThe etnaviv authors 		gpu->event[i].used = false;
732a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
733a8c21a54SThe etnaviv authors 	}
734a8c21a54SThe etnaviv authors 
735a8c21a54SThe etnaviv authors 	/* Now program the hardware */
736a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
737a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
738f6086311SRussell King 	gpu->exec_state = -1;
739a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
740a8c21a54SThe etnaviv authors 
741a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
742a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
743a8c21a54SThe etnaviv authors 
744a8c21a54SThe etnaviv authors 	return 0;
745a8c21a54SThe etnaviv authors 
746a8c21a54SThe etnaviv authors free_buffer:
747ea1f5729SLucas Stach 	etnaviv_cmdbuf_free(gpu->buffer);
748a8c21a54SThe etnaviv authors 	gpu->buffer = NULL;
74945d16a6dSLucas Stach destroy_iommu:
75045d16a6dSLucas Stach 	etnaviv_iommu_destroy(gpu->mmu);
75145d16a6dSLucas Stach 	gpu->mmu = NULL;
752a8c21a54SThe etnaviv authors fail:
753a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
754a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
755a8c21a54SThe etnaviv authors 
756a8c21a54SThe etnaviv authors 	return ret;
757a8c21a54SThe etnaviv authors }
758a8c21a54SThe etnaviv authors 
759a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
760a8c21a54SThe etnaviv authors struct dma_debug {
761a8c21a54SThe etnaviv authors 	u32 address[2];
762a8c21a54SThe etnaviv authors 	u32 state[2];
763a8c21a54SThe etnaviv authors };
764a8c21a54SThe etnaviv authors 
765a8c21a54SThe etnaviv authors static void verify_dma(struct etnaviv_gpu *gpu, struct dma_debug *debug)
766a8c21a54SThe etnaviv authors {
767a8c21a54SThe etnaviv authors 	u32 i;
768a8c21a54SThe etnaviv authors 
769a8c21a54SThe etnaviv authors 	debug->address[0] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
770a8c21a54SThe etnaviv authors 	debug->state[0]   = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
771a8c21a54SThe etnaviv authors 
772a8c21a54SThe etnaviv authors 	for (i = 0; i < 500; i++) {
773a8c21a54SThe etnaviv authors 		debug->address[1] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
774a8c21a54SThe etnaviv authors 		debug->state[1]   = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
775a8c21a54SThe etnaviv authors 
776a8c21a54SThe etnaviv authors 		if (debug->address[0] != debug->address[1])
777a8c21a54SThe etnaviv authors 			break;
778a8c21a54SThe etnaviv authors 
779a8c21a54SThe etnaviv authors 		if (debug->state[0] != debug->state[1])
780a8c21a54SThe etnaviv authors 			break;
781a8c21a54SThe etnaviv authors 	}
782a8c21a54SThe etnaviv authors }
783a8c21a54SThe etnaviv authors 
784a8c21a54SThe etnaviv authors int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m)
785a8c21a54SThe etnaviv authors {
786a8c21a54SThe etnaviv authors 	struct dma_debug debug;
787a8c21a54SThe etnaviv authors 	u32 dma_lo, dma_hi, axi, idle;
788a8c21a54SThe etnaviv authors 	int ret;
789a8c21a54SThe etnaviv authors 
790a8c21a54SThe etnaviv authors 	seq_printf(m, "%s Status:\n", dev_name(gpu->dev));
791a8c21a54SThe etnaviv authors 
792a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
793a8c21a54SThe etnaviv authors 	if (ret < 0)
794a8c21a54SThe etnaviv authors 		return ret;
795a8c21a54SThe etnaviv authors 
796a8c21a54SThe etnaviv authors 	dma_lo = gpu_read(gpu, VIVS_FE_DMA_LOW);
797a8c21a54SThe etnaviv authors 	dma_hi = gpu_read(gpu, VIVS_FE_DMA_HIGH);
798a8c21a54SThe etnaviv authors 	axi = gpu_read(gpu, VIVS_HI_AXI_STATUS);
799a8c21a54SThe etnaviv authors 	idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
800a8c21a54SThe etnaviv authors 
801a8c21a54SThe etnaviv authors 	verify_dma(gpu, &debug);
802a8c21a54SThe etnaviv authors 
803a8c21a54SThe etnaviv authors 	seq_puts(m, "\tfeatures\n");
804a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features0: 0x%08x\n",
805a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features0);
806a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features1: 0x%08x\n",
807a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features1);
808a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features2: 0x%08x\n",
809a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features2);
810a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features3: 0x%08x\n",
811a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features3);
812602eb489SRussell King 	seq_printf(m, "\t minor_features4: 0x%08x\n",
813602eb489SRussell King 		   gpu->identity.minor_features4);
814602eb489SRussell King 	seq_printf(m, "\t minor_features5: 0x%08x\n",
815602eb489SRussell King 		   gpu->identity.minor_features5);
816a8c21a54SThe etnaviv authors 
817a8c21a54SThe etnaviv authors 	seq_puts(m, "\tspecs\n");
818a8c21a54SThe etnaviv authors 	seq_printf(m, "\t stream_count:  %d\n",
819a8c21a54SThe etnaviv authors 			gpu->identity.stream_count);
820a8c21a54SThe etnaviv authors 	seq_printf(m, "\t register_max: %d\n",
821a8c21a54SThe etnaviv authors 			gpu->identity.register_max);
822a8c21a54SThe etnaviv authors 	seq_printf(m, "\t thread_count: %d\n",
823a8c21a54SThe etnaviv authors 			gpu->identity.thread_count);
824a8c21a54SThe etnaviv authors 	seq_printf(m, "\t vertex_cache_size: %d\n",
825a8c21a54SThe etnaviv authors 			gpu->identity.vertex_cache_size);
826a8c21a54SThe etnaviv authors 	seq_printf(m, "\t shader_core_count: %d\n",
827a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count);
828a8c21a54SThe etnaviv authors 	seq_printf(m, "\t pixel_pipes: %d\n",
829a8c21a54SThe etnaviv authors 			gpu->identity.pixel_pipes);
830a8c21a54SThe etnaviv authors 	seq_printf(m, "\t vertex_output_buffer_size: %d\n",
831a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size);
832a8c21a54SThe etnaviv authors 	seq_printf(m, "\t buffer_size: %d\n",
833a8c21a54SThe etnaviv authors 			gpu->identity.buffer_size);
834a8c21a54SThe etnaviv authors 	seq_printf(m, "\t instruction_count: %d\n",
835a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count);
836a8c21a54SThe etnaviv authors 	seq_printf(m, "\t num_constants: %d\n",
837a8c21a54SThe etnaviv authors 			gpu->identity.num_constants);
838602eb489SRussell King 	seq_printf(m, "\t varyings_count: %d\n",
839602eb489SRussell King 			gpu->identity.varyings_count);
840a8c21a54SThe etnaviv authors 
841a8c21a54SThe etnaviv authors 	seq_printf(m, "\taxi: 0x%08x\n", axi);
842a8c21a54SThe etnaviv authors 	seq_printf(m, "\tidle: 0x%08x\n", idle);
843a8c21a54SThe etnaviv authors 	idle |= ~gpu->idle_mask & ~VIVS_HI_IDLE_STATE_AXI_LP;
844a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_FE) == 0)
845a8c21a54SThe etnaviv authors 		seq_puts(m, "\t FE is not idle\n");
846a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_DE) == 0)
847a8c21a54SThe etnaviv authors 		seq_puts(m, "\t DE is not idle\n");
848a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_PE) == 0)
849a8c21a54SThe etnaviv authors 		seq_puts(m, "\t PE is not idle\n");
850a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_SH) == 0)
851a8c21a54SThe etnaviv authors 		seq_puts(m, "\t SH is not idle\n");
852a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_PA) == 0)
853a8c21a54SThe etnaviv authors 		seq_puts(m, "\t PA is not idle\n");
854a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_SE) == 0)
855a8c21a54SThe etnaviv authors 		seq_puts(m, "\t SE is not idle\n");
856a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_RA) == 0)
857a8c21a54SThe etnaviv authors 		seq_puts(m, "\t RA is not idle\n");
858a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_TX) == 0)
859a8c21a54SThe etnaviv authors 		seq_puts(m, "\t TX is not idle\n");
860a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_VG) == 0)
861a8c21a54SThe etnaviv authors 		seq_puts(m, "\t VG is not idle\n");
862a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_IM) == 0)
863a8c21a54SThe etnaviv authors 		seq_puts(m, "\t IM is not idle\n");
864a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_FP) == 0)
865a8c21a54SThe etnaviv authors 		seq_puts(m, "\t FP is not idle\n");
866a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_TS) == 0)
867a8c21a54SThe etnaviv authors 		seq_puts(m, "\t TS is not idle\n");
868a8c21a54SThe etnaviv authors 	if (idle & VIVS_HI_IDLE_STATE_AXI_LP)
869a8c21a54SThe etnaviv authors 		seq_puts(m, "\t AXI low power mode\n");
870a8c21a54SThe etnaviv authors 
871a8c21a54SThe etnaviv authors 	if (gpu->identity.features & chipFeatures_DEBUG_MODE) {
872a8c21a54SThe etnaviv authors 		u32 read0 = gpu_read(gpu, VIVS_MC_DEBUG_READ0);
873a8c21a54SThe etnaviv authors 		u32 read1 = gpu_read(gpu, VIVS_MC_DEBUG_READ1);
874a8c21a54SThe etnaviv authors 		u32 write = gpu_read(gpu, VIVS_MC_DEBUG_WRITE);
875a8c21a54SThe etnaviv authors 
876a8c21a54SThe etnaviv authors 		seq_puts(m, "\tMC\n");
877a8c21a54SThe etnaviv authors 		seq_printf(m, "\t read0: 0x%08x\n", read0);
878a8c21a54SThe etnaviv authors 		seq_printf(m, "\t read1: 0x%08x\n", read1);
879a8c21a54SThe etnaviv authors 		seq_printf(m, "\t write: 0x%08x\n", write);
880a8c21a54SThe etnaviv authors 	}
881a8c21a54SThe etnaviv authors 
882a8c21a54SThe etnaviv authors 	seq_puts(m, "\tDMA ");
883a8c21a54SThe etnaviv authors 
884a8c21a54SThe etnaviv authors 	if (debug.address[0] == debug.address[1] &&
885a8c21a54SThe etnaviv authors 	    debug.state[0] == debug.state[1]) {
886a8c21a54SThe etnaviv authors 		seq_puts(m, "seems to be stuck\n");
887a8c21a54SThe etnaviv authors 	} else if (debug.address[0] == debug.address[1]) {
888c01e0159SMasanari Iida 		seq_puts(m, "address is constant\n");
889a8c21a54SThe etnaviv authors 	} else {
890c01e0159SMasanari Iida 		seq_puts(m, "is running\n");
891a8c21a54SThe etnaviv authors 	}
892a8c21a54SThe etnaviv authors 
893a8c21a54SThe etnaviv authors 	seq_printf(m, "\t address 0: 0x%08x\n", debug.address[0]);
894a8c21a54SThe etnaviv authors 	seq_printf(m, "\t address 1: 0x%08x\n", debug.address[1]);
895a8c21a54SThe etnaviv authors 	seq_printf(m, "\t state 0: 0x%08x\n", debug.state[0]);
896a8c21a54SThe etnaviv authors 	seq_printf(m, "\t state 1: 0x%08x\n", debug.state[1]);
897a8c21a54SThe etnaviv authors 	seq_printf(m, "\t last fetch 64 bit word: 0x%08x 0x%08x\n",
898a8c21a54SThe etnaviv authors 		   dma_lo, dma_hi);
899a8c21a54SThe etnaviv authors 
900a8c21a54SThe etnaviv authors 	ret = 0;
901a8c21a54SThe etnaviv authors 
902a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
903a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
904a8c21a54SThe etnaviv authors 
905a8c21a54SThe etnaviv authors 	return ret;
906a8c21a54SThe etnaviv authors }
907a8c21a54SThe etnaviv authors #endif
908a8c21a54SThe etnaviv authors 
909a8c21a54SThe etnaviv authors /*
910a8c21a54SThe etnaviv authors  * Hangcheck detection for locked gpu:
911a8c21a54SThe etnaviv authors  */
912a8c21a54SThe etnaviv authors static void recover_worker(struct work_struct *work)
913a8c21a54SThe etnaviv authors {
914a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
915a8c21a54SThe etnaviv authors 					       recover_work);
916a8c21a54SThe etnaviv authors 	unsigned long flags;
917a8c21a54SThe etnaviv authors 	unsigned int i;
918a8c21a54SThe etnaviv authors 
919a8c21a54SThe etnaviv authors 	dev_err(gpu->dev, "hangcheck recover!\n");
920a8c21a54SThe etnaviv authors 
921a8c21a54SThe etnaviv authors 	if (pm_runtime_get_sync(gpu->dev) < 0)
922a8c21a54SThe etnaviv authors 		return;
923a8c21a54SThe etnaviv authors 
924a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
925a8c21a54SThe etnaviv authors 
926a8c21a54SThe etnaviv authors 	/* Only catch the first event, or when manually re-armed */
927a8c21a54SThe etnaviv authors 	if (etnaviv_dump_core) {
928a8c21a54SThe etnaviv authors 		etnaviv_core_dump(gpu);
929a8c21a54SThe etnaviv authors 		etnaviv_dump_core = false;
930a8c21a54SThe etnaviv authors 	}
931a8c21a54SThe etnaviv authors 
932a8c21a54SThe etnaviv authors 	etnaviv_hw_reset(gpu);
933a8c21a54SThe etnaviv authors 
934a8c21a54SThe etnaviv authors 	/* complete all events, the GPU won't do it after the reset */
935a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
936a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
937a8c21a54SThe etnaviv authors 		if (!gpu->event[i].used)
938a8c21a54SThe etnaviv authors 			continue;
939f54d1867SChris Wilson 		dma_fence_signal(gpu->event[i].fence);
940a8c21a54SThe etnaviv authors 		gpu->event[i].fence = NULL;
941a8c21a54SThe etnaviv authors 		gpu->event[i].used = false;
942a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
943a8c21a54SThe etnaviv authors 	}
944a8c21a54SThe etnaviv authors 	spin_unlock_irqrestore(&gpu->event_spinlock, flags);
945a8c21a54SThe etnaviv authors 	gpu->completed_fence = gpu->active_fence;
946a8c21a54SThe etnaviv authors 
947a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
9481b94a9b7SLucas Stach 	gpu->lastctx = NULL;
949f6086311SRussell King 	gpu->exec_state = -1;
950a8c21a54SThe etnaviv authors 
951a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
952a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
953a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
954a8c21a54SThe etnaviv authors 
955a8c21a54SThe etnaviv authors 	/* Retire the buffer objects in a work */
956a8c21a54SThe etnaviv authors 	etnaviv_queue_work(gpu->drm, &gpu->retire_work);
957a8c21a54SThe etnaviv authors }
958a8c21a54SThe etnaviv authors 
959a8c21a54SThe etnaviv authors static void hangcheck_timer_reset(struct etnaviv_gpu *gpu)
960a8c21a54SThe etnaviv authors {
961a8c21a54SThe etnaviv authors 	DBG("%s", dev_name(gpu->dev));
962a8c21a54SThe etnaviv authors 	mod_timer(&gpu->hangcheck_timer,
963a8c21a54SThe etnaviv authors 		  round_jiffies_up(jiffies + DRM_ETNAVIV_HANGCHECK_JIFFIES));
964a8c21a54SThe etnaviv authors }
965a8c21a54SThe etnaviv authors 
966a8c21a54SThe etnaviv authors static void hangcheck_handler(unsigned long data)
967a8c21a54SThe etnaviv authors {
968a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = (struct etnaviv_gpu *)data;
969a8c21a54SThe etnaviv authors 	u32 fence = gpu->completed_fence;
970a8c21a54SThe etnaviv authors 	bool progress = false;
971a8c21a54SThe etnaviv authors 
972a8c21a54SThe etnaviv authors 	if (fence != gpu->hangcheck_fence) {
973a8c21a54SThe etnaviv authors 		gpu->hangcheck_fence = fence;
974a8c21a54SThe etnaviv authors 		progress = true;
975a8c21a54SThe etnaviv authors 	}
976a8c21a54SThe etnaviv authors 
977a8c21a54SThe etnaviv authors 	if (!progress) {
978a8c21a54SThe etnaviv authors 		u32 dma_addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
979a8c21a54SThe etnaviv authors 		int change = dma_addr - gpu->hangcheck_dma_addr;
980a8c21a54SThe etnaviv authors 
981a8c21a54SThe etnaviv authors 		if (change < 0 || change > 16) {
982a8c21a54SThe etnaviv authors 			gpu->hangcheck_dma_addr = dma_addr;
983a8c21a54SThe etnaviv authors 			progress = true;
984a8c21a54SThe etnaviv authors 		}
985a8c21a54SThe etnaviv authors 	}
986a8c21a54SThe etnaviv authors 
987a8c21a54SThe etnaviv authors 	if (!progress && fence_after(gpu->active_fence, fence)) {
988a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "hangcheck detected gpu lockup!\n");
989a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "     completed fence: %u\n", fence);
990a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "     active fence: %u\n",
991a8c21a54SThe etnaviv authors 			gpu->active_fence);
992a8c21a54SThe etnaviv authors 		etnaviv_queue_work(gpu->drm, &gpu->recover_work);
993a8c21a54SThe etnaviv authors 	}
994a8c21a54SThe etnaviv authors 
995a8c21a54SThe etnaviv authors 	/* if still more pending work, reset the hangcheck timer: */
996a8c21a54SThe etnaviv authors 	if (fence_after(gpu->active_fence, gpu->hangcheck_fence))
997a8c21a54SThe etnaviv authors 		hangcheck_timer_reset(gpu);
998a8c21a54SThe etnaviv authors }
999a8c21a54SThe etnaviv authors 
1000a8c21a54SThe etnaviv authors static void hangcheck_disable(struct etnaviv_gpu *gpu)
1001a8c21a54SThe etnaviv authors {
1002a8c21a54SThe etnaviv authors 	del_timer_sync(&gpu->hangcheck_timer);
1003a8c21a54SThe etnaviv authors 	cancel_work_sync(&gpu->recover_work);
1004a8c21a54SThe etnaviv authors }
1005a8c21a54SThe etnaviv authors 
1006a8c21a54SThe etnaviv authors /* fence object management */
1007a8c21a54SThe etnaviv authors struct etnaviv_fence {
1008a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
1009f54d1867SChris Wilson 	struct dma_fence base;
1010a8c21a54SThe etnaviv authors };
1011a8c21a54SThe etnaviv authors 
1012f54d1867SChris Wilson static inline struct etnaviv_fence *to_etnaviv_fence(struct dma_fence *fence)
1013a8c21a54SThe etnaviv authors {
1014a8c21a54SThe etnaviv authors 	return container_of(fence, struct etnaviv_fence, base);
1015a8c21a54SThe etnaviv authors }
1016a8c21a54SThe etnaviv authors 
1017f54d1867SChris Wilson static const char *etnaviv_fence_get_driver_name(struct dma_fence *fence)
1018a8c21a54SThe etnaviv authors {
1019a8c21a54SThe etnaviv authors 	return "etnaviv";
1020a8c21a54SThe etnaviv authors }
1021a8c21a54SThe etnaviv authors 
1022f54d1867SChris Wilson static const char *etnaviv_fence_get_timeline_name(struct dma_fence *fence)
1023a8c21a54SThe etnaviv authors {
1024a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1025a8c21a54SThe etnaviv authors 
1026a8c21a54SThe etnaviv authors 	return dev_name(f->gpu->dev);
1027a8c21a54SThe etnaviv authors }
1028a8c21a54SThe etnaviv authors 
1029f54d1867SChris Wilson static bool etnaviv_fence_enable_signaling(struct dma_fence *fence)
1030a8c21a54SThe etnaviv authors {
1031a8c21a54SThe etnaviv authors 	return true;
1032a8c21a54SThe etnaviv authors }
1033a8c21a54SThe etnaviv authors 
1034f54d1867SChris Wilson static bool etnaviv_fence_signaled(struct dma_fence *fence)
1035a8c21a54SThe etnaviv authors {
1036a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1037a8c21a54SThe etnaviv authors 
1038a8c21a54SThe etnaviv authors 	return fence_completed(f->gpu, f->base.seqno);
1039a8c21a54SThe etnaviv authors }
1040a8c21a54SThe etnaviv authors 
1041f54d1867SChris Wilson static void etnaviv_fence_release(struct dma_fence *fence)
1042a8c21a54SThe etnaviv authors {
1043a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1044a8c21a54SThe etnaviv authors 
1045a8c21a54SThe etnaviv authors 	kfree_rcu(f, base.rcu);
1046a8c21a54SThe etnaviv authors }
1047a8c21a54SThe etnaviv authors 
1048f54d1867SChris Wilson static const struct dma_fence_ops etnaviv_fence_ops = {
1049a8c21a54SThe etnaviv authors 	.get_driver_name = etnaviv_fence_get_driver_name,
1050a8c21a54SThe etnaviv authors 	.get_timeline_name = etnaviv_fence_get_timeline_name,
1051a8c21a54SThe etnaviv authors 	.enable_signaling = etnaviv_fence_enable_signaling,
1052a8c21a54SThe etnaviv authors 	.signaled = etnaviv_fence_signaled,
1053f54d1867SChris Wilson 	.wait = dma_fence_default_wait,
1054a8c21a54SThe etnaviv authors 	.release = etnaviv_fence_release,
1055a8c21a54SThe etnaviv authors };
1056a8c21a54SThe etnaviv authors 
1057f54d1867SChris Wilson static struct dma_fence *etnaviv_gpu_fence_alloc(struct etnaviv_gpu *gpu)
1058a8c21a54SThe etnaviv authors {
1059a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f;
1060a8c21a54SThe etnaviv authors 
1061b27734c2SLucas Stach 	/*
1062b27734c2SLucas Stach 	 * GPU lock must already be held, otherwise fence completion order might
1063b27734c2SLucas Stach 	 * not match the seqno order assigned here.
1064b27734c2SLucas Stach 	 */
1065b27734c2SLucas Stach 	lockdep_assert_held(&gpu->lock);
1066b27734c2SLucas Stach 
1067a8c21a54SThe etnaviv authors 	f = kzalloc(sizeof(*f), GFP_KERNEL);
1068a8c21a54SThe etnaviv authors 	if (!f)
1069a8c21a54SThe etnaviv authors 		return NULL;
1070a8c21a54SThe etnaviv authors 
1071a8c21a54SThe etnaviv authors 	f->gpu = gpu;
1072a8c21a54SThe etnaviv authors 
1073f54d1867SChris Wilson 	dma_fence_init(&f->base, &etnaviv_fence_ops, &gpu->fence_spinlock,
1074a8c21a54SThe etnaviv authors 		       gpu->fence_context, ++gpu->next_fence);
1075a8c21a54SThe etnaviv authors 
1076a8c21a54SThe etnaviv authors 	return &f->base;
1077a8c21a54SThe etnaviv authors }
1078a8c21a54SThe etnaviv authors 
1079a8c21a54SThe etnaviv authors int etnaviv_gpu_fence_sync_obj(struct etnaviv_gem_object *etnaviv_obj,
10809ad59feaSPhilipp Zabel 	unsigned int context, bool exclusive, bool explicit)
1081a8c21a54SThe etnaviv authors {
1082a8c21a54SThe etnaviv authors 	struct reservation_object *robj = etnaviv_obj->resv;
1083a8c21a54SThe etnaviv authors 	struct reservation_object_list *fobj;
1084f54d1867SChris Wilson 	struct dma_fence *fence;
1085a8c21a54SThe etnaviv authors 	int i, ret;
1086a8c21a54SThe etnaviv authors 
1087a8c21a54SThe etnaviv authors 	if (!exclusive) {
1088a8c21a54SThe etnaviv authors 		ret = reservation_object_reserve_shared(robj);
1089a8c21a54SThe etnaviv authors 		if (ret)
1090a8c21a54SThe etnaviv authors 			return ret;
1091a8c21a54SThe etnaviv authors 	}
1092a8c21a54SThe etnaviv authors 
10939ad59feaSPhilipp Zabel 	if (explicit)
10949ad59feaSPhilipp Zabel 		return 0;
10959ad59feaSPhilipp Zabel 
1096a8c21a54SThe etnaviv authors 	/*
1097a8c21a54SThe etnaviv authors 	 * If we have any shared fences, then the exclusive fence
1098a8c21a54SThe etnaviv authors 	 * should be ignored as it will already have been signalled.
1099a8c21a54SThe etnaviv authors 	 */
1100a8c21a54SThe etnaviv authors 	fobj = reservation_object_get_list(robj);
1101a8c21a54SThe etnaviv authors 	if (!fobj || fobj->shared_count == 0) {
1102a8c21a54SThe etnaviv authors 		/* Wait on any existing exclusive fence which isn't our own */
1103a8c21a54SThe etnaviv authors 		fence = reservation_object_get_excl(robj);
1104a8c21a54SThe etnaviv authors 		if (fence && fence->context != context) {
1105f54d1867SChris Wilson 			ret = dma_fence_wait(fence, true);
1106a8c21a54SThe etnaviv authors 			if (ret)
1107a8c21a54SThe etnaviv authors 				return ret;
1108a8c21a54SThe etnaviv authors 		}
1109a8c21a54SThe etnaviv authors 	}
1110a8c21a54SThe etnaviv authors 
1111a8c21a54SThe etnaviv authors 	if (!exclusive || !fobj)
1112a8c21a54SThe etnaviv authors 		return 0;
1113a8c21a54SThe etnaviv authors 
1114a8c21a54SThe etnaviv authors 	for (i = 0; i < fobj->shared_count; i++) {
1115a8c21a54SThe etnaviv authors 		fence = rcu_dereference_protected(fobj->shared[i],
1116a8c21a54SThe etnaviv authors 						reservation_object_held(robj));
1117a8c21a54SThe etnaviv authors 		if (fence->context != context) {
1118f54d1867SChris Wilson 			ret = dma_fence_wait(fence, true);
1119a8c21a54SThe etnaviv authors 			if (ret)
1120a8c21a54SThe etnaviv authors 				return ret;
1121a8c21a54SThe etnaviv authors 		}
1122a8c21a54SThe etnaviv authors 	}
1123a8c21a54SThe etnaviv authors 
1124a8c21a54SThe etnaviv authors 	return 0;
1125a8c21a54SThe etnaviv authors }
1126a8c21a54SThe etnaviv authors 
1127a8c21a54SThe etnaviv authors /*
1128a8c21a54SThe etnaviv authors  * event management:
1129a8c21a54SThe etnaviv authors  */
1130a8c21a54SThe etnaviv authors 
1131a8c21a54SThe etnaviv authors static unsigned int event_alloc(struct etnaviv_gpu *gpu)
1132a8c21a54SThe etnaviv authors {
1133a8c21a54SThe etnaviv authors 	unsigned long ret, flags;
1134a8c21a54SThe etnaviv authors 	unsigned int i, event = ~0U;
1135a8c21a54SThe etnaviv authors 
1136a8c21a54SThe etnaviv authors 	ret = wait_for_completion_timeout(&gpu->event_free,
1137a8c21a54SThe etnaviv authors 					  msecs_to_jiffies(10 * 10000));
1138a8c21a54SThe etnaviv authors 	if (!ret)
1139a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "wait_for_completion_timeout failed");
1140a8c21a54SThe etnaviv authors 
1141a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
1142a8c21a54SThe etnaviv authors 
1143a8c21a54SThe etnaviv authors 	/* find first free event */
1144a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
1145a8c21a54SThe etnaviv authors 		if (gpu->event[i].used == false) {
1146a8c21a54SThe etnaviv authors 			gpu->event[i].used = true;
1147a8c21a54SThe etnaviv authors 			event = i;
1148a8c21a54SThe etnaviv authors 			break;
1149a8c21a54SThe etnaviv authors 		}
1150a8c21a54SThe etnaviv authors 	}
1151a8c21a54SThe etnaviv authors 
1152a8c21a54SThe etnaviv authors 	spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1153a8c21a54SThe etnaviv authors 
1154a8c21a54SThe etnaviv authors 	return event;
1155a8c21a54SThe etnaviv authors }
1156a8c21a54SThe etnaviv authors 
1157a8c21a54SThe etnaviv authors static void event_free(struct etnaviv_gpu *gpu, unsigned int event)
1158a8c21a54SThe etnaviv authors {
1159a8c21a54SThe etnaviv authors 	unsigned long flags;
1160a8c21a54SThe etnaviv authors 
1161a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
1162a8c21a54SThe etnaviv authors 
1163a8c21a54SThe etnaviv authors 	if (gpu->event[event].used == false) {
1164a8c21a54SThe etnaviv authors 		dev_warn(gpu->dev, "event %u is already marked as free",
1165a8c21a54SThe etnaviv authors 			 event);
1166a8c21a54SThe etnaviv authors 		spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1167a8c21a54SThe etnaviv authors 	} else {
1168a8c21a54SThe etnaviv authors 		gpu->event[event].used = false;
1169a8c21a54SThe etnaviv authors 		spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1170a8c21a54SThe etnaviv authors 
1171a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
1172a8c21a54SThe etnaviv authors 	}
1173a8c21a54SThe etnaviv authors }
1174a8c21a54SThe etnaviv authors 
1175a8c21a54SThe etnaviv authors /*
1176a8c21a54SThe etnaviv authors  * Cmdstream submission/retirement:
1177a8c21a54SThe etnaviv authors  */
1178a8c21a54SThe etnaviv authors 
1179a8c21a54SThe etnaviv authors static void retire_worker(struct work_struct *work)
1180a8c21a54SThe etnaviv authors {
1181a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
1182a8c21a54SThe etnaviv authors 					       retire_work);
1183a8c21a54SThe etnaviv authors 	u32 fence = gpu->completed_fence;
1184a8c21a54SThe etnaviv authors 	struct etnaviv_cmdbuf *cmdbuf, *tmp;
1185a8c21a54SThe etnaviv authors 	unsigned int i;
1186a8c21a54SThe etnaviv authors 
1187a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
1188a8c21a54SThe etnaviv authors 	list_for_each_entry_safe(cmdbuf, tmp, &gpu->active_cmd_list, node) {
1189f54d1867SChris Wilson 		if (!dma_fence_is_signaled(cmdbuf->fence))
1190a8c21a54SThe etnaviv authors 			break;
1191a8c21a54SThe etnaviv authors 
1192a8c21a54SThe etnaviv authors 		list_del(&cmdbuf->node);
1193f54d1867SChris Wilson 		dma_fence_put(cmdbuf->fence);
1194a8c21a54SThe etnaviv authors 
1195a8c21a54SThe etnaviv authors 		for (i = 0; i < cmdbuf->nr_bos; i++) {
1196b6325f40SRussell King 			struct etnaviv_vram_mapping *mapping = cmdbuf->bo_map[i];
1197b6325f40SRussell King 			struct etnaviv_gem_object *etnaviv_obj = mapping->object;
1198a8c21a54SThe etnaviv authors 
1199a8c21a54SThe etnaviv authors 			atomic_dec(&etnaviv_obj->gpu_active);
1200a8c21a54SThe etnaviv authors 			/* drop the refcount taken in etnaviv_gpu_submit */
1201b6325f40SRussell King 			etnaviv_gem_mapping_unreference(mapping);
1202a8c21a54SThe etnaviv authors 		}
1203a8c21a54SThe etnaviv authors 
1204ea1f5729SLucas Stach 		etnaviv_cmdbuf_free(cmdbuf);
1205d9fd0c7dSLucas Stach 		/*
1206d9fd0c7dSLucas Stach 		 * We need to balance the runtime PM count caused by
1207d9fd0c7dSLucas Stach 		 * each submission.  Upon submission, we increment
1208d9fd0c7dSLucas Stach 		 * the runtime PM counter, and allocate one event.
1209d9fd0c7dSLucas Stach 		 * So here, we put the runtime PM count for each
1210d9fd0c7dSLucas Stach 		 * completed event.
1211d9fd0c7dSLucas Stach 		 */
1212d9fd0c7dSLucas Stach 		pm_runtime_put_autosuspend(gpu->dev);
1213a8c21a54SThe etnaviv authors 	}
1214a8c21a54SThe etnaviv authors 
1215a8c21a54SThe etnaviv authors 	gpu->retired_fence = fence;
1216a8c21a54SThe etnaviv authors 
1217a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1218a8c21a54SThe etnaviv authors 
1219a8c21a54SThe etnaviv authors 	wake_up_all(&gpu->fence_event);
1220a8c21a54SThe etnaviv authors }
1221a8c21a54SThe etnaviv authors 
1222a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu,
1223a8c21a54SThe etnaviv authors 	u32 fence, struct timespec *timeout)
1224a8c21a54SThe etnaviv authors {
1225a8c21a54SThe etnaviv authors 	int ret;
1226a8c21a54SThe etnaviv authors 
1227a8c21a54SThe etnaviv authors 	if (fence_after(fence, gpu->next_fence)) {
1228a8c21a54SThe etnaviv authors 		DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
1229a8c21a54SThe etnaviv authors 				fence, gpu->next_fence);
1230a8c21a54SThe etnaviv authors 		return -EINVAL;
1231a8c21a54SThe etnaviv authors 	}
1232a8c21a54SThe etnaviv authors 
1233a8c21a54SThe etnaviv authors 	if (!timeout) {
1234a8c21a54SThe etnaviv authors 		/* No timeout was requested: just test for completion */
1235a8c21a54SThe etnaviv authors 		ret = fence_completed(gpu, fence) ? 0 : -EBUSY;
1236a8c21a54SThe etnaviv authors 	} else {
1237a8c21a54SThe etnaviv authors 		unsigned long remaining = etnaviv_timeout_to_jiffies(timeout);
1238a8c21a54SThe etnaviv authors 
1239a8c21a54SThe etnaviv authors 		ret = wait_event_interruptible_timeout(gpu->fence_event,
1240a8c21a54SThe etnaviv authors 						fence_completed(gpu, fence),
1241a8c21a54SThe etnaviv authors 						remaining);
1242a8c21a54SThe etnaviv authors 		if (ret == 0) {
1243a8c21a54SThe etnaviv authors 			DBG("timeout waiting for fence: %u (retired: %u completed: %u)",
1244a8c21a54SThe etnaviv authors 				fence, gpu->retired_fence,
1245a8c21a54SThe etnaviv authors 				gpu->completed_fence);
1246a8c21a54SThe etnaviv authors 			ret = -ETIMEDOUT;
1247a8c21a54SThe etnaviv authors 		} else if (ret != -ERESTARTSYS) {
1248a8c21a54SThe etnaviv authors 			ret = 0;
1249a8c21a54SThe etnaviv authors 		}
1250a8c21a54SThe etnaviv authors 	}
1251a8c21a54SThe etnaviv authors 
1252a8c21a54SThe etnaviv authors 	return ret;
1253a8c21a54SThe etnaviv authors }
1254a8c21a54SThe etnaviv authors 
1255a8c21a54SThe etnaviv authors /*
1256a8c21a54SThe etnaviv authors  * Wait for an object to become inactive.  This, on it's own, is not race
1257a8c21a54SThe etnaviv authors  * free: the object is moved by the retire worker off the active list, and
1258a8c21a54SThe etnaviv authors  * then the iova is put.  Moreover, the object could be re-submitted just
1259a8c21a54SThe etnaviv authors  * after we notice that it's become inactive.
1260a8c21a54SThe etnaviv authors  *
1261a8c21a54SThe etnaviv authors  * Although the retirement happens under the gpu lock, we don't want to hold
1262a8c21a54SThe etnaviv authors  * that lock in this function while waiting.
1263a8c21a54SThe etnaviv authors  */
1264a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu,
1265a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout)
1266a8c21a54SThe etnaviv authors {
1267a8c21a54SThe etnaviv authors 	unsigned long remaining;
1268a8c21a54SThe etnaviv authors 	long ret;
1269a8c21a54SThe etnaviv authors 
1270a8c21a54SThe etnaviv authors 	if (!timeout)
1271a8c21a54SThe etnaviv authors 		return !is_active(etnaviv_obj) ? 0 : -EBUSY;
1272a8c21a54SThe etnaviv authors 
1273a8c21a54SThe etnaviv authors 	remaining = etnaviv_timeout_to_jiffies(timeout);
1274a8c21a54SThe etnaviv authors 
1275a8c21a54SThe etnaviv authors 	ret = wait_event_interruptible_timeout(gpu->fence_event,
1276a8c21a54SThe etnaviv authors 					       !is_active(etnaviv_obj),
1277a8c21a54SThe etnaviv authors 					       remaining);
1278a8c21a54SThe etnaviv authors 	if (ret > 0) {
1279a8c21a54SThe etnaviv authors 		struct etnaviv_drm_private *priv = gpu->drm->dev_private;
1280a8c21a54SThe etnaviv authors 
1281a8c21a54SThe etnaviv authors 		/* Synchronise with the retire worker */
1282a8c21a54SThe etnaviv authors 		flush_workqueue(priv->wq);
1283a8c21a54SThe etnaviv authors 		return 0;
1284a8c21a54SThe etnaviv authors 	} else if (ret == -ERESTARTSYS) {
1285a8c21a54SThe etnaviv authors 		return -ERESTARTSYS;
1286a8c21a54SThe etnaviv authors 	} else {
1287a8c21a54SThe etnaviv authors 		return -ETIMEDOUT;
1288a8c21a54SThe etnaviv authors 	}
1289a8c21a54SThe etnaviv authors }
1290a8c21a54SThe etnaviv authors 
1291a8c21a54SThe etnaviv authors int etnaviv_gpu_pm_get_sync(struct etnaviv_gpu *gpu)
1292a8c21a54SThe etnaviv authors {
1293a8c21a54SThe etnaviv authors 	return pm_runtime_get_sync(gpu->dev);
1294a8c21a54SThe etnaviv authors }
1295a8c21a54SThe etnaviv authors 
1296a8c21a54SThe etnaviv authors void etnaviv_gpu_pm_put(struct etnaviv_gpu *gpu)
1297a8c21a54SThe etnaviv authors {
1298a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
1299a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
1300a8c21a54SThe etnaviv authors }
1301a8c21a54SThe etnaviv authors 
1302a8c21a54SThe etnaviv authors /* add bo's to gpu's ring, and kick gpu: */
1303a8c21a54SThe etnaviv authors int etnaviv_gpu_submit(struct etnaviv_gpu *gpu,
1304a8c21a54SThe etnaviv authors 	struct etnaviv_gem_submit *submit, struct etnaviv_cmdbuf *cmdbuf)
1305a8c21a54SThe etnaviv authors {
1306f54d1867SChris Wilson 	struct dma_fence *fence;
1307a8c21a54SThe etnaviv authors 	unsigned int event, i;
1308a8c21a54SThe etnaviv authors 	int ret;
1309a8c21a54SThe etnaviv authors 
1310a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_pm_get_sync(gpu);
1311a8c21a54SThe etnaviv authors 	if (ret < 0)
1312a8c21a54SThe etnaviv authors 		return ret;
1313a8c21a54SThe etnaviv authors 
1314a8c21a54SThe etnaviv authors 	/*
1315a8c21a54SThe etnaviv authors 	 * TODO
1316a8c21a54SThe etnaviv authors 	 *
1317a8c21a54SThe etnaviv authors 	 * - flush
1318a8c21a54SThe etnaviv authors 	 * - data endian
1319a8c21a54SThe etnaviv authors 	 * - prefetch
1320a8c21a54SThe etnaviv authors 	 *
1321a8c21a54SThe etnaviv authors 	 */
1322a8c21a54SThe etnaviv authors 
1323a8c21a54SThe etnaviv authors 	event = event_alloc(gpu);
1324a8c21a54SThe etnaviv authors 	if (unlikely(event == ~0U)) {
1325a8c21a54SThe etnaviv authors 		DRM_ERROR("no free event\n");
1326a8c21a54SThe etnaviv authors 		ret = -EBUSY;
1327d9853490SLucas Stach 		goto out_pm_put;
1328a8c21a54SThe etnaviv authors 	}
1329a8c21a54SThe etnaviv authors 
1330a8c21a54SThe etnaviv authors 	fence = etnaviv_gpu_fence_alloc(gpu);
1331a8c21a54SThe etnaviv authors 	if (!fence) {
1332a8c21a54SThe etnaviv authors 		event_free(gpu, event);
1333a8c21a54SThe etnaviv authors 		ret = -ENOMEM;
1334d9853490SLucas Stach 		goto out_pm_put;
1335a8c21a54SThe etnaviv authors 	}
1336a8c21a54SThe etnaviv authors 
1337d9853490SLucas Stach 	mutex_lock(&gpu->lock);
1338d9853490SLucas Stach 
1339a8c21a54SThe etnaviv authors 	gpu->event[event].fence = fence;
1340a8c21a54SThe etnaviv authors 	submit->fence = fence->seqno;
1341a8c21a54SThe etnaviv authors 	gpu->active_fence = submit->fence;
1342a8c21a54SThe etnaviv authors 
1343a8c21a54SThe etnaviv authors 	if (gpu->lastctx != cmdbuf->ctx) {
1344a8c21a54SThe etnaviv authors 		gpu->mmu->need_flush = true;
1345a8c21a54SThe etnaviv authors 		gpu->switch_context = true;
1346a8c21a54SThe etnaviv authors 		gpu->lastctx = cmdbuf->ctx;
1347a8c21a54SThe etnaviv authors 	}
1348a8c21a54SThe etnaviv authors 
1349a8c21a54SThe etnaviv authors 	etnaviv_buffer_queue(gpu, event, cmdbuf);
1350a8c21a54SThe etnaviv authors 
1351a8c21a54SThe etnaviv authors 	cmdbuf->fence = fence;
1352a8c21a54SThe etnaviv authors 	list_add_tail(&cmdbuf->node, &gpu->active_cmd_list);
1353a8c21a54SThe etnaviv authors 
1354a8c21a54SThe etnaviv authors 	/* We're committed to adding this command buffer, hold a PM reference */
1355a8c21a54SThe etnaviv authors 	pm_runtime_get_noresume(gpu->dev);
1356a8c21a54SThe etnaviv authors 
1357a8c21a54SThe etnaviv authors 	for (i = 0; i < submit->nr_bos; i++) {
1358a8c21a54SThe etnaviv authors 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
1359a8c21a54SThe etnaviv authors 
1360b6325f40SRussell King 		/* Each cmdbuf takes a refcount on the mapping */
1361b6325f40SRussell King 		etnaviv_gem_mapping_reference(submit->bos[i].mapping);
1362b6325f40SRussell King 		cmdbuf->bo_map[i] = submit->bos[i].mapping;
1363a8c21a54SThe etnaviv authors 		atomic_inc(&etnaviv_obj->gpu_active);
1364a8c21a54SThe etnaviv authors 
1365a8c21a54SThe etnaviv authors 		if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
1366a8c21a54SThe etnaviv authors 			reservation_object_add_excl_fence(etnaviv_obj->resv,
1367a8c21a54SThe etnaviv authors 							  fence);
1368a8c21a54SThe etnaviv authors 		else
1369a8c21a54SThe etnaviv authors 			reservation_object_add_shared_fence(etnaviv_obj->resv,
1370a8c21a54SThe etnaviv authors 							    fence);
1371a8c21a54SThe etnaviv authors 	}
1372a8c21a54SThe etnaviv authors 	cmdbuf->nr_bos = submit->nr_bos;
1373a8c21a54SThe etnaviv authors 	hangcheck_timer_reset(gpu);
1374a8c21a54SThe etnaviv authors 	ret = 0;
1375a8c21a54SThe etnaviv authors 
1376a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1377a8c21a54SThe etnaviv authors 
1378d9853490SLucas Stach out_pm_put:
1379a8c21a54SThe etnaviv authors 	etnaviv_gpu_pm_put(gpu);
1380a8c21a54SThe etnaviv authors 
1381a8c21a54SThe etnaviv authors 	return ret;
1382a8c21a54SThe etnaviv authors }
1383a8c21a54SThe etnaviv authors 
1384a8c21a54SThe etnaviv authors /*
1385a8c21a54SThe etnaviv authors  * Init/Cleanup:
1386a8c21a54SThe etnaviv authors  */
1387a8c21a54SThe etnaviv authors static irqreturn_t irq_handler(int irq, void *data)
1388a8c21a54SThe etnaviv authors {
1389a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = data;
1390a8c21a54SThe etnaviv authors 	irqreturn_t ret = IRQ_NONE;
1391a8c21a54SThe etnaviv authors 
1392a8c21a54SThe etnaviv authors 	u32 intr = gpu_read(gpu, VIVS_HI_INTR_ACKNOWLEDGE);
1393a8c21a54SThe etnaviv authors 
1394a8c21a54SThe etnaviv authors 	if (intr != 0) {
1395a8c21a54SThe etnaviv authors 		int event;
1396a8c21a54SThe etnaviv authors 
1397a8c21a54SThe etnaviv authors 		pm_runtime_mark_last_busy(gpu->dev);
1398a8c21a54SThe etnaviv authors 
1399a8c21a54SThe etnaviv authors 		dev_dbg(gpu->dev, "intr 0x%08x\n", intr);
1400a8c21a54SThe etnaviv authors 
1401a8c21a54SThe etnaviv authors 		if (intr & VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR) {
1402a8c21a54SThe etnaviv authors 			dev_err(gpu->dev, "AXI bus error\n");
1403a8c21a54SThe etnaviv authors 			intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR;
1404a8c21a54SThe etnaviv authors 		}
1405a8c21a54SThe etnaviv authors 
1406128a9b1dSLucas Stach 		if (intr & VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION) {
1407128a9b1dSLucas Stach 			int i;
1408128a9b1dSLucas Stach 
1409128a9b1dSLucas Stach 			dev_err_ratelimited(gpu->dev,
1410128a9b1dSLucas Stach 				"MMU fault status 0x%08x\n",
1411128a9b1dSLucas Stach 				gpu_read(gpu, VIVS_MMUv2_STATUS));
1412128a9b1dSLucas Stach 			for (i = 0; i < 4; i++) {
1413128a9b1dSLucas Stach 				dev_err_ratelimited(gpu->dev,
1414128a9b1dSLucas Stach 					"MMU %d fault addr 0x%08x\n",
1415128a9b1dSLucas Stach 					i, gpu_read(gpu,
1416128a9b1dSLucas Stach 					VIVS_MMUv2_EXCEPTION_ADDR(i)));
1417128a9b1dSLucas Stach 			}
1418128a9b1dSLucas Stach 			intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION;
1419128a9b1dSLucas Stach 		}
1420128a9b1dSLucas Stach 
1421a8c21a54SThe etnaviv authors 		while ((event = ffs(intr)) != 0) {
1422f54d1867SChris Wilson 			struct dma_fence *fence;
1423a8c21a54SThe etnaviv authors 
1424a8c21a54SThe etnaviv authors 			event -= 1;
1425a8c21a54SThe etnaviv authors 
1426a8c21a54SThe etnaviv authors 			intr &= ~(1 << event);
1427a8c21a54SThe etnaviv authors 
1428a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "event %u\n", event);
1429a8c21a54SThe etnaviv authors 
1430a8c21a54SThe etnaviv authors 			fence = gpu->event[event].fence;
1431a8c21a54SThe etnaviv authors 			gpu->event[event].fence = NULL;
1432f54d1867SChris Wilson 			dma_fence_signal(fence);
1433a8c21a54SThe etnaviv authors 
1434a8c21a54SThe etnaviv authors 			/*
1435a8c21a54SThe etnaviv authors 			 * Events can be processed out of order.  Eg,
1436a8c21a54SThe etnaviv authors 			 * - allocate and queue event 0
1437a8c21a54SThe etnaviv authors 			 * - allocate event 1
1438a8c21a54SThe etnaviv authors 			 * - event 0 completes, we process it
1439a8c21a54SThe etnaviv authors 			 * - allocate and queue event 0
1440a8c21a54SThe etnaviv authors 			 * - event 1 and event 0 complete
1441a8c21a54SThe etnaviv authors 			 * we can end up processing event 0 first, then 1.
1442a8c21a54SThe etnaviv authors 			 */
1443a8c21a54SThe etnaviv authors 			if (fence_after(fence->seqno, gpu->completed_fence))
1444a8c21a54SThe etnaviv authors 				gpu->completed_fence = fence->seqno;
1445a8c21a54SThe etnaviv authors 
1446a8c21a54SThe etnaviv authors 			event_free(gpu, event);
1447a8c21a54SThe etnaviv authors 		}
1448a8c21a54SThe etnaviv authors 
1449a8c21a54SThe etnaviv authors 		/* Retire the buffer objects in a work */
1450a8c21a54SThe etnaviv authors 		etnaviv_queue_work(gpu->drm, &gpu->retire_work);
1451a8c21a54SThe etnaviv authors 
1452a8c21a54SThe etnaviv authors 		ret = IRQ_HANDLED;
1453a8c21a54SThe etnaviv authors 	}
1454a8c21a54SThe etnaviv authors 
1455a8c21a54SThe etnaviv authors 	return ret;
1456a8c21a54SThe etnaviv authors }
1457a8c21a54SThe etnaviv authors 
1458a8c21a54SThe etnaviv authors static int etnaviv_gpu_clk_enable(struct etnaviv_gpu *gpu)
1459a8c21a54SThe etnaviv authors {
1460a8c21a54SThe etnaviv authors 	int ret;
1461a8c21a54SThe etnaviv authors 
14629c7310c0SLucas Stach 	if (gpu->clk_bus) {
14639c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_bus);
1464a8c21a54SThe etnaviv authors 		if (ret)
1465a8c21a54SThe etnaviv authors 			return ret;
1466a8c21a54SThe etnaviv authors 	}
1467a8c21a54SThe etnaviv authors 
14689c7310c0SLucas Stach 	if (gpu->clk_core) {
14699c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_core);
14709c7310c0SLucas Stach 		if (ret)
14719c7310c0SLucas Stach 			goto disable_clk_bus;
14729c7310c0SLucas Stach 	}
14739c7310c0SLucas Stach 
14749c7310c0SLucas Stach 	if (gpu->clk_shader) {
14759c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_shader);
14769c7310c0SLucas Stach 		if (ret)
14779c7310c0SLucas Stach 			goto disable_clk_core;
14789c7310c0SLucas Stach 	}
14799c7310c0SLucas Stach 
1480a8c21a54SThe etnaviv authors 	return 0;
14819c7310c0SLucas Stach 
14829c7310c0SLucas Stach disable_clk_core:
14839c7310c0SLucas Stach 	if (gpu->clk_core)
14849c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_core);
14859c7310c0SLucas Stach disable_clk_bus:
14869c7310c0SLucas Stach 	if (gpu->clk_bus)
14879c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_bus);
14889c7310c0SLucas Stach 
14899c7310c0SLucas Stach 	return ret;
1490a8c21a54SThe etnaviv authors }
1491a8c21a54SThe etnaviv authors 
1492a8c21a54SThe etnaviv authors static int etnaviv_gpu_clk_disable(struct etnaviv_gpu *gpu)
1493a8c21a54SThe etnaviv authors {
14949c7310c0SLucas Stach 	if (gpu->clk_shader)
14959c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_shader);
14969c7310c0SLucas Stach 	if (gpu->clk_core)
14979c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_core);
14989c7310c0SLucas Stach 	if (gpu->clk_bus)
14999c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_bus);
1500a8c21a54SThe etnaviv authors 
1501a8c21a54SThe etnaviv authors 	return 0;
1502a8c21a54SThe etnaviv authors }
1503a8c21a54SThe etnaviv authors 
1504b88163e3SLucas Stach int etnaviv_gpu_wait_idle(struct etnaviv_gpu *gpu, unsigned int timeout_ms)
1505b88163e3SLucas Stach {
1506b88163e3SLucas Stach 	unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1507b88163e3SLucas Stach 
1508b88163e3SLucas Stach 	do {
1509b88163e3SLucas Stach 		u32 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
1510b88163e3SLucas Stach 
1511b88163e3SLucas Stach 		if ((idle & gpu->idle_mask) == gpu->idle_mask)
1512b88163e3SLucas Stach 			return 0;
1513b88163e3SLucas Stach 
1514b88163e3SLucas Stach 		if (time_is_before_jiffies(timeout)) {
1515b88163e3SLucas Stach 			dev_warn(gpu->dev,
1516b88163e3SLucas Stach 				 "timed out waiting for idle: idle=0x%x\n",
1517b88163e3SLucas Stach 				 idle);
1518b88163e3SLucas Stach 			return -ETIMEDOUT;
1519b88163e3SLucas Stach 		}
1520b88163e3SLucas Stach 
1521b88163e3SLucas Stach 		udelay(5);
1522b88163e3SLucas Stach 	} while (1);
1523b88163e3SLucas Stach }
1524b88163e3SLucas Stach 
1525a8c21a54SThe etnaviv authors static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
1526a8c21a54SThe etnaviv authors {
1527a8c21a54SThe etnaviv authors 	if (gpu->buffer) {
1528a8c21a54SThe etnaviv authors 		/* Replace the last WAIT with END */
1529a8c21a54SThe etnaviv authors 		etnaviv_buffer_end(gpu);
1530a8c21a54SThe etnaviv authors 
1531a8c21a54SThe etnaviv authors 		/*
1532a8c21a54SThe etnaviv authors 		 * We know that only the FE is busy here, this should
1533a8c21a54SThe etnaviv authors 		 * happen quickly (as the WAIT is only 200 cycles).  If
1534a8c21a54SThe etnaviv authors 		 * we fail, just warn and continue.
1535a8c21a54SThe etnaviv authors 		 */
1536b88163e3SLucas Stach 		etnaviv_gpu_wait_idle(gpu, 100);
1537a8c21a54SThe etnaviv authors 	}
1538a8c21a54SThe etnaviv authors 
1539a8c21a54SThe etnaviv authors 	return etnaviv_gpu_clk_disable(gpu);
1540a8c21a54SThe etnaviv authors }
1541a8c21a54SThe etnaviv authors 
1542a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1543a8c21a54SThe etnaviv authors static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
1544a8c21a54SThe etnaviv authors {
1545a8c21a54SThe etnaviv authors 	int ret;
1546a8c21a54SThe etnaviv authors 
1547a8c21a54SThe etnaviv authors 	ret = mutex_lock_killable(&gpu->lock);
1548a8c21a54SThe etnaviv authors 	if (ret)
1549a8c21a54SThe etnaviv authors 		return ret;
1550a8c21a54SThe etnaviv authors 
1551bcdfb5e5SRussell King 	etnaviv_gpu_update_clock(gpu);
1552a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
1553a8c21a54SThe etnaviv authors 
1554a8c21a54SThe etnaviv authors 	gpu->switch_context = true;
1555f6086311SRussell King 	gpu->exec_state = -1;
1556a8c21a54SThe etnaviv authors 
1557a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1558a8c21a54SThe etnaviv authors 
1559a8c21a54SThe etnaviv authors 	return 0;
1560a8c21a54SThe etnaviv authors }
1561a8c21a54SThe etnaviv authors #endif
1562a8c21a54SThe etnaviv authors 
1563bcdfb5e5SRussell King static int
1564bcdfb5e5SRussell King etnaviv_gpu_cooling_get_max_state(struct thermal_cooling_device *cdev,
1565bcdfb5e5SRussell King 				  unsigned long *state)
1566bcdfb5e5SRussell King {
1567bcdfb5e5SRussell King 	*state = 6;
1568bcdfb5e5SRussell King 
1569bcdfb5e5SRussell King 	return 0;
1570bcdfb5e5SRussell King }
1571bcdfb5e5SRussell King 
1572bcdfb5e5SRussell King static int
1573bcdfb5e5SRussell King etnaviv_gpu_cooling_get_cur_state(struct thermal_cooling_device *cdev,
1574bcdfb5e5SRussell King 				  unsigned long *state)
1575bcdfb5e5SRussell King {
1576bcdfb5e5SRussell King 	struct etnaviv_gpu *gpu = cdev->devdata;
1577bcdfb5e5SRussell King 
1578bcdfb5e5SRussell King 	*state = gpu->freq_scale;
1579bcdfb5e5SRussell King 
1580bcdfb5e5SRussell King 	return 0;
1581bcdfb5e5SRussell King }
1582bcdfb5e5SRussell King 
1583bcdfb5e5SRussell King static int
1584bcdfb5e5SRussell King etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
1585bcdfb5e5SRussell King 				  unsigned long state)
1586bcdfb5e5SRussell King {
1587bcdfb5e5SRussell King 	struct etnaviv_gpu *gpu = cdev->devdata;
1588bcdfb5e5SRussell King 
1589bcdfb5e5SRussell King 	mutex_lock(&gpu->lock);
1590bcdfb5e5SRussell King 	gpu->freq_scale = state;
1591bcdfb5e5SRussell King 	if (!pm_runtime_suspended(gpu->dev))
1592bcdfb5e5SRussell King 		etnaviv_gpu_update_clock(gpu);
1593bcdfb5e5SRussell King 	mutex_unlock(&gpu->lock);
1594bcdfb5e5SRussell King 
1595bcdfb5e5SRussell King 	return 0;
1596bcdfb5e5SRussell King }
1597bcdfb5e5SRussell King 
1598bcdfb5e5SRussell King static struct thermal_cooling_device_ops cooling_ops = {
1599bcdfb5e5SRussell King 	.get_max_state = etnaviv_gpu_cooling_get_max_state,
1600bcdfb5e5SRussell King 	.get_cur_state = etnaviv_gpu_cooling_get_cur_state,
1601bcdfb5e5SRussell King 	.set_cur_state = etnaviv_gpu_cooling_set_cur_state,
1602bcdfb5e5SRussell King };
1603bcdfb5e5SRussell King 
1604a8c21a54SThe etnaviv authors static int etnaviv_gpu_bind(struct device *dev, struct device *master,
1605a8c21a54SThe etnaviv authors 	void *data)
1606a8c21a54SThe etnaviv authors {
1607a8c21a54SThe etnaviv authors 	struct drm_device *drm = data;
1608a8c21a54SThe etnaviv authors 	struct etnaviv_drm_private *priv = drm->dev_private;
1609a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1610a8c21a54SThe etnaviv authors 	int ret;
1611a8c21a54SThe etnaviv authors 
1612bcdfb5e5SRussell King 	gpu->cooling = thermal_of_cooling_device_register(dev->of_node,
1613bcdfb5e5SRussell King 				(char *)dev_name(dev), gpu, &cooling_ops);
1614bcdfb5e5SRussell King 	if (IS_ERR(gpu->cooling))
1615bcdfb5e5SRussell King 		return PTR_ERR(gpu->cooling);
1616bcdfb5e5SRussell King 
1617a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1618a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
1619a8c21a54SThe etnaviv authors #else
1620a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_clk_enable(gpu);
1621a8c21a54SThe etnaviv authors #endif
1622bcdfb5e5SRussell King 	if (ret < 0) {
1623bcdfb5e5SRussell King 		thermal_cooling_device_unregister(gpu->cooling);
1624a8c21a54SThe etnaviv authors 		return ret;
1625bcdfb5e5SRussell King 	}
1626a8c21a54SThe etnaviv authors 
1627a8c21a54SThe etnaviv authors 	gpu->drm = drm;
1628f54d1867SChris Wilson 	gpu->fence_context = dma_fence_context_alloc(1);
1629a8c21a54SThe etnaviv authors 	spin_lock_init(&gpu->fence_spinlock);
1630a8c21a54SThe etnaviv authors 
1631a8c21a54SThe etnaviv authors 	INIT_LIST_HEAD(&gpu->active_cmd_list);
1632a8c21a54SThe etnaviv authors 	INIT_WORK(&gpu->retire_work, retire_worker);
1633a8c21a54SThe etnaviv authors 	INIT_WORK(&gpu->recover_work, recover_worker);
1634a8c21a54SThe etnaviv authors 	init_waitqueue_head(&gpu->fence_event);
1635a8c21a54SThe etnaviv authors 
1636946dd8d5SLucas Stach 	setup_deferrable_timer(&gpu->hangcheck_timer, hangcheck_handler,
1637a8c21a54SThe etnaviv authors 			       (unsigned long)gpu);
1638a8c21a54SThe etnaviv authors 
1639a8c21a54SThe etnaviv authors 	priv->gpu[priv->num_gpus++] = gpu;
1640a8c21a54SThe etnaviv authors 
1641a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
1642a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
1643a8c21a54SThe etnaviv authors 
1644a8c21a54SThe etnaviv authors 	return 0;
1645a8c21a54SThe etnaviv authors }
1646a8c21a54SThe etnaviv authors 
1647a8c21a54SThe etnaviv authors static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
1648a8c21a54SThe etnaviv authors 	void *data)
1649a8c21a54SThe etnaviv authors {
1650a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1651a8c21a54SThe etnaviv authors 
1652a8c21a54SThe etnaviv authors 	DBG("%s", dev_name(gpu->dev));
1653a8c21a54SThe etnaviv authors 
1654a8c21a54SThe etnaviv authors 	hangcheck_disable(gpu);
1655a8c21a54SThe etnaviv authors 
1656a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1657a8c21a54SThe etnaviv authors 	pm_runtime_get_sync(gpu->dev);
1658a8c21a54SThe etnaviv authors 	pm_runtime_put_sync_suspend(gpu->dev);
1659a8c21a54SThe etnaviv authors #else
1660a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_suspend(gpu);
1661a8c21a54SThe etnaviv authors #endif
1662a8c21a54SThe etnaviv authors 
1663a8c21a54SThe etnaviv authors 	if (gpu->buffer) {
1664ea1f5729SLucas Stach 		etnaviv_cmdbuf_free(gpu->buffer);
1665a8c21a54SThe etnaviv authors 		gpu->buffer = NULL;
1666a8c21a54SThe etnaviv authors 	}
1667a8c21a54SThe etnaviv authors 
1668e66774ddSLucas Stach 	if (gpu->cmdbuf_suballoc) {
1669e66774ddSLucas Stach 		etnaviv_cmdbuf_suballoc_destroy(gpu->cmdbuf_suballoc);
1670e66774ddSLucas Stach 		gpu->cmdbuf_suballoc = NULL;
1671e66774ddSLucas Stach 	}
1672e66774ddSLucas Stach 
1673a8c21a54SThe etnaviv authors 	if (gpu->mmu) {
1674a8c21a54SThe etnaviv authors 		etnaviv_iommu_destroy(gpu->mmu);
1675a8c21a54SThe etnaviv authors 		gpu->mmu = NULL;
1676a8c21a54SThe etnaviv authors 	}
1677a8c21a54SThe etnaviv authors 
1678a8c21a54SThe etnaviv authors 	gpu->drm = NULL;
1679bcdfb5e5SRussell King 
1680bcdfb5e5SRussell King 	thermal_cooling_device_unregister(gpu->cooling);
1681bcdfb5e5SRussell King 	gpu->cooling = NULL;
1682a8c21a54SThe etnaviv authors }
1683a8c21a54SThe etnaviv authors 
1684a8c21a54SThe etnaviv authors static const struct component_ops gpu_ops = {
1685a8c21a54SThe etnaviv authors 	.bind = etnaviv_gpu_bind,
1686a8c21a54SThe etnaviv authors 	.unbind = etnaviv_gpu_unbind,
1687a8c21a54SThe etnaviv authors };
1688a8c21a54SThe etnaviv authors 
1689a8c21a54SThe etnaviv authors static const struct of_device_id etnaviv_gpu_match[] = {
1690a8c21a54SThe etnaviv authors 	{
1691a8c21a54SThe etnaviv authors 		.compatible = "vivante,gc"
1692a8c21a54SThe etnaviv authors 	},
1693a8c21a54SThe etnaviv authors 	{ /* sentinel */ }
1694a8c21a54SThe etnaviv authors };
1695a8c21a54SThe etnaviv authors 
1696a8c21a54SThe etnaviv authors static int etnaviv_gpu_platform_probe(struct platform_device *pdev)
1697a8c21a54SThe etnaviv authors {
1698a8c21a54SThe etnaviv authors 	struct device *dev = &pdev->dev;
1699a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
1700dc227890SFabio Estevam 	int err;
1701a8c21a54SThe etnaviv authors 
1702a8c21a54SThe etnaviv authors 	gpu = devm_kzalloc(dev, sizeof(*gpu), GFP_KERNEL);
1703a8c21a54SThe etnaviv authors 	if (!gpu)
1704a8c21a54SThe etnaviv authors 		return -ENOMEM;
1705a8c21a54SThe etnaviv authors 
1706a8c21a54SThe etnaviv authors 	gpu->dev = &pdev->dev;
1707a8c21a54SThe etnaviv authors 	mutex_init(&gpu->lock);
1708a8c21a54SThe etnaviv authors 
1709a8c21a54SThe etnaviv authors 	/* Map registers: */
1710a8c21a54SThe etnaviv authors 	gpu->mmio = etnaviv_ioremap(pdev, NULL, dev_name(gpu->dev));
1711a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->mmio))
1712a8c21a54SThe etnaviv authors 		return PTR_ERR(gpu->mmio);
1713a8c21a54SThe etnaviv authors 
1714a8c21a54SThe etnaviv authors 	/* Get Interrupt: */
1715a8c21a54SThe etnaviv authors 	gpu->irq = platform_get_irq(pdev, 0);
1716a8c21a54SThe etnaviv authors 	if (gpu->irq < 0) {
1717db60eda3SFabio Estevam 		dev_err(dev, "failed to get irq: %d\n", gpu->irq);
1718db60eda3SFabio Estevam 		return gpu->irq;
1719a8c21a54SThe etnaviv authors 	}
1720a8c21a54SThe etnaviv authors 
1721a8c21a54SThe etnaviv authors 	err = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 0,
1722a8c21a54SThe etnaviv authors 			       dev_name(gpu->dev), gpu);
1723a8c21a54SThe etnaviv authors 	if (err) {
1724a8c21a54SThe etnaviv authors 		dev_err(dev, "failed to request IRQ%u: %d\n", gpu->irq, err);
1725db60eda3SFabio Estevam 		return err;
1726a8c21a54SThe etnaviv authors 	}
1727a8c21a54SThe etnaviv authors 
1728a8c21a54SThe etnaviv authors 	/* Get Clocks: */
1729a8c21a54SThe etnaviv authors 	gpu->clk_bus = devm_clk_get(&pdev->dev, "bus");
1730a8c21a54SThe etnaviv authors 	DBG("clk_bus: %p", gpu->clk_bus);
1731a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_bus))
1732a8c21a54SThe etnaviv authors 		gpu->clk_bus = NULL;
1733a8c21a54SThe etnaviv authors 
1734a8c21a54SThe etnaviv authors 	gpu->clk_core = devm_clk_get(&pdev->dev, "core");
1735a8c21a54SThe etnaviv authors 	DBG("clk_core: %p", gpu->clk_core);
1736a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_core))
1737a8c21a54SThe etnaviv authors 		gpu->clk_core = NULL;
1738a8c21a54SThe etnaviv authors 
1739a8c21a54SThe etnaviv authors 	gpu->clk_shader = devm_clk_get(&pdev->dev, "shader");
1740a8c21a54SThe etnaviv authors 	DBG("clk_shader: %p", gpu->clk_shader);
1741a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_shader))
1742a8c21a54SThe etnaviv authors 		gpu->clk_shader = NULL;
1743a8c21a54SThe etnaviv authors 
1744a8c21a54SThe etnaviv authors 	/* TODO: figure out max mapped size */
1745a8c21a54SThe etnaviv authors 	dev_set_drvdata(dev, gpu);
1746a8c21a54SThe etnaviv authors 
1747a8c21a54SThe etnaviv authors 	/*
1748a8c21a54SThe etnaviv authors 	 * We treat the device as initially suspended.  The runtime PM
1749a8c21a54SThe etnaviv authors 	 * autosuspend delay is rather arbitary: no measurements have
1750a8c21a54SThe etnaviv authors 	 * yet been performed to determine an appropriate value.
1751a8c21a54SThe etnaviv authors 	 */
1752a8c21a54SThe etnaviv authors 	pm_runtime_use_autosuspend(gpu->dev);
1753a8c21a54SThe etnaviv authors 	pm_runtime_set_autosuspend_delay(gpu->dev, 200);
1754a8c21a54SThe etnaviv authors 	pm_runtime_enable(gpu->dev);
1755a8c21a54SThe etnaviv authors 
1756a8c21a54SThe etnaviv authors 	err = component_add(&pdev->dev, &gpu_ops);
1757a8c21a54SThe etnaviv authors 	if (err < 0) {
1758a8c21a54SThe etnaviv authors 		dev_err(&pdev->dev, "failed to register component: %d\n", err);
1759db60eda3SFabio Estevam 		return err;
1760a8c21a54SThe etnaviv authors 	}
1761a8c21a54SThe etnaviv authors 
1762a8c21a54SThe etnaviv authors 	return 0;
1763a8c21a54SThe etnaviv authors }
1764a8c21a54SThe etnaviv authors 
1765a8c21a54SThe etnaviv authors static int etnaviv_gpu_platform_remove(struct platform_device *pdev)
1766a8c21a54SThe etnaviv authors {
1767a8c21a54SThe etnaviv authors 	component_del(&pdev->dev, &gpu_ops);
1768a8c21a54SThe etnaviv authors 	pm_runtime_disable(&pdev->dev);
1769a8c21a54SThe etnaviv authors 	return 0;
1770a8c21a54SThe etnaviv authors }
1771a8c21a54SThe etnaviv authors 
1772a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1773a8c21a54SThe etnaviv authors static int etnaviv_gpu_rpm_suspend(struct device *dev)
1774a8c21a54SThe etnaviv authors {
1775a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1776a8c21a54SThe etnaviv authors 	u32 idle, mask;
1777a8c21a54SThe etnaviv authors 
1778a8c21a54SThe etnaviv authors 	/* If we have outstanding fences, we're not idle */
1779a8c21a54SThe etnaviv authors 	if (gpu->completed_fence != gpu->active_fence)
1780a8c21a54SThe etnaviv authors 		return -EBUSY;
1781a8c21a54SThe etnaviv authors 
1782a8c21a54SThe etnaviv authors 	/* Check whether the hardware (except FE) is idle */
1783a8c21a54SThe etnaviv authors 	mask = gpu->idle_mask & ~VIVS_HI_IDLE_STATE_FE;
1784a8c21a54SThe etnaviv authors 	idle = gpu_read(gpu, VIVS_HI_IDLE_STATE) & mask;
1785a8c21a54SThe etnaviv authors 	if (idle != mask)
1786a8c21a54SThe etnaviv authors 		return -EBUSY;
1787a8c21a54SThe etnaviv authors 
1788a8c21a54SThe etnaviv authors 	return etnaviv_gpu_hw_suspend(gpu);
1789a8c21a54SThe etnaviv authors }
1790a8c21a54SThe etnaviv authors 
1791a8c21a54SThe etnaviv authors static int etnaviv_gpu_rpm_resume(struct device *dev)
1792a8c21a54SThe etnaviv authors {
1793a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1794a8c21a54SThe etnaviv authors 	int ret;
1795a8c21a54SThe etnaviv authors 
1796a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_clk_enable(gpu);
1797a8c21a54SThe etnaviv authors 	if (ret)
1798a8c21a54SThe etnaviv authors 		return ret;
1799a8c21a54SThe etnaviv authors 
1800a8c21a54SThe etnaviv authors 	/* Re-initialise the basic hardware state */
1801a8c21a54SThe etnaviv authors 	if (gpu->drm && gpu->buffer) {
1802a8c21a54SThe etnaviv authors 		ret = etnaviv_gpu_hw_resume(gpu);
1803a8c21a54SThe etnaviv authors 		if (ret) {
1804a8c21a54SThe etnaviv authors 			etnaviv_gpu_clk_disable(gpu);
1805a8c21a54SThe etnaviv authors 			return ret;
1806a8c21a54SThe etnaviv authors 		}
1807a8c21a54SThe etnaviv authors 	}
1808a8c21a54SThe etnaviv authors 
1809a8c21a54SThe etnaviv authors 	return 0;
1810a8c21a54SThe etnaviv authors }
1811a8c21a54SThe etnaviv authors #endif
1812a8c21a54SThe etnaviv authors 
1813a8c21a54SThe etnaviv authors static const struct dev_pm_ops etnaviv_gpu_pm_ops = {
1814a8c21a54SThe etnaviv authors 	SET_RUNTIME_PM_OPS(etnaviv_gpu_rpm_suspend, etnaviv_gpu_rpm_resume,
1815a8c21a54SThe etnaviv authors 			   NULL)
1816a8c21a54SThe etnaviv authors };
1817a8c21a54SThe etnaviv authors 
1818a8c21a54SThe etnaviv authors struct platform_driver etnaviv_gpu_driver = {
1819a8c21a54SThe etnaviv authors 	.driver = {
1820a8c21a54SThe etnaviv authors 		.name = "etnaviv-gpu",
1821a8c21a54SThe etnaviv authors 		.owner = THIS_MODULE,
1822a8c21a54SThe etnaviv authors 		.pm = &etnaviv_gpu_pm_ops,
1823a8c21a54SThe etnaviv authors 		.of_match_table = etnaviv_gpu_match,
1824a8c21a54SThe etnaviv authors 	},
1825a8c21a54SThe etnaviv authors 	.probe = etnaviv_gpu_platform_probe,
1826a8c21a54SThe etnaviv authors 	.remove = etnaviv_gpu_platform_remove,
1827a8c21a54SThe etnaviv authors 	.id_table = gpu_ids,
1828a8c21a54SThe etnaviv authors };
1829