r600_dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright 2013 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 "r600d.h"
  28. u32 r600_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. */
  40. /**
  41. * r600_dma_get_rptr - get the current read pointer
  42. *
  43. * @rdev: radeon_device pointer
  44. * @ring: radeon ring pointer
  45. *
  46. * Get the current rptr from the hardware (r6xx+).
  47. */
  48. uint32_t r600_dma_get_rptr(struct radeon_device *rdev,
  49. struct radeon_ring *ring)
  50. {
  51. u32 rptr;
  52. if (rdev->wb.enabled)
  53. rptr = rdev->wb.wb[ring->rptr_offs/4];
  54. else
  55. rptr = RREG32(DMA_RB_RPTR);
  56. return (rptr & 0x3fffc) >> 2;
  57. }
  58. /**
  59. * r600_dma_get_wptr - get the current write pointer
  60. *
  61. * @rdev: radeon_device pointer
  62. * @ring: radeon ring pointer
  63. *
  64. * Get the current wptr from the hardware (r6xx+).
  65. */
  66. uint32_t r600_dma_get_wptr(struct radeon_device *rdev,
  67. struct radeon_ring *ring)
  68. {
  69. return (RREG32(DMA_RB_WPTR) & 0x3fffc) >> 2;
  70. }
  71. /**
  72. * r600_dma_set_wptr - commit the write pointer
  73. *
  74. * @rdev: radeon_device pointer
  75. * @ring: radeon ring pointer
  76. *
  77. * Write the wptr back to the hardware (r6xx+).
  78. */
  79. void r600_dma_set_wptr(struct radeon_device *rdev,
  80. struct radeon_ring *ring)
  81. {
  82. WREG32(DMA_RB_WPTR, (ring->wptr << 2) & 0x3fffc);
  83. }
  84. /**
  85. * r600_dma_stop - stop the async dma engine
  86. *
  87. * @rdev: radeon_device pointer
  88. *
  89. * Stop the async dma engine (r6xx-evergreen).
  90. */
  91. void r600_dma_stop(struct radeon_device *rdev)
  92. {
  93. u32 rb_cntl = RREG32(DMA_RB_CNTL);
  94. if (rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX)
  95. radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
  96. rb_cntl &= ~DMA_RB_ENABLE;
  97. WREG32(DMA_RB_CNTL, rb_cntl);
  98. rdev->ring[R600_RING_TYPE_DMA_INDEX].ready = false;
  99. }
  100. /**
  101. * r600_dma_resume - setup and start the async dma engine
  102. *
  103. * @rdev: radeon_device pointer
  104. *
  105. * Set up the DMA ring buffer and enable it. (r6xx-evergreen).
  106. * Returns 0 for success, error for failure.
  107. */
  108. int r600_dma_resume(struct radeon_device *rdev)
  109. {
  110. struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
  111. u32 rb_cntl, dma_cntl, ib_cntl;
  112. u32 rb_bufsz;
  113. int r;
  114. /* Reset dma */
  115. if (rdev->family >= CHIP_RV770)
  116. WREG32(SRBM_SOFT_RESET, RV770_SOFT_RESET_DMA);
  117. else
  118. WREG32(SRBM_SOFT_RESET, SOFT_RESET_DMA);
  119. RREG32(SRBM_SOFT_RESET);
  120. udelay(50);
  121. WREG32(SRBM_SOFT_RESET, 0);
  122. WREG32(DMA_SEM_INCOMPLETE_TIMER_CNTL, 0);
  123. WREG32(DMA_SEM_WAIT_FAIL_TIMER_CNTL, 0);
  124. /* Set ring buffer size in dwords */
  125. rb_bufsz = order_base_2(ring->ring_size / 4);
  126. rb_cntl = rb_bufsz << 1;
  127. #ifdef __BIG_ENDIAN
  128. rb_cntl |= DMA_RB_SWAP_ENABLE | DMA_RPTR_WRITEBACK_SWAP_ENABLE;
  129. #endif
  130. WREG32(DMA_RB_CNTL, rb_cntl);
  131. /* Initialize the ring buffer's read and write pointers */
  132. WREG32(DMA_RB_RPTR, 0);
  133. WREG32(DMA_RB_WPTR, 0);
  134. /* set the wb address whether it's enabled or not */
  135. WREG32(DMA_RB_RPTR_ADDR_HI,
  136. upper_32_bits(rdev->wb.gpu_addr + R600_WB_DMA_RPTR_OFFSET) & 0xFF);
  137. WREG32(DMA_RB_RPTR_ADDR_LO,
  138. ((rdev->wb.gpu_addr + R600_WB_DMA_RPTR_OFFSET) & 0xFFFFFFFC));
  139. if (rdev->wb.enabled)
  140. rb_cntl |= DMA_RPTR_WRITEBACK_ENABLE;
  141. WREG32(DMA_RB_BASE, ring->gpu_addr >> 8);
  142. /* enable DMA IBs */
  143. ib_cntl = DMA_IB_ENABLE;
  144. #ifdef __BIG_ENDIAN
  145. ib_cntl |= DMA_IB_SWAP_ENABLE;
  146. #endif
  147. WREG32(DMA_IB_CNTL, ib_cntl);
  148. dma_cntl = RREG32(DMA_CNTL);
  149. dma_cntl &= ~CTXEMPTY_INT_ENABLE;
  150. WREG32(DMA_CNTL, dma_cntl);
  151. if (rdev->family >= CHIP_RV770)
  152. WREG32(DMA_MODE, 1);
  153. ring->wptr = 0;
  154. WREG32(DMA_RB_WPTR, ring->wptr << 2);
  155. WREG32(DMA_RB_CNTL, rb_cntl | DMA_RB_ENABLE);
  156. ring->ready = true;
  157. r = radeon_ring_test(rdev, R600_RING_TYPE_DMA_INDEX, ring);
  158. if (r) {
  159. ring->ready = false;
  160. return r;
  161. }
  162. if (rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX)
  163. radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
  164. return 0;
  165. }
  166. /**
  167. * r600_dma_fini - tear down the async dma engine
  168. *
  169. * @rdev: radeon_device pointer
  170. *
  171. * Stop the async dma engine and free the ring (r6xx-evergreen).
  172. */
  173. void r600_dma_fini(struct radeon_device *rdev)
  174. {
  175. r600_dma_stop(rdev);
  176. radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_DMA_INDEX]);
  177. }
  178. /**
  179. * r600_dma_is_lockup - Check if the DMA engine is locked up
  180. *
  181. * @rdev: radeon_device pointer
  182. * @ring: radeon_ring structure holding ring information
  183. *
  184. * Check if the async DMA engine is locked up.
  185. * Returns true if the engine appears to be locked up, false if not.
  186. */
  187. bool r600_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  188. {
  189. u32 reset_mask = r600_gpu_check_soft_reset(rdev);
  190. if (!(reset_mask & RADEON_RESET_DMA)) {
  191. radeon_ring_lockup_update(rdev, ring);
  192. return false;
  193. }
  194. return radeon_ring_test_lockup(rdev, ring);
  195. }
  196. /**
  197. * r600_dma_ring_test - simple async dma engine test
  198. *
  199. * @rdev: radeon_device pointer
  200. * @ring: radeon_ring structure holding ring information
  201. *
  202. * Test the DMA engine by writing using it to write an
  203. * value to memory. (r6xx-SI).
  204. * Returns 0 for success, error for failure.
  205. */
  206. int r600_dma_ring_test(struct radeon_device *rdev,
  207. struct radeon_ring *ring)
  208. {
  209. unsigned i;
  210. int r;
  211. void __iomem *ptr = (void *)rdev->vram_scratch.ptr;
  212. u32 tmp;
  213. if (!ptr) {
  214. DRM_ERROR("invalid vram scratch pointer\n");
  215. return -EINVAL;
  216. }
  217. tmp = 0xCAFEDEAD;
  218. writel(tmp, ptr);
  219. r = radeon_ring_lock(rdev, ring, 4);
  220. if (r) {
  221. DRM_ERROR("radeon: dma failed to lock ring %d (%d).\n", ring->idx, r);
  222. return r;
  223. }
  224. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
  225. radeon_ring_write(ring, rdev->vram_scratch.gpu_addr & 0xfffffffc);
  226. radeon_ring_write(ring, upper_32_bits(rdev->vram_scratch.gpu_addr) & 0xff);
  227. radeon_ring_write(ring, 0xDEADBEEF);
  228. radeon_ring_unlock_commit(rdev, ring);
  229. for (i = 0; i < rdev->usec_timeout; i++) {
  230. tmp = readl(ptr);
  231. if (tmp == 0xDEADBEEF)
  232. break;
  233. DRM_UDELAY(1);
  234. }
  235. if (i < rdev->usec_timeout) {
  236. DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
  237. } else {
  238. DRM_ERROR("radeon: ring %d test failed (0x%08X)\n",
  239. ring->idx, tmp);
  240. r = -EINVAL;
  241. }
  242. return r;
  243. }
  244. /**
  245. * r600_dma_fence_ring_emit - emit a fence on the DMA ring
  246. *
  247. * @rdev: radeon_device pointer
  248. * @fence: radeon fence object
  249. *
  250. * Add a DMA fence packet to the ring to write
  251. * the fence seq number and DMA trap packet to generate
  252. * an interrupt if needed (r6xx-r7xx).
  253. */
  254. void r600_dma_fence_ring_emit(struct radeon_device *rdev,
  255. struct radeon_fence *fence)
  256. {
  257. struct radeon_ring *ring = &rdev->ring[fence->ring];
  258. u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
  259. /* write the fence */
  260. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_FENCE, 0, 0, 0));
  261. radeon_ring_write(ring, addr & 0xfffffffc);
  262. radeon_ring_write(ring, (upper_32_bits(addr) & 0xff));
  263. radeon_ring_write(ring, lower_32_bits(fence->seq));
  264. /* generate an interrupt */
  265. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_TRAP, 0, 0, 0));
  266. }
  267. /**
  268. * r600_dma_semaphore_ring_emit - emit a semaphore on the dma ring
  269. *
  270. * @rdev: radeon_device pointer
  271. * @ring: radeon_ring structure holding ring information
  272. * @semaphore: radeon semaphore object
  273. * @emit_wait: wait or signal semaphore
  274. *
  275. * Add a DMA semaphore packet to the ring wait on or signal
  276. * other rings (r6xx-SI).
  277. */
  278. bool r600_dma_semaphore_ring_emit(struct radeon_device *rdev,
  279. struct radeon_ring *ring,
  280. struct radeon_semaphore *semaphore,
  281. bool emit_wait)
  282. {
  283. u64 addr = semaphore->gpu_addr;
  284. u32 s = emit_wait ? 0 : 1;
  285. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SEMAPHORE, 0, s, 0));
  286. radeon_ring_write(ring, addr & 0xfffffffc);
  287. radeon_ring_write(ring, upper_32_bits(addr) & 0xff);
  288. return true;
  289. }
  290. /**
  291. * r600_dma_ib_test - test an IB on the DMA engine
  292. *
  293. * @rdev: radeon_device pointer
  294. * @ring: radeon_ring structure holding ring information
  295. *
  296. * Test a simple IB in the DMA ring (r6xx-SI).
  297. * Returns 0 on success, error on failure.
  298. */
  299. int r600_dma_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
  300. {
  301. struct radeon_ib ib;
  302. unsigned i;
  303. int r;
  304. void __iomem *ptr = (void *)rdev->vram_scratch.ptr;
  305. u32 tmp = 0;
  306. if (!ptr) {
  307. DRM_ERROR("invalid vram scratch pointer\n");
  308. return -EINVAL;
  309. }
  310. tmp = 0xCAFEDEAD;
  311. writel(tmp, ptr);
  312. r = radeon_ib_get(rdev, ring->idx, &ib, NULL, 256);
  313. if (r) {
  314. DRM_ERROR("radeon: failed to get ib (%d).\n", r);
  315. return r;
  316. }
  317. ib.ptr[0] = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1);
  318. ib.ptr[1] = rdev->vram_scratch.gpu_addr & 0xfffffffc;
  319. ib.ptr[2] = upper_32_bits(rdev->vram_scratch.gpu_addr) & 0xff;
  320. ib.ptr[3] = 0xDEADBEEF;
  321. ib.length_dw = 4;
  322. r = radeon_ib_schedule(rdev, &ib, NULL);
  323. if (r) {
  324. radeon_ib_free(rdev, &ib);
  325. DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
  326. return r;
  327. }
  328. r = radeon_fence_wait(ib.fence, false);
  329. if (r) {
  330. DRM_ERROR("radeon: fence wait failed (%d).\n", r);
  331. return r;
  332. }
  333. for (i = 0; i < rdev->usec_timeout; i++) {
  334. tmp = readl(ptr);
  335. if (tmp == 0xDEADBEEF)
  336. break;
  337. DRM_UDELAY(1);
  338. }
  339. if (i < rdev->usec_timeout) {
  340. DRM_INFO("ib test on ring %d succeeded in %u usecs\n", ib.fence->ring, i);
  341. } else {
  342. DRM_ERROR("radeon: ib test failed (0x%08X)\n", tmp);
  343. r = -EINVAL;
  344. }
  345. radeon_ib_free(rdev, &ib);
  346. return r;
  347. }
  348. /**
  349. * r600_dma_ring_ib_execute - Schedule an IB on the DMA engine
  350. *
  351. * @rdev: radeon_device pointer
  352. * @ib: IB object to schedule
  353. *
  354. * Schedule an IB in the DMA ring (r6xx-r7xx).
  355. */
  356. void r600_dma_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
  357. {
  358. struct radeon_ring *ring = &rdev->ring[ib->ring];
  359. if (rdev->wb.enabled) {
  360. u32 next_rptr = ring->wptr + 4;
  361. while ((next_rptr & 7) != 5)
  362. next_rptr++;
  363. next_rptr += 3;
  364. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
  365. radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
  366. radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff);
  367. radeon_ring_write(ring, next_rptr);
  368. }
  369. /* The indirect buffer packet must end on an 8 DW boundary in the DMA ring.
  370. * Pad as necessary with NOPs.
  371. */
  372. while ((ring->wptr & 7) != 5)
  373. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0));
  374. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_INDIRECT_BUFFER, 0, 0, 0));
  375. radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0));
  376. radeon_ring_write(ring, (ib->length_dw << 16) | (upper_32_bits(ib->gpu_addr) & 0xFF));
  377. }
  378. /**
  379. * r600_copy_dma - copy pages using the DMA engine
  380. *
  381. * @rdev: radeon_device pointer
  382. * @src_offset: src GPU address
  383. * @dst_offset: dst GPU address
  384. * @num_gpu_pages: number of GPU pages to xfer
  385. * @fence: radeon fence object
  386. *
  387. * Copy GPU paging using the DMA engine (r6xx).
  388. * Used by the radeon ttm implementation to move pages if
  389. * registered as the asic copy callback.
  390. */
  391. int r600_copy_dma(struct radeon_device *rdev,
  392. uint64_t src_offset, uint64_t dst_offset,
  393. unsigned num_gpu_pages,
  394. struct radeon_fence **fence)
  395. {
  396. struct radeon_semaphore *sem = NULL;
  397. int ring_index = rdev->asic->copy.dma_ring_index;
  398. struct radeon_ring *ring = &rdev->ring[ring_index];
  399. u32 size_in_dw, cur_size_in_dw;
  400. int i, num_loops;
  401. int r = 0;
  402. r = radeon_semaphore_create(rdev, &sem);
  403. if (r) {
  404. DRM_ERROR("radeon: moving bo (%d).\n", r);
  405. return r;
  406. }
  407. size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4;
  408. num_loops = DIV_ROUND_UP(size_in_dw, 0xFFFE);
  409. r = radeon_ring_lock(rdev, ring, num_loops * 4 + 8);
  410. if (r) {
  411. DRM_ERROR("radeon: moving bo (%d).\n", r);
  412. radeon_semaphore_free(rdev, &sem, NULL);
  413. return r;
  414. }
  415. radeon_semaphore_sync_to(sem, *fence);
  416. radeon_semaphore_sync_rings(rdev, sem, ring->idx);
  417. for (i = 0; i < num_loops; i++) {
  418. cur_size_in_dw = size_in_dw;
  419. if (cur_size_in_dw > 0xFFFE)
  420. cur_size_in_dw = 0xFFFE;
  421. size_in_dw -= cur_size_in_dw;
  422. radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, 0, cur_size_in_dw));
  423. radeon_ring_write(ring, dst_offset & 0xfffffffc);
  424. radeon_ring_write(ring, src_offset & 0xfffffffc);
  425. radeon_ring_write(ring, (((upper_32_bits(dst_offset) & 0xff) << 16) |
  426. (upper_32_bits(src_offset) & 0xff)));
  427. src_offset += cur_size_in_dw * 4;
  428. dst_offset += cur_size_in_dw * 4;
  429. }
  430. r = radeon_fence_emit(rdev, fence, ring->idx);
  431. if (r) {
  432. radeon_ring_unlock_undo(rdev, ring);
  433. radeon_semaphore_free(rdev, &sem, NULL);
  434. return r;
  435. }
  436. radeon_ring_unlock_commit(rdev, ring);
  437. radeon_semaphore_free(rdev, &sem, *fence);
  438. return r;
  439. }