1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019-2021 Intel Corporation
4  */
5 
6 #include <drm/ttm/ttm_placement.h>
7 #include <linux/scatterlist.h>
8 
9 #include "gem/i915_gem_region.h"
10 #include "intel_memory_region.h"
11 #include "intel_region_ttm.h"
12 
13 #include "mock_region.h"
14 
15 static void mock_region_put_pages(struct drm_i915_gem_object *obj,
16 				  struct sg_table *pages)
17 {
18 	i915_refct_sgt_put(obj->mm.rsgt);
19 	obj->mm.rsgt = NULL;
20 	intel_region_ttm_resource_free(obj->mm.region, obj->mm.res);
21 }
22 
23 static int mock_region_get_pages(struct drm_i915_gem_object *obj)
24 {
25 	struct sg_table *pages;
26 	int err;
27 
28 	obj->mm.res = intel_region_ttm_resource_alloc(obj->mm.region,
29 						      obj->base.size,
30 						      obj->flags);
31 	if (IS_ERR(obj->mm.res))
32 		return PTR_ERR(obj->mm.res);
33 
34 	obj->mm.rsgt = intel_region_ttm_resource_to_rsgt(obj->mm.region,
35 							 obj->mm.res);
36 	if (IS_ERR(obj->mm.rsgt)) {
37 		err = PTR_ERR(obj->mm.rsgt);
38 		goto err_free_resource;
39 	}
40 
41 	pages = &obj->mm.rsgt->table;
42 	__i915_gem_object_set_pages(obj, pages, i915_sg_dma_sizes(pages->sgl));
43 
44 	return 0;
45 
46 err_free_resource:
47 	intel_region_ttm_resource_free(obj->mm.region, obj->mm.res);
48 	return err;
49 }
50 
51 static const struct drm_i915_gem_object_ops mock_region_obj_ops = {
52 	.name = "mock-region",
53 	.get_pages = mock_region_get_pages,
54 	.put_pages = mock_region_put_pages,
55 	.release = i915_gem_object_release_memory_region,
56 };
57 
58 static int mock_object_init(struct intel_memory_region *mem,
59 			    struct drm_i915_gem_object *obj,
60 			    resource_size_t size,
61 			    resource_size_t page_size,
62 			    unsigned int flags)
63 {
64 	static struct lock_class_key lock_class;
65 	struct drm_i915_private *i915 = mem->i915;
66 
67 	if (size > resource_size(&mem->region))
68 		return -E2BIG;
69 
70 	drm_gem_private_object_init(&i915->drm, &obj->base, size);
71 	i915_gem_object_init(obj, &mock_region_obj_ops, &lock_class, flags);
72 
73 	obj->read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
74 
75 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
76 
77 	i915_gem_object_init_memory_region(obj, mem);
78 
79 	return 0;
80 }
81 
82 static int mock_region_fini(struct intel_memory_region *mem)
83 {
84 	struct drm_i915_private *i915 = mem->i915;
85 	int instance = mem->instance;
86 	int ret;
87 
88 	ret = intel_region_ttm_fini(mem);
89 	ida_free(&i915->selftest.mock_region_instances, instance);
90 
91 	return ret;
92 }
93 
94 static const struct intel_memory_region_ops mock_region_ops = {
95 	.init = intel_region_ttm_init,
96 	.release = mock_region_fini,
97 	.init_object = mock_object_init,
98 };
99 
100 struct intel_memory_region *
101 mock_region_create(struct drm_i915_private *i915,
102 		   resource_size_t start,
103 		   resource_size_t size,
104 		   resource_size_t min_page_size,
105 		   resource_size_t io_start,
106 		   resource_size_t io_size)
107 {
108 	int instance = ida_alloc_max(&i915->selftest.mock_region_instances,
109 				     TTM_NUM_MEM_TYPES - TTM_PL_PRIV - 1,
110 				     GFP_KERNEL);
111 
112 	if (instance < 0)
113 		return ERR_PTR(instance);
114 
115 	return intel_memory_region_create(i915, start, size, min_page_size,
116 					  io_start, io_size,
117 					  INTEL_MEMORY_MOCK, instance,
118 					  &mock_region_ops);
119 }
120