amdgpu_ib.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <linux/seq_file.h>
  30. #include <linux/slab.h>
  31. #include <drm/drmP.h>
  32. #include <drm/amdgpu_drm.h>
  33. #include "amdgpu.h"
  34. #include "atom.h"
  35. /*
  36. * IB
  37. * IBs (Indirect Buffers) and areas of GPU accessible memory where
  38. * commands are stored. You can put a pointer to the IB in the
  39. * command ring and the hw will fetch the commands from the IB
  40. * and execute them. Generally userspace acceleration drivers
  41. * produce command buffers which are send to the kernel and
  42. * put in IBs for execution by the requested ring.
  43. */
  44. static int amdgpu_debugfs_sa_init(struct amdgpu_device *adev);
  45. /**
  46. * amdgpu_ib_get - request an IB (Indirect Buffer)
  47. *
  48. * @ring: ring index the IB is associated with
  49. * @size: requested IB size
  50. * @ib: IB object returned
  51. *
  52. * Request an IB (all asics). IBs are allocated using the
  53. * suballocator.
  54. * Returns 0 on success, error on failure.
  55. */
  56. int amdgpu_ib_get(struct amdgpu_ring *ring, struct amdgpu_vm *vm,
  57. unsigned size, struct amdgpu_ib *ib)
  58. {
  59. struct amdgpu_device *adev = ring->adev;
  60. int r;
  61. if (size) {
  62. r = amdgpu_sa_bo_new(adev, &adev->ring_tmp_bo,
  63. &ib->sa_bo, size, 256);
  64. if (r) {
  65. dev_err(adev->dev, "failed to get a new IB (%d)\n", r);
  66. return r;
  67. }
  68. ib->ptr = amdgpu_sa_bo_cpu_addr(ib->sa_bo);
  69. if (!vm)
  70. ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
  71. else
  72. ib->gpu_addr = 0;
  73. } else {
  74. ib->sa_bo = NULL;
  75. ib->ptr = NULL;
  76. ib->gpu_addr = 0;
  77. }
  78. amdgpu_sync_create(&ib->sync);
  79. ib->ring = ring;
  80. ib->fence = NULL;
  81. ib->user = NULL;
  82. ib->vm = vm;
  83. ib->ctx = NULL;
  84. ib->gds_base = 0;
  85. ib->gds_size = 0;
  86. ib->gws_base = 0;
  87. ib->gws_size = 0;
  88. ib->oa_base = 0;
  89. ib->oa_size = 0;
  90. ib->flags = 0;
  91. return 0;
  92. }
  93. /**
  94. * amdgpu_ib_free - free an IB (Indirect Buffer)
  95. *
  96. * @adev: amdgpu_device pointer
  97. * @ib: IB object to free
  98. *
  99. * Free an IB (all asics).
  100. */
  101. void amdgpu_ib_free(struct amdgpu_device *adev, struct amdgpu_ib *ib)
  102. {
  103. amdgpu_sync_free(adev, &ib->sync, ib->fence);
  104. amdgpu_sa_bo_free(adev, &ib->sa_bo, ib->fence);
  105. amdgpu_fence_unref(&ib->fence);
  106. }
  107. /**
  108. * amdgpu_ib_schedule - schedule an IB (Indirect Buffer) on the ring
  109. *
  110. * @adev: amdgpu_device pointer
  111. * @num_ibs: number of IBs to schedule
  112. * @ibs: IB objects to schedule
  113. * @owner: owner for creating the fences
  114. *
  115. * Schedule an IB on the associated ring (all asics).
  116. * Returns 0 on success, error on failure.
  117. *
  118. * On SI, there are two parallel engines fed from the primary ring,
  119. * the CE (Constant Engine) and the DE (Drawing Engine). Since
  120. * resource descriptors have moved to memory, the CE allows you to
  121. * prime the caches while the DE is updating register state so that
  122. * the resource descriptors will be already in cache when the draw is
  123. * processed. To accomplish this, the userspace driver submits two
  124. * IBs, one for the CE and one for the DE. If there is a CE IB (called
  125. * a CONST_IB), it will be put on the ring prior to the DE IB. Prior
  126. * to SI there was just a DE IB.
  127. */
  128. int amdgpu_ib_schedule(struct amdgpu_device *adev, unsigned num_ibs,
  129. struct amdgpu_ib *ibs, void *owner)
  130. {
  131. struct amdgpu_ib *ib = &ibs[0];
  132. struct amdgpu_ring *ring;
  133. struct amdgpu_ctx *ctx, *old_ctx;
  134. struct amdgpu_vm *vm;
  135. uint64_t sequence;
  136. unsigned i;
  137. int r = 0;
  138. if (num_ibs == 0)
  139. return -EINVAL;
  140. ring = ibs->ring;
  141. ctx = ibs->ctx;
  142. vm = ibs->vm;
  143. if (!ring->ready) {
  144. dev_err(adev->dev, "couldn't schedule ib\n");
  145. return -EINVAL;
  146. }
  147. r = amdgpu_ring_lock(ring, (256 + AMDGPU_NUM_SYNCS * 8) * num_ibs);
  148. if (r) {
  149. dev_err(adev->dev, "scheduling IB failed (%d).\n", r);
  150. return r;
  151. }
  152. if (vm) {
  153. /* grab a vm id if necessary */
  154. r = amdgpu_vm_grab_id(ibs->vm, ibs->ring, &ibs->sync);
  155. if (r) {
  156. amdgpu_ring_unlock_undo(ring);
  157. return r;
  158. }
  159. }
  160. r = amdgpu_sync_rings(&ibs->sync, ring);
  161. if (r) {
  162. amdgpu_ring_unlock_undo(ring);
  163. dev_err(adev->dev, "failed to sync rings (%d)\n", r);
  164. return r;
  165. }
  166. if (vm) {
  167. /* do context switch */
  168. amdgpu_vm_flush(ring, vm, ib->sync.last_vm_update);
  169. if (ring->funcs->emit_gds_switch)
  170. amdgpu_ring_emit_gds_switch(ring, ib->vm->ids[ring->idx].id,
  171. ib->gds_base, ib->gds_size,
  172. ib->gws_base, ib->gws_size,
  173. ib->oa_base, ib->oa_size);
  174. if (ring->funcs->emit_hdp_flush)
  175. amdgpu_ring_emit_hdp_flush(ring);
  176. }
  177. old_ctx = ring->current_ctx;
  178. for (i = 0; i < num_ibs; ++i) {
  179. ib = &ibs[i];
  180. if (ib->ring != ring || ib->ctx != ctx || ib->vm != vm) {
  181. ring->current_ctx = old_ctx;
  182. amdgpu_ring_unlock_undo(ring);
  183. return -EINVAL;
  184. }
  185. amdgpu_ring_emit_ib(ring, ib);
  186. ring->current_ctx = ctx;
  187. }
  188. r = amdgpu_fence_emit(ring, owner, &ib->fence);
  189. if (r) {
  190. dev_err(adev->dev, "failed to emit fence (%d)\n", r);
  191. ring->current_ctx = old_ctx;
  192. amdgpu_ring_unlock_undo(ring);
  193. return r;
  194. }
  195. sequence = amdgpu_enable_scheduler ? ib->sequence : 0;
  196. if (!amdgpu_enable_scheduler && ib->ctx)
  197. ib->sequence = amdgpu_ctx_add_fence(ib->ctx, ring,
  198. &ib->fence->base,
  199. sequence);
  200. /* wrap the last IB with fence */
  201. if (ib->user) {
  202. uint64_t addr = amdgpu_bo_gpu_offset(ib->user->bo);
  203. addr += ib->user->offset;
  204. amdgpu_ring_emit_fence(ring, addr, ib->sequence,
  205. AMDGPU_FENCE_FLAG_64BIT);
  206. }
  207. if (ib->vm)
  208. amdgpu_vm_fence(adev, ib->vm, ib->fence);
  209. amdgpu_ring_unlock_commit(ring);
  210. return 0;
  211. }
  212. /**
  213. * amdgpu_ib_pool_init - Init the IB (Indirect Buffer) pool
  214. *
  215. * @adev: amdgpu_device pointer
  216. *
  217. * Initialize the suballocator to manage a pool of memory
  218. * for use as IBs (all asics).
  219. * Returns 0 on success, error on failure.
  220. */
  221. int amdgpu_ib_pool_init(struct amdgpu_device *adev)
  222. {
  223. int r;
  224. if (adev->ib_pool_ready) {
  225. return 0;
  226. }
  227. r = amdgpu_sa_bo_manager_init(adev, &adev->ring_tmp_bo,
  228. AMDGPU_IB_POOL_SIZE*64*1024,
  229. AMDGPU_GPU_PAGE_SIZE,
  230. AMDGPU_GEM_DOMAIN_GTT);
  231. if (r) {
  232. return r;
  233. }
  234. r = amdgpu_sa_bo_manager_start(adev, &adev->ring_tmp_bo);
  235. if (r) {
  236. return r;
  237. }
  238. adev->ib_pool_ready = true;
  239. if (amdgpu_debugfs_sa_init(adev)) {
  240. dev_err(adev->dev, "failed to register debugfs file for SA\n");
  241. }
  242. return 0;
  243. }
  244. /**
  245. * amdgpu_ib_pool_fini - Free the IB (Indirect Buffer) pool
  246. *
  247. * @adev: amdgpu_device pointer
  248. *
  249. * Tear down the suballocator managing the pool of memory
  250. * for use as IBs (all asics).
  251. */
  252. void amdgpu_ib_pool_fini(struct amdgpu_device *adev)
  253. {
  254. if (adev->ib_pool_ready) {
  255. amdgpu_sa_bo_manager_suspend(adev, &adev->ring_tmp_bo);
  256. amdgpu_sa_bo_manager_fini(adev, &adev->ring_tmp_bo);
  257. adev->ib_pool_ready = false;
  258. }
  259. }
  260. /**
  261. * amdgpu_ib_ring_tests - test IBs on the rings
  262. *
  263. * @adev: amdgpu_device pointer
  264. *
  265. * Test an IB (Indirect Buffer) on each ring.
  266. * If the test fails, disable the ring.
  267. * Returns 0 on success, error if the primary GFX ring
  268. * IB test fails.
  269. */
  270. int amdgpu_ib_ring_tests(struct amdgpu_device *adev)
  271. {
  272. unsigned i;
  273. int r;
  274. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  275. struct amdgpu_ring *ring = adev->rings[i];
  276. if (!ring || !ring->ready)
  277. continue;
  278. r = amdgpu_ring_test_ib(ring);
  279. if (r) {
  280. ring->ready = false;
  281. adev->needs_reset = false;
  282. if (ring == &adev->gfx.gfx_ring[0]) {
  283. /* oh, oh, that's really bad */
  284. DRM_ERROR("amdgpu: failed testing IB on GFX ring (%d).\n", r);
  285. adev->accel_working = false;
  286. return r;
  287. } else {
  288. /* still not good, but we can live with it */
  289. DRM_ERROR("amdgpu: failed testing IB on ring %d (%d).\n", i, r);
  290. }
  291. }
  292. }
  293. return 0;
  294. }
  295. /*
  296. * Debugfs info
  297. */
  298. #if defined(CONFIG_DEBUG_FS)
  299. static int amdgpu_debugfs_sa_info(struct seq_file *m, void *data)
  300. {
  301. struct drm_info_node *node = (struct drm_info_node *) m->private;
  302. struct drm_device *dev = node->minor->dev;
  303. struct amdgpu_device *adev = dev->dev_private;
  304. amdgpu_sa_bo_dump_debug_info(&adev->ring_tmp_bo, m);
  305. return 0;
  306. }
  307. static struct drm_info_list amdgpu_debugfs_sa_list[] = {
  308. {"amdgpu_sa_info", &amdgpu_debugfs_sa_info, 0, NULL},
  309. };
  310. #endif
  311. static int amdgpu_debugfs_sa_init(struct amdgpu_device *adev)
  312. {
  313. #if defined(CONFIG_DEBUG_FS)
  314. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_sa_list, 1);
  315. #else
  316. return 0;
  317. #endif
  318. }