radeon_ring.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. */
  39. int radeon_debugfs_sa_init(struct radeon_device *rdev);
  40. int radeon_ib_get(struct radeon_device *rdev, int ring,
  41. struct radeon_ib *ib, unsigned size)
  42. {
  43. int i, r;
  44. r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256, true);
  45. if (r) {
  46. dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
  47. return r;
  48. }
  49. r = radeon_semaphore_create(rdev, &ib->semaphore);
  50. if (r) {
  51. return r;
  52. }
  53. ib->ring = ring;
  54. ib->fence = NULL;
  55. ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
  56. ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
  57. ib->vm_id = 0;
  58. ib->is_const_ib = false;
  59. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  60. ib->sync_to[i] = NULL;
  61. return 0;
  62. }
  63. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
  64. {
  65. radeon_semaphore_free(rdev, &ib->semaphore, ib->fence);
  66. radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence);
  67. radeon_fence_unref(&ib->fence);
  68. }
  69. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
  70. {
  71. struct radeon_ring *ring = &rdev->ring[ib->ring];
  72. bool need_sync = false;
  73. int i, r = 0;
  74. if (!ib->length_dw || !ring->ready) {
  75. /* TODO: Nothings in the ib we should report. */
  76. dev_err(rdev->dev, "couldn't schedule ib\n");
  77. return -EINVAL;
  78. }
  79. /* 64 dwords should be enough for fence too */
  80. r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_RINGS * 8);
  81. if (r) {
  82. dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
  83. return r;
  84. }
  85. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  86. struct radeon_fence *fence = ib->sync_to[i];
  87. if (radeon_fence_need_sync(fence, ib->ring)) {
  88. need_sync = true;
  89. radeon_semaphore_sync_rings(rdev, ib->semaphore,
  90. fence->ring, ib->ring);
  91. radeon_fence_note_sync(fence, ib->ring);
  92. }
  93. }
  94. /* immediately free semaphore when we don't need to sync */
  95. if (!need_sync) {
  96. radeon_semaphore_free(rdev, &ib->semaphore, NULL);
  97. }
  98. radeon_ring_ib_execute(rdev, ib->ring, ib);
  99. r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
  100. if (r) {
  101. dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
  102. radeon_ring_unlock_undo(rdev, ring);
  103. return r;
  104. }
  105. radeon_ring_unlock_commit(rdev, ring);
  106. return 0;
  107. }
  108. int radeon_ib_pool_init(struct radeon_device *rdev)
  109. {
  110. int r;
  111. if (rdev->ib_pool_ready) {
  112. return 0;
  113. }
  114. r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
  115. RADEON_IB_POOL_SIZE*64*1024,
  116. RADEON_GEM_DOMAIN_GTT);
  117. if (r) {
  118. return r;
  119. }
  120. r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
  121. if (r) {
  122. return r;
  123. }
  124. rdev->ib_pool_ready = true;
  125. if (radeon_debugfs_sa_init(rdev)) {
  126. dev_err(rdev->dev, "failed to register debugfs file for SA\n");
  127. }
  128. return 0;
  129. }
  130. void radeon_ib_pool_fini(struct radeon_device *rdev)
  131. {
  132. if (rdev->ib_pool_ready) {
  133. radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
  134. radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
  135. rdev->ib_pool_ready = false;
  136. }
  137. }
  138. int radeon_ib_ring_tests(struct radeon_device *rdev)
  139. {
  140. unsigned i;
  141. int r;
  142. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  143. struct radeon_ring *ring = &rdev->ring[i];
  144. if (!ring->ready)
  145. continue;
  146. r = radeon_ib_test(rdev, i, ring);
  147. if (r) {
  148. ring->ready = false;
  149. if (i == RADEON_RING_TYPE_GFX_INDEX) {
  150. /* oh, oh, that's really bad */
  151. DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
  152. rdev->accel_working = false;
  153. return r;
  154. } else {
  155. /* still not good, but we can live with it */
  156. DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
  157. }
  158. }
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Ring.
  164. */
  165. int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
  166. void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
  167. {
  168. #if DRM_DEBUG_CODE
  169. if (ring->count_dw <= 0) {
  170. DRM_ERROR("radeon: writting more dword to ring than expected !\n");
  171. }
  172. #endif
  173. ring->ring[ring->wptr++] = v;
  174. ring->wptr &= ring->ptr_mask;
  175. ring->count_dw--;
  176. ring->ring_free_dw--;
  177. }
  178. int radeon_ring_index(struct radeon_device *rdev, struct radeon_ring *ring)
  179. {
  180. /* r1xx-r5xx only has CP ring */
  181. if (rdev->family < CHIP_R600)
  182. return RADEON_RING_TYPE_GFX_INDEX;
  183. if (rdev->family >= CHIP_CAYMAN) {
  184. if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX])
  185. return CAYMAN_RING_TYPE_CP1_INDEX;
  186. else if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX])
  187. return CAYMAN_RING_TYPE_CP2_INDEX;
  188. }
  189. return RADEON_RING_TYPE_GFX_INDEX;
  190. }
  191. void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
  192. {
  193. u32 rptr;
  194. if (rdev->wb.enabled)
  195. rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
  196. else
  197. rptr = RREG32(ring->rptr_reg);
  198. ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
  199. /* This works because ring_size is a power of 2 */
  200. ring->ring_free_dw = (ring->rptr + (ring->ring_size / 4));
  201. ring->ring_free_dw -= ring->wptr;
  202. ring->ring_free_dw &= ring->ptr_mask;
  203. if (!ring->ring_free_dw) {
  204. ring->ring_free_dw = ring->ring_size / 4;
  205. }
  206. }
  207. int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  208. {
  209. int r;
  210. /* Align requested size with padding so unlock_commit can
  211. * pad safely */
  212. ndw = (ndw + ring->align_mask) & ~ring->align_mask;
  213. while (ndw > (ring->ring_free_dw - 1)) {
  214. radeon_ring_free_size(rdev, ring);
  215. if (ndw < ring->ring_free_dw) {
  216. break;
  217. }
  218. r = radeon_fence_wait_next_locked(rdev, radeon_ring_index(rdev, ring));
  219. if (r)
  220. return r;
  221. }
  222. ring->count_dw = ndw;
  223. ring->wptr_old = ring->wptr;
  224. return 0;
  225. }
  226. int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  227. {
  228. int r;
  229. mutex_lock(&rdev->ring_lock);
  230. r = radeon_ring_alloc(rdev, ring, ndw);
  231. if (r) {
  232. mutex_unlock(&rdev->ring_lock);
  233. return r;
  234. }
  235. return 0;
  236. }
  237. void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  238. {
  239. /* We pad to match fetch size */
  240. while (ring->wptr & ring->align_mask) {
  241. radeon_ring_write(ring, ring->nop);
  242. }
  243. DRM_MEMORYBARRIER();
  244. WREG32(ring->wptr_reg, (ring->wptr << ring->ptr_reg_shift) & ring->ptr_reg_mask);
  245. (void)RREG32(ring->wptr_reg);
  246. }
  247. void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  248. {
  249. radeon_ring_commit(rdev, ring);
  250. mutex_unlock(&rdev->ring_lock);
  251. }
  252. void radeon_ring_undo(struct radeon_ring *ring)
  253. {
  254. ring->wptr = ring->wptr_old;
  255. }
  256. void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
  257. {
  258. radeon_ring_undo(ring);
  259. mutex_unlock(&rdev->ring_lock);
  260. }
  261. void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring)
  262. {
  263. int r;
  264. radeon_ring_free_size(rdev, ring);
  265. if (ring->rptr == ring->wptr) {
  266. r = radeon_ring_alloc(rdev, ring, 1);
  267. if (!r) {
  268. radeon_ring_write(ring, ring->nop);
  269. radeon_ring_commit(rdev, ring);
  270. }
  271. }
  272. }
  273. void radeon_ring_lockup_update(struct radeon_ring *ring)
  274. {
  275. ring->last_rptr = ring->rptr;
  276. ring->last_activity = jiffies;
  277. }
  278. /**
  279. * radeon_ring_test_lockup() - check if ring is lockedup by recording information
  280. * @rdev: radeon device structure
  281. * @ring: radeon_ring structure holding ring information
  282. *
  283. * We don't need to initialize the lockup tracking information as we will either
  284. * have CP rptr to a different value of jiffies wrap around which will force
  285. * initialization of the lockup tracking informations.
  286. *
  287. * A possible false positivie is if we get call after while and last_cp_rptr ==
  288. * the current CP rptr, even if it's unlikely it might happen. To avoid this
  289. * if the elapsed time since last call is bigger than 2 second than we return
  290. * false and update the tracking information. Due to this the caller must call
  291. * radeon_ring_test_lockup several time in less than 2sec for lockup to be reported
  292. * the fencing code should be cautious about that.
  293. *
  294. * Caller should write to the ring to force CP to do something so we don't get
  295. * false positive when CP is just gived nothing to do.
  296. *
  297. **/
  298. bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  299. {
  300. unsigned long cjiffies, elapsed;
  301. uint32_t rptr;
  302. cjiffies = jiffies;
  303. if (!time_after(cjiffies, ring->last_activity)) {
  304. /* likely a wrap around */
  305. radeon_ring_lockup_update(ring);
  306. return false;
  307. }
  308. rptr = RREG32(ring->rptr_reg);
  309. ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
  310. if (ring->rptr != ring->last_rptr) {
  311. /* CP is still working no lockup */
  312. radeon_ring_lockup_update(ring);
  313. return false;
  314. }
  315. elapsed = jiffies_to_msecs(cjiffies - ring->last_activity);
  316. if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
  317. dev_err(rdev->dev, "GPU lockup CP stall for more than %lumsec\n", elapsed);
  318. return true;
  319. }
  320. /* give a chance to the GPU ... */
  321. return false;
  322. }
  323. int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
  324. unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg,
  325. u32 ptr_reg_shift, u32 ptr_reg_mask, u32 nop)
  326. {
  327. int r;
  328. ring->ring_size = ring_size;
  329. ring->rptr_offs = rptr_offs;
  330. ring->rptr_reg = rptr_reg;
  331. ring->wptr_reg = wptr_reg;
  332. ring->ptr_reg_shift = ptr_reg_shift;
  333. ring->ptr_reg_mask = ptr_reg_mask;
  334. ring->nop = nop;
  335. /* Allocate ring buffer */
  336. if (ring->ring_obj == NULL) {
  337. r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
  338. RADEON_GEM_DOMAIN_GTT,
  339. NULL, &ring->ring_obj);
  340. if (r) {
  341. dev_err(rdev->dev, "(%d) ring create failed\n", r);
  342. return r;
  343. }
  344. r = radeon_bo_reserve(ring->ring_obj, false);
  345. if (unlikely(r != 0))
  346. return r;
  347. r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
  348. &ring->gpu_addr);
  349. if (r) {
  350. radeon_bo_unreserve(ring->ring_obj);
  351. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  352. return r;
  353. }
  354. r = radeon_bo_kmap(ring->ring_obj,
  355. (void **)&ring->ring);
  356. radeon_bo_unreserve(ring->ring_obj);
  357. if (r) {
  358. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  359. return r;
  360. }
  361. }
  362. ring->ptr_mask = (ring->ring_size / 4) - 1;
  363. ring->ring_free_dw = ring->ring_size / 4;
  364. if (radeon_debugfs_ring_init(rdev, ring)) {
  365. DRM_ERROR("Failed to register debugfs file for rings !\n");
  366. }
  367. return 0;
  368. }
  369. void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
  370. {
  371. int r;
  372. struct radeon_bo *ring_obj;
  373. mutex_lock(&rdev->ring_lock);
  374. ring_obj = ring->ring_obj;
  375. ring->ready = false;
  376. ring->ring = NULL;
  377. ring->ring_obj = NULL;
  378. mutex_unlock(&rdev->ring_lock);
  379. if (ring_obj) {
  380. r = radeon_bo_reserve(ring_obj, false);
  381. if (likely(r == 0)) {
  382. radeon_bo_kunmap(ring_obj);
  383. radeon_bo_unpin(ring_obj);
  384. radeon_bo_unreserve(ring_obj);
  385. }
  386. radeon_bo_unref(&ring_obj);
  387. }
  388. }
  389. /*
  390. * Debugfs info
  391. */
  392. #if defined(CONFIG_DEBUG_FS)
  393. static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
  394. {
  395. struct drm_info_node *node = (struct drm_info_node *) m->private;
  396. struct drm_device *dev = node->minor->dev;
  397. struct radeon_device *rdev = dev->dev_private;
  398. int ridx = *(int*)node->info_ent->data;
  399. struct radeon_ring *ring = &rdev->ring[ridx];
  400. unsigned count, i, j;
  401. radeon_ring_free_size(rdev, ring);
  402. count = (ring->ring_size / 4) - ring->ring_free_dw;
  403. seq_printf(m, "wptr(0x%04x): 0x%08x\n", ring->wptr_reg, RREG32(ring->wptr_reg));
  404. seq_printf(m, "rptr(0x%04x): 0x%08x\n", ring->rptr_reg, RREG32(ring->rptr_reg));
  405. if (ring->rptr_save_reg) {
  406. seq_printf(m, "rptr next(0x%04x): 0x%08x\n", ring->rptr_save_reg,
  407. RREG32(ring->rptr_save_reg));
  408. }
  409. seq_printf(m, "driver's copy of the wptr: 0x%08x\n", ring->wptr);
  410. seq_printf(m, "driver's copy of the rptr: 0x%08x\n", ring->rptr);
  411. seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
  412. seq_printf(m, "%u dwords in ring\n", count);
  413. i = ring->rptr;
  414. for (j = 0; j <= count; j++) {
  415. seq_printf(m, "r[%04d]=0x%08x\n", i, ring->ring[i]);
  416. i = (i + 1) & ring->ptr_mask;
  417. }
  418. return 0;
  419. }
  420. static int radeon_ring_type_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
  421. static int cayman_ring_type_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
  422. static int cayman_ring_type_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
  423. static struct drm_info_list radeon_debugfs_ring_info_list[] = {
  424. {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_ring_type_gfx_index},
  425. {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp1_index},
  426. {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp2_index},
  427. };
  428. static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
  429. {
  430. struct drm_info_node *node = (struct drm_info_node *) m->private;
  431. struct drm_device *dev = node->minor->dev;
  432. struct radeon_device *rdev = dev->dev_private;
  433. radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
  434. return 0;
  435. }
  436. static struct drm_info_list radeon_debugfs_sa_list[] = {
  437. {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
  438. };
  439. #endif
  440. int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
  441. {
  442. #if defined(CONFIG_DEBUG_FS)
  443. unsigned i;
  444. for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
  445. struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
  446. int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
  447. unsigned r;
  448. if (&rdev->ring[ridx] != ring)
  449. continue;
  450. r = radeon_debugfs_add_files(rdev, info, 1);
  451. if (r)
  452. return r;
  453. }
  454. #endif
  455. return 0;
  456. }
  457. int radeon_debugfs_sa_init(struct radeon_device *rdev)
  458. {
  459. #if defined(CONFIG_DEBUG_FS)
  460. return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
  461. #else
  462. return 0;
  463. #endif
  464. }