ni_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. unsigned vm_id = ib->vm ? ib->vm->ids[ib->ring].id : 0;
  114. if (rdev->wb.enabled) {
  115. u32 next_rptr = ring->wptr + 4;
  116. while ((next_rptr & 7) != 5)
  117. next_rptr++;
  118. next_rptr += 3;
  119. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
  120. radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
  121. radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff);
  122. radeon_ring_write(ring, next_rptr);
  123. }
  124. /* The indirect buffer packet must end on an 8 DW boundary in the DMA ring.
  125. * Pad as necessary with NOPs.
  126. */
  127. while ((ring->wptr & 7) != 5)
  128. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0));
  129. radeon_ring_write(ring, DMA_IB_PACKET(DMA_PACKET_INDIRECT_BUFFER, vm_id, 0));
  130. radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0));
  131. radeon_ring_write(ring, (ib->length_dw << 12) | (upper_32_bits(ib->gpu_addr) & 0xFF));
  132. }
  133. /**
  134. * cayman_dma_stop - stop the async dma engines
  135. *
  136. * @rdev: radeon_device pointer
  137. *
  138. * Stop the async dma engines (cayman-SI).
  139. */
  140. void cayman_dma_stop(struct radeon_device *rdev)
  141. {
  142. u32 rb_cntl;
  143. if ((rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX) ||
  144. (rdev->asic->copy.copy_ring_index == CAYMAN_RING_TYPE_DMA1_INDEX))
  145. radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
  146. /* dma0 */
  147. rb_cntl = RREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET);
  148. rb_cntl &= ~DMA_RB_ENABLE;
  149. WREG32(DMA_RB_CNTL + DMA0_REGISTER_OFFSET, rb_cntl);
  150. /* dma1 */
  151. rb_cntl = RREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET);
  152. rb_cntl &= ~DMA_RB_ENABLE;
  153. WREG32(DMA_RB_CNTL + DMA1_REGISTER_OFFSET, rb_cntl);
  154. rdev->ring[R600_RING_TYPE_DMA_INDEX].ready = false;
  155. rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready = false;
  156. }
  157. /**
  158. * cayman_dma_resume - setup and start the async dma engines
  159. *
  160. * @rdev: radeon_device pointer
  161. *
  162. * Set up the DMA ring buffers and enable them. (cayman-SI).
  163. * Returns 0 for success, error for failure.
  164. */
  165. int cayman_dma_resume(struct radeon_device *rdev)
  166. {
  167. struct radeon_ring *ring;
  168. u32 rb_cntl, dma_cntl, ib_cntl;
  169. u32 rb_bufsz;
  170. u32 reg_offset, wb_offset;
  171. int i, r;
  172. for (i = 0; i < 2; i++) {
  173. if (i == 0) {
  174. ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
  175. reg_offset = DMA0_REGISTER_OFFSET;
  176. wb_offset = R600_WB_DMA_RPTR_OFFSET;
  177. } else {
  178. ring = &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX];
  179. reg_offset = DMA1_REGISTER_OFFSET;
  180. wb_offset = CAYMAN_WB_DMA1_RPTR_OFFSET;
  181. }
  182. WREG32(DMA_SEM_INCOMPLETE_TIMER_CNTL + reg_offset, 0);
  183. WREG32(DMA_SEM_WAIT_FAIL_TIMER_CNTL + reg_offset, 0);
  184. /* Set ring buffer size in dwords */
  185. rb_bufsz = order_base_2(ring->ring_size / 4);
  186. rb_cntl = rb_bufsz << 1;
  187. #ifdef __BIG_ENDIAN
  188. rb_cntl |= DMA_RB_SWAP_ENABLE | DMA_RPTR_WRITEBACK_SWAP_ENABLE;
  189. #endif
  190. WREG32(DMA_RB_CNTL + reg_offset, rb_cntl);
  191. /* Initialize the ring buffer's read and write pointers */
  192. WREG32(DMA_RB_RPTR + reg_offset, 0);
  193. WREG32(DMA_RB_WPTR + reg_offset, 0);
  194. /* set the wb address whether it's enabled or not */
  195. WREG32(DMA_RB_RPTR_ADDR_HI + reg_offset,
  196. upper_32_bits(rdev->wb.gpu_addr + wb_offset) & 0xFF);
  197. WREG32(DMA_RB_RPTR_ADDR_LO + reg_offset,
  198. ((rdev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC));
  199. if (rdev->wb.enabled)
  200. rb_cntl |= DMA_RPTR_WRITEBACK_ENABLE;
  201. WREG32(DMA_RB_BASE + reg_offset, ring->gpu_addr >> 8);
  202. /* enable DMA IBs */
  203. ib_cntl = DMA_IB_ENABLE | CMD_VMID_FORCE;
  204. #ifdef __BIG_ENDIAN
  205. ib_cntl |= DMA_IB_SWAP_ENABLE;
  206. #endif
  207. WREG32(DMA_IB_CNTL + reg_offset, ib_cntl);
  208. dma_cntl = RREG32(DMA_CNTL + reg_offset);
  209. dma_cntl &= ~CTXEMPTY_INT_ENABLE;
  210. WREG32(DMA_CNTL + reg_offset, dma_cntl);
  211. ring->wptr = 0;
  212. WREG32(DMA_RB_WPTR + reg_offset, ring->wptr << 2);
  213. WREG32(DMA_RB_CNTL + reg_offset, rb_cntl | DMA_RB_ENABLE);
  214. ring->ready = true;
  215. r = radeon_ring_test(rdev, ring->idx, ring);
  216. if (r) {
  217. ring->ready = false;
  218. return r;
  219. }
  220. }
  221. if ((rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX) ||
  222. (rdev->asic->copy.copy_ring_index == CAYMAN_RING_TYPE_DMA1_INDEX))
  223. radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
  224. return 0;
  225. }
  226. /**
  227. * cayman_dma_fini - tear down the async dma engines
  228. *
  229. * @rdev: radeon_device pointer
  230. *
  231. * Stop the async dma engines and free the rings (cayman-SI).
  232. */
  233. void cayman_dma_fini(struct radeon_device *rdev)
  234. {
  235. cayman_dma_stop(rdev);
  236. radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_DMA_INDEX]);
  237. radeon_ring_fini(rdev, &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX]);
  238. }
  239. /**
  240. * cayman_dma_is_lockup - Check if the DMA engine is locked up
  241. *
  242. * @rdev: radeon_device pointer
  243. * @ring: radeon_ring structure holding ring information
  244. *
  245. * Check if the async DMA engine is locked up.
  246. * Returns true if the engine appears to be locked up, false if not.
  247. */
  248. bool cayman_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  249. {
  250. u32 reset_mask = cayman_gpu_check_soft_reset(rdev);
  251. u32 mask;
  252. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  253. mask = RADEON_RESET_DMA;
  254. else
  255. mask = RADEON_RESET_DMA1;
  256. if (!(reset_mask & mask)) {
  257. radeon_ring_lockup_update(rdev, ring);
  258. return false;
  259. }
  260. return radeon_ring_test_lockup(rdev, ring);
  261. }
  262. /**
  263. * cayman_dma_vm_copy_pages - update PTEs by copying them from the GART
  264. *
  265. * @rdev: radeon_device pointer
  266. * @ib: indirect buffer to fill with commands
  267. * @pe: addr of the page entry
  268. * @src: src addr where to copy from
  269. * @count: number of page entries to update
  270. *
  271. * Update PTEs by copying them from the GART using the DMA (cayman/TN).
  272. */
  273. void cayman_dma_vm_copy_pages(struct radeon_device *rdev,
  274. struct radeon_ib *ib,
  275. uint64_t pe, uint64_t src,
  276. unsigned count)
  277. {
  278. unsigned ndw;
  279. while (count) {
  280. ndw = count * 2;
  281. if (ndw > 0xFFFFE)
  282. ndw = 0xFFFFE;
  283. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_COPY,
  284. 0, 0, ndw);
  285. ib->ptr[ib->length_dw++] = lower_32_bits(pe);
  286. ib->ptr[ib->length_dw++] = lower_32_bits(src);
  287. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  288. ib->ptr[ib->length_dw++] = upper_32_bits(src) & 0xff;
  289. pe += ndw * 4;
  290. src += ndw * 4;
  291. count -= ndw / 2;
  292. }
  293. }
  294. /**
  295. * cayman_dma_vm_write_pages - update PTEs by writing them manually
  296. *
  297. * @rdev: radeon_device pointer
  298. * @ib: indirect buffer to fill with commands
  299. * @pe: addr of the page entry
  300. * @addr: dst addr to write into pe
  301. * @count: number of page entries to update
  302. * @incr: increase next addr by incr bytes
  303. * @flags: hw access flags
  304. *
  305. * Update PTEs by writing them manually using the DMA (cayman/TN).
  306. */
  307. void cayman_dma_vm_write_pages(struct radeon_device *rdev,
  308. struct radeon_ib *ib,
  309. uint64_t pe,
  310. uint64_t addr, unsigned count,
  311. uint32_t incr, uint32_t flags)
  312. {
  313. uint64_t value;
  314. unsigned ndw;
  315. while (count) {
  316. ndw = count * 2;
  317. if (ndw > 0xFFFFE)
  318. ndw = 0xFFFFE;
  319. /* for non-physically contiguous pages (system) */
  320. ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_WRITE,
  321. 0, 0, ndw);
  322. ib->ptr[ib->length_dw++] = pe;
  323. ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
  324. for (; ndw > 0; ndw -= 2, --count, pe += 8) {
  325. if (flags & R600_PTE_SYSTEM) {
  326. value = radeon_vm_map_gart(rdev, addr);
  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, struct radeon_ring *ring,
  395. unsigned vm_id, uint64_t pd_addr)
  396. {
  397. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  398. radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm_id << 2)) >> 2));
  399. radeon_ring_write(ring, pd_addr >> 12);
  400. /* flush hdp cache */
  401. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  402. radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2));
  403. radeon_ring_write(ring, 1);
  404. /* bits 0-7 are the VM contexts0-7 */
  405. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0));
  406. radeon_ring_write(ring, (0xf << 16) | (VM_INVALIDATE_REQUEST >> 2));
  407. radeon_ring_write(ring, 1 << vm_id);
  408. /* wait for invalidate to complete */
  409. radeon_ring_write(ring, DMA_SRBM_READ_PACKET);
  410. radeon_ring_write(ring, (0xff << 20) | (VM_INVALIDATE_REQUEST >> 2));
  411. radeon_ring_write(ring, 0); /* mask */
  412. radeon_ring_write(ring, 0); /* value */
  413. }