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