amdgpu_cs.c 24 KB

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