amdgpu_vm.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #define AMDGPU_PTE_VALID (1ULL << 0)
  45. #define AMDGPU_PTE_SYSTEM (1ULL << 1)
  46. #define AMDGPU_PTE_SNOOPED (1ULL << 2)
  47. /* VI only */
  48. #define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
  49. #define AMDGPU_PTE_READABLE (1ULL << 5)
  50. #define AMDGPU_PTE_WRITEABLE (1ULL << 6)
  51. #define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
  52. /* TILED for VEGA10, reserved for older ASICs */
  53. #define AMDGPU_PTE_PRT (1ULL << 51)
  54. /* PDE is handled as PTE for VEGA10 */
  55. #define AMDGPU_PDE_PTE (1ULL << 54)
  56. /* VEGA10 only */
  57. #define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
  58. #define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
  59. /* How to programm VM fault handling */
  60. #define AMDGPU_VM_FAULT_STOP_NEVER 0
  61. #define AMDGPU_VM_FAULT_STOP_FIRST 1
  62. #define AMDGPU_VM_FAULT_STOP_ALWAYS 2
  63. /* max number of VMHUB */
  64. #define AMDGPU_MAX_VMHUBS 2
  65. #define AMDGPU_GFXHUB 0
  66. #define AMDGPU_MMHUB 1
  67. /* hardcode that limit for now */
  68. #define AMDGPU_VA_RESERVED_SIZE (8 << 20)
  69. /* max vmids dedicated for process */
  70. #define AMDGPU_VM_MAX_RESERVED_VMID 1
  71. #define AMDGPU_VM_CONTEXT_GFX 0
  72. #define AMDGPU_VM_CONTEXT_COMPUTE 1
  73. /* See vm_update_mode */
  74. #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
  75. #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
  76. /* base structure for tracking BO usage in a VM */
  77. struct amdgpu_vm_bo_base {
  78. /* constant after initialization */
  79. struct amdgpu_vm *vm;
  80. struct amdgpu_bo *bo;
  81. /* protected by bo being reserved */
  82. struct list_head bo_list;
  83. /* protected by spinlock */
  84. struct list_head vm_status;
  85. };
  86. struct amdgpu_vm_pt {
  87. struct amdgpu_bo *bo;
  88. uint64_t addr;
  89. /* array of page tables, one for each directory entry */
  90. struct amdgpu_vm_pt *entries;
  91. unsigned last_entry_used;
  92. };
  93. struct amdgpu_vm {
  94. /* tree of virtual addresses mapped */
  95. struct rb_root_cached va;
  96. /* protecting invalidated */
  97. spinlock_t status_lock;
  98. /* BOs moved, but not yet updated in the PT */
  99. struct list_head moved;
  100. /* BOs cleared in the PT because of a move */
  101. struct list_head cleared;
  102. /* BO mappings freed, but not yet updated in the PT */
  103. struct list_head freed;
  104. /* contains the page directory */
  105. struct amdgpu_vm_pt root;
  106. struct dma_fence *last_dir_update;
  107. uint64_t last_eviction_counter;
  108. /* protecting freed */
  109. spinlock_t freed_lock;
  110. /* Scheduler entity for page table updates */
  111. struct amd_sched_entity entity;
  112. /* client id */
  113. u64 client_id;
  114. /* dedicated to vm */
  115. struct amdgpu_vm_id *reserved_vmid[AMDGPU_MAX_VMHUBS];
  116. /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
  117. bool use_cpu_for_update;
  118. /* Flag to indicate ATS support from PTE for GFX9 */
  119. bool pte_support_ats;
  120. };
  121. struct amdgpu_vm_id {
  122. struct list_head list;
  123. struct amdgpu_sync active;
  124. struct dma_fence *last_flush;
  125. atomic64_t owner;
  126. uint64_t pd_gpu_addr;
  127. /* last flushed PD/PT update */
  128. struct dma_fence *flushed_updates;
  129. uint32_t current_gpu_reset_count;
  130. uint32_t gds_base;
  131. uint32_t gds_size;
  132. uint32_t gws_base;
  133. uint32_t gws_size;
  134. uint32_t oa_base;
  135. uint32_t oa_size;
  136. };
  137. struct amdgpu_vm_id_manager {
  138. struct mutex lock;
  139. unsigned num_ids;
  140. struct list_head ids_lru;
  141. struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
  142. atomic_t reserved_vmid_num;
  143. };
  144. struct amdgpu_vm_manager {
  145. /* Handling of VMIDs */
  146. struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS];
  147. /* Handling of VM fences */
  148. u64 fence_context;
  149. unsigned seqno[AMDGPU_MAX_RINGS];
  150. uint64_t max_pfn;
  151. uint32_t num_level;
  152. uint64_t vm_size;
  153. uint32_t block_size;
  154. uint32_t fragment_size;
  155. /* vram base address for page table entry */
  156. u64 vram_base_offset;
  157. /* vm pte handling */
  158. const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
  159. struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
  160. unsigned vm_pte_num_rings;
  161. atomic_t vm_pte_next_ring;
  162. /* client id counter */
  163. atomic64_t client_counter;
  164. /* partial resident texture handling */
  165. spinlock_t prt_lock;
  166. atomic_t num_prt_users;
  167. /* controls how VM page tables are updated for Graphics and Compute.
  168. * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
  169. * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
  170. */
  171. int vm_update_mode;
  172. };
  173. void amdgpu_vm_manager_init(struct amdgpu_device *adev);
  174. void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
  175. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  176. int vm_context);
  177. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  178. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  179. struct list_head *validated,
  180. struct amdgpu_bo_list_entry *entry);
  181. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  182. int (*callback)(void *p, struct amdgpu_bo *bo),
  183. void *param);
  184. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  185. struct amdgpu_vm *vm,
  186. uint64_t saddr, uint64_t size);
  187. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  188. struct amdgpu_sync *sync, struct dma_fence *fence,
  189. struct amdgpu_job *job);
  190. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
  191. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
  192. unsigned vmid);
  193. void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
  194. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  195. struct amdgpu_vm *vm);
  196. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  197. struct amdgpu_vm *vm,
  198. struct dma_fence **fence);
  199. int amdgpu_vm_clear_moved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  200. struct amdgpu_sync *sync);
  201. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  202. struct amdgpu_bo_va *bo_va,
  203. bool clear);
  204. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  205. struct amdgpu_bo *bo);
  206. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  207. struct amdgpu_bo *bo);
  208. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  209. struct amdgpu_vm *vm,
  210. struct amdgpu_bo *bo);
  211. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  212. struct amdgpu_bo_va *bo_va,
  213. uint64_t addr, uint64_t offset,
  214. uint64_t size, uint64_t flags);
  215. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  216. struct amdgpu_bo_va *bo_va,
  217. uint64_t addr, uint64_t offset,
  218. uint64_t size, uint64_t flags);
  219. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  220. struct amdgpu_bo_va *bo_va,
  221. uint64_t addr);
  222. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  223. struct amdgpu_vm *vm,
  224. uint64_t saddr, uint64_t size);
  225. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  226. struct amdgpu_bo_va *bo_va);
  227. void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev,
  228. uint32_t fragment_size_default);
  229. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size,
  230. uint32_t fragment_size_default);
  231. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  232. bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
  233. struct amdgpu_job *job);
  234. void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
  235. #endif