mock_region.c 3.1 KB
Newer Older
1 2
// SPDX-License-Identifier: MIT
/*
3
 * Copyright © 2019-2021 Intel Corporation
4 5
 */

6
#include <drm/ttm/ttm_placement.h>
7 8
#include <linux/scatterlist.h>

9 10
#include "gem/i915_gem_region.h"
#include "intel_memory_region.h"
11
#include "intel_region_ttm.h"
12 13 14

#include "mock_region.h"

15 16 17
static void mock_region_put_pages(struct drm_i915_gem_object *obj,
				  struct sg_table *pages)
{
18 19
	i915_refct_sgt_put(obj->mm.rsgt);
	obj->mm.rsgt = NULL;
20
	intel_region_ttm_resource_free(obj->mm.region, obj->mm.res);
21 22 23 24 25
}

static int mock_region_get_pages(struct drm_i915_gem_object *obj)
{
	struct sg_table *pages;
26
	int err;
27

28
	obj->mm.res = intel_region_ttm_resource_alloc(obj->mm.region,
29
						      obj->bo_offset,
30
						      obj->base.size,
31
						      obj->flags);
32 33
	if (IS_ERR(obj->mm.res))
		return PTR_ERR(obj->mm.res);
34

35 36 37 38
	obj->mm.rsgt = intel_region_ttm_resource_to_rsgt(obj->mm.region,
							 obj->mm.res);
	if (IS_ERR(obj->mm.rsgt)) {
		err = PTR_ERR(obj->mm.rsgt);
39
		goto err_free_resource;
40 41
	}

42
	pages = &obj->mm.rsgt->table;
43 44 45
	__i915_gem_object_set_pages(obj, pages, i915_sg_dma_sizes(pages->sgl));

	return 0;
46 47 48 49

err_free_resource:
	intel_region_ttm_resource_free(obj->mm.region, obj->mm.res);
	return err;
50 51
}

52
static const struct drm_i915_gem_object_ops mock_region_obj_ops = {
53
	.name = "mock-region",
54 55
	.get_pages = mock_region_get_pages,
	.put_pages = mock_region_put_pages,
56 57 58
	.release = i915_gem_object_release_memory_region,
};

59 60
static int mock_object_init(struct intel_memory_region *mem,
			    struct drm_i915_gem_object *obj,
61
			    resource_size_t offset,
62
			    resource_size_t size,
63
			    resource_size_t page_size,
64
			    unsigned int flags)
65
{
66
	static struct lock_class_key lock_class;
67 68
	struct drm_i915_private *i915 = mem->i915;

69
	if (size > resource_size(&mem->region))
70
		return -E2BIG;
71 72

	drm_gem_private_object_init(&i915->drm, &obj->base, size);
73
	i915_gem_object_init(obj, &mock_region_obj_ops, &lock_class, flags);
74

75 76
	obj->bo_offset = offset;

77 78 79 80
	obj->read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;

	i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);

81
	i915_gem_object_init_memory_region(obj, mem);
82

83
	return 0;
84 85
}

86
static int mock_region_fini(struct intel_memory_region *mem)
87 88 89
{
	struct drm_i915_private *i915 = mem->i915;
	int instance = mem->instance;
90
	int ret;
91

92
	ret = intel_region_ttm_fini(mem);
93
	ida_free(&i915->selftest.mock_region_instances, instance);
94 95

	return ret;
96 97
}

98
static const struct intel_memory_region_ops mock_region_ops = {
99 100
	.init = intel_region_ttm_init,
	.release = mock_region_fini,
101
	.init_object = mock_object_init,
102 103 104 105 106 107 108
};

struct intel_memory_region *
mock_region_create(struct drm_i915_private *i915,
		   resource_size_t start,
		   resource_size_t size,
		   resource_size_t min_page_size,
M
Matthew Auld 已提交
109 110
		   resource_size_t io_start,
		   resource_size_t io_size)
111
{
112 113 114 115 116 117 118
	int instance = ida_alloc_max(&i915->selftest.mock_region_instances,
				     TTM_NUM_MEM_TYPES - TTM_PL_PRIV - 1,
				     GFP_KERNEL);

	if (instance < 0)
		return ERR_PTR(instance);

119
	return intel_memory_region_create(i915, start, size, min_page_size,
M
Matthew Auld 已提交
120 121
					  io_start, io_size,
					  INTEL_MEMORY_MOCK, instance,
122
					  &mock_region_ops);
123
}