ni_dma.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright 2010 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 "nid.h"
  28. u32 cayman_gpu_check_soft_reset(struct radeon_device *rdev);
  29. /*
  30. * DMA
  31. * Starting with R600, the GPU has an asynchronous
  32. * DMA engine. The programming model is very similar
  33. * to the 3D engine (ring buffer, IBs, etc.), but the
  34. * DMA controller has it's own packet format that is
  35. * different form the PM4 format used by the 3D engine.
  36. * It supports copying data, writing embedded data,
  37. * solid fills, and a number of other things. It also
  38. * has support for tiling/detiling of buffers.
  39. * Cayman and newer support two asynchronous DMA engines.
  40. */
  41. /**
  42. * cayman_dma_ring_ib_execute - Schedule an IB on the DMA engine
  43. *
  44. * @rdev: radeon_device pointer
  45. * @ib: IB object to schedule
  46. *
  47. * Schedule an IB in the DMA ring (cayman-SI).
  48. */
  49. void cayman_dma_ring_ib_execute(struct radeon_device *rdev,
  50. struct radeon_ib *ib)
  51. {
  52. struct radeon_ring *ring = &rdev->ring[ib->ring];
  53. if (rdev->wb.enabled) {
  54. u32 next_rptr = ring->wptr + 4;
  55. while ((next_rptr & 7) != 5)
  56. next_rptr++;
  57. next_rptr += 3;
  58. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
  59. radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
  60. radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff);
  61. radeon_ring_write(ring, next_rptr);
  62. }
  63. /* The indirect buffer packet must end on an 8 DW boundary in the DMA ring.
  64. * Pad as necessary with NOPs.
  65. */
  66. while ((ring->wptr & 7) != 5)
  67. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0));
  68. radeon_ring_write(ring, DMA_IB_PACKET(DMA_PACKET_INDIRECT_BUFFER, ib->vm ? ib->vm->id : 0, 0));
  69. radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0));
  70. radeon_ring_write(ring, (ib->length_dw << 12) | (upper_32_bits(ib->gpu_addr) & 0xFF));
  71. }
  72. /**
  73. * cayman_dma_stop - stop the async dma engines
  74. *
  75. * @rdev: radeon_device pointer
  76. *
  77. * Stop the async dma engines (cayman-SI).
  78. */
  79. void cayman_dma_stop(struct radeon_device *rdev)
  80. {
  81. u32 rb_cntl;
  82. radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
  83. /* dma0 */
  84. rb_cntl = RREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET);
  85. rb_cntl &= ~DMA_RB_ENABLE;
  86. WREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET, rb_cntl);
  87. /* dma1 */
  88. rb_cntl = RREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET);
  89. rb_cntl &= ~DMA_RB_ENABLE;
  90. WREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET, rb_cntl);
  91. rdev->ring[R600_RING_TYPE_DMA_INDEX].ready = false;
  92. rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready = false;
  93. }
  94. /**
  95. * cayman_dma_resume - setup and start the async dma engines
  96. *
  97. * @rdev: radeon_device pointer
  98. *
  99. * Set up the DMA ring buffers and enable them. (cayman-SI).
  100. * Returns 0 for success, error for failure.
  101. */
  102. int cayman_dma_resume(struct radeon_device *rdev)
  103. {
  104. struct radeon_ring *ring;
  105. u32 rb_cntl, dma_cntl, ib_cntl;
  106. u32 rb_bufsz;
  107. u32 reg_offset, wb_offset;
  108. int i, r;
  109. /* Reset dma */
  110. WREG32(SRBM_SOFT_RESET, SOFT_RESET_DMA | SOFT_RESET_DMA1);
  111. RREG32(SRBM_SOFT_RESET);
  112. udelay(50);
  113. WREG32(SRBM_SOFT_RESET, 0);
  114. for (i = 0; i < 2; i++) {
  115. if (i == 0) {
  116. ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
  117. reg_offset = DMA0_REGISTER_OFFSET;
  118. wb_offset = R600_WB_DMA_RPTR_OFFSET;
  119. } else {
  120. ring = &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX];
  121. reg_offset = DMA1_REGISTER_OFFSET;
  122. wb_offset = CAYMAN_WB_DMA1_RPTR_OFFSET;
  123. }
  124. WREG32(DMA_SEM_INCOMPLETE_TIMER_CNTL + reg_offset, 0);
  125. WREG32(DMA_SEM_WAIT_FAIL_TIMER_CNTL + reg_offset, 0);
  126. /* Set ring buffer size in dwords */
  127. rb_bufsz = order_base_2(ring->ring_size / 4);
  128. rb_cntl = rb_bufsz << 1;
  129. #ifdef __BIG_ENDIAN
  130. rb_cntl |= DMA_RB_SWAP_ENABLE | DMA_RPTR_WRITEBACK_SWAP_ENABLE;
  131. #endif
  132. WREG32(DMA_RB_CNTL + reg_offset, rb_cntl);
  133. /* Initialize the ring buffer's read and write pointers */
  134. WREG32(DMA_RB_RPTR + reg_offset, 0);
  135. WREG32(DMA_RB_WPTR + reg_offset, 0);
  136. /* set the wb address whether it's enabled or not */
  137. WREG32(DMA_RB_RPTR_ADDR_HI + reg_offset,
  138. upper_32_bits(rdev->wb.gpu_addr + wb_offset) & 0xFF);
  139. WREG32(DMA_RB_RPTR_ADDR_LO + reg_offset,
  140. ((rdev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC));
  141. if (rdev->wb.enabled)
  142. rb_cntl |= DMA_RPTR_WRITEBACK_ENABLE;
  143. WREG32(DMA_RB_BASE + reg_offset, ring->gpu_addr >> 8);
  144. /* enable DMA IBs */
  145. ib_cntl = DMA_IB_ENABLE | CMD_VMID_FORCE;
  146. #ifdef __BIG_ENDIAN
  147. ib_cntl |= DMA_IB_SWAP_ENABLE;
  148. #endif
  149. WREG32(DMA_IB_CNTL + reg_offset, ib_cntl);
  150. dma_cntl = RREG32(DMA_CNTL + reg_offset);
  151. dma_cntl &= ~CTXEMPTY_INT_ENABLE;
  152. WREG32(DMA_CNTL + reg_offset, dma_cntl);
  153. ring->wptr = 0;
  154. WREG32(DMA_RB_WPTR + reg_offset, ring->wptr << 2);
  155. ring->rptr = RREG32(DMA_RB_RPTR + reg_offset) >> 2;
  156. WREG32(DMA_RB_CNTL + reg_offset, rb_cntl | DMA_RB_ENABLE);
  157. ring->ready = true;
  158. r = radeon_ring_test(rdev, ring->idx, ring);
  159. if (r) {
  160. ring->ready = false;
  161. return r;
  162. }
  163. }
  164. radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
  165. return 0;
  166. }
  167. /**
  168. * cayman_dma_fini - tear down the async dma engines
  169. *
  170. * @rdev: radeon_device pointer
  171. *
  172. * Stop the async dma engines and free the rings (cayman-SI).
  173. */
  174. void cayman_dma_fini(struct radeon_device *rdev)
  175. {
  176. cayman_dma_stop(rdev);
  177. radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_DMA_INDEX]);
  178. radeon_ring_fini(rdev, &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX]);
  179. }
  180. /**
  181. * cayman_dma_is_lockup - Check if the DMA engine is locked up
  182. *
  183. * @rdev: radeon_device pointer
  184. * @ring: radeon_ring structure holding ring information
  185. *
  186. * Check if the async DMA engine is locked up.
  187. * Returns true if the engine appears to be locked up, false if not.
  188. */
  189. bool cayman_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  190. {
  191. u32 reset_mask = cayman_gpu_check_soft_reset(rdev);
  192. u32 mask;
  193. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  194. mask = RADEON_RESET_DMA;
  195. else
  196. mask = RADEON_RESET_DMA1;
  197. if (!(reset_mask & mask)) {
  198. radeon_ring_lockup_update(ring);
  199. return false;
  200. }
  201. /* force ring activities */
  202. radeon_ring_force_activity(rdev, ring);
  203. return radeon_ring_test_lockup(rdev, ring);
  204. }
  205. /**
  206. * cayman_dma_vm_set_page - update the page tables using the DMA
  207. *
  208. * @rdev: radeon_device pointer
  209. * @ib: indirect buffer to fill with commands
  210. * @pe: addr of the page entry
  211. * @addr: dst addr to write into pe
  212. * @count: number of page entries to update
  213. * @incr: increase next addr by incr bytes
  214. * @flags: access flags
  215. * @r600_flags: hw access flags
  216. *
  217. * Update the page tables using the DMA (cayman/TN).
  218. */
  219. void cayman_dma_vm_set_page(struct radeon_device *rdev,
  220. struct radeon_ib *ib,
  221. uint64_t pe,
  222. uint64_t addr, unsigned count,
  223. uint32_t incr, uint32_t flags)
  224. {
  225. uint32_t r600_flags = cayman_vm_page_flags(rdev, flags);
  226. uint64_t value;
  227. unsigned ndw;
  228. if ((flags & RADEON_VM_PAGE_SYSTEM) || (count == 1)) {
  229. while (count) {
  230. ndw = count * 2;
  231. if (ndw > 0xFFFFE)
  232. ndw = 0xFFFFE;
  233. /* for non-physically contiguous pages (system) */
  234. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, ndw);
  235. ib->ptr[ib->length_dw++] = pe;
  236. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  237. for (; ndw > 0; ndw -= 2, --count, pe += 8) {
  238. if (flags & RADEON_VM_PAGE_SYSTEM) {
  239. value = radeon_vm_map_gart(rdev, addr);
  240. value &= 0xFFFFFFFFFFFFF000ULL;
  241. } else if (flags & RADEON_VM_PAGE_VALID) {
  242. value = addr;
  243. } else {
  244. value = 0;
  245. }
  246. addr += incr;
  247. value |= r600_flags;
  248. ib->ptr[ib->length_dw++] = value;
  249. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  250. }
  251. }
  252. } else {
  253. while (count) {
  254. ndw = count * 2;
  255. if (ndw > 0xFFFFE)
  256. ndw = 0xFFFFE;
  257. if (flags & RADEON_VM_PAGE_VALID)
  258. value = addr;
  259. else
  260. value = 0;
  261. /* for physically contiguous pages (vram) */
  262. ib->ptr[ib->length_dw++] = DMA_PTE_PDE_PACKET(ndw);
  263. ib->ptr[ib->length_dw++] = pe; /* dst addr */
  264. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  265. ib->ptr[ib->length_dw++] = r600_flags; /* mask */
  266. ib->ptr[ib->length_dw++] = 0;
  267. ib->ptr[ib->length_dw++] = value; /* value */
  268. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  269. ib->ptr[ib->length_dw++] = incr; /* increment size */
  270. ib->ptr[ib->length_dw++] = 0;
  271. pe += ndw * 4;
  272. addr += (ndw / 2) * incr;
  273. count -= ndw / 2;
  274. }
  275. }
  276. while (ib->length_dw & 0x7)
  277. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0);
  278. }
  279. void cayman_dma_vm_flush(struct radeon_device *rdev, int ridx, struct radeon_vm *vm)
  280. {
  281. struct radeon_ring *ring = &rdev->ring[ridx];
  282. if (vm == NULL)
  283. return;
  284. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  285. radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm->id << 2)) >> 2));
  286. radeon_ring_write(ring, vm->pd_gpu_addr >> 12);
  287. /* flush hdp cache */
  288. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  289. radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2));
  290. radeon_ring_write(ring, 1);
  291. /* bits 0-7 are the VM contexts0-7 */
  292. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  293. radeon_ring_write(ring, (0xf << 16) | (VM_INVALIDATE_REQUEST >> 2));
  294. radeon_ring_write(ring, 1 << vm->id);
  295. }