radeon_ib.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * Christian König
  28. */
  29. #include <drm/drm_file.h>
  30. #include "radeon.h"
  31. /*
  32. * IB
  33. * IBs (Indirect Buffers) and areas of GPU accessible memory where
  34. * commands are stored. You can put a pointer to the IB in the
  35. * command ring and the hw will fetch the commands from the IB
  36. * and execute them. Generally userspace acceleration drivers
  37. * produce command buffers which are send to the kernel and
  38. * put in IBs for execution by the requested ring.
  39. */
  40. static void radeon_debugfs_sa_init(struct radeon_device *rdev);
  41. /**
  42. * radeon_ib_get - request an IB (Indirect Buffer)
  43. *
  44. * @rdev: radeon_device pointer
  45. * @ring: ring index the IB is associated with
  46. * @vm: requested vm
  47. * @ib: IB object returned
  48. * @size: requested IB size
  49. *
  50. * Request an IB (all asics). IBs are allocated using the
  51. * suballocator.
  52. * Returns 0 on success, error on failure.
  53. */
  54. int radeon_ib_get(struct radeon_device *rdev, int ring,
  55. struct radeon_ib *ib, struct radeon_vm *vm,
  56. unsigned size)
  57. {
  58. int r;
  59. r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256);
  60. if (r) {
  61. dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
  62. return r;
  63. }
  64. radeon_sync_create(&ib->sync);
  65. ib->ring = ring;
  66. ib->fence = NULL;
  67. ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
  68. ib->vm = vm;
  69. if (vm) {
  70. /* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address
  71. * space and soffset is the offset inside the pool bo
  72. */
  73. ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET;
  74. } else {
  75. ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
  76. }
  77. ib->is_const_ib = false;
  78. return 0;
  79. }
  80. /**
  81. * radeon_ib_free - free an IB (Indirect Buffer)
  82. *
  83. * @rdev: radeon_device pointer
  84. * @ib: IB object to free
  85. *
  86. * Free an IB (all asics).
  87. */
  88. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
  89. {
  90. radeon_sync_free(rdev, &ib->sync, ib->fence);
  91. radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence);
  92. radeon_fence_unref(&ib->fence);
  93. }
  94. /**
  95. * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring
  96. *
  97. * @rdev: radeon_device pointer
  98. * @ib: IB object to schedule
  99. * @const_ib: Const IB to schedule (SI only)
  100. * @hdp_flush: Whether or not to perform an HDP cache flush
  101. *
  102. * Schedule an IB on the associated ring (all asics).
  103. * Returns 0 on success, error on failure.
  104. *
  105. * On SI, there are two parallel engines fed from the primary ring,
  106. * the CE (Constant Engine) and the DE (Drawing Engine). Since
  107. * resource descriptors have moved to memory, the CE allows you to
  108. * prime the caches while the DE is updating register state so that
  109. * the resource descriptors will be already in cache when the draw is
  110. * processed. To accomplish this, the userspace driver submits two
  111. * IBs, one for the CE and one for the DE. If there is a CE IB (called
  112. * a CONST_IB), it will be put on the ring prior to the DE IB. Prior
  113. * to SI there was just a DE IB.
  114. */
  115. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
  116. struct radeon_ib *const_ib, bool hdp_flush)
  117. {
  118. struct radeon_ring *ring = &rdev->ring[ib->ring];
  119. int r = 0;
  120. if (!ib->length_dw || !ring->ready) {
  121. /* TODO: Nothings in the ib we should report. */
  122. dev_err(rdev->dev, "couldn't schedule ib\n");
  123. return -EINVAL;
  124. }
  125. /* 64 dwords should be enough for fence too */
  126. r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_SYNCS * 8);
  127. if (r) {
  128. dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
  129. return r;
  130. }
  131. /* grab a vm id if necessary */
  132. if (ib->vm) {
  133. struct radeon_fence *vm_id_fence;
  134. vm_id_fence = radeon_vm_grab_id(rdev, ib->vm, ib->ring);
  135. radeon_sync_fence(&ib->sync, vm_id_fence);
  136. }
  137. /* sync with other rings */
  138. r = radeon_sync_rings(rdev, &ib->sync, ib->ring);
  139. if (r) {
  140. dev_err(rdev->dev, "failed to sync rings (%d)\n", r);
  141. radeon_ring_unlock_undo(rdev, ring);
  142. return r;
  143. }
  144. if (ib->vm)
  145. radeon_vm_flush(rdev, ib->vm, ib->ring,
  146. ib->sync.last_vm_update);
  147. if (const_ib) {
  148. radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
  149. radeon_sync_free(rdev, &const_ib->sync, NULL);
  150. }
  151. radeon_ring_ib_execute(rdev, ib->ring, ib);
  152. r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
  153. if (r) {
  154. dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
  155. radeon_ring_unlock_undo(rdev, ring);
  156. return r;
  157. }
  158. if (const_ib) {
  159. const_ib->fence = radeon_fence_ref(ib->fence);
  160. }
  161. if (ib->vm)
  162. radeon_vm_fence(rdev, ib->vm, ib->fence);
  163. radeon_ring_unlock_commit(rdev, ring, hdp_flush);
  164. return 0;
  165. }
  166. /**
  167. * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
  168. *
  169. * @rdev: radeon_device pointer
  170. *
  171. * Initialize the suballocator to manage a pool of memory
  172. * for use as IBs (all asics).
  173. * Returns 0 on success, error on failure.
  174. */
  175. int radeon_ib_pool_init(struct radeon_device *rdev)
  176. {
  177. int r;
  178. if (rdev->ib_pool_ready) {
  179. return 0;
  180. }
  181. if (rdev->family >= CHIP_BONAIRE) {
  182. r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
  183. RADEON_IB_POOL_SIZE*64*1024,
  184. RADEON_GPU_PAGE_SIZE,
  185. RADEON_GEM_DOMAIN_GTT,
  186. RADEON_GEM_GTT_WC);
  187. } else {
  188. /* Before CIK, it's better to stick to cacheable GTT due
  189. * to the command stream checking
  190. */
  191. r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
  192. RADEON_IB_POOL_SIZE*64*1024,
  193. RADEON_GPU_PAGE_SIZE,
  194. RADEON_GEM_DOMAIN_GTT, 0);
  195. }
  196. if (r) {
  197. return r;
  198. }
  199. r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
  200. if (r) {
  201. return r;
  202. }
  203. rdev->ib_pool_ready = true;
  204. radeon_debugfs_sa_init(rdev);
  205. return 0;
  206. }
  207. /**
  208. * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool
  209. *
  210. * @rdev: radeon_device pointer
  211. *
  212. * Tear down the suballocator managing the pool of memory
  213. * for use as IBs (all asics).
  214. */
  215. void radeon_ib_pool_fini(struct radeon_device *rdev)
  216. {
  217. if (rdev->ib_pool_ready) {
  218. radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
  219. radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
  220. rdev->ib_pool_ready = false;
  221. }
  222. }
  223. /**
  224. * radeon_ib_ring_tests - test IBs on the rings
  225. *
  226. * @rdev: radeon_device pointer
  227. *
  228. * Test an IB (Indirect Buffer) on each ring.
  229. * If the test fails, disable the ring.
  230. * Returns 0 on success, error if the primary GFX ring
  231. * IB test fails.
  232. */
  233. int radeon_ib_ring_tests(struct radeon_device *rdev)
  234. {
  235. unsigned i;
  236. int r;
  237. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  238. struct radeon_ring *ring = &rdev->ring[i];
  239. if (!ring->ready)
  240. continue;
  241. r = radeon_ib_test(rdev, i, ring);
  242. if (r) {
  243. radeon_fence_driver_force_completion(rdev, i);
  244. ring->ready = false;
  245. rdev->needs_reset = false;
  246. if (i == RADEON_RING_TYPE_GFX_INDEX) {
  247. /* oh, oh, that's really bad */
  248. DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
  249. rdev->accel_working = false;
  250. return r;
  251. } else {
  252. /* still not good, but we can live with it */
  253. DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
  254. }
  255. }
  256. }
  257. return 0;
  258. }
  259. /*
  260. * Debugfs info
  261. */
  262. #if defined(CONFIG_DEBUG_FS)
  263. static int radeon_debugfs_sa_info_show(struct seq_file *m, void *unused)
  264. {
  265. struct radeon_device *rdev = (struct radeon_device *)m->private;
  266. radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
  267. return 0;
  268. }
  269. DEFINE_SHOW_ATTRIBUTE(radeon_debugfs_sa_info);
  270. #endif
  271. static void radeon_debugfs_sa_init(struct radeon_device *rdev)
  272. {
  273. #if defined(CONFIG_DEBUG_FS)
  274. struct dentry *root = rdev->ddev->primary->debugfs_root;
  275. debugfs_create_file("radeon_sa_info", 0444, root, rdev,
  276. &radeon_debugfs_sa_info_fops);
  277. #endif
  278. }