amdgpu_ib.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #define AMDGPU_IB_TEST_TIMEOUT msecs_to_jiffies(1000)
  36. /*
  37. * IB
  38. * IBs (Indirect Buffers) and areas of GPU accessible memory where
  39. * commands are stored. You can put a pointer to the IB in the
  40. * command ring and the hw will fetch the commands from the IB
  41. * and execute them. Generally userspace acceleration drivers
  42. * produce command buffers which are send to the kernel and
  43. * put in IBs for execution by the requested ring.
  44. */
  45. static int amdgpu_debugfs_sa_init(struct amdgpu_device *adev);
  46. /**
  47. * amdgpu_ib_get - request an IB (Indirect Buffer)
  48. *
  49. * @ring: ring index the IB is associated with
  50. * @size: requested IB size
  51. * @ib: IB object returned
  52. *
  53. * Request an IB (all asics). IBs are allocated using the
  54. * suballocator.
  55. * Returns 0 on success, error on failure.
  56. */
  57. int amdgpu_ib_get(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  58. unsigned size, struct amdgpu_ib *ib)
  59. {
  60. int r;
  61. if (size) {
  62. r = amdgpu_sa_bo_new(&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. }
  72. return 0;
  73. }
  74. /**
  75. * amdgpu_ib_free - free an IB (Indirect Buffer)
  76. *
  77. * @adev: amdgpu_device pointer
  78. * @ib: IB object to free
  79. * @f: the fence SA bo need wait on for the ib alloation
  80. *
  81. * Free an IB (all asics).
  82. */
  83. void amdgpu_ib_free(struct amdgpu_device *adev, struct amdgpu_ib *ib,
  84. struct dma_fence *f)
  85. {
  86. amdgpu_sa_bo_free(adev, &ib->sa_bo, f);
  87. }
  88. /**
  89. * amdgpu_ib_schedule - schedule an IB (Indirect Buffer) on the ring
  90. *
  91. * @adev: amdgpu_device pointer
  92. * @num_ibs: number of IBs to schedule
  93. * @ibs: IB objects to schedule
  94. * @f: fence created during this submission
  95. *
  96. * Schedule an IB on the associated ring (all asics).
  97. * Returns 0 on success, error on failure.
  98. *
  99. * On SI, there are two parallel engines fed from the primary ring,
  100. * the CE (Constant Engine) and the DE (Drawing Engine). Since
  101. * resource descriptors have moved to memory, the CE allows you to
  102. * prime the caches while the DE is updating register state so that
  103. * the resource descriptors will be already in cache when the draw is
  104. * processed. To accomplish this, the userspace driver submits two
  105. * IBs, one for the CE and one for the DE. If there is a CE IB (called
  106. * a CONST_IB), it will be put on the ring prior to the DE IB. Prior
  107. * to SI there was just a DE IB.
  108. */
  109. int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned num_ibs,
  110. struct amdgpu_ib *ibs, struct amdgpu_job *job,
  111. struct dma_fence **f)
  112. {
  113. struct amdgpu_device *adev = ring->adev;
  114. struct amdgpu_ib *ib = &ibs[0];
  115. bool skip_preamble, need_ctx_switch;
  116. unsigned patch_offset = ~0;
  117. struct amdgpu_vm *vm;
  118. uint64_t fence_ctx;
  119. uint32_t status = 0, alloc_size;
  120. unsigned i;
  121. int r = 0;
  122. if (num_ibs == 0)
  123. return -EINVAL;
  124. /* ring tests don't use a job */
  125. if (job) {
  126. vm = job->vm;
  127. fence_ctx = job->fence_ctx;
  128. } else {
  129. vm = NULL;
  130. fence_ctx = 0;
  131. }
  132. if (!ring->ready) {
  133. dev_err(adev->dev, "couldn't schedule ib on ring <%s>\n", ring->name);
  134. return -EINVAL;
  135. }
  136. if (vm && !job->vm_id) {
  137. dev_err(adev->dev, "VM IB without ID\n");
  138. return -EINVAL;
  139. }
  140. alloc_size = ring->funcs->emit_frame_size + num_ibs *
  141. ring->funcs->emit_ib_size;
  142. r = amdgpu_ring_alloc(ring, alloc_size);
  143. if (r) {
  144. dev_err(adev->dev, "scheduling IB failed (%d).\n", r);
  145. return r;
  146. }
  147. if (ring->funcs->init_cond_exec)
  148. patch_offset = amdgpu_ring_init_cond_exec(ring);
  149. if (vm) {
  150. r = amdgpu_vm_flush(ring, job);
  151. if (r) {
  152. amdgpu_ring_undo(ring);
  153. return r;
  154. }
  155. }
  156. if (ring->funcs->emit_hdp_flush)
  157. amdgpu_ring_emit_hdp_flush(ring);
  158. skip_preamble = ring->current_ctx == fence_ctx;
  159. need_ctx_switch = ring->current_ctx != fence_ctx;
  160. if (job && ring->funcs->emit_cntxcntl) {
  161. if (need_ctx_switch)
  162. status |= AMDGPU_HAVE_CTX_SWITCH;
  163. status |= job->preamble_status;
  164. if (vm)
  165. status |= AMDGPU_VM_DOMAIN;
  166. amdgpu_ring_emit_cntxcntl(ring, status);
  167. }
  168. for (i = 0; i < num_ibs; ++i) {
  169. ib = &ibs[i];
  170. /* drop preamble IBs if we don't have a context switch */
  171. if ((ib->flags & AMDGPU_IB_FLAG_PREAMBLE) &&
  172. skip_preamble &&
  173. !(status & AMDGPU_PREAMBLE_IB_PRESENT_FIRST) &&
  174. !amdgpu_sriov_vf(adev)) /* for SRIOV preemption, Preamble CE ib must be inserted anyway */
  175. continue;
  176. amdgpu_ring_emit_ib(ring, ib, job ? job->vm_id : 0,
  177. need_ctx_switch);
  178. need_ctx_switch = false;
  179. }
  180. if (ring->funcs->emit_hdp_invalidate)
  181. amdgpu_ring_emit_hdp_invalidate(ring);
  182. r = amdgpu_fence_emit(ring, f);
  183. if (r) {
  184. dev_err(adev->dev, "failed to emit fence (%d)\n", r);
  185. if (job && job->vm_id)
  186. amdgpu_vm_reset_id(adev, job->vm_id);
  187. amdgpu_ring_undo(ring);
  188. return r;
  189. }
  190. /* wrap the last IB with fence */
  191. if (job && job->uf_addr) {
  192. amdgpu_ring_emit_fence(ring, job->uf_addr, job->uf_sequence,
  193. AMDGPU_FENCE_FLAG_64BIT);
  194. }
  195. if (patch_offset != ~0 && ring->funcs->patch_cond_exec)
  196. amdgpu_ring_patch_cond_exec(ring, patch_offset);
  197. ring->current_ctx = fence_ctx;
  198. if (vm && ring->funcs->emit_switch_buffer)
  199. amdgpu_ring_emit_switch_buffer(ring);
  200. amdgpu_ring_commit(ring);
  201. return 0;
  202. }
  203. /**
  204. * amdgpu_ib_pool_init - Init the IB (Indirect Buffer) pool
  205. *
  206. * @adev: amdgpu_device pointer
  207. *
  208. * Initialize the suballocator to manage a pool of memory
  209. * for use as IBs (all asics).
  210. * Returns 0 on success, error on failure.
  211. */
  212. int amdgpu_ib_pool_init(struct amdgpu_device *adev)
  213. {
  214. int r;
  215. if (adev->ib_pool_ready) {
  216. return 0;
  217. }
  218. r = amdgpu_sa_bo_manager_init(adev, &adev->ring_tmp_bo,
  219. AMDGPU_IB_POOL_SIZE*64*1024,
  220. AMDGPU_GPU_PAGE_SIZE,
  221. AMDGPU_GEM_DOMAIN_GTT);
  222. if (r) {
  223. return r;
  224. }
  225. r = amdgpu_sa_bo_manager_start(adev, &adev->ring_tmp_bo);
  226. if (r) {
  227. return r;
  228. }
  229. adev->ib_pool_ready = true;
  230. if (amdgpu_debugfs_sa_init(adev)) {
  231. dev_err(adev->dev, "failed to register debugfs file for SA\n");
  232. }
  233. return 0;
  234. }
  235. /**
  236. * amdgpu_ib_pool_fini - Free the IB (Indirect Buffer) pool
  237. *
  238. * @adev: amdgpu_device pointer
  239. *
  240. * Tear down the suballocator managing the pool of memory
  241. * for use as IBs (all asics).
  242. */
  243. void amdgpu_ib_pool_fini(struct amdgpu_device *adev)
  244. {
  245. if (adev->ib_pool_ready) {
  246. amdgpu_sa_bo_manager_suspend(adev, &adev->ring_tmp_bo);
  247. amdgpu_sa_bo_manager_fini(adev, &adev->ring_tmp_bo);
  248. adev->ib_pool_ready = false;
  249. }
  250. }
  251. /**
  252. * amdgpu_ib_ring_tests - test IBs on the rings
  253. *
  254. * @adev: amdgpu_device pointer
  255. *
  256. * Test an IB (Indirect Buffer) on each ring.
  257. * If the test fails, disable the ring.
  258. * Returns 0 on success, error if the primary GFX ring
  259. * IB test fails.
  260. */
  261. int amdgpu_ib_ring_tests(struct amdgpu_device *adev)
  262. {
  263. unsigned i;
  264. int r, ret = 0;
  265. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  266. struct amdgpu_ring *ring = adev->rings[i];
  267. if (!ring || !ring->ready)
  268. continue;
  269. r = amdgpu_ring_test_ib(ring, AMDGPU_IB_TEST_TIMEOUT);
  270. if (r) {
  271. ring->ready = false;
  272. if (ring == &adev->gfx.gfx_ring[0]) {
  273. /* oh, oh, that's really bad */
  274. DRM_ERROR("amdgpu: failed testing IB on GFX ring (%d).\n", r);
  275. adev->accel_working = false;
  276. return r;
  277. } else {
  278. /* still not good, but we can live with it */
  279. DRM_ERROR("amdgpu: failed testing IB on ring %d (%d).\n", i, r);
  280. ret = r;
  281. }
  282. }
  283. }
  284. return ret;
  285. }
  286. /*
  287. * Debugfs info
  288. */
  289. #if defined(CONFIG_DEBUG_FS)
  290. static int amdgpu_debugfs_sa_info(struct seq_file *m, void *data)
  291. {
  292. struct drm_info_node *node = (struct drm_info_node *) m->private;
  293. struct drm_device *dev = node->minor->dev;
  294. struct amdgpu_device *adev = dev->dev_private;
  295. amdgpu_sa_bo_dump_debug_info(&adev->ring_tmp_bo, m);
  296. return 0;
  297. }
  298. static const struct drm_info_list amdgpu_debugfs_sa_list[] = {
  299. {"amdgpu_sa_info", &amdgpu_debugfs_sa_info, 0, NULL},
  300. };
  301. #endif
  302. static int amdgpu_debugfs_sa_init(struct amdgpu_device *adev)
  303. {
  304. #if defined(CONFIG_DEBUG_FS)
  305. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_sa_list, 1);
  306. #else
  307. return 0;
  308. #endif
  309. }