blob: 42ad286d5ec07e49b7d2b81f4501ac22a7413121 [file] [log] [blame]
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001/*
2 * Copyright (C) 2015 Etnaviv Project
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <linux/component.h>
Chris Wilsonf54d1862016-10-25 13:00:45 +010018#include <linux/dma-fence.h>
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010019#include <linux/moduleparam.h>
20#include <linux/of_device.h>
Russell Kingbcdfb5e2017-03-12 19:00:59 +000021#include <linux/thermal.h>
Lucas Stachea1f5722017-01-16 16:09:51 +010022
23#include "etnaviv_cmdbuf.h"
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010024#include "etnaviv_dump.h"
25#include "etnaviv_gpu.h"
26#include "etnaviv_gem.h"
27#include "etnaviv_mmu.h"
Christian Gmeiner357713c2017-09-24 15:15:28 +020028#include "etnaviv_perfmon.h"
Lucas Stache93b6de2017-12-04 18:41:58 +010029#include "etnaviv_sched.h"
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010030#include "common.xml.h"
31#include "state.xml.h"
32#include "state_hi.xml.h"
33#include "cmdstream.xml.h"
34
Lucas Stachc09d7f72018-01-04 13:40:03 +010035#ifndef PHYS_OFFSET
36#define PHYS_OFFSET 0
37#endif
38
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010039static const struct platform_device_id gpu_ids[] = {
40 { .name = "etnaviv-gpu,2d" },
41 { },
42};
43
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010044/*
45 * Driver functions:
46 */
47
48int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value)
49{
50 switch (param) {
51 case ETNAVIV_PARAM_GPU_MODEL:
52 *value = gpu->identity.model;
53 break;
54
55 case ETNAVIV_PARAM_GPU_REVISION:
56 *value = gpu->identity.revision;
57 break;
58
59 case ETNAVIV_PARAM_GPU_FEATURES_0:
60 *value = gpu->identity.features;
61 break;
62
63 case ETNAVIV_PARAM_GPU_FEATURES_1:
64 *value = gpu->identity.minor_features0;
65 break;
66
67 case ETNAVIV_PARAM_GPU_FEATURES_2:
68 *value = gpu->identity.minor_features1;
69 break;
70
71 case ETNAVIV_PARAM_GPU_FEATURES_3:
72 *value = gpu->identity.minor_features2;
73 break;
74
75 case ETNAVIV_PARAM_GPU_FEATURES_4:
76 *value = gpu->identity.minor_features3;
77 break;
78
Russell King602eb482016-01-24 17:36:04 +000079 case ETNAVIV_PARAM_GPU_FEATURES_5:
80 *value = gpu->identity.minor_features4;
81 break;
82
83 case ETNAVIV_PARAM_GPU_FEATURES_6:
84 *value = gpu->identity.minor_features5;
85 break;
86
Lucas Stach0538aaf2018-01-22 15:56:11 +010087 case ETNAVIV_PARAM_GPU_FEATURES_7:
88 *value = gpu->identity.minor_features6;
89 break;
90
91 case ETNAVIV_PARAM_GPU_FEATURES_8:
92 *value = gpu->identity.minor_features7;
93 break;
94
95 case ETNAVIV_PARAM_GPU_FEATURES_9:
96 *value = gpu->identity.minor_features8;
97 break;
98
99 case ETNAVIV_PARAM_GPU_FEATURES_10:
100 *value = gpu->identity.minor_features9;
101 break;
102
103 case ETNAVIV_PARAM_GPU_FEATURES_11:
104 *value = gpu->identity.minor_features10;
105 break;
106
107 case ETNAVIV_PARAM_GPU_FEATURES_12:
108 *value = gpu->identity.minor_features11;
109 break;
110
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100111 case ETNAVIV_PARAM_GPU_STREAM_COUNT:
112 *value = gpu->identity.stream_count;
113 break;
114
115 case ETNAVIV_PARAM_GPU_REGISTER_MAX:
116 *value = gpu->identity.register_max;
117 break;
118
119 case ETNAVIV_PARAM_GPU_THREAD_COUNT:
120 *value = gpu->identity.thread_count;
121 break;
122
123 case ETNAVIV_PARAM_GPU_VERTEX_CACHE_SIZE:
124 *value = gpu->identity.vertex_cache_size;
125 break;
126
127 case ETNAVIV_PARAM_GPU_SHADER_CORE_COUNT:
128 *value = gpu->identity.shader_core_count;
129 break;
130
131 case ETNAVIV_PARAM_GPU_PIXEL_PIPES:
132 *value = gpu->identity.pixel_pipes;
133 break;
134
135 case ETNAVIV_PARAM_GPU_VERTEX_OUTPUT_BUFFER_SIZE:
136 *value = gpu->identity.vertex_output_buffer_size;
137 break;
138
139 case ETNAVIV_PARAM_GPU_BUFFER_SIZE:
140 *value = gpu->identity.buffer_size;
141 break;
142
143 case ETNAVIV_PARAM_GPU_INSTRUCTION_COUNT:
144 *value = gpu->identity.instruction_count;
145 break;
146
147 case ETNAVIV_PARAM_GPU_NUM_CONSTANTS:
148 *value = gpu->identity.num_constants;
149 break;
150
Russell King602eb482016-01-24 17:36:04 +0000151 case ETNAVIV_PARAM_GPU_NUM_VARYINGS:
152 *value = gpu->identity.varyings_count;
153 break;
154
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100155 default:
156 DBG("%s: invalid param: %u", dev_name(gpu->dev), param);
157 return -EINVAL;
158 }
159
160 return 0;
161}
162
Russell King472f79d2016-01-24 17:35:59 +0000163
164#define etnaviv_is_model_rev(gpu, mod, rev) \
165 ((gpu)->identity.model == chipModel_##mod && \
166 (gpu)->identity.revision == rev)
Russell King52f36ba2016-01-24 17:35:54 +0000167#define etnaviv_field(val, field) \
168 (((val) & field##__MASK) >> field##__SHIFT)
169
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100170static void etnaviv_hw_specs(struct etnaviv_gpu *gpu)
171{
172 if (gpu->identity.minor_features0 &
173 chipMinorFeatures0_MORE_MINOR_FEATURES) {
Russell King602eb482016-01-24 17:36:04 +0000174 u32 specs[4];
175 unsigned int streams;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100176
177 specs[0] = gpu_read(gpu, VIVS_HI_CHIP_SPECS);
178 specs[1] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_2);
Russell King602eb482016-01-24 17:36:04 +0000179 specs[2] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_3);
180 specs[3] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_4);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100181
Russell King52f36ba2016-01-24 17:35:54 +0000182 gpu->identity.stream_count = etnaviv_field(specs[0],
183 VIVS_HI_CHIP_SPECS_STREAM_COUNT);
184 gpu->identity.register_max = etnaviv_field(specs[0],
185 VIVS_HI_CHIP_SPECS_REGISTER_MAX);
186 gpu->identity.thread_count = etnaviv_field(specs[0],
187 VIVS_HI_CHIP_SPECS_THREAD_COUNT);
188 gpu->identity.vertex_cache_size = etnaviv_field(specs[0],
189 VIVS_HI_CHIP_SPECS_VERTEX_CACHE_SIZE);
190 gpu->identity.shader_core_count = etnaviv_field(specs[0],
191 VIVS_HI_CHIP_SPECS_SHADER_CORE_COUNT);
192 gpu->identity.pixel_pipes = etnaviv_field(specs[0],
193 VIVS_HI_CHIP_SPECS_PIXEL_PIPES);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100194 gpu->identity.vertex_output_buffer_size =
Russell King52f36ba2016-01-24 17:35:54 +0000195 etnaviv_field(specs[0],
196 VIVS_HI_CHIP_SPECS_VERTEX_OUTPUT_BUFFER_SIZE);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100197
Russell King52f36ba2016-01-24 17:35:54 +0000198 gpu->identity.buffer_size = etnaviv_field(specs[1],
199 VIVS_HI_CHIP_SPECS_2_BUFFER_SIZE);
200 gpu->identity.instruction_count = etnaviv_field(specs[1],
201 VIVS_HI_CHIP_SPECS_2_INSTRUCTION_COUNT);
202 gpu->identity.num_constants = etnaviv_field(specs[1],
203 VIVS_HI_CHIP_SPECS_2_NUM_CONSTANTS);
Russell King602eb482016-01-24 17:36:04 +0000204
205 gpu->identity.varyings_count = etnaviv_field(specs[2],
206 VIVS_HI_CHIP_SPECS_3_VARYINGS_COUNT);
207
208 /* This overrides the value from older register if non-zero */
209 streams = etnaviv_field(specs[3],
210 VIVS_HI_CHIP_SPECS_4_STREAM_COUNT);
211 if (streams)
212 gpu->identity.stream_count = streams;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100213 }
214
215 /* Fill in the stream count if not specified */
216 if (gpu->identity.stream_count == 0) {
217 if (gpu->identity.model >= 0x1000)
218 gpu->identity.stream_count = 4;
219 else
220 gpu->identity.stream_count = 1;
221 }
222
223 /* Convert the register max value */
224 if (gpu->identity.register_max)
225 gpu->identity.register_max = 1 << gpu->identity.register_max;
Russell King507f8992016-01-24 17:35:48 +0000226 else if (gpu->identity.model == chipModel_GC400)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100227 gpu->identity.register_max = 32;
228 else
229 gpu->identity.register_max = 64;
230
231 /* Convert thread count */
232 if (gpu->identity.thread_count)
233 gpu->identity.thread_count = 1 << gpu->identity.thread_count;
Russell King507f8992016-01-24 17:35:48 +0000234 else if (gpu->identity.model == chipModel_GC400)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100235 gpu->identity.thread_count = 64;
Russell King507f8992016-01-24 17:35:48 +0000236 else if (gpu->identity.model == chipModel_GC500 ||
237 gpu->identity.model == chipModel_GC530)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100238 gpu->identity.thread_count = 128;
239 else
240 gpu->identity.thread_count = 256;
241
242 if (gpu->identity.vertex_cache_size == 0)
243 gpu->identity.vertex_cache_size = 8;
244
245 if (gpu->identity.shader_core_count == 0) {
246 if (gpu->identity.model >= 0x1000)
247 gpu->identity.shader_core_count = 2;
248 else
249 gpu->identity.shader_core_count = 1;
250 }
251
252 if (gpu->identity.pixel_pipes == 0)
253 gpu->identity.pixel_pipes = 1;
254
255 /* Convert virtex buffer size */
256 if (gpu->identity.vertex_output_buffer_size) {
257 gpu->identity.vertex_output_buffer_size =
258 1 << gpu->identity.vertex_output_buffer_size;
Russell King507f8992016-01-24 17:35:48 +0000259 } else if (gpu->identity.model == chipModel_GC400) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100260 if (gpu->identity.revision < 0x4000)
261 gpu->identity.vertex_output_buffer_size = 512;
262 else if (gpu->identity.revision < 0x4200)
263 gpu->identity.vertex_output_buffer_size = 256;
264 else
265 gpu->identity.vertex_output_buffer_size = 128;
266 } else {
267 gpu->identity.vertex_output_buffer_size = 512;
268 }
269
270 switch (gpu->identity.instruction_count) {
271 case 0:
Russell King472f79d2016-01-24 17:35:59 +0000272 if (etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
Russell King507f8992016-01-24 17:35:48 +0000273 gpu->identity.model == chipModel_GC880)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100274 gpu->identity.instruction_count = 512;
275 else
276 gpu->identity.instruction_count = 256;
277 break;
278
279 case 1:
280 gpu->identity.instruction_count = 1024;
281 break;
282
283 case 2:
284 gpu->identity.instruction_count = 2048;
285 break;
286
287 default:
288 gpu->identity.instruction_count = 256;
289 break;
290 }
291
292 if (gpu->identity.num_constants == 0)
293 gpu->identity.num_constants = 168;
Russell King602eb482016-01-24 17:36:04 +0000294
295 if (gpu->identity.varyings_count == 0) {
296 if (gpu->identity.minor_features1 & chipMinorFeatures1_HALTI0)
297 gpu->identity.varyings_count = 12;
298 else
299 gpu->identity.varyings_count = 8;
300 }
301
302 /*
303 * For some cores, two varyings are consumed for position, so the
304 * maximum varying count needs to be reduced by one.
305 */
306 if (etnaviv_is_model_rev(gpu, GC5000, 0x5434) ||
307 etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
308 etnaviv_is_model_rev(gpu, GC4000, 0x5245) ||
309 etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
310 etnaviv_is_model_rev(gpu, GC3000, 0x5435) ||
311 etnaviv_is_model_rev(gpu, GC2200, 0x5244) ||
312 etnaviv_is_model_rev(gpu, GC2100, 0x5108) ||
313 etnaviv_is_model_rev(gpu, GC2000, 0x5108) ||
314 etnaviv_is_model_rev(gpu, GC1500, 0x5246) ||
315 etnaviv_is_model_rev(gpu, GC880, 0x5107) ||
316 etnaviv_is_model_rev(gpu, GC880, 0x5106))
317 gpu->identity.varyings_count -= 1;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100318}
319
320static void etnaviv_hw_identify(struct etnaviv_gpu *gpu)
321{
322 u32 chipIdentity;
323
324 chipIdentity = gpu_read(gpu, VIVS_HI_CHIP_IDENTITY);
325
326 /* Special case for older graphic cores. */
Russell King52f36ba2016-01-24 17:35:54 +0000327 if (etnaviv_field(chipIdentity, VIVS_HI_CHIP_IDENTITY_FAMILY) == 0x01) {
Russell King507f8992016-01-24 17:35:48 +0000328 gpu->identity.model = chipModel_GC500;
Russell King52f36ba2016-01-24 17:35:54 +0000329 gpu->identity.revision = etnaviv_field(chipIdentity,
330 VIVS_HI_CHIP_IDENTITY_REVISION);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100331 } else {
332
333 gpu->identity.model = gpu_read(gpu, VIVS_HI_CHIP_MODEL);
334 gpu->identity.revision = gpu_read(gpu, VIVS_HI_CHIP_REV);
335
336 /*
337 * !!!! HACK ALERT !!!!
338 * Because people change device IDs without letting software
339 * know about it - here is the hack to make it all look the
340 * same. Only for GC400 family.
341 */
342 if ((gpu->identity.model & 0xff00) == 0x0400 &&
Russell King507f8992016-01-24 17:35:48 +0000343 gpu->identity.model != chipModel_GC420) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100344 gpu->identity.model = gpu->identity.model & 0x0400;
345 }
346
347 /* Another special case */
Russell King472f79d2016-01-24 17:35:59 +0000348 if (etnaviv_is_model_rev(gpu, GC300, 0x2201)) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100349 u32 chipDate = gpu_read(gpu, VIVS_HI_CHIP_DATE);
350 u32 chipTime = gpu_read(gpu, VIVS_HI_CHIP_TIME);
351
352 if (chipDate == 0x20080814 && chipTime == 0x12051100) {
353 /*
354 * This IP has an ECO; put the correct
355 * revision in it.
356 */
357 gpu->identity.revision = 0x1051;
358 }
359 }
Lucas Stach12ff4bd2016-08-15 18:16:59 +0200360
361 /*
362 * NXP likes to call the GPU on the i.MX6QP GC2000+, but in
363 * reality it's just a re-branded GC3000. We can identify this
364 * core by the upper half of the revision register being all 1.
365 * Fix model/rev here, so all other places can refer to this
366 * core by its real identity.
367 */
368 if (etnaviv_is_model_rev(gpu, GC2000, 0xffff5450)) {
369 gpu->identity.model = chipModel_GC3000;
370 gpu->identity.revision &= 0xffff;
371 }
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100372 }
373
374 dev_info(gpu->dev, "model: GC%x, revision: %x\n",
375 gpu->identity.model, gpu->identity.revision);
376
377 gpu->identity.features = gpu_read(gpu, VIVS_HI_CHIP_FEATURE);
378
379 /* Disable fast clear on GC700. */
Russell King507f8992016-01-24 17:35:48 +0000380 if (gpu->identity.model == chipModel_GC700)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100381 gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
382
Russell King507f8992016-01-24 17:35:48 +0000383 if ((gpu->identity.model == chipModel_GC500 &&
384 gpu->identity.revision < 2) ||
385 (gpu->identity.model == chipModel_GC300 &&
386 gpu->identity.revision < 0x2000)) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100387
388 /*
389 * GC500 rev 1.x and GC300 rev < 2.0 doesn't have these
390 * registers.
391 */
392 gpu->identity.minor_features0 = 0;
393 gpu->identity.minor_features1 = 0;
394 gpu->identity.minor_features2 = 0;
395 gpu->identity.minor_features3 = 0;
Russell King602eb482016-01-24 17:36:04 +0000396 gpu->identity.minor_features4 = 0;
397 gpu->identity.minor_features5 = 0;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100398 } else
399 gpu->identity.minor_features0 =
400 gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_0);
401
402 if (gpu->identity.minor_features0 &
403 chipMinorFeatures0_MORE_MINOR_FEATURES) {
404 gpu->identity.minor_features1 =
405 gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_1);
406 gpu->identity.minor_features2 =
407 gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_2);
408 gpu->identity.minor_features3 =
409 gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_3);
Russell King602eb482016-01-24 17:36:04 +0000410 gpu->identity.minor_features4 =
411 gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_4);
412 gpu->identity.minor_features5 =
413 gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_5);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100414 }
415
416 /* GC600 idle register reports zero bits where modules aren't present */
417 if (gpu->identity.model == chipModel_GC600) {
418 gpu->idle_mask = VIVS_HI_IDLE_STATE_TX |
419 VIVS_HI_IDLE_STATE_RA |
420 VIVS_HI_IDLE_STATE_SE |
421 VIVS_HI_IDLE_STATE_PA |
422 VIVS_HI_IDLE_STATE_SH |
423 VIVS_HI_IDLE_STATE_PE |
424 VIVS_HI_IDLE_STATE_DE |
425 VIVS_HI_IDLE_STATE_FE;
426 } else {
427 gpu->idle_mask = ~VIVS_HI_IDLE_STATE_AXI_LP;
428 }
429
430 etnaviv_hw_specs(gpu);
431}
432
433static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
434{
435 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock |
436 VIVS_HI_CLOCK_CONTROL_FSCALE_CMD_LOAD);
437 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
438}
439
Russell Kingbcdfb5e2017-03-12 19:00:59 +0000440static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu)
441{
Lucas Stachd79fd1ccf22017-04-11 15:54:50 +0200442 if (gpu->identity.minor_features2 &
443 chipMinorFeatures2_DYNAMIC_FREQUENCY_SCALING) {
444 clk_set_rate(gpu->clk_core,
445 gpu->base_rate_core >> gpu->freq_scale);
446 clk_set_rate(gpu->clk_shader,
447 gpu->base_rate_shader >> gpu->freq_scale);
448 } else {
449 unsigned int fscale = 1 << (6 - gpu->freq_scale);
Lucas Stach6eb3ecc2017-09-28 15:41:21 +0200450 u32 clock = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
Russell Kingbcdfb5e2017-03-12 19:00:59 +0000451
Lucas Stach6eb3ecc2017-09-28 15:41:21 +0200452 clock &= ~VIVS_HI_CLOCK_CONTROL_FSCALE_VAL__MASK;
453 clock |= VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
Lucas Stachd79fd1ccf22017-04-11 15:54:50 +0200454 etnaviv_gpu_load_clock(gpu, clock);
455 }
Russell Kingbcdfb5e2017-03-12 19:00:59 +0000456}
457
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100458static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
459{
460 u32 control, idle;
461 unsigned long timeout;
462 bool failed = true;
463
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100464 /* We hope that the GPU resets in under one second */
465 timeout = jiffies + msecs_to_jiffies(1000);
466
467 while (time_is_after_jiffies(timeout)) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100468 /* enable clock */
Lucas Stach6eb3ecc2017-09-28 15:41:21 +0200469 unsigned int fscale = 1 << (6 - gpu->freq_scale);
470 control = VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
471 etnaviv_gpu_load_clock(gpu, control);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100472
473 /* isolate the GPU. */
474 control |= VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
475 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
476
477 /* set soft reset. */
478 control |= VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
479 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
480
481 /* wait for reset. */
Philipp Zabel40462172017-10-09 12:03:30 +0200482 usleep_range(10, 20);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100483
484 /* reset soft reset bit. */
485 control &= ~VIVS_HI_CLOCK_CONTROL_SOFT_RESET;
486 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
487
488 /* reset GPU isolation. */
489 control &= ~VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU;
490 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
491
492 /* read idle register. */
493 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
494
495 /* try reseting again if FE it not idle */
496 if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) {
497 dev_dbg(gpu->dev, "FE is not idle\n");
498 continue;
499 }
500
501 /* read reset register. */
502 control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
503
504 /* is the GPU idle? */
505 if (((control & VIVS_HI_CLOCK_CONTROL_IDLE_3D) == 0) ||
506 ((control & VIVS_HI_CLOCK_CONTROL_IDLE_2D) == 0)) {
507 dev_dbg(gpu->dev, "GPU is not idle\n");
508 continue;
509 }
510
Lucas Stach6eb3ecc2017-09-28 15:41:21 +0200511 /* disable debug registers, as they are not normally needed */
512 control |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
513 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control);
514
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100515 failed = false;
516 break;
517 }
518
519 if (failed) {
520 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
521 control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
522
523 dev_err(gpu->dev, "GPU failed to reset: FE %sidle, 3D %sidle, 2D %sidle\n",
524 idle & VIVS_HI_IDLE_STATE_FE ? "" : "not ",
525 control & VIVS_HI_CLOCK_CONTROL_IDLE_3D ? "" : "not ",
526 control & VIVS_HI_CLOCK_CONTROL_IDLE_2D ? "" : "not ");
527
528 return -EBUSY;
529 }
530
531 /* We rely on the GPU running, so program the clock */
Russell Kingbcdfb5e2017-03-12 19:00:59 +0000532 etnaviv_gpu_update_clock(gpu);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100533
534 return 0;
535}
536
Russell King7d0c6e72016-01-21 15:20:45 +0000537static void etnaviv_gpu_enable_mlcg(struct etnaviv_gpu *gpu)
538{
539 u32 pmc, ppc;
540
541 /* enable clock gating */
542 ppc = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
543 ppc |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
544
545 /* Disable stall module clock gating for 4.3.0.1 and 4.3.0.2 revs */
546 if (gpu->identity.revision == 0x4301 ||
547 gpu->identity.revision == 0x4302)
548 ppc |= VIVS_PM_POWER_CONTROLS_DISABLE_STALL_MODULE_CLOCK_GATING;
549
550 gpu_write(gpu, VIVS_PM_POWER_CONTROLS, ppc);
551
552 pmc = gpu_read(gpu, VIVS_PM_MODULE_CONTROLS);
553
Lucas Stach7cef6002017-03-17 12:42:30 +0100554 /* Disable PA clock gating for GC400+ without bugfix except for GC420 */
Russell King7d0c6e72016-01-21 15:20:45 +0000555 if (gpu->identity.model >= chipModel_GC400 &&
Lucas Stach7cef6002017-03-17 12:42:30 +0100556 gpu->identity.model != chipModel_GC420 &&
557 !(gpu->identity.minor_features3 & chipMinorFeatures3_BUG_FIXES12))
Russell King7d0c6e72016-01-21 15:20:45 +0000558 pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PA;
559
560 /*
561 * Disable PE clock gating on revs < 5.0.0.0 when HZ is
562 * present without a bug fix.
563 */
564 if (gpu->identity.revision < 0x5000 &&
565 gpu->identity.minor_features0 & chipMinorFeatures0_HZ &&
566 !(gpu->identity.minor_features1 &
567 chipMinorFeatures1_DISABLE_PE_GATING))
568 pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PE;
569
570 if (gpu->identity.revision < 0x5422)
571 pmc |= BIT(15); /* Unknown bit */
572
Lucas Stach7cef6002017-03-17 12:42:30 +0100573 /* Disable TX clock gating on affected core revisions. */
574 if (etnaviv_is_model_rev(gpu, GC4000, 0x5222) ||
575 etnaviv_is_model_rev(gpu, GC2000, 0x5108))
576 pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_TX;
577
Russell King7d0c6e72016-01-21 15:20:45 +0000578 pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_HZ;
579 pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_EZ;
580
581 gpu_write(gpu, VIVS_PM_MODULE_CONTROLS, pmc);
582}
583
Lucas Stach229855b2016-08-17 15:27:52 +0200584void etnaviv_gpu_start_fe(struct etnaviv_gpu *gpu, u32 address, u16 prefetch)
585{
586 gpu_write(gpu, VIVS_FE_COMMAND_ADDRESS, address);
587 gpu_write(gpu, VIVS_FE_COMMAND_CONTROL,
588 VIVS_FE_COMMAND_CONTROL_ENABLE |
589 VIVS_FE_COMMAND_CONTROL_PREFETCH(prefetch));
590}
591
Wladimir J. van der Laane17a0de2016-12-15 13:11:30 +0100592static void etnaviv_gpu_setup_pulse_eater(struct etnaviv_gpu *gpu)
593{
594 /*
595 * Base value for VIVS_PM_PULSE_EATER register on models where it
596 * cannot be read, extracted from vivante kernel driver.
597 */
598 u32 pulse_eater = 0x01590880;
599
600 if (etnaviv_is_model_rev(gpu, GC4000, 0x5208) ||
601 etnaviv_is_model_rev(gpu, GC4000, 0x5222)) {
602 pulse_eater |= BIT(23);
603
604 }
605
606 if (etnaviv_is_model_rev(gpu, GC1000, 0x5039) ||
607 etnaviv_is_model_rev(gpu, GC1000, 0x5040)) {
608 pulse_eater &= ~BIT(16);
609 pulse_eater |= BIT(17);
610 }
611
612 if ((gpu->identity.revision > 0x5420) &&
613 (gpu->identity.features & chipFeatures_PIPE_3D))
614 {
615 /* Performance fix: disable internal DFS */
616 pulse_eater = gpu_read(gpu, VIVS_PM_PULSE_EATER);
617 pulse_eater |= BIT(18);
618 }
619
620 gpu_write(gpu, VIVS_PM_PULSE_EATER, pulse_eater);
621}
622
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100623static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu)
624{
625 u16 prefetch;
626
Russell King472f79d2016-01-24 17:35:59 +0000627 if ((etnaviv_is_model_rev(gpu, GC320, 0x5007) ||
628 etnaviv_is_model_rev(gpu, GC320, 0x5220)) &&
629 gpu_read(gpu, VIVS_HI_CHIP_TIME) != 0x2062400) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100630 u32 mc_memory_debug;
631
632 mc_memory_debug = gpu_read(gpu, VIVS_MC_DEBUG_MEMORY) & ~0xff;
633
634 if (gpu->identity.revision == 0x5007)
635 mc_memory_debug |= 0x0c;
636 else
637 mc_memory_debug |= 0x08;
638
639 gpu_write(gpu, VIVS_MC_DEBUG_MEMORY, mc_memory_debug);
640 }
641
Russell King7d0c6e72016-01-21 15:20:45 +0000642 /* enable module-level clock gating */
643 etnaviv_gpu_enable_mlcg(gpu);
644
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100645 /*
646 * Update GPU AXI cache atttribute to "cacheable, no allocate".
647 * This is necessary to prevent the iMX6 SoC locking up.
648 */
649 gpu_write(gpu, VIVS_HI_AXI_CONFIG,
650 VIVS_HI_AXI_CONFIG_AWCACHE(2) |
651 VIVS_HI_AXI_CONFIG_ARCACHE(2));
652
653 /* GC2000 rev 5108 needs a special bus config */
Russell King472f79d2016-01-24 17:35:59 +0000654 if (etnaviv_is_model_rev(gpu, GC2000, 0x5108)) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100655 u32 bus_config = gpu_read(gpu, VIVS_MC_BUS_CONFIG);
656 bus_config &= ~(VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG__MASK |
657 VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG__MASK);
658 bus_config |= VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG(1) |
659 VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG(0);
660 gpu_write(gpu, VIVS_MC_BUS_CONFIG, bus_config);
661 }
662
Wladimir J. van der Laane17a0de2016-12-15 13:11:30 +0100663 /* setup the pulse eater */
664 etnaviv_gpu_setup_pulse_eater(gpu);
665
Lucas Stach99f861b2016-08-16 11:48:49 +0200666 /* setup the MMU */
Lucas Stache095c8f2016-08-16 11:54:51 +0200667 etnaviv_iommu_restore(gpu);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100668
669 /* Start command processor */
670 prefetch = etnaviv_buffer_init(gpu);
671
672 gpu_write(gpu, VIVS_HI_INTR_ENBL, ~0U);
Lucas Stach2f9225d2017-11-24 16:56:37 +0100673 etnaviv_gpu_start_fe(gpu, etnaviv_cmdbuf_get_va(&gpu->buffer),
Lucas Stach229855b2016-08-17 15:27:52 +0200674 prefetch);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100675}
676
677int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
678{
679 int ret, i;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100680
681 ret = pm_runtime_get_sync(gpu->dev);
Lucas Stach1409df02016-06-17 12:29:02 +0200682 if (ret < 0) {
683 dev_err(gpu->dev, "Failed to enable GPU power domain\n");
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100684 return ret;
Lucas Stach1409df02016-06-17 12:29:02 +0200685 }
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100686
687 etnaviv_hw_identify(gpu);
688
689 if (gpu->identity.model == 0) {
690 dev_err(gpu->dev, "Unknown GPU model\n");
Russell Kingf6427762016-01-24 17:32:13 +0000691 ret = -ENXIO;
692 goto fail;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100693 }
694
Russell Kingb98c6682016-01-21 15:19:59 +0000695 /* Exclude VG cores with FE2.0 */
696 if (gpu->identity.features & chipFeatures_PIPE_VG &&
697 gpu->identity.features & chipFeatures_FE20) {
698 dev_info(gpu->dev, "Ignoring GPU with VG and FE2.0\n");
699 ret = -ENXIO;
700 goto fail;
701 }
702
Lucas Stach2144fff2016-04-21 13:52:38 +0200703 /*
704 * Set the GPU linear window to be at the end of the DMA window, where
705 * the CMA area is likely to reside. This ensures that we are able to
706 * map the command buffers while having the linear window overlap as
707 * much RAM as possible, so we can optimize mappings for other buffers.
708 *
709 * For 3D cores only do this if MC2.0 is present, as with MC1.0 it leads
710 * to different views of the memory on the individual engines.
711 */
712 if (!(gpu->identity.features & chipFeatures_PIPE_3D) ||
713 (gpu->identity.minor_features0 & chipMinorFeatures0_MC20)) {
714 u32 dma_mask = (u32)dma_get_required_mask(gpu->dev);
715 if (dma_mask < PHYS_OFFSET + SZ_2G)
716 gpu->memory_base = PHYS_OFFSET;
717 else
718 gpu->memory_base = dma_mask - SZ_2G + 1;
Lucas Stach1db01272016-12-02 12:19:16 +0100719 } else if (PHYS_OFFSET >= SZ_2G) {
720 dev_info(gpu->dev, "Need to move linear window on MC1.0, disabling TS\n");
721 gpu->memory_base = PHYS_OFFSET;
722 gpu->identity.features &= ~chipFeatures_FAST_CLEAR;
Lucas Stach2144fff2016-04-21 13:52:38 +0200723 }
724
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100725 ret = etnaviv_hw_reset(gpu);
Lucas Stach1409df02016-06-17 12:29:02 +0200726 if (ret) {
727 dev_err(gpu->dev, "GPU reset failed\n");
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100728 goto fail;
Lucas Stach1409df02016-06-17 12:29:02 +0200729 }
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100730
Lucas Stachdd34bb92016-08-16 12:09:08 +0200731 gpu->mmu = etnaviv_iommu_new(gpu);
732 if (IS_ERR(gpu->mmu)) {
Lucas Stach1409df02016-06-17 12:29:02 +0200733 dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n");
Lucas Stachdd34bb92016-08-16 12:09:08 +0200734 ret = PTR_ERR(gpu->mmu);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100735 goto fail;
736 }
737
Lucas Stache66774d2017-01-16 17:29:57 +0100738 gpu->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(gpu);
739 if (IS_ERR(gpu->cmdbuf_suballoc)) {
740 dev_err(gpu->dev, "Failed to create cmdbuf suballocator\n");
741 ret = PTR_ERR(gpu->cmdbuf_suballoc);
742 goto fail;
743 }
744
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100745 /* Create buffer: */
Lucas Stach2f9225d2017-11-24 16:56:37 +0100746 ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &gpu->buffer,
747 PAGE_SIZE);
748 if (ret) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100749 dev_err(gpu->dev, "could not create command buffer\n");
Lucas Stach45d16a62016-01-25 12:41:05 +0100750 goto destroy_iommu;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100751 }
Lucas Stachacfee0e2016-08-17 16:19:53 +0200752
753 if (gpu->mmu->version == ETNAVIV_IOMMU_V1 &&
Lucas Stach2f9225d2017-11-24 16:56:37 +0100754 etnaviv_cmdbuf_get_va(&gpu->buffer) > 0x80000000) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100755 ret = -EINVAL;
756 dev_err(gpu->dev,
757 "command buffer outside valid memory window\n");
758 goto free_buffer;
759 }
760
761 /* Setup event management */
762 spin_lock_init(&gpu->event_spinlock);
763 init_completion(&gpu->event_free);
Christian Gmeiner355502e2017-09-24 15:15:19 +0200764 bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS);
765 for (i = 0; i < ARRAY_SIZE(gpu->event); i++)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100766 complete(&gpu->event_free);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100767
768 /* Now program the hardware */
769 mutex_lock(&gpu->lock);
770 etnaviv_gpu_hw_init(gpu);
Russell Kingf6086312016-01-21 15:20:19 +0000771 gpu->exec_state = -1;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100772 mutex_unlock(&gpu->lock);
773
774 pm_runtime_mark_last_busy(gpu->dev);
775 pm_runtime_put_autosuspend(gpu->dev);
776
777 return 0;
778
779free_buffer:
Lucas Stach2f9225d2017-11-24 16:56:37 +0100780 etnaviv_cmdbuf_free(&gpu->buffer);
Lucas Stach45d16a62016-01-25 12:41:05 +0100781destroy_iommu:
782 etnaviv_iommu_destroy(gpu->mmu);
783 gpu->mmu = NULL;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100784fail:
785 pm_runtime_mark_last_busy(gpu->dev);
786 pm_runtime_put_autosuspend(gpu->dev);
787
788 return ret;
789}
790
791#ifdef CONFIG_DEBUG_FS
792struct dma_debug {
793 u32 address[2];
794 u32 state[2];
795};
796
797static void verify_dma(struct etnaviv_gpu *gpu, struct dma_debug *debug)
798{
799 u32 i;
800
801 debug->address[0] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
802 debug->state[0] = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
803
804 for (i = 0; i < 500; i++) {
805 debug->address[1] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
806 debug->state[1] = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE);
807
808 if (debug->address[0] != debug->address[1])
809 break;
810
811 if (debug->state[0] != debug->state[1])
812 break;
813 }
814}
815
816int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m)
817{
818 struct dma_debug debug;
819 u32 dma_lo, dma_hi, axi, idle;
820 int ret;
821
822 seq_printf(m, "%s Status:\n", dev_name(gpu->dev));
823
824 ret = pm_runtime_get_sync(gpu->dev);
825 if (ret < 0)
826 return ret;
827
828 dma_lo = gpu_read(gpu, VIVS_FE_DMA_LOW);
829 dma_hi = gpu_read(gpu, VIVS_FE_DMA_HIGH);
830 axi = gpu_read(gpu, VIVS_HI_AXI_STATUS);
831 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
832
833 verify_dma(gpu, &debug);
834
835 seq_puts(m, "\tfeatures\n");
Lucas Stach3d9fc642018-01-04 13:50:14 +0100836 seq_printf(m, "\t major_features: 0x%08x\n",
837 gpu->identity.features);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100838 seq_printf(m, "\t minor_features0: 0x%08x\n",
839 gpu->identity.minor_features0);
840 seq_printf(m, "\t minor_features1: 0x%08x\n",
841 gpu->identity.minor_features1);
842 seq_printf(m, "\t minor_features2: 0x%08x\n",
843 gpu->identity.minor_features2);
844 seq_printf(m, "\t minor_features3: 0x%08x\n",
845 gpu->identity.minor_features3);
Russell King602eb482016-01-24 17:36:04 +0000846 seq_printf(m, "\t minor_features4: 0x%08x\n",
847 gpu->identity.minor_features4);
848 seq_printf(m, "\t minor_features5: 0x%08x\n",
849 gpu->identity.minor_features5);
Lucas Stach0538aaf2018-01-22 15:56:11 +0100850 seq_printf(m, "\t minor_features6: 0x%08x\n",
851 gpu->identity.minor_features6);
852 seq_printf(m, "\t minor_features7: 0x%08x\n",
853 gpu->identity.minor_features7);
854 seq_printf(m, "\t minor_features8: 0x%08x\n",
855 gpu->identity.minor_features8);
856 seq_printf(m, "\t minor_features9: 0x%08x\n",
857 gpu->identity.minor_features9);
858 seq_printf(m, "\t minor_features10: 0x%08x\n",
859 gpu->identity.minor_features10);
860 seq_printf(m, "\t minor_features11: 0x%08x\n",
861 gpu->identity.minor_features11);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100862
863 seq_puts(m, "\tspecs\n");
864 seq_printf(m, "\t stream_count: %d\n",
865 gpu->identity.stream_count);
866 seq_printf(m, "\t register_max: %d\n",
867 gpu->identity.register_max);
868 seq_printf(m, "\t thread_count: %d\n",
869 gpu->identity.thread_count);
870 seq_printf(m, "\t vertex_cache_size: %d\n",
871 gpu->identity.vertex_cache_size);
872 seq_printf(m, "\t shader_core_count: %d\n",
873 gpu->identity.shader_core_count);
874 seq_printf(m, "\t pixel_pipes: %d\n",
875 gpu->identity.pixel_pipes);
876 seq_printf(m, "\t vertex_output_buffer_size: %d\n",
877 gpu->identity.vertex_output_buffer_size);
878 seq_printf(m, "\t buffer_size: %d\n",
879 gpu->identity.buffer_size);
880 seq_printf(m, "\t instruction_count: %d\n",
881 gpu->identity.instruction_count);
882 seq_printf(m, "\t num_constants: %d\n",
883 gpu->identity.num_constants);
Russell King602eb482016-01-24 17:36:04 +0000884 seq_printf(m, "\t varyings_count: %d\n",
885 gpu->identity.varyings_count);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100886
887 seq_printf(m, "\taxi: 0x%08x\n", axi);
888 seq_printf(m, "\tidle: 0x%08x\n", idle);
889 idle |= ~gpu->idle_mask & ~VIVS_HI_IDLE_STATE_AXI_LP;
890 if ((idle & VIVS_HI_IDLE_STATE_FE) == 0)
891 seq_puts(m, "\t FE is not idle\n");
892 if ((idle & VIVS_HI_IDLE_STATE_DE) == 0)
893 seq_puts(m, "\t DE is not idle\n");
894 if ((idle & VIVS_HI_IDLE_STATE_PE) == 0)
895 seq_puts(m, "\t PE is not idle\n");
896 if ((idle & VIVS_HI_IDLE_STATE_SH) == 0)
897 seq_puts(m, "\t SH is not idle\n");
898 if ((idle & VIVS_HI_IDLE_STATE_PA) == 0)
899 seq_puts(m, "\t PA is not idle\n");
900 if ((idle & VIVS_HI_IDLE_STATE_SE) == 0)
901 seq_puts(m, "\t SE is not idle\n");
902 if ((idle & VIVS_HI_IDLE_STATE_RA) == 0)
903 seq_puts(m, "\t RA is not idle\n");
904 if ((idle & VIVS_HI_IDLE_STATE_TX) == 0)
905 seq_puts(m, "\t TX is not idle\n");
906 if ((idle & VIVS_HI_IDLE_STATE_VG) == 0)
907 seq_puts(m, "\t VG is not idle\n");
908 if ((idle & VIVS_HI_IDLE_STATE_IM) == 0)
909 seq_puts(m, "\t IM is not idle\n");
910 if ((idle & VIVS_HI_IDLE_STATE_FP) == 0)
911 seq_puts(m, "\t FP is not idle\n");
912 if ((idle & VIVS_HI_IDLE_STATE_TS) == 0)
913 seq_puts(m, "\t TS is not idle\n");
914 if (idle & VIVS_HI_IDLE_STATE_AXI_LP)
915 seq_puts(m, "\t AXI low power mode\n");
916
917 if (gpu->identity.features & chipFeatures_DEBUG_MODE) {
918 u32 read0 = gpu_read(gpu, VIVS_MC_DEBUG_READ0);
919 u32 read1 = gpu_read(gpu, VIVS_MC_DEBUG_READ1);
920 u32 write = gpu_read(gpu, VIVS_MC_DEBUG_WRITE);
921
922 seq_puts(m, "\tMC\n");
923 seq_printf(m, "\t read0: 0x%08x\n", read0);
924 seq_printf(m, "\t read1: 0x%08x\n", read1);
925 seq_printf(m, "\t write: 0x%08x\n", write);
926 }
927
928 seq_puts(m, "\tDMA ");
929
930 if (debug.address[0] == debug.address[1] &&
931 debug.state[0] == debug.state[1]) {
932 seq_puts(m, "seems to be stuck\n");
933 } else if (debug.address[0] == debug.address[1]) {
Masanari Iidac01e0152016-04-20 00:27:33 +0900934 seq_puts(m, "address is constant\n");
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100935 } else {
Masanari Iidac01e0152016-04-20 00:27:33 +0900936 seq_puts(m, "is running\n");
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100937 }
938
939 seq_printf(m, "\t address 0: 0x%08x\n", debug.address[0]);
940 seq_printf(m, "\t address 1: 0x%08x\n", debug.address[1]);
941 seq_printf(m, "\t state 0: 0x%08x\n", debug.state[0]);
942 seq_printf(m, "\t state 1: 0x%08x\n", debug.state[1]);
943 seq_printf(m, "\t last fetch 64 bit word: 0x%08x 0x%08x\n",
944 dma_lo, dma_hi);
945
946 ret = 0;
947
948 pm_runtime_mark_last_busy(gpu->dev);
949 pm_runtime_put_autosuspend(gpu->dev);
950
951 return ret;
952}
953#endif
954
Lucas Stach6d7a20c2017-12-06 10:53:27 +0100955void etnaviv_gpu_recover_hang(struct etnaviv_gpu *gpu)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100956{
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100957 unsigned long flags;
Christian Gmeiner355502e2017-09-24 15:15:19 +0200958 unsigned int i = 0;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100959
Lucas Stach6d7a20c2017-12-06 10:53:27 +0100960 dev_err(gpu->dev, "recover hung GPU!\n");
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100961
962 if (pm_runtime_get_sync(gpu->dev) < 0)
963 return;
964
965 mutex_lock(&gpu->lock);
966
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100967 etnaviv_hw_reset(gpu);
968
969 /* complete all events, the GPU won't do it after the reset */
970 spin_lock_irqsave(&gpu->event_spinlock, flags);
Lucas Stach6d7a20c2017-12-06 10:53:27 +0100971 for_each_set_bit_from(i, gpu->event_bitmap, ETNA_NR_EVENTS)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100972 complete(&gpu->event_free);
Christian Gmeiner355502e2017-09-24 15:15:19 +0200973 bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100974 spin_unlock_irqrestore(&gpu->event_spinlock, flags);
975 gpu->completed_fence = gpu->active_fence;
976
977 etnaviv_gpu_hw_init(gpu);
Lucas Stach1b94a9b2016-09-15 12:57:32 +0200978 gpu->lastctx = NULL;
Russell Kingf6086312016-01-21 15:20:19 +0000979 gpu->exec_state = -1;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100980
981 mutex_unlock(&gpu->lock);
982 pm_runtime_mark_last_busy(gpu->dev);
983 pm_runtime_put_autosuspend(gpu->dev);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100984}
985
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100986/* fence object management */
987struct etnaviv_fence {
988 struct etnaviv_gpu *gpu;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100989 struct dma_fence base;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100990};
991
Chris Wilsonf54d1862016-10-25 13:00:45 +0100992static inline struct etnaviv_fence *to_etnaviv_fence(struct dma_fence *fence)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100993{
994 return container_of(fence, struct etnaviv_fence, base);
995}
996
Chris Wilsonf54d1862016-10-25 13:00:45 +0100997static const char *etnaviv_fence_get_driver_name(struct dma_fence *fence)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100998{
999 return "etnaviv";
1000}
1001
Chris Wilsonf54d1862016-10-25 13:00:45 +01001002static const char *etnaviv_fence_get_timeline_name(struct dma_fence *fence)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001003{
1004 struct etnaviv_fence *f = to_etnaviv_fence(fence);
1005
1006 return dev_name(f->gpu->dev);
1007}
1008
Chris Wilsonf54d1862016-10-25 13:00:45 +01001009static bool etnaviv_fence_enable_signaling(struct dma_fence *fence)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001010{
1011 return true;
1012}
1013
Chris Wilsonf54d1862016-10-25 13:00:45 +01001014static bool etnaviv_fence_signaled(struct dma_fence *fence)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001015{
1016 struct etnaviv_fence *f = to_etnaviv_fence(fence);
1017
1018 return fence_completed(f->gpu, f->base.seqno);
1019}
1020
Chris Wilsonf54d1862016-10-25 13:00:45 +01001021static void etnaviv_fence_release(struct dma_fence *fence)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001022{
1023 struct etnaviv_fence *f = to_etnaviv_fence(fence);
1024
1025 kfree_rcu(f, base.rcu);
1026}
1027
Chris Wilsonf54d1862016-10-25 13:00:45 +01001028static const struct dma_fence_ops etnaviv_fence_ops = {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001029 .get_driver_name = etnaviv_fence_get_driver_name,
1030 .get_timeline_name = etnaviv_fence_get_timeline_name,
1031 .enable_signaling = etnaviv_fence_enable_signaling,
1032 .signaled = etnaviv_fence_signaled,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001033 .wait = dma_fence_default_wait,
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001034 .release = etnaviv_fence_release,
1035};
1036
Chris Wilsonf54d1862016-10-25 13:00:45 +01001037static struct dma_fence *etnaviv_gpu_fence_alloc(struct etnaviv_gpu *gpu)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001038{
1039 struct etnaviv_fence *f;
1040
Lucas Stachb27734c22017-03-22 12:23:43 +01001041 /*
1042 * GPU lock must already be held, otherwise fence completion order might
1043 * not match the seqno order assigned here.
1044 */
1045 lockdep_assert_held(&gpu->lock);
1046
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001047 f = kzalloc(sizeof(*f), GFP_KERNEL);
1048 if (!f)
1049 return NULL;
1050
1051 f->gpu = gpu;
1052
Chris Wilsonf54d1862016-10-25 13:00:45 +01001053 dma_fence_init(&f->base, &etnaviv_fence_ops, &gpu->fence_spinlock,
1054 gpu->fence_context, ++gpu->next_fence);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001055
1056 return &f->base;
1057}
1058
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001059/*
1060 * event management:
1061 */
1062
Christian Gmeiner95a428c2017-09-24 15:15:20 +02001063static int event_alloc(struct etnaviv_gpu *gpu, unsigned nr_events,
1064 unsigned int *events)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001065{
Christian Gmeiner95a428c2017-09-24 15:15:20 +02001066 unsigned long flags, timeout = msecs_to_jiffies(10 * 10000);
1067 unsigned i, acquired = 0;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001068
Christian Gmeiner95a428c2017-09-24 15:15:20 +02001069 for (i = 0; i < nr_events; i++) {
1070 unsigned long ret;
1071
1072 ret = wait_for_completion_timeout(&gpu->event_free, timeout);
1073
1074 if (!ret) {
1075 dev_err(gpu->dev, "wait_for_completion_timeout failed");
1076 goto out;
1077 }
1078
1079 acquired++;
1080 timeout = ret;
1081 }
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001082
1083 spin_lock_irqsave(&gpu->event_spinlock, flags);
1084
Christian Gmeiner95a428c2017-09-24 15:15:20 +02001085 for (i = 0; i < nr_events; i++) {
1086 int event = find_first_zero_bit(gpu->event_bitmap, ETNA_NR_EVENTS);
1087
1088 events[i] = event;
Christian Gmeiner547d3402017-09-24 15:15:29 +02001089 memset(&gpu->event[event], 0, sizeof(struct etnaviv_event));
Christian Gmeiner355502e2017-09-24 15:15:19 +02001090 set_bit(event, gpu->event_bitmap);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001091 }
1092
1093 spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1094
Christian Gmeiner95a428c2017-09-24 15:15:20 +02001095 return 0;
1096
1097out:
1098 for (i = 0; i < acquired; i++)
1099 complete(&gpu->event_free);
1100
1101 return -EBUSY;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001102}
1103
1104static void event_free(struct etnaviv_gpu *gpu, unsigned int event)
1105{
1106 unsigned long flags;
1107
1108 spin_lock_irqsave(&gpu->event_spinlock, flags);
1109
Christian Gmeiner355502e2017-09-24 15:15:19 +02001110 if (!test_bit(event, gpu->event_bitmap)) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001111 dev_warn(gpu->dev, "event %u is already marked as free",
1112 event);
1113 spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1114 } else {
Christian Gmeiner355502e2017-09-24 15:15:19 +02001115 clear_bit(event, gpu->event_bitmap);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001116 spin_unlock_irqrestore(&gpu->event_spinlock, flags);
1117
1118 complete(&gpu->event_free);
1119 }
1120}
1121
1122/*
1123 * Cmdstream submission/retirement:
1124 */
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001125int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu,
Lucas Stach8bc4d882017-11-29 14:49:04 +01001126 u32 id, struct timespec *timeout)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001127{
Lucas Stach8bc4d882017-11-29 14:49:04 +01001128 struct dma_fence *fence;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001129 int ret;
1130
Lucas Stach8bc4d882017-11-29 14:49:04 +01001131 /*
Lucas Stache93b6de2017-12-04 18:41:58 +01001132 * Look up the fence and take a reference. We might still find a fence
Lucas Stach8bc4d882017-11-29 14:49:04 +01001133 * whose refcount has already dropped to zero. dma_fence_get_rcu
1134 * pretends we didn't find a fence in that case.
1135 */
Lucas Stache93b6de2017-12-04 18:41:58 +01001136 rcu_read_lock();
Lucas Stach8bc4d882017-11-29 14:49:04 +01001137 fence = idr_find(&gpu->fence_idr, id);
1138 if (fence)
1139 fence = dma_fence_get_rcu(fence);
Lucas Stache93b6de2017-12-04 18:41:58 +01001140 rcu_read_unlock();
Lucas Stach8bc4d882017-11-29 14:49:04 +01001141
1142 if (!fence)
1143 return 0;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001144
1145 if (!timeout) {
1146 /* No timeout was requested: just test for completion */
Lucas Stach8bc4d882017-11-29 14:49:04 +01001147 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001148 } else {
1149 unsigned long remaining = etnaviv_timeout_to_jiffies(timeout);
1150
Lucas Stach8bc4d882017-11-29 14:49:04 +01001151 ret = dma_fence_wait_timeout(fence, true, remaining);
1152 if (ret == 0)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001153 ret = -ETIMEDOUT;
Lucas Stach8bc4d882017-11-29 14:49:04 +01001154 else if (ret != -ERESTARTSYS)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001155 ret = 0;
Lucas Stach8bc4d882017-11-29 14:49:04 +01001156
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001157 }
1158
Lucas Stach8bc4d882017-11-29 14:49:04 +01001159 dma_fence_put(fence);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001160 return ret;
1161}
1162
1163/*
1164 * Wait for an object to become inactive. This, on it's own, is not race
Lucas Stache93b6de2017-12-04 18:41:58 +01001165 * free: the object is moved by the scheduler off the active list, and
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001166 * then the iova is put. Moreover, the object could be re-submitted just
1167 * after we notice that it's become inactive.
1168 *
1169 * Although the retirement happens under the gpu lock, we don't want to hold
1170 * that lock in this function while waiting.
1171 */
1172int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu,
1173 struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout)
1174{
1175 unsigned long remaining;
1176 long ret;
1177
1178 if (!timeout)
1179 return !is_active(etnaviv_obj) ? 0 : -EBUSY;
1180
1181 remaining = etnaviv_timeout_to_jiffies(timeout);
1182
1183 ret = wait_event_interruptible_timeout(gpu->fence_event,
1184 !is_active(etnaviv_obj),
1185 remaining);
Lucas Stachfa67ac82017-11-17 16:35:32 +01001186 if (ret > 0)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001187 return 0;
Lucas Stachfa67ac82017-11-17 16:35:32 +01001188 else if (ret == -ERESTARTSYS)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001189 return -ERESTARTSYS;
Lucas Stachfa67ac82017-11-17 16:35:32 +01001190 else
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001191 return -ETIMEDOUT;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001192}
1193
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001194static void sync_point_perfmon_sample(struct etnaviv_gpu *gpu,
1195 struct etnaviv_event *event, unsigned int flags)
1196{
Lucas Stachef146c002017-11-24 12:02:38 +01001197 const struct etnaviv_gem_submit *submit = event->submit;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001198 unsigned int i;
1199
Lucas Stachef146c002017-11-24 12:02:38 +01001200 for (i = 0; i < submit->nr_pmrs; i++) {
1201 const struct etnaviv_perfmon_request *pmr = submit->pmrs + i;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001202
1203 if (pmr->flags == flags)
Lucas Stach7a9c0fe2017-11-24 15:19:16 +01001204 etnaviv_perfmon_process(gpu, pmr, submit->exec_state);
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001205 }
1206}
1207
1208static void sync_point_perfmon_sample_pre(struct etnaviv_gpu *gpu,
1209 struct etnaviv_event *event)
1210{
Christian Gmeiner2c8b0c52017-09-24 15:15:39 +02001211 u32 val;
1212
1213 /* disable clock gating */
1214 val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
1215 val &= ~VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
1216 gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val);
1217
Christian Gmeiner04a7d182017-09-24 15:15:42 +02001218 /* enable debug register */
1219 val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
1220 val &= ~VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
1221 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val);
1222
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001223 sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_PRE);
1224}
1225
1226static void sync_point_perfmon_sample_post(struct etnaviv_gpu *gpu,
1227 struct etnaviv_event *event)
1228{
Lucas Stachef146c002017-11-24 12:02:38 +01001229 const struct etnaviv_gem_submit *submit = event->submit;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001230 unsigned int i;
Christian Gmeiner2c8b0c52017-09-24 15:15:39 +02001231 u32 val;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001232
1233 sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_POST);
1234
Lucas Stachef146c002017-11-24 12:02:38 +01001235 for (i = 0; i < submit->nr_pmrs; i++) {
1236 const struct etnaviv_perfmon_request *pmr = submit->pmrs + i;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001237
1238 *pmr->bo_vma = pmr->sequence;
1239 }
Christian Gmeiner2c8b0c52017-09-24 15:15:39 +02001240
Christian Gmeiner04a7d182017-09-24 15:15:42 +02001241 /* disable debug register */
1242 val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
1243 val |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS;
1244 gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val);
1245
Christian Gmeiner2c8b0c52017-09-24 15:15:39 +02001246 /* enable clock gating */
1247 val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS);
1248 val |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING;
1249 gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val);
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001250}
1251
1252
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001253/* add bo's to gpu's ring, and kick gpu: */
Lucas Stache93b6de2017-12-04 18:41:58 +01001254struct dma_fence *etnaviv_gpu_submit(struct etnaviv_gem_submit *submit)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001255{
Lucas Stache93b6de2017-12-04 18:41:58 +01001256 struct etnaviv_gpu *gpu = submit->gpu;
1257 struct dma_fence *gpu_fence;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001258 unsigned int i, nr_events = 1, event[3];
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001259 int ret;
1260
Lucas Stach6d7a20c2017-12-06 10:53:27 +01001261 if (!submit->runtime_resumed) {
1262 ret = pm_runtime_get_sync(gpu->dev);
1263 if (ret < 0)
1264 return NULL;
1265 submit->runtime_resumed = true;
1266 }
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001267
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001268 /*
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001269 * if there are performance monitor requests we need to have
1270 * - a sync point to re-configure gpu and process ETNA_PM_PROCESS_PRE
1271 * requests.
1272 * - a sync point to re-configure gpu, process ETNA_PM_PROCESS_POST requests
1273 * and update the sequence number for userspace.
1274 */
Lucas Stachef146c002017-11-24 12:02:38 +01001275 if (submit->nr_pmrs)
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001276 nr_events = 3;
1277
1278 ret = event_alloc(gpu, nr_events, event);
Christian Gmeiner95a428c2017-09-24 15:15:20 +02001279 if (ret) {
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001280 DRM_ERROR("no free events\n");
Lucas Stache93b6de2017-12-04 18:41:58 +01001281 return NULL;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001282 }
1283
Lucas Stachf3cd1b02017-03-22 12:07:23 +01001284 mutex_lock(&gpu->lock);
1285
Lucas Stache93b6de2017-12-04 18:41:58 +01001286 gpu_fence = etnaviv_gpu_fence_alloc(gpu);
1287 if (!gpu_fence) {
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001288 for (i = 0; i < nr_events; i++)
1289 event_free(gpu, event[i]);
1290
Wei Yongjun45abdf32017-04-12 00:31:16 +00001291 goto out_unlock;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001292 }
1293
Lucas Stache93b6de2017-12-04 18:41:58 +01001294 gpu->active_fence = gpu_fence->seqno;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001295
Lucas Stachef146c002017-11-24 12:02:38 +01001296 if (submit->nr_pmrs) {
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001297 gpu->event[event[1]].sync_point = &sync_point_perfmon_sample_pre;
Lucas Stachef146c002017-11-24 12:02:38 +01001298 kref_get(&submit->refcount);
1299 gpu->event[event[1]].submit = submit;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001300 etnaviv_sync_point_queue(gpu, event[1]);
1301 }
1302
Lucas Stache93b6de2017-12-04 18:41:58 +01001303 gpu->event[event[0]].fence = gpu_fence;
Lucas Stach6d7a20c2017-12-06 10:53:27 +01001304 submit->cmdbuf.user_size = submit->cmdbuf.size - 8;
Lucas Stach2f9225d2017-11-24 16:56:37 +01001305 etnaviv_buffer_queue(gpu, submit->exec_state, event[0],
1306 &submit->cmdbuf);
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001307
Lucas Stachef146c002017-11-24 12:02:38 +01001308 if (submit->nr_pmrs) {
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001309 gpu->event[event[2]].sync_point = &sync_point_perfmon_sample_post;
Lucas Stachef146c002017-11-24 12:02:38 +01001310 kref_get(&submit->refcount);
1311 gpu->event[event[2]].submit = submit;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001312 etnaviv_sync_point_queue(gpu, event[2]);
1313 }
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001314
Wei Yongjun45abdf32017-04-12 00:31:16 +00001315out_unlock:
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001316 mutex_unlock(&gpu->lock);
1317
Lucas Stache93b6de2017-12-04 18:41:58 +01001318 return gpu_fence;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001319}
1320
Christian Gmeiner357713c2017-09-24 15:15:28 +02001321static void sync_point_worker(struct work_struct *work)
1322{
1323 struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu,
1324 sync_point_work);
Lucas Stachb9a48aa2017-10-19 13:48:40 +02001325 struct etnaviv_event *event = &gpu->event[gpu->sync_point_event];
1326 u32 addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS);
Christian Gmeiner357713c2017-09-24 15:15:28 +02001327
Lucas Stachb9a48aa2017-10-19 13:48:40 +02001328 event->sync_point(gpu, event);
Lucas Stachef146c002017-11-24 12:02:38 +01001329 etnaviv_submit_put(event->submit);
Christian Gmeiner357713c2017-09-24 15:15:28 +02001330 event_free(gpu, gpu->sync_point_event);
Lucas Stachb9a48aa2017-10-19 13:48:40 +02001331
1332 /* restart FE last to avoid GPU and IRQ racing against this worker */
1333 etnaviv_gpu_start_fe(gpu, addr + 2, 2);
Christian Gmeiner357713c2017-09-24 15:15:28 +02001334}
1335
Lucas Stach4df30002018-01-19 12:22:30 +01001336static void dump_mmu_fault(struct etnaviv_gpu *gpu)
1337{
1338 u32 status = gpu_read(gpu, VIVS_MMUv2_STATUS);
1339 int i;
1340
1341 dev_err_ratelimited(gpu->dev, "MMU fault status 0x%08x\n", status);
1342
1343 for (i = 0; i < 4; i++) {
1344 if (!(status & (VIVS_MMUv2_STATUS_EXCEPTION0__MASK << (i * 4))))
1345 continue;
1346
1347 dev_err_ratelimited(gpu->dev, "MMU %d fault addr 0x%08x\n", i,
1348 gpu_read(gpu, VIVS_MMUv2_EXCEPTION_ADDR(i)));
1349 }
1350}
1351
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001352static irqreturn_t irq_handler(int irq, void *data)
1353{
1354 struct etnaviv_gpu *gpu = data;
1355 irqreturn_t ret = IRQ_NONE;
1356
1357 u32 intr = gpu_read(gpu, VIVS_HI_INTR_ACKNOWLEDGE);
1358
1359 if (intr != 0) {
1360 int event;
1361
1362 pm_runtime_mark_last_busy(gpu->dev);
1363
1364 dev_dbg(gpu->dev, "intr 0x%08x\n", intr);
1365
1366 if (intr & VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR) {
1367 dev_err(gpu->dev, "AXI bus error\n");
1368 intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR;
1369 }
1370
Lucas Stach128a9b12016-08-20 00:14:43 +02001371 if (intr & VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION) {
Lucas Stach4df30002018-01-19 12:22:30 +01001372 dump_mmu_fault(gpu);
Lucas Stach128a9b12016-08-20 00:14:43 +02001373 intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION;
1374 }
1375
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001376 while ((event = ffs(intr)) != 0) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001377 struct dma_fence *fence;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001378
1379 event -= 1;
1380
1381 intr &= ~(1 << event);
1382
1383 dev_dbg(gpu->dev, "event %u\n", event);
1384
Christian Gmeiner357713c2017-09-24 15:15:28 +02001385 if (gpu->event[event].sync_point) {
1386 gpu->sync_point_event = event;
Lucas Stacha7790d72017-11-17 17:43:37 +01001387 queue_work(gpu->wq, &gpu->sync_point_work);
Christian Gmeiner357713c2017-09-24 15:15:28 +02001388 }
1389
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001390 fence = gpu->event[event].fence;
Christian Gmeiner68dc0b22017-09-24 15:15:30 +02001391 if (!fence)
1392 continue;
1393
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001394 gpu->event[event].fence = NULL;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001395
1396 /*
1397 * Events can be processed out of order. Eg,
1398 * - allocate and queue event 0
1399 * - allocate event 1
1400 * - event 0 completes, we process it
1401 * - allocate and queue event 0
1402 * - event 1 and event 0 complete
1403 * we can end up processing event 0 first, then 1.
1404 */
1405 if (fence_after(fence->seqno, gpu->completed_fence))
1406 gpu->completed_fence = fence->seqno;
Lucas Stach8bc4d882017-11-29 14:49:04 +01001407 dma_fence_signal(fence);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001408
1409 event_free(gpu, event);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001410 }
1411
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001412 ret = IRQ_HANDLED;
1413 }
1414
1415 return ret;
1416}
1417
1418static int etnaviv_gpu_clk_enable(struct etnaviv_gpu *gpu)
1419{
1420 int ret;
1421
Lucas Stach65f037e2018-01-19 15:05:40 +01001422 if (gpu->clk_reg) {
1423 ret = clk_prepare_enable(gpu->clk_reg);
1424 if (ret)
1425 return ret;
1426 }
1427
Lucas Stach9c7310c2016-08-22 15:26:19 +02001428 if (gpu->clk_bus) {
1429 ret = clk_prepare_enable(gpu->clk_bus);
1430 if (ret)
1431 return ret;
1432 }
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001433
Lucas Stach9c7310c2016-08-22 15:26:19 +02001434 if (gpu->clk_core) {
1435 ret = clk_prepare_enable(gpu->clk_core);
1436 if (ret)
1437 goto disable_clk_bus;
1438 }
1439
1440 if (gpu->clk_shader) {
1441 ret = clk_prepare_enable(gpu->clk_shader);
1442 if (ret)
1443 goto disable_clk_core;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001444 }
1445
1446 return 0;
Lucas Stach9c7310c2016-08-22 15:26:19 +02001447
1448disable_clk_core:
1449 if (gpu->clk_core)
1450 clk_disable_unprepare(gpu->clk_core);
1451disable_clk_bus:
1452 if (gpu->clk_bus)
1453 clk_disable_unprepare(gpu->clk_bus);
1454
1455 return ret;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001456}
1457
1458static int etnaviv_gpu_clk_disable(struct etnaviv_gpu *gpu)
1459{
Lucas Stach9c7310c2016-08-22 15:26:19 +02001460 if (gpu->clk_shader)
1461 clk_disable_unprepare(gpu->clk_shader);
1462 if (gpu->clk_core)
1463 clk_disable_unprepare(gpu->clk_core);
1464 if (gpu->clk_bus)
1465 clk_disable_unprepare(gpu->clk_bus);
Lucas Stach65f037e2018-01-19 15:05:40 +01001466 if (gpu->clk_reg)
1467 clk_disable_unprepare(gpu->clk_reg);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001468
1469 return 0;
1470}
1471
Lucas Stachb88163e2016-08-17 15:16:57 +02001472int etnaviv_gpu_wait_idle(struct etnaviv_gpu *gpu, unsigned int timeout_ms)
1473{
1474 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1475
1476 do {
1477 u32 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE);
1478
1479 if ((idle & gpu->idle_mask) == gpu->idle_mask)
1480 return 0;
1481
1482 if (time_is_before_jiffies(timeout)) {
1483 dev_warn(gpu->dev,
1484 "timed out waiting for idle: idle=0x%x\n",
1485 idle);
1486 return -ETIMEDOUT;
1487 }
1488
1489 udelay(5);
1490 } while (1);
1491}
1492
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001493static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
1494{
Lucas Stach2f9225d2017-11-24 16:56:37 +01001495 if (gpu->buffer.suballoc) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001496 /* Replace the last WAIT with END */
Lucas Stach40c27bd2017-11-17 17:59:26 +01001497 mutex_lock(&gpu->lock);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001498 etnaviv_buffer_end(gpu);
Lucas Stach40c27bd2017-11-17 17:59:26 +01001499 mutex_unlock(&gpu->lock);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001500
1501 /*
1502 * We know that only the FE is busy here, this should
1503 * happen quickly (as the WAIT is only 200 cycles). If
1504 * we fail, just warn and continue.
1505 */
Lucas Stachb88163e2016-08-17 15:16:57 +02001506 etnaviv_gpu_wait_idle(gpu, 100);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001507 }
1508
1509 return etnaviv_gpu_clk_disable(gpu);
1510}
1511
1512#ifdef CONFIG_PM
1513static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
1514{
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001515 int ret;
1516
1517 ret = mutex_lock_killable(&gpu->lock);
1518 if (ret)
1519 return ret;
1520
Russell Kingbcdfb5e2017-03-12 19:00:59 +00001521 etnaviv_gpu_update_clock(gpu);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001522 etnaviv_gpu_hw_init(gpu);
1523
Lucas Stach4375fff2017-11-17 17:19:50 +01001524 gpu->lastctx = NULL;
Russell Kingf6086312016-01-21 15:20:19 +00001525 gpu->exec_state = -1;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001526
1527 mutex_unlock(&gpu->lock);
1528
1529 return 0;
1530}
1531#endif
1532
Russell Kingbcdfb5e2017-03-12 19:00:59 +00001533static int
1534etnaviv_gpu_cooling_get_max_state(struct thermal_cooling_device *cdev,
1535 unsigned long *state)
1536{
1537 *state = 6;
1538
1539 return 0;
1540}
1541
1542static int
1543etnaviv_gpu_cooling_get_cur_state(struct thermal_cooling_device *cdev,
1544 unsigned long *state)
1545{
1546 struct etnaviv_gpu *gpu = cdev->devdata;
1547
1548 *state = gpu->freq_scale;
1549
1550 return 0;
1551}
1552
1553static int
1554etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
1555 unsigned long state)
1556{
1557 struct etnaviv_gpu *gpu = cdev->devdata;
1558
1559 mutex_lock(&gpu->lock);
1560 gpu->freq_scale = state;
1561 if (!pm_runtime_suspended(gpu->dev))
1562 etnaviv_gpu_update_clock(gpu);
1563 mutex_unlock(&gpu->lock);
1564
1565 return 0;
1566}
1567
1568static struct thermal_cooling_device_ops cooling_ops = {
1569 .get_max_state = etnaviv_gpu_cooling_get_max_state,
1570 .get_cur_state = etnaviv_gpu_cooling_get_cur_state,
1571 .set_cur_state = etnaviv_gpu_cooling_set_cur_state,
1572};
1573
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001574static int etnaviv_gpu_bind(struct device *dev, struct device *master,
1575 void *data)
1576{
1577 struct drm_device *drm = data;
1578 struct etnaviv_drm_private *priv = drm->dev_private;
1579 struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1580 int ret;
1581
Philipp Zabel49b82c32017-12-01 16:00:41 +01001582 if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) {
Lucas Stach5247e2a2017-08-08 15:28:25 +02001583 gpu->cooling = thermal_of_cooling_device_register(dev->of_node,
Russell Kingbcdfb5e2017-03-12 19:00:59 +00001584 (char *)dev_name(dev), gpu, &cooling_ops);
Lucas Stach5247e2a2017-08-08 15:28:25 +02001585 if (IS_ERR(gpu->cooling))
1586 return PTR_ERR(gpu->cooling);
1587 }
Russell Kingbcdfb5e2017-03-12 19:00:59 +00001588
Lucas Stacha7790d72017-11-17 17:43:37 +01001589 gpu->wq = alloc_ordered_workqueue(dev_name(dev), 0);
1590 if (!gpu->wq) {
Lucas Stache93b6de2017-12-04 18:41:58 +01001591 ret = -ENOMEM;
1592 goto out_thermal;
Lucas Stacha7790d72017-11-17 17:43:37 +01001593 }
1594
Lucas Stache93b6de2017-12-04 18:41:58 +01001595 ret = etnaviv_sched_init(gpu);
1596 if (ret)
1597 goto out_workqueue;
1598
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001599#ifdef CONFIG_PM
1600 ret = pm_runtime_get_sync(gpu->dev);
1601#else
1602 ret = etnaviv_gpu_clk_enable(gpu);
1603#endif
Lucas Stache93b6de2017-12-04 18:41:58 +01001604 if (ret < 0)
1605 goto out_sched;
1606
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001607
1608 gpu->drm = drm;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001609 gpu->fence_context = dma_fence_context_alloc(1);
Lucas Stach8bc4d882017-11-29 14:49:04 +01001610 idr_init(&gpu->fence_idr);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001611 spin_lock_init(&gpu->fence_spinlock);
1612
Christian Gmeiner357713c2017-09-24 15:15:28 +02001613 INIT_WORK(&gpu->sync_point_work, sync_point_worker);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001614 init_waitqueue_head(&gpu->fence_event);
1615
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001616 priv->gpu[priv->num_gpus++] = gpu;
1617
1618 pm_runtime_mark_last_busy(gpu->dev);
1619 pm_runtime_put_autosuspend(gpu->dev);
1620
1621 return 0;
Lucas Stache93b6de2017-12-04 18:41:58 +01001622
1623out_sched:
1624 etnaviv_sched_fini(gpu);
1625
1626out_workqueue:
1627 destroy_workqueue(gpu->wq);
1628
1629out_thermal:
1630 if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
1631 thermal_cooling_device_unregister(gpu->cooling);
1632
1633 return ret;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001634}
1635
1636static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
1637 void *data)
1638{
1639 struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1640
1641 DBG("%s", dev_name(gpu->dev));
1642
Lucas Stacha7790d72017-11-17 17:43:37 +01001643 flush_workqueue(gpu->wq);
1644 destroy_workqueue(gpu->wq);
1645
Lucas Stache93b6de2017-12-04 18:41:58 +01001646 etnaviv_sched_fini(gpu);
1647
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001648#ifdef CONFIG_PM
1649 pm_runtime_get_sync(gpu->dev);
1650 pm_runtime_put_sync_suspend(gpu->dev);
1651#else
1652 etnaviv_gpu_hw_suspend(gpu);
1653#endif
1654
Lucas Stach2f9225d2017-11-24 16:56:37 +01001655 if (gpu->buffer.suballoc)
1656 etnaviv_cmdbuf_free(&gpu->buffer);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001657
Lucas Stache66774d2017-01-16 17:29:57 +01001658 if (gpu->cmdbuf_suballoc) {
1659 etnaviv_cmdbuf_suballoc_destroy(gpu->cmdbuf_suballoc);
1660 gpu->cmdbuf_suballoc = NULL;
1661 }
1662
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001663 if (gpu->mmu) {
1664 etnaviv_iommu_destroy(gpu->mmu);
1665 gpu->mmu = NULL;
1666 }
1667
1668 gpu->drm = NULL;
Lucas Stach8bc4d882017-11-29 14:49:04 +01001669 idr_destroy(&gpu->fence_idr);
Russell Kingbcdfb5e2017-03-12 19:00:59 +00001670
Philipp Zabel49b82c32017-12-01 16:00:41 +01001671 if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
1672 thermal_cooling_device_unregister(gpu->cooling);
Russell Kingbcdfb5e2017-03-12 19:00:59 +00001673 gpu->cooling = NULL;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001674}
1675
1676static const struct component_ops gpu_ops = {
1677 .bind = etnaviv_gpu_bind,
1678 .unbind = etnaviv_gpu_unbind,
1679};
1680
1681static const struct of_device_id etnaviv_gpu_match[] = {
1682 {
1683 .compatible = "vivante,gc"
1684 },
1685 { /* sentinel */ }
1686};
Lucas Stach246774d2018-01-24 15:30:29 +01001687MODULE_DEVICE_TABLE(of, etnaviv_gpu_match);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001688
1689static int etnaviv_gpu_platform_probe(struct platform_device *pdev)
1690{
1691 struct device *dev = &pdev->dev;
1692 struct etnaviv_gpu *gpu;
Fabio Estevamdc227892016-08-21 19:32:15 -03001693 int err;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001694
1695 gpu = devm_kzalloc(dev, sizeof(*gpu), GFP_KERNEL);
1696 if (!gpu)
1697 return -ENOMEM;
1698
1699 gpu->dev = &pdev->dev;
1700 mutex_init(&gpu->lock);
Lucas Stache93b6de2017-12-04 18:41:58 +01001701 mutex_init(&gpu->fence_idr_lock);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001702
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001703 /* Map registers: */
1704 gpu->mmio = etnaviv_ioremap(pdev, NULL, dev_name(gpu->dev));
1705 if (IS_ERR(gpu->mmio))
1706 return PTR_ERR(gpu->mmio);
1707
1708 /* Get Interrupt: */
1709 gpu->irq = platform_get_irq(pdev, 0);
1710 if (gpu->irq < 0) {
Fabio Estevamdb60eda2016-08-21 19:32:14 -03001711 dev_err(dev, "failed to get irq: %d\n", gpu->irq);
1712 return gpu->irq;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001713 }
1714
1715 err = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 0,
1716 dev_name(gpu->dev), gpu);
1717 if (err) {
1718 dev_err(dev, "failed to request IRQ%u: %d\n", gpu->irq, err);
Fabio Estevamdb60eda2016-08-21 19:32:14 -03001719 return err;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001720 }
1721
1722 /* Get Clocks: */
Lucas Stach65f037e2018-01-19 15:05:40 +01001723 gpu->clk_reg = devm_clk_get(&pdev->dev, "reg");
1724 DBG("clk_reg: %p", gpu->clk_reg);
1725 if (IS_ERR(gpu->clk_reg))
1726 gpu->clk_reg = NULL;
1727
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001728 gpu->clk_bus = devm_clk_get(&pdev->dev, "bus");
1729 DBG("clk_bus: %p", gpu->clk_bus);
1730 if (IS_ERR(gpu->clk_bus))
1731 gpu->clk_bus = NULL;
1732
1733 gpu->clk_core = devm_clk_get(&pdev->dev, "core");
1734 DBG("clk_core: %p", gpu->clk_core);
1735 if (IS_ERR(gpu->clk_core))
1736 gpu->clk_core = NULL;
Lucas Stachd79fd1ccf22017-04-11 15:54:50 +02001737 gpu->base_rate_core = clk_get_rate(gpu->clk_core);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001738
1739 gpu->clk_shader = devm_clk_get(&pdev->dev, "shader");
1740 DBG("clk_shader: %p", gpu->clk_shader);
1741 if (IS_ERR(gpu->clk_shader))
1742 gpu->clk_shader = NULL;
Lucas Stachd79fd1ccf22017-04-11 15:54:50 +02001743 gpu->base_rate_shader = clk_get_rate(gpu->clk_shader);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001744
1745 /* TODO: figure out max mapped size */
1746 dev_set_drvdata(dev, gpu);
1747
1748 /*
1749 * We treat the device as initially suspended. The runtime PM
1750 * autosuspend delay is rather arbitary: no measurements have
1751 * yet been performed to determine an appropriate value.
1752 */
1753 pm_runtime_use_autosuspend(gpu->dev);
1754 pm_runtime_set_autosuspend_delay(gpu->dev, 200);
1755 pm_runtime_enable(gpu->dev);
1756
1757 err = component_add(&pdev->dev, &gpu_ops);
1758 if (err < 0) {
1759 dev_err(&pdev->dev, "failed to register component: %d\n", err);
Fabio Estevamdb60eda2016-08-21 19:32:14 -03001760 return err;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001761 }
1762
1763 return 0;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001764}
1765
1766static int etnaviv_gpu_platform_remove(struct platform_device *pdev)
1767{
1768 component_del(&pdev->dev, &gpu_ops);
1769 pm_runtime_disable(&pdev->dev);
1770 return 0;
1771}
1772
1773#ifdef CONFIG_PM
1774static int etnaviv_gpu_rpm_suspend(struct device *dev)
1775{
1776 struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1777 u32 idle, mask;
1778
1779 /* If we have outstanding fences, we're not idle */
1780 if (gpu->completed_fence != gpu->active_fence)
1781 return -EBUSY;
1782
1783 /* Check whether the hardware (except FE) is idle */
1784 mask = gpu->idle_mask & ~VIVS_HI_IDLE_STATE_FE;
1785 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE) & mask;
1786 if (idle != mask)
1787 return -EBUSY;
1788
1789 return etnaviv_gpu_hw_suspend(gpu);
1790}
1791
1792static int etnaviv_gpu_rpm_resume(struct device *dev)
1793{
1794 struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
1795 int ret;
1796
1797 ret = etnaviv_gpu_clk_enable(gpu);
1798 if (ret)
1799 return ret;
1800
1801 /* Re-initialise the basic hardware state */
Lucas Stach2f9225d2017-11-24 16:56:37 +01001802 if (gpu->drm && gpu->buffer.suballoc) {
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01001803 ret = etnaviv_gpu_hw_resume(gpu);
1804 if (ret) {
1805 etnaviv_gpu_clk_disable(gpu);
1806 return ret;
1807 }
1808 }
1809
1810 return 0;
1811}
1812#endif
1813
1814static const struct dev_pm_ops etnaviv_gpu_pm_ops = {
1815 SET_RUNTIME_PM_OPS(etnaviv_gpu_rpm_suspend, etnaviv_gpu_rpm_resume,
1816 NULL)
1817};
1818
1819struct platform_driver etnaviv_gpu_driver = {
1820 .driver = {
1821 .name = "etnaviv-gpu",
1822 .owner = THIS_MODULE,
1823 .pm = &etnaviv_gpu_pm_ops,
1824 .of_match_table = etnaviv_gpu_match,
1825 },
1826 .probe = etnaviv_gpu_platform_probe,
1827 .remove = etnaviv_gpu_platform_remove,
1828 .id_table = gpu_ids,
1829};