radeon_ring.c 14 KB

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