1213d5092SThomas Hellström /* SPDX-License-Identifier: MIT */
2213d5092SThomas Hellström /*
3213d5092SThomas Hellström  * Copyright © 2021 Intel Corporation
4213d5092SThomas Hellström  */
5213d5092SThomas Hellström #ifndef _I915_GEM_TTM_H_
6213d5092SThomas Hellström #define _I915_GEM_TTM_H_
7213d5092SThomas Hellström 
83589fdbdSThomas Hellström #include <drm/ttm/ttm_placement.h>
93589fdbdSThomas Hellström 
10213d5092SThomas Hellström #include "gem/i915_gem_object_types.h"
11213d5092SThomas Hellström 
12213d5092SThomas Hellström /**
13213d5092SThomas Hellström  * i915_gem_to_ttm - Convert a struct drm_i915_gem_object to a
14213d5092SThomas Hellström  * struct ttm_buffer_object.
15213d5092SThomas Hellström  * @obj: Pointer to the gem object.
16213d5092SThomas Hellström  *
17213d5092SThomas Hellström  * Return: Pointer to the embedded struct ttm_buffer_object.
18213d5092SThomas Hellström  */
19213d5092SThomas Hellström static inline struct ttm_buffer_object *
i915_gem_to_ttm(struct drm_i915_gem_object * obj)20213d5092SThomas Hellström i915_gem_to_ttm(struct drm_i915_gem_object *obj)
21213d5092SThomas Hellström {
22213d5092SThomas Hellström 	return &obj->__do_not_access;
23213d5092SThomas Hellström }
24213d5092SThomas Hellström 
25213d5092SThomas Hellström /*
26213d5092SThomas Hellström  * i915 ttm gem object destructor. Internal use only.
27213d5092SThomas Hellström  */
28213d5092SThomas Hellström void i915_ttm_bo_destroy(struct ttm_buffer_object *bo);
29213d5092SThomas Hellström 
30213d5092SThomas Hellström /**
316667d78aSNirmoy Das  * i915_ttm_is_ghost_object - Check if the ttm bo is a ghost object.
326667d78aSNirmoy Das  * @bo: Pointer to the ttm buffer object
336667d78aSNirmoy Das  *
346667d78aSNirmoy Das  * Return: True if the ttm bo is not a i915 object but a ghost ttm object,
356667d78aSNirmoy Das  * False otherwise.
366667d78aSNirmoy Das  */
i915_ttm_is_ghost_object(struct ttm_buffer_object * bo)376667d78aSNirmoy Das static inline bool i915_ttm_is_ghost_object(struct ttm_buffer_object *bo)
386667d78aSNirmoy Das {
396667d78aSNirmoy Das 	return bo->destroy != i915_ttm_bo_destroy;
406667d78aSNirmoy Das }
416667d78aSNirmoy Das 
426667d78aSNirmoy Das /**
43213d5092SThomas Hellström  * i915_ttm_to_gem - Convert a struct ttm_buffer_object to an embedding
44213d5092SThomas Hellström  * struct drm_i915_gem_object.
45*c6948d8cSJani Nikula  * @bo: Pointer to the ttm buffer object
46213d5092SThomas Hellström  *
47*c6948d8cSJani Nikula  * Return: Pointer to the embedding struct drm_i915_gem_object.
48213d5092SThomas Hellström  */
49213d5092SThomas Hellström static inline struct drm_i915_gem_object *
i915_ttm_to_gem(struct ttm_buffer_object * bo)50213d5092SThomas Hellström i915_ttm_to_gem(struct ttm_buffer_object *bo)
51213d5092SThomas Hellström {
52213d5092SThomas Hellström 	return container_of(bo, struct drm_i915_gem_object, __do_not_access);
53213d5092SThomas Hellström }
54213d5092SThomas Hellström 
55213d5092SThomas Hellström int __i915_gem_ttm_object_init(struct intel_memory_region *mem,
56213d5092SThomas Hellström 			       struct drm_i915_gem_object *obj,
579b78b5daSMatthew Auld 			       resource_size_t offset,
58213d5092SThomas Hellström 			       resource_size_t size,
59d22632c8SMatthew Auld 			       resource_size_t page_size,
60213d5092SThomas Hellström 			       unsigned int flags);
610d938863SThomas Hellström 
62c56ce956SThomas Hellström /* Internal I915 TTM declarations and definitions below. */
63c56ce956SThomas Hellström 
64c56ce956SThomas Hellström #define I915_PL_LMEM0 TTM_PL_PRIV
65c56ce956SThomas Hellström #define I915_PL_SYSTEM TTM_PL_SYSTEM
66c56ce956SThomas Hellström #define I915_PL_STOLEN TTM_PL_VRAM
67c56ce956SThomas Hellström #define I915_PL_GGTT TTM_PL_TT
68c56ce956SThomas Hellström 
69c56ce956SThomas Hellström struct ttm_placement *i915_ttm_sys_placement(void);
70c56ce956SThomas Hellström 
713589fdbdSThomas Hellström void i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object *obj);
723589fdbdSThomas Hellström 
733589fdbdSThomas Hellström struct i915_refct_sgt *
743589fdbdSThomas Hellström i915_ttm_resource_get_st(struct drm_i915_gem_object *obj,
753589fdbdSThomas Hellström 			 struct ttm_resource *res);
763589fdbdSThomas Hellström 
773589fdbdSThomas Hellström void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj);
783589fdbdSThomas Hellström 
793589fdbdSThomas Hellström int i915_ttm_purge(struct drm_i915_gem_object *obj);
803589fdbdSThomas Hellström 
813589fdbdSThomas Hellström /**
823589fdbdSThomas Hellström  * i915_ttm_gtt_binds_lmem - Should the memory be viewed as LMEM by the GTT?
833589fdbdSThomas Hellström  * @mem: struct ttm_resource representing the memory.
843589fdbdSThomas Hellström  *
853589fdbdSThomas Hellström  * Return: true if memory should be viewed as LMEM for GTT binding purposes,
863589fdbdSThomas Hellström  * false otherwise.
873589fdbdSThomas Hellström  */
i915_ttm_gtt_binds_lmem(struct ttm_resource * mem)883589fdbdSThomas Hellström static inline bool i915_ttm_gtt_binds_lmem(struct ttm_resource *mem)
893589fdbdSThomas Hellström {
903589fdbdSThomas Hellström 	return mem->mem_type != I915_PL_SYSTEM;
913589fdbdSThomas Hellström }
923589fdbdSThomas Hellström 
933589fdbdSThomas Hellström /**
943589fdbdSThomas Hellström  * i915_ttm_cpu_maps_iomem - Should the memory be viewed as IOMEM by the CPU?
953589fdbdSThomas Hellström  * @mem: struct ttm_resource representing the memory.
963589fdbdSThomas Hellström  *
973589fdbdSThomas Hellström  * Return: true if memory should be viewed as IOMEM for CPU mapping purposes.
983589fdbdSThomas Hellström  */
i915_ttm_cpu_maps_iomem(struct ttm_resource * mem)993589fdbdSThomas Hellström static inline bool i915_ttm_cpu_maps_iomem(struct ttm_resource *mem)
1003589fdbdSThomas Hellström {
1013589fdbdSThomas Hellström 	/* Once / if we support GGTT, this is also false for cached ttm_tts */
10258c7ee06SMatthew Auld 	return mem && mem->mem_type != I915_PL_SYSTEM;
1033589fdbdSThomas Hellström }
104bfe53be2SMatthew Auld 
105bfe53be2SMatthew Auld bool i915_ttm_resource_mappable(struct ttm_resource *res);
106bfe53be2SMatthew Auld 
107213d5092SThomas Hellström #endif
108