amdgpu_cs.c 24 KB

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