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_to_gem - Convert a struct ttm_buffer_object to an embedding
32  * struct drm_i915_gem_object.
33  *
34  * Return: Pointer to the embedding struct ttm_buffer_object, or NULL
35  * if the object was not an i915 ttm object.
36  */
37 static inline struct drm_i915_gem_object *
38 i915_ttm_to_gem(struct ttm_buffer_object *bo)
39 {
40 	if (bo->destroy != i915_ttm_bo_destroy)
41 		return NULL;
42 
43 	return container_of(bo, struct drm_i915_gem_object, __do_not_access);
44 }
45 
46 int __i915_gem_ttm_object_init(struct intel_memory_region *mem,
47 			       struct drm_i915_gem_object *obj,
48 			       resource_size_t size,
49 			       resource_size_t page_size,
50 			       unsigned int flags);
51 
52 /* Internal I915 TTM declarations and definitions below. */
53 
54 #define I915_PL_LMEM0 TTM_PL_PRIV
55 #define I915_PL_SYSTEM TTM_PL_SYSTEM
56 #define I915_PL_STOLEN TTM_PL_VRAM
57 #define I915_PL_GGTT TTM_PL_TT
58 
59 struct ttm_placement *i915_ttm_sys_placement(void);
60 
61 void i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object *obj);
62 
63 struct i915_refct_sgt *
64 i915_ttm_resource_get_st(struct drm_i915_gem_object *obj,
65 			 struct ttm_resource *res);
66 
67 void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj);
68 
69 int i915_ttm_purge(struct drm_i915_gem_object *obj);
70 
71 /**
72  * i915_ttm_gtt_binds_lmem - Should the memory be viewed as LMEM by the GTT?
73  * @mem: struct ttm_resource representing the memory.
74  *
75  * Return: true if memory should be viewed as LMEM for GTT binding purposes,
76  * false otherwise.
77  */
78 static inline bool i915_ttm_gtt_binds_lmem(struct ttm_resource *mem)
79 {
80 	return mem->mem_type != I915_PL_SYSTEM;
81 }
82 
83 /**
84  * i915_ttm_cpu_maps_iomem - Should the memory be viewed as IOMEM by the CPU?
85  * @mem: struct ttm_resource representing the memory.
86  *
87  * Return: true if memory should be viewed as IOMEM for CPU mapping purposes.
88  */
89 static inline bool i915_ttm_cpu_maps_iomem(struct ttm_resource *mem)
90 {
91 	/* Once / if we support GGTT, this is also false for cached ttm_tts */
92 	return mem->mem_type != I915_PL_SYSTEM;
93 }
94 #endif
95