amdgpu_virt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. */
  23. #include "amdgpu.h"
  24. #define MAX_KIQ_REG_WAIT 100000
  25. int amdgpu_allocate_static_csa(struct amdgpu_device *adev)
  26. {
  27. int r;
  28. void *ptr;
  29. r = amdgpu_bo_create_kernel(adev, AMDGPU_CSA_SIZE, PAGE_SIZE,
  30. AMDGPU_GEM_DOMAIN_VRAM, &adev->virt.csa_obj,
  31. &adev->virt.csa_vmid0_addr, &ptr);
  32. if (r)
  33. return r;
  34. memset(ptr, 0, AMDGPU_CSA_SIZE);
  35. return 0;
  36. }
  37. /*
  38. * amdgpu_map_static_csa should be called during amdgpu_vm_init
  39. * it maps virtual address "AMDGPU_VA_RESERVED_SIZE - AMDGPU_CSA_SIZE"
  40. * to this VM, and each command submission of GFX should use this virtual
  41. * address within META_DATA init package to support SRIOV gfx preemption.
  42. */
  43. int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  44. struct amdgpu_bo_va **bo_va)
  45. {
  46. struct ww_acquire_ctx ticket;
  47. struct list_head list;
  48. struct amdgpu_bo_list_entry pd;
  49. struct ttm_validate_buffer csa_tv;
  50. int r;
  51. INIT_LIST_HEAD(&list);
  52. INIT_LIST_HEAD(&csa_tv.head);
  53. csa_tv.bo = &adev->virt.csa_obj->tbo;
  54. csa_tv.shared = true;
  55. list_add(&csa_tv.head, &list);
  56. amdgpu_vm_get_pd_bo(vm, &list, &pd);
  57. r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
  58. if (r) {
  59. DRM_ERROR("failed to reserve CSA,PD BOs: err=%d\n", r);
  60. return r;
  61. }
  62. *bo_va = amdgpu_vm_bo_add(adev, vm, adev->virt.csa_obj);
  63. if (!*bo_va) {
  64. ttm_eu_backoff_reservation(&ticket, &list);
  65. DRM_ERROR("failed to create bo_va for static CSA\n");
  66. return -ENOMEM;
  67. }
  68. r = amdgpu_vm_alloc_pts(adev, (*bo_va)->base.vm, AMDGPU_CSA_VADDR,
  69. AMDGPU_CSA_SIZE);
  70. if (r) {
  71. DRM_ERROR("failed to allocate pts for static CSA, err=%d\n", r);
  72. amdgpu_vm_bo_rmv(adev, *bo_va);
  73. ttm_eu_backoff_reservation(&ticket, &list);
  74. return r;
  75. }
  76. r = amdgpu_vm_bo_map(adev, *bo_va, AMDGPU_CSA_VADDR, 0, AMDGPU_CSA_SIZE,
  77. AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
  78. AMDGPU_PTE_EXECUTABLE);
  79. if (r) {
  80. DRM_ERROR("failed to do bo_map on static CSA, err=%d\n", r);
  81. amdgpu_vm_bo_rmv(adev, *bo_va);
  82. ttm_eu_backoff_reservation(&ticket, &list);
  83. return r;
  84. }
  85. ttm_eu_backoff_reservation(&ticket, &list);
  86. return 0;
  87. }
  88. void amdgpu_virt_init_setting(struct amdgpu_device *adev)
  89. {
  90. /* enable virtual display */
  91. adev->mode_info.num_crtc = 1;
  92. adev->enable_virtual_display = true;
  93. adev->cg_flags = 0;
  94. adev->pg_flags = 0;
  95. mutex_init(&adev->virt.lock_reset);
  96. }
  97. uint32_t amdgpu_virt_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
  98. {
  99. signed long r;
  100. uint32_t val;
  101. struct dma_fence *f;
  102. struct amdgpu_kiq *kiq = &adev->gfx.kiq;
  103. struct amdgpu_ring *ring = &kiq->ring;
  104. BUG_ON(!ring->funcs->emit_rreg);
  105. mutex_lock(&kiq->ring_mutex);
  106. amdgpu_ring_alloc(ring, 32);
  107. amdgpu_ring_emit_rreg(ring, reg);
  108. amdgpu_fence_emit(ring, &f);
  109. amdgpu_ring_commit(ring);
  110. mutex_unlock(&kiq->ring_mutex);
  111. r = dma_fence_wait_timeout(f, false, msecs_to_jiffies(MAX_KIQ_REG_WAIT));
  112. dma_fence_put(f);
  113. if (r < 1) {
  114. DRM_ERROR("wait for kiq fence error: %ld.\n", r);
  115. return ~0;
  116. }
  117. val = adev->wb.wb[adev->virt.reg_val_offs];
  118. return val;
  119. }
  120. void amdgpu_virt_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  121. {
  122. signed long r;
  123. struct dma_fence *f;
  124. struct amdgpu_kiq *kiq = &adev->gfx.kiq;
  125. struct amdgpu_ring *ring = &kiq->ring;
  126. BUG_ON(!ring->funcs->emit_wreg);
  127. mutex_lock(&kiq->ring_mutex);
  128. amdgpu_ring_alloc(ring, 32);
  129. amdgpu_ring_emit_wreg(ring, reg, v);
  130. amdgpu_fence_emit(ring, &f);
  131. amdgpu_ring_commit(ring);
  132. mutex_unlock(&kiq->ring_mutex);
  133. r = dma_fence_wait_timeout(f, false, msecs_to_jiffies(MAX_KIQ_REG_WAIT));
  134. if (r < 1)
  135. DRM_ERROR("wait for kiq fence error: %ld.\n", r);
  136. dma_fence_put(f);
  137. }
  138. /**
  139. * amdgpu_virt_request_full_gpu() - request full gpu access
  140. * @amdgpu: amdgpu device.
  141. * @init: is driver init time.
  142. * When start to init/fini driver, first need to request full gpu access.
  143. * Return: Zero if request success, otherwise will return error.
  144. */
  145. int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init)
  146. {
  147. struct amdgpu_virt *virt = &adev->virt;
  148. int r;
  149. if (virt->ops && virt->ops->req_full_gpu) {
  150. r = virt->ops->req_full_gpu(adev, init);
  151. if (r)
  152. return r;
  153. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  154. }
  155. return 0;
  156. }
  157. /**
  158. * amdgpu_virt_release_full_gpu() - release full gpu access
  159. * @amdgpu: amdgpu device.
  160. * @init: is driver init time.
  161. * When finishing driver init/fini, need to release full gpu access.
  162. * Return: Zero if release success, otherwise will returen error.
  163. */
  164. int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init)
  165. {
  166. struct amdgpu_virt *virt = &adev->virt;
  167. int r;
  168. if (virt->ops && virt->ops->rel_full_gpu) {
  169. r = virt->ops->rel_full_gpu(adev, init);
  170. if (r)
  171. return r;
  172. adev->virt.caps |= AMDGPU_SRIOV_CAPS_RUNTIME;
  173. }
  174. return 0;
  175. }
  176. /**
  177. * amdgpu_virt_reset_gpu() - reset gpu
  178. * @amdgpu: amdgpu device.
  179. * Send reset command to GPU hypervisor to reset GPU that VM is using
  180. * Return: Zero if reset success, otherwise will return error.
  181. */
  182. int amdgpu_virt_reset_gpu(struct amdgpu_device *adev)
  183. {
  184. struct amdgpu_virt *virt = &adev->virt;
  185. int r;
  186. if (virt->ops && virt->ops->reset_gpu) {
  187. r = virt->ops->reset_gpu(adev);
  188. if (r)
  189. return r;
  190. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  191. }
  192. return 0;
  193. }
  194. /**
  195. * amdgpu_virt_alloc_mm_table() - alloc memory for mm table
  196. * @amdgpu: amdgpu device.
  197. * MM table is used by UVD and VCE for its initialization
  198. * Return: Zero if allocate success.
  199. */
  200. int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev)
  201. {
  202. int r;
  203. if (!amdgpu_sriov_vf(adev) || adev->virt.mm_table.gpu_addr)
  204. return 0;
  205. r = amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
  206. AMDGPU_GEM_DOMAIN_VRAM,
  207. &adev->virt.mm_table.bo,
  208. &adev->virt.mm_table.gpu_addr,
  209. (void *)&adev->virt.mm_table.cpu_addr);
  210. if (r) {
  211. DRM_ERROR("failed to alloc mm table and error = %d.\n", r);
  212. return r;
  213. }
  214. memset((void *)adev->virt.mm_table.cpu_addr, 0, PAGE_SIZE);
  215. DRM_INFO("MM table gpu addr = 0x%llx, cpu addr = %p.\n",
  216. adev->virt.mm_table.gpu_addr,
  217. adev->virt.mm_table.cpu_addr);
  218. return 0;
  219. }
  220. /**
  221. * amdgpu_virt_free_mm_table() - free mm table memory
  222. * @amdgpu: amdgpu device.
  223. * Free MM table memory
  224. */
  225. void amdgpu_virt_free_mm_table(struct amdgpu_device *adev)
  226. {
  227. if (!amdgpu_sriov_vf(adev) || !adev->virt.mm_table.gpu_addr)
  228. return;
  229. amdgpu_bo_free_kernel(&adev->virt.mm_table.bo,
  230. &adev->virt.mm_table.gpu_addr,
  231. (void *)&adev->virt.mm_table.cpu_addr);
  232. adev->virt.mm_table.gpu_addr = 0;
  233. }