radeon_ring.c 14 KB

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