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