radeon_ring.c 25 KB

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