amdgpu_cs.c 25 KB

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