amdgpu_amdkfd.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2014 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. /* amdgpu_amdkfd.h defines the private interface between amdgpu and amdkfd. */
  23. #ifndef AMDGPU_AMDKFD_H_INCLUDED
  24. #define AMDGPU_AMDKFD_H_INCLUDED
  25. #include <linux/types.h>
  26. #include <linux/mmu_context.h>
  27. #include <kgd_kfd_interface.h>
  28. #include <drm/ttm/ttm_execbuf_util.h>
  29. #include "amdgpu_sync.h"
  30. #include "amdgpu_vm.h"
  31. extern const struct kgd2kfd_calls *kgd2kfd;
  32. struct amdgpu_device;
  33. struct kfd_bo_va_list {
  34. struct list_head bo_list;
  35. struct amdgpu_bo_va *bo_va;
  36. void *kgd_dev;
  37. bool is_mapped;
  38. uint64_t va;
  39. uint64_t pte_flags;
  40. };
  41. struct kgd_mem {
  42. struct mutex lock;
  43. struct amdgpu_bo *bo;
  44. struct list_head bo_va_list;
  45. /* protected by amdkfd_process_info.lock */
  46. struct ttm_validate_buffer validate_list;
  47. struct ttm_validate_buffer resv_list;
  48. uint32_t domain;
  49. unsigned int mapped_to_gpu_memory;
  50. uint64_t va;
  51. uint32_t mapping_flags;
  52. struct amdkfd_process_info *process_info;
  53. struct amdgpu_sync sync;
  54. bool aql_queue;
  55. };
  56. /* KFD Memory Eviction */
  57. struct amdgpu_amdkfd_fence {
  58. struct dma_fence base;
  59. struct mm_struct *mm;
  60. spinlock_t lock;
  61. char timeline_name[TASK_COMM_LEN];
  62. };
  63. struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
  64. struct mm_struct *mm);
  65. bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm);
  66. struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f);
  67. struct amdkfd_process_info {
  68. /* List head of all VMs that belong to a KFD process */
  69. struct list_head vm_list_head;
  70. /* List head for all KFD BOs that belong to a KFD process. */
  71. struct list_head kfd_bo_list;
  72. /* Lock to protect kfd_bo_list */
  73. struct mutex lock;
  74. /* Number of VMs */
  75. unsigned int n_vms;
  76. /* Eviction Fence */
  77. struct amdgpu_amdkfd_fence *eviction_fence;
  78. };
  79. /* struct amdkfd_vm -
  80. * For Memory Eviction KGD requires a mechanism to keep track of all KFD BOs
  81. * belonging to a KFD process. All the VMs belonging to the same process point
  82. * to the same amdkfd_process_info.
  83. */
  84. struct amdkfd_vm {
  85. /* Keep base as the first parameter for pointer compatibility between
  86. * amdkfd_vm and amdgpu_vm.
  87. */
  88. struct amdgpu_vm base;
  89. /* List node in amdkfd_process_info.vm_list_head*/
  90. struct list_head vm_list_node;
  91. struct amdgpu_device *adev;
  92. /* Points to the KFD process VM info*/
  93. struct amdkfd_process_info *process_info;
  94. uint64_t pd_phys_addr;
  95. };
  96. int amdgpu_amdkfd_init(void);
  97. void amdgpu_amdkfd_fini(void);
  98. void amdgpu_amdkfd_suspend(struct amdgpu_device *adev);
  99. int amdgpu_amdkfd_resume(struct amdgpu_device *adev);
  100. void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
  101. const void *ih_ring_entry);
  102. void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);
  103. void amdgpu_amdkfd_device_init(struct amdgpu_device *adev);
  104. void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev);
  105. int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine,
  106. uint32_t vmid, uint64_t gpu_addr,
  107. uint32_t *ib_cmd, uint32_t ib_len);
  108. struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void);
  109. struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void);
  110. bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid);
  111. /* Shared API */
  112. int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
  113. void **mem_obj, uint64_t *gpu_addr,
  114. void **cpu_ptr);
  115. void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj);
  116. void get_local_mem_info(struct kgd_dev *kgd,
  117. struct kfd_local_mem_info *mem_info);
  118. uint64_t get_gpu_clock_counter(struct kgd_dev *kgd);
  119. uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd);
  120. void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info);
  121. uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd);
  122. #define read_user_wptr(mmptr, wptr, dst) \
  123. ({ \
  124. bool valid = false; \
  125. if ((mmptr) && (wptr)) { \
  126. if ((mmptr) == current->mm) { \
  127. valid = !get_user((dst), (wptr)); \
  128. } else if (current->mm == NULL) { \
  129. use_mm(mmptr); \
  130. valid = !get_user((dst), (wptr)); \
  131. unuse_mm(mmptr); \
  132. } \
  133. } \
  134. valid; \
  135. })
  136. /* GPUVM API */
  137. int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, void **vm,
  138. void **process_info,
  139. struct dma_fence **ef);
  140. void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm);
  141. uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm);
  142. int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
  143. struct kgd_dev *kgd, uint64_t va, uint64_t size,
  144. void *vm, struct kgd_mem **mem,
  145. uint64_t *offset, uint32_t flags);
  146. int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  147. struct kgd_dev *kgd, struct kgd_mem *mem);
  148. int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
  149. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
  150. int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  151. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
  152. int amdgpu_amdkfd_gpuvm_sync_memory(
  153. struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
  154. int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
  155. struct kgd_mem *mem, void **kptr, uint64_t *size);
  156. int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
  157. struct dma_fence **ef);
  158. void amdgpu_amdkfd_gpuvm_init_mem_limits(void);
  159. void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo);
  160. #endif /* AMDGPU_AMDKFD_H_INCLUDED */