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_addr = uf_offset;
  195. kfree(chunk_array);
  196. return 0;
  197. free_all_kdata:
  198. i = p->nchunks - 1;
  199. free_partial_kdata:
  200. for (; i >= 0; i--)
  201. drm_free_large(p->chunks[i].kdata);
  202. kfree(p->chunks);
  203. put_ctx:
  204. amdgpu_ctx_put(p->ctx);
  205. free_chunk:
  206. kfree(chunk_array);
  207. return ret;
  208. }
  209. /* Returns how many bytes TTM can move per IB.
  210. */
  211. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  212. {
  213. u64 real_vram_size = adev->mc.real_vram_size;
  214. u64 vram_usage = atomic64_read(&adev->vram_usage);
  215. /* This function is based on the current VRAM usage.
  216. *
  217. * - If all of VRAM is free, allow relocating the number of bytes that
  218. * is equal to 1/4 of the size of VRAM for this IB.
  219. * - If more than one half of VRAM is occupied, only allow relocating
  220. * 1 MB of data for this IB.
  221. *
  222. * - From 0 to one half of used VRAM, the threshold decreases
  223. * linearly.
  224. * __________________
  225. * 1/4 of -|\ |
  226. * VRAM | \ |
  227. * | \ |
  228. * | \ |
  229. * | \ |
  230. * | \ |
  231. * | \ |
  232. * | \________|1 MB
  233. * |----------------|
  234. * VRAM 0 % 100 %
  235. * used used
  236. *
  237. * Note: It's a threshold, not a limit. The threshold must be crossed
  238. * for buffer relocations to stop, so any buffer of an arbitrary size
  239. * can be moved as long as the threshold isn't crossed before
  240. * the relocation takes place. We don't want to disable buffer
  241. * relocations completely.
  242. *
  243. * The idea is that buffers should be placed in VRAM at creation time
  244. * and TTM should only do a minimum number of relocations during
  245. * command submission. In practice, you need to submit at least
  246. * a dozen IBs to move all buffers to VRAM if they are in GTT.
  247. *
  248. * Also, things can get pretty crazy under memory pressure and actual
  249. * VRAM usage can change a lot, so playing safe even at 50% does
  250. * consistently increase performance.
  251. */
  252. u64 half_vram = real_vram_size >> 1;
  253. u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
  254. u64 bytes_moved_threshold = half_free_vram >> 1;
  255. return max(bytes_moved_threshold, 1024*1024ull);
  256. }
  257. int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  258. struct list_head *validated)
  259. {
  260. struct amdgpu_bo_list_entry *lobj;
  261. u64 initial_bytes_moved;
  262. int r;
  263. list_for_each_entry(lobj, validated, tv.head) {
  264. struct amdgpu_bo *bo = lobj->robj;
  265. bool binding_userptr = false;
  266. struct mm_struct *usermm;
  267. uint32_t domain;
  268. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  269. if (usermm && usermm != current->mm)
  270. return -EPERM;
  271. /* Check if we have user pages and nobody bound the BO already */
  272. if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
  273. size_t size = sizeof(struct page *);
  274. size *= bo->tbo.ttm->num_pages;
  275. memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
  276. binding_userptr = true;
  277. }
  278. if (bo->pin_count)
  279. continue;
  280. /* Avoid moving this one if we have moved too many buffers
  281. * for this IB already.
  282. *
  283. * Note that this allows moving at least one buffer of
  284. * any size, because it doesn't take the current "bo"
  285. * into account. We don't want to disallow buffer moves
  286. * completely.
  287. */
  288. if (p->bytes_moved <= p->bytes_moved_threshold)
  289. domain = bo->prefered_domains;
  290. else
  291. domain = bo->allowed_domains;
  292. retry:
  293. amdgpu_ttm_placement_from_domain(bo, domain);
  294. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  295. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  296. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  297. initial_bytes_moved;
  298. if (unlikely(r)) {
  299. if (r != -ERESTARTSYS && domain != bo->allowed_domains) {
  300. domain = bo->allowed_domains;
  301. goto retry;
  302. }
  303. return r;
  304. }
  305. if (binding_userptr) {
  306. drm_free_large(lobj->user_pages);
  307. lobj->user_pages = NULL;
  308. }
  309. }
  310. return 0;
  311. }
  312. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  313. union drm_amdgpu_cs *cs)
  314. {
  315. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  316. struct amdgpu_bo_list_entry *e;
  317. struct list_head duplicates;
  318. bool need_mmap_lock = false;
  319. unsigned i, tries = 10;
  320. int r;
  321. INIT_LIST_HEAD(&p->validated);
  322. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  323. if (p->bo_list) {
  324. need_mmap_lock = p->bo_list->first_userptr !=
  325. p->bo_list->num_entries;
  326. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  327. }
  328. INIT_LIST_HEAD(&duplicates);
  329. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  330. if (p->uf_entry.robj)
  331. list_add(&p->uf_entry.tv.head, &p->validated);
  332. if (need_mmap_lock)
  333. down_read(&current->mm->mmap_sem);
  334. while (1) {
  335. struct list_head need_pages;
  336. unsigned i;
  337. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
  338. &duplicates);
  339. if (unlikely(r != 0))
  340. goto error_free_pages;
  341. /* Without a BO list we don't have userptr BOs */
  342. if (!p->bo_list)
  343. break;
  344. INIT_LIST_HEAD(&need_pages);
  345. for (i = p->bo_list->first_userptr;
  346. i < p->bo_list->num_entries; ++i) {
  347. e = &p->bo_list->array[i];
  348. if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
  349. &e->user_invalidated) && e->user_pages) {
  350. /* We acquired a page array, but somebody
  351. * invalidated it. Free it an try again
  352. */
  353. release_pages(e->user_pages,
  354. e->robj->tbo.ttm->num_pages,
  355. false);
  356. drm_free_large(e->user_pages);
  357. e->user_pages = NULL;
  358. }
  359. if (e->robj->tbo.ttm->state != tt_bound &&
  360. !e->user_pages) {
  361. list_del(&e->tv.head);
  362. list_add(&e->tv.head, &need_pages);
  363. amdgpu_bo_unreserve(e->robj);
  364. }
  365. }
  366. if (list_empty(&need_pages))
  367. break;
  368. /* Unreserve everything again. */
  369. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  370. /* We tried to often, just abort */
  371. if (!--tries) {
  372. r = -EDEADLK;
  373. goto error_free_pages;
  374. }
  375. /* Fill the page arrays for all useptrs. */
  376. list_for_each_entry(e, &need_pages, tv.head) {
  377. struct ttm_tt *ttm = e->robj->tbo.ttm;
  378. e->user_pages = drm_calloc_large(ttm->num_pages,
  379. sizeof(struct page*));
  380. if (!e->user_pages) {
  381. r = -ENOMEM;
  382. goto error_free_pages;
  383. }
  384. r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
  385. if (r) {
  386. drm_free_large(e->user_pages);
  387. e->user_pages = NULL;
  388. goto error_free_pages;
  389. }
  390. }
  391. /* And try again. */
  392. list_splice(&need_pages, &p->validated);
  393. }
  394. amdgpu_vm_get_pt_bos(p->adev, &fpriv->vm, &duplicates);
  395. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  396. p->bytes_moved = 0;
  397. r = amdgpu_cs_list_validate(p, &duplicates);
  398. if (r)
  399. goto error_validate;
  400. r = amdgpu_cs_list_validate(p, &p->validated);
  401. if (r)
  402. goto error_validate;
  403. fpriv->vm.last_eviction_counter =
  404. atomic64_read(&p->adev->num_evictions);
  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. if (p->uf_entry.robj)
  429. p->job->uf_addr += amdgpu_bo_gpu_offset(p->uf_entry.robj);
  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. p->job->vm = NULL;
  555. for (i = 0; i < p->job->num_ibs; i++) {
  556. r = amdgpu_ring_parse_cs(ring, p, i);
  557. if (r)
  558. return r;
  559. }
  560. } else {
  561. p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  562. r = amdgpu_bo_vm_update_pte(p, vm);
  563. if (r)
  564. return r;
  565. }
  566. return amdgpu_cs_sync_rings(p);
  567. }
  568. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  569. {
  570. if (r == -EDEADLK) {
  571. r = amdgpu_gpu_reset(adev);
  572. if (!r)
  573. r = -EAGAIN;
  574. }
  575. return r;
  576. }
  577. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  578. struct amdgpu_cs_parser *parser)
  579. {
  580. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  581. struct amdgpu_vm *vm = &fpriv->vm;
  582. int i, j;
  583. int r;
  584. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  585. struct amdgpu_cs_chunk *chunk;
  586. struct amdgpu_ib *ib;
  587. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  588. struct amdgpu_ring *ring;
  589. chunk = &parser->chunks[i];
  590. ib = &parser->job->ibs[j];
  591. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  592. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  593. continue;
  594. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  595. chunk_ib->ip_instance, chunk_ib->ring,
  596. &ring);
  597. if (r)
  598. return r;
  599. if (parser->job->ring && parser->job->ring != ring)
  600. return -EINVAL;
  601. parser->job->ring = ring;
  602. if (ring->funcs->parse_cs) {
  603. struct amdgpu_bo_va_mapping *m;
  604. struct amdgpu_bo *aobj = NULL;
  605. uint64_t offset;
  606. uint8_t *kptr;
  607. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  608. &aobj);
  609. if (!aobj) {
  610. DRM_ERROR("IB va_start is invalid\n");
  611. return -EINVAL;
  612. }
  613. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  614. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  615. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  616. return -EINVAL;
  617. }
  618. /* the IB should be reserved at this point */
  619. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  620. if (r) {
  621. return r;
  622. }
  623. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  624. kptr += chunk_ib->va_start - offset;
  625. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  626. if (r) {
  627. DRM_ERROR("Failed to get ib !\n");
  628. return r;
  629. }
  630. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  631. amdgpu_bo_kunmap(aobj);
  632. } else {
  633. r = amdgpu_ib_get(adev, vm, 0, ib);
  634. if (r) {
  635. DRM_ERROR("Failed to get ib !\n");
  636. return r;
  637. }
  638. ib->gpu_addr = chunk_ib->va_start;
  639. }
  640. ib->length_dw = chunk_ib->ib_bytes / 4;
  641. ib->flags = chunk_ib->flags;
  642. j++;
  643. }
  644. /* UVD & VCE fw doesn't support user fences */
  645. if (parser->job->uf_addr && (
  646. parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  647. parser->job->ring->type == AMDGPU_RING_TYPE_VCE))
  648. return -EINVAL;
  649. return 0;
  650. }
  651. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  652. struct amdgpu_cs_parser *p)
  653. {
  654. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  655. int i, j, r;
  656. for (i = 0; i < p->nchunks; ++i) {
  657. struct drm_amdgpu_cs_chunk_dep *deps;
  658. struct amdgpu_cs_chunk *chunk;
  659. unsigned num_deps;
  660. chunk = &p->chunks[i];
  661. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  662. continue;
  663. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  664. num_deps = chunk->length_dw * 4 /
  665. sizeof(struct drm_amdgpu_cs_chunk_dep);
  666. for (j = 0; j < num_deps; ++j) {
  667. struct amdgpu_ring *ring;
  668. struct amdgpu_ctx *ctx;
  669. struct fence *fence;
  670. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  671. deps[j].ip_instance,
  672. deps[j].ring, &ring);
  673. if (r)
  674. return r;
  675. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  676. if (ctx == NULL)
  677. return -EINVAL;
  678. fence = amdgpu_ctx_get_fence(ctx, ring,
  679. deps[j].handle);
  680. if (IS_ERR(fence)) {
  681. r = PTR_ERR(fence);
  682. amdgpu_ctx_put(ctx);
  683. return r;
  684. } else if (fence) {
  685. r = amdgpu_sync_fence(adev, &p->job->sync,
  686. fence);
  687. fence_put(fence);
  688. amdgpu_ctx_put(ctx);
  689. if (r)
  690. return r;
  691. }
  692. }
  693. }
  694. return 0;
  695. }
  696. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  697. union drm_amdgpu_cs *cs)
  698. {
  699. struct amdgpu_ring *ring = p->job->ring;
  700. struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
  701. struct amdgpu_job *job;
  702. int r;
  703. job = p->job;
  704. p->job = NULL;
  705. r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
  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(&job->base.s_fence->finished);
  713. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
  714. job->uf_sequence = cs->out.handle;
  715. amdgpu_job_free_resources(job);
  716. trace_amdgpu_cs_ioctl(job);
  717. amd_sched_entity_push_job(&job->base);
  718. return 0;
  719. }
  720. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  721. {
  722. struct amdgpu_device *adev = dev->dev_private;
  723. union drm_amdgpu_cs *cs = data;
  724. struct amdgpu_cs_parser parser = {};
  725. bool reserved_buffers = false;
  726. int i, r;
  727. if (!adev->accel_working)
  728. return -EBUSY;
  729. parser.adev = adev;
  730. parser.filp = filp;
  731. r = amdgpu_cs_parser_init(&parser, data);
  732. if (r) {
  733. DRM_ERROR("Failed to initialize parser !\n");
  734. amdgpu_cs_parser_fini(&parser, r, false);
  735. r = amdgpu_cs_handle_lockup(adev, r);
  736. return r;
  737. }
  738. r = amdgpu_cs_parser_bos(&parser, data);
  739. if (r == -ENOMEM)
  740. DRM_ERROR("Not enough memory for command submission!\n");
  741. else if (r && r != -ERESTARTSYS)
  742. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  743. else if (!r) {
  744. reserved_buffers = true;
  745. r = amdgpu_cs_ib_fill(adev, &parser);
  746. }
  747. if (!r) {
  748. r = amdgpu_cs_dependencies(adev, &parser);
  749. if (r)
  750. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  751. }
  752. if (r)
  753. goto out;
  754. for (i = 0; i < parser.job->num_ibs; i++)
  755. trace_amdgpu_cs(&parser, i);
  756. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  757. if (r)
  758. goto out;
  759. r = amdgpu_cs_submit(&parser, cs);
  760. out:
  761. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  762. r = amdgpu_cs_handle_lockup(adev, r);
  763. return r;
  764. }
  765. /**
  766. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  767. *
  768. * @dev: drm device
  769. * @data: data from userspace
  770. * @filp: file private
  771. *
  772. * Wait for the command submission identified by handle to finish.
  773. */
  774. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  775. struct drm_file *filp)
  776. {
  777. union drm_amdgpu_wait_cs *wait = data;
  778. struct amdgpu_device *adev = dev->dev_private;
  779. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  780. struct amdgpu_ring *ring = NULL;
  781. struct amdgpu_ctx *ctx;
  782. struct fence *fence;
  783. long r;
  784. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  785. wait->in.ring, &ring);
  786. if (r)
  787. return r;
  788. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  789. if (ctx == NULL)
  790. return -EINVAL;
  791. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  792. if (IS_ERR(fence))
  793. r = PTR_ERR(fence);
  794. else if (fence) {
  795. r = fence_wait_timeout(fence, true, timeout);
  796. fence_put(fence);
  797. } else
  798. r = 1;
  799. amdgpu_ctx_put(ctx);
  800. if (r < 0)
  801. return r;
  802. memset(wait, 0, sizeof(*wait));
  803. wait->out.status = (r == 0);
  804. return 0;
  805. }
  806. /**
  807. * amdgpu_cs_find_bo_va - find bo_va for VM address
  808. *
  809. * @parser: command submission parser context
  810. * @addr: VM address
  811. * @bo: resulting BO of the mapping found
  812. *
  813. * Search the buffer objects in the command submission context for a certain
  814. * virtual memory address. Returns allocation structure when found, NULL
  815. * otherwise.
  816. */
  817. struct amdgpu_bo_va_mapping *
  818. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  819. uint64_t addr, struct amdgpu_bo **bo)
  820. {
  821. struct amdgpu_bo_va_mapping *mapping;
  822. unsigned i;
  823. if (!parser->bo_list)
  824. return NULL;
  825. addr /= AMDGPU_GPU_PAGE_SIZE;
  826. for (i = 0; i < parser->bo_list->num_entries; i++) {
  827. struct amdgpu_bo_list_entry *lobj;
  828. lobj = &parser->bo_list->array[i];
  829. if (!lobj->bo_va)
  830. continue;
  831. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  832. if (mapping->it.start > addr ||
  833. addr > mapping->it.last)
  834. continue;
  835. *bo = lobj->bo_va->bo;
  836. return mapping;
  837. }
  838. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  839. if (mapping->it.start > addr ||
  840. addr > mapping->it.last)
  841. continue;
  842. *bo = lobj->bo_va->bo;
  843. return mapping;
  844. }
  845. }
  846. return NULL;
  847. }