radeon_ring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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_device *rdev, uint32_t v)
  57. {
  58. #if DRM_DEBUG_CODE
  59. if (rdev->cp.count_dw <= 0) {
  60. DRM_ERROR("radeon: writting more dword to ring than expected !\n");
  61. }
  62. #endif
  63. rdev->cp.ring[rdev->cp.wptr++] = v;
  64. rdev->cp.wptr &= rdev->cp.ptr_mask;
  65. rdev->cp.count_dw--;
  66. rdev->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, 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);
  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->emited)
  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. int r = 0;
  166. if (!ib->length_dw || !rdev->cp.ready) {
  167. /* TODO: Nothings in the ib we should report. */
  168. DRM_ERROR("radeon: couldn't schedule IB(%u).\n", ib->idx);
  169. return -EINVAL;
  170. }
  171. /* 64 dwords should be enough for fence too */
  172. r = radeon_ring_lock(rdev, 64);
  173. if (r) {
  174. DRM_ERROR("radeon: scheduling IB failed (%d).\n", r);
  175. return r;
  176. }
  177. radeon_ring_ib_execute(rdev, ib);
  178. radeon_fence_emit(rdev, ib->fence);
  179. mutex_lock(&rdev->ib_pool.mutex);
  180. /* once scheduled IB is considered free and protected by the fence */
  181. ib->free = true;
  182. mutex_unlock(&rdev->ib_pool.mutex);
  183. radeon_ring_unlock_commit(rdev);
  184. return 0;
  185. }
  186. int radeon_ib_pool_init(struct radeon_device *rdev)
  187. {
  188. void *ptr;
  189. uint64_t gpu_addr;
  190. int i;
  191. int r = 0;
  192. if (rdev->ib_pool.robj)
  193. return 0;
  194. INIT_LIST_HEAD(&rdev->ib_pool.bogus_ib);
  195. /* Allocate 1M object buffer */
  196. r = radeon_bo_create(rdev, RADEON_IB_POOL_SIZE*64*1024,
  197. PAGE_SIZE, true, RADEON_GEM_DOMAIN_GTT,
  198. &rdev->ib_pool.robj);
  199. if (r) {
  200. DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
  201. return r;
  202. }
  203. r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  204. if (unlikely(r != 0))
  205. return r;
  206. r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
  207. if (r) {
  208. radeon_bo_unreserve(rdev->ib_pool.robj);
  209. DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
  210. return r;
  211. }
  212. r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
  213. radeon_bo_unreserve(rdev->ib_pool.robj);
  214. if (r) {
  215. DRM_ERROR("radeon: failed to map ib pool (%d).\n", r);
  216. return r;
  217. }
  218. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  219. unsigned offset;
  220. offset = i * 64 * 1024;
  221. rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
  222. rdev->ib_pool.ibs[i].ptr = ptr + offset;
  223. rdev->ib_pool.ibs[i].idx = i;
  224. rdev->ib_pool.ibs[i].length_dw = 0;
  225. rdev->ib_pool.ibs[i].free = true;
  226. }
  227. rdev->ib_pool.head_id = 0;
  228. rdev->ib_pool.ready = true;
  229. DRM_INFO("radeon: ib pool ready.\n");
  230. if (radeon_debugfs_ib_init(rdev)) {
  231. DRM_ERROR("Failed to register debugfs file for IB !\n");
  232. }
  233. return r;
  234. }
  235. void radeon_ib_pool_fini(struct radeon_device *rdev)
  236. {
  237. int r;
  238. struct radeon_bo *robj;
  239. if (!rdev->ib_pool.ready) {
  240. return;
  241. }
  242. mutex_lock(&rdev->ib_pool.mutex);
  243. radeon_ib_bogus_cleanup(rdev);
  244. robj = rdev->ib_pool.robj;
  245. rdev->ib_pool.robj = NULL;
  246. mutex_unlock(&rdev->ib_pool.mutex);
  247. if (robj) {
  248. r = radeon_bo_reserve(robj, false);
  249. if (likely(r == 0)) {
  250. radeon_bo_kunmap(robj);
  251. radeon_bo_unpin(robj);
  252. radeon_bo_unreserve(robj);
  253. }
  254. radeon_bo_unref(&robj);
  255. }
  256. }
  257. /*
  258. * Ring.
  259. */
  260. void radeon_ring_free_size(struct radeon_device *rdev)
  261. {
  262. if (rdev->wb.enabled)
  263. rdev->cp.rptr = le32_to_cpu(rdev->wb.wb[RADEON_WB_CP_RPTR_OFFSET/4]);
  264. else {
  265. if (rdev->family >= CHIP_R600)
  266. rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
  267. else
  268. rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  269. }
  270. /* This works because ring_size is a power of 2 */
  271. rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
  272. rdev->cp.ring_free_dw -= rdev->cp.wptr;
  273. rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
  274. if (!rdev->cp.ring_free_dw) {
  275. rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  276. }
  277. }
  278. int radeon_ring_alloc(struct radeon_device *rdev, unsigned ndw)
  279. {
  280. int r;
  281. /* Align requested size with padding so unlock_commit can
  282. * pad safely */
  283. ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
  284. while (ndw > (rdev->cp.ring_free_dw - 1)) {
  285. radeon_ring_free_size(rdev);
  286. if (ndw < rdev->cp.ring_free_dw) {
  287. break;
  288. }
  289. r = radeon_fence_wait_next(rdev);
  290. if (r)
  291. return r;
  292. }
  293. rdev->cp.count_dw = ndw;
  294. rdev->cp.wptr_old = rdev->cp.wptr;
  295. return 0;
  296. }
  297. int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
  298. {
  299. int r;
  300. mutex_lock(&rdev->cp.mutex);
  301. r = radeon_ring_alloc(rdev, ndw);
  302. if (r) {
  303. mutex_unlock(&rdev->cp.mutex);
  304. return r;
  305. }
  306. return 0;
  307. }
  308. void radeon_ring_commit(struct radeon_device *rdev)
  309. {
  310. unsigned count_dw_pad;
  311. unsigned i;
  312. /* We pad to match fetch size */
  313. count_dw_pad = (rdev->cp.align_mask + 1) -
  314. (rdev->cp.wptr & rdev->cp.align_mask);
  315. for (i = 0; i < count_dw_pad; i++) {
  316. radeon_ring_write(rdev, 2 << 30);
  317. }
  318. DRM_MEMORYBARRIER();
  319. radeon_cp_commit(rdev);
  320. }
  321. void radeon_ring_unlock_commit(struct radeon_device *rdev)
  322. {
  323. radeon_ring_commit(rdev);
  324. mutex_unlock(&rdev->cp.mutex);
  325. }
  326. void radeon_ring_unlock_undo(struct radeon_device *rdev)
  327. {
  328. rdev->cp.wptr = rdev->cp.wptr_old;
  329. mutex_unlock(&rdev->cp.mutex);
  330. }
  331. int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
  332. {
  333. int r;
  334. rdev->cp.ring_size = ring_size;
  335. /* Allocate ring buffer */
  336. if (rdev->cp.ring_obj == NULL) {
  337. r = radeon_bo_create(rdev, rdev->cp.ring_size, PAGE_SIZE, true,
  338. RADEON_GEM_DOMAIN_GTT,
  339. &rdev->cp.ring_obj);
  340. if (r) {
  341. dev_err(rdev->dev, "(%d) ring create failed\n", r);
  342. return r;
  343. }
  344. r = radeon_bo_reserve(rdev->cp.ring_obj, false);
  345. if (unlikely(r != 0))
  346. return r;
  347. r = radeon_bo_pin(rdev->cp.ring_obj, RADEON_GEM_DOMAIN_GTT,
  348. &rdev->cp.gpu_addr);
  349. if (r) {
  350. radeon_bo_unreserve(rdev->cp.ring_obj);
  351. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  352. return r;
  353. }
  354. r = radeon_bo_kmap(rdev->cp.ring_obj,
  355. (void **)&rdev->cp.ring);
  356. radeon_bo_unreserve(rdev->cp.ring_obj);
  357. if (r) {
  358. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  359. return r;
  360. }
  361. }
  362. rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
  363. rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  364. return 0;
  365. }
  366. void radeon_ring_fini(struct radeon_device *rdev)
  367. {
  368. int r;
  369. struct radeon_bo *ring_obj;
  370. mutex_lock(&rdev->cp.mutex);
  371. ring_obj = rdev->cp.ring_obj;
  372. rdev->cp.ring = NULL;
  373. rdev->cp.ring_obj = NULL;
  374. mutex_unlock(&rdev->cp.mutex);
  375. if (ring_obj) {
  376. r = radeon_bo_reserve(ring_obj, false);
  377. if (likely(r == 0)) {
  378. radeon_bo_kunmap(ring_obj);
  379. radeon_bo_unpin(ring_obj);
  380. radeon_bo_unreserve(ring_obj);
  381. }
  382. radeon_bo_unref(&ring_obj);
  383. }
  384. }
  385. /*
  386. * Debugfs info
  387. */
  388. #if defined(CONFIG_DEBUG_FS)
  389. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  390. {
  391. struct drm_info_node *node = (struct drm_info_node *) m->private;
  392. struct radeon_ib *ib = node->info_ent->data;
  393. unsigned i;
  394. if (ib == NULL) {
  395. return 0;
  396. }
  397. seq_printf(m, "IB %04u\n", ib->idx);
  398. seq_printf(m, "IB fence %p\n", ib->fence);
  399. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  400. for (i = 0; i < ib->length_dw; i++) {
  401. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  402. }
  403. return 0;
  404. }
  405. static int radeon_debugfs_ib_bogus_info(struct seq_file *m, void *data)
  406. {
  407. struct drm_info_node *node = (struct drm_info_node *) m->private;
  408. struct radeon_device *rdev = node->info_ent->data;
  409. struct radeon_ib *ib;
  410. unsigned i;
  411. mutex_lock(&rdev->ib_pool.mutex);
  412. if (list_empty(&rdev->ib_pool.bogus_ib)) {
  413. mutex_unlock(&rdev->ib_pool.mutex);
  414. seq_printf(m, "no bogus IB recorded\n");
  415. return 0;
  416. }
  417. ib = list_first_entry(&rdev->ib_pool.bogus_ib, struct radeon_ib, list);
  418. list_del_init(&ib->list);
  419. mutex_unlock(&rdev->ib_pool.mutex);
  420. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  421. for (i = 0; i < ib->length_dw; i++) {
  422. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  423. }
  424. vfree(ib->ptr);
  425. kfree(ib);
  426. return 0;
  427. }
  428. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  429. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  430. static struct drm_info_list radeon_debugfs_ib_bogus_info_list[] = {
  431. {"radeon_ib_bogus", radeon_debugfs_ib_bogus_info, 0, NULL},
  432. };
  433. #endif
  434. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  435. {
  436. #if defined(CONFIG_DEBUG_FS)
  437. unsigned i;
  438. int r;
  439. radeon_debugfs_ib_bogus_info_list[0].data = rdev;
  440. r = radeon_debugfs_add_files(rdev, radeon_debugfs_ib_bogus_info_list, 1);
  441. if (r)
  442. return r;
  443. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  444. sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  445. radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  446. radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  447. radeon_debugfs_ib_list[i].driver_features = 0;
  448. radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  449. }
  450. return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  451. RADEON_IB_POOL_SIZE);
  452. #else
  453. return 0;
  454. #endif
  455. }