xref: /linux/drivers/gpu/drm/etnaviv/etnaviv_buffer.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2014-2018 Etnaviv Project
4  */
5 
6 #include <drm/drm_drv.h>
7 #include <drm/drm_print.h>
8 
9 #include "etnaviv_cmdbuf.h"
10 #include "etnaviv_gpu.h"
11 #include "etnaviv_gem.h"
12 #include "etnaviv_mmu.h"
13 #include "etnaviv_buffer.h"
14 
15 #include "common.xml.h"
16 #include "state.xml.h"
17 #include "state_blt.xml.h"
18 #include "state_hi.xml.h"
19 #include "state_3d.xml.h"
20 #include "cmdstream.xml.h"
21 
22 #include "etnaviv_flop_reset.h"
23 
24 static void etnaviv_cmd_select_pipe(struct etnaviv_gpu *gpu,
25 	struct etnaviv_cmdbuf *buffer, u8 pipe)
26 {
27 	u32 flush = 0;
28 
29 	lockdep_assert_held(&gpu->lock);
30 
31 	/*
32 	 * This assumes that if we're switching to 2D, we're switching
33 	 * away from 3D, and vice versa.  Hence, if we're switching to
34 	 * the 2D core, we need to flush the 3D depth and color caches,
35 	 * otherwise we need to flush the 2D pixel engine cache.
36 	 */
37 	if (gpu->exec_state == ETNA_PIPE_2D)
38 		flush = VIVS_GL_FLUSH_CACHE_PE2D;
39 	else if (gpu->exec_state == ETNA_PIPE_3D)
40 		flush = VIVS_GL_FLUSH_CACHE_DEPTH | VIVS_GL_FLUSH_CACHE_COLOR;
41 
42 	CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE, flush);
43 	CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
44 	CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
45 
46 	CMD_LOAD_STATE(buffer, VIVS_GL_PIPE_SELECT,
47 		       VIVS_GL_PIPE_SELECT_PIPE(pipe));
48 }
49 
50 static void etnaviv_buffer_dump(struct etnaviv_gpu *gpu,
51 	struct etnaviv_cmdbuf *buf, u32 off, u32 len)
52 {
53 	u32 size = buf->size;
54 	u32 *ptr = buf->vaddr + off;
55 
56 	dev_info(gpu->dev, "virt %p phys 0x%08x free 0x%08x\n",
57 			ptr, etnaviv_cmdbuf_get_va(buf,
58 			&gpu->mmu_context->cmdbuf_mapping) +
59 			off, size - len * 4 - off);
60 
61 	print_hex_dump(KERN_INFO, "cmd ", DUMP_PREFIX_OFFSET, 16, 4,
62 			ptr, len * 4, 0);
63 }
64 
65 /*
66  * Safely replace the WAIT of a waitlink with a new command and argument.
67  * The GPU may be executing this WAIT while we're modifying it, so we have
68  * to write it in a specific order to avoid the GPU branching to somewhere
69  * else.  'wl_offset' is the offset to the first byte of the WAIT command.
70  */
71 static void etnaviv_buffer_replace_wait(struct etnaviv_cmdbuf *buffer,
72 	unsigned int wl_offset, u32 cmd, u32 arg)
73 {
74 	u32 *lw = buffer->vaddr + wl_offset;
75 
76 	lw[1] = arg;
77 	mb();
78 	lw[0] = cmd;
79 	mb();
80 }
81 
82 /*
83  * Ensure that there is space in the command buffer to contiguously write
84  * 'cmd_dwords' 64-bit words into the buffer, wrapping if necessary.
85  */
86 static u32 etnaviv_buffer_reserve(struct etnaviv_gpu *gpu,
87 	struct etnaviv_cmdbuf *buffer, unsigned int cmd_dwords)
88 {
89 	if (buffer->user_size + cmd_dwords * sizeof(u64) > buffer->size)
90 		buffer->user_size = 0;
91 
92 	return etnaviv_cmdbuf_get_va(buffer,
93 				     &gpu->mmu_context->cmdbuf_mapping) +
94 	       buffer->user_size;
95 }
96 
97 u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu)
98 {
99 	struct etnaviv_cmdbuf *buffer = &gpu->buffer;
100 
101 	lockdep_assert_held(&gpu->lock);
102 
103 	/* initialize buffer */
104 	buffer->user_size = 0;
105 
106 	/* Queue in PPU flop reset */
107 	if (etnaviv_flop_reset_ppu_require(&gpu->identity))
108 		etnaviv_flop_reset_ppu_run(gpu);
109 
110 	CMD_WAIT(buffer, gpu->fe_waitcycles);
111 	CMD_LINK(buffer, 2,
112 		 etnaviv_cmdbuf_get_va(buffer, &gpu->mmu_context->cmdbuf_mapping)
113 		 + buffer->user_size - 4);
114 
115 	return buffer->user_size / 8;
116 }
117 
118 u16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe_addr)
119 {
120 	struct etnaviv_cmdbuf *buffer = &gpu->buffer;
121 
122 	lockdep_assert_held(&gpu->lock);
123 
124 	buffer->user_size = 0;
125 
126 	if (gpu->identity.features & chipFeatures_PIPE_3D) {
127 		CMD_LOAD_STATE(buffer, VIVS_GL_PIPE_SELECT,
128 			       VIVS_GL_PIPE_SELECT_PIPE(ETNA_PIPE_3D));
129 		CMD_LOAD_STATE(buffer, VIVS_MMUv2_CONFIGURATION,
130 			mtlb_addr | VIVS_MMUv2_CONFIGURATION_MODE_MODE4_K);
131 		CMD_LOAD_STATE(buffer, VIVS_MMUv2_SAFE_ADDRESS, safe_addr);
132 		CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
133 		CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
134 	}
135 
136 	if (gpu->identity.features & chipFeatures_PIPE_2D) {
137 		CMD_LOAD_STATE(buffer, VIVS_GL_PIPE_SELECT,
138 			       VIVS_GL_PIPE_SELECT_PIPE(ETNA_PIPE_2D));
139 		CMD_LOAD_STATE(buffer, VIVS_MMUv2_CONFIGURATION,
140 			mtlb_addr | VIVS_MMUv2_CONFIGURATION_MODE_MODE4_K);
141 		CMD_LOAD_STATE(buffer, VIVS_MMUv2_SAFE_ADDRESS, safe_addr);
142 		CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
143 		CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
144 	}
145 
146 	CMD_END(buffer);
147 
148 	buffer->user_size = ALIGN(buffer->user_size, 8);
149 
150 	return buffer->user_size / 8;
151 }
152 
153 u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu, unsigned short id)
154 {
155 	struct etnaviv_cmdbuf *buffer = &gpu->buffer;
156 
157 	lockdep_assert_held(&gpu->lock);
158 
159 	buffer->user_size = 0;
160 
161 	CMD_LOAD_STATE(buffer, VIVS_MMUv2_PTA_CONFIG,
162 		       VIVS_MMUv2_PTA_CONFIG_INDEX(id));
163 
164 	CMD_END(buffer);
165 
166 	buffer->user_size = ALIGN(buffer->user_size, 8);
167 
168 	return buffer->user_size / 8;
169 }
170 
171 static void etnaviv_cmd_flush_ts(struct etnaviv_cmdbuf *buffer, bool has_blt)
172 {
173 	CMD_LOAD_STATE(buffer, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
174 
175 	if (has_blt) {
176 		CMD_LOAD_STATE(buffer, VIVS_BLT_ENABLE, 0x1);
177 		CMD_LOAD_STATE(buffer, VIVS_BLT_SET_COMMAND, 0x1);
178 		CMD_LOAD_STATE(buffer, VIVS_BLT_ENABLE, 0x0);
179 	}
180 }
181 
182 void etnaviv_buffer_end(struct etnaviv_gpu *gpu)
183 {
184 	struct etnaviv_cmdbuf *buffer = &gpu->buffer;
185 	unsigned int waitlink_offset = buffer->user_size - 16;
186 	u32 link_target, flush = 0;
187 	bool has_blt = !!(gpu->identity.minor_features5 &
188 			  chipMinorFeatures5_BLT_ENGINE);
189 
190 	lockdep_assert_held(&gpu->lock);
191 
192 	if (gpu->exec_state == ETNA_PIPE_2D)
193 		flush = VIVS_GL_FLUSH_CACHE_PE2D;
194 	else if (gpu->exec_state == ETNA_PIPE_3D)
195 		flush = VIVS_GL_FLUSH_CACHE_DEPTH |
196 			VIVS_GL_FLUSH_CACHE_COLOR |
197 			VIVS_GL_FLUSH_CACHE_TEXTURE |
198 			VIVS_GL_FLUSH_CACHE_TEXTUREVS |
199 			VIVS_GL_FLUSH_CACHE_SHADER_L2;
200 
201 	if (flush) {
202 		unsigned int dwords = 7;
203 
204 		if (has_blt)
205 			dwords += 11;
206 
207 		link_target = etnaviv_buffer_reserve(gpu, buffer, dwords);
208 
209 		CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
210 		CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
211 		if (has_blt) {
212 			CMD_LOAD_STATE(buffer, VIVS_BLT_ENABLE, 0x1);
213 			CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_BLT);
214 			CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_BLT);
215 			CMD_LOAD_STATE(buffer, VIVS_BLT_ENABLE, 0x0);
216 		}
217 		CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE, flush);
218 		if (gpu->exec_state == ETNA_PIPE_3D)
219 			etnaviv_cmd_flush_ts(buffer, has_blt);
220 		CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
221 		CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
222 		if (has_blt) {
223 			CMD_LOAD_STATE(buffer, VIVS_BLT_ENABLE, 0x1);
224 			CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_BLT);
225 			CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_BLT);
226 			CMD_LOAD_STATE(buffer, VIVS_BLT_ENABLE, 0x0);
227 		}
228 		CMD_END(buffer);
229 
230 		etnaviv_buffer_replace_wait(buffer, waitlink_offset,
231 					    VIV_FE_LINK_HEADER_OP_LINK |
232 					    VIV_FE_LINK_HEADER_PREFETCH(dwords),
233 					    link_target);
234 	} else {
235 		/* Replace the last link-wait with an "END" command */
236 		etnaviv_buffer_replace_wait(buffer, waitlink_offset,
237 					    VIV_FE_END_HEADER_OP_END, 0);
238 	}
239 }
240 
241 /* Append a 'sync point' to the ring buffer. */
242 void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event)
243 {
244 	struct etnaviv_cmdbuf *buffer = &gpu->buffer;
245 	unsigned int waitlink_offset = buffer->user_size - 16;
246 	u32 dwords, target;
247 
248 	lockdep_assert_held(&gpu->lock);
249 
250 	/*
251 	 * We need at most 3 dwords in the return target:
252 	 * 1 event + 1 end + 1 wait + 1 link.
253 	 */
254 	dwords = 4;
255 	target = etnaviv_buffer_reserve(gpu, buffer, dwords);
256 
257 	/* Signal sync point event */
258 	CMD_LOAD_STATE(buffer, VIVS_GL_EVENT, VIVS_GL_EVENT_EVENT_ID(event) |
259 		       VIVS_GL_EVENT_FROM_PE);
260 
261 	/* Stop the FE to 'pause' the GPU */
262 	CMD_END(buffer);
263 
264 	/* Append waitlink */
265 	CMD_WAIT(buffer, gpu->fe_waitcycles);
266 	CMD_LINK(buffer, 2,
267 		 etnaviv_cmdbuf_get_va(buffer, &gpu->mmu_context->cmdbuf_mapping)
268 		 + buffer->user_size - 4);
269 
270 	/*
271 	 * Kick off the 'sync point' command by replacing the previous
272 	 * WAIT with a link to the address in the ring buffer.
273 	 */
274 	etnaviv_buffer_replace_wait(buffer, waitlink_offset,
275 				    VIV_FE_LINK_HEADER_OP_LINK |
276 				    VIV_FE_LINK_HEADER_PREFETCH(dwords),
277 				    target);
278 }
279 
280 /* Append a command buffer to the ring buffer. */
281 void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
282 	struct etnaviv_iommu_context *mmu_context, unsigned int event,
283 	struct etnaviv_cmdbuf *cmdbuf)
284 {
285 	struct etnaviv_cmdbuf *buffer = &gpu->buffer;
286 	unsigned int waitlink_offset = buffer->user_size - 16;
287 	u32 return_target, return_dwords;
288 	u32 link_target, link_dwords;
289 	bool switch_context = gpu->exec_state != exec_state;
290 	bool switch_mmu_context = gpu->mmu_context != mmu_context;
291 	unsigned int new_flush_seq = READ_ONCE(mmu_context->flush_seq);
292 	bool need_flush = switch_mmu_context || gpu->flush_seq != new_flush_seq;
293 	bool has_blt = !!(gpu->identity.minor_features5 &
294 			  chipMinorFeatures5_BLT_ENGINE);
295 
296 	lockdep_assert_held(&gpu->lock);
297 
298 	if (drm_debug_enabled(DRM_UT_DRIVER))
299 		etnaviv_buffer_dump(gpu, buffer, 0, 0x50);
300 
301 	link_target = etnaviv_cmdbuf_get_va(cmdbuf,
302 					    &gpu->mmu_context->cmdbuf_mapping);
303 	link_dwords = cmdbuf->size / 8;
304 
305 	/*
306 	 * If we need maintenance prior to submitting this buffer, we will
307 	 * need to append a mmu flush load state, followed by a new
308 	 * link to this buffer - a total of four additional words.
309 	 */
310 	if (need_flush || switch_context) {
311 		u32 target, extra_dwords;
312 
313 		/* link command */
314 		extra_dwords = 1;
315 
316 		/* flush command */
317 		if (need_flush) {
318 			if (gpu->mmu_context->global->version == ETNAVIV_IOMMU_V1)
319 				extra_dwords += 1;
320 			else
321 				extra_dwords += 3;
322 		}
323 
324 		/* pipe switch commands */
325 		if (switch_context)
326 			extra_dwords += 4;
327 
328 		/* PTA load command */
329 		if (switch_mmu_context && gpu->sec_mode == ETNA_SEC_KERNEL)
330 			extra_dwords += 1;
331 
332 		target = etnaviv_buffer_reserve(gpu, buffer, extra_dwords);
333 		/*
334 		 * Switch MMU context if necessary. Must be done after the
335 		 * link target has been calculated, as the jump forward in the
336 		 * kernel ring still uses the last active MMU context before
337 		 * the switch.
338 		 */
339 		if (switch_mmu_context) {
340 			struct etnaviv_iommu_context *old_context = gpu->mmu_context;
341 
342 			gpu->mmu_context = etnaviv_iommu_context_get(mmu_context);
343 			etnaviv_iommu_context_put(old_context);
344 		}
345 
346 		if (need_flush) {
347 			/* Add the MMU flush */
348 			if (gpu->mmu_context->global->version == ETNAVIV_IOMMU_V1) {
349 				CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_MMU,
350 					       VIVS_GL_FLUSH_MMU_FLUSH_FEMMU |
351 					       VIVS_GL_FLUSH_MMU_FLUSH_UNK1 |
352 					       VIVS_GL_FLUSH_MMU_FLUSH_UNK2 |
353 					       VIVS_GL_FLUSH_MMU_FLUSH_PEMMU |
354 					       VIVS_GL_FLUSH_MMU_FLUSH_UNK4);
355 			} else {
356 				u32 flush = VIVS_MMUv2_CONFIGURATION_MODE_MASK |
357 					    VIVS_MMUv2_CONFIGURATION_FLUSH_FLUSH;
358 
359 				if (switch_mmu_context &&
360 				    gpu->sec_mode == ETNA_SEC_KERNEL) {
361 					unsigned short id =
362 						etnaviv_iommuv2_get_pta_id(gpu->mmu_context);
363 					CMD_LOAD_STATE(buffer,
364 						VIVS_MMUv2_PTA_CONFIG,
365 						VIVS_MMUv2_PTA_CONFIG_INDEX(id));
366 				}
367 
368 				if (gpu->sec_mode == ETNA_SEC_NONE)
369 					flush |= etnaviv_iommuv2_get_mtlb_addr(gpu->mmu_context);
370 
371 				CMD_LOAD_STATE(buffer, VIVS_MMUv2_CONFIGURATION,
372 					       flush);
373 				CMD_SEM(buffer, SYNC_RECIPIENT_FE,
374 					SYNC_RECIPIENT_PE);
375 				CMD_STALL(buffer, SYNC_RECIPIENT_FE,
376 					SYNC_RECIPIENT_PE);
377 			}
378 
379 			gpu->flush_seq = new_flush_seq;
380 		}
381 
382 		if (switch_context) {
383 			etnaviv_cmd_select_pipe(gpu, buffer, exec_state);
384 			gpu->exec_state = exec_state;
385 		}
386 
387 		/* And the link to the submitted buffer */
388 		link_target = etnaviv_cmdbuf_get_va(cmdbuf,
389 					&gpu->mmu_context->cmdbuf_mapping);
390 		CMD_LINK(buffer, link_dwords, link_target);
391 
392 		/* Update the link target to point to above instructions */
393 		link_target = target;
394 		link_dwords = extra_dwords;
395 	}
396 
397 	/*
398 	 * Append a LINK to the submitted command buffer to return to
399 	 * the ring buffer.  return_target is the ring target address.
400 	 * We need at most 7 dwords in the return target: 2 cache flush +
401 	 * 2 semaphore stall + 1 event + 1 wait + 1 link.
402 	 */
403 	return_dwords = 7;
404 
405 	/*
406 	 * When the BLT engine is present we need 7 more dwords in the return
407 	 * target: 3 enable/flush/disable + 4 enable/semaphore stall/disable,
408 	 * on top of the normal TS flush state.
409 	 */
410 	if (has_blt)
411 		return_dwords += 7;
412 
413 	return_target = etnaviv_buffer_reserve(gpu, buffer, return_dwords);
414 	CMD_LINK(cmdbuf, return_dwords, return_target);
415 
416 	/*
417 	 * Append a cache flush, stall, event, wait and link pointing back to
418 	 * the wait command to the ring buffer.
419 	 */
420 	if (gpu->exec_state == ETNA_PIPE_2D) {
421 		CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE,
422 				       VIVS_GL_FLUSH_CACHE_PE2D);
423 	} else {
424 		CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE,
425 				       VIVS_GL_FLUSH_CACHE_DEPTH |
426 				       VIVS_GL_FLUSH_CACHE_COLOR |
427 				       VIVS_GL_FLUSH_CACHE_SHADER_L1);
428 		etnaviv_cmd_flush_ts(buffer, has_blt);
429 	}
430 	CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
431 	CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
432 
433 	if (has_blt) {
434 		CMD_LOAD_STATE(buffer, VIVS_BLT_ENABLE, 0x1);
435 		CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_BLT);
436 		CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_BLT);
437 		CMD_LOAD_STATE(buffer, VIVS_BLT_ENABLE, 0x0);
438 	}
439 
440 	CMD_LOAD_STATE(buffer, VIVS_GL_EVENT, VIVS_GL_EVENT_EVENT_ID(event) |
441 		       VIVS_GL_EVENT_FROM_PE);
442 	CMD_WAIT(buffer, gpu->fe_waitcycles);
443 	CMD_LINK(buffer, 2,
444 		 etnaviv_cmdbuf_get_va(buffer, &gpu->mmu_context->cmdbuf_mapping)
445 		 + buffer->user_size - 4);
446 
447 	if (drm_debug_enabled(DRM_UT_DRIVER))
448 		pr_info("stream link to 0x%08x @ 0x%08x %p\n",
449 			return_target,
450 			etnaviv_cmdbuf_get_va(cmdbuf, &gpu->mmu_context->cmdbuf_mapping),
451 			cmdbuf->vaddr);
452 
453 	if (drm_debug_enabled(DRM_UT_DRIVER)) {
454 		print_hex_dump(KERN_INFO, "cmd ", DUMP_PREFIX_OFFSET, 16, 4,
455 			       cmdbuf->vaddr, cmdbuf->size, 0);
456 
457 		pr_info("link op: %p\n", buffer->vaddr + waitlink_offset);
458 		pr_info("addr: 0x%08x\n", link_target);
459 		pr_info("back: 0x%08x\n", return_target);
460 		pr_info("event: %d\n", event);
461 	}
462 
463 	/*
464 	 * Kick off the submitted command by replacing the previous
465 	 * WAIT with a link to the address in the ring buffer.
466 	 */
467 	etnaviv_buffer_replace_wait(buffer, waitlink_offset,
468 				    VIV_FE_LINK_HEADER_OP_LINK |
469 				    VIV_FE_LINK_HEADER_PREFETCH(link_dwords),
470 				    link_target);
471 
472 	if (drm_debug_enabled(DRM_UT_DRIVER))
473 		etnaviv_buffer_dump(gpu, buffer, 0, 0x50);
474 }
475