18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2016 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#ifndef __AMDGPU_TTM_H__
258c2ecf20Sopenharmony_ci#define __AMDGPU_TTM_H__
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/dma-direction.h>
288c2ecf20Sopenharmony_ci#include <drm/gpu_scheduler.h>
298c2ecf20Sopenharmony_ci#include "amdgpu.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define AMDGPU_PL_GDS		(TTM_PL_PRIV + 0)
328c2ecf20Sopenharmony_ci#define AMDGPU_PL_GWS		(TTM_PL_PRIV + 1)
338c2ecf20Sopenharmony_ci#define AMDGPU_PL_OA		(TTM_PL_PRIV + 2)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define AMDGPU_GTT_MAX_TRANSFER_SIZE	512
368c2ecf20Sopenharmony_ci#define AMDGPU_GTT_NUM_TRANSFER_WINDOWS	2
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define AMDGPU_POISON	0xd0bed0be
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistruct amdgpu_vram_mgr {
418c2ecf20Sopenharmony_ci	struct ttm_resource_manager manager;
428c2ecf20Sopenharmony_ci	struct drm_mm mm;
438c2ecf20Sopenharmony_ci	spinlock_t lock;
448c2ecf20Sopenharmony_ci	atomic64_t usage;
458c2ecf20Sopenharmony_ci	atomic64_t vis_usage;
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct amdgpu_gtt_mgr {
498c2ecf20Sopenharmony_ci	struct ttm_resource_manager manager;
508c2ecf20Sopenharmony_ci	struct drm_mm mm;
518c2ecf20Sopenharmony_ci	spinlock_t lock;
528c2ecf20Sopenharmony_ci	atomic64_t available;
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct amdgpu_mman {
568c2ecf20Sopenharmony_ci	struct ttm_bo_device		bdev;
578c2ecf20Sopenharmony_ci	bool				mem_global_referenced;
588c2ecf20Sopenharmony_ci	bool				initialized;
598c2ecf20Sopenharmony_ci	void __iomem			*aper_base_kaddr;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
628c2ecf20Sopenharmony_ci	struct dentry			*debugfs_entries[8];
638c2ecf20Sopenharmony_ci#endif
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/* buffer handling */
668c2ecf20Sopenharmony_ci	const struct amdgpu_buffer_funcs	*buffer_funcs;
678c2ecf20Sopenharmony_ci	struct amdgpu_ring			*buffer_funcs_ring;
688c2ecf20Sopenharmony_ci	bool					buffer_funcs_enabled;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	struct mutex				gtt_window_lock;
718c2ecf20Sopenharmony_ci	/* Scheduler entity for buffer moves */
728c2ecf20Sopenharmony_ci	struct drm_sched_entity			entity;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	struct amdgpu_vram_mgr vram_mgr;
758c2ecf20Sopenharmony_ci	struct amdgpu_gtt_mgr gtt_mgr;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	uint64_t		stolen_vga_size;
788c2ecf20Sopenharmony_ci	struct amdgpu_bo	*stolen_vga_memory;
798c2ecf20Sopenharmony_ci	uint64_t		stolen_extended_size;
808c2ecf20Sopenharmony_ci	struct amdgpu_bo	*stolen_extended_memory;
818c2ecf20Sopenharmony_ci	bool			keep_stolen_vga_memory;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* discovery */
848c2ecf20Sopenharmony_ci	uint8_t				*discovery_bin;
858c2ecf20Sopenharmony_ci	uint32_t			discovery_tmr_size;
868c2ecf20Sopenharmony_ci	struct amdgpu_bo		*discovery_memory;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/* firmware VRAM reservation */
898c2ecf20Sopenharmony_ci	u64		fw_vram_usage_start_offset;
908c2ecf20Sopenharmony_ci	u64		fw_vram_usage_size;
918c2ecf20Sopenharmony_ci	struct amdgpu_bo	*fw_vram_usage_reserved_bo;
928c2ecf20Sopenharmony_ci	void		*fw_vram_usage_va;
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistruct amdgpu_copy_mem {
968c2ecf20Sopenharmony_ci	struct ttm_buffer_object	*bo;
978c2ecf20Sopenharmony_ci	struct ttm_resource		*mem;
988c2ecf20Sopenharmony_ci	unsigned long			offset;
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciint amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size);
1028c2ecf20Sopenharmony_civoid amdgpu_gtt_mgr_fini(struct amdgpu_device *adev);
1038c2ecf20Sopenharmony_ciint amdgpu_vram_mgr_init(struct amdgpu_device *adev);
1048c2ecf20Sopenharmony_civoid amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cibool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
1078c2ecf20Sopenharmony_ciuint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man);
1088c2ecf20Sopenharmony_ciint amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciu64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo);
1118c2ecf20Sopenharmony_ciint amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
1128c2ecf20Sopenharmony_ci			      struct ttm_resource *mem,
1138c2ecf20Sopenharmony_ci			      struct device *dev,
1148c2ecf20Sopenharmony_ci			      enum dma_data_direction dir,
1158c2ecf20Sopenharmony_ci			      struct sg_table **sgt);
1168c2ecf20Sopenharmony_civoid amdgpu_vram_mgr_free_sgt(struct amdgpu_device *adev,
1178c2ecf20Sopenharmony_ci			      struct device *dev,
1188c2ecf20Sopenharmony_ci			      enum dma_data_direction dir,
1198c2ecf20Sopenharmony_ci			      struct sg_table *sgt);
1208c2ecf20Sopenharmony_ciuint64_t amdgpu_vram_mgr_usage(struct ttm_resource_manager *man);
1218c2ecf20Sopenharmony_ciuint64_t amdgpu_vram_mgr_vis_usage(struct ttm_resource_manager *man);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciint amdgpu_ttm_init(struct amdgpu_device *adev);
1248c2ecf20Sopenharmony_civoid amdgpu_ttm_late_init(struct amdgpu_device *adev);
1258c2ecf20Sopenharmony_civoid amdgpu_ttm_fini(struct amdgpu_device *adev);
1268c2ecf20Sopenharmony_civoid amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev,
1278c2ecf20Sopenharmony_ci					bool enable);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciint amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1308c2ecf20Sopenharmony_ci		       uint64_t dst_offset, uint32_t byte_count,
1318c2ecf20Sopenharmony_ci		       struct dma_resv *resv,
1328c2ecf20Sopenharmony_ci		       struct dma_fence **fence, bool direct_submit,
1338c2ecf20Sopenharmony_ci		       bool vm_needs_flush, bool tmz);
1348c2ecf20Sopenharmony_ciint amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
1358c2ecf20Sopenharmony_ci			       const struct amdgpu_copy_mem *src,
1368c2ecf20Sopenharmony_ci			       const struct amdgpu_copy_mem *dst,
1378c2ecf20Sopenharmony_ci			       uint64_t size, bool tmz,
1388c2ecf20Sopenharmony_ci			       struct dma_resv *resv,
1398c2ecf20Sopenharmony_ci			       struct dma_fence **f);
1408c2ecf20Sopenharmony_ciint amdgpu_fill_buffer(struct amdgpu_bo *bo,
1418c2ecf20Sopenharmony_ci			uint32_t src_data,
1428c2ecf20Sopenharmony_ci			struct dma_resv *resv,
1438c2ecf20Sopenharmony_ci			struct dma_fence **fence);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ciint amdgpu_mmap(struct file *filp, struct vm_area_struct *vma);
1468c2ecf20Sopenharmony_ciint amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
1478c2ecf20Sopenharmony_ciint amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
1488c2ecf20Sopenharmony_ciuint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
1518c2ecf20Sopenharmony_ciint amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages);
1528c2ecf20Sopenharmony_cibool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm);
1538c2ecf20Sopenharmony_ci#else
1548c2ecf20Sopenharmony_cistatic inline int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
1558c2ecf20Sopenharmony_ci					       struct page **pages)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	return -EPERM;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_cistatic inline bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	return false;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci#endif
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_civoid amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages);
1668c2ecf20Sopenharmony_ciint amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
1678c2ecf20Sopenharmony_ci			      uint64_t addr, uint32_t flags);
1688c2ecf20Sopenharmony_cibool amdgpu_ttm_tt_has_userptr(struct ttm_tt *ttm);
1698c2ecf20Sopenharmony_cistruct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm);
1708c2ecf20Sopenharmony_cibool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1718c2ecf20Sopenharmony_ci				  unsigned long end);
1728c2ecf20Sopenharmony_cibool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
1738c2ecf20Sopenharmony_ci				       int *last_invalidated);
1748c2ecf20Sopenharmony_cibool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
1758c2ecf20Sopenharmony_cibool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
1768c2ecf20Sopenharmony_ciuint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem);
1778c2ecf20Sopenharmony_ciuint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1788c2ecf20Sopenharmony_ci				 struct ttm_resource *mem);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciint amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#endif
183