radeon_ring.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. */
  28. #include <linux/seq_file.h>
  29. #include <linux/slab.h>
  30. #include "drmP.h"
  31. #include "radeon_drm.h"
  32. #include "radeon_reg.h"
  33. #include "radeon.h"
  34. #include "atom.h"
  35. int radeon_debugfs_ib_init(struct radeon_device *rdev);
  36. int radeon_debugfs_ring_init(struct radeon_device *rdev);
  37. u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
  38. {
  39. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  40. u32 pg_idx, pg_offset;
  41. u32 idx_value = 0;
  42. int new_page;
  43. pg_idx = (idx * 4) / PAGE_SIZE;
  44. pg_offset = (idx * 4) % PAGE_SIZE;
  45. if (ibc->kpage_idx[0] == pg_idx)
  46. return ibc->kpage[0][pg_offset/4];
  47. if (ibc->kpage_idx[1] == pg_idx)
  48. return ibc->kpage[1][pg_offset/4];
  49. new_page = radeon_cs_update_pages(p, pg_idx);
  50. if (new_page < 0) {
  51. p->parser_error = new_page;
  52. return 0;
  53. }
  54. idx_value = ibc->kpage[new_page][pg_offset/4];
  55. return idx_value;
  56. }
  57. void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
  58. {
  59. #if DRM_DEBUG_CODE
  60. if (ring->count_dw <= 0) {
  61. DRM_ERROR("radeon: writting more dword to ring than expected !\n");
  62. }
  63. #endif
  64. ring->ring[ring->wptr++] = v;
  65. ring->wptr &= ring->ptr_mask;
  66. ring->count_dw--;
  67. ring->ring_free_dw--;
  68. }
  69. /*
  70. * IB.
  71. */
  72. bool radeon_ib_try_free(struct radeon_device *rdev, struct radeon_ib *ib)
  73. {
  74. bool done = false;
  75. /* only free ib which have been emited */
  76. if (ib->fence && ib->fence->emitted) {
  77. if (radeon_fence_signaled(ib->fence)) {
  78. radeon_fence_unref(&ib->fence);
  79. radeon_sa_bo_free(rdev, &ib->sa_bo);
  80. done = true;
  81. }
  82. }
  83. return done;
  84. }
  85. int radeon_ib_get(struct radeon_device *rdev, int ring,
  86. struct radeon_ib **ib, unsigned size)
  87. {
  88. struct radeon_fence *fence;
  89. unsigned cretry = 0;
  90. int r = 0, i, idx;
  91. *ib = NULL;
  92. /* align size on 256 bytes */
  93. size = ALIGN(size, 256);
  94. r = radeon_fence_create(rdev, &fence, ring);
  95. if (r) {
  96. dev_err(rdev->dev, "failed to create fence for new IB\n");
  97. return r;
  98. }
  99. mutex_lock(&rdev->ib_pool.mutex);
  100. idx = rdev->ib_pool.head_id;
  101. retry:
  102. if (cretry > 5) {
  103. dev_err(rdev->dev, "failed to get an ib after 5 retry\n");
  104. mutex_unlock(&rdev->ib_pool.mutex);
  105. radeon_fence_unref(&fence);
  106. return -ENOMEM;
  107. }
  108. cretry++;
  109. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  110. radeon_ib_try_free(rdev, &rdev->ib_pool.ibs[idx]);
  111. if (rdev->ib_pool.ibs[idx].fence == NULL) {
  112. r = radeon_sa_bo_new(rdev, &rdev->ib_pool.sa_manager,
  113. &rdev->ib_pool.ibs[idx].sa_bo,
  114. size, 256);
  115. if (!r) {
  116. *ib = &rdev->ib_pool.ibs[idx];
  117. (*ib)->ptr = rdev->ib_pool.sa_manager.cpu_ptr;
  118. (*ib)->ptr += ((*ib)->sa_bo.offset >> 2);
  119. (*ib)->gpu_addr = rdev->ib_pool.sa_manager.gpu_addr;
  120. (*ib)->gpu_addr += (*ib)->sa_bo.offset;
  121. (*ib)->fence = fence;
  122. /* ib are most likely to be allocated in a ring fashion
  123. * thus rdev->ib_pool.head_id should be the id of the
  124. * oldest ib
  125. */
  126. rdev->ib_pool.head_id = (1 + idx);
  127. rdev->ib_pool.head_id &= (RADEON_IB_POOL_SIZE - 1);
  128. mutex_unlock(&rdev->ib_pool.mutex);
  129. return 0;
  130. }
  131. }
  132. idx = (idx + 1) & (RADEON_IB_POOL_SIZE - 1);
  133. }
  134. /* this should be rare event, ie all ib scheduled none signaled yet.
  135. */
  136. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  137. if (rdev->ib_pool.ibs[idx].fence && rdev->ib_pool.ibs[idx].fence->emitted) {
  138. r = radeon_fence_wait(rdev->ib_pool.ibs[idx].fence, false);
  139. if (!r) {
  140. goto retry;
  141. }
  142. /* an error happened */
  143. break;
  144. }
  145. idx = (idx + 1) & (RADEON_IB_POOL_SIZE - 1);
  146. }
  147. mutex_unlock(&rdev->ib_pool.mutex);
  148. radeon_fence_unref(&fence);
  149. return r;
  150. }
  151. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
  152. {
  153. struct radeon_ib *tmp = *ib;
  154. *ib = NULL;
  155. if (tmp == NULL) {
  156. return;
  157. }
  158. mutex_lock(&rdev->ib_pool.mutex);
  159. if (tmp->fence && !tmp->fence->emitted) {
  160. radeon_sa_bo_free(rdev, &tmp->sa_bo);
  161. radeon_fence_unref(&tmp->fence);
  162. }
  163. mutex_unlock(&rdev->ib_pool.mutex);
  164. }
  165. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
  166. {
  167. struct radeon_ring *ring = &rdev->ring[ib->fence->ring];
  168. int r = 0;
  169. if (!ib->length_dw || !ring->ready) {
  170. /* TODO: Nothings in the ib we should report. */
  171. DRM_ERROR("radeon: couldn't schedule IB(%u).\n", ib->idx);
  172. return -EINVAL;
  173. }
  174. /* 64 dwords should be enough for fence too */
  175. r = radeon_ring_lock(rdev, ring, 64);
  176. if (r) {
  177. DRM_ERROR("radeon: scheduling IB failed (%d).\n", r);
  178. return r;
  179. }
  180. radeon_ring_ib_execute(rdev, ib->fence->ring, ib);
  181. radeon_fence_emit(rdev, ib->fence);
  182. radeon_ring_unlock_commit(rdev, ring);
  183. return 0;
  184. }
  185. int radeon_ib_pool_init(struct radeon_device *rdev)
  186. {
  187. int i, r;
  188. mutex_lock(&rdev->ib_pool.mutex);
  189. if (rdev->ib_pool.ready) {
  190. mutex_unlock(&rdev->ib_pool.mutex);
  191. return 0;
  192. }
  193. r = radeon_sa_bo_manager_init(rdev, &rdev->ib_pool.sa_manager,
  194. RADEON_IB_POOL_SIZE*64*1024,
  195. RADEON_GEM_DOMAIN_GTT);
  196. if (r) {
  197. mutex_unlock(&rdev->ib_pool.mutex);
  198. return r;
  199. }
  200. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  201. rdev->ib_pool.ibs[i].fence = NULL;
  202. rdev->ib_pool.ibs[i].idx = i;
  203. rdev->ib_pool.ibs[i].length_dw = 0;
  204. INIT_LIST_HEAD(&rdev->ib_pool.ibs[i].sa_bo.list);
  205. }
  206. rdev->ib_pool.head_id = 0;
  207. rdev->ib_pool.ready = true;
  208. DRM_INFO("radeon: ib pool ready.\n");
  209. if (radeon_debugfs_ib_init(rdev)) {
  210. DRM_ERROR("Failed to register debugfs file for IB !\n");
  211. }
  212. if (radeon_debugfs_ring_init(rdev)) {
  213. DRM_ERROR("Failed to register debugfs file for rings !\n");
  214. }
  215. mutex_unlock(&rdev->ib_pool.mutex);
  216. return 0;
  217. }
  218. void radeon_ib_pool_fini(struct radeon_device *rdev)
  219. {
  220. unsigned i;
  221. mutex_lock(&rdev->ib_pool.mutex);
  222. if (rdev->ib_pool.ready) {
  223. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  224. radeon_sa_bo_free(rdev, &rdev->ib_pool.ibs[i].sa_bo);
  225. radeon_fence_unref(&rdev->ib_pool.ibs[i].fence);
  226. }
  227. radeon_sa_bo_manager_fini(rdev, &rdev->ib_pool.sa_manager);
  228. rdev->ib_pool.ready = false;
  229. }
  230. mutex_unlock(&rdev->ib_pool.mutex);
  231. }
  232. int radeon_ib_pool_start(struct radeon_device *rdev)
  233. {
  234. return radeon_sa_bo_manager_start(rdev, &rdev->ib_pool.sa_manager);
  235. }
  236. int radeon_ib_pool_suspend(struct radeon_device *rdev)
  237. {
  238. return radeon_sa_bo_manager_suspend(rdev, &rdev->ib_pool.sa_manager);
  239. }
  240. /*
  241. * Ring.
  242. */
  243. int radeon_ring_index(struct radeon_device *rdev, struct radeon_ring *ring)
  244. {
  245. /* r1xx-r5xx only has CP ring */
  246. if (rdev->family < CHIP_R600)
  247. return RADEON_RING_TYPE_GFX_INDEX;
  248. if (rdev->family >= CHIP_CAYMAN) {
  249. if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX])
  250. return CAYMAN_RING_TYPE_CP1_INDEX;
  251. else if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX])
  252. return CAYMAN_RING_TYPE_CP2_INDEX;
  253. }
  254. return RADEON_RING_TYPE_GFX_INDEX;
  255. }
  256. void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
  257. {
  258. u32 rptr;
  259. if (rdev->wb.enabled)
  260. rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
  261. else
  262. rptr = RREG32(ring->rptr_reg);
  263. ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
  264. /* This works because ring_size is a power of 2 */
  265. ring->ring_free_dw = (ring->rptr + (ring->ring_size / 4));
  266. ring->ring_free_dw -= ring->wptr;
  267. ring->ring_free_dw &= ring->ptr_mask;
  268. if (!ring->ring_free_dw) {
  269. ring->ring_free_dw = ring->ring_size / 4;
  270. }
  271. }
  272. int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  273. {
  274. int r;
  275. /* Align requested size with padding so unlock_commit can
  276. * pad safely */
  277. ndw = (ndw + ring->align_mask) & ~ring->align_mask;
  278. while (ndw > (ring->ring_free_dw - 1)) {
  279. radeon_ring_free_size(rdev, ring);
  280. if (ndw < ring->ring_free_dw) {
  281. break;
  282. }
  283. r = radeon_fence_wait_next(rdev, radeon_ring_index(rdev, ring));
  284. if (r)
  285. return r;
  286. }
  287. ring->count_dw = ndw;
  288. ring->wptr_old = ring->wptr;
  289. return 0;
  290. }
  291. int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  292. {
  293. int r;
  294. mutex_lock(&ring->mutex);
  295. r = radeon_ring_alloc(rdev, ring, ndw);
  296. if (r) {
  297. mutex_unlock(&ring->mutex);
  298. return r;
  299. }
  300. return 0;
  301. }
  302. void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  303. {
  304. unsigned count_dw_pad;
  305. unsigned i;
  306. /* We pad to match fetch size */
  307. count_dw_pad = (ring->align_mask + 1) -
  308. (ring->wptr & ring->align_mask);
  309. for (i = 0; i < count_dw_pad; i++) {
  310. radeon_ring_write(ring, ring->nop);
  311. }
  312. DRM_MEMORYBARRIER();
  313. WREG32(ring->wptr_reg, (ring->wptr << ring->ptr_reg_shift) & ring->ptr_reg_mask);
  314. (void)RREG32(ring->wptr_reg);
  315. }
  316. void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  317. {
  318. radeon_ring_commit(rdev, ring);
  319. mutex_unlock(&ring->mutex);
  320. }
  321. void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
  322. {
  323. ring->wptr = ring->wptr_old;
  324. mutex_unlock(&ring->mutex);
  325. }
  326. int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
  327. unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg,
  328. u32 ptr_reg_shift, u32 ptr_reg_mask, u32 nop)
  329. {
  330. int r;
  331. ring->ring_size = ring_size;
  332. ring->rptr_offs = rptr_offs;
  333. ring->rptr_reg = rptr_reg;
  334. ring->wptr_reg = wptr_reg;
  335. ring->ptr_reg_shift = ptr_reg_shift;
  336. ring->ptr_reg_mask = ptr_reg_mask;
  337. ring->nop = nop;
  338. /* Allocate ring buffer */
  339. if (ring->ring_obj == NULL) {
  340. r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
  341. RADEON_GEM_DOMAIN_GTT,
  342. &ring->ring_obj);
  343. if (r) {
  344. dev_err(rdev->dev, "(%d) ring create failed\n", r);
  345. return r;
  346. }
  347. r = radeon_bo_reserve(ring->ring_obj, false);
  348. if (unlikely(r != 0))
  349. return r;
  350. r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
  351. &ring->gpu_addr);
  352. if (r) {
  353. radeon_bo_unreserve(ring->ring_obj);
  354. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  355. return r;
  356. }
  357. r = radeon_bo_kmap(ring->ring_obj,
  358. (void **)&ring->ring);
  359. radeon_bo_unreserve(ring->ring_obj);
  360. if (r) {
  361. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  362. return r;
  363. }
  364. }
  365. ring->ptr_mask = (ring->ring_size / 4) - 1;
  366. ring->ring_free_dw = ring->ring_size / 4;
  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(&ring->mutex);
  374. ring_obj = ring->ring_obj;
  375. ring->ring = NULL;
  376. ring->ring_obj = NULL;
  377. mutex_unlock(&ring->mutex);
  378. if (ring_obj) {
  379. r = radeon_bo_reserve(ring_obj, false);
  380. if (likely(r == 0)) {
  381. radeon_bo_kunmap(ring_obj);
  382. radeon_bo_unpin(ring_obj);
  383. radeon_bo_unreserve(ring_obj);
  384. }
  385. radeon_bo_unref(&ring_obj);
  386. }
  387. }
  388. /*
  389. * Debugfs info
  390. */
  391. #if defined(CONFIG_DEBUG_FS)
  392. static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
  393. {
  394. struct drm_info_node *node = (struct drm_info_node *) m->private;
  395. struct drm_device *dev = node->minor->dev;
  396. struct radeon_device *rdev = dev->dev_private;
  397. int ridx = *(int*)node->info_ent->data;
  398. struct radeon_ring *ring = &rdev->ring[ridx];
  399. unsigned count, i, j;
  400. radeon_ring_free_size(rdev, ring);
  401. count = (ring->ring_size / 4) - ring->ring_free_dw;
  402. seq_printf(m, "wptr(0x%04x): 0x%08x\n", ring->wptr_reg, RREG32(ring->wptr_reg));
  403. seq_printf(m, "rptr(0x%04x): 0x%08x\n", ring->rptr_reg, RREG32(ring->rptr_reg));
  404. seq_printf(m, "driver's copy of the wptr: 0x%08x\n", ring->wptr);
  405. seq_printf(m, "driver's copy of the rptr: 0x%08x\n", ring->rptr);
  406. seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
  407. seq_printf(m, "%u dwords in ring\n", count);
  408. i = ring->rptr;
  409. for (j = 0; j <= count; j++) {
  410. seq_printf(m, "r[%04d]=0x%08x\n", i, ring->ring[i]);
  411. i = (i + 1) & ring->ptr_mask;
  412. }
  413. return 0;
  414. }
  415. static int radeon_ring_type_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
  416. static int cayman_ring_type_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
  417. static int cayman_ring_type_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
  418. static struct drm_info_list radeon_debugfs_ring_info_list[] = {
  419. {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_ring_type_gfx_index},
  420. {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp1_index},
  421. {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp2_index},
  422. };
  423. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  424. {
  425. struct drm_info_node *node = (struct drm_info_node *) m->private;
  426. struct radeon_ib *ib = node->info_ent->data;
  427. unsigned i;
  428. if (ib == NULL) {
  429. return 0;
  430. }
  431. seq_printf(m, "IB %04u\n", ib->idx);
  432. seq_printf(m, "IB fence %p\n", ib->fence);
  433. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  434. for (i = 0; i < ib->length_dw; i++) {
  435. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  436. }
  437. return 0;
  438. }
  439. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  440. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  441. #endif
  442. int radeon_debugfs_ring_init(struct radeon_device *rdev)
  443. {
  444. #if defined(CONFIG_DEBUG_FS)
  445. return radeon_debugfs_add_files(rdev, radeon_debugfs_ring_info_list,
  446. ARRAY_SIZE(radeon_debugfs_ring_info_list));
  447. #else
  448. return 0;
  449. #endif
  450. }
  451. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  452. {
  453. #if defined(CONFIG_DEBUG_FS)
  454. unsigned i;
  455. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  456. sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  457. radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  458. radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  459. radeon_debugfs_ib_list[i].driver_features = 0;
  460. radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  461. }
  462. return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  463. RADEON_IB_POOL_SIZE);
  464. #else
  465. return 0;
  466. #endif
  467. }