1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include "intel_memory_region.h"
7 #include "i915_gem_region.h"
8 #include "i915_drv.h"
9 #include "i915_trace.h"
10 
11 void i915_gem_object_init_memory_region(struct drm_i915_gem_object *obj,
12 					struct intel_memory_region *mem)
13 {
14 	obj->mm.region = intel_memory_region_get(mem);
15 
16 	mutex_lock(&mem->objects.lock);
17 	list_add(&obj->mm.region_link, &mem->objects.list);
18 	mutex_unlock(&mem->objects.lock);
19 }
20 
21 void i915_gem_object_release_memory_region(struct drm_i915_gem_object *obj)
22 {
23 	struct intel_memory_region *mem = obj->mm.region;
24 
25 	mutex_lock(&mem->objects.lock);
26 	list_del(&obj->mm.region_link);
27 	mutex_unlock(&mem->objects.lock);
28 
29 	intel_memory_region_put(mem);
30 }
31 
32 struct drm_i915_gem_object *
33 i915_gem_object_create_region(struct intel_memory_region *mem,
34 			      resource_size_t size,
35 			      resource_size_t page_size,
36 			      unsigned int flags)
37 {
38 	struct drm_i915_gem_object *obj;
39 	resource_size_t default_page_size;
40 	int err;
41 
42 	/*
43 	 * NB: Our use of resource_size_t for the size stems from using struct
44 	 * resource for the mem->region. We might need to revisit this in the
45 	 * future.
46 	 */
47 
48 	GEM_BUG_ON(flags & ~I915_BO_ALLOC_FLAGS);
49 
50 	if (!mem)
51 		return ERR_PTR(-ENODEV);
52 
53 	default_page_size = mem->min_page_size;
54 	if (page_size)
55 		default_page_size = page_size;
56 
57 	GEM_BUG_ON(!is_power_of_2_u64(default_page_size));
58 	GEM_BUG_ON(default_page_size < PAGE_SIZE);
59 
60 	size = round_up(size, default_page_size);
61 
62 	GEM_BUG_ON(!size);
63 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_MIN_ALIGNMENT));
64 
65 	if (i915_gem_object_size_2big(size))
66 		return ERR_PTR(-E2BIG);
67 
68 	obj = i915_gem_object_alloc();
69 	if (!obj)
70 		return ERR_PTR(-ENOMEM);
71 
72 	err = mem->ops->init_object(mem, obj, size, page_size, flags);
73 	if (err)
74 		goto err_object_free;
75 
76 	trace_i915_gem_object_create(obj);
77 	return obj;
78 
79 err_object_free:
80 	i915_gem_object_free(obj);
81 	return ERR_PTR(err);
82 }
83