ni_dma.c 10.0 KB

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