igt_gem_utils.c 3.43 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright © 2018 Intel Corporation
 */

#include "igt_gem_utils.h"

9 10
#include "gem/i915_gem_context.h"
#include "gem/i915_gem_pm.h"
11
#include "gt/intel_context.h"
12
#include "gt/intel_gpu_commands.h"
13
#include "gt/intel_gt.h"
14 15
#include "i915_vma.h"
#include "i915_drv.h"
16

17
#include "i915_request.h"
18 19 20 21 22 23 24 25 26 27 28 29

struct i915_request *
igt_request_alloc(struct i915_gem_context *ctx, struct intel_engine_cs *engine)
{
	struct intel_context *ce;
	struct i915_request *rq;

	/*
	 * Pinning the contexts may generate requests in order to acquire
	 * GGTT space, so do this first before we reserve a seqno for
	 * ourselves.
	 */
30
	ce = i915_gem_context_get_engine(ctx, engine->legacy_idx);
31 32 33 34 35 36 37 38
	if (IS_ERR(ce))
		return ERR_CAST(ce);

	rq = intel_context_create_request(ce);
	intel_context_put(ce);

	return rq;
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

struct i915_vma *
igt_emit_store_dw(struct i915_vma *vma,
		  u64 offset,
		  unsigned long count,
		  u32 val)
{
	struct drm_i915_gem_object *obj;
	const int gen = INTEL_GEN(vma->vm->i915);
	unsigned long n, size;
	u32 *cmd;
	int err;

	size = (4 * count + 1) * sizeof(u32);
	size = round_up(size, PAGE_SIZE);
	obj = i915_gem_object_create_internal(vma->vm->i915, size);
	if (IS_ERR(obj))
		return ERR_CAST(obj);

58
	cmd = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WC);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	if (IS_ERR(cmd)) {
		err = PTR_ERR(cmd);
		goto err;
	}

	GEM_BUG_ON(offset + (count - 1) * PAGE_SIZE > vma->node.size);
	offset += vma->node.start;

	for (n = 0; n < count; n++) {
		if (gen >= 8) {
			*cmd++ = MI_STORE_DWORD_IMM_GEN4;
			*cmd++ = lower_32_bits(offset);
			*cmd++ = upper_32_bits(offset);
			*cmd++ = val;
		} else if (gen >= 4) {
			*cmd++ = MI_STORE_DWORD_IMM_GEN4 |
				(gen < 6 ? MI_USE_GGTT : 0);
			*cmd++ = 0;
			*cmd++ = offset;
			*cmd++ = val;
		} else {
			*cmd++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
			*cmd++ = offset;
			*cmd++ = val;
		}
		offset += PAGE_SIZE;
	}
	*cmd = MI_BATCH_BUFFER_END;
87

88
	i915_gem_object_flush_map(obj);
89 90
	i915_gem_object_unpin_map(obj);

91 92
	intel_gt_chipset_flush(vma->vm->gt);

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	vma = i915_vma_instance(obj, vma->vm, NULL);
	if (IS_ERR(vma)) {
		err = PTR_ERR(vma);
		goto err;
	}

	err = i915_vma_pin(vma, 0, 0, PIN_USER);
	if (err)
		goto err;

	return vma;

err:
	i915_gem_object_put(obj);
	return ERR_PTR(err);
}

110 111 112
int igt_gpu_fill_dw(struct intel_context *ce,
		    struct i915_vma *vma, u64 offset,
		    unsigned long count, u32 val)
113 114 115 116 117 118
{
	struct i915_request *rq;
	struct i915_vma *batch;
	unsigned int flags;
	int err;

119
	GEM_BUG_ON(!intel_engine_can_store_dword(ce->engine));
120 121 122 123 124 125
	GEM_BUG_ON(!i915_vma_is_pinned(vma));

	batch = igt_emit_store_dw(vma, offset, count, val);
	if (IS_ERR(batch))
		return PTR_ERR(batch);

126
	rq = intel_context_create_request(ce);
127 128 129 130 131 132
	if (IS_ERR(rq)) {
		err = PTR_ERR(rq);
		goto err_batch;
	}

	i915_vma_lock(batch);
133 134 135
	err = i915_request_await_object(rq, batch->obj, false);
	if (err == 0)
		err = i915_vma_move_to_active(batch, rq, 0);
136 137 138 139 140
	i915_vma_unlock(batch);
	if (err)
		goto skip_request;

	i915_vma_lock(vma);
141 142 143
	err = i915_request_await_object(rq, vma->obj, true);
	if (err == 0)
		err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
144 145 146 147
	i915_vma_unlock(vma);
	if (err)
		goto skip_request;

148 149 150
	flags = 0;
	if (INTEL_GEN(ce->vm->i915) <= 5)
		flags |= I915_DISPATCH_SECURE;
151

152 153 154
	err = rq->engine->emit_bb_start(rq,
					batch->node.start, batch->node.size,
					flags);
155 156

skip_request:
157 158
	if (err)
		i915_request_set_error_once(rq, err);
159 160
	i915_request_add(rq);
err_batch:
161
	i915_vma_unpin_and_release(&batch, 0);
162 163
	return err;
}