1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include "intel_memory_region.h"
7 #include "gem/i915_gem_region.h"
8 #include "gem/i915_gem_lmem.h"
9 #include "i915_drv.h"
10 
11 void __iomem *
12 i915_gem_object_lmem_io_map(struct drm_i915_gem_object *obj,
13 			    unsigned long n,
14 			    unsigned long size)
15 {
16 	resource_size_t offset;
17 
18 	GEM_BUG_ON(!i915_gem_object_is_contiguous(obj));
19 
20 	offset = i915_gem_object_get_dma_address(obj, n);
21 	offset -= obj->mm.region->region.start;
22 
23 	return io_mapping_map_wc(&obj->mm.region->iomap, offset, size);
24 }
25 
26 /**
27  * i915_gem_object_is_lmem - Whether the object is resident in
28  * lmem
29  * @obj: The object to check.
30  *
31  * Even if an object is allowed to migrate and change memory region,
32  * this function checks whether it will always be present in lmem when
33  * valid *or* if that's not the case, whether it's currently resident in lmem.
34  * For migratable and evictable objects, the latter only makes sense when
35  * the object is locked.
36  *
37  * Return: Whether the object migratable but resident in lmem, or not
38  * migratable and will be present in lmem when valid.
39  */
40 bool i915_gem_object_is_lmem(struct drm_i915_gem_object *obj)
41 {
42 	struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
43 
44 #ifdef CONFIG_LOCKDEP
45 	if (i915_gem_object_migratable(obj) &&
46 	    i915_gem_object_evictable(obj))
47 		assert_object_held(obj);
48 #endif
49 	return mr && (mr->type == INTEL_MEMORY_LOCAL ||
50 		      mr->type == INTEL_MEMORY_STOLEN_LOCAL);
51 }
52 
53 /**
54  * __i915_gem_object_is_lmem - Whether the object is resident in
55  * lmem while in the fence signaling critical path.
56  * @obj: The object to check.
57  *
58  * This function is intended to be called from within the fence signaling
59  * path where the fence keeps the object from being migrated. For example
60  * during gpu reset or similar.
61  *
62  * Return: Whether the object is resident in lmem.
63  */
64 bool __i915_gem_object_is_lmem(struct drm_i915_gem_object *obj)
65 {
66 	struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
67 
68 #ifdef CONFIG_LOCKDEP
69 	GEM_WARN_ON(dma_resv_test_signaled(obj->base.resv, true));
70 #endif
71 	return mr && (mr->type == INTEL_MEMORY_LOCAL ||
72 		      mr->type == INTEL_MEMORY_STOLEN_LOCAL);
73 }
74 
75 /**
76  * __i915_gem_object_create_lmem_with_ps - Create lmem object and force the
77  * minimum page size for the backing pages.
78  * @i915: The i915 instance.
79  * @size: The size in bytes for the object. Note that we need to round the size
80  * up depending on the @page_size. The final object size can be fished out from
81  * the drm GEM object.
82  * @page_size: The requested minimum page size in bytes for this object. This is
83  * useful if we need something bigger than the regions min_page_size due to some
84  * hw restriction, or in some very specialised cases where it needs to be
85  * smaller, where the internal fragmentation cost is too great when rounding up
86  * the object size.
87  * @flags: The optional BO allocation flags.
88  *
89  * Note that this interface assumes you know what you are doing when forcing the
90  * @page_size. If this is smaller than the regions min_page_size then it can
91  * never be inserted into any GTT, otherwise it might lead to undefined
92  * behaviour.
93  *
94  * Return: The object pointer, which might be an ERR_PTR in the case of failure.
95  */
96 struct drm_i915_gem_object *
97 __i915_gem_object_create_lmem_with_ps(struct drm_i915_private *i915,
98 				      resource_size_t size,
99 				      resource_size_t page_size,
100 				      unsigned int flags)
101 {
102 	return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_LMEM],
103 					     size, page_size, flags);
104 }
105 
106 struct drm_i915_gem_object *
107 i915_gem_object_create_lmem_from_data(struct drm_i915_private *i915,
108 				      const void *data, size_t size)
109 {
110 	struct drm_i915_gem_object *obj;
111 	void *map;
112 
113 	obj = i915_gem_object_create_lmem(i915,
114 					  round_up(size, PAGE_SIZE),
115 					  I915_BO_ALLOC_CONTIGUOUS);
116 	if (IS_ERR(obj))
117 		return obj;
118 
119 	map = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WC);
120 	if (IS_ERR(map)) {
121 		i915_gem_object_put(obj);
122 		return map;
123 	}
124 
125 	memcpy(map, data, size);
126 
127 	i915_gem_object_unpin_map(obj);
128 
129 	return obj;
130 }
131 
132 struct drm_i915_gem_object *
133 i915_gem_object_create_lmem(struct drm_i915_private *i915,
134 			    resource_size_t size,
135 			    unsigned int flags)
136 {
137 	return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_LMEM],
138 					     size, 0, flags);
139 }
140