radeon_ring.c 14 KB

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