amdgpu_cs.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. #define AMDGPU_CS_MAX_PRIORITY 32u
  33. #define AMDGPU_CS_NUM_BUCKETS (AMDGPU_CS_MAX_PRIORITY + 1)
  34. /* This is based on the bucket sort with O(n) time complexity.
  35. * An item with priority "i" is added to bucket[i]. The lists are then
  36. * concatenated in descending order.
  37. */
  38. struct amdgpu_cs_buckets {
  39. struct list_head bucket[AMDGPU_CS_NUM_BUCKETS];
  40. };
  41. static void amdgpu_cs_buckets_init(struct amdgpu_cs_buckets *b)
  42. {
  43. unsigned i;
  44. for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++)
  45. INIT_LIST_HEAD(&b->bucket[i]);
  46. }
  47. static void amdgpu_cs_buckets_add(struct amdgpu_cs_buckets *b,
  48. struct list_head *item, unsigned priority)
  49. {
  50. /* Since buffers which appear sooner in the relocation list are
  51. * likely to be used more often than buffers which appear later
  52. * in the list, the sort mustn't change the ordering of buffers
  53. * with the same priority, i.e. it must be stable.
  54. */
  55. list_add_tail(item, &b->bucket[min(priority, AMDGPU_CS_MAX_PRIORITY)]);
  56. }
  57. static void amdgpu_cs_buckets_get_list(struct amdgpu_cs_buckets *b,
  58. struct list_head *out_list)
  59. {
  60. unsigned i;
  61. /* Connect the sorted buckets in the output list. */
  62. for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++) {
  63. list_splice(&b->bucket[i], out_list);
  64. }
  65. }
  66. int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
  67. u32 ip_instance, u32 ring,
  68. struct amdgpu_ring **out_ring)
  69. {
  70. /* Right now all IPs have only one instance - multiple rings. */
  71. if (ip_instance != 0) {
  72. DRM_ERROR("invalid ip instance: %d\n", ip_instance);
  73. return -EINVAL;
  74. }
  75. switch (ip_type) {
  76. default:
  77. DRM_ERROR("unknown ip type: %d\n", ip_type);
  78. return -EINVAL;
  79. case AMDGPU_HW_IP_GFX:
  80. if (ring < adev->gfx.num_gfx_rings) {
  81. *out_ring = &adev->gfx.gfx_ring[ring];
  82. } else {
  83. DRM_ERROR("only %d gfx rings are supported now\n",
  84. adev->gfx.num_gfx_rings);
  85. return -EINVAL;
  86. }
  87. break;
  88. case AMDGPU_HW_IP_COMPUTE:
  89. if (ring < adev->gfx.num_compute_rings) {
  90. *out_ring = &adev->gfx.compute_ring[ring];
  91. } else {
  92. DRM_ERROR("only %d compute rings are supported now\n",
  93. adev->gfx.num_compute_rings);
  94. return -EINVAL;
  95. }
  96. break;
  97. case AMDGPU_HW_IP_DMA:
  98. if (ring < adev->sdma.num_instances) {
  99. *out_ring = &adev->sdma.instance[ring].ring;
  100. } else {
  101. DRM_ERROR("only %d SDMA rings are supported\n",
  102. adev->sdma.num_instances);
  103. return -EINVAL;
  104. }
  105. break;
  106. case AMDGPU_HW_IP_UVD:
  107. *out_ring = &adev->uvd.ring;
  108. break;
  109. case AMDGPU_HW_IP_VCE:
  110. if (ring < 2){
  111. *out_ring = &adev->vce.ring[ring];
  112. } else {
  113. DRM_ERROR("only two VCE rings are supported\n");
  114. return -EINVAL;
  115. }
  116. break;
  117. }
  118. return 0;
  119. }
  120. static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
  121. struct drm_amdgpu_cs_chunk_fence *fence_data)
  122. {
  123. struct drm_gem_object *gobj;
  124. uint32_t handle;
  125. handle = fence_data->handle;
  126. gobj = drm_gem_object_lookup(p->adev->ddev, p->filp,
  127. fence_data->handle);
  128. if (gobj == NULL)
  129. return -EINVAL;
  130. p->uf.bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  131. p->uf.offset = fence_data->offset;
  132. if (amdgpu_ttm_tt_has_userptr(p->uf.bo->tbo.ttm)) {
  133. drm_gem_object_unreference_unlocked(gobj);
  134. return -EINVAL;
  135. }
  136. p->uf_entry.robj = amdgpu_bo_ref(p->uf.bo);
  137. p->uf_entry.prefered_domains = AMDGPU_GEM_DOMAIN_GTT;
  138. p->uf_entry.allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
  139. p->uf_entry.priority = 0;
  140. p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
  141. p->uf_entry.tv.shared = true;
  142. drm_gem_object_unreference_unlocked(gobj);
  143. return 0;
  144. }
  145. int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
  146. {
  147. union drm_amdgpu_cs *cs = data;
  148. uint64_t *chunk_array_user;
  149. uint64_t *chunk_array;
  150. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  151. unsigned size;
  152. int i;
  153. int ret;
  154. if (cs->in.num_chunks == 0)
  155. return 0;
  156. chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
  157. if (!chunk_array)
  158. return -ENOMEM;
  159. p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
  160. if (!p->ctx) {
  161. ret = -EINVAL;
  162. goto free_chunk;
  163. }
  164. p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  165. /* get chunks */
  166. INIT_LIST_HEAD(&p->validated);
  167. chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks);
  168. if (copy_from_user(chunk_array, chunk_array_user,
  169. sizeof(uint64_t)*cs->in.num_chunks)) {
  170. ret = -EFAULT;
  171. goto put_bo_list;
  172. }
  173. p->nchunks = cs->in.num_chunks;
  174. p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
  175. GFP_KERNEL);
  176. if (!p->chunks) {
  177. ret = -ENOMEM;
  178. goto put_bo_list;
  179. }
  180. for (i = 0; i < p->nchunks; i++) {
  181. struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
  182. struct drm_amdgpu_cs_chunk user_chunk;
  183. uint32_t __user *cdata;
  184. chunk_ptr = (void __user *)(unsigned long)chunk_array[i];
  185. if (copy_from_user(&user_chunk, chunk_ptr,
  186. sizeof(struct drm_amdgpu_cs_chunk))) {
  187. ret = -EFAULT;
  188. i--;
  189. goto free_partial_kdata;
  190. }
  191. p->chunks[i].chunk_id = user_chunk.chunk_id;
  192. p->chunks[i].length_dw = user_chunk.length_dw;
  193. size = p->chunks[i].length_dw;
  194. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  195. p->chunks[i].user_ptr = cdata;
  196. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  197. if (p->chunks[i].kdata == NULL) {
  198. ret = -ENOMEM;
  199. i--;
  200. goto free_partial_kdata;
  201. }
  202. size *= sizeof(uint32_t);
  203. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  204. ret = -EFAULT;
  205. goto free_partial_kdata;
  206. }
  207. switch (p->chunks[i].chunk_id) {
  208. case AMDGPU_CHUNK_ID_IB:
  209. p->num_ibs++;
  210. break;
  211. case AMDGPU_CHUNK_ID_FENCE:
  212. size = sizeof(struct drm_amdgpu_cs_chunk_fence);
  213. if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
  214. ret = -EINVAL;
  215. goto free_partial_kdata;
  216. }
  217. ret = amdgpu_cs_user_fence_chunk(p, (void *)p->chunks[i].kdata);
  218. if (ret)
  219. goto free_partial_kdata;
  220. break;
  221. case AMDGPU_CHUNK_ID_DEPENDENCIES:
  222. break;
  223. default:
  224. ret = -EINVAL;
  225. goto free_partial_kdata;
  226. }
  227. }
  228. p->ibs = kcalloc(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL);
  229. if (!p->ibs) {
  230. ret = -ENOMEM;
  231. goto free_all_kdata;
  232. }
  233. kfree(chunk_array);
  234. return 0;
  235. free_all_kdata:
  236. i = p->nchunks - 1;
  237. free_partial_kdata:
  238. for (; i >= 0; i--)
  239. drm_free_large(p->chunks[i].kdata);
  240. kfree(p->chunks);
  241. put_bo_list:
  242. if (p->bo_list)
  243. amdgpu_bo_list_put(p->bo_list);
  244. amdgpu_ctx_put(p->ctx);
  245. free_chunk:
  246. kfree(chunk_array);
  247. return ret;
  248. }
  249. /* Returns how many bytes TTM can move per IB.
  250. */
  251. static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
  252. {
  253. u64 real_vram_size = adev->mc.real_vram_size;
  254. u64 vram_usage = atomic64_read(&adev->vram_usage);
  255. /* This function is based on the current VRAM usage.
  256. *
  257. * - If all of VRAM is free, allow relocating the number of bytes that
  258. * is equal to 1/4 of the size of VRAM for this IB.
  259. * - If more than one half of VRAM is occupied, only allow relocating
  260. * 1 MB of data for this IB.
  261. *
  262. * - From 0 to one half of used VRAM, the threshold decreases
  263. * linearly.
  264. * __________________
  265. * 1/4 of -|\ |
  266. * VRAM | \ |
  267. * | \ |
  268. * | \ |
  269. * | \ |
  270. * | \ |
  271. * | \ |
  272. * | \________|1 MB
  273. * |----------------|
  274. * VRAM 0 % 100 %
  275. * used used
  276. *
  277. * Note: It's a threshold, not a limit. The threshold must be crossed
  278. * for buffer relocations to stop, so any buffer of an arbitrary size
  279. * can be moved as long as the threshold isn't crossed before
  280. * the relocation takes place. We don't want to disable buffer
  281. * relocations completely.
  282. *
  283. * The idea is that buffers should be placed in VRAM at creation time
  284. * and TTM should only do a minimum number of relocations during
  285. * command submission. In practice, you need to submit at least
  286. * a dozen IBs to move all buffers to VRAM if they are in GTT.
  287. *
  288. * Also, things can get pretty crazy under memory pressure and actual
  289. * VRAM usage can change a lot, so playing safe even at 50% does
  290. * consistently increase performance.
  291. */
  292. u64 half_vram = real_vram_size >> 1;
  293. u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
  294. u64 bytes_moved_threshold = half_free_vram >> 1;
  295. return max(bytes_moved_threshold, 1024*1024ull);
  296. }
  297. int amdgpu_cs_list_validate(struct amdgpu_device *adev,
  298. struct amdgpu_vm *vm,
  299. struct list_head *validated)
  300. {
  301. struct amdgpu_bo_list_entry *lobj;
  302. struct amdgpu_bo *bo;
  303. u64 bytes_moved = 0, initial_bytes_moved;
  304. u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev);
  305. int r;
  306. list_for_each_entry(lobj, validated, tv.head) {
  307. bo = lobj->robj;
  308. if (!bo->pin_count) {
  309. u32 domain = lobj->prefered_domains;
  310. u32 current_domain =
  311. amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  312. /* Check if this buffer will be moved and don't move it
  313. * if we have moved too many buffers for this IB already.
  314. *
  315. * Note that this allows moving at least one buffer of
  316. * any size, because it doesn't take the current "bo"
  317. * into account. We don't want to disallow buffer moves
  318. * completely.
  319. */
  320. if ((lobj->allowed_domains & current_domain) != 0 &&
  321. (domain & current_domain) == 0 && /* will be moved */
  322. bytes_moved > bytes_moved_threshold) {
  323. /* don't move it */
  324. domain = current_domain;
  325. }
  326. retry:
  327. amdgpu_ttm_placement_from_domain(bo, domain);
  328. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  329. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  330. bytes_moved += atomic64_read(&adev->num_bytes_moved) -
  331. initial_bytes_moved;
  332. if (unlikely(r)) {
  333. if (r != -ERESTARTSYS && domain != lobj->allowed_domains) {
  334. domain = lobj->allowed_domains;
  335. goto retry;
  336. }
  337. return r;
  338. }
  339. }
  340. lobj->bo_va = amdgpu_vm_bo_find(vm, bo);
  341. }
  342. return 0;
  343. }
  344. static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p)
  345. {
  346. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  347. struct amdgpu_cs_buckets buckets;
  348. struct list_head duplicates;
  349. bool need_mmap_lock = false;
  350. int i, r;
  351. if (p->bo_list) {
  352. need_mmap_lock = p->bo_list->has_userptr;
  353. amdgpu_cs_buckets_init(&buckets);
  354. for (i = 0; i < p->bo_list->num_entries; i++)
  355. amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head,
  356. p->bo_list->array[i].priority);
  357. amdgpu_cs_buckets_get_list(&buckets, &p->validated);
  358. }
  359. INIT_LIST_HEAD(&duplicates);
  360. amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
  361. if (p->uf.bo)
  362. list_add(&p->uf_entry.tv.head, &p->validated);
  363. if (need_mmap_lock)
  364. down_read(&current->mm->mmap_sem);
  365. r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates);
  366. if (unlikely(r != 0))
  367. goto error_reserve;
  368. amdgpu_vm_get_pt_bos(&fpriv->vm, &duplicates);
  369. r = amdgpu_cs_list_validate(p->adev, &fpriv->vm, &p->validated);
  370. if (r)
  371. goto error_validate;
  372. r = amdgpu_cs_list_validate(p->adev, &fpriv->vm, &duplicates);
  373. error_validate:
  374. if (r) {
  375. amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
  376. ttm_eu_backoff_reservation(&p->ticket, &p->validated);
  377. }
  378. error_reserve:
  379. if (need_mmap_lock)
  380. up_read(&current->mm->mmap_sem);
  381. return r;
  382. }
  383. static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  384. {
  385. struct amdgpu_bo_list_entry *e;
  386. int r;
  387. list_for_each_entry(e, &p->validated, tv.head) {
  388. struct reservation_object *resv = e->robj->tbo.resv;
  389. r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp);
  390. if (r)
  391. return r;
  392. }
  393. return 0;
  394. }
  395. static int cmp_size_smaller_first(void *priv, struct list_head *a,
  396. struct list_head *b)
  397. {
  398. struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head);
  399. struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head);
  400. /* Sort A before B if A is smaller. */
  401. return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
  402. }
  403. /**
  404. * cs_parser_fini() - clean parser states
  405. * @parser: parser structure holding parsing context.
  406. * @error: error number
  407. *
  408. * If error is set than unvalidate buffer, otherwise just free memory
  409. * used by parsing context.
  410. **/
  411. static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
  412. {
  413. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  414. unsigned i;
  415. amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
  416. if (!error) {
  417. /* Sort the buffer list from the smallest to largest buffer,
  418. * which affects the order of buffers in the LRU list.
  419. * This assures that the smallest buffers are added first
  420. * to the LRU list, so they are likely to be later evicted
  421. * first, instead of large buffers whose eviction is more
  422. * expensive.
  423. *
  424. * This slightly lowers the number of bytes moved by TTM
  425. * per frame under memory pressure.
  426. */
  427. list_sort(NULL, &parser->validated, cmp_size_smaller_first);
  428. ttm_eu_fence_buffer_objects(&parser->ticket,
  429. &parser->validated,
  430. parser->fence);
  431. } else if (backoff) {
  432. ttm_eu_backoff_reservation(&parser->ticket,
  433. &parser->validated);
  434. }
  435. fence_put(parser->fence);
  436. if (parser->ctx)
  437. amdgpu_ctx_put(parser->ctx);
  438. if (parser->bo_list)
  439. amdgpu_bo_list_put(parser->bo_list);
  440. for (i = 0; i < parser->nchunks; i++)
  441. drm_free_large(parser->chunks[i].kdata);
  442. kfree(parser->chunks);
  443. if (parser->ibs)
  444. for (i = 0; i < parser->num_ibs; i++)
  445. amdgpu_ib_free(parser->adev, &parser->ibs[i]);
  446. kfree(parser->ibs);
  447. amdgpu_bo_unref(&parser->uf.bo);
  448. amdgpu_bo_unref(&parser->uf_entry.robj);
  449. }
  450. static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p,
  451. struct amdgpu_vm *vm)
  452. {
  453. struct amdgpu_device *adev = p->adev;
  454. struct amdgpu_bo_va *bo_va;
  455. struct amdgpu_bo *bo;
  456. int i, r;
  457. r = amdgpu_vm_update_page_directory(adev, vm);
  458. if (r)
  459. return r;
  460. r = amdgpu_sync_fence(adev, &p->ibs[0].sync, vm->page_directory_fence);
  461. if (r)
  462. return r;
  463. r = amdgpu_vm_clear_freed(adev, vm);
  464. if (r)
  465. return r;
  466. if (p->bo_list) {
  467. for (i = 0; i < p->bo_list->num_entries; i++) {
  468. struct fence *f;
  469. /* ignore duplicates */
  470. bo = p->bo_list->array[i].robj;
  471. if (!bo)
  472. continue;
  473. bo_va = p->bo_list->array[i].bo_va;
  474. if (bo_va == NULL)
  475. continue;
  476. r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem);
  477. if (r)
  478. return r;
  479. f = bo_va->last_pt_update;
  480. r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f);
  481. if (r)
  482. return r;
  483. }
  484. }
  485. r = amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync);
  486. if (amdgpu_vm_debug && p->bo_list) {
  487. /* Invalidate all BOs to test for userspace bugs */
  488. for (i = 0; i < p->bo_list->num_entries; i++) {
  489. /* ignore duplicates */
  490. bo = p->bo_list->array[i].robj;
  491. if (!bo)
  492. continue;
  493. amdgpu_vm_bo_invalidate(adev, bo);
  494. }
  495. }
  496. return r;
  497. }
  498. static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
  499. struct amdgpu_cs_parser *parser)
  500. {
  501. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  502. struct amdgpu_vm *vm = &fpriv->vm;
  503. struct amdgpu_ring *ring;
  504. int i, r;
  505. if (parser->num_ibs == 0)
  506. return 0;
  507. /* Only for UVD/VCE VM emulation */
  508. for (i = 0; i < parser->num_ibs; i++) {
  509. ring = parser->ibs[i].ring;
  510. if (ring->funcs->parse_cs) {
  511. r = amdgpu_ring_parse_cs(ring, parser, i);
  512. if (r)
  513. return r;
  514. }
  515. }
  516. r = amdgpu_bo_vm_update_pte(parser, vm);
  517. if (!r)
  518. amdgpu_cs_sync_rings(parser);
  519. return r;
  520. }
  521. static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r)
  522. {
  523. if (r == -EDEADLK) {
  524. r = amdgpu_gpu_reset(adev);
  525. if (!r)
  526. r = -EAGAIN;
  527. }
  528. return r;
  529. }
  530. static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
  531. struct amdgpu_cs_parser *parser)
  532. {
  533. struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  534. struct amdgpu_vm *vm = &fpriv->vm;
  535. int i, j;
  536. int r;
  537. for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) {
  538. struct amdgpu_cs_chunk *chunk;
  539. struct amdgpu_ib *ib;
  540. struct drm_amdgpu_cs_chunk_ib *chunk_ib;
  541. struct amdgpu_ring *ring;
  542. chunk = &parser->chunks[i];
  543. ib = &parser->ibs[j];
  544. chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
  545. if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
  546. continue;
  547. r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
  548. chunk_ib->ip_instance, chunk_ib->ring,
  549. &ring);
  550. if (r)
  551. return r;
  552. if (ring->funcs->parse_cs) {
  553. struct amdgpu_bo_va_mapping *m;
  554. struct amdgpu_bo *aobj = NULL;
  555. uint64_t offset;
  556. uint8_t *kptr;
  557. m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
  558. &aobj);
  559. if (!aobj) {
  560. DRM_ERROR("IB va_start is invalid\n");
  561. return -EINVAL;
  562. }
  563. if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
  564. (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  565. DRM_ERROR("IB va_start+ib_bytes is invalid\n");
  566. return -EINVAL;
  567. }
  568. /* the IB should be reserved at this point */
  569. r = amdgpu_bo_kmap(aobj, (void **)&kptr);
  570. if (r) {
  571. return r;
  572. }
  573. offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE;
  574. kptr += chunk_ib->va_start - offset;
  575. r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib);
  576. if (r) {
  577. DRM_ERROR("Failed to get ib !\n");
  578. return r;
  579. }
  580. memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
  581. amdgpu_bo_kunmap(aobj);
  582. } else {
  583. r = amdgpu_ib_get(ring, vm, 0, ib);
  584. if (r) {
  585. DRM_ERROR("Failed to get ib !\n");
  586. return r;
  587. }
  588. ib->gpu_addr = chunk_ib->va_start;
  589. }
  590. ib->length_dw = chunk_ib->ib_bytes / 4;
  591. ib->flags = chunk_ib->flags;
  592. ib->ctx = parser->ctx;
  593. j++;
  594. }
  595. if (!parser->num_ibs)
  596. return 0;
  597. /* add GDS resources to first IB */
  598. if (parser->bo_list) {
  599. struct amdgpu_bo *gds = parser->bo_list->gds_obj;
  600. struct amdgpu_bo *gws = parser->bo_list->gws_obj;
  601. struct amdgpu_bo *oa = parser->bo_list->oa_obj;
  602. struct amdgpu_ib *ib = &parser->ibs[0];
  603. if (gds) {
  604. ib->gds_base = amdgpu_bo_gpu_offset(gds);
  605. ib->gds_size = amdgpu_bo_size(gds);
  606. }
  607. if (gws) {
  608. ib->gws_base = amdgpu_bo_gpu_offset(gws);
  609. ib->gws_size = amdgpu_bo_size(gws);
  610. }
  611. if (oa) {
  612. ib->oa_base = amdgpu_bo_gpu_offset(oa);
  613. ib->oa_size = amdgpu_bo_size(oa);
  614. }
  615. }
  616. /* wrap the last IB with user fence */
  617. if (parser->uf.bo) {
  618. struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1];
  619. /* UVD & VCE fw doesn't support user fences */
  620. if (ib->ring->type == AMDGPU_RING_TYPE_UVD ||
  621. ib->ring->type == AMDGPU_RING_TYPE_VCE)
  622. return -EINVAL;
  623. ib->user = &parser->uf;
  624. }
  625. return 0;
  626. }
  627. static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
  628. struct amdgpu_cs_parser *p)
  629. {
  630. struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  631. struct amdgpu_ib *ib;
  632. int i, j, r;
  633. if (!p->num_ibs)
  634. return 0;
  635. /* Add dependencies to first IB */
  636. ib = &p->ibs[0];
  637. for (i = 0; i < p->nchunks; ++i) {
  638. struct drm_amdgpu_cs_chunk_dep *deps;
  639. struct amdgpu_cs_chunk *chunk;
  640. unsigned num_deps;
  641. chunk = &p->chunks[i];
  642. if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
  643. continue;
  644. deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
  645. num_deps = chunk->length_dw * 4 /
  646. sizeof(struct drm_amdgpu_cs_chunk_dep);
  647. for (j = 0; j < num_deps; ++j) {
  648. struct amdgpu_ring *ring;
  649. struct amdgpu_ctx *ctx;
  650. struct fence *fence;
  651. r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
  652. deps[j].ip_instance,
  653. deps[j].ring, &ring);
  654. if (r)
  655. return r;
  656. ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
  657. if (ctx == NULL)
  658. return -EINVAL;
  659. fence = amdgpu_ctx_get_fence(ctx, ring,
  660. deps[j].handle);
  661. if (IS_ERR(fence)) {
  662. r = PTR_ERR(fence);
  663. amdgpu_ctx_put(ctx);
  664. return r;
  665. } else if (fence) {
  666. r = amdgpu_sync_fence(adev, &ib->sync, fence);
  667. fence_put(fence);
  668. amdgpu_ctx_put(ctx);
  669. if (r)
  670. return r;
  671. }
  672. }
  673. }
  674. return 0;
  675. }
  676. static int amdgpu_cs_free_job(struct amdgpu_job *job)
  677. {
  678. int i;
  679. if (job->ibs)
  680. for (i = 0; i < job->num_ibs; i++)
  681. amdgpu_ib_free(job->adev, &job->ibs[i]);
  682. kfree(job->ibs);
  683. if (job->uf.bo)
  684. amdgpu_bo_unref(&job->uf.bo);
  685. return 0;
  686. }
  687. int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  688. {
  689. struct amdgpu_device *adev = dev->dev_private;
  690. union drm_amdgpu_cs *cs = data;
  691. struct amdgpu_cs_parser parser = {};
  692. bool reserved_buffers = false;
  693. int i, r;
  694. if (!adev->accel_working)
  695. return -EBUSY;
  696. parser.adev = adev;
  697. parser.filp = filp;
  698. r = amdgpu_cs_parser_init(&parser, data);
  699. if (r) {
  700. DRM_ERROR("Failed to initialize parser !\n");
  701. amdgpu_cs_parser_fini(&parser, r, false);
  702. r = amdgpu_cs_handle_lockup(adev, r);
  703. return r;
  704. }
  705. r = amdgpu_cs_parser_relocs(&parser);
  706. if (r == -ENOMEM)
  707. DRM_ERROR("Not enough memory for command submission!\n");
  708. else if (r && r != -ERESTARTSYS)
  709. DRM_ERROR("Failed to process the buffer list %d!\n", r);
  710. else if (!r) {
  711. reserved_buffers = true;
  712. r = amdgpu_cs_ib_fill(adev, &parser);
  713. }
  714. if (!r) {
  715. r = amdgpu_cs_dependencies(adev, &parser);
  716. if (r)
  717. DRM_ERROR("Failed in the dependencies handling %d!\n", r);
  718. }
  719. if (r)
  720. goto out;
  721. for (i = 0; i < parser.num_ibs; i++)
  722. trace_amdgpu_cs(&parser, i);
  723. r = amdgpu_cs_ib_vm_chunk(adev, &parser);
  724. if (r)
  725. goto out;
  726. if (amdgpu_enable_scheduler && parser.num_ibs) {
  727. struct amdgpu_ring * ring = parser.ibs->ring;
  728. struct amd_sched_fence *fence;
  729. struct amdgpu_job *job;
  730. job = kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL);
  731. if (!job) {
  732. r = -ENOMEM;
  733. goto out;
  734. }
  735. job->base.sched = &ring->sched;
  736. job->base.s_entity = &parser.ctx->rings[ring->idx].entity;
  737. job->adev = parser.adev;
  738. job->owner = parser.filp;
  739. job->free_job = amdgpu_cs_free_job;
  740. job->ibs = parser.ibs;
  741. job->num_ibs = parser.num_ibs;
  742. parser.ibs = NULL;
  743. parser.num_ibs = 0;
  744. if (job->ibs[job->num_ibs - 1].user) {
  745. job->uf = parser.uf;
  746. job->ibs[job->num_ibs - 1].user = &job->uf;
  747. parser.uf.bo = NULL;
  748. }
  749. fence = amd_sched_fence_create(job->base.s_entity,
  750. parser.filp);
  751. if (!fence) {
  752. r = -ENOMEM;
  753. amdgpu_cs_free_job(job);
  754. kfree(job);
  755. goto out;
  756. }
  757. job->base.s_fence = fence;
  758. parser.fence = fence_get(&fence->base);
  759. cs->out.handle = amdgpu_ctx_add_fence(parser.ctx, ring,
  760. &fence->base);
  761. job->ibs[job->num_ibs - 1].sequence = cs->out.handle;
  762. trace_amdgpu_cs_ioctl(job);
  763. amd_sched_entity_push_job(&job->base);
  764. } else {
  765. struct amdgpu_fence *fence;
  766. r = amdgpu_ib_schedule(adev, parser.num_ibs, parser.ibs,
  767. parser.filp);
  768. fence = parser.ibs[parser.num_ibs - 1].fence;
  769. parser.fence = fence_get(&fence->base);
  770. cs->out.handle = parser.ibs[parser.num_ibs - 1].sequence;
  771. }
  772. out:
  773. amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
  774. r = amdgpu_cs_handle_lockup(adev, r);
  775. return r;
  776. }
  777. /**
  778. * amdgpu_cs_wait_ioctl - wait for a command submission to finish
  779. *
  780. * @dev: drm device
  781. * @data: data from userspace
  782. * @filp: file private
  783. *
  784. * Wait for the command submission identified by handle to finish.
  785. */
  786. int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
  787. struct drm_file *filp)
  788. {
  789. union drm_amdgpu_wait_cs *wait = data;
  790. struct amdgpu_device *adev = dev->dev_private;
  791. unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
  792. struct amdgpu_ring *ring = NULL;
  793. struct amdgpu_ctx *ctx;
  794. struct fence *fence;
  795. long r;
  796. r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
  797. wait->in.ring, &ring);
  798. if (r)
  799. return r;
  800. ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
  801. if (ctx == NULL)
  802. return -EINVAL;
  803. fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
  804. if (IS_ERR(fence))
  805. r = PTR_ERR(fence);
  806. else if (fence) {
  807. r = fence_wait_timeout(fence, true, timeout);
  808. fence_put(fence);
  809. } else
  810. r = 1;
  811. amdgpu_ctx_put(ctx);
  812. if (r < 0)
  813. return r;
  814. memset(wait, 0, sizeof(*wait));
  815. wait->out.status = (r == 0);
  816. return 0;
  817. }
  818. /**
  819. * amdgpu_cs_find_bo_va - find bo_va for VM address
  820. *
  821. * @parser: command submission parser context
  822. * @addr: VM address
  823. * @bo: resulting BO of the mapping found
  824. *
  825. * Search the buffer objects in the command submission context for a certain
  826. * virtual memory address. Returns allocation structure when found, NULL
  827. * otherwise.
  828. */
  829. struct amdgpu_bo_va_mapping *
  830. amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
  831. uint64_t addr, struct amdgpu_bo **bo)
  832. {
  833. struct amdgpu_bo_list_entry *reloc;
  834. struct amdgpu_bo_va_mapping *mapping;
  835. addr /= AMDGPU_GPU_PAGE_SIZE;
  836. list_for_each_entry(reloc, &parser->validated, tv.head) {
  837. if (!reloc->bo_va)
  838. continue;
  839. list_for_each_entry(mapping, &reloc->bo_va->valids, list) {
  840. if (mapping->it.start > addr ||
  841. addr > mapping->it.last)
  842. continue;
  843. *bo = reloc->bo_va->bo;
  844. return mapping;
  845. }
  846. list_for_each_entry(mapping, &reloc->bo_va->invalids, list) {
  847. if (mapping->it.start > addr ||
  848. addr > mapping->it.last)
  849. continue;
  850. *bo = reloc->bo_va->bo;
  851. return mapping;
  852. }
  853. }
  854. return NULL;
  855. }