radeon_ring.c 23 KB

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