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>
18a8c21a54SThe etnaviv authors #include <linux/fence.h>
19a8c21a54SThe etnaviv authors #include <linux/moduleparam.h>
20a8c21a54SThe etnaviv authors #include <linux/of_device.h>
21a8c21a54SThe etnaviv authors #include "etnaviv_dump.h"
22a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
23a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
24a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
25a8c21a54SThe etnaviv authors #include "etnaviv_iommu.h"
26a8c21a54SThe etnaviv authors #include "etnaviv_iommu_v2.h"
27a8c21a54SThe etnaviv authors #include "common.xml.h"
28a8c21a54SThe etnaviv authors #include "state.xml.h"
29a8c21a54SThe etnaviv authors #include "state_hi.xml.h"
30a8c21a54SThe etnaviv authors #include "cmdstream.xml.h"
31a8c21a54SThe etnaviv authors 
32a8c21a54SThe etnaviv authors static const struct platform_device_id gpu_ids[] = {
33a8c21a54SThe etnaviv authors 	{ .name = "etnaviv-gpu,2d" },
34a8c21a54SThe etnaviv authors 	{ },
35a8c21a54SThe etnaviv authors };
36a8c21a54SThe etnaviv authors 
37a8c21a54SThe etnaviv authors static bool etnaviv_dump_core = true;
38a8c21a54SThe etnaviv authors module_param_named(dump_core, etnaviv_dump_core, bool, 0600);
39a8c21a54SThe etnaviv authors 
40a8c21a54SThe etnaviv authors /*
41a8c21a54SThe etnaviv authors  * Driver functions:
42a8c21a54SThe etnaviv authors  */
43a8c21a54SThe etnaviv authors 
44a8c21a54SThe etnaviv authors int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value)
45a8c21a54SThe etnaviv authors {
46a8c21a54SThe etnaviv authors 	switch (param) {
47a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_MODEL:
48a8c21a54SThe etnaviv authors 		*value = gpu->identity.model;
49a8c21a54SThe etnaviv authors 		break;
50a8c21a54SThe etnaviv authors 
51a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_REVISION:
52a8c21a54SThe etnaviv authors 		*value = gpu->identity.revision;
53a8c21a54SThe etnaviv authors 		break;
54a8c21a54SThe etnaviv authors 
55a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_0:
56a8c21a54SThe etnaviv authors 		*value = gpu->identity.features;
57a8c21a54SThe etnaviv authors 		break;
58a8c21a54SThe etnaviv authors 
59a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_1:
60a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features0;
61a8c21a54SThe etnaviv authors 		break;
62a8c21a54SThe etnaviv authors 
63a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_2:
64a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features1;
65a8c21a54SThe etnaviv authors 		break;
66a8c21a54SThe etnaviv authors 
67a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_3:
68a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features2;
69a8c21a54SThe etnaviv authors 		break;
70a8c21a54SThe etnaviv authors 
71a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_FEATURES_4:
72a8c21a54SThe etnaviv authors 		*value = gpu->identity.minor_features3;
73a8c21a54SThe etnaviv authors 		break;
74a8c21a54SThe etnaviv authors 
75602eb489SRussell King 	case ETNAVIV_PARAM_GPU_FEATURES_5:
76602eb489SRussell King 		*value = gpu->identity.minor_features4;
77602eb489SRussell King 		break;
78602eb489SRussell King 
79602eb489SRussell King 	case ETNAVIV_PARAM_GPU_FEATURES_6:
80602eb489SRussell King 		*value = gpu->identity.minor_features5;
81602eb489SRussell King 		break;
82602eb489SRussell King 
83a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_STREAM_COUNT:
84a8c21a54SThe etnaviv authors 		*value = gpu->identity.stream_count;
85a8c21a54SThe etnaviv authors 		break;
86a8c21a54SThe etnaviv authors 
87a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_REGISTER_MAX:
88a8c21a54SThe etnaviv authors 		*value = gpu->identity.register_max;
89a8c21a54SThe etnaviv authors 		break;
90a8c21a54SThe etnaviv authors 
91a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_THREAD_COUNT:
92a8c21a54SThe etnaviv authors 		*value = gpu->identity.thread_count;
93a8c21a54SThe etnaviv authors 		break;
94a8c21a54SThe etnaviv authors 
95a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_VERTEX_CACHE_SIZE:
96a8c21a54SThe etnaviv authors 		*value = gpu->identity.vertex_cache_size;
97a8c21a54SThe etnaviv authors 		break;
98a8c21a54SThe etnaviv authors 
99a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_SHADER_CORE_COUNT:
100a8c21a54SThe etnaviv authors 		*value = gpu->identity.shader_core_count;
101a8c21a54SThe etnaviv authors 		break;
102a8c21a54SThe etnaviv authors 
103a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_PIXEL_PIPES:
104a8c21a54SThe etnaviv authors 		*value = gpu->identity.pixel_pipes;
105a8c21a54SThe etnaviv authors 		break;
106a8c21a54SThe etnaviv authors 
107a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_VERTEX_OUTPUT_BUFFER_SIZE:
108a8c21a54SThe etnaviv authors 		*value = gpu->identity.vertex_output_buffer_size;
109a8c21a54SThe etnaviv authors 		break;
110a8c21a54SThe etnaviv authors 
111a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_BUFFER_SIZE:
112a8c21a54SThe etnaviv authors 		*value = gpu->identity.buffer_size;
113a8c21a54SThe etnaviv authors 		break;
114a8c21a54SThe etnaviv authors 
115a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_INSTRUCTION_COUNT:
116a8c21a54SThe etnaviv authors 		*value = gpu->identity.instruction_count;
117a8c21a54SThe etnaviv authors 		break;
118a8c21a54SThe etnaviv authors 
119a8c21a54SThe etnaviv authors 	case ETNAVIV_PARAM_GPU_NUM_CONSTANTS:
120a8c21a54SThe etnaviv authors 		*value = gpu->identity.num_constants;
121a8c21a54SThe etnaviv authors 		break;
122a8c21a54SThe etnaviv authors 
123602eb489SRussell King 	case ETNAVIV_PARAM_GPU_NUM_VARYINGS:
124602eb489SRussell King 		*value = gpu->identity.varyings_count;
125602eb489SRussell King 		break;
126602eb489SRussell King 
127a8c21a54SThe etnaviv authors 	default:
128a8c21a54SThe etnaviv authors 		DBG("%s: invalid param: %u", dev_name(gpu->dev), param);
129a8c21a54SThe etnaviv authors 		return -EINVAL;
130a8c21a54SThe etnaviv authors 	}
131a8c21a54SThe etnaviv authors 
132a8c21a54SThe etnaviv authors 	return 0;
133a8c21a54SThe etnaviv authors }
134a8c21a54SThe etnaviv authors 
135472f79dcSRussell King 
136472f79dcSRussell King #define etnaviv_is_model_rev(gpu, mod, rev) \
137472f79dcSRussell King 	((gpu)->identity.model == chipModel_##mod && \
138472f79dcSRussell King 	 (gpu)->identity.revision == rev)
13952f36ba1SRussell King #define etnaviv_field(val, field) \
14052f36ba1SRussell King 	(((val) & field##__MASK) >> field##__SHIFT)
14152f36ba1SRussell King 
142a8c21a54SThe etnaviv authors static void etnaviv_hw_specs(struct etnaviv_gpu *gpu)
143a8c21a54SThe etnaviv authors {
144a8c21a54SThe etnaviv authors 	if (gpu->identity.minor_features0 &
145a8c21a54SThe etnaviv authors 	    chipMinorFeatures0_MORE_MINOR_FEATURES) {
146602eb489SRussell King 		u32 specs[4];
147602eb489SRussell King 		unsigned int streams;
148a8c21a54SThe etnaviv authors 
149a8c21a54SThe etnaviv authors 		specs[0] = gpu_read(gpu, VIVS_HI_CHIP_SPECS);
150a8c21a54SThe etnaviv authors 		specs[1] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_2);
151602eb489SRussell King 		specs[2] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_3);
152602eb489SRussell King 		specs[3] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_4);
153a8c21a54SThe etnaviv authors 
15452f36ba1SRussell King 		gpu->identity.stream_count = etnaviv_field(specs[0],
15552f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_STREAM_COUNT);
15652f36ba1SRussell King 		gpu->identity.register_max = etnaviv_field(specs[0],
15752f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_REGISTER_MAX);
15852f36ba1SRussell King 		gpu->identity.thread_count = etnaviv_field(specs[0],
15952f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_THREAD_COUNT);
16052f36ba1SRussell King 		gpu->identity.vertex_cache_size = etnaviv_field(specs[0],
16152f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_VERTEX_CACHE_SIZE);
16252f36ba1SRussell King 		gpu->identity.shader_core_count = etnaviv_field(specs[0],
16352f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_SHADER_CORE_COUNT);
16452f36ba1SRussell King 		gpu->identity.pixel_pipes = etnaviv_field(specs[0],
16552f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_PIXEL_PIPES);
166a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size =
16752f36ba1SRussell King 			etnaviv_field(specs[0],
16852f36ba1SRussell King 				VIVS_HI_CHIP_SPECS_VERTEX_OUTPUT_BUFFER_SIZE);
169a8c21a54SThe etnaviv authors 
17052f36ba1SRussell King 		gpu->identity.buffer_size = etnaviv_field(specs[1],
17152f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_BUFFER_SIZE);
17252f36ba1SRussell King 		gpu->identity.instruction_count = etnaviv_field(specs[1],
17352f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_INSTRUCTION_COUNT);
17452f36ba1SRussell King 		gpu->identity.num_constants = etnaviv_field(specs[1],
17552f36ba1SRussell King 					VIVS_HI_CHIP_SPECS_2_NUM_CONSTANTS);
176602eb489SRussell King 
177602eb489SRussell King 		gpu->identity.varyings_count = etnaviv_field(specs[2],
178602eb489SRussell King 					VIVS_HI_CHIP_SPECS_3_VARYINGS_COUNT);
179602eb489SRussell King 
180602eb489SRussell King 		/* This overrides the value from older register if non-zero */
181602eb489SRussell King 		streams = etnaviv_field(specs[3],
182602eb489SRussell King 					VIVS_HI_CHIP_SPECS_4_STREAM_COUNT);
183602eb489SRussell King 		if (streams)
184602eb489SRussell King 			gpu->identity.stream_count = streams;
185a8c21a54SThe etnaviv authors 	}
186a8c21a54SThe etnaviv authors 
187a8c21a54SThe etnaviv authors 	/* Fill in the stream count if not specified */
188a8c21a54SThe etnaviv authors 	if (gpu->identity.stream_count == 0) {
189a8c21a54SThe etnaviv authors 		if (gpu->identity.model >= 0x1000)
190a8c21a54SThe etnaviv authors 			gpu->identity.stream_count = 4;
191a8c21a54SThe etnaviv authors 		else
192a8c21a54SThe etnaviv authors 			gpu->identity.stream_count = 1;
193a8c21a54SThe etnaviv authors 	}
194a8c21a54SThe etnaviv authors 
195a8c21a54SThe etnaviv authors 	/* Convert the register max value */
196a8c21a54SThe etnaviv authors 	if (gpu->identity.register_max)
197a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 1 << gpu->identity.register_max;
198507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC400)
199a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 32;
200a8c21a54SThe etnaviv authors 	else
201a8c21a54SThe etnaviv authors 		gpu->identity.register_max = 64;
202a8c21a54SThe etnaviv authors 
203a8c21a54SThe etnaviv authors 	/* Convert thread count */
204a8c21a54SThe etnaviv authors 	if (gpu->identity.thread_count)
205a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 1 << gpu->identity.thread_count;
206507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC400)
207a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 64;
208507f8991SRussell King 	else if (gpu->identity.model == chipModel_GC500 ||
209507f8991SRussell King 		 gpu->identity.model == chipModel_GC530)
210a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 128;
211a8c21a54SThe etnaviv authors 	else
212a8c21a54SThe etnaviv authors 		gpu->identity.thread_count = 256;
213a8c21a54SThe etnaviv authors 
214a8c21a54SThe etnaviv authors 	if (gpu->identity.vertex_cache_size == 0)
215a8c21a54SThe etnaviv authors 		gpu->identity.vertex_cache_size = 8;
216a8c21a54SThe etnaviv authors 
217a8c21a54SThe etnaviv authors 	if (gpu->identity.shader_core_count == 0) {
218a8c21a54SThe etnaviv authors 		if (gpu->identity.model >= 0x1000)
219a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count = 2;
220a8c21a54SThe etnaviv authors 		else
221a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count = 1;
222a8c21a54SThe etnaviv authors 	}
223a8c21a54SThe etnaviv authors 
224a8c21a54SThe etnaviv authors 	if (gpu->identity.pixel_pipes == 0)
225a8c21a54SThe etnaviv authors 		gpu->identity.pixel_pipes = 1;
226a8c21a54SThe etnaviv authors 
227a8c21a54SThe etnaviv authors 	/* Convert virtex buffer size */
228a8c21a54SThe etnaviv authors 	if (gpu->identity.vertex_output_buffer_size) {
229a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size =
230a8c21a54SThe etnaviv authors 			1 << gpu->identity.vertex_output_buffer_size;
231507f8991SRussell King 	} else if (gpu->identity.model == chipModel_GC400) {
232a8c21a54SThe etnaviv authors 		if (gpu->identity.revision < 0x4000)
233a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 512;
234a8c21a54SThe etnaviv authors 		else if (gpu->identity.revision < 0x4200)
235a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 256;
236a8c21a54SThe etnaviv authors 		else
237a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size = 128;
238a8c21a54SThe etnaviv authors 	} else {
239a8c21a54SThe etnaviv authors 		gpu->identity.vertex_output_buffer_size = 512;
240a8c21a54SThe etnaviv authors 	}
241a8c21a54SThe etnaviv authors 
242a8c21a54SThe etnaviv authors 	switch (gpu->identity.instruction_count) {
243a8c21a54SThe etnaviv authors 	case 0:
244472f79dcSRussell King 		if (etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
245507f8991SRussell King 		    gpu->identity.model == chipModel_GC880)
246a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count = 512;
247a8c21a54SThe etnaviv authors 		else
248a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count = 256;
249a8c21a54SThe etnaviv authors 		break;
250a8c21a54SThe etnaviv authors 
251a8c21a54SThe etnaviv authors 	case 1:
252a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 1024;
253a8c21a54SThe etnaviv authors 		break;
254a8c21a54SThe etnaviv authors 
255a8c21a54SThe etnaviv authors 	case 2:
256a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 2048;
257a8c21a54SThe etnaviv authors 		break;
258a8c21a54SThe etnaviv authors 
259a8c21a54SThe etnaviv authors 	default:
260a8c21a54SThe etnaviv authors 		gpu->identity.instruction_count = 256;
261a8c21a54SThe etnaviv authors 		break;
262a8c21a54SThe etnaviv authors 	}
263a8c21a54SThe etnaviv authors 
264a8c21a54SThe etnaviv authors 	if (gpu->identity.num_constants == 0)
265a8c21a54SThe etnaviv authors 		gpu->identity.num_constants = 168;
266602eb489SRussell King 
267602eb489SRussell King 	if (gpu->identity.varyings_count == 0) {
268602eb489SRussell King 		if (gpu->identity.minor_features1 & chipMinorFeatures1_HALTI0)
269602eb489SRussell King 			gpu->identity.varyings_count = 12;
270602eb489SRussell King 		else
271602eb489SRussell King 			gpu->identity.varyings_count = 8;
272602eb489SRussell King 	}
273602eb489SRussell King 
274602eb489SRussell King 	/*
275602eb489SRussell King 	 * For some cores, two varyings are consumed for position, so the
276602eb489SRussell King 	 * maximum varying count needs to be reduced by one.
277602eb489SRussell King 	 */
278602eb489SRussell King 	if (etnaviv_is_model_rev(gpu, GC5000, 0x5434) ||
279602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
280602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5245) ||
281602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
282602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC3000, 0x5435) ||
283602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2200, 0x5244) ||
284602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2100, 0x5108) ||
285602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
286602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC1500, 0x5246) ||
287602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC880, 0x5107) ||
288602eb489SRussell King 	    etnaviv_is_model_rev(gpu, GC880, 0x5106))
289602eb489SRussell King 		gpu->identity.varyings_count -= 1;
290a8c21a54SThe etnaviv authors }
291a8c21a54SThe etnaviv authors 
292a8c21a54SThe etnaviv authors static void etnaviv_hw_identify(struct etnaviv_gpu *gpu)
293a8c21a54SThe etnaviv authors {
294a8c21a54SThe etnaviv authors 	u32 chipIdentity;
295a8c21a54SThe etnaviv authors 
296a8c21a54SThe etnaviv authors 	chipIdentity = gpu_read(gpu, VIVS_HI_CHIP_IDENTITY);
297a8c21a54SThe etnaviv authors 
298a8c21a54SThe etnaviv authors 	/* Special case for older graphic cores. */
29952f36ba1SRussell King 	if (etnaviv_field(chipIdentity, VIVS_HI_CHIP_IDENTITY_FAMILY) == 0x01) {
300507f8991SRussell King 		gpu->identity.model    = chipModel_GC500;
30152f36ba1SRussell King 		gpu->identity.revision = etnaviv_field(chipIdentity,
30252f36ba1SRussell King 					 VIVS_HI_CHIP_IDENTITY_REVISION);
303a8c21a54SThe etnaviv authors 	} else {
304a8c21a54SThe etnaviv authors 
305a8c21a54SThe etnaviv authors 		gpu->identity.model = gpu_read(gpu, VIVS_HI_CHIP_MODEL);
306a8c21a54SThe etnaviv authors 		gpu->identity.revision = gpu_read(gpu, VIVS_HI_CHIP_REV);
307a8c21a54SThe etnaviv authors 
308a8c21a54SThe etnaviv authors 		/*
309a8c21a54SThe etnaviv authors 		 * !!!! HACK ALERT !!!!
310a8c21a54SThe etnaviv authors 		 * Because people change device IDs without letting software
311a8c21a54SThe etnaviv authors 		 * know about it - here is the hack to make it all look the
312a8c21a54SThe etnaviv authors 		 * same.  Only for GC400 family.
313a8c21a54SThe etnaviv authors 		 */
314a8c21a54SThe etnaviv authors 		if ((gpu->identity.model & 0xff00) == 0x0400 &&
315507f8991SRussell King 		    gpu->identity.model != chipModel_GC420) {
316a8c21a54SThe etnaviv authors 			gpu->identity.model = gpu->identity.model & 0x0400;
317a8c21a54SThe etnaviv authors 		}
318a8c21a54SThe etnaviv authors 
319a8c21a54SThe etnaviv authors 		/* Another special case */
320472f79dcSRussell King 		if (etnaviv_is_model_rev(gpu, GC300, 0x2201)) {
321a8c21a54SThe etnaviv authors 			u32 chipDate = gpu_read(gpu, VIVS_HI_CHIP_DATE);
322a8c21a54SThe etnaviv authors 			u32 chipTime = gpu_read(gpu, VIVS_HI_CHIP_TIME);
323a8c21a54SThe etnaviv authors 
324a8c21a54SThe etnaviv authors 			if (chipDate == 0x20080814 && chipTime == 0x12051100) {
325a8c21a54SThe etnaviv authors 				/*
326a8c21a54SThe etnaviv authors 				 * This IP has an ECO; put the correct
327a8c21a54SThe etnaviv authors 				 * revision in it.
328a8c21a54SThe etnaviv authors 				 */
329a8c21a54SThe etnaviv authors 				gpu->identity.revision = 0x1051;
330a8c21a54SThe etnaviv authors 			}
331a8c21a54SThe etnaviv authors 		}
332a8c21a54SThe etnaviv authors 	}
333a8c21a54SThe etnaviv authors 
334a8c21a54SThe etnaviv authors 	dev_info(gpu->dev, "model: GC%x, revision: %x\n",
335a8c21a54SThe etnaviv authors 		 gpu->identity.model, gpu->identity.revision);
336a8c21a54SThe etnaviv authors 
337a8c21a54SThe etnaviv authors 	gpu->identity.features = gpu_read(gpu, VIVS_HI_CHIP_FEATURE);
338a8c21a54SThe etnaviv authors 
339a8c21a54SThe etnaviv authors 	/* Disable fast clear on GC700. */
340507f8991SRussell King 	if (gpu->identity.model == chipModel_GC700)
341a8c21a54SThe etnaviv authors 		gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
342a8c21a54SThe etnaviv authors 
343507f8991SRussell King 	if ((gpu->identity.model == chipModel_GC500 &&
344507f8991SRussell King 	     gpu->identity.revision < 2) ||
345507f8991SRussell King 	    (gpu->identity.model == chipModel_GC300 &&
346507f8991SRussell King 	     gpu->identity.revision < 0x2000)) {
347a8c21a54SThe etnaviv authors 
348a8c21a54SThe etnaviv authors 		/*
349a8c21a54SThe etnaviv authors 		 * GC500 rev 1.x and GC300 rev < 2.0 doesn't have these
350a8c21a54SThe etnaviv authors 		 * registers.
351a8c21a54SThe etnaviv authors 		 */
352a8c21a54SThe etnaviv authors 		gpu->identity.minor_features0 = 0;
353a8c21a54SThe etnaviv authors 		gpu->identity.minor_features1 = 0;
354a8c21a54SThe etnaviv authors 		gpu->identity.minor_features2 = 0;
355a8c21a54SThe etnaviv authors 		gpu->identity.minor_features3 = 0;
356602eb489SRussell King 		gpu->identity.minor_features4 = 0;
357602eb489SRussell King 		gpu->identity.minor_features5 = 0;
358a8c21a54SThe etnaviv authors 	} else
359a8c21a54SThe etnaviv authors 		gpu->identity.minor_features0 =
360a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_0);
361a8c21a54SThe etnaviv authors 
362a8c21a54SThe etnaviv authors 	if (gpu->identity.minor_features0 &
363a8c21a54SThe etnaviv authors 	    chipMinorFeatures0_MORE_MINOR_FEATURES) {
364a8c21a54SThe etnaviv authors 		gpu->identity.minor_features1 =
365a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_1);
366a8c21a54SThe etnaviv authors 		gpu->identity.minor_features2 =
367a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_2);
368a8c21a54SThe etnaviv authors 		gpu->identity.minor_features3 =
369a8c21a54SThe etnaviv authors 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_3);
370602eb489SRussell King 		gpu->identity.minor_features4 =
371602eb489SRussell King 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_4);
372602eb489SRussell King 		gpu->identity.minor_features5 =
373602eb489SRussell King 				gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_5);
374a8c21a54SThe etnaviv authors 	}
375a8c21a54SThe etnaviv authors 
376a8c21a54SThe etnaviv authors 	/* GC600 idle register reports zero bits where modules aren't present */
377a8c21a54SThe etnaviv authors 	if (gpu->identity.model == chipModel_GC600) {
378a8c21a54SThe etnaviv authors 		gpu->idle_mask = VIVS_HI_IDLE_STATE_TX |
379a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_RA |
380a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_SE |
381a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_PA |
382a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_SH |
383a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_PE |
384a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_DE |
385a8c21a54SThe etnaviv authors 				 VIVS_HI_IDLE_STATE_FE;
386a8c21a54SThe etnaviv authors 	} else {
387a8c21a54SThe etnaviv authors 		gpu->idle_mask = ~VIVS_HI_IDLE_STATE_AXI_LP;
388a8c21a54SThe etnaviv authors 	}
389a8c21a54SThe etnaviv authors 
390a8c21a54SThe etnaviv authors 	etnaviv_hw_specs(gpu);
391a8c21a54SThe etnaviv authors }
392a8c21a54SThe etnaviv authors 
393a8c21a54SThe etnaviv authors static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
394a8c21a54SThe etnaviv authors {
395a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock |
396a8c21a54SThe etnaviv authors 		  VIVS_HI_CLOCK_CONTROL_FSCALE_CMD_LOAD);
397a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
398a8c21a54SThe etnaviv authors }
399a8c21a54SThe etnaviv authors 
400a8c21a54SThe etnaviv authors static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
401a8c21a54SThe etnaviv authors {
402a8c21a54SThe etnaviv authors 	u32 control, idle;
403a8c21a54SThe etnaviv authors 	unsigned long timeout;
404a8c21a54SThe etnaviv authors 	bool failed = true;
405a8c21a54SThe etnaviv authors 
406a8c21a54SThe etnaviv authors 	/* TODO
407a8c21a54SThe etnaviv authors 	 *
408a8c21a54SThe etnaviv authors 	 * - clock gating
409a8c21a54SThe etnaviv authors 	 * - puls eater
410a8c21a54SThe etnaviv authors 	 * - what about VG?
411a8c21a54SThe etnaviv authors 	 */
412a8c21a54SThe etnaviv authors 
413a8c21a54SThe etnaviv authors 	/* We hope that the GPU resets in under one second */
414a8c21a54SThe etnaviv authors 	timeout = jiffies + msecs_to_jiffies(1000);
415a8c21a54SThe etnaviv authors 
416a8c21a54SThe etnaviv authors 	while (time_is_after_jiffies(timeout)) {
417a8c21a54SThe etnaviv authors 		control = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
418a8c21a54SThe etnaviv authors 			  VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(0x40);
419a8c21a54SThe etnaviv authors 
420a8c21a54SThe etnaviv authors 		/* enable clock */
421a8c21a54SThe etnaviv authors 		etnaviv_gpu_load_clock(gpu, control);
422a8c21a54SThe etnaviv authors 
423a8c21a54SThe etnaviv authors 		/* Wait for stable clock.  Vivante's code waited for 1ms */
424a8c21a54SThe etnaviv authors 		usleep_range(1000, 10000);
425a8c21a54SThe etnaviv authors 
426a8c21a54SThe etnaviv authors 		/* isolate the GPU. */
427a8c21a54SThe etnaviv authors 		control |= VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
428a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
429a8c21a54SThe etnaviv authors 
430a8c21a54SThe etnaviv authors 		/* set soft reset. */
431a8c21a54SThe etnaviv authors 		control |= VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
432a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
433a8c21a54SThe etnaviv authors 
434a8c21a54SThe etnaviv authors 		/* wait for reset. */
435a8c21a54SThe etnaviv authors 		msleep(1);
436a8c21a54SThe etnaviv authors 
437a8c21a54SThe etnaviv authors 		/* reset soft reset bit. */
438a8c21a54SThe etnaviv authors 		control &= ~VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
439a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
440a8c21a54SThe etnaviv authors 
441a8c21a54SThe etnaviv authors 		/* reset GPU isolation. */
442a8c21a54SThe etnaviv authors 		control &= ~VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
443a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
444a8c21a54SThe etnaviv authors 
445a8c21a54SThe etnaviv authors 		/* read idle register. */
446a8c21a54SThe etnaviv authors 		idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
447a8c21a54SThe etnaviv authors 
448a8c21a54SThe etnaviv authors 		/* try reseting again if FE it not idle */
449a8c21a54SThe etnaviv authors 		if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) {
450a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "FE is not idle\n");
451a8c21a54SThe etnaviv authors 			continue;
452a8c21a54SThe etnaviv authors 		}
453a8c21a54SThe etnaviv authors 
454a8c21a54SThe etnaviv authors 		/* read reset register. */
455a8c21a54SThe etnaviv authors 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
456a8c21a54SThe etnaviv authors 
457a8c21a54SThe etnaviv authors 		/* is the GPU idle? */
458a8c21a54SThe etnaviv authors 		if (((control & VIVS_HI_CLOCK_CONTROL_IDLE_3D) == 0) ||
459a8c21a54SThe etnaviv authors 		    ((control & VIVS_HI_CLOCK_CONTROL_IDLE_2D) == 0)) {
460a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "GPU is not idle\n");
461a8c21a54SThe etnaviv authors 			continue;
462a8c21a54SThe etnaviv authors 		}
463a8c21a54SThe etnaviv authors 
464a8c21a54SThe etnaviv authors 		failed = false;
465a8c21a54SThe etnaviv authors 		break;
466a8c21a54SThe etnaviv authors 	}
467a8c21a54SThe etnaviv authors 
468a8c21a54SThe etnaviv authors 	if (failed) {
469a8c21a54SThe etnaviv authors 		idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
470a8c21a54SThe etnaviv authors 		control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
471a8c21a54SThe etnaviv authors 
472a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "GPU failed to reset: FE %sidle, 3D %sidle, 2D %sidle\n",
473a8c21a54SThe etnaviv authors 			idle & VIVS_HI_IDLE_STATE_FE ? "" : "not ",
474a8c21a54SThe etnaviv authors 			control & VIVS_HI_CLOCK_CONTROL_IDLE_3D ? "" : "not ",
475a8c21a54SThe etnaviv authors 			control & VIVS_HI_CLOCK_CONTROL_IDLE_2D ? "" : "not ");
476a8c21a54SThe etnaviv authors 
477a8c21a54SThe etnaviv authors 		return -EBUSY;
478a8c21a54SThe etnaviv authors 	}
479a8c21a54SThe etnaviv authors 
480a8c21a54SThe etnaviv authors 	/* We rely on the GPU running, so program the clock */
481a8c21a54SThe etnaviv authors 	control = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
482a8c21a54SThe etnaviv authors 		  VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(0x40);
483a8c21a54SThe etnaviv authors 
484a8c21a54SThe etnaviv authors 	/* enable clock */
485a8c21a54SThe etnaviv authors 	etnaviv_gpu_load_clock(gpu, control);
486a8c21a54SThe etnaviv authors 
487a8c21a54SThe etnaviv authors 	return 0;
488a8c21a54SThe etnaviv authors }
489a8c21a54SThe etnaviv authors 
490a8c21a54SThe etnaviv authors static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu)
491a8c21a54SThe etnaviv authors {
492a8c21a54SThe etnaviv authors 	u16 prefetch;
493a8c21a54SThe etnaviv authors 
494472f79dcSRussell King 	if ((etnaviv_is_model_rev(gpu, GC320, 0x5007) ||
495472f79dcSRussell King 	     etnaviv_is_model_rev(gpu, GC320, 0x5220)) &&
496472f79dcSRussell King 	    gpu_read(gpu, VIVS_HI_CHIP_TIME) != 0x2062400) {
497a8c21a54SThe etnaviv authors 		u32 mc_memory_debug;
498a8c21a54SThe etnaviv authors 
499a8c21a54SThe etnaviv authors 		mc_memory_debug = gpu_read(gpu, VIVS_MC_DEBUG_MEMORY) & ~0xff;
500a8c21a54SThe etnaviv authors 
501a8c21a54SThe etnaviv authors 		if (gpu->identity.revision == 0x5007)
502a8c21a54SThe etnaviv authors 			mc_memory_debug |= 0x0c;
503a8c21a54SThe etnaviv authors 		else
504a8c21a54SThe etnaviv authors 			mc_memory_debug |= 0x08;
505a8c21a54SThe etnaviv authors 
506a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_MC_DEBUG_MEMORY, mc_memory_debug);
507a8c21a54SThe etnaviv authors 	}
508a8c21a54SThe etnaviv authors 
509a8c21a54SThe etnaviv authors 	/*
510a8c21a54SThe etnaviv authors 	 * Update GPU AXI cache atttribute to "cacheable, no allocate".
511a8c21a54SThe etnaviv authors 	 * This is necessary to prevent the iMX6 SoC locking up.
512a8c21a54SThe etnaviv authors 	 */
513a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_AXI_CONFIG,
514a8c21a54SThe etnaviv authors 		  VIVS_HI_AXI_CONFIG_AWCACHE(2) |
515a8c21a54SThe etnaviv authors 		  VIVS_HI_AXI_CONFIG_ARCACHE(2));
516a8c21a54SThe etnaviv authors 
517a8c21a54SThe etnaviv authors 	/* GC2000 rev 5108 needs a special bus config */
518472f79dcSRussell King 	if (etnaviv_is_model_rev(gpu, GC2000, 0x5108)) {
519a8c21a54SThe etnaviv authors 		u32 bus_config = gpu_read(gpu, VIVS_MC_BUS_CONFIG);
520a8c21a54SThe etnaviv authors 		bus_config &= ~(VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG__MASK |
521a8c21a54SThe etnaviv authors 				VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG__MASK);
522a8c21a54SThe etnaviv authors 		bus_config |= VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG(1) |
523a8c21a54SThe etnaviv authors 			      VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG(0);
524a8c21a54SThe etnaviv authors 		gpu_write(gpu, VIVS_MC_BUS_CONFIG, bus_config);
525a8c21a54SThe etnaviv authors 	}
526a8c21a54SThe etnaviv authors 
527a8c21a54SThe etnaviv authors 	/* set base addresses */
528a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_RA, gpu->memory_base);
529a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_FE, gpu->memory_base);
530a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_TX, gpu->memory_base);
531a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_PEZ, gpu->memory_base);
532a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_PE, gpu->memory_base);
533a8c21a54SThe etnaviv authors 
534a8c21a54SThe etnaviv authors 	/* setup the MMU page table pointers */
535a8c21a54SThe etnaviv authors 	etnaviv_iommu_domain_restore(gpu, gpu->mmu->domain);
536a8c21a54SThe etnaviv authors 
537a8c21a54SThe etnaviv authors 	/* Start command processor */
538a8c21a54SThe etnaviv authors 	prefetch = etnaviv_buffer_init(gpu);
539a8c21a54SThe etnaviv authors 
540a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_HI_INTR_ENBL, ~0U);
541a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_FE_COMMAND_ADDRESS,
542a8c21a54SThe etnaviv authors 		  gpu->buffer->paddr - gpu->memory_base);
543a8c21a54SThe etnaviv authors 	gpu_write(gpu, VIVS_FE_COMMAND_CONTROL,
544a8c21a54SThe etnaviv authors 		  VIVS_FE_COMMAND_CONTROL_ENABLE |
545a8c21a54SThe etnaviv authors 		  VIVS_FE_COMMAND_CONTROL_PREFETCH(prefetch));
546a8c21a54SThe etnaviv authors }
547a8c21a54SThe etnaviv authors 
548a8c21a54SThe etnaviv authors int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
549a8c21a54SThe etnaviv authors {
550a8c21a54SThe etnaviv authors 	int ret, i;
551a8c21a54SThe etnaviv authors 	struct iommu_domain *iommu;
552a8c21a54SThe etnaviv authors 	enum etnaviv_iommu_version version;
553a8c21a54SThe etnaviv authors 	bool mmuv2;
554a8c21a54SThe etnaviv authors 
555a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
556a8c21a54SThe etnaviv authors 	if (ret < 0)
557a8c21a54SThe etnaviv authors 		return ret;
558a8c21a54SThe etnaviv authors 
559a8c21a54SThe etnaviv authors 	etnaviv_hw_identify(gpu);
560a8c21a54SThe etnaviv authors 
561a8c21a54SThe etnaviv authors 	if (gpu->identity.model == 0) {
562a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "Unknown GPU model\n");
563f6427760SRussell King 		ret = -ENXIO;
564f6427760SRussell King 		goto fail;
565a8c21a54SThe etnaviv authors 	}
566a8c21a54SThe etnaviv authors 
567b98c6688SRussell King 	/* Exclude VG cores with FE2.0 */
568b98c6688SRussell King 	if (gpu->identity.features & chipFeatures_PIPE_VG &&
569b98c6688SRussell King 	    gpu->identity.features & chipFeatures_FE20) {
570b98c6688SRussell King 		dev_info(gpu->dev, "Ignoring GPU with VG and FE2.0\n");
571b98c6688SRussell King 		ret = -ENXIO;
572b98c6688SRussell King 		goto fail;
573b98c6688SRussell King 	}
574b98c6688SRussell King 
5752144fff7SLucas Stach 	/*
5762144fff7SLucas Stach 	 * Set the GPU linear window to be at the end of the DMA window, where
5772144fff7SLucas Stach 	 * the CMA area is likely to reside. This ensures that we are able to
5782144fff7SLucas Stach 	 * map the command buffers while having the linear window overlap as
5792144fff7SLucas Stach 	 * much RAM as possible, so we can optimize mappings for other buffers.
5802144fff7SLucas Stach 	 *
5812144fff7SLucas Stach 	 * For 3D cores only do this if MC2.0 is present, as with MC1.0 it leads
5822144fff7SLucas Stach 	 * to different views of the memory on the individual engines.
5832144fff7SLucas Stach 	 */
5842144fff7SLucas Stach 	if (!(gpu->identity.features & chipFeatures_PIPE_3D) ||
5852144fff7SLucas Stach 	    (gpu->identity.minor_features0 & chipMinorFeatures0_MC20)) {
5862144fff7SLucas Stach 		u32 dma_mask = (u32)dma_get_required_mask(gpu->dev);
5872144fff7SLucas Stach 		if (dma_mask < PHYS_OFFSET + SZ_2G)
5882144fff7SLucas Stach 			gpu->memory_base = PHYS_OFFSET;
5892144fff7SLucas Stach 		else
5902144fff7SLucas Stach 			gpu->memory_base = dma_mask - SZ_2G + 1;
5912144fff7SLucas Stach 	}
5922144fff7SLucas Stach 
593a8c21a54SThe etnaviv authors 	ret = etnaviv_hw_reset(gpu);
594a8c21a54SThe etnaviv authors 	if (ret)
595a8c21a54SThe etnaviv authors 		goto fail;
596a8c21a54SThe etnaviv authors 
597a8c21a54SThe etnaviv authors 	/* Setup IOMMU.. eventually we will (I think) do this once per context
598a8c21a54SThe etnaviv authors 	 * and have separate page tables per context.  For now, to keep things
599a8c21a54SThe etnaviv authors 	 * simple and to get something working, just use a single address space:
600a8c21a54SThe etnaviv authors 	 */
601a8c21a54SThe etnaviv authors 	mmuv2 = gpu->identity.minor_features1 & chipMinorFeatures1_MMU_VERSION;
602a8c21a54SThe etnaviv authors 	dev_dbg(gpu->dev, "mmuv2: %d\n", mmuv2);
603a8c21a54SThe etnaviv authors 
604a8c21a54SThe etnaviv authors 	if (!mmuv2) {
605a8c21a54SThe etnaviv authors 		iommu = etnaviv_iommu_domain_alloc(gpu);
606a8c21a54SThe etnaviv authors 		version = ETNAVIV_IOMMU_V1;
607a8c21a54SThe etnaviv authors 	} else {
608a8c21a54SThe etnaviv authors 		iommu = etnaviv_iommu_v2_domain_alloc(gpu);
609a8c21a54SThe etnaviv authors 		version = ETNAVIV_IOMMU_V2;
610a8c21a54SThe etnaviv authors 	}
611a8c21a54SThe etnaviv authors 
612a8c21a54SThe etnaviv authors 	if (!iommu) {
613a8c21a54SThe etnaviv authors 		ret = -ENOMEM;
614a8c21a54SThe etnaviv authors 		goto fail;
615a8c21a54SThe etnaviv authors 	}
616a8c21a54SThe etnaviv authors 
617a8c21a54SThe etnaviv authors 	gpu->mmu = etnaviv_iommu_new(gpu, iommu, version);
618a8c21a54SThe etnaviv authors 	if (!gpu->mmu) {
61945d16a6dSLucas Stach 		iommu_domain_free(iommu);
620a8c21a54SThe etnaviv authors 		ret = -ENOMEM;
621a8c21a54SThe etnaviv authors 		goto fail;
622a8c21a54SThe etnaviv authors 	}
623a8c21a54SThe etnaviv authors 
624a8c21a54SThe etnaviv authors 	/* Create buffer: */
625a8c21a54SThe etnaviv authors 	gpu->buffer = etnaviv_gpu_cmdbuf_new(gpu, PAGE_SIZE, 0);
626a8c21a54SThe etnaviv authors 	if (!gpu->buffer) {
627a8c21a54SThe etnaviv authors 		ret = -ENOMEM;
628a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "could not create command buffer\n");
62945d16a6dSLucas Stach 		goto destroy_iommu;
630a8c21a54SThe etnaviv authors 	}
631a8c21a54SThe etnaviv authors 	if (gpu->buffer->paddr - gpu->memory_base > 0x80000000) {
632a8c21a54SThe etnaviv authors 		ret = -EINVAL;
633a8c21a54SThe etnaviv authors 		dev_err(gpu->dev,
634a8c21a54SThe etnaviv authors 			"command buffer outside valid memory window\n");
635a8c21a54SThe etnaviv authors 		goto free_buffer;
636a8c21a54SThe etnaviv authors 	}
637a8c21a54SThe etnaviv authors 
638a8c21a54SThe etnaviv authors 	/* Setup event management */
639a8c21a54SThe etnaviv authors 	spin_lock_init(&gpu->event_spinlock);
640a8c21a54SThe etnaviv authors 	init_completion(&gpu->event_free);
641a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
642a8c21a54SThe etnaviv authors 		gpu->event[i].used = false;
643a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
644a8c21a54SThe etnaviv authors 	}
645a8c21a54SThe etnaviv authors 
646a8c21a54SThe etnaviv authors 	/* Now program the hardware */
647a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
648a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
649f6086311SRussell King 	gpu->exec_state = -1;
650a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
651a8c21a54SThe etnaviv authors 
652a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
653a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
654a8c21a54SThe etnaviv authors 
655a8c21a54SThe etnaviv authors 	return 0;
656a8c21a54SThe etnaviv authors 
657a8c21a54SThe etnaviv authors free_buffer:
658a8c21a54SThe etnaviv authors 	etnaviv_gpu_cmdbuf_free(gpu->buffer);
659a8c21a54SThe etnaviv authors 	gpu->buffer = NULL;
66045d16a6dSLucas Stach destroy_iommu:
66145d16a6dSLucas Stach 	etnaviv_iommu_destroy(gpu->mmu);
66245d16a6dSLucas Stach 	gpu->mmu = NULL;
663a8c21a54SThe etnaviv authors fail:
664a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
665a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
666a8c21a54SThe etnaviv authors 
667a8c21a54SThe etnaviv authors 	return ret;
668a8c21a54SThe etnaviv authors }
669a8c21a54SThe etnaviv authors 
670a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
671a8c21a54SThe etnaviv authors struct dma_debug {
672a8c21a54SThe etnaviv authors 	u32 address[2];
673a8c21a54SThe etnaviv authors 	u32 state[2];
674a8c21a54SThe etnaviv authors };
675a8c21a54SThe etnaviv authors 
676a8c21a54SThe etnaviv authors static void verify_dma(struct etnaviv_gpu *gpu, struct dma_debug *debug)
677a8c21a54SThe etnaviv authors {
678a8c21a54SThe etnaviv authors 	u32 i;
679a8c21a54SThe etnaviv authors 
680a8c21a54SThe etnaviv authors 	debug->address[0] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
681a8c21a54SThe etnaviv authors 	debug->state[0]   = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
682a8c21a54SThe etnaviv authors 
683a8c21a54SThe etnaviv authors 	for (i = 0; i < 500; i++) {
684a8c21a54SThe etnaviv authors 		debug->address[1] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
685a8c21a54SThe etnaviv authors 		debug->state[1]   = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
686a8c21a54SThe etnaviv authors 
687a8c21a54SThe etnaviv authors 		if (debug->address[0] != debug->address[1])
688a8c21a54SThe etnaviv authors 			break;
689a8c21a54SThe etnaviv authors 
690a8c21a54SThe etnaviv authors 		if (debug->state[0] != debug->state[1])
691a8c21a54SThe etnaviv authors 			break;
692a8c21a54SThe etnaviv authors 	}
693a8c21a54SThe etnaviv authors }
694a8c21a54SThe etnaviv authors 
695a8c21a54SThe etnaviv authors int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m)
696a8c21a54SThe etnaviv authors {
697a8c21a54SThe etnaviv authors 	struct dma_debug debug;
698a8c21a54SThe etnaviv authors 	u32 dma_lo, dma_hi, axi, idle;
699a8c21a54SThe etnaviv authors 	int ret;
700a8c21a54SThe etnaviv authors 
701a8c21a54SThe etnaviv authors 	seq_printf(m, "%s Status:\n", dev_name(gpu->dev));
702a8c21a54SThe etnaviv authors 
703a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
704a8c21a54SThe etnaviv authors 	if (ret < 0)
705a8c21a54SThe etnaviv authors 		return ret;
706a8c21a54SThe etnaviv authors 
707a8c21a54SThe etnaviv authors 	dma_lo = gpu_read(gpu, VIVS_FE_DMA_LOW);
708a8c21a54SThe etnaviv authors 	dma_hi = gpu_read(gpu, VIVS_FE_DMA_HIGH);
709a8c21a54SThe etnaviv authors 	axi = gpu_read(gpu, VIVS_HI_AXI_STATUS);
710a8c21a54SThe etnaviv authors 	idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
711a8c21a54SThe etnaviv authors 
712a8c21a54SThe etnaviv authors 	verify_dma(gpu, &debug);
713a8c21a54SThe etnaviv authors 
714a8c21a54SThe etnaviv authors 	seq_puts(m, "\tfeatures\n");
715a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features0: 0x%08x\n",
716a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features0);
717a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features1: 0x%08x\n",
718a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features1);
719a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features2: 0x%08x\n",
720a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features2);
721a8c21a54SThe etnaviv authors 	seq_printf(m, "\t minor_features3: 0x%08x\n",
722a8c21a54SThe etnaviv authors 		   gpu->identity.minor_features3);
723602eb489SRussell King 	seq_printf(m, "\t minor_features4: 0x%08x\n",
724602eb489SRussell King 		   gpu->identity.minor_features4);
725602eb489SRussell King 	seq_printf(m, "\t minor_features5: 0x%08x\n",
726602eb489SRussell King 		   gpu->identity.minor_features5);
727a8c21a54SThe etnaviv authors 
728a8c21a54SThe etnaviv authors 	seq_puts(m, "\tspecs\n");
729a8c21a54SThe etnaviv authors 	seq_printf(m, "\t stream_count:  %d\n",
730a8c21a54SThe etnaviv authors 			gpu->identity.stream_count);
731a8c21a54SThe etnaviv authors 	seq_printf(m, "\t register_max: %d\n",
732a8c21a54SThe etnaviv authors 			gpu->identity.register_max);
733a8c21a54SThe etnaviv authors 	seq_printf(m, "\t thread_count: %d\n",
734a8c21a54SThe etnaviv authors 			gpu->identity.thread_count);
735a8c21a54SThe etnaviv authors 	seq_printf(m, "\t vertex_cache_size: %d\n",
736a8c21a54SThe etnaviv authors 			gpu->identity.vertex_cache_size);
737a8c21a54SThe etnaviv authors 	seq_printf(m, "\t shader_core_count: %d\n",
738a8c21a54SThe etnaviv authors 			gpu->identity.shader_core_count);
739a8c21a54SThe etnaviv authors 	seq_printf(m, "\t pixel_pipes: %d\n",
740a8c21a54SThe etnaviv authors 			gpu->identity.pixel_pipes);
741a8c21a54SThe etnaviv authors 	seq_printf(m, "\t vertex_output_buffer_size: %d\n",
742a8c21a54SThe etnaviv authors 			gpu->identity.vertex_output_buffer_size);
743a8c21a54SThe etnaviv authors 	seq_printf(m, "\t buffer_size: %d\n",
744a8c21a54SThe etnaviv authors 			gpu->identity.buffer_size);
745a8c21a54SThe etnaviv authors 	seq_printf(m, "\t instruction_count: %d\n",
746a8c21a54SThe etnaviv authors 			gpu->identity.instruction_count);
747a8c21a54SThe etnaviv authors 	seq_printf(m, "\t num_constants: %d\n",
748a8c21a54SThe etnaviv authors 			gpu->identity.num_constants);
749602eb489SRussell King 	seq_printf(m, "\t varyings_count: %d\n",
750602eb489SRussell King 			gpu->identity.varyings_count);
751a8c21a54SThe etnaviv authors 
752a8c21a54SThe etnaviv authors 	seq_printf(m, "\taxi: 0x%08x\n", axi);
753a8c21a54SThe etnaviv authors 	seq_printf(m, "\tidle: 0x%08x\n", idle);
754a8c21a54SThe etnaviv authors 	idle |= ~gpu->idle_mask & ~VIVS_HI_IDLE_STATE_AXI_LP;
755a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_FE) == 0)
756a8c21a54SThe etnaviv authors 		seq_puts(m, "\t FE is not idle\n");
757a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_DE) == 0)
758a8c21a54SThe etnaviv authors 		seq_puts(m, "\t DE is not idle\n");
759a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_PE) == 0)
760a8c21a54SThe etnaviv authors 		seq_puts(m, "\t PE is not idle\n");
761a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_SH) == 0)
762a8c21a54SThe etnaviv authors 		seq_puts(m, "\t SH is not idle\n");
763a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_PA) == 0)
764a8c21a54SThe etnaviv authors 		seq_puts(m, "\t PA is not idle\n");
765a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_SE) == 0)
766a8c21a54SThe etnaviv authors 		seq_puts(m, "\t SE is not idle\n");
767a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_RA) == 0)
768a8c21a54SThe etnaviv authors 		seq_puts(m, "\t RA is not idle\n");
769a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_TX) == 0)
770a8c21a54SThe etnaviv authors 		seq_puts(m, "\t TX is not idle\n");
771a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_VG) == 0)
772a8c21a54SThe etnaviv authors 		seq_puts(m, "\t VG is not idle\n");
773a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_IM) == 0)
774a8c21a54SThe etnaviv authors 		seq_puts(m, "\t IM is not idle\n");
775a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_FP) == 0)
776a8c21a54SThe etnaviv authors 		seq_puts(m, "\t FP is not idle\n");
777a8c21a54SThe etnaviv authors 	if ((idle & VIVS_HI_IDLE_STATE_TS) == 0)
778a8c21a54SThe etnaviv authors 		seq_puts(m, "\t TS is not idle\n");
779a8c21a54SThe etnaviv authors 	if (idle & VIVS_HI_IDLE_STATE_AXI_LP)
780a8c21a54SThe etnaviv authors 		seq_puts(m, "\t AXI low power mode\n");
781a8c21a54SThe etnaviv authors 
782a8c21a54SThe etnaviv authors 	if (gpu->identity.features & chipFeatures_DEBUG_MODE) {
783a8c21a54SThe etnaviv authors 		u32 read0 = gpu_read(gpu, VIVS_MC_DEBUG_READ0);
784a8c21a54SThe etnaviv authors 		u32 read1 = gpu_read(gpu, VIVS_MC_DEBUG_READ1);
785a8c21a54SThe etnaviv authors 		u32 write = gpu_read(gpu, VIVS_MC_DEBUG_WRITE);
786a8c21a54SThe etnaviv authors 
787a8c21a54SThe etnaviv authors 		seq_puts(m, "\tMC\n");
788a8c21a54SThe etnaviv authors 		seq_printf(m, "\t read0: 0x%08x\n", read0);
789a8c21a54SThe etnaviv authors 		seq_printf(m, "\t read1: 0x%08x\n", read1);
790a8c21a54SThe etnaviv authors 		seq_printf(m, "\t write: 0x%08x\n", write);
791a8c21a54SThe etnaviv authors 	}
792a8c21a54SThe etnaviv authors 
793a8c21a54SThe etnaviv authors 	seq_puts(m, "\tDMA ");
794a8c21a54SThe etnaviv authors 
795a8c21a54SThe etnaviv authors 	if (debug.address[0] == debug.address[1] &&
796a8c21a54SThe etnaviv authors 	    debug.state[0] == debug.state[1]) {
797a8c21a54SThe etnaviv authors 		seq_puts(m, "seems to be stuck\n");
798a8c21a54SThe etnaviv authors 	} else if (debug.address[0] == debug.address[1]) {
799a8c21a54SThe etnaviv authors 		seq_puts(m, "adress is constant\n");
800a8c21a54SThe etnaviv authors 	} else {
801a8c21a54SThe etnaviv authors 		seq_puts(m, "is runing\n");
802a8c21a54SThe etnaviv authors 	}
803a8c21a54SThe etnaviv authors 
804a8c21a54SThe etnaviv authors 	seq_printf(m, "\t address 0: 0x%08x\n", debug.address[0]);
805a8c21a54SThe etnaviv authors 	seq_printf(m, "\t address 1: 0x%08x\n", debug.address[1]);
806a8c21a54SThe etnaviv authors 	seq_printf(m, "\t state 0: 0x%08x\n", debug.state[0]);
807a8c21a54SThe etnaviv authors 	seq_printf(m, "\t state 1: 0x%08x\n", debug.state[1]);
808a8c21a54SThe etnaviv authors 	seq_printf(m, "\t last fetch 64 bit word: 0x%08x 0x%08x\n",
809a8c21a54SThe etnaviv authors 		   dma_lo, dma_hi);
810a8c21a54SThe etnaviv authors 
811a8c21a54SThe etnaviv authors 	ret = 0;
812a8c21a54SThe etnaviv authors 
813a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
814a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
815a8c21a54SThe etnaviv authors 
816a8c21a54SThe etnaviv authors 	return ret;
817a8c21a54SThe etnaviv authors }
818a8c21a54SThe etnaviv authors #endif
819a8c21a54SThe etnaviv authors 
820a8c21a54SThe etnaviv authors /*
821a8c21a54SThe etnaviv authors  * Power Management:
822a8c21a54SThe etnaviv authors  */
823a8c21a54SThe etnaviv authors static int enable_clk(struct etnaviv_gpu *gpu)
824a8c21a54SThe etnaviv authors {
825a8c21a54SThe etnaviv authors 	if (gpu->clk_core)
826a8c21a54SThe etnaviv authors 		clk_prepare_enable(gpu->clk_core);
827a8c21a54SThe etnaviv authors 	if (gpu->clk_shader)
828a8c21a54SThe etnaviv authors 		clk_prepare_enable(gpu->clk_shader);
829a8c21a54SThe etnaviv authors 
830a8c21a54SThe etnaviv authors 	return 0;
831a8c21a54SThe etnaviv authors }
832a8c21a54SThe etnaviv authors 
833a8c21a54SThe etnaviv authors static int disable_clk(struct etnaviv_gpu *gpu)
834a8c21a54SThe etnaviv authors {
835a8c21a54SThe etnaviv authors 	if (gpu->clk_core)
836a8c21a54SThe etnaviv authors 		clk_disable_unprepare(gpu->clk_core);
837a8c21a54SThe etnaviv authors 	if (gpu->clk_shader)
838a8c21a54SThe etnaviv authors 		clk_disable_unprepare(gpu->clk_shader);
839a8c21a54SThe etnaviv authors 
840a8c21a54SThe etnaviv authors 	return 0;
841a8c21a54SThe etnaviv authors }
842a8c21a54SThe etnaviv authors 
843a8c21a54SThe etnaviv authors static int enable_axi(struct etnaviv_gpu *gpu)
844a8c21a54SThe etnaviv authors {
845a8c21a54SThe etnaviv authors 	if (gpu->clk_bus)
846a8c21a54SThe etnaviv authors 		clk_prepare_enable(gpu->clk_bus);
847a8c21a54SThe etnaviv authors 
848a8c21a54SThe etnaviv authors 	return 0;
849a8c21a54SThe etnaviv authors }
850a8c21a54SThe etnaviv authors 
851a8c21a54SThe etnaviv authors static int disable_axi(struct etnaviv_gpu *gpu)
852a8c21a54SThe etnaviv authors {
853a8c21a54SThe etnaviv authors 	if (gpu->clk_bus)
854a8c21a54SThe etnaviv authors 		clk_disable_unprepare(gpu->clk_bus);
855a8c21a54SThe etnaviv authors 
856a8c21a54SThe etnaviv authors 	return 0;
857a8c21a54SThe etnaviv authors }
858a8c21a54SThe etnaviv authors 
859a8c21a54SThe etnaviv authors /*
860a8c21a54SThe etnaviv authors  * Hangcheck detection for locked gpu:
861a8c21a54SThe etnaviv authors  */
862a8c21a54SThe etnaviv authors static void recover_worker(struct work_struct *work)
863a8c21a54SThe etnaviv authors {
864a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
865a8c21a54SThe etnaviv authors 					       recover_work);
866a8c21a54SThe etnaviv authors 	unsigned long flags;
867a8c21a54SThe etnaviv authors 	unsigned int i;
868a8c21a54SThe etnaviv authors 
869a8c21a54SThe etnaviv authors 	dev_err(gpu->dev, "hangcheck recover!\n");
870a8c21a54SThe etnaviv authors 
871a8c21a54SThe etnaviv authors 	if (pm_runtime_get_sync(gpu->dev) < 0)
872a8c21a54SThe etnaviv authors 		return;
873a8c21a54SThe etnaviv authors 
874a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
875a8c21a54SThe etnaviv authors 
876a8c21a54SThe etnaviv authors 	/* Only catch the first event, or when manually re-armed */
877a8c21a54SThe etnaviv authors 	if (etnaviv_dump_core) {
878a8c21a54SThe etnaviv authors 		etnaviv_core_dump(gpu);
879a8c21a54SThe etnaviv authors 		etnaviv_dump_core = false;
880a8c21a54SThe etnaviv authors 	}
881a8c21a54SThe etnaviv authors 
882a8c21a54SThe etnaviv authors 	etnaviv_hw_reset(gpu);
883a8c21a54SThe etnaviv authors 
884a8c21a54SThe etnaviv authors 	/* complete all events, the GPU won't do it after the reset */
885a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
886a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
887a8c21a54SThe etnaviv authors 		if (!gpu->event[i].used)
888a8c21a54SThe etnaviv authors 			continue;
889a8c21a54SThe etnaviv authors 		fence_signal(gpu->event[i].fence);
890a8c21a54SThe etnaviv authors 		gpu->event[i].fence = NULL;
891a8c21a54SThe etnaviv authors 		gpu->event[i].used = false;
892a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
893a8c21a54SThe etnaviv authors 	}
894a8c21a54SThe etnaviv authors 	spin_unlock_irqrestore(&gpu->event_spinlock, flags);
895a8c21a54SThe etnaviv authors 	gpu->completed_fence = gpu->active_fence;
896a8c21a54SThe etnaviv authors 
897a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
898a8c21a54SThe etnaviv authors 	gpu->switch_context = true;
899f6086311SRussell King 	gpu->exec_state = -1;
900a8c21a54SThe etnaviv authors 
901a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
902a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
903a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
904a8c21a54SThe etnaviv authors 
905a8c21a54SThe etnaviv authors 	/* Retire the buffer objects in a work */
906a8c21a54SThe etnaviv authors 	etnaviv_queue_work(gpu->drm, &gpu->retire_work);
907a8c21a54SThe etnaviv authors }
908a8c21a54SThe etnaviv authors 
909a8c21a54SThe etnaviv authors static void hangcheck_timer_reset(struct etnaviv_gpu *gpu)
910a8c21a54SThe etnaviv authors {
911a8c21a54SThe etnaviv authors 	DBG("%s", dev_name(gpu->dev));
912a8c21a54SThe etnaviv authors 	mod_timer(&gpu->hangcheck_timer,
913a8c21a54SThe etnaviv authors 		  round_jiffies_up(jiffies + DRM_ETNAVIV_HANGCHECK_JIFFIES));
914a8c21a54SThe etnaviv authors }
915a8c21a54SThe etnaviv authors 
916a8c21a54SThe etnaviv authors static void hangcheck_handler(unsigned long data)
917a8c21a54SThe etnaviv authors {
918a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = (struct etnaviv_gpu *)data;
919a8c21a54SThe etnaviv authors 	u32 fence = gpu->completed_fence;
920a8c21a54SThe etnaviv authors 	bool progress = false;
921a8c21a54SThe etnaviv authors 
922a8c21a54SThe etnaviv authors 	if (fence != gpu->hangcheck_fence) {
923a8c21a54SThe etnaviv authors 		gpu->hangcheck_fence = fence;
924a8c21a54SThe etnaviv authors 		progress = true;
925a8c21a54SThe etnaviv authors 	}
926a8c21a54SThe etnaviv authors 
927a8c21a54SThe etnaviv authors 	if (!progress) {
928a8c21a54SThe etnaviv authors 		u32 dma_addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
929a8c21a54SThe etnaviv authors 		int change = dma_addr - gpu->hangcheck_dma_addr;
930a8c21a54SThe etnaviv authors 
931a8c21a54SThe etnaviv authors 		if (change < 0 || change > 16) {
932a8c21a54SThe etnaviv authors 			gpu->hangcheck_dma_addr = dma_addr;
933a8c21a54SThe etnaviv authors 			progress = true;
934a8c21a54SThe etnaviv authors 		}
935a8c21a54SThe etnaviv authors 	}
936a8c21a54SThe etnaviv authors 
937a8c21a54SThe etnaviv authors 	if (!progress && fence_after(gpu->active_fence, fence)) {
938a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "hangcheck detected gpu lockup!\n");
939a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "     completed fence: %u\n", fence);
940a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "     active fence: %u\n",
941a8c21a54SThe etnaviv authors 			gpu->active_fence);
942a8c21a54SThe etnaviv authors 		etnaviv_queue_work(gpu->drm, &gpu->recover_work);
943a8c21a54SThe etnaviv authors 	}
944a8c21a54SThe etnaviv authors 
945a8c21a54SThe etnaviv authors 	/* if still more pending work, reset the hangcheck timer: */
946a8c21a54SThe etnaviv authors 	if (fence_after(gpu->active_fence, gpu->hangcheck_fence))
947a8c21a54SThe etnaviv authors 		hangcheck_timer_reset(gpu);
948a8c21a54SThe etnaviv authors }
949a8c21a54SThe etnaviv authors 
950a8c21a54SThe etnaviv authors static void hangcheck_disable(struct etnaviv_gpu *gpu)
951a8c21a54SThe etnaviv authors {
952a8c21a54SThe etnaviv authors 	del_timer_sync(&gpu->hangcheck_timer);
953a8c21a54SThe etnaviv authors 	cancel_work_sync(&gpu->recover_work);
954a8c21a54SThe etnaviv authors }
955a8c21a54SThe etnaviv authors 
956a8c21a54SThe etnaviv authors /* fence object management */
957a8c21a54SThe etnaviv authors struct etnaviv_fence {
958a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
959a8c21a54SThe etnaviv authors 	struct fence base;
960a8c21a54SThe etnaviv authors };
961a8c21a54SThe etnaviv authors 
962a8c21a54SThe etnaviv authors static inline struct etnaviv_fence *to_etnaviv_fence(struct fence *fence)
963a8c21a54SThe etnaviv authors {
964a8c21a54SThe etnaviv authors 	return container_of(fence, struct etnaviv_fence, base);
965a8c21a54SThe etnaviv authors }
966a8c21a54SThe etnaviv authors 
967a8c21a54SThe etnaviv authors static const char *etnaviv_fence_get_driver_name(struct fence *fence)
968a8c21a54SThe etnaviv authors {
969a8c21a54SThe etnaviv authors 	return "etnaviv";
970a8c21a54SThe etnaviv authors }
971a8c21a54SThe etnaviv authors 
972a8c21a54SThe etnaviv authors static const char *etnaviv_fence_get_timeline_name(struct fence *fence)
973a8c21a54SThe etnaviv authors {
974a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
975a8c21a54SThe etnaviv authors 
976a8c21a54SThe etnaviv authors 	return dev_name(f->gpu->dev);
977a8c21a54SThe etnaviv authors }
978a8c21a54SThe etnaviv authors 
979a8c21a54SThe etnaviv authors static bool etnaviv_fence_enable_signaling(struct fence *fence)
980a8c21a54SThe etnaviv authors {
981a8c21a54SThe etnaviv authors 	return true;
982a8c21a54SThe etnaviv authors }
983a8c21a54SThe etnaviv authors 
984a8c21a54SThe etnaviv authors static bool etnaviv_fence_signaled(struct fence *fence)
985a8c21a54SThe etnaviv authors {
986a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
987a8c21a54SThe etnaviv authors 
988a8c21a54SThe etnaviv authors 	return fence_completed(f->gpu, f->base.seqno);
989a8c21a54SThe etnaviv authors }
990a8c21a54SThe etnaviv authors 
991a8c21a54SThe etnaviv authors static void etnaviv_fence_release(struct fence *fence)
992a8c21a54SThe etnaviv authors {
993a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f = to_etnaviv_fence(fence);
994a8c21a54SThe etnaviv authors 
995a8c21a54SThe etnaviv authors 	kfree_rcu(f, base.rcu);
996a8c21a54SThe etnaviv authors }
997a8c21a54SThe etnaviv authors 
998a8c21a54SThe etnaviv authors static const struct fence_ops etnaviv_fence_ops = {
999a8c21a54SThe etnaviv authors 	.get_driver_name = etnaviv_fence_get_driver_name,
1000a8c21a54SThe etnaviv authors 	.get_timeline_name = etnaviv_fence_get_timeline_name,
1001a8c21a54SThe etnaviv authors 	.enable_signaling = etnaviv_fence_enable_signaling,
1002a8c21a54SThe etnaviv authors 	.signaled = etnaviv_fence_signaled,
1003a8c21a54SThe etnaviv authors 	.wait = fence_default_wait,
1004a8c21a54SThe etnaviv authors 	.release = etnaviv_fence_release,
1005a8c21a54SThe etnaviv authors };
1006a8c21a54SThe etnaviv authors 
1007a8c21a54SThe etnaviv authors static struct fence *etnaviv_gpu_fence_alloc(struct etnaviv_gpu *gpu)
1008a8c21a54SThe etnaviv authors {
1009a8c21a54SThe etnaviv authors 	struct etnaviv_fence *f;
1010a8c21a54SThe etnaviv authors 
1011a8c21a54SThe etnaviv authors 	f = kzalloc(sizeof(*f), GFP_KERNEL);
1012a8c21a54SThe etnaviv authors 	if (!f)
1013a8c21a54SThe etnaviv authors 		return NULL;
1014a8c21a54SThe etnaviv authors 
1015a8c21a54SThe etnaviv authors 	f->gpu = gpu;
1016a8c21a54SThe etnaviv authors 
1017a8c21a54SThe etnaviv authors 	fence_init(&f->base, &etnaviv_fence_ops, &gpu->fence_spinlock,
1018a8c21a54SThe etnaviv authors 		   gpu->fence_context, ++gpu->next_fence);
1019a8c21a54SThe etnaviv authors 
1020a8c21a54SThe etnaviv authors 	return &f->base;
1021a8c21a54SThe etnaviv authors }
1022a8c21a54SThe etnaviv authors 
1023a8c21a54SThe etnaviv authors int etnaviv_gpu_fence_sync_obj(struct etnaviv_gem_object *etnaviv_obj,
1024a8c21a54SThe etnaviv authors 	unsigned int context, bool exclusive)
1025a8c21a54SThe etnaviv authors {
1026a8c21a54SThe etnaviv authors 	struct reservation_object *robj = etnaviv_obj->resv;
1027a8c21a54SThe etnaviv authors 	struct reservation_object_list *fobj;
1028a8c21a54SThe etnaviv authors 	struct fence *fence;
1029a8c21a54SThe etnaviv authors 	int i, ret;
1030a8c21a54SThe etnaviv authors 
1031a8c21a54SThe etnaviv authors 	if (!exclusive) {
1032a8c21a54SThe etnaviv authors 		ret = reservation_object_reserve_shared(robj);
1033a8c21a54SThe etnaviv authors 		if (ret)
1034a8c21a54SThe etnaviv authors 			return ret;
1035a8c21a54SThe etnaviv authors 	}
1036a8c21a54SThe etnaviv authors 
1037a8c21a54SThe etnaviv authors 	/*
1038a8c21a54SThe etnaviv authors 	 * If we have any shared fences, then the exclusive fence
1039a8c21a54SThe etnaviv authors 	 * should be ignored as it will already have been signalled.
1040a8c21a54SThe etnaviv authors 	 */
1041a8c21a54SThe etnaviv authors 	fobj = reservation_object_get_list(robj);
1042a8c21a54SThe etnaviv authors 	if (!fobj || fobj->shared_count == 0) {
1043a8c21a54SThe etnaviv authors 		/* Wait on any existing exclusive fence which isn't our own */
1044a8c21a54SThe etnaviv authors 		fence = reservation_object_get_excl(robj);
1045a8c21a54SThe etnaviv authors 		if (fence && fence->context != context) {
1046a8c21a54SThe etnaviv authors 			ret = fence_wait(fence, true);
1047a8c21a54SThe etnaviv authors 			if (ret)
1048a8c21a54SThe etnaviv authors 				return ret;
1049a8c21a54SThe etnaviv authors 		}
1050a8c21a54SThe etnaviv authors 	}
1051a8c21a54SThe etnaviv authors 
1052a8c21a54SThe etnaviv authors 	if (!exclusive || !fobj)
1053a8c21a54SThe etnaviv authors 		return 0;
1054a8c21a54SThe etnaviv authors 
1055a8c21a54SThe etnaviv authors 	for (i = 0; i < fobj->shared_count; i++) {
1056a8c21a54SThe etnaviv authors 		fence = rcu_dereference_protected(fobj->shared[i],
1057a8c21a54SThe etnaviv authors 						reservation_object_held(robj));
1058a8c21a54SThe etnaviv authors 		if (fence->context != context) {
1059a8c21a54SThe etnaviv authors 			ret = fence_wait(fence, true);
1060a8c21a54SThe etnaviv authors 			if (ret)
1061a8c21a54SThe etnaviv authors 				return ret;
1062a8c21a54SThe etnaviv authors 		}
1063a8c21a54SThe etnaviv authors 	}
1064a8c21a54SThe etnaviv authors 
1065a8c21a54SThe etnaviv authors 	return 0;
1066a8c21a54SThe etnaviv authors }
1067a8c21a54SThe etnaviv authors 
1068a8c21a54SThe etnaviv authors /*
1069a8c21a54SThe etnaviv authors  * event management:
1070a8c21a54SThe etnaviv authors  */
1071a8c21a54SThe etnaviv authors 
1072a8c21a54SThe etnaviv authors static unsigned int event_alloc(struct etnaviv_gpu *gpu)
1073a8c21a54SThe etnaviv authors {
1074a8c21a54SThe etnaviv authors 	unsigned long ret, flags;
1075a8c21a54SThe etnaviv authors 	unsigned int i, event = ~0U;
1076a8c21a54SThe etnaviv authors 
1077a8c21a54SThe etnaviv authors 	ret = wait_for_completion_timeout(&gpu->event_free,
1078a8c21a54SThe etnaviv authors 					  msecs_to_jiffies(10 * 10000));
1079a8c21a54SThe etnaviv authors 	if (!ret)
1080a8c21a54SThe etnaviv authors 		dev_err(gpu->dev, "wait_for_completion_timeout failed");
1081a8c21a54SThe etnaviv authors 
1082a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
1083a8c21a54SThe etnaviv authors 
1084a8c21a54SThe etnaviv authors 	/* find first free event */
1085a8c21a54SThe etnaviv authors 	for (i = 0; i < ARRAY_SIZE(gpu->event); i++) {
1086a8c21a54SThe etnaviv authors 		if (gpu->event[i].used == false) {
1087a8c21a54SThe etnaviv authors 			gpu->event[i].used = true;
1088a8c21a54SThe etnaviv authors 			event = i;
1089a8c21a54SThe etnaviv authors 			break;
1090a8c21a54SThe etnaviv authors 		}
1091a8c21a54SThe etnaviv authors 	}
1092a8c21a54SThe etnaviv authors 
1093a8c21a54SThe etnaviv authors 	spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1094a8c21a54SThe etnaviv authors 
1095a8c21a54SThe etnaviv authors 	return event;
1096a8c21a54SThe etnaviv authors }
1097a8c21a54SThe etnaviv authors 
1098a8c21a54SThe etnaviv authors static void event_free(struct etnaviv_gpu *gpu, unsigned int event)
1099a8c21a54SThe etnaviv authors {
1100a8c21a54SThe etnaviv authors 	unsigned long flags;
1101a8c21a54SThe etnaviv authors 
1102a8c21a54SThe etnaviv authors 	spin_lock_irqsave(&gpu->event_spinlock, flags);
1103a8c21a54SThe etnaviv authors 
1104a8c21a54SThe etnaviv authors 	if (gpu->event[event].used == false) {
1105a8c21a54SThe etnaviv authors 		dev_warn(gpu->dev, "event %u is already marked as free",
1106a8c21a54SThe etnaviv authors 			 event);
1107a8c21a54SThe etnaviv authors 		spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1108a8c21a54SThe etnaviv authors 	} else {
1109a8c21a54SThe etnaviv authors 		gpu->event[event].used = false;
1110a8c21a54SThe etnaviv authors 		spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1111a8c21a54SThe etnaviv authors 
1112a8c21a54SThe etnaviv authors 		complete(&gpu->event_free);
1113a8c21a54SThe etnaviv authors 	}
1114a8c21a54SThe etnaviv authors }
1115a8c21a54SThe etnaviv authors 
1116a8c21a54SThe etnaviv authors /*
1117a8c21a54SThe etnaviv authors  * Cmdstream submission/retirement:
1118a8c21a54SThe etnaviv authors  */
1119a8c21a54SThe etnaviv authors 
1120a8c21a54SThe etnaviv authors struct etnaviv_cmdbuf *etnaviv_gpu_cmdbuf_new(struct etnaviv_gpu *gpu, u32 size,
1121a8c21a54SThe etnaviv authors 	size_t nr_bos)
1122a8c21a54SThe etnaviv authors {
1123a8c21a54SThe etnaviv authors 	struct etnaviv_cmdbuf *cmdbuf;
1124b6325f40SRussell King 	size_t sz = size_vstruct(nr_bos, sizeof(cmdbuf->bo_map[0]),
1125a8c21a54SThe etnaviv authors 				 sizeof(*cmdbuf));
1126a8c21a54SThe etnaviv authors 
1127a8c21a54SThe etnaviv authors 	cmdbuf = kzalloc(sz, GFP_KERNEL);
1128a8c21a54SThe etnaviv authors 	if (!cmdbuf)
1129a8c21a54SThe etnaviv authors 		return NULL;
1130a8c21a54SThe etnaviv authors 
1131f6e45661SLuis R. Rodriguez 	cmdbuf->vaddr = dma_alloc_wc(gpu->dev, size, &cmdbuf->paddr,
1132a8c21a54SThe etnaviv authors 				     GFP_KERNEL);
1133a8c21a54SThe etnaviv authors 	if (!cmdbuf->vaddr) {
1134a8c21a54SThe etnaviv authors 		kfree(cmdbuf);
1135a8c21a54SThe etnaviv authors 		return NULL;
1136a8c21a54SThe etnaviv authors 	}
1137a8c21a54SThe etnaviv authors 
1138a8c21a54SThe etnaviv authors 	cmdbuf->gpu = gpu;
1139a8c21a54SThe etnaviv authors 	cmdbuf->size = size;
1140a8c21a54SThe etnaviv authors 
1141a8c21a54SThe etnaviv authors 	return cmdbuf;
1142a8c21a54SThe etnaviv authors }
1143a8c21a54SThe etnaviv authors 
1144a8c21a54SThe etnaviv authors void etnaviv_gpu_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
1145a8c21a54SThe etnaviv authors {
1146f6e45661SLuis R. Rodriguez 	dma_free_wc(cmdbuf->gpu->dev, cmdbuf->size, cmdbuf->vaddr,
1147f6e45661SLuis R. Rodriguez 		    cmdbuf->paddr);
1148a8c21a54SThe etnaviv authors 	kfree(cmdbuf);
1149a8c21a54SThe etnaviv authors }
1150a8c21a54SThe etnaviv authors 
1151a8c21a54SThe etnaviv authors static void retire_worker(struct work_struct *work)
1152a8c21a54SThe etnaviv authors {
1153a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
1154a8c21a54SThe etnaviv authors 					       retire_work);
1155a8c21a54SThe etnaviv authors 	u32 fence = gpu->completed_fence;
1156a8c21a54SThe etnaviv authors 	struct etnaviv_cmdbuf *cmdbuf, *tmp;
1157a8c21a54SThe etnaviv authors 	unsigned int i;
1158a8c21a54SThe etnaviv authors 
1159a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
1160a8c21a54SThe etnaviv authors 	list_for_each_entry_safe(cmdbuf, tmp, &gpu->active_cmd_list, node) {
1161a8c21a54SThe etnaviv authors 		if (!fence_is_signaled(cmdbuf->fence))
1162a8c21a54SThe etnaviv authors 			break;
1163a8c21a54SThe etnaviv authors 
1164a8c21a54SThe etnaviv authors 		list_del(&cmdbuf->node);
1165a8c21a54SThe etnaviv authors 		fence_put(cmdbuf->fence);
1166a8c21a54SThe etnaviv authors 
1167a8c21a54SThe etnaviv authors 		for (i = 0; i < cmdbuf->nr_bos; i++) {
1168b6325f40SRussell King 			struct etnaviv_vram_mapping *mapping = cmdbuf->bo_map[i];
1169b6325f40SRussell King 			struct etnaviv_gem_object *etnaviv_obj = mapping->object;
1170a8c21a54SThe etnaviv authors 
1171a8c21a54SThe etnaviv authors 			atomic_dec(&etnaviv_obj->gpu_active);
1172a8c21a54SThe etnaviv authors 			/* drop the refcount taken in etnaviv_gpu_submit */
1173b6325f40SRussell King 			etnaviv_gem_mapping_unreference(mapping);
1174a8c21a54SThe etnaviv authors 		}
1175a8c21a54SThe etnaviv authors 
1176a8c21a54SThe etnaviv authors 		etnaviv_gpu_cmdbuf_free(cmdbuf);
1177d9fd0c7dSLucas Stach 		/*
1178d9fd0c7dSLucas Stach 		 * We need to balance the runtime PM count caused by
1179d9fd0c7dSLucas Stach 		 * each submission.  Upon submission, we increment
1180d9fd0c7dSLucas Stach 		 * the runtime PM counter, and allocate one event.
1181d9fd0c7dSLucas Stach 		 * So here, we put the runtime PM count for each
1182d9fd0c7dSLucas Stach 		 * completed event.
1183d9fd0c7dSLucas Stach 		 */
1184d9fd0c7dSLucas Stach 		pm_runtime_put_autosuspend(gpu->dev);
1185a8c21a54SThe etnaviv authors 	}
1186a8c21a54SThe etnaviv authors 
1187a8c21a54SThe etnaviv authors 	gpu->retired_fence = fence;
1188a8c21a54SThe etnaviv authors 
1189a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1190a8c21a54SThe etnaviv authors 
1191a8c21a54SThe etnaviv authors 	wake_up_all(&gpu->fence_event);
1192a8c21a54SThe etnaviv authors }
1193a8c21a54SThe etnaviv authors 
1194a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu,
1195a8c21a54SThe etnaviv authors 	u32 fence, struct timespec *timeout)
1196a8c21a54SThe etnaviv authors {
1197a8c21a54SThe etnaviv authors 	int ret;
1198a8c21a54SThe etnaviv authors 
1199a8c21a54SThe etnaviv authors 	if (fence_after(fence, gpu->next_fence)) {
1200a8c21a54SThe etnaviv authors 		DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
1201a8c21a54SThe etnaviv authors 				fence, gpu->next_fence);
1202a8c21a54SThe etnaviv authors 		return -EINVAL;
1203a8c21a54SThe etnaviv authors 	}
1204a8c21a54SThe etnaviv authors 
1205a8c21a54SThe etnaviv authors 	if (!timeout) {
1206a8c21a54SThe etnaviv authors 		/* No timeout was requested: just test for completion */
1207a8c21a54SThe etnaviv authors 		ret = fence_completed(gpu, fence) ? 0 : -EBUSY;
1208a8c21a54SThe etnaviv authors 	} else {
1209a8c21a54SThe etnaviv authors 		unsigned long remaining = etnaviv_timeout_to_jiffies(timeout);
1210a8c21a54SThe etnaviv authors 
1211a8c21a54SThe etnaviv authors 		ret = wait_event_interruptible_timeout(gpu->fence_event,
1212a8c21a54SThe etnaviv authors 						fence_completed(gpu, fence),
1213a8c21a54SThe etnaviv authors 						remaining);
1214a8c21a54SThe etnaviv authors 		if (ret == 0) {
1215a8c21a54SThe etnaviv authors 			DBG("timeout waiting for fence: %u (retired: %u completed: %u)",
1216a8c21a54SThe etnaviv authors 				fence, gpu->retired_fence,
1217a8c21a54SThe etnaviv authors 				gpu->completed_fence);
1218a8c21a54SThe etnaviv authors 			ret = -ETIMEDOUT;
1219a8c21a54SThe etnaviv authors 		} else if (ret != -ERESTARTSYS) {
1220a8c21a54SThe etnaviv authors 			ret = 0;
1221a8c21a54SThe etnaviv authors 		}
1222a8c21a54SThe etnaviv authors 	}
1223a8c21a54SThe etnaviv authors 
1224a8c21a54SThe etnaviv authors 	return ret;
1225a8c21a54SThe etnaviv authors }
1226a8c21a54SThe etnaviv authors 
1227a8c21a54SThe etnaviv authors /*
1228a8c21a54SThe etnaviv authors  * Wait for an object to become inactive.  This, on it's own, is not race
1229a8c21a54SThe etnaviv authors  * free: the object is moved by the retire worker off the active list, and
1230a8c21a54SThe etnaviv authors  * then the iova is put.  Moreover, the object could be re-submitted just
1231a8c21a54SThe etnaviv authors  * after we notice that it's become inactive.
1232a8c21a54SThe etnaviv authors  *
1233a8c21a54SThe etnaviv authors  * Although the retirement happens under the gpu lock, we don't want to hold
1234a8c21a54SThe etnaviv authors  * that lock in this function while waiting.
1235a8c21a54SThe etnaviv authors  */
1236a8c21a54SThe etnaviv authors int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu,
1237a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout)
1238a8c21a54SThe etnaviv authors {
1239a8c21a54SThe etnaviv authors 	unsigned long remaining;
1240a8c21a54SThe etnaviv authors 	long ret;
1241a8c21a54SThe etnaviv authors 
1242a8c21a54SThe etnaviv authors 	if (!timeout)
1243a8c21a54SThe etnaviv authors 		return !is_active(etnaviv_obj) ? 0 : -EBUSY;
1244a8c21a54SThe etnaviv authors 
1245a8c21a54SThe etnaviv authors 	remaining = etnaviv_timeout_to_jiffies(timeout);
1246a8c21a54SThe etnaviv authors 
1247a8c21a54SThe etnaviv authors 	ret = wait_event_interruptible_timeout(gpu->fence_event,
1248a8c21a54SThe etnaviv authors 					       !is_active(etnaviv_obj),
1249a8c21a54SThe etnaviv authors 					       remaining);
1250a8c21a54SThe etnaviv authors 	if (ret > 0) {
1251a8c21a54SThe etnaviv authors 		struct etnaviv_drm_private *priv = gpu->drm->dev_private;
1252a8c21a54SThe etnaviv authors 
1253a8c21a54SThe etnaviv authors 		/* Synchronise with the retire worker */
1254a8c21a54SThe etnaviv authors 		flush_workqueue(priv->wq);
1255a8c21a54SThe etnaviv authors 		return 0;
1256a8c21a54SThe etnaviv authors 	} else if (ret == -ERESTARTSYS) {
1257a8c21a54SThe etnaviv authors 		return -ERESTARTSYS;
1258a8c21a54SThe etnaviv authors 	} else {
1259a8c21a54SThe etnaviv authors 		return -ETIMEDOUT;
1260a8c21a54SThe etnaviv authors 	}
1261a8c21a54SThe etnaviv authors }
1262a8c21a54SThe etnaviv authors 
1263a8c21a54SThe etnaviv authors int etnaviv_gpu_pm_get_sync(struct etnaviv_gpu *gpu)
1264a8c21a54SThe etnaviv authors {
1265a8c21a54SThe etnaviv authors 	return pm_runtime_get_sync(gpu->dev);
1266a8c21a54SThe etnaviv authors }
1267a8c21a54SThe etnaviv authors 
1268a8c21a54SThe etnaviv authors void etnaviv_gpu_pm_put(struct etnaviv_gpu *gpu)
1269a8c21a54SThe etnaviv authors {
1270a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
1271a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
1272a8c21a54SThe etnaviv authors }
1273a8c21a54SThe etnaviv authors 
1274a8c21a54SThe etnaviv authors /* add bo's to gpu's ring, and kick gpu: */
1275a8c21a54SThe etnaviv authors int etnaviv_gpu_submit(struct etnaviv_gpu *gpu,
1276a8c21a54SThe etnaviv authors 	struct etnaviv_gem_submit *submit, struct etnaviv_cmdbuf *cmdbuf)
1277a8c21a54SThe etnaviv authors {
1278a8c21a54SThe etnaviv authors 	struct fence *fence;
1279a8c21a54SThe etnaviv authors 	unsigned int event, i;
1280a8c21a54SThe etnaviv authors 	int ret;
1281a8c21a54SThe etnaviv authors 
1282a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_pm_get_sync(gpu);
1283a8c21a54SThe etnaviv authors 	if (ret < 0)
1284a8c21a54SThe etnaviv authors 		return ret;
1285a8c21a54SThe etnaviv authors 
1286a8c21a54SThe etnaviv authors 	mutex_lock(&gpu->lock);
1287a8c21a54SThe etnaviv authors 
1288a8c21a54SThe etnaviv authors 	/*
1289a8c21a54SThe etnaviv authors 	 * TODO
1290a8c21a54SThe etnaviv authors 	 *
1291a8c21a54SThe etnaviv authors 	 * - flush
1292a8c21a54SThe etnaviv authors 	 * - data endian
1293a8c21a54SThe etnaviv authors 	 * - prefetch
1294a8c21a54SThe etnaviv authors 	 *
1295a8c21a54SThe etnaviv authors 	 */
1296a8c21a54SThe etnaviv authors 
1297a8c21a54SThe etnaviv authors 	event = event_alloc(gpu);
1298a8c21a54SThe etnaviv authors 	if (unlikely(event == ~0U)) {
1299a8c21a54SThe etnaviv authors 		DRM_ERROR("no free event\n");
1300a8c21a54SThe etnaviv authors 		ret = -EBUSY;
1301a8c21a54SThe etnaviv authors 		goto out_unlock;
1302a8c21a54SThe etnaviv authors 	}
1303a8c21a54SThe etnaviv authors 
1304a8c21a54SThe etnaviv authors 	fence = etnaviv_gpu_fence_alloc(gpu);
1305a8c21a54SThe etnaviv authors 	if (!fence) {
1306a8c21a54SThe etnaviv authors 		event_free(gpu, event);
1307a8c21a54SThe etnaviv authors 		ret = -ENOMEM;
1308a8c21a54SThe etnaviv authors 		goto out_unlock;
1309a8c21a54SThe etnaviv authors 	}
1310a8c21a54SThe etnaviv authors 
1311a8c21a54SThe etnaviv authors 	gpu->event[event].fence = fence;
1312a8c21a54SThe etnaviv authors 	submit->fence = fence->seqno;
1313a8c21a54SThe etnaviv authors 	gpu->active_fence = submit->fence;
1314a8c21a54SThe etnaviv authors 
1315a8c21a54SThe etnaviv authors 	if (gpu->lastctx != cmdbuf->ctx) {
1316a8c21a54SThe etnaviv authors 		gpu->mmu->need_flush = true;
1317a8c21a54SThe etnaviv authors 		gpu->switch_context = true;
1318a8c21a54SThe etnaviv authors 		gpu->lastctx = cmdbuf->ctx;
1319a8c21a54SThe etnaviv authors 	}
1320a8c21a54SThe etnaviv authors 
1321a8c21a54SThe etnaviv authors 	etnaviv_buffer_queue(gpu, event, cmdbuf);
1322a8c21a54SThe etnaviv authors 
1323a8c21a54SThe etnaviv authors 	cmdbuf->fence = fence;
1324a8c21a54SThe etnaviv authors 	list_add_tail(&cmdbuf->node, &gpu->active_cmd_list);
1325a8c21a54SThe etnaviv authors 
1326a8c21a54SThe etnaviv authors 	/* We're committed to adding this command buffer, hold a PM reference */
1327a8c21a54SThe etnaviv authors 	pm_runtime_get_noresume(gpu->dev);
1328a8c21a54SThe etnaviv authors 
1329a8c21a54SThe etnaviv authors 	for (i = 0; i < submit->nr_bos; i++) {
1330a8c21a54SThe etnaviv authors 		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
1331a8c21a54SThe etnaviv authors 
1332b6325f40SRussell King 		/* Each cmdbuf takes a refcount on the mapping */
1333b6325f40SRussell King 		etnaviv_gem_mapping_reference(submit->bos[i].mapping);
1334b6325f40SRussell King 		cmdbuf->bo_map[i] = submit->bos[i].mapping;
1335a8c21a54SThe etnaviv authors 		atomic_inc(&etnaviv_obj->gpu_active);
1336a8c21a54SThe etnaviv authors 
1337a8c21a54SThe etnaviv authors 		if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE)
1338a8c21a54SThe etnaviv authors 			reservation_object_add_excl_fence(etnaviv_obj->resv,
1339a8c21a54SThe etnaviv authors 							  fence);
1340a8c21a54SThe etnaviv authors 		else
1341a8c21a54SThe etnaviv authors 			reservation_object_add_shared_fence(etnaviv_obj->resv,
1342a8c21a54SThe etnaviv authors 							    fence);
1343a8c21a54SThe etnaviv authors 	}
1344a8c21a54SThe etnaviv authors 	cmdbuf->nr_bos = submit->nr_bos;
1345a8c21a54SThe etnaviv authors 	hangcheck_timer_reset(gpu);
1346a8c21a54SThe etnaviv authors 	ret = 0;
1347a8c21a54SThe etnaviv authors 
1348a8c21a54SThe etnaviv authors out_unlock:
1349a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1350a8c21a54SThe etnaviv authors 
1351a8c21a54SThe etnaviv authors 	etnaviv_gpu_pm_put(gpu);
1352a8c21a54SThe etnaviv authors 
1353a8c21a54SThe etnaviv authors 	return ret;
1354a8c21a54SThe etnaviv authors }
1355a8c21a54SThe etnaviv authors 
1356a8c21a54SThe etnaviv authors /*
1357a8c21a54SThe etnaviv authors  * Init/Cleanup:
1358a8c21a54SThe etnaviv authors  */
1359a8c21a54SThe etnaviv authors static irqreturn_t irq_handler(int irq, void *data)
1360a8c21a54SThe etnaviv authors {
1361a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = data;
1362a8c21a54SThe etnaviv authors 	irqreturn_t ret = IRQ_NONE;
1363a8c21a54SThe etnaviv authors 
1364a8c21a54SThe etnaviv authors 	u32 intr = gpu_read(gpu, VIVS_HI_INTR_ACKNOWLEDGE);
1365a8c21a54SThe etnaviv authors 
1366a8c21a54SThe etnaviv authors 	if (intr != 0) {
1367a8c21a54SThe etnaviv authors 		int event;
1368a8c21a54SThe etnaviv authors 
1369a8c21a54SThe etnaviv authors 		pm_runtime_mark_last_busy(gpu->dev);
1370a8c21a54SThe etnaviv authors 
1371a8c21a54SThe etnaviv authors 		dev_dbg(gpu->dev, "intr 0x%08x\n", intr);
1372a8c21a54SThe etnaviv authors 
1373a8c21a54SThe etnaviv authors 		if (intr & VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR) {
1374a8c21a54SThe etnaviv authors 			dev_err(gpu->dev, "AXI bus error\n");
1375a8c21a54SThe etnaviv authors 			intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR;
1376a8c21a54SThe etnaviv authors 		}
1377a8c21a54SThe etnaviv authors 
1378a8c21a54SThe etnaviv authors 		while ((event = ffs(intr)) != 0) {
1379a8c21a54SThe etnaviv authors 			struct fence *fence;
1380a8c21a54SThe etnaviv authors 
1381a8c21a54SThe etnaviv authors 			event -= 1;
1382a8c21a54SThe etnaviv authors 
1383a8c21a54SThe etnaviv authors 			intr &= ~(1 << event);
1384a8c21a54SThe etnaviv authors 
1385a8c21a54SThe etnaviv authors 			dev_dbg(gpu->dev, "event %u\n", event);
1386a8c21a54SThe etnaviv authors 
1387a8c21a54SThe etnaviv authors 			fence = gpu->event[event].fence;
1388a8c21a54SThe etnaviv authors 			gpu->event[event].fence = NULL;
1389a8c21a54SThe etnaviv authors 			fence_signal(fence);
1390a8c21a54SThe etnaviv authors 
1391a8c21a54SThe etnaviv authors 			/*
1392a8c21a54SThe etnaviv authors 			 * Events can be processed out of order.  Eg,
1393a8c21a54SThe etnaviv authors 			 * - allocate and queue event 0
1394a8c21a54SThe etnaviv authors 			 * - allocate event 1
1395a8c21a54SThe etnaviv authors 			 * - event 0 completes, we process it
1396a8c21a54SThe etnaviv authors 			 * - allocate and queue event 0
1397a8c21a54SThe etnaviv authors 			 * - event 1 and event 0 complete
1398a8c21a54SThe etnaviv authors 			 * we can end up processing event 0 first, then 1.
1399a8c21a54SThe etnaviv authors 			 */
1400a8c21a54SThe etnaviv authors 			if (fence_after(fence->seqno, gpu->completed_fence))
1401a8c21a54SThe etnaviv authors 				gpu->completed_fence = fence->seqno;
1402a8c21a54SThe etnaviv authors 
1403a8c21a54SThe etnaviv authors 			event_free(gpu, event);
1404a8c21a54SThe etnaviv authors 		}
1405a8c21a54SThe etnaviv authors 
1406a8c21a54SThe etnaviv authors 		/* Retire the buffer objects in a work */
1407a8c21a54SThe etnaviv authors 		etnaviv_queue_work(gpu->drm, &gpu->retire_work);
1408a8c21a54SThe etnaviv authors 
1409a8c21a54SThe etnaviv authors 		ret = IRQ_HANDLED;
1410a8c21a54SThe etnaviv authors 	}
1411a8c21a54SThe etnaviv authors 
1412a8c21a54SThe etnaviv authors 	return ret;
1413a8c21a54SThe etnaviv authors }
1414a8c21a54SThe etnaviv authors 
1415a8c21a54SThe etnaviv authors static int etnaviv_gpu_clk_enable(struct etnaviv_gpu *gpu)
1416a8c21a54SThe etnaviv authors {
1417a8c21a54SThe etnaviv authors 	int ret;
1418a8c21a54SThe etnaviv authors 
1419a8c21a54SThe etnaviv authors 	ret = enable_clk(gpu);
1420a8c21a54SThe etnaviv authors 	if (ret)
1421a8c21a54SThe etnaviv authors 		return ret;
1422a8c21a54SThe etnaviv authors 
1423a8c21a54SThe etnaviv authors 	ret = enable_axi(gpu);
1424a8c21a54SThe etnaviv authors 	if (ret) {
1425a8c21a54SThe etnaviv authors 		disable_clk(gpu);
1426a8c21a54SThe etnaviv authors 		return ret;
1427a8c21a54SThe etnaviv authors 	}
1428a8c21a54SThe etnaviv authors 
1429a8c21a54SThe etnaviv authors 	return 0;
1430a8c21a54SThe etnaviv authors }
1431a8c21a54SThe etnaviv authors 
1432a8c21a54SThe etnaviv authors static int etnaviv_gpu_clk_disable(struct etnaviv_gpu *gpu)
1433a8c21a54SThe etnaviv authors {
1434a8c21a54SThe etnaviv authors 	int ret;
1435a8c21a54SThe etnaviv authors 
1436a8c21a54SThe etnaviv authors 	ret = disable_axi(gpu);
1437a8c21a54SThe etnaviv authors 	if (ret)
1438a8c21a54SThe etnaviv authors 		return ret;
1439a8c21a54SThe etnaviv authors 
1440a8c21a54SThe etnaviv authors 	ret = disable_clk(gpu);
1441a8c21a54SThe etnaviv authors 	if (ret)
1442a8c21a54SThe etnaviv authors 		return ret;
1443a8c21a54SThe etnaviv authors 
1444a8c21a54SThe etnaviv authors 	return 0;
1445a8c21a54SThe etnaviv authors }
1446a8c21a54SThe etnaviv authors 
1447a8c21a54SThe etnaviv authors static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
1448a8c21a54SThe etnaviv authors {
1449a8c21a54SThe etnaviv authors 	if (gpu->buffer) {
1450a8c21a54SThe etnaviv authors 		unsigned long timeout;
1451a8c21a54SThe etnaviv authors 
1452a8c21a54SThe etnaviv authors 		/* Replace the last WAIT with END */
1453a8c21a54SThe etnaviv authors 		etnaviv_buffer_end(gpu);
1454a8c21a54SThe etnaviv authors 
1455a8c21a54SThe etnaviv authors 		/*
1456a8c21a54SThe etnaviv authors 		 * We know that only the FE is busy here, this should
1457a8c21a54SThe etnaviv authors 		 * happen quickly (as the WAIT is only 200 cycles).  If
1458a8c21a54SThe etnaviv authors 		 * we fail, just warn and continue.
1459a8c21a54SThe etnaviv authors 		 */
1460a8c21a54SThe etnaviv authors 		timeout = jiffies + msecs_to_jiffies(100);
1461a8c21a54SThe etnaviv authors 		do {
1462a8c21a54SThe etnaviv authors 			u32 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
1463a8c21a54SThe etnaviv authors 
1464a8c21a54SThe etnaviv authors 			if ((idle & gpu->idle_mask) == gpu->idle_mask)
1465a8c21a54SThe etnaviv authors 				break;
1466a8c21a54SThe etnaviv authors 
1467a8c21a54SThe etnaviv authors 			if (time_is_before_jiffies(timeout)) {
1468a8c21a54SThe etnaviv authors 				dev_warn(gpu->dev,
1469a8c21a54SThe etnaviv authors 					 "timed out waiting for idle: idle=0x%x\n",
1470a8c21a54SThe etnaviv authors 					 idle);
1471a8c21a54SThe etnaviv authors 				break;
1472a8c21a54SThe etnaviv authors 			}
1473a8c21a54SThe etnaviv authors 
1474a8c21a54SThe etnaviv authors 			udelay(5);
1475a8c21a54SThe etnaviv authors 		} while (1);
1476a8c21a54SThe etnaviv authors 	}
1477a8c21a54SThe etnaviv authors 
1478a8c21a54SThe etnaviv authors 	return etnaviv_gpu_clk_disable(gpu);
1479a8c21a54SThe etnaviv authors }
1480a8c21a54SThe etnaviv authors 
1481a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1482a8c21a54SThe etnaviv authors static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
1483a8c21a54SThe etnaviv authors {
1484a8c21a54SThe etnaviv authors 	u32 clock;
1485a8c21a54SThe etnaviv authors 	int ret;
1486a8c21a54SThe etnaviv authors 
1487a8c21a54SThe etnaviv authors 	ret = mutex_lock_killable(&gpu->lock);
1488a8c21a54SThe etnaviv authors 	if (ret)
1489a8c21a54SThe etnaviv authors 		return ret;
1490a8c21a54SThe etnaviv authors 
1491a8c21a54SThe etnaviv authors 	clock = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
1492a8c21a54SThe etnaviv authors 		VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(0x40);
1493a8c21a54SThe etnaviv authors 
1494a8c21a54SThe etnaviv authors 	etnaviv_gpu_load_clock(gpu, clock);
1495a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_init(gpu);
1496a8c21a54SThe etnaviv authors 
1497a8c21a54SThe etnaviv authors 	gpu->switch_context = true;
1498f6086311SRussell King 	gpu->exec_state = -1;
1499a8c21a54SThe etnaviv authors 
1500a8c21a54SThe etnaviv authors 	mutex_unlock(&gpu->lock);
1501a8c21a54SThe etnaviv authors 
1502a8c21a54SThe etnaviv authors 	return 0;
1503a8c21a54SThe etnaviv authors }
1504a8c21a54SThe etnaviv authors #endif
1505a8c21a54SThe etnaviv authors 
1506a8c21a54SThe etnaviv authors static int etnaviv_gpu_bind(struct device *dev, struct device *master,
1507a8c21a54SThe etnaviv authors 	void *data)
1508a8c21a54SThe etnaviv authors {
1509a8c21a54SThe etnaviv authors 	struct drm_device *drm = data;
1510a8c21a54SThe etnaviv authors 	struct etnaviv_drm_private *priv = drm->dev_private;
1511a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1512a8c21a54SThe etnaviv authors 	int ret;
1513a8c21a54SThe etnaviv authors 
1514a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1515a8c21a54SThe etnaviv authors 	ret = pm_runtime_get_sync(gpu->dev);
1516a8c21a54SThe etnaviv authors #else
1517a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_clk_enable(gpu);
1518a8c21a54SThe etnaviv authors #endif
1519a8c21a54SThe etnaviv authors 	if (ret < 0)
1520a8c21a54SThe etnaviv authors 		return ret;
1521a8c21a54SThe etnaviv authors 
1522a8c21a54SThe etnaviv authors 	gpu->drm = drm;
1523a8c21a54SThe etnaviv authors 	gpu->fence_context = fence_context_alloc(1);
1524a8c21a54SThe etnaviv authors 	spin_lock_init(&gpu->fence_spinlock);
1525a8c21a54SThe etnaviv authors 
1526a8c21a54SThe etnaviv authors 	INIT_LIST_HEAD(&gpu->active_cmd_list);
1527a8c21a54SThe etnaviv authors 	INIT_WORK(&gpu->retire_work, retire_worker);
1528a8c21a54SThe etnaviv authors 	INIT_WORK(&gpu->recover_work, recover_worker);
1529a8c21a54SThe etnaviv authors 	init_waitqueue_head(&gpu->fence_event);
1530a8c21a54SThe etnaviv authors 
1531946dd8d5SLucas Stach 	setup_deferrable_timer(&gpu->hangcheck_timer, hangcheck_handler,
1532a8c21a54SThe etnaviv authors 			       (unsigned long)gpu);
1533a8c21a54SThe etnaviv authors 
1534a8c21a54SThe etnaviv authors 	priv->gpu[priv->num_gpus++] = gpu;
1535a8c21a54SThe etnaviv authors 
1536a8c21a54SThe etnaviv authors 	pm_runtime_mark_last_busy(gpu->dev);
1537a8c21a54SThe etnaviv authors 	pm_runtime_put_autosuspend(gpu->dev);
1538a8c21a54SThe etnaviv authors 
1539a8c21a54SThe etnaviv authors 	return 0;
1540a8c21a54SThe etnaviv authors }
1541a8c21a54SThe etnaviv authors 
1542a8c21a54SThe etnaviv authors static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
1543a8c21a54SThe etnaviv authors 	void *data)
1544a8c21a54SThe etnaviv authors {
1545a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1546a8c21a54SThe etnaviv authors 
1547a8c21a54SThe etnaviv authors 	DBG("%s", dev_name(gpu->dev));
1548a8c21a54SThe etnaviv authors 
1549a8c21a54SThe etnaviv authors 	hangcheck_disable(gpu);
1550a8c21a54SThe etnaviv authors 
1551a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1552a8c21a54SThe etnaviv authors 	pm_runtime_get_sync(gpu->dev);
1553a8c21a54SThe etnaviv authors 	pm_runtime_put_sync_suspend(gpu->dev);
1554a8c21a54SThe etnaviv authors #else
1555a8c21a54SThe etnaviv authors 	etnaviv_gpu_hw_suspend(gpu);
1556a8c21a54SThe etnaviv authors #endif
1557a8c21a54SThe etnaviv authors 
1558a8c21a54SThe etnaviv authors 	if (gpu->buffer) {
1559a8c21a54SThe etnaviv authors 		etnaviv_gpu_cmdbuf_free(gpu->buffer);
1560a8c21a54SThe etnaviv authors 		gpu->buffer = NULL;
1561a8c21a54SThe etnaviv authors 	}
1562a8c21a54SThe etnaviv authors 
1563a8c21a54SThe etnaviv authors 	if (gpu->mmu) {
1564a8c21a54SThe etnaviv authors 		etnaviv_iommu_destroy(gpu->mmu);
1565a8c21a54SThe etnaviv authors 		gpu->mmu = NULL;
1566a8c21a54SThe etnaviv authors 	}
1567a8c21a54SThe etnaviv authors 
1568a8c21a54SThe etnaviv authors 	gpu->drm = NULL;
1569a8c21a54SThe etnaviv authors }
1570a8c21a54SThe etnaviv authors 
1571a8c21a54SThe etnaviv authors static const struct component_ops gpu_ops = {
1572a8c21a54SThe etnaviv authors 	.bind = etnaviv_gpu_bind,
1573a8c21a54SThe etnaviv authors 	.unbind = etnaviv_gpu_unbind,
1574a8c21a54SThe etnaviv authors };
1575a8c21a54SThe etnaviv authors 
1576a8c21a54SThe etnaviv authors static const struct of_device_id etnaviv_gpu_match[] = {
1577a8c21a54SThe etnaviv authors 	{
1578a8c21a54SThe etnaviv authors 		.compatible = "vivante,gc"
1579a8c21a54SThe etnaviv authors 	},
1580a8c21a54SThe etnaviv authors 	{ /* sentinel */ }
1581a8c21a54SThe etnaviv authors };
1582a8c21a54SThe etnaviv authors 
1583a8c21a54SThe etnaviv authors static int etnaviv_gpu_platform_probe(struct platform_device *pdev)
1584a8c21a54SThe etnaviv authors {
1585a8c21a54SThe etnaviv authors 	struct device *dev = &pdev->dev;
1586a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu;
1587a8c21a54SThe etnaviv authors 	int err = 0;
1588a8c21a54SThe etnaviv authors 
1589a8c21a54SThe etnaviv authors 	gpu = devm_kzalloc(dev, sizeof(*gpu), GFP_KERNEL);
1590a8c21a54SThe etnaviv authors 	if (!gpu)
1591a8c21a54SThe etnaviv authors 		return -ENOMEM;
1592a8c21a54SThe etnaviv authors 
1593a8c21a54SThe etnaviv authors 	gpu->dev = &pdev->dev;
1594a8c21a54SThe etnaviv authors 	mutex_init(&gpu->lock);
1595a8c21a54SThe etnaviv authors 
1596a8c21a54SThe etnaviv authors 	/* Map registers: */
1597a8c21a54SThe etnaviv authors 	gpu->mmio = etnaviv_ioremap(pdev, NULL, dev_name(gpu->dev));
1598a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->mmio))
1599a8c21a54SThe etnaviv authors 		return PTR_ERR(gpu->mmio);
1600a8c21a54SThe etnaviv authors 
1601a8c21a54SThe etnaviv authors 	/* Get Interrupt: */
1602a8c21a54SThe etnaviv authors 	gpu->irq = platform_get_irq(pdev, 0);
1603a8c21a54SThe etnaviv authors 	if (gpu->irq < 0) {
1604a8c21a54SThe etnaviv authors 		err = gpu->irq;
1605a8c21a54SThe etnaviv authors 		dev_err(dev, "failed to get irq: %d\n", err);
1606a8c21a54SThe etnaviv authors 		goto fail;
1607a8c21a54SThe etnaviv authors 	}
1608a8c21a54SThe etnaviv authors 
1609a8c21a54SThe etnaviv authors 	err = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 0,
1610a8c21a54SThe etnaviv authors 			       dev_name(gpu->dev), gpu);
1611a8c21a54SThe etnaviv authors 	if (err) {
1612a8c21a54SThe etnaviv authors 		dev_err(dev, "failed to request IRQ%u: %d\n", gpu->irq, err);
1613a8c21a54SThe etnaviv authors 		goto fail;
1614a8c21a54SThe etnaviv authors 	}
1615a8c21a54SThe etnaviv authors 
1616a8c21a54SThe etnaviv authors 	/* Get Clocks: */
1617a8c21a54SThe etnaviv authors 	gpu->clk_bus = devm_clk_get(&pdev->dev, "bus");
1618a8c21a54SThe etnaviv authors 	DBG("clk_bus: %p", gpu->clk_bus);
1619a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_bus))
1620a8c21a54SThe etnaviv authors 		gpu->clk_bus = NULL;
1621a8c21a54SThe etnaviv authors 
1622a8c21a54SThe etnaviv authors 	gpu->clk_core = devm_clk_get(&pdev->dev, "core");
1623a8c21a54SThe etnaviv authors 	DBG("clk_core: %p", gpu->clk_core);
1624a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_core))
1625a8c21a54SThe etnaviv authors 		gpu->clk_core = NULL;
1626a8c21a54SThe etnaviv authors 
1627a8c21a54SThe etnaviv authors 	gpu->clk_shader = devm_clk_get(&pdev->dev, "shader");
1628a8c21a54SThe etnaviv authors 	DBG("clk_shader: %p", gpu->clk_shader);
1629a8c21a54SThe etnaviv authors 	if (IS_ERR(gpu->clk_shader))
1630a8c21a54SThe etnaviv authors 		gpu->clk_shader = NULL;
1631a8c21a54SThe etnaviv authors 
1632a8c21a54SThe etnaviv authors 	/* TODO: figure out max mapped size */
1633a8c21a54SThe etnaviv authors 	dev_set_drvdata(dev, gpu);
1634a8c21a54SThe etnaviv authors 
1635a8c21a54SThe etnaviv authors 	/*
1636a8c21a54SThe etnaviv authors 	 * We treat the device as initially suspended.  The runtime PM
1637a8c21a54SThe etnaviv authors 	 * autosuspend delay is rather arbitary: no measurements have
1638a8c21a54SThe etnaviv authors 	 * yet been performed to determine an appropriate value.
1639a8c21a54SThe etnaviv authors 	 */
1640a8c21a54SThe etnaviv authors 	pm_runtime_use_autosuspend(gpu->dev);
1641a8c21a54SThe etnaviv authors 	pm_runtime_set_autosuspend_delay(gpu->dev, 200);
1642a8c21a54SThe etnaviv authors 	pm_runtime_enable(gpu->dev);
1643a8c21a54SThe etnaviv authors 
1644a8c21a54SThe etnaviv authors 	err = component_add(&pdev->dev, &gpu_ops);
1645a8c21a54SThe etnaviv authors 	if (err < 0) {
1646a8c21a54SThe etnaviv authors 		dev_err(&pdev->dev, "failed to register component: %d\n", err);
1647a8c21a54SThe etnaviv authors 		goto fail;
1648a8c21a54SThe etnaviv authors 	}
1649a8c21a54SThe etnaviv authors 
1650a8c21a54SThe etnaviv authors 	return 0;
1651a8c21a54SThe etnaviv authors 
1652a8c21a54SThe etnaviv authors fail:
1653a8c21a54SThe etnaviv authors 	return err;
1654a8c21a54SThe etnaviv authors }
1655a8c21a54SThe etnaviv authors 
1656a8c21a54SThe etnaviv authors static int etnaviv_gpu_platform_remove(struct platform_device *pdev)
1657a8c21a54SThe etnaviv authors {
1658a8c21a54SThe etnaviv authors 	component_del(&pdev->dev, &gpu_ops);
1659a8c21a54SThe etnaviv authors 	pm_runtime_disable(&pdev->dev);
1660a8c21a54SThe etnaviv authors 	return 0;
1661a8c21a54SThe etnaviv authors }
1662a8c21a54SThe etnaviv authors 
1663a8c21a54SThe etnaviv authors #ifdef CONFIG_PM
1664a8c21a54SThe etnaviv authors static int etnaviv_gpu_rpm_suspend(struct device *dev)
1665a8c21a54SThe etnaviv authors {
1666a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1667a8c21a54SThe etnaviv authors 	u32 idle, mask;
1668a8c21a54SThe etnaviv authors 
1669a8c21a54SThe etnaviv authors 	/* If we have outstanding fences, we're not idle */
1670a8c21a54SThe etnaviv authors 	if (gpu->completed_fence != gpu->active_fence)
1671a8c21a54SThe etnaviv authors 		return -EBUSY;
1672a8c21a54SThe etnaviv authors 
1673a8c21a54SThe etnaviv authors 	/* Check whether the hardware (except FE) is idle */
1674a8c21a54SThe etnaviv authors 	mask = gpu->idle_mask & ~VIVS_HI_IDLE_STATE_FE;
1675a8c21a54SThe etnaviv authors 	idle = gpu_read(gpu, VIVS_HI_IDLE_STATE) & mask;
1676a8c21a54SThe etnaviv authors 	if (idle != mask)
1677a8c21a54SThe etnaviv authors 		return -EBUSY;
1678a8c21a54SThe etnaviv authors 
1679a8c21a54SThe etnaviv authors 	return etnaviv_gpu_hw_suspend(gpu);
1680a8c21a54SThe etnaviv authors }
1681a8c21a54SThe etnaviv authors 
1682a8c21a54SThe etnaviv authors static int etnaviv_gpu_rpm_resume(struct device *dev)
1683a8c21a54SThe etnaviv authors {
1684a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1685a8c21a54SThe etnaviv authors 	int ret;
1686a8c21a54SThe etnaviv authors 
1687a8c21a54SThe etnaviv authors 	ret = etnaviv_gpu_clk_enable(gpu);
1688a8c21a54SThe etnaviv authors 	if (ret)
1689a8c21a54SThe etnaviv authors 		return ret;
1690a8c21a54SThe etnaviv authors 
1691a8c21a54SThe etnaviv authors 	/* Re-initialise the basic hardware state */
1692a8c21a54SThe etnaviv authors 	if (gpu->drm && gpu->buffer) {
1693a8c21a54SThe etnaviv authors 		ret = etnaviv_gpu_hw_resume(gpu);
1694a8c21a54SThe etnaviv authors 		if (ret) {
1695a8c21a54SThe etnaviv authors 			etnaviv_gpu_clk_disable(gpu);
1696a8c21a54SThe etnaviv authors 			return ret;
1697a8c21a54SThe etnaviv authors 		}
1698a8c21a54SThe etnaviv authors 	}
1699a8c21a54SThe etnaviv authors 
1700a8c21a54SThe etnaviv authors 	return 0;
1701a8c21a54SThe etnaviv authors }
1702a8c21a54SThe etnaviv authors #endif
1703a8c21a54SThe etnaviv authors 
1704a8c21a54SThe etnaviv authors static const struct dev_pm_ops etnaviv_gpu_pm_ops = {
1705a8c21a54SThe etnaviv authors 	SET_RUNTIME_PM_OPS(etnaviv_gpu_rpm_suspend, etnaviv_gpu_rpm_resume,
1706a8c21a54SThe etnaviv authors 			   NULL)
1707a8c21a54SThe etnaviv authors };
1708a8c21a54SThe etnaviv authors 
1709a8c21a54SThe etnaviv authors struct platform_driver etnaviv_gpu_driver = {
1710a8c21a54SThe etnaviv authors 	.driver = {
1711a8c21a54SThe etnaviv authors 		.name = "etnaviv-gpu",
1712a8c21a54SThe etnaviv authors 		.owner = THIS_MODULE,
1713a8c21a54SThe etnaviv authors 		.pm = &etnaviv_gpu_pm_ops,
1714a8c21a54SThe etnaviv authors 		.of_match_table = etnaviv_gpu_match,
1715a8c21a54SThe etnaviv authors 	},
1716a8c21a54SThe etnaviv authors 	.probe = etnaviv_gpu_platform_probe,
1717a8c21a54SThe etnaviv authors 	.remove = etnaviv_gpu_platform_remove,
1718a8c21a54SThe etnaviv authors 	.id_table = gpu_ids,
1719a8c21a54SThe etnaviv authors };
1720