amdgpu_object.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #ifndef __AMDGPU_OBJECT_H__
  29. #define __AMDGPU_OBJECT_H__
  30. #include <drm/amdgpu_drm.h>
  31. #include "amdgpu.h"
  32. #define AMDGPU_BO_INVALID_OFFSET LONG_MAX
  33. /* bo virtual addresses in a vm */
  34. struct amdgpu_bo_va_mapping {
  35. struct list_head list;
  36. struct rb_node rb;
  37. uint64_t start;
  38. uint64_t last;
  39. uint64_t __subtree_last;
  40. uint64_t offset;
  41. uint64_t flags;
  42. };
  43. /* User space allocated BO in a VM */
  44. struct amdgpu_bo_va {
  45. struct amdgpu_vm_bo_base base;
  46. /* protected by bo being reserved */
  47. struct dma_fence *last_pt_update;
  48. unsigned ref_count;
  49. /* mappings for this bo_va */
  50. struct list_head invalids;
  51. struct list_head valids;
  52. };
  53. struct amdgpu_bo {
  54. /* Protected by tbo.reserved */
  55. u32 preferred_domains;
  56. u32 allowed_domains;
  57. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  58. struct ttm_placement placement;
  59. struct ttm_buffer_object tbo;
  60. struct ttm_bo_kmap_obj kmap;
  61. u64 flags;
  62. unsigned pin_count;
  63. u64 tiling_flags;
  64. u64 metadata_flags;
  65. void *metadata;
  66. u32 metadata_size;
  67. unsigned prime_shared_count;
  68. /* list of all virtual address to which this bo is associated to */
  69. struct list_head va;
  70. /* Constant after initialization */
  71. struct drm_gem_object gem_base;
  72. struct amdgpu_bo *parent;
  73. struct amdgpu_bo *shadow;
  74. struct ttm_bo_kmap_obj dma_buf_vmap;
  75. struct amdgpu_mn *mn;
  76. union {
  77. struct list_head mn_list;
  78. struct list_head shadow_list;
  79. };
  80. };
  81. /**
  82. * amdgpu_mem_type_to_domain - return domain corresponding to mem_type
  83. * @mem_type: ttm memory type
  84. *
  85. * Returns corresponding domain of the ttm mem_type
  86. */
  87. static inline unsigned amdgpu_mem_type_to_domain(u32 mem_type)
  88. {
  89. switch (mem_type) {
  90. case TTM_PL_VRAM:
  91. return AMDGPU_GEM_DOMAIN_VRAM;
  92. case TTM_PL_TT:
  93. return AMDGPU_GEM_DOMAIN_GTT;
  94. case TTM_PL_SYSTEM:
  95. return AMDGPU_GEM_DOMAIN_CPU;
  96. case AMDGPU_PL_GDS:
  97. return AMDGPU_GEM_DOMAIN_GDS;
  98. case AMDGPU_PL_GWS:
  99. return AMDGPU_GEM_DOMAIN_GWS;
  100. case AMDGPU_PL_OA:
  101. return AMDGPU_GEM_DOMAIN_OA;
  102. default:
  103. break;
  104. }
  105. return 0;
  106. }
  107. /**
  108. * amdgpu_bo_reserve - reserve bo
  109. * @bo: bo structure
  110. * @no_intr: don't return -ERESTARTSYS on pending signal
  111. *
  112. * Returns:
  113. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  114. * a signal. Release all buffer reservations and return to user-space.
  115. */
  116. static inline int amdgpu_bo_reserve(struct amdgpu_bo *bo, bool no_intr)
  117. {
  118. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  119. int r;
  120. r = ttm_bo_reserve(&bo->tbo, !no_intr, false, NULL);
  121. if (unlikely(r != 0)) {
  122. if (r != -ERESTARTSYS)
  123. dev_err(adev->dev, "%p reserve failed\n", bo);
  124. return r;
  125. }
  126. return 0;
  127. }
  128. static inline void amdgpu_bo_unreserve(struct amdgpu_bo *bo)
  129. {
  130. ttm_bo_unreserve(&bo->tbo);
  131. }
  132. static inline unsigned long amdgpu_bo_size(struct amdgpu_bo *bo)
  133. {
  134. return bo->tbo.num_pages << PAGE_SHIFT;
  135. }
  136. static inline unsigned amdgpu_bo_ngpu_pages(struct amdgpu_bo *bo)
  137. {
  138. return (bo->tbo.num_pages << PAGE_SHIFT) / AMDGPU_GPU_PAGE_SIZE;
  139. }
  140. static inline unsigned amdgpu_bo_gpu_page_alignment(struct amdgpu_bo *bo)
  141. {
  142. return (bo->tbo.mem.page_alignment << PAGE_SHIFT) / AMDGPU_GPU_PAGE_SIZE;
  143. }
  144. /**
  145. * amdgpu_bo_mmap_offset - return mmap offset of bo
  146. * @bo: amdgpu object for which we query the offset
  147. *
  148. * Returns mmap offset of the object.
  149. */
  150. static inline u64 amdgpu_bo_mmap_offset(struct amdgpu_bo *bo)
  151. {
  152. return drm_vma_node_offset_addr(&bo->tbo.vma_node);
  153. }
  154. /**
  155. * amdgpu_bo_gpu_accessible - return whether the bo is currently in memory that
  156. * is accessible to the GPU.
  157. */
  158. static inline bool amdgpu_bo_gpu_accessible(struct amdgpu_bo *bo)
  159. {
  160. switch (bo->tbo.mem.mem_type) {
  161. case TTM_PL_TT: return amdgpu_ttm_is_bound(bo->tbo.ttm);
  162. case TTM_PL_VRAM: return true;
  163. default: return false;
  164. }
  165. }
  166. int amdgpu_bo_create(struct amdgpu_device *adev,
  167. unsigned long size, int byte_align,
  168. bool kernel, u32 domain, u64 flags,
  169. struct sg_table *sg,
  170. struct reservation_object *resv,
  171. uint64_t init_value,
  172. struct amdgpu_bo **bo_ptr);
  173. int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
  174. unsigned long size, int byte_align,
  175. bool kernel, u32 domain, u64 flags,
  176. struct sg_table *sg,
  177. struct ttm_placement *placement,
  178. struct reservation_object *resv,
  179. uint64_t init_value,
  180. struct amdgpu_bo **bo_ptr);
  181. int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
  182. unsigned long size, int align,
  183. u32 domain, struct amdgpu_bo **bo_ptr,
  184. u64 *gpu_addr, void **cpu_addr);
  185. int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  186. unsigned long size, int align,
  187. u32 domain, struct amdgpu_bo **bo_ptr,
  188. u64 *gpu_addr, void **cpu_addr);
  189. void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  190. void **cpu_addr);
  191. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr);
  192. void *amdgpu_bo_kptr(struct amdgpu_bo *bo);
  193. void amdgpu_bo_kunmap(struct amdgpu_bo *bo);
  194. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo);
  195. void amdgpu_bo_unref(struct amdgpu_bo **bo);
  196. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr);
  197. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  198. u64 min_offset, u64 max_offset,
  199. u64 *gpu_addr);
  200. int amdgpu_bo_unpin(struct amdgpu_bo *bo);
  201. int amdgpu_bo_evict_vram(struct amdgpu_device *adev);
  202. int amdgpu_bo_init(struct amdgpu_device *adev);
  203. void amdgpu_bo_fini(struct amdgpu_device *adev);
  204. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  205. struct vm_area_struct *vma);
  206. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags);
  207. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags);
  208. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  209. uint32_t metadata_size, uint64_t flags);
  210. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  211. size_t buffer_size, uint32_t *metadata_size,
  212. uint64_t *flags);
  213. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  214. bool evict,
  215. struct ttm_mem_reg *new_mem);
  216. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo);
  217. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
  218. bool shared);
  219. u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo);
  220. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  221. struct amdgpu_ring *ring,
  222. struct amdgpu_bo *bo,
  223. struct reservation_object *resv,
  224. struct dma_fence **fence, bool direct);
  225. int amdgpu_bo_validate(struct amdgpu_bo *bo);
  226. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  227. struct amdgpu_ring *ring,
  228. struct amdgpu_bo *bo,
  229. struct reservation_object *resv,
  230. struct dma_fence **fence,
  231. bool direct);
  232. /*
  233. * sub allocation
  234. */
  235. static inline uint64_t amdgpu_sa_bo_gpu_addr(struct amdgpu_sa_bo *sa_bo)
  236. {
  237. return sa_bo->manager->gpu_addr + sa_bo->soffset;
  238. }
  239. static inline void * amdgpu_sa_bo_cpu_addr(struct amdgpu_sa_bo *sa_bo)
  240. {
  241. return sa_bo->manager->cpu_ptr + sa_bo->soffset;
  242. }
  243. int amdgpu_sa_bo_manager_init(struct amdgpu_device *adev,
  244. struct amdgpu_sa_manager *sa_manager,
  245. unsigned size, u32 align, u32 domain);
  246. void amdgpu_sa_bo_manager_fini(struct amdgpu_device *adev,
  247. struct amdgpu_sa_manager *sa_manager);
  248. int amdgpu_sa_bo_manager_start(struct amdgpu_device *adev,
  249. struct amdgpu_sa_manager *sa_manager);
  250. int amdgpu_sa_bo_manager_suspend(struct amdgpu_device *adev,
  251. struct amdgpu_sa_manager *sa_manager);
  252. int amdgpu_sa_bo_new(struct amdgpu_sa_manager *sa_manager,
  253. struct amdgpu_sa_bo **sa_bo,
  254. unsigned size, unsigned align);
  255. void amdgpu_sa_bo_free(struct amdgpu_device *adev,
  256. struct amdgpu_sa_bo **sa_bo,
  257. struct dma_fence *fence);
  258. #if defined(CONFIG_DEBUG_FS)
  259. void amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager *sa_manager,
  260. struct seq_file *m);
  261. #endif
  262. #endif