radeon_ring.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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, true);
  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_RINGS * 8);
  133. if (r) {
  134. dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
  135. return r;
  136. }
  137. /* sync with other rings */
  138. r = radeon_semaphore_sync_rings(rdev, ib->semaphore, ib->ring);
  139. if (r) {
  140. dev_err(rdev->dev, "failed to sync rings (%d)\n", r);
  141. radeon_ring_unlock_undo(rdev, ring);
  142. return r;
  143. }
  144. /* if we can't remember our last VM flush then flush now! */
  145. /* XXX figure out why we have to flush for every IB */
  146. if (ib->vm /*&& !ib->vm->last_flush*/) {
  147. radeon_ring_vm_flush(rdev, ib->ring, ib->vm);
  148. }
  149. if (const_ib) {
  150. radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
  151. radeon_semaphore_free(rdev, &const_ib->semaphore, NULL);
  152. }
  153. radeon_ring_ib_execute(rdev, ib->ring, ib);
  154. r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
  155. if (r) {
  156. dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
  157. radeon_ring_unlock_undo(rdev, ring);
  158. return r;
  159. }
  160. if (const_ib) {
  161. const_ib->fence = radeon_fence_ref(ib->fence);
  162. }
  163. /* we just flushed the VM, remember that */
  164. if (ib->vm && !ib->vm->last_flush) {
  165. ib->vm->last_flush = radeon_fence_ref(ib->fence);
  166. }
  167. radeon_ring_unlock_commit(rdev, ring);
  168. return 0;
  169. }
  170. /**
  171. * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
  172. *
  173. * @rdev: radeon_device pointer
  174. *
  175. * Initialize the suballocator to manage a pool of memory
  176. * for use as IBs (all asics).
  177. * Returns 0 on success, error on failure.
  178. */
  179. int radeon_ib_pool_init(struct radeon_device *rdev)
  180. {
  181. int r;
  182. if (rdev->ib_pool_ready) {
  183. return 0;
  184. }
  185. r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
  186. RADEON_IB_POOL_SIZE*64*1024,
  187. RADEON_GPU_PAGE_SIZE,
  188. RADEON_GEM_DOMAIN_GTT);
  189. if (r) {
  190. return r;
  191. }
  192. r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
  193. if (r) {
  194. return r;
  195. }
  196. rdev->ib_pool_ready = true;
  197. if (radeon_debugfs_sa_init(rdev)) {
  198. dev_err(rdev->dev, "failed to register debugfs file for SA\n");
  199. }
  200. return 0;
  201. }
  202. /**
  203. * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool
  204. *
  205. * @rdev: radeon_device pointer
  206. *
  207. * Tear down the suballocator managing the pool of memory
  208. * for use as IBs (all asics).
  209. */
  210. void radeon_ib_pool_fini(struct radeon_device *rdev)
  211. {
  212. if (rdev->ib_pool_ready) {
  213. radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
  214. radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
  215. rdev->ib_pool_ready = false;
  216. }
  217. }
  218. /**
  219. * radeon_ib_ring_tests - test IBs on the rings
  220. *
  221. * @rdev: radeon_device pointer
  222. *
  223. * Test an IB (Indirect Buffer) on each ring.
  224. * If the test fails, disable the ring.
  225. * Returns 0 on success, error if the primary GFX ring
  226. * IB test fails.
  227. */
  228. int radeon_ib_ring_tests(struct radeon_device *rdev)
  229. {
  230. unsigned i;
  231. int r;
  232. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  233. struct radeon_ring *ring = &rdev->ring[i];
  234. if (!ring->ready)
  235. continue;
  236. r = radeon_ib_test(rdev, i, ring);
  237. if (r) {
  238. ring->ready = false;
  239. if (i == RADEON_RING_TYPE_GFX_INDEX) {
  240. /* oh, oh, that's really bad */
  241. DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
  242. rdev->accel_working = false;
  243. return r;
  244. } else {
  245. /* still not good, but we can live with it */
  246. DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
  247. }
  248. }
  249. }
  250. return 0;
  251. }
  252. /*
  253. * Rings
  254. * Most engines on the GPU are fed via ring buffers. Ring
  255. * buffers are areas of GPU accessible memory that the host
  256. * writes commands into and the GPU reads commands out of.
  257. * There is a rptr (read pointer) that determines where the
  258. * GPU is currently reading, and a wptr (write pointer)
  259. * which determines where the host has written. When the
  260. * pointers are equal, the ring is idle. When the host
  261. * writes commands to the ring buffer, it increments the
  262. * wptr. The GPU then starts fetching commands and executes
  263. * them until the pointers are equal again.
  264. */
  265. static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
  266. /**
  267. * radeon_ring_write - write a value to the ring
  268. *
  269. * @ring: radeon_ring structure holding ring information
  270. * @v: dword (dw) value to write
  271. *
  272. * Write a value to the requested ring buffer (all asics).
  273. */
  274. void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
  275. {
  276. #if DRM_DEBUG_CODE
  277. if (ring->count_dw <= 0) {
  278. DRM_ERROR("radeon: writing more dwords to the ring than expected!\n");
  279. }
  280. #endif
  281. ring->ring[ring->wptr++] = v;
  282. ring->wptr &= ring->ptr_mask;
  283. ring->count_dw--;
  284. ring->ring_free_dw--;
  285. }
  286. /**
  287. * radeon_ring_supports_scratch_reg - check if the ring supports
  288. * writing to scratch registers
  289. *
  290. * @rdev: radeon_device pointer
  291. * @ring: radeon_ring structure holding ring information
  292. *
  293. * Check if a specific ring supports writing to scratch registers (all asics).
  294. * Returns true if the ring supports writing to scratch regs, false if not.
  295. */
  296. bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
  297. struct radeon_ring *ring)
  298. {
  299. switch (ring->idx) {
  300. case RADEON_RING_TYPE_GFX_INDEX:
  301. case CAYMAN_RING_TYPE_CP1_INDEX:
  302. case CAYMAN_RING_TYPE_CP2_INDEX:
  303. return true;
  304. default:
  305. return false;
  306. }
  307. }
  308. u32 radeon_ring_generic_get_rptr(struct radeon_device *rdev,
  309. struct radeon_ring *ring)
  310. {
  311. u32 rptr;
  312. if (rdev->wb.enabled)
  313. rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
  314. else
  315. rptr = RREG32(ring->rptr_reg);
  316. return rptr;
  317. }
  318. u32 radeon_ring_generic_get_wptr(struct radeon_device *rdev,
  319. struct radeon_ring *ring)
  320. {
  321. u32 wptr;
  322. wptr = RREG32(ring->wptr_reg);
  323. return wptr;
  324. }
  325. void radeon_ring_generic_set_wptr(struct radeon_device *rdev,
  326. struct radeon_ring *ring)
  327. {
  328. WREG32(ring->wptr_reg, ring->wptr);
  329. (void)RREG32(ring->wptr_reg);
  330. }
  331. /**
  332. * radeon_ring_free_size - update the free size
  333. *
  334. * @rdev: radeon_device pointer
  335. * @ring: radeon_ring structure holding ring information
  336. *
  337. * Update the free dw slots in the ring buffer (all asics).
  338. */
  339. void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
  340. {
  341. ring->rptr = radeon_ring_get_rptr(rdev, ring);
  342. /* This works because ring_size is a power of 2 */
  343. ring->ring_free_dw = (ring->rptr + (ring->ring_size / 4));
  344. ring->ring_free_dw -= ring->wptr;
  345. ring->ring_free_dw &= ring->ptr_mask;
  346. if (!ring->ring_free_dw) {
  347. ring->ring_free_dw = ring->ring_size / 4;
  348. }
  349. }
  350. /**
  351. * radeon_ring_alloc - allocate space on the ring buffer
  352. *
  353. * @rdev: radeon_device pointer
  354. * @ring: radeon_ring structure holding ring information
  355. * @ndw: number of dwords to allocate in the ring buffer
  356. *
  357. * Allocate @ndw dwords in the ring buffer (all asics).
  358. * Returns 0 on success, error on failure.
  359. */
  360. int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  361. {
  362. int r;
  363. /* make sure we aren't trying to allocate more space than there is on the ring */
  364. if (ndw > (ring->ring_size / 4))
  365. return -ENOMEM;
  366. /* Align requested size with padding so unlock_commit can
  367. * pad safely */
  368. radeon_ring_free_size(rdev, ring);
  369. if (ring->ring_free_dw == (ring->ring_size / 4)) {
  370. /* This is an empty ring update lockup info to avoid
  371. * false positive.
  372. */
  373. radeon_ring_lockup_update(ring);
  374. }
  375. ndw = (ndw + ring->align_mask) & ~ring->align_mask;
  376. while (ndw > (ring->ring_free_dw - 1)) {
  377. radeon_ring_free_size(rdev, ring);
  378. if (ndw < ring->ring_free_dw) {
  379. break;
  380. }
  381. r = radeon_fence_wait_next_locked(rdev, ring->idx);
  382. if (r)
  383. return r;
  384. }
  385. ring->count_dw = ndw;
  386. ring->wptr_old = ring->wptr;
  387. return 0;
  388. }
  389. /**
  390. * radeon_ring_lock - lock the ring and allocate space on it
  391. *
  392. * @rdev: radeon_device pointer
  393. * @ring: radeon_ring structure holding ring information
  394. * @ndw: number of dwords to allocate in the ring buffer
  395. *
  396. * Lock the ring and allocate @ndw dwords in the ring buffer
  397. * (all asics).
  398. * Returns 0 on success, error on failure.
  399. */
  400. int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  401. {
  402. int r;
  403. mutex_lock(&rdev->ring_lock);
  404. r = radeon_ring_alloc(rdev, ring, ndw);
  405. if (r) {
  406. mutex_unlock(&rdev->ring_lock);
  407. return r;
  408. }
  409. return 0;
  410. }
  411. /**
  412. * radeon_ring_commit - tell the GPU to execute the new
  413. * commands on the ring buffer
  414. *
  415. * @rdev: radeon_device pointer
  416. * @ring: radeon_ring structure holding ring information
  417. *
  418. * Update the wptr (write pointer) to tell the GPU to
  419. * execute new commands on the ring buffer (all asics).
  420. */
  421. void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  422. {
  423. /* We pad to match fetch size */
  424. while (ring->wptr & ring->align_mask) {
  425. radeon_ring_write(ring, ring->nop);
  426. }
  427. DRM_MEMORYBARRIER();
  428. radeon_ring_set_wptr(rdev, ring);
  429. }
  430. /**
  431. * radeon_ring_unlock_commit - tell the GPU to execute the new
  432. * commands on the ring buffer and unlock it
  433. *
  434. * @rdev: radeon_device pointer
  435. * @ring: radeon_ring structure holding ring information
  436. *
  437. * Call radeon_ring_commit() then unlock the ring (all asics).
  438. */
  439. void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  440. {
  441. radeon_ring_commit(rdev, ring);
  442. mutex_unlock(&rdev->ring_lock);
  443. }
  444. /**
  445. * radeon_ring_undo - reset the wptr
  446. *
  447. * @ring: radeon_ring structure holding ring information
  448. *
  449. * Reset the driver's copy of the wptr (all asics).
  450. */
  451. void radeon_ring_undo(struct radeon_ring *ring)
  452. {
  453. ring->wptr = ring->wptr_old;
  454. }
  455. /**
  456. * radeon_ring_unlock_undo - reset the wptr and unlock the ring
  457. *
  458. * @ring: radeon_ring structure holding ring information
  459. *
  460. * Call radeon_ring_undo() then unlock the ring (all asics).
  461. */
  462. void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
  463. {
  464. radeon_ring_undo(ring);
  465. mutex_unlock(&rdev->ring_lock);
  466. }
  467. /**
  468. * radeon_ring_force_activity - add some nop packets to the ring
  469. *
  470. * @rdev: radeon_device pointer
  471. * @ring: radeon_ring structure holding ring information
  472. *
  473. * Add some nop packets to the ring to force activity (all asics).
  474. * Used for lockup detection to see if the rptr is advancing.
  475. */
  476. void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring)
  477. {
  478. int r;
  479. radeon_ring_free_size(rdev, ring);
  480. if (ring->rptr == ring->wptr) {
  481. r = radeon_ring_alloc(rdev, ring, 1);
  482. if (!r) {
  483. radeon_ring_write(ring, ring->nop);
  484. radeon_ring_commit(rdev, ring);
  485. }
  486. }
  487. }
  488. /**
  489. * radeon_ring_lockup_update - update lockup variables
  490. *
  491. * @ring: radeon_ring structure holding ring information
  492. *
  493. * Update the last rptr value and timestamp (all asics).
  494. */
  495. void radeon_ring_lockup_update(struct radeon_ring *ring)
  496. {
  497. ring->last_rptr = ring->rptr;
  498. ring->last_activity = jiffies;
  499. }
  500. /**
  501. * radeon_ring_test_lockup() - check if ring is lockedup by recording information
  502. * @rdev: radeon device structure
  503. * @ring: radeon_ring structure holding ring information
  504. *
  505. * We don't need to initialize the lockup tracking information as we will either
  506. * have CP rptr to a different value of jiffies wrap around which will force
  507. * initialization of the lockup tracking informations.
  508. *
  509. * A possible false positivie is if we get call after while and last_cp_rptr ==
  510. * the current CP rptr, even if it's unlikely it might happen. To avoid this
  511. * if the elapsed time since last call is bigger than 2 second than we return
  512. * false and update the tracking information. Due to this the caller must call
  513. * radeon_ring_test_lockup several time in less than 2sec for lockup to be reported
  514. * the fencing code should be cautious about that.
  515. *
  516. * Caller should write to the ring to force CP to do something so we don't get
  517. * false positive when CP is just gived nothing to do.
  518. *
  519. **/
  520. bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  521. {
  522. unsigned long cjiffies, elapsed;
  523. cjiffies = jiffies;
  524. if (!time_after(cjiffies, ring->last_activity)) {
  525. /* likely a wrap around */
  526. radeon_ring_lockup_update(ring);
  527. return false;
  528. }
  529. ring->rptr = radeon_ring_get_rptr(rdev, ring);
  530. if (ring->rptr != ring->last_rptr) {
  531. /* CP is still working no lockup */
  532. radeon_ring_lockup_update(ring);
  533. return false;
  534. }
  535. elapsed = jiffies_to_msecs(cjiffies - ring->last_activity);
  536. if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
  537. dev_err(rdev->dev, "GPU lockup CP stall for more than %lumsec\n", elapsed);
  538. return true;
  539. }
  540. /* give a chance to the GPU ... */
  541. return false;
  542. }
  543. /**
  544. * radeon_ring_backup - Back up the content of a ring
  545. *
  546. * @rdev: radeon_device pointer
  547. * @ring: the ring we want to back up
  548. *
  549. * Saves all unprocessed commits from a ring, returns the number of dwords saved.
  550. */
  551. unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
  552. uint32_t **data)
  553. {
  554. unsigned size, ptr, i;
  555. /* just in case lock the ring */
  556. mutex_lock(&rdev->ring_lock);
  557. *data = NULL;
  558. if (ring->ring_obj == NULL) {
  559. mutex_unlock(&rdev->ring_lock);
  560. return 0;
  561. }
  562. /* it doesn't make sense to save anything if all fences are signaled */
  563. if (!radeon_fence_count_emitted(rdev, ring->idx)) {
  564. mutex_unlock(&rdev->ring_lock);
  565. return 0;
  566. }
  567. /* calculate the number of dw on the ring */
  568. if (ring->rptr_save_reg)
  569. ptr = RREG32(ring->rptr_save_reg);
  570. else if (rdev->wb.enabled)
  571. ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
  572. else {
  573. /* no way to read back the next rptr */
  574. mutex_unlock(&rdev->ring_lock);
  575. return 0;
  576. }
  577. size = ring->wptr + (ring->ring_size / 4);
  578. size -= ptr;
  579. size &= ring->ptr_mask;
  580. if (size == 0) {
  581. mutex_unlock(&rdev->ring_lock);
  582. return 0;
  583. }
  584. /* and then save the content of the ring */
  585. *data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
  586. if (!*data) {
  587. mutex_unlock(&rdev->ring_lock);
  588. return 0;
  589. }
  590. for (i = 0; i < size; ++i) {
  591. (*data)[i] = ring->ring[ptr++];
  592. ptr &= ring->ptr_mask;
  593. }
  594. mutex_unlock(&rdev->ring_lock);
  595. return size;
  596. }
  597. /**
  598. * radeon_ring_restore - append saved commands to the ring again
  599. *
  600. * @rdev: radeon_device pointer
  601. * @ring: ring to append commands to
  602. * @size: number of dwords we want to write
  603. * @data: saved commands
  604. *
  605. * Allocates space on the ring and restore the previously saved commands.
  606. */
  607. int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
  608. unsigned size, uint32_t *data)
  609. {
  610. int i, r;
  611. if (!size || !data)
  612. return 0;
  613. /* restore the saved ring content */
  614. r = radeon_ring_lock(rdev, ring, size);
  615. if (r)
  616. return r;
  617. for (i = 0; i < size; ++i) {
  618. radeon_ring_write(ring, data[i]);
  619. }
  620. radeon_ring_unlock_commit(rdev, ring);
  621. kfree(data);
  622. return 0;
  623. }
  624. /**
  625. * radeon_ring_init - init driver ring struct.
  626. *
  627. * @rdev: radeon_device pointer
  628. * @ring: radeon_ring structure holding ring information
  629. * @ring_size: size of the ring
  630. * @rptr_offs: offset of the rptr writeback location in the WB buffer
  631. * @rptr_reg: MMIO offset of the rptr register
  632. * @wptr_reg: MMIO offset of the wptr register
  633. * @nop: nop packet for this ring
  634. *
  635. * Initialize the driver information for the selected ring (all asics).
  636. * Returns 0 on success, error on failure.
  637. */
  638. int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
  639. unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg, u32 nop)
  640. {
  641. int r;
  642. ring->ring_size = ring_size;
  643. ring->rptr_offs = rptr_offs;
  644. ring->rptr_reg = rptr_reg;
  645. ring->wptr_reg = wptr_reg;
  646. ring->nop = nop;
  647. /* Allocate ring buffer */
  648. if (ring->ring_obj == NULL) {
  649. r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
  650. RADEON_GEM_DOMAIN_GTT,
  651. NULL, &ring->ring_obj);
  652. if (r) {
  653. dev_err(rdev->dev, "(%d) ring create failed\n", r);
  654. return r;
  655. }
  656. r = radeon_bo_reserve(ring->ring_obj, false);
  657. if (unlikely(r != 0))
  658. return r;
  659. r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
  660. &ring->gpu_addr);
  661. if (r) {
  662. radeon_bo_unreserve(ring->ring_obj);
  663. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  664. return r;
  665. }
  666. r = radeon_bo_kmap(ring->ring_obj,
  667. (void **)&ring->ring);
  668. radeon_bo_unreserve(ring->ring_obj);
  669. if (r) {
  670. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  671. return r;
  672. }
  673. }
  674. ring->ptr_mask = (ring->ring_size / 4) - 1;
  675. ring->ring_free_dw = ring->ring_size / 4;
  676. if (rdev->wb.enabled) {
  677. u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
  678. ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
  679. ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
  680. }
  681. if (radeon_debugfs_ring_init(rdev, ring)) {
  682. DRM_ERROR("Failed to register debugfs file for rings !\n");
  683. }
  684. radeon_ring_lockup_update(ring);
  685. return 0;
  686. }
  687. /**
  688. * radeon_ring_fini - tear down the driver ring struct.
  689. *
  690. * @rdev: radeon_device pointer
  691. * @ring: radeon_ring structure holding ring information
  692. *
  693. * Tear down the driver information for the selected ring (all asics).
  694. */
  695. void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
  696. {
  697. int r;
  698. struct radeon_bo *ring_obj;
  699. mutex_lock(&rdev->ring_lock);
  700. ring_obj = ring->ring_obj;
  701. ring->ready = false;
  702. ring->ring = NULL;
  703. ring->ring_obj = NULL;
  704. mutex_unlock(&rdev->ring_lock);
  705. if (ring_obj) {
  706. r = radeon_bo_reserve(ring_obj, false);
  707. if (likely(r == 0)) {
  708. radeon_bo_kunmap(ring_obj);
  709. radeon_bo_unpin(ring_obj);
  710. radeon_bo_unreserve(ring_obj);
  711. }
  712. radeon_bo_unref(&ring_obj);
  713. }
  714. }
  715. /*
  716. * Debugfs info
  717. */
  718. #if defined(CONFIG_DEBUG_FS)
  719. static int radeon_debugfs_ring_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. int ridx = *(int*)node->info_ent->data;
  725. struct radeon_ring *ring = &rdev->ring[ridx];
  726. unsigned count, i, j;
  727. u32 tmp;
  728. radeon_ring_free_size(rdev, ring);
  729. count = (ring->ring_size / 4) - ring->ring_free_dw;
  730. tmp = radeon_ring_get_wptr(rdev, ring);
  731. seq_printf(m, "wptr(0x%04x): 0x%08x [%5d]\n", ring->wptr_reg, tmp, tmp);
  732. tmp = radeon_ring_get_rptr(rdev, ring);
  733. seq_printf(m, "rptr(0x%04x): 0x%08x [%5d]\n", ring->rptr_reg, tmp, tmp);
  734. if (ring->rptr_save_reg) {
  735. seq_printf(m, "rptr next(0x%04x): 0x%08x\n", ring->rptr_save_reg,
  736. RREG32(ring->rptr_save_reg));
  737. }
  738. seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n", ring->wptr, ring->wptr);
  739. seq_printf(m, "driver's copy of the rptr: 0x%08x [%5d]\n", ring->rptr, ring->rptr);
  740. seq_printf(m, "last semaphore signal addr : 0x%016llx\n", ring->last_semaphore_signal_addr);
  741. seq_printf(m, "last semaphore wait addr : 0x%016llx\n", ring->last_semaphore_wait_addr);
  742. seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
  743. seq_printf(m, "%u dwords in ring\n", count);
  744. /* print 8 dw before current rptr as often it's the last executed
  745. * packet that is the root issue
  746. */
  747. i = (ring->rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
  748. if (ring->ready) {
  749. for (j = 0; j <= (count + 32); j++) {
  750. seq_printf(m, "r[%5d]=0x%08x\n", i, ring->ring[i]);
  751. i = (i + 1) & ring->ptr_mask;
  752. }
  753. }
  754. return 0;
  755. }
  756. static int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
  757. static int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
  758. static int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
  759. static int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX;
  760. static int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
  761. static int r600_uvd_index = R600_RING_TYPE_UVD_INDEX;
  762. static struct drm_info_list radeon_debugfs_ring_info_list[] = {
  763. {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index},
  764. {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index},
  765. {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index},
  766. {"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index},
  767. {"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index},
  768. {"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index},
  769. };
  770. static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
  771. {
  772. struct drm_info_node *node = (struct drm_info_node *) m->private;
  773. struct drm_device *dev = node->minor->dev;
  774. struct radeon_device *rdev = dev->dev_private;
  775. radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
  776. return 0;
  777. }
  778. static struct drm_info_list radeon_debugfs_sa_list[] = {
  779. {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
  780. };
  781. #endif
  782. static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
  783. {
  784. #if defined(CONFIG_DEBUG_FS)
  785. unsigned i;
  786. for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
  787. struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
  788. int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
  789. unsigned r;
  790. if (&rdev->ring[ridx] != ring)
  791. continue;
  792. r = radeon_debugfs_add_files(rdev, info, 1);
  793. if (r)
  794. return r;
  795. }
  796. #endif
  797. return 0;
  798. }
  799. static int radeon_debugfs_sa_init(struct radeon_device *rdev)
  800. {
  801. #if defined(CONFIG_DEBUG_FS)
  802. return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
  803. #else
  804. return 0;
  805. #endif
  806. }