si_dma.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2013 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: Alex Deucher
  23. */
  24. #include <drm/drmP.h>
  25. #include "radeon.h"
  26. #include "radeon_asic.h"
  27. #include "sid.h"
  28. u32 si_gpu_check_soft_reset(struct radeon_device *rdev);
  29. /**
  30. * si_dma_is_lockup - Check if the DMA engine is locked up
  31. *
  32. * @rdev: radeon_device pointer
  33. * @ring: radeon_ring structure holding ring information
  34. *
  35. * Check if the async DMA engine is locked up.
  36. * Returns true if the engine appears to be locked up, false if not.
  37. */
  38. bool si_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  39. {
  40. u32 reset_mask = si_gpu_check_soft_reset(rdev);
  41. u32 mask;
  42. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  43. mask = RADEON_RESET_DMA;
  44. else
  45. mask = RADEON_RESET_DMA1;
  46. if (!(reset_mask & mask)) {
  47. radeon_ring_lockup_update(ring);
  48. return false;
  49. }
  50. /* force ring activities */
  51. radeon_ring_force_activity(rdev, ring);
  52. return radeon_ring_test_lockup(rdev, ring);
  53. }
  54. /**
  55. * si_dma_vm_set_page - update the page tables using the DMA
  56. *
  57. * @rdev: radeon_device pointer
  58. * @ib: indirect buffer to fill with commands
  59. * @pe: addr of the page entry
  60. * @addr: dst addr to write into pe
  61. * @count: number of page entries to update
  62. * @incr: increase next addr by incr bytes
  63. * @flags: access flags
  64. *
  65. * Update the page tables using the DMA (SI).
  66. */
  67. void si_dma_vm_set_page(struct radeon_device *rdev,
  68. struct radeon_ib *ib,
  69. uint64_t pe,
  70. uint64_t addr, unsigned count,
  71. uint32_t incr, uint32_t flags)
  72. {
  73. uint32_t r600_flags = cayman_vm_page_flags(rdev, flags);
  74. uint64_t value;
  75. unsigned ndw;
  76. if (flags & RADEON_VM_PAGE_SYSTEM) {
  77. while (count) {
  78. ndw = count * 2;
  79. if (ndw > 0xFFFFE)
  80. ndw = 0xFFFFE;
  81. /* for non-physically contiguous pages (system) */
  82. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 0, ndw);
  83. ib->ptr[ib->length_dw++] = pe;
  84. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  85. for (; ndw > 0; ndw -= 2, --count, pe += 8) {
  86. if (flags & RADEON_VM_PAGE_SYSTEM) {
  87. value = radeon_vm_map_gart(rdev, addr);
  88. value &= 0xFFFFFFFFFFFFF000ULL;
  89. } else if (flags & RADEON_VM_PAGE_VALID) {
  90. value = addr;
  91. } else {
  92. value = 0;
  93. }
  94. addr += incr;
  95. value |= r600_flags;
  96. ib->ptr[ib->length_dw++] = value;
  97. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  98. }
  99. }
  100. } else {
  101. while (count) {
  102. ndw = count * 2;
  103. if (ndw > 0xFFFFE)
  104. ndw = 0xFFFFE;
  105. if (flags & RADEON_VM_PAGE_VALID)
  106. value = addr;
  107. else
  108. value = 0;
  109. /* for physically contiguous pages (vram) */
  110. ib->ptr[ib->length_dw++] = DMA_PTE_PDE_PACKET(ndw);
  111. ib->ptr[ib->length_dw++] = pe; /* dst addr */
  112. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  113. ib->ptr[ib->length_dw++] = r600_flags; /* mask */
  114. ib->ptr[ib->length_dw++] = 0;
  115. ib->ptr[ib->length_dw++] = value; /* value */
  116. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  117. ib->ptr[ib->length_dw++] = incr; /* increment size */
  118. ib->ptr[ib->length_dw++] = 0;
  119. pe += ndw * 4;
  120. addr += (ndw / 2) * incr;
  121. count -= ndw / 2;
  122. }
  123. }
  124. while (ib->length_dw & 0x7)
  125. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0, 0);
  126. }
  127. void si_dma_vm_flush(struct radeon_device *rdev, int ridx, struct radeon_vm *vm)
  128. {
  129. struct radeon_ring *ring = &rdev->ring[ridx];
  130. if (vm == NULL)
  131. return;
  132. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0));
  133. if (vm->id < 8) {
  134. radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm->id << 2)) >> 2));
  135. } else {
  136. radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((vm->id - 8) << 2)) >> 2));
  137. }
  138. radeon_ring_write(ring, vm->pd_gpu_addr >> 12);
  139. /* flush hdp cache */
  140. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0));
  141. radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2));
  142. radeon_ring_write(ring, 1);
  143. /* bits 0-7 are the VM contexts0-7 */
  144. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0));
  145. radeon_ring_write(ring, (0xf << 16) | (VM_INVALIDATE_REQUEST >> 2));
  146. radeon_ring_write(ring, 1 << vm->id);
  147. }
  148. /**
  149. * si_copy_dma - copy pages using the DMA engine
  150. *
  151. * @rdev: radeon_device pointer
  152. * @src_offset: src GPU address
  153. * @dst_offset: dst GPU address
  154. * @num_gpu_pages: number of GPU pages to xfer
  155. * @fence: radeon fence object
  156. *
  157. * Copy GPU paging using the DMA engine (SI).
  158. * Used by the radeon ttm implementation to move pages if
  159. * registered as the asic copy callback.
  160. */
  161. int si_copy_dma(struct radeon_device *rdev,
  162. uint64_t src_offset, uint64_t dst_offset,
  163. unsigned num_gpu_pages,
  164. struct radeon_fence **fence)
  165. {
  166. struct radeon_semaphore *sem = NULL;
  167. int ring_index = rdev->asic->copy.dma_ring_index;
  168. struct radeon_ring *ring = &rdev->ring[ring_index];
  169. u32 size_in_bytes, cur_size_in_bytes;
  170. int i, num_loops;
  171. int r = 0;
  172. r = radeon_semaphore_create(rdev, &sem);
  173. if (r) {
  174. DRM_ERROR("radeon: moving bo (%d).\n", r);
  175. return r;
  176. }
  177. size_in_bytes = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT);
  178. num_loops = DIV_ROUND_UP(size_in_bytes, 0xfffff);
  179. r = radeon_ring_lock(rdev, ring, num_loops * 5 + 11);
  180. if (r) {
  181. DRM_ERROR("radeon: moving bo (%d).\n", r);
  182. radeon_semaphore_free(rdev, &sem, NULL);
  183. return r;
  184. }
  185. if (radeon_fence_need_sync(*fence, ring->idx)) {
  186. radeon_semaphore_sync_rings(rdev, sem, (*fence)->ring,
  187. ring->idx);
  188. radeon_fence_note_sync(*fence, ring->idx);
  189. } else {
  190. radeon_semaphore_free(rdev, &sem, NULL);
  191. }
  192. for (i = 0; i < num_loops; i++) {
  193. cur_size_in_bytes = size_in_bytes;
  194. if (cur_size_in_bytes > 0xFFFFF)
  195. cur_size_in_bytes = 0xFFFFF;
  196. size_in_bytes -= cur_size_in_bytes;
  197. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 1, 0, 0, cur_size_in_bytes));
  198. radeon_ring_write(ring, dst_offset & 0xffffffff);
  199. radeon_ring_write(ring, src_offset & 0xffffffff);
  200. radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff);
  201. radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff);
  202. src_offset += cur_size_in_bytes;
  203. dst_offset += cur_size_in_bytes;
  204. }
  205. r = radeon_fence_emit(rdev, fence, ring->idx);
  206. if (r) {
  207. radeon_ring_unlock_undo(rdev, ring);
  208. return r;
  209. }
  210. radeon_ring_unlock_commit(rdev, ring);
  211. radeon_semaphore_free(rdev, &sem, *fence);
  212. return r;
  213. }