1d38ceaf9SAlex Deucher /* 2d38ceaf9SAlex Deucher * Copyright 2008 Advanced Micro Devices, Inc. 3d38ceaf9SAlex Deucher * Copyright 2008 Red Hat Inc. 4d38ceaf9SAlex Deucher * Copyright 2009 Jerome Glisse. 5d38ceaf9SAlex Deucher * 6d38ceaf9SAlex Deucher * Permission is hereby granted, free of charge, to any person obtaining a 7d38ceaf9SAlex Deucher * copy of this software and associated documentation files (the "Software"), 8d38ceaf9SAlex Deucher * to deal in the Software without restriction, including without limitation 9d38ceaf9SAlex Deucher * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10d38ceaf9SAlex Deucher * and/or sell copies of the Software, and to permit persons to whom the 11d38ceaf9SAlex Deucher * Software is furnished to do so, subject to the following conditions: 12d38ceaf9SAlex Deucher * 13d38ceaf9SAlex Deucher * The above copyright notice and this permission notice shall be included in 14d38ceaf9SAlex Deucher * all copies or substantial portions of the Software. 15d38ceaf9SAlex Deucher * 16d38ceaf9SAlex Deucher * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17d38ceaf9SAlex Deucher * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18d38ceaf9SAlex Deucher * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19d38ceaf9SAlex Deucher * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20d38ceaf9SAlex Deucher * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21d38ceaf9SAlex Deucher * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22d38ceaf9SAlex Deucher * OTHER DEALINGS IN THE SOFTWARE. 23d38ceaf9SAlex Deucher * 24d38ceaf9SAlex Deucher * Authors: Dave Airlie 25d38ceaf9SAlex Deucher * Alex Deucher 26d38ceaf9SAlex Deucher * Jerome Glisse 27d38ceaf9SAlex Deucher */ 28d38ceaf9SAlex Deucher #ifndef __AMDGPU_OBJECT_H__ 29d38ceaf9SAlex Deucher #define __AMDGPU_OBJECT_H__ 30d38ceaf9SAlex Deucher 31d38ceaf9SAlex Deucher #include <drm/amdgpu_drm.h> 32d38ceaf9SAlex Deucher #include "amdgpu.h" 33d38ceaf9SAlex Deucher 349702d40dSChristian König #define AMDGPU_BO_INVALID_OFFSET LONG_MAX 35bf314ca3SChristian König #define AMDGPU_BO_MAX_PLACEMENTS 3 369702d40dSChristian König 37a906dbb1SChunming Zhou struct amdgpu_bo_param { 38a906dbb1SChunming Zhou unsigned long size; 39a906dbb1SChunming Zhou int byte_align; 40a906dbb1SChunming Zhou u32 domain; 41aa2b2e28SChunming Zhou u32 preferred_domain; 42a906dbb1SChunming Zhou u64 flags; 43a906dbb1SChunming Zhou enum ttm_bo_type type; 44a906dbb1SChunming Zhou struct reservation_object *resv; 45a906dbb1SChunming Zhou }; 46a906dbb1SChunming Zhou 47ec681545SChristian König /* bo virtual addresses in a vm */ 489124a398SChristian König struct amdgpu_bo_va_mapping { 49aebc5e6fSChristian König struct amdgpu_bo_va *bo_va; 509124a398SChristian König struct list_head list; 519124a398SChristian König struct rb_node rb; 529124a398SChristian König uint64_t start; 539124a398SChristian König uint64_t last; 549124a398SChristian König uint64_t __subtree_last; 559124a398SChristian König uint64_t offset; 569124a398SChristian König uint64_t flags; 579124a398SChristian König }; 589124a398SChristian König 59ec681545SChristian König /* User space allocated BO in a VM */ 609124a398SChristian König struct amdgpu_bo_va { 61ec681545SChristian König struct amdgpu_vm_bo_base base; 62ec681545SChristian König 639124a398SChristian König /* protected by bo being reserved */ 649124a398SChristian König unsigned ref_count; 659124a398SChristian König 6600b5cc83SChristian König /* all other members protected by the VM PD being reserved */ 6700b5cc83SChristian König struct dma_fence *last_pt_update; 6800b5cc83SChristian König 699124a398SChristian König /* mappings for this bo_va */ 709124a398SChristian König struct list_head invalids; 719124a398SChristian König struct list_head valids; 72cb7b6ec2SChristian König 73cb7b6ec2SChristian König /* If the mappings are cleared or filled */ 74cb7b6ec2SChristian König bool cleared; 75df399b06Sshaoyunl 76df399b06Sshaoyunl bool is_xgmi; 779124a398SChristian König }; 789124a398SChristian König 799124a398SChristian König struct amdgpu_bo { 809124a398SChristian König /* Protected by tbo.reserved */ 816d7d9c5aSKent Russell u32 preferred_domains; 829124a398SChristian König u32 allowed_domains; 83bf314ca3SChristian König struct ttm_place placements[AMDGPU_BO_MAX_PLACEMENTS]; 849124a398SChristian König struct ttm_placement placement; 859124a398SChristian König struct ttm_buffer_object tbo; 869124a398SChristian König struct ttm_bo_kmap_obj kmap; 879124a398SChristian König u64 flags; 889124a398SChristian König unsigned pin_count; 899124a398SChristian König u64 tiling_flags; 909124a398SChristian König u64 metadata_flags; 919124a398SChristian König void *metadata; 929124a398SChristian König u32 metadata_size; 939124a398SChristian König unsigned prime_shared_count; 94646b9025SChristian König /* per VM structure for page tables and with virtual addresses */ 95646b9025SChristian König struct amdgpu_vm_bo_base *vm_bo; 969124a398SChristian König /* Constant after initialization */ 979124a398SChristian König struct drm_gem_object gem_base; 989124a398SChristian König struct amdgpu_bo *parent; 999124a398SChristian König struct amdgpu_bo *shadow; 1009124a398SChristian König 1019124a398SChristian König struct ttm_bo_kmap_obj dma_buf_vmap; 1029124a398SChristian König struct amdgpu_mn *mn; 103ed5b89c6SChristian König 104ed5b89c6SChristian König union { 1059124a398SChristian König struct list_head mn_list; 1069124a398SChristian König struct list_head shadow_list; 1079124a398SChristian König }; 108a46a2cd1SFelix Kuehling 109a46a2cd1SFelix Kuehling struct kgd_mem *kfd_bo; 110ed5b89c6SChristian König }; 1119124a398SChristian König 112b82485fdSAndres Rodriguez static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo) 113b82485fdSAndres Rodriguez { 114b82485fdSAndres Rodriguez return container_of(tbo, struct amdgpu_bo, tbo); 115b82485fdSAndres Rodriguez } 116b82485fdSAndres Rodriguez 117d38ceaf9SAlex Deucher /** 118d38ceaf9SAlex Deucher * amdgpu_mem_type_to_domain - return domain corresponding to mem_type 119d38ceaf9SAlex Deucher * @mem_type: ttm memory type 120d38ceaf9SAlex Deucher * 121d38ceaf9SAlex Deucher * Returns corresponding domain of the ttm mem_type 122d38ceaf9SAlex Deucher */ 123d38ceaf9SAlex Deucher static inline unsigned amdgpu_mem_type_to_domain(u32 mem_type) 124d38ceaf9SAlex Deucher { 125d38ceaf9SAlex Deucher switch (mem_type) { 126d38ceaf9SAlex Deucher case TTM_PL_VRAM: 127d38ceaf9SAlex Deucher return AMDGPU_GEM_DOMAIN_VRAM; 128d38ceaf9SAlex Deucher case TTM_PL_TT: 129d38ceaf9SAlex Deucher return AMDGPU_GEM_DOMAIN_GTT; 130d38ceaf9SAlex Deucher case TTM_PL_SYSTEM: 131d38ceaf9SAlex Deucher return AMDGPU_GEM_DOMAIN_CPU; 132d38ceaf9SAlex Deucher case AMDGPU_PL_GDS: 133d38ceaf9SAlex Deucher return AMDGPU_GEM_DOMAIN_GDS; 134d38ceaf9SAlex Deucher case AMDGPU_PL_GWS: 135d38ceaf9SAlex Deucher return AMDGPU_GEM_DOMAIN_GWS; 136d38ceaf9SAlex Deucher case AMDGPU_PL_OA: 137d38ceaf9SAlex Deucher return AMDGPU_GEM_DOMAIN_OA; 138d38ceaf9SAlex Deucher default: 139d38ceaf9SAlex Deucher break; 140d38ceaf9SAlex Deucher } 141d38ceaf9SAlex Deucher return 0; 142d38ceaf9SAlex Deucher } 143d38ceaf9SAlex Deucher 144d38ceaf9SAlex Deucher /** 145d38ceaf9SAlex Deucher * amdgpu_bo_reserve - reserve bo 146d38ceaf9SAlex Deucher * @bo: bo structure 147d38ceaf9SAlex Deucher * @no_intr: don't return -ERESTARTSYS on pending signal 148d38ceaf9SAlex Deucher * 149d38ceaf9SAlex Deucher * Returns: 150d38ceaf9SAlex Deucher * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by 151d38ceaf9SAlex Deucher * a signal. Release all buffer reservations and return to user-space. 152d38ceaf9SAlex Deucher */ 153d38ceaf9SAlex Deucher static inline int amdgpu_bo_reserve(struct amdgpu_bo *bo, bool no_intr) 154d38ceaf9SAlex Deucher { 155a7d64de6SChristian König struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 156d38ceaf9SAlex Deucher int r; 157d38ceaf9SAlex Deucher 15855c2e5a1SChristian König r = __ttm_bo_reserve(&bo->tbo, !no_intr, false, NULL); 159d38ceaf9SAlex Deucher if (unlikely(r != 0)) { 160d38ceaf9SAlex Deucher if (r != -ERESTARTSYS) 161a7d64de6SChristian König dev_err(adev->dev, "%p reserve failed\n", bo); 162d38ceaf9SAlex Deucher return r; 163d38ceaf9SAlex Deucher } 164d38ceaf9SAlex Deucher return 0; 165d38ceaf9SAlex Deucher } 166d38ceaf9SAlex Deucher 167d38ceaf9SAlex Deucher static inline void amdgpu_bo_unreserve(struct amdgpu_bo *bo) 168d38ceaf9SAlex Deucher { 169d38ceaf9SAlex Deucher ttm_bo_unreserve(&bo->tbo); 170d38ceaf9SAlex Deucher } 171d38ceaf9SAlex Deucher 172d38ceaf9SAlex Deucher static inline unsigned long amdgpu_bo_size(struct amdgpu_bo *bo) 173d38ceaf9SAlex Deucher { 174d38ceaf9SAlex Deucher return bo->tbo.num_pages << PAGE_SHIFT; 175d38ceaf9SAlex Deucher } 176d38ceaf9SAlex Deucher 177d38ceaf9SAlex Deucher static inline unsigned amdgpu_bo_ngpu_pages(struct amdgpu_bo *bo) 178d38ceaf9SAlex Deucher { 179d38ceaf9SAlex Deucher return (bo->tbo.num_pages << PAGE_SHIFT) / AMDGPU_GPU_PAGE_SIZE; 180d38ceaf9SAlex Deucher } 181d38ceaf9SAlex Deucher 182d38ceaf9SAlex Deucher static inline unsigned amdgpu_bo_gpu_page_alignment(struct amdgpu_bo *bo) 183d38ceaf9SAlex Deucher { 184d38ceaf9SAlex Deucher return (bo->tbo.mem.page_alignment << PAGE_SHIFT) / AMDGPU_GPU_PAGE_SIZE; 185d38ceaf9SAlex Deucher } 186d38ceaf9SAlex Deucher 187d38ceaf9SAlex Deucher /** 188d38ceaf9SAlex Deucher * amdgpu_bo_mmap_offset - return mmap offset of bo 189d38ceaf9SAlex Deucher * @bo: amdgpu object for which we query the offset 190d38ceaf9SAlex Deucher * 191d38ceaf9SAlex Deucher * Returns mmap offset of the object. 192d38ceaf9SAlex Deucher */ 193d38ceaf9SAlex Deucher static inline u64 amdgpu_bo_mmap_offset(struct amdgpu_bo *bo) 194d38ceaf9SAlex Deucher { 195d38ceaf9SAlex Deucher return drm_vma_node_offset_addr(&bo->tbo.vma_node); 196d38ceaf9SAlex Deucher } 197d38ceaf9SAlex Deucher 198b99f3103SNicolai Hähnle /** 1995422a28fSChristian König * amdgpu_bo_in_cpu_visible_vram - check if BO is (partly) in visible VRAM 2005422a28fSChristian König */ 2015422a28fSChristian König static inline bool amdgpu_bo_in_cpu_visible_vram(struct amdgpu_bo *bo) 2025422a28fSChristian König { 2035422a28fSChristian König struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 2045422a28fSChristian König unsigned fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT; 2055422a28fSChristian König struct drm_mm_node *node = bo->tbo.mem.mm_node; 2065422a28fSChristian König unsigned long pages_left; 2075422a28fSChristian König 2085422a28fSChristian König if (bo->tbo.mem.mem_type != TTM_PL_VRAM) 2095422a28fSChristian König return false; 2105422a28fSChristian König 2115422a28fSChristian König for (pages_left = bo->tbo.mem.num_pages; pages_left; 2125422a28fSChristian König pages_left -= node->size, node++) 2135422a28fSChristian König if (node->start < fpfn) 2145422a28fSChristian König return true; 2155422a28fSChristian König 2165422a28fSChristian König return false; 2175422a28fSChristian König } 2185422a28fSChristian König 2195422a28fSChristian König /** 220177ae09bSAndres Rodriguez * amdgpu_bo_explicit_sync - return whether the bo is explicitly synced 221177ae09bSAndres Rodriguez */ 222177ae09bSAndres Rodriguez static inline bool amdgpu_bo_explicit_sync(struct amdgpu_bo *bo) 223177ae09bSAndres Rodriguez { 224177ae09bSAndres Rodriguez return bo->flags & AMDGPU_GEM_CREATE_EXPLICIT_SYNC; 225177ae09bSAndres Rodriguez } 226177ae09bSAndres Rodriguez 227c704ab18SChristian König bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo); 228c704ab18SChristian König void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain); 229c704ab18SChristian König 2303216c6b7SChunming Zhou int amdgpu_bo_create(struct amdgpu_device *adev, 2313216c6b7SChunming Zhou struct amdgpu_bo_param *bp, 232d38ceaf9SAlex Deucher struct amdgpu_bo **bo_ptr); 2339d903cbdSChristian König int amdgpu_bo_create_reserved(struct amdgpu_device *adev, 2349d903cbdSChristian König unsigned long size, int align, 2359d903cbdSChristian König u32 domain, struct amdgpu_bo **bo_ptr, 2369d903cbdSChristian König u64 *gpu_addr, void **cpu_addr); 2377c204889SChristian König int amdgpu_bo_create_kernel(struct amdgpu_device *adev, 2387c204889SChristian König unsigned long size, int align, 2397c204889SChristian König u32 domain, struct amdgpu_bo **bo_ptr, 2407c204889SChristian König u64 *gpu_addr, void **cpu_addr); 241aa1d562eSJunwei Zhang void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr, 242aa1d562eSJunwei Zhang void **cpu_addr); 243d38ceaf9SAlex Deucher int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr); 244f5e1c740SChristian König void *amdgpu_bo_kptr(struct amdgpu_bo *bo); 245d38ceaf9SAlex Deucher void amdgpu_bo_kunmap(struct amdgpu_bo *bo); 246d38ceaf9SAlex Deucher struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo); 247d38ceaf9SAlex Deucher void amdgpu_bo_unref(struct amdgpu_bo **bo); 2487b7c6c81SJunwei Zhang int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain); 249d38ceaf9SAlex Deucher int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain, 2507b7c6c81SJunwei Zhang u64 min_offset, u64 max_offset); 251d38ceaf9SAlex Deucher int amdgpu_bo_unpin(struct amdgpu_bo *bo); 252d38ceaf9SAlex Deucher int amdgpu_bo_evict_vram(struct amdgpu_device *adev); 253d38ceaf9SAlex Deucher int amdgpu_bo_init(struct amdgpu_device *adev); 2546f752ec2SAndrey Grodzovsky int amdgpu_bo_late_init(struct amdgpu_device *adev); 255d38ceaf9SAlex Deucher void amdgpu_bo_fini(struct amdgpu_device *adev); 256d38ceaf9SAlex Deucher int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo, 257d38ceaf9SAlex Deucher struct vm_area_struct *vma); 258d38ceaf9SAlex Deucher int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags); 259d38ceaf9SAlex Deucher void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags); 260d38ceaf9SAlex Deucher int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata, 261d38ceaf9SAlex Deucher uint32_t metadata_size, uint64_t flags); 262d38ceaf9SAlex Deucher int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer, 263d38ceaf9SAlex Deucher size_t buffer_size, uint32_t *metadata_size, 264d38ceaf9SAlex Deucher uint64_t *flags); 265d38ceaf9SAlex Deucher void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, 26666257db7SNicolai Hähnle bool evict, 267d38ceaf9SAlex Deucher struct ttm_mem_reg *new_mem); 268d38ceaf9SAlex Deucher int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo); 269f54d1867SChris Wilson void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence, 270d38ceaf9SAlex Deucher bool shared); 271e8e32426SFelix Kuehling int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr); 272cdb7e8f2SChristian König u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo); 27382521316SRoger.He int amdgpu_bo_validate(struct amdgpu_bo *bo); 274403009bfSChristian König int amdgpu_bo_restore_shadow(struct amdgpu_bo *shadow, 275403009bfSChristian König struct dma_fence **fence); 27684b74608SDeepak Sharma uint32_t amdgpu_bo_get_preferred_pin_domain(struct amdgpu_device *adev, 27784b74608SDeepak Sharma uint32_t domain); 278d38ceaf9SAlex Deucher 279d38ceaf9SAlex Deucher /* 280d38ceaf9SAlex Deucher * sub allocation 281d38ceaf9SAlex Deucher */ 282d38ceaf9SAlex Deucher 283d38ceaf9SAlex Deucher static inline uint64_t amdgpu_sa_bo_gpu_addr(struct amdgpu_sa_bo *sa_bo) 284d38ceaf9SAlex Deucher { 285d38ceaf9SAlex Deucher return sa_bo->manager->gpu_addr + sa_bo->soffset; 286d38ceaf9SAlex Deucher } 287d38ceaf9SAlex Deucher 288d38ceaf9SAlex Deucher static inline void * amdgpu_sa_bo_cpu_addr(struct amdgpu_sa_bo *sa_bo) 289d38ceaf9SAlex Deucher { 290d38ceaf9SAlex Deucher return sa_bo->manager->cpu_ptr + sa_bo->soffset; 291d38ceaf9SAlex Deucher } 292d38ceaf9SAlex Deucher 293d38ceaf9SAlex Deucher int amdgpu_sa_bo_manager_init(struct amdgpu_device *adev, 294d38ceaf9SAlex Deucher struct amdgpu_sa_manager *sa_manager, 295d38ceaf9SAlex Deucher unsigned size, u32 align, u32 domain); 296d38ceaf9SAlex Deucher void amdgpu_sa_bo_manager_fini(struct amdgpu_device *adev, 297d38ceaf9SAlex Deucher struct amdgpu_sa_manager *sa_manager); 298d38ceaf9SAlex Deucher int amdgpu_sa_bo_manager_start(struct amdgpu_device *adev, 299d38ceaf9SAlex Deucher struct amdgpu_sa_manager *sa_manager); 300bbf0b345SJunwei Zhang int amdgpu_sa_bo_new(struct amdgpu_sa_manager *sa_manager, 301d38ceaf9SAlex Deucher struct amdgpu_sa_bo **sa_bo, 302d38ceaf9SAlex Deucher unsigned size, unsigned align); 303d38ceaf9SAlex Deucher void amdgpu_sa_bo_free(struct amdgpu_device *adev, 304d38ceaf9SAlex Deucher struct amdgpu_sa_bo **sa_bo, 305f54d1867SChris Wilson struct dma_fence *fence); 306d38ceaf9SAlex Deucher #if defined(CONFIG_DEBUG_FS) 307d38ceaf9SAlex Deucher void amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager *sa_manager, 308d38ceaf9SAlex Deucher struct seq_file *m); 309d38ceaf9SAlex Deucher #endif 310d38ceaf9SAlex Deucher 311*3d1b8ec7SAndrey Grodzovsky bool amdgpu_bo_support_uswc(u64 bo_flags); 312*3d1b8ec7SAndrey Grodzovsky 313d38ceaf9SAlex Deucher 314d38ceaf9SAlex Deucher #endif 315