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"
28357713ceSChristian Gmeiner #include "etnaviv_perfmon.h"
29a8c21a54SThe etnaviv authors #include "common.xml.h"
30a8c21a54SThe etnaviv authors #include "state.xml.h"
31a8c21a54SThe etnaviv authors #include "state_hi.xml.h"
32a8c21a54SThe etnaviv authors #include "cmdstream.xml.h"
33a8c21a54SThe etnaviv authors 
34c09d7f79SLucas Stach #ifndef PHYS_OFFSET
35c09d7f79SLucas Stach #define PHYS_OFFSET 0
36c09d7f79SLucas Stach #endif
37c09d7f79SLucas Stach 
38a8c21a54SThe etnaviv authors static const struct platform_device_id gpu_ids[] = {
39a8c21a54SThe etnaviv authors 	{ .name = "etnaviv-gpu,2d" },
40a8c21a54SThe etnaviv authors 	{ },
41a8c21a54SThe etnaviv authors };
42a8c21a54SThe etnaviv authors 
43a8c21a54SThe etnaviv authors static bool etnaviv_dump_core = true;
44a8c21a54SThe etnaviv authors module_param_named(dump_core, etnaviv_dump_core, bool, 0600);
45a8c21a54SThe etnaviv authors 
46a8c21a54SThe etnaviv authors /*
47a8c21a54SThe etnaviv authors  * Driver functions:
48a8c21a54SThe etnaviv authors  */
49a8c21a54SThe etnaviv authors 
50a8c21a54SThe etnaviv authors int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value)
51a8c21a54SThe etnaviv authors {
52a8c21a54SThe etnaviv authors 	switch (param) {
53a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_MODEL:
54a8c21a54SThe etnaviv authors 		*value = gpu->identity.model;
55a8c21a54SThe etnaviv authors 		break;
56a8c21a54SThe etnaviv authors 
57a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_REVISION:
58a8c21a54SThe etnaviv authors 		*value = gpu->identity.revision;
59a8c21a54SThe etnaviv authors 		break;
60a8c21a54SThe etnaviv authors 
61a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_0:
62a8c21a54SThe etnaviv authors 		*value = gpu->identity.features;
63a8c21a54SThe etnaviv authors 		break;
64a8c21a54SThe etnaviv authors 
65a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_1:
66a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features0;
67a8c21a54SThe etnaviv authors 		break;
68a8c21a54SThe etnaviv authors 
69a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_2:
70a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features1;
71a8c21a54SThe etnaviv authors 		break;
72a8c21a54SThe etnaviv authors 
73a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_3:
74a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features2;
75a8c21a54SThe etnaviv authors 		break;
76a8c21a54SThe etnaviv authors 
77a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_4:
78a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features3;
79a8c21a54SThe etnaviv authors 		break;
80a8c21a54SThe etnaviv authors 
81602eb489SRussell King 	case ETNAVIV_PARAM_GPU_FEATURES_5:
82602eb489SRussell King 		*value = gpu->identity.minor_features4;
83602eb489SRussell King 		break;
84602eb489SRussell King 
85602eb489SRussell King 	case ETNAVIV_PARAM_GPU_FEATURES_6:
86602eb489SRussell King 		*value = gpu->identity.minor_features5;
87602eb489SRussell King 		break;
88602eb489SRussell King 
89a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_STREAM_COUNT:
90a8c21a54SThe etnaviv authors 		*value = gpu->identity.stream_count;
91a8c21a54SThe etnaviv authors 		break;
92a8c21a54SThe etnaviv authors 
93a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_REGISTER_MAX:
94a8c21a54SThe etnaviv authors 		*value = gpu->identity.register_max;
95a8c21a54SThe etnaviv authors 		break;
96a8c21a54SThe etnaviv authors 
97a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_THREAD_COUNT:
98a8c21a54SThe etnaviv authors 		*value = gpu->identity.thread_count;
99a8c21a54SThe etnaviv authors 		break;
100a8c21a54SThe etnaviv authors 
101a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_VERTEX_CACHE_SIZE:
102a8c21a54SThe etnaviv authors 		*value = gpu->identity.vertex_cache_size;
103a8c21a54SThe etnaviv authors 		break;
104a8c21a54SThe etnaviv authors 
105a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_SHADER_CORE_COUNT:
106a8c21a54SThe etnaviv authors 		*value = gpu->identity.shader_core_count;
107a8c21a54SThe etnaviv authors 		break;
108a8c21a54SThe etnaviv authors 
109a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_PIXEL_PIPES:
110a8c21a54SThe etnaviv authors 		*value = gpu->identity.pixel_pipes;
111a8c21a54SThe etnaviv authors 		break;
112a8c21a54SThe etnaviv authors 
113a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_VERTEX_OUTPUT_BUFFER_SIZE:
114a8c21a54SThe etnaviv authors 		*value = gpu->identity.vertex_output_buffer_size;
115a8c21a54SThe etnaviv authors 		break;
116a8c21a54SThe etnaviv authors 
117a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_BUFFER_SIZE:
118a8c21a54SThe etnaviv authors 		*value = gpu->identity.buffer_size;
119a8c21a54SThe etnaviv authors 		break;
120a8c21a54SThe etnaviv authors 
121a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_INSTRUCTION_COUNT:
122a8c21a54SThe etnaviv authors 		*value = gpu->identity.instruction_count;
123a8c21a54SThe etnaviv authors 		break;
124a8c21a54SThe etnaviv authors 
125a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_NUM_CONSTANTS:
126a8c21a54SThe etnaviv authors 		*value = gpu->identity.num_constants;
127a8c21a54SThe etnaviv authors 		break;
128a8c21a54SThe etnaviv authors 
129602eb489SRussell King 	case ETNAVIV_PARAM_GPU_NUM_VARYINGS:
130602eb489SRussell King 		*value = gpu->identity.varyings_count;
131602eb489SRussell King 		break;
132602eb489SRussell King 
133a8c21a54SThe etnaviv authors 	default:
134a8c21a54SThe etnaviv authors 		DBG("%s: invalid param: %u", dev_name(gpu->dev), param);
135a8c21a54SThe etnaviv authors 		return -EINVAL;
136a8c21a54SThe etnaviv authors 	}
137a8c21a54SThe etnaviv authors 
138a8c21a54SThe etnaviv authors 	return 0;
139a8c21a54SThe etnaviv authors }
140a8c21a54SThe etnaviv authors 
141472f79dcSRussell King 
142472f79dcSRussell King #define etnaviv_is_model_rev(gpu, mod, rev) \
143472f79dcSRussell King 	((gpu)->identity.model == chipModel_##mod && \
144472f79dcSRussell King 	 (gpu)->identity.revision == rev)
14552f36ba1SRussell King #define etnaviv_field(val, field) \
14652f36ba1SRussell King 	(((val) & field##__MASK) >> field##__SHIFT)
14752f36ba1SRussell King 
148a8c21a54SThe etnaviv authors static void etnaviv_hw_specs(struct etnaviv_gpu *gpu)
149a8c21a54SThe etnaviv authors {
150a8c21a54SThe etnaviv authors 	if (gpu->identity.minor_features0 &
151a8c21a54SThe etnaviv authors 	    chipMinorFeatures0_MORE_MINOR_FEATURES) {
152602eb489SRussell King 		u32 specs[4];
153602eb489SRussell King 		unsigned int streams;
154a8c21a54SThe etnaviv authors 
155a8c21a54SThe etnaviv authors 		specs[0] = gpu_read(gpu, VIVS_HI_CHIP_SPECS);
156a8c21a54SThe etnaviv authors 		specs[1] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_2);
157602eb489SRussell King 		specs[2] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_3);
158602eb489SRussell King 		specs[3] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_4);
159a8c21a54SThe etnaviv authors 
16052f36ba1SRussell King 		gpu->identity.stream_count = etnaviv_field(specs[0],
16152f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_STREAM_COUNT);
16252f36ba1SRussell King 		gpu->identity.register_max = etnaviv_field(specs[0],
16352f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_REGISTER_MAX);
16452f36ba1SRussell King 		gpu->identity.thread_count = etnaviv_field(specs[0],
16552f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_THREAD_COUNT);
16652f36ba1SRussell King 		gpu->identity.vertex_cache_size = etnaviv_field(specs[0],
16752f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_VERTEX_CACHE_SIZE);
16852f36ba1SRussell King 		gpu->identity.shader_core_count = etnaviv_field(specs[0],
16952f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_SHADER_CORE_COUNT);
17052f36ba1SRussell King 		gpu->identity.pixel_pipes = etnaviv_field(specs[0],
17152f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_PIXEL_PIPES);
172a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size =
17352f36ba1SRussell King 			etnaviv_field(specs[0],
17452f36ba1SRussell King 				VIVS_HI_CHIP_SPECS_VERTEX_OUTPUT_BUFFER_SIZE);
175a8c21a54SThe etnaviv authors 
17652f36ba1SRussell King 		gpu->identity.buffer_size = etnaviv_field(specs[1],
17752f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_BUFFER_SIZE);
17852f36ba1SRussell King 		gpu->identity.instruction_count = etnaviv_field(specs[1],
17952f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_INSTRUCTION_COUNT);
18052f36ba1SRussell King 		gpu->identity.num_constants = etnaviv_field(specs[1],
18152f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_NUM_CONSTANTS);
182602eb489SRussell King 
183602eb489SRussell King 		gpu->identity.varyings_count = etnaviv_field(specs[2],
184602eb489SRussell King 					VIVS_HI_CHIP_SPECS_3_VARYINGS_COUNT);
185602eb489SRussell King 
186602eb489SRussell King 		/* This overrides the value from older register if non-zero */
187602eb489SRussell King 		streams = etnaviv_field(specs[3],
188602eb489SRussell King 					VIVS_HI_CHIP_SPECS_4_STREAM_COUNT);
189602eb489SRussell King 		if (streams)
190602eb489SRussell King 			gpu->identity.stream_count = streams;
191a8c21a54SThe etnaviv authors 	}
192a8c21a54SThe etnaviv authors 
193a8c21a54SThe etnaviv authors 	/* Fill in the stream count if not specified */
194a8c21a54SThe etnaviv authors 	if (gpu->identity.stream_count == 0) {
195a8c21a54SThe etnaviv authors 		if (gpu->identity.model >= 0x1000)
196a8c21a54SThe etnaviv authors 			gpu->identity.stream_count = 4;
197a8c21a54SThe etnaviv authors 		else
198a8c21a54SThe etnaviv authors 			gpu->identity.stream_count = 1;
199a8c21a54SThe etnaviv authors 	}
200a8c21a54SThe etnaviv authors 
201a8c21a54SThe etnaviv authors 	/* Convert the register max value */
202a8c21a54SThe etnaviv authors 	if (gpu->identity.register_max)
203a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 1 << gpu->identity.register_max;
204507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC400)
205a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 32;
206a8c21a54SThe etnaviv authors 	else
207a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 64;
208a8c21a54SThe etnaviv authors 
209a8c21a54SThe etnaviv authors 	/* Convert thread count */
210a8c21a54SThe etnaviv authors 	if (gpu->identity.thread_count)
211a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 1 << gpu->identity.thread_count;
212507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC400)
213a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 64;
214507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC500 ||
215507f8991SRussell King 		 gpu->identity.model == chipModel_GC530)
216a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 128;
217a8c21a54SThe etnaviv authors 	else
218a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 256;
219a8c21a54SThe etnaviv authors 
220a8c21a54SThe etnaviv authors 	if (gpu->identity.vertex_cache_size == 0)
221a8c21a54SThe etnaviv authors 		gpu->identity.vertex_cache_size = 8;
222a8c21a54SThe etnaviv authors 
223a8c21a54SThe etnaviv authors 	if (gpu->identity.shader_core_count == 0) {
224a8c21a54SThe etnaviv authors 		if (gpu->identity.model >= 0x1000)
225a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count = 2;
226a8c21a54SThe etnaviv authors 		else
227a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count = 1;
228a8c21a54SThe etnaviv authors 	}
229a8c21a54SThe etnaviv authors 
230a8c21a54SThe etnaviv authors 	if (gpu->identity.pixel_pipes == 0)
231a8c21a54SThe etnaviv authors 		gpu->identity.pixel_pipes = 1;
232a8c21a54SThe etnaviv authors 
233a8c21a54SThe etnaviv authors 	/* Convert virtex buffer size */
234a8c21a54SThe etnaviv authors 	if (gpu->identity.vertex_output_buffer_size) {
235a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size =
236a8c21a54SThe etnaviv authors 			1 << gpu->identity.vertex_output_buffer_size;
237507f8991SRussell King 	} else if (gpu->identity.model == chipModel_GC400) {
238a8c21a54SThe etnaviv authors 		if (gpu->identity.revision < 0x4000)
239a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 512;
240a8c21a54SThe etnaviv authors 		else if (gpu->identity.revision < 0x4200)
241a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 256;
242a8c21a54SThe etnaviv authors 		else
243a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 128;
244a8c21a54SThe etnaviv authors 	} else {
245a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size = 512;
246a8c21a54SThe etnaviv authors 	}
247a8c21a54SThe etnaviv authors 
248a8c21a54SThe etnaviv authors 	switch (gpu->identity.instruction_count) {
249a8c21a54SThe etnaviv authors 	case 0:
250472f79dcSRussell King 		if (etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
251507f8991SRussell King 		    gpu->identity.model == chipModel_GC880)
252a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count = 512;
253a8c21a54SThe etnaviv authors 		else
254a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count = 256;
255a8c21a54SThe etnaviv authors 		break;
256a8c21a54SThe etnaviv authors 
257a8c21a54SThe etnaviv authors 	case 1:
258a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 1024;
259a8c21a54SThe etnaviv authors 		break;
260a8c21a54SThe etnaviv authors 
261a8c21a54SThe etnaviv authors 	case 2:
262a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 2048;
263a8c21a54SThe etnaviv authors 		break;
264a8c21a54SThe etnaviv authors 
265a8c21a54SThe etnaviv authors 	default:
266a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 256;
267a8c21a54SThe etnaviv authors 		break;
268a8c21a54SThe etnaviv authors 	}
269a8c21a54SThe etnaviv authors 
270a8c21a54SThe etnaviv authors 	if (gpu->identity.num_constants == 0)
271a8c21a54SThe etnaviv authors 		gpu->identity.num_constants = 168;
272602eb489SRussell King 
273602eb489SRussell King 	if (gpu->identity.varyings_count == 0) {
274602eb489SRussell King 		if (gpu->identity.minor_features1 & chipMinorFeatures1_HALTI0)
275602eb489SRussell King 			gpu->identity.varyings_count = 12;
276602eb489SRussell King 		else
277602eb489SRussell King 			gpu->identity.varyings_count = 8;
278602eb489SRussell King 	}
279602eb489SRussell King 
280602eb489SRussell King 	/*
281602eb489SRussell King 	 * For some cores, two varyings are consumed for position, so the
282602eb489SRussell King 	 * maximum varying count needs to be reduced by one.
283602eb489SRussell King 	 */
284602eb489SRussell King 	if (etnaviv_is_model_rev(gpu, GC5000, 0x5434) ||
285602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
286602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5245) ||
287602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
288602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC3000, 0x5435) ||
289602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2200, 0x5244) ||
290602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2100, 0x5108) ||
291602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
292602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC1500, 0x5246) ||
293602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC880, 0x5107) ||
294602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC880, 0x5106))
295602eb489SRussell King 		gpu->identity.varyings_count -= 1;
296a8c21a54SThe etnaviv authors }
297a8c21a54SThe etnaviv authors 
298a8c21a54SThe etnaviv authors static void etnaviv_hw_identify(struct etnaviv_gpu *gpu)
299a8c21a54SThe etnaviv authors {
300a8c21a54SThe etnaviv authors 	u32 chipIdentity;
301a8c21a54SThe etnaviv authors 
302a8c21a54SThe etnaviv authors 	chipIdentity = gpu_read(gpu, VIVS_HI_CHIP_IDENTITY);
303a8c21a54SThe etnaviv authors 
304a8c21a54SThe etnaviv authors 	/* Special case for older graphic cores. */
30552f36ba1SRussell King 	if (etnaviv_field(chipIdentity, VIVS_HI_CHIP_IDENTITY_FAMILY) == 0x01) {
306507f8991SRussell King 		gpu->identity.model    = chipModel_GC500;
30752f36ba1SRussell King 		gpu->identity.revision = etnaviv_field(chipIdentity,
30852f36ba1SRussell King 					 VIVS_HI_CHIP_IDENTITY_REVISION);
309a8c21a54SThe etnaviv authors 	} else {
310a8c21a54SThe etnaviv authors 
311a8c21a54SThe etnaviv authors 		gpu->identity.model = gpu_read(gpu, VIVS_HI_CHIP_MODEL);
312a8c21a54SThe etnaviv authors 		gpu->identity.revision = gpu_read(gpu, VIVS_HI_CHIP_REV);
313a8c21a54SThe etnaviv authors 
314a8c21a54SThe etnaviv authors 		/*
315a8c21a54SThe etnaviv authors 		 * !!!! HACK ALERT !!!!
316a8c21a54SThe etnaviv authors 		 * Because people change device IDs without letting software
317a8c21a54SThe etnaviv authors 		 * know about it - here is the hack to make it all look the
318a8c21a54SThe etnaviv authors 		 * same.  Only for GC400 family.
319a8c21a54SThe etnaviv authors 		 */
320a8c21a54SThe etnaviv authors 		if ((gpu->identity.model & 0xff00) == 0x0400 &&
321507f8991SRussell King 		    gpu->identity.model != chipModel_GC420) {
322a8c21a54SThe etnaviv authors 			gpu->identity.model = gpu->identity.model & 0x0400;
323a8c21a54SThe etnaviv authors 		}
324a8c21a54SThe etnaviv authors 
325a8c21a54SThe etnaviv authors 		/* Another special case */
326472f79dcSRussell King 		if (etnaviv_is_model_rev(gpu, GC300, 0x2201)) {
327a8c21a54SThe etnaviv authors 			u32 chipDate = gpu_read(gpu, VIVS_HI_CHIP_DATE);
328a8c21a54SThe etnaviv authors 			u32 chipTime = gpu_read(gpu, VIVS_HI_CHIP_TIME);
329a8c21a54SThe etnaviv authors 
330a8c21a54SThe etnaviv authors 			if (chipDate == 0x20080814 && chipTime == 0x12051100) {
331a8c21a54SThe etnaviv authors 				/*
332a8c21a54SThe etnaviv authors 				 * This IP has an ECO; put the correct
333a8c21a54SThe etnaviv authors 				 * revision in it.
334a8c21a54SThe etnaviv authors 				 */
335a8c21a54SThe etnaviv authors 				gpu->identity.revision = 0x1051;
336a8c21a54SThe etnaviv authors 			}
337a8c21a54SThe etnaviv authors 		}
33812ff4bdeSLucas Stach 
33912ff4bdeSLucas Stach 		/*
34012ff4bdeSLucas Stach 		 * NXP likes to call the GPU on the i.MX6QP GC2000+, but in
34112ff4bdeSLucas Stach 		 * reality it's just a re-branded GC3000. We can identify this
34212ff4bdeSLucas Stach 		 * core by the upper half of the revision register being all 1.
34312ff4bdeSLucas Stach 		 * Fix model/rev here, so all other places can refer to this
34412ff4bdeSLucas Stach 		 * core by its real identity.
34512ff4bdeSLucas Stach 		 */
34612ff4bdeSLucas Stach 		if (etnaviv_is_model_rev(gpu, GC2000, 0xffff5450)) {
34712ff4bdeSLucas Stach 			gpu->identity.model = chipModel_GC3000;
34812ff4bdeSLucas Stach 			gpu->identity.revision &= 0xffff;
34912ff4bdeSLucas Stach 		}
350a8c21a54SThe etnaviv authors 	}
351a8c21a54SThe etnaviv authors 
352a8c21a54SThe etnaviv authors 	dev_info(gpu->dev, "model: GC%x, revision: %x\n",
353a8c21a54SThe etnaviv authors 		 gpu->identity.model, gpu->identity.revision);
354a8c21a54SThe etnaviv authors 
355a8c21a54SThe etnaviv authors 	gpu->identity.features = gpu_read(gpu, VIVS_HI_CHIP_FEATURE);
356a8c21a54SThe etnaviv authors 
357a8c21a54SThe etnaviv authors 	/* Disable fast clear on GC700. */
358507f8991SRussell King 	if (gpu->identity.model == chipModel_GC700)
359a8c21a54SThe etnaviv authors 		gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
360a8c21a54SThe etnaviv authors 
361507f8991SRussell King 	if ((gpu->identity.model == chipModel_GC500 &&
362507f8991SRussell King 	     gpu->identity.revision < 2) ||
363507f8991SRussell King 	    (gpu->identity.model == chipModel_GC300 &&
364507f8991SRussell King 	     gpu->identity.revision < 0x2000)) {
365a8c21a54SThe etnaviv authors 
366a8c21a54SThe etnaviv authors 		/*
367a8c21a54SThe etnaviv authors 		 * GC500 rev 1.x and GC300 rev < 2.0 doesn't have these
368a8c21a54SThe etnaviv authors 		 * registers.
369a8c21a54SThe etnaviv authors 		 */
370a8c21a54SThe etnaviv authors 		gpu->identity.minor_features0 = 0;
371a8c21a54SThe etnaviv authors 		gpu->identity.minor_features1 = 0;
372a8c21a54SThe etnaviv authors 		gpu->identity.minor_features2 = 0;
373a8c21a54SThe etnaviv authors 		gpu->identity.minor_features3 = 0;
374602eb489SRussell King 		gpu->identity.minor_features4 = 0;
375602eb489SRussell King 		gpu->identity.minor_features5 = 0;
376a8c21a54SThe etnaviv authors 	} else
377a8c21a54SThe etnaviv authors 		gpu->identity.minor_features0 =
378a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_0);
379a8c21a54SThe etnaviv authors 
380a8c21a54SThe etnaviv authors 	if (gpu->identity.minor_features0 &
381a8c21a54SThe etnaviv authors 	    chipMinorFeatures0_MORE_MINOR_FEATURES) {
382a8c21a54SThe etnaviv authors 		gpu->identity.minor_features1 =
383a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_1);
384a8c21a54SThe etnaviv authors 		gpu->identity.minor_features2 =
385a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_2);
386a8c21a54SThe etnaviv authors 		gpu->identity.minor_features3 =
387a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_3);
388602eb489SRussell King 		gpu->identity.minor_features4 =
389602eb489SRussell King 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_4);
390602eb489SRussell King 		gpu->identity.minor_features5 =
391602eb489SRussell King 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_5);
392a8c21a54SThe etnaviv authors 	}
393a8c21a54SThe etnaviv authors 
394a8c21a54SThe etnaviv authors 	/* GC600 idle register reports zero bits where modules aren't present */
395a8c21a54SThe etnaviv authors 	if (gpu->identity.model == chipModel_GC600) {
396a8c21a54SThe etnaviv authors 		gpu->idle_mask = VIVS_HI_IDLE_STATE_TX |
397a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_RA |
398a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_SE |
399a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_PA |
400a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_SH |
401a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_PE |
402a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_DE |
403a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_FE;
404a8c21a54SThe etnaviv authors 	} else {
405a8c21a54SThe etnaviv authors 		gpu->idle_mask = ~VIVS_HI_IDLE_STATE_AXI_LP;
406a8c21a54SThe etnaviv authors 	}
407a8c21a54SThe etnaviv authors 
408a8c21a54SThe etnaviv authors 	etnaviv_hw_specs(gpu);
409a8c21a54SThe etnaviv authors }
410a8c21a54SThe etnaviv authors 
411a8c21a54SThe etnaviv authors static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
412a8c21a54SThe etnaviv authors {
413a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock |
414a8c21a54SThe etnaviv authors 		  VIVS_HI_CLOCK_CONTROL_FSCALE_CMD_LOAD);
415a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
416a8c21a54SThe etnaviv authors }
417a8c21a54SThe etnaviv authors 
418bcdfb5e5SRussell King static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu)
419bcdfb5e5SRussell King {
420d79fd1ccSLucas Stach 	if (gpu->identity.minor_features2 &
421d79fd1ccSLucas Stach 	    chipMinorFeatures2_DYNAMIC_FREQUENCY_SCALING) {
422d79fd1ccSLucas Stach 		clk_set_rate(gpu->clk_core,
423d79fd1ccSLucas Stach 			     gpu->base_rate_core >> gpu->freq_scale);
424d79fd1ccSLucas Stach 		clk_set_rate(gpu->clk_shader,
425d79fd1ccSLucas Stach 			     gpu->base_rate_shader >> gpu->freq_scale);
426d79fd1ccSLucas Stach 	} else {
427bcdfb5e5SRussell King 		unsigned int fscale = 1 << (6 - gpu->freq_scale);
4286eb3ecc3SLucas Stach 		u32 clock = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
429bcdfb5e5SRussell King 
4306eb3ecc3SLucas Stach 		clock &= ~VIVS_HI_CLOCK_CONTROL_FSCALE_VAL__MASK;
4316eb3ecc3SLucas Stach 		clock |= VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
432bcdfb5e5SRussell King 		etnaviv_gpu_load_clock(gpu, clock);
433bcdfb5e5SRussell King 	}
434d79fd1ccSLucas Stach }
435bcdfb5e5SRussell King 
436a8c21a54SThe etnaviv authors static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
437a8c21a54SThe etnaviv authors {
438a8c21a54SThe etnaviv authors 	u32 control, idle;
439a8c21a54SThe etnaviv authors 	unsigned long timeout;
440a8c21a54SThe etnaviv authors 	bool failed = true;
441a8c21a54SThe etnaviv authors 
442a8c21a54SThe etnaviv authors 	/* We hope that the GPU resets in under one second */
443a8c21a54SThe etnaviv authors 	timeout = jiffies + msecs_to_jiffies(1000);
444a8c21a54SThe etnaviv authors 
445a8c21a54SThe etnaviv authors 	while (time_is_after_jiffies(timeout)) {
446a8c21a54SThe etnaviv authors 		/* enable clock */
4476eb3ecc3SLucas Stach 		unsigned int fscale = 1 << (6 - gpu->freq_scale);
4486eb3ecc3SLucas Stach 		control = VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
4496eb3ecc3SLucas Stach 		etnaviv_gpu_load_clock(gpu, control);
450a8c21a54SThe etnaviv authors 
451a8c21a54SThe etnaviv authors 		/* isolate the GPU. */
452a8c21a54SThe etnaviv authors 		control |= VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
453a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
454a8c21a54SThe etnaviv authors 
455a8c21a54SThe etnaviv authors 		/* set soft reset. */
456a8c21a54SThe etnaviv authors 		control |= VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
457a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
458a8c21a54SThe etnaviv authors 
459a8c21a54SThe etnaviv authors 		/* wait for reset. */
46040462179SPhilipp Zabel 		usleep_range(10, 20);
461a8c21a54SThe etnaviv authors 
462a8c21a54SThe etnaviv authors 		/* reset soft reset bit. */
463a8c21a54SThe etnaviv authors 		control &= ~VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
464a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
465a8c21a54SThe etnaviv authors 
466a8c21a54SThe etnaviv authors 		/* reset GPU isolation. */
467a8c21a54SThe etnaviv authors 		control &= ~VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
468a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
469a8c21a54SThe etnaviv authors 
470a8c21a54SThe etnaviv authors 		/* read idle register. */
471a8c21a54SThe etnaviv authors 		idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
472a8c21a54SThe etnaviv authors 
473a8c21a54SThe etnaviv authors 		/* try reseting again if FE it not idle */
474a8c21a54SThe etnaviv authors 		if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) {
475a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "FE is not idle\n");
476a8c21a54SThe etnaviv authors 			continue;
477a8c21a54SThe etnaviv authors 		}
478a8c21a54SThe etnaviv authors 
479a8c21a54SThe etnaviv authors 		/* read reset register. */
480a8c21a54SThe etnaviv authors 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
481a8c21a54SThe etnaviv authors 
482a8c21a54SThe etnaviv authors 		/* is the GPU idle? */
483a8c21a54SThe etnaviv authors 		if (((control & VIVS_HI_CLOCK_CONTROL_IDLE_3D) == 0) ||
484a8c21a54SThe etnaviv authors 		    ((control & VIVS_HI_CLOCK_CONTROL_IDLE_2D) == 0)) {
485a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "GPU is not idle\n");
486a8c21a54SThe etnaviv authors 			continue;
487a8c21a54SThe etnaviv authors 		}
488a8c21a54SThe etnaviv authors 
4896eb3ecc3SLucas Stach 		/* disable debug registers, as they are not normally needed */
4906eb3ecc3SLucas Stach 		control |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
4916eb3ecc3SLucas Stach 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
4926eb3ecc3SLucas Stach 
493a8c21a54SThe etnaviv authors 		failed = false;
494a8c21a54SThe etnaviv authors 		break;
495a8c21a54SThe etnaviv authors 	}
496a8c21a54SThe etnaviv authors 
497a8c21a54SThe etnaviv authors 	if (failed) {
498a8c21a54SThe etnaviv authors 		idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
499a8c21a54SThe etnaviv authors 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
500a8c21a54SThe etnaviv authors 
501a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "GPU failed to reset: FE %sidle, 3D %sidle, 2D %sidle\n",
502a8c21a54SThe etnaviv authors 			idle & VIVS_HI_IDLE_STATE_FE ? "" : "not ",
503a8c21a54SThe etnaviv authors 			control & VIVS_HI_CLOCK_CONTROL_IDLE_3D ? "" : "not ",
504a8c21a54SThe etnaviv authors 			control & VIVS_HI_CLOCK_CONTROL_IDLE_2D ? "" : "not ");
505a8c21a54SThe etnaviv authors 
506a8c21a54SThe etnaviv authors 		return -EBUSY;
507a8c21a54SThe etnaviv authors 	}
508a8c21a54SThe etnaviv authors 
509a8c21a54SThe etnaviv authors 	/* We rely on the GPU running, so program the clock */
510bcdfb5e5SRussell King 	etnaviv_gpu_update_clock(gpu);
511a8c21a54SThe etnaviv authors 
512a8c21a54SThe etnaviv authors 	return 0;
513a8c21a54SThe etnaviv authors }
514a8c21a54SThe etnaviv authors 
5157d0c6e71SRussell King static void etnaviv_gpu_enable_mlcg(struct etnaviv_gpu *gpu)
5167d0c6e71SRussell King {
5177d0c6e71SRussell King 	u32 pmc, ppc;
5187d0c6e71SRussell King 
5197d0c6e71SRussell King 	/* enable clock gating */
5207d0c6e71SRussell King 	ppc = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
5217d0c6e71SRussell King 	ppc |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
5227d0c6e71SRussell King 
5237d0c6e71SRussell King 	/* Disable stall module clock gating for 4.3.0.1 and 4.3.0.2 revs */
5247d0c6e71SRussell King 	if (gpu->identity.revision == 0x4301 ||
5257d0c6e71SRussell King 	    gpu->identity.revision == 0x4302)
5267d0c6e71SRussell King 		ppc |= VIVS_PM_POWER_CONTROLS_DISABLE_STALL_MODULE_CLOCK_GATING;
5277d0c6e71SRussell King 
5287d0c6e71SRussell King 	gpu_write(gpu, VIVS_PM_POWER_CONTROLS, ppc);
5297d0c6e71SRussell King 
5307d0c6e71SRussell King 	pmc = gpu_read(gpu, VIVS_PM_MODULE_CONTROLS);
5317d0c6e71SRussell King 
5327cef6004SLucas Stach 	/* Disable PA clock gating for GC400+ without bugfix except for GC420 */
5337d0c6e71SRussell King 	if (gpu->identity.model >= chipModel_GC400 &&
5347cef6004SLucas Stach 	    gpu->identity.model != chipModel_GC420 &&
5357cef6004SLucas Stach 	    !(gpu->identity.minor_features3 & chipMinorFeatures3_BUG_FIXES12))
5367d0c6e71SRussell King 		pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PA;
5377d0c6e71SRussell King 
5387d0c6e71SRussell King 	/*
5397d0c6e71SRussell King 	 * Disable PE clock gating on revs < 5.0.0.0 when HZ is
5407d0c6e71SRussell King 	 * present without a bug fix.
5417d0c6e71SRussell King 	 */
5427d0c6e71SRussell King 	if (gpu->identity.revision < 0x5000 &&
5437d0c6e71SRussell King 	    gpu->identity.minor_features0 & chipMinorFeatures0_HZ &&
5447d0c6e71SRussell King 	    !(gpu->identity.minor_features1 &
5457d0c6e71SRussell King 	      chipMinorFeatures1_DISABLE_PE_GATING))
5467d0c6e71SRussell King 		pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PE;
5477d0c6e71SRussell King 
5487d0c6e71SRussell King 	if (gpu->identity.revision < 0x5422)
5497d0c6e71SRussell King 		pmc |= BIT(15); /* Unknown bit */
5507d0c6e71SRussell King 
5517cef6004SLucas Stach 	/* Disable TX clock gating on affected core revisions. */
5527cef6004SLucas Stach 	if (etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
5537cef6004SLucas Stach 	    etnaviv_is_model_rev(gpu, GC2000, 0x5108))
5547cef6004SLucas Stach 		pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_TX;
5557cef6004SLucas Stach 
5567d0c6e71SRussell King 	pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_HZ;
5577d0c6e71SRussell King 	pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_EZ;
5587d0c6e71SRussell King 
5597d0c6e71SRussell King 	gpu_write(gpu, VIVS_PM_MODULE_CONTROLS, pmc);
5607d0c6e71SRussell King }
5617d0c6e71SRussell King 
562229855b6SLucas Stach void etnaviv_gpu_start_fe(struct etnaviv_gpu *gpu, u32 address, u16 prefetch)
563229855b6SLucas Stach {
564229855b6SLucas Stach 	gpu_write(gpu, VIVS_FE_COMMAND_ADDRESS, address);
565229855b6SLucas Stach 	gpu_write(gpu, VIVS_FE_COMMAND_CONTROL,
566229855b6SLucas Stach 		  VIVS_FE_COMMAND_CONTROL_ENABLE |
567229855b6SLucas Stach 		  VIVS_FE_COMMAND_CONTROL_PREFETCH(prefetch));
568229855b6SLucas Stach }
569229855b6SLucas Stach 
570e17a0dedSWladimir J. van der Laan static void etnaviv_gpu_setup_pulse_eater(struct etnaviv_gpu *gpu)
571e17a0dedSWladimir J. van der Laan {
572e17a0dedSWladimir J. van der Laan 	/*
573e17a0dedSWladimir J. van der Laan 	 * Base value for VIVS_PM_PULSE_EATER register on models where it
574e17a0dedSWladimir J. van der Laan 	 * cannot be read, extracted from vivante kernel driver.
575e17a0dedSWladimir J. van der Laan 	 */
576e17a0dedSWladimir J. van der Laan 	u32 pulse_eater = 0x01590880;
577e17a0dedSWladimir J. van der Laan 
578e17a0dedSWladimir J. van der Laan 	if (etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
579e17a0dedSWladimir J. van der Laan 	    etnaviv_is_model_rev(gpu, GC4000, 0x5222)) {
580e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(23);
581e17a0dedSWladimir J. van der Laan 
582e17a0dedSWladimir J. van der Laan 	}
583e17a0dedSWladimir J. van der Laan 
584e17a0dedSWladimir J. van der Laan 	if (etnaviv_is_model_rev(gpu, GC1000, 0x5039) ||
585e17a0dedSWladimir J. van der Laan 	    etnaviv_is_model_rev(gpu, GC1000, 0x5040)) {
586e17a0dedSWladimir J. van der Laan 		pulse_eater &= ~BIT(16);
587e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(17);
588e17a0dedSWladimir J. van der Laan 	}
589e17a0dedSWladimir J. van der Laan 
590e17a0dedSWladimir J. van der Laan 	if ((gpu->identity.revision > 0x5420) &&
591e17a0dedSWladimir J. van der Laan 	    (gpu->identity.features & chipFeatures_PIPE_3D))
592e17a0dedSWladimir J. van der Laan 	{
593e17a0dedSWladimir J. van der Laan 		/* Performance fix: disable internal DFS */
594e17a0dedSWladimir J. van der Laan 		pulse_eater = gpu_read(gpu, VIVS_PM_PULSE_EATER);
595e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(18);
596e17a0dedSWladimir J. van der Laan 	}
597e17a0dedSWladimir J. van der Laan 
598e17a0dedSWladimir J. van der Laan 	gpu_write(gpu, VIVS_PM_PULSE_EATER, pulse_eater);
599e17a0dedSWladimir J. van der Laan }
600e17a0dedSWladimir J. van der Laan 
601a8c21a54SThe etnaviv authors static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu)
602a8c21a54SThe etnaviv authors {
603a8c21a54SThe etnaviv authors 	u16 prefetch;
604a8c21a54SThe etnaviv authors 
605472f79dcSRussell King 	if ((etnaviv_is_model_rev(gpu, GC320, 0x5007) ||
606472f79dcSRussell King 	     etnaviv_is_model_rev(gpu, GC320, 0x5220)) &&
607472f79dcSRussell King 	    gpu_read(gpu, VIVS_HI_CHIP_TIME) != 0x2062400) {
608a8c21a54SThe etnaviv authors 		u32 mc_memory_debug;
609a8c21a54SThe etnaviv authors 
610a8c21a54SThe etnaviv authors 		mc_memory_debug = gpu_read(gpu, VIVS_MC_DEBUG_MEMORY) & ~0xff;
611a8c21a54SThe etnaviv authors 
612a8c21a54SThe etnaviv authors 		if (gpu->identity.revision == 0x5007)
613a8c21a54SThe etnaviv authors 			mc_memory_debug |= 0x0c;
614a8c21a54SThe etnaviv authors 		else
615a8c21a54SThe etnaviv authors 			mc_memory_debug |= 0x08;
616a8c21a54SThe etnaviv authors 
617a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_MC_DEBUG_MEMORY, mc_memory_debug);
618a8c21a54SThe etnaviv authors 	}
619a8c21a54SThe etnaviv authors 
6207d0c6e71SRussell King 	/* enable module-level clock gating */
6217d0c6e71SRussell King 	etnaviv_gpu_enable_mlcg(gpu);
6227d0c6e71SRussell King 
623a8c21a54SThe etnaviv authors 	/*
624a8c21a54SThe etnaviv authors 	 * Update GPU AXI cache atttribute to "cacheable, no allocate".
625a8c21a54SThe etnaviv authors 	 * This is necessary to prevent the iMX6 SoC locking up.
626a8c21a54SThe etnaviv authors 	 */
627a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_AXI_CONFIG,
628a8c21a54SThe etnaviv authors 		  VIVS_HI_AXI_CONFIG_AWCACHE(2) |
629a8c21a54SThe etnaviv authors 		  VIVS_HI_AXI_CONFIG_ARCACHE(2));
630a8c21a54SThe etnaviv authors 
631a8c21a54SThe etnaviv authors 	/* GC2000 rev 5108 needs a special bus config */
632472f79dcSRussell King 	if (etnaviv_is_model_rev(gpu, GC2000, 0x5108)) {
633a8c21a54SThe etnaviv authors 		u32 bus_config = gpu_read(gpu, VIVS_MC_BUS_CONFIG);
634a8c21a54SThe etnaviv authors 		bus_config &= ~(VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG__MASK |
635a8c21a54SThe etnaviv authors 				VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG__MASK);
636a8c21a54SThe etnaviv authors 		bus_config |= VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG(1) |
637a8c21a54SThe etnaviv authors 			      VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG(0);
638a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_MC_BUS_CONFIG, bus_config);
639a8c21a54SThe etnaviv authors 	}
640a8c21a54SThe etnaviv authors 
641e17a0dedSWladimir J. van der Laan 	/* setup the pulse eater */
642e17a0dedSWladimir J. van der Laan 	etnaviv_gpu_setup_pulse_eater(gpu);
643e17a0dedSWladimir J. van der Laan 
64499f861bcSLucas Stach 	/* setup the MMU */
645e095c8feSLucas Stach 	etnaviv_iommu_restore(gpu);
646a8c21a54SThe etnaviv authors 
647a8c21a54SThe etnaviv authors 	/* Start command processor */
648a8c21a54SThe etnaviv authors 	prefetch = etnaviv_buffer_init(gpu);
649a8c21a54SThe etnaviv authors 
650a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_INTR_ENBL, ~0U);
6512f9225dbSLucas Stach 	etnaviv_gpu_start_fe(gpu, etnaviv_cmdbuf_get_va(&gpu->buffer),
652229855b6SLucas Stach 			     prefetch);
653a8c21a54SThe etnaviv authors }
654a8c21a54SThe etnaviv authors 
655a8c21a54SThe etnaviv authors int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
656a8c21a54SThe etnaviv authors {
657a8c21a54SThe etnaviv authors 	int ret, i;
658a8c21a54SThe etnaviv authors 
659a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
6601409df04SLucas Stach 	if (ret < 0) {
6611409df04SLucas Stach 		dev_err(gpu->dev, "Failed to enable GPU power domain\n");
662a8c21a54SThe etnaviv authors 		return ret;
6631409df04SLucas Stach 	}
664a8c21a54SThe etnaviv authors 
665a8c21a54SThe etnaviv authors 	etnaviv_hw_identify(gpu);
666a8c21a54SThe etnaviv authors 
667a8c21a54SThe etnaviv authors 	if (gpu->identity.model == 0) {
668a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "Unknown GPU model\n");
669f6427760SRussell King 		ret = -ENXIO;
670f6427760SRussell King 		goto fail;
671a8c21a54SThe etnaviv authors 	}
672a8c21a54SThe etnaviv authors 
673b98c6688SRussell King 	/* Exclude VG cores with FE2.0 */
674b98c6688SRussell King 	if (gpu->identity.features & chipFeatures_PIPE_VG &&
675b98c6688SRussell King 	    gpu->identity.features & chipFeatures_FE20) {
676b98c6688SRussell King 		dev_info(gpu->dev, "Ignoring GPU with VG and FE2.0\n");
677b98c6688SRussell King 		ret = -ENXIO;
678b98c6688SRussell King 		goto fail;
679b98c6688SRussell King 	}
680b98c6688SRussell King 
6812144fff7SLucas Stach 	/*
6822144fff7SLucas Stach 	 * Set the GPU linear window to be at the end of the DMA window, where
6832144fff7SLucas Stach 	 * the CMA area is likely to reside. This ensures that we are able to
6842144fff7SLucas Stach 	 * map the command buffers while having the linear window overlap as
6852144fff7SLucas Stach 	 * much RAM as possible, so we can optimize mappings for other buffers.
6862144fff7SLucas Stach 	 *
6872144fff7SLucas Stach 	 * For 3D cores only do this if MC2.0 is present, as with MC1.0 it leads
6882144fff7SLucas Stach 	 * to different views of the memory on the individual engines.
6892144fff7SLucas Stach 	 */
6902144fff7SLucas Stach 	if (!(gpu->identity.features & chipFeatures_PIPE_3D) ||
6912144fff7SLucas Stach 	    (gpu->identity.minor_features0 & chipMinorFeatures0_MC20)) {
6922144fff7SLucas Stach 		u32 dma_mask = (u32)dma_get_required_mask(gpu->dev);
6932144fff7SLucas Stach 		if (dma_mask < PHYS_OFFSET + SZ_2G)
6942144fff7SLucas Stach 			gpu->memory_base = PHYS_OFFSET;
6952144fff7SLucas Stach 		else
6962144fff7SLucas Stach 			gpu->memory_base = dma_mask - SZ_2G + 1;
6971db01279SLucas Stach 	} else if (PHYS_OFFSET >= SZ_2G) {
6981db01279SLucas Stach 		dev_info(gpu->dev, "Need to move linear window on MC1.0, disabling TS\n");
6991db01279SLucas Stach 		gpu->memory_base = PHYS_OFFSET;
7001db01279SLucas Stach 		gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
7012144fff7SLucas Stach 	}
7022144fff7SLucas Stach 
703a8c21a54SThe etnaviv authors 	ret = etnaviv_hw_reset(gpu);
7041409df04SLucas Stach 	if (ret) {
7051409df04SLucas Stach 		dev_err(gpu->dev, "GPU reset failed\n");
706a8c21a54SThe etnaviv authors 		goto fail;
7071409df04SLucas Stach 	}
708a8c21a54SThe etnaviv authors 
709dd34bb96SLucas Stach 	gpu->mmu = etnaviv_iommu_new(gpu);
710dd34bb96SLucas Stach 	if (IS_ERR(gpu->mmu)) {
7111409df04SLucas Stach 		dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n");
712dd34bb96SLucas Stach 		ret = PTR_ERR(gpu->mmu);
713a8c21a54SThe etnaviv authors 		goto fail;
714a8c21a54SThe etnaviv authors 	}
715a8c21a54SThe etnaviv authors 
716e66774ddSLucas Stach 	gpu->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(gpu);
717e66774ddSLucas Stach 	if (IS_ERR(gpu->cmdbuf_suballoc)) {
718e66774ddSLucas Stach 		dev_err(gpu->dev, "Failed to create cmdbuf suballocator\n");
719e66774ddSLucas Stach 		ret = PTR_ERR(gpu->cmdbuf_suballoc);
720e66774ddSLucas Stach 		goto fail;
721e66774ddSLucas Stach 	}
722e66774ddSLucas Stach 
723a8c21a54SThe etnaviv authors 	/* Create buffer: */
7242f9225dbSLucas Stach 	ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &gpu->buffer,
7252f9225dbSLucas Stach 				  PAGE_SIZE);
7262f9225dbSLucas Stach 	if (ret) {
727a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "could not create command buffer\n");
72845d16a6dSLucas Stach 		goto destroy_iommu;
729a8c21a54SThe etnaviv authors 	}
730acfee0ecSLucas Stach 
731acfee0ecSLucas Stach 	if (gpu->mmu->version == ETNAVIV_IOMMU_V1 &&
7322f9225dbSLucas Stach 	    etnaviv_cmdbuf_get_va(&gpu->buffer) > 0x80000000) {
733a8c21a54SThe etnaviv authors 		ret = -EINVAL;
734a8c21a54SThe etnaviv authors 		dev_err(gpu->dev,
735a8c21a54SThe etnaviv authors 			"command buffer outside valid memory window\n");
736a8c21a54SThe etnaviv authors 		goto free_buffer;
737a8c21a54SThe etnaviv authors 	}
738a8c21a54SThe etnaviv authors 
739a8c21a54SThe etnaviv authors 	/* Setup event management */
740a8c21a54SThe etnaviv authors 	spin_lock_init(&gpu->event_spinlock);
741a8c21a54SThe etnaviv authors 	init_completion(&gpu->event_free);
742355502e0SChristian Gmeiner 	bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS);
743355502e0SChristian Gmeiner 	for (i = 0; i < ARRAY_SIZE(gpu->event); i++)
744a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
745a8c21a54SThe etnaviv authors 
746a8c21a54SThe etnaviv authors 	/* Now program the hardware */
747a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
748a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
749f6086311SRussell King 	gpu->exec_state = -1;
750a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
751a8c21a54SThe etnaviv authors 
752a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
753a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
754a8c21a54SThe etnaviv authors 
755a8c21a54SThe etnaviv authors 	return 0;
756a8c21a54SThe etnaviv authors 
757a8c21a54SThe etnaviv authors free_buffer:
7582f9225dbSLucas Stach 	etnaviv_cmdbuf_free(&gpu->buffer);
75945d16a6dSLucas Stach destroy_iommu:
76045d16a6dSLucas Stach 	etnaviv_iommu_destroy(gpu->mmu);
76145d16a6dSLucas Stach 	gpu->mmu = NULL;
762a8c21a54SThe etnaviv authors fail:
763a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
764a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
765a8c21a54SThe etnaviv authors 
766a8c21a54SThe etnaviv authors 	return ret;
767a8c21a54SThe etnaviv authors }
768a8c21a54SThe etnaviv authors 
769a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
770a8c21a54SThe etnaviv authors struct dma_debug {
771a8c21a54SThe etnaviv authors 	u32 address[2];
772a8c21a54SThe etnaviv authors 	u32 state[2];
773a8c21a54SThe etnaviv authors };
774a8c21a54SThe etnaviv authors 
775a8c21a54SThe etnaviv authors static void verify_dma(struct etnaviv_gpu *gpu, struct dma_debug *debug)
776a8c21a54SThe etnaviv authors {
777a8c21a54SThe etnaviv authors 	u32 i;
778a8c21a54SThe etnaviv authors 
779a8c21a54SThe etnaviv authors 	debug->address[0] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
780a8c21a54SThe etnaviv authors 	debug->state[0]   = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
781a8c21a54SThe etnaviv authors 
782a8c21a54SThe etnaviv authors 	for (i = 0; i < 500; i++) {
783a8c21a54SThe etnaviv authors 		debug->address[1] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
784a8c21a54SThe etnaviv authors 		debug->state[1]   = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
785a8c21a54SThe etnaviv authors 
786a8c21a54SThe etnaviv authors 		if (debug->address[0] != debug->address[1])
787a8c21a54SThe etnaviv authors 			break;
788a8c21a54SThe etnaviv authors 
789a8c21a54SThe etnaviv authors 		if (debug->state[0] != debug->state[1])
790a8c21a54SThe etnaviv authors 			break;
791a8c21a54SThe etnaviv authors 	}
792a8c21a54SThe etnaviv authors }
793a8c21a54SThe etnaviv authors 
794a8c21a54SThe etnaviv authors int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m)
795a8c21a54SThe etnaviv authors {
796a8c21a54SThe etnaviv authors 	struct dma_debug debug;
797a8c21a54SThe etnaviv authors 	u32 dma_lo, dma_hi, axi, idle;
798a8c21a54SThe etnaviv authors 	int ret;
799a8c21a54SThe etnaviv authors 
800a8c21a54SThe etnaviv authors 	seq_printf(m, "%s Status:\n", dev_name(gpu->dev));
801a8c21a54SThe etnaviv authors 
802a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
803a8c21a54SThe etnaviv authors 	if (ret < 0)
804a8c21a54SThe etnaviv authors 		return ret;
805a8c21a54SThe etnaviv authors 
806a8c21a54SThe etnaviv authors 	dma_lo = gpu_read(gpu, VIVS_FE_DMA_LOW);
807a8c21a54SThe etnaviv authors 	dma_hi = gpu_read(gpu, VIVS_FE_DMA_HIGH);
808a8c21a54SThe etnaviv authors 	axi = gpu_read(gpu, VIVS_HI_AXI_STATUS);
809a8c21a54SThe etnaviv authors 	idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
810a8c21a54SThe etnaviv authors 
811a8c21a54SThe etnaviv authors 	verify_dma(gpu, &debug);
812a8c21a54SThe etnaviv authors 
813a8c21a54SThe etnaviv authors 	seq_puts(m, "\tfeatures\n");
814a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features0: 0x%08x\n",
815a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features0);
816a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features1: 0x%08x\n",
817a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features1);
818a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features2: 0x%08x\n",
819a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features2);
820a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features3: 0x%08x\n",
821a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features3);
822602eb489SRussell King 	seq_printf(m, "\t minor_features4: 0x%08x\n",
823602eb489SRussell King 		   gpu->identity.minor_features4);
824602eb489SRussell King 	seq_printf(m, "\t minor_features5: 0x%08x\n",
825602eb489SRussell King 		   gpu->identity.minor_features5);
826a8c21a54SThe etnaviv authors 
827a8c21a54SThe etnaviv authors 	seq_puts(m, "\tspecs\n");
828a8c21a54SThe etnaviv authors 	seq_printf(m, "\t stream_count:  %d\n",
829a8c21a54SThe etnaviv authors 			gpu->identity.stream_count);
830a8c21a54SThe etnaviv authors 	seq_printf(m, "\t register_max: %d\n",
831a8c21a54SThe etnaviv authors 			gpu->identity.register_max);
832a8c21a54SThe etnaviv authors 	seq_printf(m, "\t thread_count: %d\n",
833a8c21a54SThe etnaviv authors 			gpu->identity.thread_count);
834a8c21a54SThe etnaviv authors 	seq_printf(m, "\t vertex_cache_size: %d\n",
835a8c21a54SThe etnaviv authors 			gpu->identity.vertex_cache_size);
836a8c21a54SThe etnaviv authors 	seq_printf(m, "\t shader_core_count: %d\n",
837a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count);
838a8c21a54SThe etnaviv authors 	seq_printf(m, "\t pixel_pipes: %d\n",
839a8c21a54SThe etnaviv authors 			gpu->identity.pixel_pipes);
840a8c21a54SThe etnaviv authors 	seq_printf(m, "\t vertex_output_buffer_size: %d\n",
841a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size);
842a8c21a54SThe etnaviv authors 	seq_printf(m, "\t buffer_size: %d\n",
843a8c21a54SThe etnaviv authors 			gpu->identity.buffer_size);
844a8c21a54SThe etnaviv authors 	seq_printf(m, "\t instruction_count: %d\n",
845a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count);
846a8c21a54SThe etnaviv authors 	seq_printf(m, "\t num_constants: %d\n",
847a8c21a54SThe etnaviv authors 			gpu->identity.num_constants);
848602eb489SRussell King 	seq_printf(m, "\t varyings_count: %d\n",
849602eb489SRussell King 			gpu->identity.varyings_count);
850a8c21a54SThe etnaviv authors 
851a8c21a54SThe etnaviv authors 	seq_printf(m, "\taxi: 0x%08x\n", axi);
852a8c21a54SThe etnaviv authors 	seq_printf(m, "\tidle: 0x%08x\n", idle);
853a8c21a54SThe etnaviv authors 	idle |= ~gpu->idle_mask & ~VIVS_HI_IDLE_STATE_AXI_LP;
854a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_FE) == 0)
855a8c21a54SThe etnaviv authors 		seq_puts(m, "\t FE is not idle\n");
856a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_DE) == 0)
857a8c21a54SThe etnaviv authors 		seq_puts(m, "\t DE is not idle\n");
858a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_PE) == 0)
859a8c21a54SThe etnaviv authors 		seq_puts(m, "\t PE is not idle\n");
860a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_SH) == 0)
861a8c21a54SThe etnaviv authors 		seq_puts(m, "\t SH is not idle\n");
862a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_PA) == 0)
863a8c21a54SThe etnaviv authors 		seq_puts(m, "\t PA is not idle\n");
864a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_SE) == 0)
865a8c21a54SThe etnaviv authors 		seq_puts(m, "\t SE is not idle\n");
866a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_RA) == 0)
867a8c21a54SThe etnaviv authors 		seq_puts(m, "\t RA is not idle\n");
868a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_TX) == 0)
869a8c21a54SThe etnaviv authors 		seq_puts(m, "\t TX is not idle\n");
870a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_VG) == 0)
871a8c21a54SThe etnaviv authors 		seq_puts(m, "\t VG is not idle\n");
872a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_IM) == 0)
873a8c21a54SThe etnaviv authors 		seq_puts(m, "\t IM is not idle\n");
874a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_FP) == 0)
875a8c21a54SThe etnaviv authors 		seq_puts(m, "\t FP is not idle\n");
876a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_TS) == 0)
877a8c21a54SThe etnaviv authors 		seq_puts(m, "\t TS is not idle\n");
878a8c21a54SThe etnaviv authors 	if (idle & VIVS_HI_IDLE_STATE_AXI_LP)
879a8c21a54SThe etnaviv authors 		seq_puts(m, "\t AXI low power mode\n");
880a8c21a54SThe etnaviv authors 
881a8c21a54SThe etnaviv authors 	if (gpu->identity.features & chipFeatures_DEBUG_MODE) {
882a8c21a54SThe etnaviv authors 		u32 read0 = gpu_read(gpu, VIVS_MC_DEBUG_READ0);
883a8c21a54SThe etnaviv authors 		u32 read1 = gpu_read(gpu, VIVS_MC_DEBUG_READ1);
884a8c21a54SThe etnaviv authors 		u32 write = gpu_read(gpu, VIVS_MC_DEBUG_WRITE);
885a8c21a54SThe etnaviv authors 
886a8c21a54SThe etnaviv authors 		seq_puts(m, "\tMC\n");
887a8c21a54SThe etnaviv authors 		seq_printf(m, "\t read0: 0x%08x\n", read0);
888a8c21a54SThe etnaviv authors 		seq_printf(m, "\t read1: 0x%08x\n", read1);
889a8c21a54SThe etnaviv authors 		seq_printf(m, "\t write: 0x%08x\n", write);
890a8c21a54SThe etnaviv authors 	}
891a8c21a54SThe etnaviv authors 
892a8c21a54SThe etnaviv authors 	seq_puts(m, "\tDMA ");
893a8c21a54SThe etnaviv authors 
894a8c21a54SThe etnaviv authors 	if (debug.address[0] == debug.address[1] &&
895a8c21a54SThe etnaviv authors 	    debug.state[0] == debug.state[1]) {
896a8c21a54SThe etnaviv authors 		seq_puts(m, "seems to be stuck\n");
897a8c21a54SThe etnaviv authors 	} else if (debug.address[0] == debug.address[1]) {
898c01e0159SMasanari Iida 		seq_puts(m, "address is constant\n");
899a8c21a54SThe etnaviv authors 	} else {
900c01e0159SMasanari Iida 		seq_puts(m, "is running\n");
901a8c21a54SThe etnaviv authors 	}
902a8c21a54SThe etnaviv authors 
903a8c21a54SThe etnaviv authors 	seq_printf(m, "\t address 0: 0x%08x\n", debug.address[0]);
904a8c21a54SThe etnaviv authors 	seq_printf(m, "\t address 1: 0x%08x\n", debug.address[1]);
905a8c21a54SThe etnaviv authors 	seq_printf(m, "\t state 0: 0x%08x\n", debug.state[0]);
906a8c21a54SThe etnaviv authors 	seq_printf(m, "\t state 1: 0x%08x\n", debug.state[1]);
907a8c21a54SThe etnaviv authors 	seq_printf(m, "\t last fetch 64 bit word: 0x%08x 0x%08x\n",
908a8c21a54SThe etnaviv authors 		   dma_lo, dma_hi);
909a8c21a54SThe etnaviv authors 
910a8c21a54SThe etnaviv authors 	ret = 0;
911a8c21a54SThe etnaviv authors 
912a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
913a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
914a8c21a54SThe etnaviv authors 
915a8c21a54SThe etnaviv authors 	return ret;
916a8c21a54SThe etnaviv authors }
917a8c21a54SThe etnaviv authors #endif
918a8c21a54SThe etnaviv authors 
919a8c21a54SThe etnaviv authors /*
920a8c21a54SThe etnaviv authors  * Hangcheck detection for locked gpu:
921a8c21a54SThe etnaviv authors  */
922a8c21a54SThe etnaviv authors static void recover_worker(struct work_struct *work)
923a8c21a54SThe etnaviv authors {
924a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
925a8c21a54SThe etnaviv authors 					       recover_work);
926a8c21a54SThe etnaviv authors 	unsigned long flags;
927355502e0SChristian Gmeiner 	unsigned int i = 0;
928a8c21a54SThe etnaviv authors 
929a8c21a54SThe etnaviv authors 	dev_err(gpu->dev, "hangcheck recover!\n");
930a8c21a54SThe etnaviv authors 
931a8c21a54SThe etnaviv authors 	if (pm_runtime_get_sync(gpu->dev) < 0)
932a8c21a54SThe etnaviv authors 		return;
933a8c21a54SThe etnaviv authors 
934a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
935a8c21a54SThe etnaviv authors 
936a8c21a54SThe etnaviv authors 	/* Only catch the first event, or when manually re-armed */
937a8c21a54SThe etnaviv authors 	if (etnaviv_dump_core) {
938a8c21a54SThe etnaviv authors 		etnaviv_core_dump(gpu);
939a8c21a54SThe etnaviv authors 		etnaviv_dump_core = false;
940a8c21a54SThe etnaviv authors 	}
941a8c21a54SThe etnaviv authors 
942a8c21a54SThe etnaviv authors 	etnaviv_hw_reset(gpu);
943a8c21a54SThe etnaviv authors 
944a8c21a54SThe etnaviv authors 	/* complete all events, the GPU won't do it after the reset */
945a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
946355502e0SChristian Gmeiner 	for_each_set_bit_from(i, gpu->event_bitmap, ETNA_NR_EVENTS) {
947f54d1867SChris Wilson 		dma_fence_signal(gpu->event[i].fence);
948a8c21a54SThe etnaviv authors 		gpu->event[i].fence = NULL;
949a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
950a8c21a54SThe etnaviv authors 	}
951355502e0SChristian Gmeiner 	bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS);
952a8c21a54SThe etnaviv authors 	spin_unlock_irqrestore(&gpu->event_spinlock, flags);
953a8c21a54SThe etnaviv authors 	gpu->completed_fence = gpu->active_fence;
954a8c21a54SThe etnaviv authors 
955a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
9561b94a9b7SLucas Stach 	gpu->lastctx = NULL;
957f6086311SRussell King 	gpu->exec_state = -1;
958a8c21a54SThe etnaviv authors 
959a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
960a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
961a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
962a8c21a54SThe etnaviv authors 
963a8c21a54SThe etnaviv authors 	/* Retire the buffer objects in a work */
964a7790d78SLucas Stach 	queue_work(gpu->wq, &gpu->retire_work);
965a8c21a54SThe etnaviv authors }
966a8c21a54SThe etnaviv authors 
967a8c21a54SThe etnaviv authors static void hangcheck_timer_reset(struct etnaviv_gpu *gpu)
968a8c21a54SThe etnaviv authors {
969a8c21a54SThe etnaviv authors 	DBG("%s", dev_name(gpu->dev));
970a8c21a54SThe etnaviv authors 	mod_timer(&gpu->hangcheck_timer,
971a8c21a54SThe etnaviv authors 		  round_jiffies_up(jiffies + DRM_ETNAVIV_HANGCHECK_JIFFIES));
972a8c21a54SThe etnaviv authors }
973a8c21a54SThe etnaviv authors 
97443b70524SKees Cook static void hangcheck_handler(struct timer_list *t)
975a8c21a54SThe etnaviv authors {
97643b70524SKees Cook 	struct etnaviv_gpu *gpu = from_timer(gpu, t, hangcheck_timer);
977a8c21a54SThe etnaviv authors 	u32 fence = gpu->completed_fence;
978a8c21a54SThe etnaviv authors 	bool progress = false;
979a8c21a54SThe etnaviv authors 
980a8c21a54SThe etnaviv authors 	if (fence != gpu->hangcheck_fence) {
981a8c21a54SThe etnaviv authors 		gpu->hangcheck_fence = fence;
982a8c21a54SThe etnaviv authors 		progress = true;
983a8c21a54SThe etnaviv authors 	}
984a8c21a54SThe etnaviv authors 
985a8c21a54SThe etnaviv authors 	if (!progress) {
986a8c21a54SThe etnaviv authors 		u32 dma_addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
987a8c21a54SThe etnaviv authors 		int change = dma_addr - gpu->hangcheck_dma_addr;
988a8c21a54SThe etnaviv authors 
989a8c21a54SThe etnaviv authors 		if (change < 0 || change > 16) {
990a8c21a54SThe etnaviv authors 			gpu->hangcheck_dma_addr = dma_addr;
991a8c21a54SThe etnaviv authors 			progress = true;
992a8c21a54SThe etnaviv authors 		}
993a8c21a54SThe etnaviv authors 	}
994a8c21a54SThe etnaviv authors 
995a8c21a54SThe etnaviv authors 	if (!progress && fence_after(gpu->active_fence, fence)) {
996a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "hangcheck detected gpu lockup!\n");
997a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "     completed fence: %u\n", fence);
998a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "     active fence: %u\n",
999a8c21a54SThe etnaviv authors 			gpu->active_fence);
1000a7790d78SLucas Stach 		queue_work(gpu->wq, &gpu->recover_work);
1001a8c21a54SThe etnaviv authors 	}
1002a8c21a54SThe etnaviv authors 
1003a8c21a54SThe etnaviv authors 	/* if still more pending work, reset the hangcheck timer: */
1004a8c21a54SThe etnaviv authors 	if (fence_after(gpu->active_fence, gpu->hangcheck_fence))
1005a8c21a54SThe etnaviv authors 		hangcheck_timer_reset(gpu);
1006a8c21a54SThe etnaviv authors }
1007a8c21a54SThe etnaviv authors 
1008a8c21a54SThe etnaviv authors static void hangcheck_disable(struct etnaviv_gpu *gpu)
1009a8c21a54SThe etnaviv authors {
1010a8c21a54SThe etnaviv authors 	del_timer_sync(&gpu->hangcheck_timer);
1011a8c21a54SThe etnaviv authors 	cancel_work_sync(&gpu->recover_work);
1012a8c21a54SThe etnaviv authors }
1013a8c21a54SThe etnaviv authors 
1014a8c21a54SThe etnaviv authors /* fence object management */
1015a8c21a54SThe etnaviv authors struct etnaviv_fence {
1016a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
1017f54d1867SChris Wilson 	struct dma_fence base;
1018a8c21a54SThe etnaviv authors };
1019a8c21a54SThe etnaviv authors 
1020f54d1867SChris Wilson static inline struct etnaviv_fence *to_etnaviv_fence(struct dma_fence *fence)
1021a8c21a54SThe etnaviv authors {
1022a8c21a54SThe etnaviv authors 	return container_of(fence, struct etnaviv_fence, base);
1023a8c21a54SThe etnaviv authors }
1024a8c21a54SThe etnaviv authors 
1025f54d1867SChris Wilson static const char *etnaviv_fence_get_driver_name(struct dma_fence *fence)
1026a8c21a54SThe etnaviv authors {
1027a8c21a54SThe etnaviv authors 	return "etnaviv";
1028a8c21a54SThe etnaviv authors }
1029a8c21a54SThe etnaviv authors 
1030f54d1867SChris Wilson static const char *etnaviv_fence_get_timeline_name(struct dma_fence *fence)
1031a8c21a54SThe etnaviv authors {
1032a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1033a8c21a54SThe etnaviv authors 
1034a8c21a54SThe etnaviv authors 	return dev_name(f->gpu->dev);
1035a8c21a54SThe etnaviv authors }
1036a8c21a54SThe etnaviv authors 
1037f54d1867SChris Wilson static bool etnaviv_fence_enable_signaling(struct dma_fence *fence)
1038a8c21a54SThe etnaviv authors {
1039a8c21a54SThe etnaviv authors 	return true;
1040a8c21a54SThe etnaviv authors }
1041a8c21a54SThe etnaviv authors 
1042f54d1867SChris Wilson static bool etnaviv_fence_signaled(struct dma_fence *fence)
1043a8c21a54SThe etnaviv authors {
1044a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1045a8c21a54SThe etnaviv authors 
1046a8c21a54SThe etnaviv authors 	return fence_completed(f->gpu, f->base.seqno);
1047a8c21a54SThe etnaviv authors }
1048a8c21a54SThe etnaviv authors 
1049f54d1867SChris Wilson static void etnaviv_fence_release(struct dma_fence *fence)
1050a8c21a54SThe etnaviv authors {
1051a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1052a8c21a54SThe etnaviv authors 
1053a8c21a54SThe etnaviv authors 	kfree_rcu(f, base.rcu);
1054a8c21a54SThe etnaviv authors }
1055a8c21a54SThe etnaviv authors 
1056f54d1867SChris Wilson static const struct dma_fence_ops etnaviv_fence_ops = {
1057a8c21a54SThe etnaviv authors 	.get_driver_name = etnaviv_fence_get_driver_name,
1058a8c21a54SThe etnaviv authors 	.get_timeline_name = etnaviv_fence_get_timeline_name,
1059a8c21a54SThe etnaviv authors 	.enable_signaling = etnaviv_fence_enable_signaling,
1060a8c21a54SThe etnaviv authors 	.signaled = etnaviv_fence_signaled,
1061f54d1867SChris Wilson 	.wait = dma_fence_default_wait,
1062a8c21a54SThe etnaviv authors 	.release = etnaviv_fence_release,
1063a8c21a54SThe etnaviv authors };
1064a8c21a54SThe etnaviv authors 
1065f54d1867SChris Wilson static struct dma_fence *etnaviv_gpu_fence_alloc(struct etnaviv_gpu *gpu)
1066a8c21a54SThe etnaviv authors {
1067a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f;
1068a8c21a54SThe etnaviv authors 
1069b27734c2SLucas Stach 	/*
1070b27734c2SLucas Stach 	 * GPU lock must already be held, otherwise fence completion order might
1071b27734c2SLucas Stach 	 * not match the seqno order assigned here.
1072b27734c2SLucas Stach 	 */
1073b27734c2SLucas Stach 	lockdep_assert_held(&gpu->lock);
1074b27734c2SLucas Stach 
1075a8c21a54SThe etnaviv authors 	f = kzalloc(sizeof(*f), GFP_KERNEL);
1076a8c21a54SThe etnaviv authors 	if (!f)
1077a8c21a54SThe etnaviv authors 		return NULL;
1078a8c21a54SThe etnaviv authors 
1079a8c21a54SThe etnaviv authors 	f->gpu = gpu;
1080a8c21a54SThe etnaviv authors 
1081f54d1867SChris Wilson 	dma_fence_init(&f->base, &etnaviv_fence_ops, &gpu->fence_spinlock,
1082a8c21a54SThe etnaviv authors 		       gpu->fence_context, ++gpu->next_fence);
1083a8c21a54SThe etnaviv authors 
1084a8c21a54SThe etnaviv authors 	return &f->base;
1085a8c21a54SThe etnaviv authors }
1086a8c21a54SThe etnaviv authors 
1087a8c21a54SThe etnaviv authors int etnaviv_gpu_fence_sync_obj(struct etnaviv_gem_object *etnaviv_obj,
10889ad59feaSPhilipp Zabel 	unsigned int context, bool exclusive, bool explicit)
1089a8c21a54SThe etnaviv authors {
1090a8c21a54SThe etnaviv authors 	struct reservation_object *robj = etnaviv_obj->resv;
1091a8c21a54SThe etnaviv authors 	struct reservation_object_list *fobj;
1092f54d1867SChris Wilson 	struct dma_fence *fence;
1093a8c21a54SThe etnaviv authors 	int i, ret;
1094a8c21a54SThe etnaviv authors 
1095a8c21a54SThe etnaviv authors 	if (!exclusive) {
1096a8c21a54SThe etnaviv authors 		ret = reservation_object_reserve_shared(robj);
1097a8c21a54SThe etnaviv authors 		if (ret)
1098a8c21a54SThe etnaviv authors 			return ret;
1099a8c21a54SThe etnaviv authors 	}
1100a8c21a54SThe etnaviv authors 
11019ad59feaSPhilipp Zabel 	if (explicit)
11029ad59feaSPhilipp Zabel 		return 0;
11039ad59feaSPhilipp Zabel 
1104a8c21a54SThe etnaviv authors 	/*
1105a8c21a54SThe etnaviv authors 	 * If we have any shared fences, then the exclusive fence
1106a8c21a54SThe etnaviv authors 	 * should be ignored as it will already have been signalled.
1107a8c21a54SThe etnaviv authors 	 */
1108a8c21a54SThe etnaviv authors 	fobj = reservation_object_get_list(robj);
1109a8c21a54SThe etnaviv authors 	if (!fobj || fobj->shared_count == 0) {
1110a8c21a54SThe etnaviv authors 		/* Wait on any existing exclusive fence which isn't our own */
1111a8c21a54SThe etnaviv authors 		fence = reservation_object_get_excl(robj);
1112a8c21a54SThe etnaviv authors 		if (fence && fence->context != context) {
1113f54d1867SChris Wilson 			ret = dma_fence_wait(fence, true);
1114a8c21a54SThe etnaviv authors 			if (ret)
1115a8c21a54SThe etnaviv authors 				return ret;
1116a8c21a54SThe etnaviv authors 		}
1117a8c21a54SThe etnaviv authors 	}
1118a8c21a54SThe etnaviv authors 
1119a8c21a54SThe etnaviv authors 	if (!exclusive || !fobj)
1120a8c21a54SThe etnaviv authors 		return 0;
1121a8c21a54SThe etnaviv authors 
1122a8c21a54SThe etnaviv authors 	for (i = 0; i < fobj->shared_count; i++) {
1123a8c21a54SThe etnaviv authors 		fence = rcu_dereference_protected(fobj->shared[i],
1124a8c21a54SThe etnaviv authors 						reservation_object_held(robj));
1125a8c21a54SThe etnaviv authors 		if (fence->context != context) {
1126f54d1867SChris Wilson 			ret = dma_fence_wait(fence, true);
1127a8c21a54SThe etnaviv authors 			if (ret)
1128a8c21a54SThe etnaviv authors 				return ret;
1129a8c21a54SThe etnaviv authors 		}
1130a8c21a54SThe etnaviv authors 	}
1131a8c21a54SThe etnaviv authors 
1132a8c21a54SThe etnaviv authors 	return 0;
1133a8c21a54SThe etnaviv authors }
1134a8c21a54SThe etnaviv authors 
1135a8c21a54SThe etnaviv authors /*
1136a8c21a54SThe etnaviv authors  * event management:
1137a8c21a54SThe etnaviv authors  */
1138a8c21a54SThe etnaviv authors 
113995a428c1SChristian Gmeiner static int event_alloc(struct etnaviv_gpu *gpu, unsigned nr_events,
114095a428c1SChristian Gmeiner 	unsigned int *events)
1141a8c21a54SThe etnaviv authors {
114295a428c1SChristian Gmeiner 	unsigned long flags, timeout = msecs_to_jiffies(10 * 10000);
114395a428c1SChristian Gmeiner 	unsigned i, acquired = 0;
1144a8c21a54SThe etnaviv authors 
114595a428c1SChristian Gmeiner 	for (i = 0; i < nr_events; i++) {
114695a428c1SChristian Gmeiner 		unsigned long ret;
114795a428c1SChristian Gmeiner 
114895a428c1SChristian Gmeiner 		ret = wait_for_completion_timeout(&gpu->event_free, timeout);
114995a428c1SChristian Gmeiner 
115095a428c1SChristian Gmeiner 		if (!ret) {
1151a8c21a54SThe etnaviv authors 			dev_err(gpu->dev, "wait_for_completion_timeout failed");
115295a428c1SChristian Gmeiner 			goto out;
115395a428c1SChristian Gmeiner 		}
115495a428c1SChristian Gmeiner 
115595a428c1SChristian Gmeiner 		acquired++;
115695a428c1SChristian Gmeiner 		timeout = ret;
115795a428c1SChristian Gmeiner 	}
1158a8c21a54SThe etnaviv authors 
1159a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
1160a8c21a54SThe etnaviv authors 
116195a428c1SChristian Gmeiner 	for (i = 0; i < nr_events; i++) {
116295a428c1SChristian Gmeiner 		int event = find_first_zero_bit(gpu->event_bitmap, ETNA_NR_EVENTS);
116395a428c1SChristian Gmeiner 
116495a428c1SChristian Gmeiner 		events[i] = event;
1165547d340dSChristian Gmeiner 		memset(&gpu->event[event], 0, sizeof(struct etnaviv_event));
1166355502e0SChristian Gmeiner 		set_bit(event, gpu->event_bitmap);
1167a8c21a54SThe etnaviv authors 	}
1168a8c21a54SThe etnaviv authors 
1169a8c21a54SThe etnaviv authors 	spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1170a8c21a54SThe etnaviv authors 
117195a428c1SChristian Gmeiner 	return 0;
117295a428c1SChristian Gmeiner 
117395a428c1SChristian Gmeiner out:
117495a428c1SChristian Gmeiner 	for (i = 0; i < acquired; i++)
117595a428c1SChristian Gmeiner 		complete(&gpu->event_free);
117695a428c1SChristian Gmeiner 
117795a428c1SChristian Gmeiner 	return -EBUSY;
1178a8c21a54SThe etnaviv authors }
1179a8c21a54SThe etnaviv authors 
1180a8c21a54SThe etnaviv authors static void event_free(struct etnaviv_gpu *gpu, unsigned int event)
1181a8c21a54SThe etnaviv authors {
1182a8c21a54SThe etnaviv authors 	unsigned long flags;
1183a8c21a54SThe etnaviv authors 
1184a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
1185a8c21a54SThe etnaviv authors 
1186355502e0SChristian Gmeiner 	if (!test_bit(event, gpu->event_bitmap)) {
1187a8c21a54SThe etnaviv authors 		dev_warn(gpu->dev, "event %u is already marked as free",
1188a8c21a54SThe etnaviv authors 			 event);
1189a8c21a54SThe etnaviv authors 		spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1190a8c21a54SThe etnaviv authors 	} else {
1191355502e0SChristian Gmeiner 		clear_bit(event, gpu->event_bitmap);
1192a8c21a54SThe etnaviv authors 		spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1193a8c21a54SThe etnaviv authors 
1194a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
1195a8c21a54SThe etnaviv authors 	}
1196a8c21a54SThe etnaviv authors }
1197a8c21a54SThe etnaviv authors 
1198a8c21a54SThe etnaviv authors /*
1199a8c21a54SThe etnaviv authors  * Cmdstream submission/retirement:
1200a8c21a54SThe etnaviv authors  */
1201a8c21a54SThe etnaviv authors 
1202a8c21a54SThe etnaviv authors static void retire_worker(struct work_struct *work)
1203a8c21a54SThe etnaviv authors {
1204a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
1205a8c21a54SThe etnaviv authors 					       retire_work);
1206a8c21a54SThe etnaviv authors 	u32 fence = gpu->completed_fence;
12072f9225dbSLucas Stach 	struct etnaviv_gem_submit *submit, *tmp;
12082e3a2ddaSLucas Stach 	LIST_HEAD(retire_list);
1209a8c21a54SThe etnaviv authors 
1210a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
12112f9225dbSLucas Stach 	list_for_each_entry_safe(submit, tmp, &gpu->active_submit_list, node) {
12122f9225dbSLucas Stach 		if (!dma_fence_is_signaled(submit->out_fence))
1213a8c21a54SThe etnaviv authors 			break;
1214a8c21a54SThe etnaviv authors 
12152e3a2ddaSLucas Stach 		list_move(&submit->node, &retire_list);
1216a8c21a54SThe etnaviv authors 	}
1217a8c21a54SThe etnaviv authors 
1218a8c21a54SThe etnaviv authors 	gpu->retired_fence = fence;
1219a8c21a54SThe etnaviv authors 
1220a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
12212e3a2ddaSLucas Stach 
12222e3a2ddaSLucas Stach 	list_for_each_entry_safe(submit, tmp, &retire_list, node)
12232e3a2ddaSLucas Stach 		etnaviv_submit_put(submit);
1224a8c21a54SThe etnaviv authors }
1225a8c21a54SThe etnaviv authors 
1226a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu,
1227a8c21a54SThe etnaviv authors 	u32 fence, struct timespec *timeout)
1228a8c21a54SThe etnaviv authors {
1229a8c21a54SThe etnaviv authors 	int ret;
1230a8c21a54SThe etnaviv authors 
1231a8c21a54SThe etnaviv authors 	if (fence_after(fence, gpu->next_fence)) {
1232a8c21a54SThe etnaviv authors 		DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
1233a8c21a54SThe etnaviv authors 				fence, gpu->next_fence);
1234a8c21a54SThe etnaviv authors 		return -EINVAL;
1235a8c21a54SThe etnaviv authors 	}
1236a8c21a54SThe etnaviv authors 
1237a8c21a54SThe etnaviv authors 	if (!timeout) {
1238a8c21a54SThe etnaviv authors 		/* No timeout was requested: just test for completion */
1239a8c21a54SThe etnaviv authors 		ret = fence_completed(gpu, fence) ? 0 : -EBUSY;
1240a8c21a54SThe etnaviv authors 	} else {
1241a8c21a54SThe etnaviv authors 		unsigned long remaining = etnaviv_timeout_to_jiffies(timeout);
1242a8c21a54SThe etnaviv authors 
1243a8c21a54SThe etnaviv authors 		ret = wait_event_interruptible_timeout(gpu->fence_event,
1244a8c21a54SThe etnaviv authors 						fence_completed(gpu, fence),
1245a8c21a54SThe etnaviv authors 						remaining);
1246a8c21a54SThe etnaviv authors 		if (ret == 0) {
1247a8c21a54SThe etnaviv authors 			DBG("timeout waiting for fence: %u (retired: %u completed: %u)",
1248a8c21a54SThe etnaviv authors 				fence, gpu->retired_fence,
1249a8c21a54SThe etnaviv authors 				gpu->completed_fence);
1250a8c21a54SThe etnaviv authors 			ret = -ETIMEDOUT;
1251a8c21a54SThe etnaviv authors 		} else if (ret != -ERESTARTSYS) {
1252a8c21a54SThe etnaviv authors 			ret = 0;
1253a8c21a54SThe etnaviv authors 		}
1254a8c21a54SThe etnaviv authors 	}
1255a8c21a54SThe etnaviv authors 
1256a8c21a54SThe etnaviv authors 	return ret;
1257a8c21a54SThe etnaviv authors }
1258a8c21a54SThe etnaviv authors 
1259a8c21a54SThe etnaviv authors /*
1260a8c21a54SThe etnaviv authors  * Wait for an object to become inactive.  This, on it's own, is not race
1261a8c21a54SThe etnaviv authors  * free: the object is moved by the retire worker off the active list, and
1262a8c21a54SThe etnaviv authors  * then the iova is put.  Moreover, the object could be re-submitted just
1263a8c21a54SThe etnaviv authors  * after we notice that it's become inactive.
1264a8c21a54SThe etnaviv authors  *
1265a8c21a54SThe etnaviv authors  * Although the retirement happens under the gpu lock, we don't want to hold
1266a8c21a54SThe etnaviv authors  * that lock in this function while waiting.
1267a8c21a54SThe etnaviv authors  */
1268a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu,
1269a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout)
1270a8c21a54SThe etnaviv authors {
1271a8c21a54SThe etnaviv authors 	unsigned long remaining;
1272a8c21a54SThe etnaviv authors 	long ret;
1273a8c21a54SThe etnaviv authors 
1274a8c21a54SThe etnaviv authors 	if (!timeout)
1275a8c21a54SThe etnaviv authors 		return !is_active(etnaviv_obj) ? 0 : -EBUSY;
1276a8c21a54SThe etnaviv authors 
1277a8c21a54SThe etnaviv authors 	remaining = etnaviv_timeout_to_jiffies(timeout);
1278a8c21a54SThe etnaviv authors 
1279a8c21a54SThe etnaviv authors 	ret = wait_event_interruptible_timeout(gpu->fence_event,
1280a8c21a54SThe etnaviv authors 					       !is_active(etnaviv_obj),
1281a8c21a54SThe etnaviv authors 					       remaining);
1282fa67ac84SLucas Stach 	if (ret > 0)
1283a8c21a54SThe etnaviv authors 		return 0;
1284fa67ac84SLucas Stach 	else if (ret == -ERESTARTSYS)
1285a8c21a54SThe etnaviv authors 		return -ERESTARTSYS;
1286fa67ac84SLucas Stach 	else
1287a8c21a54SThe etnaviv authors 		return -ETIMEDOUT;
1288a8c21a54SThe etnaviv authors }
1289a8c21a54SThe etnaviv authors 
129068dc0b29SChristian Gmeiner static void sync_point_perfmon_sample(struct etnaviv_gpu *gpu,
129168dc0b29SChristian Gmeiner 	struct etnaviv_event *event, unsigned int flags)
129268dc0b29SChristian Gmeiner {
1293ef146c00SLucas Stach 	const struct etnaviv_gem_submit *submit = event->submit;
129468dc0b29SChristian Gmeiner 	unsigned int i;
129568dc0b29SChristian Gmeiner 
1296ef146c00SLucas Stach 	for (i = 0; i < submit->nr_pmrs; i++) {
1297ef146c00SLucas Stach 		const struct etnaviv_perfmon_request *pmr = submit->pmrs + i;
129868dc0b29SChristian Gmeiner 
129968dc0b29SChristian Gmeiner 		if (pmr->flags == flags)
13007a9c0fe2SLucas Stach 			etnaviv_perfmon_process(gpu, pmr, submit->exec_state);
130168dc0b29SChristian Gmeiner 	}
130268dc0b29SChristian Gmeiner }
130368dc0b29SChristian Gmeiner 
130468dc0b29SChristian Gmeiner static void sync_point_perfmon_sample_pre(struct etnaviv_gpu *gpu,
130568dc0b29SChristian Gmeiner 	struct etnaviv_event *event)
130668dc0b29SChristian Gmeiner {
13072c8b0c5aSChristian Gmeiner 	u32 val;
13082c8b0c5aSChristian Gmeiner 
13092c8b0c5aSChristian Gmeiner 	/* disable clock gating */
13102c8b0c5aSChristian Gmeiner 	val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
13112c8b0c5aSChristian Gmeiner 	val &= ~VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
13122c8b0c5aSChristian Gmeiner 	gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val);
13132c8b0c5aSChristian Gmeiner 
131404a7d18dSChristian Gmeiner 	/* enable debug register */
131504a7d18dSChristian Gmeiner 	val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
131604a7d18dSChristian Gmeiner 	val &= ~VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
131704a7d18dSChristian Gmeiner 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val);
131804a7d18dSChristian Gmeiner 
131968dc0b29SChristian Gmeiner 	sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_PRE);
132068dc0b29SChristian Gmeiner }
132168dc0b29SChristian Gmeiner 
132268dc0b29SChristian Gmeiner static void sync_point_perfmon_sample_post(struct etnaviv_gpu *gpu,
132368dc0b29SChristian Gmeiner 	struct etnaviv_event *event)
132468dc0b29SChristian Gmeiner {
1325ef146c00SLucas Stach 	const struct etnaviv_gem_submit *submit = event->submit;
132668dc0b29SChristian Gmeiner 	unsigned int i;
13272c8b0c5aSChristian Gmeiner 	u32 val;
132868dc0b29SChristian Gmeiner 
132968dc0b29SChristian Gmeiner 	sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_POST);
133068dc0b29SChristian Gmeiner 
1331ef146c00SLucas Stach 	for (i = 0; i < submit->nr_pmrs; i++) {
1332ef146c00SLucas Stach 		const struct etnaviv_perfmon_request *pmr = submit->pmrs + i;
133368dc0b29SChristian Gmeiner 
133468dc0b29SChristian Gmeiner 		*pmr->bo_vma = pmr->sequence;
133568dc0b29SChristian Gmeiner 	}
13362c8b0c5aSChristian Gmeiner 
133704a7d18dSChristian Gmeiner 	/* disable debug register */
133804a7d18dSChristian Gmeiner 	val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
133904a7d18dSChristian Gmeiner 	val |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
134004a7d18dSChristian Gmeiner 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val);
134104a7d18dSChristian Gmeiner 
13422c8b0c5aSChristian Gmeiner 	/* enable clock gating */
13432c8b0c5aSChristian Gmeiner 	val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
13442c8b0c5aSChristian Gmeiner 	val |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
13452c8b0c5aSChristian Gmeiner 	gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val);
134668dc0b29SChristian Gmeiner }
134768dc0b29SChristian Gmeiner 
134868dc0b29SChristian Gmeiner 
1349a8c21a54SThe etnaviv authors /* add bo's to gpu's ring, and kick gpu: */
1350a8c21a54SThe etnaviv authors int etnaviv_gpu_submit(struct etnaviv_gpu *gpu,
13512f9225dbSLucas Stach 	struct etnaviv_gem_submit *submit)
1352a8c21a54SThe etnaviv authors {
135368dc0b29SChristian Gmeiner 	unsigned int i, nr_events = 1, event[3];
1354a8c21a54SThe etnaviv authors 	int ret;
1355a8c21a54SThe etnaviv authors 
13568bda1516SLucas Stach 	ret = pm_runtime_get_sync(gpu->dev);
1357a8c21a54SThe etnaviv authors 	if (ret < 0)
1358a8c21a54SThe etnaviv authors 		return ret;
13598bda1516SLucas Stach 	submit->runtime_resumed = true;
1360a8c21a54SThe etnaviv authors 
1361a8c21a54SThe etnaviv authors 	/*
136268dc0b29SChristian Gmeiner 	 * if there are performance monitor requests we need to have
136368dc0b29SChristian Gmeiner 	 * - a sync point to re-configure gpu and process ETNA_PM_PROCESS_PRE
136468dc0b29SChristian Gmeiner 	 *   requests.
136568dc0b29SChristian Gmeiner 	 * - a sync point to re-configure gpu, process ETNA_PM_PROCESS_POST requests
136668dc0b29SChristian Gmeiner 	 *   and update the sequence number for userspace.
136768dc0b29SChristian Gmeiner 	 */
1368ef146c00SLucas Stach 	if (submit->nr_pmrs)
136968dc0b29SChristian Gmeiner 		nr_events = 3;
137068dc0b29SChristian Gmeiner 
137168dc0b29SChristian Gmeiner 	ret = event_alloc(gpu, nr_events, event);
137295a428c1SChristian Gmeiner 	if (ret) {
137368dc0b29SChristian Gmeiner 		DRM_ERROR("no free events\n");
13748bda1516SLucas Stach 		return ret;
1375a8c21a54SThe etnaviv authors 	}
1376a8c21a54SThe etnaviv authors 
1377f3cd1b06SLucas Stach 	mutex_lock(&gpu->lock);
1378f3cd1b06SLucas Stach 
13792f9225dbSLucas Stach 	submit->out_fence = etnaviv_gpu_fence_alloc(gpu);
13802f9225dbSLucas Stach 	if (!submit->out_fence) {
138168dc0b29SChristian Gmeiner 		for (i = 0; i < nr_events; i++)
138268dc0b29SChristian Gmeiner 			event_free(gpu, event[i]);
138368dc0b29SChristian Gmeiner 
1384a8c21a54SThe etnaviv authors 		ret = -ENOMEM;
138545abdf35SWei Yongjun 		goto out_unlock;
1386a8c21a54SThe etnaviv authors 	}
1387a8c21a54SThe etnaviv authors 
138810009ea2SLucas Stach 	gpu->active_fence = submit->out_fence->seqno;
1389a8c21a54SThe etnaviv authors 
1390ef146c00SLucas Stach 	if (submit->nr_pmrs) {
139168dc0b29SChristian Gmeiner 		gpu->event[event[1]].sync_point = &sync_point_perfmon_sample_pre;
1392ef146c00SLucas Stach 		kref_get(&submit->refcount);
1393ef146c00SLucas Stach 		gpu->event[event[1]].submit = submit;
139468dc0b29SChristian Gmeiner 		etnaviv_sync_point_queue(gpu, event[1]);
139568dc0b29SChristian Gmeiner 	}
139668dc0b29SChristian Gmeiner 
13972f9225dbSLucas Stach 	kref_get(&submit->refcount);
13982f9225dbSLucas Stach 	gpu->event[event[0]].fence = submit->out_fence;
13992f9225dbSLucas Stach 	etnaviv_buffer_queue(gpu, submit->exec_state, event[0],
14002f9225dbSLucas Stach 			     &submit->cmdbuf);
140168dc0b29SChristian Gmeiner 
1402ef146c00SLucas Stach 	if (submit->nr_pmrs) {
140368dc0b29SChristian Gmeiner 		gpu->event[event[2]].sync_point = &sync_point_perfmon_sample_post;
1404ef146c00SLucas Stach 		kref_get(&submit->refcount);
1405ef146c00SLucas Stach 		gpu->event[event[2]].submit = submit;
140668dc0b29SChristian Gmeiner 		etnaviv_sync_point_queue(gpu, event[2]);
140768dc0b29SChristian Gmeiner 	}
1408a8c21a54SThe etnaviv authors 
14092f9225dbSLucas Stach 	list_add_tail(&submit->node, &gpu->active_submit_list);
1410a8c21a54SThe etnaviv authors 
1411a8c21a54SThe etnaviv authors 	hangcheck_timer_reset(gpu);
1412a8c21a54SThe etnaviv authors 	ret = 0;
1413a8c21a54SThe etnaviv authors 
141445abdf35SWei Yongjun out_unlock:
1415a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1416a8c21a54SThe etnaviv authors 
1417a8c21a54SThe etnaviv authors 	return ret;
1418a8c21a54SThe etnaviv authors }
1419a8c21a54SThe etnaviv authors 
1420357713ceSChristian Gmeiner static void sync_point_worker(struct work_struct *work)
1421357713ceSChristian Gmeiner {
1422357713ceSChristian Gmeiner 	struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
1423357713ceSChristian Gmeiner 					       sync_point_work);
1424b9a48aa7SLucas Stach 	struct etnaviv_event *event = &gpu->event[gpu->sync_point_event];
1425b9a48aa7SLucas Stach 	u32 addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
1426357713ceSChristian Gmeiner 
1427b9a48aa7SLucas Stach 	event->sync_point(gpu, event);
1428ef146c00SLucas Stach 	etnaviv_submit_put(event->submit);
1429357713ceSChristian Gmeiner 	event_free(gpu, gpu->sync_point_event);
1430b9a48aa7SLucas Stach 
1431b9a48aa7SLucas Stach 	/* restart FE last to avoid GPU and IRQ racing against this worker */
1432b9a48aa7SLucas Stach 	etnaviv_gpu_start_fe(gpu, addr + 2, 2);
1433357713ceSChristian Gmeiner }
1434357713ceSChristian Gmeiner 
1435a8c21a54SThe etnaviv authors /*
1436a8c21a54SThe etnaviv authors  * Init/Cleanup:
1437a8c21a54SThe etnaviv authors  */
1438a8c21a54SThe etnaviv authors static irqreturn_t irq_handler(int irq, void *data)
1439a8c21a54SThe etnaviv authors {
1440a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = data;
1441a8c21a54SThe etnaviv authors 	irqreturn_t ret = IRQ_NONE;
1442a8c21a54SThe etnaviv authors 
1443a8c21a54SThe etnaviv authors 	u32 intr = gpu_read(gpu, VIVS_HI_INTR_ACKNOWLEDGE);
1444a8c21a54SThe etnaviv authors 
1445a8c21a54SThe etnaviv authors 	if (intr != 0) {
1446a8c21a54SThe etnaviv authors 		int event;
1447a8c21a54SThe etnaviv authors 
1448a8c21a54SThe etnaviv authors 		pm_runtime_mark_last_busy(gpu->dev);
1449a8c21a54SThe etnaviv authors 
1450a8c21a54SThe etnaviv authors 		dev_dbg(gpu->dev, "intr 0x%08x\n", intr);
1451a8c21a54SThe etnaviv authors 
1452a8c21a54SThe etnaviv authors 		if (intr & VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR) {
1453a8c21a54SThe etnaviv authors 			dev_err(gpu->dev, "AXI bus error\n");
1454a8c21a54SThe etnaviv authors 			intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR;
1455a8c21a54SThe etnaviv authors 		}
1456a8c21a54SThe etnaviv authors 
1457128a9b1dSLucas Stach 		if (intr & VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION) {
1458128a9b1dSLucas Stach 			int i;
1459128a9b1dSLucas Stach 
1460128a9b1dSLucas Stach 			dev_err_ratelimited(gpu->dev,
1461128a9b1dSLucas Stach 				"MMU fault status 0x%08x\n",
1462128a9b1dSLucas Stach 				gpu_read(gpu, VIVS_MMUv2_STATUS));
1463128a9b1dSLucas Stach 			for (i = 0; i < 4; i++) {
1464128a9b1dSLucas Stach 				dev_err_ratelimited(gpu->dev,
1465128a9b1dSLucas Stach 					"MMU %d fault addr 0x%08x\n",
1466128a9b1dSLucas Stach 					i, gpu_read(gpu,
1467128a9b1dSLucas Stach 					VIVS_MMUv2_EXCEPTION_ADDR(i)));
1468128a9b1dSLucas Stach 			}
1469128a9b1dSLucas Stach 			intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION;
1470128a9b1dSLucas Stach 		}
1471128a9b1dSLucas Stach 
1472a8c21a54SThe etnaviv authors 		while ((event = ffs(intr)) != 0) {
1473f54d1867SChris Wilson 			struct dma_fence *fence;
1474a8c21a54SThe etnaviv authors 
1475a8c21a54SThe etnaviv authors 			event -= 1;
1476a8c21a54SThe etnaviv authors 
1477a8c21a54SThe etnaviv authors 			intr &= ~(1 << event);
1478a8c21a54SThe etnaviv authors 
1479a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "event %u\n", event);
1480a8c21a54SThe etnaviv authors 
1481357713ceSChristian Gmeiner 			if (gpu->event[event].sync_point) {
1482357713ceSChristian Gmeiner 				gpu->sync_point_event = event;
1483a7790d78SLucas Stach 				queue_work(gpu->wq, &gpu->sync_point_work);
1484357713ceSChristian Gmeiner 			}
1485357713ceSChristian Gmeiner 
1486a8c21a54SThe etnaviv authors 			fence = gpu->event[event].fence;
148768dc0b29SChristian Gmeiner 			if (!fence)
148868dc0b29SChristian Gmeiner 				continue;
148968dc0b29SChristian Gmeiner 
1490a8c21a54SThe etnaviv authors 			gpu->event[event].fence = NULL;
1491f54d1867SChris Wilson 			dma_fence_signal(fence);
1492a8c21a54SThe etnaviv authors 
1493a8c21a54SThe etnaviv authors 			/*
1494a8c21a54SThe etnaviv authors 			 * Events can be processed out of order.  Eg,
1495a8c21a54SThe etnaviv authors 			 * - allocate and queue event 0
1496a8c21a54SThe etnaviv authors 			 * - allocate event 1
1497a8c21a54SThe etnaviv authors 			 * - event 0 completes, we process it
1498a8c21a54SThe etnaviv authors 			 * - allocate and queue event 0
1499a8c21a54SThe etnaviv authors 			 * - event 1 and event 0 complete
1500a8c21a54SThe etnaviv authors 			 * we can end up processing event 0 first, then 1.
1501a8c21a54SThe etnaviv authors 			 */
1502a8c21a54SThe etnaviv authors 			if (fence_after(fence->seqno, gpu->completed_fence))
1503a8c21a54SThe etnaviv authors 				gpu->completed_fence = fence->seqno;
1504a8c21a54SThe etnaviv authors 
1505a8c21a54SThe etnaviv authors 			event_free(gpu, event);
1506a8c21a54SThe etnaviv authors 		}
1507a8c21a54SThe etnaviv authors 
1508a8c21a54SThe etnaviv authors 		/* Retire the buffer objects in a work */
1509a7790d78SLucas Stach 		queue_work(gpu->wq, &gpu->retire_work);
1510a8c21a54SThe etnaviv authors 
1511a8c21a54SThe etnaviv authors 		ret = IRQ_HANDLED;
1512a8c21a54SThe etnaviv authors 	}
1513a8c21a54SThe etnaviv authors 
1514a8c21a54SThe etnaviv authors 	return ret;
1515a8c21a54SThe etnaviv authors }
1516a8c21a54SThe etnaviv authors 
1517a8c21a54SThe etnaviv authors static int etnaviv_gpu_clk_enable(struct etnaviv_gpu *gpu)
1518a8c21a54SThe etnaviv authors {
1519a8c21a54SThe etnaviv authors 	int ret;
1520a8c21a54SThe etnaviv authors 
15219c7310c0SLucas Stach 	if (gpu->clk_bus) {
15229c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_bus);
1523a8c21a54SThe etnaviv authors 		if (ret)
1524a8c21a54SThe etnaviv authors 			return ret;
1525a8c21a54SThe etnaviv authors 	}
1526a8c21a54SThe etnaviv authors 
15279c7310c0SLucas Stach 	if (gpu->clk_core) {
15289c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_core);
15299c7310c0SLucas Stach 		if (ret)
15309c7310c0SLucas Stach 			goto disable_clk_bus;
15319c7310c0SLucas Stach 	}
15329c7310c0SLucas Stach 
15339c7310c0SLucas Stach 	if (gpu->clk_shader) {
15349c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_shader);
15359c7310c0SLucas Stach 		if (ret)
15369c7310c0SLucas Stach 			goto disable_clk_core;
15379c7310c0SLucas Stach 	}
15389c7310c0SLucas Stach 
1539a8c21a54SThe etnaviv authors 	return 0;
15409c7310c0SLucas Stach 
15419c7310c0SLucas Stach disable_clk_core:
15429c7310c0SLucas Stach 	if (gpu->clk_core)
15439c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_core);
15449c7310c0SLucas Stach disable_clk_bus:
15459c7310c0SLucas Stach 	if (gpu->clk_bus)
15469c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_bus);
15479c7310c0SLucas Stach 
15489c7310c0SLucas Stach 	return ret;
1549a8c21a54SThe etnaviv authors }
1550a8c21a54SThe etnaviv authors 
1551a8c21a54SThe etnaviv authors static int etnaviv_gpu_clk_disable(struct etnaviv_gpu *gpu)
1552a8c21a54SThe etnaviv authors {
15539c7310c0SLucas Stach 	if (gpu->clk_shader)
15549c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_shader);
15559c7310c0SLucas Stach 	if (gpu->clk_core)
15569c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_core);
15579c7310c0SLucas Stach 	if (gpu->clk_bus)
15589c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_bus);
1559a8c21a54SThe etnaviv authors 
1560a8c21a54SThe etnaviv authors 	return 0;
1561a8c21a54SThe etnaviv authors }
1562a8c21a54SThe etnaviv authors 
1563b88163e3SLucas Stach int etnaviv_gpu_wait_idle(struct etnaviv_gpu *gpu, unsigned int timeout_ms)
1564b88163e3SLucas Stach {
1565b88163e3SLucas Stach 	unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1566b88163e3SLucas Stach 
1567b88163e3SLucas Stach 	do {
1568b88163e3SLucas Stach 		u32 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
1569b88163e3SLucas Stach 
1570b88163e3SLucas Stach 		if ((idle & gpu->idle_mask) == gpu->idle_mask)
1571b88163e3SLucas Stach 			return 0;
1572b88163e3SLucas Stach 
1573b88163e3SLucas Stach 		if (time_is_before_jiffies(timeout)) {
1574b88163e3SLucas Stach 			dev_warn(gpu->dev,
1575b88163e3SLucas Stach 				 "timed out waiting for idle: idle=0x%x\n",
1576b88163e3SLucas Stach 				 idle);
1577b88163e3SLucas Stach 			return -ETIMEDOUT;
1578b88163e3SLucas Stach 		}
1579b88163e3SLucas Stach 
1580b88163e3SLucas Stach 		udelay(5);
1581b88163e3SLucas Stach 	} while (1);
1582b88163e3SLucas Stach }
1583b88163e3SLucas Stach 
1584a8c21a54SThe etnaviv authors static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
1585a8c21a54SThe etnaviv authors {
15862f9225dbSLucas Stach 	if (gpu->buffer.suballoc) {
1587a8c21a54SThe etnaviv authors 		/* Replace the last WAIT with END */
158840c27bdeSLucas Stach 		mutex_lock(&gpu->lock);
1589a8c21a54SThe etnaviv authors 		etnaviv_buffer_end(gpu);
159040c27bdeSLucas Stach 		mutex_unlock(&gpu->lock);
1591a8c21a54SThe etnaviv authors 
1592a8c21a54SThe etnaviv authors 		/*
1593a8c21a54SThe etnaviv authors 		 * We know that only the FE is busy here, this should
1594a8c21a54SThe etnaviv authors 		 * happen quickly (as the WAIT is only 200 cycles).  If
1595a8c21a54SThe etnaviv authors 		 * we fail, just warn and continue.
1596a8c21a54SThe etnaviv authors 		 */
1597b88163e3SLucas Stach 		etnaviv_gpu_wait_idle(gpu, 100);
1598a8c21a54SThe etnaviv authors 	}
1599a8c21a54SThe etnaviv authors 
1600a8c21a54SThe etnaviv authors 	return etnaviv_gpu_clk_disable(gpu);
1601a8c21a54SThe etnaviv authors }
1602a8c21a54SThe etnaviv authors 
1603a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1604a8c21a54SThe etnaviv authors static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
1605a8c21a54SThe etnaviv authors {
1606a8c21a54SThe etnaviv authors 	int ret;
1607a8c21a54SThe etnaviv authors 
1608a8c21a54SThe etnaviv authors 	ret = mutex_lock_killable(&gpu->lock);
1609a8c21a54SThe etnaviv authors 	if (ret)
1610a8c21a54SThe etnaviv authors 		return ret;
1611a8c21a54SThe etnaviv authors 
1612bcdfb5e5SRussell King 	etnaviv_gpu_update_clock(gpu);
1613a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
1614a8c21a54SThe etnaviv authors 
16154375ffffSLucas Stach 	gpu->lastctx = NULL;
1616f6086311SRussell King 	gpu->exec_state = -1;
1617a8c21a54SThe etnaviv authors 
1618a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1619a8c21a54SThe etnaviv authors 
1620a8c21a54SThe etnaviv authors 	return 0;
1621a8c21a54SThe etnaviv authors }
1622a8c21a54SThe etnaviv authors #endif
1623a8c21a54SThe etnaviv authors 
1624bcdfb5e5SRussell King static int
1625bcdfb5e5SRussell King etnaviv_gpu_cooling_get_max_state(struct thermal_cooling_device *cdev,
1626bcdfb5e5SRussell King 				  unsigned long *state)
1627bcdfb5e5SRussell King {
1628bcdfb5e5SRussell King 	*state = 6;
1629bcdfb5e5SRussell King 
1630bcdfb5e5SRussell King 	return 0;
1631bcdfb5e5SRussell King }
1632bcdfb5e5SRussell King 
1633bcdfb5e5SRussell King static int
1634bcdfb5e5SRussell King etnaviv_gpu_cooling_get_cur_state(struct thermal_cooling_device *cdev,
1635bcdfb5e5SRussell King 				  unsigned long *state)
1636bcdfb5e5SRussell King {
1637bcdfb5e5SRussell King 	struct etnaviv_gpu *gpu = cdev->devdata;
1638bcdfb5e5SRussell King 
1639bcdfb5e5SRussell King 	*state = gpu->freq_scale;
1640bcdfb5e5SRussell King 
1641bcdfb5e5SRussell King 	return 0;
1642bcdfb5e5SRussell King }
1643bcdfb5e5SRussell King 
1644bcdfb5e5SRussell King static int
1645bcdfb5e5SRussell King etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
1646bcdfb5e5SRussell King 				  unsigned long state)
1647bcdfb5e5SRussell King {
1648bcdfb5e5SRussell King 	struct etnaviv_gpu *gpu = cdev->devdata;
1649bcdfb5e5SRussell King 
1650bcdfb5e5SRussell King 	mutex_lock(&gpu->lock);
1651bcdfb5e5SRussell King 	gpu->freq_scale = state;
1652bcdfb5e5SRussell King 	if (!pm_runtime_suspended(gpu->dev))
1653bcdfb5e5SRussell King 		etnaviv_gpu_update_clock(gpu);
1654bcdfb5e5SRussell King 	mutex_unlock(&gpu->lock);
1655bcdfb5e5SRussell King 
1656bcdfb5e5SRussell King 	return 0;
1657bcdfb5e5SRussell King }
1658bcdfb5e5SRussell King 
1659bcdfb5e5SRussell King static struct thermal_cooling_device_ops cooling_ops = {
1660bcdfb5e5SRussell King 	.get_max_state = etnaviv_gpu_cooling_get_max_state,
1661bcdfb5e5SRussell King 	.get_cur_state = etnaviv_gpu_cooling_get_cur_state,
1662bcdfb5e5SRussell King 	.set_cur_state = etnaviv_gpu_cooling_set_cur_state,
1663bcdfb5e5SRussell King };
1664bcdfb5e5SRussell King 
1665a8c21a54SThe etnaviv authors static int etnaviv_gpu_bind(struct device *dev, struct device *master,
1666a8c21a54SThe etnaviv authors 	void *data)
1667a8c21a54SThe etnaviv authors {
1668a8c21a54SThe etnaviv authors 	struct drm_device *drm = data;
1669a8c21a54SThe etnaviv authors 	struct etnaviv_drm_private *priv = drm->dev_private;
1670a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1671a8c21a54SThe etnaviv authors 	int ret;
1672a8c21a54SThe etnaviv authors 
167349b82c38SPhilipp Zabel 	if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) {
1674bcdfb5e5SRussell King 		gpu->cooling = thermal_of_cooling_device_register(dev->of_node,
1675bcdfb5e5SRussell King 				(char *)dev_name(dev), gpu, &cooling_ops);
1676bcdfb5e5SRussell King 		if (IS_ERR(gpu->cooling))
1677bcdfb5e5SRussell King 			return PTR_ERR(gpu->cooling);
16785247e2aaSLucas Stach 	}
1679bcdfb5e5SRussell King 
1680a7790d78SLucas Stach 	gpu->wq = alloc_ordered_workqueue(dev_name(dev), 0);
1681a7790d78SLucas Stach 	if (!gpu->wq) {
1682a7790d78SLucas Stach 		if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
1683a7790d78SLucas Stach 			thermal_cooling_device_unregister(gpu->cooling);
1684a7790d78SLucas Stach 		return -ENOMEM;
1685a7790d78SLucas Stach 	}
1686a7790d78SLucas Stach 
1687a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1688a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
1689a8c21a54SThe etnaviv authors #else
1690a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_clk_enable(gpu);
1691a8c21a54SThe etnaviv authors #endif
1692bcdfb5e5SRussell King 	if (ret < 0) {
1693a7790d78SLucas Stach 		destroy_workqueue(gpu->wq);
169449b82c38SPhilipp Zabel 		if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
1695bcdfb5e5SRussell King 			thermal_cooling_device_unregister(gpu->cooling);
1696a8c21a54SThe etnaviv authors 		return ret;
1697bcdfb5e5SRussell King 	}
1698a8c21a54SThe etnaviv authors 
1699a8c21a54SThe etnaviv authors 	gpu->drm = drm;
1700f54d1867SChris Wilson 	gpu->fence_context = dma_fence_context_alloc(1);
1701a8c21a54SThe etnaviv authors 	spin_lock_init(&gpu->fence_spinlock);
1702a8c21a54SThe etnaviv authors 
17032f9225dbSLucas Stach 	INIT_LIST_HEAD(&gpu->active_submit_list);
1704a8c21a54SThe etnaviv authors 	INIT_WORK(&gpu->retire_work, retire_worker);
1705357713ceSChristian Gmeiner 	INIT_WORK(&gpu->sync_point_work, sync_point_worker);
1706a8c21a54SThe etnaviv authors 	INIT_WORK(&gpu->recover_work, recover_worker);
1707a8c21a54SThe etnaviv authors 	init_waitqueue_head(&gpu->fence_event);
1708a8c21a54SThe etnaviv authors 
170943b70524SKees Cook 	timer_setup(&gpu->hangcheck_timer, hangcheck_handler, TIMER_DEFERRABLE);
1710a8c21a54SThe etnaviv authors 
1711a8c21a54SThe etnaviv authors 	priv->gpu[priv->num_gpus++] = gpu;
1712a8c21a54SThe etnaviv authors 
1713a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
1714a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
1715a8c21a54SThe etnaviv authors 
1716a8c21a54SThe etnaviv authors 	return 0;
1717a8c21a54SThe etnaviv authors }
1718a8c21a54SThe etnaviv authors 
1719a8c21a54SThe etnaviv authors static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
1720a8c21a54SThe etnaviv authors 	void *data)
1721a8c21a54SThe etnaviv authors {
1722a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1723a8c21a54SThe etnaviv authors 
1724a8c21a54SThe etnaviv authors 	DBG("%s", dev_name(gpu->dev));
1725a8c21a54SThe etnaviv authors 
1726a8c21a54SThe etnaviv authors 	hangcheck_disable(gpu);
1727a8c21a54SThe etnaviv authors 
1728a7790d78SLucas Stach 	flush_workqueue(gpu->wq);
1729a7790d78SLucas Stach 	destroy_workqueue(gpu->wq);
1730a7790d78SLucas Stach 
1731a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1732a8c21a54SThe etnaviv authors 	pm_runtime_get_sync(gpu->dev);
1733a8c21a54SThe etnaviv authors 	pm_runtime_put_sync_suspend(gpu->dev);
1734a8c21a54SThe etnaviv authors #else
1735a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_suspend(gpu);
1736a8c21a54SThe etnaviv authors #endif
1737a8c21a54SThe etnaviv authors 
17382f9225dbSLucas Stach 	if (gpu->buffer.suballoc)
17392f9225dbSLucas Stach 		etnaviv_cmdbuf_free(&gpu->buffer);
1740a8c21a54SThe etnaviv authors 
1741e66774ddSLucas Stach 	if (gpu->cmdbuf_suballoc) {
1742e66774ddSLucas Stach 		etnaviv_cmdbuf_suballoc_destroy(gpu->cmdbuf_suballoc);
1743e66774ddSLucas Stach 		gpu->cmdbuf_suballoc = NULL;
1744e66774ddSLucas Stach 	}
1745e66774ddSLucas Stach 
1746a8c21a54SThe etnaviv authors 	if (gpu->mmu) {
1747a8c21a54SThe etnaviv authors 		etnaviv_iommu_destroy(gpu->mmu);
1748a8c21a54SThe etnaviv authors 		gpu->mmu = NULL;
1749a8c21a54SThe etnaviv authors 	}
1750a8c21a54SThe etnaviv authors 
1751a8c21a54SThe etnaviv authors 	gpu->drm = NULL;
1752bcdfb5e5SRussell King 
175349b82c38SPhilipp Zabel 	if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
1754bcdfb5e5SRussell King 		thermal_cooling_device_unregister(gpu->cooling);
1755bcdfb5e5SRussell King 	gpu->cooling = NULL;
1756a8c21a54SThe etnaviv authors }
1757a8c21a54SThe etnaviv authors 
1758a8c21a54SThe etnaviv authors static const struct component_ops gpu_ops = {
1759a8c21a54SThe etnaviv authors 	.bind = etnaviv_gpu_bind,
1760a8c21a54SThe etnaviv authors 	.unbind = etnaviv_gpu_unbind,
1761a8c21a54SThe etnaviv authors };
1762a8c21a54SThe etnaviv authors 
1763a8c21a54SThe etnaviv authors static const struct of_device_id etnaviv_gpu_match[] = {
1764a8c21a54SThe etnaviv authors 	{
1765a8c21a54SThe etnaviv authors 		.compatible = "vivante,gc"
1766a8c21a54SThe etnaviv authors 	},
1767a8c21a54SThe etnaviv authors 	{ /* sentinel */ }
1768a8c21a54SThe etnaviv authors };
1769a8c21a54SThe etnaviv authors 
1770a8c21a54SThe etnaviv authors static int etnaviv_gpu_platform_probe(struct platform_device *pdev)
1771a8c21a54SThe etnaviv authors {
1772a8c21a54SThe etnaviv authors 	struct device *dev = &pdev->dev;
1773a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
1774dc227890SFabio Estevam 	int err;
1775a8c21a54SThe etnaviv authors 
1776a8c21a54SThe etnaviv authors 	gpu = devm_kzalloc(dev, sizeof(*gpu), GFP_KERNEL);
1777a8c21a54SThe etnaviv authors 	if (!gpu)
1778a8c21a54SThe etnaviv authors 		return -ENOMEM;
1779a8c21a54SThe etnaviv authors 
1780a8c21a54SThe etnaviv authors 	gpu->dev = &pdev->dev;
1781a8c21a54SThe etnaviv authors 	mutex_init(&gpu->lock);
1782a8c21a54SThe etnaviv authors 
1783a8c21a54SThe etnaviv authors 	/* Map registers: */
1784a8c21a54SThe etnaviv authors 	gpu->mmio = etnaviv_ioremap(pdev, NULL, dev_name(gpu->dev));
1785a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->mmio))
1786a8c21a54SThe etnaviv authors 		return PTR_ERR(gpu->mmio);
1787a8c21a54SThe etnaviv authors 
1788a8c21a54SThe etnaviv authors 	/* Get Interrupt: */
1789a8c21a54SThe etnaviv authors 	gpu->irq = platform_get_irq(pdev, 0);
1790a8c21a54SThe etnaviv authors 	if (gpu->irq < 0) {
1791db60eda3SFabio Estevam 		dev_err(dev, "failed to get irq: %d\n", gpu->irq);
1792db60eda3SFabio Estevam 		return gpu->irq;
1793a8c21a54SThe etnaviv authors 	}
1794a8c21a54SThe etnaviv authors 
1795a8c21a54SThe etnaviv authors 	err = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 0,
1796a8c21a54SThe etnaviv authors 			       dev_name(gpu->dev), gpu);
1797a8c21a54SThe etnaviv authors 	if (err) {
1798a8c21a54SThe etnaviv authors 		dev_err(dev, "failed to request IRQ%u: %d\n", gpu->irq, err);
1799db60eda3SFabio Estevam 		return err;
1800a8c21a54SThe etnaviv authors 	}
1801a8c21a54SThe etnaviv authors 
1802a8c21a54SThe etnaviv authors 	/* Get Clocks: */
1803a8c21a54SThe etnaviv authors 	gpu->clk_bus = devm_clk_get(&pdev->dev, "bus");
1804a8c21a54SThe etnaviv authors 	DBG("clk_bus: %p", gpu->clk_bus);
1805a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_bus))
1806a8c21a54SThe etnaviv authors 		gpu->clk_bus = NULL;
1807a8c21a54SThe etnaviv authors 
1808a8c21a54SThe etnaviv authors 	gpu->clk_core = devm_clk_get(&pdev->dev, "core");
1809a8c21a54SThe etnaviv authors 	DBG("clk_core: %p", gpu->clk_core);
1810a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_core))
1811a8c21a54SThe etnaviv authors 		gpu->clk_core = NULL;
1812d79fd1ccSLucas Stach 	gpu->base_rate_core = clk_get_rate(gpu->clk_core);
1813a8c21a54SThe etnaviv authors 
1814a8c21a54SThe etnaviv authors 	gpu->clk_shader = devm_clk_get(&pdev->dev, "shader");
1815a8c21a54SThe etnaviv authors 	DBG("clk_shader: %p", gpu->clk_shader);
1816a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_shader))
1817a8c21a54SThe etnaviv authors 		gpu->clk_shader = NULL;
1818d79fd1ccSLucas Stach 	gpu->base_rate_shader = clk_get_rate(gpu->clk_shader);
1819a8c21a54SThe etnaviv authors 
1820a8c21a54SThe etnaviv authors 	/* TODO: figure out max mapped size */
1821a8c21a54SThe etnaviv authors 	dev_set_drvdata(dev, gpu);
1822a8c21a54SThe etnaviv authors 
1823a8c21a54SThe etnaviv authors 	/*
1824a8c21a54SThe etnaviv authors 	 * We treat the device as initially suspended.  The runtime PM
1825a8c21a54SThe etnaviv authors 	 * autosuspend delay is rather arbitary: no measurements have
1826a8c21a54SThe etnaviv authors 	 * yet been performed to determine an appropriate value.
1827a8c21a54SThe etnaviv authors 	 */
1828a8c21a54SThe etnaviv authors 	pm_runtime_use_autosuspend(gpu->dev);
1829a8c21a54SThe etnaviv authors 	pm_runtime_set_autosuspend_delay(gpu->dev, 200);
1830a8c21a54SThe etnaviv authors 	pm_runtime_enable(gpu->dev);
1831a8c21a54SThe etnaviv authors 
1832a8c21a54SThe etnaviv authors 	err = component_add(&pdev->dev, &gpu_ops);
1833a8c21a54SThe etnaviv authors 	if (err < 0) {
1834a8c21a54SThe etnaviv authors 		dev_err(&pdev->dev, "failed to register component: %d\n", err);
1835db60eda3SFabio Estevam 		return err;
1836a8c21a54SThe etnaviv authors 	}
1837a8c21a54SThe etnaviv authors 
1838a8c21a54SThe etnaviv authors 	return 0;
1839a8c21a54SThe etnaviv authors }
1840a8c21a54SThe etnaviv authors 
1841a8c21a54SThe etnaviv authors static int etnaviv_gpu_platform_remove(struct platform_device *pdev)
1842a8c21a54SThe etnaviv authors {
1843a8c21a54SThe etnaviv authors 	component_del(&pdev->dev, &gpu_ops);
1844a8c21a54SThe etnaviv authors 	pm_runtime_disable(&pdev->dev);
1845a8c21a54SThe etnaviv authors 	return 0;
1846a8c21a54SThe etnaviv authors }
1847a8c21a54SThe etnaviv authors 
1848a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1849a8c21a54SThe etnaviv authors static int etnaviv_gpu_rpm_suspend(struct device *dev)
1850a8c21a54SThe etnaviv authors {
1851a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1852a8c21a54SThe etnaviv authors 	u32 idle, mask;
1853a8c21a54SThe etnaviv authors 
1854a8c21a54SThe etnaviv authors 	/* If we have outstanding fences, we're not idle */
1855a8c21a54SThe etnaviv authors 	if (gpu->completed_fence != gpu->active_fence)
1856a8c21a54SThe etnaviv authors 		return -EBUSY;
1857a8c21a54SThe etnaviv authors 
1858a8c21a54SThe etnaviv authors 	/* Check whether the hardware (except FE) is idle */
1859a8c21a54SThe etnaviv authors 	mask = gpu->idle_mask & ~VIVS_HI_IDLE_STATE_FE;
1860a8c21a54SThe etnaviv authors 	idle = gpu_read(gpu, VIVS_HI_IDLE_STATE) & mask;
1861a8c21a54SThe etnaviv authors 	if (idle != mask)
1862a8c21a54SThe etnaviv authors 		return -EBUSY;
1863a8c21a54SThe etnaviv authors 
1864a8c21a54SThe etnaviv authors 	return etnaviv_gpu_hw_suspend(gpu);
1865a8c21a54SThe etnaviv authors }
1866a8c21a54SThe etnaviv authors 
1867a8c21a54SThe etnaviv authors static int etnaviv_gpu_rpm_resume(struct device *dev)
1868a8c21a54SThe etnaviv authors {
1869a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1870a8c21a54SThe etnaviv authors 	int ret;
1871a8c21a54SThe etnaviv authors 
1872a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_clk_enable(gpu);
1873a8c21a54SThe etnaviv authors 	if (ret)
1874a8c21a54SThe etnaviv authors 		return ret;
1875a8c21a54SThe etnaviv authors 
1876a8c21a54SThe etnaviv authors 	/* Re-initialise the basic hardware state */
18772f9225dbSLucas Stach 	if (gpu->drm && gpu->buffer.suballoc) {
1878a8c21a54SThe etnaviv authors 		ret = etnaviv_gpu_hw_resume(gpu);
1879a8c21a54SThe etnaviv authors 		if (ret) {
1880a8c21a54SThe etnaviv authors 			etnaviv_gpu_clk_disable(gpu);
1881a8c21a54SThe etnaviv authors 			return ret;
1882a8c21a54SThe etnaviv authors 		}
1883a8c21a54SThe etnaviv authors 	}
1884a8c21a54SThe etnaviv authors 
1885a8c21a54SThe etnaviv authors 	return 0;
1886a8c21a54SThe etnaviv authors }
1887a8c21a54SThe etnaviv authors #endif
1888a8c21a54SThe etnaviv authors 
1889a8c21a54SThe etnaviv authors static const struct dev_pm_ops etnaviv_gpu_pm_ops = {
1890a8c21a54SThe etnaviv authors 	SET_RUNTIME_PM_OPS(etnaviv_gpu_rpm_suspend, etnaviv_gpu_rpm_resume,
1891a8c21a54SThe etnaviv authors 			   NULL)
1892a8c21a54SThe etnaviv authors };
1893a8c21a54SThe etnaviv authors 
1894a8c21a54SThe etnaviv authors struct platform_driver etnaviv_gpu_driver = {
1895a8c21a54SThe etnaviv authors 	.driver = {
1896a8c21a54SThe etnaviv authors 		.name = "etnaviv-gpu",
1897a8c21a54SThe etnaviv authors 		.owner = THIS_MODULE,
1898a8c21a54SThe etnaviv authors 		.pm = &etnaviv_gpu_pm_ops,
1899a8c21a54SThe etnaviv authors 		.of_match_table = etnaviv_gpu_match,
1900a8c21a54SThe etnaviv authors 	},
1901a8c21a54SThe etnaviv authors 	.probe = etnaviv_gpu_platform_probe,
1902a8c21a54SThe etnaviv authors 	.remove = etnaviv_gpu_platform_remove,
1903a8c21a54SThe etnaviv authors 	.id_table = gpu_ids,
1904a8c21a54SThe etnaviv authors };
1905