amdgpu_cs.c 24 KB

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