amdgpu_cs.c 25 KB

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