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 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, num_ibs = 0;
  116. int i;
  117. int ret;
  118. if (cs->in.num_chunks == 0)
  119. return 0;
  120. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  121. if (!chunk_array)
  122. return -ENOMEM;
  123. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  124. if (!p->ctx) {
  125. ret = -EINVAL;
  126. goto free_chunk;
  127. }
  128. /* get chunks */
  129. chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
  130. if (copy_from_user(chunk_array, chunk_array_user,
  131. sizeof(uint64_t)*cs->in.num_chunks)) {
  132. ret = -EFAULT;
  133. goto put_ctx;
  134. }
  135. p->nchunks = cs->in.num_chunks;
  136. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  137. GFP_KERNEL);
  138. if (!p->chunks) {
  139. ret = -ENOMEM;
  140. goto put_ctx;
  141. }
  142. for (i = 0; i < p->nchunks; i++) {
  143. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  144. struct drm_amdgpu_cs_chunk user_chunk;
  145. uint32_t __user *cdata;
  146. chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
  147. if (copy_from_user(&user_chunk, chunk_ptr,
  148. sizeof(struct drm_amdgpu_cs_chunk))) {
  149. ret = -EFAULT;
  150. i--;
  151. goto free_partial_kdata;
  152. }
  153. p->chunks[i].chunk_id = user_chunk.chunk_id;
  154. p->chunks[i].length_dw = user_chunk.length_dw;
  155. size = p->chunks[i].length_dw;
  156. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  157. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  158. if (p->chunks[i].kdata == NULL) {
  159. ret = -ENOMEM;
  160. i--;
  161. goto free_partial_kdata;
  162. }
  163. size *= sizeof(uint32_t);
  164. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  165. ret = -EFAULT;
  166. goto free_partial_kdata;
  167. }
  168. switch (p->chunks[i].chunk_id) {
  169. case AMDGPU_CHUNK_ID_IB:
  170. ++num_ibs;
  171. break;
  172. case AMDGPU_CHUNK_ID_FENCE:
  173. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  174. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  175. ret = -EINVAL;
  176. goto free_partial_kdata;
  177. }
  178. ret = amdgpu_cs_user_fence_chunk(p, (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. ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job);
  190. if (ret)
  191. goto free_all_kdata;
  192. kfree(chunk_array);
  193. return 0;
  194. free_all_kdata:
  195. i = p->nchunks - 1;
  196. free_partial_kdata:
  197. for (; i >= 0; i--)
  198. drm_free_large(p->chunks[i].kdata);
  199. kfree(p->chunks);
  200. put_ctx:
  201. amdgpu_ctx_put(p->ctx);
  202. free_chunk:
  203. kfree(chunk_array);
  204. return ret;
  205. }
  206. /* Returns how many bytes TTM can move per IB.
  207. */
  208. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  209. {
  210. u64 real_vram_size = adev->mc.real_vram_size;
  211. u64 vram_usage = atomic64_read(&adev->vram_usage);
  212. /* This function is based on the current VRAM usage.
  213. *
  214. * - If all of VRAM is free, allow relocating the number of bytes that
  215. * is equal to 1/4 of the size of VRAM for this IB.
  216. * - If more than one half of VRAM is occupied, only allow relocating
  217. * 1 MB of data for this IB.
  218. *
  219. * - From 0 to one half of used VRAM, the threshold decreases
  220. * linearly.
  221. * __________________
  222. * 1/4 of -|\ |
  223. * VRAM | \ |
  224. * | \ |
  225. * | \ |
  226. * | \ |
  227. * | \ |
  228. * | \ |
  229. * | \________|1 MB
  230. * |----------------|
  231. * VRAM 0 % 100 %
  232. * used used
  233. *
  234. * Note: It's a threshold, not a limit. The threshold must be crossed
  235. * for buffer relocations to stop, so any buffer of an arbitrary size
  236. * can be moved as long as the threshold isn't crossed before
  237. * the relocation takes place. We don't want to disable buffer
  238. * relocations completely.
  239. *
  240. * The idea is that buffers should be placed in VRAM at creation time
  241. * and TTM should only do a minimum number of relocations during
  242. * command submission. In practice, you need to submit at least
  243. * a dozen IBs to move all buffers to VRAM if they are in GTT.
  244. *
  245. * Also, things can get pretty crazy under memory pressure and actual
  246. * VRAM usage can change a lot, so playing safe even at 50% does
  247. * consistently increase performance.
  248. */
  249. u64 half_vram = real_vram_size >> 1;
  250. u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
  251. u64 bytes_moved_threshold = half_free_vram >> 1;
  252. return max(bytes_moved_threshold, 1024*1024ull);
  253. }
  254. int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
  255. struct list_head *validated)
  256. {
  257. struct amdgpu_bo_list_entry *lobj;
  258. u64 initial_bytes_moved;
  259. int r;
  260. list_for_each_entry(lobj, validated, tv.head) {
  261. struct amdgpu_bo *bo = lobj->robj;
  262. struct mm_struct *usermm;
  263. uint32_t domain;
  264. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  265. if (usermm && usermm != current->mm)
  266. return -EPERM;
  267. if (bo->pin_count)
  268. continue;
  269. /* Avoid moving this one if we have moved too many buffers
  270. * for this IB already.
  271. *
  272. * Note that this allows moving at least one buffer of
  273. * any size, because it doesn't take the current "bo"
  274. * into account. We don't want to disallow buffer moves
  275. * completely.
  276. */
  277. if (p->bytes_moved <= p->bytes_moved_threshold)
  278. domain = bo->prefered_domains;
  279. else
  280. domain = bo->allowed_domains;
  281. retry:
  282. amdgpu_ttm_placement_from_domain(bo, domain);
  283. initial_bytes_moved = atomic64_read(&bo->adev->num_bytes_moved);
  284. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  285. p->bytes_moved += atomic64_read(&bo->adev->num_bytes_moved) -
  286. initial_bytes_moved;
  287. if (unlikely(r)) {
  288. if (r != -ERESTARTSYS && domain != bo->allowed_domains) {
  289. domain = bo->allowed_domains;
  290. goto retry;
  291. }
  292. return r;
  293. }
  294. }
  295. return 0;
  296. }
  297. static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  298. union drm_amdgpu_cs *cs)
  299. {
  300. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  301. struct list_head duplicates;
  302. bool need_mmap_lock = false;
  303. int r;
  304. INIT_LIST_HEAD(&p->validated);
  305. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  306. if (p->bo_list) {
  307. need_mmap_lock = p->bo_list->has_userptr;
  308. amdgpu_bo_list_get_list(p->bo_list, &p->validated);
  309. }
  310. INIT_LIST_HEAD(&duplicates);
  311. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  312. if (p->uf.bo)
  313. list_add(&p->uf_entry.tv.head, &p->validated);
  314. if (need_mmap_lock)
  315. down_read(&current->mm->mmap_sem);
  316. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
  317. if (unlikely(r != 0))
  318. goto error_reserve;
  319. amdgpu_vm_get_pt_bos(&fpriv->vm, &duplicates);
  320. p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
  321. p->bytes_moved = 0;
  322. r = amdgpu_cs_list_validate(p, &duplicates);
  323. if (r)
  324. goto error_validate;
  325. r = amdgpu_cs_list_validate(p, &p->validated);
  326. if (r)
  327. goto error_validate;
  328. if (p->bo_list) {
  329. struct amdgpu_vm *vm = &fpriv->vm;
  330. unsigned i;
  331. for (i = 0; i < p->bo_list->num_entries; i++) {
  332. struct amdgpu_bo *bo = p->bo_list->array[i].robj;
  333. p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
  334. }
  335. }
  336. error_validate:
  337. if (r) {
  338. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  339. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  340. }
  341. error_reserve:
  342. if (need_mmap_lock)
  343. up_read(&current->mm->mmap_sem);
  344. return r;
  345. }
  346. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  347. {
  348. struct amdgpu_bo_list_entry *e;
  349. int r;
  350. list_for_each_entry(e, &p->validated, tv.head) {
  351. struct reservation_object *resv = e->robj->tbo.resv;
  352. r = amdgpu_sync_resv(p->adev, &p->job->ibs[0].sync, resv, p->filp);
  353. if (r)
  354. return r;
  355. }
  356. return 0;
  357. }
  358. static int cmp_size_smaller_first(void *priv, struct list_head *a,
  359. struct list_head *b)
  360. {
  361. struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
  362. struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
  363. /* Sort A before B if A is smaller. */
  364. return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
  365. }
  366. /**
  367. * cs_parser_fini() - clean parser states
  368. * @parser: parser structure holding parsing context.
  369. * @error: error number
  370. *
  371. * If error is set than unvalidate buffer, otherwise just free memory
  372. * used by parsing context.
  373. **/
  374. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  375. {
  376. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  377. unsigned i;
  378. if (!error) {
  379. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  380. /* Sort the buffer list from the smallest to largest buffer,
  381. * which affects the order of buffers in the LRU list.
  382. * This assures that the smallest buffers are added first
  383. * to the LRU list, so they are likely to be later evicted
  384. * first, instead of large buffers whose eviction is more
  385. * expensive.
  386. *
  387. * This slightly lowers the number of bytes moved by TTM
  388. * per frame under memory pressure.
  389. */
  390. list_sort(NULL, &parser->validated, cmp_size_smaller_first);
  391. ttm_eu_fence_buffer_objects(&parser->ticket,
  392. &parser->validated,
  393. parser->fence);
  394. } else if (backoff) {
  395. ttm_eu_backoff_reservation(&parser->ticket,
  396. &parser->validated);
  397. }
  398. fence_put(parser->fence);
  399. if (parser->ctx)
  400. amdgpu_ctx_put(parser->ctx);
  401. if (parser->bo_list)
  402. amdgpu_bo_list_put(parser->bo_list);
  403. for (i = 0; i < parser->nchunks; i++)
  404. drm_free_large(parser->chunks[i].kdata);
  405. kfree(parser->chunks);
  406. if (parser->job)
  407. amdgpu_job_free(parser->job);
  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->job->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->job->ibs[0].sync, f);
  442. if (r)
  443. return r;
  444. }
  445. }
  446. r = amdgpu_vm_clear_invalids(adev, vm, &p->job->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. /* Only for UVD/VCE VM emulation */
  467. for (i = 0; i < parser->job->num_ibs; i++) {
  468. ring = parser->job->ibs[i].ring;
  469. if (ring->funcs->parse_cs) {
  470. r = amdgpu_ring_parse_cs(ring, parser, i);
  471. if (r)
  472. return r;
  473. }
  474. }
  475. r = amdgpu_bo_vm_update_pte(parser, vm);
  476. if (!r)
  477. amdgpu_cs_sync_rings(parser);
  478. return r;
  479. }
  480. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  481. {
  482. if (r == -EDEADLK) {
  483. r = amdgpu_gpu_reset(adev);
  484. if (!r)
  485. r = -EAGAIN;
  486. }
  487. return r;
  488. }
  489. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  490. struct amdgpu_cs_parser *parser)
  491. {
  492. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  493. struct amdgpu_vm *vm = &fpriv->vm;
  494. int i, j;
  495. int r;
  496. for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
  497. struct amdgpu_cs_chunk *chunk;
  498. struct amdgpu_ib *ib;
  499. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  500. struct amdgpu_ring *ring;
  501. chunk = &parser->chunks[i];
  502. ib = &parser->job->ibs[j];
  503. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  504. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  505. continue;
  506. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  507. chunk_ib->ip_instance, chunk_ib->ring,
  508. &ring);
  509. if (r)
  510. return r;
  511. if (ring->funcs->parse_cs) {
  512. struct amdgpu_bo_va_mapping *m;
  513. struct amdgpu_bo *aobj = NULL;
  514. uint64_t offset;
  515. uint8_t *kptr;
  516. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  517. &aobj);
  518. if (!aobj) {
  519. DRM_ERROR("IB va_start is invalid\n");
  520. return -EINVAL;
  521. }
  522. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  523. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  524. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  525. return -EINVAL;
  526. }
  527. /* the IB should be reserved at this point */
  528. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  529. if (r) {
  530. return r;
  531. }
  532. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  533. kptr += chunk_ib->va_start - offset;
  534. r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
  535. if (r) {
  536. DRM_ERROR("Failed to get ib !\n");
  537. return r;
  538. }
  539. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  540. amdgpu_bo_kunmap(aobj);
  541. } else {
  542. r = amdgpu_ib_get(ring, vm, 0, ib);
  543. if (r) {
  544. DRM_ERROR("Failed to get ib !\n");
  545. return r;
  546. }
  547. ib->gpu_addr = chunk_ib->va_start;
  548. }
  549. ib->length_dw = chunk_ib->ib_bytes / 4;
  550. ib->flags = chunk_ib->flags;
  551. ib->ctx = parser->ctx;
  552. j++;
  553. }
  554. /* add GDS resources to first IB */
  555. if (parser->bo_list) {
  556. struct amdgpu_bo *gds = parser->bo_list->gds_obj;
  557. struct amdgpu_bo *gws = parser->bo_list->gws_obj;
  558. struct amdgpu_bo *oa = parser->bo_list->oa_obj;
  559. struct amdgpu_ib *ib = &parser->job->ibs[0];
  560. if (gds) {
  561. ib->gds_base = amdgpu_bo_gpu_offset(gds);
  562. ib->gds_size = amdgpu_bo_size(gds);
  563. }
  564. if (gws) {
  565. ib->gws_base = amdgpu_bo_gpu_offset(gws);
  566. ib->gws_size = amdgpu_bo_size(gws);
  567. }
  568. if (oa) {
  569. ib->oa_base = amdgpu_bo_gpu_offset(oa);
  570. ib->oa_size = amdgpu_bo_size(oa);
  571. }
  572. }
  573. /* wrap the last IB with user fence */
  574. if (parser->uf.bo) {
  575. struct amdgpu_ib *ib = &parser->job->ibs[parser->job->num_ibs - 1];
  576. /* UVD & VCE fw doesn't support user fences */
  577. if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
  578. ib->ring->type == AMDGPU_RING_TYPE_VCE)
  579. return -EINVAL;
  580. ib->user = &parser->uf;
  581. }
  582. return 0;
  583. }
  584. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  585. struct amdgpu_cs_parser *p)
  586. {
  587. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  588. struct amdgpu_ib *ib;
  589. int i, j, r;
  590. /* Add dependencies to first IB */
  591. ib = &p->job->ibs[0];
  592. for (i = 0; i < p->nchunks; ++i) {
  593. struct drm_amdgpu_cs_chunk_dep *deps;
  594. struct amdgpu_cs_chunk *chunk;
  595. unsigned num_deps;
  596. chunk = &p->chunks[i];
  597. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  598. continue;
  599. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  600. num_deps = chunk->length_dw * 4 /
  601. sizeof(struct drm_amdgpu_cs_chunk_dep);
  602. for (j = 0; j < num_deps; ++j) {
  603. struct amdgpu_ring *ring;
  604. struct amdgpu_ctx *ctx;
  605. struct fence *fence;
  606. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  607. deps[j].ip_instance,
  608. deps[j].ring, &ring);
  609. if (r)
  610. return r;
  611. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  612. if (ctx == NULL)
  613. return -EINVAL;
  614. fence = amdgpu_ctx_get_fence(ctx, ring,
  615. deps[j].handle);
  616. if (IS_ERR(fence)) {
  617. r = PTR_ERR(fence);
  618. amdgpu_ctx_put(ctx);
  619. return r;
  620. } else if (fence) {
  621. r = amdgpu_sync_fence(adev, &ib->sync, fence);
  622. fence_put(fence);
  623. amdgpu_ctx_put(ctx);
  624. if (r)
  625. return r;
  626. }
  627. }
  628. }
  629. return 0;
  630. }
  631. static int amdgpu_cs_free_job(struct amdgpu_job *job)
  632. {
  633. amdgpu_job_free(job);
  634. return 0;
  635. }
  636. static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  637. union drm_amdgpu_cs *cs)
  638. {
  639. struct amdgpu_ring * ring = p->job->ibs->ring;
  640. struct amd_sched_fence *fence;
  641. struct amdgpu_job *job;
  642. job = p->job;
  643. p->job = NULL;
  644. job->base.sched = &ring->sched;
  645. job->base.s_entity = &p->ctx->rings[ring->idx].entity;
  646. job->adev = p->adev;
  647. job->owner = p->filp;
  648. job->free_job = amdgpu_cs_free_job;
  649. if (job->ibs[job->num_ibs - 1].user) {
  650. job->uf = p->uf;
  651. job->ibs[job->num_ibs - 1].user = &job->uf;
  652. p->uf.bo = NULL;
  653. }
  654. fence = amd_sched_fence_create(job->base.s_entity, p->filp);
  655. if (!fence) {
  656. amdgpu_cs_free_job(job);
  657. kfree(job);
  658. return -ENOMEM;
  659. }
  660. job->base.s_fence = fence;
  661. p->fence = fence_get(&fence->base);
  662. cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring,
  663. &fence->base);
  664. job->ibs[job->num_ibs - 1].sequence = cs->out.handle;
  665. trace_amdgpu_cs_ioctl(job);
  666. amd_sched_entity_push_job(&job->base);
  667. return 0;
  668. }
  669. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  670. {
  671. struct amdgpu_device *adev = dev->dev_private;
  672. union drm_amdgpu_cs *cs = data;
  673. struct amdgpu_cs_parser parser = {};
  674. bool reserved_buffers = false;
  675. int i, r;
  676. if (!adev->accel_working)
  677. return -EBUSY;
  678. parser.adev = adev;
  679. parser.filp = filp;
  680. r = amdgpu_cs_parser_init(&parser, data);
  681. if (r) {
  682. DRM_ERROR("Failed to initialize parser !\n");
  683. amdgpu_cs_parser_fini(&parser, r, false);
  684. r = amdgpu_cs_handle_lockup(adev, r);
  685. return r;
  686. }
  687. r = amdgpu_cs_parser_bos(&parser, data);
  688. if (r == -ENOMEM)
  689. DRM_ERROR("Not enough memory for command submission!\n");
  690. else if (r && r != -ERESTARTSYS)
  691. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  692. else if (!r) {
  693. reserved_buffers = true;
  694. r = amdgpu_cs_ib_fill(adev, &parser);
  695. }
  696. if (!r) {
  697. r = amdgpu_cs_dependencies(adev, &parser);
  698. if (r)
  699. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  700. }
  701. if (r)
  702. goto out;
  703. for (i = 0; i < parser.job->num_ibs; i++)
  704. trace_amdgpu_cs(&parser, i);
  705. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  706. if (r)
  707. goto out;
  708. r = amdgpu_cs_submit(&parser, cs);
  709. out:
  710. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  711. r = amdgpu_cs_handle_lockup(adev, r);
  712. return r;
  713. }
  714. /**
  715. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  716. *
  717. * @dev: drm device
  718. * @data: data from userspace
  719. * @filp: file private
  720. *
  721. * Wait for the command submission identified by handle to finish.
  722. */
  723. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  724. struct drm_file *filp)
  725. {
  726. union drm_amdgpu_wait_cs *wait = data;
  727. struct amdgpu_device *adev = dev->dev_private;
  728. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  729. struct amdgpu_ring *ring = NULL;
  730. struct amdgpu_ctx *ctx;
  731. struct fence *fence;
  732. long r;
  733. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  734. wait->in.ring, &ring);
  735. if (r)
  736. return r;
  737. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  738. if (ctx == NULL)
  739. return -EINVAL;
  740. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  741. if (IS_ERR(fence))
  742. r = PTR_ERR(fence);
  743. else if (fence) {
  744. r = fence_wait_timeout(fence, true, timeout);
  745. fence_put(fence);
  746. } else
  747. r = 1;
  748. amdgpu_ctx_put(ctx);
  749. if (r < 0)
  750. return r;
  751. memset(wait, 0, sizeof(*wait));
  752. wait->out.status = (r == 0);
  753. return 0;
  754. }
  755. /**
  756. * amdgpu_cs_find_bo_va - find bo_va for VM address
  757. *
  758. * @parser: command submission parser context
  759. * @addr: VM address
  760. * @bo: resulting BO of the mapping found
  761. *
  762. * Search the buffer objects in the command submission context for a certain
  763. * virtual memory address. Returns allocation structure when found, NULL
  764. * otherwise.
  765. */
  766. struct amdgpu_bo_va_mapping *
  767. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  768. uint64_t addr, struct amdgpu_bo **bo)
  769. {
  770. struct amdgpu_bo_va_mapping *mapping;
  771. unsigned i;
  772. if (!parser->bo_list)
  773. return NULL;
  774. addr /= AMDGPU_GPU_PAGE_SIZE;
  775. for (i = 0; i < parser->bo_list->num_entries; i++) {
  776. struct amdgpu_bo_list_entry *lobj;
  777. lobj = &parser->bo_list->array[i];
  778. if (!lobj->bo_va)
  779. continue;
  780. list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
  781. if (mapping->it.start > addr ||
  782. addr > mapping->it.last)
  783. continue;
  784. *bo = lobj->bo_va->bo;
  785. return mapping;
  786. }
  787. list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
  788. if (mapping->it.start > addr ||
  789. addr > mapping->it.last)
  790. continue;
  791. *bo = lobj->bo_va->bo;
  792. return mapping;
  793. }
  794. }
  795. return NULL;
  796. }