1c632d799SFlora Cui /*
2c632d799SFlora Cui  * Copyright 2016 Advanced Micro Devices, Inc.
3c632d799SFlora Cui  *
4c632d799SFlora Cui  * Permission is hereby granted, free of charge, to any person obtaining a
5c632d799SFlora Cui  * copy of this software and associated documentation files (the "Software"),
6c632d799SFlora Cui  * to deal in the Software without restriction, including without limitation
7c632d799SFlora Cui  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c632d799SFlora Cui  * and/or sell copies of the Software, and to permit persons to whom the
9c632d799SFlora Cui  * Software is furnished to do so, subject to the following conditions:
10c632d799SFlora Cui  *
11c632d799SFlora Cui  * The above copyright notice and this permission notice shall be included in
12c632d799SFlora Cui  * all copies or substantial portions of the Software.
13c632d799SFlora Cui  *
14c632d799SFlora Cui  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15c632d799SFlora Cui  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16c632d799SFlora Cui  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17c632d799SFlora Cui  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18c632d799SFlora Cui  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19c632d799SFlora Cui  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20c632d799SFlora Cui  * OTHER DEALINGS IN THE SOFTWARE.
21c632d799SFlora Cui  *
22c632d799SFlora Cui  */
23c632d799SFlora Cui 
24c632d799SFlora Cui #ifndef __AMDGPU_TTM_H__
25c632d799SFlora Cui #define __AMDGPU_TTM_H__
26c632d799SFlora Cui 
27*f44ffd67SChristian König #include <linux/dma-direction.h>
281b1f42d8SLucas Stach #include <drm/gpu_scheduler.h>
29*f44ffd67SChristian König #include "amdgpu.h"
30c632d799SFlora Cui 
31283cde69SChristian König #define AMDGPU_PL_GDS		(TTM_PL_PRIV + 0)
32283cde69SChristian König #define AMDGPU_PL_GWS		(TTM_PL_PRIV + 1)
33283cde69SChristian König #define AMDGPU_PL_OA		(TTM_PL_PRIV + 2)
34c632d799SFlora Cui 
35283cde69SChristian König #define AMDGPU_PL_FLAG_GDS		(TTM_PL_FLAG_PRIV << 0)
36283cde69SChristian König #define AMDGPU_PL_FLAG_GWS		(TTM_PL_FLAG_PRIV << 1)
37283cde69SChristian König #define AMDGPU_PL_FLAG_OA		(TTM_PL_FLAG_PRIV << 2)
38c632d799SFlora Cui 
39cc25188aSChristian König #define AMDGPU_GTT_MAX_TRANSFER_SIZE	512
40cc25188aSChristian König #define AMDGPU_GTT_NUM_TRANSFER_WINDOWS	2
41cc25188aSChristian König 
42ab2f7a5cSFelix Kuehling #define AMDGPU_POISON	0xd0bed0be
43ab2f7a5cSFelix Kuehling 
44c632d799SFlora Cui struct amdgpu_mman {
45c632d799SFlora Cui 	struct ttm_bo_device		bdev;
46c632d799SFlora Cui 	bool				mem_global_referenced;
47c632d799SFlora Cui 	bool				initialized;
48f8f4b9a6SAmber Lin 	void __iomem			*aper_base_kaddr;
49c632d799SFlora Cui 
50c632d799SFlora Cui #if defined(CONFIG_DEBUG_FS)
51a40cfa0bSTom St Denis 	struct dentry			*debugfs_entries[8];
52c632d799SFlora Cui #endif
53c632d799SFlora Cui 
54c632d799SFlora Cui 	/* buffer handling */
55c632d799SFlora Cui 	const struct amdgpu_buffer_funcs	*buffer_funcs;
56c632d799SFlora Cui 	struct amdgpu_ring			*buffer_funcs_ring;
5781988f9cSChristian König 	bool					buffer_funcs_enabled;
58abca90f1SChristian König 
59abca90f1SChristian König 	struct mutex				gtt_window_lock;
60c632d799SFlora Cui 	/* Scheduler entity for buffer moves */
611b1f42d8SLucas Stach 	struct drm_sched_entity			entity;
62c632d799SFlora Cui };
63c632d799SFlora Cui 
641eca5a53SHarish Kasiviswanathan struct amdgpu_copy_mem {
651eca5a53SHarish Kasiviswanathan 	struct ttm_buffer_object	*bo;
661eca5a53SHarish Kasiviswanathan 	struct ttm_mem_reg		*mem;
671eca5a53SHarish Kasiviswanathan 	unsigned long			offset;
681eca5a53SHarish Kasiviswanathan };
691eca5a53SHarish Kasiviswanathan 
70bb990bb0SChristian König extern const struct ttm_mem_type_manager_func amdgpu_gtt_mgr_func;
716a7f76e7SChristian König extern const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func;
72bb990bb0SChristian König 
733da917b6SChristian König bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_mem_reg *mem);
749255d77dSChristian König uint64_t amdgpu_gtt_mgr_usage(struct ttm_mem_type_manager *man);
75c1c7ce8fSChristian König int amdgpu_gtt_mgr_recover(struct ttm_mem_type_manager *man);
76bb990bb0SChristian König 
77ddc21af4SMichel Dänzer u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo);
78*f44ffd67SChristian König int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
79*f44ffd67SChristian König 			      struct ttm_mem_reg *mem,
80*f44ffd67SChristian König 			      struct device *dev,
81*f44ffd67SChristian König 			      enum dma_data_direction dir,
82*f44ffd67SChristian König 			      struct sg_table **sgt);
83*f44ffd67SChristian König void amdgpu_vram_mgr_free_sgt(struct amdgpu_device *adev,
84*f44ffd67SChristian König 			      struct device *dev,
85*f44ffd67SChristian König 			      enum dma_data_direction dir,
86*f44ffd67SChristian König 			      struct sg_table *sgt);
873c848bb3SChristian König uint64_t amdgpu_vram_mgr_usage(struct ttm_mem_type_manager *man);
883c848bb3SChristian König uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_mem_type_manager *man);
893c848bb3SChristian König 
90c396ef9bSChristian König int amdgpu_ttm_init(struct amdgpu_device *adev);
916f752ec2SAndrey Grodzovsky void amdgpu_ttm_late_init(struct amdgpu_device *adev);
92c396ef9bSChristian König void amdgpu_ttm_fini(struct amdgpu_device *adev);
9357adc4ceSChristian König void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev,
9457adc4ceSChristian König 					bool enable);
95c396ef9bSChristian König 
96fc9c8f54SChristian König int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
97fc9c8f54SChristian König 		       uint64_t dst_offset, uint32_t byte_count,
9852791eeeSChristian König 		       struct dma_resv *resv,
99fc9c8f54SChristian König 		       struct dma_fence **fence, bool direct_submit,
100fc9c8f54SChristian König 		       bool vm_needs_flush);
1011eca5a53SHarish Kasiviswanathan int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
1021eca5a53SHarish Kasiviswanathan 			       struct amdgpu_copy_mem *src,
1031eca5a53SHarish Kasiviswanathan 			       struct amdgpu_copy_mem *dst,
1041eca5a53SHarish Kasiviswanathan 			       uint64_t size,
10552791eeeSChristian König 			       struct dma_resv *resv,
1061eca5a53SHarish Kasiviswanathan 			       struct dma_fence **f);
107c632d799SFlora Cui int amdgpu_fill_buffer(struct amdgpu_bo *bo,
10844e1baebSChristian König 			uint32_t src_data,
10952791eeeSChristian König 			struct dma_resv *resv,
110f54d1867SChris Wilson 			struct dma_fence **fence);
111c632d799SFlora Cui 
112c632d799SFlora Cui int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma);
113c5835bbbSChristian König int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
114c1c7ce8fSChristian König int amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
115c855e250SChristian König 
116ad595b86SPhilip Yang #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
117e5eaa7ccSPhilip Yang int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages);
118899fbde1SPhilip Yang bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm);
119ad595b86SPhilip Yang #else
120e5eaa7ccSPhilip Yang static inline int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
121e5eaa7ccSPhilip Yang 					       struct page **pages)
122ad595b86SPhilip Yang {
123ad595b86SPhilip Yang 	return -EPERM;
124ad595b86SPhilip Yang }
125ad595b86SPhilip Yang static inline bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm)
126ad595b86SPhilip Yang {
127ad595b86SPhilip Yang 	return false;
128ad595b86SPhilip Yang }
129ad595b86SPhilip Yang #endif
130ad595b86SPhilip Yang 
131711becf0SChristian König void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages);
132711becf0SChristian König int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
133711becf0SChristian König 				     uint32_t flags);
134711becf0SChristian König bool amdgpu_ttm_tt_has_userptr(struct ttm_tt *ttm);
135711becf0SChristian König struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm);
136711becf0SChristian König bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
137711becf0SChristian König 				  unsigned long end);
138711becf0SChristian König bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
139711becf0SChristian König 				       int *last_invalidated);
140899fbde1SPhilip Yang bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
141711becf0SChristian König bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
14224a8d289SChristian König uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_mem_reg *mem);
143711becf0SChristian König uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
144711becf0SChristian König 				 struct ttm_mem_reg *mem);
145711becf0SChristian König 
146c5820361SAlex Deucher int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
147c5820361SAlex Deucher 
148c632d799SFlora Cui #endif
149