amdgpu_vm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <drm/drm_file.h>
  31. #include <drm/ttm/ttm_bo_driver.h>
  32. #include <linux/chash.h>
  33. #include "amdgpu_sync.h"
  34. #include "amdgpu_ring.h"
  35. #include "amdgpu_ids.h"
  36. struct amdgpu_bo_va;
  37. struct amdgpu_job;
  38. struct amdgpu_bo_list_entry;
  39. /*
  40. * GPUVM handling
  41. */
  42. /* Maximum number of PTEs the hardware can write with one command */
  43. #define AMDGPU_VM_MAX_UPDATE_SIZE 0x3FFFF
  44. /* number of entries in page table */
  45. #define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
  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. /* For GFX9 */
  63. #define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57)
  64. #define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL)
  65. #define AMDGPU_MTYPE_NC 0
  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 (1ULL << 20)
  83. /* max vmids dedicated for process */
  84. #define AMDGPU_VM_MAX_RESERVED_VMID 1
  85. #define AMDGPU_VM_CONTEXT_GFX 0
  86. #define AMDGPU_VM_CONTEXT_COMPUTE 1
  87. /* See vm_update_mode */
  88. #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
  89. #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
  90. /* VMPT level enumerate, and the hiberachy is:
  91. * PDB2->PDB1->PDB0->PTB
  92. */
  93. enum amdgpu_vm_level {
  94. AMDGPU_VM_PDB2,
  95. AMDGPU_VM_PDB1,
  96. AMDGPU_VM_PDB0,
  97. AMDGPU_VM_PTB
  98. };
  99. /* base structure for tracking BO usage in a VM */
  100. struct amdgpu_vm_bo_base {
  101. /* constant after initialization */
  102. struct amdgpu_vm *vm;
  103. struct amdgpu_bo *bo;
  104. /* protected by bo being reserved */
  105. struct amdgpu_vm_bo_base *next;
  106. /* protected by spinlock */
  107. struct list_head vm_status;
  108. /* protected by the BO being reserved */
  109. bool moved;
  110. };
  111. struct amdgpu_vm_pt {
  112. struct amdgpu_vm_bo_base base;
  113. bool huge;
  114. /* array of page tables, one for each directory entry */
  115. struct amdgpu_vm_pt *entries;
  116. };
  117. /* provided by hw blocks that can write ptes, e.g., sdma */
  118. struct amdgpu_vm_pte_funcs {
  119. /* number of dw to reserve per operation */
  120. unsigned copy_pte_num_dw;
  121. /* copy pte entries from GART */
  122. void (*copy_pte)(struct amdgpu_ib *ib,
  123. uint64_t pe, uint64_t src,
  124. unsigned count);
  125. /* write pte one entry at a time with addr mapping */
  126. void (*write_pte)(struct amdgpu_ib *ib, uint64_t pe,
  127. uint64_t value, unsigned count,
  128. uint32_t incr);
  129. /* for linear pte/pde updates without addr mapping */
  130. void (*set_pte_pde)(struct amdgpu_ib *ib,
  131. uint64_t pe,
  132. uint64_t addr, unsigned count,
  133. uint32_t incr, uint64_t flags);
  134. };
  135. #define AMDGPU_VM_FAULT(pasid, addr) (((u64)(pasid) << 48) | (addr))
  136. #define AMDGPU_VM_FAULT_PASID(fault) ((u64)(fault) >> 48)
  137. #define AMDGPU_VM_FAULT_ADDR(fault) ((u64)(fault) & 0xfffffffff000ULL)
  138. struct amdgpu_task_info {
  139. char process_name[TASK_COMM_LEN];
  140. char task_name[TASK_COMM_LEN];
  141. pid_t pid;
  142. pid_t tgid;
  143. };
  144. #define AMDGPU_PAGEFAULT_HASH_BITS 8
  145. struct amdgpu_retryfault_hashtable {
  146. DECLARE_CHASH_TABLE(hash, AMDGPU_PAGEFAULT_HASH_BITS, 8, 0);
  147. spinlock_t lock;
  148. int count;
  149. };
  150. struct amdgpu_vm {
  151. /* tree of virtual addresses mapped */
  152. struct rb_root_cached va;
  153. /* BOs who needs a validation */
  154. struct list_head evicted;
  155. /* PT BOs which relocated and their parent need an update */
  156. struct list_head relocated;
  157. /* per VM BOs moved, but not yet updated in the PT */
  158. struct list_head moved;
  159. /* All BOs of this VM not currently in the state machine */
  160. struct list_head idle;
  161. /* regular invalidated BOs, but not yet updated in the PT */
  162. struct list_head invalidated;
  163. spinlock_t invalidated_lock;
  164. /* BO mappings freed, but not yet updated in the PT */
  165. struct list_head freed;
  166. /* contains the page directory */
  167. struct amdgpu_vm_pt root;
  168. struct dma_fence *last_update;
  169. /* Scheduler entity for page table updates */
  170. struct drm_sched_entity entity;
  171. unsigned int pasid;
  172. /* dedicated to vm */
  173. struct amdgpu_vmid *reserved_vmid[AMDGPU_MAX_VMHUBS];
  174. /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
  175. bool use_cpu_for_update;
  176. /* Flag to indicate ATS support from PTE for GFX9 */
  177. bool pte_support_ats;
  178. /* Up to 128 pending retry page faults */
  179. DECLARE_KFIFO(faults, u64, 128);
  180. /* Limit non-retry fault storms */
  181. unsigned int fault_credit;
  182. /* Points to the KFD process VM info */
  183. struct amdkfd_process_info *process_info;
  184. /* List node in amdkfd_process_info.vm_list_head */
  185. struct list_head vm_list_node;
  186. /* Valid while the PD is reserved or fenced */
  187. uint64_t pd_phys_addr;
  188. /* Some basic info about the task */
  189. struct amdgpu_task_info task_info;
  190. /* Store positions of group of BOs */
  191. struct ttm_lru_bulk_move lru_bulk_move;
  192. /* mark whether can do the bulk move */
  193. bool bulk_moveable;
  194. struct amdgpu_retryfault_hashtable *fault_hash;
  195. };
  196. struct amdgpu_vm_manager {
  197. /* Handling of VMIDs */
  198. struct amdgpu_vmid_mgr id_mgr[AMDGPU_MAX_VMHUBS];
  199. /* Handling of VM fences */
  200. u64 fence_context;
  201. unsigned seqno[AMDGPU_MAX_RINGS];
  202. uint64_t max_pfn;
  203. uint32_t num_level;
  204. uint32_t block_size;
  205. uint32_t fragment_size;
  206. enum amdgpu_vm_level root_level;
  207. /* vram base address for page table entry */
  208. u64 vram_base_offset;
  209. /* vm pte handling */
  210. const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
  211. struct drm_sched_rq *vm_pte_rqs[AMDGPU_MAX_RINGS];
  212. unsigned vm_pte_num_rqs;
  213. /* partial resident texture handling */
  214. spinlock_t prt_lock;
  215. atomic_t num_prt_users;
  216. /* controls how VM page tables are updated for Graphics and Compute.
  217. * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
  218. * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
  219. */
  220. int vm_update_mode;
  221. /* PASID to VM mapping, will be used in interrupt context to
  222. * look up VM of a page fault
  223. */
  224. struct idr pasid_idr;
  225. spinlock_t pasid_lock;
  226. };
  227. #define amdgpu_vm_copy_pte(adev, ib, pe, src, count) ((adev)->vm_manager.vm_pte_funcs->copy_pte((ib), (pe), (src), (count)))
  228. #define amdgpu_vm_write_pte(adev, ib, pe, value, count, incr) ((adev)->vm_manager.vm_pte_funcs->write_pte((ib), (pe), (value), (count), (incr)))
  229. #define amdgpu_vm_set_pte_pde(adev, ib, pe, addr, count, incr, flags) ((adev)->vm_manager.vm_pte_funcs->set_pte_pde((ib), (pe), (addr), (count), (incr), (flags)))
  230. void amdgpu_vm_manager_init(struct amdgpu_device *adev);
  231. void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
  232. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  233. int vm_context, unsigned int pasid);
  234. int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm, unsigned int pasid);
  235. void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  236. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  237. bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
  238. unsigned int pasid);
  239. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  240. struct list_head *validated,
  241. struct amdgpu_bo_list_entry *entry);
  242. bool amdgpu_vm_ready(struct amdgpu_vm *vm);
  243. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  244. int (*callback)(void *p, struct amdgpu_bo *bo),
  245. void *param);
  246. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  247. struct amdgpu_vm *vm,
  248. uint64_t saddr, uint64_t size);
  249. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
  250. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  251. struct amdgpu_vm *vm);
  252. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  253. struct amdgpu_vm *vm,
  254. struct dma_fence **fence);
  255. int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
  256. struct amdgpu_vm *vm);
  257. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  258. struct amdgpu_bo_va *bo_va,
  259. bool clear);
  260. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  261. struct amdgpu_bo *bo, bool evicted);
  262. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  263. struct amdgpu_bo *bo);
  264. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  265. struct amdgpu_vm *vm,
  266. struct amdgpu_bo *bo);
  267. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  268. struct amdgpu_bo_va *bo_va,
  269. uint64_t addr, uint64_t offset,
  270. uint64_t size, uint64_t flags);
  271. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  272. struct amdgpu_bo_va *bo_va,
  273. uint64_t addr, uint64_t offset,
  274. uint64_t size, uint64_t flags);
  275. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  276. struct amdgpu_bo_va *bo_va,
  277. uint64_t addr);
  278. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  279. struct amdgpu_vm *vm,
  280. uint64_t saddr, uint64_t size);
  281. struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
  282. uint64_t addr);
  283. void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket);
  284. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  285. struct amdgpu_bo_va *bo_va);
  286. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
  287. uint32_t fragment_size_default, unsigned max_level,
  288. unsigned max_bits);
  289. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  290. bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
  291. struct amdgpu_job *job);
  292. void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
  293. void amdgpu_vm_get_task_info(struct amdgpu_device *adev, unsigned int pasid,
  294. struct amdgpu_task_info *task_info);
  295. void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
  296. void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
  297. struct amdgpu_vm *vm);
  298. int amdgpu_vm_add_fault(struct amdgpu_retryfault_hashtable *fault_hash, u64 key);
  299. void amdgpu_vm_clear_fault(struct amdgpu_retryfault_hashtable *fault_hash, u64 key);
  300. #endif