1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019-2021 Intel Corporation
6 #include <drm/ttm/ttm_placement.h>
7 #include <linux/scatterlist.h>
9 #include "gem/i915_gem_region.h"
10 #include "intel_memory_region.h"
11 #include "intel_region_ttm.h"
13 #include "mock_region.h"
15 static void mock_region_put_pages(struct drm_i915_gem_object *obj,
16 struct sg_table *pages)
18 i915_refct_sgt_put(obj->mm.rsgt);
20 intel_region_ttm_resource_free(obj->mm.region, obj->mm.res);
23 static int mock_region_get_pages(struct drm_i915_gem_object *obj)
25 struct sg_table *pages;
28 obj->mm.res = intel_region_ttm_resource_alloc(obj->mm.region,
32 if (IS_ERR(obj->mm.res))
33 return PTR_ERR(obj->mm.res);
35 obj->mm.rsgt = intel_region_ttm_resource_to_rsgt(obj->mm.region,
37 obj->mm.region->min_page_size);
38 if (IS_ERR(obj->mm.rsgt)) {
39 err = PTR_ERR(obj->mm.rsgt);
40 goto err_free_resource;
43 pages = &obj->mm.rsgt->table;
44 __i915_gem_object_set_pages(obj, pages, i915_sg_dma_sizes(pages->sgl));
49 intel_region_ttm_resource_free(obj->mm.region, obj->mm.res);
53 static const struct drm_i915_gem_object_ops mock_region_obj_ops = {
54 .name = "mock-region",
55 .get_pages = mock_region_get_pages,
56 .put_pages = mock_region_put_pages,
57 .release = i915_gem_object_release_memory_region,
60 static int mock_object_init(struct intel_memory_region *mem,
61 struct drm_i915_gem_object *obj,
62 resource_size_t offset,
64 resource_size_t page_size,
67 static struct lock_class_key lock_class;
68 struct drm_i915_private *i915 = mem->i915;
70 if (size > resource_size(&mem->region))
73 drm_gem_private_object_init(&i915->drm, &obj->base, size);
74 i915_gem_object_init(obj, &mock_region_obj_ops, &lock_class, flags);
76 obj->bo_offset = offset;
78 obj->read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
80 i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
82 i915_gem_object_init_memory_region(obj, mem);
87 static int mock_region_fini(struct intel_memory_region *mem)
89 struct drm_i915_private *i915 = mem->i915;
90 int instance = mem->instance;
93 ret = intel_region_ttm_fini(mem);
94 ida_free(&i915->selftest.mock_region_instances, instance);
99 static const struct intel_memory_region_ops mock_region_ops = {
100 .init = intel_region_ttm_init,
101 .release = mock_region_fini,
102 .init_object = mock_object_init,
105 struct intel_memory_region *
106 mock_region_create(struct drm_i915_private *i915,
107 resource_size_t start,
108 resource_size_t size,
109 resource_size_t min_page_size,
110 resource_size_t io_start,
111 resource_size_t io_size)
113 int instance = ida_alloc_max(&i915->selftest.mock_region_instances,
114 TTM_NUM_MEM_TYPES - TTM_PL_PRIV - 1,
118 return ERR_PTR(instance);
120 return intel_memory_region_create(i915, start, size, min_page_size,
122 INTEL_MEMORY_MOCK, instance,