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 
27a40cfa0bSTom St Denis #include "amdgpu.h"
281b1f42d8SLucas Stach #include <drm/gpu_scheduler.h>
29c632d799SFlora Cui 
30283cde69SChristian König #define AMDGPU_PL_GDS		(TTM_PL_PRIV + 0)
31283cde69SChristian König #define AMDGPU_PL_GWS		(TTM_PL_PRIV + 1)
32283cde69SChristian König #define AMDGPU_PL_OA		(TTM_PL_PRIV + 2)
33c632d799SFlora Cui 
34283cde69SChristian König #define AMDGPU_PL_FLAG_GDS		(TTM_PL_FLAG_PRIV << 0)
35283cde69SChristian König #define AMDGPU_PL_FLAG_GWS		(TTM_PL_FLAG_PRIV << 1)
36283cde69SChristian König #define AMDGPU_PL_FLAG_OA		(TTM_PL_FLAG_PRIV << 2)
37c632d799SFlora Cui 
38cc25188aSChristian König #define AMDGPU_GTT_MAX_TRANSFER_SIZE	512
39cc25188aSChristian König #define AMDGPU_GTT_NUM_TRANSFER_WINDOWS	2
40cc25188aSChristian König 
41c632d799SFlora Cui struct amdgpu_mman {
42c632d799SFlora Cui 	struct ttm_bo_device		bdev;
43c632d799SFlora Cui 	bool				mem_global_referenced;
44c632d799SFlora Cui 	bool				initialized;
45f8f4b9a6SAmber Lin 	void __iomem			*aper_base_kaddr;
46c632d799SFlora Cui 
47c632d799SFlora Cui #if defined(CONFIG_DEBUG_FS)
48a40cfa0bSTom St Denis 	struct dentry			*debugfs_entries[8];
49c632d799SFlora Cui #endif
50c632d799SFlora Cui 
51c632d799SFlora Cui 	/* buffer handling */
52c632d799SFlora Cui 	const struct amdgpu_buffer_funcs	*buffer_funcs;
53c632d799SFlora Cui 	struct amdgpu_ring			*buffer_funcs_ring;
5481988f9cSChristian König 	bool					buffer_funcs_enabled;
55abca90f1SChristian König 
56abca90f1SChristian König 	struct mutex				gtt_window_lock;
57c632d799SFlora Cui 	/* Scheduler entity for buffer moves */
581b1f42d8SLucas Stach 	struct drm_sched_entity			entity;
59c632d799SFlora Cui };
60c632d799SFlora Cui 
611eca5a53SHarish Kasiviswanathan struct amdgpu_copy_mem {
621eca5a53SHarish Kasiviswanathan 	struct ttm_buffer_object	*bo;
631eca5a53SHarish Kasiviswanathan 	struct ttm_mem_reg		*mem;
641eca5a53SHarish Kasiviswanathan 	unsigned long			offset;
651eca5a53SHarish Kasiviswanathan };
661eca5a53SHarish Kasiviswanathan 
67bb990bb0SChristian König extern const struct ttm_mem_type_manager_func amdgpu_gtt_mgr_func;
686a7f76e7SChristian König extern const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func;
69bb990bb0SChristian König 
703da917b6SChristian König bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_mem_reg *mem);
719255d77dSChristian König uint64_t amdgpu_gtt_mgr_usage(struct ttm_mem_type_manager *man);
72c1c7ce8fSChristian König int amdgpu_gtt_mgr_recover(struct ttm_mem_type_manager *man);
73bb990bb0SChristian König 
74ddc21af4SMichel Dänzer u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo);
753c848bb3SChristian König uint64_t amdgpu_vram_mgr_usage(struct ttm_mem_type_manager *man);
763c848bb3SChristian König uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_mem_type_manager *man);
773c848bb3SChristian König 
78c396ef9bSChristian König int amdgpu_ttm_init(struct amdgpu_device *adev);
796f752ec2SAndrey Grodzovsky void amdgpu_ttm_late_init(struct amdgpu_device *adev);
80c396ef9bSChristian König void amdgpu_ttm_fini(struct amdgpu_device *adev);
8157adc4ceSChristian König void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev,
8257adc4ceSChristian König 					bool enable);
83c396ef9bSChristian König 
84fc9c8f54SChristian König int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
85fc9c8f54SChristian König 		       uint64_t dst_offset, uint32_t byte_count,
86c632d799SFlora Cui 		       struct reservation_object *resv,
87fc9c8f54SChristian König 		       struct dma_fence **fence, bool direct_submit,
88fc9c8f54SChristian König 		       bool vm_needs_flush);
891eca5a53SHarish Kasiviswanathan int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
901eca5a53SHarish Kasiviswanathan 			       struct amdgpu_copy_mem *src,
911eca5a53SHarish Kasiviswanathan 			       struct amdgpu_copy_mem *dst,
921eca5a53SHarish Kasiviswanathan 			       uint64_t size,
931eca5a53SHarish Kasiviswanathan 			       struct reservation_object *resv,
941eca5a53SHarish Kasiviswanathan 			       struct dma_fence **f);
95c632d799SFlora Cui int amdgpu_fill_buffer(struct amdgpu_bo *bo,
9644e1baebSChristian König 			uint32_t src_data,
97c632d799SFlora Cui 			struct reservation_object *resv,
98f54d1867SChris Wilson 			struct dma_fence **fence);
99c632d799SFlora Cui 
100c632d799SFlora Cui int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma);
101c5835bbbSChristian König int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
102c1c7ce8fSChristian König int amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
103c855e250SChristian König 
104*ad595b86SPhilip Yang #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
105711becf0SChristian König int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages);
106899fbde1SPhilip Yang bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm);
107*ad595b86SPhilip Yang #else
108*ad595b86SPhilip Yang static inline int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
109*ad595b86SPhilip Yang {
110*ad595b86SPhilip Yang 	return -EPERM;
111*ad595b86SPhilip Yang }
112*ad595b86SPhilip Yang static inline bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm)
113*ad595b86SPhilip Yang {
114*ad595b86SPhilip Yang 	return false;
115*ad595b86SPhilip Yang }
116*ad595b86SPhilip Yang #endif
117*ad595b86SPhilip Yang 
118711becf0SChristian König void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages);
119711becf0SChristian König int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
120711becf0SChristian König 				     uint32_t flags);
121711becf0SChristian König bool amdgpu_ttm_tt_has_userptr(struct ttm_tt *ttm);
122711becf0SChristian König struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm);
123711becf0SChristian König bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
124711becf0SChristian König 				  unsigned long end);
125711becf0SChristian König bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
126711becf0SChristian König 				       int *last_invalidated);
127899fbde1SPhilip Yang bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
128711becf0SChristian König bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
12924a8d289SChristian König uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_mem_reg *mem);
130711becf0SChristian König uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
131711becf0SChristian König 				 struct ttm_mem_reg *mem);
132711becf0SChristian König 
133c632d799SFlora Cui #endif
134