amdgpu_cs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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_get_usermm(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. struct mm_struct *usermm;
  265. uint32_t domain;
  266. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  267. if (usermm && usermm != current->mm)
  268. return -EPERM;
  269. if (bo->pin_count)
  270. continue;
  271. /* Avoid moving this one if we have moved too many buffers
  272. * for this IB already.
  273. *
  274. * Note that this allows moving at least one buffer of
  275. * any size, because it doesn't take the current "bo"
  276. * into account. We don't want to disallow buffer moves
  277. * completely.
  278. */
  279. if (p->bytes_moved <= p->bytes_moved_threshold)
  280. domain = bo->prefered_domains;
  281. else
  282. domain = bo->allowed_domains;
  283. retry:
  284. amdgpu_ttm_placement_from_domain(bo, domain);
  285. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  286. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  287. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  288. initial_bytes_moved;
  289. if (unlikely(r)) {
  290. if (r != -ERESTARTSYS && domain != bo->allowed_domains) {
  291. domain = bo->allowed_domains;
  292. goto retry;
  293. }
  294. return r;
  295. }
  296. }
  297. return 0;
  298. }
  299. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  300. union drm_amdgpu_cs *cs)
  301. {
  302. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  303. struct list_head duplicates;
  304. bool need_mmap_lock = false;
  305. int r;
  306. INIT_LIST_HEAD(&p->validated);
  307. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  308. if (p->bo_list) {
  309. need_mmap_lock = p->bo_list->has_userptr;
  310. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  311. }
  312. INIT_LIST_HEAD(&duplicates);
  313. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  314. if (p->uf.bo)
  315. list_add(&p->uf_entry.tv.head, &p->validated);
  316. if (need_mmap_lock)
  317. down_read(&current->mm->mmap_sem);
  318. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
  319. if (unlikely(r != 0))
  320. goto error_reserve;
  321. amdgpu_vm_get_pt_bos(&fpriv->vm, &duplicates);
  322. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  323. p->bytes_moved = 0;
  324. r = amdgpu_cs_list_validate(p, &duplicates);
  325. if (r)
  326. goto error_validate;
  327. r = amdgpu_cs_list_validate(p, &p->validated);
  328. if (r)
  329. goto error_validate;
  330. if (p->bo_list) {
  331. struct amdgpu_vm *vm = &fpriv->vm;
  332. unsigned i;
  333. for (i = 0; i < p->bo_list->num_entries; i++) {
  334. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  335. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  336. }
  337. }
  338. error_validate:
  339. if (r) {
  340. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  341. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  342. }
  343. error_reserve:
  344. if (need_mmap_lock)
  345. up_read(&current->mm->mmap_sem);
  346. return r;
  347. }
  348. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  349. {
  350. struct amdgpu_bo_list_entry *e;
  351. int r;
  352. list_for_each_entry(e, &p->validated, tv.head) {
  353. struct reservation_object *resv = e->robj->tbo.resv;
  354. r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
  355. if (r)
  356. return r;
  357. }
  358. return 0;
  359. }
  360. static int cmp_size_smaller_first(void *priv, struct list_head *a,
  361. struct list_head *b)
  362. {
  363. struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
  364. struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
  365. /* Sort A before B if A is smaller. */
  366. return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
  367. }
  368. /**
  369. * cs_parser_fini() - clean parser states
  370. * @parser: parser structure holding parsing context.
  371. * @error: error number
  372. *
  373. * If error is set than unvalidate buffer, otherwise just free memory
  374. * used by parsing context.
  375. **/
  376. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  377. {
  378. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  379. unsigned i;
  380. if (!error) {
  381. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  382. /* Sort the buffer list from the smallest to largest buffer,
  383. * which affects the order of buffers in the LRU list.
  384. * This assures that the smallest buffers are added first
  385. * to the LRU list, so they are likely to be later evicted
  386. * first, instead of large buffers whose eviction is more
  387. * expensive.
  388. *
  389. * This slightly lowers the number of bytes moved by TTM
  390. * per frame under memory pressure.
  391. */
  392. list_sort(NULL, &parser->validated, cmp_size_smaller_first);
  393. ttm_eu_fence_buffer_objects(&parser->ticket,
  394. &parser->validated,
  395. parser->fence);
  396. } else if (backoff) {
  397. ttm_eu_backoff_reservation(&parser->ticket,
  398. &parser->validated);
  399. }
  400. fence_put(parser->fence);
  401. if (parser->ctx)
  402. amdgpu_ctx_put(parser->ctx);
  403. if (parser->bo_list)
  404. amdgpu_bo_list_put(parser->bo_list);
  405. for (i = 0; i < parser->nchunks; i++)
  406. drm_free_large(parser->chunks[i].kdata);
  407. kfree(parser->chunks);
  408. if (parser->ibs)
  409. for (i = 0; i < parser->num_ibs; i++)
  410. amdgpu_ib_free(parser->adev, &parser->ibs[i]);
  411. kfree(parser->ibs);
  412. amdgpu_bo_unref(&parser->uf.bo);
  413. amdgpu_bo_unref(&parser->uf_entry.robj);
  414. }
  415. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  416. struct amdgpu_vm *vm)
  417. {
  418. struct amdgpu_device *adev = p->adev;
  419. struct amdgpu_bo_va *bo_va;
  420. struct amdgpu_bo *bo;
  421. int i, r;
  422. r = amdgpu_vm_update_page_directory(adev, vm);
  423. if (r)
  424. return r;
  425. r = amdgpu_sync_fence(adev, &p->ibs[0].sync, vm->page_directory_fence);
  426. if (r)
  427. return r;
  428. r = amdgpu_vm_clear_freed(adev, vm);
  429. if (r)
  430. return r;
  431. if (p->bo_list) {
  432. for (i = 0; i < p->bo_list->num_entries; i++) {
  433. struct fence *f;
  434. /* ignore duplicates */
  435. bo = p->bo_list->array[i].robj;
  436. if (!bo)
  437. continue;
  438. bo_va = p->bo_list->array[i].bo_va;
  439. if (bo_va == NULL)
  440. continue;
  441. r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
  442. if (r)
  443. return r;
  444. f = bo_va->last_pt_update;
  445. r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
  446. if (r)
  447. return r;
  448. }
  449. }
  450. r = amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
  451. if (amdgpu_vm_debug && p->bo_list) {
  452. /* Invalidate all BOs to test for userspace bugs */
  453. for (i = 0; i < p->bo_list->num_entries; i++) {
  454. /* ignore duplicates */
  455. bo = p->bo_list->array[i].robj;
  456. if (!bo)
  457. continue;
  458. amdgpu_vm_bo_invalidate(adev, bo);
  459. }
  460. }
  461. return r;
  462. }
  463. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  464. struct amdgpu_cs_parser *parser)
  465. {
  466. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  467. struct amdgpu_vm *vm = &fpriv->vm;
  468. struct amdgpu_ring *ring;
  469. int i, r;
  470. if (parser->num_ibs == 0)
  471. return 0;
  472. /* Only for UVD/VCE VM emulation */
  473. for (i = 0; i < parser->num_ibs; i++) {
  474. ring = parser->ibs[i].ring;
  475. if (ring->funcs->parse_cs) {
  476. r = amdgpu_ring_parse_cs(ring, parser, i);
  477. if (r)
  478. return r;
  479. }
  480. }
  481. r = amdgpu_bo_vm_update_pte(parser, vm);
  482. if (!r)
  483. amdgpu_cs_sync_rings(parser);
  484. return r;
  485. }
  486. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  487. {
  488. if (r == -EDEADLK) {
  489. r = amdgpu_gpu_reset(adev);
  490. if (!r)
  491. r = -EAGAIN;
  492. }
  493. return r;
  494. }
  495. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  496. struct amdgpu_cs_parser *parser)
  497. {
  498. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  499. struct amdgpu_vm *vm = &fpriv->vm;
  500. int i, j;
  501. int r;
  502. for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
  503. struct amdgpu_cs_chunk *chunk;
  504. struct amdgpu_ib *ib;
  505. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  506. struct amdgpu_ring *ring;
  507. chunk = &parser->chunks[i];
  508. ib = &parser->ibs[j];
  509. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  510. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  511. continue;
  512. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  513. chunk_ib->ip_instance, chunk_ib->ring,
  514. &ring);
  515. if (r)
  516. return r;
  517. if (ring->funcs->parse_cs) {
  518. struct amdgpu_bo_va_mapping *m;
  519. struct amdgpu_bo *aobj = NULL;
  520. uint64_t offset;
  521. uint8_t *kptr;
  522. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  523. &aobj);
  524. if (!aobj) {
  525. DRM_ERROR("IB va_start is invalid\n");
  526. return -EINVAL;
  527. }
  528. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  529. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  530. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  531. return -EINVAL;
  532. }
  533. /* the IB should be reserved at this point */
  534. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  535. if (r) {
  536. return r;
  537. }
  538. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  539. kptr += chunk_ib->va_start - offset;
  540. r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
  541. if (r) {
  542. DRM_ERROR("Failed to get ib !\n");
  543. return r;
  544. }
  545. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  546. amdgpu_bo_kunmap(aobj);
  547. } else {
  548. r = amdgpu_ib_get(ring, vm, 0, ib);
  549. if (r) {
  550. DRM_ERROR("Failed to get ib !\n");
  551. return r;
  552. }
  553. ib->gpu_addr = chunk_ib->va_start;
  554. }
  555. ib->length_dw = chunk_ib->ib_bytes / 4;
  556. ib->flags = chunk_ib->flags;
  557. ib->ctx = parser->ctx;
  558. j++;
  559. }
  560. if (!parser->num_ibs)
  561. return 0;
  562. /* add GDS resources to first IB */
  563. if (parser->bo_list) {
  564. struct amdgpu_bo *gds = parser->bo_list->gds_obj;
  565. struct amdgpu_bo *gws = parser->bo_list->gws_obj;
  566. struct amdgpu_bo *oa = parser->bo_list->oa_obj;
  567. struct amdgpu_ib *ib = &parser->ibs[0];
  568. if (gds) {
  569. ib->gds_base = amdgpu_bo_gpu_offset(gds);
  570. ib->gds_size = amdgpu_bo_size(gds);
  571. }
  572. if (gws) {
  573. ib->gws_base = amdgpu_bo_gpu_offset(gws);
  574. ib->gws_size = amdgpu_bo_size(gws);
  575. }
  576. if (oa) {
  577. ib->oa_base = amdgpu_bo_gpu_offset(oa);
  578. ib->oa_size = amdgpu_bo_size(oa);
  579. }
  580. }
  581. /* wrap the last IB with user fence */
  582. if (parser->uf.bo) {
  583. struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
  584. /* UVD & VCE fw doesn't support user fences */
  585. if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
  586. ib->ring->type == AMDGPU_RING_TYPE_VCE)
  587. return -EINVAL;
  588. ib->user = &parser->uf;
  589. }
  590. return 0;
  591. }
  592. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  593. struct amdgpu_cs_parser *p)
  594. {
  595. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  596. struct amdgpu_ib *ib;
  597. int i, j, r;
  598. if (!p->num_ibs)
  599. return 0;
  600. /* Add dependencies to first IB */
  601. ib = &p->ibs[0];
  602. for (i = 0; i < p->nchunks; ++i) {
  603. struct drm_amdgpu_cs_chunk_dep *deps;
  604. struct amdgpu_cs_chunk *chunk;
  605. unsigned num_deps;
  606. chunk = &p->chunks[i];
  607. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  608. continue;
  609. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  610. num_deps = chunk->length_dw * 4 /
  611. sizeof(struct drm_amdgpu_cs_chunk_dep);
  612. for (j = 0; j < num_deps; ++j) {
  613. struct amdgpu_ring *ring;
  614. struct amdgpu_ctx *ctx;
  615. struct fence *fence;
  616. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  617. deps[j].ip_instance,
  618. deps[j].ring, &ring);
  619. if (r)
  620. return r;
  621. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  622. if (ctx == NULL)
  623. return -EINVAL;
  624. fence = amdgpu_ctx_get_fence(ctx, ring,
  625. deps[j].handle);
  626. if (IS_ERR(fence)) {
  627. r = PTR_ERR(fence);
  628. amdgpu_ctx_put(ctx);
  629. return r;
  630. } else if (fence) {
  631. r = amdgpu_sync_fence(adev, &ib->sync, fence);
  632. fence_put(fence);
  633. amdgpu_ctx_put(ctx);
  634. if (r)
  635. return r;
  636. }
  637. }
  638. }
  639. return 0;
  640. }
  641. static int amdgpu_cs_free_job(struct amdgpu_job *job)
  642. {
  643. int i;
  644. if (job->ibs)
  645. for (i = 0; i < job->num_ibs; i++)
  646. amdgpu_ib_free(job->adev, &job->ibs[i]);
  647. kfree(job->ibs);
  648. if (job->uf.bo)
  649. amdgpu_bo_unref(&job->uf.bo);
  650. return 0;
  651. }
  652. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  653. union drm_amdgpu_cs *cs)
  654. {
  655. struct amdgpu_ring * ring = p->ibs->ring;
  656. struct amd_sched_fence *fence;
  657. struct amdgpu_job *job;
  658. job = kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL);
  659. if (!job)
  660. return -ENOMEM;
  661. job->base.sched = &ring->sched;
  662. job->base.s_entity = &p->ctx->rings[ring->idx].entity;
  663. job->adev = p->adev;
  664. job->owner = p->filp;
  665. job->free_job = amdgpu_cs_free_job;
  666. job->ibs = p->ibs;
  667. job->num_ibs = p->num_ibs;
  668. p->ibs = NULL;
  669. p->num_ibs = 0;
  670. if (job->ibs[job->num_ibs - 1].user) {
  671. job->uf = p->uf;
  672. job->ibs[job->num_ibs - 1].user = &job->uf;
  673. p->uf.bo = NULL;
  674. }
  675. fence = amd_sched_fence_create(job->base.s_entity, p->filp);
  676. if (!fence) {
  677. amdgpu_cs_free_job(job);
  678. kfree(job);
  679. return -ENOMEM;
  680. }
  681. job->base.s_fence = fence;
  682. p->fence = fence_get(&fence->base);
  683. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring,
  684. &fence->base);
  685. job->ibs[job->num_ibs - 1].sequence = cs->out.handle;
  686. trace_amdgpu_cs_ioctl(job);
  687. amd_sched_entity_push_job(&job->base);
  688. return 0;
  689. }
  690. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  691. {
  692. struct amdgpu_device *adev = dev->dev_private;
  693. union drm_amdgpu_cs *cs = data;
  694. struct amdgpu_cs_parser parser = {};
  695. bool reserved_buffers = false;
  696. int i, r;
  697. if (!adev->accel_working)
  698. return -EBUSY;
  699. parser.adev = adev;
  700. parser.filp = filp;
  701. r = amdgpu_cs_parser_init(&parser, data);
  702. if (r) {
  703. DRM_ERROR("Failed to initialize parser !\n");
  704. amdgpu_cs_parser_fini(&parser, r, false);
  705. r = amdgpu_cs_handle_lockup(adev, r);
  706. return r;
  707. }
  708. r = amdgpu_cs_parser_bos(&parser, data);
  709. if (r == -ENOMEM)
  710. DRM_ERROR("Not enough memory for command submission!\n");
  711. else if (r && r != -ERESTARTSYS)
  712. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  713. else if (!r) {
  714. reserved_buffers = true;
  715. r = amdgpu_cs_ib_fill(adev, &parser);
  716. }
  717. if (!r) {
  718. r = amdgpu_cs_dependencies(adev, &parser);
  719. if (r)
  720. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  721. }
  722. if (r)
  723. goto out;
  724. for (i = 0; i < parser.num_ibs; i++)
  725. trace_amdgpu_cs(&parser, i);
  726. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  727. if (r)
  728. goto out;
  729. if (parser.num_ibs)
  730. r = amdgpu_cs_submit(&parser, cs);
  731. out:
  732. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  733. r = amdgpu_cs_handle_lockup(adev, r);
  734. return r;
  735. }
  736. /**
  737. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  738. *
  739. * @dev: drm device
  740. * @data: data from userspace
  741. * @filp: file private
  742. *
  743. * Wait for the command submission identified by handle to finish.
  744. */
  745. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  746. struct drm_file *filp)
  747. {
  748. union drm_amdgpu_wait_cs *wait = data;
  749. struct amdgpu_device *adev = dev->dev_private;
  750. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  751. struct amdgpu_ring *ring = NULL;
  752. struct amdgpu_ctx *ctx;
  753. struct fence *fence;
  754. long r;
  755. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  756. wait->in.ring, &ring);
  757. if (r)
  758. return r;
  759. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  760. if (ctx == NULL)
  761. return -EINVAL;
  762. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  763. if (IS_ERR(fence))
  764. r = PTR_ERR(fence);
  765. else if (fence) {
  766. r = fence_wait_timeout(fence, true, timeout);
  767. fence_put(fence);
  768. } else
  769. r = 1;
  770. amdgpu_ctx_put(ctx);
  771. if (r < 0)
  772. return r;
  773. memset(wait, 0, sizeof(*wait));
  774. wait->out.status = (r == 0);
  775. return 0;
  776. }
  777. /**
  778. * amdgpu_cs_find_bo_va - find bo_va for VM address
  779. *
  780. * @parser: command submission parser context
  781. * @addr: VM address
  782. * @bo: resulting BO of the mapping found
  783. *
  784. * Search the buffer objects in the command submission context for a certain
  785. * virtual memory address. Returns allocation structure when found, NULL
  786. * otherwise.
  787. */
  788. struct amdgpu_bo_va_mapping *
  789. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  790. uint64_t addr, struct amdgpu_bo **bo)
  791. {
  792. struct amdgpu_bo_va_mapping *mapping;
  793. unsigned i;
  794. if (!parser->bo_list)
  795. return NULL;
  796. addr /= AMDGPU_GPU_PAGE_SIZE;
  797. for (i = 0; i < parser->bo_list->num_entries; i++) {
  798. struct amdgpu_bo_list_entry *lobj;
  799. lobj = &parser->bo_list->array[i];
  800. if (!lobj->bo_va)
  801. continue;
  802. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  803. if (mapping->it.start > addr ||
  804. addr > mapping->it.last)
  805. continue;
  806. *bo = lobj->bo_va->bo;
  807. return mapping;
  808. }
  809. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  810. if (mapping->it.start > addr ||
  811. addr > mapping->it.last)
  812. continue;
  813. *bo = lobj->bo_va->bo;
  814. return mapping;
  815. }
  816. }
  817. return NULL;
  818. }