amdgpu_cs.c 25 KB

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