amdgpu_vm.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 <linux/idr.h>
  28. #include "gpu_scheduler.h"
  29. #include "amdgpu_sync.h"
  30. #include "amdgpu_ring.h"
  31. struct amdgpu_bo_va;
  32. struct amdgpu_job;
  33. struct amdgpu_bo_list_entry;
  34. /*
  35. * GPUVM handling
  36. */
  37. /* maximum number of VMIDs */
  38. #define AMDGPU_NUM_VM 16
  39. /* Maximum number of PTEs the hardware can write with one command */
  40. #define AMDGPU_VM_MAX_UPDATE_SIZE 0x3FFFF
  41. /* number of entries in page table */
  42. #define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
  43. /* PTBs (Page Table Blocks) need to be aligned to 32K */
  44. #define AMDGPU_VM_PTB_ALIGN_SIZE 32768
  45. #define AMDGPU_PTE_VALID (1ULL << 0)
  46. #define AMDGPU_PTE_SYSTEM (1ULL << 1)
  47. #define AMDGPU_PTE_SNOOPED (1ULL << 2)
  48. /* VI only */
  49. #define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
  50. #define AMDGPU_PTE_READABLE (1ULL << 5)
  51. #define AMDGPU_PTE_WRITEABLE (1ULL << 6)
  52. #define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
  53. /* TILED for VEGA10, reserved for older ASICs */
  54. #define AMDGPU_PTE_PRT (1ULL << 51)
  55. /* PDE is handled as PTE for VEGA10 */
  56. #define AMDGPU_PDE_PTE (1ULL << 54)
  57. /* VEGA10 only */
  58. #define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
  59. #define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
  60. /* For Raven */
  61. #define AMDGPU_MTYPE_CC 2
  62. #define AMDGPU_PTE_DEFAULT_ATC (AMDGPU_PTE_SYSTEM \
  63. | AMDGPU_PTE_SNOOPED \
  64. | AMDGPU_PTE_EXECUTABLE \
  65. | AMDGPU_PTE_READABLE \
  66. | AMDGPU_PTE_WRITEABLE \
  67. | AMDGPU_PTE_MTYPE(AMDGPU_MTYPE_CC))
  68. /* How to programm VM fault handling */
  69. #define AMDGPU_VM_FAULT_STOP_NEVER 0
  70. #define AMDGPU_VM_FAULT_STOP_FIRST 1
  71. #define AMDGPU_VM_FAULT_STOP_ALWAYS 2
  72. /* max number of VMHUB */
  73. #define AMDGPU_MAX_VMHUBS 2
  74. #define AMDGPU_GFXHUB 0
  75. #define AMDGPU_MMHUB 1
  76. /* hardcode that limit for now */
  77. #define AMDGPU_VA_RESERVED_SIZE (8ULL << 20)
  78. /* VA hole for 48bit addresses on Vega10 */
  79. #define AMDGPU_VA_HOLE_START 0x0000800000000000ULL
  80. #define AMDGPU_VA_HOLE_END 0xffff800000000000ULL
  81. /*
  82. * Hardware is programmed as if the hole doesn't exists with start and end
  83. * address values.
  84. *
  85. * This mask is used to remove the upper 16bits of the VA and so come up with
  86. * the linear addr value.
  87. */
  88. #define AMDGPU_VA_HOLE_MASK 0x0000ffffffffffffULL
  89. /* max vmids dedicated for process */
  90. #define AMDGPU_VM_MAX_RESERVED_VMID 1
  91. #define AMDGPU_VM_CONTEXT_GFX 0
  92. #define AMDGPU_VM_CONTEXT_COMPUTE 1
  93. /* See vm_update_mode */
  94. #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
  95. #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
  96. /* base structure for tracking BO usage in a VM */
  97. struct amdgpu_vm_bo_base {
  98. /* constant after initialization */
  99. struct amdgpu_vm *vm;
  100. struct amdgpu_bo *bo;
  101. /* protected by bo being reserved */
  102. struct list_head bo_list;
  103. /* protected by spinlock */
  104. struct list_head vm_status;
  105. /* protected by the BO being reserved */
  106. bool moved;
  107. };
  108. struct amdgpu_vm_pt {
  109. struct amdgpu_vm_bo_base base;
  110. uint64_t addr;
  111. /* array of page tables, one for each directory entry */
  112. struct amdgpu_vm_pt *entries;
  113. unsigned last_entry_used;
  114. };
  115. #define AMDGPU_VM_FAULT(pasid, addr) (((u64)(pasid) << 48) | (addr))
  116. #define AMDGPU_VM_FAULT_PASID(fault) ((u64)(fault) >> 48)
  117. #define AMDGPU_VM_FAULT_ADDR(fault) ((u64)(fault) & 0xfffffffff000ULL)
  118. struct amdgpu_vm {
  119. /* tree of virtual addresses mapped */
  120. struct rb_root_cached va;
  121. /* protecting invalidated */
  122. spinlock_t status_lock;
  123. /* BOs who needs a validation */
  124. struct list_head evicted;
  125. /* PT BOs which relocated and their parent need an update */
  126. struct list_head relocated;
  127. /* BOs moved, but not yet updated in the PT */
  128. struct list_head moved;
  129. /* BO mappings freed, but not yet updated in the PT */
  130. struct list_head freed;
  131. /* contains the page directory */
  132. struct amdgpu_vm_pt root;
  133. struct dma_fence *last_update;
  134. /* protecting freed */
  135. spinlock_t freed_lock;
  136. /* Scheduler entity for page table updates */
  137. struct amd_sched_entity entity;
  138. /* client id and PASID (TODO: replace client_id with PASID) */
  139. u64 client_id;
  140. unsigned int pasid;
  141. /* dedicated to vm */
  142. struct amdgpu_vm_id *reserved_vmid[AMDGPU_MAX_VMHUBS];
  143. /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
  144. bool use_cpu_for_update;
  145. /* Flag to indicate ATS support from PTE for GFX9 */
  146. bool pte_support_ats;
  147. /* Up to 128 pending retry page faults */
  148. DECLARE_KFIFO(faults, u64, 128);
  149. /* Limit non-retry fault storms */
  150. unsigned int fault_credit;
  151. };
  152. struct amdgpu_vm_id {
  153. struct list_head list;
  154. struct amdgpu_sync active;
  155. struct dma_fence *last_flush;
  156. atomic64_t owner;
  157. uint64_t pd_gpu_addr;
  158. /* last flushed PD/PT update */
  159. struct dma_fence *flushed_updates;
  160. uint32_t current_gpu_reset_count;
  161. uint32_t gds_base;
  162. uint32_t gds_size;
  163. uint32_t gws_base;
  164. uint32_t gws_size;
  165. uint32_t oa_base;
  166. uint32_t oa_size;
  167. };
  168. struct amdgpu_vm_id_manager {
  169. struct mutex lock;
  170. unsigned num_ids;
  171. struct list_head ids_lru;
  172. struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
  173. atomic_t reserved_vmid_num;
  174. };
  175. struct amdgpu_vm_manager {
  176. /* Handling of VMIDs */
  177. struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS];
  178. /* Handling of VM fences */
  179. u64 fence_context;
  180. unsigned seqno[AMDGPU_MAX_RINGS];
  181. uint64_t max_pfn;
  182. uint32_t num_level;
  183. uint32_t block_size;
  184. uint32_t fragment_size;
  185. /* vram base address for page table entry */
  186. u64 vram_base_offset;
  187. /* vm pte handling */
  188. const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
  189. struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
  190. unsigned vm_pte_num_rings;
  191. atomic_t vm_pte_next_ring;
  192. /* client id counter */
  193. atomic64_t client_counter;
  194. /* partial resident texture handling */
  195. spinlock_t prt_lock;
  196. atomic_t num_prt_users;
  197. /* controls how VM page tables are updated for Graphics and Compute.
  198. * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
  199. * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
  200. */
  201. int vm_update_mode;
  202. /* PASID to VM mapping, will be used in interrupt context to
  203. * look up VM of a page fault
  204. */
  205. struct idr pasid_idr;
  206. spinlock_t pasid_lock;
  207. };
  208. int amdgpu_vm_alloc_pasid(unsigned int bits);
  209. void amdgpu_vm_free_pasid(unsigned int pasid);
  210. void amdgpu_vm_manager_init(struct amdgpu_device *adev);
  211. void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
  212. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  213. int vm_context, unsigned int pasid);
  214. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  215. bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
  216. unsigned int pasid);
  217. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  218. struct list_head *validated,
  219. struct amdgpu_bo_list_entry *entry);
  220. bool amdgpu_vm_ready(struct amdgpu_vm *vm);
  221. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  222. int (*callback)(void *p, struct amdgpu_bo *bo),
  223. void *param);
  224. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  225. struct amdgpu_vm *vm,
  226. uint64_t saddr, uint64_t size);
  227. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  228. struct amdgpu_sync *sync, struct dma_fence *fence,
  229. struct amdgpu_job *job);
  230. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
  231. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
  232. unsigned vmid);
  233. void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
  234. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  235. struct amdgpu_vm *vm);
  236. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  237. struct amdgpu_vm *vm,
  238. struct dma_fence **fence);
  239. int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
  240. struct amdgpu_vm *vm);
  241. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  242. struct amdgpu_bo_va *bo_va,
  243. bool clear);
  244. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  245. struct amdgpu_bo *bo, bool evicted);
  246. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  247. struct amdgpu_bo *bo);
  248. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  249. struct amdgpu_vm *vm,
  250. struct amdgpu_bo *bo);
  251. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  252. struct amdgpu_bo_va *bo_va,
  253. uint64_t addr, uint64_t offset,
  254. uint64_t size, uint64_t flags);
  255. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  256. struct amdgpu_bo_va *bo_va,
  257. uint64_t addr, uint64_t offset,
  258. uint64_t size, uint64_t flags);
  259. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  260. struct amdgpu_bo_va *bo_va,
  261. uint64_t addr);
  262. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  263. struct amdgpu_vm *vm,
  264. uint64_t saddr, uint64_t size);
  265. struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
  266. uint64_t addr);
  267. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  268. struct amdgpu_bo_va *bo_va);
  269. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
  270. uint32_t fragment_size_default, unsigned max_level);
  271. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  272. bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
  273. struct amdgpu_job *job);
  274. void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
  275. #endif