radeon_ib.c 8.7 KB

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