1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include "i915_selftest.h"
7 
8 #include "gt/intel_context.h"
9 #include "gt/intel_engine_regs.h"
10 #include "gt/intel_engine_user.h"
11 #include "gt/intel_gpu_commands.h"
12 #include "gt/intel_gt.h"
13 #include "gt/intel_gt_regs.h"
14 #include "gem/i915_gem_lmem.h"
15 
16 #include "gem/selftests/igt_gem_utils.h"
17 #include "selftests/igt_flush_test.h"
18 #include "selftests/mock_drm.h"
19 #include "selftests/i915_random.h"
20 #include "huge_gem_object.h"
21 #include "mock_context.h"
22 
23 #define OW_SIZE 16                      /* in bytes */
24 #define F_SUBTILE_SIZE 64               /* in bytes */
25 #define F_TILE_WIDTH 128                /* in bytes */
26 #define F_TILE_HEIGHT 32                /* in pixels */
27 #define F_SUBTILE_WIDTH  OW_SIZE        /* in bytes */
28 #define F_SUBTILE_HEIGHT 4              /* in pixels */
29 
30 static int linear_x_y_to_ftiled_pos(int x, int y, u32 stride, int bpp)
31 {
32 	int tile_base;
33 	int tile_x, tile_y;
34 	int swizzle, subtile;
35 	int pixel_size = bpp / 8;
36 	int pos;
37 
38 	/*
39 	 * Subtile remapping for F tile. Note that map[a]==b implies map[b]==a
40 	 * so we can use the same table to tile and until.
41 	 */
42 	static const u8 f_subtile_map[] = {
43 		 0,  1,  2,  3,  8,  9, 10, 11,
44 		 4,  5,  6,  7, 12, 13, 14, 15,
45 		16, 17, 18, 19, 24, 25, 26, 27,
46 		20, 21, 22, 23, 28, 29, 30, 31,
47 		32, 33, 34, 35, 40, 41, 42, 43,
48 		36, 37, 38, 39, 44, 45, 46, 47,
49 		48, 49, 50, 51, 56, 57, 58, 59,
50 		52, 53, 54, 55, 60, 61, 62, 63
51 	};
52 
53 	x *= pixel_size;
54 	/*
55 	 * Where does the 4k tile start (in bytes)?  This is the same for Y and
56 	 * F so we can use the Y-tile algorithm to get to that point.
57 	 */
58 	tile_base =
59 		y / F_TILE_HEIGHT * stride * F_TILE_HEIGHT +
60 		x / F_TILE_WIDTH * 4096;
61 
62 	/* Find pixel within tile */
63 	tile_x = x % F_TILE_WIDTH;
64 	tile_y = y % F_TILE_HEIGHT;
65 
66 	/* And figure out the subtile within the 4k tile */
67 	subtile = tile_y / F_SUBTILE_HEIGHT * 8 + tile_x / F_SUBTILE_WIDTH;
68 
69 	/* Swizzle the subtile number according to the bspec diagram */
70 	swizzle = f_subtile_map[subtile];
71 
72 	/* Calculate new position */
73 	pos = tile_base +
74 		swizzle * F_SUBTILE_SIZE +
75 		tile_y % F_SUBTILE_HEIGHT * OW_SIZE +
76 		tile_x % F_SUBTILE_WIDTH;
77 
78 	GEM_BUG_ON(!IS_ALIGNED(pos, pixel_size));
79 
80 	return pos / pixel_size * 4;
81 }
82 
83 enum client_tiling {
84 	CLIENT_TILING_LINEAR,
85 	CLIENT_TILING_X,
86 	CLIENT_TILING_Y,
87 	CLIENT_TILING_4,
88 	CLIENT_NUM_TILING_TYPES
89 };
90 
91 #define WIDTH 512
92 #define HEIGHT 32
93 
94 struct blit_buffer {
95 	struct i915_vma *vma;
96 	u32 start_val;
97 	enum client_tiling tiling;
98 };
99 
100 struct tiled_blits {
101 	struct intel_context *ce;
102 	struct blit_buffer buffers[3];
103 	struct blit_buffer scratch;
104 	struct i915_vma *batch;
105 	u64 hole;
106 	u64 align;
107 	u32 width;
108 	u32 height;
109 };
110 
111 static bool supports_x_tiling(const struct drm_i915_private *i915)
112 {
113 	int gen = GRAPHICS_VER(i915);
114 
115 	if (gen < 12)
116 		return true;
117 
118 	if (!HAS_LMEM(i915) || IS_DG1(i915))
119 		return false;
120 
121 	return true;
122 }
123 
124 static bool fast_blit_ok(const struct blit_buffer *buf)
125 {
126 	int gen = GRAPHICS_VER(buf->vma->vm->i915);
127 
128 	if (gen < 9)
129 		return false;
130 
131 	if (gen < 12)
132 		return true;
133 
134 	/* filter out platforms with unsupported X-tile support in fastblit */
135 	if (buf->tiling == CLIENT_TILING_X && !supports_x_tiling(buf->vma->vm->i915))
136 		return false;
137 
138 	return true;
139 }
140 
141 static int prepare_blit(const struct tiled_blits *t,
142 			struct blit_buffer *dst,
143 			struct blit_buffer *src,
144 			struct drm_i915_gem_object *batch)
145 {
146 	const int ver = GRAPHICS_VER(to_i915(batch->base.dev));
147 	bool use_64b_reloc = ver >= 8;
148 	u32 src_pitch, dst_pitch;
149 	u32 cmd, *cs;
150 
151 	cs = i915_gem_object_pin_map_unlocked(batch, I915_MAP_WC);
152 	if (IS_ERR(cs))
153 		return PTR_ERR(cs);
154 
155 	if (fast_blit_ok(dst) && fast_blit_ok(src)) {
156 		struct intel_gt *gt = t->ce->engine->gt;
157 		u32 src_tiles = 0, dst_tiles = 0;
158 		u32 src_4t = 0, dst_4t = 0;
159 
160 		/* Need to program BLIT_CCTL if it is not done previously
161 		 * before using XY_FAST_COPY_BLT
162 		 */
163 		*cs++ = MI_LOAD_REGISTER_IMM(1);
164 		*cs++ = i915_mmio_reg_offset(BLIT_CCTL(t->ce->engine->mmio_base));
165 		*cs++ = (BLIT_CCTL_SRC_MOCS(gt->mocs.uc_index) |
166 			 BLIT_CCTL_DST_MOCS(gt->mocs.uc_index));
167 
168 		src_pitch = t->width; /* in dwords */
169 		if (src->tiling == CLIENT_TILING_4) {
170 			src_tiles = XY_FAST_COPY_BLT_D0_SRC_TILE_MODE(YMAJOR);
171 			src_4t = XY_FAST_COPY_BLT_D1_SRC_TILE4;
172 		} else if (src->tiling == CLIENT_TILING_Y) {
173 			src_tiles = XY_FAST_COPY_BLT_D0_SRC_TILE_MODE(YMAJOR);
174 		} else if (src->tiling == CLIENT_TILING_X) {
175 			src_tiles = XY_FAST_COPY_BLT_D0_SRC_TILE_MODE(TILE_X);
176 		} else {
177 			src_pitch *= 4; /* in bytes */
178 		}
179 
180 		dst_pitch = t->width; /* in dwords */
181 		if (dst->tiling == CLIENT_TILING_4) {
182 			dst_tiles = XY_FAST_COPY_BLT_D0_DST_TILE_MODE(YMAJOR);
183 			dst_4t = XY_FAST_COPY_BLT_D1_DST_TILE4;
184 		} else if (dst->tiling == CLIENT_TILING_Y) {
185 			dst_tiles = XY_FAST_COPY_BLT_D0_DST_TILE_MODE(YMAJOR);
186 		} else if (dst->tiling == CLIENT_TILING_X) {
187 			dst_tiles = XY_FAST_COPY_BLT_D0_DST_TILE_MODE(TILE_X);
188 		} else {
189 			dst_pitch *= 4; /* in bytes */
190 		}
191 
192 		*cs++ = GEN9_XY_FAST_COPY_BLT_CMD | (10 - 2) |
193 			src_tiles | dst_tiles;
194 		*cs++ = src_4t | dst_4t | BLT_DEPTH_32 | dst_pitch;
195 		*cs++ = 0;
196 		*cs++ = t->height << 16 | t->width;
197 		*cs++ = lower_32_bits(i915_vma_offset(dst->vma));
198 		*cs++ = upper_32_bits(i915_vma_offset(dst->vma));
199 		*cs++ = 0;
200 		*cs++ = src_pitch;
201 		*cs++ = lower_32_bits(i915_vma_offset(src->vma));
202 		*cs++ = upper_32_bits(i915_vma_offset(src->vma));
203 	} else {
204 		if (ver >= 6) {
205 			*cs++ = MI_LOAD_REGISTER_IMM(1);
206 			*cs++ = i915_mmio_reg_offset(BCS_SWCTRL);
207 			cmd = (BCS_SRC_Y | BCS_DST_Y) << 16;
208 			if (src->tiling == CLIENT_TILING_Y)
209 				cmd |= BCS_SRC_Y;
210 			if (dst->tiling == CLIENT_TILING_Y)
211 				cmd |= BCS_DST_Y;
212 			*cs++ = cmd;
213 
214 			cmd = MI_FLUSH_DW;
215 			if (ver >= 8)
216 				cmd++;
217 			*cs++ = cmd;
218 			*cs++ = 0;
219 			*cs++ = 0;
220 			*cs++ = 0;
221 		}
222 
223 		cmd = XY_SRC_COPY_BLT_CMD | BLT_WRITE_RGBA | (8 - 2);
224 		if (ver >= 8)
225 			cmd += 2;
226 
227 		src_pitch = t->width * 4;
228 		if (src->tiling) {
229 			cmd |= XY_SRC_COPY_BLT_SRC_TILED;
230 			src_pitch /= 4;
231 		}
232 
233 		dst_pitch = t->width * 4;
234 		if (dst->tiling) {
235 			cmd |= XY_SRC_COPY_BLT_DST_TILED;
236 			dst_pitch /= 4;
237 		}
238 
239 		*cs++ = cmd;
240 		*cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | dst_pitch;
241 		*cs++ = 0;
242 		*cs++ = t->height << 16 | t->width;
243 		*cs++ = lower_32_bits(i915_vma_offset(dst->vma));
244 		if (use_64b_reloc)
245 			*cs++ = upper_32_bits(i915_vma_offset(dst->vma));
246 		*cs++ = 0;
247 		*cs++ = src_pitch;
248 		*cs++ = lower_32_bits(i915_vma_offset(src->vma));
249 		if (use_64b_reloc)
250 			*cs++ = upper_32_bits(i915_vma_offset(src->vma));
251 	}
252 
253 	*cs++ = MI_BATCH_BUFFER_END;
254 
255 	i915_gem_object_flush_map(batch);
256 	i915_gem_object_unpin_map(batch);
257 
258 	return 0;
259 }
260 
261 static void tiled_blits_destroy_buffers(struct tiled_blits *t)
262 {
263 	int i;
264 
265 	for (i = 0; i < ARRAY_SIZE(t->buffers); i++)
266 		i915_vma_put(t->buffers[i].vma);
267 
268 	i915_vma_put(t->scratch.vma);
269 	i915_vma_put(t->batch);
270 }
271 
272 static struct i915_vma *
273 __create_vma(struct tiled_blits *t, size_t size, bool lmem)
274 {
275 	struct drm_i915_private *i915 = t->ce->vm->i915;
276 	struct drm_i915_gem_object *obj;
277 	struct i915_vma *vma;
278 
279 	if (lmem)
280 		obj = i915_gem_object_create_lmem(i915, size, 0);
281 	else
282 		obj = i915_gem_object_create_shmem(i915, size);
283 	if (IS_ERR(obj))
284 		return ERR_CAST(obj);
285 
286 	vma = i915_vma_instance(obj, t->ce->vm, NULL);
287 	if (IS_ERR(vma))
288 		i915_gem_object_put(obj);
289 
290 	return vma;
291 }
292 
293 static struct i915_vma *create_vma(struct tiled_blits *t, bool lmem)
294 {
295 	return __create_vma(t, PAGE_ALIGN(t->width * t->height * 4), lmem);
296 }
297 
298 static int tiled_blits_create_buffers(struct tiled_blits *t,
299 				      int width, int height,
300 				      struct rnd_state *prng)
301 {
302 	struct drm_i915_private *i915 = t->ce->engine->i915;
303 	int i;
304 
305 	t->width = width;
306 	t->height = height;
307 
308 	t->batch = __create_vma(t, PAGE_SIZE, false);
309 	if (IS_ERR(t->batch))
310 		return PTR_ERR(t->batch);
311 
312 	t->scratch.vma = create_vma(t, false);
313 	if (IS_ERR(t->scratch.vma)) {
314 		i915_vma_put(t->batch);
315 		return PTR_ERR(t->scratch.vma);
316 	}
317 
318 	for (i = 0; i < ARRAY_SIZE(t->buffers); i++) {
319 		struct i915_vma *vma;
320 
321 		vma = create_vma(t, HAS_LMEM(i915) && i % 2);
322 		if (IS_ERR(vma)) {
323 			tiled_blits_destroy_buffers(t);
324 			return PTR_ERR(vma);
325 		}
326 
327 		t->buffers[i].vma = vma;
328 		t->buffers[i].tiling =
329 			i915_prandom_u32_max_state(CLIENT_NUM_TILING_TYPES, prng);
330 
331 		/* Platforms support either TileY or Tile4, not both */
332 		if (HAS_4TILE(i915) && t->buffers[i].tiling == CLIENT_TILING_Y)
333 			t->buffers[i].tiling = CLIENT_TILING_4;
334 		else if (!HAS_4TILE(i915) && t->buffers[i].tiling == CLIENT_TILING_4)
335 			t->buffers[i].tiling = CLIENT_TILING_Y;
336 	}
337 
338 	return 0;
339 }
340 
341 static void fill_scratch(struct tiled_blits *t, u32 *vaddr, u32 val)
342 {
343 	int i;
344 
345 	t->scratch.start_val = val;
346 	for (i = 0; i < t->width * t->height; i++)
347 		vaddr[i] = val++;
348 
349 	i915_gem_object_flush_map(t->scratch.vma->obj);
350 }
351 
352 static u64 swizzle_bit(unsigned int bit, u64 offset)
353 {
354 	return (offset & BIT_ULL(bit)) >> (bit - 6);
355 }
356 
357 static u64 tiled_offset(const struct intel_gt *gt,
358 			u64 v,
359 			unsigned int stride,
360 			enum client_tiling tiling,
361 			int x_pos, int y_pos)
362 {
363 	unsigned int swizzle;
364 	u64 x, y;
365 
366 	if (tiling == CLIENT_TILING_LINEAR)
367 		return v;
368 
369 	y = div64_u64_rem(v, stride, &x);
370 
371 	if (tiling == CLIENT_TILING_4) {
372 		v = linear_x_y_to_ftiled_pos(x_pos, y_pos, stride, 32);
373 
374 		/* no swizzling for f-tiling */
375 		swizzle = I915_BIT_6_SWIZZLE_NONE;
376 	} else if (tiling == CLIENT_TILING_X) {
377 		v = div64_u64_rem(y, 8, &y) * stride * 8;
378 		v += y * 512;
379 		v += div64_u64_rem(x, 512, &x) << 12;
380 		v += x;
381 
382 		swizzle = gt->ggtt->bit_6_swizzle_x;
383 	} else {
384 		const unsigned int ytile_span = 16;
385 		const unsigned int ytile_height = 512;
386 
387 		v = div64_u64_rem(y, 32, &y) * stride * 32;
388 		v += y * ytile_span;
389 		v += div64_u64_rem(x, ytile_span, &x) * ytile_height;
390 		v += x;
391 
392 		swizzle = gt->ggtt->bit_6_swizzle_y;
393 	}
394 
395 	switch (swizzle) {
396 	case I915_BIT_6_SWIZZLE_9:
397 		v ^= swizzle_bit(9, v);
398 		break;
399 	case I915_BIT_6_SWIZZLE_9_10:
400 		v ^= swizzle_bit(9, v) ^ swizzle_bit(10, v);
401 		break;
402 	case I915_BIT_6_SWIZZLE_9_11:
403 		v ^= swizzle_bit(9, v) ^ swizzle_bit(11, v);
404 		break;
405 	case I915_BIT_6_SWIZZLE_9_10_11:
406 		v ^= swizzle_bit(9, v) ^ swizzle_bit(10, v) ^ swizzle_bit(11, v);
407 		break;
408 	}
409 
410 	return v;
411 }
412 
413 static const char *repr_tiling(enum client_tiling tiling)
414 {
415 	switch (tiling) {
416 	case CLIENT_TILING_LINEAR: return "linear";
417 	case CLIENT_TILING_X: return "X";
418 	case CLIENT_TILING_Y: return "Y";
419 	case CLIENT_TILING_4: return "F";
420 	default: return "unknown";
421 	}
422 }
423 
424 static int verify_buffer(const struct tiled_blits *t,
425 			 struct blit_buffer *buf,
426 			 struct rnd_state *prng)
427 {
428 	const u32 *vaddr;
429 	int ret = 0;
430 	int x, y, p;
431 
432 	x = i915_prandom_u32_max_state(t->width, prng);
433 	y = i915_prandom_u32_max_state(t->height, prng);
434 	p = y * t->width + x;
435 
436 	vaddr = i915_gem_object_pin_map_unlocked(buf->vma->obj, I915_MAP_WC);
437 	if (IS_ERR(vaddr))
438 		return PTR_ERR(vaddr);
439 
440 	if (vaddr[0] != buf->start_val) {
441 		ret = -EINVAL;
442 	} else {
443 		u64 v = tiled_offset(buf->vma->vm->gt,
444 				     p * 4, t->width * 4,
445 				     buf->tiling, x, y);
446 
447 		if (vaddr[v / sizeof(*vaddr)] != buf->start_val + p)
448 			ret = -EINVAL;
449 	}
450 	if (ret) {
451 		pr_err("Invalid %s tiling detected at (%d, %d), start_val %x\n",
452 		       repr_tiling(buf->tiling),
453 		       x, y, buf->start_val);
454 		igt_hexdump(vaddr, 4096);
455 	}
456 
457 	i915_gem_object_unpin_map(buf->vma->obj);
458 	return ret;
459 }
460 
461 static int pin_buffer(struct i915_vma *vma, u64 addr)
462 {
463 	int err;
464 
465 	if (drm_mm_node_allocated(&vma->node) && i915_vma_offset(vma) != addr) {
466 		err = i915_vma_unbind_unlocked(vma);
467 		if (err)
468 			return err;
469 	}
470 
471 	err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_OFFSET_FIXED | addr);
472 	if (err)
473 		return err;
474 
475 	GEM_BUG_ON(i915_vma_offset(vma) != addr);
476 	return 0;
477 }
478 
479 static int
480 tiled_blit(struct tiled_blits *t,
481 	   struct blit_buffer *dst, u64 dst_addr,
482 	   struct blit_buffer *src, u64 src_addr)
483 {
484 	struct i915_request *rq;
485 	int err;
486 
487 	err = pin_buffer(src->vma, src_addr);
488 	if (err) {
489 		pr_err("Cannot pin src @ %llx\n", src_addr);
490 		return err;
491 	}
492 
493 	err = pin_buffer(dst->vma, dst_addr);
494 	if (err) {
495 		pr_err("Cannot pin dst @ %llx\n", dst_addr);
496 		goto err_src;
497 	}
498 
499 	err = i915_vma_pin(t->batch, 0, 0, PIN_USER | PIN_HIGH);
500 	if (err) {
501 		pr_err("cannot pin batch\n");
502 		goto err_dst;
503 	}
504 
505 	err = prepare_blit(t, dst, src, t->batch->obj);
506 	if (err)
507 		goto err_bb;
508 
509 	rq = intel_context_create_request(t->ce);
510 	if (IS_ERR(rq)) {
511 		err = PTR_ERR(rq);
512 		goto err_bb;
513 	}
514 
515 	err = igt_vma_move_to_active_unlocked(t->batch, rq, 0);
516 	if (!err)
517 		err = igt_vma_move_to_active_unlocked(src->vma, rq, 0);
518 	if (!err)
519 		err = igt_vma_move_to_active_unlocked(dst->vma, rq, 0);
520 	if (!err)
521 		err = rq->engine->emit_bb_start(rq,
522 						i915_vma_offset(t->batch),
523 						i915_vma_size(t->batch),
524 						0);
525 	i915_request_get(rq);
526 	i915_request_add(rq);
527 	if (i915_request_wait(rq, 0, HZ / 2) < 0)
528 		err = -ETIME;
529 	i915_request_put(rq);
530 
531 	dst->start_val = src->start_val;
532 err_bb:
533 	i915_vma_unpin(t->batch);
534 err_dst:
535 	i915_vma_unpin(dst->vma);
536 err_src:
537 	i915_vma_unpin(src->vma);
538 	return err;
539 }
540 
541 static struct tiled_blits *
542 tiled_blits_create(struct intel_engine_cs *engine, struct rnd_state *prng)
543 {
544 	struct drm_mm_node hole;
545 	struct tiled_blits *t;
546 	u64 hole_size;
547 	int err;
548 
549 	t = kzalloc(sizeof(*t), GFP_KERNEL);
550 	if (!t)
551 		return ERR_PTR(-ENOMEM);
552 
553 	t->ce = intel_context_create(engine);
554 	if (IS_ERR(t->ce)) {
555 		err = PTR_ERR(t->ce);
556 		goto err_free;
557 	}
558 
559 	t->align = i915_vm_min_alignment(t->ce->vm, INTEL_MEMORY_LOCAL);
560 	t->align = max(t->align,
561 		       i915_vm_min_alignment(t->ce->vm, INTEL_MEMORY_SYSTEM));
562 
563 	hole_size = 2 * round_up(WIDTH * HEIGHT * 4, t->align);
564 	hole_size *= 2; /* room to maneuver */
565 	hole_size += 2 * t->align; /* padding on either side */
566 
567 	mutex_lock(&t->ce->vm->mutex);
568 	memset(&hole, 0, sizeof(hole));
569 	err = drm_mm_insert_node_in_range(&t->ce->vm->mm, &hole,
570 					  hole_size, t->align,
571 					  I915_COLOR_UNEVICTABLE,
572 					  0, U64_MAX,
573 					  DRM_MM_INSERT_BEST);
574 	if (!err)
575 		drm_mm_remove_node(&hole);
576 	mutex_unlock(&t->ce->vm->mutex);
577 	if (err) {
578 		err = -ENODEV;
579 		goto err_put;
580 	}
581 
582 	t->hole = hole.start + t->align;
583 	pr_info("Using hole at %llx\n", t->hole);
584 
585 	err = tiled_blits_create_buffers(t, WIDTH, HEIGHT, prng);
586 	if (err)
587 		goto err_put;
588 
589 	return t;
590 
591 err_put:
592 	intel_context_put(t->ce);
593 err_free:
594 	kfree(t);
595 	return ERR_PTR(err);
596 }
597 
598 static void tiled_blits_destroy(struct tiled_blits *t)
599 {
600 	tiled_blits_destroy_buffers(t);
601 
602 	intel_context_put(t->ce);
603 	kfree(t);
604 }
605 
606 static int tiled_blits_prepare(struct tiled_blits *t,
607 			       struct rnd_state *prng)
608 {
609 	u64 offset = round_up(t->width * t->height * 4, t->align);
610 	u32 *map;
611 	int err;
612 	int i;
613 
614 	map = i915_gem_object_pin_map_unlocked(t->scratch.vma->obj, I915_MAP_WC);
615 	if (IS_ERR(map))
616 		return PTR_ERR(map);
617 
618 	/* Use scratch to fill objects */
619 	for (i = 0; i < ARRAY_SIZE(t->buffers); i++) {
620 		fill_scratch(t, map, prandom_u32_state(prng));
621 		GEM_BUG_ON(verify_buffer(t, &t->scratch, prng));
622 
623 		err = tiled_blit(t,
624 				 &t->buffers[i], t->hole + offset,
625 				 &t->scratch, t->hole);
626 		if (err == 0)
627 			err = verify_buffer(t, &t->buffers[i], prng);
628 		if (err) {
629 			pr_err("Failed to create buffer %d\n", i);
630 			break;
631 		}
632 	}
633 
634 	i915_gem_object_unpin_map(t->scratch.vma->obj);
635 	return err;
636 }
637 
638 static int tiled_blits_bounce(struct tiled_blits *t, struct rnd_state *prng)
639 {
640 	u64 offset = round_up(t->width * t->height * 4, 2 * t->align);
641 	int err;
642 
643 	/* We want to check position invariant tiling across GTT eviction */
644 
645 	err = tiled_blit(t,
646 			 &t->buffers[1], t->hole + offset / 2,
647 			 &t->buffers[0], t->hole + 2 * offset);
648 	if (err)
649 		return err;
650 
651 	/* Simulating GTT eviction of the same buffer / layout */
652 	t->buffers[2].tiling = t->buffers[0].tiling;
653 
654 	/* Reposition so that we overlap the old addresses, and slightly off */
655 	err = tiled_blit(t,
656 			 &t->buffers[2], t->hole + t->align,
657 			 &t->buffers[1], t->hole + 3 * offset / 2);
658 	if (err)
659 		return err;
660 
661 	err = verify_buffer(t, &t->buffers[2], prng);
662 	if (err)
663 		return err;
664 
665 	return 0;
666 }
667 
668 static int __igt_client_tiled_blits(struct intel_engine_cs *engine,
669 				    struct rnd_state *prng)
670 {
671 	struct tiled_blits *t;
672 	int err;
673 
674 	t = tiled_blits_create(engine, prng);
675 	if (IS_ERR(t))
676 		return PTR_ERR(t);
677 
678 	err = tiled_blits_prepare(t, prng);
679 	if (err)
680 		goto out;
681 
682 	err = tiled_blits_bounce(t, prng);
683 	if (err)
684 		goto out;
685 
686 out:
687 	tiled_blits_destroy(t);
688 	return err;
689 }
690 
691 static bool has_bit17_swizzle(int sw)
692 {
693 	return (sw == I915_BIT_6_SWIZZLE_9_10_17 ||
694 		sw == I915_BIT_6_SWIZZLE_9_17);
695 }
696 
697 static bool bad_swizzling(struct drm_i915_private *i915)
698 {
699 	struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
700 
701 	if (i915->gem_quirks & GEM_QUIRK_PIN_SWIZZLED_PAGES)
702 		return true;
703 
704 	if (has_bit17_swizzle(ggtt->bit_6_swizzle_x) ||
705 	    has_bit17_swizzle(ggtt->bit_6_swizzle_y))
706 		return true;
707 
708 	return false;
709 }
710 
711 static int igt_client_tiled_blits(void *arg)
712 {
713 	struct drm_i915_private *i915 = arg;
714 	I915_RND_STATE(prng);
715 	int inst = 0;
716 
717 	/* Test requires explicit BLT tiling controls */
718 	if (GRAPHICS_VER(i915) < 4)
719 		return 0;
720 
721 	if (bad_swizzling(i915)) /* Requires sane (sub-page) swizzling */
722 		return 0;
723 
724 	do {
725 		struct intel_engine_cs *engine;
726 		int err;
727 
728 		engine = intel_engine_lookup_user(i915,
729 						  I915_ENGINE_CLASS_COPY,
730 						  inst++);
731 		if (!engine)
732 			return 0;
733 
734 		err = __igt_client_tiled_blits(engine, &prng);
735 		if (err == -ENODEV)
736 			err = 0;
737 		if (err)
738 			return err;
739 	} while (1);
740 }
741 
742 int i915_gem_client_blt_live_selftests(struct drm_i915_private *i915)
743 {
744 	static const struct i915_subtest tests[] = {
745 		SUBTEST(igt_client_tiled_blits),
746 	};
747 
748 	if (intel_gt_is_wedged(to_gt(i915)))
749 		return 0;
750 
751 	return i915_live_subtests(tests, i915);
752 }
753