radeon_ib.c 8.7 KB

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