amdgpu_ib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #include "amdgpu_trace.h"
  36. #define AMDGPU_IB_TEST_TIMEOUT msecs_to_jiffies(1000)
  37. /*
  38. * IB
  39. * IBs (Indirect Buffers) and areas of GPU accessible memory where
  40. * commands are stored. You can put a pointer to the IB in the
  41. * command ring and the hw will fetch the commands from the IB
  42. * and execute them. Generally userspace acceleration drivers
  43. * produce command buffers which are send to the kernel and
  44. * put in IBs for execution by the requested ring.
  45. */
  46. static int amdgpu_debugfs_sa_init(struct amdgpu_device *adev);
  47. /**
  48. * amdgpu_ib_get - request an IB (Indirect Buffer)
  49. *
  50. * @ring: ring index the IB is associated with
  51. * @size: requested IB size
  52. * @ib: IB object returned
  53. *
  54. * Request an IB (all asics). IBs are allocated using the
  55. * suballocator.
  56. * Returns 0 on success, error on failure.
  57. */
  58. int amdgpu_ib_get(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  59. unsigned size, struct amdgpu_ib *ib)
  60. {
  61. int r;
  62. if (size) {
  63. r = amdgpu_sa_bo_new(&adev->ring_tmp_bo,
  64. &ib->sa_bo, size, 256);
  65. if (r) {
  66. dev_err(adev->dev, "failed to get a new IB (%d)\n", r);
  67. return r;
  68. }
  69. ib->ptr = amdgpu_sa_bo_cpu_addr(ib->sa_bo);
  70. if (!vm)
  71. ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
  72. }
  73. return 0;
  74. }
  75. /**
  76. * amdgpu_ib_free - free an IB (Indirect Buffer)
  77. *
  78. * @adev: amdgpu_device pointer
  79. * @ib: IB object to free
  80. * @f: the fence SA bo need wait on for the ib alloation
  81. *
  82. * Free an IB (all asics).
  83. */
  84. void amdgpu_ib_free(struct amdgpu_device *adev, struct amdgpu_ib *ib,
  85. struct dma_fence *f)
  86. {
  87. amdgpu_sa_bo_free(adev, &ib->sa_bo, f);
  88. }
  89. /**
  90. * amdgpu_ib_schedule - schedule an IB (Indirect Buffer) on the ring
  91. *
  92. * @adev: amdgpu_device pointer
  93. * @num_ibs: number of IBs to schedule
  94. * @ibs: IB objects to schedule
  95. * @f: fence created during this submission
  96. *
  97. * Schedule an IB on the associated ring (all asics).
  98. * Returns 0 on success, error on failure.
  99. *
  100. * On SI, there are two parallel engines fed from the primary ring,
  101. * the CE (Constant Engine) and the DE (Drawing Engine). Since
  102. * resource descriptors have moved to memory, the CE allows you to
  103. * prime the caches while the DE is updating register state so that
  104. * the resource descriptors will be already in cache when the draw is
  105. * processed. To accomplish this, the userspace driver submits two
  106. * IBs, one for the CE and one for the DE. If there is a CE IB (called
  107. * a CONST_IB), it will be put on the ring prior to the DE IB. Prior
  108. * to SI there was just a DE IB.
  109. */
  110. int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned num_ibs,
  111. struct amdgpu_ib *ibs, struct amdgpu_job *job,
  112. struct dma_fence **f)
  113. {
  114. struct amdgpu_device *adev = ring->adev;
  115. struct amdgpu_ib *ib = &ibs[0];
  116. struct dma_fence *tmp = NULL;
  117. bool skip_preamble, need_ctx_switch;
  118. unsigned patch_offset = ~0;
  119. struct amdgpu_vm *vm;
  120. uint64_t fence_ctx;
  121. uint32_t status = 0, alloc_size;
  122. unsigned fence_flags = 0;
  123. unsigned i;
  124. int r = 0;
  125. bool need_pipe_sync = false;
  126. if (num_ibs == 0)
  127. return -EINVAL;
  128. /* ring tests don't use a job */
  129. if (job) {
  130. vm = job->vm;
  131. fence_ctx = job->base.s_fence->scheduled.context;
  132. } else {
  133. vm = NULL;
  134. fence_ctx = 0;
  135. }
  136. if (!ring->ready) {
  137. dev_err(adev->dev, "couldn't schedule ib on ring <%s>\n", ring->name);
  138. return -EINVAL;
  139. }
  140. if (vm && !job->vmid) {
  141. dev_err(adev->dev, "VM IB without ID\n");
  142. return -EINVAL;
  143. }
  144. alloc_size = ring->funcs->emit_frame_size + num_ibs *
  145. ring->funcs->emit_ib_size;
  146. r = amdgpu_ring_alloc(ring, alloc_size);
  147. if (r) {
  148. dev_err(adev->dev, "scheduling IB failed (%d).\n", r);
  149. return r;
  150. }
  151. need_ctx_switch = ring->current_ctx != fence_ctx;
  152. if (ring->funcs->emit_pipeline_sync && job &&
  153. ((tmp = amdgpu_sync_get_fence(&job->sched_sync, NULL)) ||
  154. (amdgpu_sriov_vf(adev) && need_ctx_switch) ||
  155. amdgpu_vm_need_pipeline_sync(ring, job))) {
  156. need_pipe_sync = true;
  157. if (tmp)
  158. trace_amdgpu_ib_pipe_sync(job, tmp);
  159. dma_fence_put(tmp);
  160. }
  161. if (ring->funcs->insert_start)
  162. ring->funcs->insert_start(ring);
  163. if (job) {
  164. r = amdgpu_vm_flush(ring, job, need_pipe_sync);
  165. if (r) {
  166. amdgpu_ring_undo(ring);
  167. return r;
  168. }
  169. }
  170. if (job && ring->funcs->init_cond_exec)
  171. patch_offset = amdgpu_ring_init_cond_exec(ring);
  172. #ifdef CONFIG_X86_64
  173. if (!(adev->flags & AMD_IS_APU))
  174. #endif
  175. {
  176. if (ring->funcs->emit_hdp_flush)
  177. amdgpu_ring_emit_hdp_flush(ring);
  178. else
  179. amdgpu_asic_flush_hdp(adev, ring);
  180. }
  181. skip_preamble = ring->current_ctx == fence_ctx;
  182. if (job && ring->funcs->emit_cntxcntl) {
  183. if (need_ctx_switch)
  184. status |= AMDGPU_HAVE_CTX_SWITCH;
  185. status |= job->preamble_status;
  186. amdgpu_ring_emit_cntxcntl(ring, status);
  187. }
  188. for (i = 0; i < num_ibs; ++i) {
  189. ib = &ibs[i];
  190. /* drop preamble IBs if we don't have a context switch */
  191. if ((ib->flags & AMDGPU_IB_FLAG_PREAMBLE) &&
  192. skip_preamble &&
  193. !(status & AMDGPU_PREAMBLE_IB_PRESENT_FIRST) &&
  194. !amdgpu_sriov_vf(adev)) /* for SRIOV preemption, Preamble CE ib must be inserted anyway */
  195. continue;
  196. amdgpu_ring_emit_ib(ring, ib, job ? job->vmid : 0,
  197. need_ctx_switch);
  198. need_ctx_switch = false;
  199. }
  200. if (ring->funcs->emit_tmz)
  201. amdgpu_ring_emit_tmz(ring, false);
  202. #ifdef CONFIG_X86_64
  203. if (!(adev->flags & AMD_IS_APU))
  204. #endif
  205. amdgpu_asic_invalidate_hdp(adev, ring);
  206. if (ib->flags & AMDGPU_IB_FLAG_TC_WB_NOT_INVALIDATE)
  207. fence_flags |= AMDGPU_FENCE_FLAG_TC_WB_ONLY;
  208. /* wrap the last IB with fence */
  209. if (job && job->uf_addr) {
  210. amdgpu_ring_emit_fence(ring, job->uf_addr, job->uf_sequence,
  211. fence_flags | AMDGPU_FENCE_FLAG_64BIT);
  212. }
  213. r = amdgpu_fence_emit(ring, f, fence_flags);
  214. if (r) {
  215. dev_err(adev->dev, "failed to emit fence (%d)\n", r);
  216. if (job && job->vmid)
  217. amdgpu_vmid_reset(adev, ring->funcs->vmhub, job->vmid);
  218. amdgpu_ring_undo(ring);
  219. return r;
  220. }
  221. if (ring->funcs->insert_end)
  222. ring->funcs->insert_end(ring);
  223. if (patch_offset != ~0 && ring->funcs->patch_cond_exec)
  224. amdgpu_ring_patch_cond_exec(ring, patch_offset);
  225. ring->current_ctx = fence_ctx;
  226. if (vm && ring->funcs->emit_switch_buffer)
  227. amdgpu_ring_emit_switch_buffer(ring);
  228. amdgpu_ring_commit(ring);
  229. return 0;
  230. }
  231. /**
  232. * amdgpu_ib_pool_init - Init the IB (Indirect Buffer) pool
  233. *
  234. * @adev: amdgpu_device pointer
  235. *
  236. * Initialize the suballocator to manage a pool of memory
  237. * for use as IBs (all asics).
  238. * Returns 0 on success, error on failure.
  239. */
  240. int amdgpu_ib_pool_init(struct amdgpu_device *adev)
  241. {
  242. int r;
  243. if (adev->ib_pool_ready) {
  244. return 0;
  245. }
  246. r = amdgpu_sa_bo_manager_init(adev, &adev->ring_tmp_bo,
  247. AMDGPU_IB_POOL_SIZE*64*1024,
  248. AMDGPU_GPU_PAGE_SIZE,
  249. AMDGPU_GEM_DOMAIN_GTT);
  250. if (r) {
  251. return r;
  252. }
  253. adev->ib_pool_ready = true;
  254. if (amdgpu_debugfs_sa_init(adev)) {
  255. dev_err(adev->dev, "failed to register debugfs file for SA\n");
  256. }
  257. return 0;
  258. }
  259. /**
  260. * amdgpu_ib_pool_fini - Free the IB (Indirect Buffer) pool
  261. *
  262. * @adev: amdgpu_device pointer
  263. *
  264. * Tear down the suballocator managing the pool of memory
  265. * for use as IBs (all asics).
  266. */
  267. void amdgpu_ib_pool_fini(struct amdgpu_device *adev)
  268. {
  269. if (adev->ib_pool_ready) {
  270. amdgpu_sa_bo_manager_fini(adev, &adev->ring_tmp_bo);
  271. adev->ib_pool_ready = false;
  272. }
  273. }
  274. /**
  275. * amdgpu_ib_ring_tests - test IBs on the rings
  276. *
  277. * @adev: amdgpu_device pointer
  278. *
  279. * Test an IB (Indirect Buffer) on each ring.
  280. * If the test fails, disable the ring.
  281. * Returns 0 on success, error if the primary GFX ring
  282. * IB test fails.
  283. */
  284. int amdgpu_ib_ring_tests(struct amdgpu_device *adev)
  285. {
  286. unsigned i;
  287. int r, ret = 0;
  288. long tmo_gfx, tmo_mm;
  289. tmo_mm = tmo_gfx = AMDGPU_IB_TEST_TIMEOUT;
  290. if (amdgpu_sriov_vf(adev)) {
  291. /* for MM engines in hypervisor side they are not scheduled together
  292. * with CP and SDMA engines, so even in exclusive mode MM engine could
  293. * still running on other VF thus the IB TEST TIMEOUT for MM engines
  294. * under SR-IOV should be set to a long time. 8 sec should be enough
  295. * for the MM comes back to this VF.
  296. */
  297. tmo_mm = 8 * AMDGPU_IB_TEST_TIMEOUT;
  298. }
  299. if (amdgpu_sriov_runtime(adev)) {
  300. /* for CP & SDMA engines since they are scheduled together so
  301. * need to make the timeout width enough to cover the time
  302. * cost waiting for it coming back under RUNTIME only
  303. */
  304. tmo_gfx = 8 * AMDGPU_IB_TEST_TIMEOUT;
  305. }
  306. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  307. struct amdgpu_ring *ring = adev->rings[i];
  308. long tmo;
  309. if (!ring || !ring->ready)
  310. continue;
  311. /* MM engine need more time */
  312. if (ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
  313. ring->funcs->type == AMDGPU_RING_TYPE_VCE ||
  314. ring->funcs->type == AMDGPU_RING_TYPE_UVD_ENC ||
  315. ring->funcs->type == AMDGPU_RING_TYPE_VCN_DEC ||
  316. ring->funcs->type == AMDGPU_RING_TYPE_VCN_ENC ||
  317. ring->funcs->type == AMDGPU_RING_TYPE_VCN_JPEG)
  318. tmo = tmo_mm;
  319. else
  320. tmo = tmo_gfx;
  321. r = amdgpu_ring_test_ib(ring, tmo);
  322. if (r) {
  323. ring->ready = false;
  324. if (ring == &adev->gfx.gfx_ring[0]) {
  325. /* oh, oh, that's really bad */
  326. DRM_ERROR("amdgpu: failed testing IB on GFX ring (%d).\n", r);
  327. adev->accel_working = false;
  328. return r;
  329. } else {
  330. /* still not good, but we can live with it */
  331. DRM_ERROR("amdgpu: failed testing IB on ring %d (%d).\n", i, r);
  332. ret = r;
  333. }
  334. }
  335. }
  336. return ret;
  337. }
  338. /*
  339. * Debugfs info
  340. */
  341. #if defined(CONFIG_DEBUG_FS)
  342. static int amdgpu_debugfs_sa_info(struct seq_file *m, void *data)
  343. {
  344. struct drm_info_node *node = (struct drm_info_node *) m->private;
  345. struct drm_device *dev = node->minor->dev;
  346. struct amdgpu_device *adev = dev->dev_private;
  347. amdgpu_sa_bo_dump_debug_info(&adev->ring_tmp_bo, m);
  348. return 0;
  349. }
  350. static const struct drm_info_list amdgpu_debugfs_sa_list[] = {
  351. {"amdgpu_sa_info", &amdgpu_debugfs_sa_info, 0, NULL},
  352. };
  353. #endif
  354. static int amdgpu_debugfs_sa_init(struct amdgpu_device *adev)
  355. {
  356. #if defined(CONFIG_DEBUG_FS)
  357. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_sa_list, 1);
  358. #else
  359. return 0;
  360. #endif
  361. }