xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h (revision 38f7e5450ebfc6f2e046a249a3f629ea7bec8c31)
1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef __AMDGPU_TTM_H__
25 #define __AMDGPU_TTM_H__
26 
27 #include <linux/dma-direction.h>
28 #include <drm/gpu_scheduler.h>
29 #include <drm/ttm/ttm_placement.h>
30 #include "amdgpu_vram_mgr.h"
31 #include "amdgpu_hmm.h"
32 #include "amdgpu_gmc.h"
33 
34 #define AMDGPU_PL_GDS		(TTM_PL_PRIV + 0)
35 #define AMDGPU_PL_GWS		(TTM_PL_PRIV + 1)
36 #define AMDGPU_PL_OA		(TTM_PL_PRIV + 2)
37 #define AMDGPU_PL_PREEMPT	(TTM_PL_PRIV + 3)
38 #define AMDGPU_PL_DOORBELL	(TTM_PL_PRIV + 4)
39 #define AMDGPU_PL_MMIO_REMAP	(TTM_PL_PRIV + 5)
40 #define __AMDGPU_PL_NUM	(TTM_PL_PRIV + 6)
41 
42 #define AMDGPU_GTT_MAX_TRANSFER_SIZE	1024
43 
44 extern const struct attribute_group amdgpu_vram_mgr_attr_group;
45 extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
46 
47 struct hmm_range;
48 
49 struct amdgpu_gtt_mgr {
50 	struct ttm_resource_manager manager;
51 	struct drm_mm mm;
52 	spinlock_t lock;
53 };
54 
55 struct amdgpu_ttm_buffer_entity {
56 	struct drm_sched_entity base;
57 	struct mutex		lock;
58 	struct drm_mm_node	gart_node;
59 	u64			gart_window_offs[2];
60 };
61 
62 struct amdgpu_mman {
63 	struct ttm_device		bdev;
64 	struct ttm_pool			*ttm_pools;
65 	bool				initialized;
66 	void __iomem			*aper_base_kaddr;
67 
68 	/* buffer handling */
69 	const struct amdgpu_buffer_funcs	*buffer_funcs;
70 	struct amdgpu_ring			*buffer_funcs_ring;
71 	bool					buffer_funcs_enabled;
72 
73 	/* @default_entity: for workarounds, has no gart windows */
74 	struct amdgpu_ttm_buffer_entity default_entity;
75 	struct amdgpu_ttm_buffer_entity clear_entity;
76 	struct amdgpu_ttm_buffer_entity move_entity;
77 
78 	struct amdgpu_vram_mgr vram_mgr;
79 	struct amdgpu_gtt_mgr gtt_mgr;
80 	struct ttm_resource_manager preempt_mgr;
81 
82 	uint64_t		stolen_vga_size;
83 	struct amdgpu_bo	*stolen_vga_memory;
84 	uint64_t		stolen_extended_size;
85 	struct amdgpu_bo	*stolen_extended_memory;
86 	bool			keep_stolen_vga_memory;
87 
88 	struct amdgpu_bo	*stolen_reserved_memory;
89 	uint64_t		stolen_reserved_offset;
90 	uint64_t		stolen_reserved_size;
91 
92 	/* fw reserved memory */
93 	struct amdgpu_bo		*fw_reserved_memory;
94 	struct amdgpu_bo		*fw_reserved_memory_extend;
95 
96 	/* firmware VRAM reservation */
97 	u64		fw_vram_usage_start_offset;
98 	u64		fw_vram_usage_size;
99 	struct amdgpu_bo	*fw_vram_usage_reserved_bo;
100 	void		*fw_vram_usage_va;
101 
102 	/* driver VRAM reservation */
103 	u64		drv_vram_usage_start_offset;
104 	u64		drv_vram_usage_size;
105 	struct amdgpu_bo	*drv_vram_usage_reserved_bo;
106 	void		*drv_vram_usage_va;
107 
108 	/* PAGE_SIZE'd BO for process memory r/w over SDMA. */
109 	struct amdgpu_bo	*sdma_access_bo;
110 	void			*sdma_access_ptr;
111 };
112 
113 struct amdgpu_copy_mem {
114 	struct ttm_buffer_object	*bo;
115 	struct ttm_resource		*mem;
116 	unsigned long			offset;
117 };
118 
119 #define AMDGPU_COPY_FLAGS_TMZ		(1 << 0)
120 #define AMDGPU_COPY_FLAGS_READ_DECOMPRESSED	(1 << 1)
121 #define AMDGPU_COPY_FLAGS_WRITE_COMPRESSED	(1 << 2)
122 #define AMDGPU_COPY_FLAGS_MAX_COMPRESSED_SHIFT		3
123 #define AMDGPU_COPY_FLAGS_MAX_COMPRESSED_MASK		0x03
124 #define AMDGPU_COPY_FLAGS_NUMBER_TYPE_SHIFT		5
125 #define AMDGPU_COPY_FLAGS_NUMBER_TYPE_MASK		0x07
126 #define AMDGPU_COPY_FLAGS_DATA_FORMAT_SHIFT		8
127 #define AMDGPU_COPY_FLAGS_DATA_FORMAT_MASK		0x3f
128 #define AMDGPU_COPY_FLAGS_WRITE_COMPRESS_DISABLE_SHIFT	14
129 #define AMDGPU_COPY_FLAGS_WRITE_COMPRESS_DISABLE_MASK	0x1
130 
131 #define AMDGPU_COPY_FLAGS_SET(field, value) \
132 	(((__u32)(value) & AMDGPU_COPY_FLAGS_##field##_MASK) << AMDGPU_COPY_FLAGS_##field##_SHIFT)
133 #define AMDGPU_COPY_FLAGS_GET(value, field) \
134 	(((__u32)(value) >> AMDGPU_COPY_FLAGS_##field##_SHIFT) & AMDGPU_COPY_FLAGS_##field##_MASK)
135 
136 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size);
137 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev);
138 int amdgpu_preempt_mgr_init(struct amdgpu_device *adev);
139 void amdgpu_preempt_mgr_fini(struct amdgpu_device *adev);
140 int amdgpu_vram_mgr_init(struct amdgpu_device *adev);
141 void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
142 
143 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
144 void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
145 
146 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
147 				 struct drm_mm_node *mm_node,
148 				 u64 num_pages,
149 				 enum drm_mm_insert_mode mode);
150 void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
151 				 struct drm_mm_node *mm_node);
152 uint64_t amdgpu_preempt_mgr_usage(struct ttm_resource_manager *man);
153 
154 u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo);
155 int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
156 			      struct ttm_resource *mem,
157 			      u64 offset, u64 size,
158 			      struct device *dev,
159 			      enum dma_data_direction dir,
160 			      struct sg_table **sgt);
161 void amdgpu_vram_mgr_free_sgt(struct device *dev,
162 			      enum dma_data_direction dir,
163 			      struct sg_table *sgt);
164 uint64_t amdgpu_vram_mgr_vis_usage(struct amdgpu_vram_mgr *mgr);
165 int amdgpu_vram_mgr_reserve_range(struct amdgpu_vram_mgr *mgr,
166 				  uint64_t start, uint64_t size);
167 int amdgpu_vram_mgr_query_page_status(struct amdgpu_vram_mgr *mgr,
168 				      uint64_t start);
169 void amdgpu_vram_mgr_clear_reset_blocks(struct amdgpu_device *adev);
170 
171 bool amdgpu_res_cpu_visible(struct amdgpu_device *adev,
172 			    struct ttm_resource *res);
173 
174 int amdgpu_ttm_init(struct amdgpu_device *adev);
175 void amdgpu_ttm_fini(struct amdgpu_device *adev);
176 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev,
177 					bool enable);
178 int amdgpu_copy_buffer(struct amdgpu_device *adev,
179 		       struct amdgpu_ttm_buffer_entity *entity,
180 		       uint64_t src_offset,
181 		       uint64_t dst_offset, uint32_t byte_count,
182 		       struct dma_resv *resv,
183 		       struct dma_fence **fence,
184 		       bool vm_needs_flush, uint32_t copy_flags);
185 int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
186 			    struct dma_resv *resv,
187 			    struct dma_fence **fence);
188 int amdgpu_fill_buffer(struct amdgpu_ttm_buffer_entity *entity,
189 		       struct amdgpu_bo *bo,
190 		       uint32_t src_data,
191 		       struct dma_resv *resv,
192 		       struct dma_fence **f,
193 		       u64 k_job_id);
194 
195 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
196 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
197 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
198 
199 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
200 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
201 				 struct amdgpu_hmm_range *range);
202 #else
203 static inline int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
204 					       struct amdgpu_hmm_range *range)
205 {
206 	return -EPERM;
207 }
208 #endif
209 
210 /**
211  * amdgpu_compute_gart_address() - Returns GART address of an entity's window
212  * @gmc: The &struct amdgpu_gmc instance to use
213  * @entity: The &struct amdgpu_ttm_buffer_entity owning the GART window
214  * @index: The window to use (must be 0 or 1)
215  */
216 static inline u64 amdgpu_compute_gart_address(struct amdgpu_gmc *gmc,
217 					      struct amdgpu_ttm_buffer_entity *entity,
218 					      int index)
219 {
220 	return gmc->gart_start + entity->gart_window_offs[index];
221 }
222 
223 /**
224  * amdgpu_gtt_node_to_byte_offset() - Returns a byte offset of a gtt node
225  */
226 static inline u64 amdgpu_gtt_node_to_byte_offset(const struct drm_mm_node *gtt_node)
227 {
228 	return gtt_node->start * (u64)PAGE_SIZE;
229 }
230 
231 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct amdgpu_hmm_range *range);
232 int amdgpu_ttm_tt_get_userptr(const struct ttm_buffer_object *tbo,
233 			      uint64_t *user_addr);
234 int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
235 			      uint64_t addr, uint32_t flags);
236 bool amdgpu_ttm_tt_has_userptr(struct ttm_tt *ttm);
237 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm);
238 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
239 				  unsigned long end, unsigned long *userptr);
240 bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
241 				       int *last_invalidated);
242 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
243 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
244 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem);
245 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
246 				 struct ttm_resource *mem);
247 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type);
248 
249 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
250 
251 int amdgpu_ttm_mmio_remap_alloc_sgt(struct amdgpu_device *adev,
252 				    struct ttm_resource *res,
253 				    struct device *dev,
254 				    enum dma_data_direction dir,
255 				    struct sg_table **sgt);
256 void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
257 				    enum dma_data_direction dir,
258 				    struct sg_table *sgt);
259 
260 #endif
261