amdgpu_cs.c 26 KB

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