1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include "i915_selftest.h"
8 #include "gt/intel_context.h"
9 #include "gt/intel_engine_user.h"
10 #include "gt/intel_gt.h"
11 #include "gt/intel_gpu_commands.h"
12 #include "gem/i915_gem_lmem.h"
14 #include "selftests/igt_flush_test.h"
15 #include "selftests/mock_drm.h"
16 #include "selftests/i915_random.h"
17 #include "huge_gem_object.h"
18 #include "mock_context.h"
24 CLIENT_NUM_TILING_TYPES
33 enum client_tiling tiling;
37 struct intel_context *ce;
38 struct blit_buffer buffers[3];
39 struct blit_buffer scratch;
40 struct i915_vma *batch;
46 static int prepare_blit(const struct tiled_blits *t,
47 struct blit_buffer *dst,
48 struct blit_buffer *src,
49 struct drm_i915_gem_object *batch)
51 const int ver = GRAPHICS_VER(to_i915(batch->base.dev));
52 bool use_64b_reloc = ver >= 8;
53 u32 src_pitch, dst_pitch;
56 cs = i915_gem_object_pin_map_unlocked(batch, I915_MAP_WC);
60 *cs++ = MI_LOAD_REGISTER_IMM(1);
61 *cs++ = i915_mmio_reg_offset(BCS_SWCTRL);
62 cmd = (BCS_SRC_Y | BCS_DST_Y) << 16;
63 if (src->tiling == CLIENT_TILING_Y)
65 if (dst->tiling == CLIENT_TILING_Y)
77 cmd = XY_SRC_COPY_BLT_CMD | BLT_WRITE_RGBA | (8 - 2);
81 src_pitch = t->width * 4;
83 cmd |= XY_SRC_COPY_BLT_SRC_TILED;
87 dst_pitch = t->width * 4;
89 cmd |= XY_SRC_COPY_BLT_DST_TILED;
94 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | dst_pitch;
96 *cs++ = t->height << 16 | t->width;
97 *cs++ = lower_32_bits(dst->vma->node.start);
99 *cs++ = upper_32_bits(dst->vma->node.start);
102 *cs++ = lower_32_bits(src->vma->node.start);
104 *cs++ = upper_32_bits(src->vma->node.start);
106 *cs++ = MI_BATCH_BUFFER_END;
108 i915_gem_object_flush_map(batch);
109 i915_gem_object_unpin_map(batch);
114 static void tiled_blits_destroy_buffers(struct tiled_blits *t)
118 for (i = 0; i < ARRAY_SIZE(t->buffers); i++)
119 i915_vma_put(t->buffers[i].vma);
121 i915_vma_put(t->scratch.vma);
122 i915_vma_put(t->batch);
125 static struct i915_vma *
126 __create_vma(struct tiled_blits *t, size_t size, bool lmem)
128 struct drm_i915_private *i915 = t->ce->vm->i915;
129 struct drm_i915_gem_object *obj;
130 struct i915_vma *vma;
133 obj = i915_gem_object_create_lmem(i915, size, 0);
135 obj = i915_gem_object_create_shmem(i915, size);
137 return ERR_CAST(obj);
139 vma = i915_vma_instance(obj, t->ce->vm, NULL);
141 i915_gem_object_put(obj);
146 static struct i915_vma *create_vma(struct tiled_blits *t, bool lmem)
148 return __create_vma(t, PAGE_ALIGN(t->width * t->height * 4), lmem);
151 static int tiled_blits_create_buffers(struct tiled_blits *t,
152 int width, int height,
153 struct rnd_state *prng)
155 struct drm_i915_private *i915 = t->ce->engine->i915;
161 t->batch = __create_vma(t, PAGE_SIZE, false);
162 if (IS_ERR(t->batch))
163 return PTR_ERR(t->batch);
165 t->scratch.vma = create_vma(t, false);
166 if (IS_ERR(t->scratch.vma)) {
167 i915_vma_put(t->batch);
168 return PTR_ERR(t->scratch.vma);
171 for (i = 0; i < ARRAY_SIZE(t->buffers); i++) {
172 struct i915_vma *vma;
174 vma = create_vma(t, HAS_LMEM(i915) && i % 2);
176 tiled_blits_destroy_buffers(t);
180 t->buffers[i].vma = vma;
181 t->buffers[i].tiling =
182 i915_prandom_u32_max_state(CLIENT_TILING_Y + 1, prng);
188 static void fill_scratch(struct tiled_blits *t, u32 *vaddr, u32 val)
192 t->scratch.start_val = val;
193 for (i = 0; i < t->width * t->height; i++)
196 i915_gem_object_flush_map(t->scratch.vma->obj);
199 static u64 swizzle_bit(unsigned int bit, u64 offset)
201 return (offset & BIT_ULL(bit)) >> (bit - 6);
204 static u64 tiled_offset(const struct intel_gt *gt,
207 enum client_tiling tiling)
209 unsigned int swizzle;
212 if (tiling == CLIENT_TILING_LINEAR)
215 y = div64_u64_rem(v, stride, &x);
217 if (tiling == CLIENT_TILING_X) {
218 v = div64_u64_rem(y, 8, &y) * stride * 8;
220 v += div64_u64_rem(x, 512, &x) << 12;
223 swizzle = gt->ggtt->bit_6_swizzle_x;
225 const unsigned int ytile_span = 16;
226 const unsigned int ytile_height = 512;
228 v = div64_u64_rem(y, 32, &y) * stride * 32;
230 v += div64_u64_rem(x, ytile_span, &x) * ytile_height;
233 swizzle = gt->ggtt->bit_6_swizzle_y;
237 case I915_BIT_6_SWIZZLE_9:
238 v ^= swizzle_bit(9, v);
240 case I915_BIT_6_SWIZZLE_9_10:
241 v ^= swizzle_bit(9, v) ^ swizzle_bit(10, v);
243 case I915_BIT_6_SWIZZLE_9_11:
244 v ^= swizzle_bit(9, v) ^ swizzle_bit(11, v);
246 case I915_BIT_6_SWIZZLE_9_10_11:
247 v ^= swizzle_bit(9, v) ^ swizzle_bit(10, v) ^ swizzle_bit(11, v);
254 static const char *repr_tiling(enum client_tiling tiling)
257 case CLIENT_TILING_LINEAR: return "linear";
258 case CLIENT_TILING_X: return "X";
259 case CLIENT_TILING_Y: return "Y";
260 default: return "unknown";
264 static int verify_buffer(const struct tiled_blits *t,
265 struct blit_buffer *buf,
266 struct rnd_state *prng)
272 x = i915_prandom_u32_max_state(t->width, prng);
273 y = i915_prandom_u32_max_state(t->height, prng);
274 p = y * t->width + x;
276 vaddr = i915_gem_object_pin_map_unlocked(buf->vma->obj, I915_MAP_WC);
278 return PTR_ERR(vaddr);
280 if (vaddr[0] != buf->start_val) {
283 u64 v = tiled_offset(buf->vma->vm->gt,
287 if (vaddr[v / sizeof(*vaddr)] != buf->start_val + p)
291 pr_err("Invalid %s tiling detected at (%d, %d), start_val %x\n",
292 repr_tiling(buf->tiling),
293 x, y, buf->start_val);
294 igt_hexdump(vaddr, 4096);
297 i915_gem_object_unpin_map(buf->vma->obj);
301 static int move_to_active(struct i915_vma *vma,
302 struct i915_request *rq,
308 err = i915_request_await_object(rq, vma->obj, false);
310 err = i915_vma_move_to_active(vma, rq, flags);
311 i915_vma_unlock(vma);
316 static int pin_buffer(struct i915_vma *vma, u64 addr)
320 if (drm_mm_node_allocated(&vma->node) && vma->node.start != addr) {
321 err = i915_vma_unbind(vma);
326 err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_OFFSET_FIXED | addr);
334 tiled_blit(struct tiled_blits *t,
335 struct blit_buffer *dst, u64 dst_addr,
336 struct blit_buffer *src, u64 src_addr)
338 struct i915_request *rq;
341 err = pin_buffer(src->vma, src_addr);
343 pr_err("Cannot pin src @ %llx\n", src_addr);
347 err = pin_buffer(dst->vma, dst_addr);
349 pr_err("Cannot pin dst @ %llx\n", dst_addr);
353 err = i915_vma_pin(t->batch, 0, 0, PIN_USER | PIN_HIGH);
355 pr_err("cannot pin batch\n");
359 err = prepare_blit(t, dst, src, t->batch->obj);
363 rq = intel_context_create_request(t->ce);
369 err = move_to_active(t->batch, rq, 0);
371 err = move_to_active(src->vma, rq, 0);
373 err = move_to_active(dst->vma, rq, 0);
375 err = rq->engine->emit_bb_start(rq,
376 t->batch->node.start,
379 i915_request_get(rq);
380 i915_request_add(rq);
381 if (i915_request_wait(rq, 0, HZ / 2) < 0)
383 i915_request_put(rq);
385 dst->start_val = src->start_val;
387 i915_vma_unpin(t->batch);
389 i915_vma_unpin(dst->vma);
391 i915_vma_unpin(src->vma);
395 static struct tiled_blits *
396 tiled_blits_create(struct intel_engine_cs *engine, struct rnd_state *prng)
398 struct drm_mm_node hole;
399 struct tiled_blits *t;
403 t = kzalloc(sizeof(*t), GFP_KERNEL);
405 return ERR_PTR(-ENOMEM);
407 t->ce = intel_context_create(engine);
409 err = PTR_ERR(t->ce);
413 hole_size = 2 * PAGE_ALIGN(WIDTH * HEIGHT * 4);
414 hole_size *= 2; /* room to maneuver */
415 hole_size += 2 * I915_GTT_MIN_ALIGNMENT;
417 mutex_lock(&t->ce->vm->mutex);
418 memset(&hole, 0, sizeof(hole));
419 err = drm_mm_insert_node_in_range(&t->ce->vm->mm, &hole,
420 hole_size, 0, I915_COLOR_UNEVICTABLE,
424 drm_mm_remove_node(&hole);
425 mutex_unlock(&t->ce->vm->mutex);
431 t->hole = hole.start + I915_GTT_MIN_ALIGNMENT;
432 pr_info("Using hole at %llx\n", t->hole);
434 err = tiled_blits_create_buffers(t, WIDTH, HEIGHT, prng);
441 intel_context_put(t->ce);
447 static void tiled_blits_destroy(struct tiled_blits *t)
449 tiled_blits_destroy_buffers(t);
451 intel_context_put(t->ce);
455 static int tiled_blits_prepare(struct tiled_blits *t,
456 struct rnd_state *prng)
458 u64 offset = PAGE_ALIGN(t->width * t->height * 4);
463 map = i915_gem_object_pin_map_unlocked(t->scratch.vma->obj, I915_MAP_WC);
467 /* Use scratch to fill objects */
468 for (i = 0; i < ARRAY_SIZE(t->buffers); i++) {
469 fill_scratch(t, map, prandom_u32_state(prng));
470 GEM_BUG_ON(verify_buffer(t, &t->scratch, prng));
473 &t->buffers[i], t->hole + offset,
474 &t->scratch, t->hole);
476 err = verify_buffer(t, &t->buffers[i], prng);
478 pr_err("Failed to create buffer %d\n", i);
483 i915_gem_object_unpin_map(t->scratch.vma->obj);
487 static int tiled_blits_bounce(struct tiled_blits *t, struct rnd_state *prng)
490 round_up(t->width * t->height * 4, 2 * I915_GTT_MIN_ALIGNMENT);
493 /* We want to check position invariant tiling across GTT eviction */
496 &t->buffers[1], t->hole + offset / 2,
497 &t->buffers[0], t->hole + 2 * offset);
501 /* Reposition so that we overlap the old addresses, and slightly off */
503 &t->buffers[2], t->hole + I915_GTT_MIN_ALIGNMENT,
504 &t->buffers[1], t->hole + 3 * offset / 2);
508 err = verify_buffer(t, &t->buffers[2], prng);
515 static int __igt_client_tiled_blits(struct intel_engine_cs *engine,
516 struct rnd_state *prng)
518 struct tiled_blits *t;
521 t = tiled_blits_create(engine, prng);
525 err = tiled_blits_prepare(t, prng);
529 err = tiled_blits_bounce(t, prng);
534 tiled_blits_destroy(t);
538 static bool has_bit17_swizzle(int sw)
540 return (sw == I915_BIT_6_SWIZZLE_9_10_17 ||
541 sw == I915_BIT_6_SWIZZLE_9_17);
544 static bool bad_swizzling(struct drm_i915_private *i915)
546 struct i915_ggtt *ggtt = &i915->ggtt;
548 if (i915->quirks & QUIRK_PIN_SWIZZLED_PAGES)
551 if (has_bit17_swizzle(ggtt->bit_6_swizzle_x) ||
552 has_bit17_swizzle(ggtt->bit_6_swizzle_y))
558 static int igt_client_tiled_blits(void *arg)
560 struct drm_i915_private *i915 = arg;
561 I915_RND_STATE(prng);
564 /* Test requires explicit BLT tiling controls */
565 if (GRAPHICS_VER(i915) < 4)
568 if (bad_swizzling(i915)) /* Requires sane (sub-page) swizzling */
572 struct intel_engine_cs *engine;
575 engine = intel_engine_lookup_user(i915,
576 I915_ENGINE_CLASS_COPY,
581 err = __igt_client_tiled_blits(engine, &prng);
589 int i915_gem_client_blt_live_selftests(struct drm_i915_private *i915)
591 static const struct i915_subtest tests[] = {
592 SUBTEST(igt_client_tiled_blits),
595 if (intel_gt_is_wedged(&i915->gt))
598 return i915_live_subtests(tests, i915);