xref: /linux/drivers/gpu/drm/i915/selftests/mock_region.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include "gem/i915_gem_region.h"
7 #include "intel_memory_region.h"
8 
9 #include "mock_region.h"
10 
11 static const struct drm_i915_gem_object_ops mock_region_obj_ops = {
12 	.name = "mock-region",
13 	.get_pages = i915_gem_object_get_pages_buddy,
14 	.put_pages = i915_gem_object_put_pages_buddy,
15 	.release = i915_gem_object_release_memory_region,
16 };
17 
18 static int mock_object_init(struct intel_memory_region *mem,
19 			    struct drm_i915_gem_object *obj,
20 			    resource_size_t size,
21 			    unsigned int flags)
22 {
23 	static struct lock_class_key lock_class;
24 	struct drm_i915_private *i915 = mem->i915;
25 
26 	if (size > mem->mm.size)
27 		return -E2BIG;
28 
29 	drm_gem_private_object_init(&i915->drm, &obj->base, size);
30 	i915_gem_object_init(obj, &mock_region_obj_ops, &lock_class, flags);
31 
32 	obj->read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
33 
34 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
35 
36 	i915_gem_object_init_memory_region(obj, mem);
37 
38 	return 0;
39 }
40 
41 static const struct intel_memory_region_ops mock_region_ops = {
42 	.init = intel_memory_region_init_buddy,
43 	.release = intel_memory_region_release_buddy,
44 	.init_object = mock_object_init,
45 };
46 
47 struct intel_memory_region *
48 mock_region_create(struct drm_i915_private *i915,
49 		   resource_size_t start,
50 		   resource_size_t size,
51 		   resource_size_t min_page_size,
52 		   resource_size_t io_start)
53 {
54 	return intel_memory_region_create(i915, start, size, min_page_size,
55 					  io_start, &mock_region_ops);
56 }
57