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"
29e93b6deeSLucas Stach #include "etnaviv_sched.h"
30a8c21a54SThe etnaviv authors #include "common.xml.h"
31a8c21a54SThe etnaviv authors #include "state.xml.h"
32a8c21a54SThe etnaviv authors #include "state_hi.xml.h"
33a8c21a54SThe etnaviv authors #include "cmdstream.xml.h"
34a8c21a54SThe etnaviv authors 
35c09d7f79SLucas Stach #ifndef PHYS_OFFSET
36c09d7f79SLucas Stach #define PHYS_OFFSET 0
37c09d7f79SLucas Stach #endif
38c09d7f79SLucas Stach 
39a8c21a54SThe etnaviv authors static const struct platform_device_id gpu_ids[] = {
40a8c21a54SThe etnaviv authors 	{ .name = "etnaviv-gpu,2d" },
41a8c21a54SThe etnaviv authors 	{ },
42a8c21a54SThe etnaviv authors };
43a8c21a54SThe etnaviv authors 
44a8c21a54SThe etnaviv authors /*
45a8c21a54SThe etnaviv authors  * Driver functions:
46a8c21a54SThe etnaviv authors  */
47a8c21a54SThe etnaviv authors 
48a8c21a54SThe etnaviv authors int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value)
49a8c21a54SThe etnaviv authors {
50a8c21a54SThe etnaviv authors 	switch (param) {
51a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_MODEL:
52a8c21a54SThe etnaviv authors 		*value = gpu->identity.model;
53a8c21a54SThe etnaviv authors 		break;
54a8c21a54SThe etnaviv authors 
55a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_REVISION:
56a8c21a54SThe etnaviv authors 		*value = gpu->identity.revision;
57a8c21a54SThe etnaviv authors 		break;
58a8c21a54SThe etnaviv authors 
59a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_0:
60a8c21a54SThe etnaviv authors 		*value = gpu->identity.features;
61a8c21a54SThe etnaviv authors 		break;
62a8c21a54SThe etnaviv authors 
63a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_1:
64a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features0;
65a8c21a54SThe etnaviv authors 		break;
66a8c21a54SThe etnaviv authors 
67a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_2:
68a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features1;
69a8c21a54SThe etnaviv authors 		break;
70a8c21a54SThe etnaviv authors 
71a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_3:
72a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features2;
73a8c21a54SThe etnaviv authors 		break;
74a8c21a54SThe etnaviv authors 
75a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_4:
76a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features3;
77a8c21a54SThe etnaviv authors 		break;
78a8c21a54SThe etnaviv authors 
79602eb489SRussell King 	case ETNAVIV_PARAM_GPU_FEATURES_5:
80602eb489SRussell King 		*value = gpu->identity.minor_features4;
81602eb489SRussell King 		break;
82602eb489SRussell King 
83602eb489SRussell King 	case ETNAVIV_PARAM_GPU_FEATURES_6:
84602eb489SRussell King 		*value = gpu->identity.minor_features5;
85602eb489SRussell King 		break;
86602eb489SRussell King 
870538aaf9SLucas Stach 	case ETNAVIV_PARAM_GPU_FEATURES_7:
880538aaf9SLucas Stach 		*value = gpu->identity.minor_features6;
890538aaf9SLucas Stach 		break;
900538aaf9SLucas Stach 
910538aaf9SLucas Stach 	case ETNAVIV_PARAM_GPU_FEATURES_8:
920538aaf9SLucas Stach 		*value = gpu->identity.minor_features7;
930538aaf9SLucas Stach 		break;
940538aaf9SLucas Stach 
950538aaf9SLucas Stach 	case ETNAVIV_PARAM_GPU_FEATURES_9:
960538aaf9SLucas Stach 		*value = gpu->identity.minor_features8;
970538aaf9SLucas Stach 		break;
980538aaf9SLucas Stach 
990538aaf9SLucas Stach 	case ETNAVIV_PARAM_GPU_FEATURES_10:
1000538aaf9SLucas Stach 		*value = gpu->identity.minor_features9;
1010538aaf9SLucas Stach 		break;
1020538aaf9SLucas Stach 
1030538aaf9SLucas Stach 	case ETNAVIV_PARAM_GPU_FEATURES_11:
1040538aaf9SLucas Stach 		*value = gpu->identity.minor_features10;
1050538aaf9SLucas Stach 		break;
1060538aaf9SLucas Stach 
1070538aaf9SLucas Stach 	case ETNAVIV_PARAM_GPU_FEATURES_12:
1080538aaf9SLucas Stach 		*value = gpu->identity.minor_features11;
1090538aaf9SLucas Stach 		break;
1100538aaf9SLucas Stach 
111a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_STREAM_COUNT:
112a8c21a54SThe etnaviv authors 		*value = gpu->identity.stream_count;
113a8c21a54SThe etnaviv authors 		break;
114a8c21a54SThe etnaviv authors 
115a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_REGISTER_MAX:
116a8c21a54SThe etnaviv authors 		*value = gpu->identity.register_max;
117a8c21a54SThe etnaviv authors 		break;
118a8c21a54SThe etnaviv authors 
119a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_THREAD_COUNT:
120a8c21a54SThe etnaviv authors 		*value = gpu->identity.thread_count;
121a8c21a54SThe etnaviv authors 		break;
122a8c21a54SThe etnaviv authors 
123a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_VERTEX_CACHE_SIZE:
124a8c21a54SThe etnaviv authors 		*value = gpu->identity.vertex_cache_size;
125a8c21a54SThe etnaviv authors 		break;
126a8c21a54SThe etnaviv authors 
127a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_SHADER_CORE_COUNT:
128a8c21a54SThe etnaviv authors 		*value = gpu->identity.shader_core_count;
129a8c21a54SThe etnaviv authors 		break;
130a8c21a54SThe etnaviv authors 
131a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_PIXEL_PIPES:
132a8c21a54SThe etnaviv authors 		*value = gpu->identity.pixel_pipes;
133a8c21a54SThe etnaviv authors 		break;
134a8c21a54SThe etnaviv authors 
135a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_VERTEX_OUTPUT_BUFFER_SIZE:
136a8c21a54SThe etnaviv authors 		*value = gpu->identity.vertex_output_buffer_size;
137a8c21a54SThe etnaviv authors 		break;
138a8c21a54SThe etnaviv authors 
139a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_BUFFER_SIZE:
140a8c21a54SThe etnaviv authors 		*value = gpu->identity.buffer_size;
141a8c21a54SThe etnaviv authors 		break;
142a8c21a54SThe etnaviv authors 
143a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_INSTRUCTION_COUNT:
144a8c21a54SThe etnaviv authors 		*value = gpu->identity.instruction_count;
145a8c21a54SThe etnaviv authors 		break;
146a8c21a54SThe etnaviv authors 
147a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_NUM_CONSTANTS:
148a8c21a54SThe etnaviv authors 		*value = gpu->identity.num_constants;
149a8c21a54SThe etnaviv authors 		break;
150a8c21a54SThe etnaviv authors 
151602eb489SRussell King 	case ETNAVIV_PARAM_GPU_NUM_VARYINGS:
152602eb489SRussell King 		*value = gpu->identity.varyings_count;
153602eb489SRussell King 		break;
154602eb489SRussell King 
155a8c21a54SThe etnaviv authors 	default:
156a8c21a54SThe etnaviv authors 		DBG("%s: invalid param: %u", dev_name(gpu->dev), param);
157a8c21a54SThe etnaviv authors 		return -EINVAL;
158a8c21a54SThe etnaviv authors 	}
159a8c21a54SThe etnaviv authors 
160a8c21a54SThe etnaviv authors 	return 0;
161a8c21a54SThe etnaviv authors }
162a8c21a54SThe etnaviv authors 
163472f79dcSRussell King 
164472f79dcSRussell King #define etnaviv_is_model_rev(gpu, mod, rev) \
165472f79dcSRussell King 	((gpu)->identity.model == chipModel_##mod && \
166472f79dcSRussell King 	 (gpu)->identity.revision == rev)
16752f36ba1SRussell King #define etnaviv_field(val, field) \
16852f36ba1SRussell King 	(((val) & field##__MASK) >> field##__SHIFT)
16952f36ba1SRussell King 
170a8c21a54SThe etnaviv authors static void etnaviv_hw_specs(struct etnaviv_gpu *gpu)
171a8c21a54SThe etnaviv authors {
172a8c21a54SThe etnaviv authors 	if (gpu->identity.minor_features0 &
173a8c21a54SThe etnaviv authors 	    chipMinorFeatures0_MORE_MINOR_FEATURES) {
174602eb489SRussell King 		u32 specs[4];
175602eb489SRussell King 		unsigned int streams;
176a8c21a54SThe etnaviv authors 
177a8c21a54SThe etnaviv authors 		specs[0] = gpu_read(gpu, VIVS_HI_CHIP_SPECS);
178a8c21a54SThe etnaviv authors 		specs[1] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_2);
179602eb489SRussell King 		specs[2] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_3);
180602eb489SRussell King 		specs[3] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_4);
181a8c21a54SThe etnaviv authors 
18252f36ba1SRussell King 		gpu->identity.stream_count = etnaviv_field(specs[0],
18352f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_STREAM_COUNT);
18452f36ba1SRussell King 		gpu->identity.register_max = etnaviv_field(specs[0],
18552f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_REGISTER_MAX);
18652f36ba1SRussell King 		gpu->identity.thread_count = etnaviv_field(specs[0],
18752f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_THREAD_COUNT);
18852f36ba1SRussell King 		gpu->identity.vertex_cache_size = etnaviv_field(specs[0],
18952f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_VERTEX_CACHE_SIZE);
19052f36ba1SRussell King 		gpu->identity.shader_core_count = etnaviv_field(specs[0],
19152f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_SHADER_CORE_COUNT);
19252f36ba1SRussell King 		gpu->identity.pixel_pipes = etnaviv_field(specs[0],
19352f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_PIXEL_PIPES);
194a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size =
19552f36ba1SRussell King 			etnaviv_field(specs[0],
19652f36ba1SRussell King 				VIVS_HI_CHIP_SPECS_VERTEX_OUTPUT_BUFFER_SIZE);
197a8c21a54SThe etnaviv authors 
19852f36ba1SRussell King 		gpu->identity.buffer_size = etnaviv_field(specs[1],
19952f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_BUFFER_SIZE);
20052f36ba1SRussell King 		gpu->identity.instruction_count = etnaviv_field(specs[1],
20152f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_INSTRUCTION_COUNT);
20252f36ba1SRussell King 		gpu->identity.num_constants = etnaviv_field(specs[1],
20352f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_NUM_CONSTANTS);
204602eb489SRussell King 
205602eb489SRussell King 		gpu->identity.varyings_count = etnaviv_field(specs[2],
206602eb489SRussell King 					VIVS_HI_CHIP_SPECS_3_VARYINGS_COUNT);
207602eb489SRussell King 
208602eb489SRussell King 		/* This overrides the value from older register if non-zero */
209602eb489SRussell King 		streams = etnaviv_field(specs[3],
210602eb489SRussell King 					VIVS_HI_CHIP_SPECS_4_STREAM_COUNT);
211602eb489SRussell King 		if (streams)
212602eb489SRussell King 			gpu->identity.stream_count = streams;
213a8c21a54SThe etnaviv authors 	}
214a8c21a54SThe etnaviv authors 
215a8c21a54SThe etnaviv authors 	/* Fill in the stream count if not specified */
216a8c21a54SThe etnaviv authors 	if (gpu->identity.stream_count == 0) {
217a8c21a54SThe etnaviv authors 		if (gpu->identity.model >= 0x1000)
218a8c21a54SThe etnaviv authors 			gpu->identity.stream_count = 4;
219a8c21a54SThe etnaviv authors 		else
220a8c21a54SThe etnaviv authors 			gpu->identity.stream_count = 1;
221a8c21a54SThe etnaviv authors 	}
222a8c21a54SThe etnaviv authors 
223a8c21a54SThe etnaviv authors 	/* Convert the register max value */
224a8c21a54SThe etnaviv authors 	if (gpu->identity.register_max)
225a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 1 << gpu->identity.register_max;
226507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC400)
227a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 32;
228a8c21a54SThe etnaviv authors 	else
229a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 64;
230a8c21a54SThe etnaviv authors 
231a8c21a54SThe etnaviv authors 	/* Convert thread count */
232a8c21a54SThe etnaviv authors 	if (gpu->identity.thread_count)
233a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 1 << gpu->identity.thread_count;
234507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC400)
235a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 64;
236507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC500 ||
237507f8991SRussell King 		 gpu->identity.model == chipModel_GC530)
238a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 128;
239a8c21a54SThe etnaviv authors 	else
240a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 256;
241a8c21a54SThe etnaviv authors 
242a8c21a54SThe etnaviv authors 	if (gpu->identity.vertex_cache_size == 0)
243a8c21a54SThe etnaviv authors 		gpu->identity.vertex_cache_size = 8;
244a8c21a54SThe etnaviv authors 
245a8c21a54SThe etnaviv authors 	if (gpu->identity.shader_core_count == 0) {
246a8c21a54SThe etnaviv authors 		if (gpu->identity.model >= 0x1000)
247a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count = 2;
248a8c21a54SThe etnaviv authors 		else
249a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count = 1;
250a8c21a54SThe etnaviv authors 	}
251a8c21a54SThe etnaviv authors 
252a8c21a54SThe etnaviv authors 	if (gpu->identity.pixel_pipes == 0)
253a8c21a54SThe etnaviv authors 		gpu->identity.pixel_pipes = 1;
254a8c21a54SThe etnaviv authors 
255a8c21a54SThe etnaviv authors 	/* Convert virtex buffer size */
256a8c21a54SThe etnaviv authors 	if (gpu->identity.vertex_output_buffer_size) {
257a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size =
258a8c21a54SThe etnaviv authors 			1 << gpu->identity.vertex_output_buffer_size;
259507f8991SRussell King 	} else if (gpu->identity.model == chipModel_GC400) {
260a8c21a54SThe etnaviv authors 		if (gpu->identity.revision < 0x4000)
261a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 512;
262a8c21a54SThe etnaviv authors 		else if (gpu->identity.revision < 0x4200)
263a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 256;
264a8c21a54SThe etnaviv authors 		else
265a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 128;
266a8c21a54SThe etnaviv authors 	} else {
267a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size = 512;
268a8c21a54SThe etnaviv authors 	}
269a8c21a54SThe etnaviv authors 
270a8c21a54SThe etnaviv authors 	switch (gpu->identity.instruction_count) {
271a8c21a54SThe etnaviv authors 	case 0:
272472f79dcSRussell King 		if (etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
273507f8991SRussell King 		    gpu->identity.model == chipModel_GC880)
274a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count = 512;
275a8c21a54SThe etnaviv authors 		else
276a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count = 256;
277a8c21a54SThe etnaviv authors 		break;
278a8c21a54SThe etnaviv authors 
279a8c21a54SThe etnaviv authors 	case 1:
280a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 1024;
281a8c21a54SThe etnaviv authors 		break;
282a8c21a54SThe etnaviv authors 
283a8c21a54SThe etnaviv authors 	case 2:
284a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 2048;
285a8c21a54SThe etnaviv authors 		break;
286a8c21a54SThe etnaviv authors 
287a8c21a54SThe etnaviv authors 	default:
288a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 256;
289a8c21a54SThe etnaviv authors 		break;
290a8c21a54SThe etnaviv authors 	}
291a8c21a54SThe etnaviv authors 
292a8c21a54SThe etnaviv authors 	if (gpu->identity.num_constants == 0)
293a8c21a54SThe etnaviv authors 		gpu->identity.num_constants = 168;
294602eb489SRussell King 
295602eb489SRussell King 	if (gpu->identity.varyings_count == 0) {
296602eb489SRussell King 		if (gpu->identity.minor_features1 & chipMinorFeatures1_HALTI0)
297602eb489SRussell King 			gpu->identity.varyings_count = 12;
298602eb489SRussell King 		else
299602eb489SRussell King 			gpu->identity.varyings_count = 8;
300602eb489SRussell King 	}
301602eb489SRussell King 
302602eb489SRussell King 	/*
303602eb489SRussell King 	 * For some cores, two varyings are consumed for position, so the
304602eb489SRussell King 	 * maximum varying count needs to be reduced by one.
305602eb489SRussell King 	 */
306602eb489SRussell King 	if (etnaviv_is_model_rev(gpu, GC5000, 0x5434) ||
307602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
308602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5245) ||
309602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
310602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC3000, 0x5435) ||
311602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2200, 0x5244) ||
312602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2100, 0x5108) ||
313602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
314602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC1500, 0x5246) ||
315602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC880, 0x5107) ||
316602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC880, 0x5106))
317602eb489SRussell King 		gpu->identity.varyings_count -= 1;
318a8c21a54SThe etnaviv authors }
319a8c21a54SThe etnaviv authors 
320a8c21a54SThe etnaviv authors static void etnaviv_hw_identify(struct etnaviv_gpu *gpu)
321a8c21a54SThe etnaviv authors {
322a8c21a54SThe etnaviv authors 	u32 chipIdentity;
323a8c21a54SThe etnaviv authors 
324a8c21a54SThe etnaviv authors 	chipIdentity = gpu_read(gpu, VIVS_HI_CHIP_IDENTITY);
325a8c21a54SThe etnaviv authors 
326a8c21a54SThe etnaviv authors 	/* Special case for older graphic cores. */
32752f36ba1SRussell King 	if (etnaviv_field(chipIdentity, VIVS_HI_CHIP_IDENTITY_FAMILY) == 0x01) {
328507f8991SRussell King 		gpu->identity.model    = chipModel_GC500;
32952f36ba1SRussell King 		gpu->identity.revision = etnaviv_field(chipIdentity,
33052f36ba1SRussell King 					 VIVS_HI_CHIP_IDENTITY_REVISION);
331a8c21a54SThe etnaviv authors 	} else {
332a8c21a54SThe etnaviv authors 
333a8c21a54SThe etnaviv authors 		gpu->identity.model = gpu_read(gpu, VIVS_HI_CHIP_MODEL);
334a8c21a54SThe etnaviv authors 		gpu->identity.revision = gpu_read(gpu, VIVS_HI_CHIP_REV);
335a8c21a54SThe etnaviv authors 
336a8c21a54SThe etnaviv authors 		/*
337a8c21a54SThe etnaviv authors 		 * !!!! HACK ALERT !!!!
338a8c21a54SThe etnaviv authors 		 * Because people change device IDs without letting software
339a8c21a54SThe etnaviv authors 		 * know about it - here is the hack to make it all look the
340a8c21a54SThe etnaviv authors 		 * same.  Only for GC400 family.
341a8c21a54SThe etnaviv authors 		 */
342a8c21a54SThe etnaviv authors 		if ((gpu->identity.model & 0xff00) == 0x0400 &&
343507f8991SRussell King 		    gpu->identity.model != chipModel_GC420) {
344a8c21a54SThe etnaviv authors 			gpu->identity.model = gpu->identity.model & 0x0400;
345a8c21a54SThe etnaviv authors 		}
346a8c21a54SThe etnaviv authors 
347a8c21a54SThe etnaviv authors 		/* Another special case */
348472f79dcSRussell King 		if (etnaviv_is_model_rev(gpu, GC300, 0x2201)) {
349a8c21a54SThe etnaviv authors 			u32 chipDate = gpu_read(gpu, VIVS_HI_CHIP_DATE);
350a8c21a54SThe etnaviv authors 			u32 chipTime = gpu_read(gpu, VIVS_HI_CHIP_TIME);
351a8c21a54SThe etnaviv authors 
352a8c21a54SThe etnaviv authors 			if (chipDate == 0x20080814 && chipTime == 0x12051100) {
353a8c21a54SThe etnaviv authors 				/*
354a8c21a54SThe etnaviv authors 				 * This IP has an ECO; put the correct
355a8c21a54SThe etnaviv authors 				 * revision in it.
356a8c21a54SThe etnaviv authors 				 */
357a8c21a54SThe etnaviv authors 				gpu->identity.revision = 0x1051;
358a8c21a54SThe etnaviv authors 			}
359a8c21a54SThe etnaviv authors 		}
36012ff4bdeSLucas Stach 
36112ff4bdeSLucas Stach 		/*
36212ff4bdeSLucas Stach 		 * NXP likes to call the GPU on the i.MX6QP GC2000+, but in
36312ff4bdeSLucas Stach 		 * reality it's just a re-branded GC3000. We can identify this
36412ff4bdeSLucas Stach 		 * core by the upper half of the revision register being all 1.
36512ff4bdeSLucas Stach 		 * Fix model/rev here, so all other places can refer to this
36612ff4bdeSLucas Stach 		 * core by its real identity.
36712ff4bdeSLucas Stach 		 */
36812ff4bdeSLucas Stach 		if (etnaviv_is_model_rev(gpu, GC2000, 0xffff5450)) {
36912ff4bdeSLucas Stach 			gpu->identity.model = chipModel_GC3000;
37012ff4bdeSLucas Stach 			gpu->identity.revision &= 0xffff;
37112ff4bdeSLucas Stach 		}
372a8c21a54SThe etnaviv authors 	}
373a8c21a54SThe etnaviv authors 
374a8c21a54SThe etnaviv authors 	dev_info(gpu->dev, "model: GC%x, revision: %x\n",
375a8c21a54SThe etnaviv authors 		 gpu->identity.model, gpu->identity.revision);
376a8c21a54SThe etnaviv authors 
377681c19c8SLucas Stach 	/*
378681c19c8SLucas Stach 	 * If there is a match in the HWDB, we aren't interested in the
379681c19c8SLucas Stach 	 * remaining register values, as they might be wrong.
380681c19c8SLucas Stach 	 */
381681c19c8SLucas Stach 	if (etnaviv_fill_identity_from_hwdb(gpu))
382681c19c8SLucas Stach 		return;
383681c19c8SLucas Stach 
384a8c21a54SThe etnaviv authors 	gpu->identity.features = gpu_read(gpu, VIVS_HI_CHIP_FEATURE);
385a8c21a54SThe etnaviv authors 
386a8c21a54SThe etnaviv authors 	/* Disable fast clear on GC700. */
387507f8991SRussell King 	if (gpu->identity.model == chipModel_GC700)
388a8c21a54SThe etnaviv authors 		gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
389a8c21a54SThe etnaviv authors 
390507f8991SRussell King 	if ((gpu->identity.model == chipModel_GC500 &&
391507f8991SRussell King 	     gpu->identity.revision < 2) ||
392507f8991SRussell King 	    (gpu->identity.model == chipModel_GC300 &&
393507f8991SRussell King 	     gpu->identity.revision < 0x2000)) {
394a8c21a54SThe etnaviv authors 
395a8c21a54SThe etnaviv authors 		/*
396a8c21a54SThe etnaviv authors 		 * GC500 rev 1.x and GC300 rev < 2.0 doesn't have these
397a8c21a54SThe etnaviv authors 		 * registers.
398a8c21a54SThe etnaviv authors 		 */
399a8c21a54SThe etnaviv authors 		gpu->identity.minor_features0 = 0;
400a8c21a54SThe etnaviv authors 		gpu->identity.minor_features1 = 0;
401a8c21a54SThe etnaviv authors 		gpu->identity.minor_features2 = 0;
402a8c21a54SThe etnaviv authors 		gpu->identity.minor_features3 = 0;
403602eb489SRussell King 		gpu->identity.minor_features4 = 0;
404602eb489SRussell King 		gpu->identity.minor_features5 = 0;
405a8c21a54SThe etnaviv authors 	} else
406a8c21a54SThe etnaviv authors 		gpu->identity.minor_features0 =
407a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_0);
408a8c21a54SThe etnaviv authors 
409a8c21a54SThe etnaviv authors 	if (gpu->identity.minor_features0 &
410a8c21a54SThe etnaviv authors 	    chipMinorFeatures0_MORE_MINOR_FEATURES) {
411a8c21a54SThe etnaviv authors 		gpu->identity.minor_features1 =
412a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_1);
413a8c21a54SThe etnaviv authors 		gpu->identity.minor_features2 =
414a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_2);
415a8c21a54SThe etnaviv authors 		gpu->identity.minor_features3 =
416a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_3);
417602eb489SRussell King 		gpu->identity.minor_features4 =
418602eb489SRussell King 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_4);
419602eb489SRussell King 		gpu->identity.minor_features5 =
420602eb489SRussell King 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_5);
421a8c21a54SThe etnaviv authors 	}
422a8c21a54SThe etnaviv authors 
423a8c21a54SThe etnaviv authors 	/* GC600 idle register reports zero bits where modules aren't present */
424a8c21a54SThe etnaviv authors 	if (gpu->identity.model == chipModel_GC600) {
425a8c21a54SThe etnaviv authors 		gpu->idle_mask = VIVS_HI_IDLE_STATE_TX |
426a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_RA |
427a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_SE |
428a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_PA |
429a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_SH |
430a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_PE |
431a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_DE |
432a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_FE;
433a8c21a54SThe etnaviv authors 	} else {
434a8c21a54SThe etnaviv authors 		gpu->idle_mask = ~VIVS_HI_IDLE_STATE_AXI_LP;
435a8c21a54SThe etnaviv authors 	}
436a8c21a54SThe etnaviv authors 
437a8c21a54SThe etnaviv authors 	etnaviv_hw_specs(gpu);
438a8c21a54SThe etnaviv authors }
439a8c21a54SThe etnaviv authors 
440a8c21a54SThe etnaviv authors static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
441a8c21a54SThe etnaviv authors {
442a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock |
443a8c21a54SThe etnaviv authors 		  VIVS_HI_CLOCK_CONTROL_FSCALE_CMD_LOAD);
444a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
445a8c21a54SThe etnaviv authors }
446a8c21a54SThe etnaviv authors 
447bcdfb5e5SRussell King static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu)
448bcdfb5e5SRussell King {
449d79fd1ccSLucas Stach 	if (gpu->identity.minor_features2 &
450d79fd1ccSLucas Stach 	    chipMinorFeatures2_DYNAMIC_FREQUENCY_SCALING) {
451d79fd1ccSLucas Stach 		clk_set_rate(gpu->clk_core,
452d79fd1ccSLucas Stach 			     gpu->base_rate_core >> gpu->freq_scale);
453d79fd1ccSLucas Stach 		clk_set_rate(gpu->clk_shader,
454d79fd1ccSLucas Stach 			     gpu->base_rate_shader >> gpu->freq_scale);
455d79fd1ccSLucas Stach 	} else {
456bcdfb5e5SRussell King 		unsigned int fscale = 1 << (6 - gpu->freq_scale);
4576eb3ecc3SLucas Stach 		u32 clock = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
458bcdfb5e5SRussell King 
4596eb3ecc3SLucas Stach 		clock &= ~VIVS_HI_CLOCK_CONTROL_FSCALE_VAL__MASK;
4606eb3ecc3SLucas Stach 		clock |= VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
461bcdfb5e5SRussell King 		etnaviv_gpu_load_clock(gpu, clock);
462bcdfb5e5SRussell King 	}
463d79fd1ccSLucas Stach }
464bcdfb5e5SRussell King 
465a8c21a54SThe etnaviv authors static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
466a8c21a54SThe etnaviv authors {
467a8c21a54SThe etnaviv authors 	u32 control, idle;
468a8c21a54SThe etnaviv authors 	unsigned long timeout;
469a8c21a54SThe etnaviv authors 	bool failed = true;
470a8c21a54SThe etnaviv authors 
471a8c21a54SThe etnaviv authors 	/* We hope that the GPU resets in under one second */
472a8c21a54SThe etnaviv authors 	timeout = jiffies + msecs_to_jiffies(1000);
473a8c21a54SThe etnaviv authors 
474a8c21a54SThe etnaviv authors 	while (time_is_after_jiffies(timeout)) {
475a8c21a54SThe etnaviv authors 		/* enable clock */
4766eb3ecc3SLucas Stach 		unsigned int fscale = 1 << (6 - gpu->freq_scale);
4776eb3ecc3SLucas Stach 		control = VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
4786eb3ecc3SLucas Stach 		etnaviv_gpu_load_clock(gpu, control);
479a8c21a54SThe etnaviv authors 
480a8c21a54SThe etnaviv authors 		/* isolate the GPU. */
481a8c21a54SThe etnaviv authors 		control |= VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
482a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
483a8c21a54SThe etnaviv authors 
484c997c3dfSLucas Stach 		if (gpu->sec_mode == ETNA_SEC_KERNEL) {
485c997c3dfSLucas Stach 			gpu_write(gpu, VIVS_MMUv2_AHB_CONTROL,
486c997c3dfSLucas Stach 			          VIVS_MMUv2_AHB_CONTROL_RESET);
487c997c3dfSLucas Stach 		} else {
488a8c21a54SThe etnaviv authors 			/* set soft reset. */
489a8c21a54SThe etnaviv authors 			control |= VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
490a8c21a54SThe etnaviv authors 			gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
491c997c3dfSLucas Stach 		}
492a8c21a54SThe etnaviv authors 
493a8c21a54SThe etnaviv authors 		/* wait for reset. */
49440462179SPhilipp Zabel 		usleep_range(10, 20);
495a8c21a54SThe etnaviv authors 
496a8c21a54SThe etnaviv authors 		/* reset soft reset bit. */
497a8c21a54SThe etnaviv authors 		control &= ~VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
498a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
499a8c21a54SThe etnaviv authors 
500a8c21a54SThe etnaviv authors 		/* reset GPU isolation. */
501a8c21a54SThe etnaviv authors 		control &= ~VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
502a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
503a8c21a54SThe etnaviv authors 
504a8c21a54SThe etnaviv authors 		/* read idle register. */
505a8c21a54SThe etnaviv authors 		idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
506a8c21a54SThe etnaviv authors 
507a8c21a54SThe etnaviv authors 		/* try reseting again if FE it not idle */
508a8c21a54SThe etnaviv authors 		if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) {
509a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "FE is not idle\n");
510a8c21a54SThe etnaviv authors 			continue;
511a8c21a54SThe etnaviv authors 		}
512a8c21a54SThe etnaviv authors 
513a8c21a54SThe etnaviv authors 		/* read reset register. */
514a8c21a54SThe etnaviv authors 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
515a8c21a54SThe etnaviv authors 
516a8c21a54SThe etnaviv authors 		/* is the GPU idle? */
517a8c21a54SThe etnaviv authors 		if (((control & VIVS_HI_CLOCK_CONTROL_IDLE_3D) == 0) ||
518a8c21a54SThe etnaviv authors 		    ((control & VIVS_HI_CLOCK_CONTROL_IDLE_2D) == 0)) {
519a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "GPU is not idle\n");
520a8c21a54SThe etnaviv authors 			continue;
521a8c21a54SThe etnaviv authors 		}
522a8c21a54SThe etnaviv authors 
5236eb3ecc3SLucas Stach 		/* disable debug registers, as they are not normally needed */
5246eb3ecc3SLucas Stach 		control |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
5256eb3ecc3SLucas Stach 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
5266eb3ecc3SLucas Stach 
527a8c21a54SThe etnaviv authors 		failed = false;
528a8c21a54SThe etnaviv authors 		break;
529a8c21a54SThe etnaviv authors 	}
530a8c21a54SThe etnaviv authors 
531a8c21a54SThe etnaviv authors 	if (failed) {
532a8c21a54SThe etnaviv authors 		idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
533a8c21a54SThe etnaviv authors 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
534a8c21a54SThe etnaviv authors 
535a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "GPU failed to reset: FE %sidle, 3D %sidle, 2D %sidle\n",
536a8c21a54SThe etnaviv authors 			idle & VIVS_HI_IDLE_STATE_FE ? "" : "not ",
537a8c21a54SThe etnaviv authors 			control & VIVS_HI_CLOCK_CONTROL_IDLE_3D ? "" : "not ",
538a8c21a54SThe etnaviv authors 			control & VIVS_HI_CLOCK_CONTROL_IDLE_2D ? "" : "not ");
539a8c21a54SThe etnaviv authors 
540a8c21a54SThe etnaviv authors 		return -EBUSY;
541a8c21a54SThe etnaviv authors 	}
542a8c21a54SThe etnaviv authors 
543a8c21a54SThe etnaviv authors 	/* We rely on the GPU running, so program the clock */
544bcdfb5e5SRussell King 	etnaviv_gpu_update_clock(gpu);
545a8c21a54SThe etnaviv authors 
546a8c21a54SThe etnaviv authors 	return 0;
547a8c21a54SThe etnaviv authors }
548a8c21a54SThe etnaviv authors 
5497d0c6e71SRussell King static void etnaviv_gpu_enable_mlcg(struct etnaviv_gpu *gpu)
5507d0c6e71SRussell King {
5517d0c6e71SRussell King 	u32 pmc, ppc;
5527d0c6e71SRussell King 
5537d0c6e71SRussell King 	/* enable clock gating */
5547d0c6e71SRussell King 	ppc = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
5557d0c6e71SRussell King 	ppc |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
5567d0c6e71SRussell King 
5577d0c6e71SRussell King 	/* Disable stall module clock gating for 4.3.0.1 and 4.3.0.2 revs */
5587d0c6e71SRussell King 	if (gpu->identity.revision == 0x4301 ||
5597d0c6e71SRussell King 	    gpu->identity.revision == 0x4302)
5607d0c6e71SRussell King 		ppc |= VIVS_PM_POWER_CONTROLS_DISABLE_STALL_MODULE_CLOCK_GATING;
5617d0c6e71SRussell King 
5627d0c6e71SRussell King 	gpu_write(gpu, VIVS_PM_POWER_CONTROLS, ppc);
5637d0c6e71SRussell King 
5647d0c6e71SRussell King 	pmc = gpu_read(gpu, VIVS_PM_MODULE_CONTROLS);
5657d0c6e71SRussell King 
5667cef6004SLucas Stach 	/* Disable PA clock gating for GC400+ without bugfix except for GC420 */
5677d0c6e71SRussell King 	if (gpu->identity.model >= chipModel_GC400 &&
5687cef6004SLucas Stach 	    gpu->identity.model != chipModel_GC420 &&
5697cef6004SLucas Stach 	    !(gpu->identity.minor_features3 & chipMinorFeatures3_BUG_FIXES12))
5707d0c6e71SRussell King 		pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PA;
5717d0c6e71SRussell King 
5727d0c6e71SRussell King 	/*
5737d0c6e71SRussell King 	 * Disable PE clock gating on revs < 5.0.0.0 when HZ is
5747d0c6e71SRussell King 	 * present without a bug fix.
5757d0c6e71SRussell King 	 */
5767d0c6e71SRussell King 	if (gpu->identity.revision < 0x5000 &&
5777d0c6e71SRussell King 	    gpu->identity.minor_features0 & chipMinorFeatures0_HZ &&
5787d0c6e71SRussell King 	    !(gpu->identity.minor_features1 &
5797d0c6e71SRussell King 	      chipMinorFeatures1_DISABLE_PE_GATING))
5807d0c6e71SRussell King 		pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PE;
5817d0c6e71SRussell King 
5827d0c6e71SRussell King 	if (gpu->identity.revision < 0x5422)
5837d0c6e71SRussell King 		pmc |= BIT(15); /* Unknown bit */
5847d0c6e71SRussell King 
5857cef6004SLucas Stach 	/* Disable TX clock gating on affected core revisions. */
5867cef6004SLucas Stach 	if (etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
5877cef6004SLucas Stach 	    etnaviv_is_model_rev(gpu, GC2000, 0x5108))
5887cef6004SLucas Stach 		pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_TX;
5897cef6004SLucas Stach 
5907d0c6e71SRussell King 	pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_HZ;
5917d0c6e71SRussell King 	pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_EZ;
5927d0c6e71SRussell King 
5937d0c6e71SRussell King 	gpu_write(gpu, VIVS_PM_MODULE_CONTROLS, pmc);
5947d0c6e71SRussell King }
5957d0c6e71SRussell King 
596229855b6SLucas Stach void etnaviv_gpu_start_fe(struct etnaviv_gpu *gpu, u32 address, u16 prefetch)
597229855b6SLucas Stach {
598229855b6SLucas Stach 	gpu_write(gpu, VIVS_FE_COMMAND_ADDRESS, address);
599229855b6SLucas Stach 	gpu_write(gpu, VIVS_FE_COMMAND_CONTROL,
600229855b6SLucas Stach 		  VIVS_FE_COMMAND_CONTROL_ENABLE |
601229855b6SLucas Stach 		  VIVS_FE_COMMAND_CONTROL_PREFETCH(prefetch));
602c997c3dfSLucas Stach 
603c997c3dfSLucas Stach 	if (gpu->sec_mode == ETNA_SEC_KERNEL) {
604c997c3dfSLucas Stach 		gpu_write(gpu, VIVS_MMUv2_SEC_COMMAND_CONTROL,
605c997c3dfSLucas Stach 			  VIVS_MMUv2_SEC_COMMAND_CONTROL_ENABLE |
606c997c3dfSLucas Stach 			  VIVS_MMUv2_SEC_COMMAND_CONTROL_PREFETCH(prefetch));
607c997c3dfSLucas Stach 	}
608229855b6SLucas Stach }
609229855b6SLucas Stach 
610e17a0dedSWladimir J. van der Laan static void etnaviv_gpu_setup_pulse_eater(struct etnaviv_gpu *gpu)
611e17a0dedSWladimir J. van der Laan {
612e17a0dedSWladimir J. van der Laan 	/*
613e17a0dedSWladimir J. van der Laan 	 * Base value for VIVS_PM_PULSE_EATER register on models where it
614e17a0dedSWladimir J. van der Laan 	 * cannot be read, extracted from vivante kernel driver.
615e17a0dedSWladimir J. van der Laan 	 */
616e17a0dedSWladimir J. van der Laan 	u32 pulse_eater = 0x01590880;
617e17a0dedSWladimir J. van der Laan 
618e17a0dedSWladimir J. van der Laan 	if (etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
619e17a0dedSWladimir J. van der Laan 	    etnaviv_is_model_rev(gpu, GC4000, 0x5222)) {
620e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(23);
621e17a0dedSWladimir J. van der Laan 
622e17a0dedSWladimir J. van der Laan 	}
623e17a0dedSWladimir J. van der Laan 
624e17a0dedSWladimir J. van der Laan 	if (etnaviv_is_model_rev(gpu, GC1000, 0x5039) ||
625e17a0dedSWladimir J. van der Laan 	    etnaviv_is_model_rev(gpu, GC1000, 0x5040)) {
626e17a0dedSWladimir J. van der Laan 		pulse_eater &= ~BIT(16);
627e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(17);
628e17a0dedSWladimir J. van der Laan 	}
629e17a0dedSWladimir J. van der Laan 
630e17a0dedSWladimir J. van der Laan 	if ((gpu->identity.revision > 0x5420) &&
631e17a0dedSWladimir J. van der Laan 	    (gpu->identity.features & chipFeatures_PIPE_3D))
632e17a0dedSWladimir J. van der Laan 	{
633e17a0dedSWladimir J. van der Laan 		/* Performance fix: disable internal DFS */
634e17a0dedSWladimir J. van der Laan 		pulse_eater = gpu_read(gpu, VIVS_PM_PULSE_EATER);
635e17a0dedSWladimir J. van der Laan 		pulse_eater |= BIT(18);
636e17a0dedSWladimir J. van der Laan 	}
637e17a0dedSWladimir J. van der Laan 
638e17a0dedSWladimir J. van der Laan 	gpu_write(gpu, VIVS_PM_PULSE_EATER, pulse_eater);
639e17a0dedSWladimir J. van der Laan }
640e17a0dedSWladimir J. van der Laan 
641a8c21a54SThe etnaviv authors static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu)
642a8c21a54SThe etnaviv authors {
643a8c21a54SThe etnaviv authors 	u16 prefetch;
644a8c21a54SThe etnaviv authors 
645472f79dcSRussell King 	if ((etnaviv_is_model_rev(gpu, GC320, 0x5007) ||
646472f79dcSRussell King 	     etnaviv_is_model_rev(gpu, GC320, 0x5220)) &&
647472f79dcSRussell King 	    gpu_read(gpu, VIVS_HI_CHIP_TIME) != 0x2062400) {
648a8c21a54SThe etnaviv authors 		u32 mc_memory_debug;
649a8c21a54SThe etnaviv authors 
650a8c21a54SThe etnaviv authors 		mc_memory_debug = gpu_read(gpu, VIVS_MC_DEBUG_MEMORY) & ~0xff;
651a8c21a54SThe etnaviv authors 
652a8c21a54SThe etnaviv authors 		if (gpu->identity.revision == 0x5007)
653a8c21a54SThe etnaviv authors 			mc_memory_debug |= 0x0c;
654a8c21a54SThe etnaviv authors 		else
655a8c21a54SThe etnaviv authors 			mc_memory_debug |= 0x08;
656a8c21a54SThe etnaviv authors 
657a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_MC_DEBUG_MEMORY, mc_memory_debug);
658a8c21a54SThe etnaviv authors 	}
659a8c21a54SThe etnaviv authors 
6607d0c6e71SRussell King 	/* enable module-level clock gating */
6617d0c6e71SRussell King 	etnaviv_gpu_enable_mlcg(gpu);
6627d0c6e71SRussell King 
663a8c21a54SThe etnaviv authors 	/*
664a8c21a54SThe etnaviv authors 	 * Update GPU AXI cache atttribute to "cacheable, no allocate".
665a8c21a54SThe etnaviv authors 	 * This is necessary to prevent the iMX6 SoC locking up.
666a8c21a54SThe etnaviv authors 	 */
667a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_AXI_CONFIG,
668a8c21a54SThe etnaviv authors 		  VIVS_HI_AXI_CONFIG_AWCACHE(2) |
669a8c21a54SThe etnaviv authors 		  VIVS_HI_AXI_CONFIG_ARCACHE(2));
670a8c21a54SThe etnaviv authors 
671a8c21a54SThe etnaviv authors 	/* GC2000 rev 5108 needs a special bus config */
672472f79dcSRussell King 	if (etnaviv_is_model_rev(gpu, GC2000, 0x5108)) {
673a8c21a54SThe etnaviv authors 		u32 bus_config = gpu_read(gpu, VIVS_MC_BUS_CONFIG);
674a8c21a54SThe etnaviv authors 		bus_config &= ~(VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG__MASK |
675a8c21a54SThe etnaviv authors 				VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG__MASK);
676a8c21a54SThe etnaviv authors 		bus_config |= VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG(1) |
677a8c21a54SThe etnaviv authors 			      VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG(0);
678a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_MC_BUS_CONFIG, bus_config);
679a8c21a54SThe etnaviv authors 	}
680a8c21a54SThe etnaviv authors 
681c997c3dfSLucas Stach 	if (gpu->sec_mode == ETNA_SEC_KERNEL) {
682c997c3dfSLucas Stach 		u32 val = gpu_read(gpu, VIVS_MMUv2_AHB_CONTROL);
683c997c3dfSLucas Stach 		val |= VIVS_MMUv2_AHB_CONTROL_NONSEC_ACCESS;
684c997c3dfSLucas Stach 		gpu_write(gpu, VIVS_MMUv2_AHB_CONTROL, val);
685c997c3dfSLucas Stach 	}
686c997c3dfSLucas Stach 
687e17a0dedSWladimir J. van der Laan 	/* setup the pulse eater */
688e17a0dedSWladimir J. van der Laan 	etnaviv_gpu_setup_pulse_eater(gpu);
689e17a0dedSWladimir J. van der Laan 
69099f861bcSLucas Stach 	/* setup the MMU */
691e095c8feSLucas Stach 	etnaviv_iommu_restore(gpu);
692a8c21a54SThe etnaviv authors 
693a8c21a54SThe etnaviv authors 	/* Start command processor */
694a8c21a54SThe etnaviv authors 	prefetch = etnaviv_buffer_init(gpu);
695a8c21a54SThe etnaviv authors 
696a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_INTR_ENBL, ~0U);
6972f9225dbSLucas Stach 	etnaviv_gpu_start_fe(gpu, etnaviv_cmdbuf_get_va(&gpu->buffer),
698229855b6SLucas Stach 			     prefetch);
699a8c21a54SThe etnaviv authors }
700a8c21a54SThe etnaviv authors 
701a8c21a54SThe etnaviv authors int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
702a8c21a54SThe etnaviv authors {
703a8c21a54SThe etnaviv authors 	int ret, i;
704a8c21a54SThe etnaviv authors 
705a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
7061409df04SLucas Stach 	if (ret < 0) {
7071409df04SLucas Stach 		dev_err(gpu->dev, "Failed to enable GPU power domain\n");
708a8c21a54SThe etnaviv authors 		return ret;
7091409df04SLucas Stach 	}
710a8c21a54SThe etnaviv authors 
711a8c21a54SThe etnaviv authors 	etnaviv_hw_identify(gpu);
712a8c21a54SThe etnaviv authors 
713a8c21a54SThe etnaviv authors 	if (gpu->identity.model == 0) {
714a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "Unknown GPU model\n");
715f6427760SRussell King 		ret = -ENXIO;
716f6427760SRussell King 		goto fail;
717a8c21a54SThe etnaviv authors 	}
718a8c21a54SThe etnaviv authors 
719b98c6688SRussell King 	/* Exclude VG cores with FE2.0 */
720b98c6688SRussell King 	if (gpu->identity.features & chipFeatures_PIPE_VG &&
721b98c6688SRussell King 	    gpu->identity.features & chipFeatures_FE20) {
722b98c6688SRussell King 		dev_info(gpu->dev, "Ignoring GPU with VG and FE2.0\n");
723b98c6688SRussell King 		ret = -ENXIO;
724b98c6688SRussell King 		goto fail;
725b98c6688SRussell King 	}
726b98c6688SRussell King 
7272144fff7SLucas Stach 	/*
7282144fff7SLucas Stach 	 * Set the GPU linear window to be at the end of the DMA window, where
7292144fff7SLucas Stach 	 * the CMA area is likely to reside. This ensures that we are able to
7302144fff7SLucas Stach 	 * map the command buffers while having the linear window overlap as
7312144fff7SLucas Stach 	 * much RAM as possible, so we can optimize mappings for other buffers.
7322144fff7SLucas Stach 	 *
7332144fff7SLucas Stach 	 * For 3D cores only do this if MC2.0 is present, as with MC1.0 it leads
7342144fff7SLucas Stach 	 * to different views of the memory on the individual engines.
7352144fff7SLucas Stach 	 */
7362144fff7SLucas Stach 	if (!(gpu->identity.features & chipFeatures_PIPE_3D) ||
7372144fff7SLucas Stach 	    (gpu->identity.minor_features0 & chipMinorFeatures0_MC20)) {
7382144fff7SLucas Stach 		u32 dma_mask = (u32)dma_get_required_mask(gpu->dev);
7392144fff7SLucas Stach 		if (dma_mask < PHYS_OFFSET + SZ_2G)
7402144fff7SLucas Stach 			gpu->memory_base = PHYS_OFFSET;
7412144fff7SLucas Stach 		else
7422144fff7SLucas Stach 			gpu->memory_base = dma_mask - SZ_2G + 1;
7431db01279SLucas Stach 	} else if (PHYS_OFFSET >= SZ_2G) {
7441db01279SLucas Stach 		dev_info(gpu->dev, "Need to move linear window on MC1.0, disabling TS\n");
7451db01279SLucas Stach 		gpu->memory_base = PHYS_OFFSET;
7461db01279SLucas Stach 		gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
7472144fff7SLucas Stach 	}
7482144fff7SLucas Stach 
749c997c3dfSLucas Stach 	/*
750c997c3dfSLucas Stach 	 * On cores with security features supported, we claim control over the
751c997c3dfSLucas Stach 	 * security states.
752c997c3dfSLucas Stach 	 */
753c997c3dfSLucas Stach 	if ((gpu->identity.minor_features7 & chipMinorFeatures7_BIT_SECURITY) &&
754c997c3dfSLucas Stach 	    (gpu->identity.minor_features10 & chipMinorFeatures10_SECURITY_AHB))
755c997c3dfSLucas Stach 		gpu->sec_mode = ETNA_SEC_KERNEL;
756c997c3dfSLucas Stach 
757a8c21a54SThe etnaviv authors 	ret = etnaviv_hw_reset(gpu);
7581409df04SLucas Stach 	if (ret) {
7591409df04SLucas Stach 		dev_err(gpu->dev, "GPU reset failed\n");
760a8c21a54SThe etnaviv authors 		goto fail;
7611409df04SLucas Stach 	}
762a8c21a54SThe etnaviv authors 
763dd34bb96SLucas Stach 	gpu->mmu = etnaviv_iommu_new(gpu);
764dd34bb96SLucas Stach 	if (IS_ERR(gpu->mmu)) {
7651409df04SLucas Stach 		dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n");
766dd34bb96SLucas Stach 		ret = PTR_ERR(gpu->mmu);
767a8c21a54SThe etnaviv authors 		goto fail;
768a8c21a54SThe etnaviv authors 	}
769a8c21a54SThe etnaviv authors 
770e66774ddSLucas Stach 	gpu->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(gpu);
771e66774ddSLucas Stach 	if (IS_ERR(gpu->cmdbuf_suballoc)) {
772e66774ddSLucas Stach 		dev_err(gpu->dev, "Failed to create cmdbuf suballocator\n");
773e66774ddSLucas Stach 		ret = PTR_ERR(gpu->cmdbuf_suballoc);
774e66774ddSLucas Stach 		goto fail;
775e66774ddSLucas Stach 	}
776e66774ddSLucas Stach 
777a8c21a54SThe etnaviv authors 	/* Create buffer: */
7782f9225dbSLucas Stach 	ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &gpu->buffer,
7792f9225dbSLucas Stach 				  PAGE_SIZE);
7802f9225dbSLucas Stach 	if (ret) {
781a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "could not create command buffer\n");
78245d16a6dSLucas Stach 		goto destroy_iommu;
783a8c21a54SThe etnaviv authors 	}
784acfee0ecSLucas Stach 
785acfee0ecSLucas Stach 	if (gpu->mmu->version == ETNAVIV_IOMMU_V1 &&
7862f9225dbSLucas Stach 	    etnaviv_cmdbuf_get_va(&gpu->buffer) > 0x80000000) {
787a8c21a54SThe etnaviv authors 		ret = -EINVAL;
788a8c21a54SThe etnaviv authors 		dev_err(gpu->dev,
789a8c21a54SThe etnaviv authors 			"command buffer outside valid memory window\n");
790a8c21a54SThe etnaviv authors 		goto free_buffer;
791a8c21a54SThe etnaviv authors 	}
792a8c21a54SThe etnaviv authors 
793a8c21a54SThe etnaviv authors 	/* Setup event management */
794a8c21a54SThe etnaviv authors 	spin_lock_init(&gpu->event_spinlock);
795a8c21a54SThe etnaviv authors 	init_completion(&gpu->event_free);
796355502e0SChristian Gmeiner 	bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS);
797355502e0SChristian Gmeiner 	for (i = 0; i < ARRAY_SIZE(gpu->event); i++)
798a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
799a8c21a54SThe etnaviv authors 
800a8c21a54SThe etnaviv authors 	/* Now program the hardware */
801a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
802a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
803f6086311SRussell King 	gpu->exec_state = -1;
804a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
805a8c21a54SThe etnaviv authors 
806a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
807a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
808a8c21a54SThe etnaviv authors 
809a8c21a54SThe etnaviv authors 	return 0;
810a8c21a54SThe etnaviv authors 
811a8c21a54SThe etnaviv authors free_buffer:
8122f9225dbSLucas Stach 	etnaviv_cmdbuf_free(&gpu->buffer);
81345d16a6dSLucas Stach destroy_iommu:
81445d16a6dSLucas Stach 	etnaviv_iommu_destroy(gpu->mmu);
81545d16a6dSLucas Stach 	gpu->mmu = NULL;
816a8c21a54SThe etnaviv authors fail:
817a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
818a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
819a8c21a54SThe etnaviv authors 
820a8c21a54SThe etnaviv authors 	return ret;
821a8c21a54SThe etnaviv authors }
822a8c21a54SThe etnaviv authors 
823a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
824a8c21a54SThe etnaviv authors struct dma_debug {
825a8c21a54SThe etnaviv authors 	u32 address[2];
826a8c21a54SThe etnaviv authors 	u32 state[2];
827a8c21a54SThe etnaviv authors };
828a8c21a54SThe etnaviv authors 
829a8c21a54SThe etnaviv authors static void verify_dma(struct etnaviv_gpu *gpu, struct dma_debug *debug)
830a8c21a54SThe etnaviv authors {
831a8c21a54SThe etnaviv authors 	u32 i;
832a8c21a54SThe etnaviv authors 
833a8c21a54SThe etnaviv authors 	debug->address[0] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
834a8c21a54SThe etnaviv authors 	debug->state[0]   = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
835a8c21a54SThe etnaviv authors 
836a8c21a54SThe etnaviv authors 	for (i = 0; i < 500; i++) {
837a8c21a54SThe etnaviv authors 		debug->address[1] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
838a8c21a54SThe etnaviv authors 		debug->state[1]   = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
839a8c21a54SThe etnaviv authors 
840a8c21a54SThe etnaviv authors 		if (debug->address[0] != debug->address[1])
841a8c21a54SThe etnaviv authors 			break;
842a8c21a54SThe etnaviv authors 
843a8c21a54SThe etnaviv authors 		if (debug->state[0] != debug->state[1])
844a8c21a54SThe etnaviv authors 			break;
845a8c21a54SThe etnaviv authors 	}
846a8c21a54SThe etnaviv authors }
847a8c21a54SThe etnaviv authors 
848a8c21a54SThe etnaviv authors int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m)
849a8c21a54SThe etnaviv authors {
850a8c21a54SThe etnaviv authors 	struct dma_debug debug;
851a8c21a54SThe etnaviv authors 	u32 dma_lo, dma_hi, axi, idle;
852a8c21a54SThe etnaviv authors 	int ret;
853a8c21a54SThe etnaviv authors 
854a8c21a54SThe etnaviv authors 	seq_printf(m, "%s Status:\n", dev_name(gpu->dev));
855a8c21a54SThe etnaviv authors 
856a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
857a8c21a54SThe etnaviv authors 	if (ret < 0)
858a8c21a54SThe etnaviv authors 		return ret;
859a8c21a54SThe etnaviv authors 
860a8c21a54SThe etnaviv authors 	dma_lo = gpu_read(gpu, VIVS_FE_DMA_LOW);
861a8c21a54SThe etnaviv authors 	dma_hi = gpu_read(gpu, VIVS_FE_DMA_HIGH);
862a8c21a54SThe etnaviv authors 	axi = gpu_read(gpu, VIVS_HI_AXI_STATUS);
863a8c21a54SThe etnaviv authors 	idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
864a8c21a54SThe etnaviv authors 
865a8c21a54SThe etnaviv authors 	verify_dma(gpu, &debug);
866a8c21a54SThe etnaviv authors 
867a8c21a54SThe etnaviv authors 	seq_puts(m, "\tfeatures\n");
8683d9fc642SLucas Stach 	seq_printf(m, "\t major_features: 0x%08x\n",
8693d9fc642SLucas Stach 		   gpu->identity.features);
870a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features0: 0x%08x\n",
871a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features0);
872a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features1: 0x%08x\n",
873a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features1);
874a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features2: 0x%08x\n",
875a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features2);
876a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features3: 0x%08x\n",
877a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features3);
878602eb489SRussell King 	seq_printf(m, "\t minor_features4: 0x%08x\n",
879602eb489SRussell King 		   gpu->identity.minor_features4);
880602eb489SRussell King 	seq_printf(m, "\t minor_features5: 0x%08x\n",
881602eb489SRussell King 		   gpu->identity.minor_features5);
8820538aaf9SLucas Stach 	seq_printf(m, "\t minor_features6: 0x%08x\n",
8830538aaf9SLucas Stach 		   gpu->identity.minor_features6);
8840538aaf9SLucas Stach 	seq_printf(m, "\t minor_features7: 0x%08x\n",
8850538aaf9SLucas Stach 		   gpu->identity.minor_features7);
8860538aaf9SLucas Stach 	seq_printf(m, "\t minor_features8: 0x%08x\n",
8870538aaf9SLucas Stach 		   gpu->identity.minor_features8);
8880538aaf9SLucas Stach 	seq_printf(m, "\t minor_features9: 0x%08x\n",
8890538aaf9SLucas Stach 		   gpu->identity.minor_features9);
8900538aaf9SLucas Stach 	seq_printf(m, "\t minor_features10: 0x%08x\n",
8910538aaf9SLucas Stach 		   gpu->identity.minor_features10);
8920538aaf9SLucas Stach 	seq_printf(m, "\t minor_features11: 0x%08x\n",
8930538aaf9SLucas Stach 		   gpu->identity.minor_features11);
894a8c21a54SThe etnaviv authors 
895a8c21a54SThe etnaviv authors 	seq_puts(m, "\tspecs\n");
896a8c21a54SThe etnaviv authors 	seq_printf(m, "\t stream_count:  %d\n",
897a8c21a54SThe etnaviv authors 			gpu->identity.stream_count);
898a8c21a54SThe etnaviv authors 	seq_printf(m, "\t register_max: %d\n",
899a8c21a54SThe etnaviv authors 			gpu->identity.register_max);
900a8c21a54SThe etnaviv authors 	seq_printf(m, "\t thread_count: %d\n",
901a8c21a54SThe etnaviv authors 			gpu->identity.thread_count);
902a8c21a54SThe etnaviv authors 	seq_printf(m, "\t vertex_cache_size: %d\n",
903a8c21a54SThe etnaviv authors 			gpu->identity.vertex_cache_size);
904a8c21a54SThe etnaviv authors 	seq_printf(m, "\t shader_core_count: %d\n",
905a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count);
906a8c21a54SThe etnaviv authors 	seq_printf(m, "\t pixel_pipes: %d\n",
907a8c21a54SThe etnaviv authors 			gpu->identity.pixel_pipes);
908a8c21a54SThe etnaviv authors 	seq_printf(m, "\t vertex_output_buffer_size: %d\n",
909a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size);
910a8c21a54SThe etnaviv authors 	seq_printf(m, "\t buffer_size: %d\n",
911a8c21a54SThe etnaviv authors 			gpu->identity.buffer_size);
912a8c21a54SThe etnaviv authors 	seq_printf(m, "\t instruction_count: %d\n",
913a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count);
914a8c21a54SThe etnaviv authors 	seq_printf(m, "\t num_constants: %d\n",
915a8c21a54SThe etnaviv authors 			gpu->identity.num_constants);
916602eb489SRussell King 	seq_printf(m, "\t varyings_count: %d\n",
917602eb489SRussell King 			gpu->identity.varyings_count);
918a8c21a54SThe etnaviv authors 
919a8c21a54SThe etnaviv authors 	seq_printf(m, "\taxi: 0x%08x\n", axi);
920a8c21a54SThe etnaviv authors 	seq_printf(m, "\tidle: 0x%08x\n", idle);
921a8c21a54SThe etnaviv authors 	idle |= ~gpu->idle_mask & ~VIVS_HI_IDLE_STATE_AXI_LP;
922a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_FE) == 0)
923a8c21a54SThe etnaviv authors 		seq_puts(m, "\t FE is not idle\n");
924a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_DE) == 0)
925a8c21a54SThe etnaviv authors 		seq_puts(m, "\t DE is not idle\n");
926a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_PE) == 0)
927a8c21a54SThe etnaviv authors 		seq_puts(m, "\t PE is not idle\n");
928a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_SH) == 0)
929a8c21a54SThe etnaviv authors 		seq_puts(m, "\t SH is not idle\n");
930a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_PA) == 0)
931a8c21a54SThe etnaviv authors 		seq_puts(m, "\t PA is not idle\n");
932a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_SE) == 0)
933a8c21a54SThe etnaviv authors 		seq_puts(m, "\t SE is not idle\n");
934a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_RA) == 0)
935a8c21a54SThe etnaviv authors 		seq_puts(m, "\t RA is not idle\n");
936a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_TX) == 0)
937a8c21a54SThe etnaviv authors 		seq_puts(m, "\t TX is not idle\n");
938a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_VG) == 0)
939a8c21a54SThe etnaviv authors 		seq_puts(m, "\t VG is not idle\n");
940a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_IM) == 0)
941a8c21a54SThe etnaviv authors 		seq_puts(m, "\t IM is not idle\n");
942a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_FP) == 0)
943a8c21a54SThe etnaviv authors 		seq_puts(m, "\t FP is not idle\n");
944a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_TS) == 0)
945a8c21a54SThe etnaviv authors 		seq_puts(m, "\t TS is not idle\n");
946a8c21a54SThe etnaviv authors 	if (idle & VIVS_HI_IDLE_STATE_AXI_LP)
947a8c21a54SThe etnaviv authors 		seq_puts(m, "\t AXI low power mode\n");
948a8c21a54SThe etnaviv authors 
949a8c21a54SThe etnaviv authors 	if (gpu->identity.features & chipFeatures_DEBUG_MODE) {
950a8c21a54SThe etnaviv authors 		u32 read0 = gpu_read(gpu, VIVS_MC_DEBUG_READ0);
951a8c21a54SThe etnaviv authors 		u32 read1 = gpu_read(gpu, VIVS_MC_DEBUG_READ1);
952a8c21a54SThe etnaviv authors 		u32 write = gpu_read(gpu, VIVS_MC_DEBUG_WRITE);
953a8c21a54SThe etnaviv authors 
954a8c21a54SThe etnaviv authors 		seq_puts(m, "\tMC\n");
955a8c21a54SThe etnaviv authors 		seq_printf(m, "\t read0: 0x%08x\n", read0);
956a8c21a54SThe etnaviv authors 		seq_printf(m, "\t read1: 0x%08x\n", read1);
957a8c21a54SThe etnaviv authors 		seq_printf(m, "\t write: 0x%08x\n", write);
958a8c21a54SThe etnaviv authors 	}
959a8c21a54SThe etnaviv authors 
960a8c21a54SThe etnaviv authors 	seq_puts(m, "\tDMA ");
961a8c21a54SThe etnaviv authors 
962a8c21a54SThe etnaviv authors 	if (debug.address[0] == debug.address[1] &&
963a8c21a54SThe etnaviv authors 	    debug.state[0] == debug.state[1]) {
964a8c21a54SThe etnaviv authors 		seq_puts(m, "seems to be stuck\n");
965a8c21a54SThe etnaviv authors 	} else if (debug.address[0] == debug.address[1]) {
966c01e0159SMasanari Iida 		seq_puts(m, "address is constant\n");
967a8c21a54SThe etnaviv authors 	} else {
968c01e0159SMasanari Iida 		seq_puts(m, "is running\n");
969a8c21a54SThe etnaviv authors 	}
970a8c21a54SThe etnaviv authors 
971a8c21a54SThe etnaviv authors 	seq_printf(m, "\t address 0: 0x%08x\n", debug.address[0]);
972a8c21a54SThe etnaviv authors 	seq_printf(m, "\t address 1: 0x%08x\n", debug.address[1]);
973a8c21a54SThe etnaviv authors 	seq_printf(m, "\t state 0: 0x%08x\n", debug.state[0]);
974a8c21a54SThe etnaviv authors 	seq_printf(m, "\t state 1: 0x%08x\n", debug.state[1]);
975a8c21a54SThe etnaviv authors 	seq_printf(m, "\t last fetch 64 bit word: 0x%08x 0x%08x\n",
976a8c21a54SThe etnaviv authors 		   dma_lo, dma_hi);
977a8c21a54SThe etnaviv authors 
978a8c21a54SThe etnaviv authors 	ret = 0;
979a8c21a54SThe etnaviv authors 
980a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
981a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
982a8c21a54SThe etnaviv authors 
983a8c21a54SThe etnaviv authors 	return ret;
984a8c21a54SThe etnaviv authors }
985a8c21a54SThe etnaviv authors #endif
986a8c21a54SThe etnaviv authors 
9876d7a20c0SLucas Stach void etnaviv_gpu_recover_hang(struct etnaviv_gpu *gpu)
988a8c21a54SThe etnaviv authors {
989a8c21a54SThe etnaviv authors 	unsigned long flags;
990355502e0SChristian Gmeiner 	unsigned int i = 0;
991a8c21a54SThe etnaviv authors 
9926d7a20c0SLucas Stach 	dev_err(gpu->dev, "recover hung GPU!\n");
993a8c21a54SThe etnaviv authors 
994a8c21a54SThe etnaviv authors 	if (pm_runtime_get_sync(gpu->dev) < 0)
995a8c21a54SThe etnaviv authors 		return;
996a8c21a54SThe etnaviv authors 
997a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
998a8c21a54SThe etnaviv authors 
999a8c21a54SThe etnaviv authors 	etnaviv_hw_reset(gpu);
1000a8c21a54SThe etnaviv authors 
1001a8c21a54SThe etnaviv authors 	/* complete all events, the GPU won't do it after the reset */
1002a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
10036d7a20c0SLucas Stach 	for_each_set_bit_from(i, gpu->event_bitmap, ETNA_NR_EVENTS)
1004a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
1005355502e0SChristian Gmeiner 	bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS);
1006a8c21a54SThe etnaviv authors 	spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1007a8c21a54SThe etnaviv authors 	gpu->completed_fence = gpu->active_fence;
1008a8c21a54SThe etnaviv authors 
1009a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
10101b94a9b7SLucas Stach 	gpu->lastctx = NULL;
1011f6086311SRussell King 	gpu->exec_state = -1;
1012a8c21a54SThe etnaviv authors 
1013a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1014a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
1015a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
1016a8c21a54SThe etnaviv authors }
1017a8c21a54SThe etnaviv authors 
1018a8c21a54SThe etnaviv authors /* fence object management */
1019a8c21a54SThe etnaviv authors struct etnaviv_fence {
1020a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
1021f54d1867SChris Wilson 	struct dma_fence base;
1022a8c21a54SThe etnaviv authors };
1023a8c21a54SThe etnaviv authors 
1024f54d1867SChris Wilson static inline struct etnaviv_fence *to_etnaviv_fence(struct dma_fence *fence)
1025a8c21a54SThe etnaviv authors {
1026a8c21a54SThe etnaviv authors 	return container_of(fence, struct etnaviv_fence, base);
1027a8c21a54SThe etnaviv authors }
1028a8c21a54SThe etnaviv authors 
1029f54d1867SChris Wilson static const char *etnaviv_fence_get_driver_name(struct dma_fence *fence)
1030a8c21a54SThe etnaviv authors {
1031a8c21a54SThe etnaviv authors 	return "etnaviv";
1032a8c21a54SThe etnaviv authors }
1033a8c21a54SThe etnaviv authors 
1034f54d1867SChris Wilson static const char *etnaviv_fence_get_timeline_name(struct dma_fence *fence)
1035a8c21a54SThe etnaviv authors {
1036a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1037a8c21a54SThe etnaviv authors 
1038a8c21a54SThe etnaviv authors 	return dev_name(f->gpu->dev);
1039a8c21a54SThe etnaviv authors }
1040a8c21a54SThe etnaviv authors 
1041f54d1867SChris Wilson static bool etnaviv_fence_enable_signaling(struct dma_fence *fence)
1042a8c21a54SThe etnaviv authors {
1043a8c21a54SThe etnaviv authors 	return true;
1044a8c21a54SThe etnaviv authors }
1045a8c21a54SThe etnaviv authors 
1046f54d1867SChris Wilson static bool etnaviv_fence_signaled(struct dma_fence *fence)
1047a8c21a54SThe etnaviv authors {
1048a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1049a8c21a54SThe etnaviv authors 
1050a8c21a54SThe etnaviv authors 	return fence_completed(f->gpu, f->base.seqno);
1051a8c21a54SThe etnaviv authors }
1052a8c21a54SThe etnaviv authors 
1053f54d1867SChris Wilson static void etnaviv_fence_release(struct dma_fence *fence)
1054a8c21a54SThe etnaviv authors {
1055a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
1056a8c21a54SThe etnaviv authors 
1057a8c21a54SThe etnaviv authors 	kfree_rcu(f, base.rcu);
1058a8c21a54SThe etnaviv authors }
1059a8c21a54SThe etnaviv authors 
1060f54d1867SChris Wilson static const struct dma_fence_ops etnaviv_fence_ops = {
1061a8c21a54SThe etnaviv authors 	.get_driver_name = etnaviv_fence_get_driver_name,
1062a8c21a54SThe etnaviv authors 	.get_timeline_name = etnaviv_fence_get_timeline_name,
1063a8c21a54SThe etnaviv authors 	.enable_signaling = etnaviv_fence_enable_signaling,
1064a8c21a54SThe etnaviv authors 	.signaled = etnaviv_fence_signaled,
1065f54d1867SChris Wilson 	.wait = dma_fence_default_wait,
1066a8c21a54SThe etnaviv authors 	.release = etnaviv_fence_release,
1067a8c21a54SThe etnaviv authors };
1068a8c21a54SThe etnaviv authors 
1069f54d1867SChris Wilson static struct dma_fence *etnaviv_gpu_fence_alloc(struct etnaviv_gpu *gpu)
1070a8c21a54SThe etnaviv authors {
1071a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f;
1072a8c21a54SThe etnaviv authors 
1073b27734c2SLucas Stach 	/*
1074b27734c2SLucas Stach 	 * GPU lock must already be held, otherwise fence completion order might
1075b27734c2SLucas Stach 	 * not match the seqno order assigned here.
1076b27734c2SLucas Stach 	 */
1077b27734c2SLucas Stach 	lockdep_assert_held(&gpu->lock);
1078b27734c2SLucas Stach 
1079a8c21a54SThe etnaviv authors 	f = kzalloc(sizeof(*f), GFP_KERNEL);
1080a8c21a54SThe etnaviv authors 	if (!f)
1081a8c21a54SThe etnaviv authors 		return NULL;
1082a8c21a54SThe etnaviv authors 
1083a8c21a54SThe etnaviv authors 	f->gpu = gpu;
1084a8c21a54SThe etnaviv authors 
1085f54d1867SChris Wilson 	dma_fence_init(&f->base, &etnaviv_fence_ops, &gpu->fence_spinlock,
1086a8c21a54SThe etnaviv authors 		       gpu->fence_context, ++gpu->next_fence);
1087a8c21a54SThe etnaviv authors 
1088a8c21a54SThe etnaviv authors 	return &f->base;
1089a8c21a54SThe etnaviv authors }
1090a8c21a54SThe etnaviv authors 
1091a8c21a54SThe etnaviv authors /*
1092a8c21a54SThe etnaviv authors  * event management:
1093a8c21a54SThe etnaviv authors  */
1094a8c21a54SThe etnaviv authors 
109595a428c1SChristian Gmeiner static int event_alloc(struct etnaviv_gpu *gpu, unsigned nr_events,
109695a428c1SChristian Gmeiner 	unsigned int *events)
1097a8c21a54SThe etnaviv authors {
109895a428c1SChristian Gmeiner 	unsigned long flags, timeout = msecs_to_jiffies(10 * 10000);
109995a428c1SChristian Gmeiner 	unsigned i, acquired = 0;
1100a8c21a54SThe etnaviv authors 
110195a428c1SChristian Gmeiner 	for (i = 0; i < nr_events; i++) {
110295a428c1SChristian Gmeiner 		unsigned long ret;
110395a428c1SChristian Gmeiner 
110495a428c1SChristian Gmeiner 		ret = wait_for_completion_timeout(&gpu->event_free, timeout);
110595a428c1SChristian Gmeiner 
110695a428c1SChristian Gmeiner 		if (!ret) {
1107a8c21a54SThe etnaviv authors 			dev_err(gpu->dev, "wait_for_completion_timeout failed");
110895a428c1SChristian Gmeiner 			goto out;
110995a428c1SChristian Gmeiner 		}
111095a428c1SChristian Gmeiner 
111195a428c1SChristian Gmeiner 		acquired++;
111295a428c1SChristian Gmeiner 		timeout = ret;
111395a428c1SChristian Gmeiner 	}
1114a8c21a54SThe etnaviv authors 
1115a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
1116a8c21a54SThe etnaviv authors 
111795a428c1SChristian Gmeiner 	for (i = 0; i < nr_events; i++) {
111895a428c1SChristian Gmeiner 		int event = find_first_zero_bit(gpu->event_bitmap, ETNA_NR_EVENTS);
111995a428c1SChristian Gmeiner 
112095a428c1SChristian Gmeiner 		events[i] = event;
1121547d340dSChristian Gmeiner 		memset(&gpu->event[event], 0, sizeof(struct etnaviv_event));
1122355502e0SChristian Gmeiner 		set_bit(event, gpu->event_bitmap);
1123a8c21a54SThe etnaviv authors 	}
1124a8c21a54SThe etnaviv authors 
1125a8c21a54SThe etnaviv authors 	spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1126a8c21a54SThe etnaviv authors 
112795a428c1SChristian Gmeiner 	return 0;
112895a428c1SChristian Gmeiner 
112995a428c1SChristian Gmeiner out:
113095a428c1SChristian Gmeiner 	for (i = 0; i < acquired; i++)
113195a428c1SChristian Gmeiner 		complete(&gpu->event_free);
113295a428c1SChristian Gmeiner 
113395a428c1SChristian Gmeiner 	return -EBUSY;
1134a8c21a54SThe etnaviv authors }
1135a8c21a54SThe etnaviv authors 
1136a8c21a54SThe etnaviv authors static void event_free(struct etnaviv_gpu *gpu, unsigned int event)
1137a8c21a54SThe etnaviv authors {
1138a8c21a54SThe etnaviv authors 	unsigned long flags;
1139a8c21a54SThe etnaviv authors 
1140a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
1141a8c21a54SThe etnaviv authors 
1142355502e0SChristian Gmeiner 	if (!test_bit(event, gpu->event_bitmap)) {
1143a8c21a54SThe etnaviv authors 		dev_warn(gpu->dev, "event %u is already marked as free",
1144a8c21a54SThe etnaviv authors 			 event);
1145a8c21a54SThe etnaviv authors 		spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1146a8c21a54SThe etnaviv authors 	} else {
1147355502e0SChristian Gmeiner 		clear_bit(event, gpu->event_bitmap);
1148a8c21a54SThe etnaviv authors 		spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1149a8c21a54SThe etnaviv authors 
1150a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
1151a8c21a54SThe etnaviv authors 	}
1152a8c21a54SThe etnaviv authors }
1153a8c21a54SThe etnaviv authors 
1154a8c21a54SThe etnaviv authors /*
1155a8c21a54SThe etnaviv authors  * Cmdstream submission/retirement:
1156a8c21a54SThe etnaviv authors  */
1157a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu,
11588bc4d885SLucas Stach 	u32 id, struct timespec *timeout)
1159a8c21a54SThe etnaviv authors {
11608bc4d885SLucas Stach 	struct dma_fence *fence;
1161a8c21a54SThe etnaviv authors 	int ret;
1162a8c21a54SThe etnaviv authors 
11638bc4d885SLucas Stach 	/*
1164e93b6deeSLucas Stach 	 * Look up the fence and take a reference. We might still find a fence
11658bc4d885SLucas Stach 	 * whose refcount has already dropped to zero. dma_fence_get_rcu
11668bc4d885SLucas Stach 	 * pretends we didn't find a fence in that case.
11678bc4d885SLucas Stach 	 */
1168e93b6deeSLucas Stach 	rcu_read_lock();
11698bc4d885SLucas Stach 	fence = idr_find(&gpu->fence_idr, id);
11708bc4d885SLucas Stach 	if (fence)
11718bc4d885SLucas Stach 		fence = dma_fence_get_rcu(fence);
1172e93b6deeSLucas Stach 	rcu_read_unlock();
11738bc4d885SLucas Stach 
11748bc4d885SLucas Stach 	if (!fence)
11758bc4d885SLucas Stach 		return 0;
1176a8c21a54SThe etnaviv authors 
1177a8c21a54SThe etnaviv authors 	if (!timeout) {
1178a8c21a54SThe etnaviv authors 		/* No timeout was requested: just test for completion */
11798bc4d885SLucas Stach 		ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
1180a8c21a54SThe etnaviv authors 	} else {
1181a8c21a54SThe etnaviv authors 		unsigned long remaining = etnaviv_timeout_to_jiffies(timeout);
1182a8c21a54SThe etnaviv authors 
11838bc4d885SLucas Stach 		ret = dma_fence_wait_timeout(fence, true, remaining);
11848bc4d885SLucas Stach 		if (ret == 0)
1185a8c21a54SThe etnaviv authors 			ret = -ETIMEDOUT;
11868bc4d885SLucas Stach 		else if (ret != -ERESTARTSYS)
1187a8c21a54SThe etnaviv authors 			ret = 0;
11888bc4d885SLucas Stach 
1189a8c21a54SThe etnaviv authors 	}
1190a8c21a54SThe etnaviv authors 
11918bc4d885SLucas Stach 	dma_fence_put(fence);
1192a8c21a54SThe etnaviv authors 	return ret;
1193a8c21a54SThe etnaviv authors }
1194a8c21a54SThe etnaviv authors 
1195a8c21a54SThe etnaviv authors /*
1196a8c21a54SThe etnaviv authors  * Wait for an object to become inactive.  This, on it's own, is not race
1197e93b6deeSLucas Stach  * free: the object is moved by the scheduler off the active list, and
1198a8c21a54SThe etnaviv authors  * then the iova is put.  Moreover, the object could be re-submitted just
1199a8c21a54SThe etnaviv authors  * after we notice that it's become inactive.
1200a8c21a54SThe etnaviv authors  *
1201a8c21a54SThe etnaviv authors  * Although the retirement happens under the gpu lock, we don't want to hold
1202a8c21a54SThe etnaviv authors  * that lock in this function while waiting.
1203a8c21a54SThe etnaviv authors  */
1204a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu,
1205a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout)
1206a8c21a54SThe etnaviv authors {
1207a8c21a54SThe etnaviv authors 	unsigned long remaining;
1208a8c21a54SThe etnaviv authors 	long ret;
1209a8c21a54SThe etnaviv authors 
1210a8c21a54SThe etnaviv authors 	if (!timeout)
1211a8c21a54SThe etnaviv authors 		return !is_active(etnaviv_obj) ? 0 : -EBUSY;
1212a8c21a54SThe etnaviv authors 
1213a8c21a54SThe etnaviv authors 	remaining = etnaviv_timeout_to_jiffies(timeout);
1214a8c21a54SThe etnaviv authors 
1215a8c21a54SThe etnaviv authors 	ret = wait_event_interruptible_timeout(gpu->fence_event,
1216a8c21a54SThe etnaviv authors 					       !is_active(etnaviv_obj),
1217a8c21a54SThe etnaviv authors 					       remaining);
1218fa67ac84SLucas Stach 	if (ret > 0)
1219a8c21a54SThe etnaviv authors 		return 0;
1220fa67ac84SLucas Stach 	else if (ret == -ERESTARTSYS)
1221a8c21a54SThe etnaviv authors 		return -ERESTARTSYS;
1222fa67ac84SLucas Stach 	else
1223a8c21a54SThe etnaviv authors 		return -ETIMEDOUT;
1224a8c21a54SThe etnaviv authors }
1225a8c21a54SThe etnaviv authors 
122668dc0b29SChristian Gmeiner static void sync_point_perfmon_sample(struct etnaviv_gpu *gpu,
122768dc0b29SChristian Gmeiner 	struct etnaviv_event *event, unsigned int flags)
122868dc0b29SChristian Gmeiner {
1229ef146c00SLucas Stach 	const struct etnaviv_gem_submit *submit = event->submit;
123068dc0b29SChristian Gmeiner 	unsigned int i;
123168dc0b29SChristian Gmeiner 
1232ef146c00SLucas Stach 	for (i = 0; i < submit->nr_pmrs; i++) {
1233ef146c00SLucas Stach 		const struct etnaviv_perfmon_request *pmr = submit->pmrs + i;
123468dc0b29SChristian Gmeiner 
123568dc0b29SChristian Gmeiner 		if (pmr->flags == flags)
12367a9c0fe2SLucas Stach 			etnaviv_perfmon_process(gpu, pmr, submit->exec_state);
123768dc0b29SChristian Gmeiner 	}
123868dc0b29SChristian Gmeiner }
123968dc0b29SChristian Gmeiner 
124068dc0b29SChristian Gmeiner static void sync_point_perfmon_sample_pre(struct etnaviv_gpu *gpu,
124168dc0b29SChristian Gmeiner 	struct etnaviv_event *event)
124268dc0b29SChristian Gmeiner {
12432c8b0c5aSChristian Gmeiner 	u32 val;
12442c8b0c5aSChristian Gmeiner 
12452c8b0c5aSChristian Gmeiner 	/* disable clock gating */
12462c8b0c5aSChristian Gmeiner 	val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
12472c8b0c5aSChristian Gmeiner 	val &= ~VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
12482c8b0c5aSChristian Gmeiner 	gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val);
12492c8b0c5aSChristian Gmeiner 
125004a7d18dSChristian Gmeiner 	/* enable debug register */
125104a7d18dSChristian Gmeiner 	val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
125204a7d18dSChristian Gmeiner 	val &= ~VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
125304a7d18dSChristian Gmeiner 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val);
125404a7d18dSChristian Gmeiner 
125568dc0b29SChristian Gmeiner 	sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_PRE);
125668dc0b29SChristian Gmeiner }
125768dc0b29SChristian Gmeiner 
125868dc0b29SChristian Gmeiner static void sync_point_perfmon_sample_post(struct etnaviv_gpu *gpu,
125968dc0b29SChristian Gmeiner 	struct etnaviv_event *event)
126068dc0b29SChristian Gmeiner {
1261ef146c00SLucas Stach 	const struct etnaviv_gem_submit *submit = event->submit;
126268dc0b29SChristian Gmeiner 	unsigned int i;
12632c8b0c5aSChristian Gmeiner 	u32 val;
126468dc0b29SChristian Gmeiner 
126568dc0b29SChristian Gmeiner 	sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_POST);
126668dc0b29SChristian Gmeiner 
1267ef146c00SLucas Stach 	for (i = 0; i < submit->nr_pmrs; i++) {
1268ef146c00SLucas Stach 		const struct etnaviv_perfmon_request *pmr = submit->pmrs + i;
126968dc0b29SChristian Gmeiner 
127068dc0b29SChristian Gmeiner 		*pmr->bo_vma = pmr->sequence;
127168dc0b29SChristian Gmeiner 	}
12722c8b0c5aSChristian Gmeiner 
127304a7d18dSChristian Gmeiner 	/* disable debug register */
127404a7d18dSChristian Gmeiner 	val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
127504a7d18dSChristian Gmeiner 	val |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
127604a7d18dSChristian Gmeiner 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val);
127704a7d18dSChristian Gmeiner 
12782c8b0c5aSChristian Gmeiner 	/* enable clock gating */
12792c8b0c5aSChristian Gmeiner 	val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
12802c8b0c5aSChristian Gmeiner 	val |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
12812c8b0c5aSChristian Gmeiner 	gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val);
128268dc0b29SChristian Gmeiner }
128368dc0b29SChristian Gmeiner 
128468dc0b29SChristian Gmeiner 
1285a8c21a54SThe etnaviv authors /* add bo's to gpu's ring, and kick gpu: */
1286e93b6deeSLucas Stach struct dma_fence *etnaviv_gpu_submit(struct etnaviv_gem_submit *submit)
1287a8c21a54SThe etnaviv authors {
1288e93b6deeSLucas Stach 	struct etnaviv_gpu *gpu = submit->gpu;
1289e93b6deeSLucas Stach 	struct dma_fence *gpu_fence;
129068dc0b29SChristian Gmeiner 	unsigned int i, nr_events = 1, event[3];
1291a8c21a54SThe etnaviv authors 	int ret;
1292a8c21a54SThe etnaviv authors 
12936d7a20c0SLucas Stach 	if (!submit->runtime_resumed) {
12948bda1516SLucas Stach 		ret = pm_runtime_get_sync(gpu->dev);
1295a8c21a54SThe etnaviv authors 		if (ret < 0)
1296e93b6deeSLucas Stach 			return NULL;
12978bda1516SLucas Stach 		submit->runtime_resumed = true;
12986d7a20c0SLucas Stach 	}
1299a8c21a54SThe etnaviv authors 
1300a8c21a54SThe etnaviv authors 	/*
130168dc0b29SChristian Gmeiner 	 * if there are performance monitor requests we need to have
130268dc0b29SChristian Gmeiner 	 * - a sync point to re-configure gpu and process ETNA_PM_PROCESS_PRE
130368dc0b29SChristian Gmeiner 	 *   requests.
130468dc0b29SChristian Gmeiner 	 * - a sync point to re-configure gpu, process ETNA_PM_PROCESS_POST requests
130568dc0b29SChristian Gmeiner 	 *   and update the sequence number for userspace.
130668dc0b29SChristian Gmeiner 	 */
1307ef146c00SLucas Stach 	if (submit->nr_pmrs)
130868dc0b29SChristian Gmeiner 		nr_events = 3;
130968dc0b29SChristian Gmeiner 
131068dc0b29SChristian Gmeiner 	ret = event_alloc(gpu, nr_events, event);
131195a428c1SChristian Gmeiner 	if (ret) {
131268dc0b29SChristian Gmeiner 		DRM_ERROR("no free events\n");
1313e93b6deeSLucas Stach 		return NULL;
1314a8c21a54SThe etnaviv authors 	}
1315a8c21a54SThe etnaviv authors 
1316f3cd1b06SLucas Stach 	mutex_lock(&gpu->lock);
1317f3cd1b06SLucas Stach 
1318e93b6deeSLucas Stach 	gpu_fence = etnaviv_gpu_fence_alloc(gpu);
1319e93b6deeSLucas Stach 	if (!gpu_fence) {
132068dc0b29SChristian Gmeiner 		for (i = 0; i < nr_events; i++)
132168dc0b29SChristian Gmeiner 			event_free(gpu, event[i]);
132268dc0b29SChristian Gmeiner 
132345abdf35SWei Yongjun 		goto out_unlock;
1324a8c21a54SThe etnaviv authors 	}
1325a8c21a54SThe etnaviv authors 
1326e93b6deeSLucas Stach 	gpu->active_fence = gpu_fence->seqno;
1327a8c21a54SThe etnaviv authors 
1328ef146c00SLucas Stach 	if (submit->nr_pmrs) {
132968dc0b29SChristian Gmeiner 		gpu->event[event[1]].sync_point = &sync_point_perfmon_sample_pre;
1330ef146c00SLucas Stach 		kref_get(&submit->refcount);
1331ef146c00SLucas Stach 		gpu->event[event[1]].submit = submit;
133268dc0b29SChristian Gmeiner 		etnaviv_sync_point_queue(gpu, event[1]);
133368dc0b29SChristian Gmeiner 	}
133468dc0b29SChristian Gmeiner 
1335e93b6deeSLucas Stach 	gpu->event[event[0]].fence = gpu_fence;
13366d7a20c0SLucas Stach 	submit->cmdbuf.user_size = submit->cmdbuf.size - 8;
13372f9225dbSLucas Stach 	etnaviv_buffer_queue(gpu, submit->exec_state, event[0],
13382f9225dbSLucas Stach 			     &submit->cmdbuf);
133968dc0b29SChristian Gmeiner 
1340ef146c00SLucas Stach 	if (submit->nr_pmrs) {
134168dc0b29SChristian Gmeiner 		gpu->event[event[2]].sync_point = &sync_point_perfmon_sample_post;
1342ef146c00SLucas Stach 		kref_get(&submit->refcount);
1343ef146c00SLucas Stach 		gpu->event[event[2]].submit = submit;
134468dc0b29SChristian Gmeiner 		etnaviv_sync_point_queue(gpu, event[2]);
134568dc0b29SChristian Gmeiner 	}
1346a8c21a54SThe etnaviv authors 
134745abdf35SWei Yongjun out_unlock:
1348a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1349a8c21a54SThe etnaviv authors 
1350e93b6deeSLucas Stach 	return gpu_fence;
1351a8c21a54SThe etnaviv authors }
1352a8c21a54SThe etnaviv authors 
1353357713ceSChristian Gmeiner static void sync_point_worker(struct work_struct *work)
1354357713ceSChristian Gmeiner {
1355357713ceSChristian Gmeiner 	struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
1356357713ceSChristian Gmeiner 					       sync_point_work);
1357b9a48aa7SLucas Stach 	struct etnaviv_event *event = &gpu->event[gpu->sync_point_event];
1358b9a48aa7SLucas Stach 	u32 addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
1359357713ceSChristian Gmeiner 
1360b9a48aa7SLucas Stach 	event->sync_point(gpu, event);
1361ef146c00SLucas Stach 	etnaviv_submit_put(event->submit);
1362357713ceSChristian Gmeiner 	event_free(gpu, gpu->sync_point_event);
1363b9a48aa7SLucas Stach 
1364b9a48aa7SLucas Stach 	/* restart FE last to avoid GPU and IRQ racing against this worker */
1365b9a48aa7SLucas Stach 	etnaviv_gpu_start_fe(gpu, addr + 2, 2);
1366357713ceSChristian Gmeiner }
1367357713ceSChristian Gmeiner 
13684df3000eSLucas Stach static void dump_mmu_fault(struct etnaviv_gpu *gpu)
13694df3000eSLucas Stach {
1370c997c3dfSLucas Stach 	u32 status_reg, status;
13714df3000eSLucas Stach 	int i;
13724df3000eSLucas Stach 
1373c997c3dfSLucas Stach 	if (gpu->sec_mode == ETNA_SEC_NONE)
1374c997c3dfSLucas Stach 		status_reg = VIVS_MMUv2_STATUS;
1375c997c3dfSLucas Stach 	else
1376c997c3dfSLucas Stach 		status_reg = VIVS_MMUv2_SEC_STATUS;
1377c997c3dfSLucas Stach 
1378c997c3dfSLucas Stach 	status = gpu_read(gpu, status_reg);
13794df3000eSLucas Stach 	dev_err_ratelimited(gpu->dev, "MMU fault status 0x%08x\n", status);
13804df3000eSLucas Stach 
13814df3000eSLucas Stach 	for (i = 0; i < 4; i++) {
1382c997c3dfSLucas Stach 		u32 address_reg;
1383c997c3dfSLucas Stach 
13844df3000eSLucas Stach 		if (!(status & (VIVS_MMUv2_STATUS_EXCEPTION0__MASK << (i * 4))))
13854df3000eSLucas Stach 			continue;
13864df3000eSLucas Stach 
1387c997c3dfSLucas Stach 		if (gpu->sec_mode == ETNA_SEC_NONE)
1388c997c3dfSLucas Stach 			address_reg = VIVS_MMUv2_EXCEPTION_ADDR(i);
1389c997c3dfSLucas Stach 		else
1390c997c3dfSLucas Stach 			address_reg = VIVS_MMUv2_SEC_EXCEPTION_ADDR;
1391c997c3dfSLucas Stach 
13924df3000eSLucas Stach 		dev_err_ratelimited(gpu->dev, "MMU %d fault addr 0x%08x\n", i,
1393c997c3dfSLucas Stach 				    gpu_read(gpu, address_reg));
13944df3000eSLucas Stach 	}
13954df3000eSLucas Stach }
13964df3000eSLucas Stach 
1397a8c21a54SThe etnaviv authors static irqreturn_t irq_handler(int irq, void *data)
1398a8c21a54SThe etnaviv authors {
1399a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = data;
1400a8c21a54SThe etnaviv authors 	irqreturn_t ret = IRQ_NONE;
1401a8c21a54SThe etnaviv authors 
1402a8c21a54SThe etnaviv authors 	u32 intr = gpu_read(gpu, VIVS_HI_INTR_ACKNOWLEDGE);
1403a8c21a54SThe etnaviv authors 
1404a8c21a54SThe etnaviv authors 	if (intr != 0) {
1405a8c21a54SThe etnaviv authors 		int event;
1406a8c21a54SThe etnaviv authors 
1407a8c21a54SThe etnaviv authors 		pm_runtime_mark_last_busy(gpu->dev);
1408a8c21a54SThe etnaviv authors 
1409a8c21a54SThe etnaviv authors 		dev_dbg(gpu->dev, "intr 0x%08x\n", intr);
1410a8c21a54SThe etnaviv authors 
1411a8c21a54SThe etnaviv authors 		if (intr & VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR) {
1412a8c21a54SThe etnaviv authors 			dev_err(gpu->dev, "AXI bus error\n");
1413a8c21a54SThe etnaviv authors 			intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR;
1414a8c21a54SThe etnaviv authors 		}
1415a8c21a54SThe etnaviv authors 
1416128a9b1dSLucas Stach 		if (intr & VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION) {
14174df3000eSLucas Stach 			dump_mmu_fault(gpu);
1418128a9b1dSLucas Stach 			intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION;
1419128a9b1dSLucas Stach 		}
1420128a9b1dSLucas Stach 
1421a8c21a54SThe etnaviv authors 		while ((event = ffs(intr)) != 0) {
1422f54d1867SChris Wilson 			struct dma_fence *fence;
1423a8c21a54SThe etnaviv authors 
1424a8c21a54SThe etnaviv authors 			event -= 1;
1425a8c21a54SThe etnaviv authors 
1426a8c21a54SThe etnaviv authors 			intr &= ~(1 << event);
1427a8c21a54SThe etnaviv authors 
1428a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "event %u\n", event);
1429a8c21a54SThe etnaviv authors 
1430357713ceSChristian Gmeiner 			if (gpu->event[event].sync_point) {
1431357713ceSChristian Gmeiner 				gpu->sync_point_event = event;
1432a7790d78SLucas Stach 				queue_work(gpu->wq, &gpu->sync_point_work);
1433357713ceSChristian Gmeiner 			}
1434357713ceSChristian Gmeiner 
1435a8c21a54SThe etnaviv authors 			fence = gpu->event[event].fence;
143668dc0b29SChristian Gmeiner 			if (!fence)
143768dc0b29SChristian Gmeiner 				continue;
143868dc0b29SChristian Gmeiner 
1439a8c21a54SThe etnaviv authors 			gpu->event[event].fence = NULL;
1440a8c21a54SThe etnaviv authors 
1441a8c21a54SThe etnaviv authors 			/*
1442a8c21a54SThe etnaviv authors 			 * Events can be processed out of order.  Eg,
1443a8c21a54SThe etnaviv authors 			 * - allocate and queue event 0
1444a8c21a54SThe etnaviv authors 			 * - allocate event 1
1445a8c21a54SThe etnaviv authors 			 * - event 0 completes, we process it
1446a8c21a54SThe etnaviv authors 			 * - allocate and queue event 0
1447a8c21a54SThe etnaviv authors 			 * - event 1 and event 0 complete
1448a8c21a54SThe etnaviv authors 			 * we can end up processing event 0 first, then 1.
1449a8c21a54SThe etnaviv authors 			 */
1450a8c21a54SThe etnaviv authors 			if (fence_after(fence->seqno, gpu->completed_fence))
1451a8c21a54SThe etnaviv authors 				gpu->completed_fence = fence->seqno;
14528bc4d885SLucas Stach 			dma_fence_signal(fence);
1453a8c21a54SThe etnaviv authors 
1454a8c21a54SThe etnaviv authors 			event_free(gpu, event);
1455a8c21a54SThe etnaviv authors 		}
1456a8c21a54SThe etnaviv authors 
1457a8c21a54SThe etnaviv authors 		ret = IRQ_HANDLED;
1458a8c21a54SThe etnaviv authors 	}
1459a8c21a54SThe etnaviv authors 
1460a8c21a54SThe etnaviv authors 	return ret;
1461a8c21a54SThe etnaviv authors }
1462a8c21a54SThe etnaviv authors 
1463a8c21a54SThe etnaviv authors static int etnaviv_gpu_clk_enable(struct etnaviv_gpu *gpu)
1464a8c21a54SThe etnaviv authors {
1465a8c21a54SThe etnaviv authors 	int ret;
1466a8c21a54SThe etnaviv authors 
146765f037e8SLucas Stach 	if (gpu->clk_reg) {
146865f037e8SLucas Stach 		ret = clk_prepare_enable(gpu->clk_reg);
146965f037e8SLucas Stach 		if (ret)
147065f037e8SLucas Stach 			return ret;
147165f037e8SLucas Stach 	}
147265f037e8SLucas Stach 
14739c7310c0SLucas Stach 	if (gpu->clk_bus) {
14749c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_bus);
1475a8c21a54SThe etnaviv authors 		if (ret)
1476a8c21a54SThe etnaviv authors 			return ret;
1477a8c21a54SThe etnaviv authors 	}
1478a8c21a54SThe etnaviv authors 
14799c7310c0SLucas Stach 	if (gpu->clk_core) {
14809c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_core);
14819c7310c0SLucas Stach 		if (ret)
14829c7310c0SLucas Stach 			goto disable_clk_bus;
14839c7310c0SLucas Stach 	}
14849c7310c0SLucas Stach 
14859c7310c0SLucas Stach 	if (gpu->clk_shader) {
14869c7310c0SLucas Stach 		ret = clk_prepare_enable(gpu->clk_shader);
14879c7310c0SLucas Stach 		if (ret)
14889c7310c0SLucas Stach 			goto disable_clk_core;
14899c7310c0SLucas Stach 	}
14909c7310c0SLucas Stach 
1491a8c21a54SThe etnaviv authors 	return 0;
14929c7310c0SLucas Stach 
14939c7310c0SLucas Stach disable_clk_core:
14949c7310c0SLucas Stach 	if (gpu->clk_core)
14959c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_core);
14969c7310c0SLucas Stach disable_clk_bus:
14979c7310c0SLucas Stach 	if (gpu->clk_bus)
14989c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_bus);
14999c7310c0SLucas Stach 
15009c7310c0SLucas Stach 	return ret;
1501a8c21a54SThe etnaviv authors }
1502a8c21a54SThe etnaviv authors 
1503a8c21a54SThe etnaviv authors static int etnaviv_gpu_clk_disable(struct etnaviv_gpu *gpu)
1504a8c21a54SThe etnaviv authors {
15059c7310c0SLucas Stach 	if (gpu->clk_shader)
15069c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_shader);
15079c7310c0SLucas Stach 	if (gpu->clk_core)
15089c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_core);
15099c7310c0SLucas Stach 	if (gpu->clk_bus)
15109c7310c0SLucas Stach 		clk_disable_unprepare(gpu->clk_bus);
151165f037e8SLucas Stach 	if (gpu->clk_reg)
151265f037e8SLucas Stach 		clk_disable_unprepare(gpu->clk_reg);
1513a8c21a54SThe etnaviv authors 
1514a8c21a54SThe etnaviv authors 	return 0;
1515a8c21a54SThe etnaviv authors }
1516a8c21a54SThe etnaviv authors 
1517b88163e3SLucas Stach int etnaviv_gpu_wait_idle(struct etnaviv_gpu *gpu, unsigned int timeout_ms)
1518b88163e3SLucas Stach {
1519b88163e3SLucas Stach 	unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1520b88163e3SLucas Stach 
1521b88163e3SLucas Stach 	do {
1522b88163e3SLucas Stach 		u32 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
1523b88163e3SLucas Stach 
1524b88163e3SLucas Stach 		if ((idle & gpu->idle_mask) == gpu->idle_mask)
1525b88163e3SLucas Stach 			return 0;
1526b88163e3SLucas Stach 
1527b88163e3SLucas Stach 		if (time_is_before_jiffies(timeout)) {
1528b88163e3SLucas Stach 			dev_warn(gpu->dev,
1529b88163e3SLucas Stach 				 "timed out waiting for idle: idle=0x%x\n",
1530b88163e3SLucas Stach 				 idle);
1531b88163e3SLucas Stach 			return -ETIMEDOUT;
1532b88163e3SLucas Stach 		}
1533b88163e3SLucas Stach 
1534b88163e3SLucas Stach 		udelay(5);
1535b88163e3SLucas Stach 	} while (1);
1536b88163e3SLucas Stach }
1537b88163e3SLucas Stach 
1538a8c21a54SThe etnaviv authors static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
1539a8c21a54SThe etnaviv authors {
15402f9225dbSLucas Stach 	if (gpu->buffer.suballoc) {
1541a8c21a54SThe etnaviv authors 		/* Replace the last WAIT with END */
154240c27bdeSLucas Stach 		mutex_lock(&gpu->lock);
1543a8c21a54SThe etnaviv authors 		etnaviv_buffer_end(gpu);
154440c27bdeSLucas Stach 		mutex_unlock(&gpu->lock);
1545a8c21a54SThe etnaviv authors 
1546a8c21a54SThe etnaviv authors 		/*
1547a8c21a54SThe etnaviv authors 		 * We know that only the FE is busy here, this should
1548a8c21a54SThe etnaviv authors 		 * happen quickly (as the WAIT is only 200 cycles).  If
1549a8c21a54SThe etnaviv authors 		 * we fail, just warn and continue.
1550a8c21a54SThe etnaviv authors 		 */
1551b88163e3SLucas Stach 		etnaviv_gpu_wait_idle(gpu, 100);
1552a8c21a54SThe etnaviv authors 	}
1553a8c21a54SThe etnaviv authors 
1554a8c21a54SThe etnaviv authors 	return etnaviv_gpu_clk_disable(gpu);
1555a8c21a54SThe etnaviv authors }
1556a8c21a54SThe etnaviv authors 
1557a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1558a8c21a54SThe etnaviv authors static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
1559a8c21a54SThe etnaviv authors {
1560a8c21a54SThe etnaviv authors 	int ret;
1561a8c21a54SThe etnaviv authors 
1562a8c21a54SThe etnaviv authors 	ret = mutex_lock_killable(&gpu->lock);
1563a8c21a54SThe etnaviv authors 	if (ret)
1564a8c21a54SThe etnaviv authors 		return ret;
1565a8c21a54SThe etnaviv authors 
1566bcdfb5e5SRussell King 	etnaviv_gpu_update_clock(gpu);
1567a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
1568a8c21a54SThe etnaviv authors 
15694375ffffSLucas Stach 	gpu->lastctx = NULL;
1570f6086311SRussell King 	gpu->exec_state = -1;
1571a8c21a54SThe etnaviv authors 
1572a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1573a8c21a54SThe etnaviv authors 
1574a8c21a54SThe etnaviv authors 	return 0;
1575a8c21a54SThe etnaviv authors }
1576a8c21a54SThe etnaviv authors #endif
1577a8c21a54SThe etnaviv authors 
1578bcdfb5e5SRussell King static int
1579bcdfb5e5SRussell King etnaviv_gpu_cooling_get_max_state(struct thermal_cooling_device *cdev,
1580bcdfb5e5SRussell King 				  unsigned long *state)
1581bcdfb5e5SRussell King {
1582bcdfb5e5SRussell King 	*state = 6;
1583bcdfb5e5SRussell King 
1584bcdfb5e5SRussell King 	return 0;
1585bcdfb5e5SRussell King }
1586bcdfb5e5SRussell King 
1587bcdfb5e5SRussell King static int
1588bcdfb5e5SRussell King etnaviv_gpu_cooling_get_cur_state(struct thermal_cooling_device *cdev,
1589bcdfb5e5SRussell King 				  unsigned long *state)
1590bcdfb5e5SRussell King {
1591bcdfb5e5SRussell King 	struct etnaviv_gpu *gpu = cdev->devdata;
1592bcdfb5e5SRussell King 
1593bcdfb5e5SRussell King 	*state = gpu->freq_scale;
1594bcdfb5e5SRussell King 
1595bcdfb5e5SRussell King 	return 0;
1596bcdfb5e5SRussell King }
1597bcdfb5e5SRussell King 
1598bcdfb5e5SRussell King static int
1599bcdfb5e5SRussell King etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
1600bcdfb5e5SRussell King 				  unsigned long state)
1601bcdfb5e5SRussell King {
1602bcdfb5e5SRussell King 	struct etnaviv_gpu *gpu = cdev->devdata;
1603bcdfb5e5SRussell King 
1604bcdfb5e5SRussell King 	mutex_lock(&gpu->lock);
1605bcdfb5e5SRussell King 	gpu->freq_scale = state;
1606bcdfb5e5SRussell King 	if (!pm_runtime_suspended(gpu->dev))
1607bcdfb5e5SRussell King 		etnaviv_gpu_update_clock(gpu);
1608bcdfb5e5SRussell King 	mutex_unlock(&gpu->lock);
1609bcdfb5e5SRussell King 
1610bcdfb5e5SRussell King 	return 0;
1611bcdfb5e5SRussell King }
1612bcdfb5e5SRussell King 
1613bcdfb5e5SRussell King static struct thermal_cooling_device_ops cooling_ops = {
1614bcdfb5e5SRussell King 	.get_max_state = etnaviv_gpu_cooling_get_max_state,
1615bcdfb5e5SRussell King 	.get_cur_state = etnaviv_gpu_cooling_get_cur_state,
1616bcdfb5e5SRussell King 	.set_cur_state = etnaviv_gpu_cooling_set_cur_state,
1617bcdfb5e5SRussell King };
1618bcdfb5e5SRussell King 
1619a8c21a54SThe etnaviv authors static int etnaviv_gpu_bind(struct device *dev, struct device *master,
1620a8c21a54SThe etnaviv authors 	void *data)
1621a8c21a54SThe etnaviv authors {
1622a8c21a54SThe etnaviv authors 	struct drm_device *drm = data;
1623a8c21a54SThe etnaviv authors 	struct etnaviv_drm_private *priv = drm->dev_private;
1624a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1625a8c21a54SThe etnaviv authors 	int ret;
1626a8c21a54SThe etnaviv authors 
162749b82c38SPhilipp Zabel 	if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) {
1628bcdfb5e5SRussell King 		gpu->cooling = thermal_of_cooling_device_register(dev->of_node,
1629bcdfb5e5SRussell King 				(char *)dev_name(dev), gpu, &cooling_ops);
1630bcdfb5e5SRussell King 		if (IS_ERR(gpu->cooling))
1631bcdfb5e5SRussell King 			return PTR_ERR(gpu->cooling);
16325247e2aaSLucas Stach 	}
1633bcdfb5e5SRussell King 
1634a7790d78SLucas Stach 	gpu->wq = alloc_ordered_workqueue(dev_name(dev), 0);
1635a7790d78SLucas Stach 	if (!gpu->wq) {
1636e93b6deeSLucas Stach 		ret = -ENOMEM;
1637e93b6deeSLucas Stach 		goto out_thermal;
1638a7790d78SLucas Stach 	}
1639a7790d78SLucas Stach 
1640e93b6deeSLucas Stach 	ret = etnaviv_sched_init(gpu);
1641e93b6deeSLucas Stach 	if (ret)
1642e93b6deeSLucas Stach 		goto out_workqueue;
1643e93b6deeSLucas Stach 
1644a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1645a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
1646a8c21a54SThe etnaviv authors #else
1647a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_clk_enable(gpu);
1648a8c21a54SThe etnaviv authors #endif
1649e93b6deeSLucas Stach 	if (ret < 0)
1650e93b6deeSLucas Stach 		goto out_sched;
1651e93b6deeSLucas Stach 
1652a8c21a54SThe etnaviv authors 
1653a8c21a54SThe etnaviv authors 	gpu->drm = drm;
1654f54d1867SChris Wilson 	gpu->fence_context = dma_fence_context_alloc(1);
16558bc4d885SLucas Stach 	idr_init(&gpu->fence_idr);
1656a8c21a54SThe etnaviv authors 	spin_lock_init(&gpu->fence_spinlock);
1657a8c21a54SThe etnaviv authors 
1658357713ceSChristian Gmeiner 	INIT_WORK(&gpu->sync_point_work, sync_point_worker);
1659a8c21a54SThe etnaviv authors 	init_waitqueue_head(&gpu->fence_event);
1660a8c21a54SThe etnaviv authors 
1661a8c21a54SThe etnaviv authors 	priv->gpu[priv->num_gpus++] = gpu;
1662a8c21a54SThe etnaviv authors 
1663a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
1664a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
1665a8c21a54SThe etnaviv authors 
1666a8c21a54SThe etnaviv authors 	return 0;
1667e93b6deeSLucas Stach 
1668e93b6deeSLucas Stach out_sched:
1669e93b6deeSLucas Stach 	etnaviv_sched_fini(gpu);
1670e93b6deeSLucas Stach 
1671e93b6deeSLucas Stach out_workqueue:
1672e93b6deeSLucas Stach 	destroy_workqueue(gpu->wq);
1673e93b6deeSLucas Stach 
1674e93b6deeSLucas Stach out_thermal:
1675e93b6deeSLucas Stach 	if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
1676e93b6deeSLucas Stach 		thermal_cooling_device_unregister(gpu->cooling);
1677e93b6deeSLucas Stach 
1678e93b6deeSLucas Stach 	return ret;
1679a8c21a54SThe etnaviv authors }
1680a8c21a54SThe etnaviv authors 
1681a8c21a54SThe etnaviv authors static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
1682a8c21a54SThe etnaviv authors 	void *data)
1683a8c21a54SThe etnaviv authors {
1684a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1685a8c21a54SThe etnaviv authors 
1686a8c21a54SThe etnaviv authors 	DBG("%s", dev_name(gpu->dev));
1687a8c21a54SThe etnaviv authors 
1688a7790d78SLucas Stach 	flush_workqueue(gpu->wq);
1689a7790d78SLucas Stach 	destroy_workqueue(gpu->wq);
1690a7790d78SLucas Stach 
1691e93b6deeSLucas Stach 	etnaviv_sched_fini(gpu);
1692e93b6deeSLucas Stach 
1693a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1694a8c21a54SThe etnaviv authors 	pm_runtime_get_sync(gpu->dev);
1695a8c21a54SThe etnaviv authors 	pm_runtime_put_sync_suspend(gpu->dev);
1696a8c21a54SThe etnaviv authors #else
1697a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_suspend(gpu);
1698a8c21a54SThe etnaviv authors #endif
1699a8c21a54SThe etnaviv authors 
17002f9225dbSLucas Stach 	if (gpu->buffer.suballoc)
17012f9225dbSLucas Stach 		etnaviv_cmdbuf_free(&gpu->buffer);
1702a8c21a54SThe etnaviv authors 
1703e66774ddSLucas Stach 	if (gpu->cmdbuf_suballoc) {
1704e66774ddSLucas Stach 		etnaviv_cmdbuf_suballoc_destroy(gpu->cmdbuf_suballoc);
1705e66774ddSLucas Stach 		gpu->cmdbuf_suballoc = NULL;
1706e66774ddSLucas Stach 	}
1707e66774ddSLucas Stach 
1708a8c21a54SThe etnaviv authors 	if (gpu->mmu) {
1709a8c21a54SThe etnaviv authors 		etnaviv_iommu_destroy(gpu->mmu);
1710a8c21a54SThe etnaviv authors 		gpu->mmu = NULL;
1711a8c21a54SThe etnaviv authors 	}
1712a8c21a54SThe etnaviv authors 
1713a8c21a54SThe etnaviv authors 	gpu->drm = NULL;
17148bc4d885SLucas Stach 	idr_destroy(&gpu->fence_idr);
1715bcdfb5e5SRussell King 
171649b82c38SPhilipp Zabel 	if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
1717bcdfb5e5SRussell King 		thermal_cooling_device_unregister(gpu->cooling);
1718bcdfb5e5SRussell King 	gpu->cooling = NULL;
1719a8c21a54SThe etnaviv authors }
1720a8c21a54SThe etnaviv authors 
1721a8c21a54SThe etnaviv authors static const struct component_ops gpu_ops = {
1722a8c21a54SThe etnaviv authors 	.bind = etnaviv_gpu_bind,
1723a8c21a54SThe etnaviv authors 	.unbind = etnaviv_gpu_unbind,
1724a8c21a54SThe etnaviv authors };
1725a8c21a54SThe etnaviv authors 
1726a8c21a54SThe etnaviv authors static const struct of_device_id etnaviv_gpu_match[] = {
1727a8c21a54SThe etnaviv authors 	{
1728a8c21a54SThe etnaviv authors 		.compatible = "vivante,gc"
1729a8c21a54SThe etnaviv authors 	},
1730a8c21a54SThe etnaviv authors 	{ /* sentinel */ }
1731a8c21a54SThe etnaviv authors };
1732246774d1SLucas Stach MODULE_DEVICE_TABLE(of, etnaviv_gpu_match);
1733a8c21a54SThe etnaviv authors 
1734a8c21a54SThe etnaviv authors static int etnaviv_gpu_platform_probe(struct platform_device *pdev)
1735a8c21a54SThe etnaviv authors {
1736a8c21a54SThe etnaviv authors 	struct device *dev = &pdev->dev;
1737a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
1738a98b1e78SLucas Stach 	struct resource *res;
1739dc227890SFabio Estevam 	int err;
1740a8c21a54SThe etnaviv authors 
1741a8c21a54SThe etnaviv authors 	gpu = devm_kzalloc(dev, sizeof(*gpu), GFP_KERNEL);
1742a8c21a54SThe etnaviv authors 	if (!gpu)
1743a8c21a54SThe etnaviv authors 		return -ENOMEM;
1744a8c21a54SThe etnaviv authors 
1745a8c21a54SThe etnaviv authors 	gpu->dev = &pdev->dev;
1746a8c21a54SThe etnaviv authors 	mutex_init(&gpu->lock);
1747e93b6deeSLucas Stach 	mutex_init(&gpu->fence_idr_lock);
1748a8c21a54SThe etnaviv authors 
1749a8c21a54SThe etnaviv authors 	/* Map registers: */
1750a98b1e78SLucas Stach 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1751a98b1e78SLucas Stach 	gpu->mmio = devm_ioremap_resource(&pdev->dev, res);
1752a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->mmio))
1753a8c21a54SThe etnaviv authors 		return PTR_ERR(gpu->mmio);
1754a8c21a54SThe etnaviv authors 
1755a8c21a54SThe etnaviv authors 	/* Get Interrupt: */
1756a8c21a54SThe etnaviv authors 	gpu->irq = platform_get_irq(pdev, 0);
1757a8c21a54SThe etnaviv authors 	if (gpu->irq < 0) {
1758db60eda3SFabio Estevam 		dev_err(dev, "failed to get irq: %d\n", gpu->irq);
1759db60eda3SFabio Estevam 		return gpu->irq;
1760a8c21a54SThe etnaviv authors 	}
1761a8c21a54SThe etnaviv authors 
1762a8c21a54SThe etnaviv authors 	err = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 0,
1763a8c21a54SThe etnaviv authors 			       dev_name(gpu->dev), gpu);
1764a8c21a54SThe etnaviv authors 	if (err) {
1765a8c21a54SThe etnaviv authors 		dev_err(dev, "failed to request IRQ%u: %d\n", gpu->irq, err);
1766db60eda3SFabio Estevam 		return err;
1767a8c21a54SThe etnaviv authors 	}
1768a8c21a54SThe etnaviv authors 
1769a8c21a54SThe etnaviv authors 	/* Get Clocks: */
177065f037e8SLucas Stach 	gpu->clk_reg = devm_clk_get(&pdev->dev, "reg");
177165f037e8SLucas Stach 	DBG("clk_reg: %p", gpu->clk_reg);
177265f037e8SLucas Stach 	if (IS_ERR(gpu->clk_reg))
177365f037e8SLucas Stach 		gpu->clk_reg = NULL;
177465f037e8SLucas Stach 
1775a8c21a54SThe etnaviv authors 	gpu->clk_bus = devm_clk_get(&pdev->dev, "bus");
1776a8c21a54SThe etnaviv authors 	DBG("clk_bus: %p", gpu->clk_bus);
1777a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_bus))
1778a8c21a54SThe etnaviv authors 		gpu->clk_bus = NULL;
1779a8c21a54SThe etnaviv authors 
1780a8c21a54SThe etnaviv authors 	gpu->clk_core = devm_clk_get(&pdev->dev, "core");
1781a8c21a54SThe etnaviv authors 	DBG("clk_core: %p", gpu->clk_core);
1782a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_core))
1783a8c21a54SThe etnaviv authors 		gpu->clk_core = NULL;
1784d79fd1ccSLucas Stach 	gpu->base_rate_core = clk_get_rate(gpu->clk_core);
1785a8c21a54SThe etnaviv authors 
1786a8c21a54SThe etnaviv authors 	gpu->clk_shader = devm_clk_get(&pdev->dev, "shader");
1787a8c21a54SThe etnaviv authors 	DBG("clk_shader: %p", gpu->clk_shader);
1788a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_shader))
1789a8c21a54SThe etnaviv authors 		gpu->clk_shader = NULL;
1790d79fd1ccSLucas Stach 	gpu->base_rate_shader = clk_get_rate(gpu->clk_shader);
1791a8c21a54SThe etnaviv authors 
1792a8c21a54SThe etnaviv authors 	/* TODO: figure out max mapped size */
1793a8c21a54SThe etnaviv authors 	dev_set_drvdata(dev, gpu);
1794a8c21a54SThe etnaviv authors 
1795a8c21a54SThe etnaviv authors 	/*
1796a8c21a54SThe etnaviv authors 	 * We treat the device as initially suspended.  The runtime PM
1797a8c21a54SThe etnaviv authors 	 * autosuspend delay is rather arbitary: no measurements have
1798a8c21a54SThe etnaviv authors 	 * yet been performed to determine an appropriate value.
1799a8c21a54SThe etnaviv authors 	 */
1800a8c21a54SThe etnaviv authors 	pm_runtime_use_autosuspend(gpu->dev);
1801a8c21a54SThe etnaviv authors 	pm_runtime_set_autosuspend_delay(gpu->dev, 200);
1802a8c21a54SThe etnaviv authors 	pm_runtime_enable(gpu->dev);
1803a8c21a54SThe etnaviv authors 
1804a8c21a54SThe etnaviv authors 	err = component_add(&pdev->dev, &gpu_ops);
1805a8c21a54SThe etnaviv authors 	if (err < 0) {
1806a8c21a54SThe etnaviv authors 		dev_err(&pdev->dev, "failed to register component: %d\n", err);
1807db60eda3SFabio Estevam 		return err;
1808a8c21a54SThe etnaviv authors 	}
1809a8c21a54SThe etnaviv authors 
1810a8c21a54SThe etnaviv authors 	return 0;
1811a8c21a54SThe etnaviv authors }
1812a8c21a54SThe etnaviv authors 
1813a8c21a54SThe etnaviv authors static int etnaviv_gpu_platform_remove(struct platform_device *pdev)
1814a8c21a54SThe etnaviv authors {
1815a8c21a54SThe etnaviv authors 	component_del(&pdev->dev, &gpu_ops);
1816a8c21a54SThe etnaviv authors 	pm_runtime_disable(&pdev->dev);
1817a8c21a54SThe etnaviv authors 	return 0;
1818a8c21a54SThe etnaviv authors }
1819a8c21a54SThe etnaviv authors 
1820a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1821a8c21a54SThe etnaviv authors static int etnaviv_gpu_rpm_suspend(struct device *dev)
1822a8c21a54SThe etnaviv authors {
1823a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1824a8c21a54SThe etnaviv authors 	u32 idle, mask;
1825a8c21a54SThe etnaviv authors 
1826a8c21a54SThe etnaviv authors 	/* If we have outstanding fences, we're not idle */
1827a8c21a54SThe etnaviv authors 	if (gpu->completed_fence != gpu->active_fence)
1828a8c21a54SThe etnaviv authors 		return -EBUSY;
1829a8c21a54SThe etnaviv authors 
1830a8c21a54SThe etnaviv authors 	/* Check whether the hardware (except FE) is idle */
1831a8c21a54SThe etnaviv authors 	mask = gpu->idle_mask & ~VIVS_HI_IDLE_STATE_FE;
1832a8c21a54SThe etnaviv authors 	idle = gpu_read(gpu, VIVS_HI_IDLE_STATE) & mask;
1833a8c21a54SThe etnaviv authors 	if (idle != mask)
1834a8c21a54SThe etnaviv authors 		return -EBUSY;
1835a8c21a54SThe etnaviv authors 
1836a8c21a54SThe etnaviv authors 	return etnaviv_gpu_hw_suspend(gpu);
1837a8c21a54SThe etnaviv authors }
1838a8c21a54SThe etnaviv authors 
1839a8c21a54SThe etnaviv authors static int etnaviv_gpu_rpm_resume(struct device *dev)
1840a8c21a54SThe etnaviv authors {
1841a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1842a8c21a54SThe etnaviv authors 	int ret;
1843a8c21a54SThe etnaviv authors 
1844a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_clk_enable(gpu);
1845a8c21a54SThe etnaviv authors 	if (ret)
1846a8c21a54SThe etnaviv authors 		return ret;
1847a8c21a54SThe etnaviv authors 
1848a8c21a54SThe etnaviv authors 	/* Re-initialise the basic hardware state */
18492f9225dbSLucas Stach 	if (gpu->drm && gpu->buffer.suballoc) {
1850a8c21a54SThe etnaviv authors 		ret = etnaviv_gpu_hw_resume(gpu);
1851a8c21a54SThe etnaviv authors 		if (ret) {
1852a8c21a54SThe etnaviv authors 			etnaviv_gpu_clk_disable(gpu);
1853a8c21a54SThe etnaviv authors 			return ret;
1854a8c21a54SThe etnaviv authors 		}
1855a8c21a54SThe etnaviv authors 	}
1856a8c21a54SThe etnaviv authors 
1857a8c21a54SThe etnaviv authors 	return 0;
1858a8c21a54SThe etnaviv authors }
1859a8c21a54SThe etnaviv authors #endif
1860a8c21a54SThe etnaviv authors 
1861a8c21a54SThe etnaviv authors static const struct dev_pm_ops etnaviv_gpu_pm_ops = {
1862a8c21a54SThe etnaviv authors 	SET_RUNTIME_PM_OPS(etnaviv_gpu_rpm_suspend, etnaviv_gpu_rpm_resume,
1863a8c21a54SThe etnaviv authors 			   NULL)
1864a8c21a54SThe etnaviv authors };
1865a8c21a54SThe etnaviv authors 
1866a8c21a54SThe etnaviv authors struct platform_driver etnaviv_gpu_driver = {
1867a8c21a54SThe etnaviv authors 	.driver = {
1868a8c21a54SThe etnaviv authors 		.name = "etnaviv-gpu",
1869a8c21a54SThe etnaviv authors 		.owner = THIS_MODULE,
1870a8c21a54SThe etnaviv authors 		.pm = &etnaviv_gpu_pm_ops,
1871a8c21a54SThe etnaviv authors 		.of_match_table = etnaviv_gpu_match,
1872a8c21a54SThe etnaviv authors 	},
1873a8c21a54SThe etnaviv authors 	.probe = etnaviv_gpu_platform_probe,
1874a8c21a54SThe etnaviv authors 	.remove = etnaviv_gpu_platform_remove,
1875a8c21a54SThe etnaviv authors 	.id_table = gpu_ids,
1876a8c21a54SThe etnaviv authors };
1877