radeon_ring.c 15 KB

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