radeon_ring.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. * Christian König
  28. */
  29. #include <drm/drmP.h>
  30. #include "radeon.h"
  31. /*
  32. * Rings
  33. * Most engines on the GPU are fed via ring buffers. Ring
  34. * buffers are areas of GPU accessible memory that the host
  35. * writes commands into and the GPU reads commands out of.
  36. * There is a rptr (read pointer) that determines where the
  37. * GPU is currently reading, and a wptr (write pointer)
  38. * which determines where the host has written. When the
  39. * pointers are equal, the ring is idle. When the host
  40. * writes commands to the ring buffer, it increments the
  41. * wptr. The GPU then starts fetching commands and executes
  42. * them until the pointers are equal again.
  43. */
  44. static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
  45. /**
  46. * radeon_ring_supports_scratch_reg - check if the ring supports
  47. * writing to scratch registers
  48. *
  49. * @rdev: radeon_device pointer
  50. * @ring: radeon_ring structure holding ring information
  51. *
  52. * Check if a specific ring supports writing to scratch registers (all asics).
  53. * Returns true if the ring supports writing to scratch regs, false if not.
  54. */
  55. bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
  56. struct radeon_ring *ring)
  57. {
  58. switch (ring->idx) {
  59. case RADEON_RING_TYPE_GFX_INDEX:
  60. case CAYMAN_RING_TYPE_CP1_INDEX:
  61. case CAYMAN_RING_TYPE_CP2_INDEX:
  62. return true;
  63. default:
  64. return false;
  65. }
  66. }
  67. /**
  68. * radeon_ring_free_size - update the free size
  69. *
  70. * @rdev: radeon_device pointer
  71. * @ring: radeon_ring structure holding ring information
  72. *
  73. * Update the free dw slots in the ring buffer (all asics).
  74. */
  75. void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
  76. {
  77. uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
  78. /* This works because ring_size is a power of 2 */
  79. ring->ring_free_dw = rptr + (ring->ring_size / 4);
  80. ring->ring_free_dw -= ring->wptr;
  81. ring->ring_free_dw &= ring->ptr_mask;
  82. if (!ring->ring_free_dw) {
  83. /* this is an empty ring */
  84. ring->ring_free_dw = ring->ring_size / 4;
  85. /* update lockup info to avoid false positive */
  86. radeon_ring_lockup_update(rdev, ring);
  87. }
  88. }
  89. /**
  90. * radeon_ring_alloc - allocate space on the ring buffer
  91. *
  92. * @rdev: radeon_device pointer
  93. * @ring: radeon_ring structure holding ring information
  94. * @ndw: number of dwords to allocate in the ring buffer
  95. *
  96. * Allocate @ndw dwords in the ring buffer (all asics).
  97. * Returns 0 on success, error on failure.
  98. */
  99. int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  100. {
  101. int r;
  102. /* make sure we aren't trying to allocate more space than there is on the ring */
  103. if (ndw > (ring->ring_size / 4))
  104. return -ENOMEM;
  105. /* Align requested size with padding so unlock_commit can
  106. * pad safely */
  107. radeon_ring_free_size(rdev, ring);
  108. ndw = (ndw + ring->align_mask) & ~ring->align_mask;
  109. while (ndw > (ring->ring_free_dw - 1)) {
  110. radeon_ring_free_size(rdev, ring);
  111. if (ndw < ring->ring_free_dw) {
  112. break;
  113. }
  114. r = radeon_fence_wait_next(rdev, ring->idx);
  115. if (r)
  116. return r;
  117. }
  118. ring->count_dw = ndw;
  119. ring->wptr_old = ring->wptr;
  120. return 0;
  121. }
  122. /**
  123. * radeon_ring_lock - lock the ring and allocate space on it
  124. *
  125. * @rdev: radeon_device pointer
  126. * @ring: radeon_ring structure holding ring information
  127. * @ndw: number of dwords to allocate in the ring buffer
  128. *
  129. * Lock the ring and allocate @ndw dwords in the ring buffer
  130. * (all asics).
  131. * Returns 0 on success, error on failure.
  132. */
  133. int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  134. {
  135. int r;
  136. mutex_lock(&rdev->ring_lock);
  137. r = radeon_ring_alloc(rdev, ring, ndw);
  138. if (r) {
  139. mutex_unlock(&rdev->ring_lock);
  140. return r;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * radeon_ring_commit - tell the GPU to execute the new
  146. * commands on the ring buffer
  147. *
  148. * @rdev: radeon_device pointer
  149. * @ring: radeon_ring structure holding ring information
  150. * @hdp_flush: Whether or not to perform an HDP cache flush
  151. *
  152. * Update the wptr (write pointer) to tell the GPU to
  153. * execute new commands on the ring buffer (all asics).
  154. */
  155. void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring,
  156. bool hdp_flush)
  157. {
  158. /* If we are emitting the HDP flush via the ring buffer, we need to
  159. * do it before padding.
  160. */
  161. if (hdp_flush && rdev->asic->ring[ring->idx]->hdp_flush)
  162. rdev->asic->ring[ring->idx]->hdp_flush(rdev, ring);
  163. /* We pad to match fetch size */
  164. while (ring->wptr & ring->align_mask) {
  165. radeon_ring_write(ring, ring->nop);
  166. }
  167. mb();
  168. /* If we are emitting the HDP flush via MMIO, we need to do it after
  169. * all CPU writes to VRAM finished.
  170. */
  171. if (hdp_flush && rdev->asic->mmio_hdp_flush)
  172. rdev->asic->mmio_hdp_flush(rdev);
  173. radeon_ring_set_wptr(rdev, ring);
  174. }
  175. /**
  176. * radeon_ring_unlock_commit - tell the GPU to execute the new
  177. * commands on the ring buffer and unlock it
  178. *
  179. * @rdev: radeon_device pointer
  180. * @ring: radeon_ring structure holding ring information
  181. * @hdp_flush: Whether or not to perform an HDP cache flush
  182. *
  183. * Call radeon_ring_commit() then unlock the ring (all asics).
  184. */
  185. void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring,
  186. bool hdp_flush)
  187. {
  188. radeon_ring_commit(rdev, ring, hdp_flush);
  189. mutex_unlock(&rdev->ring_lock);
  190. }
  191. /**
  192. * radeon_ring_undo - reset the wptr
  193. *
  194. * @ring: radeon_ring structure holding ring information
  195. *
  196. * Reset the driver's copy of the wptr (all asics).
  197. */
  198. void radeon_ring_undo(struct radeon_ring *ring)
  199. {
  200. ring->wptr = ring->wptr_old;
  201. }
  202. /**
  203. * radeon_ring_unlock_undo - reset the wptr and unlock the ring
  204. *
  205. * @ring: radeon_ring structure holding ring information
  206. *
  207. * Call radeon_ring_undo() then unlock the ring (all asics).
  208. */
  209. void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
  210. {
  211. radeon_ring_undo(ring);
  212. mutex_unlock(&rdev->ring_lock);
  213. }
  214. /**
  215. * radeon_ring_lockup_update - update lockup variables
  216. *
  217. * @ring: radeon_ring structure holding ring information
  218. *
  219. * Update the last rptr value and timestamp (all asics).
  220. */
  221. void radeon_ring_lockup_update(struct radeon_device *rdev,
  222. struct radeon_ring *ring)
  223. {
  224. atomic_set(&ring->last_rptr, radeon_ring_get_rptr(rdev, ring));
  225. atomic64_set(&ring->last_activity, jiffies_64);
  226. }
  227. /**
  228. * radeon_ring_test_lockup() - check if ring is lockedup by recording information
  229. * @rdev: radeon device structure
  230. * @ring: radeon_ring structure holding ring information
  231. *
  232. */
  233. bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  234. {
  235. uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
  236. uint64_t last = atomic64_read(&ring->last_activity);
  237. uint64_t elapsed;
  238. if (rptr != atomic_read(&ring->last_rptr)) {
  239. /* ring is still working, no lockup */
  240. radeon_ring_lockup_update(rdev, ring);
  241. return false;
  242. }
  243. elapsed = jiffies_to_msecs(jiffies_64 - last);
  244. if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
  245. dev_err(rdev->dev, "ring %d stalled for more than %llumsec\n",
  246. ring->idx, elapsed);
  247. return true;
  248. }
  249. /* give a chance to the GPU ... */
  250. return false;
  251. }
  252. /**
  253. * radeon_ring_backup - Back up the content of a ring
  254. *
  255. * @rdev: radeon_device pointer
  256. * @ring: the ring we want to back up
  257. *
  258. * Saves all unprocessed commits from a ring, returns the number of dwords saved.
  259. */
  260. unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
  261. uint32_t **data)
  262. {
  263. unsigned size, ptr, i;
  264. /* just in case lock the ring */
  265. mutex_lock(&rdev->ring_lock);
  266. *data = NULL;
  267. if (ring->ring_obj == NULL) {
  268. mutex_unlock(&rdev->ring_lock);
  269. return 0;
  270. }
  271. /* it doesn't make sense to save anything if all fences are signaled */
  272. if (!radeon_fence_count_emitted(rdev, ring->idx)) {
  273. mutex_unlock(&rdev->ring_lock);
  274. return 0;
  275. }
  276. /* calculate the number of dw on the ring */
  277. if (ring->rptr_save_reg)
  278. ptr = RREG32(ring->rptr_save_reg);
  279. else if (rdev->wb.enabled)
  280. ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
  281. else {
  282. /* no way to read back the next rptr */
  283. mutex_unlock(&rdev->ring_lock);
  284. return 0;
  285. }
  286. size = ring->wptr + (ring->ring_size / 4);
  287. size -= ptr;
  288. size &= ring->ptr_mask;
  289. if (size == 0) {
  290. mutex_unlock(&rdev->ring_lock);
  291. return 0;
  292. }
  293. /* and then save the content of the ring */
  294. *data = drm_malloc_ab(size, sizeof(uint32_t));
  295. if (!*data) {
  296. mutex_unlock(&rdev->ring_lock);
  297. return 0;
  298. }
  299. for (i = 0; i < size; ++i) {
  300. (*data)[i] = ring->ring[ptr++];
  301. ptr &= ring->ptr_mask;
  302. }
  303. mutex_unlock(&rdev->ring_lock);
  304. return size;
  305. }
  306. /**
  307. * radeon_ring_restore - append saved commands to the ring again
  308. *
  309. * @rdev: radeon_device pointer
  310. * @ring: ring to append commands to
  311. * @size: number of dwords we want to write
  312. * @data: saved commands
  313. *
  314. * Allocates space on the ring and restore the previously saved commands.
  315. */
  316. int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
  317. unsigned size, uint32_t *data)
  318. {
  319. int i, r;
  320. if (!size || !data)
  321. return 0;
  322. /* restore the saved ring content */
  323. r = radeon_ring_lock(rdev, ring, size);
  324. if (r)
  325. return r;
  326. for (i = 0; i < size; ++i) {
  327. radeon_ring_write(ring, data[i]);
  328. }
  329. radeon_ring_unlock_commit(rdev, ring, false);
  330. drm_free_large(data);
  331. return 0;
  332. }
  333. /**
  334. * radeon_ring_init - init driver ring struct.
  335. *
  336. * @rdev: radeon_device pointer
  337. * @ring: radeon_ring structure holding ring information
  338. * @ring_size: size of the ring
  339. * @rptr_offs: offset of the rptr writeback location in the WB buffer
  340. * @nop: nop packet for this ring
  341. *
  342. * Initialize the driver information for the selected ring (all asics).
  343. * Returns 0 on success, error on failure.
  344. */
  345. int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
  346. unsigned rptr_offs, u32 nop)
  347. {
  348. int r;
  349. ring->ring_size = ring_size;
  350. ring->rptr_offs = rptr_offs;
  351. ring->nop = nop;
  352. /* Allocate ring buffer */
  353. if (ring->ring_obj == NULL) {
  354. r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
  355. RADEON_GEM_DOMAIN_GTT, 0, NULL,
  356. NULL, &ring->ring_obj);
  357. if (r) {
  358. dev_err(rdev->dev, "(%d) ring create failed\n", r);
  359. return r;
  360. }
  361. r = radeon_bo_reserve(ring->ring_obj, false);
  362. if (unlikely(r != 0))
  363. return r;
  364. r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
  365. &ring->gpu_addr);
  366. if (r) {
  367. radeon_bo_unreserve(ring->ring_obj);
  368. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  369. return r;
  370. }
  371. r = radeon_bo_kmap(ring->ring_obj,
  372. (void **)&ring->ring);
  373. radeon_bo_unreserve(ring->ring_obj);
  374. if (r) {
  375. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  376. return r;
  377. }
  378. }
  379. ring->ptr_mask = (ring->ring_size / 4) - 1;
  380. ring->ring_free_dw = ring->ring_size / 4;
  381. if (rdev->wb.enabled) {
  382. u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
  383. ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
  384. ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
  385. }
  386. if (radeon_debugfs_ring_init(rdev, ring)) {
  387. DRM_ERROR("Failed to register debugfs file for rings !\n");
  388. }
  389. radeon_ring_lockup_update(rdev, ring);
  390. return 0;
  391. }
  392. /**
  393. * radeon_ring_fini - tear down the driver ring struct.
  394. *
  395. * @rdev: radeon_device pointer
  396. * @ring: radeon_ring structure holding ring information
  397. *
  398. * Tear down the driver information for the selected ring (all asics).
  399. */
  400. void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
  401. {
  402. int r;
  403. struct radeon_bo *ring_obj;
  404. mutex_lock(&rdev->ring_lock);
  405. ring_obj = ring->ring_obj;
  406. ring->ready = false;
  407. ring->ring = NULL;
  408. ring->ring_obj = NULL;
  409. mutex_unlock(&rdev->ring_lock);
  410. if (ring_obj) {
  411. r = radeon_bo_reserve(ring_obj, false);
  412. if (likely(r == 0)) {
  413. radeon_bo_kunmap(ring_obj);
  414. radeon_bo_unpin(ring_obj);
  415. radeon_bo_unreserve(ring_obj);
  416. }
  417. radeon_bo_unref(&ring_obj);
  418. }
  419. }
  420. /*
  421. * Debugfs info
  422. */
  423. #if defined(CONFIG_DEBUG_FS)
  424. static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
  425. {
  426. struct drm_info_node *node = (struct drm_info_node *) m->private;
  427. struct drm_device *dev = node->minor->dev;
  428. struct radeon_device *rdev = dev->dev_private;
  429. int ridx = *(int*)node->info_ent->data;
  430. struct radeon_ring *ring = &rdev->ring[ridx];
  431. uint32_t rptr, wptr, rptr_next;
  432. unsigned count, i, j;
  433. radeon_ring_free_size(rdev, ring);
  434. count = (ring->ring_size / 4) - ring->ring_free_dw;
  435. wptr = radeon_ring_get_wptr(rdev, ring);
  436. seq_printf(m, "wptr: 0x%08x [%5d]\n",
  437. wptr, wptr);
  438. rptr = radeon_ring_get_rptr(rdev, ring);
  439. seq_printf(m, "rptr: 0x%08x [%5d]\n",
  440. rptr, rptr);
  441. if (ring->rptr_save_reg) {
  442. rptr_next = RREG32(ring->rptr_save_reg);
  443. seq_printf(m, "rptr next(0x%04x): 0x%08x [%5d]\n",
  444. ring->rptr_save_reg, rptr_next, rptr_next);
  445. } else
  446. rptr_next = ~0;
  447. seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
  448. ring->wptr, ring->wptr);
  449. seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
  450. ring->last_semaphore_signal_addr);
  451. seq_printf(m, "last semaphore wait addr : 0x%016llx\n",
  452. ring->last_semaphore_wait_addr);
  453. seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
  454. seq_printf(m, "%u dwords in ring\n", count);
  455. if (!ring->ring)
  456. return 0;
  457. /* print 8 dw before current rptr as often it's the last executed
  458. * packet that is the root issue
  459. */
  460. i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
  461. for (j = 0; j <= (count + 32); j++) {
  462. seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
  463. if (rptr == i)
  464. seq_puts(m, " *");
  465. if (rptr_next == i)
  466. seq_puts(m, " #");
  467. seq_puts(m, "\n");
  468. i = (i + 1) & ring->ptr_mask;
  469. }
  470. return 0;
  471. }
  472. static int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
  473. static int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
  474. static int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
  475. static int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX;
  476. static int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
  477. static int r600_uvd_index = R600_RING_TYPE_UVD_INDEX;
  478. static int si_vce1_index = TN_RING_TYPE_VCE1_INDEX;
  479. static int si_vce2_index = TN_RING_TYPE_VCE2_INDEX;
  480. static struct drm_info_list radeon_debugfs_ring_info_list[] = {
  481. {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index},
  482. {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index},
  483. {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index},
  484. {"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index},
  485. {"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index},
  486. {"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index},
  487. {"radeon_ring_vce1", radeon_debugfs_ring_info, 0, &si_vce1_index},
  488. {"radeon_ring_vce2", radeon_debugfs_ring_info, 0, &si_vce2_index},
  489. };
  490. #endif
  491. static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
  492. {
  493. #if defined(CONFIG_DEBUG_FS)
  494. unsigned i;
  495. for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
  496. struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
  497. int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
  498. unsigned r;
  499. if (&rdev->ring[ridx] != ring)
  500. continue;
  501. r = radeon_debugfs_add_files(rdev, info, 1);
  502. if (r)
  503. return r;
  504. }
  505. #endif
  506. return 0;
  507. }