radeon_ring.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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/seq_file.h>
  30. #include <linux/slab.h>
  31. #include <drm/drmP.h>
  32. #include <drm/radeon_drm.h>
  33. #include "radeon_reg.h"
  34. #include "radeon.h"
  35. #include "atom.h"
  36. /*
  37. * IB
  38. * IBs (Indirect Buffers) and areas of GPU accessible memory where
  39. * commands are stored. You can put a pointer to the IB in the
  40. * command ring and the hw will fetch the commands from the IB
  41. * and execute them. Generally userspace acceleration drivers
  42. * produce command buffers which are send to the kernel and
  43. * put in IBs for execution by the requested ring.
  44. */
  45. static int radeon_debugfs_sa_init(struct radeon_device *rdev);
  46. /**
  47. * radeon_ib_get - request an IB (Indirect Buffer)
  48. *
  49. * @rdev: radeon_device pointer
  50. * @ring: ring index the IB is associated with
  51. * @ib: IB object returned
  52. * @size: requested IB size
  53. *
  54. * Request an IB (all asics). IBs are allocated using the
  55. * suballocator.
  56. * Returns 0 on success, error on failure.
  57. */
  58. int radeon_ib_get(struct radeon_device *rdev, int ring,
  59. struct radeon_ib *ib, struct radeon_vm *vm,
  60. unsigned size)
  61. {
  62. int r;
  63. r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256);
  64. if (r) {
  65. dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
  66. return r;
  67. }
  68. r = radeon_semaphore_create(rdev, &ib->semaphore);
  69. if (r) {
  70. return r;
  71. }
  72. ib->ring = ring;
  73. ib->fence = NULL;
  74. ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
  75. ib->vm = vm;
  76. if (vm) {
  77. /* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address
  78. * space and soffset is the offset inside the pool bo
  79. */
  80. ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET;
  81. } else {
  82. ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
  83. }
  84. ib->is_const_ib = false;
  85. return 0;
  86. }
  87. /**
  88. * radeon_ib_free - free an IB (Indirect Buffer)
  89. *
  90. * @rdev: radeon_device pointer
  91. * @ib: IB object to free
  92. *
  93. * Free an IB (all asics).
  94. */
  95. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
  96. {
  97. radeon_semaphore_free(rdev, &ib->semaphore, ib->fence);
  98. radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence);
  99. radeon_fence_unref(&ib->fence);
  100. }
  101. /**
  102. * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring
  103. *
  104. * @rdev: radeon_device pointer
  105. * @ib: IB object to schedule
  106. * @const_ib: Const IB to schedule (SI only)
  107. *
  108. * Schedule an IB on the associated ring (all asics).
  109. * Returns 0 on success, error on failure.
  110. *
  111. * On SI, there are two parallel engines fed from the primary ring,
  112. * the CE (Constant Engine) and the DE (Drawing Engine). Since
  113. * resource descriptors have moved to memory, the CE allows you to
  114. * prime the caches while the DE is updating register state so that
  115. * the resource descriptors will be already in cache when the draw is
  116. * processed. To accomplish this, the userspace driver submits two
  117. * IBs, one for the CE and one for the DE. If there is a CE IB (called
  118. * a CONST_IB), it will be put on the ring prior to the DE IB. Prior
  119. * to SI there was just a DE IB.
  120. */
  121. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
  122. struct radeon_ib *const_ib)
  123. {
  124. struct radeon_ring *ring = &rdev->ring[ib->ring];
  125. int r = 0;
  126. if (!ib->length_dw || !ring->ready) {
  127. /* TODO: Nothings in the ib we should report. */
  128. dev_err(rdev->dev, "couldn't schedule ib\n");
  129. return -EINVAL;
  130. }
  131. /* 64 dwords should be enough for fence too */
  132. r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_SYNCS * 8);
  133. if (r) {
  134. dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
  135. return r;
  136. }
  137. /* grab a vm id if necessary */
  138. if (ib->vm) {
  139. struct radeon_fence *vm_id_fence;
  140. vm_id_fence = radeon_vm_grab_id(rdev, ib->vm, ib->ring);
  141. radeon_semaphore_sync_to(ib->semaphore, vm_id_fence);
  142. }
  143. /* sync with other rings */
  144. r = radeon_semaphore_sync_rings(rdev, ib->semaphore, ib->ring);
  145. if (r) {
  146. dev_err(rdev->dev, "failed to sync rings (%d)\n", r);
  147. radeon_ring_unlock_undo(rdev, ring);
  148. return r;
  149. }
  150. if (ib->vm)
  151. radeon_vm_flush(rdev, ib->vm, ib->ring);
  152. if (const_ib) {
  153. radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
  154. radeon_semaphore_free(rdev, &const_ib->semaphore, NULL);
  155. }
  156. radeon_ring_ib_execute(rdev, ib->ring, ib);
  157. r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
  158. if (r) {
  159. dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
  160. radeon_ring_unlock_undo(rdev, ring);
  161. return r;
  162. }
  163. if (const_ib) {
  164. const_ib->fence = radeon_fence_ref(ib->fence);
  165. }
  166. if (ib->vm)
  167. radeon_vm_fence(rdev, ib->vm, ib->fence);
  168. radeon_ring_unlock_commit(rdev, ring);
  169. return 0;
  170. }
  171. /**
  172. * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
  173. *
  174. * @rdev: radeon_device pointer
  175. *
  176. * Initialize the suballocator to manage a pool of memory
  177. * for use as IBs (all asics).
  178. * Returns 0 on success, error on failure.
  179. */
  180. int radeon_ib_pool_init(struct radeon_device *rdev)
  181. {
  182. int r;
  183. if (rdev->ib_pool_ready) {
  184. return 0;
  185. }
  186. r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
  187. RADEON_IB_POOL_SIZE*64*1024,
  188. RADEON_GPU_PAGE_SIZE,
  189. RADEON_GEM_DOMAIN_GTT);
  190. if (r) {
  191. return r;
  192. }
  193. r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
  194. if (r) {
  195. return r;
  196. }
  197. rdev->ib_pool_ready = true;
  198. if (radeon_debugfs_sa_init(rdev)) {
  199. dev_err(rdev->dev, "failed to register debugfs file for SA\n");
  200. }
  201. return 0;
  202. }
  203. /**
  204. * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool
  205. *
  206. * @rdev: radeon_device pointer
  207. *
  208. * Tear down the suballocator managing the pool of memory
  209. * for use as IBs (all asics).
  210. */
  211. void radeon_ib_pool_fini(struct radeon_device *rdev)
  212. {
  213. if (rdev->ib_pool_ready) {
  214. radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
  215. radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
  216. rdev->ib_pool_ready = false;
  217. }
  218. }
  219. /**
  220. * radeon_ib_ring_tests - test IBs on the rings
  221. *
  222. * @rdev: radeon_device pointer
  223. *
  224. * Test an IB (Indirect Buffer) on each ring.
  225. * If the test fails, disable the ring.
  226. * Returns 0 on success, error if the primary GFX ring
  227. * IB test fails.
  228. */
  229. int radeon_ib_ring_tests(struct radeon_device *rdev)
  230. {
  231. unsigned i;
  232. int r;
  233. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  234. struct radeon_ring *ring = &rdev->ring[i];
  235. if (!ring->ready)
  236. continue;
  237. r = radeon_ib_test(rdev, i, ring);
  238. if (r) {
  239. ring->ready = false;
  240. rdev->needs_reset = false;
  241. if (i == RADEON_RING_TYPE_GFX_INDEX) {
  242. /* oh, oh, that's really bad */
  243. DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
  244. rdev->accel_working = false;
  245. return r;
  246. } else {
  247. /* still not good, but we can live with it */
  248. DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
  249. }
  250. }
  251. }
  252. return 0;
  253. }
  254. /*
  255. * Rings
  256. * Most engines on the GPU are fed via ring buffers. Ring
  257. * buffers are areas of GPU accessible memory that the host
  258. * writes commands into and the GPU reads commands out of.
  259. * There is a rptr (read pointer) that determines where the
  260. * GPU is currently reading, and a wptr (write pointer)
  261. * which determines where the host has written. When the
  262. * pointers are equal, the ring is idle. When the host
  263. * writes commands to the ring buffer, it increments the
  264. * wptr. The GPU then starts fetching commands and executes
  265. * them until the pointers are equal again.
  266. */
  267. static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
  268. /**
  269. * radeon_ring_write - write a value to the ring
  270. *
  271. * @ring: radeon_ring structure holding ring information
  272. * @v: dword (dw) value to write
  273. *
  274. * Write a value to the requested ring buffer (all asics).
  275. */
  276. void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
  277. {
  278. #if DRM_DEBUG_CODE
  279. if (ring->count_dw <= 0) {
  280. DRM_ERROR("radeon: writing more dwords to the ring than expected!\n");
  281. }
  282. #endif
  283. ring->ring[ring->wptr++] = v;
  284. ring->wptr &= ring->ptr_mask;
  285. ring->count_dw--;
  286. ring->ring_free_dw--;
  287. }
  288. /**
  289. * radeon_ring_supports_scratch_reg - check if the ring supports
  290. * writing to scratch registers
  291. *
  292. * @rdev: radeon_device pointer
  293. * @ring: radeon_ring structure holding ring information
  294. *
  295. * Check if a specific ring supports writing to scratch registers (all asics).
  296. * Returns true if the ring supports writing to scratch regs, false if not.
  297. */
  298. bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
  299. struct radeon_ring *ring)
  300. {
  301. switch (ring->idx) {
  302. case RADEON_RING_TYPE_GFX_INDEX:
  303. case CAYMAN_RING_TYPE_CP1_INDEX:
  304. case CAYMAN_RING_TYPE_CP2_INDEX:
  305. return true;
  306. default:
  307. return false;
  308. }
  309. }
  310. /**
  311. * radeon_ring_free_size - update the free size
  312. *
  313. * @rdev: radeon_device pointer
  314. * @ring: radeon_ring structure holding ring information
  315. *
  316. * Update the free dw slots in the ring buffer (all asics).
  317. */
  318. void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
  319. {
  320. uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
  321. /* This works because ring_size is a power of 2 */
  322. ring->ring_free_dw = rptr + (ring->ring_size / 4);
  323. ring->ring_free_dw -= ring->wptr;
  324. ring->ring_free_dw &= ring->ptr_mask;
  325. if (!ring->ring_free_dw) {
  326. /* this is an empty ring */
  327. ring->ring_free_dw = ring->ring_size / 4;
  328. /* update lockup info to avoid false positive */
  329. radeon_ring_lockup_update(rdev, ring);
  330. }
  331. }
  332. /**
  333. * radeon_ring_alloc - allocate space on the ring buffer
  334. *
  335. * @rdev: radeon_device pointer
  336. * @ring: radeon_ring structure holding ring information
  337. * @ndw: number of dwords to allocate in the ring buffer
  338. *
  339. * Allocate @ndw dwords in the ring buffer (all asics).
  340. * Returns 0 on success, error on failure.
  341. */
  342. int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  343. {
  344. int r;
  345. /* make sure we aren't trying to allocate more space than there is on the ring */
  346. if (ndw > (ring->ring_size / 4))
  347. return -ENOMEM;
  348. /* Align requested size with padding so unlock_commit can
  349. * pad safely */
  350. radeon_ring_free_size(rdev, ring);
  351. ndw = (ndw + ring->align_mask) & ~ring->align_mask;
  352. while (ndw > (ring->ring_free_dw - 1)) {
  353. radeon_ring_free_size(rdev, ring);
  354. if (ndw < ring->ring_free_dw) {
  355. break;
  356. }
  357. r = radeon_fence_wait_next(rdev, ring->idx);
  358. if (r)
  359. return r;
  360. }
  361. ring->count_dw = ndw;
  362. ring->wptr_old = ring->wptr;
  363. return 0;
  364. }
  365. /**
  366. * radeon_ring_lock - lock the ring and allocate space on it
  367. *
  368. * @rdev: radeon_device pointer
  369. * @ring: radeon_ring structure holding ring information
  370. * @ndw: number of dwords to allocate in the ring buffer
  371. *
  372. * Lock the ring and allocate @ndw dwords in the ring buffer
  373. * (all asics).
  374. * Returns 0 on success, error on failure.
  375. */
  376. int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  377. {
  378. int r;
  379. mutex_lock(&rdev->ring_lock);
  380. r = radeon_ring_alloc(rdev, ring, ndw);
  381. if (r) {
  382. mutex_unlock(&rdev->ring_lock);
  383. return r;
  384. }
  385. return 0;
  386. }
  387. /**
  388. * radeon_ring_commit - tell the GPU to execute the new
  389. * commands on the ring buffer
  390. *
  391. * @rdev: radeon_device pointer
  392. * @ring: radeon_ring structure holding ring information
  393. *
  394. * Update the wptr (write pointer) to tell the GPU to
  395. * execute new commands on the ring buffer (all asics).
  396. */
  397. void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  398. {
  399. /* We pad to match fetch size */
  400. while (ring->wptr & ring->align_mask) {
  401. radeon_ring_write(ring, ring->nop);
  402. }
  403. mb();
  404. radeon_ring_set_wptr(rdev, ring);
  405. }
  406. /**
  407. * radeon_ring_unlock_commit - tell the GPU to execute the new
  408. * commands on the ring buffer and unlock it
  409. *
  410. * @rdev: radeon_device pointer
  411. * @ring: radeon_ring structure holding ring information
  412. *
  413. * Call radeon_ring_commit() then unlock the ring (all asics).
  414. */
  415. void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  416. {
  417. radeon_ring_commit(rdev, ring);
  418. mutex_unlock(&rdev->ring_lock);
  419. }
  420. /**
  421. * radeon_ring_undo - reset the wptr
  422. *
  423. * @ring: radeon_ring structure holding ring information
  424. *
  425. * Reset the driver's copy of the wptr (all asics).
  426. */
  427. void radeon_ring_undo(struct radeon_ring *ring)
  428. {
  429. ring->wptr = ring->wptr_old;
  430. }
  431. /**
  432. * radeon_ring_unlock_undo - reset the wptr and unlock the ring
  433. *
  434. * @ring: radeon_ring structure holding ring information
  435. *
  436. * Call radeon_ring_undo() then unlock the ring (all asics).
  437. */
  438. void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
  439. {
  440. radeon_ring_undo(ring);
  441. mutex_unlock(&rdev->ring_lock);
  442. }
  443. /**
  444. * radeon_ring_lockup_update - update lockup variables
  445. *
  446. * @ring: radeon_ring structure holding ring information
  447. *
  448. * Update the last rptr value and timestamp (all asics).
  449. */
  450. void radeon_ring_lockup_update(struct radeon_device *rdev,
  451. struct radeon_ring *ring)
  452. {
  453. atomic_set(&ring->last_rptr, radeon_ring_get_rptr(rdev, ring));
  454. atomic64_set(&ring->last_activity, jiffies_64);
  455. }
  456. /**
  457. * radeon_ring_test_lockup() - check if ring is lockedup by recording information
  458. * @rdev: radeon device structure
  459. * @ring: radeon_ring structure holding ring information
  460. *
  461. */
  462. bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  463. {
  464. uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
  465. uint64_t last = atomic64_read(&ring->last_activity);
  466. uint64_t elapsed;
  467. if (rptr != atomic_read(&ring->last_rptr)) {
  468. /* ring is still working, no lockup */
  469. radeon_ring_lockup_update(rdev, ring);
  470. return false;
  471. }
  472. elapsed = jiffies_to_msecs(jiffies_64 - last);
  473. if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
  474. dev_err(rdev->dev, "ring %d stalled for more than %llumsec\n",
  475. ring->idx, elapsed);
  476. return true;
  477. }
  478. /* give a chance to the GPU ... */
  479. return false;
  480. }
  481. /**
  482. * radeon_ring_backup - Back up the content of a ring
  483. *
  484. * @rdev: radeon_device pointer
  485. * @ring: the ring we want to back up
  486. *
  487. * Saves all unprocessed commits from a ring, returns the number of dwords saved.
  488. */
  489. unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
  490. uint32_t **data)
  491. {
  492. unsigned size, ptr, i;
  493. /* just in case lock the ring */
  494. mutex_lock(&rdev->ring_lock);
  495. *data = NULL;
  496. if (ring->ring_obj == NULL) {
  497. mutex_unlock(&rdev->ring_lock);
  498. return 0;
  499. }
  500. /* it doesn't make sense to save anything if all fences are signaled */
  501. if (!radeon_fence_count_emitted(rdev, ring->idx)) {
  502. mutex_unlock(&rdev->ring_lock);
  503. return 0;
  504. }
  505. /* calculate the number of dw on the ring */
  506. if (ring->rptr_save_reg)
  507. ptr = RREG32(ring->rptr_save_reg);
  508. else if (rdev->wb.enabled)
  509. ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
  510. else {
  511. /* no way to read back the next rptr */
  512. mutex_unlock(&rdev->ring_lock);
  513. return 0;
  514. }
  515. size = ring->wptr + (ring->ring_size / 4);
  516. size -= ptr;
  517. size &= ring->ptr_mask;
  518. if (size == 0) {
  519. mutex_unlock(&rdev->ring_lock);
  520. return 0;
  521. }
  522. /* and then save the content of the ring */
  523. *data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
  524. if (!*data) {
  525. mutex_unlock(&rdev->ring_lock);
  526. return 0;
  527. }
  528. for (i = 0; i < size; ++i) {
  529. (*data)[i] = ring->ring[ptr++];
  530. ptr &= ring->ptr_mask;
  531. }
  532. mutex_unlock(&rdev->ring_lock);
  533. return size;
  534. }
  535. /**
  536. * radeon_ring_restore - append saved commands to the ring again
  537. *
  538. * @rdev: radeon_device pointer
  539. * @ring: ring to append commands to
  540. * @size: number of dwords we want to write
  541. * @data: saved commands
  542. *
  543. * Allocates space on the ring and restore the previously saved commands.
  544. */
  545. int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
  546. unsigned size, uint32_t *data)
  547. {
  548. int i, r;
  549. if (!size || !data)
  550. return 0;
  551. /* restore the saved ring content */
  552. r = radeon_ring_lock(rdev, ring, size);
  553. if (r)
  554. return r;
  555. for (i = 0; i < size; ++i) {
  556. radeon_ring_write(ring, data[i]);
  557. }
  558. radeon_ring_unlock_commit(rdev, ring);
  559. kfree(data);
  560. return 0;
  561. }
  562. /**
  563. * radeon_ring_init - init driver ring struct.
  564. *
  565. * @rdev: radeon_device pointer
  566. * @ring: radeon_ring structure holding ring information
  567. * @ring_size: size of the ring
  568. * @rptr_offs: offset of the rptr writeback location in the WB buffer
  569. * @nop: nop packet for this ring
  570. *
  571. * Initialize the driver information for the selected ring (all asics).
  572. * Returns 0 on success, error on failure.
  573. */
  574. int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
  575. unsigned rptr_offs, u32 nop)
  576. {
  577. int r;
  578. ring->ring_size = ring_size;
  579. ring->rptr_offs = rptr_offs;
  580. ring->nop = nop;
  581. /* Allocate ring buffer */
  582. if (ring->ring_obj == NULL) {
  583. r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
  584. RADEON_GEM_DOMAIN_GTT,
  585. NULL, &ring->ring_obj);
  586. if (r) {
  587. dev_err(rdev->dev, "(%d) ring create failed\n", r);
  588. return r;
  589. }
  590. r = radeon_bo_reserve(ring->ring_obj, false);
  591. if (unlikely(r != 0))
  592. return r;
  593. r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
  594. &ring->gpu_addr);
  595. if (r) {
  596. radeon_bo_unreserve(ring->ring_obj);
  597. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  598. return r;
  599. }
  600. r = radeon_bo_kmap(ring->ring_obj,
  601. (void **)&ring->ring);
  602. radeon_bo_unreserve(ring->ring_obj);
  603. if (r) {
  604. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  605. return r;
  606. }
  607. }
  608. ring->ptr_mask = (ring->ring_size / 4) - 1;
  609. ring->ring_free_dw = ring->ring_size / 4;
  610. if (rdev->wb.enabled) {
  611. u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
  612. ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
  613. ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
  614. }
  615. if (radeon_debugfs_ring_init(rdev, ring)) {
  616. DRM_ERROR("Failed to register debugfs file for rings !\n");
  617. }
  618. radeon_ring_lockup_update(rdev, ring);
  619. return 0;
  620. }
  621. /**
  622. * radeon_ring_fini - tear down the driver ring struct.
  623. *
  624. * @rdev: radeon_device pointer
  625. * @ring: radeon_ring structure holding ring information
  626. *
  627. * Tear down the driver information for the selected ring (all asics).
  628. */
  629. void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
  630. {
  631. int r;
  632. struct radeon_bo *ring_obj;
  633. mutex_lock(&rdev->ring_lock);
  634. ring_obj = ring->ring_obj;
  635. ring->ready = false;
  636. ring->ring = NULL;
  637. ring->ring_obj = NULL;
  638. mutex_unlock(&rdev->ring_lock);
  639. if (ring_obj) {
  640. r = radeon_bo_reserve(ring_obj, false);
  641. if (likely(r == 0)) {
  642. radeon_bo_kunmap(ring_obj);
  643. radeon_bo_unpin(ring_obj);
  644. radeon_bo_unreserve(ring_obj);
  645. }
  646. radeon_bo_unref(&ring_obj);
  647. }
  648. }
  649. /*
  650. * Debugfs info
  651. */
  652. #if defined(CONFIG_DEBUG_FS)
  653. static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
  654. {
  655. struct drm_info_node *node = (struct drm_info_node *) m->private;
  656. struct drm_device *dev = node->minor->dev;
  657. struct radeon_device *rdev = dev->dev_private;
  658. int ridx = *(int*)node->info_ent->data;
  659. struct radeon_ring *ring = &rdev->ring[ridx];
  660. uint32_t rptr, wptr, rptr_next;
  661. unsigned count, i, j;
  662. radeon_ring_free_size(rdev, ring);
  663. count = (ring->ring_size / 4) - ring->ring_free_dw;
  664. wptr = radeon_ring_get_wptr(rdev, ring);
  665. seq_printf(m, "wptr: 0x%08x [%5d]\n",
  666. wptr, wptr);
  667. rptr = radeon_ring_get_rptr(rdev, ring);
  668. seq_printf(m, "rptr: 0x%08x [%5d]\n",
  669. rptr, rptr);
  670. if (ring->rptr_save_reg) {
  671. rptr_next = RREG32(ring->rptr_save_reg);
  672. seq_printf(m, "rptr next(0x%04x): 0x%08x [%5d]\n",
  673. ring->rptr_save_reg, rptr_next, rptr_next);
  674. } else
  675. rptr_next = ~0;
  676. seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
  677. ring->wptr, ring->wptr);
  678. seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
  679. ring->last_semaphore_signal_addr);
  680. seq_printf(m, "last semaphore wait addr : 0x%016llx\n",
  681. ring->last_semaphore_wait_addr);
  682. seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
  683. seq_printf(m, "%u dwords in ring\n", count);
  684. if (!ring->ready)
  685. return 0;
  686. /* print 8 dw before current rptr as often it's the last executed
  687. * packet that is the root issue
  688. */
  689. i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
  690. for (j = 0; j <= (count + 32); j++) {
  691. seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
  692. if (rptr == i)
  693. seq_puts(m, " *");
  694. if (rptr_next == i)
  695. seq_puts(m, " #");
  696. seq_puts(m, "\n");
  697. i = (i + 1) & ring->ptr_mask;
  698. }
  699. return 0;
  700. }
  701. static int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
  702. static int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
  703. static int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
  704. static int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX;
  705. static int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
  706. static int r600_uvd_index = R600_RING_TYPE_UVD_INDEX;
  707. static int si_vce1_index = TN_RING_TYPE_VCE1_INDEX;
  708. static int si_vce2_index = TN_RING_TYPE_VCE2_INDEX;
  709. static struct drm_info_list radeon_debugfs_ring_info_list[] = {
  710. {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index},
  711. {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index},
  712. {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index},
  713. {"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index},
  714. {"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index},
  715. {"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index},
  716. {"radeon_ring_vce1", radeon_debugfs_ring_info, 0, &si_vce1_index},
  717. {"radeon_ring_vce2", radeon_debugfs_ring_info, 0, &si_vce2_index},
  718. };
  719. static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
  720. {
  721. struct drm_info_node *node = (struct drm_info_node *) m->private;
  722. struct drm_device *dev = node->minor->dev;
  723. struct radeon_device *rdev = dev->dev_private;
  724. radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
  725. return 0;
  726. }
  727. static struct drm_info_list radeon_debugfs_sa_list[] = {
  728. {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
  729. };
  730. #endif
  731. static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
  732. {
  733. #if defined(CONFIG_DEBUG_FS)
  734. unsigned i;
  735. for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
  736. struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
  737. int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
  738. unsigned r;
  739. if (&rdev->ring[ridx] != ring)
  740. continue;
  741. r = radeon_debugfs_add_files(rdev, info, 1);
  742. if (r)
  743. return r;
  744. }
  745. #endif
  746. return 0;
  747. }
  748. static int radeon_debugfs_sa_init(struct radeon_device *rdev)
  749. {
  750. #if defined(CONFIG_DEBUG_FS)
  751. return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
  752. #else
  753. return 0;
  754. #endif
  755. }