ni_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 "radeon.h"
  25. #include "radeon_asic.h"
  26. #include "radeon_trace.h"
  27. #include "ni.h"
  28. #include "nid.h"
  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_get_rptr - get the current read pointer
  43. *
  44. * @rdev: radeon_device pointer
  45. * @ring: radeon ring pointer
  46. *
  47. * Get the current rptr from the hardware (cayman+).
  48. */
  49. uint32_t cayman_dma_get_rptr(struct radeon_device *rdev,
  50. struct radeon_ring *ring)
  51. {
  52. u32 rptr, reg;
  53. if (rdev->wb.enabled) {
  54. rptr = rdev->wb.wb[ring->rptr_offs/4];
  55. } else {
  56. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  57. reg = DMA_RB_RPTR + DMA0_REGISTER_OFFSET;
  58. else
  59. reg = DMA_RB_RPTR + DMA1_REGISTER_OFFSET;
  60. rptr = RREG32(reg);
  61. }
  62. return (rptr & 0x3fffc) >> 2;
  63. }
  64. /**
  65. * cayman_dma_get_wptr - get the current write pointer
  66. *
  67. * @rdev: radeon_device pointer
  68. * @ring: radeon ring pointer
  69. *
  70. * Get the current wptr from the hardware (cayman+).
  71. */
  72. uint32_t cayman_dma_get_wptr(struct radeon_device *rdev,
  73. struct radeon_ring *ring)
  74. {
  75. u32 reg;
  76. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  77. reg = DMA_RB_WPTR + DMA0_REGISTER_OFFSET;
  78. else
  79. reg = DMA_RB_WPTR + DMA1_REGISTER_OFFSET;
  80. return (RREG32(reg) & 0x3fffc) >> 2;
  81. }
  82. /**
  83. * cayman_dma_set_wptr - commit the write pointer
  84. *
  85. * @rdev: radeon_device pointer
  86. * @ring: radeon ring pointer
  87. *
  88. * Write the wptr back to the hardware (cayman+).
  89. */
  90. void cayman_dma_set_wptr(struct radeon_device *rdev,
  91. struct radeon_ring *ring)
  92. {
  93. u32 reg;
  94. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  95. reg = DMA_RB_WPTR + DMA0_REGISTER_OFFSET;
  96. else
  97. reg = DMA_RB_WPTR + DMA1_REGISTER_OFFSET;
  98. WREG32(reg, (ring->wptr << 2) & 0x3fffc);
  99. }
  100. /**
  101. * cayman_dma_ring_ib_execute - Schedule an IB on the DMA engine
  102. *
  103. * @rdev: radeon_device pointer
  104. * @ib: IB object to schedule
  105. *
  106. * Schedule an IB in the DMA ring (cayman-SI).
  107. */
  108. void cayman_dma_ring_ib_execute(struct radeon_device *rdev,
  109. struct radeon_ib *ib)
  110. {
  111. struct radeon_ring *ring = &rdev->ring[ib->ring];
  112. unsigned vm_id = ib->vm ? ib->vm->ids[ib->ring].id : 0;
  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, vm_id, 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. } else if (flags & R600_PTE_VALID) {
  327. value = addr;
  328. } else {
  329. value = 0;
  330. }
  331. addr += incr;
  332. value |= flags;
  333. ib->ptr[ib->length_dw++] = value;
  334. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  335. }
  336. }
  337. }
  338. /**
  339. * cayman_dma_vm_set_pages - update the page tables using the DMA
  340. *
  341. * @rdev: radeon_device pointer
  342. * @ib: indirect buffer to fill with commands
  343. * @pe: addr of the page entry
  344. * @addr: dst addr to write into pe
  345. * @count: number of page entries to update
  346. * @incr: increase next addr by incr bytes
  347. * @flags: hw access flags
  348. *
  349. * Update the page tables using the DMA (cayman/TN).
  350. */
  351. void cayman_dma_vm_set_pages(struct radeon_device *rdev,
  352. struct radeon_ib *ib,
  353. uint64_t pe,
  354. uint64_t addr, unsigned count,
  355. uint32_t incr, uint32_t flags)
  356. {
  357. uint64_t value;
  358. unsigned ndw;
  359. while (count) {
  360. ndw = count * 2;
  361. if (ndw > 0xFFFFE)
  362. ndw = 0xFFFFE;
  363. if (flags & R600_PTE_VALID)
  364. value = addr;
  365. else
  366. value = 0;
  367. /* for physically contiguous pages (vram) */
  368. ib->ptr[ib->length_dw++] = DMA_PTE_PDE_PACKET(ndw);
  369. ib->ptr[ib->length_dw++] = pe; /* dst addr */
  370. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  371. ib->ptr[ib->length_dw++] = flags; /* mask */
  372. ib->ptr[ib->length_dw++] = 0;
  373. ib->ptr[ib->length_dw++] = value; /* value */
  374. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  375. ib->ptr[ib->length_dw++] = incr; /* increment size */
  376. ib->ptr[ib->length_dw++] = 0;
  377. pe += ndw * 4;
  378. addr += (ndw / 2) * incr;
  379. count -= ndw / 2;
  380. }
  381. }
  382. /**
  383. * cayman_dma_vm_pad_ib - pad the IB to the required number of dw
  384. *
  385. * @ib: indirect buffer to fill with padding
  386. *
  387. */
  388. void cayman_dma_vm_pad_ib(struct radeon_ib *ib)
  389. {
  390. while (ib->length_dw & 0x7)
  391. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0);
  392. }
  393. void cayman_dma_vm_flush(struct radeon_device *rdev, struct radeon_ring *ring,
  394. unsigned vm_id, uint64_t pd_addr)
  395. {
  396. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  397. radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm_id << 2)) >> 2));
  398. radeon_ring_write(ring, pd_addr >> 12);
  399. /* flush hdp cache */
  400. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  401. radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2));
  402. radeon_ring_write(ring, 1);
  403. /* bits 0-7 are the VM contexts0-7 */
  404. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  405. radeon_ring_write(ring, (0xf << 16) | (VM_INVALIDATE_REQUEST >> 2));
  406. radeon_ring_write(ring, 1 << vm_id);
  407. /* wait for invalidate to complete */
  408. radeon_ring_write(ring, DMA_SRBM_READ_PACKET);
  409. radeon_ring_write(ring, (0xff << 20) | (VM_INVALIDATE_REQUEST >> 2));
  410. radeon_ring_write(ring, 0); /* mask */
  411. radeon_ring_write(ring, 0); /* value */
  412. }