ni_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_get_rptr - get the current read pointer
  44. *
  45. * @rdev: radeon_device pointer
  46. * @ring: radeon ring pointer
  47. *
  48. * Get the current rptr from the hardware (cayman+).
  49. */
  50. uint32_t cayman_dma_get_rptr(struct radeon_device *rdev,
  51. struct radeon_ring *ring)
  52. {
  53. u32 rptr, reg;
  54. if (rdev->wb.enabled) {
  55. rptr = rdev->wb.wb[ring->rptr_offs/4];
  56. } else {
  57. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  58. reg = DMA_RB_RPTR + DMA0_REGISTER_OFFSET;
  59. else
  60. reg = DMA_RB_RPTR + DMA1_REGISTER_OFFSET;
  61. rptr = RREG32(reg);
  62. }
  63. return (rptr & 0x3fffc) >> 2;
  64. }
  65. /**
  66. * cayman_dma_get_wptr - get the current write pointer
  67. *
  68. * @rdev: radeon_device pointer
  69. * @ring: radeon ring pointer
  70. *
  71. * Get the current wptr from the hardware (cayman+).
  72. */
  73. uint32_t cayman_dma_get_wptr(struct radeon_device *rdev,
  74. struct radeon_ring *ring)
  75. {
  76. u32 reg;
  77. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  78. reg = DMA_RB_WPTR + DMA0_REGISTER_OFFSET;
  79. else
  80. reg = DMA_RB_WPTR + DMA1_REGISTER_OFFSET;
  81. return (RREG32(reg) & 0x3fffc) >> 2;
  82. }
  83. /**
  84. * cayman_dma_set_wptr - commit the write pointer
  85. *
  86. * @rdev: radeon_device pointer
  87. * @ring: radeon ring pointer
  88. *
  89. * Write the wptr back to the hardware (cayman+).
  90. */
  91. void cayman_dma_set_wptr(struct radeon_device *rdev,
  92. struct radeon_ring *ring)
  93. {
  94. u32 reg;
  95. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  96. reg = DMA_RB_WPTR + DMA0_REGISTER_OFFSET;
  97. else
  98. reg = DMA_RB_WPTR + DMA1_REGISTER_OFFSET;
  99. WREG32(reg, (ring->wptr << 2) & 0x3fffc);
  100. }
  101. /**
  102. * cayman_dma_ring_ib_execute - Schedule an IB on the DMA engine
  103. *
  104. * @rdev: radeon_device pointer
  105. * @ib: IB object to schedule
  106. *
  107. * Schedule an IB in the DMA ring (cayman-SI).
  108. */
  109. void cayman_dma_ring_ib_execute(struct radeon_device *rdev,
  110. struct radeon_ib *ib)
  111. {
  112. struct radeon_ring *ring = &rdev->ring[ib->ring];
  113. if (rdev->wb.enabled) {
  114. u32 next_rptr = ring->wptr + 4;
  115. while ((next_rptr & 7) != 5)
  116. next_rptr++;
  117. next_rptr += 3;
  118. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
  119. radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
  120. radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff);
  121. radeon_ring_write(ring, next_rptr);
  122. }
  123. /* The indirect buffer packet must end on an 8 DW boundary in the DMA ring.
  124. * Pad as necessary with NOPs.
  125. */
  126. while ((ring->wptr & 7) != 5)
  127. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0));
  128. radeon_ring_write(ring, DMA_IB_PACKET(DMA_PACKET_INDIRECT_BUFFER, ib->vm ? ib->vm->id : 0, 0));
  129. radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0));
  130. radeon_ring_write(ring, (ib->length_dw << 12) | (upper_32_bits(ib->gpu_addr) & 0xFF));
  131. }
  132. /**
  133. * cayman_dma_stop - stop the async dma engines
  134. *
  135. * @rdev: radeon_device pointer
  136. *
  137. * Stop the async dma engines (cayman-SI).
  138. */
  139. void cayman_dma_stop(struct radeon_device *rdev)
  140. {
  141. u32 rb_cntl;
  142. if ((rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX) ||
  143. (rdev->asic->copy.copy_ring_index == CAYMAN_RING_TYPE_DMA1_INDEX))
  144. radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
  145. /* dma0 */
  146. rb_cntl = RREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET);
  147. rb_cntl &= ~DMA_RB_ENABLE;
  148. WREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET, rb_cntl);
  149. /* dma1 */
  150. rb_cntl = RREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET);
  151. rb_cntl &= ~DMA_RB_ENABLE;
  152. WREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET, rb_cntl);
  153. rdev->ring[R600_RING_TYPE_DMA_INDEX].ready = false;
  154. rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready = false;
  155. }
  156. /**
  157. * cayman_dma_resume - setup and start the async dma engines
  158. *
  159. * @rdev: radeon_device pointer
  160. *
  161. * Set up the DMA ring buffers and enable them. (cayman-SI).
  162. * Returns 0 for success, error for failure.
  163. */
  164. int cayman_dma_resume(struct radeon_device *rdev)
  165. {
  166. struct radeon_ring *ring;
  167. u32 rb_cntl, dma_cntl, ib_cntl;
  168. u32 rb_bufsz;
  169. u32 reg_offset, wb_offset;
  170. int i, r;
  171. for (i = 0; i < 2; i++) {
  172. if (i == 0) {
  173. ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
  174. reg_offset = DMA0_REGISTER_OFFSET;
  175. wb_offset = R600_WB_DMA_RPTR_OFFSET;
  176. } else {
  177. ring = &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX];
  178. reg_offset = DMA1_REGISTER_OFFSET;
  179. wb_offset = CAYMAN_WB_DMA1_RPTR_OFFSET;
  180. }
  181. WREG32(DMA_SEM_INCOMPLETE_TIMER_CNTL + reg_offset, 0);
  182. WREG32(DMA_SEM_WAIT_FAIL_TIMER_CNTL + reg_offset, 0);
  183. /* Set ring buffer size in dwords */
  184. rb_bufsz = order_base_2(ring->ring_size / 4);
  185. rb_cntl = rb_bufsz << 1;
  186. #ifdef __BIG_ENDIAN
  187. rb_cntl |= DMA_RB_SWAP_ENABLE | DMA_RPTR_WRITEBACK_SWAP_ENABLE;
  188. #endif
  189. WREG32(DMA_RB_CNTL + reg_offset, rb_cntl);
  190. /* Initialize the ring buffer's read and write pointers */
  191. WREG32(DMA_RB_RPTR + reg_offset, 0);
  192. WREG32(DMA_RB_WPTR + reg_offset, 0);
  193. /* set the wb address whether it's enabled or not */
  194. WREG32(DMA_RB_RPTR_ADDR_HI + reg_offset,
  195. upper_32_bits(rdev->wb.gpu_addr + wb_offset) & 0xFF);
  196. WREG32(DMA_RB_RPTR_ADDR_LO + reg_offset,
  197. ((rdev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC));
  198. if (rdev->wb.enabled)
  199. rb_cntl |= DMA_RPTR_WRITEBACK_ENABLE;
  200. WREG32(DMA_RB_BASE + reg_offset, ring->gpu_addr >> 8);
  201. /* enable DMA IBs */
  202. ib_cntl = DMA_IB_ENABLE | CMD_VMID_FORCE;
  203. #ifdef __BIG_ENDIAN
  204. ib_cntl |= DMA_IB_SWAP_ENABLE;
  205. #endif
  206. WREG32(DMA_IB_CNTL + reg_offset, ib_cntl);
  207. dma_cntl = RREG32(DMA_CNTL + reg_offset);
  208. dma_cntl &= ~CTXEMPTY_INT_ENABLE;
  209. WREG32(DMA_CNTL + reg_offset, dma_cntl);
  210. ring->wptr = 0;
  211. WREG32(DMA_RB_WPTR + reg_offset, ring->wptr << 2);
  212. WREG32(DMA_RB_CNTL + reg_offset, rb_cntl | DMA_RB_ENABLE);
  213. ring->ready = true;
  214. r = radeon_ring_test(rdev, ring->idx, ring);
  215. if (r) {
  216. ring->ready = false;
  217. return r;
  218. }
  219. }
  220. if ((rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX) ||
  221. (rdev->asic->copy.copy_ring_index == CAYMAN_RING_TYPE_DMA1_INDEX))
  222. radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
  223. return 0;
  224. }
  225. /**
  226. * cayman_dma_fini - tear down the async dma engines
  227. *
  228. * @rdev: radeon_device pointer
  229. *
  230. * Stop the async dma engines and free the rings (cayman-SI).
  231. */
  232. void cayman_dma_fini(struct radeon_device *rdev)
  233. {
  234. cayman_dma_stop(rdev);
  235. radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_DMA_INDEX]);
  236. radeon_ring_fini(rdev, &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX]);
  237. }
  238. /**
  239. * cayman_dma_is_lockup - Check if the DMA engine is locked up
  240. *
  241. * @rdev: radeon_device pointer
  242. * @ring: radeon_ring structure holding ring information
  243. *
  244. * Check if the async DMA engine is locked up.
  245. * Returns true if the engine appears to be locked up, false if not.
  246. */
  247. bool cayman_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  248. {
  249. u32 reset_mask = cayman_gpu_check_soft_reset(rdev);
  250. u32 mask;
  251. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  252. mask = RADEON_RESET_DMA;
  253. else
  254. mask = RADEON_RESET_DMA1;
  255. if (!(reset_mask & mask)) {
  256. radeon_ring_lockup_update(rdev, ring);
  257. return false;
  258. }
  259. return radeon_ring_test_lockup(rdev, ring);
  260. }
  261. /**
  262. * cayman_dma_vm_copy_pages - update PTEs by copying them from the GART
  263. *
  264. * @rdev: radeon_device pointer
  265. * @ib: indirect buffer to fill with commands
  266. * @pe: addr of the page entry
  267. * @src: src addr where to copy from
  268. * @count: number of page entries to update
  269. *
  270. * Update PTEs by copying them from the GART using the DMA (cayman/TN).
  271. */
  272. void cayman_dma_vm_copy_pages(struct radeon_device *rdev,
  273. struct radeon_ib *ib,
  274. uint64_t pe, uint64_t src,
  275. unsigned count)
  276. {
  277. unsigned ndw;
  278. while (count) {
  279. ndw = count * 2;
  280. if (ndw > 0xFFFFE)
  281. ndw = 0xFFFFE;
  282. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_COPY,
  283. 0, 0, ndw);
  284. ib->ptr[ib->length_dw++] = lower_32_bits(pe);
  285. ib->ptr[ib->length_dw++] = lower_32_bits(src);
  286. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  287. ib->ptr[ib->length_dw++] = upper_32_bits(src) & 0xff;
  288. pe += ndw * 4;
  289. src += ndw * 4;
  290. count -= ndw / 2;
  291. }
  292. }
  293. /**
  294. * cayman_dma_vm_write_pages - update PTEs by writing them manually
  295. *
  296. * @rdev: radeon_device pointer
  297. * @ib: indirect buffer to fill with commands
  298. * @pe: addr of the page entry
  299. * @addr: dst addr to write into pe
  300. * @count: number of page entries to update
  301. * @incr: increase next addr by incr bytes
  302. * @flags: hw access flags
  303. *
  304. * Update PTEs by writing them manually using the DMA (cayman/TN).
  305. */
  306. void cayman_dma_vm_write_pages(struct radeon_device *rdev,
  307. struct radeon_ib *ib,
  308. uint64_t pe,
  309. uint64_t addr, unsigned count,
  310. uint32_t incr, uint32_t flags)
  311. {
  312. uint64_t value;
  313. unsigned ndw;
  314. while (count) {
  315. ndw = count * 2;
  316. if (ndw > 0xFFFFE)
  317. ndw = 0xFFFFE;
  318. /* for non-physically contiguous pages (system) */
  319. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_WRITE,
  320. 0, 0, ndw);
  321. ib->ptr[ib->length_dw++] = pe;
  322. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  323. for (; ndw > 0; ndw -= 2, --count, pe += 8) {
  324. if (flags & R600_PTE_SYSTEM) {
  325. value = radeon_vm_map_gart(rdev, addr);
  326. value &= 0xFFFFFFFFFFFFF000ULL;
  327. } else if (flags & R600_PTE_VALID) {
  328. value = addr;
  329. } else {
  330. value = 0;
  331. }
  332. addr += incr;
  333. value |= flags;
  334. ib->ptr[ib->length_dw++] = value;
  335. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  336. }
  337. }
  338. }
  339. /**
  340. * cayman_dma_vm_set_pages - update the page tables using the DMA
  341. *
  342. * @rdev: radeon_device pointer
  343. * @ib: indirect buffer to fill with commands
  344. * @pe: addr of the page entry
  345. * @addr: dst addr to write into pe
  346. * @count: number of page entries to update
  347. * @incr: increase next addr by incr bytes
  348. * @flags: hw access flags
  349. *
  350. * Update the page tables using the DMA (cayman/TN).
  351. */
  352. void cayman_dma_vm_set_pages(struct radeon_device *rdev,
  353. struct radeon_ib *ib,
  354. uint64_t pe,
  355. uint64_t addr, unsigned count,
  356. uint32_t incr, uint32_t flags)
  357. {
  358. uint64_t value;
  359. unsigned ndw;
  360. while (count) {
  361. ndw = count * 2;
  362. if (ndw > 0xFFFFE)
  363. ndw = 0xFFFFE;
  364. if (flags & R600_PTE_VALID)
  365. value = addr;
  366. else
  367. value = 0;
  368. /* for physically contiguous pages (vram) */
  369. ib->ptr[ib->length_dw++] = DMA_PTE_PDE_PACKET(ndw);
  370. ib->ptr[ib->length_dw++] = pe; /* dst addr */
  371. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  372. ib->ptr[ib->length_dw++] = flags; /* mask */
  373. ib->ptr[ib->length_dw++] = 0;
  374. ib->ptr[ib->length_dw++] = value; /* value */
  375. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  376. ib->ptr[ib->length_dw++] = incr; /* increment size */
  377. ib->ptr[ib->length_dw++] = 0;
  378. pe += ndw * 4;
  379. addr += (ndw / 2) * incr;
  380. count -= ndw / 2;
  381. }
  382. }
  383. /**
  384. * cayman_dma_vm_pad_ib - pad the IB to the required number of dw
  385. *
  386. * @ib: indirect buffer to fill with padding
  387. *
  388. */
  389. void cayman_dma_vm_pad_ib(struct radeon_ib *ib)
  390. {
  391. while (ib->length_dw & 0x7)
  392. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0);
  393. }
  394. void cayman_dma_vm_flush(struct radeon_device *rdev, int ridx, struct radeon_vm *vm)
  395. {
  396. struct radeon_ring *ring = &rdev->ring[ridx];
  397. if (vm == NULL)
  398. return;
  399. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  400. radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm->id << 2)) >> 2));
  401. radeon_ring_write(ring, vm->pd_gpu_addr >> 12);
  402. /* flush hdp cache */
  403. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  404. radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2));
  405. radeon_ring_write(ring, 1);
  406. /* bits 0-7 are the VM contexts0-7 */
  407. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  408. radeon_ring_write(ring, (0xf << 16) | (VM_INVALIDATE_REQUEST >> 2));
  409. radeon_ring_write(ring, 1 << vm->id);
  410. }