amdgpu_cs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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_bo = amdgpu_bo_ref(p->uf_entry.robj);
  195. p->job->uf_offset = uf_offset;
  196. }
  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->uf_entry.robj)
  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_bo *gds = p->bo_list->gds_obj;
  407. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  408. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  409. struct amdgpu_vm *vm = &fpriv->vm;
  410. unsigned i;
  411. for (i = 0; i < p->bo_list->num_entries; i++) {
  412. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  413. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  414. }
  415. if (gds) {
  416. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  417. p->job->gds_size = amdgpu_bo_size(gds);
  418. }
  419. if (gws) {
  420. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  421. p->job->gws_size = amdgpu_bo_size(gws);
  422. }
  423. if (oa) {
  424. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  425. p->job->oa_size = amdgpu_bo_size(oa);
  426. }
  427. }
  428. error_validate:
  429. if (r) {
  430. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  431. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  432. }
  433. error_free_pages:
  434. if (need_mmap_lock)
  435. up_read(&current->mm->mmap_sem);
  436. if (p->bo_list) {
  437. for (i = p->bo_list->first_userptr;
  438. i < p->bo_list->num_entries; ++i) {
  439. e = &p->bo_list->array[i];
  440. if (!e->user_pages)
  441. continue;
  442. release_pages(e->user_pages,
  443. e->robj->tbo.ttm->num_pages,
  444. false);
  445. drm_free_large(e->user_pages);
  446. }
  447. }
  448. return r;
  449. }
  450. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  451. {
  452. struct amdgpu_bo_list_entry *e;
  453. int r;
  454. list_for_each_entry(e, &p->validated, tv.head) {
  455. struct reservation_object *resv = e->robj->tbo.resv;
  456. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  457. if (r)
  458. return r;
  459. }
  460. return 0;
  461. }
  462. /**
  463. * cs_parser_fini() - clean parser states
  464. * @parser: parser structure holding parsing context.
  465. * @error: error number
  466. *
  467. * If error is set than unvalidate buffer, otherwise just free memory
  468. * used by parsing context.
  469. **/
  470. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  471. {
  472. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  473. unsigned i;
  474. if (!error) {
  475. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  476. ttm_eu_fence_buffer_objects(&parser->ticket,
  477. &parser->validated,
  478. parser->fence);
  479. } else if (backoff) {
  480. ttm_eu_backoff_reservation(&parser->ticket,
  481. &parser->validated);
  482. }
  483. fence_put(parser->fence);
  484. if (parser->ctx)
  485. amdgpu_ctx_put(parser->ctx);
  486. if (parser->bo_list)
  487. amdgpu_bo_list_put(parser->bo_list);
  488. for (i = 0; i < parser->nchunks; i++)
  489. drm_free_large(parser->chunks[i].kdata);
  490. kfree(parser->chunks);
  491. if (parser->job)
  492. amdgpu_job_free(parser->job);
  493. amdgpu_bo_unref(&parser->uf_entry.robj);
  494. }
  495. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  496. struct amdgpu_vm *vm)
  497. {
  498. struct amdgpu_device *adev = p->adev;
  499. struct amdgpu_bo_va *bo_va;
  500. struct amdgpu_bo *bo;
  501. int i, r;
  502. r = amdgpu_vm_update_page_directory(adev, vm);
  503. if (r)
  504. return r;
  505. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  506. if (r)
  507. return r;
  508. r = amdgpu_vm_clear_freed(adev, vm);
  509. if (r)
  510. return r;
  511. if (p->bo_list) {
  512. for (i = 0; i < p->bo_list->num_entries; i++) {
  513. struct fence *f;
  514. /* ignore duplicates */
  515. bo = p->bo_list->array[i].robj;
  516. if (!bo)
  517. continue;
  518. bo_va = p->bo_list->array[i].bo_va;
  519. if (bo_va == NULL)
  520. continue;
  521. r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
  522. if (r)
  523. return r;
  524. f = bo_va->last_pt_update;
  525. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  526. if (r)
  527. return r;
  528. }
  529. }
  530. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  531. if (amdgpu_vm_debug && p->bo_list) {
  532. /* Invalidate all BOs to test for userspace bugs */
  533. for (i = 0; i < p->bo_list->num_entries; i++) {
  534. /* ignore duplicates */
  535. bo = p->bo_list->array[i].robj;
  536. if (!bo)
  537. continue;
  538. amdgpu_vm_bo_invalidate(adev, bo);
  539. }
  540. }
  541. return r;
  542. }
  543. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  544. struct amdgpu_cs_parser *p)
  545. {
  546. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  547. struct amdgpu_vm *vm = &fpriv->vm;
  548. struct amdgpu_ring *ring = p->job->ring;
  549. int i, r;
  550. /* Only for UVD/VCE VM emulation */
  551. if (ring->funcs->parse_cs) {
  552. for (i = 0; i < p->job->num_ibs; i++) {
  553. r = amdgpu_ring_parse_cs(ring, p, i);
  554. if (r)
  555. return r;
  556. }
  557. }
  558. r = amdgpu_bo_vm_update_pte(p, vm);
  559. if (!r)
  560. amdgpu_cs_sync_rings(p);
  561. return r;
  562. }
  563. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  564. {
  565. if (r == -EDEADLK) {
  566. r = amdgpu_gpu_reset(adev);
  567. if (!r)
  568. r = -EAGAIN;
  569. }
  570. return r;
  571. }
  572. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  573. struct amdgpu_cs_parser *parser)
  574. {
  575. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  576. struct amdgpu_vm *vm = &fpriv->vm;
  577. int i, j;
  578. int r;
  579. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  580. struct amdgpu_cs_chunk *chunk;
  581. struct amdgpu_ib *ib;
  582. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  583. struct amdgpu_ring *ring;
  584. chunk = &parser->chunks[i];
  585. ib = &parser->job->ibs[j];
  586. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  587. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  588. continue;
  589. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  590. chunk_ib->ip_instance, chunk_ib->ring,
  591. &ring);
  592. if (r)
  593. return r;
  594. if (parser->job->ring && parser->job->ring != ring)
  595. return -EINVAL;
  596. parser->job->ring = ring;
  597. if (ring->funcs->parse_cs) {
  598. struct amdgpu_bo_va_mapping *m;
  599. struct amdgpu_bo *aobj = NULL;
  600. uint64_t offset;
  601. uint8_t *kptr;
  602. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  603. &aobj);
  604. if (!aobj) {
  605. DRM_ERROR("IB va_start is invalid\n");
  606. return -EINVAL;
  607. }
  608. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  609. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  610. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  611. return -EINVAL;
  612. }
  613. /* the IB should be reserved at this point */
  614. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  615. if (r) {
  616. return r;
  617. }
  618. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  619. kptr += chunk_ib->va_start - offset;
  620. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  621. if (r) {
  622. DRM_ERROR("Failed to get ib !\n");
  623. return r;
  624. }
  625. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  626. amdgpu_bo_kunmap(aobj);
  627. } else {
  628. r = amdgpu_ib_get(adev, vm, 0, ib);
  629. if (r) {
  630. DRM_ERROR("Failed to get ib !\n");
  631. return r;
  632. }
  633. ib->gpu_addr = chunk_ib->va_start;
  634. }
  635. ib->length_dw = chunk_ib->ib_bytes / 4;
  636. ib->flags = chunk_ib->flags;
  637. j++;
  638. }
  639. /* UVD & VCE fw doesn't support user fences */
  640. if (parser->job->uf_bo && (
  641. parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  642. parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
  643. return -EINVAL;
  644. return 0;
  645. }
  646. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  647. struct amdgpu_cs_parser *p)
  648. {
  649. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  650. int i, j, r;
  651. for (i = 0; i < p->nchunks; ++i) {
  652. struct drm_amdgpu_cs_chunk_dep *deps;
  653. struct amdgpu_cs_chunk *chunk;
  654. unsigned num_deps;
  655. chunk = &p->chunks[i];
  656. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  657. continue;
  658. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  659. num_deps = chunk->length_dw * 4 /
  660. sizeof(struct drm_amdgpu_cs_chunk_dep);
  661. for (j = 0; j < num_deps; ++j) {
  662. struct amdgpu_ring *ring;
  663. struct amdgpu_ctx *ctx;
  664. struct fence *fence;
  665. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  666. deps[j].ip_instance,
  667. deps[j].ring, &ring);
  668. if (r)
  669. return r;
  670. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  671. if (ctx == NULL)
  672. return -EINVAL;
  673. fence = amdgpu_ctx_get_fence(ctx, ring,
  674. deps[j].handle);
  675. if (IS_ERR(fence)) {
  676. r = PTR_ERR(fence);
  677. amdgpu_ctx_put(ctx);
  678. return r;
  679. } else if (fence) {
  680. r = amdgpu_sync_fence(adev, &p->job->sync,
  681. fence);
  682. fence_put(fence);
  683. amdgpu_ctx_put(ctx);
  684. if (r)
  685. return r;
  686. }
  687. }
  688. }
  689. return 0;
  690. }
  691. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  692. union drm_amdgpu_cs *cs)
  693. {
  694. struct amdgpu_ring *ring = p->job->ring;
  695. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  696. struct fence *fence;
  697. struct amdgpu_job *job;
  698. int r;
  699. job = p->job;
  700. p->job = NULL;
  701. r = amd_sched_job_init(&job->base, &ring->sched,
  702. entity, amdgpu_job_timeout_func,
  703. amdgpu_job_free_func,
  704. p->filp, &fence);
  705. if (r) {
  706. amdgpu_job_free(job);
  707. return r;
  708. }
  709. job->owner = p->filp;
  710. job->ctx = entity->fence_context;
  711. p->fence = fence_get(fence);
  712. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, fence);
  713. job->uf_sequence = cs->out.handle;
  714. trace_amdgpu_cs_ioctl(job);
  715. amd_sched_entity_push_job(&job->base);
  716. return 0;
  717. }
  718. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  719. {
  720. struct amdgpu_device *adev = dev->dev_private;
  721. union drm_amdgpu_cs *cs = data;
  722. struct amdgpu_cs_parser parser = {};
  723. bool reserved_buffers = false;
  724. int i, r;
  725. if (!adev->accel_working)
  726. return -EBUSY;
  727. parser.adev = adev;
  728. parser.filp = filp;
  729. r = amdgpu_cs_parser_init(&parser, data);
  730. if (r) {
  731. DRM_ERROR("Failed to initialize parser !\n");
  732. amdgpu_cs_parser_fini(&parser, r, false);
  733. r = amdgpu_cs_handle_lockup(adev, r);
  734. return r;
  735. }
  736. r = amdgpu_cs_parser_bos(&parser, data);
  737. if (r == -ENOMEM)
  738. DRM_ERROR("Not enough memory for command submission!\n");
  739. else if (r && r != -ERESTARTSYS)
  740. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  741. else if (!r) {
  742. reserved_buffers = true;
  743. r = amdgpu_cs_ib_fill(adev, &parser);
  744. }
  745. if (!r) {
  746. r = amdgpu_cs_dependencies(adev, &parser);
  747. if (r)
  748. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  749. }
  750. if (r)
  751. goto out;
  752. for (i = 0; i < parser.job->num_ibs; i++)
  753. trace_amdgpu_cs(&parser, i);
  754. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  755. if (r)
  756. goto out;
  757. r = amdgpu_cs_submit(&parser, cs);
  758. out:
  759. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  760. r = amdgpu_cs_handle_lockup(adev, r);
  761. return r;
  762. }
  763. /**
  764. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  765. *
  766. * @dev: drm device
  767. * @data: data from userspace
  768. * @filp: file private
  769. *
  770. * Wait for the command submission identified by handle to finish.
  771. */
  772. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  773. struct drm_file *filp)
  774. {
  775. union drm_amdgpu_wait_cs *wait = data;
  776. struct amdgpu_device *adev = dev->dev_private;
  777. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  778. struct amdgpu_ring *ring = NULL;
  779. struct amdgpu_ctx *ctx;
  780. struct fence *fence;
  781. long r;
  782. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  783. wait->in.ring, &ring);
  784. if (r)
  785. return r;
  786. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  787. if (ctx == NULL)
  788. return -EINVAL;
  789. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  790. if (IS_ERR(fence))
  791. r = PTR_ERR(fence);
  792. else if (fence) {
  793. r = fence_wait_timeout(fence, true, timeout);
  794. fence_put(fence);
  795. } else
  796. r = 1;
  797. amdgpu_ctx_put(ctx);
  798. if (r < 0)
  799. return r;
  800. memset(wait, 0, sizeof(*wait));
  801. wait->out.status = (r == 0);
  802. return 0;
  803. }
  804. /**
  805. * amdgpu_cs_find_bo_va - find bo_va for VM address
  806. *
  807. * @parser: command submission parser context
  808. * @addr: VM address
  809. * @bo: resulting BO of the mapping found
  810. *
  811. * Search the buffer objects in the command submission context for a certain
  812. * virtual memory address. Returns allocation structure when found, NULL
  813. * otherwise.
  814. */
  815. struct amdgpu_bo_va_mapping *
  816. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  817. uint64_t addr, struct amdgpu_bo **bo)
  818. {
  819. struct amdgpu_bo_va_mapping *mapping;
  820. unsigned i;
  821. if (!parser->bo_list)
  822. return NULL;
  823. addr /= AMDGPU_GPU_PAGE_SIZE;
  824. for (i = 0; i < parser->bo_list->num_entries; i++) {
  825. struct amdgpu_bo_list_entry *lobj;
  826. lobj = &parser->bo_list->array[i];
  827. if (!lobj->bo_va)
  828. continue;
  829. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  830. if (mapping->it.start > addr ||
  831. addr > mapping->it.last)
  832. continue;
  833. *bo = lobj->bo_va->bo;
  834. return mapping;
  835. }
  836. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  837. if (mapping->it.start > addr ||
  838. addr > mapping->it.last)
  839. continue;
  840. *bo = lobj->bo_va->bo;
  841. return mapping;
  842. }
  843. }
  844. return NULL;
  845. }