amdgpu_cs.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * Copyright 2008 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * 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. * PRECISION INSIGHT AND/OR ITS SUPPLIERS 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 OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Jerome Glisse <glisse@freedesktop.org>
  26. */
  27. #include <linux/pagemap.h>
  28. #include <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  33. u32 ip_instance, u32 ring,
  34. struct amdgpu_ring **out_ring)
  35. {
  36. /* Right now all IPs have only one instance - multiple rings. */
  37. if (ip_instance != 0) {
  38. DRM_ERROR("invalid ip instance: %d\n", ip_instance);
  39. return -EINVAL;
  40. }
  41. switch (ip_type) {
  42. default:
  43. DRM_ERROR("unknown ip type: %d\n", ip_type);
  44. return -EINVAL;
  45. case AMDGPU_HW_IP_GFX:
  46. if (ring < adev->gfx.num_gfx_rings) {
  47. *out_ring = &adev->gfx.gfx_ring[ring];
  48. } else {
  49. DRM_ERROR("only %d gfx rings are supported now\n",
  50. adev->gfx.num_gfx_rings);
  51. return -EINVAL;
  52. }
  53. break;
  54. case AMDGPU_HW_IP_COMPUTE:
  55. if (ring < adev->gfx.num_compute_rings) {
  56. *out_ring = &adev->gfx.compute_ring[ring];
  57. } else {
  58. DRM_ERROR("only %d compute rings are supported now\n",
  59. adev->gfx.num_compute_rings);
  60. return -EINVAL;
  61. }
  62. break;
  63. case AMDGPU_HW_IP_DMA:
  64. if (ring < adev->sdma.num_instances) {
  65. *out_ring = &adev->sdma.instance[ring].ring;
  66. } else {
  67. DRM_ERROR("only %d SDMA rings are supported\n",
  68. adev->sdma.num_instances);
  69. return -EINVAL;
  70. }
  71. break;
  72. case AMDGPU_HW_IP_UVD:
  73. *out_ring = &adev->uvd.ring;
  74. break;
  75. case AMDGPU_HW_IP_VCE:
  76. if (ring < 2){
  77. *out_ring = &adev->vce.ring[ring];
  78. } else {
  79. DRM_ERROR("only two VCE rings are supported\n");
  80. return -EINVAL;
  81. }
  82. break;
  83. }
  84. return 0;
  85. }
  86. static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
  87. struct drm_amdgpu_cs_chunk_fence *data,
  88. uint32_t *offset)
  89. {
  90. struct drm_gem_object *gobj;
  91. gobj = drm_gem_object_lookup(p->filp, data->handle);
  92. if (gobj == NULL)
  93. return -EINVAL;
  94. p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  95. p->uf_entry.priority = 0;
  96. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  97. p->uf_entry.tv.shared = true;
  98. p->uf_entry.user_pages = NULL;
  99. *offset = data->offset;
  100. drm_gem_object_unreference_unlocked(gobj);
  101. if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
  102. amdgpu_bo_unref(&p->uf_entry.robj);
  103. return -EINVAL;
  104. }
  105. return 0;
  106. }
  107. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  108. {
  109. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  110. struct amdgpu_vm *vm = &fpriv->vm;
  111. union drm_amdgpu_cs *cs = data;
  112. uint64_t *chunk_array_user;
  113. uint64_t *chunk_array;
  114. unsigned size, num_ibs = 0;
  115. uint32_t uf_offset = 0;
  116. int i;
  117. int ret;
  118. if (cs->in.num_chunks == 0)
  119. return 0;
  120. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  121. if (!chunk_array)
  122. return -ENOMEM;
  123. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  124. if (!p->ctx) {
  125. ret = -EINVAL;
  126. goto free_chunk;
  127. }
  128. /* get chunks */
  129. chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
  130. if (copy_from_user(chunk_array, chunk_array_user,
  131. sizeof(uint64_t)*cs->in.num_chunks)) {
  132. ret = -EFAULT;
  133. goto put_ctx;
  134. }
  135. p->nchunks = cs->in.num_chunks;
  136. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  137. GFP_KERNEL);
  138. if (!p->chunks) {
  139. ret = -ENOMEM;
  140. goto put_ctx;
  141. }
  142. for (i = 0; i < p->nchunks; i++) {
  143. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  144. struct drm_amdgpu_cs_chunk user_chunk;
  145. uint32_t __user *cdata;
  146. chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
  147. if (copy_from_user(&user_chunk, chunk_ptr,
  148. sizeof(struct drm_amdgpu_cs_chunk))) {
  149. ret = -EFAULT;
  150. i--;
  151. goto free_partial_kdata;
  152. }
  153. p->chunks[i].chunk_id = user_chunk.chunk_id;
  154. p->chunks[i].length_dw = user_chunk.length_dw;
  155. size = p->chunks[i].length_dw;
  156. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  157. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  158. if (p->chunks[i].kdata == NULL) {
  159. ret = -ENOMEM;
  160. i--;
  161. goto free_partial_kdata;
  162. }
  163. size *= sizeof(uint32_t);
  164. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  165. ret = -EFAULT;
  166. goto free_partial_kdata;
  167. }
  168. switch (p->chunks[i].chunk_id) {
  169. case AMDGPU_CHUNK_ID_IB:
  170. ++num_ibs;
  171. break;
  172. case AMDGPU_CHUNK_ID_FENCE:
  173. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  174. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  175. ret = -EINVAL;
  176. goto free_partial_kdata;
  177. }
  178. ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
  179. &uf_offset);
  180. if (ret)
  181. goto free_partial_kdata;
  182. break;
  183. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  184. break;
  185. default:
  186. ret = -EINVAL;
  187. goto free_partial_kdata;
  188. }
  189. }
  190. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
  191. if (ret)
  192. goto free_all_kdata;
  193. if (p->uf_entry.robj)
  194. p->job->uf_addr = uf_offset;
  195. kfree(chunk_array);
  196. return 0;
  197. free_all_kdata:
  198. i = p->nchunks - 1;
  199. free_partial_kdata:
  200. for (; i >= 0; i--)
  201. drm_free_large(p->chunks[i].kdata);
  202. kfree(p->chunks);
  203. put_ctx:
  204. amdgpu_ctx_put(p->ctx);
  205. free_chunk:
  206. kfree(chunk_array);
  207. return ret;
  208. }
  209. /* Returns how many bytes TTM can move per IB.
  210. */
  211. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  212. {
  213. u64 real_vram_size = adev->mc.real_vram_size;
  214. u64 vram_usage = atomic64_read(&adev->vram_usage);
  215. /* This function is based on the current VRAM usage.
  216. *
  217. * - If all of VRAM is free, allow relocating the number of bytes that
  218. * is equal to 1/4 of the size of VRAM for this IB.
  219. * - If more than one half of VRAM is occupied, only allow relocating
  220. * 1 MB of data for this IB.
  221. *
  222. * - From 0 to one half of used VRAM, the threshold decreases
  223. * linearly.
  224. * __________________
  225. * 1/4 of -|\ |
  226. * VRAM | \ |
  227. * | \ |
  228. * | \ |
  229. * | \ |
  230. * | \ |
  231. * | \ |
  232. * | \________|1 MB
  233. * |----------------|
  234. * VRAM 0 % 100 %
  235. * used used
  236. *
  237. * Note: It's a threshold, not a limit. The threshold must be crossed
  238. * for buffer relocations to stop, so any buffer of an arbitrary size
  239. * can be moved as long as the threshold isn't crossed before
  240. * the relocation takes place. We don't want to disable buffer
  241. * relocations completely.
  242. *
  243. * The idea is that buffers should be placed in VRAM at creation time
  244. * and TTM should only do a minimum number of relocations during
  245. * command submission. In practice, you need to submit at least
  246. * a dozen IBs to move all buffers to VRAM if they are in GTT.
  247. *
  248. * Also, things can get pretty crazy under memory pressure and actual
  249. * VRAM usage can change a lot, so playing safe even at 50% does
  250. * consistently increase performance.
  251. */
  252. u64 half_vram = real_vram_size >> 1;
  253. u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
  254. u64 bytes_moved_threshold = half_free_vram >> 1;
  255. return max(bytes_moved_threshold, 1024*1024ull);
  256. }
  257. static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
  258. struct amdgpu_bo *bo)
  259. {
  260. u64 initial_bytes_moved;
  261. uint32_t domain;
  262. int r;
  263. if (bo->pin_count)
  264. return 0;
  265. /* Avoid moving this one if we have moved too many buffers
  266. * for this IB already.
  267. *
  268. * Note that this allows moving at least one buffer of
  269. * any size, because it doesn't take the current "bo"
  270. * into account. We don't want to disallow buffer moves
  271. * completely.
  272. */
  273. if (p->bytes_moved <= p->bytes_moved_threshold)
  274. domain = bo->prefered_domains;
  275. else
  276. domain = bo->allowed_domains;
  277. retry:
  278. amdgpu_ttm_placement_from_domain(bo, domain);
  279. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  280. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  281. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  282. initial_bytes_moved;
  283. if (unlikely(r)) {
  284. if (r != -ERESTARTSYS && domain != bo->allowed_domains) {
  285. domain = bo->allowed_domains;
  286. goto retry;
  287. }
  288. }
  289. return r;
  290. }
  291. int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  292. struct list_head *validated)
  293. {
  294. struct amdgpu_bo_list_entry *lobj;
  295. int r;
  296. list_for_each_entry(lobj, validated, tv.head) {
  297. struct amdgpu_bo *bo = lobj->robj;
  298. bool binding_userptr = false;
  299. struct mm_struct *usermm;
  300. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  301. if (usermm && usermm != current->mm)
  302. return -EPERM;
  303. /* Check if we have user pages and nobody bound the BO already */
  304. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  305. size_t size = sizeof(struct page *);
  306. size *= bo->tbo.ttm->num_pages;
  307. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  308. binding_userptr = true;
  309. }
  310. r = amdgpu_cs_bo_validate(p, bo);
  311. if (r)
  312. return r;
  313. if (bo->shadow) {
  314. r = amdgpu_cs_bo_validate(p, bo);
  315. if (r)
  316. return r;
  317. }
  318. if (binding_userptr) {
  319. drm_free_large(lobj->user_pages);
  320. lobj->user_pages = NULL;
  321. }
  322. }
  323. return 0;
  324. }
  325. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  326. union drm_amdgpu_cs *cs)
  327. {
  328. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  329. struct amdgpu_bo_list_entry *e;
  330. struct list_head duplicates;
  331. bool need_mmap_lock = false;
  332. unsigned i, tries = 10;
  333. int r;
  334. INIT_LIST_HEAD(&p->validated);
  335. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  336. if (p->bo_list) {
  337. need_mmap_lock = p->bo_list->first_userptr !=
  338. p->bo_list->num_entries;
  339. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  340. }
  341. INIT_LIST_HEAD(&duplicates);
  342. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  343. if (p->uf_entry.robj)
  344. list_add(&p->uf_entry.tv.head, &p->validated);
  345. if (need_mmap_lock)
  346. down_read(&current->mm->mmap_sem);
  347. while (1) {
  348. struct list_head need_pages;
  349. unsigned i;
  350. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  351. &duplicates);
  352. if (unlikely(r != 0)) {
  353. DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
  354. goto error_free_pages;
  355. }
  356. /* Without a BO list we don't have userptr BOs */
  357. if (!p->bo_list)
  358. break;
  359. INIT_LIST_HEAD(&need_pages);
  360. for (i = p->bo_list->first_userptr;
  361. i < p->bo_list->num_entries; ++i) {
  362. e = &p->bo_list->array[i];
  363. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  364. &e->user_invalidated) && e->user_pages) {
  365. /* We acquired a page array, but somebody
  366. * invalidated it. Free it an try again
  367. */
  368. release_pages(e->user_pages,
  369. e->robj->tbo.ttm->num_pages,
  370. false);
  371. drm_free_large(e->user_pages);
  372. e->user_pages = NULL;
  373. }
  374. if (e->robj->tbo.ttm->state != tt_bound &&
  375. !e->user_pages) {
  376. list_del(&e->tv.head);
  377. list_add(&e->tv.head, &need_pages);
  378. amdgpu_bo_unreserve(e->robj);
  379. }
  380. }
  381. if (list_empty(&need_pages))
  382. break;
  383. /* Unreserve everything again. */
  384. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  385. /* We tried too many times, just abort */
  386. if (!--tries) {
  387. r = -EDEADLK;
  388. DRM_ERROR("deadlock in %s\n", __func__);
  389. goto error_free_pages;
  390. }
  391. /* Fill the page arrays for all useptrs. */
  392. list_for_each_entry(e, &need_pages, tv.head) {
  393. struct ttm_tt *ttm = e->robj->tbo.ttm;
  394. e->user_pages = drm_calloc_large(ttm->num_pages,
  395. sizeof(struct page*));
  396. if (!e->user_pages) {
  397. r = -ENOMEM;
  398. DRM_ERROR("calloc failure in %s\n", __func__);
  399. goto error_free_pages;
  400. }
  401. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  402. if (r) {
  403. DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
  404. drm_free_large(e->user_pages);
  405. e->user_pages = NULL;
  406. goto error_free_pages;
  407. }
  408. }
  409. /* And try again. */
  410. list_splice(&need_pages, &p->validated);
  411. }
  412. amdgpu_vm_get_pt_bos(p->adev, &fpriv->vm, &duplicates);
  413. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  414. p->bytes_moved = 0;
  415. r = amdgpu_cs_list_validate(p, &duplicates);
  416. if (r) {
  417. DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
  418. goto error_validate;
  419. }
  420. r = amdgpu_cs_list_validate(p, &p->validated);
  421. if (r) {
  422. DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
  423. goto error_validate;
  424. }
  425. fpriv->vm.last_eviction_counter =
  426. atomic64_read(&p->adev->num_evictions);
  427. if (p->bo_list) {
  428. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  429. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  430. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  431. struct amdgpu_vm *vm = &fpriv->vm;
  432. unsigned i;
  433. for (i = 0; i < p->bo_list->num_entries; i++) {
  434. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  435. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  436. }
  437. if (gds) {
  438. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  439. p->job->gds_size = amdgpu_bo_size(gds);
  440. }
  441. if (gws) {
  442. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  443. p->job->gws_size = amdgpu_bo_size(gws);
  444. }
  445. if (oa) {
  446. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  447. p->job->oa_size = amdgpu_bo_size(oa);
  448. }
  449. }
  450. if (p->uf_entry.robj)
  451. p->job->uf_addr += amdgpu_bo_gpu_offset(p->uf_entry.robj);
  452. error_validate:
  453. if (r) {
  454. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  455. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  456. }
  457. error_free_pages:
  458. if (need_mmap_lock)
  459. up_read(&current->mm->mmap_sem);
  460. if (p->bo_list) {
  461. for (i = p->bo_list->first_userptr;
  462. i < p->bo_list->num_entries; ++i) {
  463. e = &p->bo_list->array[i];
  464. if (!e->user_pages)
  465. continue;
  466. release_pages(e->user_pages,
  467. e->robj->tbo.ttm->num_pages,
  468. false);
  469. drm_free_large(e->user_pages);
  470. }
  471. }
  472. return r;
  473. }
  474. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  475. {
  476. struct amdgpu_bo_list_entry *e;
  477. int r;
  478. list_for_each_entry(e, &p->validated, tv.head) {
  479. struct reservation_object *resv = e->robj->tbo.resv;
  480. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  481. if (r)
  482. return r;
  483. }
  484. return 0;
  485. }
  486. /**
  487. * cs_parser_fini() - clean parser states
  488. * @parser: parser structure holding parsing context.
  489. * @error: error number
  490. *
  491. * If error is set than unvalidate buffer, otherwise just free memory
  492. * used by parsing context.
  493. **/
  494. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  495. {
  496. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  497. unsigned i;
  498. if (!error) {
  499. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  500. ttm_eu_fence_buffer_objects(&parser->ticket,
  501. &parser->validated,
  502. parser->fence);
  503. } else if (backoff) {
  504. ttm_eu_backoff_reservation(&parser->ticket,
  505. &parser->validated);
  506. }
  507. fence_put(parser->fence);
  508. if (parser->ctx)
  509. amdgpu_ctx_put(parser->ctx);
  510. if (parser->bo_list)
  511. amdgpu_bo_list_put(parser->bo_list);
  512. for (i = 0; i < parser->nchunks; i++)
  513. drm_free_large(parser->chunks[i].kdata);
  514. kfree(parser->chunks);
  515. if (parser->job)
  516. amdgpu_job_free(parser->job);
  517. amdgpu_bo_unref(&parser->uf_entry.robj);
  518. }
  519. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  520. struct amdgpu_vm *vm)
  521. {
  522. struct amdgpu_device *adev = p->adev;
  523. struct amdgpu_bo_va *bo_va;
  524. struct amdgpu_bo *bo;
  525. int i, r;
  526. r = amdgpu_vm_update_page_directory(adev, vm);
  527. if (r)
  528. return r;
  529. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  530. if (r)
  531. return r;
  532. r = amdgpu_vm_clear_freed(adev, vm);
  533. if (r)
  534. return r;
  535. if (p->bo_list) {
  536. for (i = 0; i < p->bo_list->num_entries; i++) {
  537. struct fence *f;
  538. /* ignore duplicates */
  539. bo = p->bo_list->array[i].robj;
  540. if (!bo)
  541. continue;
  542. bo_va = p->bo_list->array[i].bo_va;
  543. if (bo_va == NULL)
  544. continue;
  545. r = amdgpu_vm_bo_update(adev, bo_va, false);
  546. if (r)
  547. return r;
  548. f = bo_va->last_pt_update;
  549. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  550. if (r)
  551. return r;
  552. }
  553. }
  554. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  555. if (amdgpu_vm_debug && p->bo_list) {
  556. /* Invalidate all BOs to test for userspace bugs */
  557. for (i = 0; i < p->bo_list->num_entries; i++) {
  558. /* ignore duplicates */
  559. bo = p->bo_list->array[i].robj;
  560. if (!bo)
  561. continue;
  562. amdgpu_vm_bo_invalidate(adev, bo);
  563. }
  564. }
  565. return r;
  566. }
  567. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  568. struct amdgpu_cs_parser *p)
  569. {
  570. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  571. struct amdgpu_vm *vm = &fpriv->vm;
  572. struct amdgpu_ring *ring = p->job->ring;
  573. int i, r;
  574. /* Only for UVD/VCE VM emulation */
  575. if (ring->funcs->parse_cs) {
  576. p->job->vm = NULL;
  577. for (i = 0; i < p->job->num_ibs; i++) {
  578. r = amdgpu_ring_parse_cs(ring, p, i);
  579. if (r)
  580. return r;
  581. }
  582. } else {
  583. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  584. r = amdgpu_bo_vm_update_pte(p, vm);
  585. if (r)
  586. return r;
  587. }
  588. return amdgpu_cs_sync_rings(p);
  589. }
  590. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  591. {
  592. if (r == -EDEADLK) {
  593. r = amdgpu_gpu_reset(adev);
  594. if (!r)
  595. r = -EAGAIN;
  596. }
  597. return r;
  598. }
  599. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  600. struct amdgpu_cs_parser *parser)
  601. {
  602. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  603. struct amdgpu_vm *vm = &fpriv->vm;
  604. int i, j;
  605. int r;
  606. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  607. struct amdgpu_cs_chunk *chunk;
  608. struct amdgpu_ib *ib;
  609. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  610. struct amdgpu_ring *ring;
  611. chunk = &parser->chunks[i];
  612. ib = &parser->job->ibs[j];
  613. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  614. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  615. continue;
  616. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  617. chunk_ib->ip_instance, chunk_ib->ring,
  618. &ring);
  619. if (r)
  620. return r;
  621. if (parser->job->ring && parser->job->ring != ring)
  622. return -EINVAL;
  623. parser->job->ring = ring;
  624. if (ring->funcs->parse_cs) {
  625. struct amdgpu_bo_va_mapping *m;
  626. struct amdgpu_bo *aobj = NULL;
  627. uint64_t offset;
  628. uint8_t *kptr;
  629. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  630. &aobj);
  631. if (!aobj) {
  632. DRM_ERROR("IB va_start is invalid\n");
  633. return -EINVAL;
  634. }
  635. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  636. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  637. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  638. return -EINVAL;
  639. }
  640. /* the IB should be reserved at this point */
  641. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  642. if (r) {
  643. return r;
  644. }
  645. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  646. kptr += chunk_ib->va_start - offset;
  647. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  648. if (r) {
  649. DRM_ERROR("Failed to get ib !\n");
  650. return r;
  651. }
  652. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  653. amdgpu_bo_kunmap(aobj);
  654. } else {
  655. r = amdgpu_ib_get(adev, vm, 0, ib);
  656. if (r) {
  657. DRM_ERROR("Failed to get ib !\n");
  658. return r;
  659. }
  660. ib->gpu_addr = chunk_ib->va_start;
  661. }
  662. ib->length_dw = chunk_ib->ib_bytes / 4;
  663. ib->flags = chunk_ib->flags;
  664. j++;
  665. }
  666. /* UVD & VCE fw doesn't support user fences */
  667. if (parser->job->uf_addr && (
  668. parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  669. parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
  670. return -EINVAL;
  671. return 0;
  672. }
  673. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  674. struct amdgpu_cs_parser *p)
  675. {
  676. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  677. int i, j, r;
  678. for (i = 0; i < p->nchunks; ++i) {
  679. struct drm_amdgpu_cs_chunk_dep *deps;
  680. struct amdgpu_cs_chunk *chunk;
  681. unsigned num_deps;
  682. chunk = &p->chunks[i];
  683. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  684. continue;
  685. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  686. num_deps = chunk->length_dw * 4 /
  687. sizeof(struct drm_amdgpu_cs_chunk_dep);
  688. for (j = 0; j < num_deps; ++j) {
  689. struct amdgpu_ring *ring;
  690. struct amdgpu_ctx *ctx;
  691. struct fence *fence;
  692. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  693. deps[j].ip_instance,
  694. deps[j].ring, &ring);
  695. if (r)
  696. return r;
  697. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  698. if (ctx == NULL)
  699. return -EINVAL;
  700. fence = amdgpu_ctx_get_fence(ctx, ring,
  701. deps[j].handle);
  702. if (IS_ERR(fence)) {
  703. r = PTR_ERR(fence);
  704. amdgpu_ctx_put(ctx);
  705. return r;
  706. } else if (fence) {
  707. r = amdgpu_sync_fence(adev, &p->job->sync,
  708. fence);
  709. fence_put(fence);
  710. amdgpu_ctx_put(ctx);
  711. if (r)
  712. return r;
  713. }
  714. }
  715. }
  716. return 0;
  717. }
  718. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  719. union drm_amdgpu_cs *cs)
  720. {
  721. struct amdgpu_ring *ring = p->job->ring;
  722. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  723. struct amdgpu_job *job;
  724. int r;
  725. job = p->job;
  726. p->job = NULL;
  727. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  728. if (r) {
  729. amdgpu_job_free(job);
  730. return r;
  731. }
  732. job->owner = p->filp;
  733. job->ctx = entity->fence_context;
  734. p->fence = fence_get(&job->base.s_fence->finished);
  735. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  736. job->uf_sequence = cs->out.handle;
  737. amdgpu_job_free_resources(job);
  738. trace_amdgpu_cs_ioctl(job);
  739. amd_sched_entity_push_job(&job->base);
  740. return 0;
  741. }
  742. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  743. {
  744. struct amdgpu_device *adev = dev->dev_private;
  745. union drm_amdgpu_cs *cs = data;
  746. struct amdgpu_cs_parser parser = {};
  747. bool reserved_buffers = false;
  748. int i, r;
  749. if (!adev->accel_working)
  750. return -EBUSY;
  751. parser.adev = adev;
  752. parser.filp = filp;
  753. r = amdgpu_cs_parser_init(&parser, data);
  754. if (r) {
  755. DRM_ERROR("Failed to initialize parser !\n");
  756. amdgpu_cs_parser_fini(&parser, r, false);
  757. r = amdgpu_cs_handle_lockup(adev, r);
  758. return r;
  759. }
  760. r = amdgpu_cs_parser_bos(&parser, data);
  761. if (r == -ENOMEM)
  762. DRM_ERROR("Not enough memory for command submission!\n");
  763. else if (r && r != -ERESTARTSYS)
  764. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  765. else if (!r) {
  766. reserved_buffers = true;
  767. r = amdgpu_cs_ib_fill(adev, &parser);
  768. }
  769. if (!r) {
  770. r = amdgpu_cs_dependencies(adev, &parser);
  771. if (r)
  772. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  773. }
  774. if (r)
  775. goto out;
  776. for (i = 0; i < parser.job->num_ibs; i++)
  777. trace_amdgpu_cs(&parser, i);
  778. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  779. if (r)
  780. goto out;
  781. r = amdgpu_cs_submit(&parser, cs);
  782. out:
  783. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  784. r = amdgpu_cs_handle_lockup(adev, r);
  785. return r;
  786. }
  787. /**
  788. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  789. *
  790. * @dev: drm device
  791. * @data: data from userspace
  792. * @filp: file private
  793. *
  794. * Wait for the command submission identified by handle to finish.
  795. */
  796. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  797. struct drm_file *filp)
  798. {
  799. union drm_amdgpu_wait_cs *wait = data;
  800. struct amdgpu_device *adev = dev->dev_private;
  801. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  802. struct amdgpu_ring *ring = NULL;
  803. struct amdgpu_ctx *ctx;
  804. struct fence *fence;
  805. long r;
  806. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  807. wait->in.ring, &ring);
  808. if (r)
  809. return r;
  810. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  811. if (ctx == NULL)
  812. return -EINVAL;
  813. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  814. if (IS_ERR(fence))
  815. r = PTR_ERR(fence);
  816. else if (fence) {
  817. r = fence_wait_timeout(fence, true, timeout);
  818. fence_put(fence);
  819. } else
  820. r = 1;
  821. amdgpu_ctx_put(ctx);
  822. if (r < 0)
  823. return r;
  824. memset(wait, 0, sizeof(*wait));
  825. wait->out.status = (r == 0);
  826. return 0;
  827. }
  828. /**
  829. * amdgpu_cs_find_bo_va - find bo_va for VM address
  830. *
  831. * @parser: command submission parser context
  832. * @addr: VM address
  833. * @bo: resulting BO of the mapping found
  834. *
  835. * Search the buffer objects in the command submission context for a certain
  836. * virtual memory address. Returns allocation structure when found, NULL
  837. * otherwise.
  838. */
  839. struct amdgpu_bo_va_mapping *
  840. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  841. uint64_t addr, struct amdgpu_bo **bo)
  842. {
  843. struct amdgpu_bo_va_mapping *mapping;
  844. unsigned i;
  845. if (!parser->bo_list)
  846. return NULL;
  847. addr /= AMDGPU_GPU_PAGE_SIZE;
  848. for (i = 0; i < parser->bo_list->num_entries; i++) {
  849. struct amdgpu_bo_list_entry *lobj;
  850. lobj = &parser->bo_list->array[i];
  851. if (!lobj->bo_va)
  852. continue;
  853. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  854. if (mapping->it.start > addr ||
  855. addr > mapping->it.last)
  856. continue;
  857. *bo = lobj->bo_va->bo;
  858. return mapping;
  859. }
  860. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  861. if (mapping->it.start > addr ||
  862. addr > mapping->it.last)
  863. continue;
  864. *bo = lobj->bo_va->bo;
  865. return mapping;
  866. }
  867. }
  868. return NULL;
  869. }