1 #include <assert.h>
2 #include <stdlib.h>
3 #include <sys/ioctl.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <fcntl.h>
8 #include <inttypes.h>
9 #include <errno.h>
10 #include <sys/stat.h>
11 #include <sys/time.h>
12 
13 #include <drm.h>
14 #include <i915_drm.h>
15 
16 #include "drmtest.h"
17 #include "intel_bufmgr.h"
18 #include "intel_batchbuffer.h"
19 #include "intel_chipset.h"
20 #include "intel_io.h"
21 #include "rendercopy.h"
22 #include "gen8_render.h"
23 #include "intel_reg.h"
24 #include "igt_aux.h"
25 
26 #include "intel_aub.h"
27 
28 #define VERTEX_SIZE (3*4)
29 
30 #if DEBUG_RENDERCPY
dump_batch(struct intel_batchbuffer * batch)31 static void dump_batch(struct intel_batchbuffer *batch) {
32 	int fd = open("/tmp/i965-batchbuffers.dump", O_WRONLY | O_CREAT,  0666);
33 	if (fd != -1) {
34 		igt_assert_eq(write(fd, batch->buffer, 4096), 4096);
35 		fd = close(fd);
36 	}
37 }
38 #else
39 #define dump_batch(x) do { } while(0)
40 #endif
41 
42 struct {
43 	uint32_t cc_state;
44 	uint32_t blend_state;
45 } cc;
46 
47 struct {
48 	uint32_t cc_state;
49 	uint32_t sf_clip_state;
50 } viewport;
51 
52 /* see lib/i915/shaders/ps/blit.g7a */
53 static const uint32_t ps_kernel[][4] = {
54 #if 1
55    { 0x0080005a, 0x2f403ae8, 0x3a0000c0, 0x008d0040 },
56    { 0x0080005a, 0x2f803ae8, 0x3a0000d0, 0x008d0040 },
57    { 0x02800031, 0x2e203a48, 0x0e8d0f40, 0x08840001 },
58    { 0x05800031, 0x20003a40, 0x0e8d0e20, 0x90031000 },
59 #else
60    /* Write all -1 */
61    { 0x00600001, 0x2e000608, 0x00000000, 0x3f800000 },
62    { 0x00600001, 0x2e200608, 0x00000000, 0x3f800000 },
63    { 0x00600001, 0x2e400608, 0x00000000, 0x3f800000 },
64    { 0x00600001, 0x2e600608, 0x00000000, 0x3f800000 },
65    { 0x00600001, 0x2e800608, 0x00000000, 0x3f800000 },
66    { 0x00600001, 0x2ea00608, 0x00000000, 0x3f800000 },
67    { 0x00600001, 0x2ec00608, 0x00000000, 0x3f800000 },
68    { 0x00600001, 0x2ee00608, 0x00000000, 0x3f800000 },
69    { 0x05800031, 0x200022e0, 0x0e000e00, 0x90031000 },
70 #endif
71 };
72 
73 /* AUB annotation support */
74 #define MAX_ANNOTATIONS	33
75 struct annotations_context {
76 	drm_intel_aub_annotation annotations[MAX_ANNOTATIONS];
77 	int index;
78 	uint32_t offset;
79 };
80 
annotation_init(struct annotations_context * aub)81 static void annotation_init(struct annotations_context *aub)
82 {
83 	/* aub->annotations is an array keeping a list of annotations of the
84 	 * batch buffer ordered by offset. aub->annotations[0] is thus left
85 	 * for the command stream and will be filled just before executing
86 	 * the batch buffer with annotations_add_batch() */
87 	aub->index = 1;
88 }
89 
add_annotation(drm_intel_aub_annotation * a,uint32_t type,uint32_t subtype,uint32_t ending_offset)90 static void add_annotation(drm_intel_aub_annotation *a,
91 			   uint32_t type, uint32_t subtype,
92 			   uint32_t ending_offset)
93 {
94 	a->type = type;
95 	a->subtype = subtype;
96 	a->ending_offset = ending_offset;
97 }
98 
annotation_add_batch(struct annotations_context * aub,size_t size)99 static void annotation_add_batch(struct annotations_context *aub, size_t size)
100 {
101 	add_annotation(&aub->annotations[0], AUB_TRACE_TYPE_BATCH, 0, size);
102 }
103 
annotation_add_state(struct annotations_context * aub,uint32_t state_type,uint32_t start_offset,size_t size)104 static void annotation_add_state(struct annotations_context *aub,
105 				 uint32_t state_type,
106 				 uint32_t start_offset,
107 				 size_t   size)
108 {
109 	igt_assert(aub->index < MAX_ANNOTATIONS);
110 
111 	add_annotation(&aub->annotations[aub->index++],
112 		       AUB_TRACE_TYPE_NOTYPE, 0,
113 		       start_offset);
114 	add_annotation(&aub->annotations[aub->index++],
115 		       AUB_TRACE_TYPE(state_type),
116 		       AUB_TRACE_SUBTYPE(state_type),
117 		       start_offset + size);
118 }
119 
annotation_flush(struct annotations_context * aub,struct intel_batchbuffer * batch)120 static void annotation_flush(struct annotations_context *aub,
121 			     struct intel_batchbuffer *batch)
122 {
123 	if (!igt_aub_dump_enabled())
124 		return;
125 
126 	drm_intel_bufmgr_gem_set_aub_annotations(batch->bo,
127 						 aub->annotations,
128 						 aub->index);
129 }
130 
131 static void
gen6_render_flush(struct intel_batchbuffer * batch,drm_intel_context * context,uint32_t batch_end)132 gen6_render_flush(struct intel_batchbuffer *batch,
133 		  drm_intel_context *context, uint32_t batch_end)
134 {
135 	int ret;
136 
137 	ret = drm_intel_bo_subdata(batch->bo, 0, 4096, batch->buffer);
138 	if (ret == 0)
139 		ret = drm_intel_gem_bo_context_exec(batch->bo, context,
140 						    batch_end, 0);
141 	igt_assert(ret == 0);
142 }
143 
144 /* Mostly copy+paste from gen6, except height, width, pitch moved */
145 static uint32_t
gen8_bind_buf(struct intel_batchbuffer * batch,struct annotations_context * aub,const struct igt_buf * buf,int is_dst)146 gen8_bind_buf(struct intel_batchbuffer *batch,
147 	      struct annotations_context *aub,
148 	      const struct igt_buf *buf, int is_dst)
149 {
150 	struct gen8_surface_state *ss;
151 	uint32_t write_domain, read_domain, offset;
152 	int ret;
153 
154 	igt_assert_lte(buf->stride, 256*1024);
155 	igt_assert_lte(igt_buf_width(buf), 16384);
156 	igt_assert_lte(igt_buf_height(buf), 16384);
157 
158 	if (is_dst) {
159 		write_domain = read_domain = I915_GEM_DOMAIN_RENDER;
160 	} else {
161 		write_domain = 0;
162 		read_domain = I915_GEM_DOMAIN_SAMPLER;
163 	}
164 
165 	ss = intel_batchbuffer_subdata_alloc(batch, sizeof(*ss), 64);
166 	offset = intel_batchbuffer_subdata_offset(batch, ss);
167 	annotation_add_state(aub, AUB_TRACE_SURFACE_STATE, offset, sizeof(*ss));
168 
169 	ss->ss0.surface_type = SURFACE_2D;
170 	switch (buf->bpp) {
171 		case 8: ss->ss0.surface_format = SURFACEFORMAT_R8_UNORM; break;
172 		case 16: ss->ss0.surface_format = SURFACEFORMAT_R8G8_UNORM; break;
173 		case 32: ss->ss0.surface_format = SURFACEFORMAT_B8G8R8A8_UNORM; break;
174 		case 64: ss->ss0.surface_format = SURFACEFORMAT_R16G16B16A16_FLOAT; break;
175 		default: igt_assert(0);
176 	}
177 	ss->ss0.render_cache_read_write = 1;
178 	ss->ss0.vertical_alignment = 1; /* align 4 */
179 	ss->ss0.horizontal_alignment = 1; /* align 4 */
180 	if (buf->tiling == I915_TILING_X)
181 		ss->ss0.tiled_mode = 2;
182 	else if (buf->tiling == I915_TILING_Y)
183 		ss->ss0.tiled_mode = 3;
184 
185 	if (IS_CHERRYVIEW(batch->devid))
186 		ss->ss1.memory_object_control = CHV_MOCS_WB | CHV_MOCS_L3;
187 	else
188 		ss->ss1.memory_object_control = BDW_MOCS_PTE |
189 			BDW_MOCS_TC_L3_PTE | BDW_MOCS_AGE(0);
190 
191 	ss->ss8.base_addr = buf->bo->offset64;
192 	ss->ss9.base_addr_hi = buf->bo->offset64 >> 32;
193 
194 	ret = drm_intel_bo_emit_reloc(batch->bo,
195 				      intel_batchbuffer_subdata_offset(batch, &ss->ss8),
196 				      buf->bo, 0,
197 				      read_domain, write_domain);
198 	igt_assert(ret == 0);
199 
200 	ss->ss2.height = igt_buf_height(buf) - 1;
201 	ss->ss2.width  = igt_buf_width(buf) - 1;
202 	ss->ss3.pitch  = buf->stride - 1;
203 
204 	ss->ss7.shader_chanel_select_r = 4;
205 	ss->ss7.shader_chanel_select_g = 5;
206 	ss->ss7.shader_chanel_select_b = 6;
207 	ss->ss7.shader_chanel_select_a = 7;
208 
209 	return offset;
210 }
211 
212 static uint32_t
gen8_bind_surfaces(struct intel_batchbuffer * batch,struct annotations_context * aub,const struct igt_buf * src,const struct igt_buf * dst)213 gen8_bind_surfaces(struct intel_batchbuffer *batch,
214 		   struct annotations_context *aub,
215 		   const struct igt_buf *src,
216 		   const struct igt_buf *dst)
217 {
218 	uint32_t *binding_table, offset;
219 
220 	binding_table = intel_batchbuffer_subdata_alloc(batch, 8, 32);
221 	offset = intel_batchbuffer_subdata_offset(batch, binding_table);
222 	annotation_add_state(aub, AUB_TRACE_BINDING_TABLE, offset, 8);
223 
224 	binding_table[0] = gen8_bind_buf(batch, aub, dst, 1);
225 	binding_table[1] = gen8_bind_buf(batch, aub, src, 0);
226 
227 	return offset;
228 }
229 
230 /* Mostly copy+paste from gen6, except wrap modes moved */
231 static uint32_t
gen8_create_sampler(struct intel_batchbuffer * batch,struct annotations_context * aub)232 gen8_create_sampler(struct intel_batchbuffer *batch,
233 		    struct annotations_context *aub)
234 {
235 	struct gen8_sampler_state *ss;
236 	uint32_t offset;
237 
238 	ss = intel_batchbuffer_subdata_alloc(batch, sizeof(*ss), 64);
239 	offset = intel_batchbuffer_subdata_offset(batch, ss);
240 	annotation_add_state(aub, AUB_TRACE_SAMPLER_STATE,
241 			     offset, sizeof(*ss));
242 
243 	ss->ss0.min_filter = GEN4_MAPFILTER_NEAREST;
244 	ss->ss0.mag_filter = GEN4_MAPFILTER_NEAREST;
245 	ss->ss3.r_wrap_mode = GEN4_TEXCOORDMODE_CLAMP;
246 	ss->ss3.s_wrap_mode = GEN4_TEXCOORDMODE_CLAMP;
247 	ss->ss3.t_wrap_mode = GEN4_TEXCOORDMODE_CLAMP;
248 
249 	/* I've experimented with non-normalized coordinates and using the LD
250 	 * sampler fetch, but couldn't make it work. */
251 	ss->ss3.non_normalized_coord = 0;
252 
253 	return offset;
254 }
255 
256 static uint32_t
gen8_fill_ps(struct intel_batchbuffer * batch,struct annotations_context * aub,const uint32_t kernel[][4],size_t size)257 gen8_fill_ps(struct intel_batchbuffer *batch,
258 	     struct annotations_context *aub,
259 	     const uint32_t kernel[][4],
260 	     size_t size)
261 {
262 	uint32_t offset;
263 
264 	offset = intel_batchbuffer_copy_data(batch, kernel, size, 64);
265 	annotation_add_state(aub, AUB_TRACE_KERNEL_INSTRUCTIONS, offset, size);
266 
267 	return offset;
268 }
269 
270 /*
271  * gen7_fill_vertex_buffer_data populate vertex buffer with data.
272  *
273  * The vertex buffer consists of 3 vertices to construct a RECTLIST. The 4th
274  * vertex is implied (automatically derived by the HW). Each element has the
275  * destination offset, and the normalized texture offset (src). The rectangle
276  * itself will span the entire subsurface to be copied.
277  *
278  * see gen6_emit_vertex_elements
279  */
280 static uint32_t
gen7_fill_vertex_buffer_data(struct intel_batchbuffer * batch,struct annotations_context * aub,const struct igt_buf * src,uint32_t src_x,uint32_t src_y,uint32_t dst_x,uint32_t dst_y,uint32_t width,uint32_t height)281 gen7_fill_vertex_buffer_data(struct intel_batchbuffer *batch,
282 			     struct annotations_context *aub,
283 			     const struct igt_buf *src,
284 			     uint32_t src_x, uint32_t src_y,
285 			     uint32_t dst_x, uint32_t dst_y,
286 			     uint32_t width, uint32_t height)
287 {
288 	void *start;
289 	uint32_t offset;
290 
291 	intel_batchbuffer_align(batch, 8);
292 	start = batch->ptr;
293 
294 	emit_vertex_2s(batch, dst_x + width, dst_y + height);
295 	emit_vertex_normalized(batch, src_x + width, igt_buf_width(src));
296 	emit_vertex_normalized(batch, src_y + height, igt_buf_height(src));
297 
298 	emit_vertex_2s(batch, dst_x, dst_y + height);
299 	emit_vertex_normalized(batch, src_x, igt_buf_width(src));
300 	emit_vertex_normalized(batch, src_y + height, igt_buf_height(src));
301 
302 	emit_vertex_2s(batch, dst_x, dst_y);
303 	emit_vertex_normalized(batch, src_x, igt_buf_width(src));
304 	emit_vertex_normalized(batch, src_y, igt_buf_height(src));
305 
306 	offset = intel_batchbuffer_subdata_offset(batch, start);
307 	annotation_add_state(aub, AUB_TRACE_VERTEX_BUFFER,
308 			     offset, 3 * VERTEX_SIZE);
309 	return offset;
310 }
311 
312 /*
313  * gen6_emit_vertex_elements - The vertex elements describe the contents of the
314  * vertex buffer. We pack the vertex buffer in a semi weird way, conforming to
315  * what gen6_rendercopy did. The most straightforward would be to store
316  * everything as floats.
317  *
318  * see gen7_fill_vertex_buffer_data() for where the corresponding elements are
319  * packed.
320  */
321 static void
gen6_emit_vertex_elements(struct intel_batchbuffer * batch)322 gen6_emit_vertex_elements(struct intel_batchbuffer *batch) {
323 	/*
324 	 * The VUE layout
325 	 *    dword 0-3: pad (0, 0, 0. 0)
326 	 *    dword 4-7: position (x, y, 0, 1.0),
327 	 *    dword 8-11: texture coordinate 0 (u0, v0, 0, 1.0)
328 	 */
329 	OUT_BATCH(GEN4_3DSTATE_VERTEX_ELEMENTS | (3 * 2 + 1 - 2));
330 
331 	/* Element state 0. These are 4 dwords of 0 required for the VUE format.
332 	 * We don't really know or care what they do.
333 	 */
334 	OUT_BATCH(0 << GEN6_VE0_VERTEX_BUFFER_INDEX_SHIFT | GEN6_VE0_VALID |
335 		  SURFACEFORMAT_R32G32B32A32_FLOAT << VE0_FORMAT_SHIFT |
336 		  0 << VE0_OFFSET_SHIFT); /* we specify 0, but it's really does not exist */
337 	OUT_BATCH(GEN4_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_0_SHIFT |
338 		  GEN4_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT |
339 		  GEN4_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
340 		  GEN4_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_3_SHIFT);
341 
342 	/* Element state 1 - Our "destination" vertices. These are passed down
343 	 * through the pipeline, and eventually make it to the pixel shader as
344 	 * the offsets in the destination surface. It's packed as the 16
345 	 * signed/scaled because of gen6 rendercopy. I see no particular reason
346 	 * for doing this though.
347 	 */
348 	OUT_BATCH(0 << GEN6_VE0_VERTEX_BUFFER_INDEX_SHIFT | GEN6_VE0_VALID |
349 		  SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT |
350 		  0 << VE0_OFFSET_SHIFT); /* offsets vb in bytes */
351 	OUT_BATCH(GEN4_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
352 		  GEN4_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
353 		  GEN4_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
354 		  GEN4_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT);
355 
356 	/* Element state 2. Last but not least we store the U,V components as
357 	 * normalized floats. These will be used in the pixel shader to sample
358 	 * from the source buffer.
359 	 */
360 	OUT_BATCH(0 << GEN6_VE0_VERTEX_BUFFER_INDEX_SHIFT | GEN6_VE0_VALID |
361 		  SURFACEFORMAT_R32G32_FLOAT << VE0_FORMAT_SHIFT |
362 		  4 << VE0_OFFSET_SHIFT);	/* offset vb in bytes */
363 	OUT_BATCH(GEN4_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
364 		  GEN4_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
365 		  GEN4_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
366 		  GEN4_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT);
367 }
368 
369 /*
370  * gen8_emit_vertex_buffer emit the vertex buffers command
371  *
372  * @batch
373  * @offset - bytw offset within the @batch where the vertex buffer starts.
374  */
gen8_emit_vertex_buffer(struct intel_batchbuffer * batch,uint32_t offset)375 static void gen8_emit_vertex_buffer(struct intel_batchbuffer *batch,
376 				    uint32_t offset) {
377 	OUT_BATCH(GEN4_3DSTATE_VERTEX_BUFFERS | (1 + (4 * 1) - 2));
378 	OUT_BATCH(0 << GEN6_VB0_BUFFER_INDEX_SHIFT | /* VB 0th index */
379 		  GEN8_VB0_BUFFER_ADDR_MOD_EN | /* Address Modify Enable */
380 		  VERTEX_SIZE << VB0_BUFFER_PITCH_SHIFT);
381 	OUT_RELOC(batch->bo, I915_GEM_DOMAIN_VERTEX, 0, offset);
382 	OUT_BATCH(3 * VERTEX_SIZE);
383 }
384 
385 static uint32_t
gen6_create_cc_state(struct intel_batchbuffer * batch,struct annotations_context * aub)386 gen6_create_cc_state(struct intel_batchbuffer *batch,
387 		     struct annotations_context *aub)
388 {
389 	struct gen6_color_calc_state *cc_state;
390 	uint32_t offset;
391 
392 	cc_state = intel_batchbuffer_subdata_alloc(batch,
393 						   sizeof(*cc_state), 64);
394 	offset = intel_batchbuffer_subdata_offset(batch, cc_state);
395 	annotation_add_state(aub, AUB_TRACE_CC_STATE,
396 			     offset, sizeof(*cc_state));
397 
398 	return offset;
399 }
400 
401 static uint32_t
gen8_create_blend_state(struct intel_batchbuffer * batch,struct annotations_context * aub)402 gen8_create_blend_state(struct intel_batchbuffer *batch,
403 			struct annotations_context *aub)
404 {
405 	struct gen8_blend_state *blend;
406 	int i;
407 	uint32_t offset;
408 
409 	blend = intel_batchbuffer_subdata_alloc(batch, sizeof(*blend), 64);
410 	offset = intel_batchbuffer_subdata_offset(batch, blend);
411 	annotation_add_state(aub, AUB_TRACE_BLEND_STATE,
412 			     offset, sizeof(*blend));
413 
414 	for (i = 0; i < 16; i++) {
415 		blend->bs[i].dest_blend_factor = GEN6_BLENDFACTOR_ZERO;
416 		blend->bs[i].source_blend_factor = GEN6_BLENDFACTOR_ONE;
417 		blend->bs[i].color_blend_func = GEN6_BLENDFUNCTION_ADD;
418 		blend->bs[i].pre_blend_color_clamp = 1;
419 		blend->bs[i].color_buffer_blend = 0;
420 	}
421 
422 	return offset;
423 }
424 
425 static uint32_t
gen6_create_cc_viewport(struct intel_batchbuffer * batch,struct annotations_context * aub)426 gen6_create_cc_viewport(struct intel_batchbuffer *batch,
427 			struct annotations_context *aub)
428 {
429 	struct gen4_cc_viewport *vp;
430 	uint32_t offset;
431 
432 	vp = intel_batchbuffer_subdata_alloc(batch, sizeof(*vp), 32);
433 	offset = intel_batchbuffer_subdata_offset(batch, vp);
434 	annotation_add_state(aub, AUB_TRACE_CC_VP_STATE,
435 			     offset, sizeof(*vp));
436 
437 	/* XXX I don't understand this */
438 	vp->min_depth = -1.e35;
439 	vp->max_depth = 1.e35;
440 
441 	return offset;
442 }
443 
444 static uint32_t
gen7_create_sf_clip_viewport(struct intel_batchbuffer * batch,struct annotations_context * aub)445 gen7_create_sf_clip_viewport(struct intel_batchbuffer *batch,
446 			struct annotations_context *aub)
447 {
448 	/* XXX these are likely not needed */
449 	struct gen7_sf_clip_viewport *scv_state;
450 	uint32_t offset;
451 
452 	scv_state = intel_batchbuffer_subdata_alloc(batch,
453 						    sizeof(*scv_state), 64);
454 	offset = intel_batchbuffer_subdata_offset(batch, scv_state);
455 	annotation_add_state(aub, AUB_TRACE_CLIP_VP_STATE,
456 			     offset, sizeof(*scv_state));
457 
458 	scv_state->guardband.xmin = 0;
459 	scv_state->guardband.xmax = 1.0f;
460 	scv_state->guardband.ymin = 0;
461 	scv_state->guardband.ymax = 1.0f;
462 
463 	return offset;
464 }
465 
466 static uint32_t
gen6_create_scissor_rect(struct intel_batchbuffer * batch,struct annotations_context * aub)467 gen6_create_scissor_rect(struct intel_batchbuffer *batch,
468 			struct annotations_context *aub)
469 {
470 	struct gen6_scissor_rect *scissor;
471 	uint32_t offset;
472 
473 	scissor = intel_batchbuffer_subdata_alloc(batch, sizeof(*scissor), 64);
474 	offset = intel_batchbuffer_subdata_offset(batch, scissor);
475 	annotation_add_state(aub, AUB_TRACE_SCISSOR_STATE,
476 			     offset, sizeof(*scissor));
477 
478 	return offset;
479 }
480 
481 static void
gen8_emit_sip(struct intel_batchbuffer * batch)482 gen8_emit_sip(struct intel_batchbuffer *batch) {
483 	OUT_BATCH(GEN4_STATE_SIP | (3 - 2));
484 	OUT_BATCH(0);
485 	OUT_BATCH(0);
486 }
487 
488 static void
gen7_emit_push_constants(struct intel_batchbuffer * batch)489 gen7_emit_push_constants(struct intel_batchbuffer *batch) {
490 	OUT_BATCH(GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_VS);
491 	OUT_BATCH(0);
492 	OUT_BATCH(GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_HS);
493 	OUT_BATCH(0);
494 	OUT_BATCH(GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_DS);
495 	OUT_BATCH(0);
496 	OUT_BATCH(GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS);
497 	OUT_BATCH(0);
498 	OUT_BATCH(GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_PS);
499 	OUT_BATCH(0);
500 }
501 
502 static void
gen8_emit_state_base_address(struct intel_batchbuffer * batch)503 gen8_emit_state_base_address(struct intel_batchbuffer *batch) {
504 	OUT_BATCH(GEN4_STATE_BASE_ADDRESS | (16 - 2));
505 
506 	/* general */
507 	OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
508 	OUT_BATCH(0);
509 
510 	/* stateless data port */
511 	OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
512 
513 	/* surface */
514 	OUT_RELOC(batch->bo, I915_GEM_DOMAIN_SAMPLER, 0, BASE_ADDRESS_MODIFY);
515 
516 	/* dynamic */
517 	OUT_RELOC(batch->bo, I915_GEM_DOMAIN_RENDER | I915_GEM_DOMAIN_INSTRUCTION,
518 		  0, BASE_ADDRESS_MODIFY);
519 
520 	/* indirect */
521 	OUT_BATCH(0);
522 	OUT_BATCH(0);
523 
524 	/* instruction */
525 	OUT_RELOC(batch->bo, I915_GEM_DOMAIN_INSTRUCTION, 0, BASE_ADDRESS_MODIFY);
526 
527 	/* general state buffer size */
528 	OUT_BATCH(0xfffff000 | 1);
529 	/* dynamic state buffer size */
530 	OUT_BATCH(1 << 12 | 1);
531 	/* indirect object buffer size */
532 	OUT_BATCH(0xfffff000 | 1);
533 	/* intruction buffer size */
534 	OUT_BATCH(1 << 12 | 1);
535 }
536 
537 static void
gen7_emit_urb(struct intel_batchbuffer * batch)538 gen7_emit_urb(struct intel_batchbuffer *batch) {
539 	/* XXX: Min valid values from mesa */
540 	const int vs_entries = 64;
541 	const int vs_size = 2;
542 	const int vs_start = 2;
543 
544 	OUT_BATCH(GEN7_3DSTATE_URB_VS);
545 	OUT_BATCH(vs_entries | ((vs_size - 1) << 16) | (vs_start << 25));
546 	OUT_BATCH(GEN7_3DSTATE_URB_GS);
547 	OUT_BATCH(vs_start << 25);
548 	OUT_BATCH(GEN7_3DSTATE_URB_HS);
549 	OUT_BATCH(vs_start << 25);
550 	OUT_BATCH(GEN7_3DSTATE_URB_DS);
551 	OUT_BATCH(vs_start << 25);
552 }
553 
554 static void
gen8_emit_cc(struct intel_batchbuffer * batch)555 gen8_emit_cc(struct intel_batchbuffer *batch) {
556 	OUT_BATCH(GEN7_3DSTATE_BLEND_STATE_POINTERS);
557 	OUT_BATCH(cc.blend_state | 1);
558 
559 	OUT_BATCH(GEN6_3DSTATE_CC_STATE_POINTERS);
560 	OUT_BATCH(cc.cc_state | 1);
561 }
562 
563 static void
gen8_emit_multisample(struct intel_batchbuffer * batch)564 gen8_emit_multisample(struct intel_batchbuffer *batch) {
565 	OUT_BATCH(GEN8_3DSTATE_MULTISAMPLE);
566 	OUT_BATCH(0);
567 
568 	OUT_BATCH(GEN6_3DSTATE_SAMPLE_MASK);
569 	OUT_BATCH(1);
570 }
571 
572 static void
gen8_emit_vs(struct intel_batchbuffer * batch)573 gen8_emit_vs(struct intel_batchbuffer *batch) {
574 	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_VS);
575 	OUT_BATCH(0);
576 
577 	OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_VS);
578 	OUT_BATCH(0);
579 
580 	OUT_BATCH(GEN6_3DSTATE_CONSTANT_VS | (11 - 2));
581 	OUT_BATCH(0);
582 	OUT_BATCH(0);
583 	OUT_BATCH(0);
584 	OUT_BATCH(0);
585 	OUT_BATCH(0);
586 	OUT_BATCH(0);
587 	OUT_BATCH(0);
588 	OUT_BATCH(0);
589 	OUT_BATCH(0);
590 	OUT_BATCH(0);
591 
592 	OUT_BATCH(GEN6_3DSTATE_VS | (9-2));
593 	OUT_BATCH(0);
594 	OUT_BATCH(0);
595 	OUT_BATCH(0);
596 	OUT_BATCH(0);
597 	OUT_BATCH(0);
598 	OUT_BATCH(0);
599 	OUT_BATCH(0);
600 	OUT_BATCH(0);
601 }
602 
603 static void
gen8_emit_hs(struct intel_batchbuffer * batch)604 gen8_emit_hs(struct intel_batchbuffer *batch) {
605 	OUT_BATCH(GEN7_3DSTATE_CONSTANT_HS | (11 - 2));
606 	OUT_BATCH(0);
607 	OUT_BATCH(0);
608 	OUT_BATCH(0);
609 	OUT_BATCH(0);
610 	OUT_BATCH(0);
611 	OUT_BATCH(0);
612 	OUT_BATCH(0);
613 	OUT_BATCH(0);
614 	OUT_BATCH(0);
615 	OUT_BATCH(0);
616 
617 	OUT_BATCH(GEN7_3DSTATE_HS | (9-2));
618 	OUT_BATCH(0);
619 	OUT_BATCH(0);
620 	OUT_BATCH(0);
621 	OUT_BATCH(0);
622 	OUT_BATCH(0);
623 	OUT_BATCH(0);
624 	OUT_BATCH(0);
625 	OUT_BATCH(0);
626 
627 	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_HS);
628 	OUT_BATCH(0);
629 
630 	OUT_BATCH(GEN8_3DSTATE_SAMPLER_STATE_POINTERS_HS);
631 	OUT_BATCH(0);
632 }
633 
634 static void
gen8_emit_gs(struct intel_batchbuffer * batch)635 gen8_emit_gs(struct intel_batchbuffer *batch) {
636 	OUT_BATCH(GEN6_3DSTATE_CONSTANT_GS | (11 - 2));
637 	OUT_BATCH(0);
638 	OUT_BATCH(0);
639 	OUT_BATCH(0);
640 	OUT_BATCH(0);
641 	OUT_BATCH(0);
642 	OUT_BATCH(0);
643 	OUT_BATCH(0);
644 	OUT_BATCH(0);
645 	OUT_BATCH(0);
646 	OUT_BATCH(0);
647 
648 	OUT_BATCH(GEN6_3DSTATE_GS | (10-2));
649 	OUT_BATCH(0);
650 	OUT_BATCH(0);
651 	OUT_BATCH(0);
652 	OUT_BATCH(0);
653 	OUT_BATCH(0);
654 	OUT_BATCH(0);
655 	OUT_BATCH(0);
656 	OUT_BATCH(0);
657 	OUT_BATCH(0);
658 
659 	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_GS);
660 	OUT_BATCH(0);
661 
662 	OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_GS);
663 	OUT_BATCH(0);
664 }
665 
666 static void
gen8_emit_ds(struct intel_batchbuffer * batch)667 gen8_emit_ds(struct intel_batchbuffer *batch) {
668 	OUT_BATCH(GEN7_3DSTATE_CONSTANT_DS | (11 - 2));
669 	OUT_BATCH(0);
670 	OUT_BATCH(0);
671 	OUT_BATCH(0);
672 	OUT_BATCH(0);
673 	OUT_BATCH(0);
674 	OUT_BATCH(0);
675 	OUT_BATCH(0);
676 	OUT_BATCH(0);
677 	OUT_BATCH(0);
678 	OUT_BATCH(0);
679 
680 	OUT_BATCH(GEN7_3DSTATE_DS | (9-2));
681 	OUT_BATCH(0);
682 	OUT_BATCH(0);
683 	OUT_BATCH(0);
684 	OUT_BATCH(0);
685 	OUT_BATCH(0);
686 	OUT_BATCH(0);
687 	OUT_BATCH(0);
688 	OUT_BATCH(0);
689 
690 	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_DS);
691 	OUT_BATCH(0);
692 
693 	OUT_BATCH(GEN8_3DSTATE_SAMPLER_STATE_POINTERS_DS);
694 	OUT_BATCH(0);
695 }
696 
697 static void
gen8_emit_wm_hz_op(struct intel_batchbuffer * batch)698 gen8_emit_wm_hz_op(struct intel_batchbuffer *batch) {
699 	OUT_BATCH(GEN8_3DSTATE_WM_HZ_OP | (5-2));
700 	OUT_BATCH(0);
701 	OUT_BATCH(0);
702 	OUT_BATCH(0);
703 	OUT_BATCH(0);
704 }
705 
706 static void
gen8_emit_null_state(struct intel_batchbuffer * batch)707 gen8_emit_null_state(struct intel_batchbuffer *batch) {
708 	gen8_emit_wm_hz_op(batch);
709 	gen8_emit_hs(batch);
710 	OUT_BATCH(GEN7_3DSTATE_TE | (4-2));
711 	OUT_BATCH(0);
712 	OUT_BATCH(0);
713 	OUT_BATCH(0);
714 	gen8_emit_gs(batch);
715 	gen8_emit_ds(batch);
716 	gen8_emit_vs(batch);
717 }
718 
719 static void
gen7_emit_clip(struct intel_batchbuffer * batch)720 gen7_emit_clip(struct intel_batchbuffer *batch) {
721 	OUT_BATCH(GEN6_3DSTATE_CLIP | (4 - 2));
722 	OUT_BATCH(0);
723 	OUT_BATCH(0); /*  pass-through */
724 	OUT_BATCH(0);
725 }
726 
727 static void
gen8_emit_sf(struct intel_batchbuffer * batch)728 gen8_emit_sf(struct intel_batchbuffer *batch)
729 {
730 	int i;
731 
732 	OUT_BATCH(GEN7_3DSTATE_SBE | (4 - 2));
733 	OUT_BATCH(1 << GEN7_SBE_NUM_OUTPUTS_SHIFT |
734 		  GEN8_SBE_FORCE_URB_ENTRY_READ_LENGTH |
735 		  GEN8_SBE_FORCE_URB_ENTRY_READ_OFFSET |
736 		  1 << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
737 		  1 << GEN8_SBE_URB_ENTRY_READ_OFFSET_SHIFT);
738 	OUT_BATCH(0);
739 	OUT_BATCH(0);
740 
741 	OUT_BATCH(GEN8_3DSTATE_SBE_SWIZ | (11 - 2));
742 	for (i = 0; i < 8; i++)
743 		OUT_BATCH(0);
744 	OUT_BATCH(0);
745 	OUT_BATCH(0);
746 
747 	OUT_BATCH(GEN8_3DSTATE_RASTER | (5 - 2));
748 	OUT_BATCH(GEN8_RASTER_FRONT_WINDING_CCW | GEN8_RASTER_CULL_NONE);
749 	OUT_BATCH(0);
750 	OUT_BATCH(0);
751 	OUT_BATCH(0);
752 
753 	OUT_BATCH(GEN6_3DSTATE_SF | (4 - 2));
754 	OUT_BATCH(0);
755 	OUT_BATCH(0);
756 	OUT_BATCH(0);
757 }
758 
759 static void
gen8_emit_ps(struct intel_batchbuffer * batch,uint32_t kernel)760 gen8_emit_ps(struct intel_batchbuffer *batch, uint32_t kernel) {
761 	const int max_threads = 63;
762 
763 	OUT_BATCH(GEN6_3DSTATE_WM | (2 - 2));
764 	OUT_BATCH(/* XXX: I don't understand the BARYCENTRIC stuff, but it
765 		   * appears we need it to put our setup data in the place we
766 		   * expect (g6, see below) */
767 		  GEN8_3DSTATE_PS_PERSPECTIVE_PIXEL_BARYCENTRIC);
768 
769 	OUT_BATCH(GEN6_3DSTATE_CONSTANT_PS | (11-2));
770 	OUT_BATCH(0);
771 	OUT_BATCH(0);
772 	OUT_BATCH(0);
773 	OUT_BATCH(0);
774 	OUT_BATCH(0);
775 	OUT_BATCH(0);
776 	OUT_BATCH(0);
777 	OUT_BATCH(0);
778 	OUT_BATCH(0);
779 	OUT_BATCH(0);
780 
781 	OUT_BATCH(GEN7_3DSTATE_PS | (12-2));
782 	OUT_BATCH(kernel);
783 	OUT_BATCH(0); /* kernel hi */
784 	OUT_BATCH(1 << GEN6_3DSTATE_WM_SAMPLER_COUNT_SHIFT |
785 		  2 << GEN6_3DSTATE_WM_BINDING_TABLE_ENTRY_COUNT_SHIFT);
786 	OUT_BATCH(0); /* scratch space stuff */
787 	OUT_BATCH(0); /* scratch hi */
788 	OUT_BATCH((max_threads - 1) << GEN8_3DSTATE_PS_MAX_THREADS_SHIFT |
789 		  GEN6_3DSTATE_WM_16_DISPATCH_ENABLE);
790 	OUT_BATCH(6 << GEN6_3DSTATE_WM_DISPATCH_START_GRF_0_SHIFT);
791 	OUT_BATCH(0); // kernel 1
792 	OUT_BATCH(0); /* kernel 1 hi */
793 	OUT_BATCH(0); // kernel 2
794 	OUT_BATCH(0); /* kernel 2 hi */
795 
796 	OUT_BATCH(GEN8_3DSTATE_PS_BLEND | (2 - 2));
797 	OUT_BATCH(GEN8_PS_BLEND_HAS_WRITEABLE_RT);
798 
799 	OUT_BATCH(GEN8_3DSTATE_PS_EXTRA | (2 - 2));
800 	OUT_BATCH(GEN8_PSX_PIXEL_SHADER_VALID | GEN8_PSX_ATTRIBUTE_ENABLE);
801 }
802 
803 static void
gen8_emit_depth(struct intel_batchbuffer * batch)804 gen8_emit_depth(struct intel_batchbuffer *batch) {
805 	OUT_BATCH(GEN8_3DSTATE_WM_DEPTH_STENCIL | (3 - 2));
806 	OUT_BATCH(0);
807 	OUT_BATCH(0);
808 
809 	OUT_BATCH(GEN7_3DSTATE_DEPTH_BUFFER | (8-2));
810 	OUT_BATCH(0);
811 	OUT_BATCH(0);
812 	OUT_BATCH(0);
813 	OUT_BATCH(0);
814 	OUT_BATCH(0);
815 	OUT_BATCH(0);
816 	OUT_BATCH(0);
817 
818 	OUT_BATCH(GEN8_3DSTATE_HIER_DEPTH_BUFFER | (5 - 2));
819 	OUT_BATCH(0);
820 	OUT_BATCH(0);
821 	OUT_BATCH(0);
822 	OUT_BATCH(0);
823 
824 	OUT_BATCH(GEN8_3DSTATE_STENCIL_BUFFER | (5 - 2));
825 	OUT_BATCH(0);
826 	OUT_BATCH(0);
827 	OUT_BATCH(0);
828 	OUT_BATCH(0);
829 }
830 
831 static void
gen7_emit_clear(struct intel_batchbuffer * batch)832 gen7_emit_clear(struct intel_batchbuffer *batch) {
833 	OUT_BATCH(GEN7_3DSTATE_CLEAR_PARAMS | (3-2));
834 	OUT_BATCH(0);
835 	OUT_BATCH(1); // clear valid
836 }
837 
838 static void
gen6_emit_drawing_rectangle(struct intel_batchbuffer * batch,const struct igt_buf * dst)839 gen6_emit_drawing_rectangle(struct intel_batchbuffer *batch, const struct igt_buf *dst)
840 {
841 	OUT_BATCH(GEN4_3DSTATE_DRAWING_RECTANGLE | (4 - 2));
842 	OUT_BATCH(0);
843 	OUT_BATCH((igt_buf_height(dst) - 1) << 16 | (igt_buf_width(dst) - 1));
844 	OUT_BATCH(0);
845 }
846 
gen8_emit_vf_topology(struct intel_batchbuffer * batch)847 static void gen8_emit_vf_topology(struct intel_batchbuffer *batch)
848 {
849 	OUT_BATCH(GEN8_3DSTATE_VF_TOPOLOGY);
850 	OUT_BATCH(_3DPRIM_RECTLIST);
851 }
852 
853 /* Vertex elements MUST be defined before this according to spec */
gen8_emit_primitive(struct intel_batchbuffer * batch,uint32_t offset)854 static void gen8_emit_primitive(struct intel_batchbuffer *batch, uint32_t offset)
855 {
856 	OUT_BATCH(GEN8_3DSTATE_VF_INSTANCING | (3 - 2));
857 	OUT_BATCH(0);
858 	OUT_BATCH(0);
859 
860 	OUT_BATCH(GEN4_3DPRIMITIVE | (7-2));
861 	OUT_BATCH(0);	/* gen8+ ignore the topology type field */
862 	OUT_BATCH(3);	/* vertex count */
863 	OUT_BATCH(0);	/*  We're specifying this instead with offset in GEN6_3DSTATE_VERTEX_BUFFERS */
864 	OUT_BATCH(1);	/* single instance */
865 	OUT_BATCH(0);	/* start instance location */
866 	OUT_BATCH(0);	/* index buffer offset, ignored */
867 }
868 
869 /* The general rule is if it's named gen6 it is directly copied from
870  * gen6_render_copyfunc.
871  *
872  * This sets up most of the 3d pipeline, and most of that to NULL state. The
873  * docs aren't specific about exactly what must be set up NULL, but the general
874  * rule is we could be run at any time, and so the most state we set to NULL,
875  * the better our odds of success.
876  *
877  * +---------------+ <---- 4096
878  * |       ^       |
879  * |       |       |
880  * |    various    |
881  * |      state    |
882  * |       |       |
883  * |_______|_______| <---- 2048 + ?
884  * |       ^       |
885  * |       |       |
886  * |   batch       |
887  * |    commands   |
888  * |       |       |
889  * |       |       |
890  * +---------------+ <---- 0 + ?
891  *
892  * The batch commands point to state within tthe batch, so all state offsets should be
893  * 0 < offset < 4096. Both commands and state build upwards, and are constructed
894  * in that order. This means too many batch commands can delete state if not
895  * careful.
896  *
897  */
898 
899 #define BATCH_STATE_SPLIT 2048
900 
gen8_render_copyfunc(struct intel_batchbuffer * batch,drm_intel_context * context,const struct igt_buf * src,unsigned src_x,unsigned src_y,unsigned width,unsigned height,const struct igt_buf * dst,unsigned dst_x,unsigned dst_y)901 void gen8_render_copyfunc(struct intel_batchbuffer *batch,
902 			  drm_intel_context *context,
903 			  const struct igt_buf *src, unsigned src_x, unsigned src_y,
904 			  unsigned width, unsigned height,
905 			  const struct igt_buf *dst, unsigned dst_x, unsigned dst_y)
906 {
907 	struct annotations_context aub_annotations;
908 	uint32_t ps_sampler_state, ps_kernel_off, ps_binding_table;
909 	uint32_t scissor_state;
910 	uint32_t vertex_buffer;
911 	uint32_t batch_end;
912 
913 	igt_assert(src->bpp == dst->bpp);
914 	intel_batchbuffer_flush_with_context(batch, context);
915 
916 	intel_batchbuffer_align(batch, 8);
917 
918 	batch->ptr = &batch->buffer[BATCH_STATE_SPLIT];
919 
920 	annotation_init(&aub_annotations);
921 
922 	ps_binding_table  = gen8_bind_surfaces(batch, &aub_annotations,
923 					       src, dst);
924 	ps_sampler_state  = gen8_create_sampler(batch, &aub_annotations);
925 	ps_kernel_off = gen8_fill_ps(batch, &aub_annotations,
926 				     ps_kernel, sizeof(ps_kernel));
927 	vertex_buffer = gen7_fill_vertex_buffer_data(batch, &aub_annotations,
928 						     src,
929 						     src_x, src_y,
930 						     dst_x, dst_y,
931 						     width, height);
932 	cc.cc_state = gen6_create_cc_state(batch, &aub_annotations);
933 	cc.blend_state = gen8_create_blend_state(batch, &aub_annotations);
934 	viewport.cc_state = gen6_create_cc_viewport(batch, &aub_annotations);
935 	viewport.sf_clip_state = gen7_create_sf_clip_viewport(batch, &aub_annotations);
936 	scissor_state = gen6_create_scissor_rect(batch, &aub_annotations);
937 	/* TODO: theree is other state which isn't setup */
938 
939 	igt_assert(batch->ptr < &batch->buffer[4095]);
940 
941 	batch->ptr = batch->buffer;
942 
943 	/* Start emitting the commands. The order roughly follows the mesa blorp
944 	 * order */
945 	OUT_BATCH(G4X_PIPELINE_SELECT | PIPELINE_SELECT_3D);
946 
947 	gen8_emit_sip(batch);
948 
949 	gen7_emit_push_constants(batch);
950 
951 	gen8_emit_state_base_address(batch);
952 
953 	OUT_BATCH(GEN7_3DSTATE_VIEWPORT_STATE_POINTERS_CC);
954 	OUT_BATCH(viewport.cc_state);
955 	OUT_BATCH(GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP);
956 	OUT_BATCH(viewport.sf_clip_state);
957 
958 	gen7_emit_urb(batch);
959 
960 	gen8_emit_cc(batch);
961 
962 	gen8_emit_multisample(batch);
963 
964 	gen8_emit_null_state(batch);
965 
966 	OUT_BATCH(GEN7_3DSTATE_STREAMOUT | (5-2));
967 	OUT_BATCH(0);
968 	OUT_BATCH(0);
969 	OUT_BATCH(0);
970 	OUT_BATCH(0);
971 
972 	gen7_emit_clip(batch);
973 
974 	gen8_emit_sf(batch);
975 
976 	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_PS);
977 	OUT_BATCH(ps_binding_table);
978 
979 	OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_PS);
980 	OUT_BATCH(ps_sampler_state);
981 
982 	gen8_emit_ps(batch, ps_kernel_off);
983 
984 	OUT_BATCH(GEN8_3DSTATE_SCISSOR_STATE_POINTERS);
985 	OUT_BATCH(scissor_state);
986 
987 	gen8_emit_depth(batch);
988 
989 	gen7_emit_clear(batch);
990 
991 	gen6_emit_drawing_rectangle(batch, dst);
992 
993 	gen8_emit_vertex_buffer(batch, vertex_buffer);
994 	gen6_emit_vertex_elements(batch);
995 
996 	gen8_emit_vf_topology(batch);
997 	gen8_emit_primitive(batch, vertex_buffer);
998 
999 	OUT_BATCH(MI_BATCH_BUFFER_END);
1000 
1001 	batch_end = intel_batchbuffer_align(batch, 8);
1002 	igt_assert(batch_end < BATCH_STATE_SPLIT);
1003 	annotation_add_batch(&aub_annotations, batch_end);
1004 
1005 	dump_batch(batch);
1006 
1007 	annotation_flush(&aub_annotations, batch);
1008 
1009 	gen6_render_flush(batch, context, batch_end);
1010 	intel_batchbuffer_reset(batch);
1011 }
1012