amdgpu_vm.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright 2016 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Christian König
  23. */
  24. #ifndef __AMDGPU_VM_H__
  25. #define __AMDGPU_VM_H__
  26. #include <linux/rbtree.h>
  27. #include "gpu_scheduler.h"
  28. #include "amdgpu_sync.h"
  29. #include "amdgpu_ring.h"
  30. struct amdgpu_bo_va;
  31. struct amdgpu_job;
  32. struct amdgpu_bo_list_entry;
  33. /*
  34. * GPUVM handling
  35. */
  36. /* maximum number of VMIDs */
  37. #define AMDGPU_NUM_VM 16
  38. /* Maximum number of PTEs the hardware can write with one command */
  39. #define AMDGPU_VM_MAX_UPDATE_SIZE 0x3FFFF
  40. /* number of entries in page table */
  41. #define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
  42. /* PTBs (Page Table Blocks) need to be aligned to 32K */
  43. #define AMDGPU_VM_PTB_ALIGN_SIZE 32768
  44. /* LOG2 number of continuous pages for the fragment field */
  45. #define AMDGPU_LOG2_PAGES_PER_FRAG(adev) \
  46. ((adev)->asic_type < CHIP_VEGA10 ? 4 : \
  47. (adev)->vm_manager.block_size)
  48. #define AMDGPU_PTE_VALID (1ULL << 0)
  49. #define AMDGPU_PTE_SYSTEM (1ULL << 1)
  50. #define AMDGPU_PTE_SNOOPED (1ULL << 2)
  51. /* VI only */
  52. #define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
  53. #define AMDGPU_PTE_READABLE (1ULL << 5)
  54. #define AMDGPU_PTE_WRITEABLE (1ULL << 6)
  55. #define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
  56. /* TILED for VEGA10, reserved for older ASICs */
  57. #define AMDGPU_PTE_PRT (1ULL << 51)
  58. /* PDE is handled as PTE for VEGA10 */
  59. #define AMDGPU_PDE_PTE (1ULL << 54)
  60. /* VEGA10 only */
  61. #define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
  62. #define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
  63. /* How to programm VM fault handling */
  64. #define AMDGPU_VM_FAULT_STOP_NEVER 0
  65. #define AMDGPU_VM_FAULT_STOP_FIRST 1
  66. #define AMDGPU_VM_FAULT_STOP_ALWAYS 2
  67. /* max number of VMHUB */
  68. #define AMDGPU_MAX_VMHUBS 2
  69. #define AMDGPU_GFXHUB 0
  70. #define AMDGPU_MMHUB 1
  71. /* hardcode that limit for now */
  72. #define AMDGPU_VA_RESERVED_SIZE (8 << 20)
  73. /* max vmids dedicated for process */
  74. #define AMDGPU_VM_MAX_RESERVED_VMID 1
  75. #define AMDGPU_VM_CONTEXT_GFX 0
  76. #define AMDGPU_VM_CONTEXT_COMPUTE 1
  77. /* See vm_update_mode */
  78. #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
  79. #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
  80. struct amdgpu_vm_pt {
  81. struct amdgpu_bo *bo;
  82. uint64_t addr;
  83. bool huge_page;
  84. /* array of page tables, one for each directory entry */
  85. struct amdgpu_vm_pt *entries;
  86. unsigned last_entry_used;
  87. };
  88. struct amdgpu_vm {
  89. /* tree of virtual addresses mapped */
  90. struct rb_root va;
  91. /* protecting invalidated */
  92. spinlock_t status_lock;
  93. /* BOs moved, but not yet updated in the PT */
  94. struct list_head invalidated;
  95. /* BOs cleared in the PT because of a move */
  96. struct list_head cleared;
  97. /* BO mappings freed, but not yet updated in the PT */
  98. struct list_head freed;
  99. /* contains the page directory */
  100. struct amdgpu_vm_pt root;
  101. struct dma_fence *last_dir_update;
  102. uint64_t last_eviction_counter;
  103. /* protecting freed */
  104. spinlock_t freed_lock;
  105. /* Scheduler entity for page table updates */
  106. struct amd_sched_entity entity;
  107. /* client id */
  108. u64 client_id;
  109. /* dedicated to vm */
  110. struct amdgpu_vm_id *reserved_vmid[AMDGPU_MAX_VMHUBS];
  111. /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
  112. bool use_cpu_for_update;
  113. /* Flag to indicate ATS support from PTE for GFX9 */
  114. bool pte_support_ats;
  115. };
  116. struct amdgpu_vm_id {
  117. struct list_head list;
  118. struct amdgpu_sync active;
  119. struct dma_fence *last_flush;
  120. atomic64_t owner;
  121. uint64_t pd_gpu_addr;
  122. /* last flushed PD/PT update */
  123. struct dma_fence *flushed_updates;
  124. uint32_t current_gpu_reset_count;
  125. uint32_t gds_base;
  126. uint32_t gds_size;
  127. uint32_t gws_base;
  128. uint32_t gws_size;
  129. uint32_t oa_base;
  130. uint32_t oa_size;
  131. };
  132. struct amdgpu_vm_id_manager {
  133. struct mutex lock;
  134. unsigned num_ids;
  135. struct list_head ids_lru;
  136. struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
  137. atomic_t reserved_vmid_num;
  138. };
  139. struct amdgpu_vm_manager {
  140. /* Handling of VMIDs */
  141. struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS];
  142. /* Handling of VM fences */
  143. u64 fence_context;
  144. unsigned seqno[AMDGPU_MAX_RINGS];
  145. uint64_t max_pfn;
  146. uint32_t num_level;
  147. uint64_t vm_size;
  148. uint32_t block_size;
  149. /* vram base address for page table entry */
  150. u64 vram_base_offset;
  151. /* vm pte handling */
  152. const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
  153. struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
  154. unsigned vm_pte_num_rings;
  155. atomic_t vm_pte_next_ring;
  156. /* client id counter */
  157. atomic64_t client_counter;
  158. /* partial resident texture handling */
  159. spinlock_t prt_lock;
  160. atomic_t num_prt_users;
  161. /* controls how VM page tables are updated for Graphics and Compute.
  162. * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
  163. * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
  164. */
  165. int vm_update_mode;
  166. };
  167. void amdgpu_vm_manager_init(struct amdgpu_device *adev);
  168. void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
  169. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  170. int vm_context);
  171. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  172. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  173. struct list_head *validated,
  174. struct amdgpu_bo_list_entry *entry);
  175. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  176. int (*callback)(void *p, struct amdgpu_bo *bo),
  177. void *param);
  178. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  179. struct amdgpu_vm *vm,
  180. uint64_t saddr, uint64_t size);
  181. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  182. struct amdgpu_sync *sync, struct dma_fence *fence,
  183. struct amdgpu_job *job);
  184. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
  185. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
  186. unsigned vmid);
  187. void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
  188. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  189. struct amdgpu_vm *vm);
  190. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  191. struct amdgpu_vm *vm,
  192. struct dma_fence **fence);
  193. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  194. struct amdgpu_sync *sync);
  195. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  196. struct amdgpu_bo_va *bo_va,
  197. bool clear);
  198. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  199. struct amdgpu_bo *bo);
  200. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  201. struct amdgpu_bo *bo);
  202. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  203. struct amdgpu_vm *vm,
  204. struct amdgpu_bo *bo);
  205. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  206. struct amdgpu_bo_va *bo_va,
  207. uint64_t addr, uint64_t offset,
  208. uint64_t size, uint64_t flags);
  209. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  210. struct amdgpu_bo_va *bo_va,
  211. uint64_t addr, uint64_t offset,
  212. uint64_t size, uint64_t flags);
  213. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  214. struct amdgpu_bo_va *bo_va,
  215. uint64_t addr);
  216. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  217. struct amdgpu_vm *vm,
  218. uint64_t saddr, uint64_t size);
  219. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  220. struct amdgpu_bo_va *bo_va);
  221. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size);
  222. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  223. bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
  224. struct amdgpu_job *job);
  225. void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
  226. #endif