amdgpu_vm.h 11 KB

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