amdgpu_vm.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 (1 << amdgpu_vm_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 (1 << 0)
  47. #define AMDGPU_PTE_SYSTEM (1 << 1)
  48. #define AMDGPU_PTE_SNOOPED (1 << 2)
  49. /* VI only */
  50. #define AMDGPU_PTE_EXECUTABLE (1 << 4)
  51. #define AMDGPU_PTE_READABLE (1 << 5)
  52. #define AMDGPU_PTE_WRITEABLE (1 << 6)
  53. #define AMDGPU_PTE_FRAG(x) ((x & 0x1f) << 7)
  54. /* How to programm VM fault handling */
  55. #define AMDGPU_VM_FAULT_STOP_NEVER 0
  56. #define AMDGPU_VM_FAULT_STOP_FIRST 1
  57. #define AMDGPU_VM_FAULT_STOP_ALWAYS 2
  58. struct amdgpu_vm_pt {
  59. struct amdgpu_bo *bo;
  60. uint64_t addr;
  61. };
  62. struct amdgpu_vm {
  63. /* tree of virtual addresses mapped */
  64. struct rb_root va;
  65. /* protecting invalidated */
  66. spinlock_t status_lock;
  67. /* BOs moved, but not yet updated in the PT */
  68. struct list_head invalidated;
  69. /* BOs cleared in the PT because of a move */
  70. struct list_head cleared;
  71. /* BO mappings freed, but not yet updated in the PT */
  72. struct list_head freed;
  73. /* contains the page directory */
  74. struct amdgpu_bo *page_directory;
  75. unsigned max_pde_used;
  76. struct dma_fence *page_directory_fence;
  77. uint64_t last_eviction_counter;
  78. /* array of page tables, one for each page directory entry */
  79. struct amdgpu_vm_pt *page_tables;
  80. /* for id and flush management per ring */
  81. struct amdgpu_vm_id *ids[AMDGPU_MAX_RINGS];
  82. /* protecting freed */
  83. spinlock_t freed_lock;
  84. /* Scheduler entity for page table updates */
  85. struct amd_sched_entity entity;
  86. /* client id */
  87. u64 client_id;
  88. /* each VM will map on CSA */
  89. struct amdgpu_bo_va *csa_bo_va;
  90. };
  91. struct amdgpu_vm_id {
  92. struct list_head list;
  93. struct dma_fence *first;
  94. struct amdgpu_sync active;
  95. struct dma_fence *last_flush;
  96. atomic64_t owner;
  97. uint64_t pd_gpu_addr;
  98. /* last flushed PD/PT update */
  99. struct dma_fence *flushed_updates;
  100. uint32_t current_gpu_reset_count;
  101. uint32_t gds_base;
  102. uint32_t gds_size;
  103. uint32_t gws_base;
  104. uint32_t gws_size;
  105. uint32_t oa_base;
  106. uint32_t oa_size;
  107. };
  108. struct amdgpu_vm_manager {
  109. /* Handling of VMIDs */
  110. struct mutex lock;
  111. unsigned num_ids;
  112. struct list_head ids_lru;
  113. struct amdgpu_vm_id ids[AMDGPU_NUM_VM];
  114. /* Handling of VM fences */
  115. u64 fence_context;
  116. unsigned seqno[AMDGPU_MAX_RINGS];
  117. uint32_t max_pfn;
  118. /* vram base address for page table entry */
  119. u64 vram_base_offset;
  120. /* is vm enabled? */
  121. bool enabled;
  122. /* vm pte handling */
  123. const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
  124. struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS];
  125. unsigned vm_pte_num_rings;
  126. atomic_t vm_pte_next_ring;
  127. /* client id counter */
  128. atomic64_t client_counter;
  129. };
  130. void amdgpu_vm_manager_init(struct amdgpu_device *adev);
  131. void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
  132. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  133. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  134. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  135. struct list_head *validated,
  136. struct amdgpu_bo_list_entry *entry);
  137. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  138. int (*callback)(void *p, struct amdgpu_bo *bo),
  139. void *param);
  140. void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
  141. struct amdgpu_vm *vm);
  142. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  143. struct amdgpu_sync *sync, struct dma_fence *fence,
  144. struct amdgpu_job *job);
  145. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job);
  146. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id);
  147. int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
  148. struct amdgpu_vm *vm);
  149. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  150. struct amdgpu_vm *vm);
  151. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  152. struct amdgpu_sync *sync);
  153. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  154. struct amdgpu_bo_va *bo_va,
  155. bool clear);
  156. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  157. struct amdgpu_bo *bo);
  158. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  159. struct amdgpu_bo *bo);
  160. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  161. struct amdgpu_vm *vm,
  162. struct amdgpu_bo *bo);
  163. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  164. struct amdgpu_bo_va *bo_va,
  165. uint64_t addr, uint64_t offset,
  166. uint64_t size, uint64_t flags);
  167. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  168. struct amdgpu_bo_va *bo_va,
  169. uint64_t addr);
  170. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  171. struct amdgpu_bo_va *bo_va);
  172. #endif