radeon_ring.c 15 KB

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