amdgpu_cs.c 24 KB

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