radeon_ring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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_ring *ring, uint32_t v)
  57. {
  58. #if DRM_DEBUG_CODE
  59. if (ring->count_dw <= 0) {
  60. DRM_ERROR("radeon: writting more dword to ring than expected !\n");
  61. }
  62. #endif
  63. ring->ring[ring->wptr++] = v;
  64. ring->wptr &= ring->ptr_mask;
  65. ring->count_dw--;
  66. ring->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_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. 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, ring);
  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. int radeon_ring_index(struct radeon_device *rdev, struct radeon_ring *ring)
  262. {
  263. /* r1xx-r5xx only has CP ring */
  264. if (rdev->family < CHIP_R600)
  265. return RADEON_RING_TYPE_GFX_INDEX;
  266. if (rdev->family >= CHIP_CAYMAN) {
  267. if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX])
  268. return CAYMAN_RING_TYPE_CP1_INDEX;
  269. else if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX])
  270. return CAYMAN_RING_TYPE_CP2_INDEX;
  271. }
  272. return RADEON_RING_TYPE_GFX_INDEX;
  273. }
  274. void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
  275. {
  276. if (rdev->wb.enabled)
  277. ring->rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
  278. else
  279. ring->rptr = RREG32(ring->rptr_reg);
  280. /* This works because ring_size is a power of 2 */
  281. ring->ring_free_dw = (ring->rptr + (ring->ring_size / 4));
  282. ring->ring_free_dw -= ring->wptr;
  283. ring->ring_free_dw &= ring->ptr_mask;
  284. if (!ring->ring_free_dw) {
  285. ring->ring_free_dw = ring->ring_size / 4;
  286. }
  287. }
  288. int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  289. {
  290. int r;
  291. /* Align requested size with padding so unlock_commit can
  292. * pad safely */
  293. ndw = (ndw + ring->align_mask) & ~ring->align_mask;
  294. while (ndw > (ring->ring_free_dw - 1)) {
  295. radeon_ring_free_size(rdev, ring);
  296. if (ndw < ring->ring_free_dw) {
  297. break;
  298. }
  299. r = radeon_fence_wait_next(rdev, radeon_ring_index(rdev, ring));
  300. if (r)
  301. return r;
  302. }
  303. ring->count_dw = ndw;
  304. ring->wptr_old = ring->wptr;
  305. return 0;
  306. }
  307. int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
  308. {
  309. int r;
  310. mutex_lock(&ring->mutex);
  311. r = radeon_ring_alloc(rdev, ring, ndw);
  312. if (r) {
  313. mutex_unlock(&ring->mutex);
  314. return r;
  315. }
  316. return 0;
  317. }
  318. void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  319. {
  320. unsigned count_dw_pad;
  321. unsigned i;
  322. /* We pad to match fetch size */
  323. count_dw_pad = (ring->align_mask + 1) -
  324. (ring->wptr & ring->align_mask);
  325. for (i = 0; i < count_dw_pad; i++) {
  326. radeon_ring_write(ring, 2 << 30);
  327. }
  328. DRM_MEMORYBARRIER();
  329. WREG32(ring->wptr_reg, ring->wptr);
  330. (void)RREG32(ring->wptr_reg);
  331. }
  332. void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
  333. {
  334. radeon_ring_commit(rdev, ring);
  335. mutex_unlock(&ring->mutex);
  336. }
  337. void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
  338. {
  339. ring->wptr = ring->wptr_old;
  340. mutex_unlock(&ring->mutex);
  341. }
  342. int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
  343. unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg)
  344. {
  345. int r;
  346. ring->ring_size = ring_size;
  347. ring->rptr_offs = rptr_offs;
  348. ring->rptr_reg = rptr_reg;
  349. ring->wptr_reg = wptr_reg;
  350. /* Allocate ring buffer */
  351. if (ring->ring_obj == NULL) {
  352. r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
  353. RADEON_GEM_DOMAIN_GTT,
  354. &ring->ring_obj);
  355. if (r) {
  356. dev_err(rdev->dev, "(%d) ring create failed\n", r);
  357. return r;
  358. }
  359. r = radeon_bo_reserve(ring->ring_obj, false);
  360. if (unlikely(r != 0))
  361. return r;
  362. r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
  363. &ring->gpu_addr);
  364. if (r) {
  365. radeon_bo_unreserve(ring->ring_obj);
  366. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  367. return r;
  368. }
  369. r = radeon_bo_kmap(ring->ring_obj,
  370. (void **)&ring->ring);
  371. radeon_bo_unreserve(ring->ring_obj);
  372. if (r) {
  373. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  374. return r;
  375. }
  376. }
  377. ring->ptr_mask = (ring->ring_size / 4) - 1;
  378. ring->ring_free_dw = ring->ring_size / 4;
  379. return 0;
  380. }
  381. void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
  382. {
  383. int r;
  384. struct radeon_bo *ring_obj;
  385. mutex_lock(&ring->mutex);
  386. ring_obj = ring->ring_obj;
  387. ring->ring = NULL;
  388. ring->ring_obj = NULL;
  389. mutex_unlock(&ring->mutex);
  390. if (ring_obj) {
  391. r = radeon_bo_reserve(ring_obj, false);
  392. if (likely(r == 0)) {
  393. radeon_bo_kunmap(ring_obj);
  394. radeon_bo_unpin(ring_obj);
  395. radeon_bo_unreserve(ring_obj);
  396. }
  397. radeon_bo_unref(&ring_obj);
  398. }
  399. }
  400. /*
  401. * Debugfs info
  402. */
  403. #if defined(CONFIG_DEBUG_FS)
  404. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  405. {
  406. struct drm_info_node *node = (struct drm_info_node *) m->private;
  407. struct radeon_ib *ib = node->info_ent->data;
  408. unsigned i;
  409. if (ib == NULL) {
  410. return 0;
  411. }
  412. seq_printf(m, "IB %04u\n", ib->idx);
  413. seq_printf(m, "IB fence %p\n", ib->fence);
  414. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  415. for (i = 0; i < ib->length_dw; i++) {
  416. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  417. }
  418. return 0;
  419. }
  420. static int radeon_debugfs_ib_bogus_info(struct seq_file *m, void *data)
  421. {
  422. struct drm_info_node *node = (struct drm_info_node *) m->private;
  423. struct radeon_device *rdev = node->info_ent->data;
  424. struct radeon_ib *ib;
  425. unsigned i;
  426. mutex_lock(&rdev->ib_pool.mutex);
  427. if (list_empty(&rdev->ib_pool.bogus_ib)) {
  428. mutex_unlock(&rdev->ib_pool.mutex);
  429. seq_printf(m, "no bogus IB recorded\n");
  430. return 0;
  431. }
  432. ib = list_first_entry(&rdev->ib_pool.bogus_ib, struct radeon_ib, list);
  433. list_del_init(&ib->list);
  434. mutex_unlock(&rdev->ib_pool.mutex);
  435. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  436. for (i = 0; i < ib->length_dw; i++) {
  437. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  438. }
  439. vfree(ib->ptr);
  440. kfree(ib);
  441. return 0;
  442. }
  443. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  444. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  445. static struct drm_info_list radeon_debugfs_ib_bogus_info_list[] = {
  446. {"radeon_ib_bogus", radeon_debugfs_ib_bogus_info, 0, NULL},
  447. };
  448. #endif
  449. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  450. {
  451. #if defined(CONFIG_DEBUG_FS)
  452. unsigned i;
  453. int r;
  454. radeon_debugfs_ib_bogus_info_list[0].data = rdev;
  455. r = radeon_debugfs_add_files(rdev, radeon_debugfs_ib_bogus_info_list, 1);
  456. if (r)
  457. return r;
  458. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  459. sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  460. radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  461. radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  462. radeon_debugfs_ib_list[i].driver_features = 0;
  463. radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  464. }
  465. return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  466. RADEON_IB_POOL_SIZE);
  467. #else
  468. return 0;
  469. #endif
  470. }