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