amdgpu_cs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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/list_sort.h>
  28. #include <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  33. u32 ip_instance, u32 ring,
  34. struct amdgpu_ring **out_ring)
  35. {
  36. /* Right now all IPs have only one instance - multiple rings. */
  37. if (ip_instance != 0) {
  38. DRM_ERROR("invalid ip instance: %d\n", ip_instance);
  39. return -EINVAL;
  40. }
  41. switch (ip_type) {
  42. default:
  43. DRM_ERROR("unknown ip type: %d\n", ip_type);
  44. return -EINVAL;
  45. case AMDGPU_HW_IP_GFX:
  46. if (ring < adev->gfx.num_gfx_rings) {
  47. *out_ring = &adev->gfx.gfx_ring[ring];
  48. } else {
  49. DRM_ERROR("only %d gfx rings are supported now\n",
  50. adev->gfx.num_gfx_rings);
  51. return -EINVAL;
  52. }
  53. break;
  54. case AMDGPU_HW_IP_COMPUTE:
  55. if (ring < adev->gfx.num_compute_rings) {
  56. *out_ring = &adev->gfx.compute_ring[ring];
  57. } else {
  58. DRM_ERROR("only %d compute rings are supported now\n",
  59. adev->gfx.num_compute_rings);
  60. return -EINVAL;
  61. }
  62. break;
  63. case AMDGPU_HW_IP_DMA:
  64. if (ring < adev->sdma.num_instances) {
  65. *out_ring = &adev->sdma.instance[ring].ring;
  66. } else {
  67. DRM_ERROR("only %d SDMA rings are supported\n",
  68. adev->sdma.num_instances);
  69. return -EINVAL;
  70. }
  71. break;
  72. case AMDGPU_HW_IP_UVD:
  73. *out_ring = &adev->uvd.ring;
  74. break;
  75. case AMDGPU_HW_IP_VCE:
  76. if (ring < 2){
  77. *out_ring = &adev->vce.ring[ring];
  78. } else {
  79. DRM_ERROR("only two VCE rings are supported\n");
  80. return -EINVAL;
  81. }
  82. break;
  83. }
  84. return 0;
  85. }
  86. static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
  87. struct amdgpu_user_fence *uf,
  88. struct drm_amdgpu_cs_chunk_fence *fence_data)
  89. {
  90. struct drm_gem_object *gobj;
  91. uint32_t handle;
  92. handle = fence_data->handle;
  93. gobj = drm_gem_object_lookup(p->adev->ddev, p->filp,
  94. fence_data->handle);
  95. if (gobj == NULL)
  96. return -EINVAL;
  97. uf->bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  98. uf->offset = fence_data->offset;
  99. if (amdgpu_ttm_tt_get_usermm(uf->bo->tbo.ttm)) {
  100. drm_gem_object_unreference_unlocked(gobj);
  101. return -EINVAL;
  102. }
  103. p->uf_entry.robj = amdgpu_bo_ref(uf->bo);
  104. p->uf_entry.priority = 0;
  105. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  106. p->uf_entry.tv.shared = true;
  107. drm_gem_object_unreference_unlocked(gobj);
  108. return 0;
  109. }
  110. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  111. {
  112. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  113. union drm_amdgpu_cs *cs = data;
  114. uint64_t *chunk_array_user;
  115. uint64_t *chunk_array;
  116. struct amdgpu_user_fence uf = {};
  117. unsigned size, num_ibs = 0;
  118. int i;
  119. int ret;
  120. if (cs->in.num_chunks == 0)
  121. return 0;
  122. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  123. if (!chunk_array)
  124. return -ENOMEM;
  125. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  126. if (!p->ctx) {
  127. ret = -EINVAL;
  128. goto free_chunk;
  129. }
  130. /* get chunks */
  131. chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
  132. if (copy_from_user(chunk_array, chunk_array_user,
  133. sizeof(uint64_t)*cs->in.num_chunks)) {
  134. ret = -EFAULT;
  135. goto put_ctx;
  136. }
  137. p->nchunks = cs->in.num_chunks;
  138. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  139. GFP_KERNEL);
  140. if (!p->chunks) {
  141. ret = -ENOMEM;
  142. goto put_ctx;
  143. }
  144. for (i = 0; i < p->nchunks; i++) {
  145. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  146. struct drm_amdgpu_cs_chunk user_chunk;
  147. uint32_t __user *cdata;
  148. chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
  149. if (copy_from_user(&user_chunk, chunk_ptr,
  150. sizeof(struct drm_amdgpu_cs_chunk))) {
  151. ret = -EFAULT;
  152. i--;
  153. goto free_partial_kdata;
  154. }
  155. p->chunks[i].chunk_id = user_chunk.chunk_id;
  156. p->chunks[i].length_dw = user_chunk.length_dw;
  157. size = p->chunks[i].length_dw;
  158. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  159. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  160. if (p->chunks[i].kdata == NULL) {
  161. ret = -ENOMEM;
  162. i--;
  163. goto free_partial_kdata;
  164. }
  165. size *= sizeof(uint32_t);
  166. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  167. ret = -EFAULT;
  168. goto free_partial_kdata;
  169. }
  170. switch (p->chunks[i].chunk_id) {
  171. case AMDGPU_CHUNK_ID_IB:
  172. ++num_ibs;
  173. break;
  174. case AMDGPU_CHUNK_ID_FENCE:
  175. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  176. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  177. ret = -EINVAL;
  178. goto free_partial_kdata;
  179. }
  180. ret = amdgpu_cs_user_fence_chunk(p, &uf, (void *)p->chunks[i].kdata);
  181. if (ret)
  182. goto free_partial_kdata;
  183. break;
  184. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  185. break;
  186. default:
  187. ret = -EINVAL;
  188. goto free_partial_kdata;
  189. }
  190. }
  191. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job);
  192. if (ret)
  193. goto free_all_kdata;
  194. p->job->uf = uf;
  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. struct mm_struct *usermm;
  266. uint32_t domain;
  267. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  268. if (usermm && usermm != current->mm)
  269. return -EPERM;
  270. if (bo->pin_count)
  271. continue;
  272. /* Avoid moving this one if we have moved too many buffers
  273. * for this IB already.
  274. *
  275. * Note that this allows moving at least one buffer of
  276. * any size, because it doesn't take the current "bo"
  277. * into account. We don't want to disallow buffer moves
  278. * completely.
  279. */
  280. if (p->bytes_moved <= p->bytes_moved_threshold)
  281. domain = bo->prefered_domains;
  282. else
  283. domain = bo->allowed_domains;
  284. retry:
  285. amdgpu_ttm_placement_from_domain(bo, domain);
  286. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  287. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  288. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  289. initial_bytes_moved;
  290. if (unlikely(r)) {
  291. if (r != -ERESTARTSYS && domain != bo->allowed_domains) {
  292. domain = bo->allowed_domains;
  293. goto retry;
  294. }
  295. return r;
  296. }
  297. }
  298. return 0;
  299. }
  300. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  301. union drm_amdgpu_cs *cs)
  302. {
  303. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  304. struct list_head duplicates;
  305. bool need_mmap_lock = false;
  306. int r;
  307. INIT_LIST_HEAD(&p->validated);
  308. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  309. if (p->bo_list) {
  310. need_mmap_lock = p->bo_list->has_userptr;
  311. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  312. }
  313. INIT_LIST_HEAD(&duplicates);
  314. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  315. if (p->job->uf.bo)
  316. list_add(&p->uf_entry.tv.head, &p->validated);
  317. if (need_mmap_lock)
  318. down_read(&current->mm->mmap_sem);
  319. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
  320. if (unlikely(r != 0))
  321. goto error_reserve;
  322. amdgpu_vm_get_pt_bos(&fpriv->vm, &duplicates);
  323. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  324. p->bytes_moved = 0;
  325. r = amdgpu_cs_list_validate(p, &duplicates);
  326. if (r)
  327. goto error_validate;
  328. r = amdgpu_cs_list_validate(p, &p->validated);
  329. if (r)
  330. goto error_validate;
  331. if (p->bo_list) {
  332. struct amdgpu_vm *vm = &fpriv->vm;
  333. unsigned i;
  334. for (i = 0; i < p->bo_list->num_entries; i++) {
  335. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  336. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  337. }
  338. }
  339. error_validate:
  340. if (r) {
  341. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  342. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  343. }
  344. error_reserve:
  345. if (need_mmap_lock)
  346. up_read(&current->mm->mmap_sem);
  347. return r;
  348. }
  349. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  350. {
  351. struct amdgpu_bo_list_entry *e;
  352. int r;
  353. list_for_each_entry(e, &p->validated, tv.head) {
  354. struct reservation_object *resv = e->robj->tbo.resv;
  355. r = amdgpu_sync_resv(p->adev, &p->job->ibs[0].sync, resv, p->filp);
  356. if (r)
  357. return r;
  358. }
  359. return 0;
  360. }
  361. static int cmp_size_smaller_first(void *priv, struct list_head *a,
  362. struct list_head *b)
  363. {
  364. struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
  365. struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
  366. /* Sort A before B if A is smaller. */
  367. return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
  368. }
  369. /**
  370. * cs_parser_fini() - clean parser states
  371. * @parser: parser structure holding parsing context.
  372. * @error: error number
  373. *
  374. * If error is set than unvalidate buffer, otherwise just free memory
  375. * used by parsing context.
  376. **/
  377. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  378. {
  379. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  380. unsigned i;
  381. if (!error) {
  382. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  383. /* Sort the buffer list from the smallest to largest buffer,
  384. * which affects the order of buffers in the LRU list.
  385. * This assures that the smallest buffers are added first
  386. * to the LRU list, so they are likely to be later evicted
  387. * first, instead of large buffers whose eviction is more
  388. * expensive.
  389. *
  390. * This slightly lowers the number of bytes moved by TTM
  391. * per frame under memory pressure.
  392. */
  393. list_sort(NULL, &parser->validated, cmp_size_smaller_first);
  394. ttm_eu_fence_buffer_objects(&parser->ticket,
  395. &parser->validated,
  396. parser->fence);
  397. } else if (backoff) {
  398. ttm_eu_backoff_reservation(&parser->ticket,
  399. &parser->validated);
  400. }
  401. fence_put(parser->fence);
  402. if (parser->ctx)
  403. amdgpu_ctx_put(parser->ctx);
  404. if (parser->bo_list)
  405. amdgpu_bo_list_put(parser->bo_list);
  406. for (i = 0; i < parser->nchunks; i++)
  407. drm_free_large(parser->chunks[i].kdata);
  408. kfree(parser->chunks);
  409. if (parser->job)
  410. amdgpu_job_free(parser->job);
  411. amdgpu_bo_unref(&parser->uf_entry.robj);
  412. }
  413. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  414. struct amdgpu_vm *vm)
  415. {
  416. struct amdgpu_device *adev = p->adev;
  417. struct amdgpu_bo_va *bo_va;
  418. struct amdgpu_bo *bo;
  419. int i, r;
  420. r = amdgpu_vm_update_page_directory(adev, vm);
  421. if (r)
  422. return r;
  423. r = amdgpu_sync_fence(adev, &p->job->ibs[0].sync, vm->page_directory_fence);
  424. if (r)
  425. return r;
  426. r = amdgpu_vm_clear_freed(adev, vm);
  427. if (r)
  428. return r;
  429. if (p->bo_list) {
  430. for (i = 0; i < p->bo_list->num_entries; i++) {
  431. struct fence *f;
  432. /* ignore duplicates */
  433. bo = p->bo_list->array[i].robj;
  434. if (!bo)
  435. continue;
  436. bo_va = p->bo_list->array[i].bo_va;
  437. if (bo_va == NULL)
  438. continue;
  439. r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
  440. if (r)
  441. return r;
  442. f = bo_va->last_pt_update;
  443. r = amdgpu_sync_fence(adev, &p->job->ibs[0].sync, f);
  444. if (r)
  445. return r;
  446. }
  447. }
  448. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->ibs[0].sync);
  449. if (amdgpu_vm_debug && p->bo_list) {
  450. /* Invalidate all BOs to test for userspace bugs */
  451. for (i = 0; i < p->bo_list->num_entries; i++) {
  452. /* ignore duplicates */
  453. bo = p->bo_list->array[i].robj;
  454. if (!bo)
  455. continue;
  456. amdgpu_vm_bo_invalidate(adev, bo);
  457. }
  458. }
  459. return r;
  460. }
  461. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  462. struct amdgpu_cs_parser *p)
  463. {
  464. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  465. struct amdgpu_vm *vm = &fpriv->vm;
  466. struct amdgpu_ring *ring = p->job->ring;
  467. int i, r;
  468. /* Only for UVD/VCE VM emulation */
  469. if (ring->funcs->parse_cs) {
  470. for (i = 0; i < p->job->num_ibs; i++) {
  471. r = amdgpu_ring_parse_cs(ring, p, i);
  472. if (r)
  473. return r;
  474. }
  475. }
  476. r = amdgpu_bo_vm_update_pte(p, vm);
  477. if (!r)
  478. amdgpu_cs_sync_rings(p);
  479. return r;
  480. }
  481. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  482. {
  483. if (r == -EDEADLK) {
  484. r = amdgpu_gpu_reset(adev);
  485. if (!r)
  486. r = -EAGAIN;
  487. }
  488. return r;
  489. }
  490. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  491. struct amdgpu_cs_parser *parser)
  492. {
  493. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  494. struct amdgpu_vm *vm = &fpriv->vm;
  495. int i, j;
  496. int r;
  497. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  498. struct amdgpu_cs_chunk *chunk;
  499. struct amdgpu_ib *ib;
  500. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  501. struct amdgpu_ring *ring;
  502. chunk = &parser->chunks[i];
  503. ib = &parser->job->ibs[j];
  504. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  505. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  506. continue;
  507. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  508. chunk_ib->ip_instance, chunk_ib->ring,
  509. &ring);
  510. if (r)
  511. return r;
  512. if (parser->job->ring && parser->job->ring != ring)
  513. return -EINVAL;
  514. parser->job->ring = ring;
  515. if (ring->funcs->parse_cs) {
  516. struct amdgpu_bo_va_mapping *m;
  517. struct amdgpu_bo *aobj = NULL;
  518. uint64_t offset;
  519. uint8_t *kptr;
  520. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  521. &aobj);
  522. if (!aobj) {
  523. DRM_ERROR("IB va_start is invalid\n");
  524. return -EINVAL;
  525. }
  526. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  527. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  528. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  529. return -EINVAL;
  530. }
  531. /* the IB should be reserved at this point */
  532. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  533. if (r) {
  534. return r;
  535. }
  536. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  537. kptr += chunk_ib->va_start - offset;
  538. r = amdgpu_ib_get(adev, NULL, chunk_ib->ib_bytes, ib);
  539. if (r) {
  540. DRM_ERROR("Failed to get ib !\n");
  541. return r;
  542. }
  543. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  544. amdgpu_bo_kunmap(aobj);
  545. } else {
  546. r = amdgpu_ib_get(adev, vm, 0, ib);
  547. if (r) {
  548. DRM_ERROR("Failed to get ib !\n");
  549. return r;
  550. }
  551. ib->gpu_addr = chunk_ib->va_start;
  552. }
  553. ib->length_dw = chunk_ib->ib_bytes / 4;
  554. ib->flags = chunk_ib->flags;
  555. ib->ctx = parser->ctx;
  556. j++;
  557. }
  558. /* add GDS resources to first IB */
  559. if (parser->bo_list) {
  560. struct amdgpu_bo *gds = parser->bo_list->gds_obj;
  561. struct amdgpu_bo *gws = parser->bo_list->gws_obj;
  562. struct amdgpu_bo *oa = parser->bo_list->oa_obj;
  563. struct amdgpu_ib *ib = &parser->job->ibs[0];
  564. if (gds) {
  565. ib->gds_base = amdgpu_bo_gpu_offset(gds);
  566. ib->gds_size = amdgpu_bo_size(gds);
  567. }
  568. if (gws) {
  569. ib->gws_base = amdgpu_bo_gpu_offset(gws);
  570. ib->gws_size = amdgpu_bo_size(gws);
  571. }
  572. if (oa) {
  573. ib->oa_base = amdgpu_bo_gpu_offset(oa);
  574. ib->oa_size = amdgpu_bo_size(oa);
  575. }
  576. }
  577. /* wrap the last IB with user fence */
  578. if (parser->job->uf.bo) {
  579. struct amdgpu_ib *ib = &parser->job->ibs[parser->job->num_ibs - 1];
  580. /* UVD & VCE fw doesn't support user fences */
  581. if (parser->job->ring->type == AMDGPU_RING_TYPE_UVD ||
  582. parser->job->ring->type == AMDGPU_RING_TYPE_VCE)
  583. return -EINVAL;
  584. ib->user = &parser->job->uf;
  585. }
  586. return 0;
  587. }
  588. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  589. struct amdgpu_cs_parser *p)
  590. {
  591. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  592. struct amdgpu_ib *ib;
  593. int i, j, r;
  594. /* Add dependencies to first IB */
  595. ib = &p->job->ibs[0];
  596. for (i = 0; i < p->nchunks; ++i) {
  597. struct drm_amdgpu_cs_chunk_dep *deps;
  598. struct amdgpu_cs_chunk *chunk;
  599. unsigned num_deps;
  600. chunk = &p->chunks[i];
  601. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  602. continue;
  603. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  604. num_deps = chunk->length_dw * 4 /
  605. sizeof(struct drm_amdgpu_cs_chunk_dep);
  606. for (j = 0; j < num_deps; ++j) {
  607. struct amdgpu_ring *ring;
  608. struct amdgpu_ctx *ctx;
  609. struct fence *fence;
  610. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  611. deps[j].ip_instance,
  612. deps[j].ring, &ring);
  613. if (r)
  614. return r;
  615. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  616. if (ctx == NULL)
  617. return -EINVAL;
  618. fence = amdgpu_ctx_get_fence(ctx, ring,
  619. deps[j].handle);
  620. if (IS_ERR(fence)) {
  621. r = PTR_ERR(fence);
  622. amdgpu_ctx_put(ctx);
  623. return r;
  624. } else if (fence) {
  625. r = amdgpu_sync_fence(adev, &ib->sync, fence);
  626. fence_put(fence);
  627. amdgpu_ctx_put(ctx);
  628. if (r)
  629. return r;
  630. }
  631. }
  632. }
  633. return 0;
  634. }
  635. static int amdgpu_cs_free_job(struct amdgpu_job *job)
  636. {
  637. amdgpu_job_free(job);
  638. return 0;
  639. }
  640. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  641. union drm_amdgpu_cs *cs)
  642. {
  643. struct amdgpu_ring *ring = p->job->ring;
  644. struct amd_sched_fence *fence;
  645. struct amdgpu_job *job;
  646. job = p->job;
  647. p->job = NULL;
  648. job->base.sched = &ring->sched;
  649. job->base.s_entity = &p->ctx->rings[ring->idx].entity;
  650. job->owner = p->filp;
  651. job->free_job = amdgpu_cs_free_job;
  652. fence = amd_sched_fence_create(job->base.s_entity, p->filp);
  653. if (!fence) {
  654. amdgpu_cs_free_job(job);
  655. kfree(job);
  656. return -ENOMEM;
  657. }
  658. job->base.s_fence = fence;
  659. p->fence = fence_get(&fence->base);
  660. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring,
  661. &fence->base);
  662. job->ibs[job->num_ibs - 1].sequence = cs->out.handle;
  663. trace_amdgpu_cs_ioctl(job);
  664. amd_sched_entity_push_job(&job->base);
  665. return 0;
  666. }
  667. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  668. {
  669. struct amdgpu_device *adev = dev->dev_private;
  670. union drm_amdgpu_cs *cs = data;
  671. struct amdgpu_cs_parser parser = {};
  672. bool reserved_buffers = false;
  673. int i, r;
  674. if (!adev->accel_working)
  675. return -EBUSY;
  676. parser.adev = adev;
  677. parser.filp = filp;
  678. r = amdgpu_cs_parser_init(&parser, data);
  679. if (r) {
  680. DRM_ERROR("Failed to initialize parser !\n");
  681. amdgpu_cs_parser_fini(&parser, r, false);
  682. r = amdgpu_cs_handle_lockup(adev, r);
  683. return r;
  684. }
  685. r = amdgpu_cs_parser_bos(&parser, data);
  686. if (r == -ENOMEM)
  687. DRM_ERROR("Not enough memory for command submission!\n");
  688. else if (r && r != -ERESTARTSYS)
  689. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  690. else if (!r) {
  691. reserved_buffers = true;
  692. r = amdgpu_cs_ib_fill(adev, &parser);
  693. }
  694. if (!r) {
  695. r = amdgpu_cs_dependencies(adev, &parser);
  696. if (r)
  697. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  698. }
  699. if (r)
  700. goto out;
  701. for (i = 0; i < parser.job->num_ibs; i++)
  702. trace_amdgpu_cs(&parser, i);
  703. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  704. if (r)
  705. goto out;
  706. r = amdgpu_cs_submit(&parser, cs);
  707. out:
  708. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  709. r = amdgpu_cs_handle_lockup(adev, r);
  710. return r;
  711. }
  712. /**
  713. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  714. *
  715. * @dev: drm device
  716. * @data: data from userspace
  717. * @filp: file private
  718. *
  719. * Wait for the command submission identified by handle to finish.
  720. */
  721. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  722. struct drm_file *filp)
  723. {
  724. union drm_amdgpu_wait_cs *wait = data;
  725. struct amdgpu_device *adev = dev->dev_private;
  726. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  727. struct amdgpu_ring *ring = NULL;
  728. struct amdgpu_ctx *ctx;
  729. struct fence *fence;
  730. long r;
  731. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  732. wait->in.ring, &ring);
  733. if (r)
  734. return r;
  735. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  736. if (ctx == NULL)
  737. return -EINVAL;
  738. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  739. if (IS_ERR(fence))
  740. r = PTR_ERR(fence);
  741. else if (fence) {
  742. r = fence_wait_timeout(fence, true, timeout);
  743. fence_put(fence);
  744. } else
  745. r = 1;
  746. amdgpu_ctx_put(ctx);
  747. if (r < 0)
  748. return r;
  749. memset(wait, 0, sizeof(*wait));
  750. wait->out.status = (r == 0);
  751. return 0;
  752. }
  753. /**
  754. * amdgpu_cs_find_bo_va - find bo_va for VM address
  755. *
  756. * @parser: command submission parser context
  757. * @addr: VM address
  758. * @bo: resulting BO of the mapping found
  759. *
  760. * Search the buffer objects in the command submission context for a certain
  761. * virtual memory address. Returns allocation structure when found, NULL
  762. * otherwise.
  763. */
  764. struct amdgpu_bo_va_mapping *
  765. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  766. uint64_t addr, struct amdgpu_bo **bo)
  767. {
  768. struct amdgpu_bo_va_mapping *mapping;
  769. unsigned i;
  770. if (!parser->bo_list)
  771. return NULL;
  772. addr /= AMDGPU_GPU_PAGE_SIZE;
  773. for (i = 0; i < parser->bo_list->num_entries; i++) {
  774. struct amdgpu_bo_list_entry *lobj;
  775. lobj = &parser->bo_list->array[i];
  776. if (!lobj->bo_va)
  777. continue;
  778. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  779. if (mapping->it.start > addr ||
  780. addr > mapping->it.last)
  781. continue;
  782. *bo = lobj->bo_va->bo;
  783. return mapping;
  784. }
  785. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  786. if (mapping->it.start > addr ||
  787. addr > mapping->it.last)
  788. continue;
  789. *bo = lobj->bo_va->bo;
  790. return mapping;
  791. }
  792. }
  793. return NULL;
  794. }