amdgpu_virt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. int amdgpu_allocate_static_csa(struct amdgpu_device *adev)
  25. {
  26. int r;
  27. void *ptr;
  28. r = amdgpu_bo_create_kernel(adev, AMDGPU_CSA_SIZE, PAGE_SIZE,
  29. AMDGPU_GEM_DOMAIN_VRAM, &adev->virt.csa_obj,
  30. &adev->virt.csa_vmid0_addr, &ptr);
  31. if (r)
  32. return r;
  33. memset(ptr, 0, AMDGPU_CSA_SIZE);
  34. return 0;
  35. }
  36. /*
  37. * amdgpu_map_static_csa should be called during amdgpu_vm_init
  38. * it maps virtual address "AMDGPU_VA_RESERVED_SIZE - AMDGPU_CSA_SIZE"
  39. * to this VM, and each command submission of GFX should use this virtual
  40. * address within META_DATA init package to support SRIOV gfx preemption.
  41. */
  42. int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  43. {
  44. int r;
  45. struct amdgpu_bo_va *bo_va;
  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. INIT_LIST_HEAD(&list);
  51. INIT_LIST_HEAD(&csa_tv.head);
  52. csa_tv.bo = &adev->virt.csa_obj->tbo;
  53. csa_tv.shared = true;
  54. list_add(&csa_tv.head, &list);
  55. amdgpu_vm_get_pd_bo(vm, &list, &pd);
  56. r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
  57. if (r) {
  58. DRM_ERROR("failed to reserve CSA,PD BOs: err=%d\n", r);
  59. return r;
  60. }
  61. bo_va = amdgpu_vm_bo_add(adev, vm, adev->virt.csa_obj);
  62. if (!bo_va) {
  63. ttm_eu_backoff_reservation(&ticket, &list);
  64. DRM_ERROR("failed to create bo_va for static CSA\n");
  65. return -ENOMEM;
  66. }
  67. r = amdgpu_vm_bo_map(adev, bo_va, AMDGPU_CSA_VADDR, 0,AMDGPU_CSA_SIZE,
  68. AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
  69. AMDGPU_PTE_EXECUTABLE);
  70. if (r) {
  71. DRM_ERROR("failed to do bo_map on static CSA, err=%d\n", r);
  72. amdgpu_vm_bo_rmv(adev, bo_va);
  73. ttm_eu_backoff_reservation(&ticket, &list);
  74. kfree(bo_va);
  75. return r;
  76. }
  77. vm->csa_bo_va = bo_va;
  78. ttm_eu_backoff_reservation(&ticket, &list);
  79. return 0;
  80. }
  81. void amdgpu_virt_init_setting(struct amdgpu_device *adev)
  82. {
  83. mutex_init(&adev->virt.lock);
  84. }
  85. uint32_t amdgpu_virt_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
  86. {
  87. signed long r;
  88. uint32_t val;
  89. struct dma_fence *f;
  90. struct amdgpu_kiq *kiq = &adev->gfx.kiq;
  91. struct amdgpu_ring *ring = &kiq->ring;
  92. BUG_ON(!ring->funcs->emit_rreg);
  93. mutex_lock(&adev->virt.lock);
  94. amdgpu_ring_alloc(ring, 32);
  95. amdgpu_ring_emit_hdp_flush(ring);
  96. amdgpu_ring_emit_rreg(ring, reg);
  97. amdgpu_ring_emit_hdp_invalidate(ring);
  98. amdgpu_fence_emit(ring, &f);
  99. amdgpu_ring_commit(ring);
  100. mutex_unlock(&adev->virt.lock);
  101. r = dma_fence_wait(f, false);
  102. if (r)
  103. DRM_ERROR("wait for kiq fence error: %ld.\n", r);
  104. dma_fence_put(f);
  105. val = adev->wb.wb[adev->virt.reg_val_offs];
  106. return val;
  107. }
  108. void amdgpu_virt_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  109. {
  110. signed long r;
  111. struct dma_fence *f;
  112. struct amdgpu_kiq *kiq = &adev->gfx.kiq;
  113. struct amdgpu_ring *ring = &kiq->ring;
  114. BUG_ON(!ring->funcs->emit_wreg);
  115. mutex_lock(&adev->virt.lock);
  116. amdgpu_ring_alloc(ring, 32);
  117. amdgpu_ring_emit_hdp_flush(ring);
  118. amdgpu_ring_emit_wreg(ring, reg, v);
  119. amdgpu_ring_emit_hdp_invalidate(ring);
  120. amdgpu_fence_emit(ring, &f);
  121. amdgpu_ring_commit(ring);
  122. mutex_unlock(&adev->virt.lock);
  123. r = dma_fence_wait(f, false);
  124. if (r)
  125. DRM_ERROR("wait for kiq fence error: %ld.\n", r);
  126. dma_fence_put(f);
  127. }