amdgpu_cs.c 25 KB

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