amdgpu_vm.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 4
  46. #define AMDGPU_PTE_VALID (1ULL << 0)
  47. #define AMDGPU_PTE_SYSTEM (1ULL << 1)
  48. #define AMDGPU_PTE_SNOOPED (1ULL << 2)
  49. /* VI only */
  50. #define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
  51. #define AMDGPU_PTE_READABLE (1ULL << 5)
  52. #define AMDGPU_PTE_WRITEABLE (1ULL << 6)
  53. #define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
  54. #define AMDGPU_PTE_PRT (1ULL << 63)
  55. /* VEGA10 only */
  56. #define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
  57. #define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
  58. /* How to programm VM fault handling */
  59. #define AMDGPU_VM_FAULT_STOP_NEVER 0
  60. #define AMDGPU_VM_FAULT_STOP_FIRST 1
  61. #define AMDGPU_VM_FAULT_STOP_ALWAYS 2
  62. /* max number of VMHUB */
  63. #define AMDGPU_MAX_VMHUBS 2
  64. #define AMDGPU_GFXHUB 0
  65. #define AMDGPU_MMHUB 1
  66. /* hardcode that limit for now */
  67. #define AMDGPU_VA_RESERVED_SIZE (8 << 20)
  68. struct amdgpu_vm_pt {
  69. struct amdgpu_bo *bo;
  70. uint64_t addr;
  71. /* array of page tables, one for each directory entry */
  72. struct amdgpu_vm_pt *entries;
  73. unsigned last_entry_used;
  74. };
  75. struct amdgpu_vm {
  76. /* tree of virtual addresses mapped */
  77. struct rb_root va;
  78. /* protecting invalidated */
  79. spinlock_t status_lock;
  80. /* BOs moved, but not yet updated in the PT */
  81. struct list_head invalidated;
  82. /* BOs cleared in the PT because of a move */
  83. struct list_head cleared;
  84. /* BO mappings freed, but not yet updated in the PT */
  85. struct list_head freed;
  86. /* contains the page directory */
  87. struct amdgpu_vm_pt root;
  88. struct dma_fence *last_dir_update;
  89. uint64_t last_eviction_counter;
  90. /* protecting freed */
  91. spinlock_t freed_lock;
  92. /* Scheduler entity for page table updates */
  93. struct amd_sched_entity entity;
  94. /* client id */
  95. u64 client_id;
  96. /* each VM will map on CSA */
  97. struct amdgpu_bo_va *csa_bo_va;
  98. };
  99. struct amdgpu_vm_id {
  100. struct list_head list;
  101. struct amdgpu_sync active;
  102. struct dma_fence *last_flush;
  103. atomic64_t owner;
  104. uint64_t pd_gpu_addr;
  105. /* last flushed PD/PT update */
  106. struct dma_fence *flushed_updates;
  107. uint32_t current_gpu_reset_count;
  108. uint32_t gds_base;
  109. uint32_t gds_size;
  110. uint32_t gws_base;
  111. uint32_t gws_size;
  112. uint32_t oa_base;
  113. uint32_t oa_size;
  114. };
  115. struct amdgpu_vm_id_manager {
  116. struct mutex lock;
  117. unsigned num_ids;
  118. struct list_head ids_lru;
  119. struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
  120. };
  121. struct amdgpu_vm_manager {
  122. /* Handling of VMIDs */
  123. struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS];
  124. /* Handling of VM fences */
  125. u64 fence_context;
  126. unsigned seqno[AMDGPU_MAX_RINGS];
  127. uint64_t max_pfn;
  128. uint32_t num_level;
  129. uint64_t vm_size;
  130. uint32_t block_size;
  131. /* vram base address for page table entry */
  132. u64 vram_base_offset;
  133. /* is vm enabled? */
  134. bool enabled;
  135. /* vm pte handling */
  136. const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
  137. struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
  138. unsigned vm_pte_num_rings;
  139. atomic_t vm_pte_next_ring;
  140. /* client id counter */
  141. atomic64_t client_counter;
  142. /* partial resident texture handling */
  143. spinlock_t prt_lock;
  144. atomic_t num_prt_users;
  145. };
  146. void amdgpu_vm_manager_init(struct amdgpu_device *adev);
  147. void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
  148. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  149. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  150. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  151. struct list_head *validated,
  152. struct amdgpu_bo_list_entry *entry);
  153. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  154. int (*callback)(void *p, struct amdgpu_bo *bo),
  155. void *param);
  156. void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
  157. struct amdgpu_vm *vm);
  158. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  159. struct amdgpu_vm *vm,
  160. uint64_t saddr, uint64_t size);
  161. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  162. struct amdgpu_sync *sync, struct dma_fence *fence,
  163. struct amdgpu_job *job);
  164. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job);
  165. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
  166. unsigned vmid);
  167. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  168. struct amdgpu_vm *vm);
  169. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  170. struct amdgpu_vm *vm,
  171. struct dma_fence **fence);
  172. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  173. struct amdgpu_sync *sync);
  174. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  175. struct amdgpu_bo_va *bo_va,
  176. bool clear);
  177. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  178. struct amdgpu_bo *bo);
  179. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  180. struct amdgpu_bo *bo);
  181. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  182. struct amdgpu_vm *vm,
  183. struct amdgpu_bo *bo);
  184. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  185. struct amdgpu_bo_va *bo_va,
  186. uint64_t addr, uint64_t offset,
  187. uint64_t size, uint64_t flags);
  188. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  189. struct amdgpu_bo_va *bo_va,
  190. uint64_t addr, uint64_t offset,
  191. uint64_t size, uint64_t flags);
  192. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  193. struct amdgpu_bo_va *bo_va,
  194. uint64_t addr);
  195. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  196. struct amdgpu_vm *vm,
  197. uint64_t saddr, uint64_t size);
  198. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  199. struct amdgpu_bo_va *bo_va);
  200. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size);
  201. #endif