1 /* $NetBSD: amdgpu_object.h,v 1.4 2020/02/14 14:34:58 maya Exp $ */ 2 3 /* 4 * Copyright 2008 Advanced Micro Devices, Inc. 5 * Copyright 2008 Red Hat Inc. 6 * Copyright 2009 Jerome Glisse. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 * 26 * Authors: Dave Airlie 27 * Alex Deucher 28 * Jerome Glisse 29 */ 30 #ifndef __AMDGPU_OBJECT_H__ 31 #define __AMDGPU_OBJECT_H__ 32 33 #include <drm/amdgpu_drm.h> 34 #include "amdgpu.h" 35 36 /** 37 * amdgpu_mem_type_to_domain - return domain corresponding to mem_type 38 * @mem_type: ttm memory type 39 * 40 * Returns corresponding domain of the ttm mem_type 41 */ 42 static inline unsigned amdgpu_mem_type_to_domain(u32 mem_type) 43 { 44 switch (mem_type) { 45 case TTM_PL_VRAM: 46 return AMDGPU_GEM_DOMAIN_VRAM; 47 case TTM_PL_TT: 48 return AMDGPU_GEM_DOMAIN_GTT; 49 case TTM_PL_SYSTEM: 50 return AMDGPU_GEM_DOMAIN_CPU; 51 case AMDGPU_PL_GDS: 52 return AMDGPU_GEM_DOMAIN_GDS; 53 case AMDGPU_PL_GWS: 54 return AMDGPU_GEM_DOMAIN_GWS; 55 case AMDGPU_PL_OA: 56 return AMDGPU_GEM_DOMAIN_OA; 57 default: 58 break; 59 } 60 return 0; 61 } 62 63 /** 64 * amdgpu_bo_reserve - reserve bo 65 * @bo: bo structure 66 * @no_intr: don't return -ERESTARTSYS on pending signal 67 * 68 * Returns: 69 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by 70 * a signal. Release all buffer reservations and return to user-space. 71 */ 72 static inline int amdgpu_bo_reserve(struct amdgpu_bo *bo, bool no_intr) 73 { 74 int r; 75 76 r = ttm_bo_reserve(&bo->tbo, !no_intr, false, false, 0); 77 if (unlikely(r != 0)) { 78 if (r != -ERESTARTSYS) 79 dev_err(bo->adev->dev, "%p reserve failed\n", bo); 80 return r; 81 } 82 return 0; 83 } 84 85 static inline void amdgpu_bo_unreserve(struct amdgpu_bo *bo) 86 { 87 ttm_bo_unreserve(&bo->tbo); 88 } 89 90 /** 91 * amdgpu_bo_gpu_offset - return GPU offset of bo 92 * @bo: amdgpu object for which we query the offset 93 * 94 * Returns current GPU offset of the object. 95 * 96 * Note: object should either be pinned or reserved when calling this 97 * function, it might be useful to add check for this for debugging. 98 */ 99 static inline u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo) 100 { 101 return bo->tbo.offset; 102 } 103 104 static inline unsigned long amdgpu_bo_size(struct amdgpu_bo *bo) 105 { 106 return bo->tbo.num_pages << PAGE_SHIFT; 107 } 108 109 static inline unsigned amdgpu_bo_ngpu_pages(struct amdgpu_bo *bo) 110 { 111 return (bo->tbo.num_pages << PAGE_SHIFT) / AMDGPU_GPU_PAGE_SIZE; 112 } 113 114 static inline unsigned amdgpu_bo_gpu_page_alignment(struct amdgpu_bo *bo) 115 { 116 return (bo->tbo.mem.page_alignment << PAGE_SHIFT) / AMDGPU_GPU_PAGE_SIZE; 117 } 118 119 /** 120 * amdgpu_bo_mmap_offset - return mmap offset of bo 121 * @bo: amdgpu object for which we query the offset 122 * 123 * Returns mmap offset of the object. 124 */ 125 static inline u64 amdgpu_bo_mmap_offset(struct amdgpu_bo *bo) 126 { 127 return drm_vma_node_offset_addr(&bo->tbo.vma_node); 128 } 129 130 int amdgpu_bo_create(struct amdgpu_device *adev, 131 unsigned long size, int byte_align, 132 bool kernel, u32 domain, u64 flags, 133 struct sg_table *sg, 134 struct reservation_object *resv, 135 struct amdgpu_bo **bo_ptr); 136 int amdgpu_bo_create_restricted(struct amdgpu_device *adev, 137 unsigned long size, int byte_align, 138 bool kernel, u32 domain, u64 flags, 139 struct sg_table *sg, 140 struct ttm_placement *placement, 141 struct reservation_object *resv, 142 struct amdgpu_bo **bo_ptr); 143 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr); 144 void amdgpu_bo_kunmap(struct amdgpu_bo *bo); 145 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo); 146 void amdgpu_bo_unref(struct amdgpu_bo **bo); 147 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr); 148 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain, 149 u64 min_offset, u64 max_offset, 150 u64 *gpu_addr); 151 int amdgpu_bo_unpin(struct amdgpu_bo *bo); 152 int amdgpu_bo_evict_vram(struct amdgpu_device *adev); 153 void amdgpu_bo_force_delete(struct amdgpu_device *adev); 154 int amdgpu_bo_init(struct amdgpu_device *adev); 155 void amdgpu_bo_fini(struct amdgpu_device *adev); 156 #ifndef __NetBSD__ 157 int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo, 158 struct vm_area_struct *vma); 159 #endif 160 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags); 161 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags); 162 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata, 163 uint32_t metadata_size, uint64_t flags); 164 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer, 165 size_t buffer_size, uint32_t *metadata_size, 166 uint64_t *flags); 167 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, 168 struct ttm_mem_reg *new_mem); 169 int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo); 170 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct fence *fence, 171 bool shared); 172 173 /* 174 * sub allocation 175 */ 176 177 static inline uint64_t amdgpu_sa_bo_gpu_addr(struct amdgpu_sa_bo *sa_bo) 178 { 179 return sa_bo->manager->gpu_addr + sa_bo->soffset; 180 } 181 182 static inline void * amdgpu_sa_bo_cpu_addr(struct amdgpu_sa_bo *sa_bo) 183 { 184 return (char *)sa_bo->manager->cpu_ptr + sa_bo->soffset; 185 } 186 187 int amdgpu_sa_bo_manager_init(struct amdgpu_device *adev, 188 struct amdgpu_sa_manager *sa_manager, 189 unsigned size, u32 align, u32 domain); 190 void amdgpu_sa_bo_manager_fini(struct amdgpu_device *adev, 191 struct amdgpu_sa_manager *sa_manager); 192 int amdgpu_sa_bo_manager_start(struct amdgpu_device *adev, 193 struct amdgpu_sa_manager *sa_manager); 194 int amdgpu_sa_bo_manager_suspend(struct amdgpu_device *adev, 195 struct amdgpu_sa_manager *sa_manager); 196 int amdgpu_sa_bo_new(struct amdgpu_sa_manager *sa_manager, 197 struct amdgpu_sa_bo **sa_bo, 198 unsigned size, unsigned align); 199 void amdgpu_sa_bo_free(struct amdgpu_device *adev, 200 struct amdgpu_sa_bo **sa_bo, 201 struct fence *fence); 202 #if defined(CONFIG_DEBUG_FS) 203 void amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager *sa_manager, 204 struct seq_file *m); 205 #endif 206 207 208 #endif 209