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_validates_to_lmem - Whether the object is resident in
28  * lmem when pages are present.
29  * @obj: The object to check.
30  *
31  * Migratable objects residency may change from under us if the object is
32  * not pinned or locked. This function is intended to be used to check whether
33  * the object can only reside in lmem when pages are present.
34  *
35  * Return: Whether the object is always resident in lmem when pages are
36  * present.
37  */
38 bool i915_gem_object_validates_to_lmem(struct drm_i915_gem_object *obj)
39 {
40 	struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
41 
42 	return !i915_gem_object_migratable(obj) &&
43 		mr && (mr->type == INTEL_MEMORY_LOCAL ||
44 		       mr->type == INTEL_MEMORY_STOLEN_LOCAL);
45 }
46 
47 /**
48  * i915_gem_object_is_lmem - Whether the object is resident in
49  * lmem
50  * @obj: The object to check.
51  *
52  * Even if an object is allowed to migrate and change memory region,
53  * this function checks whether it will always be present in lmem when
54  * valid *or* if that's not the case, whether it's currently resident in lmem.
55  * For migratable and evictable objects, the latter only makes sense when
56  * the object is locked.
57  *
58  * Return: Whether the object migratable but resident in lmem, or not
59  * migratable and will be present in lmem when valid.
60  */
61 bool i915_gem_object_is_lmem(struct drm_i915_gem_object *obj)
62 {
63 	struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
64 
65 #ifdef CONFIG_LOCKDEP
66 	if (i915_gem_object_migratable(obj) &&
67 	    i915_gem_object_evictable(obj))
68 		assert_object_held(obj);
69 #endif
70 	return mr && (mr->type == INTEL_MEMORY_LOCAL ||
71 		      mr->type == INTEL_MEMORY_STOLEN_LOCAL);
72 }
73 
74 struct drm_i915_gem_object *
75 i915_gem_object_create_lmem(struct drm_i915_private *i915,
76 			    resource_size_t size,
77 			    unsigned int flags)
78 {
79 	return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_LMEM],
80 					     size, flags);
81 }
82