radeon_ring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
  37. {
  38. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  39. u32 pg_idx, pg_offset;
  40. u32 idx_value = 0;
  41. int new_page;
  42. pg_idx = (idx * 4) / PAGE_SIZE;
  43. pg_offset = (idx * 4) % PAGE_SIZE;
  44. if (ibc->kpage_idx[0] == pg_idx)
  45. return ibc->kpage[0][pg_offset/4];
  46. if (ibc->kpage_idx[1] == pg_idx)
  47. return ibc->kpage[1][pg_offset/4];
  48. new_page = radeon_cs_update_pages(p, pg_idx);
  49. if (new_page < 0) {
  50. p->parser_error = new_page;
  51. return 0;
  52. }
  53. idx_value = ibc->kpage[new_page][pg_offset/4];
  54. return idx_value;
  55. }
  56. void radeon_ring_write(struct radeon_cp *cp, uint32_t v)
  57. {
  58. #if DRM_DEBUG_CODE
  59. if (cp->count_dw <= 0) {
  60. DRM_ERROR("radeon: writting more dword to ring than expected !\n");
  61. }
  62. #endif
  63. cp->ring[cp->wptr++] = v;
  64. cp->wptr &= cp->ptr_mask;
  65. cp->count_dw--;
  66. cp->ring_free_dw--;
  67. }
  68. void radeon_ib_bogus_cleanup(struct radeon_device *rdev)
  69. {
  70. struct radeon_ib *ib, *n;
  71. list_for_each_entry_safe(ib, n, &rdev->ib_pool.bogus_ib, list) {
  72. list_del(&ib->list);
  73. vfree(ib->ptr);
  74. kfree(ib);
  75. }
  76. }
  77. void radeon_ib_bogus_add(struct radeon_device *rdev, struct radeon_ib *ib)
  78. {
  79. struct radeon_ib *bib;
  80. bib = kmalloc(sizeof(*bib), GFP_KERNEL);
  81. if (bib == NULL)
  82. return;
  83. bib->ptr = vmalloc(ib->length_dw * 4);
  84. if (bib->ptr == NULL) {
  85. kfree(bib);
  86. return;
  87. }
  88. memcpy(bib->ptr, ib->ptr, ib->length_dw * 4);
  89. bib->length_dw = ib->length_dw;
  90. mutex_lock(&rdev->ib_pool.mutex);
  91. list_add_tail(&bib->list, &rdev->ib_pool.bogus_ib);
  92. mutex_unlock(&rdev->ib_pool.mutex);
  93. }
  94. /*
  95. * IB.
  96. */
  97. int radeon_ib_get(struct radeon_device *rdev, int ring, struct radeon_ib **ib)
  98. {
  99. struct radeon_fence *fence;
  100. struct radeon_ib *nib;
  101. int r = 0, i, c;
  102. *ib = NULL;
  103. r = radeon_fence_create(rdev, &fence, ring);
  104. if (r) {
  105. dev_err(rdev->dev, "failed to create fence for new IB\n");
  106. return r;
  107. }
  108. mutex_lock(&rdev->ib_pool.mutex);
  109. for (i = rdev->ib_pool.head_id, c = 0, nib = NULL; c < RADEON_IB_POOL_SIZE; c++, i++) {
  110. i &= (RADEON_IB_POOL_SIZE - 1);
  111. if (rdev->ib_pool.ibs[i].free) {
  112. nib = &rdev->ib_pool.ibs[i];
  113. break;
  114. }
  115. }
  116. if (nib == NULL) {
  117. /* This should never happen, it means we allocated all
  118. * IB and haven't scheduled one yet, return EBUSY to
  119. * userspace hoping that on ioctl recall we get better
  120. * luck
  121. */
  122. dev_err(rdev->dev, "no free indirect buffer !\n");
  123. mutex_unlock(&rdev->ib_pool.mutex);
  124. radeon_fence_unref(&fence);
  125. return -EBUSY;
  126. }
  127. rdev->ib_pool.head_id = (nib->idx + 1) & (RADEON_IB_POOL_SIZE - 1);
  128. nib->free = false;
  129. if (nib->fence) {
  130. mutex_unlock(&rdev->ib_pool.mutex);
  131. r = radeon_fence_wait(nib->fence, false);
  132. if (r) {
  133. dev_err(rdev->dev, "error waiting fence of IB(%u:0x%016lX:%u)\n",
  134. nib->idx, (unsigned long)nib->gpu_addr, nib->length_dw);
  135. mutex_lock(&rdev->ib_pool.mutex);
  136. nib->free = true;
  137. mutex_unlock(&rdev->ib_pool.mutex);
  138. radeon_fence_unref(&fence);
  139. return r;
  140. }
  141. mutex_lock(&rdev->ib_pool.mutex);
  142. }
  143. radeon_fence_unref(&nib->fence);
  144. nib->fence = fence;
  145. nib->length_dw = 0;
  146. mutex_unlock(&rdev->ib_pool.mutex);
  147. *ib = nib;
  148. return 0;
  149. }
  150. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
  151. {
  152. struct radeon_ib *tmp = *ib;
  153. *ib = NULL;
  154. if (tmp == NULL) {
  155. return;
  156. }
  157. if (!tmp->fence->emitted)
  158. radeon_fence_unref(&tmp->fence);
  159. mutex_lock(&rdev->ib_pool.mutex);
  160. tmp->free = true;
  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_cp *cp = &rdev->cp;
  166. int r = 0;
  167. if (!ib->length_dw || !cp->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, cp, 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);
  179. radeon_fence_emit(rdev, ib->fence);
  180. mutex_lock(&rdev->ib_pool.mutex);
  181. /* once scheduled IB is considered free and protected by the fence */
  182. ib->free = true;
  183. mutex_unlock(&rdev->ib_pool.mutex);
  184. radeon_ring_unlock_commit(rdev, cp);
  185. return 0;
  186. }
  187. int radeon_ib_pool_init(struct radeon_device *rdev)
  188. {
  189. void *ptr;
  190. uint64_t gpu_addr;
  191. int i;
  192. int r = 0;
  193. if (rdev->ib_pool.robj)
  194. return 0;
  195. INIT_LIST_HEAD(&rdev->ib_pool.bogus_ib);
  196. /* Allocate 1M object buffer */
  197. r = radeon_bo_create(rdev, RADEON_IB_POOL_SIZE*64*1024,
  198. PAGE_SIZE, true, RADEON_GEM_DOMAIN_GTT,
  199. &rdev->ib_pool.robj);
  200. if (r) {
  201. DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
  202. return r;
  203. }
  204. r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  205. if (unlikely(r != 0))
  206. return r;
  207. r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
  208. if (r) {
  209. radeon_bo_unreserve(rdev->ib_pool.robj);
  210. DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
  211. return r;
  212. }
  213. r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
  214. radeon_bo_unreserve(rdev->ib_pool.robj);
  215. if (r) {
  216. DRM_ERROR("radeon: failed to map ib pool (%d).\n", r);
  217. return r;
  218. }
  219. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  220. unsigned offset;
  221. offset = i * 64 * 1024;
  222. rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
  223. rdev->ib_pool.ibs[i].ptr = ptr + offset;
  224. rdev->ib_pool.ibs[i].idx = i;
  225. rdev->ib_pool.ibs[i].length_dw = 0;
  226. rdev->ib_pool.ibs[i].free = true;
  227. }
  228. rdev->ib_pool.head_id = 0;
  229. rdev->ib_pool.ready = true;
  230. DRM_INFO("radeon: ib pool ready.\n");
  231. if (radeon_debugfs_ib_init(rdev)) {
  232. DRM_ERROR("Failed to register debugfs file for IB !\n");
  233. }
  234. return r;
  235. }
  236. void radeon_ib_pool_fini(struct radeon_device *rdev)
  237. {
  238. int r;
  239. struct radeon_bo *robj;
  240. if (!rdev->ib_pool.ready) {
  241. return;
  242. }
  243. mutex_lock(&rdev->ib_pool.mutex);
  244. radeon_ib_bogus_cleanup(rdev);
  245. robj = rdev->ib_pool.robj;
  246. rdev->ib_pool.robj = NULL;
  247. mutex_unlock(&rdev->ib_pool.mutex);
  248. if (robj) {
  249. r = radeon_bo_reserve(robj, false);
  250. if (likely(r == 0)) {
  251. radeon_bo_kunmap(robj);
  252. radeon_bo_unpin(robj);
  253. radeon_bo_unreserve(robj);
  254. }
  255. radeon_bo_unref(&robj);
  256. }
  257. }
  258. /*
  259. * Ring.
  260. */
  261. void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_cp *cp)
  262. {
  263. if (rdev->wb.enabled)
  264. rdev->cp.rptr = le32_to_cpu(rdev->wb.wb[RADEON_WB_CP_RPTR_OFFSET/4]);
  265. else {
  266. if (rdev->family >= CHIP_R600)
  267. rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
  268. else
  269. rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  270. }
  271. /* This works because ring_size is a power of 2 */
  272. cp->ring_free_dw = (cp->rptr + (cp->ring_size / 4));
  273. cp->ring_free_dw -= cp->wptr;
  274. cp->ring_free_dw &= cp->ptr_mask;
  275. if (!cp->ring_free_dw) {
  276. cp->ring_free_dw = cp->ring_size / 4;
  277. }
  278. }
  279. int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ndw)
  280. {
  281. int r;
  282. /* Align requested size with padding so unlock_commit can
  283. * pad safely */
  284. ndw = (ndw + cp->align_mask) & ~cp->align_mask;
  285. while (ndw > (cp->ring_free_dw - 1)) {
  286. radeon_ring_free_size(rdev, cp);
  287. if (ndw < cp->ring_free_dw) {
  288. break;
  289. }
  290. r = radeon_fence_wait_next(rdev, RADEON_RING_TYPE_GFX_INDEX);
  291. if (r)
  292. return r;
  293. }
  294. cp->count_dw = ndw;
  295. cp->wptr_old = cp->wptr;
  296. return 0;
  297. }
  298. int radeon_ring_lock(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ndw)
  299. {
  300. int r;
  301. mutex_lock(&cp->mutex);
  302. r = radeon_ring_alloc(rdev, cp, ndw);
  303. if (r) {
  304. mutex_unlock(&cp->mutex);
  305. return r;
  306. }
  307. return 0;
  308. }
  309. void radeon_ring_commit(struct radeon_device *rdev, struct radeon_cp *cp)
  310. {
  311. unsigned count_dw_pad;
  312. unsigned i;
  313. /* We pad to match fetch size */
  314. count_dw_pad = (cp->align_mask + 1) -
  315. (cp->wptr & cp->align_mask);
  316. for (i = 0; i < count_dw_pad; i++) {
  317. radeon_ring_write(cp, 2 << 30);
  318. }
  319. DRM_MEMORYBARRIER();
  320. radeon_cp_commit(rdev, cp);
  321. }
  322. void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_cp *cp)
  323. {
  324. radeon_ring_commit(rdev, cp);
  325. mutex_unlock(&cp->mutex);
  326. }
  327. void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_cp *cp)
  328. {
  329. cp->wptr = cp->wptr_old;
  330. mutex_unlock(&cp->mutex);
  331. }
  332. int radeon_ring_init(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ring_size)
  333. {
  334. int r;
  335. cp->ring_size = ring_size;
  336. /* Allocate ring buffer */
  337. if (cp->ring_obj == NULL) {
  338. r = radeon_bo_create(rdev, cp->ring_size, PAGE_SIZE, true,
  339. RADEON_GEM_DOMAIN_GTT,
  340. &cp->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(cp->ring_obj, false);
  346. if (unlikely(r != 0))
  347. return r;
  348. r = radeon_bo_pin(cp->ring_obj, RADEON_GEM_DOMAIN_GTT,
  349. &cp->gpu_addr);
  350. if (r) {
  351. radeon_bo_unreserve(cp->ring_obj);
  352. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  353. return r;
  354. }
  355. r = radeon_bo_kmap(cp->ring_obj,
  356. (void **)&cp->ring);
  357. radeon_bo_unreserve(cp->ring_obj);
  358. if (r) {
  359. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  360. return r;
  361. }
  362. }
  363. cp->ptr_mask = (cp->ring_size / 4) - 1;
  364. cp->ring_free_dw = cp->ring_size / 4;
  365. return 0;
  366. }
  367. void radeon_ring_fini(struct radeon_device *rdev, struct radeon_cp *cp)
  368. {
  369. int r;
  370. struct radeon_bo *ring_obj;
  371. mutex_lock(&cp->mutex);
  372. ring_obj = cp->ring_obj;
  373. cp->ring = NULL;
  374. cp->ring_obj = NULL;
  375. mutex_unlock(&cp->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_ib_info(struct seq_file *m, void *data)
  391. {
  392. struct drm_info_node *node = (struct drm_info_node *) m->private;
  393. struct radeon_ib *ib = node->info_ent->data;
  394. unsigned i;
  395. if (ib == NULL) {
  396. return 0;
  397. }
  398. seq_printf(m, "IB %04u\n", ib->idx);
  399. seq_printf(m, "IB fence %p\n", ib->fence);
  400. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  401. for (i = 0; i < ib->length_dw; i++) {
  402. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  403. }
  404. return 0;
  405. }
  406. static int radeon_debugfs_ib_bogus_info(struct seq_file *m, void *data)
  407. {
  408. struct drm_info_node *node = (struct drm_info_node *) m->private;
  409. struct radeon_device *rdev = node->info_ent->data;
  410. struct radeon_ib *ib;
  411. unsigned i;
  412. mutex_lock(&rdev->ib_pool.mutex);
  413. if (list_empty(&rdev->ib_pool.bogus_ib)) {
  414. mutex_unlock(&rdev->ib_pool.mutex);
  415. seq_printf(m, "no bogus IB recorded\n");
  416. return 0;
  417. }
  418. ib = list_first_entry(&rdev->ib_pool.bogus_ib, struct radeon_ib, list);
  419. list_del_init(&ib->list);
  420. mutex_unlock(&rdev->ib_pool.mutex);
  421. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  422. for (i = 0; i < ib->length_dw; i++) {
  423. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  424. }
  425. vfree(ib->ptr);
  426. kfree(ib);
  427. return 0;
  428. }
  429. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  430. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  431. static struct drm_info_list radeon_debugfs_ib_bogus_info_list[] = {
  432. {"radeon_ib_bogus", radeon_debugfs_ib_bogus_info, 0, NULL},
  433. };
  434. #endif
  435. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  436. {
  437. #if defined(CONFIG_DEBUG_FS)
  438. unsigned i;
  439. int r;
  440. radeon_debugfs_ib_bogus_info_list[0].data = rdev;
  441. r = radeon_debugfs_add_files(rdev, radeon_debugfs_ib_bogus_info_list, 1);
  442. if (r)
  443. return r;
  444. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  445. sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  446. radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  447. radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  448. radeon_debugfs_ib_list[i].driver_features = 0;
  449. radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  450. }
  451. return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  452. RADEON_IB_POOL_SIZE);
  453. #else
  454. return 0;
  455. #endif
  456. }