1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 #ifndef _I915_GEM_TTM_H_
6 #define _I915_GEM_TTM_H_
7 
8 #include <drm/ttm/ttm_placement.h>
9 
10 #include "gem/i915_gem_object_types.h"
11 
12 /**
13  * i915_gem_to_ttm - Convert a struct drm_i915_gem_object to a
14  * struct ttm_buffer_object.
15  * @obj: Pointer to the gem object.
16  *
17  * Return: Pointer to the embedded struct ttm_buffer_object.
18  */
19 static inline struct ttm_buffer_object *
20 i915_gem_to_ttm(struct drm_i915_gem_object *obj)
21 {
22 	return &obj->__do_not_access;
23 }
24 
25 /*
26  * i915 ttm gem object destructor. Internal use only.
27  */
28 void i915_ttm_bo_destroy(struct ttm_buffer_object *bo);
29 
30 /**
31  * i915_ttm_is_ghost_object - Check if the ttm bo is a ghost object.
32  * @bo: Pointer to the ttm buffer object
33  *
34  * Return: True if the ttm bo is not a i915 object but a ghost ttm object,
35  * False otherwise.
36  */
37 static inline bool i915_ttm_is_ghost_object(struct ttm_buffer_object *bo)
38 {
39 	return bo->destroy != i915_ttm_bo_destroy;
40 }
41 
42 /**
43  * i915_ttm_to_gem - Convert a struct ttm_buffer_object to an embedding
44  * struct drm_i915_gem_object.
45  *
46  * Return: Pointer to the embedding struct ttm_buffer_object.
47  */
48 static inline struct drm_i915_gem_object *
49 i915_ttm_to_gem(struct ttm_buffer_object *bo)
50 {
51 	return container_of(bo, struct drm_i915_gem_object, __do_not_access);
52 }
53 
54 int __i915_gem_ttm_object_init(struct intel_memory_region *mem,
55 			       struct drm_i915_gem_object *obj,
56 			       resource_size_t offset,
57 			       resource_size_t size,
58 			       resource_size_t page_size,
59 			       unsigned int flags);
60 
61 /* Internal I915 TTM declarations and definitions below. */
62 
63 #define I915_PL_LMEM0 TTM_PL_PRIV
64 #define I915_PL_SYSTEM TTM_PL_SYSTEM
65 #define I915_PL_STOLEN TTM_PL_VRAM
66 #define I915_PL_GGTT TTM_PL_TT
67 
68 struct ttm_placement *i915_ttm_sys_placement(void);
69 
70 void i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object *obj);
71 
72 struct i915_refct_sgt *
73 i915_ttm_resource_get_st(struct drm_i915_gem_object *obj,
74 			 struct ttm_resource *res);
75 
76 void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj);
77 
78 int i915_ttm_purge(struct drm_i915_gem_object *obj);
79 
80 /**
81  * i915_ttm_gtt_binds_lmem - Should the memory be viewed as LMEM by the GTT?
82  * @mem: struct ttm_resource representing the memory.
83  *
84  * Return: true if memory should be viewed as LMEM for GTT binding purposes,
85  * false otherwise.
86  */
87 static inline bool i915_ttm_gtt_binds_lmem(struct ttm_resource *mem)
88 {
89 	return mem->mem_type != I915_PL_SYSTEM;
90 }
91 
92 /**
93  * i915_ttm_cpu_maps_iomem - Should the memory be viewed as IOMEM by the CPU?
94  * @mem: struct ttm_resource representing the memory.
95  *
96  * Return: true if memory should be viewed as IOMEM for CPU mapping purposes.
97  */
98 static inline bool i915_ttm_cpu_maps_iomem(struct ttm_resource *mem)
99 {
100 	/* Once / if we support GGTT, this is also false for cached ttm_tts */
101 	return mem && mem->mem_type != I915_PL_SYSTEM;
102 }
103 
104 bool i915_ttm_resource_mappable(struct ttm_resource *res);
105 
106 #endif
107