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 struct drm_i915_gem_object *
19 mock_object_create(struct intel_memory_region *mem,
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 	struct drm_i915_gem_object *obj;
26 
27 	if (size > BIT(mem->mm.max_order) * mem->mm.chunk_size)
28 		return ERR_PTR(-E2BIG);
29 
30 	obj = i915_gem_object_alloc();
31 	if (!obj)
32 		return ERR_PTR(-ENOMEM);
33 
34 	drm_gem_private_object_init(&i915->drm, &obj->base, size);
35 	i915_gem_object_init(obj, &mock_region_obj_ops, &lock_class);
36 
37 	obj->read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
38 
39 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
40 
41 	i915_gem_object_init_memory_region(obj, mem, flags);
42 
43 	return obj;
44 }
45 
46 static const struct intel_memory_region_ops mock_region_ops = {
47 	.init = intel_memory_region_init_buddy,
48 	.release = intel_memory_region_release_buddy,
49 	.create_object = mock_object_create,
50 };
51 
52 struct intel_memory_region *
53 mock_region_create(struct drm_i915_private *i915,
54 		   resource_size_t start,
55 		   resource_size_t size,
56 		   resource_size_t min_page_size,
57 		   resource_size_t io_start)
58 {
59 	return intel_memory_region_create(i915, start, size, min_page_size,
60 					  io_start, &mock_region_ops);
61 }
62