amdgpu_cs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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(p->adev, &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. fpriv->vm.last_eviction_counter =
  406. atomic64_read(&p->adev->num_evictions);
  407. if (p->bo_list) {
  408. struct amdgpu_bo *gds = p->bo_list->gds_obj;
  409. struct amdgpu_bo *gws = p->bo_list->gws_obj;
  410. struct amdgpu_bo *oa = p->bo_list->oa_obj;
  411. struct amdgpu_vm *vm = &fpriv->vm;
  412. unsigned i;
  413. for (i = 0; i < p->bo_list->num_entries; i++) {
  414. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  415. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  416. }
  417. if (gds) {
  418. p->job->gds_base = amdgpu_bo_gpu_offset(gds);
  419. p->job->gds_size = amdgpu_bo_size(gds);
  420. }
  421. if (gws) {
  422. p->job->gws_base = amdgpu_bo_gpu_offset(gws);
  423. p->job->gws_size = amdgpu_bo_size(gws);
  424. }
  425. if (oa) {
  426. p->job->oa_base = amdgpu_bo_gpu_offset(oa);
  427. p->job->oa_size = amdgpu_bo_size(oa);
  428. }
  429. }
  430. error_validate:
  431. if (r) {
  432. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  433. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  434. }
  435. error_free_pages:
  436. if (need_mmap_lock)
  437. up_read(&current->mm->mmap_sem);
  438. if (p->bo_list) {
  439. for (i = p->bo_list->first_userptr;
  440. i < p->bo_list->num_entries; ++i) {
  441. e = &p->bo_list->array[i];
  442. if (!e->user_pages)
  443. continue;
  444. release_pages(e->user_pages,
  445. e->robj->tbo.ttm->num_pages,
  446. false);
  447. drm_free_large(e->user_pages);
  448. }
  449. }
  450. return r;
  451. }
  452. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  453. {
  454. struct amdgpu_bo_list_entry *e;
  455. int r;
  456. list_for_each_entry(e, &p->validated, tv.head) {
  457. struct reservation_object *resv = e->robj->tbo.resv;
  458. r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
  459. if (r)
  460. return r;
  461. }
  462. return 0;
  463. }
  464. /**
  465. * cs_parser_fini() - clean parser states
  466. * @parser: parser structure holding parsing context.
  467. * @error: error number
  468. *
  469. * If error is set than unvalidate buffer, otherwise just free memory
  470. * used by parsing context.
  471. **/
  472. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  473. {
  474. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  475. unsigned i;
  476. if (!error) {
  477. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  478. ttm_eu_fence_buffer_objects(&parser->ticket,
  479. &parser->validated,
  480. parser->fence);
  481. } else if (backoff) {
  482. ttm_eu_backoff_reservation(&parser->ticket,
  483. &parser->validated);
  484. }
  485. fence_put(parser->fence);
  486. if (parser->ctx)
  487. amdgpu_ctx_put(parser->ctx);
  488. if (parser->bo_list)
  489. amdgpu_bo_list_put(parser->bo_list);
  490. for (i = 0; i < parser->nchunks; i++)
  491. drm_free_large(parser->chunks[i].kdata);
  492. kfree(parser->chunks);
  493. if (parser->job)
  494. amdgpu_job_free(parser->job);
  495. amdgpu_bo_unref(&parser->uf_entry.robj);
  496. }
  497. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  498. struct amdgpu_vm *vm)
  499. {
  500. struct amdgpu_device *adev = p->adev;
  501. struct amdgpu_bo_va *bo_va;
  502. struct amdgpu_bo *bo;
  503. int i, r;
  504. r = amdgpu_vm_update_page_directory(adev, vm);
  505. if (r)
  506. return r;
  507. r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence);
  508. if (r)
  509. return r;
  510. r = amdgpu_vm_clear_freed(adev, vm);
  511. if (r)
  512. return r;
  513. if (p->bo_list) {
  514. for (i = 0; i < p->bo_list->num_entries; i++) {
  515. struct fence *f;
  516. /* ignore duplicates */
  517. bo = p->bo_list->array[i].robj;
  518. if (!bo)
  519. continue;
  520. bo_va = p->bo_list->array[i].bo_va;
  521. if (bo_va == NULL)
  522. continue;
  523. r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
  524. if (r)
  525. return r;
  526. f = bo_va->last_pt_update;
  527. r = amdgpu_sync_fence(adev, &p->job->sync, f);
  528. if (r)
  529. return r;
  530. }
  531. }
  532. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
  533. if (amdgpu_vm_debug && p->bo_list) {
  534. /* Invalidate all BOs to test for userspace bugs */
  535. for (i = 0; i < p->bo_list->num_entries; i++) {
  536. /* ignore duplicates */
  537. bo = p->bo_list->array[i].robj;
  538. if (!bo)
  539. continue;
  540. amdgpu_vm_bo_invalidate(adev, bo);
  541. }
  542. }
  543. return r;
  544. }
  545. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  546. struct amdgpu_cs_parser *p)
  547. {
  548. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  549. struct amdgpu_vm *vm = &fpriv->vm;
  550. struct amdgpu_ring *ring = p->job->ring;
  551. int i, r;
  552. /* Only for UVD/VCE VM emulation */
  553. if (ring->funcs->parse_cs) {
  554. for (i = 0; i < p->job->num_ibs; i++) {
  555. r = amdgpu_ring_parse_cs(ring, p, i);
  556. if (r)
  557. return r;
  558. }
  559. }
  560. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  561. r = amdgpu_bo_vm_update_pte(p, vm);
  562. if (!r)
  563. amdgpu_cs_sync_rings(p);
  564. return r;
  565. }
  566. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  567. {
  568. if (r == -EDEADLK) {
  569. r = amdgpu_gpu_reset(adev);
  570. if (!r)
  571. r = -EAGAIN;
  572. }
  573. return r;
  574. }
  575. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  576. struct amdgpu_cs_parser *parser)
  577. {
  578. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  579. struct amdgpu_vm *vm = &fpriv->vm;
  580. int i, j;
  581. int r;
  582. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  583. struct amdgpu_cs_chunk *chunk;
  584. struct amdgpu_ib *ib;
  585. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  586. struct amdgpu_ring *ring;
  587. chunk = &parser->chunks[i];
  588. ib = &parser->job->ibs[j];
  589. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  590. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  591. continue;
  592. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  593. chunk_ib->ip_instance, chunk_ib->ring,
  594. &ring);
  595. if (r)
  596. return r;
  597. if (parser->job->ring && parser->job->ring != ring)
  598. return -EINVAL;
  599. parser->job->ring = ring;
  600. if (ring->funcs->parse_cs) {
  601. struct amdgpu_bo_va_mapping *m;
  602. struct amdgpu_bo *aobj = NULL;
  603. uint64_t offset;
  604. uint8_t *kptr;
  605. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  606. &aobj);
  607. if (!aobj) {
  608. DRM_ERROR("IB va_start is invalid\n");
  609. return -EINVAL;
  610. }
  611. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  612. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  613. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  614. return -EINVAL;
  615. }
  616. /* the IB should be reserved at this point */
  617. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  618. if (r) {
  619. return r;
  620. }
  621. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  622. kptr += chunk_ib->va_start - offset;
  623. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  624. if (r) {
  625. DRM_ERROR("Failed to get ib !\n");
  626. return r;
  627. }
  628. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  629. amdgpu_bo_kunmap(aobj);
  630. } else {
  631. r = amdgpu_ib_get(adev, vm, 0, ib);
  632. if (r) {
  633. DRM_ERROR("Failed to get ib !\n");
  634. return r;
  635. }
  636. ib->gpu_addr = chunk_ib->va_start;
  637. }
  638. ib->length_dw = chunk_ib->ib_bytes / 4;
  639. ib->flags = chunk_ib->flags;
  640. j++;
  641. }
  642. /* UVD & VCE fw doesn't support user fences */
  643. if (parser->job->uf_bo && (
  644. parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  645. parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
  646. return -EINVAL;
  647. return 0;
  648. }
  649. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  650. struct amdgpu_cs_parser *p)
  651. {
  652. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  653. int i, j, r;
  654. for (i = 0; i < p->nchunks; ++i) {
  655. struct drm_amdgpu_cs_chunk_dep *deps;
  656. struct amdgpu_cs_chunk *chunk;
  657. unsigned num_deps;
  658. chunk = &p->chunks[i];
  659. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  660. continue;
  661. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  662. num_deps = chunk->length_dw * 4 /
  663. sizeof(struct drm_amdgpu_cs_chunk_dep);
  664. for (j = 0; j < num_deps; ++j) {
  665. struct amdgpu_ring *ring;
  666. struct amdgpu_ctx *ctx;
  667. struct fence *fence;
  668. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  669. deps[j].ip_instance,
  670. deps[j].ring, &ring);
  671. if (r)
  672. return r;
  673. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  674. if (ctx == NULL)
  675. return -EINVAL;
  676. fence = amdgpu_ctx_get_fence(ctx, ring,
  677. deps[j].handle);
  678. if (IS_ERR(fence)) {
  679. r = PTR_ERR(fence);
  680. amdgpu_ctx_put(ctx);
  681. return r;
  682. } else if (fence) {
  683. r = amdgpu_sync_fence(adev, &p->job->sync,
  684. fence);
  685. fence_put(fence);
  686. amdgpu_ctx_put(ctx);
  687. if (r)
  688. return r;
  689. }
  690. }
  691. }
  692. return 0;
  693. }
  694. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  695. union drm_amdgpu_cs *cs)
  696. {
  697. struct amdgpu_ring *ring = p->job->ring;
  698. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  699. struct fence *fence;
  700. struct amdgpu_job *job;
  701. int r;
  702. job = p->job;
  703. p->job = NULL;
  704. r = amd_sched_job_init(&job->base, &ring->sched,
  705. entity, 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. }