amdgpu_cs.c 25 KB

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