amdgpu_ib.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_device *adev, struct amdgpu_vm *vm,
  57. unsigned size, struct amdgpu_ib *ib)
  58. {
  59. int r;
  60. if (size) {
  61. r = amdgpu_sa_bo_new(&adev->ring_tmp_bo,
  62. &ib->sa_bo, size, 256);
  63. if (r) {
  64. dev_err(adev->dev, "failed to get a new IB (%d)\n", r);
  65. return r;
  66. }
  67. ib->ptr = amdgpu_sa_bo_cpu_addr(ib->sa_bo);
  68. if (!vm)
  69. ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
  70. }
  71. return 0;
  72. }
  73. /**
  74. * amdgpu_ib_free - free an IB (Indirect Buffer)
  75. *
  76. * @adev: amdgpu_device pointer
  77. * @ib: IB object to free
  78. * @f: the fence SA bo need wait on for the ib alloation
  79. *
  80. * Free an IB (all asics).
  81. */
  82. void amdgpu_ib_free(struct amdgpu_device *adev, struct amdgpu_ib *ib,
  83. struct fence *f)
  84. {
  85. amdgpu_sa_bo_free(adev, &ib->sa_bo, f);
  86. }
  87. /**
  88. * amdgpu_ib_schedule - schedule an IB (Indirect Buffer) on the ring
  89. *
  90. * @adev: amdgpu_device pointer
  91. * @num_ibs: number of IBs to schedule
  92. * @ibs: IB objects to schedule
  93. * @f: fence created during this submission
  94. *
  95. * Schedule an IB on the associated ring (all asics).
  96. * Returns 0 on success, error on failure.
  97. *
  98. * On SI, there are two parallel engines fed from the primary ring,
  99. * the CE (Constant Engine) and the DE (Drawing Engine). Since
  100. * resource descriptors have moved to memory, the CE allows you to
  101. * prime the caches while the DE is updating register state so that
  102. * the resource descriptors will be already in cache when the draw is
  103. * processed. To accomplish this, the userspace driver submits two
  104. * IBs, one for the CE and one for the DE. If there is a CE IB (called
  105. * a CONST_IB), it will be put on the ring prior to the DE IB. Prior
  106. * to SI there was just a DE IB.
  107. */
  108. int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned num_ibs,
  109. struct amdgpu_ib *ibs, struct fence *last_vm_update,
  110. struct amdgpu_job *job, struct fence **f)
  111. {
  112. struct amdgpu_device *adev = ring->adev;
  113. struct amdgpu_ib *ib = &ibs[0];
  114. bool skip_preamble, need_ctx_switch;
  115. unsigned patch_offset = ~0;
  116. struct amdgpu_vm *vm;
  117. int vmid = 0, old_vmid = ring->vmid;
  118. struct fence *hwf;
  119. uint64_t ctx;
  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. ctx = job->ctx;
  128. vmid = job->vm_id;
  129. } else {
  130. vm = NULL;
  131. ctx = 0;
  132. vmid = 0;
  133. }
  134. if (!ring->ready) {
  135. dev_err(adev->dev, "couldn't schedule ib\n");
  136. return -EINVAL;
  137. }
  138. if (vm && !job->vm_id) {
  139. dev_err(adev->dev, "VM IB without ID\n");
  140. return -EINVAL;
  141. }
  142. r = amdgpu_ring_alloc(ring, 256 * num_ibs);
  143. if (r) {
  144. dev_err(adev->dev, "scheduling IB failed (%d).\n", r);
  145. return r;
  146. }
  147. if (ring->type == AMDGPU_RING_TYPE_SDMA && ring->funcs->init_cond_exec)
  148. patch_offset = amdgpu_ring_init_cond_exec(ring);
  149. if (vm) {
  150. r = amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr,
  151. job->gds_base, job->gds_size,
  152. job->gws_base, job->gws_size,
  153. job->oa_base, job->oa_size,
  154. (ring->current_ctx == ctx) && (old_vmid != vmid));
  155. if (r) {
  156. amdgpu_ring_undo(ring);
  157. return r;
  158. }
  159. }
  160. if (ring->funcs->emit_hdp_flush)
  161. amdgpu_ring_emit_hdp_flush(ring);
  162. /* always set cond_exec_polling to CONTINUE */
  163. *ring->cond_exe_cpu_addr = 1;
  164. skip_preamble = ring->current_ctx == ctx;
  165. need_ctx_switch = ring->current_ctx != ctx;
  166. for (i = 0; i < num_ibs; ++i) {
  167. ib = &ibs[i];
  168. /* drop preamble IBs if we don't have a context switch */
  169. if ((ib->flags & AMDGPU_IB_FLAG_PREAMBLE) && skip_preamble)
  170. continue;
  171. amdgpu_ring_emit_ib(ring, ib, job ? job->vm_id : 0,
  172. need_ctx_switch);
  173. need_ctx_switch = false;
  174. ring->vmid = vmid;
  175. }
  176. if (ring->funcs->emit_hdp_invalidate)
  177. amdgpu_ring_emit_hdp_invalidate(ring);
  178. r = amdgpu_fence_emit(ring, &hwf);
  179. if (r) {
  180. dev_err(adev->dev, "failed to emit fence (%d)\n", r);
  181. if (job && job->vm_id)
  182. amdgpu_vm_reset_id(adev, job->vm_id);
  183. ring->vmid = old_vmid;
  184. amdgpu_ring_undo(ring);
  185. return r;
  186. }
  187. /* wrap the last IB with fence */
  188. if (job && job->uf_bo) {
  189. uint64_t addr = amdgpu_bo_gpu_offset(job->uf_bo);
  190. addr += job->uf_offset;
  191. amdgpu_ring_emit_fence(ring, addr, job->uf_sequence,
  192. AMDGPU_FENCE_FLAG_64BIT);
  193. }
  194. if (f)
  195. *f = fence_get(hwf);
  196. if (patch_offset != ~0 && ring->funcs->patch_cond_exec)
  197. amdgpu_ring_patch_cond_exec(ring, patch_offset);
  198. ring->current_ctx = ctx;
  199. amdgpu_ring_commit(ring);
  200. return 0;
  201. }
  202. /**
  203. * amdgpu_ib_pool_init - Init the IB (Indirect Buffer) pool
  204. *
  205. * @adev: amdgpu_device pointer
  206. *
  207. * Initialize the suballocator to manage a pool of memory
  208. * for use as IBs (all asics).
  209. * Returns 0 on success, error on failure.
  210. */
  211. int amdgpu_ib_pool_init(struct amdgpu_device *adev)
  212. {
  213. int r;
  214. if (adev->ib_pool_ready) {
  215. return 0;
  216. }
  217. r = amdgpu_sa_bo_manager_init(adev, &adev->ring_tmp_bo,
  218. AMDGPU_IB_POOL_SIZE*64*1024,
  219. AMDGPU_GPU_PAGE_SIZE,
  220. AMDGPU_GEM_DOMAIN_GTT);
  221. if (r) {
  222. return r;
  223. }
  224. r = amdgpu_sa_bo_manager_start(adev, &adev->ring_tmp_bo);
  225. if (r) {
  226. return r;
  227. }
  228. adev->ib_pool_ready = true;
  229. if (amdgpu_debugfs_sa_init(adev)) {
  230. dev_err(adev->dev, "failed to register debugfs file for SA\n");
  231. }
  232. return 0;
  233. }
  234. /**
  235. * amdgpu_ib_pool_fini - Free the IB (Indirect Buffer) pool
  236. *
  237. * @adev: amdgpu_device pointer
  238. *
  239. * Tear down the suballocator managing the pool of memory
  240. * for use as IBs (all asics).
  241. */
  242. void amdgpu_ib_pool_fini(struct amdgpu_device *adev)
  243. {
  244. if (adev->ib_pool_ready) {
  245. amdgpu_sa_bo_manager_suspend(adev, &adev->ring_tmp_bo);
  246. amdgpu_sa_bo_manager_fini(adev, &adev->ring_tmp_bo);
  247. adev->ib_pool_ready = false;
  248. }
  249. }
  250. /**
  251. * amdgpu_ib_ring_tests - test IBs on the rings
  252. *
  253. * @adev: amdgpu_device pointer
  254. *
  255. * Test an IB (Indirect Buffer) on each ring.
  256. * If the test fails, disable the ring.
  257. * Returns 0 on success, error if the primary GFX ring
  258. * IB test fails.
  259. */
  260. int amdgpu_ib_ring_tests(struct amdgpu_device *adev)
  261. {
  262. unsigned i;
  263. int r;
  264. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  265. struct amdgpu_ring *ring = adev->rings[i];
  266. if (!ring || !ring->ready)
  267. continue;
  268. r = amdgpu_ring_test_ib(ring);
  269. if (r) {
  270. ring->ready = false;
  271. if (ring == &adev->gfx.gfx_ring[0]) {
  272. /* oh, oh, that's really bad */
  273. DRM_ERROR("amdgpu: failed testing IB on GFX ring (%d).\n", r);
  274. adev->accel_working = false;
  275. return r;
  276. } else {
  277. /* still not good, but we can live with it */
  278. DRM_ERROR("amdgpu: failed testing IB on ring %d (%d).\n", i, r);
  279. }
  280. }
  281. }
  282. return 0;
  283. }
  284. /*
  285. * Debugfs info
  286. */
  287. #if defined(CONFIG_DEBUG_FS)
  288. static int amdgpu_debugfs_sa_info(struct seq_file *m, void *data)
  289. {
  290. struct drm_info_node *node = (struct drm_info_node *) m->private;
  291. struct drm_device *dev = node->minor->dev;
  292. struct amdgpu_device *adev = dev->dev_private;
  293. amdgpu_sa_bo_dump_debug_info(&adev->ring_tmp_bo, m);
  294. return 0;
  295. }
  296. static const struct drm_info_list amdgpu_debugfs_sa_list[] = {
  297. {"amdgpu_sa_info", &amdgpu_debugfs_sa_info, 0, NULL},
  298. };
  299. #endif
  300. static int amdgpu_debugfs_sa_init(struct amdgpu_device *adev)
  301. {
  302. #if defined(CONFIG_DEBUG_FS)
  303. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_sa_list, 1);
  304. #else
  305. return 0;
  306. #endif
  307. }